intel_display.c 449 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "i915_gem_clflush.h"
  40. #include "intel_dsi.h"
  41. #include "i915_trace.h"
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_dp_helper.h>
  45. #include <drm/drm_crtc_helper.h>
  46. #include <drm/drm_plane_helper.h>
  47. #include <drm/drm_rect.h>
  48. #include <linux/dma_remapping.h>
  49. #include <linux/reservation.h>
  50. static bool is_mmio_work(struct intel_flip_work *work)
  51. {
  52. return work->mmio_work.func;
  53. }
  54. /* Primary plane formats for gen <= 3 */
  55. static const uint32_t i8xx_primary_formats[] = {
  56. DRM_FORMAT_C8,
  57. DRM_FORMAT_RGB565,
  58. DRM_FORMAT_XRGB1555,
  59. DRM_FORMAT_XRGB8888,
  60. };
  61. /* Primary plane formats for gen >= 4 */
  62. static const uint32_t i965_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_XRGB2101010,
  68. DRM_FORMAT_XBGR2101010,
  69. };
  70. static const uint32_t skl_primary_formats[] = {
  71. DRM_FORMAT_C8,
  72. DRM_FORMAT_RGB565,
  73. DRM_FORMAT_XRGB8888,
  74. DRM_FORMAT_XBGR8888,
  75. DRM_FORMAT_ARGB8888,
  76. DRM_FORMAT_ABGR8888,
  77. DRM_FORMAT_XRGB2101010,
  78. DRM_FORMAT_XBGR2101010,
  79. DRM_FORMAT_YUYV,
  80. DRM_FORMAT_YVYU,
  81. DRM_FORMAT_UYVY,
  82. DRM_FORMAT_VYUY,
  83. };
  84. /* Cursor formats */
  85. static const uint32_t intel_cursor_formats[] = {
  86. DRM_FORMAT_ARGB8888,
  87. };
  88. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  89. struct intel_crtc_state *pipe_config);
  90. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  91. struct intel_crtc_state *pipe_config);
  92. static int intel_framebuffer_init(struct intel_framebuffer *ifb,
  93. struct drm_i915_gem_object *obj,
  94. struct drm_mode_fb_cmd2 *mode_cmd);
  95. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  96. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  98. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  99. struct intel_link_m_n *m_n,
  100. struct intel_link_m_n *m2_n2);
  101. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  102. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  104. static void vlv_prepare_pll(struct intel_crtc *crtc,
  105. const struct intel_crtc_state *pipe_config);
  106. static void chv_prepare_pll(struct intel_crtc *crtc,
  107. const struct intel_crtc_state *pipe_config);
  108. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  109. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  111. struct intel_crtc_state *crtc_state);
  112. static void skylake_pfit_enable(struct intel_crtc *crtc);
  113. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  114. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  115. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  116. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  117. struct intel_limit {
  118. struct {
  119. int min, max;
  120. } dot, vco, n, m, m1, m2, p, p1;
  121. struct {
  122. int dot_limit;
  123. int p2_slow, p2_fast;
  124. } p2;
  125. };
  126. /* returns HPLL frequency in kHz */
  127. int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
  128. {
  129. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  130. /* Obtain SKU information */
  131. mutex_lock(&dev_priv->sb_lock);
  132. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  133. CCK_FUSE_HPLL_FREQ_MASK;
  134. mutex_unlock(&dev_priv->sb_lock);
  135. return vco_freq[hpll_freq] * 1000;
  136. }
  137. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  138. const char *name, u32 reg, int ref_freq)
  139. {
  140. u32 val;
  141. int divider;
  142. mutex_lock(&dev_priv->sb_lock);
  143. val = vlv_cck_read(dev_priv, reg);
  144. mutex_unlock(&dev_priv->sb_lock);
  145. divider = val & CCK_FREQUENCY_VALUES;
  146. WARN((val & CCK_FREQUENCY_STATUS) !=
  147. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  148. "%s change in progress\n", name);
  149. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  150. }
  151. int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  152. const char *name, u32 reg)
  153. {
  154. if (dev_priv->hpll_freq == 0)
  155. dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
  156. return vlv_get_cck_clock(dev_priv, name, reg,
  157. dev_priv->hpll_freq);
  158. }
  159. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  160. {
  161. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  162. return;
  163. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  164. CCK_CZ_CLOCK_CONTROL);
  165. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  166. }
  167. static inline u32 /* units of 100MHz */
  168. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  169. const struct intel_crtc_state *pipe_config)
  170. {
  171. if (HAS_DDI(dev_priv))
  172. return pipe_config->port_clock; /* SPLL */
  173. else if (IS_GEN5(dev_priv))
  174. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  175. else
  176. return 270000;
  177. }
  178. static const struct intel_limit intel_limits_i8xx_dac = {
  179. .dot = { .min = 25000, .max = 350000 },
  180. .vco = { .min = 908000, .max = 1512000 },
  181. .n = { .min = 2, .max = 16 },
  182. .m = { .min = 96, .max = 140 },
  183. .m1 = { .min = 18, .max = 26 },
  184. .m2 = { .min = 6, .max = 16 },
  185. .p = { .min = 4, .max = 128 },
  186. .p1 = { .min = 2, .max = 33 },
  187. .p2 = { .dot_limit = 165000,
  188. .p2_slow = 4, .p2_fast = 2 },
  189. };
  190. static const struct intel_limit intel_limits_i8xx_dvo = {
  191. .dot = { .min = 25000, .max = 350000 },
  192. .vco = { .min = 908000, .max = 1512000 },
  193. .n = { .min = 2, .max = 16 },
  194. .m = { .min = 96, .max = 140 },
  195. .m1 = { .min = 18, .max = 26 },
  196. .m2 = { .min = 6, .max = 16 },
  197. .p = { .min = 4, .max = 128 },
  198. .p1 = { .min = 2, .max = 33 },
  199. .p2 = { .dot_limit = 165000,
  200. .p2_slow = 4, .p2_fast = 4 },
  201. };
  202. static const struct intel_limit intel_limits_i8xx_lvds = {
  203. .dot = { .min = 25000, .max = 350000 },
  204. .vco = { .min = 908000, .max = 1512000 },
  205. .n = { .min = 2, .max = 16 },
  206. .m = { .min = 96, .max = 140 },
  207. .m1 = { .min = 18, .max = 26 },
  208. .m2 = { .min = 6, .max = 16 },
  209. .p = { .min = 4, .max = 128 },
  210. .p1 = { .min = 1, .max = 6 },
  211. .p2 = { .dot_limit = 165000,
  212. .p2_slow = 14, .p2_fast = 7 },
  213. };
  214. static const struct intel_limit intel_limits_i9xx_sdvo = {
  215. .dot = { .min = 20000, .max = 400000 },
  216. .vco = { .min = 1400000, .max = 2800000 },
  217. .n = { .min = 1, .max = 6 },
  218. .m = { .min = 70, .max = 120 },
  219. .m1 = { .min = 8, .max = 18 },
  220. .m2 = { .min = 3, .max = 7 },
  221. .p = { .min = 5, .max = 80 },
  222. .p1 = { .min = 1, .max = 8 },
  223. .p2 = { .dot_limit = 200000,
  224. .p2_slow = 10, .p2_fast = 5 },
  225. };
  226. static const struct intel_limit intel_limits_i9xx_lvds = {
  227. .dot = { .min = 20000, .max = 400000 },
  228. .vco = { .min = 1400000, .max = 2800000 },
  229. .n = { .min = 1, .max = 6 },
  230. .m = { .min = 70, .max = 120 },
  231. .m1 = { .min = 8, .max = 18 },
  232. .m2 = { .min = 3, .max = 7 },
  233. .p = { .min = 7, .max = 98 },
  234. .p1 = { .min = 1, .max = 8 },
  235. .p2 = { .dot_limit = 112000,
  236. .p2_slow = 14, .p2_fast = 7 },
  237. };
  238. static const struct intel_limit intel_limits_g4x_sdvo = {
  239. .dot = { .min = 25000, .max = 270000 },
  240. .vco = { .min = 1750000, .max = 3500000},
  241. .n = { .min = 1, .max = 4 },
  242. .m = { .min = 104, .max = 138 },
  243. .m1 = { .min = 17, .max = 23 },
  244. .m2 = { .min = 5, .max = 11 },
  245. .p = { .min = 10, .max = 30 },
  246. .p1 = { .min = 1, .max = 3},
  247. .p2 = { .dot_limit = 270000,
  248. .p2_slow = 10,
  249. .p2_fast = 10
  250. },
  251. };
  252. static const struct intel_limit intel_limits_g4x_hdmi = {
  253. .dot = { .min = 22000, .max = 400000 },
  254. .vco = { .min = 1750000, .max = 3500000},
  255. .n = { .min = 1, .max = 4 },
  256. .m = { .min = 104, .max = 138 },
  257. .m1 = { .min = 16, .max = 23 },
  258. .m2 = { .min = 5, .max = 11 },
  259. .p = { .min = 5, .max = 80 },
  260. .p1 = { .min = 1, .max = 8},
  261. .p2 = { .dot_limit = 165000,
  262. .p2_slow = 10, .p2_fast = 5 },
  263. };
  264. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  265. .dot = { .min = 20000, .max = 115000 },
  266. .vco = { .min = 1750000, .max = 3500000 },
  267. .n = { .min = 1, .max = 3 },
  268. .m = { .min = 104, .max = 138 },
  269. .m1 = { .min = 17, .max = 23 },
  270. .m2 = { .min = 5, .max = 11 },
  271. .p = { .min = 28, .max = 112 },
  272. .p1 = { .min = 2, .max = 8 },
  273. .p2 = { .dot_limit = 0,
  274. .p2_slow = 14, .p2_fast = 14
  275. },
  276. };
  277. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  278. .dot = { .min = 80000, .max = 224000 },
  279. .vco = { .min = 1750000, .max = 3500000 },
  280. .n = { .min = 1, .max = 3 },
  281. .m = { .min = 104, .max = 138 },
  282. .m1 = { .min = 17, .max = 23 },
  283. .m2 = { .min = 5, .max = 11 },
  284. .p = { .min = 14, .max = 42 },
  285. .p1 = { .min = 2, .max = 6 },
  286. .p2 = { .dot_limit = 0,
  287. .p2_slow = 7, .p2_fast = 7
  288. },
  289. };
  290. static const struct intel_limit intel_limits_pineview_sdvo = {
  291. .dot = { .min = 20000, .max = 400000},
  292. .vco = { .min = 1700000, .max = 3500000 },
  293. /* Pineview's Ncounter is a ring counter */
  294. .n = { .min = 3, .max = 6 },
  295. .m = { .min = 2, .max = 256 },
  296. /* Pineview only has one combined m divider, which we treat as m2. */
  297. .m1 = { .min = 0, .max = 0 },
  298. .m2 = { .min = 0, .max = 254 },
  299. .p = { .min = 5, .max = 80 },
  300. .p1 = { .min = 1, .max = 8 },
  301. .p2 = { .dot_limit = 200000,
  302. .p2_slow = 10, .p2_fast = 5 },
  303. };
  304. static const struct intel_limit intel_limits_pineview_lvds = {
  305. .dot = { .min = 20000, .max = 400000 },
  306. .vco = { .min = 1700000, .max = 3500000 },
  307. .n = { .min = 3, .max = 6 },
  308. .m = { .min = 2, .max = 256 },
  309. .m1 = { .min = 0, .max = 0 },
  310. .m2 = { .min = 0, .max = 254 },
  311. .p = { .min = 7, .max = 112 },
  312. .p1 = { .min = 1, .max = 8 },
  313. .p2 = { .dot_limit = 112000,
  314. .p2_slow = 14, .p2_fast = 14 },
  315. };
  316. /* Ironlake / Sandybridge
  317. *
  318. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  319. * the range value for them is (actual_value - 2).
  320. */
  321. static const struct intel_limit intel_limits_ironlake_dac = {
  322. .dot = { .min = 25000, .max = 350000 },
  323. .vco = { .min = 1760000, .max = 3510000 },
  324. .n = { .min = 1, .max = 5 },
  325. .m = { .min = 79, .max = 127 },
  326. .m1 = { .min = 12, .max = 22 },
  327. .m2 = { .min = 5, .max = 9 },
  328. .p = { .min = 5, .max = 80 },
  329. .p1 = { .min = 1, .max = 8 },
  330. .p2 = { .dot_limit = 225000,
  331. .p2_slow = 10, .p2_fast = 5 },
  332. };
  333. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  334. .dot = { .min = 25000, .max = 350000 },
  335. .vco = { .min = 1760000, .max = 3510000 },
  336. .n = { .min = 1, .max = 3 },
  337. .m = { .min = 79, .max = 118 },
  338. .m1 = { .min = 12, .max = 22 },
  339. .m2 = { .min = 5, .max = 9 },
  340. .p = { .min = 28, .max = 112 },
  341. .p1 = { .min = 2, .max = 8 },
  342. .p2 = { .dot_limit = 225000,
  343. .p2_slow = 14, .p2_fast = 14 },
  344. };
  345. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  346. .dot = { .min = 25000, .max = 350000 },
  347. .vco = { .min = 1760000, .max = 3510000 },
  348. .n = { .min = 1, .max = 3 },
  349. .m = { .min = 79, .max = 127 },
  350. .m1 = { .min = 12, .max = 22 },
  351. .m2 = { .min = 5, .max = 9 },
  352. .p = { .min = 14, .max = 56 },
  353. .p1 = { .min = 2, .max = 8 },
  354. .p2 = { .dot_limit = 225000,
  355. .p2_slow = 7, .p2_fast = 7 },
  356. };
  357. /* LVDS 100mhz refclk limits. */
  358. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  359. .dot = { .min = 25000, .max = 350000 },
  360. .vco = { .min = 1760000, .max = 3510000 },
  361. .n = { .min = 1, .max = 2 },
  362. .m = { .min = 79, .max = 126 },
  363. .m1 = { .min = 12, .max = 22 },
  364. .m2 = { .min = 5, .max = 9 },
  365. .p = { .min = 28, .max = 112 },
  366. .p1 = { .min = 2, .max = 8 },
  367. .p2 = { .dot_limit = 225000,
  368. .p2_slow = 14, .p2_fast = 14 },
  369. };
  370. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  371. .dot = { .min = 25000, .max = 350000 },
  372. .vco = { .min = 1760000, .max = 3510000 },
  373. .n = { .min = 1, .max = 3 },
  374. .m = { .min = 79, .max = 126 },
  375. .m1 = { .min = 12, .max = 22 },
  376. .m2 = { .min = 5, .max = 9 },
  377. .p = { .min = 14, .max = 42 },
  378. .p1 = { .min = 2, .max = 6 },
  379. .p2 = { .dot_limit = 225000,
  380. .p2_slow = 7, .p2_fast = 7 },
  381. };
  382. static const struct intel_limit intel_limits_vlv = {
  383. /*
  384. * These are the data rate limits (measured in fast clocks)
  385. * since those are the strictest limits we have. The fast
  386. * clock and actual rate limits are more relaxed, so checking
  387. * them would make no difference.
  388. */
  389. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  390. .vco = { .min = 4000000, .max = 6000000 },
  391. .n = { .min = 1, .max = 7 },
  392. .m1 = { .min = 2, .max = 3 },
  393. .m2 = { .min = 11, .max = 156 },
  394. .p1 = { .min = 2, .max = 3 },
  395. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  396. };
  397. static const struct intel_limit intel_limits_chv = {
  398. /*
  399. * These are the data rate limits (measured in fast clocks)
  400. * since those are the strictest limits we have. The fast
  401. * clock and actual rate limits are more relaxed, so checking
  402. * them would make no difference.
  403. */
  404. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  405. .vco = { .min = 4800000, .max = 6480000 },
  406. .n = { .min = 1, .max = 1 },
  407. .m1 = { .min = 2, .max = 2 },
  408. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  409. .p1 = { .min = 2, .max = 4 },
  410. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  411. };
  412. static const struct intel_limit intel_limits_bxt = {
  413. /* FIXME: find real dot limits */
  414. .dot = { .min = 0, .max = INT_MAX },
  415. .vco = { .min = 4800000, .max = 6700000 },
  416. .n = { .min = 1, .max = 1 },
  417. .m1 = { .min = 2, .max = 2 },
  418. /* FIXME: find real m2 limits */
  419. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  420. .p1 = { .min = 2, .max = 4 },
  421. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  422. };
  423. static bool
  424. needs_modeset(struct drm_crtc_state *state)
  425. {
  426. return drm_atomic_crtc_needs_modeset(state);
  427. }
  428. /*
  429. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  430. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  431. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  432. * The helpers' return value is the rate of the clock that is fed to the
  433. * display engine's pipe which can be the above fast dot clock rate or a
  434. * divided-down version of it.
  435. */
  436. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  437. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  438. {
  439. clock->m = clock->m2 + 2;
  440. clock->p = clock->p1 * clock->p2;
  441. if (WARN_ON(clock->n == 0 || clock->p == 0))
  442. return 0;
  443. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  444. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  445. return clock->dot;
  446. }
  447. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  448. {
  449. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  450. }
  451. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  452. {
  453. clock->m = i9xx_dpll_compute_m(clock);
  454. clock->p = clock->p1 * clock->p2;
  455. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  456. return 0;
  457. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  458. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  459. return clock->dot;
  460. }
  461. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  462. {
  463. clock->m = clock->m1 * clock->m2;
  464. clock->p = clock->p1 * clock->p2;
  465. if (WARN_ON(clock->n == 0 || clock->p == 0))
  466. return 0;
  467. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  468. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  469. return clock->dot / 5;
  470. }
  471. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  472. {
  473. clock->m = clock->m1 * clock->m2;
  474. clock->p = clock->p1 * clock->p2;
  475. if (WARN_ON(clock->n == 0 || clock->p == 0))
  476. return 0;
  477. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  478. clock->n << 22);
  479. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  480. return clock->dot / 5;
  481. }
  482. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  483. /**
  484. * Returns whether the given set of divisors are valid for a given refclk with
  485. * the given connectors.
  486. */
  487. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  488. const struct intel_limit *limit,
  489. const struct dpll *clock)
  490. {
  491. if (clock->n < limit->n.min || limit->n.max < clock->n)
  492. INTELPllInvalid("n out of range\n");
  493. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  494. INTELPllInvalid("p1 out of range\n");
  495. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  496. INTELPllInvalid("m2 out of range\n");
  497. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  498. INTELPllInvalid("m1 out of range\n");
  499. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  500. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  501. if (clock->m1 <= clock->m2)
  502. INTELPllInvalid("m1 <= m2\n");
  503. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  504. !IS_GEN9_LP(dev_priv)) {
  505. if (clock->p < limit->p.min || limit->p.max < clock->p)
  506. INTELPllInvalid("p out of range\n");
  507. if (clock->m < limit->m.min || limit->m.max < clock->m)
  508. INTELPllInvalid("m out of range\n");
  509. }
  510. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  511. INTELPllInvalid("vco out of range\n");
  512. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  513. * connector, etc., rather than just a single range.
  514. */
  515. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  516. INTELPllInvalid("dot out of range\n");
  517. return true;
  518. }
  519. static int
  520. i9xx_select_p2_div(const struct intel_limit *limit,
  521. const struct intel_crtc_state *crtc_state,
  522. int target)
  523. {
  524. struct drm_device *dev = crtc_state->base.crtc->dev;
  525. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  526. /*
  527. * For LVDS just rely on its current settings for dual-channel.
  528. * We haven't figured out how to reliably set up different
  529. * single/dual channel state, if we even can.
  530. */
  531. if (intel_is_dual_link_lvds(dev))
  532. return limit->p2.p2_fast;
  533. else
  534. return limit->p2.p2_slow;
  535. } else {
  536. if (target < limit->p2.dot_limit)
  537. return limit->p2.p2_slow;
  538. else
  539. return limit->p2.p2_fast;
  540. }
  541. }
  542. /*
  543. * Returns a set of divisors for the desired target clock with the given
  544. * refclk, or FALSE. The returned values represent the clock equation:
  545. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  546. *
  547. * Target and reference clocks are specified in kHz.
  548. *
  549. * If match_clock is provided, then best_clock P divider must match the P
  550. * divider from @match_clock used for LVDS downclocking.
  551. */
  552. static bool
  553. i9xx_find_best_dpll(const struct intel_limit *limit,
  554. struct intel_crtc_state *crtc_state,
  555. int target, int refclk, struct dpll *match_clock,
  556. struct dpll *best_clock)
  557. {
  558. struct drm_device *dev = crtc_state->base.crtc->dev;
  559. struct dpll clock;
  560. int err = target;
  561. memset(best_clock, 0, sizeof(*best_clock));
  562. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  563. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  564. clock.m1++) {
  565. for (clock.m2 = limit->m2.min;
  566. clock.m2 <= limit->m2.max; clock.m2++) {
  567. if (clock.m2 >= clock.m1)
  568. break;
  569. for (clock.n = limit->n.min;
  570. clock.n <= limit->n.max; clock.n++) {
  571. for (clock.p1 = limit->p1.min;
  572. clock.p1 <= limit->p1.max; clock.p1++) {
  573. int this_err;
  574. i9xx_calc_dpll_params(refclk, &clock);
  575. if (!intel_PLL_is_valid(to_i915(dev),
  576. limit,
  577. &clock))
  578. continue;
  579. if (match_clock &&
  580. clock.p != match_clock->p)
  581. continue;
  582. this_err = abs(clock.dot - target);
  583. if (this_err < err) {
  584. *best_clock = clock;
  585. err = this_err;
  586. }
  587. }
  588. }
  589. }
  590. }
  591. return (err != target);
  592. }
  593. /*
  594. * Returns a set of divisors for the desired target clock with the given
  595. * refclk, or FALSE. The returned values represent the clock equation:
  596. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  597. *
  598. * Target and reference clocks are specified in kHz.
  599. *
  600. * If match_clock is provided, then best_clock P divider must match the P
  601. * divider from @match_clock used for LVDS downclocking.
  602. */
  603. static bool
  604. pnv_find_best_dpll(const struct intel_limit *limit,
  605. struct intel_crtc_state *crtc_state,
  606. int target, int refclk, struct dpll *match_clock,
  607. struct dpll *best_clock)
  608. {
  609. struct drm_device *dev = crtc_state->base.crtc->dev;
  610. struct dpll clock;
  611. int err = target;
  612. memset(best_clock, 0, sizeof(*best_clock));
  613. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  614. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  615. clock.m1++) {
  616. for (clock.m2 = limit->m2.min;
  617. clock.m2 <= limit->m2.max; clock.m2++) {
  618. for (clock.n = limit->n.min;
  619. clock.n <= limit->n.max; clock.n++) {
  620. for (clock.p1 = limit->p1.min;
  621. clock.p1 <= limit->p1.max; clock.p1++) {
  622. int this_err;
  623. pnv_calc_dpll_params(refclk, &clock);
  624. if (!intel_PLL_is_valid(to_i915(dev),
  625. limit,
  626. &clock))
  627. continue;
  628. if (match_clock &&
  629. clock.p != match_clock->p)
  630. continue;
  631. this_err = abs(clock.dot - target);
  632. if (this_err < err) {
  633. *best_clock = clock;
  634. err = this_err;
  635. }
  636. }
  637. }
  638. }
  639. }
  640. return (err != target);
  641. }
  642. /*
  643. * Returns a set of divisors for the desired target clock with the given
  644. * refclk, or FALSE. The returned values represent the clock equation:
  645. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  646. *
  647. * Target and reference clocks are specified in kHz.
  648. *
  649. * If match_clock is provided, then best_clock P divider must match the P
  650. * divider from @match_clock used for LVDS downclocking.
  651. */
  652. static bool
  653. g4x_find_best_dpll(const struct intel_limit *limit,
  654. struct intel_crtc_state *crtc_state,
  655. int target, int refclk, struct dpll *match_clock,
  656. struct dpll *best_clock)
  657. {
  658. struct drm_device *dev = crtc_state->base.crtc->dev;
  659. struct dpll clock;
  660. int max_n;
  661. bool found = false;
  662. /* approximately equals target * 0.00585 */
  663. int err_most = (target >> 8) + (target >> 9);
  664. memset(best_clock, 0, sizeof(*best_clock));
  665. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  666. max_n = limit->n.max;
  667. /* based on hardware requirement, prefer smaller n to precision */
  668. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  669. /* based on hardware requirement, prefere larger m1,m2 */
  670. for (clock.m1 = limit->m1.max;
  671. clock.m1 >= limit->m1.min; clock.m1--) {
  672. for (clock.m2 = limit->m2.max;
  673. clock.m2 >= limit->m2.min; clock.m2--) {
  674. for (clock.p1 = limit->p1.max;
  675. clock.p1 >= limit->p1.min; clock.p1--) {
  676. int this_err;
  677. i9xx_calc_dpll_params(refclk, &clock);
  678. if (!intel_PLL_is_valid(to_i915(dev),
  679. limit,
  680. &clock))
  681. continue;
  682. this_err = abs(clock.dot - target);
  683. if (this_err < err_most) {
  684. *best_clock = clock;
  685. err_most = this_err;
  686. max_n = clock.n;
  687. found = true;
  688. }
  689. }
  690. }
  691. }
  692. }
  693. return found;
  694. }
  695. /*
  696. * Check if the calculated PLL configuration is more optimal compared to the
  697. * best configuration and error found so far. Return the calculated error.
  698. */
  699. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  700. const struct dpll *calculated_clock,
  701. const struct dpll *best_clock,
  702. unsigned int best_error_ppm,
  703. unsigned int *error_ppm)
  704. {
  705. /*
  706. * For CHV ignore the error and consider only the P value.
  707. * Prefer a bigger P value based on HW requirements.
  708. */
  709. if (IS_CHERRYVIEW(to_i915(dev))) {
  710. *error_ppm = 0;
  711. return calculated_clock->p > best_clock->p;
  712. }
  713. if (WARN_ON_ONCE(!target_freq))
  714. return false;
  715. *error_ppm = div_u64(1000000ULL *
  716. abs(target_freq - calculated_clock->dot),
  717. target_freq);
  718. /*
  719. * Prefer a better P value over a better (smaller) error if the error
  720. * is small. Ensure this preference for future configurations too by
  721. * setting the error to 0.
  722. */
  723. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  724. *error_ppm = 0;
  725. return true;
  726. }
  727. return *error_ppm + 10 < best_error_ppm;
  728. }
  729. /*
  730. * Returns a set of divisors for the desired target clock with the given
  731. * refclk, or FALSE. The returned values represent the clock equation:
  732. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  733. */
  734. static bool
  735. vlv_find_best_dpll(const struct intel_limit *limit,
  736. struct intel_crtc_state *crtc_state,
  737. int target, int refclk, struct dpll *match_clock,
  738. struct dpll *best_clock)
  739. {
  740. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  741. struct drm_device *dev = crtc->base.dev;
  742. struct dpll clock;
  743. unsigned int bestppm = 1000000;
  744. /* min update 19.2 MHz */
  745. int max_n = min(limit->n.max, refclk / 19200);
  746. bool found = false;
  747. target *= 5; /* fast clock */
  748. memset(best_clock, 0, sizeof(*best_clock));
  749. /* based on hardware requirement, prefer smaller n to precision */
  750. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  751. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  752. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  753. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  754. clock.p = clock.p1 * clock.p2;
  755. /* based on hardware requirement, prefer bigger m1,m2 values */
  756. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  757. unsigned int ppm;
  758. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  759. refclk * clock.m1);
  760. vlv_calc_dpll_params(refclk, &clock);
  761. if (!intel_PLL_is_valid(to_i915(dev),
  762. limit,
  763. &clock))
  764. continue;
  765. if (!vlv_PLL_is_optimal(dev, target,
  766. &clock,
  767. best_clock,
  768. bestppm, &ppm))
  769. continue;
  770. *best_clock = clock;
  771. bestppm = ppm;
  772. found = true;
  773. }
  774. }
  775. }
  776. }
  777. return found;
  778. }
  779. /*
  780. * Returns a set of divisors for the desired target clock with the given
  781. * refclk, or FALSE. The returned values represent the clock equation:
  782. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  783. */
  784. static bool
  785. chv_find_best_dpll(const struct intel_limit *limit,
  786. struct intel_crtc_state *crtc_state,
  787. int target, int refclk, struct dpll *match_clock,
  788. struct dpll *best_clock)
  789. {
  790. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  791. struct drm_device *dev = crtc->base.dev;
  792. unsigned int best_error_ppm;
  793. struct dpll clock;
  794. uint64_t m2;
  795. int found = false;
  796. memset(best_clock, 0, sizeof(*best_clock));
  797. best_error_ppm = 1000000;
  798. /*
  799. * Based on hardware doc, the n always set to 1, and m1 always
  800. * set to 2. If requires to support 200Mhz refclk, we need to
  801. * revisit this because n may not 1 anymore.
  802. */
  803. clock.n = 1, clock.m1 = 2;
  804. target *= 5; /* fast clock */
  805. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  806. for (clock.p2 = limit->p2.p2_fast;
  807. clock.p2 >= limit->p2.p2_slow;
  808. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  809. unsigned int error_ppm;
  810. clock.p = clock.p1 * clock.p2;
  811. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  812. clock.n) << 22, refclk * clock.m1);
  813. if (m2 > INT_MAX/clock.m1)
  814. continue;
  815. clock.m2 = m2;
  816. chv_calc_dpll_params(refclk, &clock);
  817. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  818. continue;
  819. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  820. best_error_ppm, &error_ppm))
  821. continue;
  822. *best_clock = clock;
  823. best_error_ppm = error_ppm;
  824. found = true;
  825. }
  826. }
  827. return found;
  828. }
  829. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  830. struct dpll *best_clock)
  831. {
  832. int refclk = 100000;
  833. const struct intel_limit *limit = &intel_limits_bxt;
  834. return chv_find_best_dpll(limit, crtc_state,
  835. target_clock, refclk, NULL, best_clock);
  836. }
  837. bool intel_crtc_active(struct intel_crtc *crtc)
  838. {
  839. /* Be paranoid as we can arrive here with only partial
  840. * state retrieved from the hardware during setup.
  841. *
  842. * We can ditch the adjusted_mode.crtc_clock check as soon
  843. * as Haswell has gained clock readout/fastboot support.
  844. *
  845. * We can ditch the crtc->primary->fb check as soon as we can
  846. * properly reconstruct framebuffers.
  847. *
  848. * FIXME: The intel_crtc->active here should be switched to
  849. * crtc->state->active once we have proper CRTC states wired up
  850. * for atomic.
  851. */
  852. return crtc->active && crtc->base.primary->state->fb &&
  853. crtc->config->base.adjusted_mode.crtc_clock;
  854. }
  855. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  856. enum pipe pipe)
  857. {
  858. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  859. return crtc->config->cpu_transcoder;
  860. }
  861. static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  862. {
  863. i915_reg_t reg = PIPEDSL(pipe);
  864. u32 line1, line2;
  865. u32 line_mask;
  866. if (IS_GEN2(dev_priv))
  867. line_mask = DSL_LINEMASK_GEN2;
  868. else
  869. line_mask = DSL_LINEMASK_GEN3;
  870. line1 = I915_READ(reg) & line_mask;
  871. msleep(5);
  872. line2 = I915_READ(reg) & line_mask;
  873. return line1 == line2;
  874. }
  875. /*
  876. * intel_wait_for_pipe_off - wait for pipe to turn off
  877. * @crtc: crtc whose pipe to wait for
  878. *
  879. * After disabling a pipe, we can't wait for vblank in the usual way,
  880. * spinning on the vblank interrupt status bit, since we won't actually
  881. * see an interrupt when the pipe is disabled.
  882. *
  883. * On Gen4 and above:
  884. * wait for the pipe register state bit to turn off
  885. *
  886. * Otherwise:
  887. * wait for the display line value to settle (it usually
  888. * ends up stopping at the start of the next frame).
  889. *
  890. */
  891. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  892. {
  893. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  894. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  895. enum pipe pipe = crtc->pipe;
  896. if (INTEL_GEN(dev_priv) >= 4) {
  897. i915_reg_t reg = PIPECONF(cpu_transcoder);
  898. /* Wait for the Pipe State to go off */
  899. if (intel_wait_for_register(dev_priv,
  900. reg, I965_PIPECONF_ACTIVE, 0,
  901. 100))
  902. WARN(1, "pipe_off wait timed out\n");
  903. } else {
  904. /* Wait for the display line to settle */
  905. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  906. WARN(1, "pipe_off wait timed out\n");
  907. }
  908. }
  909. /* Only for pre-ILK configs */
  910. void assert_pll(struct drm_i915_private *dev_priv,
  911. enum pipe pipe, bool state)
  912. {
  913. u32 val;
  914. bool cur_state;
  915. val = I915_READ(DPLL(pipe));
  916. cur_state = !!(val & DPLL_VCO_ENABLE);
  917. I915_STATE_WARN(cur_state != state,
  918. "PLL state assertion failure (expected %s, current %s)\n",
  919. onoff(state), onoff(cur_state));
  920. }
  921. /* XXX: the dsi pll is shared between MIPI DSI ports */
  922. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  923. {
  924. u32 val;
  925. bool cur_state;
  926. mutex_lock(&dev_priv->sb_lock);
  927. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  928. mutex_unlock(&dev_priv->sb_lock);
  929. cur_state = val & DSI_PLL_VCO_EN;
  930. I915_STATE_WARN(cur_state != state,
  931. "DSI PLL state assertion failure (expected %s, current %s)\n",
  932. onoff(state), onoff(cur_state));
  933. }
  934. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  935. enum pipe pipe, bool state)
  936. {
  937. bool cur_state;
  938. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  939. pipe);
  940. if (HAS_DDI(dev_priv)) {
  941. /* DDI does not have a specific FDI_TX register */
  942. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  943. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  944. } else {
  945. u32 val = I915_READ(FDI_TX_CTL(pipe));
  946. cur_state = !!(val & FDI_TX_ENABLE);
  947. }
  948. I915_STATE_WARN(cur_state != state,
  949. "FDI TX state assertion failure (expected %s, current %s)\n",
  950. onoff(state), onoff(cur_state));
  951. }
  952. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  953. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  954. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  955. enum pipe pipe, bool state)
  956. {
  957. u32 val;
  958. bool cur_state;
  959. val = I915_READ(FDI_RX_CTL(pipe));
  960. cur_state = !!(val & FDI_RX_ENABLE);
  961. I915_STATE_WARN(cur_state != state,
  962. "FDI RX state assertion failure (expected %s, current %s)\n",
  963. onoff(state), onoff(cur_state));
  964. }
  965. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  966. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  967. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  968. enum pipe pipe)
  969. {
  970. u32 val;
  971. /* ILK FDI PLL is always enabled */
  972. if (IS_GEN5(dev_priv))
  973. return;
  974. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  975. if (HAS_DDI(dev_priv))
  976. return;
  977. val = I915_READ(FDI_TX_CTL(pipe));
  978. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  979. }
  980. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  981. enum pipe pipe, bool state)
  982. {
  983. u32 val;
  984. bool cur_state;
  985. val = I915_READ(FDI_RX_CTL(pipe));
  986. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  987. I915_STATE_WARN(cur_state != state,
  988. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  989. onoff(state), onoff(cur_state));
  990. }
  991. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  992. {
  993. i915_reg_t pp_reg;
  994. u32 val;
  995. enum pipe panel_pipe = PIPE_A;
  996. bool locked = true;
  997. if (WARN_ON(HAS_DDI(dev_priv)))
  998. return;
  999. if (HAS_PCH_SPLIT(dev_priv)) {
  1000. u32 port_sel;
  1001. pp_reg = PP_CONTROL(0);
  1002. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1003. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1004. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1005. panel_pipe = PIPE_B;
  1006. /* XXX: else fix for eDP */
  1007. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1008. /* presumably write lock depends on pipe, not port select */
  1009. pp_reg = PP_CONTROL(pipe);
  1010. panel_pipe = pipe;
  1011. } else {
  1012. pp_reg = PP_CONTROL(0);
  1013. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1014. panel_pipe = PIPE_B;
  1015. }
  1016. val = I915_READ(pp_reg);
  1017. if (!(val & PANEL_POWER_ON) ||
  1018. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1019. locked = false;
  1020. I915_STATE_WARN(panel_pipe == pipe && locked,
  1021. "panel assertion failure, pipe %c regs locked\n",
  1022. pipe_name(pipe));
  1023. }
  1024. static void assert_cursor(struct drm_i915_private *dev_priv,
  1025. enum pipe pipe, bool state)
  1026. {
  1027. bool cur_state;
  1028. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1029. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1030. else
  1031. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1032. I915_STATE_WARN(cur_state != state,
  1033. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1034. pipe_name(pipe), onoff(state), onoff(cur_state));
  1035. }
  1036. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1037. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1038. void assert_pipe(struct drm_i915_private *dev_priv,
  1039. enum pipe pipe, bool state)
  1040. {
  1041. bool cur_state;
  1042. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1043. pipe);
  1044. enum intel_display_power_domain power_domain;
  1045. /* if we need the pipe quirk it must be always on */
  1046. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1047. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1048. state = true;
  1049. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1050. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1051. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1052. cur_state = !!(val & PIPECONF_ENABLE);
  1053. intel_display_power_put(dev_priv, power_domain);
  1054. } else {
  1055. cur_state = false;
  1056. }
  1057. I915_STATE_WARN(cur_state != state,
  1058. "pipe %c assertion failure (expected %s, current %s)\n",
  1059. pipe_name(pipe), onoff(state), onoff(cur_state));
  1060. }
  1061. static void assert_plane(struct drm_i915_private *dev_priv,
  1062. enum plane plane, bool state)
  1063. {
  1064. u32 val;
  1065. bool cur_state;
  1066. val = I915_READ(DSPCNTR(plane));
  1067. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1068. I915_STATE_WARN(cur_state != state,
  1069. "plane %c assertion failure (expected %s, current %s)\n",
  1070. plane_name(plane), onoff(state), onoff(cur_state));
  1071. }
  1072. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1073. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1074. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1075. enum pipe pipe)
  1076. {
  1077. int i;
  1078. /* Primary planes are fixed to pipes on gen4+ */
  1079. if (INTEL_GEN(dev_priv) >= 4) {
  1080. u32 val = I915_READ(DSPCNTR(pipe));
  1081. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1082. "plane %c assertion failure, should be disabled but not\n",
  1083. plane_name(pipe));
  1084. return;
  1085. }
  1086. /* Need to check both planes against the pipe */
  1087. for_each_pipe(dev_priv, i) {
  1088. u32 val = I915_READ(DSPCNTR(i));
  1089. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1090. DISPPLANE_SEL_PIPE_SHIFT;
  1091. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1092. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1093. plane_name(i), pipe_name(pipe));
  1094. }
  1095. }
  1096. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1097. enum pipe pipe)
  1098. {
  1099. int sprite;
  1100. if (INTEL_GEN(dev_priv) >= 9) {
  1101. for_each_sprite(dev_priv, pipe, sprite) {
  1102. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1103. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1104. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1105. sprite, pipe_name(pipe));
  1106. }
  1107. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1108. for_each_sprite(dev_priv, pipe, sprite) {
  1109. u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
  1110. I915_STATE_WARN(val & SP_ENABLE,
  1111. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1112. sprite_name(pipe, sprite), pipe_name(pipe));
  1113. }
  1114. } else if (INTEL_GEN(dev_priv) >= 7) {
  1115. u32 val = I915_READ(SPRCTL(pipe));
  1116. I915_STATE_WARN(val & SPRITE_ENABLE,
  1117. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1118. plane_name(pipe), pipe_name(pipe));
  1119. } else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
  1120. u32 val = I915_READ(DVSCNTR(pipe));
  1121. I915_STATE_WARN(val & DVS_ENABLE,
  1122. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1123. plane_name(pipe), pipe_name(pipe));
  1124. }
  1125. }
  1126. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1127. {
  1128. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1129. drm_crtc_vblank_put(crtc);
  1130. }
  1131. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1132. enum pipe pipe)
  1133. {
  1134. u32 val;
  1135. bool enabled;
  1136. val = I915_READ(PCH_TRANSCONF(pipe));
  1137. enabled = !!(val & TRANS_ENABLE);
  1138. I915_STATE_WARN(enabled,
  1139. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1140. pipe_name(pipe));
  1141. }
  1142. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1143. enum pipe pipe, u32 port_sel, u32 val)
  1144. {
  1145. if ((val & DP_PORT_EN) == 0)
  1146. return false;
  1147. if (HAS_PCH_CPT(dev_priv)) {
  1148. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1149. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1150. return false;
  1151. } else if (IS_CHERRYVIEW(dev_priv)) {
  1152. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1153. return false;
  1154. } else {
  1155. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1156. return false;
  1157. }
  1158. return true;
  1159. }
  1160. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1161. enum pipe pipe, u32 val)
  1162. {
  1163. if ((val & SDVO_ENABLE) == 0)
  1164. return false;
  1165. if (HAS_PCH_CPT(dev_priv)) {
  1166. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1167. return false;
  1168. } else if (IS_CHERRYVIEW(dev_priv)) {
  1169. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1170. return false;
  1171. } else {
  1172. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1173. return false;
  1174. }
  1175. return true;
  1176. }
  1177. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1178. enum pipe pipe, u32 val)
  1179. {
  1180. if ((val & LVDS_PORT_EN) == 0)
  1181. return false;
  1182. if (HAS_PCH_CPT(dev_priv)) {
  1183. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1184. return false;
  1185. } else {
  1186. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1187. return false;
  1188. }
  1189. return true;
  1190. }
  1191. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1192. enum pipe pipe, u32 val)
  1193. {
  1194. if ((val & ADPA_DAC_ENABLE) == 0)
  1195. return false;
  1196. if (HAS_PCH_CPT(dev_priv)) {
  1197. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1198. return false;
  1199. } else {
  1200. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1201. return false;
  1202. }
  1203. return true;
  1204. }
  1205. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1206. enum pipe pipe, i915_reg_t reg,
  1207. u32 port_sel)
  1208. {
  1209. u32 val = I915_READ(reg);
  1210. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1211. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1212. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1213. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1214. && (val & DP_PIPEB_SELECT),
  1215. "IBX PCH dp port still using transcoder B\n");
  1216. }
  1217. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1218. enum pipe pipe, i915_reg_t reg)
  1219. {
  1220. u32 val = I915_READ(reg);
  1221. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1222. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1223. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1224. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1225. && (val & SDVO_PIPE_B_SELECT),
  1226. "IBX PCH hdmi port still using transcoder B\n");
  1227. }
  1228. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1229. enum pipe pipe)
  1230. {
  1231. u32 val;
  1232. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1233. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1234. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1235. val = I915_READ(PCH_ADPA);
  1236. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1237. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1238. pipe_name(pipe));
  1239. val = I915_READ(PCH_LVDS);
  1240. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1241. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1242. pipe_name(pipe));
  1243. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1244. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1245. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1246. }
  1247. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1248. const struct intel_crtc_state *pipe_config)
  1249. {
  1250. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1251. enum pipe pipe = crtc->pipe;
  1252. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1253. POSTING_READ(DPLL(pipe));
  1254. udelay(150);
  1255. if (intel_wait_for_register(dev_priv,
  1256. DPLL(pipe),
  1257. DPLL_LOCK_VLV,
  1258. DPLL_LOCK_VLV,
  1259. 1))
  1260. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1261. }
  1262. static void vlv_enable_pll(struct intel_crtc *crtc,
  1263. const struct intel_crtc_state *pipe_config)
  1264. {
  1265. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1266. enum pipe pipe = crtc->pipe;
  1267. assert_pipe_disabled(dev_priv, pipe);
  1268. /* PLL is protected by panel, make sure we can write it */
  1269. assert_panel_unlocked(dev_priv, pipe);
  1270. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1271. _vlv_enable_pll(crtc, pipe_config);
  1272. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1273. POSTING_READ(DPLL_MD(pipe));
  1274. }
  1275. static void _chv_enable_pll(struct intel_crtc *crtc,
  1276. const struct intel_crtc_state *pipe_config)
  1277. {
  1278. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1279. enum pipe pipe = crtc->pipe;
  1280. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1281. u32 tmp;
  1282. mutex_lock(&dev_priv->sb_lock);
  1283. /* Enable back the 10bit clock to display controller */
  1284. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1285. tmp |= DPIO_DCLKP_EN;
  1286. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1287. mutex_unlock(&dev_priv->sb_lock);
  1288. /*
  1289. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1290. */
  1291. udelay(1);
  1292. /* Enable PLL */
  1293. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1294. /* Check PLL is locked */
  1295. if (intel_wait_for_register(dev_priv,
  1296. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1297. 1))
  1298. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1299. }
  1300. static void chv_enable_pll(struct intel_crtc *crtc,
  1301. const struct intel_crtc_state *pipe_config)
  1302. {
  1303. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1304. enum pipe pipe = crtc->pipe;
  1305. assert_pipe_disabled(dev_priv, pipe);
  1306. /* PLL is protected by panel, make sure we can write it */
  1307. assert_panel_unlocked(dev_priv, pipe);
  1308. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1309. _chv_enable_pll(crtc, pipe_config);
  1310. if (pipe != PIPE_A) {
  1311. /*
  1312. * WaPixelRepeatModeFixForC0:chv
  1313. *
  1314. * DPLLCMD is AWOL. Use chicken bits to propagate
  1315. * the value from DPLLBMD to either pipe B or C.
  1316. */
  1317. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1318. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1319. I915_WRITE(CBR4_VLV, 0);
  1320. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1321. /*
  1322. * DPLLB VGA mode also seems to cause problems.
  1323. * We should always have it disabled.
  1324. */
  1325. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1326. } else {
  1327. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1328. POSTING_READ(DPLL_MD(pipe));
  1329. }
  1330. }
  1331. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1332. {
  1333. struct intel_crtc *crtc;
  1334. int count = 0;
  1335. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1336. count += crtc->base.state->active &&
  1337. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1338. }
  1339. return count;
  1340. }
  1341. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1342. {
  1343. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1344. i915_reg_t reg = DPLL(crtc->pipe);
  1345. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1346. assert_pipe_disabled(dev_priv, crtc->pipe);
  1347. /* PLL is protected by panel, make sure we can write it */
  1348. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1349. assert_panel_unlocked(dev_priv, crtc->pipe);
  1350. /* Enable DVO 2x clock on both PLLs if necessary */
  1351. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1352. /*
  1353. * It appears to be important that we don't enable this
  1354. * for the current pipe before otherwise configuring the
  1355. * PLL. No idea how this should be handled if multiple
  1356. * DVO outputs are enabled simultaneosly.
  1357. */
  1358. dpll |= DPLL_DVO_2X_MODE;
  1359. I915_WRITE(DPLL(!crtc->pipe),
  1360. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1361. }
  1362. /*
  1363. * Apparently we need to have VGA mode enabled prior to changing
  1364. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1365. * dividers, even though the register value does change.
  1366. */
  1367. I915_WRITE(reg, 0);
  1368. I915_WRITE(reg, dpll);
  1369. /* Wait for the clocks to stabilize. */
  1370. POSTING_READ(reg);
  1371. udelay(150);
  1372. if (INTEL_GEN(dev_priv) >= 4) {
  1373. I915_WRITE(DPLL_MD(crtc->pipe),
  1374. crtc->config->dpll_hw_state.dpll_md);
  1375. } else {
  1376. /* The pixel multiplier can only be updated once the
  1377. * DPLL is enabled and the clocks are stable.
  1378. *
  1379. * So write it again.
  1380. */
  1381. I915_WRITE(reg, dpll);
  1382. }
  1383. /* We do this three times for luck */
  1384. I915_WRITE(reg, dpll);
  1385. POSTING_READ(reg);
  1386. udelay(150); /* wait for warmup */
  1387. I915_WRITE(reg, dpll);
  1388. POSTING_READ(reg);
  1389. udelay(150); /* wait for warmup */
  1390. I915_WRITE(reg, dpll);
  1391. POSTING_READ(reg);
  1392. udelay(150); /* wait for warmup */
  1393. }
  1394. /**
  1395. * i9xx_disable_pll - disable a PLL
  1396. * @dev_priv: i915 private structure
  1397. * @pipe: pipe PLL to disable
  1398. *
  1399. * Disable the PLL for @pipe, making sure the pipe is off first.
  1400. *
  1401. * Note! This is for pre-ILK only.
  1402. */
  1403. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1404. {
  1405. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1406. enum pipe pipe = crtc->pipe;
  1407. /* Disable DVO 2x clock on both PLLs if necessary */
  1408. if (IS_I830(dev_priv) &&
  1409. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1410. !intel_num_dvo_pipes(dev_priv)) {
  1411. I915_WRITE(DPLL(PIPE_B),
  1412. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1413. I915_WRITE(DPLL(PIPE_A),
  1414. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1415. }
  1416. /* Don't disable pipe or pipe PLLs if needed */
  1417. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1418. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1419. return;
  1420. /* Make sure the pipe isn't still relying on us */
  1421. assert_pipe_disabled(dev_priv, pipe);
  1422. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1423. POSTING_READ(DPLL(pipe));
  1424. }
  1425. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1426. {
  1427. u32 val;
  1428. /* Make sure the pipe isn't still relying on us */
  1429. assert_pipe_disabled(dev_priv, pipe);
  1430. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1431. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1432. if (pipe != PIPE_A)
  1433. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1434. I915_WRITE(DPLL(pipe), val);
  1435. POSTING_READ(DPLL(pipe));
  1436. }
  1437. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1438. {
  1439. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1440. u32 val;
  1441. /* Make sure the pipe isn't still relying on us */
  1442. assert_pipe_disabled(dev_priv, pipe);
  1443. val = DPLL_SSC_REF_CLK_CHV |
  1444. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1445. if (pipe != PIPE_A)
  1446. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1447. I915_WRITE(DPLL(pipe), val);
  1448. POSTING_READ(DPLL(pipe));
  1449. mutex_lock(&dev_priv->sb_lock);
  1450. /* Disable 10bit clock to display controller */
  1451. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1452. val &= ~DPIO_DCLKP_EN;
  1453. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1454. mutex_unlock(&dev_priv->sb_lock);
  1455. }
  1456. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1457. struct intel_digital_port *dport,
  1458. unsigned int expected_mask)
  1459. {
  1460. u32 port_mask;
  1461. i915_reg_t dpll_reg;
  1462. switch (dport->port) {
  1463. case PORT_B:
  1464. port_mask = DPLL_PORTB_READY_MASK;
  1465. dpll_reg = DPLL(0);
  1466. break;
  1467. case PORT_C:
  1468. port_mask = DPLL_PORTC_READY_MASK;
  1469. dpll_reg = DPLL(0);
  1470. expected_mask <<= 4;
  1471. break;
  1472. case PORT_D:
  1473. port_mask = DPLL_PORTD_READY_MASK;
  1474. dpll_reg = DPIO_PHY_STATUS;
  1475. break;
  1476. default:
  1477. BUG();
  1478. }
  1479. if (intel_wait_for_register(dev_priv,
  1480. dpll_reg, port_mask, expected_mask,
  1481. 1000))
  1482. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1483. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1484. }
  1485. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1486. enum pipe pipe)
  1487. {
  1488. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1489. pipe);
  1490. i915_reg_t reg;
  1491. uint32_t val, pipeconf_val;
  1492. /* Make sure PCH DPLL is enabled */
  1493. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1494. /* FDI must be feeding us bits for PCH ports */
  1495. assert_fdi_tx_enabled(dev_priv, pipe);
  1496. assert_fdi_rx_enabled(dev_priv, pipe);
  1497. if (HAS_PCH_CPT(dev_priv)) {
  1498. /* Workaround: Set the timing override bit before enabling the
  1499. * pch transcoder. */
  1500. reg = TRANS_CHICKEN2(pipe);
  1501. val = I915_READ(reg);
  1502. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1503. I915_WRITE(reg, val);
  1504. }
  1505. reg = PCH_TRANSCONF(pipe);
  1506. val = I915_READ(reg);
  1507. pipeconf_val = I915_READ(PIPECONF(pipe));
  1508. if (HAS_PCH_IBX(dev_priv)) {
  1509. /*
  1510. * Make the BPC in transcoder be consistent with
  1511. * that in pipeconf reg. For HDMI we must use 8bpc
  1512. * here for both 8bpc and 12bpc.
  1513. */
  1514. val &= ~PIPECONF_BPC_MASK;
  1515. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1516. val |= PIPECONF_8BPC;
  1517. else
  1518. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1519. }
  1520. val &= ~TRANS_INTERLACE_MASK;
  1521. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1522. if (HAS_PCH_IBX(dev_priv) &&
  1523. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1524. val |= TRANS_LEGACY_INTERLACED_ILK;
  1525. else
  1526. val |= TRANS_INTERLACED;
  1527. else
  1528. val |= TRANS_PROGRESSIVE;
  1529. I915_WRITE(reg, val | TRANS_ENABLE);
  1530. if (intel_wait_for_register(dev_priv,
  1531. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1532. 100))
  1533. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1534. }
  1535. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1536. enum transcoder cpu_transcoder)
  1537. {
  1538. u32 val, pipeconf_val;
  1539. /* FDI must be feeding us bits for PCH ports */
  1540. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1541. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1542. /* Workaround: set timing override bit. */
  1543. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1544. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1545. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1546. val = TRANS_ENABLE;
  1547. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1548. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1549. PIPECONF_INTERLACED_ILK)
  1550. val |= TRANS_INTERLACED;
  1551. else
  1552. val |= TRANS_PROGRESSIVE;
  1553. I915_WRITE(LPT_TRANSCONF, val);
  1554. if (intel_wait_for_register(dev_priv,
  1555. LPT_TRANSCONF,
  1556. TRANS_STATE_ENABLE,
  1557. TRANS_STATE_ENABLE,
  1558. 100))
  1559. DRM_ERROR("Failed to enable PCH transcoder\n");
  1560. }
  1561. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1562. enum pipe pipe)
  1563. {
  1564. i915_reg_t reg;
  1565. uint32_t val;
  1566. /* FDI relies on the transcoder */
  1567. assert_fdi_tx_disabled(dev_priv, pipe);
  1568. assert_fdi_rx_disabled(dev_priv, pipe);
  1569. /* Ports must be off as well */
  1570. assert_pch_ports_disabled(dev_priv, pipe);
  1571. reg = PCH_TRANSCONF(pipe);
  1572. val = I915_READ(reg);
  1573. val &= ~TRANS_ENABLE;
  1574. I915_WRITE(reg, val);
  1575. /* wait for PCH transcoder off, transcoder state */
  1576. if (intel_wait_for_register(dev_priv,
  1577. reg, TRANS_STATE_ENABLE, 0,
  1578. 50))
  1579. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1580. if (HAS_PCH_CPT(dev_priv)) {
  1581. /* Workaround: Clear the timing override chicken bit again. */
  1582. reg = TRANS_CHICKEN2(pipe);
  1583. val = I915_READ(reg);
  1584. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1585. I915_WRITE(reg, val);
  1586. }
  1587. }
  1588. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1589. {
  1590. u32 val;
  1591. val = I915_READ(LPT_TRANSCONF);
  1592. val &= ~TRANS_ENABLE;
  1593. I915_WRITE(LPT_TRANSCONF, val);
  1594. /* wait for PCH transcoder off, transcoder state */
  1595. if (intel_wait_for_register(dev_priv,
  1596. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1597. 50))
  1598. DRM_ERROR("Failed to disable PCH transcoder\n");
  1599. /* Workaround: clear timing override bit. */
  1600. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1601. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1602. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1603. }
  1604. enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1605. {
  1606. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1607. WARN_ON(!crtc->config->has_pch_encoder);
  1608. if (HAS_PCH_LPT(dev_priv))
  1609. return TRANSCODER_A;
  1610. else
  1611. return (enum transcoder) crtc->pipe;
  1612. }
  1613. /**
  1614. * intel_enable_pipe - enable a pipe, asserting requirements
  1615. * @crtc: crtc responsible for the pipe
  1616. *
  1617. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1618. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1619. */
  1620. static void intel_enable_pipe(struct intel_crtc *crtc)
  1621. {
  1622. struct drm_device *dev = crtc->base.dev;
  1623. struct drm_i915_private *dev_priv = to_i915(dev);
  1624. enum pipe pipe = crtc->pipe;
  1625. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1626. i915_reg_t reg;
  1627. u32 val;
  1628. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1629. assert_planes_disabled(dev_priv, pipe);
  1630. assert_cursor_disabled(dev_priv, pipe);
  1631. assert_sprites_disabled(dev_priv, pipe);
  1632. /*
  1633. * A pipe without a PLL won't actually be able to drive bits from
  1634. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1635. * need the check.
  1636. */
  1637. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1638. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1639. assert_dsi_pll_enabled(dev_priv);
  1640. else
  1641. assert_pll_enabled(dev_priv, pipe);
  1642. } else {
  1643. if (crtc->config->has_pch_encoder) {
  1644. /* if driving the PCH, we need FDI enabled */
  1645. assert_fdi_rx_pll_enabled(dev_priv,
  1646. (enum pipe) intel_crtc_pch_transcoder(crtc));
  1647. assert_fdi_tx_pll_enabled(dev_priv,
  1648. (enum pipe) cpu_transcoder);
  1649. }
  1650. /* FIXME: assert CPU port conditions for SNB+ */
  1651. }
  1652. reg = PIPECONF(cpu_transcoder);
  1653. val = I915_READ(reg);
  1654. if (val & PIPECONF_ENABLE) {
  1655. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1656. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1657. return;
  1658. }
  1659. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1660. POSTING_READ(reg);
  1661. /*
  1662. * Until the pipe starts DSL will read as 0, which would cause
  1663. * an apparent vblank timestamp jump, which messes up also the
  1664. * frame count when it's derived from the timestamps. So let's
  1665. * wait for the pipe to start properly before we call
  1666. * drm_crtc_vblank_on()
  1667. */
  1668. if (dev->max_vblank_count == 0 &&
  1669. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1670. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1671. }
  1672. /**
  1673. * intel_disable_pipe - disable a pipe, asserting requirements
  1674. * @crtc: crtc whose pipes is to be disabled
  1675. *
  1676. * Disable the pipe of @crtc, making sure that various hardware
  1677. * specific requirements are met, if applicable, e.g. plane
  1678. * disabled, panel fitter off, etc.
  1679. *
  1680. * Will wait until the pipe has shut down before returning.
  1681. */
  1682. static void intel_disable_pipe(struct intel_crtc *crtc)
  1683. {
  1684. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1685. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1686. enum pipe pipe = crtc->pipe;
  1687. i915_reg_t reg;
  1688. u32 val;
  1689. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1690. /*
  1691. * Make sure planes won't keep trying to pump pixels to us,
  1692. * or we might hang the display.
  1693. */
  1694. assert_planes_disabled(dev_priv, pipe);
  1695. assert_cursor_disabled(dev_priv, pipe);
  1696. assert_sprites_disabled(dev_priv, pipe);
  1697. reg = PIPECONF(cpu_transcoder);
  1698. val = I915_READ(reg);
  1699. if ((val & PIPECONF_ENABLE) == 0)
  1700. return;
  1701. /*
  1702. * Double wide has implications for planes
  1703. * so best keep it disabled when not needed.
  1704. */
  1705. if (crtc->config->double_wide)
  1706. val &= ~PIPECONF_DOUBLE_WIDE;
  1707. /* Don't disable pipe or pipe PLLs if needed */
  1708. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1709. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1710. val &= ~PIPECONF_ENABLE;
  1711. I915_WRITE(reg, val);
  1712. if ((val & PIPECONF_ENABLE) == 0)
  1713. intel_wait_for_pipe_off(crtc);
  1714. }
  1715. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1716. {
  1717. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1718. }
  1719. static unsigned int
  1720. intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
  1721. {
  1722. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1723. unsigned int cpp = fb->format->cpp[plane];
  1724. switch (fb->modifier) {
  1725. case DRM_FORMAT_MOD_LINEAR:
  1726. return cpp;
  1727. case I915_FORMAT_MOD_X_TILED:
  1728. if (IS_GEN2(dev_priv))
  1729. return 128;
  1730. else
  1731. return 512;
  1732. case I915_FORMAT_MOD_Y_TILED:
  1733. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1734. return 128;
  1735. else
  1736. return 512;
  1737. case I915_FORMAT_MOD_Yf_TILED:
  1738. switch (cpp) {
  1739. case 1:
  1740. return 64;
  1741. case 2:
  1742. case 4:
  1743. return 128;
  1744. case 8:
  1745. case 16:
  1746. return 256;
  1747. default:
  1748. MISSING_CASE(cpp);
  1749. return cpp;
  1750. }
  1751. break;
  1752. default:
  1753. MISSING_CASE(fb->modifier);
  1754. return cpp;
  1755. }
  1756. }
  1757. static unsigned int
  1758. intel_tile_height(const struct drm_framebuffer *fb, int plane)
  1759. {
  1760. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  1761. return 1;
  1762. else
  1763. return intel_tile_size(to_i915(fb->dev)) /
  1764. intel_tile_width_bytes(fb, plane);
  1765. }
  1766. /* Return the tile dimensions in pixel units */
  1767. static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
  1768. unsigned int *tile_width,
  1769. unsigned int *tile_height)
  1770. {
  1771. unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
  1772. unsigned int cpp = fb->format->cpp[plane];
  1773. *tile_width = tile_width_bytes / cpp;
  1774. *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
  1775. }
  1776. unsigned int
  1777. intel_fb_align_height(const struct drm_framebuffer *fb,
  1778. int plane, unsigned int height)
  1779. {
  1780. unsigned int tile_height = intel_tile_height(fb, plane);
  1781. return ALIGN(height, tile_height);
  1782. }
  1783. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1784. {
  1785. unsigned int size = 0;
  1786. int i;
  1787. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1788. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1789. return size;
  1790. }
  1791. static void
  1792. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1793. const struct drm_framebuffer *fb,
  1794. unsigned int rotation)
  1795. {
  1796. view->type = I915_GGTT_VIEW_NORMAL;
  1797. if (drm_rotation_90_or_270(rotation)) {
  1798. view->type = I915_GGTT_VIEW_ROTATED;
  1799. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1800. }
  1801. }
  1802. static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
  1803. {
  1804. if (IS_I830(dev_priv))
  1805. return 16 * 1024;
  1806. else if (IS_I85X(dev_priv))
  1807. return 256;
  1808. else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1809. return 32;
  1810. else
  1811. return 4 * 1024;
  1812. }
  1813. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1814. {
  1815. if (INTEL_INFO(dev_priv)->gen >= 9)
  1816. return 256 * 1024;
  1817. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1818. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1819. return 128 * 1024;
  1820. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1821. return 4 * 1024;
  1822. else
  1823. return 0;
  1824. }
  1825. static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
  1826. int plane)
  1827. {
  1828. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1829. /* AUX_DIST needs only 4K alignment */
  1830. if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
  1831. return 4096;
  1832. switch (fb->modifier) {
  1833. case DRM_FORMAT_MOD_LINEAR:
  1834. return intel_linear_alignment(dev_priv);
  1835. case I915_FORMAT_MOD_X_TILED:
  1836. if (INTEL_GEN(dev_priv) >= 9)
  1837. return 256 * 1024;
  1838. return 0;
  1839. case I915_FORMAT_MOD_Y_TILED:
  1840. case I915_FORMAT_MOD_Yf_TILED:
  1841. return 1 * 1024 * 1024;
  1842. default:
  1843. MISSING_CASE(fb->modifier);
  1844. return 0;
  1845. }
  1846. }
  1847. struct i915_vma *
  1848. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1849. {
  1850. struct drm_device *dev = fb->dev;
  1851. struct drm_i915_private *dev_priv = to_i915(dev);
  1852. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1853. struct i915_ggtt_view view;
  1854. struct i915_vma *vma;
  1855. u32 alignment;
  1856. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1857. alignment = intel_surf_alignment(fb, 0);
  1858. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1859. /* Note that the w/a also requires 64 PTE of padding following the
  1860. * bo. We currently fill all unused PTE with the shadow page and so
  1861. * we should always have valid PTE following the scanout preventing
  1862. * the VT-d warning.
  1863. */
  1864. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1865. alignment = 256 * 1024;
  1866. /*
  1867. * Global gtt pte registers are special registers which actually forward
  1868. * writes to a chunk of system memory. Which means that there is no risk
  1869. * that the register values disappear as soon as we call
  1870. * intel_runtime_pm_put(), so it is correct to wrap only the
  1871. * pin/unpin/fence and not more.
  1872. */
  1873. intel_runtime_pm_get(dev_priv);
  1874. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1875. if (IS_ERR(vma))
  1876. goto err;
  1877. if (i915_vma_is_map_and_fenceable(vma)) {
  1878. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1879. * fence, whereas 965+ only requires a fence if using
  1880. * framebuffer compression. For simplicity, we always, when
  1881. * possible, install a fence as the cost is not that onerous.
  1882. *
  1883. * If we fail to fence the tiled scanout, then either the
  1884. * modeset will reject the change (which is highly unlikely as
  1885. * the affected systems, all but one, do not have unmappable
  1886. * space) or we will not be able to enable full powersaving
  1887. * techniques (also likely not to apply due to various limits
  1888. * FBC and the like impose on the size of the buffer, which
  1889. * presumably we violated anyway with this unmappable buffer).
  1890. * Anyway, it is presumably better to stumble onwards with
  1891. * something and try to run the system in a "less than optimal"
  1892. * mode that matches the user configuration.
  1893. */
  1894. if (i915_vma_get_fence(vma) == 0)
  1895. i915_vma_pin_fence(vma);
  1896. }
  1897. i915_vma_get(vma);
  1898. err:
  1899. intel_runtime_pm_put(dev_priv);
  1900. return vma;
  1901. }
  1902. void intel_unpin_fb_vma(struct i915_vma *vma)
  1903. {
  1904. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1905. i915_vma_unpin_fence(vma);
  1906. i915_gem_object_unpin_from_display_plane(vma);
  1907. i915_vma_put(vma);
  1908. }
  1909. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1910. unsigned int rotation)
  1911. {
  1912. if (drm_rotation_90_or_270(rotation))
  1913. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1914. else
  1915. return fb->pitches[plane];
  1916. }
  1917. /*
  1918. * Convert the x/y offsets into a linear offset.
  1919. * Only valid with 0/180 degree rotation, which is fine since linear
  1920. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1921. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1922. */
  1923. u32 intel_fb_xy_to_linear(int x, int y,
  1924. const struct intel_plane_state *state,
  1925. int plane)
  1926. {
  1927. const struct drm_framebuffer *fb = state->base.fb;
  1928. unsigned int cpp = fb->format->cpp[plane];
  1929. unsigned int pitch = fb->pitches[plane];
  1930. return y * pitch + x * cpp;
  1931. }
  1932. /*
  1933. * Add the x/y offsets derived from fb->offsets[] to the user
  1934. * specified plane src x/y offsets. The resulting x/y offsets
  1935. * specify the start of scanout from the beginning of the gtt mapping.
  1936. */
  1937. void intel_add_fb_offsets(int *x, int *y,
  1938. const struct intel_plane_state *state,
  1939. int plane)
  1940. {
  1941. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1942. unsigned int rotation = state->base.rotation;
  1943. if (drm_rotation_90_or_270(rotation)) {
  1944. *x += intel_fb->rotated[plane].x;
  1945. *y += intel_fb->rotated[plane].y;
  1946. } else {
  1947. *x += intel_fb->normal[plane].x;
  1948. *y += intel_fb->normal[plane].y;
  1949. }
  1950. }
  1951. /*
  1952. * Input tile dimensions and pitch must already be
  1953. * rotated to match x and y, and in pixel units.
  1954. */
  1955. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1956. unsigned int tile_width,
  1957. unsigned int tile_height,
  1958. unsigned int tile_size,
  1959. unsigned int pitch_tiles,
  1960. u32 old_offset,
  1961. u32 new_offset)
  1962. {
  1963. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1964. unsigned int tiles;
  1965. WARN_ON(old_offset & (tile_size - 1));
  1966. WARN_ON(new_offset & (tile_size - 1));
  1967. WARN_ON(new_offset > old_offset);
  1968. tiles = (old_offset - new_offset) / tile_size;
  1969. *y += tiles / pitch_tiles * tile_height;
  1970. *x += tiles % pitch_tiles * tile_width;
  1971. /* minimize x in case it got needlessly big */
  1972. *y += *x / pitch_pixels * tile_height;
  1973. *x %= pitch_pixels;
  1974. return new_offset;
  1975. }
  1976. /*
  1977. * Adjust the tile offset by moving the difference into
  1978. * the x/y offsets.
  1979. */
  1980. static u32 intel_adjust_tile_offset(int *x, int *y,
  1981. const struct intel_plane_state *state, int plane,
  1982. u32 old_offset, u32 new_offset)
  1983. {
  1984. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  1985. const struct drm_framebuffer *fb = state->base.fb;
  1986. unsigned int cpp = fb->format->cpp[plane];
  1987. unsigned int rotation = state->base.rotation;
  1988. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  1989. WARN_ON(new_offset > old_offset);
  1990. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  1991. unsigned int tile_size, tile_width, tile_height;
  1992. unsigned int pitch_tiles;
  1993. tile_size = intel_tile_size(dev_priv);
  1994. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  1995. if (drm_rotation_90_or_270(rotation)) {
  1996. pitch_tiles = pitch / tile_height;
  1997. swap(tile_width, tile_height);
  1998. } else {
  1999. pitch_tiles = pitch / (tile_width * cpp);
  2000. }
  2001. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2002. tile_size, pitch_tiles,
  2003. old_offset, new_offset);
  2004. } else {
  2005. old_offset += *y * pitch + *x * cpp;
  2006. *y = (old_offset - new_offset) / pitch;
  2007. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2008. }
  2009. return new_offset;
  2010. }
  2011. /*
  2012. * Computes the linear offset to the base tile and adjusts
  2013. * x, y. bytes per pixel is assumed to be a power-of-two.
  2014. *
  2015. * In the 90/270 rotated case, x and y are assumed
  2016. * to be already rotated to match the rotated GTT view, and
  2017. * pitch is the tile_height aligned framebuffer height.
  2018. *
  2019. * This function is used when computing the derived information
  2020. * under intel_framebuffer, so using any of that information
  2021. * here is not allowed. Anything under drm_framebuffer can be
  2022. * used. This is why the user has to pass in the pitch since it
  2023. * is specified in the rotated orientation.
  2024. */
  2025. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2026. int *x, int *y,
  2027. const struct drm_framebuffer *fb, int plane,
  2028. unsigned int pitch,
  2029. unsigned int rotation,
  2030. u32 alignment)
  2031. {
  2032. uint64_t fb_modifier = fb->modifier;
  2033. unsigned int cpp = fb->format->cpp[plane];
  2034. u32 offset, offset_aligned;
  2035. if (alignment)
  2036. alignment--;
  2037. if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
  2038. unsigned int tile_size, tile_width, tile_height;
  2039. unsigned int tile_rows, tiles, pitch_tiles;
  2040. tile_size = intel_tile_size(dev_priv);
  2041. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2042. if (drm_rotation_90_or_270(rotation)) {
  2043. pitch_tiles = pitch / tile_height;
  2044. swap(tile_width, tile_height);
  2045. } else {
  2046. pitch_tiles = pitch / (tile_width * cpp);
  2047. }
  2048. tile_rows = *y / tile_height;
  2049. *y %= tile_height;
  2050. tiles = *x / tile_width;
  2051. *x %= tile_width;
  2052. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2053. offset_aligned = offset & ~alignment;
  2054. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2055. tile_size, pitch_tiles,
  2056. offset, offset_aligned);
  2057. } else {
  2058. offset = *y * pitch + *x * cpp;
  2059. offset_aligned = offset & ~alignment;
  2060. *y = (offset & alignment) / pitch;
  2061. *x = ((offset & alignment) - *y * pitch) / cpp;
  2062. }
  2063. return offset_aligned;
  2064. }
  2065. u32 intel_compute_tile_offset(int *x, int *y,
  2066. const struct intel_plane_state *state,
  2067. int plane)
  2068. {
  2069. struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
  2070. struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
  2071. const struct drm_framebuffer *fb = state->base.fb;
  2072. unsigned int rotation = state->base.rotation;
  2073. int pitch = intel_fb_pitch(fb, plane, rotation);
  2074. u32 alignment;
  2075. if (intel_plane->id == PLANE_CURSOR)
  2076. alignment = intel_cursor_alignment(dev_priv);
  2077. else
  2078. alignment = intel_surf_alignment(fb, plane);
  2079. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2080. rotation, alignment);
  2081. }
  2082. /* Convert the fb->offset[] linear offset into x/y offsets */
  2083. static void intel_fb_offset_to_xy(int *x, int *y,
  2084. const struct drm_framebuffer *fb, int plane)
  2085. {
  2086. unsigned int cpp = fb->format->cpp[plane];
  2087. unsigned int pitch = fb->pitches[plane];
  2088. u32 linear_offset = fb->offsets[plane];
  2089. *y = linear_offset / pitch;
  2090. *x = linear_offset % pitch / cpp;
  2091. }
  2092. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2093. {
  2094. switch (fb_modifier) {
  2095. case I915_FORMAT_MOD_X_TILED:
  2096. return I915_TILING_X;
  2097. case I915_FORMAT_MOD_Y_TILED:
  2098. return I915_TILING_Y;
  2099. default:
  2100. return I915_TILING_NONE;
  2101. }
  2102. }
  2103. static int
  2104. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2105. struct drm_framebuffer *fb)
  2106. {
  2107. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2108. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2109. u32 gtt_offset_rotated = 0;
  2110. unsigned int max_size = 0;
  2111. int i, num_planes = fb->format->num_planes;
  2112. unsigned int tile_size = intel_tile_size(dev_priv);
  2113. for (i = 0; i < num_planes; i++) {
  2114. unsigned int width, height;
  2115. unsigned int cpp, size;
  2116. u32 offset;
  2117. int x, y;
  2118. cpp = fb->format->cpp[i];
  2119. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2120. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2121. intel_fb_offset_to_xy(&x, &y, fb, i);
  2122. /*
  2123. * The fence (if used) is aligned to the start of the object
  2124. * so having the framebuffer wrap around across the edge of the
  2125. * fenced region doesn't really work. We have no API to configure
  2126. * the fence start offset within the object (nor could we probably
  2127. * on gen2/3). So it's just easier if we just require that the
  2128. * fb layout agrees with the fence layout. We already check that the
  2129. * fb stride matches the fence stride elsewhere.
  2130. */
  2131. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2132. (x + width) * cpp > fb->pitches[i]) {
  2133. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2134. i, fb->offsets[i]);
  2135. return -EINVAL;
  2136. }
  2137. /*
  2138. * First pixel of the framebuffer from
  2139. * the start of the normal gtt mapping.
  2140. */
  2141. intel_fb->normal[i].x = x;
  2142. intel_fb->normal[i].y = y;
  2143. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2144. fb, i, fb->pitches[i],
  2145. DRM_MODE_ROTATE_0, tile_size);
  2146. offset /= tile_size;
  2147. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2148. unsigned int tile_width, tile_height;
  2149. unsigned int pitch_tiles;
  2150. struct drm_rect r;
  2151. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2152. rot_info->plane[i].offset = offset;
  2153. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2154. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2155. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2156. intel_fb->rotated[i].pitch =
  2157. rot_info->plane[i].height * tile_height;
  2158. /* how many tiles does this plane need */
  2159. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2160. /*
  2161. * If the plane isn't horizontally tile aligned,
  2162. * we need one more tile.
  2163. */
  2164. if (x != 0)
  2165. size++;
  2166. /* rotate the x/y offsets to match the GTT view */
  2167. r.x1 = x;
  2168. r.y1 = y;
  2169. r.x2 = x + width;
  2170. r.y2 = y + height;
  2171. drm_rect_rotate(&r,
  2172. rot_info->plane[i].width * tile_width,
  2173. rot_info->plane[i].height * tile_height,
  2174. DRM_MODE_ROTATE_270);
  2175. x = r.x1;
  2176. y = r.y1;
  2177. /* rotate the tile dimensions to match the GTT view */
  2178. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2179. swap(tile_width, tile_height);
  2180. /*
  2181. * We only keep the x/y offsets, so push all of the
  2182. * gtt offset into the x/y offsets.
  2183. */
  2184. _intel_adjust_tile_offset(&x, &y,
  2185. tile_width, tile_height,
  2186. tile_size, pitch_tiles,
  2187. gtt_offset_rotated * tile_size, 0);
  2188. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2189. /*
  2190. * First pixel of the framebuffer from
  2191. * the start of the rotated gtt mapping.
  2192. */
  2193. intel_fb->rotated[i].x = x;
  2194. intel_fb->rotated[i].y = y;
  2195. } else {
  2196. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2197. x * cpp, tile_size);
  2198. }
  2199. /* how many tiles in total needed in the bo */
  2200. max_size = max(max_size, offset + size);
  2201. }
  2202. if (max_size * tile_size > intel_fb->obj->base.size) {
  2203. DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2204. max_size * tile_size, intel_fb->obj->base.size);
  2205. return -EINVAL;
  2206. }
  2207. return 0;
  2208. }
  2209. static int i9xx_format_to_fourcc(int format)
  2210. {
  2211. switch (format) {
  2212. case DISPPLANE_8BPP:
  2213. return DRM_FORMAT_C8;
  2214. case DISPPLANE_BGRX555:
  2215. return DRM_FORMAT_XRGB1555;
  2216. case DISPPLANE_BGRX565:
  2217. return DRM_FORMAT_RGB565;
  2218. default:
  2219. case DISPPLANE_BGRX888:
  2220. return DRM_FORMAT_XRGB8888;
  2221. case DISPPLANE_RGBX888:
  2222. return DRM_FORMAT_XBGR8888;
  2223. case DISPPLANE_BGRX101010:
  2224. return DRM_FORMAT_XRGB2101010;
  2225. case DISPPLANE_RGBX101010:
  2226. return DRM_FORMAT_XBGR2101010;
  2227. }
  2228. }
  2229. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2230. {
  2231. switch (format) {
  2232. case PLANE_CTL_FORMAT_RGB_565:
  2233. return DRM_FORMAT_RGB565;
  2234. default:
  2235. case PLANE_CTL_FORMAT_XRGB_8888:
  2236. if (rgb_order) {
  2237. if (alpha)
  2238. return DRM_FORMAT_ABGR8888;
  2239. else
  2240. return DRM_FORMAT_XBGR8888;
  2241. } else {
  2242. if (alpha)
  2243. return DRM_FORMAT_ARGB8888;
  2244. else
  2245. return DRM_FORMAT_XRGB8888;
  2246. }
  2247. case PLANE_CTL_FORMAT_XRGB_2101010:
  2248. if (rgb_order)
  2249. return DRM_FORMAT_XBGR2101010;
  2250. else
  2251. return DRM_FORMAT_XRGB2101010;
  2252. }
  2253. }
  2254. static bool
  2255. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2256. struct intel_initial_plane_config *plane_config)
  2257. {
  2258. struct drm_device *dev = crtc->base.dev;
  2259. struct drm_i915_private *dev_priv = to_i915(dev);
  2260. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2261. struct drm_i915_gem_object *obj = NULL;
  2262. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2263. struct drm_framebuffer *fb = &plane_config->fb->base;
  2264. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2265. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2266. PAGE_SIZE);
  2267. size_aligned -= base_aligned;
  2268. if (plane_config->size == 0)
  2269. return false;
  2270. /* If the FB is too big, just don't use it since fbdev is not very
  2271. * important and we should probably use that space with FBC or other
  2272. * features. */
  2273. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2274. return false;
  2275. mutex_lock(&dev->struct_mutex);
  2276. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2277. base_aligned,
  2278. base_aligned,
  2279. size_aligned);
  2280. mutex_unlock(&dev->struct_mutex);
  2281. if (!obj)
  2282. return false;
  2283. if (plane_config->tiling == I915_TILING_X)
  2284. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2285. mode_cmd.pixel_format = fb->format->format;
  2286. mode_cmd.width = fb->width;
  2287. mode_cmd.height = fb->height;
  2288. mode_cmd.pitches[0] = fb->pitches[0];
  2289. mode_cmd.modifier[0] = fb->modifier;
  2290. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2291. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2292. DRM_DEBUG_KMS("intel fb init failed\n");
  2293. goto out_unref_obj;
  2294. }
  2295. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2296. return true;
  2297. out_unref_obj:
  2298. i915_gem_object_put(obj);
  2299. return false;
  2300. }
  2301. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2302. static void
  2303. update_state_fb(struct drm_plane *plane)
  2304. {
  2305. if (plane->fb == plane->state->fb)
  2306. return;
  2307. if (plane->state->fb)
  2308. drm_framebuffer_unreference(plane->state->fb);
  2309. plane->state->fb = plane->fb;
  2310. if (plane->state->fb)
  2311. drm_framebuffer_reference(plane->state->fb);
  2312. }
  2313. static void
  2314. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2315. struct intel_plane_state *plane_state,
  2316. bool visible)
  2317. {
  2318. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2319. plane_state->base.visible = visible;
  2320. /* FIXME pre-g4x don't work like this */
  2321. if (visible) {
  2322. crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
  2323. crtc_state->active_planes |= BIT(plane->id);
  2324. } else {
  2325. crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
  2326. crtc_state->active_planes &= ~BIT(plane->id);
  2327. }
  2328. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2329. crtc_state->base.crtc->name,
  2330. crtc_state->active_planes);
  2331. }
  2332. static void
  2333. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2334. struct intel_initial_plane_config *plane_config)
  2335. {
  2336. struct drm_device *dev = intel_crtc->base.dev;
  2337. struct drm_i915_private *dev_priv = to_i915(dev);
  2338. struct drm_crtc *c;
  2339. struct drm_i915_gem_object *obj;
  2340. struct drm_plane *primary = intel_crtc->base.primary;
  2341. struct drm_plane_state *plane_state = primary->state;
  2342. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2343. struct intel_plane *intel_plane = to_intel_plane(primary);
  2344. struct intel_plane_state *intel_state =
  2345. to_intel_plane_state(plane_state);
  2346. struct drm_framebuffer *fb;
  2347. if (!plane_config->fb)
  2348. return;
  2349. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2350. fb = &plane_config->fb->base;
  2351. goto valid_fb;
  2352. }
  2353. kfree(plane_config->fb);
  2354. /*
  2355. * Failed to alloc the obj, check to see if we should share
  2356. * an fb with another CRTC instead
  2357. */
  2358. for_each_crtc(dev, c) {
  2359. struct intel_plane_state *state;
  2360. if (c == &intel_crtc->base)
  2361. continue;
  2362. if (!to_intel_crtc(c)->active)
  2363. continue;
  2364. state = to_intel_plane_state(c->primary->state);
  2365. if (!state->vma)
  2366. continue;
  2367. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2368. fb = c->primary->fb;
  2369. drm_framebuffer_reference(fb);
  2370. goto valid_fb;
  2371. }
  2372. }
  2373. /*
  2374. * We've failed to reconstruct the BIOS FB. Current display state
  2375. * indicates that the primary plane is visible, but has a NULL FB,
  2376. * which will lead to problems later if we don't fix it up. The
  2377. * simplest solution is to just disable the primary plane now and
  2378. * pretend the BIOS never had it enabled.
  2379. */
  2380. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2381. to_intel_plane_state(plane_state),
  2382. false);
  2383. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2384. trace_intel_disable_plane(primary, intel_crtc);
  2385. intel_plane->disable_plane(intel_plane, intel_crtc);
  2386. return;
  2387. valid_fb:
  2388. mutex_lock(&dev->struct_mutex);
  2389. intel_state->vma =
  2390. intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  2391. mutex_unlock(&dev->struct_mutex);
  2392. if (IS_ERR(intel_state->vma)) {
  2393. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2394. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2395. intel_state->vma = NULL;
  2396. drm_framebuffer_unreference(fb);
  2397. return;
  2398. }
  2399. plane_state->src_x = 0;
  2400. plane_state->src_y = 0;
  2401. plane_state->src_w = fb->width << 16;
  2402. plane_state->src_h = fb->height << 16;
  2403. plane_state->crtc_x = 0;
  2404. plane_state->crtc_y = 0;
  2405. plane_state->crtc_w = fb->width;
  2406. plane_state->crtc_h = fb->height;
  2407. intel_state->base.src = drm_plane_state_src(plane_state);
  2408. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2409. obj = intel_fb_obj(fb);
  2410. if (i915_gem_object_is_tiled(obj))
  2411. dev_priv->preserve_bios_swizzle = true;
  2412. drm_framebuffer_reference(fb);
  2413. primary->fb = primary->state->fb = fb;
  2414. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2415. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2416. to_intel_plane_state(plane_state),
  2417. true);
  2418. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2419. &obj->frontbuffer_bits);
  2420. }
  2421. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2422. unsigned int rotation)
  2423. {
  2424. int cpp = fb->format->cpp[plane];
  2425. switch (fb->modifier) {
  2426. case DRM_FORMAT_MOD_LINEAR:
  2427. case I915_FORMAT_MOD_X_TILED:
  2428. switch (cpp) {
  2429. case 8:
  2430. return 4096;
  2431. case 4:
  2432. case 2:
  2433. case 1:
  2434. return 8192;
  2435. default:
  2436. MISSING_CASE(cpp);
  2437. break;
  2438. }
  2439. break;
  2440. case I915_FORMAT_MOD_Y_TILED:
  2441. case I915_FORMAT_MOD_Yf_TILED:
  2442. switch (cpp) {
  2443. case 8:
  2444. return 2048;
  2445. case 4:
  2446. return 4096;
  2447. case 2:
  2448. case 1:
  2449. return 8192;
  2450. default:
  2451. MISSING_CASE(cpp);
  2452. break;
  2453. }
  2454. break;
  2455. default:
  2456. MISSING_CASE(fb->modifier);
  2457. }
  2458. return 2048;
  2459. }
  2460. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2461. {
  2462. const struct drm_framebuffer *fb = plane_state->base.fb;
  2463. unsigned int rotation = plane_state->base.rotation;
  2464. int x = plane_state->base.src.x1 >> 16;
  2465. int y = plane_state->base.src.y1 >> 16;
  2466. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2467. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2468. int max_width = skl_max_plane_width(fb, 0, rotation);
  2469. int max_height = 4096;
  2470. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2471. if (w > max_width || h > max_height) {
  2472. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2473. w, h, max_width, max_height);
  2474. return -EINVAL;
  2475. }
  2476. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2477. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2478. alignment = intel_surf_alignment(fb, 0);
  2479. /*
  2480. * AUX surface offset is specified as the distance from the
  2481. * main surface offset, and it must be non-negative. Make
  2482. * sure that is what we will get.
  2483. */
  2484. if (offset > aux_offset)
  2485. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2486. offset, aux_offset & ~(alignment - 1));
  2487. /*
  2488. * When using an X-tiled surface, the plane blows up
  2489. * if the x offset + width exceed the stride.
  2490. *
  2491. * TODO: linear and Y-tiled seem fine, Yf untested,
  2492. */
  2493. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2494. int cpp = fb->format->cpp[0];
  2495. while ((x + w) * cpp > fb->pitches[0]) {
  2496. if (offset == 0) {
  2497. DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
  2498. return -EINVAL;
  2499. }
  2500. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2501. offset, offset - alignment);
  2502. }
  2503. }
  2504. plane_state->main.offset = offset;
  2505. plane_state->main.x = x;
  2506. plane_state->main.y = y;
  2507. return 0;
  2508. }
  2509. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2510. {
  2511. const struct drm_framebuffer *fb = plane_state->base.fb;
  2512. unsigned int rotation = plane_state->base.rotation;
  2513. int max_width = skl_max_plane_width(fb, 1, rotation);
  2514. int max_height = 4096;
  2515. int x = plane_state->base.src.x1 >> 17;
  2516. int y = plane_state->base.src.y1 >> 17;
  2517. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2518. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2519. u32 offset;
  2520. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2521. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2522. /* FIXME not quite sure how/if these apply to the chroma plane */
  2523. if (w > max_width || h > max_height) {
  2524. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2525. w, h, max_width, max_height);
  2526. return -EINVAL;
  2527. }
  2528. plane_state->aux.offset = offset;
  2529. plane_state->aux.x = x;
  2530. plane_state->aux.y = y;
  2531. return 0;
  2532. }
  2533. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2534. {
  2535. const struct drm_framebuffer *fb = plane_state->base.fb;
  2536. unsigned int rotation = plane_state->base.rotation;
  2537. int ret;
  2538. if (!plane_state->base.visible)
  2539. return 0;
  2540. /* Rotate src coordinates to match rotated GTT view */
  2541. if (drm_rotation_90_or_270(rotation))
  2542. drm_rect_rotate(&plane_state->base.src,
  2543. fb->width << 16, fb->height << 16,
  2544. DRM_MODE_ROTATE_270);
  2545. /*
  2546. * Handle the AUX surface first since
  2547. * the main surface setup depends on it.
  2548. */
  2549. if (fb->format->format == DRM_FORMAT_NV12) {
  2550. ret = skl_check_nv12_aux_surface(plane_state);
  2551. if (ret)
  2552. return ret;
  2553. } else {
  2554. plane_state->aux.offset = ~0xfff;
  2555. plane_state->aux.x = 0;
  2556. plane_state->aux.y = 0;
  2557. }
  2558. ret = skl_check_main_surface(plane_state);
  2559. if (ret)
  2560. return ret;
  2561. return 0;
  2562. }
  2563. static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2564. const struct intel_plane_state *plane_state)
  2565. {
  2566. struct drm_i915_private *dev_priv =
  2567. to_i915(plane_state->base.plane->dev);
  2568. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2569. const struct drm_framebuffer *fb = plane_state->base.fb;
  2570. unsigned int rotation = plane_state->base.rotation;
  2571. u32 dspcntr;
  2572. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2573. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2574. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2575. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2576. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2577. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2578. if (INTEL_GEN(dev_priv) < 4)
  2579. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2580. switch (fb->format->format) {
  2581. case DRM_FORMAT_C8:
  2582. dspcntr |= DISPPLANE_8BPP;
  2583. break;
  2584. case DRM_FORMAT_XRGB1555:
  2585. dspcntr |= DISPPLANE_BGRX555;
  2586. break;
  2587. case DRM_FORMAT_RGB565:
  2588. dspcntr |= DISPPLANE_BGRX565;
  2589. break;
  2590. case DRM_FORMAT_XRGB8888:
  2591. dspcntr |= DISPPLANE_BGRX888;
  2592. break;
  2593. case DRM_FORMAT_XBGR8888:
  2594. dspcntr |= DISPPLANE_RGBX888;
  2595. break;
  2596. case DRM_FORMAT_XRGB2101010:
  2597. dspcntr |= DISPPLANE_BGRX101010;
  2598. break;
  2599. case DRM_FORMAT_XBGR2101010:
  2600. dspcntr |= DISPPLANE_RGBX101010;
  2601. break;
  2602. default:
  2603. MISSING_CASE(fb->format->format);
  2604. return 0;
  2605. }
  2606. if (INTEL_GEN(dev_priv) >= 4 &&
  2607. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2608. dspcntr |= DISPPLANE_TILED;
  2609. if (rotation & DRM_MODE_ROTATE_180)
  2610. dspcntr |= DISPPLANE_ROTATE_180;
  2611. if (rotation & DRM_MODE_REFLECT_X)
  2612. dspcntr |= DISPPLANE_MIRROR;
  2613. return dspcntr;
  2614. }
  2615. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2616. {
  2617. struct drm_i915_private *dev_priv =
  2618. to_i915(plane_state->base.plane->dev);
  2619. int src_x = plane_state->base.src.x1 >> 16;
  2620. int src_y = plane_state->base.src.y1 >> 16;
  2621. u32 offset;
  2622. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2623. if (INTEL_GEN(dev_priv) >= 4)
  2624. offset = intel_compute_tile_offset(&src_x, &src_y,
  2625. plane_state, 0);
  2626. else
  2627. offset = 0;
  2628. /* HSW/BDW do this automagically in hardware */
  2629. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2630. unsigned int rotation = plane_state->base.rotation;
  2631. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2632. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2633. if (rotation & DRM_MODE_ROTATE_180) {
  2634. src_x += src_w - 1;
  2635. src_y += src_h - 1;
  2636. } else if (rotation & DRM_MODE_REFLECT_X) {
  2637. src_x += src_w - 1;
  2638. }
  2639. }
  2640. plane_state->main.offset = offset;
  2641. plane_state->main.x = src_x;
  2642. plane_state->main.y = src_y;
  2643. return 0;
  2644. }
  2645. static void i9xx_update_primary_plane(struct intel_plane *primary,
  2646. const struct intel_crtc_state *crtc_state,
  2647. const struct intel_plane_state *plane_state)
  2648. {
  2649. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2650. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2651. const struct drm_framebuffer *fb = plane_state->base.fb;
  2652. enum plane plane = primary->plane;
  2653. u32 linear_offset;
  2654. u32 dspcntr = plane_state->ctl;
  2655. i915_reg_t reg = DSPCNTR(plane);
  2656. int x = plane_state->main.x;
  2657. int y = plane_state->main.y;
  2658. unsigned long irqflags;
  2659. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2660. if (INTEL_GEN(dev_priv) >= 4)
  2661. crtc->dspaddr_offset = plane_state->main.offset;
  2662. else
  2663. crtc->dspaddr_offset = linear_offset;
  2664. crtc->adjusted_x = x;
  2665. crtc->adjusted_y = y;
  2666. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2667. if (INTEL_GEN(dev_priv) < 4) {
  2668. /* pipesrc and dspsize control the size that is scaled from,
  2669. * which should always be the user's requested size.
  2670. */
  2671. I915_WRITE_FW(DSPSIZE(plane),
  2672. ((crtc_state->pipe_src_h - 1) << 16) |
  2673. (crtc_state->pipe_src_w - 1));
  2674. I915_WRITE_FW(DSPPOS(plane), 0);
  2675. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2676. I915_WRITE_FW(PRIMSIZE(plane),
  2677. ((crtc_state->pipe_src_h - 1) << 16) |
  2678. (crtc_state->pipe_src_w - 1));
  2679. I915_WRITE_FW(PRIMPOS(plane), 0);
  2680. I915_WRITE_FW(PRIMCNSTALPHA(plane), 0);
  2681. }
  2682. I915_WRITE_FW(reg, dspcntr);
  2683. I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
  2684. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2685. I915_WRITE_FW(DSPSURF(plane),
  2686. intel_plane_ggtt_offset(plane_state) +
  2687. crtc->dspaddr_offset);
  2688. I915_WRITE_FW(DSPOFFSET(plane), (y << 16) | x);
  2689. } else if (INTEL_GEN(dev_priv) >= 4) {
  2690. I915_WRITE_FW(DSPSURF(plane),
  2691. intel_plane_ggtt_offset(plane_state) +
  2692. crtc->dspaddr_offset);
  2693. I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
  2694. I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
  2695. } else {
  2696. I915_WRITE_FW(DSPADDR(plane),
  2697. intel_plane_ggtt_offset(plane_state) +
  2698. crtc->dspaddr_offset);
  2699. }
  2700. POSTING_READ_FW(reg);
  2701. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2702. }
  2703. static void i9xx_disable_primary_plane(struct intel_plane *primary,
  2704. struct intel_crtc *crtc)
  2705. {
  2706. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2707. enum plane plane = primary->plane;
  2708. unsigned long irqflags;
  2709. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2710. I915_WRITE_FW(DSPCNTR(plane), 0);
  2711. if (INTEL_INFO(dev_priv)->gen >= 4)
  2712. I915_WRITE_FW(DSPSURF(plane), 0);
  2713. else
  2714. I915_WRITE_FW(DSPADDR(plane), 0);
  2715. POSTING_READ_FW(DSPCNTR(plane));
  2716. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2717. }
  2718. static u32
  2719. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2720. {
  2721. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2722. return 64;
  2723. else
  2724. return intel_tile_width_bytes(fb, plane);
  2725. }
  2726. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2727. {
  2728. struct drm_device *dev = intel_crtc->base.dev;
  2729. struct drm_i915_private *dev_priv = to_i915(dev);
  2730. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2731. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2732. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2733. }
  2734. /*
  2735. * This function detaches (aka. unbinds) unused scalers in hardware
  2736. */
  2737. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2738. {
  2739. struct intel_crtc_scaler_state *scaler_state;
  2740. int i;
  2741. scaler_state = &intel_crtc->config->scaler_state;
  2742. /* loop through and disable scalers that aren't in use */
  2743. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2744. if (!scaler_state->scalers[i].in_use)
  2745. skl_detach_scaler(intel_crtc, i);
  2746. }
  2747. }
  2748. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2749. unsigned int rotation)
  2750. {
  2751. u32 stride;
  2752. if (plane >= fb->format->num_planes)
  2753. return 0;
  2754. stride = intel_fb_pitch(fb, plane, rotation);
  2755. /*
  2756. * The stride is either expressed as a multiple of 64 bytes chunks for
  2757. * linear buffers or in number of tiles for tiled buffers.
  2758. */
  2759. if (drm_rotation_90_or_270(rotation))
  2760. stride /= intel_tile_height(fb, plane);
  2761. else
  2762. stride /= intel_fb_stride_alignment(fb, plane);
  2763. return stride;
  2764. }
  2765. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  2766. {
  2767. switch (pixel_format) {
  2768. case DRM_FORMAT_C8:
  2769. return PLANE_CTL_FORMAT_INDEXED;
  2770. case DRM_FORMAT_RGB565:
  2771. return PLANE_CTL_FORMAT_RGB_565;
  2772. case DRM_FORMAT_XBGR8888:
  2773. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2774. case DRM_FORMAT_XRGB8888:
  2775. return PLANE_CTL_FORMAT_XRGB_8888;
  2776. /*
  2777. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2778. * to be already pre-multiplied. We need to add a knob (or a different
  2779. * DRM_FORMAT) for user-space to configure that.
  2780. */
  2781. case DRM_FORMAT_ABGR8888:
  2782. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2783. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2784. case DRM_FORMAT_ARGB8888:
  2785. return PLANE_CTL_FORMAT_XRGB_8888 |
  2786. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2787. case DRM_FORMAT_XRGB2101010:
  2788. return PLANE_CTL_FORMAT_XRGB_2101010;
  2789. case DRM_FORMAT_XBGR2101010:
  2790. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2791. case DRM_FORMAT_YUYV:
  2792. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2793. case DRM_FORMAT_YVYU:
  2794. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2795. case DRM_FORMAT_UYVY:
  2796. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2797. case DRM_FORMAT_VYUY:
  2798. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2799. default:
  2800. MISSING_CASE(pixel_format);
  2801. }
  2802. return 0;
  2803. }
  2804. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2805. {
  2806. switch (fb_modifier) {
  2807. case DRM_FORMAT_MOD_LINEAR:
  2808. break;
  2809. case I915_FORMAT_MOD_X_TILED:
  2810. return PLANE_CTL_TILED_X;
  2811. case I915_FORMAT_MOD_Y_TILED:
  2812. return PLANE_CTL_TILED_Y;
  2813. case I915_FORMAT_MOD_Yf_TILED:
  2814. return PLANE_CTL_TILED_YF;
  2815. default:
  2816. MISSING_CASE(fb_modifier);
  2817. }
  2818. return 0;
  2819. }
  2820. static u32 skl_plane_ctl_rotation(unsigned int rotation)
  2821. {
  2822. switch (rotation) {
  2823. case DRM_MODE_ROTATE_0:
  2824. break;
  2825. /*
  2826. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2827. * while i915 HW rotation is clockwise, thats why this swapping.
  2828. */
  2829. case DRM_MODE_ROTATE_90:
  2830. return PLANE_CTL_ROTATE_270;
  2831. case DRM_MODE_ROTATE_180:
  2832. return PLANE_CTL_ROTATE_180;
  2833. case DRM_MODE_ROTATE_270:
  2834. return PLANE_CTL_ROTATE_90;
  2835. default:
  2836. MISSING_CASE(rotation);
  2837. }
  2838. return 0;
  2839. }
  2840. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  2841. const struct intel_plane_state *plane_state)
  2842. {
  2843. struct drm_i915_private *dev_priv =
  2844. to_i915(plane_state->base.plane->dev);
  2845. const struct drm_framebuffer *fb = plane_state->base.fb;
  2846. unsigned int rotation = plane_state->base.rotation;
  2847. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  2848. u32 plane_ctl;
  2849. plane_ctl = PLANE_CTL_ENABLE;
  2850. if (!IS_GEMINILAKE(dev_priv)) {
  2851. plane_ctl |=
  2852. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2853. PLANE_CTL_PIPE_CSC_ENABLE |
  2854. PLANE_CTL_PLANE_GAMMA_DISABLE;
  2855. }
  2856. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  2857. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  2858. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2859. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  2860. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  2861. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  2862. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  2863. return plane_ctl;
  2864. }
  2865. static void skylake_update_primary_plane(struct intel_plane *plane,
  2866. const struct intel_crtc_state *crtc_state,
  2867. const struct intel_plane_state *plane_state)
  2868. {
  2869. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2870. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2871. const struct drm_framebuffer *fb = plane_state->base.fb;
  2872. enum plane_id plane_id = plane->id;
  2873. enum pipe pipe = plane->pipe;
  2874. u32 plane_ctl = plane_state->ctl;
  2875. unsigned int rotation = plane_state->base.rotation;
  2876. u32 stride = skl_plane_stride(fb, 0, rotation);
  2877. u32 surf_addr = plane_state->main.offset;
  2878. int scaler_id = plane_state->scaler_id;
  2879. int src_x = plane_state->main.x;
  2880. int src_y = plane_state->main.y;
  2881. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2882. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2883. int dst_x = plane_state->base.dst.x1;
  2884. int dst_y = plane_state->base.dst.y1;
  2885. int dst_w = drm_rect_width(&plane_state->base.dst);
  2886. int dst_h = drm_rect_height(&plane_state->base.dst);
  2887. unsigned long irqflags;
  2888. /* Sizes are 0 based */
  2889. src_w--;
  2890. src_h--;
  2891. dst_w--;
  2892. dst_h--;
  2893. crtc->dspaddr_offset = surf_addr;
  2894. crtc->adjusted_x = src_x;
  2895. crtc->adjusted_y = src_y;
  2896. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2897. if (IS_GEMINILAKE(dev_priv)) {
  2898. I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
  2899. PLANE_COLOR_PIPE_GAMMA_ENABLE |
  2900. PLANE_COLOR_PIPE_CSC_ENABLE |
  2901. PLANE_COLOR_PLANE_GAMMA_DISABLE);
  2902. }
  2903. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
  2904. I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
  2905. I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
  2906. I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  2907. if (scaler_id >= 0) {
  2908. uint32_t ps_ctrl = 0;
  2909. WARN_ON(!dst_w || !dst_h);
  2910. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
  2911. crtc_state->scaler_state.scalers[scaler_id].mode;
  2912. I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2913. I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2914. I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2915. I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2916. I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
  2917. } else {
  2918. I915_WRITE_FW(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
  2919. }
  2920. I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
  2921. intel_plane_ggtt_offset(plane_state) + surf_addr);
  2922. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  2923. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2924. }
  2925. static void skylake_disable_primary_plane(struct intel_plane *primary,
  2926. struct intel_crtc *crtc)
  2927. {
  2928. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2929. enum plane_id plane_id = primary->id;
  2930. enum pipe pipe = primary->pipe;
  2931. unsigned long irqflags;
  2932. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2933. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
  2934. I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
  2935. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  2936. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2937. }
  2938. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2939. {
  2940. struct intel_crtc *crtc;
  2941. for_each_intel_crtc(&dev_priv->drm, crtc)
  2942. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2943. }
  2944. static void intel_update_primary_planes(struct drm_device *dev)
  2945. {
  2946. struct drm_crtc *crtc;
  2947. for_each_crtc(dev, crtc) {
  2948. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2949. struct intel_plane_state *plane_state =
  2950. to_intel_plane_state(plane->base.state);
  2951. if (plane_state->base.visible) {
  2952. trace_intel_update_plane(&plane->base,
  2953. to_intel_crtc(crtc));
  2954. plane->update_plane(plane,
  2955. to_intel_crtc_state(crtc->state),
  2956. plane_state);
  2957. }
  2958. }
  2959. }
  2960. static int
  2961. __intel_display_resume(struct drm_device *dev,
  2962. struct drm_atomic_state *state,
  2963. struct drm_modeset_acquire_ctx *ctx)
  2964. {
  2965. struct drm_crtc_state *crtc_state;
  2966. struct drm_crtc *crtc;
  2967. int i, ret;
  2968. intel_modeset_setup_hw_state(dev);
  2969. i915_redisable_vga(to_i915(dev));
  2970. if (!state)
  2971. return 0;
  2972. /*
  2973. * We've duplicated the state, pointers to the old state are invalid.
  2974. *
  2975. * Don't attempt to use the old state until we commit the duplicated state.
  2976. */
  2977. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  2978. /*
  2979. * Force recalculation even if we restore
  2980. * current state. With fast modeset this may not result
  2981. * in a modeset when the state is compatible.
  2982. */
  2983. crtc_state->mode_changed = true;
  2984. }
  2985. /* ignore any reset values/BIOS leftovers in the WM registers */
  2986. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  2987. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  2988. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  2989. WARN_ON(ret == -EDEADLK);
  2990. return ret;
  2991. }
  2992. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  2993. {
  2994. return intel_has_gpu_reset(dev_priv) &&
  2995. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  2996. }
  2997. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  2998. {
  2999. struct drm_device *dev = &dev_priv->drm;
  3000. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3001. struct drm_atomic_state *state;
  3002. int ret;
  3003. /*
  3004. * Need mode_config.mutex so that we don't
  3005. * trample ongoing ->detect() and whatnot.
  3006. */
  3007. mutex_lock(&dev->mode_config.mutex);
  3008. drm_modeset_acquire_init(ctx, 0);
  3009. while (1) {
  3010. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3011. if (ret != -EDEADLK)
  3012. break;
  3013. drm_modeset_backoff(ctx);
  3014. }
  3015. /* reset doesn't touch the display, but flips might get nuked anyway, */
  3016. if (!i915.force_reset_modeset_test &&
  3017. !gpu_reset_clobbers_display(dev_priv))
  3018. return;
  3019. /*
  3020. * Disabling the crtcs gracefully seems nicer. Also the
  3021. * g33 docs say we should at least disable all the planes.
  3022. */
  3023. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3024. if (IS_ERR(state)) {
  3025. ret = PTR_ERR(state);
  3026. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3027. return;
  3028. }
  3029. ret = drm_atomic_helper_disable_all(dev, ctx);
  3030. if (ret) {
  3031. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3032. drm_atomic_state_put(state);
  3033. return;
  3034. }
  3035. dev_priv->modeset_restore_state = state;
  3036. state->acquire_ctx = ctx;
  3037. }
  3038. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3039. {
  3040. struct drm_device *dev = &dev_priv->drm;
  3041. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3042. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3043. int ret;
  3044. /*
  3045. * Flips in the rings will be nuked by the reset,
  3046. * so complete all pending flips so that user space
  3047. * will get its events and not get stuck.
  3048. */
  3049. intel_complete_page_flips(dev_priv);
  3050. dev_priv->modeset_restore_state = NULL;
  3051. /* reset doesn't touch the display */
  3052. if (!gpu_reset_clobbers_display(dev_priv)) {
  3053. if (!state) {
  3054. /*
  3055. * Flips in the rings have been nuked by the reset,
  3056. * so update the base address of all primary
  3057. * planes to the the last fb to make sure we're
  3058. * showing the correct fb after a reset.
  3059. *
  3060. * FIXME: Atomic will make this obsolete since we won't schedule
  3061. * CS-based flips (which might get lost in gpu resets) any more.
  3062. */
  3063. intel_update_primary_planes(dev);
  3064. } else {
  3065. ret = __intel_display_resume(dev, state, ctx);
  3066. if (ret)
  3067. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3068. }
  3069. } else {
  3070. /*
  3071. * The display has been reset as well,
  3072. * so need a full re-initialization.
  3073. */
  3074. intel_runtime_pm_disable_interrupts(dev_priv);
  3075. intel_runtime_pm_enable_interrupts(dev_priv);
  3076. intel_pps_unlock_regs_wa(dev_priv);
  3077. intel_modeset_init_hw(dev);
  3078. spin_lock_irq(&dev_priv->irq_lock);
  3079. if (dev_priv->display.hpd_irq_setup)
  3080. dev_priv->display.hpd_irq_setup(dev_priv);
  3081. spin_unlock_irq(&dev_priv->irq_lock);
  3082. ret = __intel_display_resume(dev, state, ctx);
  3083. if (ret)
  3084. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3085. intel_hpd_init(dev_priv);
  3086. }
  3087. if (state)
  3088. drm_atomic_state_put(state);
  3089. drm_modeset_drop_locks(ctx);
  3090. drm_modeset_acquire_fini(ctx);
  3091. mutex_unlock(&dev->mode_config.mutex);
  3092. }
  3093. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3094. {
  3095. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3096. if (i915_reset_backoff(error))
  3097. return true;
  3098. if (crtc->reset_count != i915_reset_count(error))
  3099. return true;
  3100. return false;
  3101. }
  3102. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3103. {
  3104. struct drm_device *dev = crtc->dev;
  3105. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3106. bool pending;
  3107. if (abort_flip_on_reset(intel_crtc))
  3108. return false;
  3109. spin_lock_irq(&dev->event_lock);
  3110. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3111. spin_unlock_irq(&dev->event_lock);
  3112. return pending;
  3113. }
  3114. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3115. struct intel_crtc_state *old_crtc_state)
  3116. {
  3117. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3118. struct intel_crtc_state *pipe_config =
  3119. to_intel_crtc_state(crtc->base.state);
  3120. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3121. crtc->base.mode = crtc->base.state->mode;
  3122. /*
  3123. * Update pipe size and adjust fitter if needed: the reason for this is
  3124. * that in compute_mode_changes we check the native mode (not the pfit
  3125. * mode) to see if we can flip rather than do a full mode set. In the
  3126. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3127. * pfit state, we'll end up with a big fb scanned out into the wrong
  3128. * sized surface.
  3129. */
  3130. I915_WRITE(PIPESRC(crtc->pipe),
  3131. ((pipe_config->pipe_src_w - 1) << 16) |
  3132. (pipe_config->pipe_src_h - 1));
  3133. /* on skylake this is done by detaching scalers */
  3134. if (INTEL_GEN(dev_priv) >= 9) {
  3135. skl_detach_scalers(crtc);
  3136. if (pipe_config->pch_pfit.enabled)
  3137. skylake_pfit_enable(crtc);
  3138. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3139. if (pipe_config->pch_pfit.enabled)
  3140. ironlake_pfit_enable(crtc);
  3141. else if (old_crtc_state->pch_pfit.enabled)
  3142. ironlake_pfit_disable(crtc, true);
  3143. }
  3144. }
  3145. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3146. {
  3147. struct drm_device *dev = crtc->base.dev;
  3148. struct drm_i915_private *dev_priv = to_i915(dev);
  3149. int pipe = crtc->pipe;
  3150. i915_reg_t reg;
  3151. u32 temp;
  3152. /* enable normal train */
  3153. reg = FDI_TX_CTL(pipe);
  3154. temp = I915_READ(reg);
  3155. if (IS_IVYBRIDGE(dev_priv)) {
  3156. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3157. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3158. } else {
  3159. temp &= ~FDI_LINK_TRAIN_NONE;
  3160. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3161. }
  3162. I915_WRITE(reg, temp);
  3163. reg = FDI_RX_CTL(pipe);
  3164. temp = I915_READ(reg);
  3165. if (HAS_PCH_CPT(dev_priv)) {
  3166. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3167. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3168. } else {
  3169. temp &= ~FDI_LINK_TRAIN_NONE;
  3170. temp |= FDI_LINK_TRAIN_NONE;
  3171. }
  3172. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3173. /* wait one idle pattern time */
  3174. POSTING_READ(reg);
  3175. udelay(1000);
  3176. /* IVB wants error correction enabled */
  3177. if (IS_IVYBRIDGE(dev_priv))
  3178. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3179. FDI_FE_ERRC_ENABLE);
  3180. }
  3181. /* The FDI link training functions for ILK/Ibexpeak. */
  3182. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3183. const struct intel_crtc_state *crtc_state)
  3184. {
  3185. struct drm_device *dev = crtc->base.dev;
  3186. struct drm_i915_private *dev_priv = to_i915(dev);
  3187. int pipe = crtc->pipe;
  3188. i915_reg_t reg;
  3189. u32 temp, tries;
  3190. /* FDI needs bits from pipe first */
  3191. assert_pipe_enabled(dev_priv, pipe);
  3192. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3193. for train result */
  3194. reg = FDI_RX_IMR(pipe);
  3195. temp = I915_READ(reg);
  3196. temp &= ~FDI_RX_SYMBOL_LOCK;
  3197. temp &= ~FDI_RX_BIT_LOCK;
  3198. I915_WRITE(reg, temp);
  3199. I915_READ(reg);
  3200. udelay(150);
  3201. /* enable CPU FDI TX and PCH FDI RX */
  3202. reg = FDI_TX_CTL(pipe);
  3203. temp = I915_READ(reg);
  3204. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3205. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3206. temp &= ~FDI_LINK_TRAIN_NONE;
  3207. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3208. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3209. reg = FDI_RX_CTL(pipe);
  3210. temp = I915_READ(reg);
  3211. temp &= ~FDI_LINK_TRAIN_NONE;
  3212. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3213. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3214. POSTING_READ(reg);
  3215. udelay(150);
  3216. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3217. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3218. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3219. FDI_RX_PHASE_SYNC_POINTER_EN);
  3220. reg = FDI_RX_IIR(pipe);
  3221. for (tries = 0; tries < 5; tries++) {
  3222. temp = I915_READ(reg);
  3223. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3224. if ((temp & FDI_RX_BIT_LOCK)) {
  3225. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3226. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3227. break;
  3228. }
  3229. }
  3230. if (tries == 5)
  3231. DRM_ERROR("FDI train 1 fail!\n");
  3232. /* Train 2 */
  3233. reg = FDI_TX_CTL(pipe);
  3234. temp = I915_READ(reg);
  3235. temp &= ~FDI_LINK_TRAIN_NONE;
  3236. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3237. I915_WRITE(reg, temp);
  3238. reg = FDI_RX_CTL(pipe);
  3239. temp = I915_READ(reg);
  3240. temp &= ~FDI_LINK_TRAIN_NONE;
  3241. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3242. I915_WRITE(reg, temp);
  3243. POSTING_READ(reg);
  3244. udelay(150);
  3245. reg = FDI_RX_IIR(pipe);
  3246. for (tries = 0; tries < 5; tries++) {
  3247. temp = I915_READ(reg);
  3248. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3249. if (temp & FDI_RX_SYMBOL_LOCK) {
  3250. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3251. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3252. break;
  3253. }
  3254. }
  3255. if (tries == 5)
  3256. DRM_ERROR("FDI train 2 fail!\n");
  3257. DRM_DEBUG_KMS("FDI train done\n");
  3258. }
  3259. static const int snb_b_fdi_train_param[] = {
  3260. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3261. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3262. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3263. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3264. };
  3265. /* The FDI link training functions for SNB/Cougarpoint. */
  3266. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3267. const struct intel_crtc_state *crtc_state)
  3268. {
  3269. struct drm_device *dev = crtc->base.dev;
  3270. struct drm_i915_private *dev_priv = to_i915(dev);
  3271. int pipe = crtc->pipe;
  3272. i915_reg_t reg;
  3273. u32 temp, i, retry;
  3274. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3275. for train result */
  3276. reg = FDI_RX_IMR(pipe);
  3277. temp = I915_READ(reg);
  3278. temp &= ~FDI_RX_SYMBOL_LOCK;
  3279. temp &= ~FDI_RX_BIT_LOCK;
  3280. I915_WRITE(reg, temp);
  3281. POSTING_READ(reg);
  3282. udelay(150);
  3283. /* enable CPU FDI TX and PCH FDI RX */
  3284. reg = FDI_TX_CTL(pipe);
  3285. temp = I915_READ(reg);
  3286. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3287. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3288. temp &= ~FDI_LINK_TRAIN_NONE;
  3289. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3290. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3291. /* SNB-B */
  3292. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3293. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3294. I915_WRITE(FDI_RX_MISC(pipe),
  3295. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3296. reg = FDI_RX_CTL(pipe);
  3297. temp = I915_READ(reg);
  3298. if (HAS_PCH_CPT(dev_priv)) {
  3299. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3300. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3301. } else {
  3302. temp &= ~FDI_LINK_TRAIN_NONE;
  3303. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3304. }
  3305. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3306. POSTING_READ(reg);
  3307. udelay(150);
  3308. for (i = 0; i < 4; i++) {
  3309. reg = FDI_TX_CTL(pipe);
  3310. temp = I915_READ(reg);
  3311. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3312. temp |= snb_b_fdi_train_param[i];
  3313. I915_WRITE(reg, temp);
  3314. POSTING_READ(reg);
  3315. udelay(500);
  3316. for (retry = 0; retry < 5; retry++) {
  3317. reg = FDI_RX_IIR(pipe);
  3318. temp = I915_READ(reg);
  3319. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3320. if (temp & FDI_RX_BIT_LOCK) {
  3321. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3322. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3323. break;
  3324. }
  3325. udelay(50);
  3326. }
  3327. if (retry < 5)
  3328. break;
  3329. }
  3330. if (i == 4)
  3331. DRM_ERROR("FDI train 1 fail!\n");
  3332. /* Train 2 */
  3333. reg = FDI_TX_CTL(pipe);
  3334. temp = I915_READ(reg);
  3335. temp &= ~FDI_LINK_TRAIN_NONE;
  3336. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3337. if (IS_GEN6(dev_priv)) {
  3338. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3339. /* SNB-B */
  3340. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3341. }
  3342. I915_WRITE(reg, temp);
  3343. reg = FDI_RX_CTL(pipe);
  3344. temp = I915_READ(reg);
  3345. if (HAS_PCH_CPT(dev_priv)) {
  3346. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3347. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3348. } else {
  3349. temp &= ~FDI_LINK_TRAIN_NONE;
  3350. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3351. }
  3352. I915_WRITE(reg, temp);
  3353. POSTING_READ(reg);
  3354. udelay(150);
  3355. for (i = 0; i < 4; i++) {
  3356. reg = FDI_TX_CTL(pipe);
  3357. temp = I915_READ(reg);
  3358. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3359. temp |= snb_b_fdi_train_param[i];
  3360. I915_WRITE(reg, temp);
  3361. POSTING_READ(reg);
  3362. udelay(500);
  3363. for (retry = 0; retry < 5; retry++) {
  3364. reg = FDI_RX_IIR(pipe);
  3365. temp = I915_READ(reg);
  3366. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3367. if (temp & FDI_RX_SYMBOL_LOCK) {
  3368. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3369. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3370. break;
  3371. }
  3372. udelay(50);
  3373. }
  3374. if (retry < 5)
  3375. break;
  3376. }
  3377. if (i == 4)
  3378. DRM_ERROR("FDI train 2 fail!\n");
  3379. DRM_DEBUG_KMS("FDI train done.\n");
  3380. }
  3381. /* Manual link training for Ivy Bridge A0 parts */
  3382. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3383. const struct intel_crtc_state *crtc_state)
  3384. {
  3385. struct drm_device *dev = crtc->base.dev;
  3386. struct drm_i915_private *dev_priv = to_i915(dev);
  3387. int pipe = crtc->pipe;
  3388. i915_reg_t reg;
  3389. u32 temp, i, j;
  3390. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3391. for train result */
  3392. reg = FDI_RX_IMR(pipe);
  3393. temp = I915_READ(reg);
  3394. temp &= ~FDI_RX_SYMBOL_LOCK;
  3395. temp &= ~FDI_RX_BIT_LOCK;
  3396. I915_WRITE(reg, temp);
  3397. POSTING_READ(reg);
  3398. udelay(150);
  3399. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3400. I915_READ(FDI_RX_IIR(pipe)));
  3401. /* Try each vswing and preemphasis setting twice before moving on */
  3402. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3403. /* disable first in case we need to retry */
  3404. reg = FDI_TX_CTL(pipe);
  3405. temp = I915_READ(reg);
  3406. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3407. temp &= ~FDI_TX_ENABLE;
  3408. I915_WRITE(reg, temp);
  3409. reg = FDI_RX_CTL(pipe);
  3410. temp = I915_READ(reg);
  3411. temp &= ~FDI_LINK_TRAIN_AUTO;
  3412. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3413. temp &= ~FDI_RX_ENABLE;
  3414. I915_WRITE(reg, temp);
  3415. /* enable CPU FDI TX and PCH FDI RX */
  3416. reg = FDI_TX_CTL(pipe);
  3417. temp = I915_READ(reg);
  3418. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3419. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3420. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3421. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3422. temp |= snb_b_fdi_train_param[j/2];
  3423. temp |= FDI_COMPOSITE_SYNC;
  3424. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3425. I915_WRITE(FDI_RX_MISC(pipe),
  3426. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3427. reg = FDI_RX_CTL(pipe);
  3428. temp = I915_READ(reg);
  3429. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3430. temp |= FDI_COMPOSITE_SYNC;
  3431. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3432. POSTING_READ(reg);
  3433. udelay(1); /* should be 0.5us */
  3434. for (i = 0; i < 4; i++) {
  3435. reg = FDI_RX_IIR(pipe);
  3436. temp = I915_READ(reg);
  3437. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3438. if (temp & FDI_RX_BIT_LOCK ||
  3439. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3440. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3441. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3442. i);
  3443. break;
  3444. }
  3445. udelay(1); /* should be 0.5us */
  3446. }
  3447. if (i == 4) {
  3448. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3449. continue;
  3450. }
  3451. /* Train 2 */
  3452. reg = FDI_TX_CTL(pipe);
  3453. temp = I915_READ(reg);
  3454. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3455. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3456. I915_WRITE(reg, temp);
  3457. reg = FDI_RX_CTL(pipe);
  3458. temp = I915_READ(reg);
  3459. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3460. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3461. I915_WRITE(reg, temp);
  3462. POSTING_READ(reg);
  3463. udelay(2); /* should be 1.5us */
  3464. for (i = 0; i < 4; i++) {
  3465. reg = FDI_RX_IIR(pipe);
  3466. temp = I915_READ(reg);
  3467. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3468. if (temp & FDI_RX_SYMBOL_LOCK ||
  3469. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3470. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3471. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3472. i);
  3473. goto train_done;
  3474. }
  3475. udelay(2); /* should be 1.5us */
  3476. }
  3477. if (i == 4)
  3478. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3479. }
  3480. train_done:
  3481. DRM_DEBUG_KMS("FDI train done.\n");
  3482. }
  3483. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3484. {
  3485. struct drm_device *dev = intel_crtc->base.dev;
  3486. struct drm_i915_private *dev_priv = to_i915(dev);
  3487. int pipe = intel_crtc->pipe;
  3488. i915_reg_t reg;
  3489. u32 temp;
  3490. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3491. reg = FDI_RX_CTL(pipe);
  3492. temp = I915_READ(reg);
  3493. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3494. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3495. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3496. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3497. POSTING_READ(reg);
  3498. udelay(200);
  3499. /* Switch from Rawclk to PCDclk */
  3500. temp = I915_READ(reg);
  3501. I915_WRITE(reg, temp | FDI_PCDCLK);
  3502. POSTING_READ(reg);
  3503. udelay(200);
  3504. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3505. reg = FDI_TX_CTL(pipe);
  3506. temp = I915_READ(reg);
  3507. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3508. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3509. POSTING_READ(reg);
  3510. udelay(100);
  3511. }
  3512. }
  3513. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3514. {
  3515. struct drm_device *dev = intel_crtc->base.dev;
  3516. struct drm_i915_private *dev_priv = to_i915(dev);
  3517. int pipe = intel_crtc->pipe;
  3518. i915_reg_t reg;
  3519. u32 temp;
  3520. /* Switch from PCDclk to Rawclk */
  3521. reg = FDI_RX_CTL(pipe);
  3522. temp = I915_READ(reg);
  3523. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3524. /* Disable CPU FDI TX PLL */
  3525. reg = FDI_TX_CTL(pipe);
  3526. temp = I915_READ(reg);
  3527. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3528. POSTING_READ(reg);
  3529. udelay(100);
  3530. reg = FDI_RX_CTL(pipe);
  3531. temp = I915_READ(reg);
  3532. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3533. /* Wait for the clocks to turn off. */
  3534. POSTING_READ(reg);
  3535. udelay(100);
  3536. }
  3537. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3538. {
  3539. struct drm_device *dev = crtc->dev;
  3540. struct drm_i915_private *dev_priv = to_i915(dev);
  3541. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3542. int pipe = intel_crtc->pipe;
  3543. i915_reg_t reg;
  3544. u32 temp;
  3545. /* disable CPU FDI tx and PCH FDI rx */
  3546. reg = FDI_TX_CTL(pipe);
  3547. temp = I915_READ(reg);
  3548. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3549. POSTING_READ(reg);
  3550. reg = FDI_RX_CTL(pipe);
  3551. temp = I915_READ(reg);
  3552. temp &= ~(0x7 << 16);
  3553. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3554. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3555. POSTING_READ(reg);
  3556. udelay(100);
  3557. /* Ironlake workaround, disable clock pointer after downing FDI */
  3558. if (HAS_PCH_IBX(dev_priv))
  3559. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3560. /* still set train pattern 1 */
  3561. reg = FDI_TX_CTL(pipe);
  3562. temp = I915_READ(reg);
  3563. temp &= ~FDI_LINK_TRAIN_NONE;
  3564. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3565. I915_WRITE(reg, temp);
  3566. reg = FDI_RX_CTL(pipe);
  3567. temp = I915_READ(reg);
  3568. if (HAS_PCH_CPT(dev_priv)) {
  3569. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3570. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3571. } else {
  3572. temp &= ~FDI_LINK_TRAIN_NONE;
  3573. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3574. }
  3575. /* BPC in FDI rx is consistent with that in PIPECONF */
  3576. temp &= ~(0x07 << 16);
  3577. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3578. I915_WRITE(reg, temp);
  3579. POSTING_READ(reg);
  3580. udelay(100);
  3581. }
  3582. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3583. {
  3584. struct intel_crtc *crtc;
  3585. /* Note that we don't need to be called with mode_config.lock here
  3586. * as our list of CRTC objects is static for the lifetime of the
  3587. * device and so cannot disappear as we iterate. Similarly, we can
  3588. * happily treat the predicates as racy, atomic checks as userspace
  3589. * cannot claim and pin a new fb without at least acquring the
  3590. * struct_mutex and so serialising with us.
  3591. */
  3592. for_each_intel_crtc(&dev_priv->drm, crtc) {
  3593. if (atomic_read(&crtc->unpin_work_count) == 0)
  3594. continue;
  3595. if (crtc->flip_work)
  3596. intel_wait_for_vblank(dev_priv, crtc->pipe);
  3597. return true;
  3598. }
  3599. return false;
  3600. }
  3601. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3602. {
  3603. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3604. struct intel_flip_work *work = intel_crtc->flip_work;
  3605. intel_crtc->flip_work = NULL;
  3606. if (work->event)
  3607. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3608. drm_crtc_vblank_put(&intel_crtc->base);
  3609. wake_up_all(&dev_priv->pending_flip_queue);
  3610. trace_i915_flip_complete(intel_crtc->plane,
  3611. work->pending_flip_obj);
  3612. queue_work(dev_priv->wq, &work->unpin_work);
  3613. }
  3614. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3615. {
  3616. struct drm_device *dev = crtc->dev;
  3617. struct drm_i915_private *dev_priv = to_i915(dev);
  3618. long ret;
  3619. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3620. ret = wait_event_interruptible_timeout(
  3621. dev_priv->pending_flip_queue,
  3622. !intel_crtc_has_pending_flip(crtc),
  3623. 60*HZ);
  3624. if (ret < 0)
  3625. return ret;
  3626. if (ret == 0) {
  3627. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3628. struct intel_flip_work *work;
  3629. spin_lock_irq(&dev->event_lock);
  3630. work = intel_crtc->flip_work;
  3631. if (work && !is_mmio_work(work)) {
  3632. WARN_ONCE(1, "Removing stuck page flip\n");
  3633. page_flip_completed(intel_crtc);
  3634. }
  3635. spin_unlock_irq(&dev->event_lock);
  3636. }
  3637. return 0;
  3638. }
  3639. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3640. {
  3641. u32 temp;
  3642. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3643. mutex_lock(&dev_priv->sb_lock);
  3644. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3645. temp |= SBI_SSCCTL_DISABLE;
  3646. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3647. mutex_unlock(&dev_priv->sb_lock);
  3648. }
  3649. /* Program iCLKIP clock to the desired frequency */
  3650. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3651. {
  3652. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3653. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3654. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3655. u32 temp;
  3656. lpt_disable_iclkip(dev_priv);
  3657. /* The iCLK virtual clock root frequency is in MHz,
  3658. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3659. * divisors, it is necessary to divide one by another, so we
  3660. * convert the virtual clock precision to KHz here for higher
  3661. * precision.
  3662. */
  3663. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3664. u32 iclk_virtual_root_freq = 172800 * 1000;
  3665. u32 iclk_pi_range = 64;
  3666. u32 desired_divisor;
  3667. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3668. clock << auxdiv);
  3669. divsel = (desired_divisor / iclk_pi_range) - 2;
  3670. phaseinc = desired_divisor % iclk_pi_range;
  3671. /*
  3672. * Near 20MHz is a corner case which is
  3673. * out of range for the 7-bit divisor
  3674. */
  3675. if (divsel <= 0x7f)
  3676. break;
  3677. }
  3678. /* This should not happen with any sane values */
  3679. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3680. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3681. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3682. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3683. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3684. clock,
  3685. auxdiv,
  3686. divsel,
  3687. phasedir,
  3688. phaseinc);
  3689. mutex_lock(&dev_priv->sb_lock);
  3690. /* Program SSCDIVINTPHASE6 */
  3691. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3692. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3693. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3694. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3695. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3696. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3697. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3698. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3699. /* Program SSCAUXDIV */
  3700. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3701. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3702. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3703. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3704. /* Enable modulator and associated divider */
  3705. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3706. temp &= ~SBI_SSCCTL_DISABLE;
  3707. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3708. mutex_unlock(&dev_priv->sb_lock);
  3709. /* Wait for initialization time */
  3710. udelay(24);
  3711. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3712. }
  3713. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3714. {
  3715. u32 divsel, phaseinc, auxdiv;
  3716. u32 iclk_virtual_root_freq = 172800 * 1000;
  3717. u32 iclk_pi_range = 64;
  3718. u32 desired_divisor;
  3719. u32 temp;
  3720. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3721. return 0;
  3722. mutex_lock(&dev_priv->sb_lock);
  3723. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3724. if (temp & SBI_SSCCTL_DISABLE) {
  3725. mutex_unlock(&dev_priv->sb_lock);
  3726. return 0;
  3727. }
  3728. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3729. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3730. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3731. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3732. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3733. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3734. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3735. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3736. mutex_unlock(&dev_priv->sb_lock);
  3737. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3738. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3739. desired_divisor << auxdiv);
  3740. }
  3741. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3742. enum pipe pch_transcoder)
  3743. {
  3744. struct drm_device *dev = crtc->base.dev;
  3745. struct drm_i915_private *dev_priv = to_i915(dev);
  3746. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3747. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3748. I915_READ(HTOTAL(cpu_transcoder)));
  3749. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3750. I915_READ(HBLANK(cpu_transcoder)));
  3751. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3752. I915_READ(HSYNC(cpu_transcoder)));
  3753. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3754. I915_READ(VTOTAL(cpu_transcoder)));
  3755. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3756. I915_READ(VBLANK(cpu_transcoder)));
  3757. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3758. I915_READ(VSYNC(cpu_transcoder)));
  3759. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3760. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3761. }
  3762. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3763. {
  3764. struct drm_i915_private *dev_priv = to_i915(dev);
  3765. uint32_t temp;
  3766. temp = I915_READ(SOUTH_CHICKEN1);
  3767. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3768. return;
  3769. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3770. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3771. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3772. if (enable)
  3773. temp |= FDI_BC_BIFURCATION_SELECT;
  3774. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3775. I915_WRITE(SOUTH_CHICKEN1, temp);
  3776. POSTING_READ(SOUTH_CHICKEN1);
  3777. }
  3778. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3779. {
  3780. struct drm_device *dev = intel_crtc->base.dev;
  3781. switch (intel_crtc->pipe) {
  3782. case PIPE_A:
  3783. break;
  3784. case PIPE_B:
  3785. if (intel_crtc->config->fdi_lanes > 2)
  3786. cpt_set_fdi_bc_bifurcation(dev, false);
  3787. else
  3788. cpt_set_fdi_bc_bifurcation(dev, true);
  3789. break;
  3790. case PIPE_C:
  3791. cpt_set_fdi_bc_bifurcation(dev, true);
  3792. break;
  3793. default:
  3794. BUG();
  3795. }
  3796. }
  3797. /* Return which DP Port should be selected for Transcoder DP control */
  3798. static enum port
  3799. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3800. {
  3801. struct drm_device *dev = crtc->base.dev;
  3802. struct intel_encoder *encoder;
  3803. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3804. if (encoder->type == INTEL_OUTPUT_DP ||
  3805. encoder->type == INTEL_OUTPUT_EDP)
  3806. return enc_to_dig_port(&encoder->base)->port;
  3807. }
  3808. return -1;
  3809. }
  3810. /*
  3811. * Enable PCH resources required for PCH ports:
  3812. * - PCH PLLs
  3813. * - FDI training & RX/TX
  3814. * - update transcoder timings
  3815. * - DP transcoding bits
  3816. * - transcoder
  3817. */
  3818. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3819. {
  3820. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3821. struct drm_device *dev = crtc->base.dev;
  3822. struct drm_i915_private *dev_priv = to_i915(dev);
  3823. int pipe = crtc->pipe;
  3824. u32 temp;
  3825. assert_pch_transcoder_disabled(dev_priv, pipe);
  3826. if (IS_IVYBRIDGE(dev_priv))
  3827. ivybridge_update_fdi_bc_bifurcation(crtc);
  3828. /* Write the TU size bits before fdi link training, so that error
  3829. * detection works. */
  3830. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3831. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3832. /* For PCH output, training FDI link */
  3833. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3834. /* We need to program the right clock selection before writing the pixel
  3835. * mutliplier into the DPLL. */
  3836. if (HAS_PCH_CPT(dev_priv)) {
  3837. u32 sel;
  3838. temp = I915_READ(PCH_DPLL_SEL);
  3839. temp |= TRANS_DPLL_ENABLE(pipe);
  3840. sel = TRANS_DPLLB_SEL(pipe);
  3841. if (crtc_state->shared_dpll ==
  3842. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3843. temp |= sel;
  3844. else
  3845. temp &= ~sel;
  3846. I915_WRITE(PCH_DPLL_SEL, temp);
  3847. }
  3848. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3849. * transcoder, and we actually should do this to not upset any PCH
  3850. * transcoder that already use the clock when we share it.
  3851. *
  3852. * Note that enable_shared_dpll tries to do the right thing, but
  3853. * get_shared_dpll unconditionally resets the pll - we need that to have
  3854. * the right LVDS enable sequence. */
  3855. intel_enable_shared_dpll(crtc);
  3856. /* set transcoder timing, panel must allow it */
  3857. assert_panel_unlocked(dev_priv, pipe);
  3858. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3859. intel_fdi_normal_train(crtc);
  3860. /* For PCH DP, enable TRANS_DP_CTL */
  3861. if (HAS_PCH_CPT(dev_priv) &&
  3862. intel_crtc_has_dp_encoder(crtc_state)) {
  3863. const struct drm_display_mode *adjusted_mode =
  3864. &crtc_state->base.adjusted_mode;
  3865. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3866. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3867. temp = I915_READ(reg);
  3868. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3869. TRANS_DP_SYNC_MASK |
  3870. TRANS_DP_BPC_MASK);
  3871. temp |= TRANS_DP_OUTPUT_ENABLE;
  3872. temp |= bpc << 9; /* same format but at 11:9 */
  3873. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3874. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3875. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3876. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3877. switch (intel_trans_dp_port_sel(crtc)) {
  3878. case PORT_B:
  3879. temp |= TRANS_DP_PORT_SEL_B;
  3880. break;
  3881. case PORT_C:
  3882. temp |= TRANS_DP_PORT_SEL_C;
  3883. break;
  3884. case PORT_D:
  3885. temp |= TRANS_DP_PORT_SEL_D;
  3886. break;
  3887. default:
  3888. BUG();
  3889. }
  3890. I915_WRITE(reg, temp);
  3891. }
  3892. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3893. }
  3894. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  3895. {
  3896. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3897. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3898. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  3899. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3900. lpt_program_iclkip(crtc);
  3901. /* Set transcoder timing. */
  3902. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  3903. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3904. }
  3905. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3906. {
  3907. struct drm_i915_private *dev_priv = to_i915(dev);
  3908. i915_reg_t dslreg = PIPEDSL(pipe);
  3909. u32 temp;
  3910. temp = I915_READ(dslreg);
  3911. udelay(500);
  3912. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3913. if (wait_for(I915_READ(dslreg) != temp, 5))
  3914. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3915. }
  3916. }
  3917. static int
  3918. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3919. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3920. int src_w, int src_h, int dst_w, int dst_h)
  3921. {
  3922. struct intel_crtc_scaler_state *scaler_state =
  3923. &crtc_state->scaler_state;
  3924. struct intel_crtc *intel_crtc =
  3925. to_intel_crtc(crtc_state->base.crtc);
  3926. int need_scaling;
  3927. need_scaling = drm_rotation_90_or_270(rotation) ?
  3928. (src_h != dst_w || src_w != dst_h):
  3929. (src_w != dst_w || src_h != dst_h);
  3930. /*
  3931. * if plane is being disabled or scaler is no more required or force detach
  3932. * - free scaler binded to this plane/crtc
  3933. * - in order to do this, update crtc->scaler_usage
  3934. *
  3935. * Here scaler state in crtc_state is set free so that
  3936. * scaler can be assigned to other user. Actual register
  3937. * update to free the scaler is done in plane/panel-fit programming.
  3938. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3939. */
  3940. if (force_detach || !need_scaling) {
  3941. if (*scaler_id >= 0) {
  3942. scaler_state->scaler_users &= ~(1 << scaler_user);
  3943. scaler_state->scalers[*scaler_id].in_use = 0;
  3944. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3945. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3946. intel_crtc->pipe, scaler_user, *scaler_id,
  3947. scaler_state->scaler_users);
  3948. *scaler_id = -1;
  3949. }
  3950. return 0;
  3951. }
  3952. /* range checks */
  3953. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3954. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3955. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3956. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3957. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3958. "size is out of scaler range\n",
  3959. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3960. return -EINVAL;
  3961. }
  3962. /* mark this plane as a scaler user in crtc_state */
  3963. scaler_state->scaler_users |= (1 << scaler_user);
  3964. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3965. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3966. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3967. scaler_state->scaler_users);
  3968. return 0;
  3969. }
  3970. /**
  3971. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3972. *
  3973. * @state: crtc's scaler state
  3974. *
  3975. * Return
  3976. * 0 - scaler_usage updated successfully
  3977. * error - requested scaling cannot be supported or other error condition
  3978. */
  3979. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3980. {
  3981. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3982. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3983. &state->scaler_state.scaler_id, DRM_MODE_ROTATE_0,
  3984. state->pipe_src_w, state->pipe_src_h,
  3985. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3986. }
  3987. /**
  3988. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3989. *
  3990. * @state: crtc's scaler state
  3991. * @plane_state: atomic plane state to update
  3992. *
  3993. * Return
  3994. * 0 - scaler_usage updated successfully
  3995. * error - requested scaling cannot be supported or other error condition
  3996. */
  3997. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3998. struct intel_plane_state *plane_state)
  3999. {
  4000. struct intel_plane *intel_plane =
  4001. to_intel_plane(plane_state->base.plane);
  4002. struct drm_framebuffer *fb = plane_state->base.fb;
  4003. int ret;
  4004. bool force_detach = !fb || !plane_state->base.visible;
  4005. ret = skl_update_scaler(crtc_state, force_detach,
  4006. drm_plane_index(&intel_plane->base),
  4007. &plane_state->scaler_id,
  4008. plane_state->base.rotation,
  4009. drm_rect_width(&plane_state->base.src) >> 16,
  4010. drm_rect_height(&plane_state->base.src) >> 16,
  4011. drm_rect_width(&plane_state->base.dst),
  4012. drm_rect_height(&plane_state->base.dst));
  4013. if (ret || plane_state->scaler_id < 0)
  4014. return ret;
  4015. /* check colorkey */
  4016. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4017. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4018. intel_plane->base.base.id,
  4019. intel_plane->base.name);
  4020. return -EINVAL;
  4021. }
  4022. /* Check src format */
  4023. switch (fb->format->format) {
  4024. case DRM_FORMAT_RGB565:
  4025. case DRM_FORMAT_XBGR8888:
  4026. case DRM_FORMAT_XRGB8888:
  4027. case DRM_FORMAT_ABGR8888:
  4028. case DRM_FORMAT_ARGB8888:
  4029. case DRM_FORMAT_XRGB2101010:
  4030. case DRM_FORMAT_XBGR2101010:
  4031. case DRM_FORMAT_YUYV:
  4032. case DRM_FORMAT_YVYU:
  4033. case DRM_FORMAT_UYVY:
  4034. case DRM_FORMAT_VYUY:
  4035. break;
  4036. default:
  4037. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4038. intel_plane->base.base.id, intel_plane->base.name,
  4039. fb->base.id, fb->format->format);
  4040. return -EINVAL;
  4041. }
  4042. return 0;
  4043. }
  4044. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4045. {
  4046. int i;
  4047. for (i = 0; i < crtc->num_scalers; i++)
  4048. skl_detach_scaler(crtc, i);
  4049. }
  4050. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4051. {
  4052. struct drm_device *dev = crtc->base.dev;
  4053. struct drm_i915_private *dev_priv = to_i915(dev);
  4054. int pipe = crtc->pipe;
  4055. struct intel_crtc_scaler_state *scaler_state =
  4056. &crtc->config->scaler_state;
  4057. if (crtc->config->pch_pfit.enabled) {
  4058. int id;
  4059. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4060. return;
  4061. id = scaler_state->scaler_id;
  4062. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4063. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4064. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4065. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4066. }
  4067. }
  4068. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4069. {
  4070. struct drm_device *dev = crtc->base.dev;
  4071. struct drm_i915_private *dev_priv = to_i915(dev);
  4072. int pipe = crtc->pipe;
  4073. if (crtc->config->pch_pfit.enabled) {
  4074. /* Force use of hard-coded filter coefficients
  4075. * as some pre-programmed values are broken,
  4076. * e.g. x201.
  4077. */
  4078. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4079. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4080. PF_PIPE_SEL_IVB(pipe));
  4081. else
  4082. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4083. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4084. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4085. }
  4086. }
  4087. void hsw_enable_ips(struct intel_crtc *crtc)
  4088. {
  4089. struct drm_device *dev = crtc->base.dev;
  4090. struct drm_i915_private *dev_priv = to_i915(dev);
  4091. if (!crtc->config->ips_enabled)
  4092. return;
  4093. /*
  4094. * We can only enable IPS after we enable a plane and wait for a vblank
  4095. * This function is called from post_plane_update, which is run after
  4096. * a vblank wait.
  4097. */
  4098. assert_plane_enabled(dev_priv, crtc->plane);
  4099. if (IS_BROADWELL(dev_priv)) {
  4100. mutex_lock(&dev_priv->rps.hw_lock);
  4101. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4102. mutex_unlock(&dev_priv->rps.hw_lock);
  4103. /* Quoting Art Runyan: "its not safe to expect any particular
  4104. * value in IPS_CTL bit 31 after enabling IPS through the
  4105. * mailbox." Moreover, the mailbox may return a bogus state,
  4106. * so we need to just enable it and continue on.
  4107. */
  4108. } else {
  4109. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4110. /* The bit only becomes 1 in the next vblank, so this wait here
  4111. * is essentially intel_wait_for_vblank. If we don't have this
  4112. * and don't wait for vblanks until the end of crtc_enable, then
  4113. * the HW state readout code will complain that the expected
  4114. * IPS_CTL value is not the one we read. */
  4115. if (intel_wait_for_register(dev_priv,
  4116. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4117. 50))
  4118. DRM_ERROR("Timed out waiting for IPS enable\n");
  4119. }
  4120. }
  4121. void hsw_disable_ips(struct intel_crtc *crtc)
  4122. {
  4123. struct drm_device *dev = crtc->base.dev;
  4124. struct drm_i915_private *dev_priv = to_i915(dev);
  4125. if (!crtc->config->ips_enabled)
  4126. return;
  4127. assert_plane_enabled(dev_priv, crtc->plane);
  4128. if (IS_BROADWELL(dev_priv)) {
  4129. mutex_lock(&dev_priv->rps.hw_lock);
  4130. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4131. mutex_unlock(&dev_priv->rps.hw_lock);
  4132. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4133. if (intel_wait_for_register(dev_priv,
  4134. IPS_CTL, IPS_ENABLE, 0,
  4135. 42))
  4136. DRM_ERROR("Timed out waiting for IPS disable\n");
  4137. } else {
  4138. I915_WRITE(IPS_CTL, 0);
  4139. POSTING_READ(IPS_CTL);
  4140. }
  4141. /* We need to wait for a vblank before we can disable the plane. */
  4142. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4143. }
  4144. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4145. {
  4146. if (intel_crtc->overlay) {
  4147. struct drm_device *dev = intel_crtc->base.dev;
  4148. mutex_lock(&dev->struct_mutex);
  4149. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4150. mutex_unlock(&dev->struct_mutex);
  4151. }
  4152. /* Let userspace switch the overlay on again. In most cases userspace
  4153. * has to recompute where to put it anyway.
  4154. */
  4155. }
  4156. /**
  4157. * intel_post_enable_primary - Perform operations after enabling primary plane
  4158. * @crtc: the CRTC whose primary plane was just enabled
  4159. *
  4160. * Performs potentially sleeping operations that must be done after the primary
  4161. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4162. * called due to an explicit primary plane update, or due to an implicit
  4163. * re-enable that is caused when a sprite plane is updated to no longer
  4164. * completely hide the primary plane.
  4165. */
  4166. static void
  4167. intel_post_enable_primary(struct drm_crtc *crtc)
  4168. {
  4169. struct drm_device *dev = crtc->dev;
  4170. struct drm_i915_private *dev_priv = to_i915(dev);
  4171. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4172. int pipe = intel_crtc->pipe;
  4173. /*
  4174. * FIXME IPS should be fine as long as one plane is
  4175. * enabled, but in practice it seems to have problems
  4176. * when going from primary only to sprite only and vice
  4177. * versa.
  4178. */
  4179. hsw_enable_ips(intel_crtc);
  4180. /*
  4181. * Gen2 reports pipe underruns whenever all planes are disabled.
  4182. * So don't enable underrun reporting before at least some planes
  4183. * are enabled.
  4184. * FIXME: Need to fix the logic to work when we turn off all planes
  4185. * but leave the pipe running.
  4186. */
  4187. if (IS_GEN2(dev_priv))
  4188. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4189. /* Underruns don't always raise interrupts, so check manually. */
  4190. intel_check_cpu_fifo_underruns(dev_priv);
  4191. intel_check_pch_fifo_underruns(dev_priv);
  4192. }
  4193. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4194. static void
  4195. intel_pre_disable_primary(struct drm_crtc *crtc)
  4196. {
  4197. struct drm_device *dev = crtc->dev;
  4198. struct drm_i915_private *dev_priv = to_i915(dev);
  4199. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4200. int pipe = intel_crtc->pipe;
  4201. /*
  4202. * Gen2 reports pipe underruns whenever all planes are disabled.
  4203. * So diasble underrun reporting before all the planes get disabled.
  4204. * FIXME: Need to fix the logic to work when we turn off all planes
  4205. * but leave the pipe running.
  4206. */
  4207. if (IS_GEN2(dev_priv))
  4208. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4209. /*
  4210. * FIXME IPS should be fine as long as one plane is
  4211. * enabled, but in practice it seems to have problems
  4212. * when going from primary only to sprite only and vice
  4213. * versa.
  4214. */
  4215. hsw_disable_ips(intel_crtc);
  4216. }
  4217. /* FIXME get rid of this and use pre_plane_update */
  4218. static void
  4219. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4220. {
  4221. struct drm_device *dev = crtc->dev;
  4222. struct drm_i915_private *dev_priv = to_i915(dev);
  4223. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4224. int pipe = intel_crtc->pipe;
  4225. intel_pre_disable_primary(crtc);
  4226. /*
  4227. * Vblank time updates from the shadow to live plane control register
  4228. * are blocked if the memory self-refresh mode is active at that
  4229. * moment. So to make sure the plane gets truly disabled, disable
  4230. * first the self-refresh mode. The self-refresh enable bit in turn
  4231. * will be checked/applied by the HW only at the next frame start
  4232. * event which is after the vblank start event, so we need to have a
  4233. * wait-for-vblank between disabling the plane and the pipe.
  4234. */
  4235. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4236. intel_set_memory_cxsr(dev_priv, false))
  4237. intel_wait_for_vblank(dev_priv, pipe);
  4238. }
  4239. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4240. {
  4241. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4242. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4243. struct intel_crtc_state *pipe_config =
  4244. to_intel_crtc_state(crtc->base.state);
  4245. struct drm_plane *primary = crtc->base.primary;
  4246. struct drm_plane_state *old_pri_state =
  4247. drm_atomic_get_existing_plane_state(old_state, primary);
  4248. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4249. if (pipe_config->update_wm_post && pipe_config->base.active)
  4250. intel_update_watermarks(crtc);
  4251. if (old_pri_state) {
  4252. struct intel_plane_state *primary_state =
  4253. to_intel_plane_state(primary->state);
  4254. struct intel_plane_state *old_primary_state =
  4255. to_intel_plane_state(old_pri_state);
  4256. intel_fbc_post_update(crtc);
  4257. if (primary_state->base.visible &&
  4258. (needs_modeset(&pipe_config->base) ||
  4259. !old_primary_state->base.visible))
  4260. intel_post_enable_primary(&crtc->base);
  4261. }
  4262. }
  4263. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4264. struct intel_crtc_state *pipe_config)
  4265. {
  4266. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4267. struct drm_device *dev = crtc->base.dev;
  4268. struct drm_i915_private *dev_priv = to_i915(dev);
  4269. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4270. struct drm_plane *primary = crtc->base.primary;
  4271. struct drm_plane_state *old_pri_state =
  4272. drm_atomic_get_existing_plane_state(old_state, primary);
  4273. bool modeset = needs_modeset(&pipe_config->base);
  4274. struct intel_atomic_state *old_intel_state =
  4275. to_intel_atomic_state(old_state);
  4276. if (old_pri_state) {
  4277. struct intel_plane_state *primary_state =
  4278. to_intel_plane_state(primary->state);
  4279. struct intel_plane_state *old_primary_state =
  4280. to_intel_plane_state(old_pri_state);
  4281. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4282. if (old_primary_state->base.visible &&
  4283. (modeset || !primary_state->base.visible))
  4284. intel_pre_disable_primary(&crtc->base);
  4285. }
  4286. /*
  4287. * Vblank time updates from the shadow to live plane control register
  4288. * are blocked if the memory self-refresh mode is active at that
  4289. * moment. So to make sure the plane gets truly disabled, disable
  4290. * first the self-refresh mode. The self-refresh enable bit in turn
  4291. * will be checked/applied by the HW only at the next frame start
  4292. * event which is after the vblank start event, so we need to have a
  4293. * wait-for-vblank between disabling the plane and the pipe.
  4294. */
  4295. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4296. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4297. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4298. /*
  4299. * IVB workaround: must disable low power watermarks for at least
  4300. * one frame before enabling scaling. LP watermarks can be re-enabled
  4301. * when scaling is disabled.
  4302. *
  4303. * WaCxSRDisabledForSpriteScaling:ivb
  4304. */
  4305. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4306. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4307. /*
  4308. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4309. * watermark programming here.
  4310. */
  4311. if (needs_modeset(&pipe_config->base))
  4312. return;
  4313. /*
  4314. * For platforms that support atomic watermarks, program the
  4315. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4316. * will be the intermediate values that are safe for both pre- and
  4317. * post- vblank; when vblank happens, the 'active' values will be set
  4318. * to the final 'target' values and we'll do this again to get the
  4319. * optimal watermarks. For gen9+ platforms, the values we program here
  4320. * will be the final target values which will get automatically latched
  4321. * at vblank time; no further programming will be necessary.
  4322. *
  4323. * If a platform hasn't been transitioned to atomic watermarks yet,
  4324. * we'll continue to update watermarks the old way, if flags tell
  4325. * us to.
  4326. */
  4327. if (dev_priv->display.initial_watermarks != NULL)
  4328. dev_priv->display.initial_watermarks(old_intel_state,
  4329. pipe_config);
  4330. else if (pipe_config->update_wm_pre)
  4331. intel_update_watermarks(crtc);
  4332. }
  4333. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4334. {
  4335. struct drm_device *dev = crtc->dev;
  4336. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4337. struct drm_plane *p;
  4338. int pipe = intel_crtc->pipe;
  4339. intel_crtc_dpms_overlay_disable(intel_crtc);
  4340. drm_for_each_plane_mask(p, dev, plane_mask)
  4341. to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
  4342. /*
  4343. * FIXME: Once we grow proper nuclear flip support out of this we need
  4344. * to compute the mask of flip planes precisely. For the time being
  4345. * consider this a flip to a NULL plane.
  4346. */
  4347. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4348. }
  4349. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4350. struct intel_crtc_state *crtc_state,
  4351. struct drm_atomic_state *old_state)
  4352. {
  4353. struct drm_connector_state *conn_state;
  4354. struct drm_connector *conn;
  4355. int i;
  4356. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4357. struct intel_encoder *encoder =
  4358. to_intel_encoder(conn_state->best_encoder);
  4359. if (conn_state->crtc != crtc)
  4360. continue;
  4361. if (encoder->pre_pll_enable)
  4362. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4363. }
  4364. }
  4365. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4366. struct intel_crtc_state *crtc_state,
  4367. struct drm_atomic_state *old_state)
  4368. {
  4369. struct drm_connector_state *conn_state;
  4370. struct drm_connector *conn;
  4371. int i;
  4372. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4373. struct intel_encoder *encoder =
  4374. to_intel_encoder(conn_state->best_encoder);
  4375. if (conn_state->crtc != crtc)
  4376. continue;
  4377. if (encoder->pre_enable)
  4378. encoder->pre_enable(encoder, crtc_state, conn_state);
  4379. }
  4380. }
  4381. static void intel_encoders_enable(struct drm_crtc *crtc,
  4382. struct intel_crtc_state *crtc_state,
  4383. struct drm_atomic_state *old_state)
  4384. {
  4385. struct drm_connector_state *conn_state;
  4386. struct drm_connector *conn;
  4387. int i;
  4388. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4389. struct intel_encoder *encoder =
  4390. to_intel_encoder(conn_state->best_encoder);
  4391. if (conn_state->crtc != crtc)
  4392. continue;
  4393. encoder->enable(encoder, crtc_state, conn_state);
  4394. intel_opregion_notify_encoder(encoder, true);
  4395. }
  4396. }
  4397. static void intel_encoders_disable(struct drm_crtc *crtc,
  4398. struct intel_crtc_state *old_crtc_state,
  4399. struct drm_atomic_state *old_state)
  4400. {
  4401. struct drm_connector_state *old_conn_state;
  4402. struct drm_connector *conn;
  4403. int i;
  4404. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4405. struct intel_encoder *encoder =
  4406. to_intel_encoder(old_conn_state->best_encoder);
  4407. if (old_conn_state->crtc != crtc)
  4408. continue;
  4409. intel_opregion_notify_encoder(encoder, false);
  4410. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4411. }
  4412. }
  4413. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4414. struct intel_crtc_state *old_crtc_state,
  4415. struct drm_atomic_state *old_state)
  4416. {
  4417. struct drm_connector_state *old_conn_state;
  4418. struct drm_connector *conn;
  4419. int i;
  4420. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4421. struct intel_encoder *encoder =
  4422. to_intel_encoder(old_conn_state->best_encoder);
  4423. if (old_conn_state->crtc != crtc)
  4424. continue;
  4425. if (encoder->post_disable)
  4426. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4427. }
  4428. }
  4429. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4430. struct intel_crtc_state *old_crtc_state,
  4431. struct drm_atomic_state *old_state)
  4432. {
  4433. struct drm_connector_state *old_conn_state;
  4434. struct drm_connector *conn;
  4435. int i;
  4436. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4437. struct intel_encoder *encoder =
  4438. to_intel_encoder(old_conn_state->best_encoder);
  4439. if (old_conn_state->crtc != crtc)
  4440. continue;
  4441. if (encoder->post_pll_disable)
  4442. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4443. }
  4444. }
  4445. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4446. struct drm_atomic_state *old_state)
  4447. {
  4448. struct drm_crtc *crtc = pipe_config->base.crtc;
  4449. struct drm_device *dev = crtc->dev;
  4450. struct drm_i915_private *dev_priv = to_i915(dev);
  4451. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4452. int pipe = intel_crtc->pipe;
  4453. struct intel_atomic_state *old_intel_state =
  4454. to_intel_atomic_state(old_state);
  4455. if (WARN_ON(intel_crtc->active))
  4456. return;
  4457. /*
  4458. * Sometimes spurious CPU pipe underruns happen during FDI
  4459. * training, at least with VGA+HDMI cloning. Suppress them.
  4460. *
  4461. * On ILK we get an occasional spurious CPU pipe underruns
  4462. * between eDP port A enable and vdd enable. Also PCH port
  4463. * enable seems to result in the occasional CPU pipe underrun.
  4464. *
  4465. * Spurious PCH underruns also occur during PCH enabling.
  4466. */
  4467. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4468. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4469. if (intel_crtc->config->has_pch_encoder)
  4470. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4471. if (intel_crtc->config->has_pch_encoder)
  4472. intel_prepare_shared_dpll(intel_crtc);
  4473. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4474. intel_dp_set_m_n(intel_crtc, M1_N1);
  4475. intel_set_pipe_timings(intel_crtc);
  4476. intel_set_pipe_src_size(intel_crtc);
  4477. if (intel_crtc->config->has_pch_encoder) {
  4478. intel_cpu_transcoder_set_m_n(intel_crtc,
  4479. &intel_crtc->config->fdi_m_n, NULL);
  4480. }
  4481. ironlake_set_pipeconf(crtc);
  4482. intel_crtc->active = true;
  4483. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4484. if (intel_crtc->config->has_pch_encoder) {
  4485. /* Note: FDI PLL enabling _must_ be done before we enable the
  4486. * cpu pipes, hence this is separate from all the other fdi/pch
  4487. * enabling. */
  4488. ironlake_fdi_pll_enable(intel_crtc);
  4489. } else {
  4490. assert_fdi_tx_disabled(dev_priv, pipe);
  4491. assert_fdi_rx_disabled(dev_priv, pipe);
  4492. }
  4493. ironlake_pfit_enable(intel_crtc);
  4494. /*
  4495. * On ILK+ LUT must be loaded before the pipe is running but with
  4496. * clocks enabled
  4497. */
  4498. intel_color_load_luts(&pipe_config->base);
  4499. if (dev_priv->display.initial_watermarks != NULL)
  4500. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4501. intel_enable_pipe(intel_crtc);
  4502. if (intel_crtc->config->has_pch_encoder)
  4503. ironlake_pch_enable(pipe_config);
  4504. assert_vblank_disabled(crtc);
  4505. drm_crtc_vblank_on(crtc);
  4506. intel_encoders_enable(crtc, pipe_config, old_state);
  4507. if (HAS_PCH_CPT(dev_priv))
  4508. cpt_verify_modeset(dev, intel_crtc->pipe);
  4509. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4510. if (intel_crtc->config->has_pch_encoder)
  4511. intel_wait_for_vblank(dev_priv, pipe);
  4512. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4513. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4514. }
  4515. /* IPS only exists on ULT machines and is tied to pipe A. */
  4516. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4517. {
  4518. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4519. }
  4520. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4521. struct drm_atomic_state *old_state)
  4522. {
  4523. struct drm_crtc *crtc = pipe_config->base.crtc;
  4524. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4525. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4526. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4527. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4528. struct intel_atomic_state *old_intel_state =
  4529. to_intel_atomic_state(old_state);
  4530. if (WARN_ON(intel_crtc->active))
  4531. return;
  4532. if (intel_crtc->config->has_pch_encoder)
  4533. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4534. false);
  4535. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4536. if (intel_crtc->config->shared_dpll)
  4537. intel_enable_shared_dpll(intel_crtc);
  4538. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4539. intel_dp_set_m_n(intel_crtc, M1_N1);
  4540. if (!transcoder_is_dsi(cpu_transcoder))
  4541. intel_set_pipe_timings(intel_crtc);
  4542. intel_set_pipe_src_size(intel_crtc);
  4543. if (cpu_transcoder != TRANSCODER_EDP &&
  4544. !transcoder_is_dsi(cpu_transcoder)) {
  4545. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4546. intel_crtc->config->pixel_multiplier - 1);
  4547. }
  4548. if (intel_crtc->config->has_pch_encoder) {
  4549. intel_cpu_transcoder_set_m_n(intel_crtc,
  4550. &intel_crtc->config->fdi_m_n, NULL);
  4551. }
  4552. if (!transcoder_is_dsi(cpu_transcoder))
  4553. haswell_set_pipeconf(crtc);
  4554. haswell_set_pipemisc(crtc);
  4555. intel_color_set_csc(&pipe_config->base);
  4556. intel_crtc->active = true;
  4557. if (intel_crtc->config->has_pch_encoder)
  4558. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4559. else
  4560. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4561. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4562. if (intel_crtc->config->has_pch_encoder)
  4563. dev_priv->display.fdi_link_train(intel_crtc, pipe_config);
  4564. if (!transcoder_is_dsi(cpu_transcoder))
  4565. intel_ddi_enable_pipe_clock(pipe_config);
  4566. if (INTEL_GEN(dev_priv) >= 9)
  4567. skylake_pfit_enable(intel_crtc);
  4568. else
  4569. ironlake_pfit_enable(intel_crtc);
  4570. /*
  4571. * On ILK+ LUT must be loaded before the pipe is running but with
  4572. * clocks enabled
  4573. */
  4574. intel_color_load_luts(&pipe_config->base);
  4575. intel_ddi_set_pipe_settings(pipe_config);
  4576. if (!transcoder_is_dsi(cpu_transcoder))
  4577. intel_ddi_enable_transcoder_func(pipe_config);
  4578. if (dev_priv->display.initial_watermarks != NULL)
  4579. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4580. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4581. if (!transcoder_is_dsi(cpu_transcoder))
  4582. intel_enable_pipe(intel_crtc);
  4583. if (intel_crtc->config->has_pch_encoder)
  4584. lpt_pch_enable(pipe_config);
  4585. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4586. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4587. assert_vblank_disabled(crtc);
  4588. drm_crtc_vblank_on(crtc);
  4589. intel_encoders_enable(crtc, pipe_config, old_state);
  4590. if (intel_crtc->config->has_pch_encoder) {
  4591. intel_wait_for_vblank(dev_priv, pipe);
  4592. intel_wait_for_vblank(dev_priv, pipe);
  4593. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4594. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4595. true);
  4596. }
  4597. /* If we change the relative order between pipe/planes enabling, we need
  4598. * to change the workaround. */
  4599. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4600. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4601. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4602. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4603. }
  4604. }
  4605. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4606. {
  4607. struct drm_device *dev = crtc->base.dev;
  4608. struct drm_i915_private *dev_priv = to_i915(dev);
  4609. int pipe = crtc->pipe;
  4610. /* To avoid upsetting the power well on haswell only disable the pfit if
  4611. * it's in use. The hw state code will make sure we get this right. */
  4612. if (force || crtc->config->pch_pfit.enabled) {
  4613. I915_WRITE(PF_CTL(pipe), 0);
  4614. I915_WRITE(PF_WIN_POS(pipe), 0);
  4615. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4616. }
  4617. }
  4618. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4619. struct drm_atomic_state *old_state)
  4620. {
  4621. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4622. struct drm_device *dev = crtc->dev;
  4623. struct drm_i915_private *dev_priv = to_i915(dev);
  4624. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4625. int pipe = intel_crtc->pipe;
  4626. /*
  4627. * Sometimes spurious CPU pipe underruns happen when the
  4628. * pipe is already disabled, but FDI RX/TX is still enabled.
  4629. * Happens at least with VGA+HDMI cloning. Suppress them.
  4630. */
  4631. if (intel_crtc->config->has_pch_encoder) {
  4632. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4633. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4634. }
  4635. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4636. drm_crtc_vblank_off(crtc);
  4637. assert_vblank_disabled(crtc);
  4638. intel_disable_pipe(intel_crtc);
  4639. ironlake_pfit_disable(intel_crtc, false);
  4640. if (intel_crtc->config->has_pch_encoder)
  4641. ironlake_fdi_disable(crtc);
  4642. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4643. if (intel_crtc->config->has_pch_encoder) {
  4644. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4645. if (HAS_PCH_CPT(dev_priv)) {
  4646. i915_reg_t reg;
  4647. u32 temp;
  4648. /* disable TRANS_DP_CTL */
  4649. reg = TRANS_DP_CTL(pipe);
  4650. temp = I915_READ(reg);
  4651. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4652. TRANS_DP_PORT_SEL_MASK);
  4653. temp |= TRANS_DP_PORT_SEL_NONE;
  4654. I915_WRITE(reg, temp);
  4655. /* disable DPLL_SEL */
  4656. temp = I915_READ(PCH_DPLL_SEL);
  4657. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4658. I915_WRITE(PCH_DPLL_SEL, temp);
  4659. }
  4660. ironlake_fdi_pll_disable(intel_crtc);
  4661. }
  4662. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4663. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4664. }
  4665. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4666. struct drm_atomic_state *old_state)
  4667. {
  4668. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4669. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4670. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4671. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4672. if (intel_crtc->config->has_pch_encoder)
  4673. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4674. false);
  4675. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4676. drm_crtc_vblank_off(crtc);
  4677. assert_vblank_disabled(crtc);
  4678. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4679. if (!transcoder_is_dsi(cpu_transcoder))
  4680. intel_disable_pipe(intel_crtc);
  4681. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4682. intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
  4683. if (!transcoder_is_dsi(cpu_transcoder))
  4684. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4685. if (INTEL_GEN(dev_priv) >= 9)
  4686. skylake_scaler_disable(intel_crtc);
  4687. else
  4688. ironlake_pfit_disable(intel_crtc, false);
  4689. if (!transcoder_is_dsi(cpu_transcoder))
  4690. intel_ddi_disable_pipe_clock(intel_crtc->config);
  4691. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4692. if (old_crtc_state->has_pch_encoder)
  4693. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4694. true);
  4695. }
  4696. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4697. {
  4698. struct drm_device *dev = crtc->base.dev;
  4699. struct drm_i915_private *dev_priv = to_i915(dev);
  4700. struct intel_crtc_state *pipe_config = crtc->config;
  4701. if (!pipe_config->gmch_pfit.control)
  4702. return;
  4703. /*
  4704. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4705. * according to register description and PRM.
  4706. */
  4707. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4708. assert_pipe_disabled(dev_priv, crtc->pipe);
  4709. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4710. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4711. /* Border color in case we don't scale up to the full screen. Black by
  4712. * default, change to something else for debugging. */
  4713. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4714. }
  4715. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4716. {
  4717. switch (port) {
  4718. case PORT_A:
  4719. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4720. case PORT_B:
  4721. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4722. case PORT_C:
  4723. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4724. case PORT_D:
  4725. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4726. case PORT_E:
  4727. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4728. default:
  4729. MISSING_CASE(port);
  4730. return POWER_DOMAIN_PORT_OTHER;
  4731. }
  4732. }
  4733. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4734. struct intel_crtc_state *crtc_state)
  4735. {
  4736. struct drm_device *dev = crtc->dev;
  4737. struct drm_i915_private *dev_priv = to_i915(dev);
  4738. struct drm_encoder *encoder;
  4739. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4740. enum pipe pipe = intel_crtc->pipe;
  4741. u64 mask;
  4742. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4743. if (!crtc_state->base.active)
  4744. return 0;
  4745. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4746. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4747. if (crtc_state->pch_pfit.enabled ||
  4748. crtc_state->pch_pfit.force_thru)
  4749. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4750. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4751. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4752. mask |= BIT_ULL(intel_encoder->power_domain);
  4753. }
  4754. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4755. mask |= BIT(POWER_DOMAIN_AUDIO);
  4756. if (crtc_state->shared_dpll)
  4757. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4758. return mask;
  4759. }
  4760. static u64
  4761. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4762. struct intel_crtc_state *crtc_state)
  4763. {
  4764. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4765. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4766. enum intel_display_power_domain domain;
  4767. u64 domains, new_domains, old_domains;
  4768. old_domains = intel_crtc->enabled_power_domains;
  4769. intel_crtc->enabled_power_domains = new_domains =
  4770. get_crtc_power_domains(crtc, crtc_state);
  4771. domains = new_domains & ~old_domains;
  4772. for_each_power_domain(domain, domains)
  4773. intel_display_power_get(dev_priv, domain);
  4774. return old_domains & ~new_domains;
  4775. }
  4776. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4777. u64 domains)
  4778. {
  4779. enum intel_display_power_domain domain;
  4780. for_each_power_domain(domain, domains)
  4781. intel_display_power_put(dev_priv, domain);
  4782. }
  4783. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  4784. struct drm_atomic_state *old_state)
  4785. {
  4786. struct intel_atomic_state *old_intel_state =
  4787. to_intel_atomic_state(old_state);
  4788. struct drm_crtc *crtc = pipe_config->base.crtc;
  4789. struct drm_device *dev = crtc->dev;
  4790. struct drm_i915_private *dev_priv = to_i915(dev);
  4791. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4792. int pipe = intel_crtc->pipe;
  4793. if (WARN_ON(intel_crtc->active))
  4794. return;
  4795. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4796. intel_dp_set_m_n(intel_crtc, M1_N1);
  4797. intel_set_pipe_timings(intel_crtc);
  4798. intel_set_pipe_src_size(intel_crtc);
  4799. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  4800. struct drm_i915_private *dev_priv = to_i915(dev);
  4801. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4802. I915_WRITE(CHV_CANVAS(pipe), 0);
  4803. }
  4804. i9xx_set_pipeconf(intel_crtc);
  4805. intel_crtc->active = true;
  4806. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4807. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4808. if (IS_CHERRYVIEW(dev_priv)) {
  4809. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4810. chv_enable_pll(intel_crtc, intel_crtc->config);
  4811. } else {
  4812. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4813. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4814. }
  4815. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4816. i9xx_pfit_enable(intel_crtc);
  4817. intel_color_load_luts(&pipe_config->base);
  4818. dev_priv->display.initial_watermarks(old_intel_state,
  4819. pipe_config);
  4820. intel_enable_pipe(intel_crtc);
  4821. assert_vblank_disabled(crtc);
  4822. drm_crtc_vblank_on(crtc);
  4823. intel_encoders_enable(crtc, pipe_config, old_state);
  4824. }
  4825. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4826. {
  4827. struct drm_device *dev = crtc->base.dev;
  4828. struct drm_i915_private *dev_priv = to_i915(dev);
  4829. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4830. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4831. }
  4832. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  4833. struct drm_atomic_state *old_state)
  4834. {
  4835. struct intel_atomic_state *old_intel_state =
  4836. to_intel_atomic_state(old_state);
  4837. struct drm_crtc *crtc = pipe_config->base.crtc;
  4838. struct drm_device *dev = crtc->dev;
  4839. struct drm_i915_private *dev_priv = to_i915(dev);
  4840. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4841. enum pipe pipe = intel_crtc->pipe;
  4842. if (WARN_ON(intel_crtc->active))
  4843. return;
  4844. i9xx_set_pll_dividers(intel_crtc);
  4845. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4846. intel_dp_set_m_n(intel_crtc, M1_N1);
  4847. intel_set_pipe_timings(intel_crtc);
  4848. intel_set_pipe_src_size(intel_crtc);
  4849. i9xx_set_pipeconf(intel_crtc);
  4850. intel_crtc->active = true;
  4851. if (!IS_GEN2(dev_priv))
  4852. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4853. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4854. i9xx_enable_pll(intel_crtc);
  4855. i9xx_pfit_enable(intel_crtc);
  4856. intel_color_load_luts(&pipe_config->base);
  4857. if (dev_priv->display.initial_watermarks != NULL)
  4858. dev_priv->display.initial_watermarks(old_intel_state,
  4859. intel_crtc->config);
  4860. else
  4861. intel_update_watermarks(intel_crtc);
  4862. intel_enable_pipe(intel_crtc);
  4863. assert_vblank_disabled(crtc);
  4864. drm_crtc_vblank_on(crtc);
  4865. intel_encoders_enable(crtc, pipe_config, old_state);
  4866. }
  4867. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4868. {
  4869. struct drm_device *dev = crtc->base.dev;
  4870. struct drm_i915_private *dev_priv = to_i915(dev);
  4871. if (!crtc->config->gmch_pfit.control)
  4872. return;
  4873. assert_pipe_disabled(dev_priv, crtc->pipe);
  4874. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4875. I915_READ(PFIT_CONTROL));
  4876. I915_WRITE(PFIT_CONTROL, 0);
  4877. }
  4878. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4879. struct drm_atomic_state *old_state)
  4880. {
  4881. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4882. struct drm_device *dev = crtc->dev;
  4883. struct drm_i915_private *dev_priv = to_i915(dev);
  4884. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4885. int pipe = intel_crtc->pipe;
  4886. /*
  4887. * On gen2 planes are double buffered but the pipe isn't, so we must
  4888. * wait for planes to fully turn off before disabling the pipe.
  4889. */
  4890. if (IS_GEN2(dev_priv))
  4891. intel_wait_for_vblank(dev_priv, pipe);
  4892. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4893. drm_crtc_vblank_off(crtc);
  4894. assert_vblank_disabled(crtc);
  4895. intel_disable_pipe(intel_crtc);
  4896. i9xx_pfit_disable(intel_crtc);
  4897. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4898. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  4899. if (IS_CHERRYVIEW(dev_priv))
  4900. chv_disable_pll(dev_priv, pipe);
  4901. else if (IS_VALLEYVIEW(dev_priv))
  4902. vlv_disable_pll(dev_priv, pipe);
  4903. else
  4904. i9xx_disable_pll(intel_crtc);
  4905. }
  4906. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  4907. if (!IS_GEN2(dev_priv))
  4908. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4909. if (!dev_priv->display.initial_watermarks)
  4910. intel_update_watermarks(intel_crtc);
  4911. }
  4912. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  4913. {
  4914. struct intel_encoder *encoder;
  4915. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4916. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4917. enum intel_display_power_domain domain;
  4918. u64 domains;
  4919. struct drm_atomic_state *state;
  4920. struct intel_crtc_state *crtc_state;
  4921. int ret;
  4922. if (!intel_crtc->active)
  4923. return;
  4924. if (crtc->primary->state->visible) {
  4925. WARN_ON(intel_crtc->flip_work);
  4926. intel_pre_disable_primary_noatomic(crtc);
  4927. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  4928. crtc->primary->state->visible = false;
  4929. }
  4930. state = drm_atomic_state_alloc(crtc->dev);
  4931. if (!state) {
  4932. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  4933. crtc->base.id, crtc->name);
  4934. return;
  4935. }
  4936. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  4937. /* Everything's already locked, -EDEADLK can't happen. */
  4938. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4939. ret = drm_atomic_add_affected_connectors(state, crtc);
  4940. WARN_ON(IS_ERR(crtc_state) || ret);
  4941. dev_priv->display.crtc_disable(crtc_state, state);
  4942. drm_atomic_state_put(state);
  4943. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  4944. crtc->base.id, crtc->name);
  4945. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  4946. crtc->state->active = false;
  4947. intel_crtc->active = false;
  4948. crtc->enabled = false;
  4949. crtc->state->connector_mask = 0;
  4950. crtc->state->encoder_mask = 0;
  4951. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  4952. encoder->base.crtc = NULL;
  4953. intel_fbc_disable(intel_crtc);
  4954. intel_update_watermarks(intel_crtc);
  4955. intel_disable_shared_dpll(intel_crtc);
  4956. domains = intel_crtc->enabled_power_domains;
  4957. for_each_power_domain(domain, domains)
  4958. intel_display_power_put(dev_priv, domain);
  4959. intel_crtc->enabled_power_domains = 0;
  4960. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  4961. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  4962. }
  4963. /*
  4964. * turn all crtc's off, but do not adjust state
  4965. * This has to be paired with a call to intel_modeset_setup_hw_state.
  4966. */
  4967. int intel_display_suspend(struct drm_device *dev)
  4968. {
  4969. struct drm_i915_private *dev_priv = to_i915(dev);
  4970. struct drm_atomic_state *state;
  4971. int ret;
  4972. state = drm_atomic_helper_suspend(dev);
  4973. ret = PTR_ERR_OR_ZERO(state);
  4974. if (ret)
  4975. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  4976. else
  4977. dev_priv->modeset_restore_state = state;
  4978. return ret;
  4979. }
  4980. void intel_encoder_destroy(struct drm_encoder *encoder)
  4981. {
  4982. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4983. drm_encoder_cleanup(encoder);
  4984. kfree(intel_encoder);
  4985. }
  4986. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4987. * internal consistency). */
  4988. static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
  4989. struct drm_connector_state *conn_state)
  4990. {
  4991. struct intel_connector *connector = to_intel_connector(conn_state->connector);
  4992. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4993. connector->base.base.id,
  4994. connector->base.name);
  4995. if (connector->get_hw_state(connector)) {
  4996. struct intel_encoder *encoder = connector->encoder;
  4997. I915_STATE_WARN(!crtc_state,
  4998. "connector enabled without attached crtc\n");
  4999. if (!crtc_state)
  5000. return;
  5001. I915_STATE_WARN(!crtc_state->active,
  5002. "connector is active, but attached crtc isn't\n");
  5003. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5004. return;
  5005. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5006. "atomic encoder doesn't match attached encoder\n");
  5007. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5008. "attached encoder crtc differs from connector crtc\n");
  5009. } else {
  5010. I915_STATE_WARN(crtc_state && crtc_state->active,
  5011. "attached crtc is active, but connector isn't\n");
  5012. I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
  5013. "best encoder set without crtc!\n");
  5014. }
  5015. }
  5016. int intel_connector_init(struct intel_connector *connector)
  5017. {
  5018. drm_atomic_helper_connector_reset(&connector->base);
  5019. if (!connector->base.state)
  5020. return -ENOMEM;
  5021. return 0;
  5022. }
  5023. struct intel_connector *intel_connector_alloc(void)
  5024. {
  5025. struct intel_connector *connector;
  5026. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5027. if (!connector)
  5028. return NULL;
  5029. if (intel_connector_init(connector) < 0) {
  5030. kfree(connector);
  5031. return NULL;
  5032. }
  5033. return connector;
  5034. }
  5035. /* Simple connector->get_hw_state implementation for encoders that support only
  5036. * one connector and no cloning and hence the encoder state determines the state
  5037. * of the connector. */
  5038. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5039. {
  5040. enum pipe pipe = 0;
  5041. struct intel_encoder *encoder = connector->encoder;
  5042. return encoder->get_hw_state(encoder, &pipe);
  5043. }
  5044. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5045. {
  5046. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5047. return crtc_state->fdi_lanes;
  5048. return 0;
  5049. }
  5050. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5051. struct intel_crtc_state *pipe_config)
  5052. {
  5053. struct drm_i915_private *dev_priv = to_i915(dev);
  5054. struct drm_atomic_state *state = pipe_config->base.state;
  5055. struct intel_crtc *other_crtc;
  5056. struct intel_crtc_state *other_crtc_state;
  5057. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5058. pipe_name(pipe), pipe_config->fdi_lanes);
  5059. if (pipe_config->fdi_lanes > 4) {
  5060. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5061. pipe_name(pipe), pipe_config->fdi_lanes);
  5062. return -EINVAL;
  5063. }
  5064. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5065. if (pipe_config->fdi_lanes > 2) {
  5066. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5067. pipe_config->fdi_lanes);
  5068. return -EINVAL;
  5069. } else {
  5070. return 0;
  5071. }
  5072. }
  5073. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5074. return 0;
  5075. /* Ivybridge 3 pipe is really complicated */
  5076. switch (pipe) {
  5077. case PIPE_A:
  5078. return 0;
  5079. case PIPE_B:
  5080. if (pipe_config->fdi_lanes <= 2)
  5081. return 0;
  5082. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5083. other_crtc_state =
  5084. intel_atomic_get_crtc_state(state, other_crtc);
  5085. if (IS_ERR(other_crtc_state))
  5086. return PTR_ERR(other_crtc_state);
  5087. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5088. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5089. pipe_name(pipe), pipe_config->fdi_lanes);
  5090. return -EINVAL;
  5091. }
  5092. return 0;
  5093. case PIPE_C:
  5094. if (pipe_config->fdi_lanes > 2) {
  5095. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5096. pipe_name(pipe), pipe_config->fdi_lanes);
  5097. return -EINVAL;
  5098. }
  5099. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5100. other_crtc_state =
  5101. intel_atomic_get_crtc_state(state, other_crtc);
  5102. if (IS_ERR(other_crtc_state))
  5103. return PTR_ERR(other_crtc_state);
  5104. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5105. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5106. return -EINVAL;
  5107. }
  5108. return 0;
  5109. default:
  5110. BUG();
  5111. }
  5112. }
  5113. #define RETRY 1
  5114. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5115. struct intel_crtc_state *pipe_config)
  5116. {
  5117. struct drm_device *dev = intel_crtc->base.dev;
  5118. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5119. int lane, link_bw, fdi_dotclock, ret;
  5120. bool needs_recompute = false;
  5121. retry:
  5122. /* FDI is a binary signal running at ~2.7GHz, encoding
  5123. * each output octet as 10 bits. The actual frequency
  5124. * is stored as a divider into a 100MHz clock, and the
  5125. * mode pixel clock is stored in units of 1KHz.
  5126. * Hence the bw of each lane in terms of the mode signal
  5127. * is:
  5128. */
  5129. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5130. fdi_dotclock = adjusted_mode->crtc_clock;
  5131. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5132. pipe_config->pipe_bpp);
  5133. pipe_config->fdi_lanes = lane;
  5134. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5135. link_bw, &pipe_config->fdi_m_n);
  5136. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5137. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5138. pipe_config->pipe_bpp -= 2*3;
  5139. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5140. pipe_config->pipe_bpp);
  5141. needs_recompute = true;
  5142. pipe_config->bw_constrained = true;
  5143. goto retry;
  5144. }
  5145. if (needs_recompute)
  5146. return RETRY;
  5147. return ret;
  5148. }
  5149. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5150. struct intel_crtc_state *pipe_config)
  5151. {
  5152. if (pipe_config->pipe_bpp > 24)
  5153. return false;
  5154. /* HSW can handle pixel rate up to cdclk? */
  5155. if (IS_HASWELL(dev_priv))
  5156. return true;
  5157. /*
  5158. * We compare against max which means we must take
  5159. * the increased cdclk requirement into account when
  5160. * calculating the new cdclk.
  5161. *
  5162. * Should measure whether using a lower cdclk w/o IPS
  5163. */
  5164. return pipe_config->pixel_rate <=
  5165. dev_priv->max_cdclk_freq * 95 / 100;
  5166. }
  5167. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5168. struct intel_crtc_state *pipe_config)
  5169. {
  5170. struct drm_device *dev = crtc->base.dev;
  5171. struct drm_i915_private *dev_priv = to_i915(dev);
  5172. pipe_config->ips_enabled = i915.enable_ips &&
  5173. hsw_crtc_supports_ips(crtc) &&
  5174. pipe_config_supports_ips(dev_priv, pipe_config);
  5175. }
  5176. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5177. {
  5178. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5179. /* GDG double wide on either pipe, otherwise pipe A only */
  5180. return INTEL_INFO(dev_priv)->gen < 4 &&
  5181. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5182. }
  5183. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5184. {
  5185. uint32_t pixel_rate;
  5186. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5187. /*
  5188. * We only use IF-ID interlacing. If we ever use
  5189. * PF-ID we'll need to adjust the pixel_rate here.
  5190. */
  5191. if (pipe_config->pch_pfit.enabled) {
  5192. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5193. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5194. pipe_w = pipe_config->pipe_src_w;
  5195. pipe_h = pipe_config->pipe_src_h;
  5196. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5197. pfit_h = pfit_size & 0xFFFF;
  5198. if (pipe_w < pfit_w)
  5199. pipe_w = pfit_w;
  5200. if (pipe_h < pfit_h)
  5201. pipe_h = pfit_h;
  5202. if (WARN_ON(!pfit_w || !pfit_h))
  5203. return pixel_rate;
  5204. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5205. pfit_w * pfit_h);
  5206. }
  5207. return pixel_rate;
  5208. }
  5209. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5210. {
  5211. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5212. if (HAS_GMCH_DISPLAY(dev_priv))
  5213. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5214. crtc_state->pixel_rate =
  5215. crtc_state->base.adjusted_mode.crtc_clock;
  5216. else
  5217. crtc_state->pixel_rate =
  5218. ilk_pipe_pixel_rate(crtc_state);
  5219. }
  5220. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5221. struct intel_crtc_state *pipe_config)
  5222. {
  5223. struct drm_device *dev = crtc->base.dev;
  5224. struct drm_i915_private *dev_priv = to_i915(dev);
  5225. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5226. int clock_limit = dev_priv->max_dotclk_freq;
  5227. if (INTEL_GEN(dev_priv) < 4) {
  5228. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5229. /*
  5230. * Enable double wide mode when the dot clock
  5231. * is > 90% of the (display) core speed.
  5232. */
  5233. if (intel_crtc_supports_double_wide(crtc) &&
  5234. adjusted_mode->crtc_clock > clock_limit) {
  5235. clock_limit = dev_priv->max_dotclk_freq;
  5236. pipe_config->double_wide = true;
  5237. }
  5238. }
  5239. if (adjusted_mode->crtc_clock > clock_limit) {
  5240. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5241. adjusted_mode->crtc_clock, clock_limit,
  5242. yesno(pipe_config->double_wide));
  5243. return -EINVAL;
  5244. }
  5245. /*
  5246. * Pipe horizontal size must be even in:
  5247. * - DVO ganged mode
  5248. * - LVDS dual channel mode
  5249. * - Double wide pipe
  5250. */
  5251. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5252. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5253. pipe_config->pipe_src_w &= ~1;
  5254. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5255. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5256. */
  5257. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5258. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5259. return -EINVAL;
  5260. intel_crtc_compute_pixel_rate(pipe_config);
  5261. if (HAS_IPS(dev_priv))
  5262. hsw_compute_ips_config(crtc, pipe_config);
  5263. if (pipe_config->has_pch_encoder)
  5264. return ironlake_fdi_compute_config(crtc, pipe_config);
  5265. return 0;
  5266. }
  5267. static void
  5268. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5269. {
  5270. while (*num > DATA_LINK_M_N_MASK ||
  5271. *den > DATA_LINK_M_N_MASK) {
  5272. *num >>= 1;
  5273. *den >>= 1;
  5274. }
  5275. }
  5276. static void compute_m_n(unsigned int m, unsigned int n,
  5277. uint32_t *ret_m, uint32_t *ret_n)
  5278. {
  5279. /*
  5280. * Reduce M/N as much as possible without loss in precision. Several DP
  5281. * dongles in particular seem to be fussy about too large *link* M/N
  5282. * values. The passed in values are more likely to have the least
  5283. * significant bits zero than M after rounding below, so do this first.
  5284. */
  5285. while ((m & 1) == 0 && (n & 1) == 0) {
  5286. m >>= 1;
  5287. n >>= 1;
  5288. }
  5289. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5290. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5291. intel_reduce_m_n_ratio(ret_m, ret_n);
  5292. }
  5293. void
  5294. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5295. int pixel_clock, int link_clock,
  5296. struct intel_link_m_n *m_n)
  5297. {
  5298. m_n->tu = 64;
  5299. compute_m_n(bits_per_pixel * pixel_clock,
  5300. link_clock * nlanes * 8,
  5301. &m_n->gmch_m, &m_n->gmch_n);
  5302. compute_m_n(pixel_clock, link_clock,
  5303. &m_n->link_m, &m_n->link_n);
  5304. }
  5305. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5306. {
  5307. if (i915.panel_use_ssc >= 0)
  5308. return i915.panel_use_ssc != 0;
  5309. return dev_priv->vbt.lvds_use_ssc
  5310. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5311. }
  5312. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5313. {
  5314. return (1 << dpll->n) << 16 | dpll->m2;
  5315. }
  5316. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5317. {
  5318. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5319. }
  5320. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5321. struct intel_crtc_state *crtc_state,
  5322. struct dpll *reduced_clock)
  5323. {
  5324. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5325. u32 fp, fp2 = 0;
  5326. if (IS_PINEVIEW(dev_priv)) {
  5327. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5328. if (reduced_clock)
  5329. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5330. } else {
  5331. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5332. if (reduced_clock)
  5333. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5334. }
  5335. crtc_state->dpll_hw_state.fp0 = fp;
  5336. crtc->lowfreq_avail = false;
  5337. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5338. reduced_clock) {
  5339. crtc_state->dpll_hw_state.fp1 = fp2;
  5340. crtc->lowfreq_avail = true;
  5341. } else {
  5342. crtc_state->dpll_hw_state.fp1 = fp;
  5343. }
  5344. }
  5345. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5346. pipe)
  5347. {
  5348. u32 reg_val;
  5349. /*
  5350. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5351. * and set it to a reasonable value instead.
  5352. */
  5353. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5354. reg_val &= 0xffffff00;
  5355. reg_val |= 0x00000030;
  5356. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5357. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5358. reg_val &= 0x00ffffff;
  5359. reg_val |= 0x8c000000;
  5360. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5361. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5362. reg_val &= 0xffffff00;
  5363. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5364. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5365. reg_val &= 0x00ffffff;
  5366. reg_val |= 0xb0000000;
  5367. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5368. }
  5369. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5370. struct intel_link_m_n *m_n)
  5371. {
  5372. struct drm_device *dev = crtc->base.dev;
  5373. struct drm_i915_private *dev_priv = to_i915(dev);
  5374. int pipe = crtc->pipe;
  5375. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5376. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5377. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5378. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5379. }
  5380. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5381. struct intel_link_m_n *m_n,
  5382. struct intel_link_m_n *m2_n2)
  5383. {
  5384. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5385. int pipe = crtc->pipe;
  5386. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5387. if (INTEL_GEN(dev_priv) >= 5) {
  5388. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5389. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5390. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5391. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5392. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5393. * for gen < 8) and if DRRS is supported (to make sure the
  5394. * registers are not unnecessarily accessed).
  5395. */
  5396. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5397. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5398. I915_WRITE(PIPE_DATA_M2(transcoder),
  5399. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5400. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5401. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5402. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5403. }
  5404. } else {
  5405. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5406. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5407. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5408. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5409. }
  5410. }
  5411. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5412. {
  5413. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5414. if (m_n == M1_N1) {
  5415. dp_m_n = &crtc->config->dp_m_n;
  5416. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5417. } else if (m_n == M2_N2) {
  5418. /*
  5419. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5420. * needs to be programmed into M1_N1.
  5421. */
  5422. dp_m_n = &crtc->config->dp_m2_n2;
  5423. } else {
  5424. DRM_ERROR("Unsupported divider value\n");
  5425. return;
  5426. }
  5427. if (crtc->config->has_pch_encoder)
  5428. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5429. else
  5430. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5431. }
  5432. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5433. struct intel_crtc_state *pipe_config)
  5434. {
  5435. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5436. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5437. if (crtc->pipe != PIPE_A)
  5438. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5439. /* DPLL not used with DSI, but still need the rest set up */
  5440. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5441. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5442. DPLL_EXT_BUFFER_ENABLE_VLV;
  5443. pipe_config->dpll_hw_state.dpll_md =
  5444. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5445. }
  5446. static void chv_compute_dpll(struct intel_crtc *crtc,
  5447. struct intel_crtc_state *pipe_config)
  5448. {
  5449. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5450. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5451. if (crtc->pipe != PIPE_A)
  5452. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5453. /* DPLL not used with DSI, but still need the rest set up */
  5454. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5455. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5456. pipe_config->dpll_hw_state.dpll_md =
  5457. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5458. }
  5459. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5460. const struct intel_crtc_state *pipe_config)
  5461. {
  5462. struct drm_device *dev = crtc->base.dev;
  5463. struct drm_i915_private *dev_priv = to_i915(dev);
  5464. enum pipe pipe = crtc->pipe;
  5465. u32 mdiv;
  5466. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5467. u32 coreclk, reg_val;
  5468. /* Enable Refclk */
  5469. I915_WRITE(DPLL(pipe),
  5470. pipe_config->dpll_hw_state.dpll &
  5471. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5472. /* No need to actually set up the DPLL with DSI */
  5473. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5474. return;
  5475. mutex_lock(&dev_priv->sb_lock);
  5476. bestn = pipe_config->dpll.n;
  5477. bestm1 = pipe_config->dpll.m1;
  5478. bestm2 = pipe_config->dpll.m2;
  5479. bestp1 = pipe_config->dpll.p1;
  5480. bestp2 = pipe_config->dpll.p2;
  5481. /* See eDP HDMI DPIO driver vbios notes doc */
  5482. /* PLL B needs special handling */
  5483. if (pipe == PIPE_B)
  5484. vlv_pllb_recal_opamp(dev_priv, pipe);
  5485. /* Set up Tx target for periodic Rcomp update */
  5486. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5487. /* Disable target IRef on PLL */
  5488. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5489. reg_val &= 0x00ffffff;
  5490. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5491. /* Disable fast lock */
  5492. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5493. /* Set idtafcrecal before PLL is enabled */
  5494. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5495. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5496. mdiv |= ((bestn << DPIO_N_SHIFT));
  5497. mdiv |= (1 << DPIO_K_SHIFT);
  5498. /*
  5499. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5500. * but we don't support that).
  5501. * Note: don't use the DAC post divider as it seems unstable.
  5502. */
  5503. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5504. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5505. mdiv |= DPIO_ENABLE_CALIBRATION;
  5506. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5507. /* Set HBR and RBR LPF coefficients */
  5508. if (pipe_config->port_clock == 162000 ||
  5509. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5510. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5511. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5512. 0x009f0003);
  5513. else
  5514. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5515. 0x00d0000f);
  5516. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5517. /* Use SSC source */
  5518. if (pipe == PIPE_A)
  5519. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5520. 0x0df40000);
  5521. else
  5522. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5523. 0x0df70000);
  5524. } else { /* HDMI or VGA */
  5525. /* Use bend source */
  5526. if (pipe == PIPE_A)
  5527. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5528. 0x0df70000);
  5529. else
  5530. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5531. 0x0df40000);
  5532. }
  5533. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5534. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5535. if (intel_crtc_has_dp_encoder(crtc->config))
  5536. coreclk |= 0x01000000;
  5537. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5538. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5539. mutex_unlock(&dev_priv->sb_lock);
  5540. }
  5541. static void chv_prepare_pll(struct intel_crtc *crtc,
  5542. const struct intel_crtc_state *pipe_config)
  5543. {
  5544. struct drm_device *dev = crtc->base.dev;
  5545. struct drm_i915_private *dev_priv = to_i915(dev);
  5546. enum pipe pipe = crtc->pipe;
  5547. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5548. u32 loopfilter, tribuf_calcntr;
  5549. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5550. u32 dpio_val;
  5551. int vco;
  5552. /* Enable Refclk and SSC */
  5553. I915_WRITE(DPLL(pipe),
  5554. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5555. /* No need to actually set up the DPLL with DSI */
  5556. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5557. return;
  5558. bestn = pipe_config->dpll.n;
  5559. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5560. bestm1 = pipe_config->dpll.m1;
  5561. bestm2 = pipe_config->dpll.m2 >> 22;
  5562. bestp1 = pipe_config->dpll.p1;
  5563. bestp2 = pipe_config->dpll.p2;
  5564. vco = pipe_config->dpll.vco;
  5565. dpio_val = 0;
  5566. loopfilter = 0;
  5567. mutex_lock(&dev_priv->sb_lock);
  5568. /* p1 and p2 divider */
  5569. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5570. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5571. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5572. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5573. 1 << DPIO_CHV_K_DIV_SHIFT);
  5574. /* Feedback post-divider - m2 */
  5575. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5576. /* Feedback refclk divider - n and m1 */
  5577. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5578. DPIO_CHV_M1_DIV_BY_2 |
  5579. 1 << DPIO_CHV_N_DIV_SHIFT);
  5580. /* M2 fraction division */
  5581. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5582. /* M2 fraction division enable */
  5583. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5584. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5585. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5586. if (bestm2_frac)
  5587. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5588. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5589. /* Program digital lock detect threshold */
  5590. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5591. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5592. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5593. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5594. if (!bestm2_frac)
  5595. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5596. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5597. /* Loop filter */
  5598. if (vco == 5400000) {
  5599. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5600. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5601. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5602. tribuf_calcntr = 0x9;
  5603. } else if (vco <= 6200000) {
  5604. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5605. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5606. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5607. tribuf_calcntr = 0x9;
  5608. } else if (vco <= 6480000) {
  5609. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5610. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5611. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5612. tribuf_calcntr = 0x8;
  5613. } else {
  5614. /* Not supported. Apply the same limits as in the max case */
  5615. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5616. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5617. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5618. tribuf_calcntr = 0;
  5619. }
  5620. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5621. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5622. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5623. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5624. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5625. /* AFC Recal */
  5626. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5627. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5628. DPIO_AFC_RECAL);
  5629. mutex_unlock(&dev_priv->sb_lock);
  5630. }
  5631. /**
  5632. * vlv_force_pll_on - forcibly enable just the PLL
  5633. * @dev_priv: i915 private structure
  5634. * @pipe: pipe PLL to enable
  5635. * @dpll: PLL configuration
  5636. *
  5637. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5638. * in cases where we need the PLL enabled even when @pipe is not going to
  5639. * be enabled.
  5640. */
  5641. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5642. const struct dpll *dpll)
  5643. {
  5644. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5645. struct intel_crtc_state *pipe_config;
  5646. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5647. if (!pipe_config)
  5648. return -ENOMEM;
  5649. pipe_config->base.crtc = &crtc->base;
  5650. pipe_config->pixel_multiplier = 1;
  5651. pipe_config->dpll = *dpll;
  5652. if (IS_CHERRYVIEW(dev_priv)) {
  5653. chv_compute_dpll(crtc, pipe_config);
  5654. chv_prepare_pll(crtc, pipe_config);
  5655. chv_enable_pll(crtc, pipe_config);
  5656. } else {
  5657. vlv_compute_dpll(crtc, pipe_config);
  5658. vlv_prepare_pll(crtc, pipe_config);
  5659. vlv_enable_pll(crtc, pipe_config);
  5660. }
  5661. kfree(pipe_config);
  5662. return 0;
  5663. }
  5664. /**
  5665. * vlv_force_pll_off - forcibly disable just the PLL
  5666. * @dev_priv: i915 private structure
  5667. * @pipe: pipe PLL to disable
  5668. *
  5669. * Disable the PLL for @pipe. To be used in cases where we need
  5670. * the PLL enabled even when @pipe is not going to be enabled.
  5671. */
  5672. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5673. {
  5674. if (IS_CHERRYVIEW(dev_priv))
  5675. chv_disable_pll(dev_priv, pipe);
  5676. else
  5677. vlv_disable_pll(dev_priv, pipe);
  5678. }
  5679. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5680. struct intel_crtc_state *crtc_state,
  5681. struct dpll *reduced_clock)
  5682. {
  5683. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5684. u32 dpll;
  5685. struct dpll *clock = &crtc_state->dpll;
  5686. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5687. dpll = DPLL_VGA_MODE_DIS;
  5688. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5689. dpll |= DPLLB_MODE_LVDS;
  5690. else
  5691. dpll |= DPLLB_MODE_DAC_SERIAL;
  5692. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5693. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5694. dpll |= (crtc_state->pixel_multiplier - 1)
  5695. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5696. }
  5697. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5698. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5699. dpll |= DPLL_SDVO_HIGH_SPEED;
  5700. if (intel_crtc_has_dp_encoder(crtc_state))
  5701. dpll |= DPLL_SDVO_HIGH_SPEED;
  5702. /* compute bitmask from p1 value */
  5703. if (IS_PINEVIEW(dev_priv))
  5704. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5705. else {
  5706. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5707. if (IS_G4X(dev_priv) && reduced_clock)
  5708. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5709. }
  5710. switch (clock->p2) {
  5711. case 5:
  5712. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5713. break;
  5714. case 7:
  5715. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5716. break;
  5717. case 10:
  5718. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5719. break;
  5720. case 14:
  5721. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5722. break;
  5723. }
  5724. if (INTEL_GEN(dev_priv) >= 4)
  5725. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5726. if (crtc_state->sdvo_tv_clock)
  5727. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5728. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5729. intel_panel_use_ssc(dev_priv))
  5730. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5731. else
  5732. dpll |= PLL_REF_INPUT_DREFCLK;
  5733. dpll |= DPLL_VCO_ENABLE;
  5734. crtc_state->dpll_hw_state.dpll = dpll;
  5735. if (INTEL_GEN(dev_priv) >= 4) {
  5736. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5737. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5738. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5739. }
  5740. }
  5741. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5742. struct intel_crtc_state *crtc_state,
  5743. struct dpll *reduced_clock)
  5744. {
  5745. struct drm_device *dev = crtc->base.dev;
  5746. struct drm_i915_private *dev_priv = to_i915(dev);
  5747. u32 dpll;
  5748. struct dpll *clock = &crtc_state->dpll;
  5749. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5750. dpll = DPLL_VGA_MODE_DIS;
  5751. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5752. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5753. } else {
  5754. if (clock->p1 == 2)
  5755. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5756. else
  5757. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5758. if (clock->p2 == 4)
  5759. dpll |= PLL_P2_DIVIDE_BY_4;
  5760. }
  5761. if (!IS_I830(dev_priv) &&
  5762. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5763. dpll |= DPLL_DVO_2X_MODE;
  5764. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5765. intel_panel_use_ssc(dev_priv))
  5766. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5767. else
  5768. dpll |= PLL_REF_INPUT_DREFCLK;
  5769. dpll |= DPLL_VCO_ENABLE;
  5770. crtc_state->dpll_hw_state.dpll = dpll;
  5771. }
  5772. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5773. {
  5774. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5775. enum pipe pipe = intel_crtc->pipe;
  5776. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5777. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5778. uint32_t crtc_vtotal, crtc_vblank_end;
  5779. int vsyncshift = 0;
  5780. /* We need to be careful not to changed the adjusted mode, for otherwise
  5781. * the hw state checker will get angry at the mismatch. */
  5782. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5783. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5784. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5785. /* the chip adds 2 halflines automatically */
  5786. crtc_vtotal -= 1;
  5787. crtc_vblank_end -= 1;
  5788. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5789. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5790. else
  5791. vsyncshift = adjusted_mode->crtc_hsync_start -
  5792. adjusted_mode->crtc_htotal / 2;
  5793. if (vsyncshift < 0)
  5794. vsyncshift += adjusted_mode->crtc_htotal;
  5795. }
  5796. if (INTEL_GEN(dev_priv) > 3)
  5797. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5798. I915_WRITE(HTOTAL(cpu_transcoder),
  5799. (adjusted_mode->crtc_hdisplay - 1) |
  5800. ((adjusted_mode->crtc_htotal - 1) << 16));
  5801. I915_WRITE(HBLANK(cpu_transcoder),
  5802. (adjusted_mode->crtc_hblank_start - 1) |
  5803. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5804. I915_WRITE(HSYNC(cpu_transcoder),
  5805. (adjusted_mode->crtc_hsync_start - 1) |
  5806. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5807. I915_WRITE(VTOTAL(cpu_transcoder),
  5808. (adjusted_mode->crtc_vdisplay - 1) |
  5809. ((crtc_vtotal - 1) << 16));
  5810. I915_WRITE(VBLANK(cpu_transcoder),
  5811. (adjusted_mode->crtc_vblank_start - 1) |
  5812. ((crtc_vblank_end - 1) << 16));
  5813. I915_WRITE(VSYNC(cpu_transcoder),
  5814. (adjusted_mode->crtc_vsync_start - 1) |
  5815. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5816. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5817. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5818. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5819. * bits. */
  5820. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  5821. (pipe == PIPE_B || pipe == PIPE_C))
  5822. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5823. }
  5824. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  5825. {
  5826. struct drm_device *dev = intel_crtc->base.dev;
  5827. struct drm_i915_private *dev_priv = to_i915(dev);
  5828. enum pipe pipe = intel_crtc->pipe;
  5829. /* pipesrc controls the size that is scaled from, which should
  5830. * always be the user's requested size.
  5831. */
  5832. I915_WRITE(PIPESRC(pipe),
  5833. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5834. (intel_crtc->config->pipe_src_h - 1));
  5835. }
  5836. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5837. struct intel_crtc_state *pipe_config)
  5838. {
  5839. struct drm_device *dev = crtc->base.dev;
  5840. struct drm_i915_private *dev_priv = to_i915(dev);
  5841. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5842. uint32_t tmp;
  5843. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5844. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5845. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5846. tmp = I915_READ(HBLANK(cpu_transcoder));
  5847. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5848. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5849. tmp = I915_READ(HSYNC(cpu_transcoder));
  5850. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5851. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5852. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5853. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5854. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5855. tmp = I915_READ(VBLANK(cpu_transcoder));
  5856. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5857. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5858. tmp = I915_READ(VSYNC(cpu_transcoder));
  5859. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5860. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5861. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5862. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5863. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5864. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5865. }
  5866. }
  5867. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  5868. struct intel_crtc_state *pipe_config)
  5869. {
  5870. struct drm_device *dev = crtc->base.dev;
  5871. struct drm_i915_private *dev_priv = to_i915(dev);
  5872. u32 tmp;
  5873. tmp = I915_READ(PIPESRC(crtc->pipe));
  5874. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5875. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5876. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5877. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5878. }
  5879. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5880. struct intel_crtc_state *pipe_config)
  5881. {
  5882. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5883. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5884. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5885. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5886. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5887. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5888. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5889. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5890. mode->flags = pipe_config->base.adjusted_mode.flags;
  5891. mode->type = DRM_MODE_TYPE_DRIVER;
  5892. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5893. mode->hsync = drm_mode_hsync(mode);
  5894. mode->vrefresh = drm_mode_vrefresh(mode);
  5895. drm_mode_set_name(mode);
  5896. }
  5897. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5898. {
  5899. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5900. uint32_t pipeconf;
  5901. pipeconf = 0;
  5902. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5903. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5904. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5905. if (intel_crtc->config->double_wide)
  5906. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5907. /* only g4x and later have fancy bpc/dither controls */
  5908. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  5909. IS_CHERRYVIEW(dev_priv)) {
  5910. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5911. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5912. pipeconf |= PIPECONF_DITHER_EN |
  5913. PIPECONF_DITHER_TYPE_SP;
  5914. switch (intel_crtc->config->pipe_bpp) {
  5915. case 18:
  5916. pipeconf |= PIPECONF_6BPC;
  5917. break;
  5918. case 24:
  5919. pipeconf |= PIPECONF_8BPC;
  5920. break;
  5921. case 30:
  5922. pipeconf |= PIPECONF_10BPC;
  5923. break;
  5924. default:
  5925. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5926. BUG();
  5927. }
  5928. }
  5929. if (HAS_PIPE_CXSR(dev_priv)) {
  5930. if (intel_crtc->lowfreq_avail) {
  5931. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5932. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5933. } else {
  5934. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5935. }
  5936. }
  5937. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5938. if (INTEL_GEN(dev_priv) < 4 ||
  5939. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5940. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5941. else
  5942. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5943. } else
  5944. pipeconf |= PIPECONF_PROGRESSIVE;
  5945. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  5946. intel_crtc->config->limited_color_range)
  5947. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5948. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5949. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5950. }
  5951. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  5952. struct intel_crtc_state *crtc_state)
  5953. {
  5954. struct drm_device *dev = crtc->base.dev;
  5955. struct drm_i915_private *dev_priv = to_i915(dev);
  5956. const struct intel_limit *limit;
  5957. int refclk = 48000;
  5958. memset(&crtc_state->dpll_hw_state, 0,
  5959. sizeof(crtc_state->dpll_hw_state));
  5960. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5961. if (intel_panel_use_ssc(dev_priv)) {
  5962. refclk = dev_priv->vbt.lvds_ssc_freq;
  5963. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5964. }
  5965. limit = &intel_limits_i8xx_lvds;
  5966. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  5967. limit = &intel_limits_i8xx_dvo;
  5968. } else {
  5969. limit = &intel_limits_i8xx_dac;
  5970. }
  5971. if (!crtc_state->clock_set &&
  5972. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  5973. refclk, NULL, &crtc_state->dpll)) {
  5974. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5975. return -EINVAL;
  5976. }
  5977. i8xx_compute_dpll(crtc, crtc_state, NULL);
  5978. return 0;
  5979. }
  5980. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  5981. struct intel_crtc_state *crtc_state)
  5982. {
  5983. struct drm_device *dev = crtc->base.dev;
  5984. struct drm_i915_private *dev_priv = to_i915(dev);
  5985. const struct intel_limit *limit;
  5986. int refclk = 96000;
  5987. memset(&crtc_state->dpll_hw_state, 0,
  5988. sizeof(crtc_state->dpll_hw_state));
  5989. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5990. if (intel_panel_use_ssc(dev_priv)) {
  5991. refclk = dev_priv->vbt.lvds_ssc_freq;
  5992. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5993. }
  5994. if (intel_is_dual_link_lvds(dev))
  5995. limit = &intel_limits_g4x_dual_channel_lvds;
  5996. else
  5997. limit = &intel_limits_g4x_single_channel_lvds;
  5998. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  5999. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6000. limit = &intel_limits_g4x_hdmi;
  6001. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6002. limit = &intel_limits_g4x_sdvo;
  6003. } else {
  6004. /* The option is for other outputs */
  6005. limit = &intel_limits_i9xx_sdvo;
  6006. }
  6007. if (!crtc_state->clock_set &&
  6008. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6009. refclk, NULL, &crtc_state->dpll)) {
  6010. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6011. return -EINVAL;
  6012. }
  6013. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6014. return 0;
  6015. }
  6016. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6017. struct intel_crtc_state *crtc_state)
  6018. {
  6019. struct drm_device *dev = crtc->base.dev;
  6020. struct drm_i915_private *dev_priv = to_i915(dev);
  6021. const struct intel_limit *limit;
  6022. int refclk = 96000;
  6023. memset(&crtc_state->dpll_hw_state, 0,
  6024. sizeof(crtc_state->dpll_hw_state));
  6025. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6026. if (intel_panel_use_ssc(dev_priv)) {
  6027. refclk = dev_priv->vbt.lvds_ssc_freq;
  6028. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6029. }
  6030. limit = &intel_limits_pineview_lvds;
  6031. } else {
  6032. limit = &intel_limits_pineview_sdvo;
  6033. }
  6034. if (!crtc_state->clock_set &&
  6035. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6036. refclk, NULL, &crtc_state->dpll)) {
  6037. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6038. return -EINVAL;
  6039. }
  6040. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6041. return 0;
  6042. }
  6043. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6044. struct intel_crtc_state *crtc_state)
  6045. {
  6046. struct drm_device *dev = crtc->base.dev;
  6047. struct drm_i915_private *dev_priv = to_i915(dev);
  6048. const struct intel_limit *limit;
  6049. int refclk = 96000;
  6050. memset(&crtc_state->dpll_hw_state, 0,
  6051. sizeof(crtc_state->dpll_hw_state));
  6052. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6053. if (intel_panel_use_ssc(dev_priv)) {
  6054. refclk = dev_priv->vbt.lvds_ssc_freq;
  6055. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6056. }
  6057. limit = &intel_limits_i9xx_lvds;
  6058. } else {
  6059. limit = &intel_limits_i9xx_sdvo;
  6060. }
  6061. if (!crtc_state->clock_set &&
  6062. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6063. refclk, NULL, &crtc_state->dpll)) {
  6064. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6065. return -EINVAL;
  6066. }
  6067. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6068. return 0;
  6069. }
  6070. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6071. struct intel_crtc_state *crtc_state)
  6072. {
  6073. int refclk = 100000;
  6074. const struct intel_limit *limit = &intel_limits_chv;
  6075. memset(&crtc_state->dpll_hw_state, 0,
  6076. sizeof(crtc_state->dpll_hw_state));
  6077. if (!crtc_state->clock_set &&
  6078. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6079. refclk, NULL, &crtc_state->dpll)) {
  6080. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6081. return -EINVAL;
  6082. }
  6083. chv_compute_dpll(crtc, crtc_state);
  6084. return 0;
  6085. }
  6086. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6087. struct intel_crtc_state *crtc_state)
  6088. {
  6089. int refclk = 100000;
  6090. const struct intel_limit *limit = &intel_limits_vlv;
  6091. memset(&crtc_state->dpll_hw_state, 0,
  6092. sizeof(crtc_state->dpll_hw_state));
  6093. if (!crtc_state->clock_set &&
  6094. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6095. refclk, NULL, &crtc_state->dpll)) {
  6096. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6097. return -EINVAL;
  6098. }
  6099. vlv_compute_dpll(crtc, crtc_state);
  6100. return 0;
  6101. }
  6102. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6103. struct intel_crtc_state *pipe_config)
  6104. {
  6105. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6106. uint32_t tmp;
  6107. if (INTEL_GEN(dev_priv) <= 3 &&
  6108. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6109. return;
  6110. tmp = I915_READ(PFIT_CONTROL);
  6111. if (!(tmp & PFIT_ENABLE))
  6112. return;
  6113. /* Check whether the pfit is attached to our pipe. */
  6114. if (INTEL_GEN(dev_priv) < 4) {
  6115. if (crtc->pipe != PIPE_B)
  6116. return;
  6117. } else {
  6118. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6119. return;
  6120. }
  6121. pipe_config->gmch_pfit.control = tmp;
  6122. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6123. }
  6124. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6125. struct intel_crtc_state *pipe_config)
  6126. {
  6127. struct drm_device *dev = crtc->base.dev;
  6128. struct drm_i915_private *dev_priv = to_i915(dev);
  6129. int pipe = pipe_config->cpu_transcoder;
  6130. struct dpll clock;
  6131. u32 mdiv;
  6132. int refclk = 100000;
  6133. /* In case of DSI, DPLL will not be used */
  6134. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6135. return;
  6136. mutex_lock(&dev_priv->sb_lock);
  6137. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6138. mutex_unlock(&dev_priv->sb_lock);
  6139. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6140. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6141. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6142. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6143. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6144. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6145. }
  6146. static void
  6147. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6148. struct intel_initial_plane_config *plane_config)
  6149. {
  6150. struct drm_device *dev = crtc->base.dev;
  6151. struct drm_i915_private *dev_priv = to_i915(dev);
  6152. u32 val, base, offset;
  6153. int pipe = crtc->pipe, plane = crtc->plane;
  6154. int fourcc, pixel_format;
  6155. unsigned int aligned_height;
  6156. struct drm_framebuffer *fb;
  6157. struct intel_framebuffer *intel_fb;
  6158. val = I915_READ(DSPCNTR(plane));
  6159. if (!(val & DISPLAY_PLANE_ENABLE))
  6160. return;
  6161. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6162. if (!intel_fb) {
  6163. DRM_DEBUG_KMS("failed to alloc fb\n");
  6164. return;
  6165. }
  6166. fb = &intel_fb->base;
  6167. fb->dev = dev;
  6168. if (INTEL_GEN(dev_priv) >= 4) {
  6169. if (val & DISPPLANE_TILED) {
  6170. plane_config->tiling = I915_TILING_X;
  6171. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6172. }
  6173. }
  6174. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6175. fourcc = i9xx_format_to_fourcc(pixel_format);
  6176. fb->format = drm_format_info(fourcc);
  6177. if (INTEL_GEN(dev_priv) >= 4) {
  6178. if (plane_config->tiling)
  6179. offset = I915_READ(DSPTILEOFF(plane));
  6180. else
  6181. offset = I915_READ(DSPLINOFF(plane));
  6182. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6183. } else {
  6184. base = I915_READ(DSPADDR(plane));
  6185. }
  6186. plane_config->base = base;
  6187. val = I915_READ(PIPESRC(pipe));
  6188. fb->width = ((val >> 16) & 0xfff) + 1;
  6189. fb->height = ((val >> 0) & 0xfff) + 1;
  6190. val = I915_READ(DSPSTRIDE(pipe));
  6191. fb->pitches[0] = val & 0xffffffc0;
  6192. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6193. plane_config->size = fb->pitches[0] * aligned_height;
  6194. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6195. pipe_name(pipe), plane, fb->width, fb->height,
  6196. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6197. plane_config->size);
  6198. plane_config->fb = intel_fb;
  6199. }
  6200. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6201. struct intel_crtc_state *pipe_config)
  6202. {
  6203. struct drm_device *dev = crtc->base.dev;
  6204. struct drm_i915_private *dev_priv = to_i915(dev);
  6205. int pipe = pipe_config->cpu_transcoder;
  6206. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6207. struct dpll clock;
  6208. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6209. int refclk = 100000;
  6210. /* In case of DSI, DPLL will not be used */
  6211. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6212. return;
  6213. mutex_lock(&dev_priv->sb_lock);
  6214. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6215. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6216. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6217. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6218. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6219. mutex_unlock(&dev_priv->sb_lock);
  6220. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6221. clock.m2 = (pll_dw0 & 0xff) << 22;
  6222. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6223. clock.m2 |= pll_dw2 & 0x3fffff;
  6224. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6225. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6226. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6227. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6228. }
  6229. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6230. struct intel_crtc_state *pipe_config)
  6231. {
  6232. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6233. enum intel_display_power_domain power_domain;
  6234. uint32_t tmp;
  6235. bool ret;
  6236. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6237. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6238. return false;
  6239. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6240. pipe_config->shared_dpll = NULL;
  6241. ret = false;
  6242. tmp = I915_READ(PIPECONF(crtc->pipe));
  6243. if (!(tmp & PIPECONF_ENABLE))
  6244. goto out;
  6245. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6246. IS_CHERRYVIEW(dev_priv)) {
  6247. switch (tmp & PIPECONF_BPC_MASK) {
  6248. case PIPECONF_6BPC:
  6249. pipe_config->pipe_bpp = 18;
  6250. break;
  6251. case PIPECONF_8BPC:
  6252. pipe_config->pipe_bpp = 24;
  6253. break;
  6254. case PIPECONF_10BPC:
  6255. pipe_config->pipe_bpp = 30;
  6256. break;
  6257. default:
  6258. break;
  6259. }
  6260. }
  6261. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6262. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6263. pipe_config->limited_color_range = true;
  6264. if (INTEL_GEN(dev_priv) < 4)
  6265. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6266. intel_get_pipe_timings(crtc, pipe_config);
  6267. intel_get_pipe_src_size(crtc, pipe_config);
  6268. i9xx_get_pfit_config(crtc, pipe_config);
  6269. if (INTEL_GEN(dev_priv) >= 4) {
  6270. /* No way to read it out on pipes B and C */
  6271. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6272. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6273. else
  6274. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6275. pipe_config->pixel_multiplier =
  6276. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6277. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6278. pipe_config->dpll_hw_state.dpll_md = tmp;
  6279. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6280. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6281. tmp = I915_READ(DPLL(crtc->pipe));
  6282. pipe_config->pixel_multiplier =
  6283. ((tmp & SDVO_MULTIPLIER_MASK)
  6284. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6285. } else {
  6286. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6287. * port and will be fixed up in the encoder->get_config
  6288. * function. */
  6289. pipe_config->pixel_multiplier = 1;
  6290. }
  6291. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6292. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6293. /*
  6294. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6295. * on 830. Filter it out here so that we don't
  6296. * report errors due to that.
  6297. */
  6298. if (IS_I830(dev_priv))
  6299. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6300. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6301. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6302. } else {
  6303. /* Mask out read-only status bits. */
  6304. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6305. DPLL_PORTC_READY_MASK |
  6306. DPLL_PORTB_READY_MASK);
  6307. }
  6308. if (IS_CHERRYVIEW(dev_priv))
  6309. chv_crtc_clock_get(crtc, pipe_config);
  6310. else if (IS_VALLEYVIEW(dev_priv))
  6311. vlv_crtc_clock_get(crtc, pipe_config);
  6312. else
  6313. i9xx_crtc_clock_get(crtc, pipe_config);
  6314. /*
  6315. * Normally the dotclock is filled in by the encoder .get_config()
  6316. * but in case the pipe is enabled w/o any ports we need a sane
  6317. * default.
  6318. */
  6319. pipe_config->base.adjusted_mode.crtc_clock =
  6320. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6321. ret = true;
  6322. out:
  6323. intel_display_power_put(dev_priv, power_domain);
  6324. return ret;
  6325. }
  6326. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6327. {
  6328. struct intel_encoder *encoder;
  6329. int i;
  6330. u32 val, final;
  6331. bool has_lvds = false;
  6332. bool has_cpu_edp = false;
  6333. bool has_panel = false;
  6334. bool has_ck505 = false;
  6335. bool can_ssc = false;
  6336. bool using_ssc_source = false;
  6337. /* We need to take the global config into account */
  6338. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6339. switch (encoder->type) {
  6340. case INTEL_OUTPUT_LVDS:
  6341. has_panel = true;
  6342. has_lvds = true;
  6343. break;
  6344. case INTEL_OUTPUT_EDP:
  6345. has_panel = true;
  6346. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6347. has_cpu_edp = true;
  6348. break;
  6349. default:
  6350. break;
  6351. }
  6352. }
  6353. if (HAS_PCH_IBX(dev_priv)) {
  6354. has_ck505 = dev_priv->vbt.display_clock_mode;
  6355. can_ssc = has_ck505;
  6356. } else {
  6357. has_ck505 = false;
  6358. can_ssc = true;
  6359. }
  6360. /* Check if any DPLLs are using the SSC source */
  6361. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6362. u32 temp = I915_READ(PCH_DPLL(i));
  6363. if (!(temp & DPLL_VCO_ENABLE))
  6364. continue;
  6365. if ((temp & PLL_REF_INPUT_MASK) ==
  6366. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6367. using_ssc_source = true;
  6368. break;
  6369. }
  6370. }
  6371. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6372. has_panel, has_lvds, has_ck505, using_ssc_source);
  6373. /* Ironlake: try to setup display ref clock before DPLL
  6374. * enabling. This is only under driver's control after
  6375. * PCH B stepping, previous chipset stepping should be
  6376. * ignoring this setting.
  6377. */
  6378. val = I915_READ(PCH_DREF_CONTROL);
  6379. /* As we must carefully and slowly disable/enable each source in turn,
  6380. * compute the final state we want first and check if we need to
  6381. * make any changes at all.
  6382. */
  6383. final = val;
  6384. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6385. if (has_ck505)
  6386. final |= DREF_NONSPREAD_CK505_ENABLE;
  6387. else
  6388. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6389. final &= ~DREF_SSC_SOURCE_MASK;
  6390. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6391. final &= ~DREF_SSC1_ENABLE;
  6392. if (has_panel) {
  6393. final |= DREF_SSC_SOURCE_ENABLE;
  6394. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6395. final |= DREF_SSC1_ENABLE;
  6396. if (has_cpu_edp) {
  6397. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6398. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6399. else
  6400. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6401. } else
  6402. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6403. } else if (using_ssc_source) {
  6404. final |= DREF_SSC_SOURCE_ENABLE;
  6405. final |= DREF_SSC1_ENABLE;
  6406. }
  6407. if (final == val)
  6408. return;
  6409. /* Always enable nonspread source */
  6410. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6411. if (has_ck505)
  6412. val |= DREF_NONSPREAD_CK505_ENABLE;
  6413. else
  6414. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6415. if (has_panel) {
  6416. val &= ~DREF_SSC_SOURCE_MASK;
  6417. val |= DREF_SSC_SOURCE_ENABLE;
  6418. /* SSC must be turned on before enabling the CPU output */
  6419. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6420. DRM_DEBUG_KMS("Using SSC on panel\n");
  6421. val |= DREF_SSC1_ENABLE;
  6422. } else
  6423. val &= ~DREF_SSC1_ENABLE;
  6424. /* Get SSC going before enabling the outputs */
  6425. I915_WRITE(PCH_DREF_CONTROL, val);
  6426. POSTING_READ(PCH_DREF_CONTROL);
  6427. udelay(200);
  6428. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6429. /* Enable CPU source on CPU attached eDP */
  6430. if (has_cpu_edp) {
  6431. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6432. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6433. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6434. } else
  6435. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6436. } else
  6437. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6438. I915_WRITE(PCH_DREF_CONTROL, val);
  6439. POSTING_READ(PCH_DREF_CONTROL);
  6440. udelay(200);
  6441. } else {
  6442. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6443. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6444. /* Turn off CPU output */
  6445. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6446. I915_WRITE(PCH_DREF_CONTROL, val);
  6447. POSTING_READ(PCH_DREF_CONTROL);
  6448. udelay(200);
  6449. if (!using_ssc_source) {
  6450. DRM_DEBUG_KMS("Disabling SSC source\n");
  6451. /* Turn off the SSC source */
  6452. val &= ~DREF_SSC_SOURCE_MASK;
  6453. val |= DREF_SSC_SOURCE_DISABLE;
  6454. /* Turn off SSC1 */
  6455. val &= ~DREF_SSC1_ENABLE;
  6456. I915_WRITE(PCH_DREF_CONTROL, val);
  6457. POSTING_READ(PCH_DREF_CONTROL);
  6458. udelay(200);
  6459. }
  6460. }
  6461. BUG_ON(val != final);
  6462. }
  6463. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6464. {
  6465. uint32_t tmp;
  6466. tmp = I915_READ(SOUTH_CHICKEN2);
  6467. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6468. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6469. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6470. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6471. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6472. tmp = I915_READ(SOUTH_CHICKEN2);
  6473. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6474. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6475. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6476. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6477. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6478. }
  6479. /* WaMPhyProgramming:hsw */
  6480. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6481. {
  6482. uint32_t tmp;
  6483. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6484. tmp &= ~(0xFF << 24);
  6485. tmp |= (0x12 << 24);
  6486. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6487. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6488. tmp |= (1 << 11);
  6489. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6490. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6491. tmp |= (1 << 11);
  6492. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6493. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6494. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6495. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6496. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6497. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6498. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6499. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6500. tmp &= ~(7 << 13);
  6501. tmp |= (5 << 13);
  6502. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6503. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6504. tmp &= ~(7 << 13);
  6505. tmp |= (5 << 13);
  6506. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6507. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6508. tmp &= ~0xFF;
  6509. tmp |= 0x1C;
  6510. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6511. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6512. tmp &= ~0xFF;
  6513. tmp |= 0x1C;
  6514. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6515. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6516. tmp &= ~(0xFF << 16);
  6517. tmp |= (0x1C << 16);
  6518. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6519. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6520. tmp &= ~(0xFF << 16);
  6521. tmp |= (0x1C << 16);
  6522. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6523. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6524. tmp |= (1 << 27);
  6525. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6526. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6527. tmp |= (1 << 27);
  6528. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6529. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6530. tmp &= ~(0xF << 28);
  6531. tmp |= (4 << 28);
  6532. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6533. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6534. tmp &= ~(0xF << 28);
  6535. tmp |= (4 << 28);
  6536. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6537. }
  6538. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6539. * Programming" based on the parameters passed:
  6540. * - Sequence to enable CLKOUT_DP
  6541. * - Sequence to enable CLKOUT_DP without spread
  6542. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6543. */
  6544. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6545. bool with_spread, bool with_fdi)
  6546. {
  6547. uint32_t reg, tmp;
  6548. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6549. with_spread = true;
  6550. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6551. with_fdi, "LP PCH doesn't have FDI\n"))
  6552. with_fdi = false;
  6553. mutex_lock(&dev_priv->sb_lock);
  6554. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6555. tmp &= ~SBI_SSCCTL_DISABLE;
  6556. tmp |= SBI_SSCCTL_PATHALT;
  6557. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6558. udelay(24);
  6559. if (with_spread) {
  6560. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6561. tmp &= ~SBI_SSCCTL_PATHALT;
  6562. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6563. if (with_fdi) {
  6564. lpt_reset_fdi_mphy(dev_priv);
  6565. lpt_program_fdi_mphy(dev_priv);
  6566. }
  6567. }
  6568. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6569. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6570. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6571. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6572. mutex_unlock(&dev_priv->sb_lock);
  6573. }
  6574. /* Sequence to disable CLKOUT_DP */
  6575. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6576. {
  6577. uint32_t reg, tmp;
  6578. mutex_lock(&dev_priv->sb_lock);
  6579. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6580. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6581. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6582. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6583. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6584. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6585. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6586. tmp |= SBI_SSCCTL_PATHALT;
  6587. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6588. udelay(32);
  6589. }
  6590. tmp |= SBI_SSCCTL_DISABLE;
  6591. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6592. }
  6593. mutex_unlock(&dev_priv->sb_lock);
  6594. }
  6595. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6596. static const uint16_t sscdivintphase[] = {
  6597. [BEND_IDX( 50)] = 0x3B23,
  6598. [BEND_IDX( 45)] = 0x3B23,
  6599. [BEND_IDX( 40)] = 0x3C23,
  6600. [BEND_IDX( 35)] = 0x3C23,
  6601. [BEND_IDX( 30)] = 0x3D23,
  6602. [BEND_IDX( 25)] = 0x3D23,
  6603. [BEND_IDX( 20)] = 0x3E23,
  6604. [BEND_IDX( 15)] = 0x3E23,
  6605. [BEND_IDX( 10)] = 0x3F23,
  6606. [BEND_IDX( 5)] = 0x3F23,
  6607. [BEND_IDX( 0)] = 0x0025,
  6608. [BEND_IDX( -5)] = 0x0025,
  6609. [BEND_IDX(-10)] = 0x0125,
  6610. [BEND_IDX(-15)] = 0x0125,
  6611. [BEND_IDX(-20)] = 0x0225,
  6612. [BEND_IDX(-25)] = 0x0225,
  6613. [BEND_IDX(-30)] = 0x0325,
  6614. [BEND_IDX(-35)] = 0x0325,
  6615. [BEND_IDX(-40)] = 0x0425,
  6616. [BEND_IDX(-45)] = 0x0425,
  6617. [BEND_IDX(-50)] = 0x0525,
  6618. };
  6619. /*
  6620. * Bend CLKOUT_DP
  6621. * steps -50 to 50 inclusive, in steps of 5
  6622. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6623. * change in clock period = -(steps / 10) * 5.787 ps
  6624. */
  6625. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6626. {
  6627. uint32_t tmp;
  6628. int idx = BEND_IDX(steps);
  6629. if (WARN_ON(steps % 5 != 0))
  6630. return;
  6631. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6632. return;
  6633. mutex_lock(&dev_priv->sb_lock);
  6634. if (steps % 10 != 0)
  6635. tmp = 0xAAAAAAAB;
  6636. else
  6637. tmp = 0x00000000;
  6638. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6639. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6640. tmp &= 0xffff0000;
  6641. tmp |= sscdivintphase[idx];
  6642. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6643. mutex_unlock(&dev_priv->sb_lock);
  6644. }
  6645. #undef BEND_IDX
  6646. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6647. {
  6648. struct intel_encoder *encoder;
  6649. bool has_vga = false;
  6650. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6651. switch (encoder->type) {
  6652. case INTEL_OUTPUT_ANALOG:
  6653. has_vga = true;
  6654. break;
  6655. default:
  6656. break;
  6657. }
  6658. }
  6659. if (has_vga) {
  6660. lpt_bend_clkout_dp(dev_priv, 0);
  6661. lpt_enable_clkout_dp(dev_priv, true, true);
  6662. } else {
  6663. lpt_disable_clkout_dp(dev_priv);
  6664. }
  6665. }
  6666. /*
  6667. * Initialize reference clocks when the driver loads
  6668. */
  6669. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6670. {
  6671. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6672. ironlake_init_pch_refclk(dev_priv);
  6673. else if (HAS_PCH_LPT(dev_priv))
  6674. lpt_init_pch_refclk(dev_priv);
  6675. }
  6676. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6677. {
  6678. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6679. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6680. int pipe = intel_crtc->pipe;
  6681. uint32_t val;
  6682. val = 0;
  6683. switch (intel_crtc->config->pipe_bpp) {
  6684. case 18:
  6685. val |= PIPECONF_6BPC;
  6686. break;
  6687. case 24:
  6688. val |= PIPECONF_8BPC;
  6689. break;
  6690. case 30:
  6691. val |= PIPECONF_10BPC;
  6692. break;
  6693. case 36:
  6694. val |= PIPECONF_12BPC;
  6695. break;
  6696. default:
  6697. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6698. BUG();
  6699. }
  6700. if (intel_crtc->config->dither)
  6701. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6702. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6703. val |= PIPECONF_INTERLACED_ILK;
  6704. else
  6705. val |= PIPECONF_PROGRESSIVE;
  6706. if (intel_crtc->config->limited_color_range)
  6707. val |= PIPECONF_COLOR_RANGE_SELECT;
  6708. I915_WRITE(PIPECONF(pipe), val);
  6709. POSTING_READ(PIPECONF(pipe));
  6710. }
  6711. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6712. {
  6713. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6714. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6715. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6716. u32 val = 0;
  6717. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6718. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6719. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6720. val |= PIPECONF_INTERLACED_ILK;
  6721. else
  6722. val |= PIPECONF_PROGRESSIVE;
  6723. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6724. POSTING_READ(PIPECONF(cpu_transcoder));
  6725. }
  6726. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6727. {
  6728. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6729. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6730. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  6731. u32 val = 0;
  6732. switch (intel_crtc->config->pipe_bpp) {
  6733. case 18:
  6734. val |= PIPEMISC_DITHER_6_BPC;
  6735. break;
  6736. case 24:
  6737. val |= PIPEMISC_DITHER_8_BPC;
  6738. break;
  6739. case 30:
  6740. val |= PIPEMISC_DITHER_10_BPC;
  6741. break;
  6742. case 36:
  6743. val |= PIPEMISC_DITHER_12_BPC;
  6744. break;
  6745. default:
  6746. /* Case prevented by pipe_config_set_bpp. */
  6747. BUG();
  6748. }
  6749. if (intel_crtc->config->dither)
  6750. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6751. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6752. }
  6753. }
  6754. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6755. {
  6756. /*
  6757. * Account for spread spectrum to avoid
  6758. * oversubscribing the link. Max center spread
  6759. * is 2.5%; use 5% for safety's sake.
  6760. */
  6761. u32 bps = target_clock * bpp * 21 / 20;
  6762. return DIV_ROUND_UP(bps, link_bw * 8);
  6763. }
  6764. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6765. {
  6766. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6767. }
  6768. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6769. struct intel_crtc_state *crtc_state,
  6770. struct dpll *reduced_clock)
  6771. {
  6772. struct drm_crtc *crtc = &intel_crtc->base;
  6773. struct drm_device *dev = crtc->dev;
  6774. struct drm_i915_private *dev_priv = to_i915(dev);
  6775. u32 dpll, fp, fp2;
  6776. int factor;
  6777. /* Enable autotuning of the PLL clock (if permissible) */
  6778. factor = 21;
  6779. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6780. if ((intel_panel_use_ssc(dev_priv) &&
  6781. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6782. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6783. factor = 25;
  6784. } else if (crtc_state->sdvo_tv_clock)
  6785. factor = 20;
  6786. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6787. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6788. fp |= FP_CB_TUNE;
  6789. if (reduced_clock) {
  6790. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6791. if (reduced_clock->m < factor * reduced_clock->n)
  6792. fp2 |= FP_CB_TUNE;
  6793. } else {
  6794. fp2 = fp;
  6795. }
  6796. dpll = 0;
  6797. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6798. dpll |= DPLLB_MODE_LVDS;
  6799. else
  6800. dpll |= DPLLB_MODE_DAC_SERIAL;
  6801. dpll |= (crtc_state->pixel_multiplier - 1)
  6802. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6803. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6804. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6805. dpll |= DPLL_SDVO_HIGH_SPEED;
  6806. if (intel_crtc_has_dp_encoder(crtc_state))
  6807. dpll |= DPLL_SDVO_HIGH_SPEED;
  6808. /*
  6809. * The high speed IO clock is only really required for
  6810. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6811. * possible to share the DPLL between CRT and HDMI. Enabling
  6812. * the clock needlessly does no real harm, except use up a
  6813. * bit of power potentially.
  6814. *
  6815. * We'll limit this to IVB with 3 pipes, since it has only two
  6816. * DPLLs and so DPLL sharing is the only way to get three pipes
  6817. * driving PCH ports at the same time. On SNB we could do this,
  6818. * and potentially avoid enabling the second DPLL, but it's not
  6819. * clear if it''s a win or loss power wise. No point in doing
  6820. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  6821. */
  6822. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  6823. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  6824. dpll |= DPLL_SDVO_HIGH_SPEED;
  6825. /* compute bitmask from p1 value */
  6826. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6827. /* also FPA1 */
  6828. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6829. switch (crtc_state->dpll.p2) {
  6830. case 5:
  6831. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6832. break;
  6833. case 7:
  6834. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6835. break;
  6836. case 10:
  6837. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6838. break;
  6839. case 14:
  6840. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6841. break;
  6842. }
  6843. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6844. intel_panel_use_ssc(dev_priv))
  6845. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6846. else
  6847. dpll |= PLL_REF_INPUT_DREFCLK;
  6848. dpll |= DPLL_VCO_ENABLE;
  6849. crtc_state->dpll_hw_state.dpll = dpll;
  6850. crtc_state->dpll_hw_state.fp0 = fp;
  6851. crtc_state->dpll_hw_state.fp1 = fp2;
  6852. }
  6853. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6854. struct intel_crtc_state *crtc_state)
  6855. {
  6856. struct drm_device *dev = crtc->base.dev;
  6857. struct drm_i915_private *dev_priv = to_i915(dev);
  6858. const struct intel_limit *limit;
  6859. int refclk = 120000;
  6860. memset(&crtc_state->dpll_hw_state, 0,
  6861. sizeof(crtc_state->dpll_hw_state));
  6862. crtc->lowfreq_avail = false;
  6863. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6864. if (!crtc_state->has_pch_encoder)
  6865. return 0;
  6866. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6867. if (intel_panel_use_ssc(dev_priv)) {
  6868. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6869. dev_priv->vbt.lvds_ssc_freq);
  6870. refclk = dev_priv->vbt.lvds_ssc_freq;
  6871. }
  6872. if (intel_is_dual_link_lvds(dev)) {
  6873. if (refclk == 100000)
  6874. limit = &intel_limits_ironlake_dual_lvds_100m;
  6875. else
  6876. limit = &intel_limits_ironlake_dual_lvds;
  6877. } else {
  6878. if (refclk == 100000)
  6879. limit = &intel_limits_ironlake_single_lvds_100m;
  6880. else
  6881. limit = &intel_limits_ironlake_single_lvds;
  6882. }
  6883. } else {
  6884. limit = &intel_limits_ironlake_dac;
  6885. }
  6886. if (!crtc_state->clock_set &&
  6887. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6888. refclk, NULL, &crtc_state->dpll)) {
  6889. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6890. return -EINVAL;
  6891. }
  6892. ironlake_compute_dpll(crtc, crtc_state, NULL);
  6893. if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
  6894. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6895. pipe_name(crtc->pipe));
  6896. return -EINVAL;
  6897. }
  6898. return 0;
  6899. }
  6900. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6901. struct intel_link_m_n *m_n)
  6902. {
  6903. struct drm_device *dev = crtc->base.dev;
  6904. struct drm_i915_private *dev_priv = to_i915(dev);
  6905. enum pipe pipe = crtc->pipe;
  6906. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6907. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6908. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6909. & ~TU_SIZE_MASK;
  6910. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6911. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6912. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6913. }
  6914. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6915. enum transcoder transcoder,
  6916. struct intel_link_m_n *m_n,
  6917. struct intel_link_m_n *m2_n2)
  6918. {
  6919. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6920. enum pipe pipe = crtc->pipe;
  6921. if (INTEL_GEN(dev_priv) >= 5) {
  6922. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6923. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6924. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6925. & ~TU_SIZE_MASK;
  6926. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6927. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6928. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6929. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6930. * gen < 8) and if DRRS is supported (to make sure the
  6931. * registers are not unnecessarily read).
  6932. */
  6933. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  6934. crtc->config->has_drrs) {
  6935. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6936. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6937. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6938. & ~TU_SIZE_MASK;
  6939. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6940. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6941. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6942. }
  6943. } else {
  6944. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6945. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6946. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6947. & ~TU_SIZE_MASK;
  6948. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6949. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6950. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6951. }
  6952. }
  6953. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6954. struct intel_crtc_state *pipe_config)
  6955. {
  6956. if (pipe_config->has_pch_encoder)
  6957. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6958. else
  6959. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6960. &pipe_config->dp_m_n,
  6961. &pipe_config->dp_m2_n2);
  6962. }
  6963. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6964. struct intel_crtc_state *pipe_config)
  6965. {
  6966. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6967. &pipe_config->fdi_m_n, NULL);
  6968. }
  6969. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6970. struct intel_crtc_state *pipe_config)
  6971. {
  6972. struct drm_device *dev = crtc->base.dev;
  6973. struct drm_i915_private *dev_priv = to_i915(dev);
  6974. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  6975. uint32_t ps_ctrl = 0;
  6976. int id = -1;
  6977. int i;
  6978. /* find scaler attached to this pipe */
  6979. for (i = 0; i < crtc->num_scalers; i++) {
  6980. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  6981. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  6982. id = i;
  6983. pipe_config->pch_pfit.enabled = true;
  6984. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  6985. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  6986. break;
  6987. }
  6988. }
  6989. scaler_state->scaler_id = id;
  6990. if (id >= 0) {
  6991. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  6992. } else {
  6993. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  6994. }
  6995. }
  6996. static void
  6997. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  6998. struct intel_initial_plane_config *plane_config)
  6999. {
  7000. struct drm_device *dev = crtc->base.dev;
  7001. struct drm_i915_private *dev_priv = to_i915(dev);
  7002. u32 val, base, offset, stride_mult, tiling;
  7003. int pipe = crtc->pipe;
  7004. int fourcc, pixel_format;
  7005. unsigned int aligned_height;
  7006. struct drm_framebuffer *fb;
  7007. struct intel_framebuffer *intel_fb;
  7008. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7009. if (!intel_fb) {
  7010. DRM_DEBUG_KMS("failed to alloc fb\n");
  7011. return;
  7012. }
  7013. fb = &intel_fb->base;
  7014. fb->dev = dev;
  7015. val = I915_READ(PLANE_CTL(pipe, 0));
  7016. if (!(val & PLANE_CTL_ENABLE))
  7017. goto error;
  7018. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7019. fourcc = skl_format_to_fourcc(pixel_format,
  7020. val & PLANE_CTL_ORDER_RGBX,
  7021. val & PLANE_CTL_ALPHA_MASK);
  7022. fb->format = drm_format_info(fourcc);
  7023. tiling = val & PLANE_CTL_TILED_MASK;
  7024. switch (tiling) {
  7025. case PLANE_CTL_TILED_LINEAR:
  7026. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7027. break;
  7028. case PLANE_CTL_TILED_X:
  7029. plane_config->tiling = I915_TILING_X;
  7030. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7031. break;
  7032. case PLANE_CTL_TILED_Y:
  7033. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7034. break;
  7035. case PLANE_CTL_TILED_YF:
  7036. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7037. break;
  7038. default:
  7039. MISSING_CASE(tiling);
  7040. goto error;
  7041. }
  7042. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7043. plane_config->base = base;
  7044. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7045. val = I915_READ(PLANE_SIZE(pipe, 0));
  7046. fb->height = ((val >> 16) & 0xfff) + 1;
  7047. fb->width = ((val >> 0) & 0x1fff) + 1;
  7048. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7049. stride_mult = intel_fb_stride_alignment(fb, 0);
  7050. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7051. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7052. plane_config->size = fb->pitches[0] * aligned_height;
  7053. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7054. pipe_name(pipe), fb->width, fb->height,
  7055. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7056. plane_config->size);
  7057. plane_config->fb = intel_fb;
  7058. return;
  7059. error:
  7060. kfree(intel_fb);
  7061. }
  7062. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7063. struct intel_crtc_state *pipe_config)
  7064. {
  7065. struct drm_device *dev = crtc->base.dev;
  7066. struct drm_i915_private *dev_priv = to_i915(dev);
  7067. uint32_t tmp;
  7068. tmp = I915_READ(PF_CTL(crtc->pipe));
  7069. if (tmp & PF_ENABLE) {
  7070. pipe_config->pch_pfit.enabled = true;
  7071. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7072. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7073. /* We currently do not free assignements of panel fitters on
  7074. * ivb/hsw (since we don't use the higher upscaling modes which
  7075. * differentiates them) so just WARN about this case for now. */
  7076. if (IS_GEN7(dev_priv)) {
  7077. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7078. PF_PIPE_SEL_IVB(crtc->pipe));
  7079. }
  7080. }
  7081. }
  7082. static void
  7083. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7084. struct intel_initial_plane_config *plane_config)
  7085. {
  7086. struct drm_device *dev = crtc->base.dev;
  7087. struct drm_i915_private *dev_priv = to_i915(dev);
  7088. u32 val, base, offset;
  7089. int pipe = crtc->pipe;
  7090. int fourcc, pixel_format;
  7091. unsigned int aligned_height;
  7092. struct drm_framebuffer *fb;
  7093. struct intel_framebuffer *intel_fb;
  7094. val = I915_READ(DSPCNTR(pipe));
  7095. if (!(val & DISPLAY_PLANE_ENABLE))
  7096. return;
  7097. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7098. if (!intel_fb) {
  7099. DRM_DEBUG_KMS("failed to alloc fb\n");
  7100. return;
  7101. }
  7102. fb = &intel_fb->base;
  7103. fb->dev = dev;
  7104. if (INTEL_GEN(dev_priv) >= 4) {
  7105. if (val & DISPPLANE_TILED) {
  7106. plane_config->tiling = I915_TILING_X;
  7107. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7108. }
  7109. }
  7110. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7111. fourcc = i9xx_format_to_fourcc(pixel_format);
  7112. fb->format = drm_format_info(fourcc);
  7113. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7114. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  7115. offset = I915_READ(DSPOFFSET(pipe));
  7116. } else {
  7117. if (plane_config->tiling)
  7118. offset = I915_READ(DSPTILEOFF(pipe));
  7119. else
  7120. offset = I915_READ(DSPLINOFF(pipe));
  7121. }
  7122. plane_config->base = base;
  7123. val = I915_READ(PIPESRC(pipe));
  7124. fb->width = ((val >> 16) & 0xfff) + 1;
  7125. fb->height = ((val >> 0) & 0xfff) + 1;
  7126. val = I915_READ(DSPSTRIDE(pipe));
  7127. fb->pitches[0] = val & 0xffffffc0;
  7128. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7129. plane_config->size = fb->pitches[0] * aligned_height;
  7130. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7131. pipe_name(pipe), fb->width, fb->height,
  7132. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7133. plane_config->size);
  7134. plane_config->fb = intel_fb;
  7135. }
  7136. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7137. struct intel_crtc_state *pipe_config)
  7138. {
  7139. struct drm_device *dev = crtc->base.dev;
  7140. struct drm_i915_private *dev_priv = to_i915(dev);
  7141. enum intel_display_power_domain power_domain;
  7142. uint32_t tmp;
  7143. bool ret;
  7144. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7145. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7146. return false;
  7147. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7148. pipe_config->shared_dpll = NULL;
  7149. ret = false;
  7150. tmp = I915_READ(PIPECONF(crtc->pipe));
  7151. if (!(tmp & PIPECONF_ENABLE))
  7152. goto out;
  7153. switch (tmp & PIPECONF_BPC_MASK) {
  7154. case PIPECONF_6BPC:
  7155. pipe_config->pipe_bpp = 18;
  7156. break;
  7157. case PIPECONF_8BPC:
  7158. pipe_config->pipe_bpp = 24;
  7159. break;
  7160. case PIPECONF_10BPC:
  7161. pipe_config->pipe_bpp = 30;
  7162. break;
  7163. case PIPECONF_12BPC:
  7164. pipe_config->pipe_bpp = 36;
  7165. break;
  7166. default:
  7167. break;
  7168. }
  7169. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7170. pipe_config->limited_color_range = true;
  7171. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7172. struct intel_shared_dpll *pll;
  7173. enum intel_dpll_id pll_id;
  7174. pipe_config->has_pch_encoder = true;
  7175. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7176. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7177. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7178. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7179. if (HAS_PCH_IBX(dev_priv)) {
  7180. /*
  7181. * The pipe->pch transcoder and pch transcoder->pll
  7182. * mapping is fixed.
  7183. */
  7184. pll_id = (enum intel_dpll_id) crtc->pipe;
  7185. } else {
  7186. tmp = I915_READ(PCH_DPLL_SEL);
  7187. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7188. pll_id = DPLL_ID_PCH_PLL_B;
  7189. else
  7190. pll_id= DPLL_ID_PCH_PLL_A;
  7191. }
  7192. pipe_config->shared_dpll =
  7193. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7194. pll = pipe_config->shared_dpll;
  7195. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7196. &pipe_config->dpll_hw_state));
  7197. tmp = pipe_config->dpll_hw_state.dpll;
  7198. pipe_config->pixel_multiplier =
  7199. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7200. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7201. ironlake_pch_clock_get(crtc, pipe_config);
  7202. } else {
  7203. pipe_config->pixel_multiplier = 1;
  7204. }
  7205. intel_get_pipe_timings(crtc, pipe_config);
  7206. intel_get_pipe_src_size(crtc, pipe_config);
  7207. ironlake_get_pfit_config(crtc, pipe_config);
  7208. ret = true;
  7209. out:
  7210. intel_display_power_put(dev_priv, power_domain);
  7211. return ret;
  7212. }
  7213. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7214. {
  7215. struct drm_device *dev = &dev_priv->drm;
  7216. struct intel_crtc *crtc;
  7217. for_each_intel_crtc(dev, crtc)
  7218. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7219. pipe_name(crtc->pipe));
  7220. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7221. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7222. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7223. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7224. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7225. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7226. "CPU PWM1 enabled\n");
  7227. if (IS_HASWELL(dev_priv))
  7228. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7229. "CPU PWM2 enabled\n");
  7230. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7231. "PCH PWM1 enabled\n");
  7232. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7233. "Utility pin enabled\n");
  7234. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7235. /*
  7236. * In theory we can still leave IRQs enabled, as long as only the HPD
  7237. * interrupts remain enabled. We used to check for that, but since it's
  7238. * gen-specific and since we only disable LCPLL after we fully disable
  7239. * the interrupts, the check below should be enough.
  7240. */
  7241. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7242. }
  7243. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7244. {
  7245. if (IS_HASWELL(dev_priv))
  7246. return I915_READ(D_COMP_HSW);
  7247. else
  7248. return I915_READ(D_COMP_BDW);
  7249. }
  7250. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7251. {
  7252. if (IS_HASWELL(dev_priv)) {
  7253. mutex_lock(&dev_priv->rps.hw_lock);
  7254. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7255. val))
  7256. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7257. mutex_unlock(&dev_priv->rps.hw_lock);
  7258. } else {
  7259. I915_WRITE(D_COMP_BDW, val);
  7260. POSTING_READ(D_COMP_BDW);
  7261. }
  7262. }
  7263. /*
  7264. * This function implements pieces of two sequences from BSpec:
  7265. * - Sequence for display software to disable LCPLL
  7266. * - Sequence for display software to allow package C8+
  7267. * The steps implemented here are just the steps that actually touch the LCPLL
  7268. * register. Callers should take care of disabling all the display engine
  7269. * functions, doing the mode unset, fixing interrupts, etc.
  7270. */
  7271. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7272. bool switch_to_fclk, bool allow_power_down)
  7273. {
  7274. uint32_t val;
  7275. assert_can_disable_lcpll(dev_priv);
  7276. val = I915_READ(LCPLL_CTL);
  7277. if (switch_to_fclk) {
  7278. val |= LCPLL_CD_SOURCE_FCLK;
  7279. I915_WRITE(LCPLL_CTL, val);
  7280. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7281. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7282. DRM_ERROR("Switching to FCLK failed\n");
  7283. val = I915_READ(LCPLL_CTL);
  7284. }
  7285. val |= LCPLL_PLL_DISABLE;
  7286. I915_WRITE(LCPLL_CTL, val);
  7287. POSTING_READ(LCPLL_CTL);
  7288. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7289. DRM_ERROR("LCPLL still locked\n");
  7290. val = hsw_read_dcomp(dev_priv);
  7291. val |= D_COMP_COMP_DISABLE;
  7292. hsw_write_dcomp(dev_priv, val);
  7293. ndelay(100);
  7294. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7295. 1))
  7296. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7297. if (allow_power_down) {
  7298. val = I915_READ(LCPLL_CTL);
  7299. val |= LCPLL_POWER_DOWN_ALLOW;
  7300. I915_WRITE(LCPLL_CTL, val);
  7301. POSTING_READ(LCPLL_CTL);
  7302. }
  7303. }
  7304. /*
  7305. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7306. * source.
  7307. */
  7308. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7309. {
  7310. uint32_t val;
  7311. val = I915_READ(LCPLL_CTL);
  7312. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7313. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7314. return;
  7315. /*
  7316. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7317. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7318. */
  7319. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7320. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7321. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7322. I915_WRITE(LCPLL_CTL, val);
  7323. POSTING_READ(LCPLL_CTL);
  7324. }
  7325. val = hsw_read_dcomp(dev_priv);
  7326. val |= D_COMP_COMP_FORCE;
  7327. val &= ~D_COMP_COMP_DISABLE;
  7328. hsw_write_dcomp(dev_priv, val);
  7329. val = I915_READ(LCPLL_CTL);
  7330. val &= ~LCPLL_PLL_DISABLE;
  7331. I915_WRITE(LCPLL_CTL, val);
  7332. if (intel_wait_for_register(dev_priv,
  7333. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7334. 5))
  7335. DRM_ERROR("LCPLL not locked yet\n");
  7336. if (val & LCPLL_CD_SOURCE_FCLK) {
  7337. val = I915_READ(LCPLL_CTL);
  7338. val &= ~LCPLL_CD_SOURCE_FCLK;
  7339. I915_WRITE(LCPLL_CTL, val);
  7340. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7341. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7342. DRM_ERROR("Switching back to LCPLL failed\n");
  7343. }
  7344. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7345. intel_update_cdclk(dev_priv);
  7346. }
  7347. /*
  7348. * Package states C8 and deeper are really deep PC states that can only be
  7349. * reached when all the devices on the system allow it, so even if the graphics
  7350. * device allows PC8+, it doesn't mean the system will actually get to these
  7351. * states. Our driver only allows PC8+ when going into runtime PM.
  7352. *
  7353. * The requirements for PC8+ are that all the outputs are disabled, the power
  7354. * well is disabled and most interrupts are disabled, and these are also
  7355. * requirements for runtime PM. When these conditions are met, we manually do
  7356. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7357. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7358. * hang the machine.
  7359. *
  7360. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7361. * the state of some registers, so when we come back from PC8+ we need to
  7362. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7363. * need to take care of the registers kept by RC6. Notice that this happens even
  7364. * if we don't put the device in PCI D3 state (which is what currently happens
  7365. * because of the runtime PM support).
  7366. *
  7367. * For more, read "Display Sequences for Package C8" on the hardware
  7368. * documentation.
  7369. */
  7370. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7371. {
  7372. uint32_t val;
  7373. DRM_DEBUG_KMS("Enabling package C8+\n");
  7374. if (HAS_PCH_LPT_LP(dev_priv)) {
  7375. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7376. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7377. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7378. }
  7379. lpt_disable_clkout_dp(dev_priv);
  7380. hsw_disable_lcpll(dev_priv, true, true);
  7381. }
  7382. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7383. {
  7384. uint32_t val;
  7385. DRM_DEBUG_KMS("Disabling package C8+\n");
  7386. hsw_restore_lcpll(dev_priv);
  7387. lpt_init_pch_refclk(dev_priv);
  7388. if (HAS_PCH_LPT_LP(dev_priv)) {
  7389. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7390. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7391. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7392. }
  7393. }
  7394. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7395. struct intel_crtc_state *crtc_state)
  7396. {
  7397. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7398. struct intel_encoder *encoder =
  7399. intel_ddi_get_crtc_new_encoder(crtc_state);
  7400. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7401. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7402. pipe_name(crtc->pipe));
  7403. return -EINVAL;
  7404. }
  7405. }
  7406. crtc->lowfreq_avail = false;
  7407. return 0;
  7408. }
  7409. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7410. enum port port,
  7411. struct intel_crtc_state *pipe_config)
  7412. {
  7413. enum intel_dpll_id id;
  7414. switch (port) {
  7415. case PORT_A:
  7416. id = DPLL_ID_SKL_DPLL0;
  7417. break;
  7418. case PORT_B:
  7419. id = DPLL_ID_SKL_DPLL1;
  7420. break;
  7421. case PORT_C:
  7422. id = DPLL_ID_SKL_DPLL2;
  7423. break;
  7424. default:
  7425. DRM_ERROR("Incorrect port type\n");
  7426. return;
  7427. }
  7428. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7429. }
  7430. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7431. enum port port,
  7432. struct intel_crtc_state *pipe_config)
  7433. {
  7434. enum intel_dpll_id id;
  7435. u32 temp;
  7436. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7437. id = temp >> (port * 3 + 1);
  7438. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7439. return;
  7440. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7441. }
  7442. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7443. enum port port,
  7444. struct intel_crtc_state *pipe_config)
  7445. {
  7446. enum intel_dpll_id id;
  7447. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7448. switch (ddi_pll_sel) {
  7449. case PORT_CLK_SEL_WRPLL1:
  7450. id = DPLL_ID_WRPLL1;
  7451. break;
  7452. case PORT_CLK_SEL_WRPLL2:
  7453. id = DPLL_ID_WRPLL2;
  7454. break;
  7455. case PORT_CLK_SEL_SPLL:
  7456. id = DPLL_ID_SPLL;
  7457. break;
  7458. case PORT_CLK_SEL_LCPLL_810:
  7459. id = DPLL_ID_LCPLL_810;
  7460. break;
  7461. case PORT_CLK_SEL_LCPLL_1350:
  7462. id = DPLL_ID_LCPLL_1350;
  7463. break;
  7464. case PORT_CLK_SEL_LCPLL_2700:
  7465. id = DPLL_ID_LCPLL_2700;
  7466. break;
  7467. default:
  7468. MISSING_CASE(ddi_pll_sel);
  7469. /* fall through */
  7470. case PORT_CLK_SEL_NONE:
  7471. return;
  7472. }
  7473. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7474. }
  7475. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7476. struct intel_crtc_state *pipe_config,
  7477. u64 *power_domain_mask)
  7478. {
  7479. struct drm_device *dev = crtc->base.dev;
  7480. struct drm_i915_private *dev_priv = to_i915(dev);
  7481. enum intel_display_power_domain power_domain;
  7482. u32 tmp;
  7483. /*
  7484. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7485. * transcoder handled below.
  7486. */
  7487. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7488. /*
  7489. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7490. * consistency and less surprising code; it's in always on power).
  7491. */
  7492. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7493. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7494. enum pipe trans_edp_pipe;
  7495. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7496. default:
  7497. WARN(1, "unknown pipe linked to edp transcoder\n");
  7498. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7499. case TRANS_DDI_EDP_INPUT_A_ON:
  7500. trans_edp_pipe = PIPE_A;
  7501. break;
  7502. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7503. trans_edp_pipe = PIPE_B;
  7504. break;
  7505. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7506. trans_edp_pipe = PIPE_C;
  7507. break;
  7508. }
  7509. if (trans_edp_pipe == crtc->pipe)
  7510. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7511. }
  7512. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7513. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7514. return false;
  7515. *power_domain_mask |= BIT_ULL(power_domain);
  7516. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7517. return tmp & PIPECONF_ENABLE;
  7518. }
  7519. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7520. struct intel_crtc_state *pipe_config,
  7521. u64 *power_domain_mask)
  7522. {
  7523. struct drm_device *dev = crtc->base.dev;
  7524. struct drm_i915_private *dev_priv = to_i915(dev);
  7525. enum intel_display_power_domain power_domain;
  7526. enum port port;
  7527. enum transcoder cpu_transcoder;
  7528. u32 tmp;
  7529. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7530. if (port == PORT_A)
  7531. cpu_transcoder = TRANSCODER_DSI_A;
  7532. else
  7533. cpu_transcoder = TRANSCODER_DSI_C;
  7534. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7535. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7536. continue;
  7537. *power_domain_mask |= BIT_ULL(power_domain);
  7538. /*
  7539. * The PLL needs to be enabled with a valid divider
  7540. * configuration, otherwise accessing DSI registers will hang
  7541. * the machine. See BSpec North Display Engine
  7542. * registers/MIPI[BXT]. We can break out here early, since we
  7543. * need the same DSI PLL to be enabled for both DSI ports.
  7544. */
  7545. if (!intel_dsi_pll_is_enabled(dev_priv))
  7546. break;
  7547. /* XXX: this works for video mode only */
  7548. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7549. if (!(tmp & DPI_ENABLE))
  7550. continue;
  7551. tmp = I915_READ(MIPI_CTRL(port));
  7552. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7553. continue;
  7554. pipe_config->cpu_transcoder = cpu_transcoder;
  7555. break;
  7556. }
  7557. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7558. }
  7559. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7560. struct intel_crtc_state *pipe_config)
  7561. {
  7562. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7563. struct intel_shared_dpll *pll;
  7564. enum port port;
  7565. uint32_t tmp;
  7566. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7567. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7568. if (IS_GEN9_BC(dev_priv))
  7569. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7570. else if (IS_GEN9_LP(dev_priv))
  7571. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7572. else
  7573. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7574. pll = pipe_config->shared_dpll;
  7575. if (pll) {
  7576. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7577. &pipe_config->dpll_hw_state));
  7578. }
  7579. /*
  7580. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7581. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7582. * the PCH transcoder is on.
  7583. */
  7584. if (INTEL_GEN(dev_priv) < 9 &&
  7585. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7586. pipe_config->has_pch_encoder = true;
  7587. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7588. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7589. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7590. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7591. }
  7592. }
  7593. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7594. struct intel_crtc_state *pipe_config)
  7595. {
  7596. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7597. enum intel_display_power_domain power_domain;
  7598. u64 power_domain_mask;
  7599. bool active;
  7600. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7601. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7602. return false;
  7603. power_domain_mask = BIT_ULL(power_domain);
  7604. pipe_config->shared_dpll = NULL;
  7605. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7606. if (IS_GEN9_LP(dev_priv) &&
  7607. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7608. WARN_ON(active);
  7609. active = true;
  7610. }
  7611. if (!active)
  7612. goto out;
  7613. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7614. haswell_get_ddi_port_state(crtc, pipe_config);
  7615. intel_get_pipe_timings(crtc, pipe_config);
  7616. }
  7617. intel_get_pipe_src_size(crtc, pipe_config);
  7618. pipe_config->gamma_mode =
  7619. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7620. if (INTEL_GEN(dev_priv) >= 9) {
  7621. intel_crtc_init_scalers(crtc, pipe_config);
  7622. pipe_config->scaler_state.scaler_id = -1;
  7623. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7624. }
  7625. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7626. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7627. power_domain_mask |= BIT_ULL(power_domain);
  7628. if (INTEL_GEN(dev_priv) >= 9)
  7629. skylake_get_pfit_config(crtc, pipe_config);
  7630. else
  7631. ironlake_get_pfit_config(crtc, pipe_config);
  7632. }
  7633. if (IS_HASWELL(dev_priv))
  7634. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7635. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7636. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7637. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7638. pipe_config->pixel_multiplier =
  7639. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7640. } else {
  7641. pipe_config->pixel_multiplier = 1;
  7642. }
  7643. out:
  7644. for_each_power_domain(power_domain, power_domain_mask)
  7645. intel_display_power_put(dev_priv, power_domain);
  7646. return active;
  7647. }
  7648. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  7649. {
  7650. struct drm_i915_private *dev_priv =
  7651. to_i915(plane_state->base.plane->dev);
  7652. const struct drm_framebuffer *fb = plane_state->base.fb;
  7653. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  7654. u32 base;
  7655. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  7656. base = obj->phys_handle->busaddr;
  7657. else
  7658. base = intel_plane_ggtt_offset(plane_state);
  7659. base += plane_state->main.offset;
  7660. /* ILK+ do this automagically */
  7661. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7662. plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7663. base += (plane_state->base.crtc_h *
  7664. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  7665. return base;
  7666. }
  7667. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  7668. {
  7669. int x = plane_state->base.crtc_x;
  7670. int y = plane_state->base.crtc_y;
  7671. u32 pos = 0;
  7672. if (x < 0) {
  7673. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7674. x = -x;
  7675. }
  7676. pos |= x << CURSOR_X_SHIFT;
  7677. if (y < 0) {
  7678. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7679. y = -y;
  7680. }
  7681. pos |= y << CURSOR_Y_SHIFT;
  7682. return pos;
  7683. }
  7684. static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
  7685. {
  7686. const struct drm_mode_config *config =
  7687. &plane_state->base.plane->dev->mode_config;
  7688. int width = plane_state->base.crtc_w;
  7689. int height = plane_state->base.crtc_h;
  7690. return width > 0 && width <= config->cursor_width &&
  7691. height > 0 && height <= config->cursor_height;
  7692. }
  7693. static int intel_check_cursor(struct intel_crtc_state *crtc_state,
  7694. struct intel_plane_state *plane_state)
  7695. {
  7696. const struct drm_framebuffer *fb = plane_state->base.fb;
  7697. int src_x, src_y;
  7698. u32 offset;
  7699. int ret;
  7700. ret = drm_plane_helper_check_state(&plane_state->base,
  7701. &plane_state->clip,
  7702. DRM_PLANE_HELPER_NO_SCALING,
  7703. DRM_PLANE_HELPER_NO_SCALING,
  7704. true, true);
  7705. if (ret)
  7706. return ret;
  7707. if (!fb)
  7708. return 0;
  7709. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  7710. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  7711. return -EINVAL;
  7712. }
  7713. src_x = plane_state->base.src_x >> 16;
  7714. src_y = plane_state->base.src_y >> 16;
  7715. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  7716. offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
  7717. if (src_x != 0 || src_y != 0) {
  7718. DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
  7719. return -EINVAL;
  7720. }
  7721. plane_state->main.offset = offset;
  7722. return 0;
  7723. }
  7724. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7725. const struct intel_plane_state *plane_state)
  7726. {
  7727. const struct drm_framebuffer *fb = plane_state->base.fb;
  7728. return CURSOR_ENABLE |
  7729. CURSOR_GAMMA_ENABLE |
  7730. CURSOR_FORMAT_ARGB |
  7731. CURSOR_STRIDE(fb->pitches[0]);
  7732. }
  7733. static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
  7734. {
  7735. int width = plane_state->base.crtc_w;
  7736. /*
  7737. * 845g/865g are only limited by the width of their cursors,
  7738. * the height is arbitrary up to the precision of the register.
  7739. */
  7740. return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
  7741. }
  7742. static int i845_check_cursor(struct intel_plane *plane,
  7743. struct intel_crtc_state *crtc_state,
  7744. struct intel_plane_state *plane_state)
  7745. {
  7746. const struct drm_framebuffer *fb = plane_state->base.fb;
  7747. int ret;
  7748. ret = intel_check_cursor(crtc_state, plane_state);
  7749. if (ret)
  7750. return ret;
  7751. /* if we want to turn off the cursor ignore width and height */
  7752. if (!fb)
  7753. return 0;
  7754. /* Check for which cursor types we support */
  7755. if (!i845_cursor_size_ok(plane_state)) {
  7756. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  7757. plane_state->base.crtc_w,
  7758. plane_state->base.crtc_h);
  7759. return -EINVAL;
  7760. }
  7761. switch (fb->pitches[0]) {
  7762. case 256:
  7763. case 512:
  7764. case 1024:
  7765. case 2048:
  7766. break;
  7767. default:
  7768. DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
  7769. fb->pitches[0]);
  7770. return -EINVAL;
  7771. }
  7772. plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
  7773. return 0;
  7774. }
  7775. static void i845_update_cursor(struct intel_plane *plane,
  7776. const struct intel_crtc_state *crtc_state,
  7777. const struct intel_plane_state *plane_state)
  7778. {
  7779. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7780. u32 cntl = 0, base = 0, pos = 0, size = 0;
  7781. unsigned long irqflags;
  7782. if (plane_state && plane_state->base.visible) {
  7783. unsigned int width = plane_state->base.crtc_w;
  7784. unsigned int height = plane_state->base.crtc_h;
  7785. cntl = plane_state->ctl;
  7786. size = (height << 12) | width;
  7787. base = intel_cursor_base(plane_state);
  7788. pos = intel_cursor_position(plane_state);
  7789. }
  7790. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7791. /* On these chipsets we can only modify the base/size/stride
  7792. * whilst the cursor is disabled.
  7793. */
  7794. if (plane->cursor.base != base ||
  7795. plane->cursor.size != size ||
  7796. plane->cursor.cntl != cntl) {
  7797. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  7798. I915_WRITE_FW(CURBASE(PIPE_A), base);
  7799. I915_WRITE_FW(CURSIZE, size);
  7800. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7801. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  7802. plane->cursor.base = base;
  7803. plane->cursor.size = size;
  7804. plane->cursor.cntl = cntl;
  7805. } else {
  7806. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7807. }
  7808. POSTING_READ_FW(CURCNTR(PIPE_A));
  7809. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7810. }
  7811. static void i845_disable_cursor(struct intel_plane *plane,
  7812. struct intel_crtc *crtc)
  7813. {
  7814. i845_update_cursor(plane, NULL, NULL);
  7815. }
  7816. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7817. const struct intel_plane_state *plane_state)
  7818. {
  7819. struct drm_i915_private *dev_priv =
  7820. to_i915(plane_state->base.plane->dev);
  7821. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  7822. u32 cntl;
  7823. cntl = MCURSOR_GAMMA_ENABLE;
  7824. if (HAS_DDI(dev_priv))
  7825. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7826. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  7827. switch (plane_state->base.crtc_w) {
  7828. case 64:
  7829. cntl |= CURSOR_MODE_64_ARGB_AX;
  7830. break;
  7831. case 128:
  7832. cntl |= CURSOR_MODE_128_ARGB_AX;
  7833. break;
  7834. case 256:
  7835. cntl |= CURSOR_MODE_256_ARGB_AX;
  7836. break;
  7837. default:
  7838. MISSING_CASE(plane_state->base.crtc_w);
  7839. return 0;
  7840. }
  7841. if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7842. cntl |= CURSOR_ROTATE_180;
  7843. return cntl;
  7844. }
  7845. static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
  7846. {
  7847. struct drm_i915_private *dev_priv =
  7848. to_i915(plane_state->base.plane->dev);
  7849. int width = plane_state->base.crtc_w;
  7850. int height = plane_state->base.crtc_h;
  7851. if (!intel_cursor_size_ok(plane_state))
  7852. return false;
  7853. /* Cursor width is limited to a few power-of-two sizes */
  7854. switch (width) {
  7855. case 256:
  7856. case 128:
  7857. case 64:
  7858. break;
  7859. default:
  7860. return false;
  7861. }
  7862. /*
  7863. * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
  7864. * height from 8 lines up to the cursor width, when the
  7865. * cursor is not rotated. Everything else requires square
  7866. * cursors.
  7867. */
  7868. if (HAS_CUR_FBC(dev_priv) &&
  7869. plane_state->base.rotation & DRM_MODE_ROTATE_0) {
  7870. if (height < 8 || height > width)
  7871. return false;
  7872. } else {
  7873. if (height != width)
  7874. return false;
  7875. }
  7876. return true;
  7877. }
  7878. static int i9xx_check_cursor(struct intel_plane *plane,
  7879. struct intel_crtc_state *crtc_state,
  7880. struct intel_plane_state *plane_state)
  7881. {
  7882. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7883. const struct drm_framebuffer *fb = plane_state->base.fb;
  7884. enum pipe pipe = plane->pipe;
  7885. int ret;
  7886. ret = intel_check_cursor(crtc_state, plane_state);
  7887. if (ret)
  7888. return ret;
  7889. /* if we want to turn off the cursor ignore width and height */
  7890. if (!fb)
  7891. return 0;
  7892. /* Check for which cursor types we support */
  7893. if (!i9xx_cursor_size_ok(plane_state)) {
  7894. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  7895. plane_state->base.crtc_w,
  7896. plane_state->base.crtc_h);
  7897. return -EINVAL;
  7898. }
  7899. if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
  7900. DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
  7901. fb->pitches[0], plane_state->base.crtc_w);
  7902. return -EINVAL;
  7903. }
  7904. /*
  7905. * There's something wrong with the cursor on CHV pipe C.
  7906. * If it straddles the left edge of the screen then
  7907. * moving it away from the edge or disabling it often
  7908. * results in a pipe underrun, and often that can lead to
  7909. * dead pipe (constant underrun reported, and it scans
  7910. * out just a solid color). To recover from that, the
  7911. * display power well must be turned off and on again.
  7912. * Refuse the put the cursor into that compromised position.
  7913. */
  7914. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  7915. plane_state->base.visible && plane_state->base.crtc_x < 0) {
  7916. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  7917. return -EINVAL;
  7918. }
  7919. plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
  7920. return 0;
  7921. }
  7922. static void i9xx_update_cursor(struct intel_plane *plane,
  7923. const struct intel_crtc_state *crtc_state,
  7924. const struct intel_plane_state *plane_state)
  7925. {
  7926. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7927. enum pipe pipe = plane->pipe;
  7928. u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
  7929. unsigned long irqflags;
  7930. if (plane_state && plane_state->base.visible) {
  7931. cntl = plane_state->ctl;
  7932. if (plane_state->base.crtc_h != plane_state->base.crtc_w)
  7933. fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
  7934. base = intel_cursor_base(plane_state);
  7935. pos = intel_cursor_position(plane_state);
  7936. }
  7937. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7938. /*
  7939. * On some platforms writing CURCNTR first will also
  7940. * cause CURPOS to be armed by the CURBASE write.
  7941. * Without the CURCNTR write the CURPOS write would
  7942. * arm itself.
  7943. *
  7944. * CURCNTR and CUR_FBC_CTL are always
  7945. * armed by the CURBASE write only.
  7946. */
  7947. if (plane->cursor.base != base ||
  7948. plane->cursor.size != fbc_ctl ||
  7949. plane->cursor.cntl != cntl) {
  7950. I915_WRITE_FW(CURCNTR(pipe), cntl);
  7951. if (HAS_CUR_FBC(dev_priv))
  7952. I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
  7953. I915_WRITE_FW(CURPOS(pipe), pos);
  7954. I915_WRITE_FW(CURBASE(pipe), base);
  7955. plane->cursor.base = base;
  7956. plane->cursor.size = fbc_ctl;
  7957. plane->cursor.cntl = cntl;
  7958. } else {
  7959. I915_WRITE_FW(CURPOS(pipe), pos);
  7960. }
  7961. POSTING_READ_FW(CURBASE(pipe));
  7962. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7963. }
  7964. static void i9xx_disable_cursor(struct intel_plane *plane,
  7965. struct intel_crtc *crtc)
  7966. {
  7967. i9xx_update_cursor(plane, NULL, NULL);
  7968. }
  7969. /* VESA 640x480x72Hz mode to set on the pipe */
  7970. static struct drm_display_mode load_detect_mode = {
  7971. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7972. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7973. };
  7974. struct drm_framebuffer *
  7975. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  7976. struct drm_mode_fb_cmd2 *mode_cmd)
  7977. {
  7978. struct intel_framebuffer *intel_fb;
  7979. int ret;
  7980. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7981. if (!intel_fb)
  7982. return ERR_PTR(-ENOMEM);
  7983. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  7984. if (ret)
  7985. goto err;
  7986. return &intel_fb->base;
  7987. err:
  7988. kfree(intel_fb);
  7989. return ERR_PTR(ret);
  7990. }
  7991. static u32
  7992. intel_framebuffer_pitch_for_width(int width, int bpp)
  7993. {
  7994. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7995. return ALIGN(pitch, 64);
  7996. }
  7997. static u32
  7998. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7999. {
  8000. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8001. return PAGE_ALIGN(pitch * mode->vdisplay);
  8002. }
  8003. static struct drm_framebuffer *
  8004. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8005. struct drm_display_mode *mode,
  8006. int depth, int bpp)
  8007. {
  8008. struct drm_framebuffer *fb;
  8009. struct drm_i915_gem_object *obj;
  8010. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8011. obj = i915_gem_object_create(to_i915(dev),
  8012. intel_framebuffer_size_for_mode(mode, bpp));
  8013. if (IS_ERR(obj))
  8014. return ERR_CAST(obj);
  8015. mode_cmd.width = mode->hdisplay;
  8016. mode_cmd.height = mode->vdisplay;
  8017. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8018. bpp);
  8019. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8020. fb = intel_framebuffer_create(obj, &mode_cmd);
  8021. if (IS_ERR(fb))
  8022. i915_gem_object_put(obj);
  8023. return fb;
  8024. }
  8025. static struct drm_framebuffer *
  8026. mode_fits_in_fbdev(struct drm_device *dev,
  8027. struct drm_display_mode *mode)
  8028. {
  8029. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8030. struct drm_i915_private *dev_priv = to_i915(dev);
  8031. struct drm_i915_gem_object *obj;
  8032. struct drm_framebuffer *fb;
  8033. if (!dev_priv->fbdev)
  8034. return NULL;
  8035. if (!dev_priv->fbdev->fb)
  8036. return NULL;
  8037. obj = dev_priv->fbdev->fb->obj;
  8038. BUG_ON(!obj);
  8039. fb = &dev_priv->fbdev->fb->base;
  8040. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8041. fb->format->cpp[0] * 8))
  8042. return NULL;
  8043. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8044. return NULL;
  8045. drm_framebuffer_reference(fb);
  8046. return fb;
  8047. #else
  8048. return NULL;
  8049. #endif
  8050. }
  8051. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8052. struct drm_crtc *crtc,
  8053. struct drm_display_mode *mode,
  8054. struct drm_framebuffer *fb,
  8055. int x, int y)
  8056. {
  8057. struct drm_plane_state *plane_state;
  8058. int hdisplay, vdisplay;
  8059. int ret;
  8060. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8061. if (IS_ERR(plane_state))
  8062. return PTR_ERR(plane_state);
  8063. if (mode)
  8064. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  8065. else
  8066. hdisplay = vdisplay = 0;
  8067. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8068. if (ret)
  8069. return ret;
  8070. drm_atomic_set_fb_for_plane(plane_state, fb);
  8071. plane_state->crtc_x = 0;
  8072. plane_state->crtc_y = 0;
  8073. plane_state->crtc_w = hdisplay;
  8074. plane_state->crtc_h = vdisplay;
  8075. plane_state->src_x = x << 16;
  8076. plane_state->src_y = y << 16;
  8077. plane_state->src_w = hdisplay << 16;
  8078. plane_state->src_h = vdisplay << 16;
  8079. return 0;
  8080. }
  8081. int intel_get_load_detect_pipe(struct drm_connector *connector,
  8082. struct drm_display_mode *mode,
  8083. struct intel_load_detect_pipe *old,
  8084. struct drm_modeset_acquire_ctx *ctx)
  8085. {
  8086. struct intel_crtc *intel_crtc;
  8087. struct intel_encoder *intel_encoder =
  8088. intel_attached_encoder(connector);
  8089. struct drm_crtc *possible_crtc;
  8090. struct drm_encoder *encoder = &intel_encoder->base;
  8091. struct drm_crtc *crtc = NULL;
  8092. struct drm_device *dev = encoder->dev;
  8093. struct drm_i915_private *dev_priv = to_i915(dev);
  8094. struct drm_framebuffer *fb;
  8095. struct drm_mode_config *config = &dev->mode_config;
  8096. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8097. struct drm_connector_state *connector_state;
  8098. struct intel_crtc_state *crtc_state;
  8099. int ret, i = -1;
  8100. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8101. connector->base.id, connector->name,
  8102. encoder->base.id, encoder->name);
  8103. old->restore_state = NULL;
  8104. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  8105. /*
  8106. * Algorithm gets a little messy:
  8107. *
  8108. * - if the connector already has an assigned crtc, use it (but make
  8109. * sure it's on first)
  8110. *
  8111. * - try to find the first unused crtc that can drive this connector,
  8112. * and use that if we find one
  8113. */
  8114. /* See if we already have a CRTC for this connector */
  8115. if (connector->state->crtc) {
  8116. crtc = connector->state->crtc;
  8117. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8118. if (ret)
  8119. goto fail;
  8120. /* Make sure the crtc and connector are running */
  8121. goto found;
  8122. }
  8123. /* Find an unused one (if possible) */
  8124. for_each_crtc(dev, possible_crtc) {
  8125. i++;
  8126. if (!(encoder->possible_crtcs & (1 << i)))
  8127. continue;
  8128. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8129. if (ret)
  8130. goto fail;
  8131. if (possible_crtc->state->enable) {
  8132. drm_modeset_unlock(&possible_crtc->mutex);
  8133. continue;
  8134. }
  8135. crtc = possible_crtc;
  8136. break;
  8137. }
  8138. /*
  8139. * If we didn't find an unused CRTC, don't use any.
  8140. */
  8141. if (!crtc) {
  8142. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8143. ret = -ENODEV;
  8144. goto fail;
  8145. }
  8146. found:
  8147. intel_crtc = to_intel_crtc(crtc);
  8148. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8149. if (ret)
  8150. goto fail;
  8151. state = drm_atomic_state_alloc(dev);
  8152. restore_state = drm_atomic_state_alloc(dev);
  8153. if (!state || !restore_state) {
  8154. ret = -ENOMEM;
  8155. goto fail;
  8156. }
  8157. state->acquire_ctx = ctx;
  8158. restore_state->acquire_ctx = ctx;
  8159. connector_state = drm_atomic_get_connector_state(state, connector);
  8160. if (IS_ERR(connector_state)) {
  8161. ret = PTR_ERR(connector_state);
  8162. goto fail;
  8163. }
  8164. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8165. if (ret)
  8166. goto fail;
  8167. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8168. if (IS_ERR(crtc_state)) {
  8169. ret = PTR_ERR(crtc_state);
  8170. goto fail;
  8171. }
  8172. crtc_state->base.active = crtc_state->base.enable = true;
  8173. if (!mode)
  8174. mode = &load_detect_mode;
  8175. /* We need a framebuffer large enough to accommodate all accesses
  8176. * that the plane may generate whilst we perform load detection.
  8177. * We can not rely on the fbcon either being present (we get called
  8178. * during its initialisation to detect all boot displays, or it may
  8179. * not even exist) or that it is large enough to satisfy the
  8180. * requested mode.
  8181. */
  8182. fb = mode_fits_in_fbdev(dev, mode);
  8183. if (fb == NULL) {
  8184. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8185. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8186. } else
  8187. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8188. if (IS_ERR(fb)) {
  8189. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8190. ret = PTR_ERR(fb);
  8191. goto fail;
  8192. }
  8193. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8194. if (ret)
  8195. goto fail;
  8196. drm_framebuffer_unreference(fb);
  8197. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8198. if (ret)
  8199. goto fail;
  8200. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8201. if (!ret)
  8202. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8203. if (!ret)
  8204. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8205. if (ret) {
  8206. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8207. goto fail;
  8208. }
  8209. ret = drm_atomic_commit(state);
  8210. if (ret) {
  8211. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8212. goto fail;
  8213. }
  8214. old->restore_state = restore_state;
  8215. drm_atomic_state_put(state);
  8216. /* let the connector get through one full cycle before testing */
  8217. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8218. return true;
  8219. fail:
  8220. if (state) {
  8221. drm_atomic_state_put(state);
  8222. state = NULL;
  8223. }
  8224. if (restore_state) {
  8225. drm_atomic_state_put(restore_state);
  8226. restore_state = NULL;
  8227. }
  8228. if (ret == -EDEADLK)
  8229. return ret;
  8230. return false;
  8231. }
  8232. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8233. struct intel_load_detect_pipe *old,
  8234. struct drm_modeset_acquire_ctx *ctx)
  8235. {
  8236. struct intel_encoder *intel_encoder =
  8237. intel_attached_encoder(connector);
  8238. struct drm_encoder *encoder = &intel_encoder->base;
  8239. struct drm_atomic_state *state = old->restore_state;
  8240. int ret;
  8241. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8242. connector->base.id, connector->name,
  8243. encoder->base.id, encoder->name);
  8244. if (!state)
  8245. return;
  8246. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8247. if (ret)
  8248. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8249. drm_atomic_state_put(state);
  8250. }
  8251. static int i9xx_pll_refclk(struct drm_device *dev,
  8252. const struct intel_crtc_state *pipe_config)
  8253. {
  8254. struct drm_i915_private *dev_priv = to_i915(dev);
  8255. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8256. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8257. return dev_priv->vbt.lvds_ssc_freq;
  8258. else if (HAS_PCH_SPLIT(dev_priv))
  8259. return 120000;
  8260. else if (!IS_GEN2(dev_priv))
  8261. return 96000;
  8262. else
  8263. return 48000;
  8264. }
  8265. /* Returns the clock of the currently programmed mode of the given pipe. */
  8266. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8267. struct intel_crtc_state *pipe_config)
  8268. {
  8269. struct drm_device *dev = crtc->base.dev;
  8270. struct drm_i915_private *dev_priv = to_i915(dev);
  8271. int pipe = pipe_config->cpu_transcoder;
  8272. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8273. u32 fp;
  8274. struct dpll clock;
  8275. int port_clock;
  8276. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8277. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8278. fp = pipe_config->dpll_hw_state.fp0;
  8279. else
  8280. fp = pipe_config->dpll_hw_state.fp1;
  8281. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8282. if (IS_PINEVIEW(dev_priv)) {
  8283. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8284. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8285. } else {
  8286. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8287. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8288. }
  8289. if (!IS_GEN2(dev_priv)) {
  8290. if (IS_PINEVIEW(dev_priv))
  8291. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8292. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8293. else
  8294. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8295. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8296. switch (dpll & DPLL_MODE_MASK) {
  8297. case DPLLB_MODE_DAC_SERIAL:
  8298. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8299. 5 : 10;
  8300. break;
  8301. case DPLLB_MODE_LVDS:
  8302. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8303. 7 : 14;
  8304. break;
  8305. default:
  8306. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8307. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8308. return;
  8309. }
  8310. if (IS_PINEVIEW(dev_priv))
  8311. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8312. else
  8313. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8314. } else {
  8315. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8316. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8317. if (is_lvds) {
  8318. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8319. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8320. if (lvds & LVDS_CLKB_POWER_UP)
  8321. clock.p2 = 7;
  8322. else
  8323. clock.p2 = 14;
  8324. } else {
  8325. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8326. clock.p1 = 2;
  8327. else {
  8328. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8329. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8330. }
  8331. if (dpll & PLL_P2_DIVIDE_BY_4)
  8332. clock.p2 = 4;
  8333. else
  8334. clock.p2 = 2;
  8335. }
  8336. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8337. }
  8338. /*
  8339. * This value includes pixel_multiplier. We will use
  8340. * port_clock to compute adjusted_mode.crtc_clock in the
  8341. * encoder's get_config() function.
  8342. */
  8343. pipe_config->port_clock = port_clock;
  8344. }
  8345. int intel_dotclock_calculate(int link_freq,
  8346. const struct intel_link_m_n *m_n)
  8347. {
  8348. /*
  8349. * The calculation for the data clock is:
  8350. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8351. * But we want to avoid losing precison if possible, so:
  8352. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8353. *
  8354. * and the link clock is simpler:
  8355. * link_clock = (m * link_clock) / n
  8356. */
  8357. if (!m_n->link_n)
  8358. return 0;
  8359. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8360. }
  8361. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8362. struct intel_crtc_state *pipe_config)
  8363. {
  8364. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8365. /* read out port_clock from the DPLL */
  8366. i9xx_crtc_clock_get(crtc, pipe_config);
  8367. /*
  8368. * In case there is an active pipe without active ports,
  8369. * we may need some idea for the dotclock anyway.
  8370. * Calculate one based on the FDI configuration.
  8371. */
  8372. pipe_config->base.adjusted_mode.crtc_clock =
  8373. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8374. &pipe_config->fdi_m_n);
  8375. }
  8376. /** Returns the currently programmed mode of the given pipe. */
  8377. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8378. struct drm_crtc *crtc)
  8379. {
  8380. struct drm_i915_private *dev_priv = to_i915(dev);
  8381. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8382. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8383. struct drm_display_mode *mode;
  8384. struct intel_crtc_state *pipe_config;
  8385. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8386. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8387. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8388. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8389. enum pipe pipe = intel_crtc->pipe;
  8390. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8391. if (!mode)
  8392. return NULL;
  8393. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8394. if (!pipe_config) {
  8395. kfree(mode);
  8396. return NULL;
  8397. }
  8398. /*
  8399. * Construct a pipe_config sufficient for getting the clock info
  8400. * back out of crtc_clock_get.
  8401. *
  8402. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8403. * to use a real value here instead.
  8404. */
  8405. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  8406. pipe_config->pixel_multiplier = 1;
  8407. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8408. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8409. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8410. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  8411. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  8412. mode->hdisplay = (htot & 0xffff) + 1;
  8413. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8414. mode->hsync_start = (hsync & 0xffff) + 1;
  8415. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8416. mode->vdisplay = (vtot & 0xffff) + 1;
  8417. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8418. mode->vsync_start = (vsync & 0xffff) + 1;
  8419. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8420. drm_mode_set_name(mode);
  8421. kfree(pipe_config);
  8422. return mode;
  8423. }
  8424. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8425. {
  8426. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8427. struct drm_device *dev = crtc->dev;
  8428. struct intel_flip_work *work;
  8429. spin_lock_irq(&dev->event_lock);
  8430. work = intel_crtc->flip_work;
  8431. intel_crtc->flip_work = NULL;
  8432. spin_unlock_irq(&dev->event_lock);
  8433. if (work) {
  8434. cancel_work_sync(&work->mmio_work);
  8435. cancel_work_sync(&work->unpin_work);
  8436. kfree(work);
  8437. }
  8438. drm_crtc_cleanup(crtc);
  8439. kfree(intel_crtc);
  8440. }
  8441. static void intel_unpin_work_fn(struct work_struct *__work)
  8442. {
  8443. struct intel_flip_work *work =
  8444. container_of(__work, struct intel_flip_work, unpin_work);
  8445. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8446. struct drm_device *dev = crtc->base.dev;
  8447. struct drm_plane *primary = crtc->base.primary;
  8448. if (is_mmio_work(work))
  8449. flush_work(&work->mmio_work);
  8450. mutex_lock(&dev->struct_mutex);
  8451. intel_unpin_fb_vma(work->old_vma);
  8452. i915_gem_object_put(work->pending_flip_obj);
  8453. mutex_unlock(&dev->struct_mutex);
  8454. i915_gem_request_put(work->flip_queued_req);
  8455. intel_frontbuffer_flip_complete(to_i915(dev),
  8456. to_intel_plane(primary)->frontbuffer_bit);
  8457. intel_fbc_post_update(crtc);
  8458. drm_framebuffer_unreference(work->old_fb);
  8459. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8460. atomic_dec(&crtc->unpin_work_count);
  8461. kfree(work);
  8462. }
  8463. /* Is 'a' after or equal to 'b'? */
  8464. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8465. {
  8466. return !((a - b) & 0x80000000);
  8467. }
  8468. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  8469. struct intel_flip_work *work)
  8470. {
  8471. struct drm_device *dev = crtc->base.dev;
  8472. struct drm_i915_private *dev_priv = to_i915(dev);
  8473. if (abort_flip_on_reset(crtc))
  8474. return true;
  8475. /*
  8476. * The relevant registers doen't exist on pre-ctg.
  8477. * As the flip done interrupt doesn't trigger for mmio
  8478. * flips on gmch platforms, a flip count check isn't
  8479. * really needed there. But since ctg has the registers,
  8480. * include it in the check anyway.
  8481. */
  8482. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8483. return true;
  8484. /*
  8485. * BDW signals flip done immediately if the plane
  8486. * is disabled, even if the plane enable is already
  8487. * armed to occur at the next vblank :(
  8488. */
  8489. /*
  8490. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  8491. * used the same base address. In that case the mmio flip might
  8492. * have completed, but the CS hasn't even executed the flip yet.
  8493. *
  8494. * A flip count check isn't enough as the CS might have updated
  8495. * the base address just after start of vblank, but before we
  8496. * managed to process the interrupt. This means we'd complete the
  8497. * CS flip too soon.
  8498. *
  8499. * Combining both checks should get us a good enough result. It may
  8500. * still happen that the CS flip has been executed, but has not
  8501. * yet actually completed. But in case the base address is the same
  8502. * anyway, we don't really care.
  8503. */
  8504. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  8505. crtc->flip_work->gtt_offset &&
  8506. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  8507. crtc->flip_work->flip_count);
  8508. }
  8509. static bool
  8510. __pageflip_finished_mmio(struct intel_crtc *crtc,
  8511. struct intel_flip_work *work)
  8512. {
  8513. /*
  8514. * MMIO work completes when vblank is different from
  8515. * flip_queued_vblank.
  8516. *
  8517. * Reset counter value doesn't matter, this is handled by
  8518. * i915_wait_request finishing early, so no need to handle
  8519. * reset here.
  8520. */
  8521. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  8522. }
  8523. static bool pageflip_finished(struct intel_crtc *crtc,
  8524. struct intel_flip_work *work)
  8525. {
  8526. if (!atomic_read(&work->pending))
  8527. return false;
  8528. smp_rmb();
  8529. if (is_mmio_work(work))
  8530. return __pageflip_finished_mmio(crtc, work);
  8531. else
  8532. return __pageflip_finished_cs(crtc, work);
  8533. }
  8534. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  8535. {
  8536. struct drm_device *dev = &dev_priv->drm;
  8537. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8538. struct intel_flip_work *work;
  8539. unsigned long flags;
  8540. /* Ignore early vblank irqs */
  8541. if (!crtc)
  8542. return;
  8543. /*
  8544. * This is called both by irq handlers and the reset code (to complete
  8545. * lost pageflips) so needs the full irqsave spinlocks.
  8546. */
  8547. spin_lock_irqsave(&dev->event_lock, flags);
  8548. work = crtc->flip_work;
  8549. if (work != NULL &&
  8550. !is_mmio_work(work) &&
  8551. pageflip_finished(crtc, work))
  8552. page_flip_completed(crtc);
  8553. spin_unlock_irqrestore(&dev->event_lock, flags);
  8554. }
  8555. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  8556. {
  8557. struct drm_device *dev = &dev_priv->drm;
  8558. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8559. struct intel_flip_work *work;
  8560. unsigned long flags;
  8561. /* Ignore early vblank irqs */
  8562. if (!crtc)
  8563. return;
  8564. /*
  8565. * This is called both by irq handlers and the reset code (to complete
  8566. * lost pageflips) so needs the full irqsave spinlocks.
  8567. */
  8568. spin_lock_irqsave(&dev->event_lock, flags);
  8569. work = crtc->flip_work;
  8570. if (work != NULL &&
  8571. is_mmio_work(work) &&
  8572. pageflip_finished(crtc, work))
  8573. page_flip_completed(crtc);
  8574. spin_unlock_irqrestore(&dev->event_lock, flags);
  8575. }
  8576. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  8577. struct intel_flip_work *work)
  8578. {
  8579. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  8580. /* Ensure that the work item is consistent when activating it ... */
  8581. smp_mb__before_atomic();
  8582. atomic_set(&work->pending, 1);
  8583. }
  8584. static int intel_gen2_queue_flip(struct drm_device *dev,
  8585. struct drm_crtc *crtc,
  8586. struct drm_framebuffer *fb,
  8587. struct drm_i915_gem_object *obj,
  8588. struct drm_i915_gem_request *req,
  8589. uint32_t flags)
  8590. {
  8591. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8592. u32 flip_mask, *cs;
  8593. cs = intel_ring_begin(req, 6);
  8594. if (IS_ERR(cs))
  8595. return PTR_ERR(cs);
  8596. /* Can't queue multiple flips, so wait for the previous
  8597. * one to finish before executing the next.
  8598. */
  8599. if (intel_crtc->plane)
  8600. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8601. else
  8602. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8603. *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
  8604. *cs++ = MI_NOOP;
  8605. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8606. *cs++ = fb->pitches[0];
  8607. *cs++ = intel_crtc->flip_work->gtt_offset;
  8608. *cs++ = 0; /* aux display base address, unused */
  8609. return 0;
  8610. }
  8611. static int intel_gen3_queue_flip(struct drm_device *dev,
  8612. struct drm_crtc *crtc,
  8613. struct drm_framebuffer *fb,
  8614. struct drm_i915_gem_object *obj,
  8615. struct drm_i915_gem_request *req,
  8616. uint32_t flags)
  8617. {
  8618. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8619. u32 flip_mask, *cs;
  8620. cs = intel_ring_begin(req, 6);
  8621. if (IS_ERR(cs))
  8622. return PTR_ERR(cs);
  8623. if (intel_crtc->plane)
  8624. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8625. else
  8626. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8627. *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
  8628. *cs++ = MI_NOOP;
  8629. *cs++ = MI_DISPLAY_FLIP_I915 | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8630. *cs++ = fb->pitches[0];
  8631. *cs++ = intel_crtc->flip_work->gtt_offset;
  8632. *cs++ = MI_NOOP;
  8633. return 0;
  8634. }
  8635. static int intel_gen4_queue_flip(struct drm_device *dev,
  8636. struct drm_crtc *crtc,
  8637. struct drm_framebuffer *fb,
  8638. struct drm_i915_gem_object *obj,
  8639. struct drm_i915_gem_request *req,
  8640. uint32_t flags)
  8641. {
  8642. struct drm_i915_private *dev_priv = to_i915(dev);
  8643. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8644. u32 pf, pipesrc, *cs;
  8645. cs = intel_ring_begin(req, 4);
  8646. if (IS_ERR(cs))
  8647. return PTR_ERR(cs);
  8648. /* i965+ uses the linear or tiled offsets from the
  8649. * Display Registers (which do not change across a page-flip)
  8650. * so we need only reprogram the base address.
  8651. */
  8652. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8653. *cs++ = fb->pitches[0];
  8654. *cs++ = intel_crtc->flip_work->gtt_offset |
  8655. intel_fb_modifier_to_tiling(fb->modifier);
  8656. /* XXX Enabling the panel-fitter across page-flip is so far
  8657. * untested on non-native modes, so ignore it for now.
  8658. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  8659. */
  8660. pf = 0;
  8661. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8662. *cs++ = pf | pipesrc;
  8663. return 0;
  8664. }
  8665. static int intel_gen6_queue_flip(struct drm_device *dev,
  8666. struct drm_crtc *crtc,
  8667. struct drm_framebuffer *fb,
  8668. struct drm_i915_gem_object *obj,
  8669. struct drm_i915_gem_request *req,
  8670. uint32_t flags)
  8671. {
  8672. struct drm_i915_private *dev_priv = to_i915(dev);
  8673. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8674. u32 pf, pipesrc, *cs;
  8675. cs = intel_ring_begin(req, 4);
  8676. if (IS_ERR(cs))
  8677. return PTR_ERR(cs);
  8678. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8679. *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
  8680. *cs++ = intel_crtc->flip_work->gtt_offset;
  8681. /* Contrary to the suggestions in the documentation,
  8682. * "Enable Panel Fitter" does not seem to be required when page
  8683. * flipping with a non-native mode, and worse causes a normal
  8684. * modeset to fail.
  8685. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  8686. */
  8687. pf = 0;
  8688. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8689. *cs++ = pf | pipesrc;
  8690. return 0;
  8691. }
  8692. static int intel_gen7_queue_flip(struct drm_device *dev,
  8693. struct drm_crtc *crtc,
  8694. struct drm_framebuffer *fb,
  8695. struct drm_i915_gem_object *obj,
  8696. struct drm_i915_gem_request *req,
  8697. uint32_t flags)
  8698. {
  8699. struct drm_i915_private *dev_priv = to_i915(dev);
  8700. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8701. u32 *cs, plane_bit = 0;
  8702. int len, ret;
  8703. switch (intel_crtc->plane) {
  8704. case PLANE_A:
  8705. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  8706. break;
  8707. case PLANE_B:
  8708. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  8709. break;
  8710. case PLANE_C:
  8711. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  8712. break;
  8713. default:
  8714. WARN_ONCE(1, "unknown plane in flip command\n");
  8715. return -ENODEV;
  8716. }
  8717. len = 4;
  8718. if (req->engine->id == RCS) {
  8719. len += 6;
  8720. /*
  8721. * On Gen 8, SRM is now taking an extra dword to accommodate
  8722. * 48bits addresses, and we need a NOOP for the batch size to
  8723. * stay even.
  8724. */
  8725. if (IS_GEN8(dev_priv))
  8726. len += 2;
  8727. }
  8728. /*
  8729. * BSpec MI_DISPLAY_FLIP for IVB:
  8730. * "The full packet must be contained within the same cache line."
  8731. *
  8732. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8733. * cacheline, if we ever start emitting more commands before
  8734. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8735. * then do the cacheline alignment, and finally emit the
  8736. * MI_DISPLAY_FLIP.
  8737. */
  8738. ret = intel_ring_cacheline_align(req);
  8739. if (ret)
  8740. return ret;
  8741. cs = intel_ring_begin(req, len);
  8742. if (IS_ERR(cs))
  8743. return PTR_ERR(cs);
  8744. /* Unmask the flip-done completion message. Note that the bspec says that
  8745. * we should do this for both the BCS and RCS, and that we must not unmask
  8746. * more than one flip event at any time (or ensure that one flip message
  8747. * can be sent by waiting for flip-done prior to queueing new flips).
  8748. * Experimentation says that BCS works despite DERRMR masking all
  8749. * flip-done completion events and that unmasking all planes at once
  8750. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8751. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8752. */
  8753. if (req->engine->id == RCS) {
  8754. *cs++ = MI_LOAD_REGISTER_IMM(1);
  8755. *cs++ = i915_mmio_reg_offset(DERRMR);
  8756. *cs++ = ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8757. DERRMR_PIPEB_PRI_FLIP_DONE |
  8758. DERRMR_PIPEC_PRI_FLIP_DONE);
  8759. if (IS_GEN8(dev_priv))
  8760. *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
  8761. MI_SRM_LRM_GLOBAL_GTT;
  8762. else
  8763. *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
  8764. *cs++ = i915_mmio_reg_offset(DERRMR);
  8765. *cs++ = i915_ggtt_offset(req->engine->scratch) + 256;
  8766. if (IS_GEN8(dev_priv)) {
  8767. *cs++ = 0;
  8768. *cs++ = MI_NOOP;
  8769. }
  8770. }
  8771. *cs++ = MI_DISPLAY_FLIP_I915 | plane_bit;
  8772. *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
  8773. *cs++ = intel_crtc->flip_work->gtt_offset;
  8774. *cs++ = MI_NOOP;
  8775. return 0;
  8776. }
  8777. static bool use_mmio_flip(struct intel_engine_cs *engine,
  8778. struct drm_i915_gem_object *obj)
  8779. {
  8780. /*
  8781. * This is not being used for older platforms, because
  8782. * non-availability of flip done interrupt forces us to use
  8783. * CS flips. Older platforms derive flip done using some clever
  8784. * tricks involving the flip_pending status bits and vblank irqs.
  8785. * So using MMIO flips there would disrupt this mechanism.
  8786. */
  8787. if (engine == NULL)
  8788. return true;
  8789. if (INTEL_GEN(engine->i915) < 5)
  8790. return false;
  8791. if (i915.use_mmio_flip < 0)
  8792. return false;
  8793. else if (i915.use_mmio_flip > 0)
  8794. return true;
  8795. else if (i915.enable_execlists)
  8796. return true;
  8797. return engine != i915_gem_object_last_write_engine(obj);
  8798. }
  8799. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  8800. unsigned int rotation,
  8801. struct intel_flip_work *work)
  8802. {
  8803. struct drm_device *dev = intel_crtc->base.dev;
  8804. struct drm_i915_private *dev_priv = to_i915(dev);
  8805. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8806. const enum pipe pipe = intel_crtc->pipe;
  8807. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  8808. ctl = I915_READ(PLANE_CTL(pipe, 0));
  8809. ctl &= ~PLANE_CTL_TILED_MASK;
  8810. switch (fb->modifier) {
  8811. case DRM_FORMAT_MOD_LINEAR:
  8812. break;
  8813. case I915_FORMAT_MOD_X_TILED:
  8814. ctl |= PLANE_CTL_TILED_X;
  8815. break;
  8816. case I915_FORMAT_MOD_Y_TILED:
  8817. ctl |= PLANE_CTL_TILED_Y;
  8818. break;
  8819. case I915_FORMAT_MOD_Yf_TILED:
  8820. ctl |= PLANE_CTL_TILED_YF;
  8821. break;
  8822. default:
  8823. MISSING_CASE(fb->modifier);
  8824. }
  8825. /*
  8826. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  8827. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  8828. */
  8829. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  8830. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  8831. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  8832. POSTING_READ(PLANE_SURF(pipe, 0));
  8833. }
  8834. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  8835. struct intel_flip_work *work)
  8836. {
  8837. struct drm_device *dev = intel_crtc->base.dev;
  8838. struct drm_i915_private *dev_priv = to_i915(dev);
  8839. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8840. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  8841. u32 dspcntr;
  8842. dspcntr = I915_READ(reg);
  8843. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  8844. dspcntr |= DISPPLANE_TILED;
  8845. else
  8846. dspcntr &= ~DISPPLANE_TILED;
  8847. I915_WRITE(reg, dspcntr);
  8848. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  8849. POSTING_READ(DSPSURF(intel_crtc->plane));
  8850. }
  8851. static void intel_mmio_flip_work_func(struct work_struct *w)
  8852. {
  8853. struct intel_flip_work *work =
  8854. container_of(w, struct intel_flip_work, mmio_work);
  8855. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8856. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8857. struct intel_framebuffer *intel_fb =
  8858. to_intel_framebuffer(crtc->base.primary->fb);
  8859. struct drm_i915_gem_object *obj = intel_fb->obj;
  8860. WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
  8861. intel_pipe_update_start(crtc);
  8862. if (INTEL_GEN(dev_priv) >= 9)
  8863. skl_do_mmio_flip(crtc, work->rotation, work);
  8864. else
  8865. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  8866. ilk_do_mmio_flip(crtc, work);
  8867. intel_pipe_update_end(crtc, work);
  8868. }
  8869. static int intel_default_queue_flip(struct drm_device *dev,
  8870. struct drm_crtc *crtc,
  8871. struct drm_framebuffer *fb,
  8872. struct drm_i915_gem_object *obj,
  8873. struct drm_i915_gem_request *req,
  8874. uint32_t flags)
  8875. {
  8876. return -ENODEV;
  8877. }
  8878. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  8879. struct intel_crtc *intel_crtc,
  8880. struct intel_flip_work *work)
  8881. {
  8882. u32 addr, vblank;
  8883. if (!atomic_read(&work->pending))
  8884. return false;
  8885. smp_rmb();
  8886. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  8887. if (work->flip_ready_vblank == 0) {
  8888. if (work->flip_queued_req &&
  8889. !i915_gem_request_completed(work->flip_queued_req))
  8890. return false;
  8891. work->flip_ready_vblank = vblank;
  8892. }
  8893. if (vblank - work->flip_ready_vblank < 3)
  8894. return false;
  8895. /* Potential stall - if we see that the flip has happened,
  8896. * assume a missed interrupt. */
  8897. if (INTEL_GEN(dev_priv) >= 4)
  8898. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8899. else
  8900. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8901. /* There is a potential issue here with a false positive after a flip
  8902. * to the same address. We could address this by checking for a
  8903. * non-incrementing frame counter.
  8904. */
  8905. return addr == work->gtt_offset;
  8906. }
  8907. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  8908. {
  8909. struct drm_device *dev = &dev_priv->drm;
  8910. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8911. struct intel_flip_work *work;
  8912. WARN_ON(!in_interrupt());
  8913. if (crtc == NULL)
  8914. return;
  8915. spin_lock(&dev->event_lock);
  8916. work = crtc->flip_work;
  8917. if (work != NULL && !is_mmio_work(work) &&
  8918. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  8919. WARN_ONCE(1,
  8920. "Kicking stuck page flip: queued at %d, now %d\n",
  8921. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  8922. page_flip_completed(crtc);
  8923. work = NULL;
  8924. }
  8925. if (work != NULL && !is_mmio_work(work) &&
  8926. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  8927. intel_queue_rps_boost_for_request(work->flip_queued_req);
  8928. spin_unlock(&dev->event_lock);
  8929. }
  8930. __maybe_unused
  8931. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8932. struct drm_framebuffer *fb,
  8933. struct drm_pending_vblank_event *event,
  8934. uint32_t page_flip_flags)
  8935. {
  8936. struct drm_device *dev = crtc->dev;
  8937. struct drm_i915_private *dev_priv = to_i915(dev);
  8938. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8939. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8940. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8941. struct drm_plane *primary = crtc->primary;
  8942. enum pipe pipe = intel_crtc->pipe;
  8943. struct intel_flip_work *work;
  8944. struct intel_engine_cs *engine;
  8945. bool mmio_flip;
  8946. struct drm_i915_gem_request *request;
  8947. struct i915_vma *vma;
  8948. int ret;
  8949. /*
  8950. * drm_mode_page_flip_ioctl() should already catch this, but double
  8951. * check to be safe. In the future we may enable pageflipping from
  8952. * a disabled primary plane.
  8953. */
  8954. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8955. return -EBUSY;
  8956. /* Can't change pixel format via MI display flips. */
  8957. if (fb->format != crtc->primary->fb->format)
  8958. return -EINVAL;
  8959. /*
  8960. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8961. * Note that pitch changes could also affect these register.
  8962. */
  8963. if (INTEL_GEN(dev_priv) > 3 &&
  8964. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8965. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8966. return -EINVAL;
  8967. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8968. goto out_hang;
  8969. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8970. if (work == NULL)
  8971. return -ENOMEM;
  8972. work->event = event;
  8973. work->crtc = crtc;
  8974. work->old_fb = old_fb;
  8975. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  8976. ret = drm_crtc_vblank_get(crtc);
  8977. if (ret)
  8978. goto free_work;
  8979. /* We borrow the event spin lock for protecting flip_work */
  8980. spin_lock_irq(&dev->event_lock);
  8981. if (intel_crtc->flip_work) {
  8982. /* Before declaring the flip queue wedged, check if
  8983. * the hardware completed the operation behind our backs.
  8984. */
  8985. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  8986. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8987. page_flip_completed(intel_crtc);
  8988. } else {
  8989. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8990. spin_unlock_irq(&dev->event_lock);
  8991. drm_crtc_vblank_put(crtc);
  8992. kfree(work);
  8993. return -EBUSY;
  8994. }
  8995. }
  8996. intel_crtc->flip_work = work;
  8997. spin_unlock_irq(&dev->event_lock);
  8998. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8999. flush_workqueue(dev_priv->wq);
  9000. /* Reference the objects for the scheduled work. */
  9001. drm_framebuffer_reference(work->old_fb);
  9002. crtc->primary->fb = fb;
  9003. update_state_fb(crtc->primary);
  9004. work->pending_flip_obj = i915_gem_object_get(obj);
  9005. ret = i915_mutex_lock_interruptible(dev);
  9006. if (ret)
  9007. goto cleanup;
  9008. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  9009. if (i915_reset_backoff_or_wedged(&dev_priv->gpu_error)) {
  9010. ret = -EIO;
  9011. goto unlock;
  9012. }
  9013. atomic_inc(&intel_crtc->unpin_work_count);
  9014. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  9015. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9016. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  9017. engine = dev_priv->engine[BCS];
  9018. if (fb->modifier != old_fb->modifier)
  9019. /* vlv: DISPLAY_FLIP fails to change tiling */
  9020. engine = NULL;
  9021. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  9022. engine = dev_priv->engine[BCS];
  9023. } else if (INTEL_GEN(dev_priv) >= 7) {
  9024. engine = i915_gem_object_last_write_engine(obj);
  9025. if (engine == NULL || engine->id != RCS)
  9026. engine = dev_priv->engine[BCS];
  9027. } else {
  9028. engine = dev_priv->engine[RCS];
  9029. }
  9030. mmio_flip = use_mmio_flip(engine, obj);
  9031. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  9032. if (IS_ERR(vma)) {
  9033. ret = PTR_ERR(vma);
  9034. goto cleanup_pending;
  9035. }
  9036. work->old_vma = to_intel_plane_state(primary->state)->vma;
  9037. to_intel_plane_state(primary->state)->vma = vma;
  9038. work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
  9039. work->rotation = crtc->primary->state->rotation;
  9040. /*
  9041. * There's the potential that the next frame will not be compatible with
  9042. * FBC, so we want to call pre_update() before the actual page flip.
  9043. * The problem is that pre_update() caches some information about the fb
  9044. * object, so we want to do this only after the object is pinned. Let's
  9045. * be on the safe side and do this immediately before scheduling the
  9046. * flip.
  9047. */
  9048. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  9049. to_intel_plane_state(primary->state));
  9050. if (mmio_flip) {
  9051. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  9052. queue_work(system_unbound_wq, &work->mmio_work);
  9053. } else {
  9054. request = i915_gem_request_alloc(engine,
  9055. dev_priv->kernel_context);
  9056. if (IS_ERR(request)) {
  9057. ret = PTR_ERR(request);
  9058. goto cleanup_unpin;
  9059. }
  9060. ret = i915_gem_request_await_object(request, obj, false);
  9061. if (ret)
  9062. goto cleanup_request;
  9063. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9064. page_flip_flags);
  9065. if (ret)
  9066. goto cleanup_request;
  9067. intel_mark_page_flip_active(intel_crtc, work);
  9068. work->flip_queued_req = i915_gem_request_get(request);
  9069. i915_add_request(request);
  9070. }
  9071. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  9072. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  9073. to_intel_plane(primary)->frontbuffer_bit);
  9074. mutex_unlock(&dev->struct_mutex);
  9075. intel_frontbuffer_flip_prepare(to_i915(dev),
  9076. to_intel_plane(primary)->frontbuffer_bit);
  9077. trace_i915_flip_request(intel_crtc->plane, obj);
  9078. return 0;
  9079. cleanup_request:
  9080. i915_add_request(request);
  9081. cleanup_unpin:
  9082. to_intel_plane_state(primary->state)->vma = work->old_vma;
  9083. intel_unpin_fb_vma(vma);
  9084. cleanup_pending:
  9085. atomic_dec(&intel_crtc->unpin_work_count);
  9086. unlock:
  9087. mutex_unlock(&dev->struct_mutex);
  9088. cleanup:
  9089. crtc->primary->fb = old_fb;
  9090. update_state_fb(crtc->primary);
  9091. i915_gem_object_put(obj);
  9092. drm_framebuffer_unreference(work->old_fb);
  9093. spin_lock_irq(&dev->event_lock);
  9094. intel_crtc->flip_work = NULL;
  9095. spin_unlock_irq(&dev->event_lock);
  9096. drm_crtc_vblank_put(crtc);
  9097. free_work:
  9098. kfree(work);
  9099. if (ret == -EIO) {
  9100. struct drm_atomic_state *state;
  9101. struct drm_plane_state *plane_state;
  9102. out_hang:
  9103. state = drm_atomic_state_alloc(dev);
  9104. if (!state)
  9105. return -ENOMEM;
  9106. state->acquire_ctx = dev->mode_config.acquire_ctx;
  9107. retry:
  9108. plane_state = drm_atomic_get_plane_state(state, primary);
  9109. ret = PTR_ERR_OR_ZERO(plane_state);
  9110. if (!ret) {
  9111. drm_atomic_set_fb_for_plane(plane_state, fb);
  9112. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9113. if (!ret)
  9114. ret = drm_atomic_commit(state);
  9115. }
  9116. if (ret == -EDEADLK) {
  9117. drm_modeset_backoff(state->acquire_ctx);
  9118. drm_atomic_state_clear(state);
  9119. goto retry;
  9120. }
  9121. drm_atomic_state_put(state);
  9122. if (ret == 0 && event) {
  9123. spin_lock_irq(&dev->event_lock);
  9124. drm_crtc_send_vblank_event(crtc, event);
  9125. spin_unlock_irq(&dev->event_lock);
  9126. }
  9127. }
  9128. return ret;
  9129. }
  9130. /**
  9131. * intel_wm_need_update - Check whether watermarks need updating
  9132. * @plane: drm plane
  9133. * @state: new plane state
  9134. *
  9135. * Check current plane state versus the new one to determine whether
  9136. * watermarks need to be recalculated.
  9137. *
  9138. * Returns true or false.
  9139. */
  9140. static bool intel_wm_need_update(struct drm_plane *plane,
  9141. struct drm_plane_state *state)
  9142. {
  9143. struct intel_plane_state *new = to_intel_plane_state(state);
  9144. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9145. /* Update watermarks on tiling or size changes. */
  9146. if (new->base.visible != cur->base.visible)
  9147. return true;
  9148. if (!cur->base.fb || !new->base.fb)
  9149. return false;
  9150. if (cur->base.fb->modifier != new->base.fb->modifier ||
  9151. cur->base.rotation != new->base.rotation ||
  9152. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  9153. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  9154. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  9155. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  9156. return true;
  9157. return false;
  9158. }
  9159. static bool needs_scaling(struct intel_plane_state *state)
  9160. {
  9161. int src_w = drm_rect_width(&state->base.src) >> 16;
  9162. int src_h = drm_rect_height(&state->base.src) >> 16;
  9163. int dst_w = drm_rect_width(&state->base.dst);
  9164. int dst_h = drm_rect_height(&state->base.dst);
  9165. return (src_w != dst_w || src_h != dst_h);
  9166. }
  9167. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9168. struct drm_plane_state *plane_state)
  9169. {
  9170. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9171. struct drm_crtc *crtc = crtc_state->crtc;
  9172. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9173. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  9174. struct drm_device *dev = crtc->dev;
  9175. struct drm_i915_private *dev_priv = to_i915(dev);
  9176. struct intel_plane_state *old_plane_state =
  9177. to_intel_plane_state(plane->base.state);
  9178. bool mode_changed = needs_modeset(crtc_state);
  9179. bool was_crtc_enabled = crtc->state->active;
  9180. bool is_crtc_enabled = crtc_state->active;
  9181. bool turn_off, turn_on, visible, was_visible;
  9182. struct drm_framebuffer *fb = plane_state->fb;
  9183. int ret;
  9184. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  9185. ret = skl_update_scaler_plane(
  9186. to_intel_crtc_state(crtc_state),
  9187. to_intel_plane_state(plane_state));
  9188. if (ret)
  9189. return ret;
  9190. }
  9191. was_visible = old_plane_state->base.visible;
  9192. visible = plane_state->visible;
  9193. if (!was_crtc_enabled && WARN_ON(was_visible))
  9194. was_visible = false;
  9195. /*
  9196. * Visibility is calculated as if the crtc was on, but
  9197. * after scaler setup everything depends on it being off
  9198. * when the crtc isn't active.
  9199. *
  9200. * FIXME this is wrong for watermarks. Watermarks should also
  9201. * be computed as if the pipe would be active. Perhaps move
  9202. * per-plane wm computation to the .check_plane() hook, and
  9203. * only combine the results from all planes in the current place?
  9204. */
  9205. if (!is_crtc_enabled) {
  9206. plane_state->visible = visible = false;
  9207. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  9208. }
  9209. if (!was_visible && !visible)
  9210. return 0;
  9211. if (fb != old_plane_state->base.fb)
  9212. pipe_config->fb_changed = true;
  9213. turn_off = was_visible && (!visible || mode_changed);
  9214. turn_on = visible && (!was_visible || mode_changed);
  9215. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  9216. intel_crtc->base.base.id, intel_crtc->base.name,
  9217. plane->base.base.id, plane->base.name,
  9218. fb ? fb->base.id : -1);
  9219. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  9220. plane->base.base.id, plane->base.name,
  9221. was_visible, visible,
  9222. turn_off, turn_on, mode_changed);
  9223. if (turn_on) {
  9224. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  9225. pipe_config->update_wm_pre = true;
  9226. /* must disable cxsr around plane enable/disable */
  9227. if (plane->id != PLANE_CURSOR)
  9228. pipe_config->disable_cxsr = true;
  9229. } else if (turn_off) {
  9230. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  9231. pipe_config->update_wm_post = true;
  9232. /* must disable cxsr around plane enable/disable */
  9233. if (plane->id != PLANE_CURSOR)
  9234. pipe_config->disable_cxsr = true;
  9235. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  9236. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  9237. /* FIXME bollocks */
  9238. pipe_config->update_wm_pre = true;
  9239. pipe_config->update_wm_post = true;
  9240. }
  9241. }
  9242. if (visible || was_visible)
  9243. pipe_config->fb_bits |= plane->frontbuffer_bit;
  9244. /*
  9245. * WaCxSRDisabledForSpriteScaling:ivb
  9246. *
  9247. * cstate->update_wm was already set above, so this flag will
  9248. * take effect when we commit and program watermarks.
  9249. */
  9250. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  9251. needs_scaling(to_intel_plane_state(plane_state)) &&
  9252. !needs_scaling(old_plane_state))
  9253. pipe_config->disable_lp_wm = true;
  9254. return 0;
  9255. }
  9256. static bool encoders_cloneable(const struct intel_encoder *a,
  9257. const struct intel_encoder *b)
  9258. {
  9259. /* masks could be asymmetric, so check both ways */
  9260. return a == b || (a->cloneable & (1 << b->type) &&
  9261. b->cloneable & (1 << a->type));
  9262. }
  9263. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9264. struct intel_crtc *crtc,
  9265. struct intel_encoder *encoder)
  9266. {
  9267. struct intel_encoder *source_encoder;
  9268. struct drm_connector *connector;
  9269. struct drm_connector_state *connector_state;
  9270. int i;
  9271. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9272. if (connector_state->crtc != &crtc->base)
  9273. continue;
  9274. source_encoder =
  9275. to_intel_encoder(connector_state->best_encoder);
  9276. if (!encoders_cloneable(encoder, source_encoder))
  9277. return false;
  9278. }
  9279. return true;
  9280. }
  9281. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9282. struct drm_crtc_state *crtc_state)
  9283. {
  9284. struct drm_device *dev = crtc->dev;
  9285. struct drm_i915_private *dev_priv = to_i915(dev);
  9286. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9287. struct intel_crtc_state *pipe_config =
  9288. to_intel_crtc_state(crtc_state);
  9289. struct drm_atomic_state *state = crtc_state->state;
  9290. int ret;
  9291. bool mode_changed = needs_modeset(crtc_state);
  9292. if (mode_changed && !crtc_state->active)
  9293. pipe_config->update_wm_post = true;
  9294. if (mode_changed && crtc_state->enable &&
  9295. dev_priv->display.crtc_compute_clock &&
  9296. !WARN_ON(pipe_config->shared_dpll)) {
  9297. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9298. pipe_config);
  9299. if (ret)
  9300. return ret;
  9301. }
  9302. if (crtc_state->color_mgmt_changed) {
  9303. ret = intel_color_check(crtc, crtc_state);
  9304. if (ret)
  9305. return ret;
  9306. /*
  9307. * Changing color management on Intel hardware is
  9308. * handled as part of planes update.
  9309. */
  9310. crtc_state->planes_changed = true;
  9311. }
  9312. ret = 0;
  9313. if (dev_priv->display.compute_pipe_wm) {
  9314. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  9315. if (ret) {
  9316. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  9317. return ret;
  9318. }
  9319. }
  9320. if (dev_priv->display.compute_intermediate_wm &&
  9321. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  9322. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  9323. return 0;
  9324. /*
  9325. * Calculate 'intermediate' watermarks that satisfy both the
  9326. * old state and the new state. We can program these
  9327. * immediately.
  9328. */
  9329. ret = dev_priv->display.compute_intermediate_wm(dev,
  9330. intel_crtc,
  9331. pipe_config);
  9332. if (ret) {
  9333. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  9334. return ret;
  9335. }
  9336. } else if (dev_priv->display.compute_intermediate_wm) {
  9337. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  9338. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  9339. }
  9340. if (INTEL_GEN(dev_priv) >= 9) {
  9341. if (mode_changed)
  9342. ret = skl_update_scaler_crtc(pipe_config);
  9343. if (!ret)
  9344. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  9345. pipe_config);
  9346. }
  9347. return ret;
  9348. }
  9349. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9350. .atomic_begin = intel_begin_crtc_commit,
  9351. .atomic_flush = intel_finish_crtc_commit,
  9352. .atomic_check = intel_crtc_atomic_check,
  9353. };
  9354. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9355. {
  9356. struct intel_connector *connector;
  9357. struct drm_connector_list_iter conn_iter;
  9358. drm_connector_list_iter_begin(dev, &conn_iter);
  9359. for_each_intel_connector_iter(connector, &conn_iter) {
  9360. if (connector->base.state->crtc)
  9361. drm_connector_unreference(&connector->base);
  9362. if (connector->base.encoder) {
  9363. connector->base.state->best_encoder =
  9364. connector->base.encoder;
  9365. connector->base.state->crtc =
  9366. connector->base.encoder->crtc;
  9367. drm_connector_reference(&connector->base);
  9368. } else {
  9369. connector->base.state->best_encoder = NULL;
  9370. connector->base.state->crtc = NULL;
  9371. }
  9372. }
  9373. drm_connector_list_iter_end(&conn_iter);
  9374. }
  9375. static void
  9376. connected_sink_compute_bpp(struct intel_connector *connector,
  9377. struct intel_crtc_state *pipe_config)
  9378. {
  9379. const struct drm_display_info *info = &connector->base.display_info;
  9380. int bpp = pipe_config->pipe_bpp;
  9381. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9382. connector->base.base.id,
  9383. connector->base.name);
  9384. /* Don't use an invalid EDID bpc value */
  9385. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  9386. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9387. bpp, info->bpc * 3);
  9388. pipe_config->pipe_bpp = info->bpc * 3;
  9389. }
  9390. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9391. if (info->bpc == 0 && bpp > 24) {
  9392. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9393. bpp);
  9394. pipe_config->pipe_bpp = 24;
  9395. }
  9396. }
  9397. static int
  9398. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9399. struct intel_crtc_state *pipe_config)
  9400. {
  9401. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9402. struct drm_atomic_state *state;
  9403. struct drm_connector *connector;
  9404. struct drm_connector_state *connector_state;
  9405. int bpp, i;
  9406. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  9407. IS_CHERRYVIEW(dev_priv)))
  9408. bpp = 10*3;
  9409. else if (INTEL_GEN(dev_priv) >= 5)
  9410. bpp = 12*3;
  9411. else
  9412. bpp = 8*3;
  9413. pipe_config->pipe_bpp = bpp;
  9414. state = pipe_config->base.state;
  9415. /* Clamp display bpp to EDID value */
  9416. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9417. if (connector_state->crtc != &crtc->base)
  9418. continue;
  9419. connected_sink_compute_bpp(to_intel_connector(connector),
  9420. pipe_config);
  9421. }
  9422. return bpp;
  9423. }
  9424. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9425. {
  9426. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9427. "type: 0x%x flags: 0x%x\n",
  9428. mode->crtc_clock,
  9429. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9430. mode->crtc_hsync_end, mode->crtc_htotal,
  9431. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9432. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9433. }
  9434. static inline void
  9435. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  9436. unsigned int lane_count, struct intel_link_m_n *m_n)
  9437. {
  9438. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9439. id, lane_count,
  9440. m_n->gmch_m, m_n->gmch_n,
  9441. m_n->link_m, m_n->link_n, m_n->tu);
  9442. }
  9443. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9444. struct intel_crtc_state *pipe_config,
  9445. const char *context)
  9446. {
  9447. struct drm_device *dev = crtc->base.dev;
  9448. struct drm_i915_private *dev_priv = to_i915(dev);
  9449. struct drm_plane *plane;
  9450. struct intel_plane *intel_plane;
  9451. struct intel_plane_state *state;
  9452. struct drm_framebuffer *fb;
  9453. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  9454. crtc->base.base.id, crtc->base.name, context);
  9455. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  9456. transcoder_name(pipe_config->cpu_transcoder),
  9457. pipe_config->pipe_bpp, pipe_config->dither);
  9458. if (pipe_config->has_pch_encoder)
  9459. intel_dump_m_n_config(pipe_config, "fdi",
  9460. pipe_config->fdi_lanes,
  9461. &pipe_config->fdi_m_n);
  9462. if (intel_crtc_has_dp_encoder(pipe_config)) {
  9463. intel_dump_m_n_config(pipe_config, "dp m_n",
  9464. pipe_config->lane_count, &pipe_config->dp_m_n);
  9465. if (pipe_config->has_drrs)
  9466. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  9467. pipe_config->lane_count,
  9468. &pipe_config->dp_m2_n2);
  9469. }
  9470. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9471. pipe_config->has_audio, pipe_config->has_infoframe);
  9472. DRM_DEBUG_KMS("requested mode:\n");
  9473. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9474. DRM_DEBUG_KMS("adjusted mode:\n");
  9475. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9476. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9477. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  9478. pipe_config->port_clock,
  9479. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  9480. pipe_config->pixel_rate);
  9481. if (INTEL_GEN(dev_priv) >= 9)
  9482. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9483. crtc->num_scalers,
  9484. pipe_config->scaler_state.scaler_users,
  9485. pipe_config->scaler_state.scaler_id);
  9486. if (HAS_GMCH_DISPLAY(dev_priv))
  9487. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9488. pipe_config->gmch_pfit.control,
  9489. pipe_config->gmch_pfit.pgm_ratios,
  9490. pipe_config->gmch_pfit.lvds_border_bits);
  9491. else
  9492. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9493. pipe_config->pch_pfit.pos,
  9494. pipe_config->pch_pfit.size,
  9495. enableddisabled(pipe_config->pch_pfit.enabled));
  9496. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  9497. pipe_config->ips_enabled, pipe_config->double_wide);
  9498. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  9499. DRM_DEBUG_KMS("planes on this crtc\n");
  9500. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9501. struct drm_format_name_buf format_name;
  9502. intel_plane = to_intel_plane(plane);
  9503. if (intel_plane->pipe != crtc->pipe)
  9504. continue;
  9505. state = to_intel_plane_state(plane->state);
  9506. fb = state->base.fb;
  9507. if (!fb) {
  9508. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  9509. plane->base.id, plane->name, state->scaler_id);
  9510. continue;
  9511. }
  9512. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  9513. plane->base.id, plane->name,
  9514. fb->base.id, fb->width, fb->height,
  9515. drm_get_format_name(fb->format->format, &format_name));
  9516. if (INTEL_GEN(dev_priv) >= 9)
  9517. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  9518. state->scaler_id,
  9519. state->base.src.x1 >> 16,
  9520. state->base.src.y1 >> 16,
  9521. drm_rect_width(&state->base.src) >> 16,
  9522. drm_rect_height(&state->base.src) >> 16,
  9523. state->base.dst.x1, state->base.dst.y1,
  9524. drm_rect_width(&state->base.dst),
  9525. drm_rect_height(&state->base.dst));
  9526. }
  9527. }
  9528. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9529. {
  9530. struct drm_device *dev = state->dev;
  9531. struct drm_connector *connector;
  9532. struct drm_connector_list_iter conn_iter;
  9533. unsigned int used_ports = 0;
  9534. unsigned int used_mst_ports = 0;
  9535. /*
  9536. * Walk the connector list instead of the encoder
  9537. * list to detect the problem on ddi platforms
  9538. * where there's just one encoder per digital port.
  9539. */
  9540. drm_connector_list_iter_begin(dev, &conn_iter);
  9541. drm_for_each_connector_iter(connector, &conn_iter) {
  9542. struct drm_connector_state *connector_state;
  9543. struct intel_encoder *encoder;
  9544. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  9545. if (!connector_state)
  9546. connector_state = connector->state;
  9547. if (!connector_state->best_encoder)
  9548. continue;
  9549. encoder = to_intel_encoder(connector_state->best_encoder);
  9550. WARN_ON(!connector_state->crtc);
  9551. switch (encoder->type) {
  9552. unsigned int port_mask;
  9553. case INTEL_OUTPUT_UNKNOWN:
  9554. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9555. break;
  9556. case INTEL_OUTPUT_DP:
  9557. case INTEL_OUTPUT_HDMI:
  9558. case INTEL_OUTPUT_EDP:
  9559. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9560. /* the same port mustn't appear more than once */
  9561. if (used_ports & port_mask)
  9562. return false;
  9563. used_ports |= port_mask;
  9564. break;
  9565. case INTEL_OUTPUT_DP_MST:
  9566. used_mst_ports |=
  9567. 1 << enc_to_mst(&encoder->base)->primary->port;
  9568. break;
  9569. default:
  9570. break;
  9571. }
  9572. }
  9573. drm_connector_list_iter_end(&conn_iter);
  9574. /* can't mix MST and SST/HDMI on the same port */
  9575. if (used_ports & used_mst_ports)
  9576. return false;
  9577. return true;
  9578. }
  9579. static void
  9580. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9581. {
  9582. struct drm_i915_private *dev_priv =
  9583. to_i915(crtc_state->base.crtc->dev);
  9584. struct intel_crtc_scaler_state scaler_state;
  9585. struct intel_dpll_hw_state dpll_hw_state;
  9586. struct intel_shared_dpll *shared_dpll;
  9587. struct intel_crtc_wm_state wm_state;
  9588. bool force_thru;
  9589. /* FIXME: before the switch to atomic started, a new pipe_config was
  9590. * kzalloc'd. Code that depends on any field being zero should be
  9591. * fixed, so that the crtc_state can be safely duplicated. For now,
  9592. * only fields that are know to not cause problems are preserved. */
  9593. scaler_state = crtc_state->scaler_state;
  9594. shared_dpll = crtc_state->shared_dpll;
  9595. dpll_hw_state = crtc_state->dpll_hw_state;
  9596. force_thru = crtc_state->pch_pfit.force_thru;
  9597. if (IS_G4X(dev_priv) ||
  9598. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9599. wm_state = crtc_state->wm;
  9600. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9601. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9602. memset(&crtc_state->base + 1, 0,
  9603. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9604. crtc_state->scaler_state = scaler_state;
  9605. crtc_state->shared_dpll = shared_dpll;
  9606. crtc_state->dpll_hw_state = dpll_hw_state;
  9607. crtc_state->pch_pfit.force_thru = force_thru;
  9608. if (IS_G4X(dev_priv) ||
  9609. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9610. crtc_state->wm = wm_state;
  9611. }
  9612. static int
  9613. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9614. struct intel_crtc_state *pipe_config)
  9615. {
  9616. struct drm_atomic_state *state = pipe_config->base.state;
  9617. struct intel_encoder *encoder;
  9618. struct drm_connector *connector;
  9619. struct drm_connector_state *connector_state;
  9620. int base_bpp, ret = -EINVAL;
  9621. int i;
  9622. bool retry = true;
  9623. clear_intel_crtc_state(pipe_config);
  9624. pipe_config->cpu_transcoder =
  9625. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9626. /*
  9627. * Sanitize sync polarity flags based on requested ones. If neither
  9628. * positive or negative polarity is requested, treat this as meaning
  9629. * negative polarity.
  9630. */
  9631. if (!(pipe_config->base.adjusted_mode.flags &
  9632. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9633. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9634. if (!(pipe_config->base.adjusted_mode.flags &
  9635. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9636. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9637. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9638. pipe_config);
  9639. if (base_bpp < 0)
  9640. goto fail;
  9641. /*
  9642. * Determine the real pipe dimensions. Note that stereo modes can
  9643. * increase the actual pipe size due to the frame doubling and
  9644. * insertion of additional space for blanks between the frame. This
  9645. * is stored in the crtc timings. We use the requested mode to do this
  9646. * computation to clearly distinguish it from the adjusted mode, which
  9647. * can be changed by the connectors in the below retry loop.
  9648. */
  9649. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9650. &pipe_config->pipe_src_w,
  9651. &pipe_config->pipe_src_h);
  9652. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9653. if (connector_state->crtc != crtc)
  9654. continue;
  9655. encoder = to_intel_encoder(connector_state->best_encoder);
  9656. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9657. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9658. goto fail;
  9659. }
  9660. /*
  9661. * Determine output_types before calling the .compute_config()
  9662. * hooks so that the hooks can use this information safely.
  9663. */
  9664. pipe_config->output_types |= 1 << encoder->type;
  9665. }
  9666. encoder_retry:
  9667. /* Ensure the port clock defaults are reset when retrying. */
  9668. pipe_config->port_clock = 0;
  9669. pipe_config->pixel_multiplier = 1;
  9670. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9671. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9672. CRTC_STEREO_DOUBLE);
  9673. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9674. * adjust it according to limitations or connector properties, and also
  9675. * a chance to reject the mode entirely.
  9676. */
  9677. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9678. if (connector_state->crtc != crtc)
  9679. continue;
  9680. encoder = to_intel_encoder(connector_state->best_encoder);
  9681. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9682. DRM_DEBUG_KMS("Encoder config failure\n");
  9683. goto fail;
  9684. }
  9685. }
  9686. /* Set default port clock if not overwritten by the encoder. Needs to be
  9687. * done afterwards in case the encoder adjusts the mode. */
  9688. if (!pipe_config->port_clock)
  9689. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9690. * pipe_config->pixel_multiplier;
  9691. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9692. if (ret < 0) {
  9693. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9694. goto fail;
  9695. }
  9696. if (ret == RETRY) {
  9697. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9698. ret = -EINVAL;
  9699. goto fail;
  9700. }
  9701. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9702. retry = false;
  9703. goto encoder_retry;
  9704. }
  9705. /* Dithering seems to not pass-through bits correctly when it should, so
  9706. * only enable it on 6bpc panels and when its not a compliance
  9707. * test requesting 6bpc video pattern.
  9708. */
  9709. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9710. !pipe_config->dither_force_disable;
  9711. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9712. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9713. fail:
  9714. return ret;
  9715. }
  9716. static void
  9717. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  9718. {
  9719. struct drm_crtc *crtc;
  9720. struct drm_crtc_state *new_crtc_state;
  9721. int i;
  9722. /* Double check state. */
  9723. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  9724. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  9725. /*
  9726. * Update legacy state to satisfy fbc code. This can
  9727. * be removed when fbc uses the atomic state.
  9728. */
  9729. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  9730. struct drm_plane_state *plane_state = crtc->primary->state;
  9731. crtc->primary->fb = plane_state->fb;
  9732. crtc->x = plane_state->src_x >> 16;
  9733. crtc->y = plane_state->src_y >> 16;
  9734. }
  9735. }
  9736. }
  9737. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9738. {
  9739. int diff;
  9740. if (clock1 == clock2)
  9741. return true;
  9742. if (!clock1 || !clock2)
  9743. return false;
  9744. diff = abs(clock1 - clock2);
  9745. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9746. return true;
  9747. return false;
  9748. }
  9749. static bool
  9750. intel_compare_m_n(unsigned int m, unsigned int n,
  9751. unsigned int m2, unsigned int n2,
  9752. bool exact)
  9753. {
  9754. if (m == m2 && n == n2)
  9755. return true;
  9756. if (exact || !m || !n || !m2 || !n2)
  9757. return false;
  9758. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9759. if (n > n2) {
  9760. while (n > n2) {
  9761. m2 <<= 1;
  9762. n2 <<= 1;
  9763. }
  9764. } else if (n < n2) {
  9765. while (n < n2) {
  9766. m <<= 1;
  9767. n <<= 1;
  9768. }
  9769. }
  9770. if (n != n2)
  9771. return false;
  9772. return intel_fuzzy_clock_check(m, m2);
  9773. }
  9774. static bool
  9775. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9776. struct intel_link_m_n *m2_n2,
  9777. bool adjust)
  9778. {
  9779. if (m_n->tu == m2_n2->tu &&
  9780. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9781. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9782. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9783. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9784. if (adjust)
  9785. *m2_n2 = *m_n;
  9786. return true;
  9787. }
  9788. return false;
  9789. }
  9790. static void __printf(3, 4)
  9791. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9792. {
  9793. char *level;
  9794. unsigned int category;
  9795. struct va_format vaf;
  9796. va_list args;
  9797. if (adjust) {
  9798. level = KERN_DEBUG;
  9799. category = DRM_UT_KMS;
  9800. } else {
  9801. level = KERN_ERR;
  9802. category = DRM_UT_NONE;
  9803. }
  9804. va_start(args, format);
  9805. vaf.fmt = format;
  9806. vaf.va = &args;
  9807. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  9808. va_end(args);
  9809. }
  9810. static bool
  9811. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9812. struct intel_crtc_state *current_config,
  9813. struct intel_crtc_state *pipe_config,
  9814. bool adjust)
  9815. {
  9816. bool ret = true;
  9817. #define PIPE_CONF_CHECK_X(name) \
  9818. if (current_config->name != pipe_config->name) { \
  9819. pipe_config_err(adjust, __stringify(name), \
  9820. "(expected 0x%08x, found 0x%08x)\n", \
  9821. current_config->name, \
  9822. pipe_config->name); \
  9823. ret = false; \
  9824. }
  9825. #define PIPE_CONF_CHECK_I(name) \
  9826. if (current_config->name != pipe_config->name) { \
  9827. pipe_config_err(adjust, __stringify(name), \
  9828. "(expected %i, found %i)\n", \
  9829. current_config->name, \
  9830. pipe_config->name); \
  9831. ret = false; \
  9832. }
  9833. #define PIPE_CONF_CHECK_P(name) \
  9834. if (current_config->name != pipe_config->name) { \
  9835. pipe_config_err(adjust, __stringify(name), \
  9836. "(expected %p, found %p)\n", \
  9837. current_config->name, \
  9838. pipe_config->name); \
  9839. ret = false; \
  9840. }
  9841. #define PIPE_CONF_CHECK_M_N(name) \
  9842. if (!intel_compare_link_m_n(&current_config->name, \
  9843. &pipe_config->name,\
  9844. adjust)) { \
  9845. pipe_config_err(adjust, __stringify(name), \
  9846. "(expected tu %i gmch %i/%i link %i/%i, " \
  9847. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9848. current_config->name.tu, \
  9849. current_config->name.gmch_m, \
  9850. current_config->name.gmch_n, \
  9851. current_config->name.link_m, \
  9852. current_config->name.link_n, \
  9853. pipe_config->name.tu, \
  9854. pipe_config->name.gmch_m, \
  9855. pipe_config->name.gmch_n, \
  9856. pipe_config->name.link_m, \
  9857. pipe_config->name.link_n); \
  9858. ret = false; \
  9859. }
  9860. /* This is required for BDW+ where there is only one set of registers for
  9861. * switching between high and low RR.
  9862. * This macro can be used whenever a comparison has to be made between one
  9863. * hw state and multiple sw state variables.
  9864. */
  9865. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9866. if (!intel_compare_link_m_n(&current_config->name, \
  9867. &pipe_config->name, adjust) && \
  9868. !intel_compare_link_m_n(&current_config->alt_name, \
  9869. &pipe_config->name, adjust)) { \
  9870. pipe_config_err(adjust, __stringify(name), \
  9871. "(expected tu %i gmch %i/%i link %i/%i, " \
  9872. "or tu %i gmch %i/%i link %i/%i, " \
  9873. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9874. current_config->name.tu, \
  9875. current_config->name.gmch_m, \
  9876. current_config->name.gmch_n, \
  9877. current_config->name.link_m, \
  9878. current_config->name.link_n, \
  9879. current_config->alt_name.tu, \
  9880. current_config->alt_name.gmch_m, \
  9881. current_config->alt_name.gmch_n, \
  9882. current_config->alt_name.link_m, \
  9883. current_config->alt_name.link_n, \
  9884. pipe_config->name.tu, \
  9885. pipe_config->name.gmch_m, \
  9886. pipe_config->name.gmch_n, \
  9887. pipe_config->name.link_m, \
  9888. pipe_config->name.link_n); \
  9889. ret = false; \
  9890. }
  9891. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9892. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9893. pipe_config_err(adjust, __stringify(name), \
  9894. "(%x) (expected %i, found %i)\n", \
  9895. (mask), \
  9896. current_config->name & (mask), \
  9897. pipe_config->name & (mask)); \
  9898. ret = false; \
  9899. }
  9900. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9901. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9902. pipe_config_err(adjust, __stringify(name), \
  9903. "(expected %i, found %i)\n", \
  9904. current_config->name, \
  9905. pipe_config->name); \
  9906. ret = false; \
  9907. }
  9908. #define PIPE_CONF_QUIRK(quirk) \
  9909. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9910. PIPE_CONF_CHECK_I(cpu_transcoder);
  9911. PIPE_CONF_CHECK_I(has_pch_encoder);
  9912. PIPE_CONF_CHECK_I(fdi_lanes);
  9913. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9914. PIPE_CONF_CHECK_I(lane_count);
  9915. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9916. if (INTEL_GEN(dev_priv) < 8) {
  9917. PIPE_CONF_CHECK_M_N(dp_m_n);
  9918. if (current_config->has_drrs)
  9919. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9920. } else
  9921. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9922. PIPE_CONF_CHECK_X(output_types);
  9923. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9924. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9925. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9926. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9927. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9928. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9929. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9930. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9931. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9932. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9933. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9934. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9935. PIPE_CONF_CHECK_I(pixel_multiplier);
  9936. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9937. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9938. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9939. PIPE_CONF_CHECK_I(limited_color_range);
  9940. PIPE_CONF_CHECK_I(hdmi_scrambling);
  9941. PIPE_CONF_CHECK_I(hdmi_high_tmds_clock_ratio);
  9942. PIPE_CONF_CHECK_I(has_infoframe);
  9943. PIPE_CONF_CHECK_I(has_audio);
  9944. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9945. DRM_MODE_FLAG_INTERLACE);
  9946. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9947. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9948. DRM_MODE_FLAG_PHSYNC);
  9949. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9950. DRM_MODE_FLAG_NHSYNC);
  9951. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9952. DRM_MODE_FLAG_PVSYNC);
  9953. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9954. DRM_MODE_FLAG_NVSYNC);
  9955. }
  9956. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9957. /* pfit ratios are autocomputed by the hw on gen4+ */
  9958. if (INTEL_GEN(dev_priv) < 4)
  9959. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9960. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9961. if (!adjust) {
  9962. PIPE_CONF_CHECK_I(pipe_src_w);
  9963. PIPE_CONF_CHECK_I(pipe_src_h);
  9964. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9965. if (current_config->pch_pfit.enabled) {
  9966. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9967. PIPE_CONF_CHECK_X(pch_pfit.size);
  9968. }
  9969. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9970. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9971. }
  9972. /* BDW+ don't expose a synchronous way to read the state */
  9973. if (IS_HASWELL(dev_priv))
  9974. PIPE_CONF_CHECK_I(ips_enabled);
  9975. PIPE_CONF_CHECK_I(double_wide);
  9976. PIPE_CONF_CHECK_P(shared_dpll);
  9977. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9978. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9979. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9980. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9981. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9982. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9983. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9984. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9985. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9986. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9987. PIPE_CONF_CHECK_X(dsi_pll.div);
  9988. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9989. PIPE_CONF_CHECK_I(pipe_bpp);
  9990. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9991. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9992. #undef PIPE_CONF_CHECK_X
  9993. #undef PIPE_CONF_CHECK_I
  9994. #undef PIPE_CONF_CHECK_P
  9995. #undef PIPE_CONF_CHECK_FLAGS
  9996. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9997. #undef PIPE_CONF_QUIRK
  9998. return ret;
  9999. }
  10000. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  10001. const struct intel_crtc_state *pipe_config)
  10002. {
  10003. if (pipe_config->has_pch_encoder) {
  10004. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  10005. &pipe_config->fdi_m_n);
  10006. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  10007. /*
  10008. * FDI already provided one idea for the dotclock.
  10009. * Yell if the encoder disagrees.
  10010. */
  10011. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  10012. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10013. fdi_dotclock, dotclock);
  10014. }
  10015. }
  10016. static void verify_wm_state(struct drm_crtc *crtc,
  10017. struct drm_crtc_state *new_state)
  10018. {
  10019. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  10020. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10021. struct skl_pipe_wm hw_wm, *sw_wm;
  10022. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  10023. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  10024. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10025. const enum pipe pipe = intel_crtc->pipe;
  10026. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  10027. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  10028. return;
  10029. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  10030. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  10031. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10032. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10033. /* planes */
  10034. for_each_universal_plane(dev_priv, pipe, plane) {
  10035. hw_plane_wm = &hw_wm.planes[plane];
  10036. sw_plane_wm = &sw_wm->planes[plane];
  10037. /* Watermarks */
  10038. for (level = 0; level <= max_level; level++) {
  10039. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  10040. &sw_plane_wm->wm[level]))
  10041. continue;
  10042. 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",
  10043. pipe_name(pipe), plane + 1, level,
  10044. sw_plane_wm->wm[level].plane_en,
  10045. sw_plane_wm->wm[level].plane_res_b,
  10046. sw_plane_wm->wm[level].plane_res_l,
  10047. hw_plane_wm->wm[level].plane_en,
  10048. hw_plane_wm->wm[level].plane_res_b,
  10049. hw_plane_wm->wm[level].plane_res_l);
  10050. }
  10051. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  10052. &sw_plane_wm->trans_wm)) {
  10053. 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",
  10054. pipe_name(pipe), plane + 1,
  10055. sw_plane_wm->trans_wm.plane_en,
  10056. sw_plane_wm->trans_wm.plane_res_b,
  10057. sw_plane_wm->trans_wm.plane_res_l,
  10058. hw_plane_wm->trans_wm.plane_en,
  10059. hw_plane_wm->trans_wm.plane_res_b,
  10060. hw_plane_wm->trans_wm.plane_res_l);
  10061. }
  10062. /* DDB */
  10063. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  10064. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  10065. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  10066. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  10067. pipe_name(pipe), plane + 1,
  10068. sw_ddb_entry->start, sw_ddb_entry->end,
  10069. hw_ddb_entry->start, hw_ddb_entry->end);
  10070. }
  10071. }
  10072. /*
  10073. * cursor
  10074. * If the cursor plane isn't active, we may not have updated it's ddb
  10075. * allocation. In that case since the ddb allocation will be updated
  10076. * once the plane becomes visible, we can skip this check
  10077. */
  10078. if (1) {
  10079. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  10080. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  10081. /* Watermarks */
  10082. for (level = 0; level <= max_level; level++) {
  10083. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  10084. &sw_plane_wm->wm[level]))
  10085. continue;
  10086. 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",
  10087. pipe_name(pipe), level,
  10088. sw_plane_wm->wm[level].plane_en,
  10089. sw_plane_wm->wm[level].plane_res_b,
  10090. sw_plane_wm->wm[level].plane_res_l,
  10091. hw_plane_wm->wm[level].plane_en,
  10092. hw_plane_wm->wm[level].plane_res_b,
  10093. hw_plane_wm->wm[level].plane_res_l);
  10094. }
  10095. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  10096. &sw_plane_wm->trans_wm)) {
  10097. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  10098. pipe_name(pipe),
  10099. sw_plane_wm->trans_wm.plane_en,
  10100. sw_plane_wm->trans_wm.plane_res_b,
  10101. sw_plane_wm->trans_wm.plane_res_l,
  10102. hw_plane_wm->trans_wm.plane_en,
  10103. hw_plane_wm->trans_wm.plane_res_b,
  10104. hw_plane_wm->trans_wm.plane_res_l);
  10105. }
  10106. /* DDB */
  10107. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10108. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10109. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  10110. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  10111. pipe_name(pipe),
  10112. sw_ddb_entry->start, sw_ddb_entry->end,
  10113. hw_ddb_entry->start, hw_ddb_entry->end);
  10114. }
  10115. }
  10116. }
  10117. static void
  10118. verify_connector_state(struct drm_device *dev,
  10119. struct drm_atomic_state *state,
  10120. struct drm_crtc *crtc)
  10121. {
  10122. struct drm_connector *connector;
  10123. struct drm_connector_state *new_conn_state;
  10124. int i;
  10125. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  10126. struct drm_encoder *encoder = connector->encoder;
  10127. struct drm_crtc_state *crtc_state = NULL;
  10128. if (new_conn_state->crtc != crtc)
  10129. continue;
  10130. if (crtc)
  10131. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  10132. intel_connector_verify_state(crtc_state, new_conn_state);
  10133. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  10134. "connector's atomic encoder doesn't match legacy encoder\n");
  10135. }
  10136. }
  10137. static void
  10138. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  10139. {
  10140. struct intel_encoder *encoder;
  10141. struct drm_connector *connector;
  10142. struct drm_connector_state *old_conn_state, *new_conn_state;
  10143. int i;
  10144. for_each_intel_encoder(dev, encoder) {
  10145. bool enabled = false, found = false;
  10146. enum pipe pipe;
  10147. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10148. encoder->base.base.id,
  10149. encoder->base.name);
  10150. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  10151. new_conn_state, i) {
  10152. if (old_conn_state->best_encoder == &encoder->base)
  10153. found = true;
  10154. if (new_conn_state->best_encoder != &encoder->base)
  10155. continue;
  10156. found = enabled = true;
  10157. I915_STATE_WARN(new_conn_state->crtc !=
  10158. encoder->base.crtc,
  10159. "connector's crtc doesn't match encoder crtc\n");
  10160. }
  10161. if (!found)
  10162. continue;
  10163. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10164. "encoder's enabled state mismatch "
  10165. "(expected %i, found %i)\n",
  10166. !!encoder->base.crtc, enabled);
  10167. if (!encoder->base.crtc) {
  10168. bool active;
  10169. active = encoder->get_hw_state(encoder, &pipe);
  10170. I915_STATE_WARN(active,
  10171. "encoder detached but still enabled on pipe %c.\n",
  10172. pipe_name(pipe));
  10173. }
  10174. }
  10175. }
  10176. static void
  10177. verify_crtc_state(struct drm_crtc *crtc,
  10178. struct drm_crtc_state *old_crtc_state,
  10179. struct drm_crtc_state *new_crtc_state)
  10180. {
  10181. struct drm_device *dev = crtc->dev;
  10182. struct drm_i915_private *dev_priv = to_i915(dev);
  10183. struct intel_encoder *encoder;
  10184. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10185. struct intel_crtc_state *pipe_config, *sw_config;
  10186. struct drm_atomic_state *old_state;
  10187. bool active;
  10188. old_state = old_crtc_state->state;
  10189. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10190. pipe_config = to_intel_crtc_state(old_crtc_state);
  10191. memset(pipe_config, 0, sizeof(*pipe_config));
  10192. pipe_config->base.crtc = crtc;
  10193. pipe_config->base.state = old_state;
  10194. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  10195. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10196. /* hw state is inconsistent with the pipe quirk */
  10197. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10198. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10199. active = new_crtc_state->active;
  10200. I915_STATE_WARN(new_crtc_state->active != active,
  10201. "crtc active state doesn't match with hw state "
  10202. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10203. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10204. "transitional active state does not match atomic hw state "
  10205. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10206. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10207. enum pipe pipe;
  10208. active = encoder->get_hw_state(encoder, &pipe);
  10209. I915_STATE_WARN(active != new_crtc_state->active,
  10210. "[ENCODER:%i] active %i with crtc active %i\n",
  10211. encoder->base.base.id, active, new_crtc_state->active);
  10212. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10213. "Encoder connected to wrong pipe %c\n",
  10214. pipe_name(pipe));
  10215. if (active) {
  10216. pipe_config->output_types |= 1 << encoder->type;
  10217. encoder->get_config(encoder, pipe_config);
  10218. }
  10219. }
  10220. intel_crtc_compute_pixel_rate(pipe_config);
  10221. if (!new_crtc_state->active)
  10222. return;
  10223. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10224. sw_config = to_intel_crtc_state(new_crtc_state);
  10225. if (!intel_pipe_config_compare(dev_priv, sw_config,
  10226. pipe_config, false)) {
  10227. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10228. intel_dump_pipe_config(intel_crtc, pipe_config,
  10229. "[hw state]");
  10230. intel_dump_pipe_config(intel_crtc, sw_config,
  10231. "[sw state]");
  10232. }
  10233. }
  10234. static void
  10235. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10236. struct intel_shared_dpll *pll,
  10237. struct drm_crtc *crtc,
  10238. struct drm_crtc_state *new_state)
  10239. {
  10240. struct intel_dpll_hw_state dpll_hw_state;
  10241. unsigned crtc_mask;
  10242. bool active;
  10243. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10244. DRM_DEBUG_KMS("%s\n", pll->name);
  10245. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10246. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10247. I915_STATE_WARN(!pll->on && pll->active_mask,
  10248. "pll in active use but not on in sw tracking\n");
  10249. I915_STATE_WARN(pll->on && !pll->active_mask,
  10250. "pll is on but not used by any active crtc\n");
  10251. I915_STATE_WARN(pll->on != active,
  10252. "pll on state mismatch (expected %i, found %i)\n",
  10253. pll->on, active);
  10254. }
  10255. if (!crtc) {
  10256. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  10257. "more active pll users than references: %x vs %x\n",
  10258. pll->active_mask, pll->state.crtc_mask);
  10259. return;
  10260. }
  10261. crtc_mask = 1 << drm_crtc_index(crtc);
  10262. if (new_state->active)
  10263. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10264. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10265. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10266. else
  10267. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10268. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10269. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10270. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  10271. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10272. crtc_mask, pll->state.crtc_mask);
  10273. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  10274. &dpll_hw_state,
  10275. sizeof(dpll_hw_state)),
  10276. "pll hw state mismatch\n");
  10277. }
  10278. static void
  10279. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10280. struct drm_crtc_state *old_crtc_state,
  10281. struct drm_crtc_state *new_crtc_state)
  10282. {
  10283. struct drm_i915_private *dev_priv = to_i915(dev);
  10284. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10285. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10286. if (new_state->shared_dpll)
  10287. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10288. if (old_state->shared_dpll &&
  10289. old_state->shared_dpll != new_state->shared_dpll) {
  10290. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  10291. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10292. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10293. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10294. pipe_name(drm_crtc_index(crtc)));
  10295. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  10296. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10297. pipe_name(drm_crtc_index(crtc)));
  10298. }
  10299. }
  10300. static void
  10301. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  10302. struct drm_atomic_state *state,
  10303. struct drm_crtc_state *old_state,
  10304. struct drm_crtc_state *new_state)
  10305. {
  10306. if (!needs_modeset(new_state) &&
  10307. !to_intel_crtc_state(new_state)->update_pipe)
  10308. return;
  10309. verify_wm_state(crtc, new_state);
  10310. verify_connector_state(crtc->dev, state, crtc);
  10311. verify_crtc_state(crtc, old_state, new_state);
  10312. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  10313. }
  10314. static void
  10315. verify_disabled_dpll_state(struct drm_device *dev)
  10316. {
  10317. struct drm_i915_private *dev_priv = to_i915(dev);
  10318. int i;
  10319. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10320. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  10321. }
  10322. static void
  10323. intel_modeset_verify_disabled(struct drm_device *dev,
  10324. struct drm_atomic_state *state)
  10325. {
  10326. verify_encoder_state(dev, state);
  10327. verify_connector_state(dev, state, NULL);
  10328. verify_disabled_dpll_state(dev);
  10329. }
  10330. static void update_scanline_offset(struct intel_crtc *crtc)
  10331. {
  10332. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10333. /*
  10334. * The scanline counter increments at the leading edge of hsync.
  10335. *
  10336. * On most platforms it starts counting from vtotal-1 on the
  10337. * first active line. That means the scanline counter value is
  10338. * always one less than what we would expect. Ie. just after
  10339. * start of vblank, which also occurs at start of hsync (on the
  10340. * last active line), the scanline counter will read vblank_start-1.
  10341. *
  10342. * On gen2 the scanline counter starts counting from 1 instead
  10343. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10344. * to keep the value positive), instead of adding one.
  10345. *
  10346. * On HSW+ the behaviour of the scanline counter depends on the output
  10347. * type. For DP ports it behaves like most other platforms, but on HDMI
  10348. * there's an extra 1 line difference. So we need to add two instead of
  10349. * one to the value.
  10350. */
  10351. if (IS_GEN2(dev_priv)) {
  10352. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10353. int vtotal;
  10354. vtotal = adjusted_mode->crtc_vtotal;
  10355. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10356. vtotal /= 2;
  10357. crtc->scanline_offset = vtotal - 1;
  10358. } else if (HAS_DDI(dev_priv) &&
  10359. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  10360. crtc->scanline_offset = 2;
  10361. } else
  10362. crtc->scanline_offset = 1;
  10363. }
  10364. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10365. {
  10366. struct drm_device *dev = state->dev;
  10367. struct drm_i915_private *dev_priv = to_i915(dev);
  10368. struct drm_crtc *crtc;
  10369. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10370. int i;
  10371. if (!dev_priv->display.crtc_compute_clock)
  10372. return;
  10373. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10374. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10375. struct intel_shared_dpll *old_dpll =
  10376. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  10377. if (!needs_modeset(new_crtc_state))
  10378. continue;
  10379. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  10380. if (!old_dpll)
  10381. continue;
  10382. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  10383. }
  10384. }
  10385. /*
  10386. * This implements the workaround described in the "notes" section of the mode
  10387. * set sequence documentation. When going from no pipes or single pipe to
  10388. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10389. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10390. */
  10391. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10392. {
  10393. struct drm_crtc_state *crtc_state;
  10394. struct intel_crtc *intel_crtc;
  10395. struct drm_crtc *crtc;
  10396. struct intel_crtc_state *first_crtc_state = NULL;
  10397. struct intel_crtc_state *other_crtc_state = NULL;
  10398. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10399. int i;
  10400. /* look at all crtc's that are going to be enabled in during modeset */
  10401. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  10402. intel_crtc = to_intel_crtc(crtc);
  10403. if (!crtc_state->active || !needs_modeset(crtc_state))
  10404. continue;
  10405. if (first_crtc_state) {
  10406. other_crtc_state = to_intel_crtc_state(crtc_state);
  10407. break;
  10408. } else {
  10409. first_crtc_state = to_intel_crtc_state(crtc_state);
  10410. first_pipe = intel_crtc->pipe;
  10411. }
  10412. }
  10413. /* No workaround needed? */
  10414. if (!first_crtc_state)
  10415. return 0;
  10416. /* w/a possibly needed, check how many crtc's are already enabled. */
  10417. for_each_intel_crtc(state->dev, intel_crtc) {
  10418. struct intel_crtc_state *pipe_config;
  10419. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10420. if (IS_ERR(pipe_config))
  10421. return PTR_ERR(pipe_config);
  10422. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10423. if (!pipe_config->base.active ||
  10424. needs_modeset(&pipe_config->base))
  10425. continue;
  10426. /* 2 or more enabled crtcs means no need for w/a */
  10427. if (enabled_pipe != INVALID_PIPE)
  10428. return 0;
  10429. enabled_pipe = intel_crtc->pipe;
  10430. }
  10431. if (enabled_pipe != INVALID_PIPE)
  10432. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10433. else if (other_crtc_state)
  10434. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10435. return 0;
  10436. }
  10437. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  10438. {
  10439. struct drm_crtc *crtc;
  10440. /* Add all pipes to the state */
  10441. for_each_crtc(state->dev, crtc) {
  10442. struct drm_crtc_state *crtc_state;
  10443. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10444. if (IS_ERR(crtc_state))
  10445. return PTR_ERR(crtc_state);
  10446. }
  10447. return 0;
  10448. }
  10449. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10450. {
  10451. struct drm_crtc *crtc;
  10452. /*
  10453. * Add all pipes to the state, and force
  10454. * a modeset on all the active ones.
  10455. */
  10456. for_each_crtc(state->dev, crtc) {
  10457. struct drm_crtc_state *crtc_state;
  10458. int ret;
  10459. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10460. if (IS_ERR(crtc_state))
  10461. return PTR_ERR(crtc_state);
  10462. if (!crtc_state->active || needs_modeset(crtc_state))
  10463. continue;
  10464. crtc_state->mode_changed = true;
  10465. ret = drm_atomic_add_affected_connectors(state, crtc);
  10466. if (ret)
  10467. return ret;
  10468. ret = drm_atomic_add_affected_planes(state, crtc);
  10469. if (ret)
  10470. return ret;
  10471. }
  10472. return 0;
  10473. }
  10474. static int intel_modeset_checks(struct drm_atomic_state *state)
  10475. {
  10476. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10477. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10478. struct drm_crtc *crtc;
  10479. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10480. int ret = 0, i;
  10481. if (!check_digital_port_conflicts(state)) {
  10482. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10483. return -EINVAL;
  10484. }
  10485. intel_state->modeset = true;
  10486. intel_state->active_crtcs = dev_priv->active_crtcs;
  10487. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10488. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  10489. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10490. if (new_crtc_state->active)
  10491. intel_state->active_crtcs |= 1 << i;
  10492. else
  10493. intel_state->active_crtcs &= ~(1 << i);
  10494. if (old_crtc_state->active != new_crtc_state->active)
  10495. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10496. }
  10497. /*
  10498. * See if the config requires any additional preparation, e.g.
  10499. * to adjust global state with pipes off. We need to do this
  10500. * here so we can get the modeset_pipe updated config for the new
  10501. * mode set on this crtc. For other crtcs we need to use the
  10502. * adjusted_mode bits in the crtc directly.
  10503. */
  10504. if (dev_priv->display.modeset_calc_cdclk) {
  10505. ret = dev_priv->display.modeset_calc_cdclk(state);
  10506. if (ret < 0)
  10507. return ret;
  10508. /*
  10509. * Writes to dev_priv->cdclk.logical must protected by
  10510. * holding all the crtc locks, even if we don't end up
  10511. * touching the hardware
  10512. */
  10513. if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
  10514. &intel_state->cdclk.logical)) {
  10515. ret = intel_lock_all_pipes(state);
  10516. if (ret < 0)
  10517. return ret;
  10518. }
  10519. /* All pipes must be switched off while we change the cdclk. */
  10520. if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
  10521. &intel_state->cdclk.actual)) {
  10522. ret = intel_modeset_all_pipes(state);
  10523. if (ret < 0)
  10524. return ret;
  10525. }
  10526. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10527. intel_state->cdclk.logical.cdclk,
  10528. intel_state->cdclk.actual.cdclk);
  10529. } else {
  10530. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10531. }
  10532. intel_modeset_clear_plls(state);
  10533. if (IS_HASWELL(dev_priv))
  10534. return haswell_mode_set_planes_workaround(state);
  10535. return 0;
  10536. }
  10537. /*
  10538. * Handle calculation of various watermark data at the end of the atomic check
  10539. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10540. * handlers to ensure that all derived state has been updated.
  10541. */
  10542. static int calc_watermark_data(struct drm_atomic_state *state)
  10543. {
  10544. struct drm_device *dev = state->dev;
  10545. struct drm_i915_private *dev_priv = to_i915(dev);
  10546. /* Is there platform-specific watermark information to calculate? */
  10547. if (dev_priv->display.compute_global_watermarks)
  10548. return dev_priv->display.compute_global_watermarks(state);
  10549. return 0;
  10550. }
  10551. /**
  10552. * intel_atomic_check - validate state object
  10553. * @dev: drm device
  10554. * @state: state to validate
  10555. */
  10556. static int intel_atomic_check(struct drm_device *dev,
  10557. struct drm_atomic_state *state)
  10558. {
  10559. struct drm_i915_private *dev_priv = to_i915(dev);
  10560. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10561. struct drm_crtc *crtc;
  10562. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10563. int ret, i;
  10564. bool any_ms = false;
  10565. ret = drm_atomic_helper_check_modeset(dev, state);
  10566. if (ret)
  10567. return ret;
  10568. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10569. struct intel_crtc_state *pipe_config =
  10570. to_intel_crtc_state(crtc_state);
  10571. /* Catch I915_MODE_FLAG_INHERITED */
  10572. if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
  10573. crtc_state->mode_changed = true;
  10574. if (!needs_modeset(crtc_state))
  10575. continue;
  10576. if (!crtc_state->enable) {
  10577. any_ms = true;
  10578. continue;
  10579. }
  10580. /* FIXME: For only active_changed we shouldn't need to do any
  10581. * state recomputation at all. */
  10582. ret = drm_atomic_add_affected_connectors(state, crtc);
  10583. if (ret)
  10584. return ret;
  10585. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10586. if (ret) {
  10587. intel_dump_pipe_config(to_intel_crtc(crtc),
  10588. pipe_config, "[failed]");
  10589. return ret;
  10590. }
  10591. if (i915.fastboot &&
  10592. intel_pipe_config_compare(dev_priv,
  10593. to_intel_crtc_state(old_crtc_state),
  10594. pipe_config, true)) {
  10595. crtc_state->mode_changed = false;
  10596. pipe_config->update_pipe = true;
  10597. }
  10598. if (needs_modeset(crtc_state))
  10599. any_ms = true;
  10600. ret = drm_atomic_add_affected_planes(state, crtc);
  10601. if (ret)
  10602. return ret;
  10603. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10604. needs_modeset(crtc_state) ?
  10605. "[modeset]" : "[fastset]");
  10606. }
  10607. if (any_ms) {
  10608. ret = intel_modeset_checks(state);
  10609. if (ret)
  10610. return ret;
  10611. } else {
  10612. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10613. }
  10614. ret = drm_atomic_helper_check_planes(dev, state);
  10615. if (ret)
  10616. return ret;
  10617. intel_fbc_choose_crtc(dev_priv, state);
  10618. return calc_watermark_data(state);
  10619. }
  10620. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10621. struct drm_atomic_state *state)
  10622. {
  10623. struct drm_i915_private *dev_priv = to_i915(dev);
  10624. struct drm_crtc_state *crtc_state;
  10625. struct drm_crtc *crtc;
  10626. int i, ret;
  10627. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  10628. if (state->legacy_cursor_update)
  10629. continue;
  10630. ret = intel_crtc_wait_for_pending_flips(crtc);
  10631. if (ret)
  10632. return ret;
  10633. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  10634. flush_workqueue(dev_priv->wq);
  10635. }
  10636. ret = mutex_lock_interruptible(&dev->struct_mutex);
  10637. if (ret)
  10638. return ret;
  10639. ret = drm_atomic_helper_prepare_planes(dev, state);
  10640. mutex_unlock(&dev->struct_mutex);
  10641. return ret;
  10642. }
  10643. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10644. {
  10645. struct drm_device *dev = crtc->base.dev;
  10646. if (!dev->max_vblank_count)
  10647. return drm_accurate_vblank_count(&crtc->base);
  10648. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10649. }
  10650. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  10651. struct drm_i915_private *dev_priv,
  10652. unsigned crtc_mask)
  10653. {
  10654. unsigned last_vblank_count[I915_MAX_PIPES];
  10655. enum pipe pipe;
  10656. int ret;
  10657. if (!crtc_mask)
  10658. return;
  10659. for_each_pipe(dev_priv, pipe) {
  10660. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10661. pipe);
  10662. if (!((1 << pipe) & crtc_mask))
  10663. continue;
  10664. ret = drm_crtc_vblank_get(&crtc->base);
  10665. if (WARN_ON(ret != 0)) {
  10666. crtc_mask &= ~(1 << pipe);
  10667. continue;
  10668. }
  10669. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  10670. }
  10671. for_each_pipe(dev_priv, pipe) {
  10672. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10673. pipe);
  10674. long lret;
  10675. if (!((1 << pipe) & crtc_mask))
  10676. continue;
  10677. lret = wait_event_timeout(dev->vblank[pipe].queue,
  10678. last_vblank_count[pipe] !=
  10679. drm_crtc_vblank_count(&crtc->base),
  10680. msecs_to_jiffies(50));
  10681. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  10682. drm_crtc_vblank_put(&crtc->base);
  10683. }
  10684. }
  10685. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  10686. {
  10687. /* fb updated, need to unpin old fb */
  10688. if (crtc_state->fb_changed)
  10689. return true;
  10690. /* wm changes, need vblank before final wm's */
  10691. if (crtc_state->update_wm_post)
  10692. return true;
  10693. if (crtc_state->wm.need_postvbl_update)
  10694. return true;
  10695. return false;
  10696. }
  10697. static void intel_update_crtc(struct drm_crtc *crtc,
  10698. struct drm_atomic_state *state,
  10699. struct drm_crtc_state *old_crtc_state,
  10700. struct drm_crtc_state *new_crtc_state,
  10701. unsigned int *crtc_vblank_mask)
  10702. {
  10703. struct drm_device *dev = crtc->dev;
  10704. struct drm_i915_private *dev_priv = to_i915(dev);
  10705. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10706. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10707. bool modeset = needs_modeset(new_crtc_state);
  10708. if (modeset) {
  10709. update_scanline_offset(intel_crtc);
  10710. dev_priv->display.crtc_enable(pipe_config, state);
  10711. } else {
  10712. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10713. pipe_config);
  10714. }
  10715. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10716. intel_fbc_enable(
  10717. intel_crtc, pipe_config,
  10718. to_intel_plane_state(crtc->primary->state));
  10719. }
  10720. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10721. if (needs_vblank_wait(pipe_config))
  10722. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  10723. }
  10724. static void intel_update_crtcs(struct drm_atomic_state *state,
  10725. unsigned int *crtc_vblank_mask)
  10726. {
  10727. struct drm_crtc *crtc;
  10728. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10729. int i;
  10730. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10731. if (!new_crtc_state->active)
  10732. continue;
  10733. intel_update_crtc(crtc, state, old_crtc_state,
  10734. new_crtc_state, crtc_vblank_mask);
  10735. }
  10736. }
  10737. static void skl_update_crtcs(struct drm_atomic_state *state,
  10738. unsigned int *crtc_vblank_mask)
  10739. {
  10740. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10741. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10742. struct drm_crtc *crtc;
  10743. struct intel_crtc *intel_crtc;
  10744. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10745. struct intel_crtc_state *cstate;
  10746. unsigned int updated = 0;
  10747. bool progress;
  10748. enum pipe pipe;
  10749. int i;
  10750. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10751. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10752. /* ignore allocations for crtc's that have been turned off. */
  10753. if (new_crtc_state->active)
  10754. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10755. /*
  10756. * Whenever the number of active pipes changes, we need to make sure we
  10757. * update the pipes in the right order so that their ddb allocations
  10758. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10759. * cause pipe underruns and other bad stuff.
  10760. */
  10761. do {
  10762. progress = false;
  10763. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10764. bool vbl_wait = false;
  10765. unsigned int cmask = drm_crtc_mask(crtc);
  10766. intel_crtc = to_intel_crtc(crtc);
  10767. cstate = to_intel_crtc_state(crtc->state);
  10768. pipe = intel_crtc->pipe;
  10769. if (updated & cmask || !cstate->base.active)
  10770. continue;
  10771. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  10772. continue;
  10773. updated |= cmask;
  10774. entries[i] = &cstate->wm.skl.ddb;
  10775. /*
  10776. * If this is an already active pipe, it's DDB changed,
  10777. * and this isn't the last pipe that needs updating
  10778. * then we need to wait for a vblank to pass for the
  10779. * new ddb allocation to take effect.
  10780. */
  10781. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10782. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10783. !new_crtc_state->active_changed &&
  10784. intel_state->wm_results.dirty_pipes != updated)
  10785. vbl_wait = true;
  10786. intel_update_crtc(crtc, state, old_crtc_state,
  10787. new_crtc_state, crtc_vblank_mask);
  10788. if (vbl_wait)
  10789. intel_wait_for_vblank(dev_priv, pipe);
  10790. progress = true;
  10791. }
  10792. } while (progress);
  10793. }
  10794. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10795. {
  10796. struct intel_atomic_state *state, *next;
  10797. struct llist_node *freed;
  10798. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10799. llist_for_each_entry_safe(state, next, freed, freed)
  10800. drm_atomic_state_put(&state->base);
  10801. }
  10802. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10803. {
  10804. struct drm_i915_private *dev_priv =
  10805. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10806. intel_atomic_helper_free_state(dev_priv);
  10807. }
  10808. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10809. {
  10810. struct drm_device *dev = state->dev;
  10811. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10812. struct drm_i915_private *dev_priv = to_i915(dev);
  10813. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10814. struct drm_crtc *crtc;
  10815. struct intel_crtc_state *intel_cstate;
  10816. bool hw_check = intel_state->modeset;
  10817. u64 put_domains[I915_MAX_PIPES] = {};
  10818. unsigned crtc_vblank_mask = 0;
  10819. int i;
  10820. drm_atomic_helper_wait_for_dependencies(state);
  10821. if (intel_state->modeset)
  10822. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10823. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10824. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10825. if (needs_modeset(new_crtc_state) ||
  10826. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10827. hw_check = true;
  10828. put_domains[to_intel_crtc(crtc)->pipe] =
  10829. modeset_get_crtc_power_domains(crtc,
  10830. to_intel_crtc_state(new_crtc_state));
  10831. }
  10832. if (!needs_modeset(new_crtc_state))
  10833. continue;
  10834. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10835. to_intel_crtc_state(new_crtc_state));
  10836. if (old_crtc_state->active) {
  10837. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10838. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10839. intel_crtc->active = false;
  10840. intel_fbc_disable(intel_crtc);
  10841. intel_disable_shared_dpll(intel_crtc);
  10842. /*
  10843. * Underruns don't always raise
  10844. * interrupts, so check manually.
  10845. */
  10846. intel_check_cpu_fifo_underruns(dev_priv);
  10847. intel_check_pch_fifo_underruns(dev_priv);
  10848. if (!crtc->state->active) {
  10849. /*
  10850. * Make sure we don't call initial_watermarks
  10851. * for ILK-style watermark updates.
  10852. *
  10853. * No clue what this is supposed to achieve.
  10854. */
  10855. if (INTEL_GEN(dev_priv) >= 9)
  10856. dev_priv->display.initial_watermarks(intel_state,
  10857. to_intel_crtc_state(crtc->state));
  10858. }
  10859. }
  10860. }
  10861. /* Only after disabling all output pipelines that will be changed can we
  10862. * update the the output configuration. */
  10863. intel_modeset_update_crtc_state(state);
  10864. if (intel_state->modeset) {
  10865. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10866. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10867. /*
  10868. * SKL workaround: bspec recommends we disable the SAGV when we
  10869. * have more then one pipe enabled
  10870. */
  10871. if (!intel_can_enable_sagv(state))
  10872. intel_disable_sagv(dev_priv);
  10873. intel_modeset_verify_disabled(dev, state);
  10874. }
  10875. /* Complete the events for pipes that have now been disabled */
  10876. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10877. bool modeset = needs_modeset(new_crtc_state);
  10878. /* Complete events for now disable pipes here. */
  10879. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10880. spin_lock_irq(&dev->event_lock);
  10881. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10882. spin_unlock_irq(&dev->event_lock);
  10883. new_crtc_state->event = NULL;
  10884. }
  10885. }
  10886. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10887. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  10888. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10889. * already, but still need the state for the delayed optimization. To
  10890. * fix this:
  10891. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10892. * - schedule that vblank worker _before_ calling hw_done
  10893. * - at the start of commit_tail, cancel it _synchrously
  10894. * - switch over to the vblank wait helper in the core after that since
  10895. * we don't need out special handling any more.
  10896. */
  10897. if (!state->legacy_cursor_update)
  10898. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  10899. /*
  10900. * Now that the vblank has passed, we can go ahead and program the
  10901. * optimal watermarks on platforms that need two-step watermark
  10902. * programming.
  10903. *
  10904. * TODO: Move this (and other cleanup) to an async worker eventually.
  10905. */
  10906. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10907. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10908. if (dev_priv->display.optimize_watermarks)
  10909. dev_priv->display.optimize_watermarks(intel_state,
  10910. intel_cstate);
  10911. }
  10912. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10913. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10914. if (put_domains[i])
  10915. modeset_put_power_domains(dev_priv, put_domains[i]);
  10916. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10917. }
  10918. if (intel_state->modeset && intel_can_enable_sagv(state))
  10919. intel_enable_sagv(dev_priv);
  10920. drm_atomic_helper_commit_hw_done(state);
  10921. if (intel_state->modeset)
  10922. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10923. mutex_lock(&dev->struct_mutex);
  10924. drm_atomic_helper_cleanup_planes(dev, state);
  10925. mutex_unlock(&dev->struct_mutex);
  10926. drm_atomic_helper_commit_cleanup_done(state);
  10927. drm_atomic_state_put(state);
  10928. /* As one of the primary mmio accessors, KMS has a high likelihood
  10929. * of triggering bugs in unclaimed access. After we finish
  10930. * modesetting, see if an error has been flagged, and if so
  10931. * enable debugging for the next modeset - and hope we catch
  10932. * the culprit.
  10933. *
  10934. * XXX note that we assume display power is on at this point.
  10935. * This might hold true now but we need to add pm helper to check
  10936. * unclaimed only when the hardware is on, as atomic commits
  10937. * can happen also when the device is completely off.
  10938. */
  10939. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10940. intel_atomic_helper_free_state(dev_priv);
  10941. }
  10942. static void intel_atomic_commit_work(struct work_struct *work)
  10943. {
  10944. struct drm_atomic_state *state =
  10945. container_of(work, struct drm_atomic_state, commit_work);
  10946. intel_atomic_commit_tail(state);
  10947. }
  10948. static int __i915_sw_fence_call
  10949. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10950. enum i915_sw_fence_notify notify)
  10951. {
  10952. struct intel_atomic_state *state =
  10953. container_of(fence, struct intel_atomic_state, commit_ready);
  10954. switch (notify) {
  10955. case FENCE_COMPLETE:
  10956. if (state->base.commit_work.func)
  10957. queue_work(system_unbound_wq, &state->base.commit_work);
  10958. break;
  10959. case FENCE_FREE:
  10960. {
  10961. struct intel_atomic_helper *helper =
  10962. &to_i915(state->base.dev)->atomic_helper;
  10963. if (llist_add(&state->freed, &helper->free_list))
  10964. schedule_work(&helper->free_work);
  10965. break;
  10966. }
  10967. }
  10968. return NOTIFY_DONE;
  10969. }
  10970. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10971. {
  10972. struct drm_plane_state *old_plane_state, *new_plane_state;
  10973. struct drm_plane *plane;
  10974. int i;
  10975. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10976. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10977. intel_fb_obj(new_plane_state->fb),
  10978. to_intel_plane(plane)->frontbuffer_bit);
  10979. }
  10980. /**
  10981. * intel_atomic_commit - commit validated state object
  10982. * @dev: DRM device
  10983. * @state: the top-level driver state object
  10984. * @nonblock: nonblocking commit
  10985. *
  10986. * This function commits a top-level state object that has been validated
  10987. * with drm_atomic_helper_check().
  10988. *
  10989. * RETURNS
  10990. * Zero for success or -errno.
  10991. */
  10992. static int intel_atomic_commit(struct drm_device *dev,
  10993. struct drm_atomic_state *state,
  10994. bool nonblock)
  10995. {
  10996. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10997. struct drm_i915_private *dev_priv = to_i915(dev);
  10998. int ret = 0;
  10999. ret = drm_atomic_helper_setup_commit(state, nonblock);
  11000. if (ret)
  11001. return ret;
  11002. drm_atomic_state_get(state);
  11003. i915_sw_fence_init(&intel_state->commit_ready,
  11004. intel_atomic_commit_ready);
  11005. ret = intel_atomic_prepare_commit(dev, state);
  11006. if (ret) {
  11007. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11008. i915_sw_fence_commit(&intel_state->commit_ready);
  11009. return ret;
  11010. }
  11011. /*
  11012. * The intel_legacy_cursor_update() fast path takes care
  11013. * of avoiding the vblank waits for simple cursor
  11014. * movement and flips. For cursor on/off and size changes,
  11015. * we want to perform the vblank waits so that watermark
  11016. * updates happen during the correct frames. Gen9+ have
  11017. * double buffered watermarks and so shouldn't need this.
  11018. *
  11019. * Do this after drm_atomic_helper_setup_commit() and
  11020. * intel_atomic_prepare_commit() because we still want
  11021. * to skip the flip and fb cleanup waits. Although that
  11022. * does risk yanking the mapping from under the display
  11023. * engine.
  11024. *
  11025. * FIXME doing watermarks and fb cleanup from a vblank worker
  11026. * (assuming we had any) would solve these problems.
  11027. */
  11028. if (INTEL_GEN(dev_priv) < 9)
  11029. state->legacy_cursor_update = false;
  11030. drm_atomic_helper_swap_state(state, true);
  11031. dev_priv->wm.distrust_bios_wm = false;
  11032. intel_shared_dpll_swap_state(state);
  11033. intel_atomic_track_fbs(state);
  11034. if (intel_state->modeset) {
  11035. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  11036. sizeof(intel_state->min_pixclk));
  11037. dev_priv->active_crtcs = intel_state->active_crtcs;
  11038. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  11039. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  11040. }
  11041. drm_atomic_state_get(state);
  11042. INIT_WORK(&state->commit_work,
  11043. nonblock ? intel_atomic_commit_work : NULL);
  11044. i915_sw_fence_commit(&intel_state->commit_ready);
  11045. if (!nonblock) {
  11046. i915_sw_fence_wait(&intel_state->commit_ready);
  11047. intel_atomic_commit_tail(state);
  11048. }
  11049. return 0;
  11050. }
  11051. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11052. {
  11053. struct drm_device *dev = crtc->dev;
  11054. struct drm_atomic_state *state;
  11055. struct drm_crtc_state *crtc_state;
  11056. int ret;
  11057. state = drm_atomic_state_alloc(dev);
  11058. if (!state) {
  11059. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  11060. crtc->base.id, crtc->name);
  11061. return;
  11062. }
  11063. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  11064. retry:
  11065. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11066. ret = PTR_ERR_OR_ZERO(crtc_state);
  11067. if (!ret) {
  11068. if (!crtc_state->active)
  11069. goto out;
  11070. crtc_state->mode_changed = true;
  11071. ret = drm_atomic_commit(state);
  11072. }
  11073. if (ret == -EDEADLK) {
  11074. drm_atomic_state_clear(state);
  11075. drm_modeset_backoff(state->acquire_ctx);
  11076. goto retry;
  11077. }
  11078. out:
  11079. drm_atomic_state_put(state);
  11080. }
  11081. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11082. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  11083. .set_config = drm_atomic_helper_set_config,
  11084. .set_property = drm_atomic_helper_crtc_set_property,
  11085. .destroy = intel_crtc_destroy,
  11086. .page_flip = drm_atomic_helper_page_flip,
  11087. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11088. .atomic_destroy_state = intel_crtc_destroy_state,
  11089. .set_crc_source = intel_crtc_set_crc_source,
  11090. };
  11091. /**
  11092. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11093. * @plane: drm plane to prepare for
  11094. * @fb: framebuffer to prepare for presentation
  11095. *
  11096. * Prepares a framebuffer for usage on a display plane. Generally this
  11097. * involves pinning the underlying object and updating the frontbuffer tracking
  11098. * bits. Some older platforms need special physical address handling for
  11099. * cursor planes.
  11100. *
  11101. * Must be called with struct_mutex held.
  11102. *
  11103. * Returns 0 on success, negative error code on failure.
  11104. */
  11105. int
  11106. intel_prepare_plane_fb(struct drm_plane *plane,
  11107. struct drm_plane_state *new_state)
  11108. {
  11109. struct intel_atomic_state *intel_state =
  11110. to_intel_atomic_state(new_state->state);
  11111. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11112. struct drm_framebuffer *fb = new_state->fb;
  11113. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11114. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11115. int ret;
  11116. if (obj) {
  11117. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11118. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  11119. const int align = intel_cursor_alignment(dev_priv);
  11120. ret = i915_gem_object_attach_phys(obj, align);
  11121. if (ret) {
  11122. DRM_DEBUG_KMS("failed to attach phys object\n");
  11123. return ret;
  11124. }
  11125. } else {
  11126. struct i915_vma *vma;
  11127. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  11128. if (IS_ERR(vma)) {
  11129. DRM_DEBUG_KMS("failed to pin object\n");
  11130. return PTR_ERR(vma);
  11131. }
  11132. to_intel_plane_state(new_state)->vma = vma;
  11133. }
  11134. }
  11135. if (!obj && !old_obj)
  11136. return 0;
  11137. if (old_obj) {
  11138. struct drm_crtc_state *crtc_state =
  11139. drm_atomic_get_existing_crtc_state(new_state->state,
  11140. plane->state->crtc);
  11141. /* Big Hammer, we also need to ensure that any pending
  11142. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11143. * current scanout is retired before unpinning the old
  11144. * framebuffer. Note that we rely on userspace rendering
  11145. * into the buffer attached to the pipe they are waiting
  11146. * on. If not, userspace generates a GPU hang with IPEHR
  11147. * point to the MI_WAIT_FOR_EVENT.
  11148. *
  11149. * This should only fail upon a hung GPU, in which case we
  11150. * can safely continue.
  11151. */
  11152. if (needs_modeset(crtc_state)) {
  11153. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  11154. old_obj->resv, NULL,
  11155. false, 0,
  11156. GFP_KERNEL);
  11157. if (ret < 0)
  11158. return ret;
  11159. }
  11160. }
  11161. if (new_state->fence) { /* explicit fencing */
  11162. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  11163. new_state->fence,
  11164. I915_FENCE_TIMEOUT,
  11165. GFP_KERNEL);
  11166. if (ret < 0)
  11167. return ret;
  11168. }
  11169. if (!obj)
  11170. return 0;
  11171. if (!new_state->fence) { /* implicit fencing */
  11172. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  11173. obj->resv, NULL,
  11174. false, I915_FENCE_TIMEOUT,
  11175. GFP_KERNEL);
  11176. if (ret < 0)
  11177. return ret;
  11178. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  11179. }
  11180. return 0;
  11181. }
  11182. /**
  11183. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11184. * @plane: drm plane to clean up for
  11185. * @fb: old framebuffer that was on plane
  11186. *
  11187. * Cleans up a framebuffer that has just been removed from a plane.
  11188. *
  11189. * Must be called with struct_mutex held.
  11190. */
  11191. void
  11192. intel_cleanup_plane_fb(struct drm_plane *plane,
  11193. struct drm_plane_state *old_state)
  11194. {
  11195. struct i915_vma *vma;
  11196. /* Should only be called after a successful intel_prepare_plane_fb()! */
  11197. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  11198. if (vma)
  11199. intel_unpin_fb_vma(vma);
  11200. }
  11201. int
  11202. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11203. {
  11204. struct drm_i915_private *dev_priv;
  11205. int max_scale;
  11206. int crtc_clock, max_dotclk;
  11207. if (!intel_crtc || !crtc_state->base.enable)
  11208. return DRM_PLANE_HELPER_NO_SCALING;
  11209. dev_priv = to_i915(intel_crtc->base.dev);
  11210. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11211. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  11212. if (IS_GEMINILAKE(dev_priv))
  11213. max_dotclk *= 2;
  11214. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  11215. return DRM_PLANE_HELPER_NO_SCALING;
  11216. /*
  11217. * skl max scale is lower of:
  11218. * close to 3 but not 3, -1 is for that purpose
  11219. * or
  11220. * cdclk/crtc_clock
  11221. */
  11222. max_scale = min((1 << 16) * 3 - 1,
  11223. (1 << 8) * ((max_dotclk << 8) / crtc_clock));
  11224. return max_scale;
  11225. }
  11226. static int
  11227. intel_check_primary_plane(struct intel_plane *plane,
  11228. struct intel_crtc_state *crtc_state,
  11229. struct intel_plane_state *state)
  11230. {
  11231. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  11232. struct drm_crtc *crtc = state->base.crtc;
  11233. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11234. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11235. bool can_position = false;
  11236. int ret;
  11237. if (INTEL_GEN(dev_priv) >= 9) {
  11238. /* use scaler when colorkey is not required */
  11239. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11240. min_scale = 1;
  11241. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11242. }
  11243. can_position = true;
  11244. }
  11245. ret = drm_plane_helper_check_state(&state->base,
  11246. &state->clip,
  11247. min_scale, max_scale,
  11248. can_position, true);
  11249. if (ret)
  11250. return ret;
  11251. if (!state->base.fb)
  11252. return 0;
  11253. if (INTEL_GEN(dev_priv) >= 9) {
  11254. ret = skl_check_plane_surface(state);
  11255. if (ret)
  11256. return ret;
  11257. state->ctl = skl_plane_ctl(crtc_state, state);
  11258. } else {
  11259. ret = i9xx_check_plane_surface(state);
  11260. if (ret)
  11261. return ret;
  11262. state->ctl = i9xx_plane_ctl(crtc_state, state);
  11263. }
  11264. return 0;
  11265. }
  11266. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11267. struct drm_crtc_state *old_crtc_state)
  11268. {
  11269. struct drm_device *dev = crtc->dev;
  11270. struct drm_i915_private *dev_priv = to_i915(dev);
  11271. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11272. struct intel_crtc_state *intel_cstate =
  11273. to_intel_crtc_state(crtc->state);
  11274. struct intel_crtc_state *old_intel_cstate =
  11275. to_intel_crtc_state(old_crtc_state);
  11276. struct intel_atomic_state *old_intel_state =
  11277. to_intel_atomic_state(old_crtc_state->state);
  11278. bool modeset = needs_modeset(crtc->state);
  11279. if (!modeset &&
  11280. (intel_cstate->base.color_mgmt_changed ||
  11281. intel_cstate->update_pipe)) {
  11282. intel_color_set_csc(crtc->state);
  11283. intel_color_load_luts(crtc->state);
  11284. }
  11285. /* Perform vblank evasion around commit operation */
  11286. intel_pipe_update_start(intel_crtc);
  11287. if (modeset)
  11288. goto out;
  11289. if (intel_cstate->update_pipe)
  11290. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  11291. else if (INTEL_GEN(dev_priv) >= 9)
  11292. skl_detach_scalers(intel_crtc);
  11293. out:
  11294. if (dev_priv->display.atomic_update_watermarks)
  11295. dev_priv->display.atomic_update_watermarks(old_intel_state,
  11296. intel_cstate);
  11297. }
  11298. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11299. struct drm_crtc_state *old_crtc_state)
  11300. {
  11301. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11302. intel_pipe_update_end(intel_crtc, NULL);
  11303. }
  11304. /**
  11305. * intel_plane_destroy - destroy a plane
  11306. * @plane: plane to destroy
  11307. *
  11308. * Common destruction function for all types of planes (primary, cursor,
  11309. * sprite).
  11310. */
  11311. void intel_plane_destroy(struct drm_plane *plane)
  11312. {
  11313. drm_plane_cleanup(plane);
  11314. kfree(to_intel_plane(plane));
  11315. }
  11316. const struct drm_plane_funcs intel_plane_funcs = {
  11317. .update_plane = drm_atomic_helper_update_plane,
  11318. .disable_plane = drm_atomic_helper_disable_plane,
  11319. .destroy = intel_plane_destroy,
  11320. .set_property = drm_atomic_helper_plane_set_property,
  11321. .atomic_get_property = intel_plane_atomic_get_property,
  11322. .atomic_set_property = intel_plane_atomic_set_property,
  11323. .atomic_duplicate_state = intel_plane_duplicate_state,
  11324. .atomic_destroy_state = intel_plane_destroy_state,
  11325. };
  11326. static int
  11327. intel_legacy_cursor_update(struct drm_plane *plane,
  11328. struct drm_crtc *crtc,
  11329. struct drm_framebuffer *fb,
  11330. int crtc_x, int crtc_y,
  11331. unsigned int crtc_w, unsigned int crtc_h,
  11332. uint32_t src_x, uint32_t src_y,
  11333. uint32_t src_w, uint32_t src_h,
  11334. struct drm_modeset_acquire_ctx *ctx)
  11335. {
  11336. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11337. int ret;
  11338. struct drm_plane_state *old_plane_state, *new_plane_state;
  11339. struct intel_plane *intel_plane = to_intel_plane(plane);
  11340. struct drm_framebuffer *old_fb;
  11341. struct drm_crtc_state *crtc_state = crtc->state;
  11342. struct i915_vma *old_vma;
  11343. /*
  11344. * When crtc is inactive or there is a modeset pending,
  11345. * wait for it to complete in the slowpath
  11346. */
  11347. if (!crtc_state->active || needs_modeset(crtc_state) ||
  11348. to_intel_crtc_state(crtc_state)->update_pipe)
  11349. goto slow;
  11350. old_plane_state = plane->state;
  11351. /*
  11352. * If any parameters change that may affect watermarks,
  11353. * take the slowpath. Only changing fb or position should be
  11354. * in the fastpath.
  11355. */
  11356. if (old_plane_state->crtc != crtc ||
  11357. old_plane_state->src_w != src_w ||
  11358. old_plane_state->src_h != src_h ||
  11359. old_plane_state->crtc_w != crtc_w ||
  11360. old_plane_state->crtc_h != crtc_h ||
  11361. !old_plane_state->fb != !fb)
  11362. goto slow;
  11363. new_plane_state = intel_plane_duplicate_state(plane);
  11364. if (!new_plane_state)
  11365. return -ENOMEM;
  11366. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  11367. new_plane_state->src_x = src_x;
  11368. new_plane_state->src_y = src_y;
  11369. new_plane_state->src_w = src_w;
  11370. new_plane_state->src_h = src_h;
  11371. new_plane_state->crtc_x = crtc_x;
  11372. new_plane_state->crtc_y = crtc_y;
  11373. new_plane_state->crtc_w = crtc_w;
  11374. new_plane_state->crtc_h = crtc_h;
  11375. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  11376. to_intel_plane_state(new_plane_state));
  11377. if (ret)
  11378. goto out_free;
  11379. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  11380. if (ret)
  11381. goto out_free;
  11382. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  11383. int align = intel_cursor_alignment(dev_priv);
  11384. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  11385. if (ret) {
  11386. DRM_DEBUG_KMS("failed to attach phys object\n");
  11387. goto out_unlock;
  11388. }
  11389. } else {
  11390. struct i915_vma *vma;
  11391. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  11392. if (IS_ERR(vma)) {
  11393. DRM_DEBUG_KMS("failed to pin object\n");
  11394. ret = PTR_ERR(vma);
  11395. goto out_unlock;
  11396. }
  11397. to_intel_plane_state(new_plane_state)->vma = vma;
  11398. }
  11399. old_fb = old_plane_state->fb;
  11400. old_vma = to_intel_plane_state(old_plane_state)->vma;
  11401. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  11402. intel_plane->frontbuffer_bit);
  11403. /* Swap plane state */
  11404. new_plane_state->fence = old_plane_state->fence;
  11405. *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
  11406. new_plane_state->fence = NULL;
  11407. new_plane_state->fb = old_fb;
  11408. to_intel_plane_state(new_plane_state)->vma = old_vma;
  11409. if (plane->state->visible) {
  11410. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  11411. intel_plane->update_plane(intel_plane,
  11412. to_intel_crtc_state(crtc->state),
  11413. to_intel_plane_state(plane->state));
  11414. } else {
  11415. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  11416. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  11417. }
  11418. intel_cleanup_plane_fb(plane, new_plane_state);
  11419. out_unlock:
  11420. mutex_unlock(&dev_priv->drm.struct_mutex);
  11421. out_free:
  11422. intel_plane_destroy_state(plane, new_plane_state);
  11423. return ret;
  11424. slow:
  11425. return drm_atomic_helper_update_plane(plane, crtc, fb,
  11426. crtc_x, crtc_y, crtc_w, crtc_h,
  11427. src_x, src_y, src_w, src_h, ctx);
  11428. }
  11429. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  11430. .update_plane = intel_legacy_cursor_update,
  11431. .disable_plane = drm_atomic_helper_disable_plane,
  11432. .destroy = intel_plane_destroy,
  11433. .set_property = drm_atomic_helper_plane_set_property,
  11434. .atomic_get_property = intel_plane_atomic_get_property,
  11435. .atomic_set_property = intel_plane_atomic_set_property,
  11436. .atomic_duplicate_state = intel_plane_duplicate_state,
  11437. .atomic_destroy_state = intel_plane_destroy_state,
  11438. };
  11439. static struct intel_plane *
  11440. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11441. {
  11442. struct intel_plane *primary = NULL;
  11443. struct intel_plane_state *state = NULL;
  11444. const uint32_t *intel_primary_formats;
  11445. unsigned int supported_rotations;
  11446. unsigned int num_formats;
  11447. int ret;
  11448. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11449. if (!primary) {
  11450. ret = -ENOMEM;
  11451. goto fail;
  11452. }
  11453. state = intel_create_plane_state(&primary->base);
  11454. if (!state) {
  11455. ret = -ENOMEM;
  11456. goto fail;
  11457. }
  11458. primary->base.state = &state->base;
  11459. primary->can_scale = false;
  11460. primary->max_downscale = 1;
  11461. if (INTEL_GEN(dev_priv) >= 9) {
  11462. primary->can_scale = true;
  11463. state->scaler_id = -1;
  11464. }
  11465. primary->pipe = pipe;
  11466. /*
  11467. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11468. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11469. */
  11470. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11471. primary->plane = (enum plane) !pipe;
  11472. else
  11473. primary->plane = (enum plane) pipe;
  11474. primary->id = PLANE_PRIMARY;
  11475. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11476. primary->check_plane = intel_check_primary_plane;
  11477. if (INTEL_GEN(dev_priv) >= 9) {
  11478. intel_primary_formats = skl_primary_formats;
  11479. num_formats = ARRAY_SIZE(skl_primary_formats);
  11480. primary->update_plane = skylake_update_primary_plane;
  11481. primary->disable_plane = skylake_disable_primary_plane;
  11482. } else if (INTEL_GEN(dev_priv) >= 4) {
  11483. intel_primary_formats = i965_primary_formats;
  11484. num_formats = ARRAY_SIZE(i965_primary_formats);
  11485. primary->update_plane = i9xx_update_primary_plane;
  11486. primary->disable_plane = i9xx_disable_primary_plane;
  11487. } else {
  11488. intel_primary_formats = i8xx_primary_formats;
  11489. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11490. primary->update_plane = i9xx_update_primary_plane;
  11491. primary->disable_plane = i9xx_disable_primary_plane;
  11492. }
  11493. if (INTEL_GEN(dev_priv) >= 9)
  11494. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11495. 0, &intel_plane_funcs,
  11496. intel_primary_formats, num_formats,
  11497. DRM_PLANE_TYPE_PRIMARY,
  11498. "plane 1%c", pipe_name(pipe));
  11499. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11500. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11501. 0, &intel_plane_funcs,
  11502. intel_primary_formats, num_formats,
  11503. DRM_PLANE_TYPE_PRIMARY,
  11504. "primary %c", pipe_name(pipe));
  11505. else
  11506. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11507. 0, &intel_plane_funcs,
  11508. intel_primary_formats, num_formats,
  11509. DRM_PLANE_TYPE_PRIMARY,
  11510. "plane %c", plane_name(primary->plane));
  11511. if (ret)
  11512. goto fail;
  11513. if (INTEL_GEN(dev_priv) >= 9) {
  11514. supported_rotations =
  11515. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11516. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  11517. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11518. supported_rotations =
  11519. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  11520. DRM_MODE_REFLECT_X;
  11521. } else if (INTEL_GEN(dev_priv) >= 4) {
  11522. supported_rotations =
  11523. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  11524. } else {
  11525. supported_rotations = DRM_MODE_ROTATE_0;
  11526. }
  11527. if (INTEL_GEN(dev_priv) >= 4)
  11528. drm_plane_create_rotation_property(&primary->base,
  11529. DRM_MODE_ROTATE_0,
  11530. supported_rotations);
  11531. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11532. return primary;
  11533. fail:
  11534. kfree(state);
  11535. kfree(primary);
  11536. return ERR_PTR(ret);
  11537. }
  11538. static struct intel_plane *
  11539. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11540. enum pipe pipe)
  11541. {
  11542. struct intel_plane *cursor = NULL;
  11543. struct intel_plane_state *state = NULL;
  11544. int ret;
  11545. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11546. if (!cursor) {
  11547. ret = -ENOMEM;
  11548. goto fail;
  11549. }
  11550. state = intel_create_plane_state(&cursor->base);
  11551. if (!state) {
  11552. ret = -ENOMEM;
  11553. goto fail;
  11554. }
  11555. cursor->base.state = &state->base;
  11556. cursor->can_scale = false;
  11557. cursor->max_downscale = 1;
  11558. cursor->pipe = pipe;
  11559. cursor->plane = pipe;
  11560. cursor->id = PLANE_CURSOR;
  11561. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11562. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11563. cursor->update_plane = i845_update_cursor;
  11564. cursor->disable_plane = i845_disable_cursor;
  11565. cursor->check_plane = i845_check_cursor;
  11566. } else {
  11567. cursor->update_plane = i9xx_update_cursor;
  11568. cursor->disable_plane = i9xx_disable_cursor;
  11569. cursor->check_plane = i9xx_check_cursor;
  11570. }
  11571. cursor->cursor.base = ~0;
  11572. cursor->cursor.cntl = ~0;
  11573. if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
  11574. cursor->cursor.size = ~0;
  11575. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11576. 0, &intel_cursor_plane_funcs,
  11577. intel_cursor_formats,
  11578. ARRAY_SIZE(intel_cursor_formats),
  11579. DRM_PLANE_TYPE_CURSOR,
  11580. "cursor %c", pipe_name(pipe));
  11581. if (ret)
  11582. goto fail;
  11583. if (INTEL_GEN(dev_priv) >= 4)
  11584. drm_plane_create_rotation_property(&cursor->base,
  11585. DRM_MODE_ROTATE_0,
  11586. DRM_MODE_ROTATE_0 |
  11587. DRM_MODE_ROTATE_180);
  11588. if (INTEL_GEN(dev_priv) >= 9)
  11589. state->scaler_id = -1;
  11590. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11591. return cursor;
  11592. fail:
  11593. kfree(state);
  11594. kfree(cursor);
  11595. return ERR_PTR(ret);
  11596. }
  11597. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11598. struct intel_crtc_state *crtc_state)
  11599. {
  11600. struct intel_crtc_scaler_state *scaler_state =
  11601. &crtc_state->scaler_state;
  11602. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11603. int i;
  11604. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11605. if (!crtc->num_scalers)
  11606. return;
  11607. for (i = 0; i < crtc->num_scalers; i++) {
  11608. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11609. scaler->in_use = 0;
  11610. scaler->mode = PS_SCALER_MODE_DYN;
  11611. }
  11612. scaler_state->scaler_id = -1;
  11613. }
  11614. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11615. {
  11616. struct intel_crtc *intel_crtc;
  11617. struct intel_crtc_state *crtc_state = NULL;
  11618. struct intel_plane *primary = NULL;
  11619. struct intel_plane *cursor = NULL;
  11620. int sprite, ret;
  11621. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11622. if (!intel_crtc)
  11623. return -ENOMEM;
  11624. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11625. if (!crtc_state) {
  11626. ret = -ENOMEM;
  11627. goto fail;
  11628. }
  11629. intel_crtc->config = crtc_state;
  11630. intel_crtc->base.state = &crtc_state->base;
  11631. crtc_state->base.crtc = &intel_crtc->base;
  11632. primary = intel_primary_plane_create(dev_priv, pipe);
  11633. if (IS_ERR(primary)) {
  11634. ret = PTR_ERR(primary);
  11635. goto fail;
  11636. }
  11637. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11638. for_each_sprite(dev_priv, pipe, sprite) {
  11639. struct intel_plane *plane;
  11640. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11641. if (IS_ERR(plane)) {
  11642. ret = PTR_ERR(plane);
  11643. goto fail;
  11644. }
  11645. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11646. }
  11647. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11648. if (IS_ERR(cursor)) {
  11649. ret = PTR_ERR(cursor);
  11650. goto fail;
  11651. }
  11652. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11653. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11654. &primary->base, &cursor->base,
  11655. &intel_crtc_funcs,
  11656. "pipe %c", pipe_name(pipe));
  11657. if (ret)
  11658. goto fail;
  11659. intel_crtc->pipe = pipe;
  11660. intel_crtc->plane = primary->plane;
  11661. /* initialize shared scalers */
  11662. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11663. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11664. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11665. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  11666. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11667. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11668. intel_color_init(&intel_crtc->base);
  11669. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11670. return 0;
  11671. fail:
  11672. /*
  11673. * drm_mode_config_cleanup() will free up any
  11674. * crtcs/planes already initialized.
  11675. */
  11676. kfree(crtc_state);
  11677. kfree(intel_crtc);
  11678. return ret;
  11679. }
  11680. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11681. {
  11682. struct drm_device *dev = connector->base.dev;
  11683. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11684. if (!connector->base.state->crtc)
  11685. return INVALID_PIPE;
  11686. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11687. }
  11688. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11689. struct drm_file *file)
  11690. {
  11691. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11692. struct drm_crtc *drmmode_crtc;
  11693. struct intel_crtc *crtc;
  11694. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11695. if (!drmmode_crtc)
  11696. return -ENOENT;
  11697. crtc = to_intel_crtc(drmmode_crtc);
  11698. pipe_from_crtc_id->pipe = crtc->pipe;
  11699. return 0;
  11700. }
  11701. static int intel_encoder_clones(struct intel_encoder *encoder)
  11702. {
  11703. struct drm_device *dev = encoder->base.dev;
  11704. struct intel_encoder *source_encoder;
  11705. int index_mask = 0;
  11706. int entry = 0;
  11707. for_each_intel_encoder(dev, source_encoder) {
  11708. if (encoders_cloneable(encoder, source_encoder))
  11709. index_mask |= (1 << entry);
  11710. entry++;
  11711. }
  11712. return index_mask;
  11713. }
  11714. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11715. {
  11716. if (!IS_MOBILE(dev_priv))
  11717. return false;
  11718. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11719. return false;
  11720. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11721. return false;
  11722. return true;
  11723. }
  11724. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11725. {
  11726. if (INTEL_GEN(dev_priv) >= 9)
  11727. return false;
  11728. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11729. return false;
  11730. if (IS_CHERRYVIEW(dev_priv))
  11731. return false;
  11732. if (HAS_PCH_LPT_H(dev_priv) &&
  11733. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11734. return false;
  11735. /* DDI E can't be used if DDI A requires 4 lanes */
  11736. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11737. return false;
  11738. if (!dev_priv->vbt.int_crt_support)
  11739. return false;
  11740. return true;
  11741. }
  11742. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11743. {
  11744. int pps_num;
  11745. int pps_idx;
  11746. if (HAS_DDI(dev_priv))
  11747. return;
  11748. /*
  11749. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11750. * everywhere where registers can be write protected.
  11751. */
  11752. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11753. pps_num = 2;
  11754. else
  11755. pps_num = 1;
  11756. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11757. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11758. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11759. I915_WRITE(PP_CONTROL(pps_idx), val);
  11760. }
  11761. }
  11762. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11763. {
  11764. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11765. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11766. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11767. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11768. else
  11769. dev_priv->pps_mmio_base = PPS_BASE;
  11770. intel_pps_unlock_regs_wa(dev_priv);
  11771. }
  11772. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11773. {
  11774. struct intel_encoder *encoder;
  11775. bool dpd_is_edp = false;
  11776. intel_pps_init(dev_priv);
  11777. /*
  11778. * intel_edp_init_connector() depends on this completing first, to
  11779. * prevent the registeration of both eDP and LVDS and the incorrect
  11780. * sharing of the PPS.
  11781. */
  11782. intel_lvds_init(dev_priv);
  11783. if (intel_crt_present(dev_priv))
  11784. intel_crt_init(dev_priv);
  11785. if (IS_GEN9_LP(dev_priv)) {
  11786. /*
  11787. * FIXME: Broxton doesn't support port detection via the
  11788. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11789. * detect the ports.
  11790. */
  11791. intel_ddi_init(dev_priv, PORT_A);
  11792. intel_ddi_init(dev_priv, PORT_B);
  11793. intel_ddi_init(dev_priv, PORT_C);
  11794. intel_dsi_init(dev_priv);
  11795. } else if (HAS_DDI(dev_priv)) {
  11796. int found;
  11797. /*
  11798. * Haswell uses DDI functions to detect digital outputs.
  11799. * On SKL pre-D0 the strap isn't connected, so we assume
  11800. * it's there.
  11801. */
  11802. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11803. /* WaIgnoreDDIAStrap: skl */
  11804. if (found || IS_GEN9_BC(dev_priv))
  11805. intel_ddi_init(dev_priv, PORT_A);
  11806. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11807. * register */
  11808. found = I915_READ(SFUSE_STRAP);
  11809. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11810. intel_ddi_init(dev_priv, PORT_B);
  11811. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11812. intel_ddi_init(dev_priv, PORT_C);
  11813. if (found & SFUSE_STRAP_DDID_DETECTED)
  11814. intel_ddi_init(dev_priv, PORT_D);
  11815. /*
  11816. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11817. */
  11818. if (IS_GEN9_BC(dev_priv) &&
  11819. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11820. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11821. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11822. intel_ddi_init(dev_priv, PORT_E);
  11823. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11824. int found;
  11825. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  11826. if (has_edp_a(dev_priv))
  11827. intel_dp_init(dev_priv, DP_A, PORT_A);
  11828. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11829. /* PCH SDVOB multiplex with HDMIB */
  11830. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11831. if (!found)
  11832. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11833. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11834. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11835. }
  11836. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11837. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11838. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11839. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11840. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11841. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11842. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11843. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11844. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11845. bool has_edp, has_port;
  11846. /*
  11847. * The DP_DETECTED bit is the latched state of the DDC
  11848. * SDA pin at boot. However since eDP doesn't require DDC
  11849. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11850. * eDP ports may have been muxed to an alternate function.
  11851. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11852. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11853. * detect eDP ports.
  11854. *
  11855. * Sadly the straps seem to be missing sometimes even for HDMI
  11856. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11857. * and VBT for the presence of the port. Additionally we can't
  11858. * trust the port type the VBT declares as we've seen at least
  11859. * HDMI ports that the VBT claim are DP or eDP.
  11860. */
  11861. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  11862. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11863. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11864. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11865. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11866. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11867. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  11868. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11869. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11870. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11871. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11872. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11873. if (IS_CHERRYVIEW(dev_priv)) {
  11874. /*
  11875. * eDP not supported on port D,
  11876. * so no need to worry about it
  11877. */
  11878. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11879. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11880. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11881. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11882. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11883. }
  11884. intel_dsi_init(dev_priv);
  11885. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11886. bool found = false;
  11887. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11888. DRM_DEBUG_KMS("probing SDVOB\n");
  11889. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11890. if (!found && IS_G4X(dev_priv)) {
  11891. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11892. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11893. }
  11894. if (!found && IS_G4X(dev_priv))
  11895. intel_dp_init(dev_priv, DP_B, PORT_B);
  11896. }
  11897. /* Before G4X SDVOC doesn't have its own detect register */
  11898. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11899. DRM_DEBUG_KMS("probing SDVOC\n");
  11900. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11901. }
  11902. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11903. if (IS_G4X(dev_priv)) {
  11904. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11905. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11906. }
  11907. if (IS_G4X(dev_priv))
  11908. intel_dp_init(dev_priv, DP_C, PORT_C);
  11909. }
  11910. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11911. intel_dp_init(dev_priv, DP_D, PORT_D);
  11912. } else if (IS_GEN2(dev_priv))
  11913. intel_dvo_init(dev_priv);
  11914. if (SUPPORTS_TV(dev_priv))
  11915. intel_tv_init(dev_priv);
  11916. intel_psr_init(dev_priv);
  11917. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11918. encoder->base.possible_crtcs = encoder->crtc_mask;
  11919. encoder->base.possible_clones =
  11920. intel_encoder_clones(encoder);
  11921. }
  11922. intel_init_pch_refclk(dev_priv);
  11923. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11924. }
  11925. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11926. {
  11927. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11928. drm_framebuffer_cleanup(fb);
  11929. i915_gem_object_lock(intel_fb->obj);
  11930. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11931. i915_gem_object_unlock(intel_fb->obj);
  11932. i915_gem_object_put(intel_fb->obj);
  11933. kfree(intel_fb);
  11934. }
  11935. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11936. struct drm_file *file,
  11937. unsigned int *handle)
  11938. {
  11939. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11940. struct drm_i915_gem_object *obj = intel_fb->obj;
  11941. if (obj->userptr.mm) {
  11942. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11943. return -EINVAL;
  11944. }
  11945. return drm_gem_handle_create(file, &obj->base, handle);
  11946. }
  11947. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11948. struct drm_file *file,
  11949. unsigned flags, unsigned color,
  11950. struct drm_clip_rect *clips,
  11951. unsigned num_clips)
  11952. {
  11953. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11954. i915_gem_object_flush_if_display(obj);
  11955. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11956. return 0;
  11957. }
  11958. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11959. .destroy = intel_user_framebuffer_destroy,
  11960. .create_handle = intel_user_framebuffer_create_handle,
  11961. .dirty = intel_user_framebuffer_dirty,
  11962. };
  11963. static
  11964. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11965. uint64_t fb_modifier, uint32_t pixel_format)
  11966. {
  11967. u32 gen = INTEL_GEN(dev_priv);
  11968. if (gen >= 9) {
  11969. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11970. /* "The stride in bytes must not exceed the of the size of 8K
  11971. * pixels and 32K bytes."
  11972. */
  11973. return min(8192 * cpp, 32768);
  11974. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11975. return 32*1024;
  11976. } else if (gen >= 4) {
  11977. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11978. return 16*1024;
  11979. else
  11980. return 32*1024;
  11981. } else if (gen >= 3) {
  11982. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11983. return 8*1024;
  11984. else
  11985. return 16*1024;
  11986. } else {
  11987. /* XXX DSPC is limited to 4k tiled */
  11988. return 8*1024;
  11989. }
  11990. }
  11991. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11992. struct drm_i915_gem_object *obj,
  11993. struct drm_mode_fb_cmd2 *mode_cmd)
  11994. {
  11995. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11996. struct drm_format_name_buf format_name;
  11997. u32 pitch_limit, stride_alignment;
  11998. unsigned int tiling, stride;
  11999. int ret = -EINVAL;
  12000. i915_gem_object_lock(obj);
  12001. obj->framebuffer_references++;
  12002. tiling = i915_gem_object_get_tiling(obj);
  12003. stride = i915_gem_object_get_stride(obj);
  12004. i915_gem_object_unlock(obj);
  12005. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12006. /*
  12007. * If there's a fence, enforce that
  12008. * the fb modifier and tiling mode match.
  12009. */
  12010. if (tiling != I915_TILING_NONE &&
  12011. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  12012. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  12013. goto err;
  12014. }
  12015. } else {
  12016. if (tiling == I915_TILING_X) {
  12017. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12018. } else if (tiling == I915_TILING_Y) {
  12019. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  12020. goto err;
  12021. }
  12022. }
  12023. /* Passed in modifier sanity checking. */
  12024. switch (mode_cmd->modifier[0]) {
  12025. case I915_FORMAT_MOD_Y_TILED:
  12026. case I915_FORMAT_MOD_Yf_TILED:
  12027. if (INTEL_GEN(dev_priv) < 9) {
  12028. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  12029. mode_cmd->modifier[0]);
  12030. goto err;
  12031. }
  12032. case DRM_FORMAT_MOD_LINEAR:
  12033. case I915_FORMAT_MOD_X_TILED:
  12034. break;
  12035. default:
  12036. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  12037. mode_cmd->modifier[0]);
  12038. goto err;
  12039. }
  12040. /*
  12041. * gen2/3 display engine uses the fence if present,
  12042. * so the tiling mode must match the fb modifier exactly.
  12043. */
  12044. if (INTEL_INFO(dev_priv)->gen < 4 &&
  12045. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  12046. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  12047. goto err;
  12048. }
  12049. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  12050. mode_cmd->pixel_format);
  12051. if (mode_cmd->pitches[0] > pitch_limit) {
  12052. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  12053. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  12054. "tiled" : "linear",
  12055. mode_cmd->pitches[0], pitch_limit);
  12056. goto err;
  12057. }
  12058. /*
  12059. * If there's a fence, enforce that
  12060. * the fb pitch and fence stride match.
  12061. */
  12062. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  12063. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  12064. mode_cmd->pitches[0], stride);
  12065. goto err;
  12066. }
  12067. /* Reject formats not supported by any plane early. */
  12068. switch (mode_cmd->pixel_format) {
  12069. case DRM_FORMAT_C8:
  12070. case DRM_FORMAT_RGB565:
  12071. case DRM_FORMAT_XRGB8888:
  12072. case DRM_FORMAT_ARGB8888:
  12073. break;
  12074. case DRM_FORMAT_XRGB1555:
  12075. if (INTEL_GEN(dev_priv) > 3) {
  12076. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12077. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12078. goto err;
  12079. }
  12080. break;
  12081. case DRM_FORMAT_ABGR8888:
  12082. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  12083. INTEL_GEN(dev_priv) < 9) {
  12084. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12085. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12086. goto err;
  12087. }
  12088. break;
  12089. case DRM_FORMAT_XBGR8888:
  12090. case DRM_FORMAT_XRGB2101010:
  12091. case DRM_FORMAT_XBGR2101010:
  12092. if (INTEL_GEN(dev_priv) < 4) {
  12093. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12094. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12095. goto err;
  12096. }
  12097. break;
  12098. case DRM_FORMAT_ABGR2101010:
  12099. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  12100. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12101. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12102. goto err;
  12103. }
  12104. break;
  12105. case DRM_FORMAT_YUYV:
  12106. case DRM_FORMAT_UYVY:
  12107. case DRM_FORMAT_YVYU:
  12108. case DRM_FORMAT_VYUY:
  12109. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  12110. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12111. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12112. goto err;
  12113. }
  12114. break;
  12115. default:
  12116. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12117. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12118. goto err;
  12119. }
  12120. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12121. if (mode_cmd->offsets[0] != 0)
  12122. goto err;
  12123. drm_helper_mode_fill_fb_struct(&dev_priv->drm,
  12124. &intel_fb->base, mode_cmd);
  12125. stride_alignment = intel_fb_stride_alignment(&intel_fb->base, 0);
  12126. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12127. DRM_DEBUG_KMS("pitch (%d) must be at least %u byte aligned\n",
  12128. mode_cmd->pitches[0], stride_alignment);
  12129. goto err;
  12130. }
  12131. intel_fb->obj = obj;
  12132. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  12133. if (ret)
  12134. goto err;
  12135. ret = drm_framebuffer_init(obj->base.dev,
  12136. &intel_fb->base,
  12137. &intel_fb_funcs);
  12138. if (ret) {
  12139. DRM_ERROR("framebuffer init failed %d\n", ret);
  12140. goto err;
  12141. }
  12142. return 0;
  12143. err:
  12144. i915_gem_object_lock(obj);
  12145. obj->framebuffer_references--;
  12146. i915_gem_object_unlock(obj);
  12147. return ret;
  12148. }
  12149. static struct drm_framebuffer *
  12150. intel_user_framebuffer_create(struct drm_device *dev,
  12151. struct drm_file *filp,
  12152. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12153. {
  12154. struct drm_framebuffer *fb;
  12155. struct drm_i915_gem_object *obj;
  12156. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12157. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  12158. if (!obj)
  12159. return ERR_PTR(-ENOENT);
  12160. fb = intel_framebuffer_create(obj, &mode_cmd);
  12161. if (IS_ERR(fb))
  12162. i915_gem_object_put(obj);
  12163. return fb;
  12164. }
  12165. static void intel_atomic_state_free(struct drm_atomic_state *state)
  12166. {
  12167. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12168. drm_atomic_state_default_release(state);
  12169. i915_sw_fence_fini(&intel_state->commit_ready);
  12170. kfree(state);
  12171. }
  12172. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12173. .fb_create = intel_user_framebuffer_create,
  12174. .output_poll_changed = intel_fbdev_output_poll_changed,
  12175. .atomic_check = intel_atomic_check,
  12176. .atomic_commit = intel_atomic_commit,
  12177. .atomic_state_alloc = intel_atomic_state_alloc,
  12178. .atomic_state_clear = intel_atomic_state_clear,
  12179. .atomic_state_free = intel_atomic_state_free,
  12180. };
  12181. /**
  12182. * intel_init_display_hooks - initialize the display modesetting hooks
  12183. * @dev_priv: device private
  12184. */
  12185. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12186. {
  12187. intel_init_cdclk_hooks(dev_priv);
  12188. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12189. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12190. dev_priv->display.get_initial_plane_config =
  12191. skylake_get_initial_plane_config;
  12192. dev_priv->display.crtc_compute_clock =
  12193. haswell_crtc_compute_clock;
  12194. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12195. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12196. } else if (HAS_DDI(dev_priv)) {
  12197. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12198. dev_priv->display.get_initial_plane_config =
  12199. ironlake_get_initial_plane_config;
  12200. dev_priv->display.crtc_compute_clock =
  12201. haswell_crtc_compute_clock;
  12202. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12203. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12204. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12205. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12206. dev_priv->display.get_initial_plane_config =
  12207. ironlake_get_initial_plane_config;
  12208. dev_priv->display.crtc_compute_clock =
  12209. ironlake_crtc_compute_clock;
  12210. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12211. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12212. } else if (IS_CHERRYVIEW(dev_priv)) {
  12213. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12214. dev_priv->display.get_initial_plane_config =
  12215. i9xx_get_initial_plane_config;
  12216. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12217. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12218. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12219. } else if (IS_VALLEYVIEW(dev_priv)) {
  12220. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12221. dev_priv->display.get_initial_plane_config =
  12222. i9xx_get_initial_plane_config;
  12223. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12224. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12225. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12226. } else if (IS_G4X(dev_priv)) {
  12227. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12228. dev_priv->display.get_initial_plane_config =
  12229. i9xx_get_initial_plane_config;
  12230. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12231. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12232. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12233. } else if (IS_PINEVIEW(dev_priv)) {
  12234. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12235. dev_priv->display.get_initial_plane_config =
  12236. i9xx_get_initial_plane_config;
  12237. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12238. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12239. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12240. } else if (!IS_GEN2(dev_priv)) {
  12241. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12242. dev_priv->display.get_initial_plane_config =
  12243. i9xx_get_initial_plane_config;
  12244. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12245. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12246. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12247. } else {
  12248. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12249. dev_priv->display.get_initial_plane_config =
  12250. i9xx_get_initial_plane_config;
  12251. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12252. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12253. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12254. }
  12255. if (IS_GEN5(dev_priv)) {
  12256. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12257. } else if (IS_GEN6(dev_priv)) {
  12258. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12259. } else if (IS_IVYBRIDGE(dev_priv)) {
  12260. /* FIXME: detect B0+ stepping and use auto training */
  12261. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12262. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12263. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12264. }
  12265. if (dev_priv->info.gen >= 9)
  12266. dev_priv->display.update_crtcs = skl_update_crtcs;
  12267. else
  12268. dev_priv->display.update_crtcs = intel_update_crtcs;
  12269. switch (INTEL_INFO(dev_priv)->gen) {
  12270. case 2:
  12271. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12272. break;
  12273. case 3:
  12274. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12275. break;
  12276. case 4:
  12277. case 5:
  12278. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12279. break;
  12280. case 6:
  12281. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12282. break;
  12283. case 7:
  12284. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12285. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12286. break;
  12287. case 9:
  12288. /* Drop through - unsupported since execlist only. */
  12289. default:
  12290. /* Default just returns -ENODEV to indicate unsupported */
  12291. dev_priv->display.queue_flip = intel_default_queue_flip;
  12292. }
  12293. }
  12294. /*
  12295. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12296. * resume, or other times. This quirk makes sure that's the case for
  12297. * affected systems.
  12298. */
  12299. static void quirk_pipea_force(struct drm_device *dev)
  12300. {
  12301. struct drm_i915_private *dev_priv = to_i915(dev);
  12302. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12303. DRM_INFO("applying pipe a force quirk\n");
  12304. }
  12305. static void quirk_pipeb_force(struct drm_device *dev)
  12306. {
  12307. struct drm_i915_private *dev_priv = to_i915(dev);
  12308. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12309. DRM_INFO("applying pipe b force quirk\n");
  12310. }
  12311. /*
  12312. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12313. */
  12314. static void quirk_ssc_force_disable(struct drm_device *dev)
  12315. {
  12316. struct drm_i915_private *dev_priv = to_i915(dev);
  12317. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12318. DRM_INFO("applying lvds SSC disable quirk\n");
  12319. }
  12320. /*
  12321. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12322. * brightness value
  12323. */
  12324. static void quirk_invert_brightness(struct drm_device *dev)
  12325. {
  12326. struct drm_i915_private *dev_priv = to_i915(dev);
  12327. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12328. DRM_INFO("applying inverted panel brightness quirk\n");
  12329. }
  12330. /* Some VBT's incorrectly indicate no backlight is present */
  12331. static void quirk_backlight_present(struct drm_device *dev)
  12332. {
  12333. struct drm_i915_private *dev_priv = to_i915(dev);
  12334. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12335. DRM_INFO("applying backlight present quirk\n");
  12336. }
  12337. struct intel_quirk {
  12338. int device;
  12339. int subsystem_vendor;
  12340. int subsystem_device;
  12341. void (*hook)(struct drm_device *dev);
  12342. };
  12343. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12344. struct intel_dmi_quirk {
  12345. void (*hook)(struct drm_device *dev);
  12346. const struct dmi_system_id (*dmi_id_list)[];
  12347. };
  12348. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12349. {
  12350. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12351. return 1;
  12352. }
  12353. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12354. {
  12355. .dmi_id_list = &(const struct dmi_system_id[]) {
  12356. {
  12357. .callback = intel_dmi_reverse_brightness,
  12358. .ident = "NCR Corporation",
  12359. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12360. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12361. },
  12362. },
  12363. { } /* terminating entry */
  12364. },
  12365. .hook = quirk_invert_brightness,
  12366. },
  12367. };
  12368. static struct intel_quirk intel_quirks[] = {
  12369. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12370. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12371. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12372. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12373. /* 830 needs to leave pipe A & dpll A up */
  12374. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12375. /* 830 needs to leave pipe B & dpll B up */
  12376. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12377. /* Lenovo U160 cannot use SSC on LVDS */
  12378. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12379. /* Sony Vaio Y cannot use SSC on LVDS */
  12380. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12381. /* Acer Aspire 5734Z must invert backlight brightness */
  12382. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12383. /* Acer/eMachines G725 */
  12384. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12385. /* Acer/eMachines e725 */
  12386. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12387. /* Acer/Packard Bell NCL20 */
  12388. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12389. /* Acer Aspire 4736Z */
  12390. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12391. /* Acer Aspire 5336 */
  12392. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12393. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12394. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12395. /* Acer C720 Chromebook (Core i3 4005U) */
  12396. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12397. /* Apple Macbook 2,1 (Core 2 T7400) */
  12398. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12399. /* Apple Macbook 4,1 */
  12400. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12401. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12402. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12403. /* HP Chromebook 14 (Celeron 2955U) */
  12404. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12405. /* Dell Chromebook 11 */
  12406. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12407. /* Dell Chromebook 11 (2015 version) */
  12408. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12409. };
  12410. static void intel_init_quirks(struct drm_device *dev)
  12411. {
  12412. struct pci_dev *d = dev->pdev;
  12413. int i;
  12414. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12415. struct intel_quirk *q = &intel_quirks[i];
  12416. if (d->device == q->device &&
  12417. (d->subsystem_vendor == q->subsystem_vendor ||
  12418. q->subsystem_vendor == PCI_ANY_ID) &&
  12419. (d->subsystem_device == q->subsystem_device ||
  12420. q->subsystem_device == PCI_ANY_ID))
  12421. q->hook(dev);
  12422. }
  12423. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12424. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12425. intel_dmi_quirks[i].hook(dev);
  12426. }
  12427. }
  12428. /* Disable the VGA plane that we never use */
  12429. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12430. {
  12431. struct pci_dev *pdev = dev_priv->drm.pdev;
  12432. u8 sr1;
  12433. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12434. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12435. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12436. outb(SR01, VGA_SR_INDEX);
  12437. sr1 = inb(VGA_SR_DATA);
  12438. outb(sr1 | 1<<5, VGA_SR_DATA);
  12439. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12440. udelay(300);
  12441. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12442. POSTING_READ(vga_reg);
  12443. }
  12444. void intel_modeset_init_hw(struct drm_device *dev)
  12445. {
  12446. struct drm_i915_private *dev_priv = to_i915(dev);
  12447. intel_update_cdclk(dev_priv);
  12448. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12449. intel_init_clock_gating(dev_priv);
  12450. }
  12451. /*
  12452. * Calculate what we think the watermarks should be for the state we've read
  12453. * out of the hardware and then immediately program those watermarks so that
  12454. * we ensure the hardware settings match our internal state.
  12455. *
  12456. * We can calculate what we think WM's should be by creating a duplicate of the
  12457. * current state (which was constructed during hardware readout) and running it
  12458. * through the atomic check code to calculate new watermark values in the
  12459. * state object.
  12460. */
  12461. static void sanitize_watermarks(struct drm_device *dev)
  12462. {
  12463. struct drm_i915_private *dev_priv = to_i915(dev);
  12464. struct drm_atomic_state *state;
  12465. struct intel_atomic_state *intel_state;
  12466. struct drm_crtc *crtc;
  12467. struct drm_crtc_state *cstate;
  12468. struct drm_modeset_acquire_ctx ctx;
  12469. int ret;
  12470. int i;
  12471. /* Only supported on platforms that use atomic watermark design */
  12472. if (!dev_priv->display.optimize_watermarks)
  12473. return;
  12474. /*
  12475. * We need to hold connection_mutex before calling duplicate_state so
  12476. * that the connector loop is protected.
  12477. */
  12478. drm_modeset_acquire_init(&ctx, 0);
  12479. retry:
  12480. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12481. if (ret == -EDEADLK) {
  12482. drm_modeset_backoff(&ctx);
  12483. goto retry;
  12484. } else if (WARN_ON(ret)) {
  12485. goto fail;
  12486. }
  12487. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12488. if (WARN_ON(IS_ERR(state)))
  12489. goto fail;
  12490. intel_state = to_intel_atomic_state(state);
  12491. /*
  12492. * Hardware readout is the only time we don't want to calculate
  12493. * intermediate watermarks (since we don't trust the current
  12494. * watermarks).
  12495. */
  12496. if (!HAS_GMCH_DISPLAY(dev_priv))
  12497. intel_state->skip_intermediate_wm = true;
  12498. ret = intel_atomic_check(dev, state);
  12499. if (ret) {
  12500. /*
  12501. * If we fail here, it means that the hardware appears to be
  12502. * programmed in a way that shouldn't be possible, given our
  12503. * understanding of watermark requirements. This might mean a
  12504. * mistake in the hardware readout code or a mistake in the
  12505. * watermark calculations for a given platform. Raise a WARN
  12506. * so that this is noticeable.
  12507. *
  12508. * If this actually happens, we'll have to just leave the
  12509. * BIOS-programmed watermarks untouched and hope for the best.
  12510. */
  12511. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12512. goto put_state;
  12513. }
  12514. /* Write calculated watermark values back */
  12515. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12516. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12517. cs->wm.need_postvbl_update = true;
  12518. dev_priv->display.optimize_watermarks(intel_state, cs);
  12519. }
  12520. put_state:
  12521. drm_atomic_state_put(state);
  12522. fail:
  12523. drm_modeset_drop_locks(&ctx);
  12524. drm_modeset_acquire_fini(&ctx);
  12525. }
  12526. int intel_modeset_init(struct drm_device *dev)
  12527. {
  12528. struct drm_i915_private *dev_priv = to_i915(dev);
  12529. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12530. enum pipe pipe;
  12531. struct intel_crtc *crtc;
  12532. drm_mode_config_init(dev);
  12533. dev->mode_config.min_width = 0;
  12534. dev->mode_config.min_height = 0;
  12535. dev->mode_config.preferred_depth = 24;
  12536. dev->mode_config.prefer_shadow = 1;
  12537. dev->mode_config.allow_fb_modifiers = true;
  12538. dev->mode_config.funcs = &intel_mode_funcs;
  12539. init_llist_head(&dev_priv->atomic_helper.free_list);
  12540. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12541. intel_atomic_helper_free_state_worker);
  12542. intel_init_quirks(dev);
  12543. intel_init_pm(dev_priv);
  12544. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12545. return 0;
  12546. /*
  12547. * There may be no VBT; and if the BIOS enabled SSC we can
  12548. * just keep using it to avoid unnecessary flicker. Whereas if the
  12549. * BIOS isn't using it, don't assume it will work even if the VBT
  12550. * indicates as much.
  12551. */
  12552. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12553. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12554. DREF_SSC1_ENABLE);
  12555. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12556. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12557. bios_lvds_use_ssc ? "en" : "dis",
  12558. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12559. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12560. }
  12561. }
  12562. if (IS_GEN2(dev_priv)) {
  12563. dev->mode_config.max_width = 2048;
  12564. dev->mode_config.max_height = 2048;
  12565. } else if (IS_GEN3(dev_priv)) {
  12566. dev->mode_config.max_width = 4096;
  12567. dev->mode_config.max_height = 4096;
  12568. } else {
  12569. dev->mode_config.max_width = 8192;
  12570. dev->mode_config.max_height = 8192;
  12571. }
  12572. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12573. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12574. dev->mode_config.cursor_height = 1023;
  12575. } else if (IS_GEN2(dev_priv)) {
  12576. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12577. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12578. } else {
  12579. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12580. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12581. }
  12582. dev->mode_config.fb_base = ggtt->mappable_base;
  12583. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12584. INTEL_INFO(dev_priv)->num_pipes,
  12585. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12586. for_each_pipe(dev_priv, pipe) {
  12587. int ret;
  12588. ret = intel_crtc_init(dev_priv, pipe);
  12589. if (ret) {
  12590. drm_mode_config_cleanup(dev);
  12591. return ret;
  12592. }
  12593. }
  12594. intel_shared_dpll_init(dev);
  12595. intel_update_czclk(dev_priv);
  12596. intel_modeset_init_hw(dev);
  12597. if (dev_priv->max_cdclk_freq == 0)
  12598. intel_update_max_cdclk(dev_priv);
  12599. /* Just disable it once at startup */
  12600. i915_disable_vga(dev_priv);
  12601. intel_setup_outputs(dev_priv);
  12602. drm_modeset_lock_all(dev);
  12603. intel_modeset_setup_hw_state(dev);
  12604. drm_modeset_unlock_all(dev);
  12605. for_each_intel_crtc(dev, crtc) {
  12606. struct intel_initial_plane_config plane_config = {};
  12607. if (!crtc->active)
  12608. continue;
  12609. /*
  12610. * Note that reserving the BIOS fb up front prevents us
  12611. * from stuffing other stolen allocations like the ring
  12612. * on top. This prevents some ugliness at boot time, and
  12613. * can even allow for smooth boot transitions if the BIOS
  12614. * fb is large enough for the active pipe configuration.
  12615. */
  12616. dev_priv->display.get_initial_plane_config(crtc,
  12617. &plane_config);
  12618. /*
  12619. * If the fb is shared between multiple heads, we'll
  12620. * just get the first one.
  12621. */
  12622. intel_find_initial_plane_obj(crtc, &plane_config);
  12623. }
  12624. /*
  12625. * Make sure hardware watermarks really match the state we read out.
  12626. * Note that we need to do this after reconstructing the BIOS fb's
  12627. * since the watermark calculation done here will use pstate->fb.
  12628. */
  12629. if (!HAS_GMCH_DISPLAY(dev_priv))
  12630. sanitize_watermarks(dev);
  12631. return 0;
  12632. }
  12633. static void intel_enable_pipe_a(struct drm_device *dev)
  12634. {
  12635. struct intel_connector *connector;
  12636. struct drm_connector_list_iter conn_iter;
  12637. struct drm_connector *crt = NULL;
  12638. struct intel_load_detect_pipe load_detect_temp;
  12639. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12640. int ret;
  12641. /* We can't just switch on the pipe A, we need to set things up with a
  12642. * proper mode and output configuration. As a gross hack, enable pipe A
  12643. * by enabling the load detect pipe once. */
  12644. drm_connector_list_iter_begin(dev, &conn_iter);
  12645. for_each_intel_connector_iter(connector, &conn_iter) {
  12646. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12647. crt = &connector->base;
  12648. break;
  12649. }
  12650. }
  12651. drm_connector_list_iter_end(&conn_iter);
  12652. if (!crt)
  12653. return;
  12654. ret = intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx);
  12655. WARN(ret < 0, "All modeset mutexes are locked, but intel_get_load_detect_pipe failed\n");
  12656. if (ret > 0)
  12657. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12658. }
  12659. static bool
  12660. intel_check_plane_mapping(struct intel_crtc *crtc)
  12661. {
  12662. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12663. u32 val;
  12664. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  12665. return true;
  12666. val = I915_READ(DSPCNTR(!crtc->plane));
  12667. if ((val & DISPLAY_PLANE_ENABLE) &&
  12668. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12669. return false;
  12670. return true;
  12671. }
  12672. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12673. {
  12674. struct drm_device *dev = crtc->base.dev;
  12675. struct intel_encoder *encoder;
  12676. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12677. return true;
  12678. return false;
  12679. }
  12680. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12681. {
  12682. struct drm_device *dev = encoder->base.dev;
  12683. struct intel_connector *connector;
  12684. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12685. return connector;
  12686. return NULL;
  12687. }
  12688. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12689. enum transcoder pch_transcoder)
  12690. {
  12691. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12692. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  12693. }
  12694. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12695. {
  12696. struct drm_device *dev = crtc->base.dev;
  12697. struct drm_i915_private *dev_priv = to_i915(dev);
  12698. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12699. /* Clear any frame start delays used for debugging left by the BIOS */
  12700. if (!transcoder_is_dsi(cpu_transcoder)) {
  12701. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12702. I915_WRITE(reg,
  12703. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12704. }
  12705. /* restore vblank interrupts to correct state */
  12706. drm_crtc_vblank_reset(&crtc->base);
  12707. if (crtc->active) {
  12708. struct intel_plane *plane;
  12709. drm_crtc_vblank_on(&crtc->base);
  12710. /* Disable everything but the primary plane */
  12711. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12712. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12713. continue;
  12714. trace_intel_disable_plane(&plane->base, crtc);
  12715. plane->disable_plane(plane, crtc);
  12716. }
  12717. }
  12718. /* We need to sanitize the plane -> pipe mapping first because this will
  12719. * disable the crtc (and hence change the state) if it is wrong. Note
  12720. * that gen4+ has a fixed plane -> pipe mapping. */
  12721. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  12722. bool plane;
  12723. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  12724. crtc->base.base.id, crtc->base.name);
  12725. /* Pipe has the wrong plane attached and the plane is active.
  12726. * Temporarily change the plane mapping and disable everything
  12727. * ... */
  12728. plane = crtc->plane;
  12729. crtc->base.primary->state->visible = true;
  12730. crtc->plane = !plane;
  12731. intel_crtc_disable_noatomic(&crtc->base);
  12732. crtc->plane = plane;
  12733. }
  12734. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12735. crtc->pipe == PIPE_A && !crtc->active) {
  12736. /* BIOS forgot to enable pipe A, this mostly happens after
  12737. * resume. Force-enable the pipe to fix this, the update_dpms
  12738. * call below we restore the pipe to the right state, but leave
  12739. * the required bits on. */
  12740. intel_enable_pipe_a(dev);
  12741. }
  12742. /* Adjust the state of the output pipe according to whether we
  12743. * have active connectors/encoders. */
  12744. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12745. intel_crtc_disable_noatomic(&crtc->base);
  12746. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12747. /*
  12748. * We start out with underrun reporting disabled to avoid races.
  12749. * For correct bookkeeping mark this on active crtcs.
  12750. *
  12751. * Also on gmch platforms we dont have any hardware bits to
  12752. * disable the underrun reporting. Which means we need to start
  12753. * out with underrun reporting disabled also on inactive pipes,
  12754. * since otherwise we'll complain about the garbage we read when
  12755. * e.g. coming up after runtime pm.
  12756. *
  12757. * No protection against concurrent access is required - at
  12758. * worst a fifo underrun happens which also sets this to false.
  12759. */
  12760. crtc->cpu_fifo_underrun_disabled = true;
  12761. /*
  12762. * We track the PCH trancoder underrun reporting state
  12763. * within the crtc. With crtc for pipe A housing the underrun
  12764. * reporting state for PCH transcoder A, crtc for pipe B housing
  12765. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12766. * and marking underrun reporting as disabled for the non-existing
  12767. * PCH transcoders B and C would prevent enabling the south
  12768. * error interrupt (see cpt_can_enable_serr_int()).
  12769. */
  12770. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  12771. crtc->pch_fifo_underrun_disabled = true;
  12772. }
  12773. }
  12774. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12775. {
  12776. struct intel_connector *connector;
  12777. /* We need to check both for a crtc link (meaning that the
  12778. * encoder is active and trying to read from a pipe) and the
  12779. * pipe itself being active. */
  12780. bool has_active_crtc = encoder->base.crtc &&
  12781. to_intel_crtc(encoder->base.crtc)->active;
  12782. connector = intel_encoder_find_connector(encoder);
  12783. if (connector && !has_active_crtc) {
  12784. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12785. encoder->base.base.id,
  12786. encoder->base.name);
  12787. /* Connector is active, but has no active pipe. This is
  12788. * fallout from our resume register restoring. Disable
  12789. * the encoder manually again. */
  12790. if (encoder->base.crtc) {
  12791. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12792. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12793. encoder->base.base.id,
  12794. encoder->base.name);
  12795. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12796. if (encoder->post_disable)
  12797. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12798. }
  12799. encoder->base.crtc = NULL;
  12800. /* Inconsistent output/port/pipe state happens presumably due to
  12801. * a bug in one of the get_hw_state functions. Or someplace else
  12802. * in our code, like the register restore mess on resume. Clamp
  12803. * things to off as a safer default. */
  12804. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12805. connector->base.encoder = NULL;
  12806. }
  12807. /* Enabled encoders without active connectors will be fixed in
  12808. * the crtc fixup. */
  12809. }
  12810. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12811. {
  12812. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12813. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12814. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12815. i915_disable_vga(dev_priv);
  12816. }
  12817. }
  12818. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12819. {
  12820. /* This function can be called both from intel_modeset_setup_hw_state or
  12821. * at a very early point in our resume sequence, where the power well
  12822. * structures are not yet restored. Since this function is at a very
  12823. * paranoid "someone might have enabled VGA while we were not looking"
  12824. * level, just check if the power well is enabled instead of trying to
  12825. * follow the "don't touch the power well if we don't need it" policy
  12826. * the rest of the driver uses. */
  12827. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12828. return;
  12829. i915_redisable_vga_power_on(dev_priv);
  12830. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12831. }
  12832. static bool primary_get_hw_state(struct intel_plane *plane)
  12833. {
  12834. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12835. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12836. }
  12837. /* FIXME read out full plane state for all planes */
  12838. static void readout_plane_state(struct intel_crtc *crtc)
  12839. {
  12840. struct intel_plane *primary = to_intel_plane(crtc->base.primary);
  12841. bool visible;
  12842. visible = crtc->active && primary_get_hw_state(primary);
  12843. intel_set_plane_visible(to_intel_crtc_state(crtc->base.state),
  12844. to_intel_plane_state(primary->base.state),
  12845. visible);
  12846. }
  12847. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12848. {
  12849. struct drm_i915_private *dev_priv = to_i915(dev);
  12850. enum pipe pipe;
  12851. struct intel_crtc *crtc;
  12852. struct intel_encoder *encoder;
  12853. struct intel_connector *connector;
  12854. struct drm_connector_list_iter conn_iter;
  12855. int i;
  12856. dev_priv->active_crtcs = 0;
  12857. for_each_intel_crtc(dev, crtc) {
  12858. struct intel_crtc_state *crtc_state =
  12859. to_intel_crtc_state(crtc->base.state);
  12860. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12861. memset(crtc_state, 0, sizeof(*crtc_state));
  12862. crtc_state->base.crtc = &crtc->base;
  12863. crtc_state->base.active = crtc_state->base.enable =
  12864. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12865. crtc->base.enabled = crtc_state->base.enable;
  12866. crtc->active = crtc_state->base.active;
  12867. if (crtc_state->base.active)
  12868. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12869. readout_plane_state(crtc);
  12870. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12871. crtc->base.base.id, crtc->base.name,
  12872. enableddisabled(crtc_state->base.active));
  12873. }
  12874. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12875. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12876. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12877. &pll->state.hw_state);
  12878. pll->state.crtc_mask = 0;
  12879. for_each_intel_crtc(dev, crtc) {
  12880. struct intel_crtc_state *crtc_state =
  12881. to_intel_crtc_state(crtc->base.state);
  12882. if (crtc_state->base.active &&
  12883. crtc_state->shared_dpll == pll)
  12884. pll->state.crtc_mask |= 1 << crtc->pipe;
  12885. }
  12886. pll->active_mask = pll->state.crtc_mask;
  12887. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12888. pll->name, pll->state.crtc_mask, pll->on);
  12889. }
  12890. for_each_intel_encoder(dev, encoder) {
  12891. pipe = 0;
  12892. if (encoder->get_hw_state(encoder, &pipe)) {
  12893. struct intel_crtc_state *crtc_state;
  12894. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12895. crtc_state = to_intel_crtc_state(crtc->base.state);
  12896. encoder->base.crtc = &crtc->base;
  12897. crtc_state->output_types |= 1 << encoder->type;
  12898. encoder->get_config(encoder, crtc_state);
  12899. } else {
  12900. encoder->base.crtc = NULL;
  12901. }
  12902. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12903. encoder->base.base.id, encoder->base.name,
  12904. enableddisabled(encoder->base.crtc),
  12905. pipe_name(pipe));
  12906. }
  12907. drm_connector_list_iter_begin(dev, &conn_iter);
  12908. for_each_intel_connector_iter(connector, &conn_iter) {
  12909. if (connector->get_hw_state(connector)) {
  12910. connector->base.dpms = DRM_MODE_DPMS_ON;
  12911. encoder = connector->encoder;
  12912. connector->base.encoder = &encoder->base;
  12913. if (encoder->base.crtc &&
  12914. encoder->base.crtc->state->active) {
  12915. /*
  12916. * This has to be done during hardware readout
  12917. * because anything calling .crtc_disable may
  12918. * rely on the connector_mask being accurate.
  12919. */
  12920. encoder->base.crtc->state->connector_mask |=
  12921. 1 << drm_connector_index(&connector->base);
  12922. encoder->base.crtc->state->encoder_mask |=
  12923. 1 << drm_encoder_index(&encoder->base);
  12924. }
  12925. } else {
  12926. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12927. connector->base.encoder = NULL;
  12928. }
  12929. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12930. connector->base.base.id, connector->base.name,
  12931. enableddisabled(connector->base.encoder));
  12932. }
  12933. drm_connector_list_iter_end(&conn_iter);
  12934. for_each_intel_crtc(dev, crtc) {
  12935. struct intel_crtc_state *crtc_state =
  12936. to_intel_crtc_state(crtc->base.state);
  12937. int pixclk = 0;
  12938. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12939. if (crtc_state->base.active) {
  12940. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12941. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12942. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12943. /*
  12944. * The initial mode needs to be set in order to keep
  12945. * the atomic core happy. It wants a valid mode if the
  12946. * crtc's enabled, so we do the above call.
  12947. *
  12948. * But we don't set all the derived state fully, hence
  12949. * set a flag to indicate that a full recalculation is
  12950. * needed on the next commit.
  12951. */
  12952. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12953. intel_crtc_compute_pixel_rate(crtc_state);
  12954. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
  12955. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12956. pixclk = crtc_state->pixel_rate;
  12957. else
  12958. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  12959. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  12960. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  12961. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  12962. drm_calc_timestamping_constants(&crtc->base,
  12963. &crtc_state->base.adjusted_mode);
  12964. update_scanline_offset(crtc);
  12965. }
  12966. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  12967. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12968. }
  12969. }
  12970. static void
  12971. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12972. {
  12973. struct intel_encoder *encoder;
  12974. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12975. u64 get_domains;
  12976. enum intel_display_power_domain domain;
  12977. if (!encoder->get_power_domains)
  12978. continue;
  12979. get_domains = encoder->get_power_domains(encoder);
  12980. for_each_power_domain(domain, get_domains)
  12981. intel_display_power_get(dev_priv, domain);
  12982. }
  12983. }
  12984. /* Scan out the current hw modeset state,
  12985. * and sanitizes it to the current state
  12986. */
  12987. static void
  12988. intel_modeset_setup_hw_state(struct drm_device *dev)
  12989. {
  12990. struct drm_i915_private *dev_priv = to_i915(dev);
  12991. enum pipe pipe;
  12992. struct intel_crtc *crtc;
  12993. struct intel_encoder *encoder;
  12994. int i;
  12995. intel_modeset_readout_hw_state(dev);
  12996. /* HW state is read out, now we need to sanitize this mess. */
  12997. get_encoder_power_domains(dev_priv);
  12998. for_each_intel_encoder(dev, encoder) {
  12999. intel_sanitize_encoder(encoder);
  13000. }
  13001. for_each_pipe(dev_priv, pipe) {
  13002. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  13003. intel_sanitize_crtc(crtc);
  13004. intel_dump_pipe_config(crtc, crtc->config,
  13005. "[setup_hw_state]");
  13006. }
  13007. intel_modeset_update_connector_atomic_state(dev);
  13008. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13009. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13010. if (!pll->on || pll->active_mask)
  13011. continue;
  13012. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13013. pll->funcs.disable(dev_priv, pll);
  13014. pll->on = false;
  13015. }
  13016. if (IS_G4X(dev_priv)) {
  13017. g4x_wm_get_hw_state(dev);
  13018. g4x_wm_sanitize(dev_priv);
  13019. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13020. vlv_wm_get_hw_state(dev);
  13021. vlv_wm_sanitize(dev_priv);
  13022. } else if (IS_GEN9(dev_priv)) {
  13023. skl_wm_get_hw_state(dev);
  13024. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13025. ilk_wm_get_hw_state(dev);
  13026. }
  13027. for_each_intel_crtc(dev, crtc) {
  13028. u64 put_domains;
  13029. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13030. if (WARN_ON(put_domains))
  13031. modeset_put_power_domains(dev_priv, put_domains);
  13032. }
  13033. intel_display_set_init_power(dev_priv, false);
  13034. intel_power_domains_verify_state(dev_priv);
  13035. intel_fbc_init_pipe_state(dev_priv);
  13036. }
  13037. void intel_display_resume(struct drm_device *dev)
  13038. {
  13039. struct drm_i915_private *dev_priv = to_i915(dev);
  13040. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13041. struct drm_modeset_acquire_ctx ctx;
  13042. int ret;
  13043. dev_priv->modeset_restore_state = NULL;
  13044. if (state)
  13045. state->acquire_ctx = &ctx;
  13046. drm_modeset_acquire_init(&ctx, 0);
  13047. while (1) {
  13048. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13049. if (ret != -EDEADLK)
  13050. break;
  13051. drm_modeset_backoff(&ctx);
  13052. }
  13053. if (!ret)
  13054. ret = __intel_display_resume(dev, state, &ctx);
  13055. drm_modeset_drop_locks(&ctx);
  13056. drm_modeset_acquire_fini(&ctx);
  13057. if (ret)
  13058. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13059. if (state)
  13060. drm_atomic_state_put(state);
  13061. }
  13062. void intel_modeset_gem_init(struct drm_device *dev)
  13063. {
  13064. struct drm_i915_private *dev_priv = to_i915(dev);
  13065. intel_init_gt_powersave(dev_priv);
  13066. intel_setup_overlay(dev_priv);
  13067. }
  13068. int intel_connector_register(struct drm_connector *connector)
  13069. {
  13070. struct intel_connector *intel_connector = to_intel_connector(connector);
  13071. int ret;
  13072. ret = intel_backlight_device_register(intel_connector);
  13073. if (ret)
  13074. goto err;
  13075. return 0;
  13076. err:
  13077. return ret;
  13078. }
  13079. void intel_connector_unregister(struct drm_connector *connector)
  13080. {
  13081. struct intel_connector *intel_connector = to_intel_connector(connector);
  13082. intel_backlight_device_unregister(intel_connector);
  13083. intel_panel_destroy_backlight(connector);
  13084. }
  13085. void intel_modeset_cleanup(struct drm_device *dev)
  13086. {
  13087. struct drm_i915_private *dev_priv = to_i915(dev);
  13088. flush_work(&dev_priv->atomic_helper.free_work);
  13089. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  13090. intel_disable_gt_powersave(dev_priv);
  13091. /*
  13092. * Interrupts and polling as the first thing to avoid creating havoc.
  13093. * Too much stuff here (turning of connectors, ...) would
  13094. * experience fancy races otherwise.
  13095. */
  13096. intel_irq_uninstall(dev_priv);
  13097. /*
  13098. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13099. * poll handlers. Hence disable polling after hpd handling is shut down.
  13100. */
  13101. drm_kms_helper_poll_fini(dev);
  13102. intel_unregister_dsm_handler();
  13103. intel_fbc_global_disable(dev_priv);
  13104. /* flush any delayed tasks or pending work */
  13105. flush_scheduled_work();
  13106. drm_mode_config_cleanup(dev);
  13107. intel_cleanup_overlay(dev_priv);
  13108. intel_cleanup_gt_powersave(dev_priv);
  13109. intel_teardown_gmbus(dev_priv);
  13110. }
  13111. void intel_connector_attach_encoder(struct intel_connector *connector,
  13112. struct intel_encoder *encoder)
  13113. {
  13114. connector->encoder = encoder;
  13115. drm_mode_connector_attach_encoder(&connector->base,
  13116. &encoder->base);
  13117. }
  13118. /*
  13119. * set vga decode state - true == enable VGA decode
  13120. */
  13121. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  13122. {
  13123. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13124. u16 gmch_ctrl;
  13125. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13126. DRM_ERROR("failed to read control word\n");
  13127. return -EIO;
  13128. }
  13129. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13130. return 0;
  13131. if (state)
  13132. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13133. else
  13134. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13135. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13136. DRM_ERROR("failed to write control word\n");
  13137. return -EIO;
  13138. }
  13139. return 0;
  13140. }
  13141. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  13142. struct intel_display_error_state {
  13143. u32 power_well_driver;
  13144. int num_transcoders;
  13145. struct intel_cursor_error_state {
  13146. u32 control;
  13147. u32 position;
  13148. u32 base;
  13149. u32 size;
  13150. } cursor[I915_MAX_PIPES];
  13151. struct intel_pipe_error_state {
  13152. bool power_domain_on;
  13153. u32 source;
  13154. u32 stat;
  13155. } pipe[I915_MAX_PIPES];
  13156. struct intel_plane_error_state {
  13157. u32 control;
  13158. u32 stride;
  13159. u32 size;
  13160. u32 pos;
  13161. u32 addr;
  13162. u32 surface;
  13163. u32 tile_offset;
  13164. } plane[I915_MAX_PIPES];
  13165. struct intel_transcoder_error_state {
  13166. bool power_domain_on;
  13167. enum transcoder cpu_transcoder;
  13168. u32 conf;
  13169. u32 htotal;
  13170. u32 hblank;
  13171. u32 hsync;
  13172. u32 vtotal;
  13173. u32 vblank;
  13174. u32 vsync;
  13175. } transcoder[4];
  13176. };
  13177. struct intel_display_error_state *
  13178. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13179. {
  13180. struct intel_display_error_state *error;
  13181. int transcoders[] = {
  13182. TRANSCODER_A,
  13183. TRANSCODER_B,
  13184. TRANSCODER_C,
  13185. TRANSCODER_EDP,
  13186. };
  13187. int i;
  13188. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13189. return NULL;
  13190. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13191. if (error == NULL)
  13192. return NULL;
  13193. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13194. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13195. for_each_pipe(dev_priv, i) {
  13196. error->pipe[i].power_domain_on =
  13197. __intel_display_power_is_enabled(dev_priv,
  13198. POWER_DOMAIN_PIPE(i));
  13199. if (!error->pipe[i].power_domain_on)
  13200. continue;
  13201. error->cursor[i].control = I915_READ(CURCNTR(i));
  13202. error->cursor[i].position = I915_READ(CURPOS(i));
  13203. error->cursor[i].base = I915_READ(CURBASE(i));
  13204. error->plane[i].control = I915_READ(DSPCNTR(i));
  13205. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13206. if (INTEL_GEN(dev_priv) <= 3) {
  13207. error->plane[i].size = I915_READ(DSPSIZE(i));
  13208. error->plane[i].pos = I915_READ(DSPPOS(i));
  13209. }
  13210. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13211. error->plane[i].addr = I915_READ(DSPADDR(i));
  13212. if (INTEL_GEN(dev_priv) >= 4) {
  13213. error->plane[i].surface = I915_READ(DSPSURF(i));
  13214. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13215. }
  13216. error->pipe[i].source = I915_READ(PIPESRC(i));
  13217. if (HAS_GMCH_DISPLAY(dev_priv))
  13218. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13219. }
  13220. /* Note: this does not include DSI transcoders. */
  13221. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13222. if (HAS_DDI(dev_priv))
  13223. error->num_transcoders++; /* Account for eDP. */
  13224. for (i = 0; i < error->num_transcoders; i++) {
  13225. enum transcoder cpu_transcoder = transcoders[i];
  13226. error->transcoder[i].power_domain_on =
  13227. __intel_display_power_is_enabled(dev_priv,
  13228. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13229. if (!error->transcoder[i].power_domain_on)
  13230. continue;
  13231. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13232. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13233. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13234. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13235. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13236. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13237. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13238. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13239. }
  13240. return error;
  13241. }
  13242. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13243. void
  13244. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13245. struct intel_display_error_state *error)
  13246. {
  13247. struct drm_i915_private *dev_priv = m->i915;
  13248. int i;
  13249. if (!error)
  13250. return;
  13251. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  13252. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13253. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13254. error->power_well_driver);
  13255. for_each_pipe(dev_priv, i) {
  13256. err_printf(m, "Pipe [%d]:\n", i);
  13257. err_printf(m, " Power: %s\n",
  13258. onoff(error->pipe[i].power_domain_on));
  13259. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13260. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13261. err_printf(m, "Plane [%d]:\n", i);
  13262. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13263. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13264. if (INTEL_GEN(dev_priv) <= 3) {
  13265. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13266. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13267. }
  13268. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13269. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13270. if (INTEL_GEN(dev_priv) >= 4) {
  13271. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13272. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13273. }
  13274. err_printf(m, "Cursor [%d]:\n", i);
  13275. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13276. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13277. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13278. }
  13279. for (i = 0; i < error->num_transcoders; i++) {
  13280. err_printf(m, "CPU transcoder: %s\n",
  13281. transcoder_name(error->transcoder[i].cpu_transcoder));
  13282. err_printf(m, " Power: %s\n",
  13283. onoff(error->transcoder[i].power_domain_on));
  13284. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13285. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13286. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13287. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13288. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13289. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13290. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13291. }
  13292. }
  13293. #endif