intel_display.c 447 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878
  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);
  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. {
  5266. /*
  5267. * Reduce M/N as much as possible without loss in precision. Several DP
  5268. * dongles in particular seem to be fussy about too large *link* M/N
  5269. * values. The passed in values are more likely to have the least
  5270. * significant bits zero than M after rounding below, so do this first.
  5271. */
  5272. while ((m & 1) == 0 && (n & 1) == 0) {
  5273. m >>= 1;
  5274. n >>= 1;
  5275. }
  5276. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5277. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5278. intel_reduce_m_n_ratio(ret_m, ret_n);
  5279. }
  5280. void
  5281. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5282. int pixel_clock, int link_clock,
  5283. struct intel_link_m_n *m_n)
  5284. {
  5285. m_n->tu = 64;
  5286. compute_m_n(bits_per_pixel * pixel_clock,
  5287. link_clock * nlanes * 8,
  5288. &m_n->gmch_m, &m_n->gmch_n);
  5289. compute_m_n(pixel_clock, link_clock,
  5290. &m_n->link_m, &m_n->link_n);
  5291. }
  5292. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5293. {
  5294. if (i915.panel_use_ssc >= 0)
  5295. return i915.panel_use_ssc != 0;
  5296. return dev_priv->vbt.lvds_use_ssc
  5297. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5298. }
  5299. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5300. {
  5301. return (1 << dpll->n) << 16 | dpll->m2;
  5302. }
  5303. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5304. {
  5305. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5306. }
  5307. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5308. struct intel_crtc_state *crtc_state,
  5309. struct dpll *reduced_clock)
  5310. {
  5311. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5312. u32 fp, fp2 = 0;
  5313. if (IS_PINEVIEW(dev_priv)) {
  5314. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5315. if (reduced_clock)
  5316. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5317. } else {
  5318. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5319. if (reduced_clock)
  5320. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5321. }
  5322. crtc_state->dpll_hw_state.fp0 = fp;
  5323. crtc->lowfreq_avail = false;
  5324. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5325. reduced_clock) {
  5326. crtc_state->dpll_hw_state.fp1 = fp2;
  5327. crtc->lowfreq_avail = true;
  5328. } else {
  5329. crtc_state->dpll_hw_state.fp1 = fp;
  5330. }
  5331. }
  5332. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5333. pipe)
  5334. {
  5335. u32 reg_val;
  5336. /*
  5337. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5338. * and set it to a reasonable value instead.
  5339. */
  5340. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5341. reg_val &= 0xffffff00;
  5342. reg_val |= 0x00000030;
  5343. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5344. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5345. reg_val &= 0x8cffffff;
  5346. reg_val = 0x8c000000;
  5347. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5348. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5349. reg_val &= 0xffffff00;
  5350. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5351. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5352. reg_val &= 0x00ffffff;
  5353. reg_val |= 0xb0000000;
  5354. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5355. }
  5356. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5357. struct intel_link_m_n *m_n)
  5358. {
  5359. struct drm_device *dev = crtc->base.dev;
  5360. struct drm_i915_private *dev_priv = to_i915(dev);
  5361. int pipe = crtc->pipe;
  5362. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5363. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5364. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5365. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5366. }
  5367. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5368. struct intel_link_m_n *m_n,
  5369. struct intel_link_m_n *m2_n2)
  5370. {
  5371. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5372. int pipe = crtc->pipe;
  5373. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5374. if (INTEL_GEN(dev_priv) >= 5) {
  5375. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5376. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5377. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5378. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5379. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5380. * for gen < 8) and if DRRS is supported (to make sure the
  5381. * registers are not unnecessarily accessed).
  5382. */
  5383. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5384. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5385. I915_WRITE(PIPE_DATA_M2(transcoder),
  5386. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5387. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5388. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5389. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5390. }
  5391. } else {
  5392. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5393. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5394. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5395. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5396. }
  5397. }
  5398. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5399. {
  5400. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5401. if (m_n == M1_N1) {
  5402. dp_m_n = &crtc->config->dp_m_n;
  5403. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5404. } else if (m_n == M2_N2) {
  5405. /*
  5406. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5407. * needs to be programmed into M1_N1.
  5408. */
  5409. dp_m_n = &crtc->config->dp_m2_n2;
  5410. } else {
  5411. DRM_ERROR("Unsupported divider value\n");
  5412. return;
  5413. }
  5414. if (crtc->config->has_pch_encoder)
  5415. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5416. else
  5417. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5418. }
  5419. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5420. struct intel_crtc_state *pipe_config)
  5421. {
  5422. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5423. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5424. if (crtc->pipe != PIPE_A)
  5425. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5426. /* DPLL not used with DSI, but still need the rest set up */
  5427. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5428. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5429. DPLL_EXT_BUFFER_ENABLE_VLV;
  5430. pipe_config->dpll_hw_state.dpll_md =
  5431. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5432. }
  5433. static void chv_compute_dpll(struct intel_crtc *crtc,
  5434. struct intel_crtc_state *pipe_config)
  5435. {
  5436. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5437. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5438. if (crtc->pipe != PIPE_A)
  5439. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5440. /* DPLL not used with DSI, but still need the rest set up */
  5441. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5442. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5443. pipe_config->dpll_hw_state.dpll_md =
  5444. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5445. }
  5446. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5447. const struct intel_crtc_state *pipe_config)
  5448. {
  5449. struct drm_device *dev = crtc->base.dev;
  5450. struct drm_i915_private *dev_priv = to_i915(dev);
  5451. enum pipe pipe = crtc->pipe;
  5452. u32 mdiv;
  5453. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5454. u32 coreclk, reg_val;
  5455. /* Enable Refclk */
  5456. I915_WRITE(DPLL(pipe),
  5457. pipe_config->dpll_hw_state.dpll &
  5458. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5459. /* No need to actually set up the DPLL with DSI */
  5460. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5461. return;
  5462. mutex_lock(&dev_priv->sb_lock);
  5463. bestn = pipe_config->dpll.n;
  5464. bestm1 = pipe_config->dpll.m1;
  5465. bestm2 = pipe_config->dpll.m2;
  5466. bestp1 = pipe_config->dpll.p1;
  5467. bestp2 = pipe_config->dpll.p2;
  5468. /* See eDP HDMI DPIO driver vbios notes doc */
  5469. /* PLL B needs special handling */
  5470. if (pipe == PIPE_B)
  5471. vlv_pllb_recal_opamp(dev_priv, pipe);
  5472. /* Set up Tx target for periodic Rcomp update */
  5473. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5474. /* Disable target IRef on PLL */
  5475. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5476. reg_val &= 0x00ffffff;
  5477. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5478. /* Disable fast lock */
  5479. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5480. /* Set idtafcrecal before PLL is enabled */
  5481. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5482. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5483. mdiv |= ((bestn << DPIO_N_SHIFT));
  5484. mdiv |= (1 << DPIO_K_SHIFT);
  5485. /*
  5486. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5487. * but we don't support that).
  5488. * Note: don't use the DAC post divider as it seems unstable.
  5489. */
  5490. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5491. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5492. mdiv |= DPIO_ENABLE_CALIBRATION;
  5493. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5494. /* Set HBR and RBR LPF coefficients */
  5495. if (pipe_config->port_clock == 162000 ||
  5496. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5497. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5498. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5499. 0x009f0003);
  5500. else
  5501. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5502. 0x00d0000f);
  5503. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5504. /* Use SSC source */
  5505. if (pipe == PIPE_A)
  5506. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5507. 0x0df40000);
  5508. else
  5509. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5510. 0x0df70000);
  5511. } else { /* HDMI or VGA */
  5512. /* Use bend source */
  5513. if (pipe == PIPE_A)
  5514. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5515. 0x0df70000);
  5516. else
  5517. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5518. 0x0df40000);
  5519. }
  5520. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5521. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5522. if (intel_crtc_has_dp_encoder(crtc->config))
  5523. coreclk |= 0x01000000;
  5524. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5525. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5526. mutex_unlock(&dev_priv->sb_lock);
  5527. }
  5528. static void chv_prepare_pll(struct intel_crtc *crtc,
  5529. const struct intel_crtc_state *pipe_config)
  5530. {
  5531. struct drm_device *dev = crtc->base.dev;
  5532. struct drm_i915_private *dev_priv = to_i915(dev);
  5533. enum pipe pipe = crtc->pipe;
  5534. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5535. u32 loopfilter, tribuf_calcntr;
  5536. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5537. u32 dpio_val;
  5538. int vco;
  5539. /* Enable Refclk and SSC */
  5540. I915_WRITE(DPLL(pipe),
  5541. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5542. /* No need to actually set up the DPLL with DSI */
  5543. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5544. return;
  5545. bestn = pipe_config->dpll.n;
  5546. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5547. bestm1 = pipe_config->dpll.m1;
  5548. bestm2 = pipe_config->dpll.m2 >> 22;
  5549. bestp1 = pipe_config->dpll.p1;
  5550. bestp2 = pipe_config->dpll.p2;
  5551. vco = pipe_config->dpll.vco;
  5552. dpio_val = 0;
  5553. loopfilter = 0;
  5554. mutex_lock(&dev_priv->sb_lock);
  5555. /* p1 and p2 divider */
  5556. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5557. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5558. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5559. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5560. 1 << DPIO_CHV_K_DIV_SHIFT);
  5561. /* Feedback post-divider - m2 */
  5562. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5563. /* Feedback refclk divider - n and m1 */
  5564. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5565. DPIO_CHV_M1_DIV_BY_2 |
  5566. 1 << DPIO_CHV_N_DIV_SHIFT);
  5567. /* M2 fraction division */
  5568. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5569. /* M2 fraction division enable */
  5570. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5571. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5572. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5573. if (bestm2_frac)
  5574. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5575. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5576. /* Program digital lock detect threshold */
  5577. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5578. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5579. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5580. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5581. if (!bestm2_frac)
  5582. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5583. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5584. /* Loop filter */
  5585. if (vco == 5400000) {
  5586. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5587. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5588. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5589. tribuf_calcntr = 0x9;
  5590. } else if (vco <= 6200000) {
  5591. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5592. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5593. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5594. tribuf_calcntr = 0x9;
  5595. } else if (vco <= 6480000) {
  5596. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5597. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5598. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5599. tribuf_calcntr = 0x8;
  5600. } else {
  5601. /* Not supported. Apply the same limits as in the max case */
  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 = 0;
  5606. }
  5607. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5608. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5609. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5610. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5611. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5612. /* AFC Recal */
  5613. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5614. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5615. DPIO_AFC_RECAL);
  5616. mutex_unlock(&dev_priv->sb_lock);
  5617. }
  5618. /**
  5619. * vlv_force_pll_on - forcibly enable just the PLL
  5620. * @dev_priv: i915 private structure
  5621. * @pipe: pipe PLL to enable
  5622. * @dpll: PLL configuration
  5623. *
  5624. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5625. * in cases where we need the PLL enabled even when @pipe is not going to
  5626. * be enabled.
  5627. */
  5628. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5629. const struct dpll *dpll)
  5630. {
  5631. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5632. struct intel_crtc_state *pipe_config;
  5633. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5634. if (!pipe_config)
  5635. return -ENOMEM;
  5636. pipe_config->base.crtc = &crtc->base;
  5637. pipe_config->pixel_multiplier = 1;
  5638. pipe_config->dpll = *dpll;
  5639. if (IS_CHERRYVIEW(dev_priv)) {
  5640. chv_compute_dpll(crtc, pipe_config);
  5641. chv_prepare_pll(crtc, pipe_config);
  5642. chv_enable_pll(crtc, pipe_config);
  5643. } else {
  5644. vlv_compute_dpll(crtc, pipe_config);
  5645. vlv_prepare_pll(crtc, pipe_config);
  5646. vlv_enable_pll(crtc, pipe_config);
  5647. }
  5648. kfree(pipe_config);
  5649. return 0;
  5650. }
  5651. /**
  5652. * vlv_force_pll_off - forcibly disable just the PLL
  5653. * @dev_priv: i915 private structure
  5654. * @pipe: pipe PLL to disable
  5655. *
  5656. * Disable the PLL for @pipe. To be used in cases where we need
  5657. * the PLL enabled even when @pipe is not going to be enabled.
  5658. */
  5659. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5660. {
  5661. if (IS_CHERRYVIEW(dev_priv))
  5662. chv_disable_pll(dev_priv, pipe);
  5663. else
  5664. vlv_disable_pll(dev_priv, pipe);
  5665. }
  5666. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5667. struct intel_crtc_state *crtc_state,
  5668. struct dpll *reduced_clock)
  5669. {
  5670. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5671. u32 dpll;
  5672. struct dpll *clock = &crtc_state->dpll;
  5673. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5674. dpll = DPLL_VGA_MODE_DIS;
  5675. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5676. dpll |= DPLLB_MODE_LVDS;
  5677. else
  5678. dpll |= DPLLB_MODE_DAC_SERIAL;
  5679. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5680. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5681. dpll |= (crtc_state->pixel_multiplier - 1)
  5682. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5683. }
  5684. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5685. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5686. dpll |= DPLL_SDVO_HIGH_SPEED;
  5687. if (intel_crtc_has_dp_encoder(crtc_state))
  5688. dpll |= DPLL_SDVO_HIGH_SPEED;
  5689. /* compute bitmask from p1 value */
  5690. if (IS_PINEVIEW(dev_priv))
  5691. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5692. else {
  5693. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5694. if (IS_G4X(dev_priv) && reduced_clock)
  5695. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5696. }
  5697. switch (clock->p2) {
  5698. case 5:
  5699. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5700. break;
  5701. case 7:
  5702. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5703. break;
  5704. case 10:
  5705. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5706. break;
  5707. case 14:
  5708. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5709. break;
  5710. }
  5711. if (INTEL_GEN(dev_priv) >= 4)
  5712. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5713. if (crtc_state->sdvo_tv_clock)
  5714. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5715. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5716. intel_panel_use_ssc(dev_priv))
  5717. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5718. else
  5719. dpll |= PLL_REF_INPUT_DREFCLK;
  5720. dpll |= DPLL_VCO_ENABLE;
  5721. crtc_state->dpll_hw_state.dpll = dpll;
  5722. if (INTEL_GEN(dev_priv) >= 4) {
  5723. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5724. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5725. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5726. }
  5727. }
  5728. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5729. struct intel_crtc_state *crtc_state,
  5730. struct dpll *reduced_clock)
  5731. {
  5732. struct drm_device *dev = crtc->base.dev;
  5733. struct drm_i915_private *dev_priv = to_i915(dev);
  5734. u32 dpll;
  5735. struct dpll *clock = &crtc_state->dpll;
  5736. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5737. dpll = DPLL_VGA_MODE_DIS;
  5738. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5739. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5740. } else {
  5741. if (clock->p1 == 2)
  5742. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5743. else
  5744. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5745. if (clock->p2 == 4)
  5746. dpll |= PLL_P2_DIVIDE_BY_4;
  5747. }
  5748. if (!IS_I830(dev_priv) &&
  5749. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5750. dpll |= DPLL_DVO_2X_MODE;
  5751. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5752. intel_panel_use_ssc(dev_priv))
  5753. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5754. else
  5755. dpll |= PLL_REF_INPUT_DREFCLK;
  5756. dpll |= DPLL_VCO_ENABLE;
  5757. crtc_state->dpll_hw_state.dpll = dpll;
  5758. }
  5759. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5760. {
  5761. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5762. enum pipe pipe = intel_crtc->pipe;
  5763. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5764. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5765. uint32_t crtc_vtotal, crtc_vblank_end;
  5766. int vsyncshift = 0;
  5767. /* We need to be careful not to changed the adjusted mode, for otherwise
  5768. * the hw state checker will get angry at the mismatch. */
  5769. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5770. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5771. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5772. /* the chip adds 2 halflines automatically */
  5773. crtc_vtotal -= 1;
  5774. crtc_vblank_end -= 1;
  5775. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5776. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5777. else
  5778. vsyncshift = adjusted_mode->crtc_hsync_start -
  5779. adjusted_mode->crtc_htotal / 2;
  5780. if (vsyncshift < 0)
  5781. vsyncshift += adjusted_mode->crtc_htotal;
  5782. }
  5783. if (INTEL_GEN(dev_priv) > 3)
  5784. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5785. I915_WRITE(HTOTAL(cpu_transcoder),
  5786. (adjusted_mode->crtc_hdisplay - 1) |
  5787. ((adjusted_mode->crtc_htotal - 1) << 16));
  5788. I915_WRITE(HBLANK(cpu_transcoder),
  5789. (adjusted_mode->crtc_hblank_start - 1) |
  5790. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5791. I915_WRITE(HSYNC(cpu_transcoder),
  5792. (adjusted_mode->crtc_hsync_start - 1) |
  5793. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5794. I915_WRITE(VTOTAL(cpu_transcoder),
  5795. (adjusted_mode->crtc_vdisplay - 1) |
  5796. ((crtc_vtotal - 1) << 16));
  5797. I915_WRITE(VBLANK(cpu_transcoder),
  5798. (adjusted_mode->crtc_vblank_start - 1) |
  5799. ((crtc_vblank_end - 1) << 16));
  5800. I915_WRITE(VSYNC(cpu_transcoder),
  5801. (adjusted_mode->crtc_vsync_start - 1) |
  5802. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5803. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5804. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5805. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5806. * bits. */
  5807. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  5808. (pipe == PIPE_B || pipe == PIPE_C))
  5809. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5810. }
  5811. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  5812. {
  5813. struct drm_device *dev = intel_crtc->base.dev;
  5814. struct drm_i915_private *dev_priv = to_i915(dev);
  5815. enum pipe pipe = intel_crtc->pipe;
  5816. /* pipesrc controls the size that is scaled from, which should
  5817. * always be the user's requested size.
  5818. */
  5819. I915_WRITE(PIPESRC(pipe),
  5820. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5821. (intel_crtc->config->pipe_src_h - 1));
  5822. }
  5823. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5824. struct intel_crtc_state *pipe_config)
  5825. {
  5826. struct drm_device *dev = crtc->base.dev;
  5827. struct drm_i915_private *dev_priv = to_i915(dev);
  5828. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5829. uint32_t tmp;
  5830. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5831. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5832. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5833. tmp = I915_READ(HBLANK(cpu_transcoder));
  5834. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5835. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5836. tmp = I915_READ(HSYNC(cpu_transcoder));
  5837. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5838. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5839. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5840. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5841. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5842. tmp = I915_READ(VBLANK(cpu_transcoder));
  5843. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5844. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5845. tmp = I915_READ(VSYNC(cpu_transcoder));
  5846. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5847. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5848. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5849. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5850. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5851. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5852. }
  5853. }
  5854. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  5855. struct intel_crtc_state *pipe_config)
  5856. {
  5857. struct drm_device *dev = crtc->base.dev;
  5858. struct drm_i915_private *dev_priv = to_i915(dev);
  5859. u32 tmp;
  5860. tmp = I915_READ(PIPESRC(crtc->pipe));
  5861. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5862. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5863. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5864. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5865. }
  5866. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5867. struct intel_crtc_state *pipe_config)
  5868. {
  5869. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5870. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5871. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5872. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5873. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5874. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5875. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5876. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5877. mode->flags = pipe_config->base.adjusted_mode.flags;
  5878. mode->type = DRM_MODE_TYPE_DRIVER;
  5879. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5880. mode->hsync = drm_mode_hsync(mode);
  5881. mode->vrefresh = drm_mode_vrefresh(mode);
  5882. drm_mode_set_name(mode);
  5883. }
  5884. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5885. {
  5886. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5887. uint32_t pipeconf;
  5888. pipeconf = 0;
  5889. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5890. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5891. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5892. if (intel_crtc->config->double_wide)
  5893. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5894. /* only g4x and later have fancy bpc/dither controls */
  5895. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  5896. IS_CHERRYVIEW(dev_priv)) {
  5897. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5898. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5899. pipeconf |= PIPECONF_DITHER_EN |
  5900. PIPECONF_DITHER_TYPE_SP;
  5901. switch (intel_crtc->config->pipe_bpp) {
  5902. case 18:
  5903. pipeconf |= PIPECONF_6BPC;
  5904. break;
  5905. case 24:
  5906. pipeconf |= PIPECONF_8BPC;
  5907. break;
  5908. case 30:
  5909. pipeconf |= PIPECONF_10BPC;
  5910. break;
  5911. default:
  5912. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5913. BUG();
  5914. }
  5915. }
  5916. if (HAS_PIPE_CXSR(dev_priv)) {
  5917. if (intel_crtc->lowfreq_avail) {
  5918. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5919. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5920. } else {
  5921. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5922. }
  5923. }
  5924. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5925. if (INTEL_GEN(dev_priv) < 4 ||
  5926. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5927. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5928. else
  5929. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5930. } else
  5931. pipeconf |= PIPECONF_PROGRESSIVE;
  5932. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  5933. intel_crtc->config->limited_color_range)
  5934. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5935. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5936. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5937. }
  5938. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  5939. struct intel_crtc_state *crtc_state)
  5940. {
  5941. struct drm_device *dev = crtc->base.dev;
  5942. struct drm_i915_private *dev_priv = to_i915(dev);
  5943. const struct intel_limit *limit;
  5944. int refclk = 48000;
  5945. memset(&crtc_state->dpll_hw_state, 0,
  5946. sizeof(crtc_state->dpll_hw_state));
  5947. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5948. if (intel_panel_use_ssc(dev_priv)) {
  5949. refclk = dev_priv->vbt.lvds_ssc_freq;
  5950. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5951. }
  5952. limit = &intel_limits_i8xx_lvds;
  5953. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  5954. limit = &intel_limits_i8xx_dvo;
  5955. } else {
  5956. limit = &intel_limits_i8xx_dac;
  5957. }
  5958. if (!crtc_state->clock_set &&
  5959. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  5960. refclk, NULL, &crtc_state->dpll)) {
  5961. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5962. return -EINVAL;
  5963. }
  5964. i8xx_compute_dpll(crtc, crtc_state, NULL);
  5965. return 0;
  5966. }
  5967. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  5968. struct intel_crtc_state *crtc_state)
  5969. {
  5970. struct drm_device *dev = crtc->base.dev;
  5971. struct drm_i915_private *dev_priv = to_i915(dev);
  5972. const struct intel_limit *limit;
  5973. int refclk = 96000;
  5974. memset(&crtc_state->dpll_hw_state, 0,
  5975. sizeof(crtc_state->dpll_hw_state));
  5976. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5977. if (intel_panel_use_ssc(dev_priv)) {
  5978. refclk = dev_priv->vbt.lvds_ssc_freq;
  5979. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5980. }
  5981. if (intel_is_dual_link_lvds(dev))
  5982. limit = &intel_limits_g4x_dual_channel_lvds;
  5983. else
  5984. limit = &intel_limits_g4x_single_channel_lvds;
  5985. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  5986. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  5987. limit = &intel_limits_g4x_hdmi;
  5988. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  5989. limit = &intel_limits_g4x_sdvo;
  5990. } else {
  5991. /* The option is for other outputs */
  5992. limit = &intel_limits_i9xx_sdvo;
  5993. }
  5994. if (!crtc_state->clock_set &&
  5995. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  5996. refclk, NULL, &crtc_state->dpll)) {
  5997. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5998. return -EINVAL;
  5999. }
  6000. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6001. return 0;
  6002. }
  6003. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6004. struct intel_crtc_state *crtc_state)
  6005. {
  6006. struct drm_device *dev = crtc->base.dev;
  6007. struct drm_i915_private *dev_priv = to_i915(dev);
  6008. const struct intel_limit *limit;
  6009. int refclk = 96000;
  6010. memset(&crtc_state->dpll_hw_state, 0,
  6011. sizeof(crtc_state->dpll_hw_state));
  6012. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6013. if (intel_panel_use_ssc(dev_priv)) {
  6014. refclk = dev_priv->vbt.lvds_ssc_freq;
  6015. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6016. }
  6017. limit = &intel_limits_pineview_lvds;
  6018. } else {
  6019. limit = &intel_limits_pineview_sdvo;
  6020. }
  6021. if (!crtc_state->clock_set &&
  6022. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6023. refclk, NULL, &crtc_state->dpll)) {
  6024. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6025. return -EINVAL;
  6026. }
  6027. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6028. return 0;
  6029. }
  6030. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6031. struct intel_crtc_state *crtc_state)
  6032. {
  6033. struct drm_device *dev = crtc->base.dev;
  6034. struct drm_i915_private *dev_priv = to_i915(dev);
  6035. const struct intel_limit *limit;
  6036. int refclk = 96000;
  6037. memset(&crtc_state->dpll_hw_state, 0,
  6038. sizeof(crtc_state->dpll_hw_state));
  6039. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6040. if (intel_panel_use_ssc(dev_priv)) {
  6041. refclk = dev_priv->vbt.lvds_ssc_freq;
  6042. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6043. }
  6044. limit = &intel_limits_i9xx_lvds;
  6045. } else {
  6046. limit = &intel_limits_i9xx_sdvo;
  6047. }
  6048. if (!crtc_state->clock_set &&
  6049. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6050. refclk, NULL, &crtc_state->dpll)) {
  6051. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6052. return -EINVAL;
  6053. }
  6054. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6055. return 0;
  6056. }
  6057. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6058. struct intel_crtc_state *crtc_state)
  6059. {
  6060. int refclk = 100000;
  6061. const struct intel_limit *limit = &intel_limits_chv;
  6062. memset(&crtc_state->dpll_hw_state, 0,
  6063. sizeof(crtc_state->dpll_hw_state));
  6064. if (!crtc_state->clock_set &&
  6065. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6066. refclk, NULL, &crtc_state->dpll)) {
  6067. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6068. return -EINVAL;
  6069. }
  6070. chv_compute_dpll(crtc, crtc_state);
  6071. return 0;
  6072. }
  6073. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6074. struct intel_crtc_state *crtc_state)
  6075. {
  6076. int refclk = 100000;
  6077. const struct intel_limit *limit = &intel_limits_vlv;
  6078. memset(&crtc_state->dpll_hw_state, 0,
  6079. sizeof(crtc_state->dpll_hw_state));
  6080. if (!crtc_state->clock_set &&
  6081. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6082. refclk, NULL, &crtc_state->dpll)) {
  6083. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6084. return -EINVAL;
  6085. }
  6086. vlv_compute_dpll(crtc, crtc_state);
  6087. return 0;
  6088. }
  6089. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6090. struct intel_crtc_state *pipe_config)
  6091. {
  6092. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6093. uint32_t tmp;
  6094. if (INTEL_GEN(dev_priv) <= 3 &&
  6095. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6096. return;
  6097. tmp = I915_READ(PFIT_CONTROL);
  6098. if (!(tmp & PFIT_ENABLE))
  6099. return;
  6100. /* Check whether the pfit is attached to our pipe. */
  6101. if (INTEL_GEN(dev_priv) < 4) {
  6102. if (crtc->pipe != PIPE_B)
  6103. return;
  6104. } else {
  6105. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6106. return;
  6107. }
  6108. pipe_config->gmch_pfit.control = tmp;
  6109. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6110. }
  6111. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6112. struct intel_crtc_state *pipe_config)
  6113. {
  6114. struct drm_device *dev = crtc->base.dev;
  6115. struct drm_i915_private *dev_priv = to_i915(dev);
  6116. int pipe = pipe_config->cpu_transcoder;
  6117. struct dpll clock;
  6118. u32 mdiv;
  6119. int refclk = 100000;
  6120. /* In case of DSI, DPLL will not be used */
  6121. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6122. return;
  6123. mutex_lock(&dev_priv->sb_lock);
  6124. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6125. mutex_unlock(&dev_priv->sb_lock);
  6126. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6127. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6128. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6129. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6130. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6131. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6132. }
  6133. static void
  6134. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6135. struct intel_initial_plane_config *plane_config)
  6136. {
  6137. struct drm_device *dev = crtc->base.dev;
  6138. struct drm_i915_private *dev_priv = to_i915(dev);
  6139. u32 val, base, offset;
  6140. int pipe = crtc->pipe, plane = crtc->plane;
  6141. int fourcc, pixel_format;
  6142. unsigned int aligned_height;
  6143. struct drm_framebuffer *fb;
  6144. struct intel_framebuffer *intel_fb;
  6145. val = I915_READ(DSPCNTR(plane));
  6146. if (!(val & DISPLAY_PLANE_ENABLE))
  6147. return;
  6148. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6149. if (!intel_fb) {
  6150. DRM_DEBUG_KMS("failed to alloc fb\n");
  6151. return;
  6152. }
  6153. fb = &intel_fb->base;
  6154. fb->dev = dev;
  6155. if (INTEL_GEN(dev_priv) >= 4) {
  6156. if (val & DISPPLANE_TILED) {
  6157. plane_config->tiling = I915_TILING_X;
  6158. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6159. }
  6160. }
  6161. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6162. fourcc = i9xx_format_to_fourcc(pixel_format);
  6163. fb->format = drm_format_info(fourcc);
  6164. if (INTEL_GEN(dev_priv) >= 4) {
  6165. if (plane_config->tiling)
  6166. offset = I915_READ(DSPTILEOFF(plane));
  6167. else
  6168. offset = I915_READ(DSPLINOFF(plane));
  6169. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6170. } else {
  6171. base = I915_READ(DSPADDR(plane));
  6172. }
  6173. plane_config->base = base;
  6174. val = I915_READ(PIPESRC(pipe));
  6175. fb->width = ((val >> 16) & 0xfff) + 1;
  6176. fb->height = ((val >> 0) & 0xfff) + 1;
  6177. val = I915_READ(DSPSTRIDE(pipe));
  6178. fb->pitches[0] = val & 0xffffffc0;
  6179. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6180. plane_config->size = fb->pitches[0] * aligned_height;
  6181. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6182. pipe_name(pipe), plane, fb->width, fb->height,
  6183. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6184. plane_config->size);
  6185. plane_config->fb = intel_fb;
  6186. }
  6187. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6188. struct intel_crtc_state *pipe_config)
  6189. {
  6190. struct drm_device *dev = crtc->base.dev;
  6191. struct drm_i915_private *dev_priv = to_i915(dev);
  6192. int pipe = pipe_config->cpu_transcoder;
  6193. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6194. struct dpll clock;
  6195. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6196. int refclk = 100000;
  6197. /* In case of DSI, DPLL will not be used */
  6198. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6199. return;
  6200. mutex_lock(&dev_priv->sb_lock);
  6201. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6202. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6203. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6204. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6205. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6206. mutex_unlock(&dev_priv->sb_lock);
  6207. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6208. clock.m2 = (pll_dw0 & 0xff) << 22;
  6209. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6210. clock.m2 |= pll_dw2 & 0x3fffff;
  6211. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6212. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6213. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6214. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6215. }
  6216. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6217. struct intel_crtc_state *pipe_config)
  6218. {
  6219. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6220. enum intel_display_power_domain power_domain;
  6221. uint32_t tmp;
  6222. bool ret;
  6223. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6224. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6225. return false;
  6226. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6227. pipe_config->shared_dpll = NULL;
  6228. ret = false;
  6229. tmp = I915_READ(PIPECONF(crtc->pipe));
  6230. if (!(tmp & PIPECONF_ENABLE))
  6231. goto out;
  6232. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6233. IS_CHERRYVIEW(dev_priv)) {
  6234. switch (tmp & PIPECONF_BPC_MASK) {
  6235. case PIPECONF_6BPC:
  6236. pipe_config->pipe_bpp = 18;
  6237. break;
  6238. case PIPECONF_8BPC:
  6239. pipe_config->pipe_bpp = 24;
  6240. break;
  6241. case PIPECONF_10BPC:
  6242. pipe_config->pipe_bpp = 30;
  6243. break;
  6244. default:
  6245. break;
  6246. }
  6247. }
  6248. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6249. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6250. pipe_config->limited_color_range = true;
  6251. if (INTEL_GEN(dev_priv) < 4)
  6252. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6253. intel_get_pipe_timings(crtc, pipe_config);
  6254. intel_get_pipe_src_size(crtc, pipe_config);
  6255. i9xx_get_pfit_config(crtc, pipe_config);
  6256. if (INTEL_GEN(dev_priv) >= 4) {
  6257. /* No way to read it out on pipes B and C */
  6258. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6259. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6260. else
  6261. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6262. pipe_config->pixel_multiplier =
  6263. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6264. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6265. pipe_config->dpll_hw_state.dpll_md = tmp;
  6266. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6267. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6268. tmp = I915_READ(DPLL(crtc->pipe));
  6269. pipe_config->pixel_multiplier =
  6270. ((tmp & SDVO_MULTIPLIER_MASK)
  6271. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6272. } else {
  6273. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6274. * port and will be fixed up in the encoder->get_config
  6275. * function. */
  6276. pipe_config->pixel_multiplier = 1;
  6277. }
  6278. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6279. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6280. /*
  6281. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6282. * on 830. Filter it out here so that we don't
  6283. * report errors due to that.
  6284. */
  6285. if (IS_I830(dev_priv))
  6286. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6287. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6288. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6289. } else {
  6290. /* Mask out read-only status bits. */
  6291. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6292. DPLL_PORTC_READY_MASK |
  6293. DPLL_PORTB_READY_MASK);
  6294. }
  6295. if (IS_CHERRYVIEW(dev_priv))
  6296. chv_crtc_clock_get(crtc, pipe_config);
  6297. else if (IS_VALLEYVIEW(dev_priv))
  6298. vlv_crtc_clock_get(crtc, pipe_config);
  6299. else
  6300. i9xx_crtc_clock_get(crtc, pipe_config);
  6301. /*
  6302. * Normally the dotclock is filled in by the encoder .get_config()
  6303. * but in case the pipe is enabled w/o any ports we need a sane
  6304. * default.
  6305. */
  6306. pipe_config->base.adjusted_mode.crtc_clock =
  6307. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6308. ret = true;
  6309. out:
  6310. intel_display_power_put(dev_priv, power_domain);
  6311. return ret;
  6312. }
  6313. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6314. {
  6315. struct intel_encoder *encoder;
  6316. int i;
  6317. u32 val, final;
  6318. bool has_lvds = false;
  6319. bool has_cpu_edp = false;
  6320. bool has_panel = false;
  6321. bool has_ck505 = false;
  6322. bool can_ssc = false;
  6323. bool using_ssc_source = false;
  6324. /* We need to take the global config into account */
  6325. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6326. switch (encoder->type) {
  6327. case INTEL_OUTPUT_LVDS:
  6328. has_panel = true;
  6329. has_lvds = true;
  6330. break;
  6331. case INTEL_OUTPUT_EDP:
  6332. has_panel = true;
  6333. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6334. has_cpu_edp = true;
  6335. break;
  6336. default:
  6337. break;
  6338. }
  6339. }
  6340. if (HAS_PCH_IBX(dev_priv)) {
  6341. has_ck505 = dev_priv->vbt.display_clock_mode;
  6342. can_ssc = has_ck505;
  6343. } else {
  6344. has_ck505 = false;
  6345. can_ssc = true;
  6346. }
  6347. /* Check if any DPLLs are using the SSC source */
  6348. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6349. u32 temp = I915_READ(PCH_DPLL(i));
  6350. if (!(temp & DPLL_VCO_ENABLE))
  6351. continue;
  6352. if ((temp & PLL_REF_INPUT_MASK) ==
  6353. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6354. using_ssc_source = true;
  6355. break;
  6356. }
  6357. }
  6358. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6359. has_panel, has_lvds, has_ck505, using_ssc_source);
  6360. /* Ironlake: try to setup display ref clock before DPLL
  6361. * enabling. This is only under driver's control after
  6362. * PCH B stepping, previous chipset stepping should be
  6363. * ignoring this setting.
  6364. */
  6365. val = I915_READ(PCH_DREF_CONTROL);
  6366. /* As we must carefully and slowly disable/enable each source in turn,
  6367. * compute the final state we want first and check if we need to
  6368. * make any changes at all.
  6369. */
  6370. final = val;
  6371. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6372. if (has_ck505)
  6373. final |= DREF_NONSPREAD_CK505_ENABLE;
  6374. else
  6375. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6376. final &= ~DREF_SSC_SOURCE_MASK;
  6377. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6378. final &= ~DREF_SSC1_ENABLE;
  6379. if (has_panel) {
  6380. final |= DREF_SSC_SOURCE_ENABLE;
  6381. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6382. final |= DREF_SSC1_ENABLE;
  6383. if (has_cpu_edp) {
  6384. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6385. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6386. else
  6387. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6388. } else
  6389. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6390. } else if (using_ssc_source) {
  6391. final |= DREF_SSC_SOURCE_ENABLE;
  6392. final |= DREF_SSC1_ENABLE;
  6393. }
  6394. if (final == val)
  6395. return;
  6396. /* Always enable nonspread source */
  6397. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6398. if (has_ck505)
  6399. val |= DREF_NONSPREAD_CK505_ENABLE;
  6400. else
  6401. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6402. if (has_panel) {
  6403. val &= ~DREF_SSC_SOURCE_MASK;
  6404. val |= DREF_SSC_SOURCE_ENABLE;
  6405. /* SSC must be turned on before enabling the CPU output */
  6406. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6407. DRM_DEBUG_KMS("Using SSC on panel\n");
  6408. val |= DREF_SSC1_ENABLE;
  6409. } else
  6410. val &= ~DREF_SSC1_ENABLE;
  6411. /* Get SSC going before enabling the outputs */
  6412. I915_WRITE(PCH_DREF_CONTROL, val);
  6413. POSTING_READ(PCH_DREF_CONTROL);
  6414. udelay(200);
  6415. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6416. /* Enable CPU source on CPU attached eDP */
  6417. if (has_cpu_edp) {
  6418. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6419. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6420. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6421. } else
  6422. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6423. } else
  6424. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6425. I915_WRITE(PCH_DREF_CONTROL, val);
  6426. POSTING_READ(PCH_DREF_CONTROL);
  6427. udelay(200);
  6428. } else {
  6429. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6430. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6431. /* Turn off CPU output */
  6432. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6433. I915_WRITE(PCH_DREF_CONTROL, val);
  6434. POSTING_READ(PCH_DREF_CONTROL);
  6435. udelay(200);
  6436. if (!using_ssc_source) {
  6437. DRM_DEBUG_KMS("Disabling SSC source\n");
  6438. /* Turn off the SSC source */
  6439. val &= ~DREF_SSC_SOURCE_MASK;
  6440. val |= DREF_SSC_SOURCE_DISABLE;
  6441. /* Turn off SSC1 */
  6442. val &= ~DREF_SSC1_ENABLE;
  6443. I915_WRITE(PCH_DREF_CONTROL, val);
  6444. POSTING_READ(PCH_DREF_CONTROL);
  6445. udelay(200);
  6446. }
  6447. }
  6448. BUG_ON(val != final);
  6449. }
  6450. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6451. {
  6452. uint32_t tmp;
  6453. tmp = I915_READ(SOUTH_CHICKEN2);
  6454. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6455. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6456. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6457. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6458. DRM_ERROR("FDI mPHY reset assert timeout\n");
  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) == 0, 100))
  6464. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6465. }
  6466. /* WaMPhyProgramming:hsw */
  6467. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6468. {
  6469. uint32_t tmp;
  6470. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6471. tmp &= ~(0xFF << 24);
  6472. tmp |= (0x12 << 24);
  6473. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6474. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6475. tmp |= (1 << 11);
  6476. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6477. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6478. tmp |= (1 << 11);
  6479. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6480. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6481. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6482. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6483. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6484. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6485. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6486. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6487. tmp &= ~(7 << 13);
  6488. tmp |= (5 << 13);
  6489. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6490. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6491. tmp &= ~(7 << 13);
  6492. tmp |= (5 << 13);
  6493. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6494. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6495. tmp &= ~0xFF;
  6496. tmp |= 0x1C;
  6497. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6498. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6499. tmp &= ~0xFF;
  6500. tmp |= 0x1C;
  6501. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6502. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6503. tmp &= ~(0xFF << 16);
  6504. tmp |= (0x1C << 16);
  6505. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6506. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6507. tmp &= ~(0xFF << 16);
  6508. tmp |= (0x1C << 16);
  6509. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6510. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6511. tmp |= (1 << 27);
  6512. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6513. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6514. tmp |= (1 << 27);
  6515. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6516. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6517. tmp &= ~(0xF << 28);
  6518. tmp |= (4 << 28);
  6519. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6520. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6521. tmp &= ~(0xF << 28);
  6522. tmp |= (4 << 28);
  6523. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6524. }
  6525. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6526. * Programming" based on the parameters passed:
  6527. * - Sequence to enable CLKOUT_DP
  6528. * - Sequence to enable CLKOUT_DP without spread
  6529. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6530. */
  6531. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6532. bool with_spread, bool with_fdi)
  6533. {
  6534. uint32_t reg, tmp;
  6535. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6536. with_spread = true;
  6537. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6538. with_fdi, "LP PCH doesn't have FDI\n"))
  6539. with_fdi = false;
  6540. mutex_lock(&dev_priv->sb_lock);
  6541. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6542. tmp &= ~SBI_SSCCTL_DISABLE;
  6543. tmp |= SBI_SSCCTL_PATHALT;
  6544. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6545. udelay(24);
  6546. if (with_spread) {
  6547. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6548. tmp &= ~SBI_SSCCTL_PATHALT;
  6549. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6550. if (with_fdi) {
  6551. lpt_reset_fdi_mphy(dev_priv);
  6552. lpt_program_fdi_mphy(dev_priv);
  6553. }
  6554. }
  6555. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6556. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6557. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6558. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6559. mutex_unlock(&dev_priv->sb_lock);
  6560. }
  6561. /* Sequence to disable CLKOUT_DP */
  6562. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6563. {
  6564. uint32_t reg, tmp;
  6565. mutex_lock(&dev_priv->sb_lock);
  6566. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6567. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6568. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6569. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6570. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6571. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6572. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6573. tmp |= SBI_SSCCTL_PATHALT;
  6574. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6575. udelay(32);
  6576. }
  6577. tmp |= SBI_SSCCTL_DISABLE;
  6578. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6579. }
  6580. mutex_unlock(&dev_priv->sb_lock);
  6581. }
  6582. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6583. static const uint16_t sscdivintphase[] = {
  6584. [BEND_IDX( 50)] = 0x3B23,
  6585. [BEND_IDX( 45)] = 0x3B23,
  6586. [BEND_IDX( 40)] = 0x3C23,
  6587. [BEND_IDX( 35)] = 0x3C23,
  6588. [BEND_IDX( 30)] = 0x3D23,
  6589. [BEND_IDX( 25)] = 0x3D23,
  6590. [BEND_IDX( 20)] = 0x3E23,
  6591. [BEND_IDX( 15)] = 0x3E23,
  6592. [BEND_IDX( 10)] = 0x3F23,
  6593. [BEND_IDX( 5)] = 0x3F23,
  6594. [BEND_IDX( 0)] = 0x0025,
  6595. [BEND_IDX( -5)] = 0x0025,
  6596. [BEND_IDX(-10)] = 0x0125,
  6597. [BEND_IDX(-15)] = 0x0125,
  6598. [BEND_IDX(-20)] = 0x0225,
  6599. [BEND_IDX(-25)] = 0x0225,
  6600. [BEND_IDX(-30)] = 0x0325,
  6601. [BEND_IDX(-35)] = 0x0325,
  6602. [BEND_IDX(-40)] = 0x0425,
  6603. [BEND_IDX(-45)] = 0x0425,
  6604. [BEND_IDX(-50)] = 0x0525,
  6605. };
  6606. /*
  6607. * Bend CLKOUT_DP
  6608. * steps -50 to 50 inclusive, in steps of 5
  6609. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6610. * change in clock period = -(steps / 10) * 5.787 ps
  6611. */
  6612. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6613. {
  6614. uint32_t tmp;
  6615. int idx = BEND_IDX(steps);
  6616. if (WARN_ON(steps % 5 != 0))
  6617. return;
  6618. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6619. return;
  6620. mutex_lock(&dev_priv->sb_lock);
  6621. if (steps % 10 != 0)
  6622. tmp = 0xAAAAAAAB;
  6623. else
  6624. tmp = 0x00000000;
  6625. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6626. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6627. tmp &= 0xffff0000;
  6628. tmp |= sscdivintphase[idx];
  6629. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6630. mutex_unlock(&dev_priv->sb_lock);
  6631. }
  6632. #undef BEND_IDX
  6633. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6634. {
  6635. struct intel_encoder *encoder;
  6636. bool has_vga = false;
  6637. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6638. switch (encoder->type) {
  6639. case INTEL_OUTPUT_ANALOG:
  6640. has_vga = true;
  6641. break;
  6642. default:
  6643. break;
  6644. }
  6645. }
  6646. if (has_vga) {
  6647. lpt_bend_clkout_dp(dev_priv, 0);
  6648. lpt_enable_clkout_dp(dev_priv, true, true);
  6649. } else {
  6650. lpt_disable_clkout_dp(dev_priv);
  6651. }
  6652. }
  6653. /*
  6654. * Initialize reference clocks when the driver loads
  6655. */
  6656. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6657. {
  6658. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6659. ironlake_init_pch_refclk(dev_priv);
  6660. else if (HAS_PCH_LPT(dev_priv))
  6661. lpt_init_pch_refclk(dev_priv);
  6662. }
  6663. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6664. {
  6665. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6666. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6667. int pipe = intel_crtc->pipe;
  6668. uint32_t val;
  6669. val = 0;
  6670. switch (intel_crtc->config->pipe_bpp) {
  6671. case 18:
  6672. val |= PIPECONF_6BPC;
  6673. break;
  6674. case 24:
  6675. val |= PIPECONF_8BPC;
  6676. break;
  6677. case 30:
  6678. val |= PIPECONF_10BPC;
  6679. break;
  6680. case 36:
  6681. val |= PIPECONF_12BPC;
  6682. break;
  6683. default:
  6684. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6685. BUG();
  6686. }
  6687. if (intel_crtc->config->dither)
  6688. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6689. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6690. val |= PIPECONF_INTERLACED_ILK;
  6691. else
  6692. val |= PIPECONF_PROGRESSIVE;
  6693. if (intel_crtc->config->limited_color_range)
  6694. val |= PIPECONF_COLOR_RANGE_SELECT;
  6695. I915_WRITE(PIPECONF(pipe), val);
  6696. POSTING_READ(PIPECONF(pipe));
  6697. }
  6698. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6699. {
  6700. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6701. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6702. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6703. u32 val = 0;
  6704. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6705. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6706. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6707. val |= PIPECONF_INTERLACED_ILK;
  6708. else
  6709. val |= PIPECONF_PROGRESSIVE;
  6710. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6711. POSTING_READ(PIPECONF(cpu_transcoder));
  6712. }
  6713. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6714. {
  6715. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6716. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6717. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  6718. u32 val = 0;
  6719. switch (intel_crtc->config->pipe_bpp) {
  6720. case 18:
  6721. val |= PIPEMISC_DITHER_6_BPC;
  6722. break;
  6723. case 24:
  6724. val |= PIPEMISC_DITHER_8_BPC;
  6725. break;
  6726. case 30:
  6727. val |= PIPEMISC_DITHER_10_BPC;
  6728. break;
  6729. case 36:
  6730. val |= PIPEMISC_DITHER_12_BPC;
  6731. break;
  6732. default:
  6733. /* Case prevented by pipe_config_set_bpp. */
  6734. BUG();
  6735. }
  6736. if (intel_crtc->config->dither)
  6737. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6738. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6739. }
  6740. }
  6741. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6742. {
  6743. /*
  6744. * Account for spread spectrum to avoid
  6745. * oversubscribing the link. Max center spread
  6746. * is 2.5%; use 5% for safety's sake.
  6747. */
  6748. u32 bps = target_clock * bpp * 21 / 20;
  6749. return DIV_ROUND_UP(bps, link_bw * 8);
  6750. }
  6751. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6752. {
  6753. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6754. }
  6755. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6756. struct intel_crtc_state *crtc_state,
  6757. struct dpll *reduced_clock)
  6758. {
  6759. struct drm_crtc *crtc = &intel_crtc->base;
  6760. struct drm_device *dev = crtc->dev;
  6761. struct drm_i915_private *dev_priv = to_i915(dev);
  6762. u32 dpll, fp, fp2;
  6763. int factor;
  6764. /* Enable autotuning of the PLL clock (if permissible) */
  6765. factor = 21;
  6766. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6767. if ((intel_panel_use_ssc(dev_priv) &&
  6768. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6769. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6770. factor = 25;
  6771. } else if (crtc_state->sdvo_tv_clock)
  6772. factor = 20;
  6773. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6774. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6775. fp |= FP_CB_TUNE;
  6776. if (reduced_clock) {
  6777. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6778. if (reduced_clock->m < factor * reduced_clock->n)
  6779. fp2 |= FP_CB_TUNE;
  6780. } else {
  6781. fp2 = fp;
  6782. }
  6783. dpll = 0;
  6784. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6785. dpll |= DPLLB_MODE_LVDS;
  6786. else
  6787. dpll |= DPLLB_MODE_DAC_SERIAL;
  6788. dpll |= (crtc_state->pixel_multiplier - 1)
  6789. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6790. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6791. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6792. dpll |= DPLL_SDVO_HIGH_SPEED;
  6793. if (intel_crtc_has_dp_encoder(crtc_state))
  6794. dpll |= DPLL_SDVO_HIGH_SPEED;
  6795. /*
  6796. * The high speed IO clock is only really required for
  6797. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6798. * possible to share the DPLL between CRT and HDMI. Enabling
  6799. * the clock needlessly does no real harm, except use up a
  6800. * bit of power potentially.
  6801. *
  6802. * We'll limit this to IVB with 3 pipes, since it has only two
  6803. * DPLLs and so DPLL sharing is the only way to get three pipes
  6804. * driving PCH ports at the same time. On SNB we could do this,
  6805. * and potentially avoid enabling the second DPLL, but it's not
  6806. * clear if it''s a win or loss power wise. No point in doing
  6807. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  6808. */
  6809. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  6810. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  6811. dpll |= DPLL_SDVO_HIGH_SPEED;
  6812. /* compute bitmask from p1 value */
  6813. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6814. /* also FPA1 */
  6815. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6816. switch (crtc_state->dpll.p2) {
  6817. case 5:
  6818. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6819. break;
  6820. case 7:
  6821. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6822. break;
  6823. case 10:
  6824. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6825. break;
  6826. case 14:
  6827. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6828. break;
  6829. }
  6830. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6831. intel_panel_use_ssc(dev_priv))
  6832. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6833. else
  6834. dpll |= PLL_REF_INPUT_DREFCLK;
  6835. dpll |= DPLL_VCO_ENABLE;
  6836. crtc_state->dpll_hw_state.dpll = dpll;
  6837. crtc_state->dpll_hw_state.fp0 = fp;
  6838. crtc_state->dpll_hw_state.fp1 = fp2;
  6839. }
  6840. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6841. struct intel_crtc_state *crtc_state)
  6842. {
  6843. struct drm_device *dev = crtc->base.dev;
  6844. struct drm_i915_private *dev_priv = to_i915(dev);
  6845. struct dpll reduced_clock;
  6846. bool has_reduced_clock = false;
  6847. struct intel_shared_dpll *pll;
  6848. const struct intel_limit *limit;
  6849. int refclk = 120000;
  6850. memset(&crtc_state->dpll_hw_state, 0,
  6851. sizeof(crtc_state->dpll_hw_state));
  6852. crtc->lowfreq_avail = false;
  6853. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6854. if (!crtc_state->has_pch_encoder)
  6855. return 0;
  6856. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6857. if (intel_panel_use_ssc(dev_priv)) {
  6858. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6859. dev_priv->vbt.lvds_ssc_freq);
  6860. refclk = dev_priv->vbt.lvds_ssc_freq;
  6861. }
  6862. if (intel_is_dual_link_lvds(dev)) {
  6863. if (refclk == 100000)
  6864. limit = &intel_limits_ironlake_dual_lvds_100m;
  6865. else
  6866. limit = &intel_limits_ironlake_dual_lvds;
  6867. } else {
  6868. if (refclk == 100000)
  6869. limit = &intel_limits_ironlake_single_lvds_100m;
  6870. else
  6871. limit = &intel_limits_ironlake_single_lvds;
  6872. }
  6873. } else {
  6874. limit = &intel_limits_ironlake_dac;
  6875. }
  6876. if (!crtc_state->clock_set &&
  6877. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6878. refclk, NULL, &crtc_state->dpll)) {
  6879. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6880. return -EINVAL;
  6881. }
  6882. ironlake_compute_dpll(crtc, crtc_state,
  6883. has_reduced_clock ? &reduced_clock : NULL);
  6884. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  6885. if (pll == NULL) {
  6886. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6887. pipe_name(crtc->pipe));
  6888. return -EINVAL;
  6889. }
  6890. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6891. has_reduced_clock)
  6892. crtc->lowfreq_avail = true;
  6893. return 0;
  6894. }
  6895. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6896. struct intel_link_m_n *m_n)
  6897. {
  6898. struct drm_device *dev = crtc->base.dev;
  6899. struct drm_i915_private *dev_priv = to_i915(dev);
  6900. enum pipe pipe = crtc->pipe;
  6901. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6902. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6903. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6904. & ~TU_SIZE_MASK;
  6905. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6906. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6907. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6908. }
  6909. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6910. enum transcoder transcoder,
  6911. struct intel_link_m_n *m_n,
  6912. struct intel_link_m_n *m2_n2)
  6913. {
  6914. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6915. enum pipe pipe = crtc->pipe;
  6916. if (INTEL_GEN(dev_priv) >= 5) {
  6917. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6918. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6919. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6920. & ~TU_SIZE_MASK;
  6921. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6922. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6923. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6924. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6925. * gen < 8) and if DRRS is supported (to make sure the
  6926. * registers are not unnecessarily read).
  6927. */
  6928. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  6929. crtc->config->has_drrs) {
  6930. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6931. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6932. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6933. & ~TU_SIZE_MASK;
  6934. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6935. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6936. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6937. }
  6938. } else {
  6939. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6940. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6941. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6942. & ~TU_SIZE_MASK;
  6943. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6944. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6945. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6946. }
  6947. }
  6948. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6949. struct intel_crtc_state *pipe_config)
  6950. {
  6951. if (pipe_config->has_pch_encoder)
  6952. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6953. else
  6954. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6955. &pipe_config->dp_m_n,
  6956. &pipe_config->dp_m2_n2);
  6957. }
  6958. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6959. struct intel_crtc_state *pipe_config)
  6960. {
  6961. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6962. &pipe_config->fdi_m_n, NULL);
  6963. }
  6964. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6965. struct intel_crtc_state *pipe_config)
  6966. {
  6967. struct drm_device *dev = crtc->base.dev;
  6968. struct drm_i915_private *dev_priv = to_i915(dev);
  6969. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  6970. uint32_t ps_ctrl = 0;
  6971. int id = -1;
  6972. int i;
  6973. /* find scaler attached to this pipe */
  6974. for (i = 0; i < crtc->num_scalers; i++) {
  6975. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  6976. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  6977. id = i;
  6978. pipe_config->pch_pfit.enabled = true;
  6979. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  6980. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  6981. break;
  6982. }
  6983. }
  6984. scaler_state->scaler_id = id;
  6985. if (id >= 0) {
  6986. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  6987. } else {
  6988. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  6989. }
  6990. }
  6991. static void
  6992. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  6993. struct intel_initial_plane_config *plane_config)
  6994. {
  6995. struct drm_device *dev = crtc->base.dev;
  6996. struct drm_i915_private *dev_priv = to_i915(dev);
  6997. u32 val, base, offset, stride_mult, tiling;
  6998. int pipe = crtc->pipe;
  6999. int fourcc, pixel_format;
  7000. unsigned int aligned_height;
  7001. struct drm_framebuffer *fb;
  7002. struct intel_framebuffer *intel_fb;
  7003. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7004. if (!intel_fb) {
  7005. DRM_DEBUG_KMS("failed to alloc fb\n");
  7006. return;
  7007. }
  7008. fb = &intel_fb->base;
  7009. fb->dev = dev;
  7010. val = I915_READ(PLANE_CTL(pipe, 0));
  7011. if (!(val & PLANE_CTL_ENABLE))
  7012. goto error;
  7013. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7014. fourcc = skl_format_to_fourcc(pixel_format,
  7015. val & PLANE_CTL_ORDER_RGBX,
  7016. val & PLANE_CTL_ALPHA_MASK);
  7017. fb->format = drm_format_info(fourcc);
  7018. tiling = val & PLANE_CTL_TILED_MASK;
  7019. switch (tiling) {
  7020. case PLANE_CTL_TILED_LINEAR:
  7021. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7022. break;
  7023. case PLANE_CTL_TILED_X:
  7024. plane_config->tiling = I915_TILING_X;
  7025. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7026. break;
  7027. case PLANE_CTL_TILED_Y:
  7028. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7029. break;
  7030. case PLANE_CTL_TILED_YF:
  7031. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7032. break;
  7033. default:
  7034. MISSING_CASE(tiling);
  7035. goto error;
  7036. }
  7037. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7038. plane_config->base = base;
  7039. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7040. val = I915_READ(PLANE_SIZE(pipe, 0));
  7041. fb->height = ((val >> 16) & 0xfff) + 1;
  7042. fb->width = ((val >> 0) & 0x1fff) + 1;
  7043. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7044. stride_mult = intel_fb_stride_alignment(fb, 0);
  7045. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7046. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7047. plane_config->size = fb->pitches[0] * aligned_height;
  7048. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7049. pipe_name(pipe), fb->width, fb->height,
  7050. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7051. plane_config->size);
  7052. plane_config->fb = intel_fb;
  7053. return;
  7054. error:
  7055. kfree(intel_fb);
  7056. }
  7057. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7058. struct intel_crtc_state *pipe_config)
  7059. {
  7060. struct drm_device *dev = crtc->base.dev;
  7061. struct drm_i915_private *dev_priv = to_i915(dev);
  7062. uint32_t tmp;
  7063. tmp = I915_READ(PF_CTL(crtc->pipe));
  7064. if (tmp & PF_ENABLE) {
  7065. pipe_config->pch_pfit.enabled = true;
  7066. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7067. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7068. /* We currently do not free assignements of panel fitters on
  7069. * ivb/hsw (since we don't use the higher upscaling modes which
  7070. * differentiates them) so just WARN about this case for now. */
  7071. if (IS_GEN7(dev_priv)) {
  7072. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7073. PF_PIPE_SEL_IVB(crtc->pipe));
  7074. }
  7075. }
  7076. }
  7077. static void
  7078. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7079. struct intel_initial_plane_config *plane_config)
  7080. {
  7081. struct drm_device *dev = crtc->base.dev;
  7082. struct drm_i915_private *dev_priv = to_i915(dev);
  7083. u32 val, base, offset;
  7084. int pipe = crtc->pipe;
  7085. int fourcc, pixel_format;
  7086. unsigned int aligned_height;
  7087. struct drm_framebuffer *fb;
  7088. struct intel_framebuffer *intel_fb;
  7089. val = I915_READ(DSPCNTR(pipe));
  7090. if (!(val & DISPLAY_PLANE_ENABLE))
  7091. return;
  7092. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7093. if (!intel_fb) {
  7094. DRM_DEBUG_KMS("failed to alloc fb\n");
  7095. return;
  7096. }
  7097. fb = &intel_fb->base;
  7098. fb->dev = dev;
  7099. if (INTEL_GEN(dev_priv) >= 4) {
  7100. if (val & DISPPLANE_TILED) {
  7101. plane_config->tiling = I915_TILING_X;
  7102. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7103. }
  7104. }
  7105. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7106. fourcc = i9xx_format_to_fourcc(pixel_format);
  7107. fb->format = drm_format_info(fourcc);
  7108. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7109. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  7110. offset = I915_READ(DSPOFFSET(pipe));
  7111. } else {
  7112. if (plane_config->tiling)
  7113. offset = I915_READ(DSPTILEOFF(pipe));
  7114. else
  7115. offset = I915_READ(DSPLINOFF(pipe));
  7116. }
  7117. plane_config->base = base;
  7118. val = I915_READ(PIPESRC(pipe));
  7119. fb->width = ((val >> 16) & 0xfff) + 1;
  7120. fb->height = ((val >> 0) & 0xfff) + 1;
  7121. val = I915_READ(DSPSTRIDE(pipe));
  7122. fb->pitches[0] = val & 0xffffffc0;
  7123. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7124. plane_config->size = fb->pitches[0] * aligned_height;
  7125. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7126. pipe_name(pipe), fb->width, fb->height,
  7127. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7128. plane_config->size);
  7129. plane_config->fb = intel_fb;
  7130. }
  7131. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7132. struct intel_crtc_state *pipe_config)
  7133. {
  7134. struct drm_device *dev = crtc->base.dev;
  7135. struct drm_i915_private *dev_priv = to_i915(dev);
  7136. enum intel_display_power_domain power_domain;
  7137. uint32_t tmp;
  7138. bool ret;
  7139. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7140. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7141. return false;
  7142. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7143. pipe_config->shared_dpll = NULL;
  7144. ret = false;
  7145. tmp = I915_READ(PIPECONF(crtc->pipe));
  7146. if (!(tmp & PIPECONF_ENABLE))
  7147. goto out;
  7148. switch (tmp & PIPECONF_BPC_MASK) {
  7149. case PIPECONF_6BPC:
  7150. pipe_config->pipe_bpp = 18;
  7151. break;
  7152. case PIPECONF_8BPC:
  7153. pipe_config->pipe_bpp = 24;
  7154. break;
  7155. case PIPECONF_10BPC:
  7156. pipe_config->pipe_bpp = 30;
  7157. break;
  7158. case PIPECONF_12BPC:
  7159. pipe_config->pipe_bpp = 36;
  7160. break;
  7161. default:
  7162. break;
  7163. }
  7164. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7165. pipe_config->limited_color_range = true;
  7166. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7167. struct intel_shared_dpll *pll;
  7168. enum intel_dpll_id pll_id;
  7169. pipe_config->has_pch_encoder = true;
  7170. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7171. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7172. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7173. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7174. if (HAS_PCH_IBX(dev_priv)) {
  7175. /*
  7176. * The pipe->pch transcoder and pch transcoder->pll
  7177. * mapping is fixed.
  7178. */
  7179. pll_id = (enum intel_dpll_id) crtc->pipe;
  7180. } else {
  7181. tmp = I915_READ(PCH_DPLL_SEL);
  7182. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7183. pll_id = DPLL_ID_PCH_PLL_B;
  7184. else
  7185. pll_id= DPLL_ID_PCH_PLL_A;
  7186. }
  7187. pipe_config->shared_dpll =
  7188. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7189. pll = pipe_config->shared_dpll;
  7190. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7191. &pipe_config->dpll_hw_state));
  7192. tmp = pipe_config->dpll_hw_state.dpll;
  7193. pipe_config->pixel_multiplier =
  7194. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7195. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7196. ironlake_pch_clock_get(crtc, pipe_config);
  7197. } else {
  7198. pipe_config->pixel_multiplier = 1;
  7199. }
  7200. intel_get_pipe_timings(crtc, pipe_config);
  7201. intel_get_pipe_src_size(crtc, pipe_config);
  7202. ironlake_get_pfit_config(crtc, pipe_config);
  7203. ret = true;
  7204. out:
  7205. intel_display_power_put(dev_priv, power_domain);
  7206. return ret;
  7207. }
  7208. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7209. {
  7210. struct drm_device *dev = &dev_priv->drm;
  7211. struct intel_crtc *crtc;
  7212. for_each_intel_crtc(dev, crtc)
  7213. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7214. pipe_name(crtc->pipe));
  7215. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7216. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7217. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7218. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7219. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7220. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7221. "CPU PWM1 enabled\n");
  7222. if (IS_HASWELL(dev_priv))
  7223. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7224. "CPU PWM2 enabled\n");
  7225. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7226. "PCH PWM1 enabled\n");
  7227. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7228. "Utility pin enabled\n");
  7229. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7230. /*
  7231. * In theory we can still leave IRQs enabled, as long as only the HPD
  7232. * interrupts remain enabled. We used to check for that, but since it's
  7233. * gen-specific and since we only disable LCPLL after we fully disable
  7234. * the interrupts, the check below should be enough.
  7235. */
  7236. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7237. }
  7238. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7239. {
  7240. if (IS_HASWELL(dev_priv))
  7241. return I915_READ(D_COMP_HSW);
  7242. else
  7243. return I915_READ(D_COMP_BDW);
  7244. }
  7245. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7246. {
  7247. if (IS_HASWELL(dev_priv)) {
  7248. mutex_lock(&dev_priv->rps.hw_lock);
  7249. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7250. val))
  7251. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7252. mutex_unlock(&dev_priv->rps.hw_lock);
  7253. } else {
  7254. I915_WRITE(D_COMP_BDW, val);
  7255. POSTING_READ(D_COMP_BDW);
  7256. }
  7257. }
  7258. /*
  7259. * This function implements pieces of two sequences from BSpec:
  7260. * - Sequence for display software to disable LCPLL
  7261. * - Sequence for display software to allow package C8+
  7262. * The steps implemented here are just the steps that actually touch the LCPLL
  7263. * register. Callers should take care of disabling all the display engine
  7264. * functions, doing the mode unset, fixing interrupts, etc.
  7265. */
  7266. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7267. bool switch_to_fclk, bool allow_power_down)
  7268. {
  7269. uint32_t val;
  7270. assert_can_disable_lcpll(dev_priv);
  7271. val = I915_READ(LCPLL_CTL);
  7272. if (switch_to_fclk) {
  7273. val |= LCPLL_CD_SOURCE_FCLK;
  7274. I915_WRITE(LCPLL_CTL, val);
  7275. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7276. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7277. DRM_ERROR("Switching to FCLK failed\n");
  7278. val = I915_READ(LCPLL_CTL);
  7279. }
  7280. val |= LCPLL_PLL_DISABLE;
  7281. I915_WRITE(LCPLL_CTL, val);
  7282. POSTING_READ(LCPLL_CTL);
  7283. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7284. DRM_ERROR("LCPLL still locked\n");
  7285. val = hsw_read_dcomp(dev_priv);
  7286. val |= D_COMP_COMP_DISABLE;
  7287. hsw_write_dcomp(dev_priv, val);
  7288. ndelay(100);
  7289. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7290. 1))
  7291. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7292. if (allow_power_down) {
  7293. val = I915_READ(LCPLL_CTL);
  7294. val |= LCPLL_POWER_DOWN_ALLOW;
  7295. I915_WRITE(LCPLL_CTL, val);
  7296. POSTING_READ(LCPLL_CTL);
  7297. }
  7298. }
  7299. /*
  7300. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7301. * source.
  7302. */
  7303. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7304. {
  7305. uint32_t val;
  7306. val = I915_READ(LCPLL_CTL);
  7307. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7308. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7309. return;
  7310. /*
  7311. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7312. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7313. */
  7314. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7315. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7316. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7317. I915_WRITE(LCPLL_CTL, val);
  7318. POSTING_READ(LCPLL_CTL);
  7319. }
  7320. val = hsw_read_dcomp(dev_priv);
  7321. val |= D_COMP_COMP_FORCE;
  7322. val &= ~D_COMP_COMP_DISABLE;
  7323. hsw_write_dcomp(dev_priv, val);
  7324. val = I915_READ(LCPLL_CTL);
  7325. val &= ~LCPLL_PLL_DISABLE;
  7326. I915_WRITE(LCPLL_CTL, val);
  7327. if (intel_wait_for_register(dev_priv,
  7328. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7329. 5))
  7330. DRM_ERROR("LCPLL not locked yet\n");
  7331. if (val & LCPLL_CD_SOURCE_FCLK) {
  7332. val = I915_READ(LCPLL_CTL);
  7333. val &= ~LCPLL_CD_SOURCE_FCLK;
  7334. I915_WRITE(LCPLL_CTL, val);
  7335. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7336. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7337. DRM_ERROR("Switching back to LCPLL failed\n");
  7338. }
  7339. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7340. intel_update_cdclk(dev_priv);
  7341. }
  7342. /*
  7343. * Package states C8 and deeper are really deep PC states that can only be
  7344. * reached when all the devices on the system allow it, so even if the graphics
  7345. * device allows PC8+, it doesn't mean the system will actually get to these
  7346. * states. Our driver only allows PC8+ when going into runtime PM.
  7347. *
  7348. * The requirements for PC8+ are that all the outputs are disabled, the power
  7349. * well is disabled and most interrupts are disabled, and these are also
  7350. * requirements for runtime PM. When these conditions are met, we manually do
  7351. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7352. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7353. * hang the machine.
  7354. *
  7355. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7356. * the state of some registers, so when we come back from PC8+ we need to
  7357. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7358. * need to take care of the registers kept by RC6. Notice that this happens even
  7359. * if we don't put the device in PCI D3 state (which is what currently happens
  7360. * because of the runtime PM support).
  7361. *
  7362. * For more, read "Display Sequences for Package C8" on the hardware
  7363. * documentation.
  7364. */
  7365. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7366. {
  7367. uint32_t val;
  7368. DRM_DEBUG_KMS("Enabling package C8+\n");
  7369. if (HAS_PCH_LPT_LP(dev_priv)) {
  7370. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7371. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7372. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7373. }
  7374. lpt_disable_clkout_dp(dev_priv);
  7375. hsw_disable_lcpll(dev_priv, true, true);
  7376. }
  7377. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7378. {
  7379. uint32_t val;
  7380. DRM_DEBUG_KMS("Disabling package C8+\n");
  7381. hsw_restore_lcpll(dev_priv);
  7382. lpt_init_pch_refclk(dev_priv);
  7383. if (HAS_PCH_LPT_LP(dev_priv)) {
  7384. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7385. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7386. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7387. }
  7388. }
  7389. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7390. struct intel_crtc_state *crtc_state)
  7391. {
  7392. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7393. struct intel_encoder *encoder =
  7394. intel_ddi_get_crtc_new_encoder(crtc_state);
  7395. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7396. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7397. pipe_name(crtc->pipe));
  7398. return -EINVAL;
  7399. }
  7400. }
  7401. crtc->lowfreq_avail = false;
  7402. return 0;
  7403. }
  7404. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7405. enum port port,
  7406. struct intel_crtc_state *pipe_config)
  7407. {
  7408. enum intel_dpll_id id;
  7409. switch (port) {
  7410. case PORT_A:
  7411. id = DPLL_ID_SKL_DPLL0;
  7412. break;
  7413. case PORT_B:
  7414. id = DPLL_ID_SKL_DPLL1;
  7415. break;
  7416. case PORT_C:
  7417. id = DPLL_ID_SKL_DPLL2;
  7418. break;
  7419. default:
  7420. DRM_ERROR("Incorrect port type\n");
  7421. return;
  7422. }
  7423. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7424. }
  7425. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7426. enum port port,
  7427. struct intel_crtc_state *pipe_config)
  7428. {
  7429. enum intel_dpll_id id;
  7430. u32 temp;
  7431. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7432. id = temp >> (port * 3 + 1);
  7433. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7434. return;
  7435. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7436. }
  7437. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7438. enum port port,
  7439. struct intel_crtc_state *pipe_config)
  7440. {
  7441. enum intel_dpll_id id;
  7442. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7443. switch (ddi_pll_sel) {
  7444. case PORT_CLK_SEL_WRPLL1:
  7445. id = DPLL_ID_WRPLL1;
  7446. break;
  7447. case PORT_CLK_SEL_WRPLL2:
  7448. id = DPLL_ID_WRPLL2;
  7449. break;
  7450. case PORT_CLK_SEL_SPLL:
  7451. id = DPLL_ID_SPLL;
  7452. break;
  7453. case PORT_CLK_SEL_LCPLL_810:
  7454. id = DPLL_ID_LCPLL_810;
  7455. break;
  7456. case PORT_CLK_SEL_LCPLL_1350:
  7457. id = DPLL_ID_LCPLL_1350;
  7458. break;
  7459. case PORT_CLK_SEL_LCPLL_2700:
  7460. id = DPLL_ID_LCPLL_2700;
  7461. break;
  7462. default:
  7463. MISSING_CASE(ddi_pll_sel);
  7464. /* fall through */
  7465. case PORT_CLK_SEL_NONE:
  7466. return;
  7467. }
  7468. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7469. }
  7470. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7471. struct intel_crtc_state *pipe_config,
  7472. u64 *power_domain_mask)
  7473. {
  7474. struct drm_device *dev = crtc->base.dev;
  7475. struct drm_i915_private *dev_priv = to_i915(dev);
  7476. enum intel_display_power_domain power_domain;
  7477. u32 tmp;
  7478. /*
  7479. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7480. * transcoder handled below.
  7481. */
  7482. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7483. /*
  7484. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7485. * consistency and less surprising code; it's in always on power).
  7486. */
  7487. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7488. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7489. enum pipe trans_edp_pipe;
  7490. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7491. default:
  7492. WARN(1, "unknown pipe linked to edp transcoder\n");
  7493. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7494. case TRANS_DDI_EDP_INPUT_A_ON:
  7495. trans_edp_pipe = PIPE_A;
  7496. break;
  7497. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7498. trans_edp_pipe = PIPE_B;
  7499. break;
  7500. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7501. trans_edp_pipe = PIPE_C;
  7502. break;
  7503. }
  7504. if (trans_edp_pipe == crtc->pipe)
  7505. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7506. }
  7507. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7508. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7509. return false;
  7510. *power_domain_mask |= BIT_ULL(power_domain);
  7511. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7512. return tmp & PIPECONF_ENABLE;
  7513. }
  7514. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7515. struct intel_crtc_state *pipe_config,
  7516. u64 *power_domain_mask)
  7517. {
  7518. struct drm_device *dev = crtc->base.dev;
  7519. struct drm_i915_private *dev_priv = to_i915(dev);
  7520. enum intel_display_power_domain power_domain;
  7521. enum port port;
  7522. enum transcoder cpu_transcoder;
  7523. u32 tmp;
  7524. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7525. if (port == PORT_A)
  7526. cpu_transcoder = TRANSCODER_DSI_A;
  7527. else
  7528. cpu_transcoder = TRANSCODER_DSI_C;
  7529. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7530. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7531. continue;
  7532. *power_domain_mask |= BIT_ULL(power_domain);
  7533. /*
  7534. * The PLL needs to be enabled with a valid divider
  7535. * configuration, otherwise accessing DSI registers will hang
  7536. * the machine. See BSpec North Display Engine
  7537. * registers/MIPI[BXT]. We can break out here early, since we
  7538. * need the same DSI PLL to be enabled for both DSI ports.
  7539. */
  7540. if (!intel_dsi_pll_is_enabled(dev_priv))
  7541. break;
  7542. /* XXX: this works for video mode only */
  7543. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7544. if (!(tmp & DPI_ENABLE))
  7545. continue;
  7546. tmp = I915_READ(MIPI_CTRL(port));
  7547. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7548. continue;
  7549. pipe_config->cpu_transcoder = cpu_transcoder;
  7550. break;
  7551. }
  7552. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7553. }
  7554. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7555. struct intel_crtc_state *pipe_config)
  7556. {
  7557. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7558. struct intel_shared_dpll *pll;
  7559. enum port port;
  7560. uint32_t tmp;
  7561. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7562. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7563. if (IS_GEN9_BC(dev_priv))
  7564. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7565. else if (IS_GEN9_LP(dev_priv))
  7566. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7567. else
  7568. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7569. pll = pipe_config->shared_dpll;
  7570. if (pll) {
  7571. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7572. &pipe_config->dpll_hw_state));
  7573. }
  7574. /*
  7575. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7576. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7577. * the PCH transcoder is on.
  7578. */
  7579. if (INTEL_GEN(dev_priv) < 9 &&
  7580. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7581. pipe_config->has_pch_encoder = true;
  7582. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7583. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7584. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7585. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7586. }
  7587. }
  7588. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7589. struct intel_crtc_state *pipe_config)
  7590. {
  7591. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7592. enum intel_display_power_domain power_domain;
  7593. u64 power_domain_mask;
  7594. bool active;
  7595. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7596. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7597. return false;
  7598. power_domain_mask = BIT_ULL(power_domain);
  7599. pipe_config->shared_dpll = NULL;
  7600. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7601. if (IS_GEN9_LP(dev_priv) &&
  7602. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7603. WARN_ON(active);
  7604. active = true;
  7605. }
  7606. if (!active)
  7607. goto out;
  7608. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7609. haswell_get_ddi_port_state(crtc, pipe_config);
  7610. intel_get_pipe_timings(crtc, pipe_config);
  7611. }
  7612. intel_get_pipe_src_size(crtc, pipe_config);
  7613. pipe_config->gamma_mode =
  7614. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7615. if (INTEL_GEN(dev_priv) >= 9) {
  7616. intel_crtc_init_scalers(crtc, pipe_config);
  7617. pipe_config->scaler_state.scaler_id = -1;
  7618. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7619. }
  7620. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7621. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7622. power_domain_mask |= BIT_ULL(power_domain);
  7623. if (INTEL_GEN(dev_priv) >= 9)
  7624. skylake_get_pfit_config(crtc, pipe_config);
  7625. else
  7626. ironlake_get_pfit_config(crtc, pipe_config);
  7627. }
  7628. if (IS_HASWELL(dev_priv))
  7629. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7630. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7631. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7632. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7633. pipe_config->pixel_multiplier =
  7634. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7635. } else {
  7636. pipe_config->pixel_multiplier = 1;
  7637. }
  7638. out:
  7639. for_each_power_domain(power_domain, power_domain_mask)
  7640. intel_display_power_put(dev_priv, power_domain);
  7641. return active;
  7642. }
  7643. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7644. const struct intel_plane_state *plane_state)
  7645. {
  7646. unsigned int width = plane_state->base.crtc_w;
  7647. unsigned int stride = roundup_pow_of_two(width) * 4;
  7648. switch (stride) {
  7649. default:
  7650. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  7651. width, stride);
  7652. stride = 256;
  7653. /* fallthrough */
  7654. case 256:
  7655. case 512:
  7656. case 1024:
  7657. case 2048:
  7658. break;
  7659. }
  7660. return CURSOR_ENABLE |
  7661. CURSOR_GAMMA_ENABLE |
  7662. CURSOR_FORMAT_ARGB |
  7663. CURSOR_STRIDE(stride);
  7664. }
  7665. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  7666. const struct intel_plane_state *plane_state)
  7667. {
  7668. struct drm_device *dev = crtc->dev;
  7669. struct drm_i915_private *dev_priv = to_i915(dev);
  7670. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7671. uint32_t cntl = 0, size = 0;
  7672. if (plane_state && plane_state->base.visible) {
  7673. unsigned int width = plane_state->base.crtc_w;
  7674. unsigned int height = plane_state->base.crtc_h;
  7675. cntl = plane_state->ctl;
  7676. size = (height << 12) | width;
  7677. }
  7678. if (intel_crtc->cursor_cntl != 0 &&
  7679. (intel_crtc->cursor_base != base ||
  7680. intel_crtc->cursor_size != size ||
  7681. intel_crtc->cursor_cntl != cntl)) {
  7682. /* On these chipsets we can only modify the base/size/stride
  7683. * whilst the cursor is disabled.
  7684. */
  7685. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  7686. POSTING_READ_FW(CURCNTR(PIPE_A));
  7687. intel_crtc->cursor_cntl = 0;
  7688. }
  7689. if (intel_crtc->cursor_base != base) {
  7690. I915_WRITE_FW(CURBASE(PIPE_A), base);
  7691. intel_crtc->cursor_base = base;
  7692. }
  7693. if (intel_crtc->cursor_size != size) {
  7694. I915_WRITE_FW(CURSIZE, size);
  7695. intel_crtc->cursor_size = size;
  7696. }
  7697. if (intel_crtc->cursor_cntl != cntl) {
  7698. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  7699. POSTING_READ_FW(CURCNTR(PIPE_A));
  7700. intel_crtc->cursor_cntl = cntl;
  7701. }
  7702. }
  7703. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7704. const struct intel_plane_state *plane_state)
  7705. {
  7706. struct drm_i915_private *dev_priv =
  7707. to_i915(plane_state->base.plane->dev);
  7708. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  7709. enum pipe pipe = crtc->pipe;
  7710. u32 cntl;
  7711. cntl = MCURSOR_GAMMA_ENABLE;
  7712. if (HAS_DDI(dev_priv))
  7713. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7714. cntl |= pipe << 28; /* Connect to correct pipe */
  7715. switch (plane_state->base.crtc_w) {
  7716. case 64:
  7717. cntl |= CURSOR_MODE_64_ARGB_AX;
  7718. break;
  7719. case 128:
  7720. cntl |= CURSOR_MODE_128_ARGB_AX;
  7721. break;
  7722. case 256:
  7723. cntl |= CURSOR_MODE_256_ARGB_AX;
  7724. break;
  7725. default:
  7726. MISSING_CASE(plane_state->base.crtc_w);
  7727. return 0;
  7728. }
  7729. if (plane_state->base.rotation & DRM_ROTATE_180)
  7730. cntl |= CURSOR_ROTATE_180;
  7731. return cntl;
  7732. }
  7733. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  7734. const struct intel_plane_state *plane_state)
  7735. {
  7736. struct drm_device *dev = crtc->dev;
  7737. struct drm_i915_private *dev_priv = to_i915(dev);
  7738. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7739. int pipe = intel_crtc->pipe;
  7740. uint32_t cntl = 0;
  7741. if (plane_state && plane_state->base.visible)
  7742. cntl = plane_state->ctl;
  7743. if (intel_crtc->cursor_cntl != cntl) {
  7744. I915_WRITE_FW(CURCNTR(pipe), cntl);
  7745. POSTING_READ_FW(CURCNTR(pipe));
  7746. intel_crtc->cursor_cntl = cntl;
  7747. }
  7748. /* and commit changes on next vblank */
  7749. I915_WRITE_FW(CURBASE(pipe), base);
  7750. POSTING_READ_FW(CURBASE(pipe));
  7751. intel_crtc->cursor_base = base;
  7752. }
  7753. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7754. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7755. const struct intel_plane_state *plane_state)
  7756. {
  7757. struct drm_device *dev = crtc->dev;
  7758. struct drm_i915_private *dev_priv = to_i915(dev);
  7759. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7760. int pipe = intel_crtc->pipe;
  7761. u32 base = intel_crtc->cursor_addr;
  7762. unsigned long irqflags;
  7763. u32 pos = 0;
  7764. if (plane_state) {
  7765. int x = plane_state->base.crtc_x;
  7766. int y = plane_state->base.crtc_y;
  7767. if (x < 0) {
  7768. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7769. x = -x;
  7770. }
  7771. pos |= x << CURSOR_X_SHIFT;
  7772. if (y < 0) {
  7773. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7774. y = -y;
  7775. }
  7776. pos |= y << CURSOR_Y_SHIFT;
  7777. /* ILK+ do this automagically */
  7778. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7779. plane_state->base.rotation & DRM_ROTATE_180) {
  7780. base += (plane_state->base.crtc_h *
  7781. plane_state->base.crtc_w - 1) * 4;
  7782. }
  7783. }
  7784. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7785. I915_WRITE_FW(CURPOS(pipe), pos);
  7786. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  7787. i845_update_cursor(crtc, base, plane_state);
  7788. else
  7789. i9xx_update_cursor(crtc, base, plane_state);
  7790. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7791. }
  7792. static bool cursor_size_ok(struct drm_i915_private *dev_priv,
  7793. uint32_t width, uint32_t height)
  7794. {
  7795. if (width == 0 || height == 0)
  7796. return false;
  7797. /*
  7798. * 845g/865g are special in that they are only limited by
  7799. * the width of their cursors, the height is arbitrary up to
  7800. * the precision of the register. Everything else requires
  7801. * square cursors, limited to a few power-of-two sizes.
  7802. */
  7803. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  7804. if ((width & 63) != 0)
  7805. return false;
  7806. if (width > (IS_I845G(dev_priv) ? 64 : 512))
  7807. return false;
  7808. if (height > 1023)
  7809. return false;
  7810. } else {
  7811. switch (width | height) {
  7812. case 256:
  7813. case 128:
  7814. if (IS_GEN2(dev_priv))
  7815. return false;
  7816. case 64:
  7817. break;
  7818. default:
  7819. return false;
  7820. }
  7821. }
  7822. return true;
  7823. }
  7824. /* VESA 640x480x72Hz mode to set on the pipe */
  7825. static struct drm_display_mode load_detect_mode = {
  7826. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7827. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7828. };
  7829. struct drm_framebuffer *
  7830. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  7831. struct drm_mode_fb_cmd2 *mode_cmd)
  7832. {
  7833. struct intel_framebuffer *intel_fb;
  7834. int ret;
  7835. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7836. if (!intel_fb)
  7837. return ERR_PTR(-ENOMEM);
  7838. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  7839. if (ret)
  7840. goto err;
  7841. return &intel_fb->base;
  7842. err:
  7843. kfree(intel_fb);
  7844. return ERR_PTR(ret);
  7845. }
  7846. static u32
  7847. intel_framebuffer_pitch_for_width(int width, int bpp)
  7848. {
  7849. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7850. return ALIGN(pitch, 64);
  7851. }
  7852. static u32
  7853. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7854. {
  7855. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7856. return PAGE_ALIGN(pitch * mode->vdisplay);
  7857. }
  7858. static struct drm_framebuffer *
  7859. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7860. struct drm_display_mode *mode,
  7861. int depth, int bpp)
  7862. {
  7863. struct drm_framebuffer *fb;
  7864. struct drm_i915_gem_object *obj;
  7865. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7866. obj = i915_gem_object_create(to_i915(dev),
  7867. intel_framebuffer_size_for_mode(mode, bpp));
  7868. if (IS_ERR(obj))
  7869. return ERR_CAST(obj);
  7870. mode_cmd.width = mode->hdisplay;
  7871. mode_cmd.height = mode->vdisplay;
  7872. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7873. bpp);
  7874. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7875. fb = intel_framebuffer_create(obj, &mode_cmd);
  7876. if (IS_ERR(fb))
  7877. i915_gem_object_put(obj);
  7878. return fb;
  7879. }
  7880. static struct drm_framebuffer *
  7881. mode_fits_in_fbdev(struct drm_device *dev,
  7882. struct drm_display_mode *mode)
  7883. {
  7884. #ifdef CONFIG_DRM_FBDEV_EMULATION
  7885. struct drm_i915_private *dev_priv = to_i915(dev);
  7886. struct drm_i915_gem_object *obj;
  7887. struct drm_framebuffer *fb;
  7888. if (!dev_priv->fbdev)
  7889. return NULL;
  7890. if (!dev_priv->fbdev->fb)
  7891. return NULL;
  7892. obj = dev_priv->fbdev->fb->obj;
  7893. BUG_ON(!obj);
  7894. fb = &dev_priv->fbdev->fb->base;
  7895. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7896. fb->format->cpp[0] * 8))
  7897. return NULL;
  7898. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7899. return NULL;
  7900. drm_framebuffer_reference(fb);
  7901. return fb;
  7902. #else
  7903. return NULL;
  7904. #endif
  7905. }
  7906. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  7907. struct drm_crtc *crtc,
  7908. struct drm_display_mode *mode,
  7909. struct drm_framebuffer *fb,
  7910. int x, int y)
  7911. {
  7912. struct drm_plane_state *plane_state;
  7913. int hdisplay, vdisplay;
  7914. int ret;
  7915. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  7916. if (IS_ERR(plane_state))
  7917. return PTR_ERR(plane_state);
  7918. if (mode)
  7919. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  7920. else
  7921. hdisplay = vdisplay = 0;
  7922. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  7923. if (ret)
  7924. return ret;
  7925. drm_atomic_set_fb_for_plane(plane_state, fb);
  7926. plane_state->crtc_x = 0;
  7927. plane_state->crtc_y = 0;
  7928. plane_state->crtc_w = hdisplay;
  7929. plane_state->crtc_h = vdisplay;
  7930. plane_state->src_x = x << 16;
  7931. plane_state->src_y = y << 16;
  7932. plane_state->src_w = hdisplay << 16;
  7933. plane_state->src_h = vdisplay << 16;
  7934. return 0;
  7935. }
  7936. int intel_get_load_detect_pipe(struct drm_connector *connector,
  7937. struct drm_display_mode *mode,
  7938. struct intel_load_detect_pipe *old,
  7939. struct drm_modeset_acquire_ctx *ctx)
  7940. {
  7941. struct intel_crtc *intel_crtc;
  7942. struct intel_encoder *intel_encoder =
  7943. intel_attached_encoder(connector);
  7944. struct drm_crtc *possible_crtc;
  7945. struct drm_encoder *encoder = &intel_encoder->base;
  7946. struct drm_crtc *crtc = NULL;
  7947. struct drm_device *dev = encoder->dev;
  7948. struct drm_i915_private *dev_priv = to_i915(dev);
  7949. struct drm_framebuffer *fb;
  7950. struct drm_mode_config *config = &dev->mode_config;
  7951. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  7952. struct drm_connector_state *connector_state;
  7953. struct intel_crtc_state *crtc_state;
  7954. int ret, i = -1;
  7955. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7956. connector->base.id, connector->name,
  7957. encoder->base.id, encoder->name);
  7958. old->restore_state = NULL;
  7959. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  7960. /*
  7961. * Algorithm gets a little messy:
  7962. *
  7963. * - if the connector already has an assigned crtc, use it (but make
  7964. * sure it's on first)
  7965. *
  7966. * - try to find the first unused crtc that can drive this connector,
  7967. * and use that if we find one
  7968. */
  7969. /* See if we already have a CRTC for this connector */
  7970. if (connector->state->crtc) {
  7971. crtc = connector->state->crtc;
  7972. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7973. if (ret)
  7974. goto fail;
  7975. /* Make sure the crtc and connector are running */
  7976. goto found;
  7977. }
  7978. /* Find an unused one (if possible) */
  7979. for_each_crtc(dev, possible_crtc) {
  7980. i++;
  7981. if (!(encoder->possible_crtcs & (1 << i)))
  7982. continue;
  7983. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  7984. if (ret)
  7985. goto fail;
  7986. if (possible_crtc->state->enable) {
  7987. drm_modeset_unlock(&possible_crtc->mutex);
  7988. continue;
  7989. }
  7990. crtc = possible_crtc;
  7991. break;
  7992. }
  7993. /*
  7994. * If we didn't find an unused CRTC, don't use any.
  7995. */
  7996. if (!crtc) {
  7997. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7998. goto fail;
  7999. }
  8000. found:
  8001. intel_crtc = to_intel_crtc(crtc);
  8002. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8003. if (ret)
  8004. goto fail;
  8005. state = drm_atomic_state_alloc(dev);
  8006. restore_state = drm_atomic_state_alloc(dev);
  8007. if (!state || !restore_state) {
  8008. ret = -ENOMEM;
  8009. goto fail;
  8010. }
  8011. state->acquire_ctx = ctx;
  8012. restore_state->acquire_ctx = ctx;
  8013. connector_state = drm_atomic_get_connector_state(state, connector);
  8014. if (IS_ERR(connector_state)) {
  8015. ret = PTR_ERR(connector_state);
  8016. goto fail;
  8017. }
  8018. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8019. if (ret)
  8020. goto fail;
  8021. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8022. if (IS_ERR(crtc_state)) {
  8023. ret = PTR_ERR(crtc_state);
  8024. goto fail;
  8025. }
  8026. crtc_state->base.active = crtc_state->base.enable = true;
  8027. if (!mode)
  8028. mode = &load_detect_mode;
  8029. /* We need a framebuffer large enough to accommodate all accesses
  8030. * that the plane may generate whilst we perform load detection.
  8031. * We can not rely on the fbcon either being present (we get called
  8032. * during its initialisation to detect all boot displays, or it may
  8033. * not even exist) or that it is large enough to satisfy the
  8034. * requested mode.
  8035. */
  8036. fb = mode_fits_in_fbdev(dev, mode);
  8037. if (fb == NULL) {
  8038. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8039. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8040. } else
  8041. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8042. if (IS_ERR(fb)) {
  8043. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8044. goto fail;
  8045. }
  8046. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8047. if (ret)
  8048. goto fail;
  8049. drm_framebuffer_unreference(fb);
  8050. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8051. if (ret)
  8052. goto fail;
  8053. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8054. if (!ret)
  8055. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8056. if (!ret)
  8057. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8058. if (ret) {
  8059. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8060. goto fail;
  8061. }
  8062. ret = drm_atomic_commit(state);
  8063. if (ret) {
  8064. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8065. goto fail;
  8066. }
  8067. old->restore_state = restore_state;
  8068. drm_atomic_state_put(state);
  8069. /* let the connector get through one full cycle before testing */
  8070. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8071. return true;
  8072. fail:
  8073. if (state) {
  8074. drm_atomic_state_put(state);
  8075. state = NULL;
  8076. }
  8077. if (restore_state) {
  8078. drm_atomic_state_put(restore_state);
  8079. restore_state = NULL;
  8080. }
  8081. if (ret == -EDEADLK)
  8082. return ret;
  8083. return false;
  8084. }
  8085. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8086. struct intel_load_detect_pipe *old,
  8087. struct drm_modeset_acquire_ctx *ctx)
  8088. {
  8089. struct intel_encoder *intel_encoder =
  8090. intel_attached_encoder(connector);
  8091. struct drm_encoder *encoder = &intel_encoder->base;
  8092. struct drm_atomic_state *state = old->restore_state;
  8093. int ret;
  8094. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8095. connector->base.id, connector->name,
  8096. encoder->base.id, encoder->name);
  8097. if (!state)
  8098. return;
  8099. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8100. if (ret)
  8101. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8102. drm_atomic_state_put(state);
  8103. }
  8104. static int i9xx_pll_refclk(struct drm_device *dev,
  8105. const struct intel_crtc_state *pipe_config)
  8106. {
  8107. struct drm_i915_private *dev_priv = to_i915(dev);
  8108. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8109. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8110. return dev_priv->vbt.lvds_ssc_freq;
  8111. else if (HAS_PCH_SPLIT(dev_priv))
  8112. return 120000;
  8113. else if (!IS_GEN2(dev_priv))
  8114. return 96000;
  8115. else
  8116. return 48000;
  8117. }
  8118. /* Returns the clock of the currently programmed mode of the given pipe. */
  8119. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8120. struct intel_crtc_state *pipe_config)
  8121. {
  8122. struct drm_device *dev = crtc->base.dev;
  8123. struct drm_i915_private *dev_priv = to_i915(dev);
  8124. int pipe = pipe_config->cpu_transcoder;
  8125. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8126. u32 fp;
  8127. struct dpll clock;
  8128. int port_clock;
  8129. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8130. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8131. fp = pipe_config->dpll_hw_state.fp0;
  8132. else
  8133. fp = pipe_config->dpll_hw_state.fp1;
  8134. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8135. if (IS_PINEVIEW(dev_priv)) {
  8136. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8137. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8138. } else {
  8139. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8140. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8141. }
  8142. if (!IS_GEN2(dev_priv)) {
  8143. if (IS_PINEVIEW(dev_priv))
  8144. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8145. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8146. else
  8147. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8148. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8149. switch (dpll & DPLL_MODE_MASK) {
  8150. case DPLLB_MODE_DAC_SERIAL:
  8151. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8152. 5 : 10;
  8153. break;
  8154. case DPLLB_MODE_LVDS:
  8155. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8156. 7 : 14;
  8157. break;
  8158. default:
  8159. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8160. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8161. return;
  8162. }
  8163. if (IS_PINEVIEW(dev_priv))
  8164. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8165. else
  8166. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8167. } else {
  8168. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8169. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8170. if (is_lvds) {
  8171. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8172. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8173. if (lvds & LVDS_CLKB_POWER_UP)
  8174. clock.p2 = 7;
  8175. else
  8176. clock.p2 = 14;
  8177. } else {
  8178. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8179. clock.p1 = 2;
  8180. else {
  8181. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8182. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8183. }
  8184. if (dpll & PLL_P2_DIVIDE_BY_4)
  8185. clock.p2 = 4;
  8186. else
  8187. clock.p2 = 2;
  8188. }
  8189. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8190. }
  8191. /*
  8192. * This value includes pixel_multiplier. We will use
  8193. * port_clock to compute adjusted_mode.crtc_clock in the
  8194. * encoder's get_config() function.
  8195. */
  8196. pipe_config->port_clock = port_clock;
  8197. }
  8198. int intel_dotclock_calculate(int link_freq,
  8199. const struct intel_link_m_n *m_n)
  8200. {
  8201. /*
  8202. * The calculation for the data clock is:
  8203. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8204. * But we want to avoid losing precison if possible, so:
  8205. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8206. *
  8207. * and the link clock is simpler:
  8208. * link_clock = (m * link_clock) / n
  8209. */
  8210. if (!m_n->link_n)
  8211. return 0;
  8212. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8213. }
  8214. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8215. struct intel_crtc_state *pipe_config)
  8216. {
  8217. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8218. /* read out port_clock from the DPLL */
  8219. i9xx_crtc_clock_get(crtc, pipe_config);
  8220. /*
  8221. * In case there is an active pipe without active ports,
  8222. * we may need some idea for the dotclock anyway.
  8223. * Calculate one based on the FDI configuration.
  8224. */
  8225. pipe_config->base.adjusted_mode.crtc_clock =
  8226. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8227. &pipe_config->fdi_m_n);
  8228. }
  8229. /** Returns the currently programmed mode of the given pipe. */
  8230. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8231. struct drm_crtc *crtc)
  8232. {
  8233. struct drm_i915_private *dev_priv = to_i915(dev);
  8234. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8235. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8236. struct drm_display_mode *mode;
  8237. struct intel_crtc_state *pipe_config;
  8238. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8239. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8240. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8241. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8242. enum pipe pipe = intel_crtc->pipe;
  8243. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8244. if (!mode)
  8245. return NULL;
  8246. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8247. if (!pipe_config) {
  8248. kfree(mode);
  8249. return NULL;
  8250. }
  8251. /*
  8252. * Construct a pipe_config sufficient for getting the clock info
  8253. * back out of crtc_clock_get.
  8254. *
  8255. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8256. * to use a real value here instead.
  8257. */
  8258. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  8259. pipe_config->pixel_multiplier = 1;
  8260. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8261. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8262. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8263. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  8264. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  8265. mode->hdisplay = (htot & 0xffff) + 1;
  8266. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8267. mode->hsync_start = (hsync & 0xffff) + 1;
  8268. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8269. mode->vdisplay = (vtot & 0xffff) + 1;
  8270. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8271. mode->vsync_start = (vsync & 0xffff) + 1;
  8272. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8273. drm_mode_set_name(mode);
  8274. kfree(pipe_config);
  8275. return mode;
  8276. }
  8277. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8278. {
  8279. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8280. struct drm_device *dev = crtc->dev;
  8281. struct intel_flip_work *work;
  8282. spin_lock_irq(&dev->event_lock);
  8283. work = intel_crtc->flip_work;
  8284. intel_crtc->flip_work = NULL;
  8285. spin_unlock_irq(&dev->event_lock);
  8286. if (work) {
  8287. cancel_work_sync(&work->mmio_work);
  8288. cancel_work_sync(&work->unpin_work);
  8289. kfree(work);
  8290. }
  8291. drm_crtc_cleanup(crtc);
  8292. kfree(intel_crtc);
  8293. }
  8294. static void intel_unpin_work_fn(struct work_struct *__work)
  8295. {
  8296. struct intel_flip_work *work =
  8297. container_of(__work, struct intel_flip_work, unpin_work);
  8298. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8299. struct drm_device *dev = crtc->base.dev;
  8300. struct drm_plane *primary = crtc->base.primary;
  8301. if (is_mmio_work(work))
  8302. flush_work(&work->mmio_work);
  8303. mutex_lock(&dev->struct_mutex);
  8304. intel_unpin_fb_vma(work->old_vma);
  8305. i915_gem_object_put(work->pending_flip_obj);
  8306. mutex_unlock(&dev->struct_mutex);
  8307. i915_gem_request_put(work->flip_queued_req);
  8308. intel_frontbuffer_flip_complete(to_i915(dev),
  8309. to_intel_plane(primary)->frontbuffer_bit);
  8310. intel_fbc_post_update(crtc);
  8311. drm_framebuffer_unreference(work->old_fb);
  8312. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8313. atomic_dec(&crtc->unpin_work_count);
  8314. kfree(work);
  8315. }
  8316. /* Is 'a' after or equal to 'b'? */
  8317. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8318. {
  8319. return !((a - b) & 0x80000000);
  8320. }
  8321. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  8322. struct intel_flip_work *work)
  8323. {
  8324. struct drm_device *dev = crtc->base.dev;
  8325. struct drm_i915_private *dev_priv = to_i915(dev);
  8326. if (abort_flip_on_reset(crtc))
  8327. return true;
  8328. /*
  8329. * The relevant registers doen't exist on pre-ctg.
  8330. * As the flip done interrupt doesn't trigger for mmio
  8331. * flips on gmch platforms, a flip count check isn't
  8332. * really needed there. But since ctg has the registers,
  8333. * include it in the check anyway.
  8334. */
  8335. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8336. return true;
  8337. /*
  8338. * BDW signals flip done immediately if the plane
  8339. * is disabled, even if the plane enable is already
  8340. * armed to occur at the next vblank :(
  8341. */
  8342. /*
  8343. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  8344. * used the same base address. In that case the mmio flip might
  8345. * have completed, but the CS hasn't even executed the flip yet.
  8346. *
  8347. * A flip count check isn't enough as the CS might have updated
  8348. * the base address just after start of vblank, but before we
  8349. * managed to process the interrupt. This means we'd complete the
  8350. * CS flip too soon.
  8351. *
  8352. * Combining both checks should get us a good enough result. It may
  8353. * still happen that the CS flip has been executed, but has not
  8354. * yet actually completed. But in case the base address is the same
  8355. * anyway, we don't really care.
  8356. */
  8357. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  8358. crtc->flip_work->gtt_offset &&
  8359. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  8360. crtc->flip_work->flip_count);
  8361. }
  8362. static bool
  8363. __pageflip_finished_mmio(struct intel_crtc *crtc,
  8364. struct intel_flip_work *work)
  8365. {
  8366. /*
  8367. * MMIO work completes when vblank is different from
  8368. * flip_queued_vblank.
  8369. *
  8370. * Reset counter value doesn't matter, this is handled by
  8371. * i915_wait_request finishing early, so no need to handle
  8372. * reset here.
  8373. */
  8374. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  8375. }
  8376. static bool pageflip_finished(struct intel_crtc *crtc,
  8377. struct intel_flip_work *work)
  8378. {
  8379. if (!atomic_read(&work->pending))
  8380. return false;
  8381. smp_rmb();
  8382. if (is_mmio_work(work))
  8383. return __pageflip_finished_mmio(crtc, work);
  8384. else
  8385. return __pageflip_finished_cs(crtc, work);
  8386. }
  8387. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  8388. {
  8389. struct drm_device *dev = &dev_priv->drm;
  8390. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8391. struct intel_flip_work *work;
  8392. unsigned long flags;
  8393. /* Ignore early vblank irqs */
  8394. if (!crtc)
  8395. return;
  8396. /*
  8397. * This is called both by irq handlers and the reset code (to complete
  8398. * lost pageflips) so needs the full irqsave spinlocks.
  8399. */
  8400. spin_lock_irqsave(&dev->event_lock, flags);
  8401. work = crtc->flip_work;
  8402. if (work != NULL &&
  8403. !is_mmio_work(work) &&
  8404. pageflip_finished(crtc, work))
  8405. page_flip_completed(crtc);
  8406. spin_unlock_irqrestore(&dev->event_lock, flags);
  8407. }
  8408. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  8409. {
  8410. struct drm_device *dev = &dev_priv->drm;
  8411. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8412. struct intel_flip_work *work;
  8413. unsigned long flags;
  8414. /* Ignore early vblank irqs */
  8415. if (!crtc)
  8416. return;
  8417. /*
  8418. * This is called both by irq handlers and the reset code (to complete
  8419. * lost pageflips) so needs the full irqsave spinlocks.
  8420. */
  8421. spin_lock_irqsave(&dev->event_lock, flags);
  8422. work = crtc->flip_work;
  8423. if (work != NULL &&
  8424. is_mmio_work(work) &&
  8425. pageflip_finished(crtc, work))
  8426. page_flip_completed(crtc);
  8427. spin_unlock_irqrestore(&dev->event_lock, flags);
  8428. }
  8429. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  8430. struct intel_flip_work *work)
  8431. {
  8432. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  8433. /* Ensure that the work item is consistent when activating it ... */
  8434. smp_mb__before_atomic();
  8435. atomic_set(&work->pending, 1);
  8436. }
  8437. static int intel_gen2_queue_flip(struct drm_device *dev,
  8438. struct drm_crtc *crtc,
  8439. struct drm_framebuffer *fb,
  8440. struct drm_i915_gem_object *obj,
  8441. struct drm_i915_gem_request *req,
  8442. uint32_t flags)
  8443. {
  8444. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8445. u32 flip_mask, *cs;
  8446. cs = intel_ring_begin(req, 6);
  8447. if (IS_ERR(cs))
  8448. return PTR_ERR(cs);
  8449. /* Can't queue multiple flips, so wait for the previous
  8450. * one to finish before executing the next.
  8451. */
  8452. if (intel_crtc->plane)
  8453. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8454. else
  8455. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8456. *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
  8457. *cs++ = MI_NOOP;
  8458. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8459. *cs++ = fb->pitches[0];
  8460. *cs++ = intel_crtc->flip_work->gtt_offset;
  8461. *cs++ = 0; /* aux display base address, unused */
  8462. return 0;
  8463. }
  8464. static int intel_gen3_queue_flip(struct drm_device *dev,
  8465. struct drm_crtc *crtc,
  8466. struct drm_framebuffer *fb,
  8467. struct drm_i915_gem_object *obj,
  8468. struct drm_i915_gem_request *req,
  8469. uint32_t flags)
  8470. {
  8471. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8472. u32 flip_mask, *cs;
  8473. cs = intel_ring_begin(req, 6);
  8474. if (IS_ERR(cs))
  8475. return PTR_ERR(cs);
  8476. if (intel_crtc->plane)
  8477. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8478. else
  8479. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8480. *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
  8481. *cs++ = MI_NOOP;
  8482. *cs++ = MI_DISPLAY_FLIP_I915 | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8483. *cs++ = fb->pitches[0];
  8484. *cs++ = intel_crtc->flip_work->gtt_offset;
  8485. *cs++ = MI_NOOP;
  8486. return 0;
  8487. }
  8488. static int intel_gen4_queue_flip(struct drm_device *dev,
  8489. struct drm_crtc *crtc,
  8490. struct drm_framebuffer *fb,
  8491. struct drm_i915_gem_object *obj,
  8492. struct drm_i915_gem_request *req,
  8493. uint32_t flags)
  8494. {
  8495. struct drm_i915_private *dev_priv = to_i915(dev);
  8496. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8497. u32 pf, pipesrc, *cs;
  8498. cs = intel_ring_begin(req, 4);
  8499. if (IS_ERR(cs))
  8500. return PTR_ERR(cs);
  8501. /* i965+ uses the linear or tiled offsets from the
  8502. * Display Registers (which do not change across a page-flip)
  8503. * so we need only reprogram the base address.
  8504. */
  8505. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8506. *cs++ = fb->pitches[0];
  8507. *cs++ = intel_crtc->flip_work->gtt_offset |
  8508. intel_fb_modifier_to_tiling(fb->modifier);
  8509. /* XXX Enabling the panel-fitter across page-flip is so far
  8510. * untested on non-native modes, so ignore it for now.
  8511. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  8512. */
  8513. pf = 0;
  8514. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8515. *cs++ = pf | pipesrc;
  8516. return 0;
  8517. }
  8518. static int intel_gen6_queue_flip(struct drm_device *dev,
  8519. struct drm_crtc *crtc,
  8520. struct drm_framebuffer *fb,
  8521. struct drm_i915_gem_object *obj,
  8522. struct drm_i915_gem_request *req,
  8523. uint32_t flags)
  8524. {
  8525. struct drm_i915_private *dev_priv = to_i915(dev);
  8526. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8527. u32 pf, pipesrc, *cs;
  8528. cs = intel_ring_begin(req, 4);
  8529. if (IS_ERR(cs))
  8530. return PTR_ERR(cs);
  8531. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8532. *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
  8533. *cs++ = intel_crtc->flip_work->gtt_offset;
  8534. /* Contrary to the suggestions in the documentation,
  8535. * "Enable Panel Fitter" does not seem to be required when page
  8536. * flipping with a non-native mode, and worse causes a normal
  8537. * modeset to fail.
  8538. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  8539. */
  8540. pf = 0;
  8541. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8542. *cs++ = pf | pipesrc;
  8543. return 0;
  8544. }
  8545. static int intel_gen7_queue_flip(struct drm_device *dev,
  8546. struct drm_crtc *crtc,
  8547. struct drm_framebuffer *fb,
  8548. struct drm_i915_gem_object *obj,
  8549. struct drm_i915_gem_request *req,
  8550. uint32_t flags)
  8551. {
  8552. struct drm_i915_private *dev_priv = to_i915(dev);
  8553. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8554. u32 *cs, plane_bit = 0;
  8555. int len, ret;
  8556. switch (intel_crtc->plane) {
  8557. case PLANE_A:
  8558. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  8559. break;
  8560. case PLANE_B:
  8561. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  8562. break;
  8563. case PLANE_C:
  8564. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  8565. break;
  8566. default:
  8567. WARN_ONCE(1, "unknown plane in flip command\n");
  8568. return -ENODEV;
  8569. }
  8570. len = 4;
  8571. if (req->engine->id == RCS) {
  8572. len += 6;
  8573. /*
  8574. * On Gen 8, SRM is now taking an extra dword to accommodate
  8575. * 48bits addresses, and we need a NOOP for the batch size to
  8576. * stay even.
  8577. */
  8578. if (IS_GEN8(dev_priv))
  8579. len += 2;
  8580. }
  8581. /*
  8582. * BSpec MI_DISPLAY_FLIP for IVB:
  8583. * "The full packet must be contained within the same cache line."
  8584. *
  8585. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8586. * cacheline, if we ever start emitting more commands before
  8587. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8588. * then do the cacheline alignment, and finally emit the
  8589. * MI_DISPLAY_FLIP.
  8590. */
  8591. ret = intel_ring_cacheline_align(req);
  8592. if (ret)
  8593. return ret;
  8594. cs = intel_ring_begin(req, len);
  8595. if (IS_ERR(cs))
  8596. return PTR_ERR(cs);
  8597. /* Unmask the flip-done completion message. Note that the bspec says that
  8598. * we should do this for both the BCS and RCS, and that we must not unmask
  8599. * more than one flip event at any time (or ensure that one flip message
  8600. * can be sent by waiting for flip-done prior to queueing new flips).
  8601. * Experimentation says that BCS works despite DERRMR masking all
  8602. * flip-done completion events and that unmasking all planes at once
  8603. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8604. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8605. */
  8606. if (req->engine->id == RCS) {
  8607. *cs++ = MI_LOAD_REGISTER_IMM(1);
  8608. *cs++ = i915_mmio_reg_offset(DERRMR);
  8609. *cs++ = ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8610. DERRMR_PIPEB_PRI_FLIP_DONE |
  8611. DERRMR_PIPEC_PRI_FLIP_DONE);
  8612. if (IS_GEN8(dev_priv))
  8613. *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
  8614. MI_SRM_LRM_GLOBAL_GTT;
  8615. else
  8616. *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
  8617. *cs++ = i915_mmio_reg_offset(DERRMR);
  8618. *cs++ = i915_ggtt_offset(req->engine->scratch) + 256;
  8619. if (IS_GEN8(dev_priv)) {
  8620. *cs++ = 0;
  8621. *cs++ = MI_NOOP;
  8622. }
  8623. }
  8624. *cs++ = MI_DISPLAY_FLIP_I915 | plane_bit;
  8625. *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
  8626. *cs++ = intel_crtc->flip_work->gtt_offset;
  8627. *cs++ = MI_NOOP;
  8628. return 0;
  8629. }
  8630. static bool use_mmio_flip(struct intel_engine_cs *engine,
  8631. struct drm_i915_gem_object *obj)
  8632. {
  8633. /*
  8634. * This is not being used for older platforms, because
  8635. * non-availability of flip done interrupt forces us to use
  8636. * CS flips. Older platforms derive flip done using some clever
  8637. * tricks involving the flip_pending status bits and vblank irqs.
  8638. * So using MMIO flips there would disrupt this mechanism.
  8639. */
  8640. if (engine == NULL)
  8641. return true;
  8642. if (INTEL_GEN(engine->i915) < 5)
  8643. return false;
  8644. if (i915.use_mmio_flip < 0)
  8645. return false;
  8646. else if (i915.use_mmio_flip > 0)
  8647. return true;
  8648. else if (i915.enable_execlists)
  8649. return true;
  8650. return engine != i915_gem_object_last_write_engine(obj);
  8651. }
  8652. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  8653. unsigned int rotation,
  8654. struct intel_flip_work *work)
  8655. {
  8656. struct drm_device *dev = intel_crtc->base.dev;
  8657. struct drm_i915_private *dev_priv = to_i915(dev);
  8658. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8659. const enum pipe pipe = intel_crtc->pipe;
  8660. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  8661. ctl = I915_READ(PLANE_CTL(pipe, 0));
  8662. ctl &= ~PLANE_CTL_TILED_MASK;
  8663. switch (fb->modifier) {
  8664. case DRM_FORMAT_MOD_LINEAR:
  8665. break;
  8666. case I915_FORMAT_MOD_X_TILED:
  8667. ctl |= PLANE_CTL_TILED_X;
  8668. break;
  8669. case I915_FORMAT_MOD_Y_TILED:
  8670. ctl |= PLANE_CTL_TILED_Y;
  8671. break;
  8672. case I915_FORMAT_MOD_Yf_TILED:
  8673. ctl |= PLANE_CTL_TILED_YF;
  8674. break;
  8675. default:
  8676. MISSING_CASE(fb->modifier);
  8677. }
  8678. /*
  8679. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  8680. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  8681. */
  8682. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  8683. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  8684. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  8685. POSTING_READ(PLANE_SURF(pipe, 0));
  8686. }
  8687. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  8688. struct intel_flip_work *work)
  8689. {
  8690. struct drm_device *dev = intel_crtc->base.dev;
  8691. struct drm_i915_private *dev_priv = to_i915(dev);
  8692. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8693. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  8694. u32 dspcntr;
  8695. dspcntr = I915_READ(reg);
  8696. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  8697. dspcntr |= DISPPLANE_TILED;
  8698. else
  8699. dspcntr &= ~DISPPLANE_TILED;
  8700. I915_WRITE(reg, dspcntr);
  8701. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  8702. POSTING_READ(DSPSURF(intel_crtc->plane));
  8703. }
  8704. static void intel_mmio_flip_work_func(struct work_struct *w)
  8705. {
  8706. struct intel_flip_work *work =
  8707. container_of(w, struct intel_flip_work, mmio_work);
  8708. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8709. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8710. struct intel_framebuffer *intel_fb =
  8711. to_intel_framebuffer(crtc->base.primary->fb);
  8712. struct drm_i915_gem_object *obj = intel_fb->obj;
  8713. WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
  8714. intel_pipe_update_start(crtc);
  8715. if (INTEL_GEN(dev_priv) >= 9)
  8716. skl_do_mmio_flip(crtc, work->rotation, work);
  8717. else
  8718. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  8719. ilk_do_mmio_flip(crtc, work);
  8720. intel_pipe_update_end(crtc, work);
  8721. }
  8722. static int intel_default_queue_flip(struct drm_device *dev,
  8723. struct drm_crtc *crtc,
  8724. struct drm_framebuffer *fb,
  8725. struct drm_i915_gem_object *obj,
  8726. struct drm_i915_gem_request *req,
  8727. uint32_t flags)
  8728. {
  8729. return -ENODEV;
  8730. }
  8731. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  8732. struct intel_crtc *intel_crtc,
  8733. struct intel_flip_work *work)
  8734. {
  8735. u32 addr, vblank;
  8736. if (!atomic_read(&work->pending))
  8737. return false;
  8738. smp_rmb();
  8739. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  8740. if (work->flip_ready_vblank == 0) {
  8741. if (work->flip_queued_req &&
  8742. !i915_gem_request_completed(work->flip_queued_req))
  8743. return false;
  8744. work->flip_ready_vblank = vblank;
  8745. }
  8746. if (vblank - work->flip_ready_vblank < 3)
  8747. return false;
  8748. /* Potential stall - if we see that the flip has happened,
  8749. * assume a missed interrupt. */
  8750. if (INTEL_GEN(dev_priv) >= 4)
  8751. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8752. else
  8753. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8754. /* There is a potential issue here with a false positive after a flip
  8755. * to the same address. We could address this by checking for a
  8756. * non-incrementing frame counter.
  8757. */
  8758. return addr == work->gtt_offset;
  8759. }
  8760. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  8761. {
  8762. struct drm_device *dev = &dev_priv->drm;
  8763. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8764. struct intel_flip_work *work;
  8765. WARN_ON(!in_interrupt());
  8766. if (crtc == NULL)
  8767. return;
  8768. spin_lock(&dev->event_lock);
  8769. work = crtc->flip_work;
  8770. if (work != NULL && !is_mmio_work(work) &&
  8771. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  8772. WARN_ONCE(1,
  8773. "Kicking stuck page flip: queued at %d, now %d\n",
  8774. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  8775. page_flip_completed(crtc);
  8776. work = NULL;
  8777. }
  8778. if (work != NULL && !is_mmio_work(work) &&
  8779. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  8780. intel_queue_rps_boost_for_request(work->flip_queued_req);
  8781. spin_unlock(&dev->event_lock);
  8782. }
  8783. __maybe_unused
  8784. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8785. struct drm_framebuffer *fb,
  8786. struct drm_pending_vblank_event *event,
  8787. uint32_t page_flip_flags)
  8788. {
  8789. struct drm_device *dev = crtc->dev;
  8790. struct drm_i915_private *dev_priv = to_i915(dev);
  8791. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8792. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8793. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8794. struct drm_plane *primary = crtc->primary;
  8795. enum pipe pipe = intel_crtc->pipe;
  8796. struct intel_flip_work *work;
  8797. struct intel_engine_cs *engine;
  8798. bool mmio_flip;
  8799. struct drm_i915_gem_request *request;
  8800. struct i915_vma *vma;
  8801. int ret;
  8802. /*
  8803. * drm_mode_page_flip_ioctl() should already catch this, but double
  8804. * check to be safe. In the future we may enable pageflipping from
  8805. * a disabled primary plane.
  8806. */
  8807. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8808. return -EBUSY;
  8809. /* Can't change pixel format via MI display flips. */
  8810. if (fb->format != crtc->primary->fb->format)
  8811. return -EINVAL;
  8812. /*
  8813. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8814. * Note that pitch changes could also affect these register.
  8815. */
  8816. if (INTEL_GEN(dev_priv) > 3 &&
  8817. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8818. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8819. return -EINVAL;
  8820. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8821. goto out_hang;
  8822. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8823. if (work == NULL)
  8824. return -ENOMEM;
  8825. work->event = event;
  8826. work->crtc = crtc;
  8827. work->old_fb = old_fb;
  8828. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  8829. ret = drm_crtc_vblank_get(crtc);
  8830. if (ret)
  8831. goto free_work;
  8832. /* We borrow the event spin lock for protecting flip_work */
  8833. spin_lock_irq(&dev->event_lock);
  8834. if (intel_crtc->flip_work) {
  8835. /* Before declaring the flip queue wedged, check if
  8836. * the hardware completed the operation behind our backs.
  8837. */
  8838. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  8839. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8840. page_flip_completed(intel_crtc);
  8841. } else {
  8842. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8843. spin_unlock_irq(&dev->event_lock);
  8844. drm_crtc_vblank_put(crtc);
  8845. kfree(work);
  8846. return -EBUSY;
  8847. }
  8848. }
  8849. intel_crtc->flip_work = work;
  8850. spin_unlock_irq(&dev->event_lock);
  8851. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8852. flush_workqueue(dev_priv->wq);
  8853. /* Reference the objects for the scheduled work. */
  8854. drm_framebuffer_reference(work->old_fb);
  8855. crtc->primary->fb = fb;
  8856. update_state_fb(crtc->primary);
  8857. work->pending_flip_obj = i915_gem_object_get(obj);
  8858. ret = i915_mutex_lock_interruptible(dev);
  8859. if (ret)
  8860. goto cleanup;
  8861. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  8862. if (i915_reset_backoff_or_wedged(&dev_priv->gpu_error)) {
  8863. ret = -EIO;
  8864. goto unlock;
  8865. }
  8866. atomic_inc(&intel_crtc->unpin_work_count);
  8867. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  8868. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  8869. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  8870. engine = dev_priv->engine[BCS];
  8871. if (fb->modifier != old_fb->modifier)
  8872. /* vlv: DISPLAY_FLIP fails to change tiling */
  8873. engine = NULL;
  8874. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  8875. engine = dev_priv->engine[BCS];
  8876. } else if (INTEL_GEN(dev_priv) >= 7) {
  8877. engine = i915_gem_object_last_write_engine(obj);
  8878. if (engine == NULL || engine->id != RCS)
  8879. engine = dev_priv->engine[BCS];
  8880. } else {
  8881. engine = dev_priv->engine[RCS];
  8882. }
  8883. mmio_flip = use_mmio_flip(engine, obj);
  8884. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  8885. if (IS_ERR(vma)) {
  8886. ret = PTR_ERR(vma);
  8887. goto cleanup_pending;
  8888. }
  8889. work->old_vma = to_intel_plane_state(primary->state)->vma;
  8890. to_intel_plane_state(primary->state)->vma = vma;
  8891. work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
  8892. work->rotation = crtc->primary->state->rotation;
  8893. /*
  8894. * There's the potential that the next frame will not be compatible with
  8895. * FBC, so we want to call pre_update() before the actual page flip.
  8896. * The problem is that pre_update() caches some information about the fb
  8897. * object, so we want to do this only after the object is pinned. Let's
  8898. * be on the safe side and do this immediately before scheduling the
  8899. * flip.
  8900. */
  8901. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  8902. to_intel_plane_state(primary->state));
  8903. if (mmio_flip) {
  8904. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  8905. queue_work(system_unbound_wq, &work->mmio_work);
  8906. } else {
  8907. request = i915_gem_request_alloc(engine,
  8908. dev_priv->kernel_context);
  8909. if (IS_ERR(request)) {
  8910. ret = PTR_ERR(request);
  8911. goto cleanup_unpin;
  8912. }
  8913. ret = i915_gem_request_await_object(request, obj, false);
  8914. if (ret)
  8915. goto cleanup_request;
  8916. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  8917. page_flip_flags);
  8918. if (ret)
  8919. goto cleanup_request;
  8920. intel_mark_page_flip_active(intel_crtc, work);
  8921. work->flip_queued_req = i915_gem_request_get(request);
  8922. i915_add_request(request);
  8923. }
  8924. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  8925. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  8926. to_intel_plane(primary)->frontbuffer_bit);
  8927. mutex_unlock(&dev->struct_mutex);
  8928. intel_frontbuffer_flip_prepare(to_i915(dev),
  8929. to_intel_plane(primary)->frontbuffer_bit);
  8930. trace_i915_flip_request(intel_crtc->plane, obj);
  8931. return 0;
  8932. cleanup_request:
  8933. i915_add_request(request);
  8934. cleanup_unpin:
  8935. to_intel_plane_state(primary->state)->vma = work->old_vma;
  8936. intel_unpin_fb_vma(vma);
  8937. cleanup_pending:
  8938. atomic_dec(&intel_crtc->unpin_work_count);
  8939. unlock:
  8940. mutex_unlock(&dev->struct_mutex);
  8941. cleanup:
  8942. crtc->primary->fb = old_fb;
  8943. update_state_fb(crtc->primary);
  8944. i915_gem_object_put(obj);
  8945. drm_framebuffer_unreference(work->old_fb);
  8946. spin_lock_irq(&dev->event_lock);
  8947. intel_crtc->flip_work = NULL;
  8948. spin_unlock_irq(&dev->event_lock);
  8949. drm_crtc_vblank_put(crtc);
  8950. free_work:
  8951. kfree(work);
  8952. if (ret == -EIO) {
  8953. struct drm_atomic_state *state;
  8954. struct drm_plane_state *plane_state;
  8955. out_hang:
  8956. state = drm_atomic_state_alloc(dev);
  8957. if (!state)
  8958. return -ENOMEM;
  8959. state->acquire_ctx = dev->mode_config.acquire_ctx;
  8960. retry:
  8961. plane_state = drm_atomic_get_plane_state(state, primary);
  8962. ret = PTR_ERR_OR_ZERO(plane_state);
  8963. if (!ret) {
  8964. drm_atomic_set_fb_for_plane(plane_state, fb);
  8965. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  8966. if (!ret)
  8967. ret = drm_atomic_commit(state);
  8968. }
  8969. if (ret == -EDEADLK) {
  8970. drm_modeset_backoff(state->acquire_ctx);
  8971. drm_atomic_state_clear(state);
  8972. goto retry;
  8973. }
  8974. drm_atomic_state_put(state);
  8975. if (ret == 0 && event) {
  8976. spin_lock_irq(&dev->event_lock);
  8977. drm_crtc_send_vblank_event(crtc, event);
  8978. spin_unlock_irq(&dev->event_lock);
  8979. }
  8980. }
  8981. return ret;
  8982. }
  8983. /**
  8984. * intel_wm_need_update - Check whether watermarks need updating
  8985. * @plane: drm plane
  8986. * @state: new plane state
  8987. *
  8988. * Check current plane state versus the new one to determine whether
  8989. * watermarks need to be recalculated.
  8990. *
  8991. * Returns true or false.
  8992. */
  8993. static bool intel_wm_need_update(struct drm_plane *plane,
  8994. struct drm_plane_state *state)
  8995. {
  8996. struct intel_plane_state *new = to_intel_plane_state(state);
  8997. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8998. /* Update watermarks on tiling or size changes. */
  8999. if (new->base.visible != cur->base.visible)
  9000. return true;
  9001. if (!cur->base.fb || !new->base.fb)
  9002. return false;
  9003. if (cur->base.fb->modifier != new->base.fb->modifier ||
  9004. cur->base.rotation != new->base.rotation ||
  9005. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  9006. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  9007. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  9008. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  9009. return true;
  9010. return false;
  9011. }
  9012. static bool needs_scaling(struct intel_plane_state *state)
  9013. {
  9014. int src_w = drm_rect_width(&state->base.src) >> 16;
  9015. int src_h = drm_rect_height(&state->base.src) >> 16;
  9016. int dst_w = drm_rect_width(&state->base.dst);
  9017. int dst_h = drm_rect_height(&state->base.dst);
  9018. return (src_w != dst_w || src_h != dst_h);
  9019. }
  9020. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9021. struct drm_plane_state *plane_state)
  9022. {
  9023. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9024. struct drm_crtc *crtc = crtc_state->crtc;
  9025. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9026. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  9027. struct drm_device *dev = crtc->dev;
  9028. struct drm_i915_private *dev_priv = to_i915(dev);
  9029. struct intel_plane_state *old_plane_state =
  9030. to_intel_plane_state(plane->base.state);
  9031. bool mode_changed = needs_modeset(crtc_state);
  9032. bool was_crtc_enabled = crtc->state->active;
  9033. bool is_crtc_enabled = crtc_state->active;
  9034. bool turn_off, turn_on, visible, was_visible;
  9035. struct drm_framebuffer *fb = plane_state->fb;
  9036. int ret;
  9037. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  9038. ret = skl_update_scaler_plane(
  9039. to_intel_crtc_state(crtc_state),
  9040. to_intel_plane_state(plane_state));
  9041. if (ret)
  9042. return ret;
  9043. }
  9044. was_visible = old_plane_state->base.visible;
  9045. visible = plane_state->visible;
  9046. if (!was_crtc_enabled && WARN_ON(was_visible))
  9047. was_visible = false;
  9048. /*
  9049. * Visibility is calculated as if the crtc was on, but
  9050. * after scaler setup everything depends on it being off
  9051. * when the crtc isn't active.
  9052. *
  9053. * FIXME this is wrong for watermarks. Watermarks should also
  9054. * be computed as if the pipe would be active. Perhaps move
  9055. * per-plane wm computation to the .check_plane() hook, and
  9056. * only combine the results from all planes in the current place?
  9057. */
  9058. if (!is_crtc_enabled) {
  9059. plane_state->visible = visible = false;
  9060. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  9061. }
  9062. if (!was_visible && !visible)
  9063. return 0;
  9064. if (fb != old_plane_state->base.fb)
  9065. pipe_config->fb_changed = true;
  9066. turn_off = was_visible && (!visible || mode_changed);
  9067. turn_on = visible && (!was_visible || mode_changed);
  9068. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  9069. intel_crtc->base.base.id, intel_crtc->base.name,
  9070. plane->base.base.id, plane->base.name,
  9071. fb ? fb->base.id : -1);
  9072. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  9073. plane->base.base.id, plane->base.name,
  9074. was_visible, visible,
  9075. turn_off, turn_on, mode_changed);
  9076. if (turn_on) {
  9077. if (INTEL_GEN(dev_priv) < 5)
  9078. pipe_config->update_wm_pre = true;
  9079. /* must disable cxsr around plane enable/disable */
  9080. if (plane->id != PLANE_CURSOR)
  9081. pipe_config->disable_cxsr = true;
  9082. } else if (turn_off) {
  9083. if (INTEL_GEN(dev_priv) < 5)
  9084. pipe_config->update_wm_post = true;
  9085. /* must disable cxsr around plane enable/disable */
  9086. if (plane->id != PLANE_CURSOR)
  9087. pipe_config->disable_cxsr = true;
  9088. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  9089. if (INTEL_GEN(dev_priv) < 5) {
  9090. /* FIXME bollocks */
  9091. pipe_config->update_wm_pre = true;
  9092. pipe_config->update_wm_post = true;
  9093. }
  9094. }
  9095. if (visible || was_visible)
  9096. pipe_config->fb_bits |= plane->frontbuffer_bit;
  9097. /*
  9098. * WaCxSRDisabledForSpriteScaling:ivb
  9099. *
  9100. * cstate->update_wm was already set above, so this flag will
  9101. * take effect when we commit and program watermarks.
  9102. */
  9103. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  9104. needs_scaling(to_intel_plane_state(plane_state)) &&
  9105. !needs_scaling(old_plane_state))
  9106. pipe_config->disable_lp_wm = true;
  9107. return 0;
  9108. }
  9109. static bool encoders_cloneable(const struct intel_encoder *a,
  9110. const struct intel_encoder *b)
  9111. {
  9112. /* masks could be asymmetric, so check both ways */
  9113. return a == b || (a->cloneable & (1 << b->type) &&
  9114. b->cloneable & (1 << a->type));
  9115. }
  9116. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9117. struct intel_crtc *crtc,
  9118. struct intel_encoder *encoder)
  9119. {
  9120. struct intel_encoder *source_encoder;
  9121. struct drm_connector *connector;
  9122. struct drm_connector_state *connector_state;
  9123. int i;
  9124. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9125. if (connector_state->crtc != &crtc->base)
  9126. continue;
  9127. source_encoder =
  9128. to_intel_encoder(connector_state->best_encoder);
  9129. if (!encoders_cloneable(encoder, source_encoder))
  9130. return false;
  9131. }
  9132. return true;
  9133. }
  9134. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9135. struct drm_crtc_state *crtc_state)
  9136. {
  9137. struct drm_device *dev = crtc->dev;
  9138. struct drm_i915_private *dev_priv = to_i915(dev);
  9139. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9140. struct intel_crtc_state *pipe_config =
  9141. to_intel_crtc_state(crtc_state);
  9142. struct drm_atomic_state *state = crtc_state->state;
  9143. int ret;
  9144. bool mode_changed = needs_modeset(crtc_state);
  9145. if (mode_changed && !crtc_state->active)
  9146. pipe_config->update_wm_post = true;
  9147. if (mode_changed && crtc_state->enable &&
  9148. dev_priv->display.crtc_compute_clock &&
  9149. !WARN_ON(pipe_config->shared_dpll)) {
  9150. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9151. pipe_config);
  9152. if (ret)
  9153. return ret;
  9154. }
  9155. if (crtc_state->color_mgmt_changed) {
  9156. ret = intel_color_check(crtc, crtc_state);
  9157. if (ret)
  9158. return ret;
  9159. /*
  9160. * Changing color management on Intel hardware is
  9161. * handled as part of planes update.
  9162. */
  9163. crtc_state->planes_changed = true;
  9164. }
  9165. ret = 0;
  9166. if (dev_priv->display.compute_pipe_wm) {
  9167. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  9168. if (ret) {
  9169. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  9170. return ret;
  9171. }
  9172. }
  9173. if (dev_priv->display.compute_intermediate_wm &&
  9174. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  9175. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  9176. return 0;
  9177. /*
  9178. * Calculate 'intermediate' watermarks that satisfy both the
  9179. * old state and the new state. We can program these
  9180. * immediately.
  9181. */
  9182. ret = dev_priv->display.compute_intermediate_wm(dev,
  9183. intel_crtc,
  9184. pipe_config);
  9185. if (ret) {
  9186. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  9187. return ret;
  9188. }
  9189. } else if (dev_priv->display.compute_intermediate_wm) {
  9190. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  9191. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  9192. }
  9193. if (INTEL_GEN(dev_priv) >= 9) {
  9194. if (mode_changed)
  9195. ret = skl_update_scaler_crtc(pipe_config);
  9196. if (!ret)
  9197. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  9198. pipe_config);
  9199. }
  9200. return ret;
  9201. }
  9202. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9203. .atomic_begin = intel_begin_crtc_commit,
  9204. .atomic_flush = intel_finish_crtc_commit,
  9205. .atomic_check = intel_crtc_atomic_check,
  9206. };
  9207. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9208. {
  9209. struct intel_connector *connector;
  9210. struct drm_connector_list_iter conn_iter;
  9211. drm_connector_list_iter_begin(dev, &conn_iter);
  9212. for_each_intel_connector_iter(connector, &conn_iter) {
  9213. if (connector->base.state->crtc)
  9214. drm_connector_unreference(&connector->base);
  9215. if (connector->base.encoder) {
  9216. connector->base.state->best_encoder =
  9217. connector->base.encoder;
  9218. connector->base.state->crtc =
  9219. connector->base.encoder->crtc;
  9220. drm_connector_reference(&connector->base);
  9221. } else {
  9222. connector->base.state->best_encoder = NULL;
  9223. connector->base.state->crtc = NULL;
  9224. }
  9225. }
  9226. drm_connector_list_iter_end(&conn_iter);
  9227. }
  9228. static void
  9229. connected_sink_compute_bpp(struct intel_connector *connector,
  9230. struct intel_crtc_state *pipe_config)
  9231. {
  9232. const struct drm_display_info *info = &connector->base.display_info;
  9233. int bpp = pipe_config->pipe_bpp;
  9234. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9235. connector->base.base.id,
  9236. connector->base.name);
  9237. /* Don't use an invalid EDID bpc value */
  9238. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  9239. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9240. bpp, info->bpc * 3);
  9241. pipe_config->pipe_bpp = info->bpc * 3;
  9242. }
  9243. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9244. if (info->bpc == 0 && bpp > 24) {
  9245. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9246. bpp);
  9247. pipe_config->pipe_bpp = 24;
  9248. }
  9249. }
  9250. static int
  9251. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9252. struct intel_crtc_state *pipe_config)
  9253. {
  9254. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9255. struct drm_atomic_state *state;
  9256. struct drm_connector *connector;
  9257. struct drm_connector_state *connector_state;
  9258. int bpp, i;
  9259. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  9260. IS_CHERRYVIEW(dev_priv)))
  9261. bpp = 10*3;
  9262. else if (INTEL_GEN(dev_priv) >= 5)
  9263. bpp = 12*3;
  9264. else
  9265. bpp = 8*3;
  9266. pipe_config->pipe_bpp = bpp;
  9267. state = pipe_config->base.state;
  9268. /* Clamp display bpp to EDID value */
  9269. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9270. if (connector_state->crtc != &crtc->base)
  9271. continue;
  9272. connected_sink_compute_bpp(to_intel_connector(connector),
  9273. pipe_config);
  9274. }
  9275. return bpp;
  9276. }
  9277. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9278. {
  9279. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9280. "type: 0x%x flags: 0x%x\n",
  9281. mode->crtc_clock,
  9282. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9283. mode->crtc_hsync_end, mode->crtc_htotal,
  9284. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9285. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9286. }
  9287. static inline void
  9288. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  9289. unsigned int lane_count, struct intel_link_m_n *m_n)
  9290. {
  9291. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9292. id, lane_count,
  9293. m_n->gmch_m, m_n->gmch_n,
  9294. m_n->link_m, m_n->link_n, m_n->tu);
  9295. }
  9296. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9297. struct intel_crtc_state *pipe_config,
  9298. const char *context)
  9299. {
  9300. struct drm_device *dev = crtc->base.dev;
  9301. struct drm_i915_private *dev_priv = to_i915(dev);
  9302. struct drm_plane *plane;
  9303. struct intel_plane *intel_plane;
  9304. struct intel_plane_state *state;
  9305. struct drm_framebuffer *fb;
  9306. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  9307. crtc->base.base.id, crtc->base.name, context);
  9308. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  9309. transcoder_name(pipe_config->cpu_transcoder),
  9310. pipe_config->pipe_bpp, pipe_config->dither);
  9311. if (pipe_config->has_pch_encoder)
  9312. intel_dump_m_n_config(pipe_config, "fdi",
  9313. pipe_config->fdi_lanes,
  9314. &pipe_config->fdi_m_n);
  9315. if (intel_crtc_has_dp_encoder(pipe_config)) {
  9316. intel_dump_m_n_config(pipe_config, "dp m_n",
  9317. pipe_config->lane_count, &pipe_config->dp_m_n);
  9318. if (pipe_config->has_drrs)
  9319. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  9320. pipe_config->lane_count,
  9321. &pipe_config->dp_m2_n2);
  9322. }
  9323. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9324. pipe_config->has_audio, pipe_config->has_infoframe);
  9325. DRM_DEBUG_KMS("requested mode:\n");
  9326. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9327. DRM_DEBUG_KMS("adjusted mode:\n");
  9328. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9329. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9330. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  9331. pipe_config->port_clock,
  9332. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  9333. pipe_config->pixel_rate);
  9334. if (INTEL_GEN(dev_priv) >= 9)
  9335. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9336. crtc->num_scalers,
  9337. pipe_config->scaler_state.scaler_users,
  9338. pipe_config->scaler_state.scaler_id);
  9339. if (HAS_GMCH_DISPLAY(dev_priv))
  9340. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9341. pipe_config->gmch_pfit.control,
  9342. pipe_config->gmch_pfit.pgm_ratios,
  9343. pipe_config->gmch_pfit.lvds_border_bits);
  9344. else
  9345. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9346. pipe_config->pch_pfit.pos,
  9347. pipe_config->pch_pfit.size,
  9348. enableddisabled(pipe_config->pch_pfit.enabled));
  9349. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  9350. pipe_config->ips_enabled, pipe_config->double_wide);
  9351. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  9352. DRM_DEBUG_KMS("planes on this crtc\n");
  9353. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9354. struct drm_format_name_buf format_name;
  9355. intel_plane = to_intel_plane(plane);
  9356. if (intel_plane->pipe != crtc->pipe)
  9357. continue;
  9358. state = to_intel_plane_state(plane->state);
  9359. fb = state->base.fb;
  9360. if (!fb) {
  9361. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  9362. plane->base.id, plane->name, state->scaler_id);
  9363. continue;
  9364. }
  9365. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  9366. plane->base.id, plane->name,
  9367. fb->base.id, fb->width, fb->height,
  9368. drm_get_format_name(fb->format->format, &format_name));
  9369. if (INTEL_GEN(dev_priv) >= 9)
  9370. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  9371. state->scaler_id,
  9372. state->base.src.x1 >> 16,
  9373. state->base.src.y1 >> 16,
  9374. drm_rect_width(&state->base.src) >> 16,
  9375. drm_rect_height(&state->base.src) >> 16,
  9376. state->base.dst.x1, state->base.dst.y1,
  9377. drm_rect_width(&state->base.dst),
  9378. drm_rect_height(&state->base.dst));
  9379. }
  9380. }
  9381. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9382. {
  9383. struct drm_device *dev = state->dev;
  9384. struct drm_connector *connector;
  9385. unsigned int used_ports = 0;
  9386. unsigned int used_mst_ports = 0;
  9387. /*
  9388. * Walk the connector list instead of the encoder
  9389. * list to detect the problem on ddi platforms
  9390. * where there's just one encoder per digital port.
  9391. */
  9392. drm_for_each_connector(connector, dev) {
  9393. struct drm_connector_state *connector_state;
  9394. struct intel_encoder *encoder;
  9395. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  9396. if (!connector_state)
  9397. connector_state = connector->state;
  9398. if (!connector_state->best_encoder)
  9399. continue;
  9400. encoder = to_intel_encoder(connector_state->best_encoder);
  9401. WARN_ON(!connector_state->crtc);
  9402. switch (encoder->type) {
  9403. unsigned int port_mask;
  9404. case INTEL_OUTPUT_UNKNOWN:
  9405. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9406. break;
  9407. case INTEL_OUTPUT_DP:
  9408. case INTEL_OUTPUT_HDMI:
  9409. case INTEL_OUTPUT_EDP:
  9410. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9411. /* the same port mustn't appear more than once */
  9412. if (used_ports & port_mask)
  9413. return false;
  9414. used_ports |= port_mask;
  9415. break;
  9416. case INTEL_OUTPUT_DP_MST:
  9417. used_mst_ports |=
  9418. 1 << enc_to_mst(&encoder->base)->primary->port;
  9419. break;
  9420. default:
  9421. break;
  9422. }
  9423. }
  9424. /* can't mix MST and SST/HDMI on the same port */
  9425. if (used_ports & used_mst_ports)
  9426. return false;
  9427. return true;
  9428. }
  9429. static void
  9430. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9431. {
  9432. struct drm_i915_private *dev_priv =
  9433. to_i915(crtc_state->base.crtc->dev);
  9434. struct intel_crtc_scaler_state scaler_state;
  9435. struct intel_dpll_hw_state dpll_hw_state;
  9436. struct intel_shared_dpll *shared_dpll;
  9437. struct intel_crtc_wm_state wm_state;
  9438. bool force_thru;
  9439. /* FIXME: before the switch to atomic started, a new pipe_config was
  9440. * kzalloc'd. Code that depends on any field being zero should be
  9441. * fixed, so that the crtc_state can be safely duplicated. For now,
  9442. * only fields that are know to not cause problems are preserved. */
  9443. scaler_state = crtc_state->scaler_state;
  9444. shared_dpll = crtc_state->shared_dpll;
  9445. dpll_hw_state = crtc_state->dpll_hw_state;
  9446. force_thru = crtc_state->pch_pfit.force_thru;
  9447. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9448. wm_state = crtc_state->wm;
  9449. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9450. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9451. memset(&crtc_state->base + 1, 0,
  9452. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9453. crtc_state->scaler_state = scaler_state;
  9454. crtc_state->shared_dpll = shared_dpll;
  9455. crtc_state->dpll_hw_state = dpll_hw_state;
  9456. crtc_state->pch_pfit.force_thru = force_thru;
  9457. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9458. crtc_state->wm = wm_state;
  9459. }
  9460. static int
  9461. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9462. struct intel_crtc_state *pipe_config)
  9463. {
  9464. struct drm_atomic_state *state = pipe_config->base.state;
  9465. struct intel_encoder *encoder;
  9466. struct drm_connector *connector;
  9467. struct drm_connector_state *connector_state;
  9468. int base_bpp, ret = -EINVAL;
  9469. int i;
  9470. bool retry = true;
  9471. clear_intel_crtc_state(pipe_config);
  9472. pipe_config->cpu_transcoder =
  9473. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9474. /*
  9475. * Sanitize sync polarity flags based on requested ones. If neither
  9476. * positive or negative polarity is requested, treat this as meaning
  9477. * negative polarity.
  9478. */
  9479. if (!(pipe_config->base.adjusted_mode.flags &
  9480. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9481. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9482. if (!(pipe_config->base.adjusted_mode.flags &
  9483. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9484. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9485. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9486. pipe_config);
  9487. if (base_bpp < 0)
  9488. goto fail;
  9489. /*
  9490. * Determine the real pipe dimensions. Note that stereo modes can
  9491. * increase the actual pipe size due to the frame doubling and
  9492. * insertion of additional space for blanks between the frame. This
  9493. * is stored in the crtc timings. We use the requested mode to do this
  9494. * computation to clearly distinguish it from the adjusted mode, which
  9495. * can be changed by the connectors in the below retry loop.
  9496. */
  9497. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9498. &pipe_config->pipe_src_w,
  9499. &pipe_config->pipe_src_h);
  9500. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9501. if (connector_state->crtc != crtc)
  9502. continue;
  9503. encoder = to_intel_encoder(connector_state->best_encoder);
  9504. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9505. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9506. goto fail;
  9507. }
  9508. /*
  9509. * Determine output_types before calling the .compute_config()
  9510. * hooks so that the hooks can use this information safely.
  9511. */
  9512. pipe_config->output_types |= 1 << encoder->type;
  9513. }
  9514. encoder_retry:
  9515. /* Ensure the port clock defaults are reset when retrying. */
  9516. pipe_config->port_clock = 0;
  9517. pipe_config->pixel_multiplier = 1;
  9518. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9519. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9520. CRTC_STEREO_DOUBLE);
  9521. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9522. * adjust it according to limitations or connector properties, and also
  9523. * a chance to reject the mode entirely.
  9524. */
  9525. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9526. if (connector_state->crtc != crtc)
  9527. continue;
  9528. encoder = to_intel_encoder(connector_state->best_encoder);
  9529. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9530. DRM_DEBUG_KMS("Encoder config failure\n");
  9531. goto fail;
  9532. }
  9533. }
  9534. /* Set default port clock if not overwritten by the encoder. Needs to be
  9535. * done afterwards in case the encoder adjusts the mode. */
  9536. if (!pipe_config->port_clock)
  9537. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9538. * pipe_config->pixel_multiplier;
  9539. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9540. if (ret < 0) {
  9541. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9542. goto fail;
  9543. }
  9544. if (ret == RETRY) {
  9545. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9546. ret = -EINVAL;
  9547. goto fail;
  9548. }
  9549. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9550. retry = false;
  9551. goto encoder_retry;
  9552. }
  9553. /* Dithering seems to not pass-through bits correctly when it should, so
  9554. * only enable it on 6bpc panels and when its not a compliance
  9555. * test requesting 6bpc video pattern.
  9556. */
  9557. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9558. !pipe_config->dither_force_disable;
  9559. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9560. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9561. fail:
  9562. return ret;
  9563. }
  9564. static void
  9565. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  9566. {
  9567. struct drm_crtc *crtc;
  9568. struct drm_crtc_state *new_crtc_state;
  9569. int i;
  9570. /* Double check state. */
  9571. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  9572. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  9573. /*
  9574. * Update legacy state to satisfy fbc code. This can
  9575. * be removed when fbc uses the atomic state.
  9576. */
  9577. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  9578. struct drm_plane_state *plane_state = crtc->primary->state;
  9579. crtc->primary->fb = plane_state->fb;
  9580. crtc->x = plane_state->src_x >> 16;
  9581. crtc->y = plane_state->src_y >> 16;
  9582. }
  9583. }
  9584. }
  9585. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9586. {
  9587. int diff;
  9588. if (clock1 == clock2)
  9589. return true;
  9590. if (!clock1 || !clock2)
  9591. return false;
  9592. diff = abs(clock1 - clock2);
  9593. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9594. return true;
  9595. return false;
  9596. }
  9597. static bool
  9598. intel_compare_m_n(unsigned int m, unsigned int n,
  9599. unsigned int m2, unsigned int n2,
  9600. bool exact)
  9601. {
  9602. if (m == m2 && n == n2)
  9603. return true;
  9604. if (exact || !m || !n || !m2 || !n2)
  9605. return false;
  9606. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9607. if (n > n2) {
  9608. while (n > n2) {
  9609. m2 <<= 1;
  9610. n2 <<= 1;
  9611. }
  9612. } else if (n < n2) {
  9613. while (n < n2) {
  9614. m <<= 1;
  9615. n <<= 1;
  9616. }
  9617. }
  9618. if (n != n2)
  9619. return false;
  9620. return intel_fuzzy_clock_check(m, m2);
  9621. }
  9622. static bool
  9623. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9624. struct intel_link_m_n *m2_n2,
  9625. bool adjust)
  9626. {
  9627. if (m_n->tu == m2_n2->tu &&
  9628. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9629. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9630. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9631. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9632. if (adjust)
  9633. *m2_n2 = *m_n;
  9634. return true;
  9635. }
  9636. return false;
  9637. }
  9638. static void __printf(3, 4)
  9639. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9640. {
  9641. char *level;
  9642. unsigned int category;
  9643. struct va_format vaf;
  9644. va_list args;
  9645. if (adjust) {
  9646. level = KERN_DEBUG;
  9647. category = DRM_UT_KMS;
  9648. } else {
  9649. level = KERN_ERR;
  9650. category = DRM_UT_NONE;
  9651. }
  9652. va_start(args, format);
  9653. vaf.fmt = format;
  9654. vaf.va = &args;
  9655. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  9656. va_end(args);
  9657. }
  9658. static bool
  9659. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9660. struct intel_crtc_state *current_config,
  9661. struct intel_crtc_state *pipe_config,
  9662. bool adjust)
  9663. {
  9664. bool ret = true;
  9665. #define PIPE_CONF_CHECK_X(name) \
  9666. if (current_config->name != pipe_config->name) { \
  9667. pipe_config_err(adjust, __stringify(name), \
  9668. "(expected 0x%08x, found 0x%08x)\n", \
  9669. current_config->name, \
  9670. pipe_config->name); \
  9671. ret = false; \
  9672. }
  9673. #define PIPE_CONF_CHECK_I(name) \
  9674. if (current_config->name != pipe_config->name) { \
  9675. pipe_config_err(adjust, __stringify(name), \
  9676. "(expected %i, found %i)\n", \
  9677. current_config->name, \
  9678. pipe_config->name); \
  9679. ret = false; \
  9680. }
  9681. #define PIPE_CONF_CHECK_P(name) \
  9682. if (current_config->name != pipe_config->name) { \
  9683. pipe_config_err(adjust, __stringify(name), \
  9684. "(expected %p, found %p)\n", \
  9685. current_config->name, \
  9686. pipe_config->name); \
  9687. ret = false; \
  9688. }
  9689. #define PIPE_CONF_CHECK_M_N(name) \
  9690. if (!intel_compare_link_m_n(&current_config->name, \
  9691. &pipe_config->name,\
  9692. adjust)) { \
  9693. pipe_config_err(adjust, __stringify(name), \
  9694. "(expected tu %i gmch %i/%i link %i/%i, " \
  9695. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9696. current_config->name.tu, \
  9697. current_config->name.gmch_m, \
  9698. current_config->name.gmch_n, \
  9699. current_config->name.link_m, \
  9700. current_config->name.link_n, \
  9701. pipe_config->name.tu, \
  9702. pipe_config->name.gmch_m, \
  9703. pipe_config->name.gmch_n, \
  9704. pipe_config->name.link_m, \
  9705. pipe_config->name.link_n); \
  9706. ret = false; \
  9707. }
  9708. /* This is required for BDW+ where there is only one set of registers for
  9709. * switching between high and low RR.
  9710. * This macro can be used whenever a comparison has to be made between one
  9711. * hw state and multiple sw state variables.
  9712. */
  9713. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9714. if (!intel_compare_link_m_n(&current_config->name, \
  9715. &pipe_config->name, adjust) && \
  9716. !intel_compare_link_m_n(&current_config->alt_name, \
  9717. &pipe_config->name, adjust)) { \
  9718. pipe_config_err(adjust, __stringify(name), \
  9719. "(expected tu %i gmch %i/%i link %i/%i, " \
  9720. "or tu %i gmch %i/%i link %i/%i, " \
  9721. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9722. current_config->name.tu, \
  9723. current_config->name.gmch_m, \
  9724. current_config->name.gmch_n, \
  9725. current_config->name.link_m, \
  9726. current_config->name.link_n, \
  9727. current_config->alt_name.tu, \
  9728. current_config->alt_name.gmch_m, \
  9729. current_config->alt_name.gmch_n, \
  9730. current_config->alt_name.link_m, \
  9731. current_config->alt_name.link_n, \
  9732. pipe_config->name.tu, \
  9733. pipe_config->name.gmch_m, \
  9734. pipe_config->name.gmch_n, \
  9735. pipe_config->name.link_m, \
  9736. pipe_config->name.link_n); \
  9737. ret = false; \
  9738. }
  9739. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9740. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9741. pipe_config_err(adjust, __stringify(name), \
  9742. "(%x) (expected %i, found %i)\n", \
  9743. (mask), \
  9744. current_config->name & (mask), \
  9745. pipe_config->name & (mask)); \
  9746. ret = false; \
  9747. }
  9748. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9749. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9750. pipe_config_err(adjust, __stringify(name), \
  9751. "(expected %i, found %i)\n", \
  9752. current_config->name, \
  9753. pipe_config->name); \
  9754. ret = false; \
  9755. }
  9756. #define PIPE_CONF_QUIRK(quirk) \
  9757. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9758. PIPE_CONF_CHECK_I(cpu_transcoder);
  9759. PIPE_CONF_CHECK_I(has_pch_encoder);
  9760. PIPE_CONF_CHECK_I(fdi_lanes);
  9761. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9762. PIPE_CONF_CHECK_I(lane_count);
  9763. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9764. if (INTEL_GEN(dev_priv) < 8) {
  9765. PIPE_CONF_CHECK_M_N(dp_m_n);
  9766. if (current_config->has_drrs)
  9767. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9768. } else
  9769. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9770. PIPE_CONF_CHECK_X(output_types);
  9771. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9772. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9773. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9774. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9775. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9776. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9777. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9778. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9779. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9780. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9781. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9782. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9783. PIPE_CONF_CHECK_I(pixel_multiplier);
  9784. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9785. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9786. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9787. PIPE_CONF_CHECK_I(limited_color_range);
  9788. PIPE_CONF_CHECK_I(hdmi_scrambling);
  9789. PIPE_CONF_CHECK_I(hdmi_high_tmds_clock_ratio);
  9790. PIPE_CONF_CHECK_I(has_infoframe);
  9791. PIPE_CONF_CHECK_I(has_audio);
  9792. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9793. DRM_MODE_FLAG_INTERLACE);
  9794. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9795. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9796. DRM_MODE_FLAG_PHSYNC);
  9797. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9798. DRM_MODE_FLAG_NHSYNC);
  9799. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9800. DRM_MODE_FLAG_PVSYNC);
  9801. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9802. DRM_MODE_FLAG_NVSYNC);
  9803. }
  9804. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9805. /* pfit ratios are autocomputed by the hw on gen4+ */
  9806. if (INTEL_GEN(dev_priv) < 4)
  9807. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9808. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9809. if (!adjust) {
  9810. PIPE_CONF_CHECK_I(pipe_src_w);
  9811. PIPE_CONF_CHECK_I(pipe_src_h);
  9812. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9813. if (current_config->pch_pfit.enabled) {
  9814. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9815. PIPE_CONF_CHECK_X(pch_pfit.size);
  9816. }
  9817. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9818. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9819. }
  9820. /* BDW+ don't expose a synchronous way to read the state */
  9821. if (IS_HASWELL(dev_priv))
  9822. PIPE_CONF_CHECK_I(ips_enabled);
  9823. PIPE_CONF_CHECK_I(double_wide);
  9824. PIPE_CONF_CHECK_P(shared_dpll);
  9825. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9826. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9827. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9828. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9829. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9830. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9831. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9832. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9833. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9834. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9835. PIPE_CONF_CHECK_X(dsi_pll.div);
  9836. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9837. PIPE_CONF_CHECK_I(pipe_bpp);
  9838. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9839. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9840. #undef PIPE_CONF_CHECK_X
  9841. #undef PIPE_CONF_CHECK_I
  9842. #undef PIPE_CONF_CHECK_P
  9843. #undef PIPE_CONF_CHECK_FLAGS
  9844. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9845. #undef PIPE_CONF_QUIRK
  9846. return ret;
  9847. }
  9848. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9849. const struct intel_crtc_state *pipe_config)
  9850. {
  9851. if (pipe_config->has_pch_encoder) {
  9852. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9853. &pipe_config->fdi_m_n);
  9854. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9855. /*
  9856. * FDI already provided one idea for the dotclock.
  9857. * Yell if the encoder disagrees.
  9858. */
  9859. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9860. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9861. fdi_dotclock, dotclock);
  9862. }
  9863. }
  9864. static void verify_wm_state(struct drm_crtc *crtc,
  9865. struct drm_crtc_state *new_state)
  9866. {
  9867. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9868. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9869. struct skl_pipe_wm hw_wm, *sw_wm;
  9870. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9871. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9872. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9873. const enum pipe pipe = intel_crtc->pipe;
  9874. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9875. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9876. return;
  9877. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9878. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9879. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9880. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9881. /* planes */
  9882. for_each_universal_plane(dev_priv, pipe, plane) {
  9883. hw_plane_wm = &hw_wm.planes[plane];
  9884. sw_plane_wm = &sw_wm->planes[plane];
  9885. /* Watermarks */
  9886. for (level = 0; level <= max_level; level++) {
  9887. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9888. &sw_plane_wm->wm[level]))
  9889. continue;
  9890. 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",
  9891. pipe_name(pipe), plane + 1, level,
  9892. sw_plane_wm->wm[level].plane_en,
  9893. sw_plane_wm->wm[level].plane_res_b,
  9894. sw_plane_wm->wm[level].plane_res_l,
  9895. hw_plane_wm->wm[level].plane_en,
  9896. hw_plane_wm->wm[level].plane_res_b,
  9897. hw_plane_wm->wm[level].plane_res_l);
  9898. }
  9899. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9900. &sw_plane_wm->trans_wm)) {
  9901. 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",
  9902. pipe_name(pipe), plane + 1,
  9903. sw_plane_wm->trans_wm.plane_en,
  9904. sw_plane_wm->trans_wm.plane_res_b,
  9905. sw_plane_wm->trans_wm.plane_res_l,
  9906. hw_plane_wm->trans_wm.plane_en,
  9907. hw_plane_wm->trans_wm.plane_res_b,
  9908. hw_plane_wm->trans_wm.plane_res_l);
  9909. }
  9910. /* DDB */
  9911. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9912. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9913. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9914. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9915. pipe_name(pipe), plane + 1,
  9916. sw_ddb_entry->start, sw_ddb_entry->end,
  9917. hw_ddb_entry->start, hw_ddb_entry->end);
  9918. }
  9919. }
  9920. /*
  9921. * cursor
  9922. * If the cursor plane isn't active, we may not have updated it's ddb
  9923. * allocation. In that case since the ddb allocation will be updated
  9924. * once the plane becomes visible, we can skip this check
  9925. */
  9926. if (intel_crtc->cursor_addr) {
  9927. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9928. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9929. /* Watermarks */
  9930. for (level = 0; level <= max_level; level++) {
  9931. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9932. &sw_plane_wm->wm[level]))
  9933. continue;
  9934. 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",
  9935. pipe_name(pipe), level,
  9936. sw_plane_wm->wm[level].plane_en,
  9937. sw_plane_wm->wm[level].plane_res_b,
  9938. sw_plane_wm->wm[level].plane_res_l,
  9939. hw_plane_wm->wm[level].plane_en,
  9940. hw_plane_wm->wm[level].plane_res_b,
  9941. hw_plane_wm->wm[level].plane_res_l);
  9942. }
  9943. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9944. &sw_plane_wm->trans_wm)) {
  9945. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9946. pipe_name(pipe),
  9947. sw_plane_wm->trans_wm.plane_en,
  9948. sw_plane_wm->trans_wm.plane_res_b,
  9949. sw_plane_wm->trans_wm.plane_res_l,
  9950. hw_plane_wm->trans_wm.plane_en,
  9951. hw_plane_wm->trans_wm.plane_res_b,
  9952. hw_plane_wm->trans_wm.plane_res_l);
  9953. }
  9954. /* DDB */
  9955. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9956. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9957. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9958. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9959. pipe_name(pipe),
  9960. sw_ddb_entry->start, sw_ddb_entry->end,
  9961. hw_ddb_entry->start, hw_ddb_entry->end);
  9962. }
  9963. }
  9964. }
  9965. static void
  9966. verify_connector_state(struct drm_device *dev,
  9967. struct drm_atomic_state *state,
  9968. struct drm_crtc *crtc)
  9969. {
  9970. struct drm_connector *connector;
  9971. struct drm_connector_state *new_conn_state;
  9972. int i;
  9973. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9974. struct drm_encoder *encoder = connector->encoder;
  9975. if (new_conn_state->crtc != crtc)
  9976. continue;
  9977. intel_connector_verify_state(to_intel_connector(connector));
  9978. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9979. "connector's atomic encoder doesn't match legacy encoder\n");
  9980. }
  9981. }
  9982. static void
  9983. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9984. {
  9985. struct intel_encoder *encoder;
  9986. struct drm_connector *connector;
  9987. struct drm_connector_state *old_conn_state, *new_conn_state;
  9988. int i;
  9989. for_each_intel_encoder(dev, encoder) {
  9990. bool enabled = false, found = false;
  9991. enum pipe pipe;
  9992. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9993. encoder->base.base.id,
  9994. encoder->base.name);
  9995. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  9996. new_conn_state, i) {
  9997. if (old_conn_state->best_encoder == &encoder->base)
  9998. found = true;
  9999. if (new_conn_state->best_encoder != &encoder->base)
  10000. continue;
  10001. found = enabled = true;
  10002. I915_STATE_WARN(new_conn_state->crtc !=
  10003. encoder->base.crtc,
  10004. "connector's crtc doesn't match encoder crtc\n");
  10005. }
  10006. if (!found)
  10007. continue;
  10008. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10009. "encoder's enabled state mismatch "
  10010. "(expected %i, found %i)\n",
  10011. !!encoder->base.crtc, enabled);
  10012. if (!encoder->base.crtc) {
  10013. bool active;
  10014. active = encoder->get_hw_state(encoder, &pipe);
  10015. I915_STATE_WARN(active,
  10016. "encoder detached but still enabled on pipe %c.\n",
  10017. pipe_name(pipe));
  10018. }
  10019. }
  10020. }
  10021. static void
  10022. verify_crtc_state(struct drm_crtc *crtc,
  10023. struct drm_crtc_state *old_crtc_state,
  10024. struct drm_crtc_state *new_crtc_state)
  10025. {
  10026. struct drm_device *dev = crtc->dev;
  10027. struct drm_i915_private *dev_priv = to_i915(dev);
  10028. struct intel_encoder *encoder;
  10029. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10030. struct intel_crtc_state *pipe_config, *sw_config;
  10031. struct drm_atomic_state *old_state;
  10032. bool active;
  10033. old_state = old_crtc_state->state;
  10034. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10035. pipe_config = to_intel_crtc_state(old_crtc_state);
  10036. memset(pipe_config, 0, sizeof(*pipe_config));
  10037. pipe_config->base.crtc = crtc;
  10038. pipe_config->base.state = old_state;
  10039. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  10040. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10041. /* hw state is inconsistent with the pipe quirk */
  10042. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10043. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10044. active = new_crtc_state->active;
  10045. I915_STATE_WARN(new_crtc_state->active != active,
  10046. "crtc active state doesn't match with hw state "
  10047. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10048. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10049. "transitional active state does not match atomic hw state "
  10050. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10051. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10052. enum pipe pipe;
  10053. active = encoder->get_hw_state(encoder, &pipe);
  10054. I915_STATE_WARN(active != new_crtc_state->active,
  10055. "[ENCODER:%i] active %i with crtc active %i\n",
  10056. encoder->base.base.id, active, new_crtc_state->active);
  10057. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10058. "Encoder connected to wrong pipe %c\n",
  10059. pipe_name(pipe));
  10060. if (active) {
  10061. pipe_config->output_types |= 1 << encoder->type;
  10062. encoder->get_config(encoder, pipe_config);
  10063. }
  10064. }
  10065. intel_crtc_compute_pixel_rate(pipe_config);
  10066. if (!new_crtc_state->active)
  10067. return;
  10068. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10069. sw_config = to_intel_crtc_state(crtc->state);
  10070. if (!intel_pipe_config_compare(dev_priv, sw_config,
  10071. pipe_config, false)) {
  10072. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10073. intel_dump_pipe_config(intel_crtc, pipe_config,
  10074. "[hw state]");
  10075. intel_dump_pipe_config(intel_crtc, sw_config,
  10076. "[sw state]");
  10077. }
  10078. }
  10079. static void
  10080. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10081. struct intel_shared_dpll *pll,
  10082. struct drm_crtc *crtc,
  10083. struct drm_crtc_state *new_state)
  10084. {
  10085. struct intel_dpll_hw_state dpll_hw_state;
  10086. unsigned crtc_mask;
  10087. bool active;
  10088. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10089. DRM_DEBUG_KMS("%s\n", pll->name);
  10090. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10091. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10092. I915_STATE_WARN(!pll->on && pll->active_mask,
  10093. "pll in active use but not on in sw tracking\n");
  10094. I915_STATE_WARN(pll->on && !pll->active_mask,
  10095. "pll is on but not used by any active crtc\n");
  10096. I915_STATE_WARN(pll->on != active,
  10097. "pll on state mismatch (expected %i, found %i)\n",
  10098. pll->on, active);
  10099. }
  10100. if (!crtc) {
  10101. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  10102. "more active pll users than references: %x vs %x\n",
  10103. pll->active_mask, pll->state.crtc_mask);
  10104. return;
  10105. }
  10106. crtc_mask = 1 << drm_crtc_index(crtc);
  10107. if (new_state->active)
  10108. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10109. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10110. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10111. else
  10112. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10113. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10114. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10115. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  10116. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10117. crtc_mask, pll->state.crtc_mask);
  10118. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  10119. &dpll_hw_state,
  10120. sizeof(dpll_hw_state)),
  10121. "pll hw state mismatch\n");
  10122. }
  10123. static void
  10124. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10125. struct drm_crtc_state *old_crtc_state,
  10126. struct drm_crtc_state *new_crtc_state)
  10127. {
  10128. struct drm_i915_private *dev_priv = to_i915(dev);
  10129. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10130. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10131. if (new_state->shared_dpll)
  10132. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10133. if (old_state->shared_dpll &&
  10134. old_state->shared_dpll != new_state->shared_dpll) {
  10135. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  10136. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10137. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10138. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10139. pipe_name(drm_crtc_index(crtc)));
  10140. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  10141. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10142. pipe_name(drm_crtc_index(crtc)));
  10143. }
  10144. }
  10145. static void
  10146. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  10147. struct drm_atomic_state *state,
  10148. struct drm_crtc_state *old_state,
  10149. struct drm_crtc_state *new_state)
  10150. {
  10151. if (!needs_modeset(new_state) &&
  10152. !to_intel_crtc_state(new_state)->update_pipe)
  10153. return;
  10154. verify_wm_state(crtc, new_state);
  10155. verify_connector_state(crtc->dev, state, crtc);
  10156. verify_crtc_state(crtc, old_state, new_state);
  10157. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  10158. }
  10159. static void
  10160. verify_disabled_dpll_state(struct drm_device *dev)
  10161. {
  10162. struct drm_i915_private *dev_priv = to_i915(dev);
  10163. int i;
  10164. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10165. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  10166. }
  10167. static void
  10168. intel_modeset_verify_disabled(struct drm_device *dev,
  10169. struct drm_atomic_state *state)
  10170. {
  10171. verify_encoder_state(dev, state);
  10172. verify_connector_state(dev, state, NULL);
  10173. verify_disabled_dpll_state(dev);
  10174. }
  10175. static void update_scanline_offset(struct intel_crtc *crtc)
  10176. {
  10177. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10178. /*
  10179. * The scanline counter increments at the leading edge of hsync.
  10180. *
  10181. * On most platforms it starts counting from vtotal-1 on the
  10182. * first active line. That means the scanline counter value is
  10183. * always one less than what we would expect. Ie. just after
  10184. * start of vblank, which also occurs at start of hsync (on the
  10185. * last active line), the scanline counter will read vblank_start-1.
  10186. *
  10187. * On gen2 the scanline counter starts counting from 1 instead
  10188. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10189. * to keep the value positive), instead of adding one.
  10190. *
  10191. * On HSW+ the behaviour of the scanline counter depends on the output
  10192. * type. For DP ports it behaves like most other platforms, but on HDMI
  10193. * there's an extra 1 line difference. So we need to add two instead of
  10194. * one to the value.
  10195. */
  10196. if (IS_GEN2(dev_priv)) {
  10197. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10198. int vtotal;
  10199. vtotal = adjusted_mode->crtc_vtotal;
  10200. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10201. vtotal /= 2;
  10202. crtc->scanline_offset = vtotal - 1;
  10203. } else if (HAS_DDI(dev_priv) &&
  10204. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  10205. crtc->scanline_offset = 2;
  10206. } else
  10207. crtc->scanline_offset = 1;
  10208. }
  10209. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10210. {
  10211. struct drm_device *dev = state->dev;
  10212. struct drm_i915_private *dev_priv = to_i915(dev);
  10213. struct drm_crtc *crtc;
  10214. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10215. int i;
  10216. if (!dev_priv->display.crtc_compute_clock)
  10217. return;
  10218. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10219. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10220. struct intel_shared_dpll *old_dpll =
  10221. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  10222. if (!needs_modeset(new_crtc_state))
  10223. continue;
  10224. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  10225. if (!old_dpll)
  10226. continue;
  10227. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  10228. }
  10229. }
  10230. /*
  10231. * This implements the workaround described in the "notes" section of the mode
  10232. * set sequence documentation. When going from no pipes or single pipe to
  10233. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10234. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10235. */
  10236. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10237. {
  10238. struct drm_crtc_state *crtc_state;
  10239. struct intel_crtc *intel_crtc;
  10240. struct drm_crtc *crtc;
  10241. struct intel_crtc_state *first_crtc_state = NULL;
  10242. struct intel_crtc_state *other_crtc_state = NULL;
  10243. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10244. int i;
  10245. /* look at all crtc's that are going to be enabled in during modeset */
  10246. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  10247. intel_crtc = to_intel_crtc(crtc);
  10248. if (!crtc_state->active || !needs_modeset(crtc_state))
  10249. continue;
  10250. if (first_crtc_state) {
  10251. other_crtc_state = to_intel_crtc_state(crtc_state);
  10252. break;
  10253. } else {
  10254. first_crtc_state = to_intel_crtc_state(crtc_state);
  10255. first_pipe = intel_crtc->pipe;
  10256. }
  10257. }
  10258. /* No workaround needed? */
  10259. if (!first_crtc_state)
  10260. return 0;
  10261. /* w/a possibly needed, check how many crtc's are already enabled. */
  10262. for_each_intel_crtc(state->dev, intel_crtc) {
  10263. struct intel_crtc_state *pipe_config;
  10264. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10265. if (IS_ERR(pipe_config))
  10266. return PTR_ERR(pipe_config);
  10267. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10268. if (!pipe_config->base.active ||
  10269. needs_modeset(&pipe_config->base))
  10270. continue;
  10271. /* 2 or more enabled crtcs means no need for w/a */
  10272. if (enabled_pipe != INVALID_PIPE)
  10273. return 0;
  10274. enabled_pipe = intel_crtc->pipe;
  10275. }
  10276. if (enabled_pipe != INVALID_PIPE)
  10277. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10278. else if (other_crtc_state)
  10279. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10280. return 0;
  10281. }
  10282. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  10283. {
  10284. struct drm_crtc *crtc;
  10285. /* Add all pipes to the state */
  10286. for_each_crtc(state->dev, crtc) {
  10287. struct drm_crtc_state *crtc_state;
  10288. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10289. if (IS_ERR(crtc_state))
  10290. return PTR_ERR(crtc_state);
  10291. }
  10292. return 0;
  10293. }
  10294. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10295. {
  10296. struct drm_crtc *crtc;
  10297. /*
  10298. * Add all pipes to the state, and force
  10299. * a modeset on all the active ones.
  10300. */
  10301. for_each_crtc(state->dev, crtc) {
  10302. struct drm_crtc_state *crtc_state;
  10303. int ret;
  10304. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10305. if (IS_ERR(crtc_state))
  10306. return PTR_ERR(crtc_state);
  10307. if (!crtc_state->active || needs_modeset(crtc_state))
  10308. continue;
  10309. crtc_state->mode_changed = true;
  10310. ret = drm_atomic_add_affected_connectors(state, crtc);
  10311. if (ret)
  10312. return ret;
  10313. ret = drm_atomic_add_affected_planes(state, crtc);
  10314. if (ret)
  10315. return ret;
  10316. }
  10317. return 0;
  10318. }
  10319. static int intel_modeset_checks(struct drm_atomic_state *state)
  10320. {
  10321. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10322. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10323. struct drm_crtc *crtc;
  10324. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10325. int ret = 0, i;
  10326. if (!check_digital_port_conflicts(state)) {
  10327. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10328. return -EINVAL;
  10329. }
  10330. intel_state->modeset = true;
  10331. intel_state->active_crtcs = dev_priv->active_crtcs;
  10332. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10333. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  10334. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10335. if (new_crtc_state->active)
  10336. intel_state->active_crtcs |= 1 << i;
  10337. else
  10338. intel_state->active_crtcs &= ~(1 << i);
  10339. if (old_crtc_state->active != new_crtc_state->active)
  10340. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10341. }
  10342. /*
  10343. * See if the config requires any additional preparation, e.g.
  10344. * to adjust global state with pipes off. We need to do this
  10345. * here so we can get the modeset_pipe updated config for the new
  10346. * mode set on this crtc. For other crtcs we need to use the
  10347. * adjusted_mode bits in the crtc directly.
  10348. */
  10349. if (dev_priv->display.modeset_calc_cdclk) {
  10350. ret = dev_priv->display.modeset_calc_cdclk(state);
  10351. if (ret < 0)
  10352. return ret;
  10353. /*
  10354. * Writes to dev_priv->cdclk.logical must protected by
  10355. * holding all the crtc locks, even if we don't end up
  10356. * touching the hardware
  10357. */
  10358. if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
  10359. &intel_state->cdclk.logical)) {
  10360. ret = intel_lock_all_pipes(state);
  10361. if (ret < 0)
  10362. return ret;
  10363. }
  10364. /* All pipes must be switched off while we change the cdclk. */
  10365. if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
  10366. &intel_state->cdclk.actual)) {
  10367. ret = intel_modeset_all_pipes(state);
  10368. if (ret < 0)
  10369. return ret;
  10370. }
  10371. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10372. intel_state->cdclk.logical.cdclk,
  10373. intel_state->cdclk.actual.cdclk);
  10374. } else {
  10375. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10376. }
  10377. intel_modeset_clear_plls(state);
  10378. if (IS_HASWELL(dev_priv))
  10379. return haswell_mode_set_planes_workaround(state);
  10380. return 0;
  10381. }
  10382. /*
  10383. * Handle calculation of various watermark data at the end of the atomic check
  10384. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10385. * handlers to ensure that all derived state has been updated.
  10386. */
  10387. static int calc_watermark_data(struct drm_atomic_state *state)
  10388. {
  10389. struct drm_device *dev = state->dev;
  10390. struct drm_i915_private *dev_priv = to_i915(dev);
  10391. /* Is there platform-specific watermark information to calculate? */
  10392. if (dev_priv->display.compute_global_watermarks)
  10393. return dev_priv->display.compute_global_watermarks(state);
  10394. return 0;
  10395. }
  10396. /**
  10397. * intel_atomic_check - validate state object
  10398. * @dev: drm device
  10399. * @state: state to validate
  10400. */
  10401. static int intel_atomic_check(struct drm_device *dev,
  10402. struct drm_atomic_state *state)
  10403. {
  10404. struct drm_i915_private *dev_priv = to_i915(dev);
  10405. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10406. struct drm_crtc *crtc;
  10407. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10408. int ret, i;
  10409. bool any_ms = false;
  10410. ret = drm_atomic_helper_check_modeset(dev, state);
  10411. if (ret)
  10412. return ret;
  10413. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10414. struct intel_crtc_state *pipe_config =
  10415. to_intel_crtc_state(crtc_state);
  10416. /* Catch I915_MODE_FLAG_INHERITED */
  10417. if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
  10418. crtc_state->mode_changed = true;
  10419. if (!needs_modeset(crtc_state))
  10420. continue;
  10421. if (!crtc_state->enable) {
  10422. any_ms = true;
  10423. continue;
  10424. }
  10425. /* FIXME: For only active_changed we shouldn't need to do any
  10426. * state recomputation at all. */
  10427. ret = drm_atomic_add_affected_connectors(state, crtc);
  10428. if (ret)
  10429. return ret;
  10430. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10431. if (ret) {
  10432. intel_dump_pipe_config(to_intel_crtc(crtc),
  10433. pipe_config, "[failed]");
  10434. return ret;
  10435. }
  10436. if (i915.fastboot &&
  10437. intel_pipe_config_compare(dev_priv,
  10438. to_intel_crtc_state(old_crtc_state),
  10439. pipe_config, true)) {
  10440. crtc_state->mode_changed = false;
  10441. pipe_config->update_pipe = true;
  10442. }
  10443. if (needs_modeset(crtc_state))
  10444. any_ms = true;
  10445. ret = drm_atomic_add_affected_planes(state, crtc);
  10446. if (ret)
  10447. return ret;
  10448. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10449. needs_modeset(crtc_state) ?
  10450. "[modeset]" : "[fastset]");
  10451. }
  10452. if (any_ms) {
  10453. ret = intel_modeset_checks(state);
  10454. if (ret)
  10455. return ret;
  10456. } else {
  10457. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10458. }
  10459. ret = drm_atomic_helper_check_planes(dev, state);
  10460. if (ret)
  10461. return ret;
  10462. intel_fbc_choose_crtc(dev_priv, state);
  10463. return calc_watermark_data(state);
  10464. }
  10465. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10466. struct drm_atomic_state *state)
  10467. {
  10468. struct drm_i915_private *dev_priv = to_i915(dev);
  10469. struct drm_crtc_state *crtc_state;
  10470. struct drm_crtc *crtc;
  10471. int i, ret;
  10472. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  10473. if (state->legacy_cursor_update)
  10474. continue;
  10475. ret = intel_crtc_wait_for_pending_flips(crtc);
  10476. if (ret)
  10477. return ret;
  10478. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  10479. flush_workqueue(dev_priv->wq);
  10480. }
  10481. ret = mutex_lock_interruptible(&dev->struct_mutex);
  10482. if (ret)
  10483. return ret;
  10484. ret = drm_atomic_helper_prepare_planes(dev, state);
  10485. mutex_unlock(&dev->struct_mutex);
  10486. return ret;
  10487. }
  10488. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10489. {
  10490. struct drm_device *dev = crtc->base.dev;
  10491. if (!dev->max_vblank_count)
  10492. return drm_accurate_vblank_count(&crtc->base);
  10493. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10494. }
  10495. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  10496. struct drm_i915_private *dev_priv,
  10497. unsigned crtc_mask)
  10498. {
  10499. unsigned last_vblank_count[I915_MAX_PIPES];
  10500. enum pipe pipe;
  10501. int ret;
  10502. if (!crtc_mask)
  10503. return;
  10504. for_each_pipe(dev_priv, pipe) {
  10505. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10506. pipe);
  10507. if (!((1 << pipe) & crtc_mask))
  10508. continue;
  10509. ret = drm_crtc_vblank_get(&crtc->base);
  10510. if (WARN_ON(ret != 0)) {
  10511. crtc_mask &= ~(1 << pipe);
  10512. continue;
  10513. }
  10514. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  10515. }
  10516. for_each_pipe(dev_priv, pipe) {
  10517. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10518. pipe);
  10519. long lret;
  10520. if (!((1 << pipe) & crtc_mask))
  10521. continue;
  10522. lret = wait_event_timeout(dev->vblank[pipe].queue,
  10523. last_vblank_count[pipe] !=
  10524. drm_crtc_vblank_count(&crtc->base),
  10525. msecs_to_jiffies(50));
  10526. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  10527. drm_crtc_vblank_put(&crtc->base);
  10528. }
  10529. }
  10530. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  10531. {
  10532. /* fb updated, need to unpin old fb */
  10533. if (crtc_state->fb_changed)
  10534. return true;
  10535. /* wm changes, need vblank before final wm's */
  10536. if (crtc_state->update_wm_post)
  10537. return true;
  10538. if (crtc_state->wm.need_postvbl_update)
  10539. return true;
  10540. return false;
  10541. }
  10542. static void intel_update_crtc(struct drm_crtc *crtc,
  10543. struct drm_atomic_state *state,
  10544. struct drm_crtc_state *old_crtc_state,
  10545. struct drm_crtc_state *new_crtc_state,
  10546. unsigned int *crtc_vblank_mask)
  10547. {
  10548. struct drm_device *dev = crtc->dev;
  10549. struct drm_i915_private *dev_priv = to_i915(dev);
  10550. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10551. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10552. bool modeset = needs_modeset(new_crtc_state);
  10553. if (modeset) {
  10554. update_scanline_offset(intel_crtc);
  10555. dev_priv->display.crtc_enable(pipe_config, state);
  10556. } else {
  10557. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10558. pipe_config);
  10559. }
  10560. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10561. intel_fbc_enable(
  10562. intel_crtc, pipe_config,
  10563. to_intel_plane_state(crtc->primary->state));
  10564. }
  10565. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10566. if (needs_vblank_wait(pipe_config))
  10567. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  10568. }
  10569. static void intel_update_crtcs(struct drm_atomic_state *state,
  10570. unsigned int *crtc_vblank_mask)
  10571. {
  10572. struct drm_crtc *crtc;
  10573. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10574. int i;
  10575. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10576. if (!new_crtc_state->active)
  10577. continue;
  10578. intel_update_crtc(crtc, state, old_crtc_state,
  10579. new_crtc_state, crtc_vblank_mask);
  10580. }
  10581. }
  10582. static void skl_update_crtcs(struct drm_atomic_state *state,
  10583. unsigned int *crtc_vblank_mask)
  10584. {
  10585. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10586. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10587. struct drm_crtc *crtc;
  10588. struct intel_crtc *intel_crtc;
  10589. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10590. struct intel_crtc_state *cstate;
  10591. unsigned int updated = 0;
  10592. bool progress;
  10593. enum pipe pipe;
  10594. int i;
  10595. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10596. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10597. /* ignore allocations for crtc's that have been turned off. */
  10598. if (new_crtc_state->active)
  10599. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10600. /*
  10601. * Whenever the number of active pipes changes, we need to make sure we
  10602. * update the pipes in the right order so that their ddb allocations
  10603. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10604. * cause pipe underruns and other bad stuff.
  10605. */
  10606. do {
  10607. progress = false;
  10608. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10609. bool vbl_wait = false;
  10610. unsigned int cmask = drm_crtc_mask(crtc);
  10611. intel_crtc = to_intel_crtc(crtc);
  10612. cstate = to_intel_crtc_state(crtc->state);
  10613. pipe = intel_crtc->pipe;
  10614. if (updated & cmask || !cstate->base.active)
  10615. continue;
  10616. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  10617. continue;
  10618. updated |= cmask;
  10619. entries[i] = &cstate->wm.skl.ddb;
  10620. /*
  10621. * If this is an already active pipe, it's DDB changed,
  10622. * and this isn't the last pipe that needs updating
  10623. * then we need to wait for a vblank to pass for the
  10624. * new ddb allocation to take effect.
  10625. */
  10626. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10627. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10628. !new_crtc_state->active_changed &&
  10629. intel_state->wm_results.dirty_pipes != updated)
  10630. vbl_wait = true;
  10631. intel_update_crtc(crtc, state, old_crtc_state,
  10632. new_crtc_state, crtc_vblank_mask);
  10633. if (vbl_wait)
  10634. intel_wait_for_vblank(dev_priv, pipe);
  10635. progress = true;
  10636. }
  10637. } while (progress);
  10638. }
  10639. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10640. {
  10641. struct intel_atomic_state *state, *next;
  10642. struct llist_node *freed;
  10643. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10644. llist_for_each_entry_safe(state, next, freed, freed)
  10645. drm_atomic_state_put(&state->base);
  10646. }
  10647. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10648. {
  10649. struct drm_i915_private *dev_priv =
  10650. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10651. intel_atomic_helper_free_state(dev_priv);
  10652. }
  10653. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10654. {
  10655. struct drm_device *dev = state->dev;
  10656. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10657. struct drm_i915_private *dev_priv = to_i915(dev);
  10658. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10659. struct drm_crtc *crtc;
  10660. struct intel_crtc_state *intel_cstate;
  10661. bool hw_check = intel_state->modeset;
  10662. u64 put_domains[I915_MAX_PIPES] = {};
  10663. unsigned crtc_vblank_mask = 0;
  10664. int i;
  10665. drm_atomic_helper_wait_for_dependencies(state);
  10666. if (intel_state->modeset)
  10667. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10668. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10669. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10670. if (needs_modeset(new_crtc_state) ||
  10671. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10672. hw_check = true;
  10673. put_domains[to_intel_crtc(crtc)->pipe] =
  10674. modeset_get_crtc_power_domains(crtc,
  10675. to_intel_crtc_state(new_crtc_state));
  10676. }
  10677. if (!needs_modeset(new_crtc_state))
  10678. continue;
  10679. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10680. to_intel_crtc_state(new_crtc_state));
  10681. if (old_crtc_state->active) {
  10682. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10683. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10684. intel_crtc->active = false;
  10685. intel_fbc_disable(intel_crtc);
  10686. intel_disable_shared_dpll(intel_crtc);
  10687. /*
  10688. * Underruns don't always raise
  10689. * interrupts, so check manually.
  10690. */
  10691. intel_check_cpu_fifo_underruns(dev_priv);
  10692. intel_check_pch_fifo_underruns(dev_priv);
  10693. if (!crtc->state->active) {
  10694. /*
  10695. * Make sure we don't call initial_watermarks
  10696. * for ILK-style watermark updates.
  10697. *
  10698. * No clue what this is supposed to achieve.
  10699. */
  10700. if (INTEL_GEN(dev_priv) >= 9)
  10701. dev_priv->display.initial_watermarks(intel_state,
  10702. to_intel_crtc_state(crtc->state));
  10703. }
  10704. }
  10705. }
  10706. /* Only after disabling all output pipelines that will be changed can we
  10707. * update the the output configuration. */
  10708. intel_modeset_update_crtc_state(state);
  10709. if (intel_state->modeset) {
  10710. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10711. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10712. /*
  10713. * SKL workaround: bspec recommends we disable the SAGV when we
  10714. * have more then one pipe enabled
  10715. */
  10716. if (!intel_can_enable_sagv(state))
  10717. intel_disable_sagv(dev_priv);
  10718. intel_modeset_verify_disabled(dev, state);
  10719. }
  10720. /* Complete the events for pipes that have now been disabled */
  10721. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10722. bool modeset = needs_modeset(new_crtc_state);
  10723. /* Complete events for now disable pipes here. */
  10724. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10725. spin_lock_irq(&dev->event_lock);
  10726. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10727. spin_unlock_irq(&dev->event_lock);
  10728. new_crtc_state->event = NULL;
  10729. }
  10730. }
  10731. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10732. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  10733. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10734. * already, but still need the state for the delayed optimization. To
  10735. * fix this:
  10736. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10737. * - schedule that vblank worker _before_ calling hw_done
  10738. * - at the start of commit_tail, cancel it _synchrously
  10739. * - switch over to the vblank wait helper in the core after that since
  10740. * we don't need out special handling any more.
  10741. */
  10742. if (!state->legacy_cursor_update)
  10743. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  10744. /*
  10745. * Now that the vblank has passed, we can go ahead and program the
  10746. * optimal watermarks on platforms that need two-step watermark
  10747. * programming.
  10748. *
  10749. * TODO: Move this (and other cleanup) to an async worker eventually.
  10750. */
  10751. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10752. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10753. if (dev_priv->display.optimize_watermarks)
  10754. dev_priv->display.optimize_watermarks(intel_state,
  10755. intel_cstate);
  10756. }
  10757. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10758. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10759. if (put_domains[i])
  10760. modeset_put_power_domains(dev_priv, put_domains[i]);
  10761. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10762. }
  10763. if (intel_state->modeset && intel_can_enable_sagv(state))
  10764. intel_enable_sagv(dev_priv);
  10765. drm_atomic_helper_commit_hw_done(state);
  10766. if (intel_state->modeset)
  10767. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10768. mutex_lock(&dev->struct_mutex);
  10769. drm_atomic_helper_cleanup_planes(dev, state);
  10770. mutex_unlock(&dev->struct_mutex);
  10771. drm_atomic_helper_commit_cleanup_done(state);
  10772. drm_atomic_state_put(state);
  10773. /* As one of the primary mmio accessors, KMS has a high likelihood
  10774. * of triggering bugs in unclaimed access. After we finish
  10775. * modesetting, see if an error has been flagged, and if so
  10776. * enable debugging for the next modeset - and hope we catch
  10777. * the culprit.
  10778. *
  10779. * XXX note that we assume display power is on at this point.
  10780. * This might hold true now but we need to add pm helper to check
  10781. * unclaimed only when the hardware is on, as atomic commits
  10782. * can happen also when the device is completely off.
  10783. */
  10784. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10785. intel_atomic_helper_free_state(dev_priv);
  10786. }
  10787. static void intel_atomic_commit_work(struct work_struct *work)
  10788. {
  10789. struct drm_atomic_state *state =
  10790. container_of(work, struct drm_atomic_state, commit_work);
  10791. intel_atomic_commit_tail(state);
  10792. }
  10793. static int __i915_sw_fence_call
  10794. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10795. enum i915_sw_fence_notify notify)
  10796. {
  10797. struct intel_atomic_state *state =
  10798. container_of(fence, struct intel_atomic_state, commit_ready);
  10799. switch (notify) {
  10800. case FENCE_COMPLETE:
  10801. if (state->base.commit_work.func)
  10802. queue_work(system_unbound_wq, &state->base.commit_work);
  10803. break;
  10804. case FENCE_FREE:
  10805. {
  10806. struct intel_atomic_helper *helper =
  10807. &to_i915(state->base.dev)->atomic_helper;
  10808. if (llist_add(&state->freed, &helper->free_list))
  10809. schedule_work(&helper->free_work);
  10810. break;
  10811. }
  10812. }
  10813. return NOTIFY_DONE;
  10814. }
  10815. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10816. {
  10817. struct drm_plane_state *old_plane_state, *new_plane_state;
  10818. struct drm_plane *plane;
  10819. int i;
  10820. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10821. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10822. intel_fb_obj(new_plane_state->fb),
  10823. to_intel_plane(plane)->frontbuffer_bit);
  10824. }
  10825. /**
  10826. * intel_atomic_commit - commit validated state object
  10827. * @dev: DRM device
  10828. * @state: the top-level driver state object
  10829. * @nonblock: nonblocking commit
  10830. *
  10831. * This function commits a top-level state object that has been validated
  10832. * with drm_atomic_helper_check().
  10833. *
  10834. * RETURNS
  10835. * Zero for success or -errno.
  10836. */
  10837. static int intel_atomic_commit(struct drm_device *dev,
  10838. struct drm_atomic_state *state,
  10839. bool nonblock)
  10840. {
  10841. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10842. struct drm_i915_private *dev_priv = to_i915(dev);
  10843. int ret = 0;
  10844. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10845. if (ret)
  10846. return ret;
  10847. drm_atomic_state_get(state);
  10848. i915_sw_fence_init(&intel_state->commit_ready,
  10849. intel_atomic_commit_ready);
  10850. ret = intel_atomic_prepare_commit(dev, state);
  10851. if (ret) {
  10852. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10853. i915_sw_fence_commit(&intel_state->commit_ready);
  10854. return ret;
  10855. }
  10856. /*
  10857. * The intel_legacy_cursor_update() fast path takes care
  10858. * of avoiding the vblank waits for simple cursor
  10859. * movement and flips. For cursor on/off and size changes,
  10860. * we want to perform the vblank waits so that watermark
  10861. * updates happen during the correct frames. Gen9+ have
  10862. * double buffered watermarks and so shouldn't need this.
  10863. *
  10864. * Do this after drm_atomic_helper_setup_commit() and
  10865. * intel_atomic_prepare_commit() because we still want
  10866. * to skip the flip and fb cleanup waits. Although that
  10867. * does risk yanking the mapping from under the display
  10868. * engine.
  10869. *
  10870. * FIXME doing watermarks and fb cleanup from a vblank worker
  10871. * (assuming we had any) would solve these problems.
  10872. */
  10873. if (INTEL_GEN(dev_priv) < 9)
  10874. state->legacy_cursor_update = false;
  10875. drm_atomic_helper_swap_state(state, true);
  10876. dev_priv->wm.distrust_bios_wm = false;
  10877. intel_shared_dpll_swap_state(state);
  10878. intel_atomic_track_fbs(state);
  10879. if (intel_state->modeset) {
  10880. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  10881. sizeof(intel_state->min_pixclk));
  10882. dev_priv->active_crtcs = intel_state->active_crtcs;
  10883. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10884. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10885. }
  10886. drm_atomic_state_get(state);
  10887. INIT_WORK(&state->commit_work,
  10888. nonblock ? intel_atomic_commit_work : NULL);
  10889. i915_sw_fence_commit(&intel_state->commit_ready);
  10890. if (!nonblock) {
  10891. i915_sw_fence_wait(&intel_state->commit_ready);
  10892. intel_atomic_commit_tail(state);
  10893. }
  10894. return 0;
  10895. }
  10896. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  10897. {
  10898. struct drm_device *dev = crtc->dev;
  10899. struct drm_atomic_state *state;
  10900. struct drm_crtc_state *crtc_state;
  10901. int ret;
  10902. state = drm_atomic_state_alloc(dev);
  10903. if (!state) {
  10904. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  10905. crtc->base.id, crtc->name);
  10906. return;
  10907. }
  10908. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  10909. retry:
  10910. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10911. ret = PTR_ERR_OR_ZERO(crtc_state);
  10912. if (!ret) {
  10913. if (!crtc_state->active)
  10914. goto out;
  10915. crtc_state->mode_changed = true;
  10916. ret = drm_atomic_commit(state);
  10917. }
  10918. if (ret == -EDEADLK) {
  10919. drm_atomic_state_clear(state);
  10920. drm_modeset_backoff(state->acquire_ctx);
  10921. goto retry;
  10922. }
  10923. out:
  10924. drm_atomic_state_put(state);
  10925. }
  10926. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10927. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10928. .set_config = drm_atomic_helper_set_config,
  10929. .set_property = drm_atomic_helper_crtc_set_property,
  10930. .destroy = intel_crtc_destroy,
  10931. .page_flip = drm_atomic_helper_page_flip,
  10932. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10933. .atomic_destroy_state = intel_crtc_destroy_state,
  10934. .set_crc_source = intel_crtc_set_crc_source,
  10935. };
  10936. /**
  10937. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10938. * @plane: drm plane to prepare for
  10939. * @fb: framebuffer to prepare for presentation
  10940. *
  10941. * Prepares a framebuffer for usage on a display plane. Generally this
  10942. * involves pinning the underlying object and updating the frontbuffer tracking
  10943. * bits. Some older platforms need special physical address handling for
  10944. * cursor planes.
  10945. *
  10946. * Must be called with struct_mutex held.
  10947. *
  10948. * Returns 0 on success, negative error code on failure.
  10949. */
  10950. int
  10951. intel_prepare_plane_fb(struct drm_plane *plane,
  10952. struct drm_plane_state *new_state)
  10953. {
  10954. struct intel_atomic_state *intel_state =
  10955. to_intel_atomic_state(new_state->state);
  10956. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10957. struct drm_framebuffer *fb = new_state->fb;
  10958. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10959. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10960. int ret;
  10961. if (obj) {
  10962. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10963. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10964. const int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  10965. ret = i915_gem_object_attach_phys(obj, align);
  10966. if (ret) {
  10967. DRM_DEBUG_KMS("failed to attach phys object\n");
  10968. return ret;
  10969. }
  10970. } else {
  10971. struct i915_vma *vma;
  10972. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  10973. if (IS_ERR(vma)) {
  10974. DRM_DEBUG_KMS("failed to pin object\n");
  10975. return PTR_ERR(vma);
  10976. }
  10977. to_intel_plane_state(new_state)->vma = vma;
  10978. }
  10979. }
  10980. if (!obj && !old_obj)
  10981. return 0;
  10982. if (old_obj) {
  10983. struct drm_crtc_state *crtc_state =
  10984. drm_atomic_get_existing_crtc_state(new_state->state,
  10985. plane->state->crtc);
  10986. /* Big Hammer, we also need to ensure that any pending
  10987. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10988. * current scanout is retired before unpinning the old
  10989. * framebuffer. Note that we rely on userspace rendering
  10990. * into the buffer attached to the pipe they are waiting
  10991. * on. If not, userspace generates a GPU hang with IPEHR
  10992. * point to the MI_WAIT_FOR_EVENT.
  10993. *
  10994. * This should only fail upon a hung GPU, in which case we
  10995. * can safely continue.
  10996. */
  10997. if (needs_modeset(crtc_state)) {
  10998. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10999. old_obj->resv, NULL,
  11000. false, 0,
  11001. GFP_KERNEL);
  11002. if (ret < 0)
  11003. return ret;
  11004. }
  11005. }
  11006. if (new_state->fence) { /* explicit fencing */
  11007. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  11008. new_state->fence,
  11009. I915_FENCE_TIMEOUT,
  11010. GFP_KERNEL);
  11011. if (ret < 0)
  11012. return ret;
  11013. }
  11014. if (!obj)
  11015. return 0;
  11016. if (!new_state->fence) { /* implicit fencing */
  11017. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  11018. obj->resv, NULL,
  11019. false, I915_FENCE_TIMEOUT,
  11020. GFP_KERNEL);
  11021. if (ret < 0)
  11022. return ret;
  11023. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  11024. }
  11025. return 0;
  11026. }
  11027. /**
  11028. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11029. * @plane: drm plane to clean up for
  11030. * @fb: old framebuffer that was on plane
  11031. *
  11032. * Cleans up a framebuffer that has just been removed from a plane.
  11033. *
  11034. * Must be called with struct_mutex held.
  11035. */
  11036. void
  11037. intel_cleanup_plane_fb(struct drm_plane *plane,
  11038. struct drm_plane_state *old_state)
  11039. {
  11040. struct i915_vma *vma;
  11041. /* Should only be called after a successful intel_prepare_plane_fb()! */
  11042. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  11043. if (vma)
  11044. intel_unpin_fb_vma(vma);
  11045. }
  11046. int
  11047. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11048. {
  11049. struct drm_i915_private *dev_priv;
  11050. int max_scale;
  11051. int crtc_clock, max_dotclk;
  11052. if (!intel_crtc || !crtc_state->base.enable)
  11053. return DRM_PLANE_HELPER_NO_SCALING;
  11054. dev_priv = to_i915(intel_crtc->base.dev);
  11055. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11056. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  11057. if (IS_GEMINILAKE(dev_priv))
  11058. max_dotclk *= 2;
  11059. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  11060. return DRM_PLANE_HELPER_NO_SCALING;
  11061. /*
  11062. * skl max scale is lower of:
  11063. * close to 3 but not 3, -1 is for that purpose
  11064. * or
  11065. * cdclk/crtc_clock
  11066. */
  11067. max_scale = min((1 << 16) * 3 - 1,
  11068. (1 << 8) * ((max_dotclk << 8) / crtc_clock));
  11069. return max_scale;
  11070. }
  11071. static int
  11072. intel_check_primary_plane(struct drm_plane *plane,
  11073. struct intel_crtc_state *crtc_state,
  11074. struct intel_plane_state *state)
  11075. {
  11076. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11077. struct drm_crtc *crtc = state->base.crtc;
  11078. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11079. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11080. bool can_position = false;
  11081. int ret;
  11082. if (INTEL_GEN(dev_priv) >= 9) {
  11083. /* use scaler when colorkey is not required */
  11084. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11085. min_scale = 1;
  11086. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11087. }
  11088. can_position = true;
  11089. }
  11090. ret = drm_plane_helper_check_state(&state->base,
  11091. &state->clip,
  11092. min_scale, max_scale,
  11093. can_position, true);
  11094. if (ret)
  11095. return ret;
  11096. if (!state->base.fb)
  11097. return 0;
  11098. if (INTEL_GEN(dev_priv) >= 9) {
  11099. ret = skl_check_plane_surface(state);
  11100. if (ret)
  11101. return ret;
  11102. state->ctl = skl_plane_ctl(crtc_state, state);
  11103. } else {
  11104. ret = i9xx_check_plane_surface(state);
  11105. if (ret)
  11106. return ret;
  11107. state->ctl = i9xx_plane_ctl(crtc_state, state);
  11108. }
  11109. return 0;
  11110. }
  11111. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11112. struct drm_crtc_state *old_crtc_state)
  11113. {
  11114. struct drm_device *dev = crtc->dev;
  11115. struct drm_i915_private *dev_priv = to_i915(dev);
  11116. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11117. struct intel_crtc_state *intel_cstate =
  11118. to_intel_crtc_state(crtc->state);
  11119. struct intel_crtc_state *old_intel_cstate =
  11120. to_intel_crtc_state(old_crtc_state);
  11121. struct intel_atomic_state *old_intel_state =
  11122. to_intel_atomic_state(old_crtc_state->state);
  11123. bool modeset = needs_modeset(crtc->state);
  11124. if (!modeset &&
  11125. (intel_cstate->base.color_mgmt_changed ||
  11126. intel_cstate->update_pipe)) {
  11127. intel_color_set_csc(crtc->state);
  11128. intel_color_load_luts(crtc->state);
  11129. }
  11130. /* Perform vblank evasion around commit operation */
  11131. intel_pipe_update_start(intel_crtc);
  11132. if (modeset)
  11133. goto out;
  11134. if (intel_cstate->update_pipe)
  11135. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  11136. else if (INTEL_GEN(dev_priv) >= 9)
  11137. skl_detach_scalers(intel_crtc);
  11138. out:
  11139. if (dev_priv->display.atomic_update_watermarks)
  11140. dev_priv->display.atomic_update_watermarks(old_intel_state,
  11141. intel_cstate);
  11142. }
  11143. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11144. struct drm_crtc_state *old_crtc_state)
  11145. {
  11146. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11147. intel_pipe_update_end(intel_crtc, NULL);
  11148. }
  11149. /**
  11150. * intel_plane_destroy - destroy a plane
  11151. * @plane: plane to destroy
  11152. *
  11153. * Common destruction function for all types of planes (primary, cursor,
  11154. * sprite).
  11155. */
  11156. void intel_plane_destroy(struct drm_plane *plane)
  11157. {
  11158. drm_plane_cleanup(plane);
  11159. kfree(to_intel_plane(plane));
  11160. }
  11161. const struct drm_plane_funcs intel_plane_funcs = {
  11162. .update_plane = drm_atomic_helper_update_plane,
  11163. .disable_plane = drm_atomic_helper_disable_plane,
  11164. .destroy = intel_plane_destroy,
  11165. .set_property = drm_atomic_helper_plane_set_property,
  11166. .atomic_get_property = intel_plane_atomic_get_property,
  11167. .atomic_set_property = intel_plane_atomic_set_property,
  11168. .atomic_duplicate_state = intel_plane_duplicate_state,
  11169. .atomic_destroy_state = intel_plane_destroy_state,
  11170. };
  11171. static int
  11172. intel_legacy_cursor_update(struct drm_plane *plane,
  11173. struct drm_crtc *crtc,
  11174. struct drm_framebuffer *fb,
  11175. int crtc_x, int crtc_y,
  11176. unsigned int crtc_w, unsigned int crtc_h,
  11177. uint32_t src_x, uint32_t src_y,
  11178. uint32_t src_w, uint32_t src_h,
  11179. struct drm_modeset_acquire_ctx *ctx)
  11180. {
  11181. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11182. int ret;
  11183. struct drm_plane_state *old_plane_state, *new_plane_state;
  11184. struct intel_plane *intel_plane = to_intel_plane(plane);
  11185. struct drm_framebuffer *old_fb;
  11186. struct drm_crtc_state *crtc_state = crtc->state;
  11187. struct i915_vma *old_vma;
  11188. /*
  11189. * When crtc is inactive or there is a modeset pending,
  11190. * wait for it to complete in the slowpath
  11191. */
  11192. if (!crtc_state->active || needs_modeset(crtc_state) ||
  11193. to_intel_crtc_state(crtc_state)->update_pipe)
  11194. goto slow;
  11195. old_plane_state = plane->state;
  11196. /*
  11197. * If any parameters change that may affect watermarks,
  11198. * take the slowpath. Only changing fb or position should be
  11199. * in the fastpath.
  11200. */
  11201. if (old_plane_state->crtc != crtc ||
  11202. old_plane_state->src_w != src_w ||
  11203. old_plane_state->src_h != src_h ||
  11204. old_plane_state->crtc_w != crtc_w ||
  11205. old_plane_state->crtc_h != crtc_h ||
  11206. !old_plane_state->fb != !fb)
  11207. goto slow;
  11208. new_plane_state = intel_plane_duplicate_state(plane);
  11209. if (!new_plane_state)
  11210. return -ENOMEM;
  11211. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  11212. new_plane_state->src_x = src_x;
  11213. new_plane_state->src_y = src_y;
  11214. new_plane_state->src_w = src_w;
  11215. new_plane_state->src_h = src_h;
  11216. new_plane_state->crtc_x = crtc_x;
  11217. new_plane_state->crtc_y = crtc_y;
  11218. new_plane_state->crtc_w = crtc_w;
  11219. new_plane_state->crtc_h = crtc_h;
  11220. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  11221. to_intel_plane_state(new_plane_state));
  11222. if (ret)
  11223. goto out_free;
  11224. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  11225. if (ret)
  11226. goto out_free;
  11227. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  11228. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  11229. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  11230. if (ret) {
  11231. DRM_DEBUG_KMS("failed to attach phys object\n");
  11232. goto out_unlock;
  11233. }
  11234. } else {
  11235. struct i915_vma *vma;
  11236. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  11237. if (IS_ERR(vma)) {
  11238. DRM_DEBUG_KMS("failed to pin object\n");
  11239. ret = PTR_ERR(vma);
  11240. goto out_unlock;
  11241. }
  11242. to_intel_plane_state(new_plane_state)->vma = vma;
  11243. }
  11244. old_fb = old_plane_state->fb;
  11245. old_vma = to_intel_plane_state(old_plane_state)->vma;
  11246. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  11247. intel_plane->frontbuffer_bit);
  11248. /* Swap plane state */
  11249. new_plane_state->fence = old_plane_state->fence;
  11250. *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
  11251. new_plane_state->fence = NULL;
  11252. new_plane_state->fb = old_fb;
  11253. to_intel_plane_state(new_plane_state)->vma = old_vma;
  11254. if (plane->state->visible) {
  11255. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  11256. intel_plane->update_plane(plane,
  11257. to_intel_crtc_state(crtc->state),
  11258. to_intel_plane_state(plane->state));
  11259. } else {
  11260. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  11261. intel_plane->disable_plane(plane, crtc);
  11262. }
  11263. intel_cleanup_plane_fb(plane, new_plane_state);
  11264. out_unlock:
  11265. mutex_unlock(&dev_priv->drm.struct_mutex);
  11266. out_free:
  11267. intel_plane_destroy_state(plane, new_plane_state);
  11268. return ret;
  11269. slow:
  11270. return drm_atomic_helper_update_plane(plane, crtc, fb,
  11271. crtc_x, crtc_y, crtc_w, crtc_h,
  11272. src_x, src_y, src_w, src_h, ctx);
  11273. }
  11274. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  11275. .update_plane = intel_legacy_cursor_update,
  11276. .disable_plane = drm_atomic_helper_disable_plane,
  11277. .destroy = intel_plane_destroy,
  11278. .set_property = drm_atomic_helper_plane_set_property,
  11279. .atomic_get_property = intel_plane_atomic_get_property,
  11280. .atomic_set_property = intel_plane_atomic_set_property,
  11281. .atomic_duplicate_state = intel_plane_duplicate_state,
  11282. .atomic_destroy_state = intel_plane_destroy_state,
  11283. };
  11284. static struct intel_plane *
  11285. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11286. {
  11287. struct intel_plane *primary = NULL;
  11288. struct intel_plane_state *state = NULL;
  11289. const uint32_t *intel_primary_formats;
  11290. unsigned int supported_rotations;
  11291. unsigned int num_formats;
  11292. int ret;
  11293. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11294. if (!primary) {
  11295. ret = -ENOMEM;
  11296. goto fail;
  11297. }
  11298. state = intel_create_plane_state(&primary->base);
  11299. if (!state) {
  11300. ret = -ENOMEM;
  11301. goto fail;
  11302. }
  11303. primary->base.state = &state->base;
  11304. primary->can_scale = false;
  11305. primary->max_downscale = 1;
  11306. if (INTEL_GEN(dev_priv) >= 9) {
  11307. primary->can_scale = true;
  11308. state->scaler_id = -1;
  11309. }
  11310. primary->pipe = pipe;
  11311. /*
  11312. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11313. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11314. */
  11315. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11316. primary->plane = (enum plane) !pipe;
  11317. else
  11318. primary->plane = (enum plane) pipe;
  11319. primary->id = PLANE_PRIMARY;
  11320. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11321. primary->check_plane = intel_check_primary_plane;
  11322. if (INTEL_GEN(dev_priv) >= 9) {
  11323. intel_primary_formats = skl_primary_formats;
  11324. num_formats = ARRAY_SIZE(skl_primary_formats);
  11325. primary->update_plane = skylake_update_primary_plane;
  11326. primary->disable_plane = skylake_disable_primary_plane;
  11327. } else if (INTEL_GEN(dev_priv) >= 4) {
  11328. intel_primary_formats = i965_primary_formats;
  11329. num_formats = ARRAY_SIZE(i965_primary_formats);
  11330. primary->update_plane = i9xx_update_primary_plane;
  11331. primary->disable_plane = i9xx_disable_primary_plane;
  11332. } else {
  11333. intel_primary_formats = i8xx_primary_formats;
  11334. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11335. primary->update_plane = i9xx_update_primary_plane;
  11336. primary->disable_plane = i9xx_disable_primary_plane;
  11337. }
  11338. if (INTEL_GEN(dev_priv) >= 9)
  11339. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11340. 0, &intel_plane_funcs,
  11341. intel_primary_formats, num_formats,
  11342. DRM_PLANE_TYPE_PRIMARY,
  11343. "plane 1%c", pipe_name(pipe));
  11344. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11345. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11346. 0, &intel_plane_funcs,
  11347. intel_primary_formats, num_formats,
  11348. DRM_PLANE_TYPE_PRIMARY,
  11349. "primary %c", pipe_name(pipe));
  11350. else
  11351. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11352. 0, &intel_plane_funcs,
  11353. intel_primary_formats, num_formats,
  11354. DRM_PLANE_TYPE_PRIMARY,
  11355. "plane %c", plane_name(primary->plane));
  11356. if (ret)
  11357. goto fail;
  11358. if (INTEL_GEN(dev_priv) >= 9) {
  11359. supported_rotations =
  11360. DRM_ROTATE_0 | DRM_ROTATE_90 |
  11361. DRM_ROTATE_180 | DRM_ROTATE_270;
  11362. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11363. supported_rotations =
  11364. DRM_ROTATE_0 | DRM_ROTATE_180 |
  11365. DRM_REFLECT_X;
  11366. } else if (INTEL_GEN(dev_priv) >= 4) {
  11367. supported_rotations =
  11368. DRM_ROTATE_0 | DRM_ROTATE_180;
  11369. } else {
  11370. supported_rotations = DRM_ROTATE_0;
  11371. }
  11372. if (INTEL_GEN(dev_priv) >= 4)
  11373. drm_plane_create_rotation_property(&primary->base,
  11374. DRM_ROTATE_0,
  11375. supported_rotations);
  11376. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11377. return primary;
  11378. fail:
  11379. kfree(state);
  11380. kfree(primary);
  11381. return ERR_PTR(ret);
  11382. }
  11383. static int
  11384. intel_check_cursor_plane(struct drm_plane *plane,
  11385. struct intel_crtc_state *crtc_state,
  11386. struct intel_plane_state *state)
  11387. {
  11388. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11389. struct drm_framebuffer *fb = state->base.fb;
  11390. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11391. enum pipe pipe = to_intel_plane(plane)->pipe;
  11392. unsigned stride;
  11393. int ret;
  11394. ret = drm_plane_helper_check_state(&state->base,
  11395. &state->clip,
  11396. DRM_PLANE_HELPER_NO_SCALING,
  11397. DRM_PLANE_HELPER_NO_SCALING,
  11398. true, true);
  11399. if (ret)
  11400. return ret;
  11401. /* if we want to turn off the cursor ignore width and height */
  11402. if (!obj)
  11403. return 0;
  11404. /* Check for which cursor types we support */
  11405. if (!cursor_size_ok(dev_priv, state->base.crtc_w,
  11406. state->base.crtc_h)) {
  11407. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11408. state->base.crtc_w, state->base.crtc_h);
  11409. return -EINVAL;
  11410. }
  11411. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11412. if (obj->base.size < stride * state->base.crtc_h) {
  11413. DRM_DEBUG_KMS("buffer is too small\n");
  11414. return -ENOMEM;
  11415. }
  11416. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  11417. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11418. return -EINVAL;
  11419. }
  11420. /*
  11421. * There's something wrong with the cursor on CHV pipe C.
  11422. * If it straddles the left edge of the screen then
  11423. * moving it away from the edge or disabling it often
  11424. * results in a pipe underrun, and often that can lead to
  11425. * dead pipe (constant underrun reported, and it scans
  11426. * out just a solid color). To recover from that, the
  11427. * display power well must be turned off and on again.
  11428. * Refuse the put the cursor into that compromised position.
  11429. */
  11430. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  11431. state->base.visible && state->base.crtc_x < 0) {
  11432. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  11433. return -EINVAL;
  11434. }
  11435. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  11436. state->ctl = i845_cursor_ctl(crtc_state, state);
  11437. else
  11438. state->ctl = i9xx_cursor_ctl(crtc_state, state);
  11439. return 0;
  11440. }
  11441. static void
  11442. intel_disable_cursor_plane(struct drm_plane *plane,
  11443. struct drm_crtc *crtc)
  11444. {
  11445. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11446. intel_crtc->cursor_addr = 0;
  11447. intel_crtc_update_cursor(crtc, NULL);
  11448. }
  11449. static void
  11450. intel_update_cursor_plane(struct drm_plane *plane,
  11451. const struct intel_crtc_state *crtc_state,
  11452. const struct intel_plane_state *state)
  11453. {
  11454. struct drm_crtc *crtc = crtc_state->base.crtc;
  11455. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11456. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11457. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11458. uint32_t addr;
  11459. if (!obj)
  11460. addr = 0;
  11461. else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
  11462. addr = intel_plane_ggtt_offset(state);
  11463. else
  11464. addr = obj->phys_handle->busaddr;
  11465. intel_crtc->cursor_addr = addr;
  11466. intel_crtc_update_cursor(crtc, state);
  11467. }
  11468. static struct intel_plane *
  11469. intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11470. {
  11471. struct intel_plane *cursor = NULL;
  11472. struct intel_plane_state *state = NULL;
  11473. int ret;
  11474. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11475. if (!cursor) {
  11476. ret = -ENOMEM;
  11477. goto fail;
  11478. }
  11479. state = intel_create_plane_state(&cursor->base);
  11480. if (!state) {
  11481. ret = -ENOMEM;
  11482. goto fail;
  11483. }
  11484. cursor->base.state = &state->base;
  11485. cursor->can_scale = false;
  11486. cursor->max_downscale = 1;
  11487. cursor->pipe = pipe;
  11488. cursor->plane = pipe;
  11489. cursor->id = PLANE_CURSOR;
  11490. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11491. cursor->check_plane = intel_check_cursor_plane;
  11492. cursor->update_plane = intel_update_cursor_plane;
  11493. cursor->disable_plane = intel_disable_cursor_plane;
  11494. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11495. 0, &intel_cursor_plane_funcs,
  11496. intel_cursor_formats,
  11497. ARRAY_SIZE(intel_cursor_formats),
  11498. DRM_PLANE_TYPE_CURSOR,
  11499. "cursor %c", pipe_name(pipe));
  11500. if (ret)
  11501. goto fail;
  11502. if (INTEL_GEN(dev_priv) >= 4)
  11503. drm_plane_create_rotation_property(&cursor->base,
  11504. DRM_ROTATE_0,
  11505. DRM_ROTATE_0 |
  11506. DRM_ROTATE_180);
  11507. if (INTEL_GEN(dev_priv) >= 9)
  11508. state->scaler_id = -1;
  11509. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11510. return cursor;
  11511. fail:
  11512. kfree(state);
  11513. kfree(cursor);
  11514. return ERR_PTR(ret);
  11515. }
  11516. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11517. struct intel_crtc_state *crtc_state)
  11518. {
  11519. struct intel_crtc_scaler_state *scaler_state =
  11520. &crtc_state->scaler_state;
  11521. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11522. int i;
  11523. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11524. if (!crtc->num_scalers)
  11525. return;
  11526. for (i = 0; i < crtc->num_scalers; i++) {
  11527. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11528. scaler->in_use = 0;
  11529. scaler->mode = PS_SCALER_MODE_DYN;
  11530. }
  11531. scaler_state->scaler_id = -1;
  11532. }
  11533. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11534. {
  11535. struct intel_crtc *intel_crtc;
  11536. struct intel_crtc_state *crtc_state = NULL;
  11537. struct intel_plane *primary = NULL;
  11538. struct intel_plane *cursor = NULL;
  11539. int sprite, ret;
  11540. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11541. if (!intel_crtc)
  11542. return -ENOMEM;
  11543. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11544. if (!crtc_state) {
  11545. ret = -ENOMEM;
  11546. goto fail;
  11547. }
  11548. intel_crtc->config = crtc_state;
  11549. intel_crtc->base.state = &crtc_state->base;
  11550. crtc_state->base.crtc = &intel_crtc->base;
  11551. primary = intel_primary_plane_create(dev_priv, pipe);
  11552. if (IS_ERR(primary)) {
  11553. ret = PTR_ERR(primary);
  11554. goto fail;
  11555. }
  11556. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11557. for_each_sprite(dev_priv, pipe, sprite) {
  11558. struct intel_plane *plane;
  11559. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11560. if (IS_ERR(plane)) {
  11561. ret = PTR_ERR(plane);
  11562. goto fail;
  11563. }
  11564. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11565. }
  11566. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11567. if (IS_ERR(cursor)) {
  11568. ret = PTR_ERR(cursor);
  11569. goto fail;
  11570. }
  11571. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11572. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11573. &primary->base, &cursor->base,
  11574. &intel_crtc_funcs,
  11575. "pipe %c", pipe_name(pipe));
  11576. if (ret)
  11577. goto fail;
  11578. intel_crtc->pipe = pipe;
  11579. intel_crtc->plane = primary->plane;
  11580. intel_crtc->cursor_base = ~0;
  11581. intel_crtc->cursor_cntl = ~0;
  11582. intel_crtc->cursor_size = ~0;
  11583. /* initialize shared scalers */
  11584. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11585. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11586. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11587. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  11588. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11589. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11590. intel_color_init(&intel_crtc->base);
  11591. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11592. return 0;
  11593. fail:
  11594. /*
  11595. * drm_mode_config_cleanup() will free up any
  11596. * crtcs/planes already initialized.
  11597. */
  11598. kfree(crtc_state);
  11599. kfree(intel_crtc);
  11600. return ret;
  11601. }
  11602. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11603. {
  11604. struct drm_device *dev = connector->base.dev;
  11605. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11606. if (!connector->base.state->crtc)
  11607. return INVALID_PIPE;
  11608. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11609. }
  11610. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11611. struct drm_file *file)
  11612. {
  11613. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11614. struct drm_crtc *drmmode_crtc;
  11615. struct intel_crtc *crtc;
  11616. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11617. if (!drmmode_crtc)
  11618. return -ENOENT;
  11619. crtc = to_intel_crtc(drmmode_crtc);
  11620. pipe_from_crtc_id->pipe = crtc->pipe;
  11621. return 0;
  11622. }
  11623. static int intel_encoder_clones(struct intel_encoder *encoder)
  11624. {
  11625. struct drm_device *dev = encoder->base.dev;
  11626. struct intel_encoder *source_encoder;
  11627. int index_mask = 0;
  11628. int entry = 0;
  11629. for_each_intel_encoder(dev, source_encoder) {
  11630. if (encoders_cloneable(encoder, source_encoder))
  11631. index_mask |= (1 << entry);
  11632. entry++;
  11633. }
  11634. return index_mask;
  11635. }
  11636. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11637. {
  11638. if (!IS_MOBILE(dev_priv))
  11639. return false;
  11640. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11641. return false;
  11642. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11643. return false;
  11644. return true;
  11645. }
  11646. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11647. {
  11648. if (INTEL_GEN(dev_priv) >= 9)
  11649. return false;
  11650. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11651. return false;
  11652. if (IS_CHERRYVIEW(dev_priv))
  11653. return false;
  11654. if (HAS_PCH_LPT_H(dev_priv) &&
  11655. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11656. return false;
  11657. /* DDI E can't be used if DDI A requires 4 lanes */
  11658. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11659. return false;
  11660. if (!dev_priv->vbt.int_crt_support)
  11661. return false;
  11662. return true;
  11663. }
  11664. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11665. {
  11666. int pps_num;
  11667. int pps_idx;
  11668. if (HAS_DDI(dev_priv))
  11669. return;
  11670. /*
  11671. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11672. * everywhere where registers can be write protected.
  11673. */
  11674. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11675. pps_num = 2;
  11676. else
  11677. pps_num = 1;
  11678. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11679. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11680. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11681. I915_WRITE(PP_CONTROL(pps_idx), val);
  11682. }
  11683. }
  11684. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11685. {
  11686. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11687. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11688. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11689. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11690. else
  11691. dev_priv->pps_mmio_base = PPS_BASE;
  11692. intel_pps_unlock_regs_wa(dev_priv);
  11693. }
  11694. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11695. {
  11696. struct intel_encoder *encoder;
  11697. bool dpd_is_edp = false;
  11698. intel_pps_init(dev_priv);
  11699. /*
  11700. * intel_edp_init_connector() depends on this completing first, to
  11701. * prevent the registeration of both eDP and LVDS and the incorrect
  11702. * sharing of the PPS.
  11703. */
  11704. intel_lvds_init(dev_priv);
  11705. if (intel_crt_present(dev_priv))
  11706. intel_crt_init(dev_priv);
  11707. if (IS_GEN9_LP(dev_priv)) {
  11708. /*
  11709. * FIXME: Broxton doesn't support port detection via the
  11710. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11711. * detect the ports.
  11712. */
  11713. intel_ddi_init(dev_priv, PORT_A);
  11714. intel_ddi_init(dev_priv, PORT_B);
  11715. intel_ddi_init(dev_priv, PORT_C);
  11716. intel_dsi_init(dev_priv);
  11717. } else if (HAS_DDI(dev_priv)) {
  11718. int found;
  11719. /*
  11720. * Haswell uses DDI functions to detect digital outputs.
  11721. * On SKL pre-D0 the strap isn't connected, so we assume
  11722. * it's there.
  11723. */
  11724. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11725. /* WaIgnoreDDIAStrap: skl */
  11726. if (found || IS_GEN9_BC(dev_priv))
  11727. intel_ddi_init(dev_priv, PORT_A);
  11728. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11729. * register */
  11730. found = I915_READ(SFUSE_STRAP);
  11731. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11732. intel_ddi_init(dev_priv, PORT_B);
  11733. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11734. intel_ddi_init(dev_priv, PORT_C);
  11735. if (found & SFUSE_STRAP_DDID_DETECTED)
  11736. intel_ddi_init(dev_priv, PORT_D);
  11737. /*
  11738. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11739. */
  11740. if (IS_GEN9_BC(dev_priv) &&
  11741. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11742. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11743. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11744. intel_ddi_init(dev_priv, PORT_E);
  11745. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11746. int found;
  11747. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  11748. if (has_edp_a(dev_priv))
  11749. intel_dp_init(dev_priv, DP_A, PORT_A);
  11750. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11751. /* PCH SDVOB multiplex with HDMIB */
  11752. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11753. if (!found)
  11754. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11755. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11756. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11757. }
  11758. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11759. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11760. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11761. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11762. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11763. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11764. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11765. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11766. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11767. bool has_edp, has_port;
  11768. /*
  11769. * The DP_DETECTED bit is the latched state of the DDC
  11770. * SDA pin at boot. However since eDP doesn't require DDC
  11771. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11772. * eDP ports may have been muxed to an alternate function.
  11773. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11774. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11775. * detect eDP ports.
  11776. *
  11777. * Sadly the straps seem to be missing sometimes even for HDMI
  11778. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11779. * and VBT for the presence of the port. Additionally we can't
  11780. * trust the port type the VBT declares as we've seen at least
  11781. * HDMI ports that the VBT claim are DP or eDP.
  11782. */
  11783. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  11784. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11785. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11786. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11787. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11788. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11789. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  11790. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11791. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11792. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11793. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11794. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11795. if (IS_CHERRYVIEW(dev_priv)) {
  11796. /*
  11797. * eDP not supported on port D,
  11798. * so no need to worry about it
  11799. */
  11800. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11801. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11802. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11803. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11804. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11805. }
  11806. intel_dsi_init(dev_priv);
  11807. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11808. bool found = false;
  11809. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11810. DRM_DEBUG_KMS("probing SDVOB\n");
  11811. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11812. if (!found && IS_G4X(dev_priv)) {
  11813. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11814. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11815. }
  11816. if (!found && IS_G4X(dev_priv))
  11817. intel_dp_init(dev_priv, DP_B, PORT_B);
  11818. }
  11819. /* Before G4X SDVOC doesn't have its own detect register */
  11820. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11821. DRM_DEBUG_KMS("probing SDVOC\n");
  11822. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11823. }
  11824. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11825. if (IS_G4X(dev_priv)) {
  11826. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11827. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11828. }
  11829. if (IS_G4X(dev_priv))
  11830. intel_dp_init(dev_priv, DP_C, PORT_C);
  11831. }
  11832. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11833. intel_dp_init(dev_priv, DP_D, PORT_D);
  11834. } else if (IS_GEN2(dev_priv))
  11835. intel_dvo_init(dev_priv);
  11836. if (SUPPORTS_TV(dev_priv))
  11837. intel_tv_init(dev_priv);
  11838. intel_psr_init(dev_priv);
  11839. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11840. encoder->base.possible_crtcs = encoder->crtc_mask;
  11841. encoder->base.possible_clones =
  11842. intel_encoder_clones(encoder);
  11843. }
  11844. intel_init_pch_refclk(dev_priv);
  11845. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11846. }
  11847. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11848. {
  11849. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11850. drm_framebuffer_cleanup(fb);
  11851. i915_gem_object_lock(intel_fb->obj);
  11852. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11853. i915_gem_object_unlock(intel_fb->obj);
  11854. i915_gem_object_put(intel_fb->obj);
  11855. kfree(intel_fb);
  11856. }
  11857. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11858. struct drm_file *file,
  11859. unsigned int *handle)
  11860. {
  11861. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11862. struct drm_i915_gem_object *obj = intel_fb->obj;
  11863. if (obj->userptr.mm) {
  11864. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11865. return -EINVAL;
  11866. }
  11867. return drm_gem_handle_create(file, &obj->base, handle);
  11868. }
  11869. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11870. struct drm_file *file,
  11871. unsigned flags, unsigned color,
  11872. struct drm_clip_rect *clips,
  11873. unsigned num_clips)
  11874. {
  11875. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11876. i915_gem_object_flush_if_display(obj);
  11877. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11878. return 0;
  11879. }
  11880. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11881. .destroy = intel_user_framebuffer_destroy,
  11882. .create_handle = intel_user_framebuffer_create_handle,
  11883. .dirty = intel_user_framebuffer_dirty,
  11884. };
  11885. static
  11886. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11887. uint64_t fb_modifier, uint32_t pixel_format)
  11888. {
  11889. u32 gen = INTEL_GEN(dev_priv);
  11890. if (gen >= 9) {
  11891. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11892. /* "The stride in bytes must not exceed the of the size of 8K
  11893. * pixels and 32K bytes."
  11894. */
  11895. return min(8192 * cpp, 32768);
  11896. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11897. return 32*1024;
  11898. } else if (gen >= 4) {
  11899. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11900. return 16*1024;
  11901. else
  11902. return 32*1024;
  11903. } else if (gen >= 3) {
  11904. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11905. return 8*1024;
  11906. else
  11907. return 16*1024;
  11908. } else {
  11909. /* XXX DSPC is limited to 4k tiled */
  11910. return 8*1024;
  11911. }
  11912. }
  11913. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11914. struct drm_i915_gem_object *obj,
  11915. struct drm_mode_fb_cmd2 *mode_cmd)
  11916. {
  11917. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11918. struct drm_format_name_buf format_name;
  11919. u32 pitch_limit, stride_alignment;
  11920. unsigned int tiling, stride;
  11921. int ret = -EINVAL;
  11922. i915_gem_object_lock(obj);
  11923. obj->framebuffer_references++;
  11924. tiling = i915_gem_object_get_tiling(obj);
  11925. stride = i915_gem_object_get_stride(obj);
  11926. i915_gem_object_unlock(obj);
  11927. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11928. /*
  11929. * If there's a fence, enforce that
  11930. * the fb modifier and tiling mode match.
  11931. */
  11932. if (tiling != I915_TILING_NONE &&
  11933. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11934. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11935. goto err;
  11936. }
  11937. } else {
  11938. if (tiling == I915_TILING_X) {
  11939. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11940. } else if (tiling == I915_TILING_Y) {
  11941. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11942. goto err;
  11943. }
  11944. }
  11945. /* Passed in modifier sanity checking. */
  11946. switch (mode_cmd->modifier[0]) {
  11947. case I915_FORMAT_MOD_Y_TILED:
  11948. case I915_FORMAT_MOD_Yf_TILED:
  11949. if (INTEL_GEN(dev_priv) < 9) {
  11950. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  11951. mode_cmd->modifier[0]);
  11952. goto err;
  11953. }
  11954. case DRM_FORMAT_MOD_LINEAR:
  11955. case I915_FORMAT_MOD_X_TILED:
  11956. break;
  11957. default:
  11958. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  11959. mode_cmd->modifier[0]);
  11960. goto err;
  11961. }
  11962. /*
  11963. * gen2/3 display engine uses the fence if present,
  11964. * so the tiling mode must match the fb modifier exactly.
  11965. */
  11966. if (INTEL_INFO(dev_priv)->gen < 4 &&
  11967. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11968. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  11969. goto err;
  11970. }
  11971. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11972. mode_cmd->pixel_format);
  11973. if (mode_cmd->pitches[0] > pitch_limit) {
  11974. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  11975. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  11976. "tiled" : "linear",
  11977. mode_cmd->pitches[0], pitch_limit);
  11978. goto err;
  11979. }
  11980. /*
  11981. * If there's a fence, enforce that
  11982. * the fb pitch and fence stride match.
  11983. */
  11984. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  11985. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  11986. mode_cmd->pitches[0], stride);
  11987. goto err;
  11988. }
  11989. /* Reject formats not supported by any plane early. */
  11990. switch (mode_cmd->pixel_format) {
  11991. case DRM_FORMAT_C8:
  11992. case DRM_FORMAT_RGB565:
  11993. case DRM_FORMAT_XRGB8888:
  11994. case DRM_FORMAT_ARGB8888:
  11995. break;
  11996. case DRM_FORMAT_XRGB1555:
  11997. if (INTEL_GEN(dev_priv) > 3) {
  11998. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11999. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12000. goto err;
  12001. }
  12002. break;
  12003. case DRM_FORMAT_ABGR8888:
  12004. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  12005. INTEL_GEN(dev_priv) < 9) {
  12006. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12007. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12008. goto err;
  12009. }
  12010. break;
  12011. case DRM_FORMAT_XBGR8888:
  12012. case DRM_FORMAT_XRGB2101010:
  12013. case DRM_FORMAT_XBGR2101010:
  12014. if (INTEL_GEN(dev_priv) < 4) {
  12015. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12016. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12017. goto err;
  12018. }
  12019. break;
  12020. case DRM_FORMAT_ABGR2101010:
  12021. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  12022. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12023. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12024. goto err;
  12025. }
  12026. break;
  12027. case DRM_FORMAT_YUYV:
  12028. case DRM_FORMAT_UYVY:
  12029. case DRM_FORMAT_YVYU:
  12030. case DRM_FORMAT_VYUY:
  12031. if (INTEL_GEN(dev_priv) < 5) {
  12032. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12033. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12034. goto err;
  12035. }
  12036. break;
  12037. default:
  12038. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12039. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12040. goto err;
  12041. }
  12042. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12043. if (mode_cmd->offsets[0] != 0)
  12044. goto err;
  12045. drm_helper_mode_fill_fb_struct(&dev_priv->drm,
  12046. &intel_fb->base, mode_cmd);
  12047. stride_alignment = intel_fb_stride_alignment(&intel_fb->base, 0);
  12048. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12049. DRM_DEBUG_KMS("pitch (%d) must be at least %u byte aligned\n",
  12050. mode_cmd->pitches[0], stride_alignment);
  12051. goto err;
  12052. }
  12053. intel_fb->obj = obj;
  12054. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  12055. if (ret)
  12056. goto err;
  12057. ret = drm_framebuffer_init(obj->base.dev,
  12058. &intel_fb->base,
  12059. &intel_fb_funcs);
  12060. if (ret) {
  12061. DRM_ERROR("framebuffer init failed %d\n", ret);
  12062. goto err;
  12063. }
  12064. return 0;
  12065. err:
  12066. i915_gem_object_lock(obj);
  12067. obj->framebuffer_references--;
  12068. i915_gem_object_unlock(obj);
  12069. return ret;
  12070. }
  12071. static struct drm_framebuffer *
  12072. intel_user_framebuffer_create(struct drm_device *dev,
  12073. struct drm_file *filp,
  12074. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12075. {
  12076. struct drm_framebuffer *fb;
  12077. struct drm_i915_gem_object *obj;
  12078. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12079. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  12080. if (!obj)
  12081. return ERR_PTR(-ENOENT);
  12082. fb = intel_framebuffer_create(obj, &mode_cmd);
  12083. if (IS_ERR(fb))
  12084. i915_gem_object_put(obj);
  12085. return fb;
  12086. }
  12087. static void intel_atomic_state_free(struct drm_atomic_state *state)
  12088. {
  12089. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12090. drm_atomic_state_default_release(state);
  12091. i915_sw_fence_fini(&intel_state->commit_ready);
  12092. kfree(state);
  12093. }
  12094. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12095. .fb_create = intel_user_framebuffer_create,
  12096. .output_poll_changed = intel_fbdev_output_poll_changed,
  12097. .atomic_check = intel_atomic_check,
  12098. .atomic_commit = intel_atomic_commit,
  12099. .atomic_state_alloc = intel_atomic_state_alloc,
  12100. .atomic_state_clear = intel_atomic_state_clear,
  12101. .atomic_state_free = intel_atomic_state_free,
  12102. };
  12103. /**
  12104. * intel_init_display_hooks - initialize the display modesetting hooks
  12105. * @dev_priv: device private
  12106. */
  12107. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12108. {
  12109. intel_init_cdclk_hooks(dev_priv);
  12110. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12111. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12112. dev_priv->display.get_initial_plane_config =
  12113. skylake_get_initial_plane_config;
  12114. dev_priv->display.crtc_compute_clock =
  12115. haswell_crtc_compute_clock;
  12116. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12117. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12118. } else if (HAS_DDI(dev_priv)) {
  12119. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12120. dev_priv->display.get_initial_plane_config =
  12121. ironlake_get_initial_plane_config;
  12122. dev_priv->display.crtc_compute_clock =
  12123. haswell_crtc_compute_clock;
  12124. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12125. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12126. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12127. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12128. dev_priv->display.get_initial_plane_config =
  12129. ironlake_get_initial_plane_config;
  12130. dev_priv->display.crtc_compute_clock =
  12131. ironlake_crtc_compute_clock;
  12132. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12133. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12134. } else if (IS_CHERRYVIEW(dev_priv)) {
  12135. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12136. dev_priv->display.get_initial_plane_config =
  12137. i9xx_get_initial_plane_config;
  12138. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12139. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12140. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12141. } else if (IS_VALLEYVIEW(dev_priv)) {
  12142. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12143. dev_priv->display.get_initial_plane_config =
  12144. i9xx_get_initial_plane_config;
  12145. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12146. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12147. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12148. } else if (IS_G4X(dev_priv)) {
  12149. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12150. dev_priv->display.get_initial_plane_config =
  12151. i9xx_get_initial_plane_config;
  12152. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12153. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12154. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12155. } else if (IS_PINEVIEW(dev_priv)) {
  12156. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12157. dev_priv->display.get_initial_plane_config =
  12158. i9xx_get_initial_plane_config;
  12159. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12160. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12161. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12162. } else if (!IS_GEN2(dev_priv)) {
  12163. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12164. dev_priv->display.get_initial_plane_config =
  12165. i9xx_get_initial_plane_config;
  12166. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12167. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12168. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12169. } else {
  12170. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12171. dev_priv->display.get_initial_plane_config =
  12172. i9xx_get_initial_plane_config;
  12173. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12174. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12175. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12176. }
  12177. if (IS_GEN5(dev_priv)) {
  12178. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12179. } else if (IS_GEN6(dev_priv)) {
  12180. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12181. } else if (IS_IVYBRIDGE(dev_priv)) {
  12182. /* FIXME: detect B0+ stepping and use auto training */
  12183. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12184. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12185. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12186. }
  12187. if (dev_priv->info.gen >= 9)
  12188. dev_priv->display.update_crtcs = skl_update_crtcs;
  12189. else
  12190. dev_priv->display.update_crtcs = intel_update_crtcs;
  12191. switch (INTEL_INFO(dev_priv)->gen) {
  12192. case 2:
  12193. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12194. break;
  12195. case 3:
  12196. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12197. break;
  12198. case 4:
  12199. case 5:
  12200. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12201. break;
  12202. case 6:
  12203. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12204. break;
  12205. case 7:
  12206. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12207. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12208. break;
  12209. case 9:
  12210. /* Drop through - unsupported since execlist only. */
  12211. default:
  12212. /* Default just returns -ENODEV to indicate unsupported */
  12213. dev_priv->display.queue_flip = intel_default_queue_flip;
  12214. }
  12215. }
  12216. /*
  12217. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12218. * resume, or other times. This quirk makes sure that's the case for
  12219. * affected systems.
  12220. */
  12221. static void quirk_pipea_force(struct drm_device *dev)
  12222. {
  12223. struct drm_i915_private *dev_priv = to_i915(dev);
  12224. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12225. DRM_INFO("applying pipe a force quirk\n");
  12226. }
  12227. static void quirk_pipeb_force(struct drm_device *dev)
  12228. {
  12229. struct drm_i915_private *dev_priv = to_i915(dev);
  12230. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12231. DRM_INFO("applying pipe b force quirk\n");
  12232. }
  12233. /*
  12234. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12235. */
  12236. static void quirk_ssc_force_disable(struct drm_device *dev)
  12237. {
  12238. struct drm_i915_private *dev_priv = to_i915(dev);
  12239. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12240. DRM_INFO("applying lvds SSC disable quirk\n");
  12241. }
  12242. /*
  12243. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12244. * brightness value
  12245. */
  12246. static void quirk_invert_brightness(struct drm_device *dev)
  12247. {
  12248. struct drm_i915_private *dev_priv = to_i915(dev);
  12249. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12250. DRM_INFO("applying inverted panel brightness quirk\n");
  12251. }
  12252. /* Some VBT's incorrectly indicate no backlight is present */
  12253. static void quirk_backlight_present(struct drm_device *dev)
  12254. {
  12255. struct drm_i915_private *dev_priv = to_i915(dev);
  12256. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12257. DRM_INFO("applying backlight present quirk\n");
  12258. }
  12259. struct intel_quirk {
  12260. int device;
  12261. int subsystem_vendor;
  12262. int subsystem_device;
  12263. void (*hook)(struct drm_device *dev);
  12264. };
  12265. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12266. struct intel_dmi_quirk {
  12267. void (*hook)(struct drm_device *dev);
  12268. const struct dmi_system_id (*dmi_id_list)[];
  12269. };
  12270. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12271. {
  12272. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12273. return 1;
  12274. }
  12275. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12276. {
  12277. .dmi_id_list = &(const struct dmi_system_id[]) {
  12278. {
  12279. .callback = intel_dmi_reverse_brightness,
  12280. .ident = "NCR Corporation",
  12281. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12282. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12283. },
  12284. },
  12285. { } /* terminating entry */
  12286. },
  12287. .hook = quirk_invert_brightness,
  12288. },
  12289. };
  12290. static struct intel_quirk intel_quirks[] = {
  12291. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12292. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12293. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12294. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12295. /* 830 needs to leave pipe A & dpll A up */
  12296. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12297. /* 830 needs to leave pipe B & dpll B up */
  12298. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12299. /* Lenovo U160 cannot use SSC on LVDS */
  12300. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12301. /* Sony Vaio Y cannot use SSC on LVDS */
  12302. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12303. /* Acer Aspire 5734Z must invert backlight brightness */
  12304. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12305. /* Acer/eMachines G725 */
  12306. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12307. /* Acer/eMachines e725 */
  12308. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12309. /* Acer/Packard Bell NCL20 */
  12310. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12311. /* Acer Aspire 4736Z */
  12312. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12313. /* Acer Aspire 5336 */
  12314. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12315. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12316. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12317. /* Acer C720 Chromebook (Core i3 4005U) */
  12318. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12319. /* Apple Macbook 2,1 (Core 2 T7400) */
  12320. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12321. /* Apple Macbook 4,1 */
  12322. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12323. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12324. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12325. /* HP Chromebook 14 (Celeron 2955U) */
  12326. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12327. /* Dell Chromebook 11 */
  12328. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12329. /* Dell Chromebook 11 (2015 version) */
  12330. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12331. };
  12332. static void intel_init_quirks(struct drm_device *dev)
  12333. {
  12334. struct pci_dev *d = dev->pdev;
  12335. int i;
  12336. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12337. struct intel_quirk *q = &intel_quirks[i];
  12338. if (d->device == q->device &&
  12339. (d->subsystem_vendor == q->subsystem_vendor ||
  12340. q->subsystem_vendor == PCI_ANY_ID) &&
  12341. (d->subsystem_device == q->subsystem_device ||
  12342. q->subsystem_device == PCI_ANY_ID))
  12343. q->hook(dev);
  12344. }
  12345. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12346. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12347. intel_dmi_quirks[i].hook(dev);
  12348. }
  12349. }
  12350. /* Disable the VGA plane that we never use */
  12351. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12352. {
  12353. struct pci_dev *pdev = dev_priv->drm.pdev;
  12354. u8 sr1;
  12355. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12356. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12357. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12358. outb(SR01, VGA_SR_INDEX);
  12359. sr1 = inb(VGA_SR_DATA);
  12360. outb(sr1 | 1<<5, VGA_SR_DATA);
  12361. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12362. udelay(300);
  12363. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12364. POSTING_READ(vga_reg);
  12365. }
  12366. void intel_modeset_init_hw(struct drm_device *dev)
  12367. {
  12368. struct drm_i915_private *dev_priv = to_i915(dev);
  12369. intel_update_cdclk(dev_priv);
  12370. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12371. intel_init_clock_gating(dev_priv);
  12372. }
  12373. /*
  12374. * Calculate what we think the watermarks should be for the state we've read
  12375. * out of the hardware and then immediately program those watermarks so that
  12376. * we ensure the hardware settings match our internal state.
  12377. *
  12378. * We can calculate what we think WM's should be by creating a duplicate of the
  12379. * current state (which was constructed during hardware readout) and running it
  12380. * through the atomic check code to calculate new watermark values in the
  12381. * state object.
  12382. */
  12383. static void sanitize_watermarks(struct drm_device *dev)
  12384. {
  12385. struct drm_i915_private *dev_priv = to_i915(dev);
  12386. struct drm_atomic_state *state;
  12387. struct intel_atomic_state *intel_state;
  12388. struct drm_crtc *crtc;
  12389. struct drm_crtc_state *cstate;
  12390. struct drm_modeset_acquire_ctx ctx;
  12391. int ret;
  12392. int i;
  12393. /* Only supported on platforms that use atomic watermark design */
  12394. if (!dev_priv->display.optimize_watermarks)
  12395. return;
  12396. /*
  12397. * We need to hold connection_mutex before calling duplicate_state so
  12398. * that the connector loop is protected.
  12399. */
  12400. drm_modeset_acquire_init(&ctx, 0);
  12401. retry:
  12402. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12403. if (ret == -EDEADLK) {
  12404. drm_modeset_backoff(&ctx);
  12405. goto retry;
  12406. } else if (WARN_ON(ret)) {
  12407. goto fail;
  12408. }
  12409. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12410. if (WARN_ON(IS_ERR(state)))
  12411. goto fail;
  12412. intel_state = to_intel_atomic_state(state);
  12413. /*
  12414. * Hardware readout is the only time we don't want to calculate
  12415. * intermediate watermarks (since we don't trust the current
  12416. * watermarks).
  12417. */
  12418. if (!HAS_GMCH_DISPLAY(dev_priv))
  12419. intel_state->skip_intermediate_wm = true;
  12420. ret = intel_atomic_check(dev, state);
  12421. if (ret) {
  12422. /*
  12423. * If we fail here, it means that the hardware appears to be
  12424. * programmed in a way that shouldn't be possible, given our
  12425. * understanding of watermark requirements. This might mean a
  12426. * mistake in the hardware readout code or a mistake in the
  12427. * watermark calculations for a given platform. Raise a WARN
  12428. * so that this is noticeable.
  12429. *
  12430. * If this actually happens, we'll have to just leave the
  12431. * BIOS-programmed watermarks untouched and hope for the best.
  12432. */
  12433. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12434. goto put_state;
  12435. }
  12436. /* Write calculated watermark values back */
  12437. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12438. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12439. cs->wm.need_postvbl_update = true;
  12440. dev_priv->display.optimize_watermarks(intel_state, cs);
  12441. }
  12442. put_state:
  12443. drm_atomic_state_put(state);
  12444. fail:
  12445. drm_modeset_drop_locks(&ctx);
  12446. drm_modeset_acquire_fini(&ctx);
  12447. }
  12448. int intel_modeset_init(struct drm_device *dev)
  12449. {
  12450. struct drm_i915_private *dev_priv = to_i915(dev);
  12451. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12452. enum pipe pipe;
  12453. struct intel_crtc *crtc;
  12454. drm_mode_config_init(dev);
  12455. dev->mode_config.min_width = 0;
  12456. dev->mode_config.min_height = 0;
  12457. dev->mode_config.preferred_depth = 24;
  12458. dev->mode_config.prefer_shadow = 1;
  12459. dev->mode_config.allow_fb_modifiers = true;
  12460. dev->mode_config.funcs = &intel_mode_funcs;
  12461. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12462. intel_atomic_helper_free_state_worker);
  12463. intel_init_quirks(dev);
  12464. intel_init_pm(dev_priv);
  12465. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12466. return 0;
  12467. /*
  12468. * There may be no VBT; and if the BIOS enabled SSC we can
  12469. * just keep using it to avoid unnecessary flicker. Whereas if the
  12470. * BIOS isn't using it, don't assume it will work even if the VBT
  12471. * indicates as much.
  12472. */
  12473. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12474. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12475. DREF_SSC1_ENABLE);
  12476. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12477. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12478. bios_lvds_use_ssc ? "en" : "dis",
  12479. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12480. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12481. }
  12482. }
  12483. if (IS_GEN2(dev_priv)) {
  12484. dev->mode_config.max_width = 2048;
  12485. dev->mode_config.max_height = 2048;
  12486. } else if (IS_GEN3(dev_priv)) {
  12487. dev->mode_config.max_width = 4096;
  12488. dev->mode_config.max_height = 4096;
  12489. } else {
  12490. dev->mode_config.max_width = 8192;
  12491. dev->mode_config.max_height = 8192;
  12492. }
  12493. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12494. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12495. dev->mode_config.cursor_height = 1023;
  12496. } else if (IS_GEN2(dev_priv)) {
  12497. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12498. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12499. } else {
  12500. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12501. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12502. }
  12503. dev->mode_config.fb_base = ggtt->mappable_base;
  12504. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12505. INTEL_INFO(dev_priv)->num_pipes,
  12506. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12507. for_each_pipe(dev_priv, pipe) {
  12508. int ret;
  12509. ret = intel_crtc_init(dev_priv, pipe);
  12510. if (ret) {
  12511. drm_mode_config_cleanup(dev);
  12512. return ret;
  12513. }
  12514. }
  12515. intel_shared_dpll_init(dev);
  12516. intel_update_czclk(dev_priv);
  12517. intel_modeset_init_hw(dev);
  12518. if (dev_priv->max_cdclk_freq == 0)
  12519. intel_update_max_cdclk(dev_priv);
  12520. /* Just disable it once at startup */
  12521. i915_disable_vga(dev_priv);
  12522. intel_setup_outputs(dev_priv);
  12523. drm_modeset_lock_all(dev);
  12524. intel_modeset_setup_hw_state(dev);
  12525. drm_modeset_unlock_all(dev);
  12526. for_each_intel_crtc(dev, crtc) {
  12527. struct intel_initial_plane_config plane_config = {};
  12528. if (!crtc->active)
  12529. continue;
  12530. /*
  12531. * Note that reserving the BIOS fb up front prevents us
  12532. * from stuffing other stolen allocations like the ring
  12533. * on top. This prevents some ugliness at boot time, and
  12534. * can even allow for smooth boot transitions if the BIOS
  12535. * fb is large enough for the active pipe configuration.
  12536. */
  12537. dev_priv->display.get_initial_plane_config(crtc,
  12538. &plane_config);
  12539. /*
  12540. * If the fb is shared between multiple heads, we'll
  12541. * just get the first one.
  12542. */
  12543. intel_find_initial_plane_obj(crtc, &plane_config);
  12544. }
  12545. /*
  12546. * Make sure hardware watermarks really match the state we read out.
  12547. * Note that we need to do this after reconstructing the BIOS fb's
  12548. * since the watermark calculation done here will use pstate->fb.
  12549. */
  12550. if (!HAS_GMCH_DISPLAY(dev_priv))
  12551. sanitize_watermarks(dev);
  12552. return 0;
  12553. }
  12554. static void intel_enable_pipe_a(struct drm_device *dev)
  12555. {
  12556. struct intel_connector *connector;
  12557. struct drm_connector_list_iter conn_iter;
  12558. struct drm_connector *crt = NULL;
  12559. struct intel_load_detect_pipe load_detect_temp;
  12560. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12561. int ret;
  12562. /* We can't just switch on the pipe A, we need to set things up with a
  12563. * proper mode and output configuration. As a gross hack, enable pipe A
  12564. * by enabling the load detect pipe once. */
  12565. drm_connector_list_iter_begin(dev, &conn_iter);
  12566. for_each_intel_connector_iter(connector, &conn_iter) {
  12567. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12568. crt = &connector->base;
  12569. break;
  12570. }
  12571. }
  12572. drm_connector_list_iter_end(&conn_iter);
  12573. if (!crt)
  12574. return;
  12575. ret = intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx);
  12576. WARN(ret < 0, "All modeset mutexes are locked, but intel_get_load_detect_pipe failed\n");
  12577. if (ret > 0)
  12578. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12579. }
  12580. static bool
  12581. intel_check_plane_mapping(struct intel_crtc *crtc)
  12582. {
  12583. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12584. u32 val;
  12585. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  12586. return true;
  12587. val = I915_READ(DSPCNTR(!crtc->plane));
  12588. if ((val & DISPLAY_PLANE_ENABLE) &&
  12589. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12590. return false;
  12591. return true;
  12592. }
  12593. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12594. {
  12595. struct drm_device *dev = crtc->base.dev;
  12596. struct intel_encoder *encoder;
  12597. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12598. return true;
  12599. return false;
  12600. }
  12601. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12602. {
  12603. struct drm_device *dev = encoder->base.dev;
  12604. struct intel_connector *connector;
  12605. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12606. return connector;
  12607. return NULL;
  12608. }
  12609. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12610. enum transcoder pch_transcoder)
  12611. {
  12612. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12613. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  12614. }
  12615. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12616. {
  12617. struct drm_device *dev = crtc->base.dev;
  12618. struct drm_i915_private *dev_priv = to_i915(dev);
  12619. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12620. /* Clear any frame start delays used for debugging left by the BIOS */
  12621. if (!transcoder_is_dsi(cpu_transcoder)) {
  12622. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12623. I915_WRITE(reg,
  12624. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12625. }
  12626. /* restore vblank interrupts to correct state */
  12627. drm_crtc_vblank_reset(&crtc->base);
  12628. if (crtc->active) {
  12629. struct intel_plane *plane;
  12630. drm_crtc_vblank_on(&crtc->base);
  12631. /* Disable everything but the primary plane */
  12632. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12633. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12634. continue;
  12635. trace_intel_disable_plane(&plane->base, crtc);
  12636. plane->disable_plane(&plane->base, &crtc->base);
  12637. }
  12638. }
  12639. /* We need to sanitize the plane -> pipe mapping first because this will
  12640. * disable the crtc (and hence change the state) if it is wrong. Note
  12641. * that gen4+ has a fixed plane -> pipe mapping. */
  12642. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  12643. bool plane;
  12644. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  12645. crtc->base.base.id, crtc->base.name);
  12646. /* Pipe has the wrong plane attached and the plane is active.
  12647. * Temporarily change the plane mapping and disable everything
  12648. * ... */
  12649. plane = crtc->plane;
  12650. crtc->base.primary->state->visible = true;
  12651. crtc->plane = !plane;
  12652. intel_crtc_disable_noatomic(&crtc->base);
  12653. crtc->plane = plane;
  12654. }
  12655. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12656. crtc->pipe == PIPE_A && !crtc->active) {
  12657. /* BIOS forgot to enable pipe A, this mostly happens after
  12658. * resume. Force-enable the pipe to fix this, the update_dpms
  12659. * call below we restore the pipe to the right state, but leave
  12660. * the required bits on. */
  12661. intel_enable_pipe_a(dev);
  12662. }
  12663. /* Adjust the state of the output pipe according to whether we
  12664. * have active connectors/encoders. */
  12665. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12666. intel_crtc_disable_noatomic(&crtc->base);
  12667. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12668. /*
  12669. * We start out with underrun reporting disabled to avoid races.
  12670. * For correct bookkeeping mark this on active crtcs.
  12671. *
  12672. * Also on gmch platforms we dont have any hardware bits to
  12673. * disable the underrun reporting. Which means we need to start
  12674. * out with underrun reporting disabled also on inactive pipes,
  12675. * since otherwise we'll complain about the garbage we read when
  12676. * e.g. coming up after runtime pm.
  12677. *
  12678. * No protection against concurrent access is required - at
  12679. * worst a fifo underrun happens which also sets this to false.
  12680. */
  12681. crtc->cpu_fifo_underrun_disabled = true;
  12682. /*
  12683. * We track the PCH trancoder underrun reporting state
  12684. * within the crtc. With crtc for pipe A housing the underrun
  12685. * reporting state for PCH transcoder A, crtc for pipe B housing
  12686. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12687. * and marking underrun reporting as disabled for the non-existing
  12688. * PCH transcoders B and C would prevent enabling the south
  12689. * error interrupt (see cpt_can_enable_serr_int()).
  12690. */
  12691. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  12692. crtc->pch_fifo_underrun_disabled = true;
  12693. }
  12694. }
  12695. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12696. {
  12697. struct intel_connector *connector;
  12698. /* We need to check both for a crtc link (meaning that the
  12699. * encoder is active and trying to read from a pipe) and the
  12700. * pipe itself being active. */
  12701. bool has_active_crtc = encoder->base.crtc &&
  12702. to_intel_crtc(encoder->base.crtc)->active;
  12703. connector = intel_encoder_find_connector(encoder);
  12704. if (connector && !has_active_crtc) {
  12705. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12706. encoder->base.base.id,
  12707. encoder->base.name);
  12708. /* Connector is active, but has no active pipe. This is
  12709. * fallout from our resume register restoring. Disable
  12710. * the encoder manually again. */
  12711. if (encoder->base.crtc) {
  12712. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12713. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12714. encoder->base.base.id,
  12715. encoder->base.name);
  12716. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12717. if (encoder->post_disable)
  12718. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12719. }
  12720. encoder->base.crtc = NULL;
  12721. /* Inconsistent output/port/pipe state happens presumably due to
  12722. * a bug in one of the get_hw_state functions. Or someplace else
  12723. * in our code, like the register restore mess on resume. Clamp
  12724. * things to off as a safer default. */
  12725. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12726. connector->base.encoder = NULL;
  12727. }
  12728. /* Enabled encoders without active connectors will be fixed in
  12729. * the crtc fixup. */
  12730. }
  12731. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12732. {
  12733. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12734. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12735. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12736. i915_disable_vga(dev_priv);
  12737. }
  12738. }
  12739. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12740. {
  12741. /* This function can be called both from intel_modeset_setup_hw_state or
  12742. * at a very early point in our resume sequence, where the power well
  12743. * structures are not yet restored. Since this function is at a very
  12744. * paranoid "someone might have enabled VGA while we were not looking"
  12745. * level, just check if the power well is enabled instead of trying to
  12746. * follow the "don't touch the power well if we don't need it" policy
  12747. * the rest of the driver uses. */
  12748. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12749. return;
  12750. i915_redisable_vga_power_on(dev_priv);
  12751. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12752. }
  12753. static bool primary_get_hw_state(struct intel_plane *plane)
  12754. {
  12755. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12756. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12757. }
  12758. /* FIXME read out full plane state for all planes */
  12759. static void readout_plane_state(struct intel_crtc *crtc)
  12760. {
  12761. struct intel_plane *primary = to_intel_plane(crtc->base.primary);
  12762. bool visible;
  12763. visible = crtc->active && primary_get_hw_state(primary);
  12764. intel_set_plane_visible(to_intel_crtc_state(crtc->base.state),
  12765. to_intel_plane_state(primary->base.state),
  12766. visible);
  12767. }
  12768. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12769. {
  12770. struct drm_i915_private *dev_priv = to_i915(dev);
  12771. enum pipe pipe;
  12772. struct intel_crtc *crtc;
  12773. struct intel_encoder *encoder;
  12774. struct intel_connector *connector;
  12775. struct drm_connector_list_iter conn_iter;
  12776. int i;
  12777. dev_priv->active_crtcs = 0;
  12778. for_each_intel_crtc(dev, crtc) {
  12779. struct intel_crtc_state *crtc_state =
  12780. to_intel_crtc_state(crtc->base.state);
  12781. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12782. memset(crtc_state, 0, sizeof(*crtc_state));
  12783. crtc_state->base.crtc = &crtc->base;
  12784. crtc_state->base.active = crtc_state->base.enable =
  12785. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12786. crtc->base.enabled = crtc_state->base.enable;
  12787. crtc->active = crtc_state->base.active;
  12788. if (crtc_state->base.active)
  12789. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12790. readout_plane_state(crtc);
  12791. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12792. crtc->base.base.id, crtc->base.name,
  12793. enableddisabled(crtc_state->base.active));
  12794. }
  12795. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12796. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12797. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12798. &pll->state.hw_state);
  12799. pll->state.crtc_mask = 0;
  12800. for_each_intel_crtc(dev, crtc) {
  12801. struct intel_crtc_state *crtc_state =
  12802. to_intel_crtc_state(crtc->base.state);
  12803. if (crtc_state->base.active &&
  12804. crtc_state->shared_dpll == pll)
  12805. pll->state.crtc_mask |= 1 << crtc->pipe;
  12806. }
  12807. pll->active_mask = pll->state.crtc_mask;
  12808. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12809. pll->name, pll->state.crtc_mask, pll->on);
  12810. }
  12811. for_each_intel_encoder(dev, encoder) {
  12812. pipe = 0;
  12813. if (encoder->get_hw_state(encoder, &pipe)) {
  12814. struct intel_crtc_state *crtc_state;
  12815. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12816. crtc_state = to_intel_crtc_state(crtc->base.state);
  12817. encoder->base.crtc = &crtc->base;
  12818. crtc_state->output_types |= 1 << encoder->type;
  12819. encoder->get_config(encoder, crtc_state);
  12820. } else {
  12821. encoder->base.crtc = NULL;
  12822. }
  12823. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12824. encoder->base.base.id, encoder->base.name,
  12825. enableddisabled(encoder->base.crtc),
  12826. pipe_name(pipe));
  12827. }
  12828. drm_connector_list_iter_begin(dev, &conn_iter);
  12829. for_each_intel_connector_iter(connector, &conn_iter) {
  12830. if (connector->get_hw_state(connector)) {
  12831. connector->base.dpms = DRM_MODE_DPMS_ON;
  12832. encoder = connector->encoder;
  12833. connector->base.encoder = &encoder->base;
  12834. if (encoder->base.crtc &&
  12835. encoder->base.crtc->state->active) {
  12836. /*
  12837. * This has to be done during hardware readout
  12838. * because anything calling .crtc_disable may
  12839. * rely on the connector_mask being accurate.
  12840. */
  12841. encoder->base.crtc->state->connector_mask |=
  12842. 1 << drm_connector_index(&connector->base);
  12843. encoder->base.crtc->state->encoder_mask |=
  12844. 1 << drm_encoder_index(&encoder->base);
  12845. }
  12846. } else {
  12847. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12848. connector->base.encoder = NULL;
  12849. }
  12850. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12851. connector->base.base.id, connector->base.name,
  12852. enableddisabled(connector->base.encoder));
  12853. }
  12854. drm_connector_list_iter_end(&conn_iter);
  12855. for_each_intel_crtc(dev, crtc) {
  12856. struct intel_crtc_state *crtc_state =
  12857. to_intel_crtc_state(crtc->base.state);
  12858. int pixclk = 0;
  12859. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12860. if (crtc_state->base.active) {
  12861. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12862. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12863. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12864. /*
  12865. * The initial mode needs to be set in order to keep
  12866. * the atomic core happy. It wants a valid mode if the
  12867. * crtc's enabled, so we do the above call.
  12868. *
  12869. * But we don't set all the derived state fully, hence
  12870. * set a flag to indicate that a full recalculation is
  12871. * needed on the next commit.
  12872. */
  12873. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12874. intel_crtc_compute_pixel_rate(crtc_state);
  12875. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
  12876. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12877. pixclk = crtc_state->pixel_rate;
  12878. else
  12879. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  12880. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  12881. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  12882. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  12883. drm_calc_timestamping_constants(&crtc->base,
  12884. &crtc_state->base.adjusted_mode);
  12885. update_scanline_offset(crtc);
  12886. }
  12887. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  12888. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12889. }
  12890. }
  12891. static void
  12892. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12893. {
  12894. struct intel_encoder *encoder;
  12895. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12896. u64 get_domains;
  12897. enum intel_display_power_domain domain;
  12898. if (!encoder->get_power_domains)
  12899. continue;
  12900. get_domains = encoder->get_power_domains(encoder);
  12901. for_each_power_domain(domain, get_domains)
  12902. intel_display_power_get(dev_priv, domain);
  12903. }
  12904. }
  12905. /* Scan out the current hw modeset state,
  12906. * and sanitizes it to the current state
  12907. */
  12908. static void
  12909. intel_modeset_setup_hw_state(struct drm_device *dev)
  12910. {
  12911. struct drm_i915_private *dev_priv = to_i915(dev);
  12912. enum pipe pipe;
  12913. struct intel_crtc *crtc;
  12914. struct intel_encoder *encoder;
  12915. int i;
  12916. intel_modeset_readout_hw_state(dev);
  12917. /* HW state is read out, now we need to sanitize this mess. */
  12918. get_encoder_power_domains(dev_priv);
  12919. for_each_intel_encoder(dev, encoder) {
  12920. intel_sanitize_encoder(encoder);
  12921. }
  12922. for_each_pipe(dev_priv, pipe) {
  12923. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12924. intel_sanitize_crtc(crtc);
  12925. intel_dump_pipe_config(crtc, crtc->config,
  12926. "[setup_hw_state]");
  12927. }
  12928. intel_modeset_update_connector_atomic_state(dev);
  12929. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12930. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12931. if (!pll->on || pll->active_mask)
  12932. continue;
  12933. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12934. pll->funcs.disable(dev_priv, pll);
  12935. pll->on = false;
  12936. }
  12937. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12938. vlv_wm_get_hw_state(dev);
  12939. vlv_wm_sanitize(dev_priv);
  12940. } else if (IS_GEN9(dev_priv)) {
  12941. skl_wm_get_hw_state(dev);
  12942. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12943. ilk_wm_get_hw_state(dev);
  12944. }
  12945. for_each_intel_crtc(dev, crtc) {
  12946. u64 put_domains;
  12947. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12948. if (WARN_ON(put_domains))
  12949. modeset_put_power_domains(dev_priv, put_domains);
  12950. }
  12951. intel_display_set_init_power(dev_priv, false);
  12952. intel_power_domains_verify_state(dev_priv);
  12953. intel_fbc_init_pipe_state(dev_priv);
  12954. }
  12955. void intel_display_resume(struct drm_device *dev)
  12956. {
  12957. struct drm_i915_private *dev_priv = to_i915(dev);
  12958. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12959. struct drm_modeset_acquire_ctx ctx;
  12960. int ret;
  12961. dev_priv->modeset_restore_state = NULL;
  12962. if (state)
  12963. state->acquire_ctx = &ctx;
  12964. /*
  12965. * This is a cludge because with real atomic modeset mode_config.mutex
  12966. * won't be taken. Unfortunately some probed state like
  12967. * audio_codec_enable is still protected by mode_config.mutex, so lock
  12968. * it here for now.
  12969. */
  12970. mutex_lock(&dev->mode_config.mutex);
  12971. drm_modeset_acquire_init(&ctx, 0);
  12972. while (1) {
  12973. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12974. if (ret != -EDEADLK)
  12975. break;
  12976. drm_modeset_backoff(&ctx);
  12977. }
  12978. if (!ret)
  12979. ret = __intel_display_resume(dev, state, &ctx);
  12980. drm_modeset_drop_locks(&ctx);
  12981. drm_modeset_acquire_fini(&ctx);
  12982. mutex_unlock(&dev->mode_config.mutex);
  12983. if (ret)
  12984. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12985. if (state)
  12986. drm_atomic_state_put(state);
  12987. }
  12988. void intel_modeset_gem_init(struct drm_device *dev)
  12989. {
  12990. struct drm_i915_private *dev_priv = to_i915(dev);
  12991. intel_init_gt_powersave(dev_priv);
  12992. intel_setup_overlay(dev_priv);
  12993. }
  12994. int intel_connector_register(struct drm_connector *connector)
  12995. {
  12996. struct intel_connector *intel_connector = to_intel_connector(connector);
  12997. int ret;
  12998. ret = intel_backlight_device_register(intel_connector);
  12999. if (ret)
  13000. goto err;
  13001. return 0;
  13002. err:
  13003. return ret;
  13004. }
  13005. void intel_connector_unregister(struct drm_connector *connector)
  13006. {
  13007. struct intel_connector *intel_connector = to_intel_connector(connector);
  13008. intel_backlight_device_unregister(intel_connector);
  13009. intel_panel_destroy_backlight(connector);
  13010. }
  13011. void intel_modeset_cleanup(struct drm_device *dev)
  13012. {
  13013. struct drm_i915_private *dev_priv = to_i915(dev);
  13014. flush_work(&dev_priv->atomic_helper.free_work);
  13015. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  13016. intel_disable_gt_powersave(dev_priv);
  13017. /*
  13018. * Interrupts and polling as the first thing to avoid creating havoc.
  13019. * Too much stuff here (turning of connectors, ...) would
  13020. * experience fancy races otherwise.
  13021. */
  13022. intel_irq_uninstall(dev_priv);
  13023. /*
  13024. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13025. * poll handlers. Hence disable polling after hpd handling is shut down.
  13026. */
  13027. drm_kms_helper_poll_fini(dev);
  13028. intel_unregister_dsm_handler();
  13029. intel_fbc_global_disable(dev_priv);
  13030. /* flush any delayed tasks or pending work */
  13031. flush_scheduled_work();
  13032. drm_mode_config_cleanup(dev);
  13033. intel_cleanup_overlay(dev_priv);
  13034. intel_cleanup_gt_powersave(dev_priv);
  13035. intel_teardown_gmbus(dev_priv);
  13036. }
  13037. void intel_connector_attach_encoder(struct intel_connector *connector,
  13038. struct intel_encoder *encoder)
  13039. {
  13040. connector->encoder = encoder;
  13041. drm_mode_connector_attach_encoder(&connector->base,
  13042. &encoder->base);
  13043. }
  13044. /*
  13045. * set vga decode state - true == enable VGA decode
  13046. */
  13047. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  13048. {
  13049. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13050. u16 gmch_ctrl;
  13051. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13052. DRM_ERROR("failed to read control word\n");
  13053. return -EIO;
  13054. }
  13055. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13056. return 0;
  13057. if (state)
  13058. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13059. else
  13060. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13061. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13062. DRM_ERROR("failed to write control word\n");
  13063. return -EIO;
  13064. }
  13065. return 0;
  13066. }
  13067. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  13068. struct intel_display_error_state {
  13069. u32 power_well_driver;
  13070. int num_transcoders;
  13071. struct intel_cursor_error_state {
  13072. u32 control;
  13073. u32 position;
  13074. u32 base;
  13075. u32 size;
  13076. } cursor[I915_MAX_PIPES];
  13077. struct intel_pipe_error_state {
  13078. bool power_domain_on;
  13079. u32 source;
  13080. u32 stat;
  13081. } pipe[I915_MAX_PIPES];
  13082. struct intel_plane_error_state {
  13083. u32 control;
  13084. u32 stride;
  13085. u32 size;
  13086. u32 pos;
  13087. u32 addr;
  13088. u32 surface;
  13089. u32 tile_offset;
  13090. } plane[I915_MAX_PIPES];
  13091. struct intel_transcoder_error_state {
  13092. bool power_domain_on;
  13093. enum transcoder cpu_transcoder;
  13094. u32 conf;
  13095. u32 htotal;
  13096. u32 hblank;
  13097. u32 hsync;
  13098. u32 vtotal;
  13099. u32 vblank;
  13100. u32 vsync;
  13101. } transcoder[4];
  13102. };
  13103. struct intel_display_error_state *
  13104. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13105. {
  13106. struct intel_display_error_state *error;
  13107. int transcoders[] = {
  13108. TRANSCODER_A,
  13109. TRANSCODER_B,
  13110. TRANSCODER_C,
  13111. TRANSCODER_EDP,
  13112. };
  13113. int i;
  13114. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13115. return NULL;
  13116. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13117. if (error == NULL)
  13118. return NULL;
  13119. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13120. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13121. for_each_pipe(dev_priv, i) {
  13122. error->pipe[i].power_domain_on =
  13123. __intel_display_power_is_enabled(dev_priv,
  13124. POWER_DOMAIN_PIPE(i));
  13125. if (!error->pipe[i].power_domain_on)
  13126. continue;
  13127. error->cursor[i].control = I915_READ(CURCNTR(i));
  13128. error->cursor[i].position = I915_READ(CURPOS(i));
  13129. error->cursor[i].base = I915_READ(CURBASE(i));
  13130. error->plane[i].control = I915_READ(DSPCNTR(i));
  13131. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13132. if (INTEL_GEN(dev_priv) <= 3) {
  13133. error->plane[i].size = I915_READ(DSPSIZE(i));
  13134. error->plane[i].pos = I915_READ(DSPPOS(i));
  13135. }
  13136. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13137. error->plane[i].addr = I915_READ(DSPADDR(i));
  13138. if (INTEL_GEN(dev_priv) >= 4) {
  13139. error->plane[i].surface = I915_READ(DSPSURF(i));
  13140. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13141. }
  13142. error->pipe[i].source = I915_READ(PIPESRC(i));
  13143. if (HAS_GMCH_DISPLAY(dev_priv))
  13144. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13145. }
  13146. /* Note: this does not include DSI transcoders. */
  13147. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13148. if (HAS_DDI(dev_priv))
  13149. error->num_transcoders++; /* Account for eDP. */
  13150. for (i = 0; i < error->num_transcoders; i++) {
  13151. enum transcoder cpu_transcoder = transcoders[i];
  13152. error->transcoder[i].power_domain_on =
  13153. __intel_display_power_is_enabled(dev_priv,
  13154. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13155. if (!error->transcoder[i].power_domain_on)
  13156. continue;
  13157. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13158. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13159. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13160. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13161. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13162. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13163. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13164. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13165. }
  13166. return error;
  13167. }
  13168. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13169. void
  13170. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13171. struct intel_display_error_state *error)
  13172. {
  13173. struct drm_i915_private *dev_priv = m->i915;
  13174. int i;
  13175. if (!error)
  13176. return;
  13177. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  13178. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13179. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13180. error->power_well_driver);
  13181. for_each_pipe(dev_priv, i) {
  13182. err_printf(m, "Pipe [%d]:\n", i);
  13183. err_printf(m, " Power: %s\n",
  13184. onoff(error->pipe[i].power_domain_on));
  13185. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13186. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13187. err_printf(m, "Plane [%d]:\n", i);
  13188. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13189. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13190. if (INTEL_GEN(dev_priv) <= 3) {
  13191. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13192. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13193. }
  13194. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13195. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13196. if (INTEL_GEN(dev_priv) >= 4) {
  13197. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13198. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13199. }
  13200. err_printf(m, "Cursor [%d]:\n", i);
  13201. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13202. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13203. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13204. }
  13205. for (i = 0; i < error->num_transcoders; i++) {
  13206. err_printf(m, "CPU transcoder: %s\n",
  13207. transcoder_name(error->transcoder[i].cpu_transcoder));
  13208. err_printf(m, " Power: %s\n",
  13209. onoff(error->transcoder[i].power_domain_on));
  13210. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13211. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13212. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13213. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13214. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13215. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13216. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13217. }
  13218. }
  13219. #endif