intel_display.c 446 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879
  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 "intel_dsi.h"
  40. #include "i915_trace.h"
  41. #include <drm/drm_atomic.h>
  42. #include <drm/drm_atomic_helper.h>
  43. #include <drm/drm_dp_helper.h>
  44. #include <drm/drm_crtc_helper.h>
  45. #include <drm/drm_plane_helper.h>
  46. #include <drm/drm_rect.h>
  47. #include <linux/dma_remapping.h>
  48. #include <linux/reservation.h>
  49. static bool is_mmio_work(struct intel_flip_work *work)
  50. {
  51. return work->mmio_work.func;
  52. }
  53. /* Primary plane formats for gen <= 3 */
  54. static const uint32_t i8xx_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB1555,
  58. DRM_FORMAT_XRGB8888,
  59. };
  60. /* Primary plane formats for gen >= 4 */
  61. static const uint32_t i965_primary_formats[] = {
  62. DRM_FORMAT_C8,
  63. DRM_FORMAT_RGB565,
  64. DRM_FORMAT_XRGB8888,
  65. DRM_FORMAT_XBGR8888,
  66. DRM_FORMAT_XRGB2101010,
  67. DRM_FORMAT_XBGR2101010,
  68. };
  69. static const uint32_t skl_primary_formats[] = {
  70. DRM_FORMAT_C8,
  71. DRM_FORMAT_RGB565,
  72. DRM_FORMAT_XRGB8888,
  73. DRM_FORMAT_XBGR8888,
  74. DRM_FORMAT_ARGB8888,
  75. DRM_FORMAT_ABGR8888,
  76. DRM_FORMAT_XRGB2101010,
  77. DRM_FORMAT_XBGR2101010,
  78. DRM_FORMAT_YUYV,
  79. DRM_FORMAT_YVYU,
  80. DRM_FORMAT_UYVY,
  81. DRM_FORMAT_VYUY,
  82. };
  83. /* Cursor formats */
  84. static const uint32_t intel_cursor_formats[] = {
  85. DRM_FORMAT_ARGB8888,
  86. };
  87. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  88. struct intel_crtc_state *pipe_config);
  89. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  90. struct intel_crtc_state *pipe_config);
  91. static int intel_framebuffer_init(struct intel_framebuffer *ifb,
  92. struct drm_i915_gem_object *obj,
  93. struct drm_mode_fb_cmd2 *mode_cmd);
  94. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  95. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  96. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  97. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  98. struct intel_link_m_n *m_n,
  99. struct intel_link_m_n *m2_n2);
  100. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  101. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  102. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  103. static void vlv_prepare_pll(struct intel_crtc *crtc,
  104. const struct intel_crtc_state *pipe_config);
  105. static void chv_prepare_pll(struct intel_crtc *crtc,
  106. const struct intel_crtc_state *pipe_config);
  107. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  108. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  109. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  110. struct intel_crtc_state *crtc_state);
  111. static void skylake_pfit_enable(struct intel_crtc *crtc);
  112. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  113. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  114. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  115. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  116. struct intel_limit {
  117. struct {
  118. int min, max;
  119. } dot, vco, n, m, m1, m2, p, p1;
  120. struct {
  121. int dot_limit;
  122. int p2_slow, p2_fast;
  123. } p2;
  124. };
  125. /* returns HPLL frequency in kHz */
  126. int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
  127. {
  128. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  129. /* Obtain SKU information */
  130. mutex_lock(&dev_priv->sb_lock);
  131. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  132. CCK_FUSE_HPLL_FREQ_MASK;
  133. mutex_unlock(&dev_priv->sb_lock);
  134. return vco_freq[hpll_freq] * 1000;
  135. }
  136. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  137. const char *name, u32 reg, int ref_freq)
  138. {
  139. u32 val;
  140. int divider;
  141. mutex_lock(&dev_priv->sb_lock);
  142. val = vlv_cck_read(dev_priv, reg);
  143. mutex_unlock(&dev_priv->sb_lock);
  144. divider = val & CCK_FREQUENCY_VALUES;
  145. WARN((val & CCK_FREQUENCY_STATUS) !=
  146. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  147. "%s change in progress\n", name);
  148. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  149. }
  150. int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  151. const char *name, u32 reg)
  152. {
  153. if (dev_priv->hpll_freq == 0)
  154. dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
  155. return vlv_get_cck_clock(dev_priv, name, reg,
  156. dev_priv->hpll_freq);
  157. }
  158. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  159. {
  160. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  161. return;
  162. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  163. CCK_CZ_CLOCK_CONTROL);
  164. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  165. }
  166. static inline u32 /* units of 100MHz */
  167. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  168. const struct intel_crtc_state *pipe_config)
  169. {
  170. if (HAS_DDI(dev_priv))
  171. return pipe_config->port_clock; /* SPLL */
  172. else if (IS_GEN5(dev_priv))
  173. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  174. else
  175. return 270000;
  176. }
  177. static const struct intel_limit intel_limits_i8xx_dac = {
  178. .dot = { .min = 25000, .max = 350000 },
  179. .vco = { .min = 908000, .max = 1512000 },
  180. .n = { .min = 2, .max = 16 },
  181. .m = { .min = 96, .max = 140 },
  182. .m1 = { .min = 18, .max = 26 },
  183. .m2 = { .min = 6, .max = 16 },
  184. .p = { .min = 4, .max = 128 },
  185. .p1 = { .min = 2, .max = 33 },
  186. .p2 = { .dot_limit = 165000,
  187. .p2_slow = 4, .p2_fast = 2 },
  188. };
  189. static const struct intel_limit intel_limits_i8xx_dvo = {
  190. .dot = { .min = 25000, .max = 350000 },
  191. .vco = { .min = 908000, .max = 1512000 },
  192. .n = { .min = 2, .max = 16 },
  193. .m = { .min = 96, .max = 140 },
  194. .m1 = { .min = 18, .max = 26 },
  195. .m2 = { .min = 6, .max = 16 },
  196. .p = { .min = 4, .max = 128 },
  197. .p1 = { .min = 2, .max = 33 },
  198. .p2 = { .dot_limit = 165000,
  199. .p2_slow = 4, .p2_fast = 4 },
  200. };
  201. static const struct intel_limit intel_limits_i8xx_lvds = {
  202. .dot = { .min = 25000, .max = 350000 },
  203. .vco = { .min = 908000, .max = 1512000 },
  204. .n = { .min = 2, .max = 16 },
  205. .m = { .min = 96, .max = 140 },
  206. .m1 = { .min = 18, .max = 26 },
  207. .m2 = { .min = 6, .max = 16 },
  208. .p = { .min = 4, .max = 128 },
  209. .p1 = { .min = 1, .max = 6 },
  210. .p2 = { .dot_limit = 165000,
  211. .p2_slow = 14, .p2_fast = 7 },
  212. };
  213. static const struct intel_limit intel_limits_i9xx_sdvo = {
  214. .dot = { .min = 20000, .max = 400000 },
  215. .vco = { .min = 1400000, .max = 2800000 },
  216. .n = { .min = 1, .max = 6 },
  217. .m = { .min = 70, .max = 120 },
  218. .m1 = { .min = 8, .max = 18 },
  219. .m2 = { .min = 3, .max = 7 },
  220. .p = { .min = 5, .max = 80 },
  221. .p1 = { .min = 1, .max = 8 },
  222. .p2 = { .dot_limit = 200000,
  223. .p2_slow = 10, .p2_fast = 5 },
  224. };
  225. static const struct intel_limit intel_limits_i9xx_lvds = {
  226. .dot = { .min = 20000, .max = 400000 },
  227. .vco = { .min = 1400000, .max = 2800000 },
  228. .n = { .min = 1, .max = 6 },
  229. .m = { .min = 70, .max = 120 },
  230. .m1 = { .min = 8, .max = 18 },
  231. .m2 = { .min = 3, .max = 7 },
  232. .p = { .min = 7, .max = 98 },
  233. .p1 = { .min = 1, .max = 8 },
  234. .p2 = { .dot_limit = 112000,
  235. .p2_slow = 14, .p2_fast = 7 },
  236. };
  237. static const struct intel_limit intel_limits_g4x_sdvo = {
  238. .dot = { .min = 25000, .max = 270000 },
  239. .vco = { .min = 1750000, .max = 3500000},
  240. .n = { .min = 1, .max = 4 },
  241. .m = { .min = 104, .max = 138 },
  242. .m1 = { .min = 17, .max = 23 },
  243. .m2 = { .min = 5, .max = 11 },
  244. .p = { .min = 10, .max = 30 },
  245. .p1 = { .min = 1, .max = 3},
  246. .p2 = { .dot_limit = 270000,
  247. .p2_slow = 10,
  248. .p2_fast = 10
  249. },
  250. };
  251. static const struct intel_limit intel_limits_g4x_hdmi = {
  252. .dot = { .min = 22000, .max = 400000 },
  253. .vco = { .min = 1750000, .max = 3500000},
  254. .n = { .min = 1, .max = 4 },
  255. .m = { .min = 104, .max = 138 },
  256. .m1 = { .min = 16, .max = 23 },
  257. .m2 = { .min = 5, .max = 11 },
  258. .p = { .min = 5, .max = 80 },
  259. .p1 = { .min = 1, .max = 8},
  260. .p2 = { .dot_limit = 165000,
  261. .p2_slow = 10, .p2_fast = 5 },
  262. };
  263. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  264. .dot = { .min = 20000, .max = 115000 },
  265. .vco = { .min = 1750000, .max = 3500000 },
  266. .n = { .min = 1, .max = 3 },
  267. .m = { .min = 104, .max = 138 },
  268. .m1 = { .min = 17, .max = 23 },
  269. .m2 = { .min = 5, .max = 11 },
  270. .p = { .min = 28, .max = 112 },
  271. .p1 = { .min = 2, .max = 8 },
  272. .p2 = { .dot_limit = 0,
  273. .p2_slow = 14, .p2_fast = 14
  274. },
  275. };
  276. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  277. .dot = { .min = 80000, .max = 224000 },
  278. .vco = { .min = 1750000, .max = 3500000 },
  279. .n = { .min = 1, .max = 3 },
  280. .m = { .min = 104, .max = 138 },
  281. .m1 = { .min = 17, .max = 23 },
  282. .m2 = { .min = 5, .max = 11 },
  283. .p = { .min = 14, .max = 42 },
  284. .p1 = { .min = 2, .max = 6 },
  285. .p2 = { .dot_limit = 0,
  286. .p2_slow = 7, .p2_fast = 7
  287. },
  288. };
  289. static const struct intel_limit intel_limits_pineview_sdvo = {
  290. .dot = { .min = 20000, .max = 400000},
  291. .vco = { .min = 1700000, .max = 3500000 },
  292. /* Pineview's Ncounter is a ring counter */
  293. .n = { .min = 3, .max = 6 },
  294. .m = { .min = 2, .max = 256 },
  295. /* Pineview only has one combined m divider, which we treat as m2. */
  296. .m1 = { .min = 0, .max = 0 },
  297. .m2 = { .min = 0, .max = 254 },
  298. .p = { .min = 5, .max = 80 },
  299. .p1 = { .min = 1, .max = 8 },
  300. .p2 = { .dot_limit = 200000,
  301. .p2_slow = 10, .p2_fast = 5 },
  302. };
  303. static const struct intel_limit intel_limits_pineview_lvds = {
  304. .dot = { .min = 20000, .max = 400000 },
  305. .vco = { .min = 1700000, .max = 3500000 },
  306. .n = { .min = 3, .max = 6 },
  307. .m = { .min = 2, .max = 256 },
  308. .m1 = { .min = 0, .max = 0 },
  309. .m2 = { .min = 0, .max = 254 },
  310. .p = { .min = 7, .max = 112 },
  311. .p1 = { .min = 1, .max = 8 },
  312. .p2 = { .dot_limit = 112000,
  313. .p2_slow = 14, .p2_fast = 14 },
  314. };
  315. /* Ironlake / Sandybridge
  316. *
  317. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  318. * the range value for them is (actual_value - 2).
  319. */
  320. static const struct intel_limit intel_limits_ironlake_dac = {
  321. .dot = { .min = 25000, .max = 350000 },
  322. .vco = { .min = 1760000, .max = 3510000 },
  323. .n = { .min = 1, .max = 5 },
  324. .m = { .min = 79, .max = 127 },
  325. .m1 = { .min = 12, .max = 22 },
  326. .m2 = { .min = 5, .max = 9 },
  327. .p = { .min = 5, .max = 80 },
  328. .p1 = { .min = 1, .max = 8 },
  329. .p2 = { .dot_limit = 225000,
  330. .p2_slow = 10, .p2_fast = 5 },
  331. };
  332. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  333. .dot = { .min = 25000, .max = 350000 },
  334. .vco = { .min = 1760000, .max = 3510000 },
  335. .n = { .min = 1, .max = 3 },
  336. .m = { .min = 79, .max = 118 },
  337. .m1 = { .min = 12, .max = 22 },
  338. .m2 = { .min = 5, .max = 9 },
  339. .p = { .min = 28, .max = 112 },
  340. .p1 = { .min = 2, .max = 8 },
  341. .p2 = { .dot_limit = 225000,
  342. .p2_slow = 14, .p2_fast = 14 },
  343. };
  344. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  345. .dot = { .min = 25000, .max = 350000 },
  346. .vco = { .min = 1760000, .max = 3510000 },
  347. .n = { .min = 1, .max = 3 },
  348. .m = { .min = 79, .max = 127 },
  349. .m1 = { .min = 12, .max = 22 },
  350. .m2 = { .min = 5, .max = 9 },
  351. .p = { .min = 14, .max = 56 },
  352. .p1 = { .min = 2, .max = 8 },
  353. .p2 = { .dot_limit = 225000,
  354. .p2_slow = 7, .p2_fast = 7 },
  355. };
  356. /* LVDS 100mhz refclk limits. */
  357. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  358. .dot = { .min = 25000, .max = 350000 },
  359. .vco = { .min = 1760000, .max = 3510000 },
  360. .n = { .min = 1, .max = 2 },
  361. .m = { .min = 79, .max = 126 },
  362. .m1 = { .min = 12, .max = 22 },
  363. .m2 = { .min = 5, .max = 9 },
  364. .p = { .min = 28, .max = 112 },
  365. .p1 = { .min = 2, .max = 8 },
  366. .p2 = { .dot_limit = 225000,
  367. .p2_slow = 14, .p2_fast = 14 },
  368. };
  369. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  370. .dot = { .min = 25000, .max = 350000 },
  371. .vco = { .min = 1760000, .max = 3510000 },
  372. .n = { .min = 1, .max = 3 },
  373. .m = { .min = 79, .max = 126 },
  374. .m1 = { .min = 12, .max = 22 },
  375. .m2 = { .min = 5, .max = 9 },
  376. .p = { .min = 14, .max = 42 },
  377. .p1 = { .min = 2, .max = 6 },
  378. .p2 = { .dot_limit = 225000,
  379. .p2_slow = 7, .p2_fast = 7 },
  380. };
  381. static const struct intel_limit intel_limits_vlv = {
  382. /*
  383. * These are the data rate limits (measured in fast clocks)
  384. * since those are the strictest limits we have. The fast
  385. * clock and actual rate limits are more relaxed, so checking
  386. * them would make no difference.
  387. */
  388. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  389. .vco = { .min = 4000000, .max = 6000000 },
  390. .n = { .min = 1, .max = 7 },
  391. .m1 = { .min = 2, .max = 3 },
  392. .m2 = { .min = 11, .max = 156 },
  393. .p1 = { .min = 2, .max = 3 },
  394. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  395. };
  396. static const struct intel_limit intel_limits_chv = {
  397. /*
  398. * These are the data rate limits (measured in fast clocks)
  399. * since those are the strictest limits we have. The fast
  400. * clock and actual rate limits are more relaxed, so checking
  401. * them would make no difference.
  402. */
  403. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  404. .vco = { .min = 4800000, .max = 6480000 },
  405. .n = { .min = 1, .max = 1 },
  406. .m1 = { .min = 2, .max = 2 },
  407. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  408. .p1 = { .min = 2, .max = 4 },
  409. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  410. };
  411. static const struct intel_limit intel_limits_bxt = {
  412. /* FIXME: find real dot limits */
  413. .dot = { .min = 0, .max = INT_MAX },
  414. .vco = { .min = 4800000, .max = 6700000 },
  415. .n = { .min = 1, .max = 1 },
  416. .m1 = { .min = 2, .max = 2 },
  417. /* FIXME: find real m2 limits */
  418. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  419. .p1 = { .min = 2, .max = 4 },
  420. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  421. };
  422. static bool
  423. needs_modeset(struct drm_crtc_state *state)
  424. {
  425. return drm_atomic_crtc_needs_modeset(state);
  426. }
  427. /*
  428. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  429. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  430. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  431. * The helpers' return value is the rate of the clock that is fed to the
  432. * display engine's pipe which can be the above fast dot clock rate or a
  433. * divided-down version of it.
  434. */
  435. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  436. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  437. {
  438. clock->m = clock->m2 + 2;
  439. clock->p = clock->p1 * clock->p2;
  440. if (WARN_ON(clock->n == 0 || clock->p == 0))
  441. return 0;
  442. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  443. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  444. return clock->dot;
  445. }
  446. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  447. {
  448. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  449. }
  450. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  451. {
  452. clock->m = i9xx_dpll_compute_m(clock);
  453. clock->p = clock->p1 * clock->p2;
  454. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  455. return 0;
  456. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  457. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  458. return clock->dot;
  459. }
  460. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  461. {
  462. clock->m = clock->m1 * clock->m2;
  463. clock->p = clock->p1 * clock->p2;
  464. if (WARN_ON(clock->n == 0 || clock->p == 0))
  465. return 0;
  466. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  467. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  468. return clock->dot / 5;
  469. }
  470. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  471. {
  472. clock->m = clock->m1 * clock->m2;
  473. clock->p = clock->p1 * clock->p2;
  474. if (WARN_ON(clock->n == 0 || clock->p == 0))
  475. return 0;
  476. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  477. clock->n << 22);
  478. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  479. return clock->dot / 5;
  480. }
  481. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  482. /**
  483. * Returns whether the given set of divisors are valid for a given refclk with
  484. * the given connectors.
  485. */
  486. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  487. const struct intel_limit *limit,
  488. const struct dpll *clock)
  489. {
  490. if (clock->n < limit->n.min || limit->n.max < clock->n)
  491. INTELPllInvalid("n out of range\n");
  492. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  493. INTELPllInvalid("p1 out of range\n");
  494. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  495. INTELPllInvalid("m2 out of range\n");
  496. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  497. INTELPllInvalid("m1 out of range\n");
  498. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  499. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  500. if (clock->m1 <= clock->m2)
  501. INTELPllInvalid("m1 <= m2\n");
  502. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  503. !IS_GEN9_LP(dev_priv)) {
  504. if (clock->p < limit->p.min || limit->p.max < clock->p)
  505. INTELPllInvalid("p out of range\n");
  506. if (clock->m < limit->m.min || limit->m.max < clock->m)
  507. INTELPllInvalid("m out of range\n");
  508. }
  509. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  510. INTELPllInvalid("vco out of range\n");
  511. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  512. * connector, etc., rather than just a single range.
  513. */
  514. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  515. INTELPllInvalid("dot out of range\n");
  516. return true;
  517. }
  518. static int
  519. i9xx_select_p2_div(const struct intel_limit *limit,
  520. const struct intel_crtc_state *crtc_state,
  521. int target)
  522. {
  523. struct drm_device *dev = crtc_state->base.crtc->dev;
  524. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  525. /*
  526. * For LVDS just rely on its current settings for dual-channel.
  527. * We haven't figured out how to reliably set up different
  528. * single/dual channel state, if we even can.
  529. */
  530. if (intel_is_dual_link_lvds(dev))
  531. return limit->p2.p2_fast;
  532. else
  533. return limit->p2.p2_slow;
  534. } else {
  535. if (target < limit->p2.dot_limit)
  536. return limit->p2.p2_slow;
  537. else
  538. return limit->p2.p2_fast;
  539. }
  540. }
  541. /*
  542. * Returns a set of divisors for the desired target clock with the given
  543. * refclk, or FALSE. The returned values represent the clock equation:
  544. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  545. *
  546. * Target and reference clocks are specified in kHz.
  547. *
  548. * If match_clock is provided, then best_clock P divider must match the P
  549. * divider from @match_clock used for LVDS downclocking.
  550. */
  551. static bool
  552. i9xx_find_best_dpll(const struct intel_limit *limit,
  553. struct intel_crtc_state *crtc_state,
  554. int target, int refclk, struct dpll *match_clock,
  555. struct dpll *best_clock)
  556. {
  557. struct drm_device *dev = crtc_state->base.crtc->dev;
  558. struct dpll clock;
  559. int err = target;
  560. memset(best_clock, 0, sizeof(*best_clock));
  561. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  562. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  563. clock.m1++) {
  564. for (clock.m2 = limit->m2.min;
  565. clock.m2 <= limit->m2.max; clock.m2++) {
  566. if (clock.m2 >= clock.m1)
  567. break;
  568. for (clock.n = limit->n.min;
  569. clock.n <= limit->n.max; clock.n++) {
  570. for (clock.p1 = limit->p1.min;
  571. clock.p1 <= limit->p1.max; clock.p1++) {
  572. int this_err;
  573. i9xx_calc_dpll_params(refclk, &clock);
  574. if (!intel_PLL_is_valid(to_i915(dev),
  575. limit,
  576. &clock))
  577. continue;
  578. if (match_clock &&
  579. clock.p != match_clock->p)
  580. continue;
  581. this_err = abs(clock.dot - target);
  582. if (this_err < err) {
  583. *best_clock = clock;
  584. err = this_err;
  585. }
  586. }
  587. }
  588. }
  589. }
  590. return (err != target);
  591. }
  592. /*
  593. * Returns a set of divisors for the desired target clock with the given
  594. * refclk, or FALSE. The returned values represent the clock equation:
  595. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  596. *
  597. * Target and reference clocks are specified in kHz.
  598. *
  599. * If match_clock is provided, then best_clock P divider must match the P
  600. * divider from @match_clock used for LVDS downclocking.
  601. */
  602. static bool
  603. pnv_find_best_dpll(const struct intel_limit *limit,
  604. struct intel_crtc_state *crtc_state,
  605. int target, int refclk, struct dpll *match_clock,
  606. struct dpll *best_clock)
  607. {
  608. struct drm_device *dev = crtc_state->base.crtc->dev;
  609. struct dpll clock;
  610. int err = target;
  611. memset(best_clock, 0, sizeof(*best_clock));
  612. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  613. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  614. clock.m1++) {
  615. for (clock.m2 = limit->m2.min;
  616. clock.m2 <= limit->m2.max; clock.m2++) {
  617. for (clock.n = limit->n.min;
  618. clock.n <= limit->n.max; clock.n++) {
  619. for (clock.p1 = limit->p1.min;
  620. clock.p1 <= limit->p1.max; clock.p1++) {
  621. int this_err;
  622. pnv_calc_dpll_params(refclk, &clock);
  623. if (!intel_PLL_is_valid(to_i915(dev),
  624. limit,
  625. &clock))
  626. continue;
  627. if (match_clock &&
  628. clock.p != match_clock->p)
  629. continue;
  630. this_err = abs(clock.dot - target);
  631. if (this_err < err) {
  632. *best_clock = clock;
  633. err = this_err;
  634. }
  635. }
  636. }
  637. }
  638. }
  639. return (err != target);
  640. }
  641. /*
  642. * Returns a set of divisors for the desired target clock with the given
  643. * refclk, or FALSE. The returned values represent the clock equation:
  644. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  645. *
  646. * Target and reference clocks are specified in kHz.
  647. *
  648. * If match_clock is provided, then best_clock P divider must match the P
  649. * divider from @match_clock used for LVDS downclocking.
  650. */
  651. static bool
  652. g4x_find_best_dpll(const struct intel_limit *limit,
  653. struct intel_crtc_state *crtc_state,
  654. int target, int refclk, struct dpll *match_clock,
  655. struct dpll *best_clock)
  656. {
  657. struct drm_device *dev = crtc_state->base.crtc->dev;
  658. struct dpll clock;
  659. int max_n;
  660. bool found = false;
  661. /* approximately equals target * 0.00585 */
  662. int err_most = (target >> 8) + (target >> 9);
  663. memset(best_clock, 0, sizeof(*best_clock));
  664. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  665. max_n = limit->n.max;
  666. /* based on hardware requirement, prefer smaller n to precision */
  667. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  668. /* based on hardware requirement, prefere larger m1,m2 */
  669. for (clock.m1 = limit->m1.max;
  670. clock.m1 >= limit->m1.min; clock.m1--) {
  671. for (clock.m2 = limit->m2.max;
  672. clock.m2 >= limit->m2.min; clock.m2--) {
  673. for (clock.p1 = limit->p1.max;
  674. clock.p1 >= limit->p1.min; clock.p1--) {
  675. int this_err;
  676. i9xx_calc_dpll_params(refclk, &clock);
  677. if (!intel_PLL_is_valid(to_i915(dev),
  678. limit,
  679. &clock))
  680. continue;
  681. this_err = abs(clock.dot - target);
  682. if (this_err < err_most) {
  683. *best_clock = clock;
  684. err_most = this_err;
  685. max_n = clock.n;
  686. found = true;
  687. }
  688. }
  689. }
  690. }
  691. }
  692. return found;
  693. }
  694. /*
  695. * Check if the calculated PLL configuration is more optimal compared to the
  696. * best configuration and error found so far. Return the calculated error.
  697. */
  698. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  699. const struct dpll *calculated_clock,
  700. const struct dpll *best_clock,
  701. unsigned int best_error_ppm,
  702. unsigned int *error_ppm)
  703. {
  704. /*
  705. * For CHV ignore the error and consider only the P value.
  706. * Prefer a bigger P value based on HW requirements.
  707. */
  708. if (IS_CHERRYVIEW(to_i915(dev))) {
  709. *error_ppm = 0;
  710. return calculated_clock->p > best_clock->p;
  711. }
  712. if (WARN_ON_ONCE(!target_freq))
  713. return false;
  714. *error_ppm = div_u64(1000000ULL *
  715. abs(target_freq - calculated_clock->dot),
  716. target_freq);
  717. /*
  718. * Prefer a better P value over a better (smaller) error if the error
  719. * is small. Ensure this preference for future configurations too by
  720. * setting the error to 0.
  721. */
  722. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  723. *error_ppm = 0;
  724. return true;
  725. }
  726. return *error_ppm + 10 < best_error_ppm;
  727. }
  728. /*
  729. * Returns a set of divisors for the desired target clock with the given
  730. * refclk, or FALSE. The returned values represent the clock equation:
  731. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  732. */
  733. static bool
  734. vlv_find_best_dpll(const struct intel_limit *limit,
  735. struct intel_crtc_state *crtc_state,
  736. int target, int refclk, struct dpll *match_clock,
  737. struct dpll *best_clock)
  738. {
  739. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  740. struct drm_device *dev = crtc->base.dev;
  741. struct dpll clock;
  742. unsigned int bestppm = 1000000;
  743. /* min update 19.2 MHz */
  744. int max_n = min(limit->n.max, refclk / 19200);
  745. bool found = false;
  746. target *= 5; /* fast clock */
  747. memset(best_clock, 0, sizeof(*best_clock));
  748. /* based on hardware requirement, prefer smaller n to precision */
  749. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  750. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  751. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  752. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  753. clock.p = clock.p1 * clock.p2;
  754. /* based on hardware requirement, prefer bigger m1,m2 values */
  755. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  756. unsigned int ppm;
  757. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  758. refclk * clock.m1);
  759. vlv_calc_dpll_params(refclk, &clock);
  760. if (!intel_PLL_is_valid(to_i915(dev),
  761. limit,
  762. &clock))
  763. continue;
  764. if (!vlv_PLL_is_optimal(dev, target,
  765. &clock,
  766. best_clock,
  767. bestppm, &ppm))
  768. continue;
  769. *best_clock = clock;
  770. bestppm = ppm;
  771. found = true;
  772. }
  773. }
  774. }
  775. }
  776. return found;
  777. }
  778. /*
  779. * Returns a set of divisors for the desired target clock with the given
  780. * refclk, or FALSE. The returned values represent the clock equation:
  781. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  782. */
  783. static bool
  784. chv_find_best_dpll(const struct intel_limit *limit,
  785. struct intel_crtc_state *crtc_state,
  786. int target, int refclk, struct dpll *match_clock,
  787. struct dpll *best_clock)
  788. {
  789. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  790. struct drm_device *dev = crtc->base.dev;
  791. unsigned int best_error_ppm;
  792. struct dpll clock;
  793. uint64_t m2;
  794. int found = false;
  795. memset(best_clock, 0, sizeof(*best_clock));
  796. best_error_ppm = 1000000;
  797. /*
  798. * Based on hardware doc, the n always set to 1, and m1 always
  799. * set to 2. If requires to support 200Mhz refclk, we need to
  800. * revisit this because n may not 1 anymore.
  801. */
  802. clock.n = 1, clock.m1 = 2;
  803. target *= 5; /* fast clock */
  804. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  805. for (clock.p2 = limit->p2.p2_fast;
  806. clock.p2 >= limit->p2.p2_slow;
  807. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  808. unsigned int error_ppm;
  809. clock.p = clock.p1 * clock.p2;
  810. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  811. clock.n) << 22, refclk * clock.m1);
  812. if (m2 > INT_MAX/clock.m1)
  813. continue;
  814. clock.m2 = m2;
  815. chv_calc_dpll_params(refclk, &clock);
  816. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  817. continue;
  818. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  819. best_error_ppm, &error_ppm))
  820. continue;
  821. *best_clock = clock;
  822. best_error_ppm = error_ppm;
  823. found = true;
  824. }
  825. }
  826. return found;
  827. }
  828. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  829. struct dpll *best_clock)
  830. {
  831. int refclk = 100000;
  832. const struct intel_limit *limit = &intel_limits_bxt;
  833. return chv_find_best_dpll(limit, crtc_state,
  834. target_clock, refclk, NULL, best_clock);
  835. }
  836. bool intel_crtc_active(struct intel_crtc *crtc)
  837. {
  838. /* Be paranoid as we can arrive here with only partial
  839. * state retrieved from the hardware during setup.
  840. *
  841. * We can ditch the adjusted_mode.crtc_clock check as soon
  842. * as Haswell has gained clock readout/fastboot support.
  843. *
  844. * We can ditch the crtc->primary->fb check as soon as we can
  845. * properly reconstruct framebuffers.
  846. *
  847. * FIXME: The intel_crtc->active here should be switched to
  848. * crtc->state->active once we have proper CRTC states wired up
  849. * for atomic.
  850. */
  851. return crtc->active && crtc->base.primary->state->fb &&
  852. crtc->config->base.adjusted_mode.crtc_clock;
  853. }
  854. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  855. enum pipe pipe)
  856. {
  857. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  858. return crtc->config->cpu_transcoder;
  859. }
  860. static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  861. {
  862. i915_reg_t reg = PIPEDSL(pipe);
  863. u32 line1, line2;
  864. u32 line_mask;
  865. if (IS_GEN2(dev_priv))
  866. line_mask = DSL_LINEMASK_GEN2;
  867. else
  868. line_mask = DSL_LINEMASK_GEN3;
  869. line1 = I915_READ(reg) & line_mask;
  870. msleep(5);
  871. line2 = I915_READ(reg) & line_mask;
  872. return line1 == line2;
  873. }
  874. /*
  875. * intel_wait_for_pipe_off - wait for pipe to turn off
  876. * @crtc: crtc whose pipe to wait for
  877. *
  878. * After disabling a pipe, we can't wait for vblank in the usual way,
  879. * spinning on the vblank interrupt status bit, since we won't actually
  880. * see an interrupt when the pipe is disabled.
  881. *
  882. * On Gen4 and above:
  883. * wait for the pipe register state bit to turn off
  884. *
  885. * Otherwise:
  886. * wait for the display line value to settle (it usually
  887. * ends up stopping at the start of the next frame).
  888. *
  889. */
  890. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  891. {
  892. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  893. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  894. enum pipe pipe = crtc->pipe;
  895. if (INTEL_GEN(dev_priv) >= 4) {
  896. i915_reg_t reg = PIPECONF(cpu_transcoder);
  897. /* Wait for the Pipe State to go off */
  898. if (intel_wait_for_register(dev_priv,
  899. reg, I965_PIPECONF_ACTIVE, 0,
  900. 100))
  901. WARN(1, "pipe_off wait timed out\n");
  902. } else {
  903. /* Wait for the display line to settle */
  904. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  905. WARN(1, "pipe_off wait timed out\n");
  906. }
  907. }
  908. /* Only for pre-ILK configs */
  909. void assert_pll(struct drm_i915_private *dev_priv,
  910. enum pipe pipe, bool state)
  911. {
  912. u32 val;
  913. bool cur_state;
  914. val = I915_READ(DPLL(pipe));
  915. cur_state = !!(val & DPLL_VCO_ENABLE);
  916. I915_STATE_WARN(cur_state != state,
  917. "PLL state assertion failure (expected %s, current %s)\n",
  918. onoff(state), onoff(cur_state));
  919. }
  920. /* XXX: the dsi pll is shared between MIPI DSI ports */
  921. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  922. {
  923. u32 val;
  924. bool cur_state;
  925. mutex_lock(&dev_priv->sb_lock);
  926. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  927. mutex_unlock(&dev_priv->sb_lock);
  928. cur_state = val & DSI_PLL_VCO_EN;
  929. I915_STATE_WARN(cur_state != state,
  930. "DSI PLL state assertion failure (expected %s, current %s)\n",
  931. onoff(state), onoff(cur_state));
  932. }
  933. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  934. enum pipe pipe, bool state)
  935. {
  936. bool cur_state;
  937. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  938. pipe);
  939. if (HAS_DDI(dev_priv)) {
  940. /* DDI does not have a specific FDI_TX register */
  941. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  942. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  943. } else {
  944. u32 val = I915_READ(FDI_TX_CTL(pipe));
  945. cur_state = !!(val & FDI_TX_ENABLE);
  946. }
  947. I915_STATE_WARN(cur_state != state,
  948. "FDI TX state assertion failure (expected %s, current %s)\n",
  949. onoff(state), onoff(cur_state));
  950. }
  951. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  952. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  953. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  954. enum pipe pipe, bool state)
  955. {
  956. u32 val;
  957. bool cur_state;
  958. val = I915_READ(FDI_RX_CTL(pipe));
  959. cur_state = !!(val & FDI_RX_ENABLE);
  960. I915_STATE_WARN(cur_state != state,
  961. "FDI RX state assertion failure (expected %s, current %s)\n",
  962. onoff(state), onoff(cur_state));
  963. }
  964. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  965. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  966. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  967. enum pipe pipe)
  968. {
  969. u32 val;
  970. /* ILK FDI PLL is always enabled */
  971. if (IS_GEN5(dev_priv))
  972. return;
  973. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  974. if (HAS_DDI(dev_priv))
  975. return;
  976. val = I915_READ(FDI_TX_CTL(pipe));
  977. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  978. }
  979. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  980. enum pipe pipe, bool state)
  981. {
  982. u32 val;
  983. bool cur_state;
  984. val = I915_READ(FDI_RX_CTL(pipe));
  985. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  986. I915_STATE_WARN(cur_state != state,
  987. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  988. onoff(state), onoff(cur_state));
  989. }
  990. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  991. {
  992. i915_reg_t pp_reg;
  993. u32 val;
  994. enum pipe panel_pipe = PIPE_A;
  995. bool locked = true;
  996. if (WARN_ON(HAS_DDI(dev_priv)))
  997. return;
  998. if (HAS_PCH_SPLIT(dev_priv)) {
  999. u32 port_sel;
  1000. pp_reg = PP_CONTROL(0);
  1001. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1002. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1003. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1004. panel_pipe = PIPE_B;
  1005. /* XXX: else fix for eDP */
  1006. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1007. /* presumably write lock depends on pipe, not port select */
  1008. pp_reg = PP_CONTROL(pipe);
  1009. panel_pipe = pipe;
  1010. } else {
  1011. pp_reg = PP_CONTROL(0);
  1012. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1013. panel_pipe = PIPE_B;
  1014. }
  1015. val = I915_READ(pp_reg);
  1016. if (!(val & PANEL_POWER_ON) ||
  1017. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1018. locked = false;
  1019. I915_STATE_WARN(panel_pipe == pipe && locked,
  1020. "panel assertion failure, pipe %c regs locked\n",
  1021. pipe_name(pipe));
  1022. }
  1023. static void assert_cursor(struct drm_i915_private *dev_priv,
  1024. enum pipe pipe, bool state)
  1025. {
  1026. bool cur_state;
  1027. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1028. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1029. else
  1030. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1031. I915_STATE_WARN(cur_state != state,
  1032. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1033. pipe_name(pipe), onoff(state), onoff(cur_state));
  1034. }
  1035. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1036. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1037. void assert_pipe(struct drm_i915_private *dev_priv,
  1038. enum pipe pipe, bool state)
  1039. {
  1040. bool cur_state;
  1041. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1042. pipe);
  1043. enum intel_display_power_domain power_domain;
  1044. /* if we need the pipe quirk it must be always on */
  1045. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1046. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1047. state = true;
  1048. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1049. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1050. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1051. cur_state = !!(val & PIPECONF_ENABLE);
  1052. intel_display_power_put(dev_priv, power_domain);
  1053. } else {
  1054. cur_state = false;
  1055. }
  1056. I915_STATE_WARN(cur_state != state,
  1057. "pipe %c assertion failure (expected %s, current %s)\n",
  1058. pipe_name(pipe), onoff(state), onoff(cur_state));
  1059. }
  1060. static void assert_plane(struct drm_i915_private *dev_priv,
  1061. enum plane plane, bool state)
  1062. {
  1063. u32 val;
  1064. bool cur_state;
  1065. val = I915_READ(DSPCNTR(plane));
  1066. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1067. I915_STATE_WARN(cur_state != state,
  1068. "plane %c assertion failure (expected %s, current %s)\n",
  1069. plane_name(plane), onoff(state), onoff(cur_state));
  1070. }
  1071. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1072. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1073. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1074. enum pipe pipe)
  1075. {
  1076. int i;
  1077. /* Primary planes are fixed to pipes on gen4+ */
  1078. if (INTEL_GEN(dev_priv) >= 4) {
  1079. u32 val = I915_READ(DSPCNTR(pipe));
  1080. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1081. "plane %c assertion failure, should be disabled but not\n",
  1082. plane_name(pipe));
  1083. return;
  1084. }
  1085. /* Need to check both planes against the pipe */
  1086. for_each_pipe(dev_priv, i) {
  1087. u32 val = I915_READ(DSPCNTR(i));
  1088. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1089. DISPPLANE_SEL_PIPE_SHIFT;
  1090. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1091. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1092. plane_name(i), pipe_name(pipe));
  1093. }
  1094. }
  1095. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1096. enum pipe pipe)
  1097. {
  1098. int sprite;
  1099. if (INTEL_GEN(dev_priv) >= 9) {
  1100. for_each_sprite(dev_priv, pipe, sprite) {
  1101. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1102. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1103. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1104. sprite, pipe_name(pipe));
  1105. }
  1106. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1107. for_each_sprite(dev_priv, pipe, sprite) {
  1108. u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
  1109. I915_STATE_WARN(val & SP_ENABLE,
  1110. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1111. sprite_name(pipe, sprite), pipe_name(pipe));
  1112. }
  1113. } else if (INTEL_GEN(dev_priv) >= 7) {
  1114. u32 val = I915_READ(SPRCTL(pipe));
  1115. I915_STATE_WARN(val & SPRITE_ENABLE,
  1116. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1117. plane_name(pipe), pipe_name(pipe));
  1118. } else if (INTEL_GEN(dev_priv) >= 5) {
  1119. u32 val = I915_READ(DVSCNTR(pipe));
  1120. I915_STATE_WARN(val & DVS_ENABLE,
  1121. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1122. plane_name(pipe), pipe_name(pipe));
  1123. }
  1124. }
  1125. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1126. {
  1127. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1128. drm_crtc_vblank_put(crtc);
  1129. }
  1130. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1131. enum pipe pipe)
  1132. {
  1133. u32 val;
  1134. bool enabled;
  1135. val = I915_READ(PCH_TRANSCONF(pipe));
  1136. enabled = !!(val & TRANS_ENABLE);
  1137. I915_STATE_WARN(enabled,
  1138. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1139. pipe_name(pipe));
  1140. }
  1141. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1142. enum pipe pipe, u32 port_sel, u32 val)
  1143. {
  1144. if ((val & DP_PORT_EN) == 0)
  1145. return false;
  1146. if (HAS_PCH_CPT(dev_priv)) {
  1147. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1148. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1149. return false;
  1150. } else if (IS_CHERRYVIEW(dev_priv)) {
  1151. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1152. return false;
  1153. } else {
  1154. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1155. return false;
  1156. }
  1157. return true;
  1158. }
  1159. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1160. enum pipe pipe, u32 val)
  1161. {
  1162. if ((val & SDVO_ENABLE) == 0)
  1163. return false;
  1164. if (HAS_PCH_CPT(dev_priv)) {
  1165. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1166. return false;
  1167. } else if (IS_CHERRYVIEW(dev_priv)) {
  1168. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1169. return false;
  1170. } else {
  1171. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1172. return false;
  1173. }
  1174. return true;
  1175. }
  1176. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1177. enum pipe pipe, u32 val)
  1178. {
  1179. if ((val & LVDS_PORT_EN) == 0)
  1180. return false;
  1181. if (HAS_PCH_CPT(dev_priv)) {
  1182. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1183. return false;
  1184. } else {
  1185. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1186. return false;
  1187. }
  1188. return true;
  1189. }
  1190. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1191. enum pipe pipe, u32 val)
  1192. {
  1193. if ((val & ADPA_DAC_ENABLE) == 0)
  1194. return false;
  1195. if (HAS_PCH_CPT(dev_priv)) {
  1196. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1197. return false;
  1198. } else {
  1199. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1200. return false;
  1201. }
  1202. return true;
  1203. }
  1204. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1205. enum pipe pipe, i915_reg_t reg,
  1206. u32 port_sel)
  1207. {
  1208. u32 val = I915_READ(reg);
  1209. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1210. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1211. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1212. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1213. && (val & DP_PIPEB_SELECT),
  1214. "IBX PCH dp port still using transcoder B\n");
  1215. }
  1216. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1217. enum pipe pipe, i915_reg_t reg)
  1218. {
  1219. u32 val = I915_READ(reg);
  1220. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1221. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1222. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1223. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1224. && (val & SDVO_PIPE_B_SELECT),
  1225. "IBX PCH hdmi port still using transcoder B\n");
  1226. }
  1227. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1228. enum pipe pipe)
  1229. {
  1230. u32 val;
  1231. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1232. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1233. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1234. val = I915_READ(PCH_ADPA);
  1235. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1236. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1237. pipe_name(pipe));
  1238. val = I915_READ(PCH_LVDS);
  1239. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1240. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1241. pipe_name(pipe));
  1242. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1243. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1244. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1245. }
  1246. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1247. const struct intel_crtc_state *pipe_config)
  1248. {
  1249. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1250. enum pipe pipe = crtc->pipe;
  1251. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1252. POSTING_READ(DPLL(pipe));
  1253. udelay(150);
  1254. if (intel_wait_for_register(dev_priv,
  1255. DPLL(pipe),
  1256. DPLL_LOCK_VLV,
  1257. DPLL_LOCK_VLV,
  1258. 1))
  1259. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1260. }
  1261. static void vlv_enable_pll(struct intel_crtc *crtc,
  1262. const struct intel_crtc_state *pipe_config)
  1263. {
  1264. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1265. enum pipe pipe = crtc->pipe;
  1266. assert_pipe_disabled(dev_priv, pipe);
  1267. /* PLL is protected by panel, make sure we can write it */
  1268. assert_panel_unlocked(dev_priv, pipe);
  1269. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1270. _vlv_enable_pll(crtc, pipe_config);
  1271. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1272. POSTING_READ(DPLL_MD(pipe));
  1273. }
  1274. static void _chv_enable_pll(struct intel_crtc *crtc,
  1275. const struct intel_crtc_state *pipe_config)
  1276. {
  1277. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1278. enum pipe pipe = crtc->pipe;
  1279. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1280. u32 tmp;
  1281. mutex_lock(&dev_priv->sb_lock);
  1282. /* Enable back the 10bit clock to display controller */
  1283. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1284. tmp |= DPIO_DCLKP_EN;
  1285. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1286. mutex_unlock(&dev_priv->sb_lock);
  1287. /*
  1288. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1289. */
  1290. udelay(1);
  1291. /* Enable PLL */
  1292. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1293. /* Check PLL is locked */
  1294. if (intel_wait_for_register(dev_priv,
  1295. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1296. 1))
  1297. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1298. }
  1299. static void chv_enable_pll(struct intel_crtc *crtc,
  1300. const struct intel_crtc_state *pipe_config)
  1301. {
  1302. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1303. enum pipe pipe = crtc->pipe;
  1304. assert_pipe_disabled(dev_priv, pipe);
  1305. /* PLL is protected by panel, make sure we can write it */
  1306. assert_panel_unlocked(dev_priv, pipe);
  1307. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1308. _chv_enable_pll(crtc, pipe_config);
  1309. if (pipe != PIPE_A) {
  1310. /*
  1311. * WaPixelRepeatModeFixForC0:chv
  1312. *
  1313. * DPLLCMD is AWOL. Use chicken bits to propagate
  1314. * the value from DPLLBMD to either pipe B or C.
  1315. */
  1316. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1317. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1318. I915_WRITE(CBR4_VLV, 0);
  1319. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1320. /*
  1321. * DPLLB VGA mode also seems to cause problems.
  1322. * We should always have it disabled.
  1323. */
  1324. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1325. } else {
  1326. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1327. POSTING_READ(DPLL_MD(pipe));
  1328. }
  1329. }
  1330. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1331. {
  1332. struct intel_crtc *crtc;
  1333. int count = 0;
  1334. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1335. count += crtc->base.state->active &&
  1336. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1337. }
  1338. return count;
  1339. }
  1340. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1341. {
  1342. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1343. i915_reg_t reg = DPLL(crtc->pipe);
  1344. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1345. assert_pipe_disabled(dev_priv, crtc->pipe);
  1346. /* PLL is protected by panel, make sure we can write it */
  1347. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1348. assert_panel_unlocked(dev_priv, crtc->pipe);
  1349. /* Enable DVO 2x clock on both PLLs if necessary */
  1350. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1351. /*
  1352. * It appears to be important that we don't enable this
  1353. * for the current pipe before otherwise configuring the
  1354. * PLL. No idea how this should be handled if multiple
  1355. * DVO outputs are enabled simultaneosly.
  1356. */
  1357. dpll |= DPLL_DVO_2X_MODE;
  1358. I915_WRITE(DPLL(!crtc->pipe),
  1359. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1360. }
  1361. /*
  1362. * Apparently we need to have VGA mode enabled prior to changing
  1363. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1364. * dividers, even though the register value does change.
  1365. */
  1366. I915_WRITE(reg, 0);
  1367. I915_WRITE(reg, dpll);
  1368. /* Wait for the clocks to stabilize. */
  1369. POSTING_READ(reg);
  1370. udelay(150);
  1371. if (INTEL_GEN(dev_priv) >= 4) {
  1372. I915_WRITE(DPLL_MD(crtc->pipe),
  1373. crtc->config->dpll_hw_state.dpll_md);
  1374. } else {
  1375. /* The pixel multiplier can only be updated once the
  1376. * DPLL is enabled and the clocks are stable.
  1377. *
  1378. * So write it again.
  1379. */
  1380. I915_WRITE(reg, dpll);
  1381. }
  1382. /* We do this three times for luck */
  1383. I915_WRITE(reg, dpll);
  1384. POSTING_READ(reg);
  1385. udelay(150); /* wait for warmup */
  1386. I915_WRITE(reg, dpll);
  1387. POSTING_READ(reg);
  1388. udelay(150); /* wait for warmup */
  1389. I915_WRITE(reg, dpll);
  1390. POSTING_READ(reg);
  1391. udelay(150); /* wait for warmup */
  1392. }
  1393. /**
  1394. * i9xx_disable_pll - disable a PLL
  1395. * @dev_priv: i915 private structure
  1396. * @pipe: pipe PLL to disable
  1397. *
  1398. * Disable the PLL for @pipe, making sure the pipe is off first.
  1399. *
  1400. * Note! This is for pre-ILK only.
  1401. */
  1402. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1403. {
  1404. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1405. enum pipe pipe = crtc->pipe;
  1406. /* Disable DVO 2x clock on both PLLs if necessary */
  1407. if (IS_I830(dev_priv) &&
  1408. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1409. !intel_num_dvo_pipes(dev_priv)) {
  1410. I915_WRITE(DPLL(PIPE_B),
  1411. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1412. I915_WRITE(DPLL(PIPE_A),
  1413. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1414. }
  1415. /* Don't disable pipe or pipe PLLs if needed */
  1416. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1417. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1418. return;
  1419. /* Make sure the pipe isn't still relying on us */
  1420. assert_pipe_disabled(dev_priv, pipe);
  1421. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1422. POSTING_READ(DPLL(pipe));
  1423. }
  1424. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1425. {
  1426. u32 val;
  1427. /* Make sure the pipe isn't still relying on us */
  1428. assert_pipe_disabled(dev_priv, pipe);
  1429. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1430. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1431. if (pipe != PIPE_A)
  1432. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1433. I915_WRITE(DPLL(pipe), val);
  1434. POSTING_READ(DPLL(pipe));
  1435. }
  1436. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1437. {
  1438. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1439. u32 val;
  1440. /* Make sure the pipe isn't still relying on us */
  1441. assert_pipe_disabled(dev_priv, pipe);
  1442. val = DPLL_SSC_REF_CLK_CHV |
  1443. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1444. if (pipe != PIPE_A)
  1445. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1446. I915_WRITE(DPLL(pipe), val);
  1447. POSTING_READ(DPLL(pipe));
  1448. mutex_lock(&dev_priv->sb_lock);
  1449. /* Disable 10bit clock to display controller */
  1450. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1451. val &= ~DPIO_DCLKP_EN;
  1452. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1453. mutex_unlock(&dev_priv->sb_lock);
  1454. }
  1455. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1456. struct intel_digital_port *dport,
  1457. unsigned int expected_mask)
  1458. {
  1459. u32 port_mask;
  1460. i915_reg_t dpll_reg;
  1461. switch (dport->port) {
  1462. case PORT_B:
  1463. port_mask = DPLL_PORTB_READY_MASK;
  1464. dpll_reg = DPLL(0);
  1465. break;
  1466. case PORT_C:
  1467. port_mask = DPLL_PORTC_READY_MASK;
  1468. dpll_reg = DPLL(0);
  1469. expected_mask <<= 4;
  1470. break;
  1471. case PORT_D:
  1472. port_mask = DPLL_PORTD_READY_MASK;
  1473. dpll_reg = DPIO_PHY_STATUS;
  1474. break;
  1475. default:
  1476. BUG();
  1477. }
  1478. if (intel_wait_for_register(dev_priv,
  1479. dpll_reg, port_mask, expected_mask,
  1480. 1000))
  1481. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1482. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1483. }
  1484. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1485. enum pipe pipe)
  1486. {
  1487. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1488. pipe);
  1489. i915_reg_t reg;
  1490. uint32_t val, pipeconf_val;
  1491. /* Make sure PCH DPLL is enabled */
  1492. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1493. /* FDI must be feeding us bits for PCH ports */
  1494. assert_fdi_tx_enabled(dev_priv, pipe);
  1495. assert_fdi_rx_enabled(dev_priv, pipe);
  1496. if (HAS_PCH_CPT(dev_priv)) {
  1497. /* Workaround: Set the timing override bit before enabling the
  1498. * pch transcoder. */
  1499. reg = TRANS_CHICKEN2(pipe);
  1500. val = I915_READ(reg);
  1501. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1502. I915_WRITE(reg, val);
  1503. }
  1504. reg = PCH_TRANSCONF(pipe);
  1505. val = I915_READ(reg);
  1506. pipeconf_val = I915_READ(PIPECONF(pipe));
  1507. if (HAS_PCH_IBX(dev_priv)) {
  1508. /*
  1509. * Make the BPC in transcoder be consistent with
  1510. * that in pipeconf reg. For HDMI we must use 8bpc
  1511. * here for both 8bpc and 12bpc.
  1512. */
  1513. val &= ~PIPECONF_BPC_MASK;
  1514. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1515. val |= PIPECONF_8BPC;
  1516. else
  1517. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1518. }
  1519. val &= ~TRANS_INTERLACE_MASK;
  1520. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1521. if (HAS_PCH_IBX(dev_priv) &&
  1522. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1523. val |= TRANS_LEGACY_INTERLACED_ILK;
  1524. else
  1525. val |= TRANS_INTERLACED;
  1526. else
  1527. val |= TRANS_PROGRESSIVE;
  1528. I915_WRITE(reg, val | TRANS_ENABLE);
  1529. if (intel_wait_for_register(dev_priv,
  1530. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1531. 100))
  1532. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1533. }
  1534. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1535. enum transcoder cpu_transcoder)
  1536. {
  1537. u32 val, pipeconf_val;
  1538. /* FDI must be feeding us bits for PCH ports */
  1539. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1540. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1541. /* Workaround: set timing override bit. */
  1542. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1543. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1544. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1545. val = TRANS_ENABLE;
  1546. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1547. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1548. PIPECONF_INTERLACED_ILK)
  1549. val |= TRANS_INTERLACED;
  1550. else
  1551. val |= TRANS_PROGRESSIVE;
  1552. I915_WRITE(LPT_TRANSCONF, val);
  1553. if (intel_wait_for_register(dev_priv,
  1554. LPT_TRANSCONF,
  1555. TRANS_STATE_ENABLE,
  1556. TRANS_STATE_ENABLE,
  1557. 100))
  1558. DRM_ERROR("Failed to enable PCH transcoder\n");
  1559. }
  1560. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1561. enum pipe pipe)
  1562. {
  1563. i915_reg_t reg;
  1564. uint32_t val;
  1565. /* FDI relies on the transcoder */
  1566. assert_fdi_tx_disabled(dev_priv, pipe);
  1567. assert_fdi_rx_disabled(dev_priv, pipe);
  1568. /* Ports must be off as well */
  1569. assert_pch_ports_disabled(dev_priv, pipe);
  1570. reg = PCH_TRANSCONF(pipe);
  1571. val = I915_READ(reg);
  1572. val &= ~TRANS_ENABLE;
  1573. I915_WRITE(reg, val);
  1574. /* wait for PCH transcoder off, transcoder state */
  1575. if (intel_wait_for_register(dev_priv,
  1576. reg, TRANS_STATE_ENABLE, 0,
  1577. 50))
  1578. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1579. if (HAS_PCH_CPT(dev_priv)) {
  1580. /* Workaround: Clear the timing override chicken bit again. */
  1581. reg = TRANS_CHICKEN2(pipe);
  1582. val = I915_READ(reg);
  1583. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1584. I915_WRITE(reg, val);
  1585. }
  1586. }
  1587. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1588. {
  1589. u32 val;
  1590. val = I915_READ(LPT_TRANSCONF);
  1591. val &= ~TRANS_ENABLE;
  1592. I915_WRITE(LPT_TRANSCONF, val);
  1593. /* wait for PCH transcoder off, transcoder state */
  1594. if (intel_wait_for_register(dev_priv,
  1595. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1596. 50))
  1597. DRM_ERROR("Failed to disable PCH transcoder\n");
  1598. /* Workaround: clear timing override bit. */
  1599. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1600. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1601. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1602. }
  1603. enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1604. {
  1605. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1606. WARN_ON(!crtc->config->has_pch_encoder);
  1607. if (HAS_PCH_LPT(dev_priv))
  1608. return TRANSCODER_A;
  1609. else
  1610. return (enum transcoder) crtc->pipe;
  1611. }
  1612. /**
  1613. * intel_enable_pipe - enable a pipe, asserting requirements
  1614. * @crtc: crtc responsible for the pipe
  1615. *
  1616. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1617. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1618. */
  1619. static void intel_enable_pipe(struct intel_crtc *crtc)
  1620. {
  1621. struct drm_device *dev = crtc->base.dev;
  1622. struct drm_i915_private *dev_priv = to_i915(dev);
  1623. enum pipe pipe = crtc->pipe;
  1624. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1625. i915_reg_t reg;
  1626. u32 val;
  1627. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1628. assert_planes_disabled(dev_priv, pipe);
  1629. assert_cursor_disabled(dev_priv, pipe);
  1630. assert_sprites_disabled(dev_priv, pipe);
  1631. /*
  1632. * A pipe without a PLL won't actually be able to drive bits from
  1633. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1634. * need the check.
  1635. */
  1636. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1637. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1638. assert_dsi_pll_enabled(dev_priv);
  1639. else
  1640. assert_pll_enabled(dev_priv, pipe);
  1641. } else {
  1642. if (crtc->config->has_pch_encoder) {
  1643. /* if driving the PCH, we need FDI enabled */
  1644. assert_fdi_rx_pll_enabled(dev_priv,
  1645. (enum pipe) intel_crtc_pch_transcoder(crtc));
  1646. assert_fdi_tx_pll_enabled(dev_priv,
  1647. (enum pipe) cpu_transcoder);
  1648. }
  1649. /* FIXME: assert CPU port conditions for SNB+ */
  1650. }
  1651. reg = PIPECONF(cpu_transcoder);
  1652. val = I915_READ(reg);
  1653. if (val & PIPECONF_ENABLE) {
  1654. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1655. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1656. return;
  1657. }
  1658. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1659. POSTING_READ(reg);
  1660. /*
  1661. * Until the pipe starts DSL will read as 0, which would cause
  1662. * an apparent vblank timestamp jump, which messes up also the
  1663. * frame count when it's derived from the timestamps. So let's
  1664. * wait for the pipe to start properly before we call
  1665. * drm_crtc_vblank_on()
  1666. */
  1667. if (dev->max_vblank_count == 0 &&
  1668. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1669. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1670. }
  1671. /**
  1672. * intel_disable_pipe - disable a pipe, asserting requirements
  1673. * @crtc: crtc whose pipes is to be disabled
  1674. *
  1675. * Disable the pipe of @crtc, making sure that various hardware
  1676. * specific requirements are met, if applicable, e.g. plane
  1677. * disabled, panel fitter off, etc.
  1678. *
  1679. * Will wait until the pipe has shut down before returning.
  1680. */
  1681. static void intel_disable_pipe(struct intel_crtc *crtc)
  1682. {
  1683. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1684. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1685. enum pipe pipe = crtc->pipe;
  1686. i915_reg_t reg;
  1687. u32 val;
  1688. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1689. /*
  1690. * Make sure planes won't keep trying to pump pixels to us,
  1691. * or we might hang the display.
  1692. */
  1693. assert_planes_disabled(dev_priv, pipe);
  1694. assert_cursor_disabled(dev_priv, pipe);
  1695. assert_sprites_disabled(dev_priv, pipe);
  1696. reg = PIPECONF(cpu_transcoder);
  1697. val = I915_READ(reg);
  1698. if ((val & PIPECONF_ENABLE) == 0)
  1699. return;
  1700. /*
  1701. * Double wide has implications for planes
  1702. * so best keep it disabled when not needed.
  1703. */
  1704. if (crtc->config->double_wide)
  1705. val &= ~PIPECONF_DOUBLE_WIDE;
  1706. /* Don't disable pipe or pipe PLLs if needed */
  1707. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1708. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1709. val &= ~PIPECONF_ENABLE;
  1710. I915_WRITE(reg, val);
  1711. if ((val & PIPECONF_ENABLE) == 0)
  1712. intel_wait_for_pipe_off(crtc);
  1713. }
  1714. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1715. {
  1716. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1717. }
  1718. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1719. uint64_t fb_modifier, unsigned int cpp)
  1720. {
  1721. switch (fb_modifier) {
  1722. case DRM_FORMAT_MOD_NONE:
  1723. return cpp;
  1724. case I915_FORMAT_MOD_X_TILED:
  1725. if (IS_GEN2(dev_priv))
  1726. return 128;
  1727. else
  1728. return 512;
  1729. case I915_FORMAT_MOD_Y_TILED:
  1730. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1731. return 128;
  1732. else
  1733. return 512;
  1734. case I915_FORMAT_MOD_Yf_TILED:
  1735. switch (cpp) {
  1736. case 1:
  1737. return 64;
  1738. case 2:
  1739. case 4:
  1740. return 128;
  1741. case 8:
  1742. case 16:
  1743. return 256;
  1744. default:
  1745. MISSING_CASE(cpp);
  1746. return cpp;
  1747. }
  1748. break;
  1749. default:
  1750. MISSING_CASE(fb_modifier);
  1751. return cpp;
  1752. }
  1753. }
  1754. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1755. uint64_t fb_modifier, unsigned int cpp)
  1756. {
  1757. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1758. return 1;
  1759. else
  1760. return intel_tile_size(dev_priv) /
  1761. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1762. }
  1763. /* Return the tile dimensions in pixel units */
  1764. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1765. unsigned int *tile_width,
  1766. unsigned int *tile_height,
  1767. uint64_t fb_modifier,
  1768. unsigned int cpp)
  1769. {
  1770. unsigned int tile_width_bytes =
  1771. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1772. *tile_width = tile_width_bytes / cpp;
  1773. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1774. }
  1775. unsigned int
  1776. intel_fb_align_height(struct drm_i915_private *dev_priv,
  1777. unsigned int height,
  1778. uint32_t pixel_format,
  1779. uint64_t fb_modifier)
  1780. {
  1781. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1782. unsigned int tile_height = intel_tile_height(dev_priv, fb_modifier, cpp);
  1783. return ALIGN(height, tile_height);
  1784. }
  1785. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1786. {
  1787. unsigned int size = 0;
  1788. int i;
  1789. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1790. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1791. return size;
  1792. }
  1793. static void
  1794. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1795. const struct drm_framebuffer *fb,
  1796. unsigned int rotation)
  1797. {
  1798. view->type = I915_GGTT_VIEW_NORMAL;
  1799. if (drm_rotation_90_or_270(rotation)) {
  1800. view->type = I915_GGTT_VIEW_ROTATED;
  1801. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1802. }
  1803. }
  1804. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1805. {
  1806. if (INTEL_INFO(dev_priv)->gen >= 9)
  1807. return 256 * 1024;
  1808. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1809. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1810. return 128 * 1024;
  1811. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1812. return 4 * 1024;
  1813. else
  1814. return 0;
  1815. }
  1816. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1817. uint64_t fb_modifier)
  1818. {
  1819. switch (fb_modifier) {
  1820. case DRM_FORMAT_MOD_NONE:
  1821. return intel_linear_alignment(dev_priv);
  1822. case I915_FORMAT_MOD_X_TILED:
  1823. if (INTEL_INFO(dev_priv)->gen >= 9)
  1824. return 256 * 1024;
  1825. return 0;
  1826. case I915_FORMAT_MOD_Y_TILED:
  1827. case I915_FORMAT_MOD_Yf_TILED:
  1828. return 1 * 1024 * 1024;
  1829. default:
  1830. MISSING_CASE(fb_modifier);
  1831. return 0;
  1832. }
  1833. }
  1834. struct i915_vma *
  1835. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1836. {
  1837. struct drm_device *dev = fb->dev;
  1838. struct drm_i915_private *dev_priv = to_i915(dev);
  1839. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1840. struct i915_ggtt_view view;
  1841. struct i915_vma *vma;
  1842. u32 alignment;
  1843. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1844. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  1845. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1846. /* Note that the w/a also requires 64 PTE of padding following the
  1847. * bo. We currently fill all unused PTE with the shadow page and so
  1848. * we should always have valid PTE following the scanout preventing
  1849. * the VT-d warning.
  1850. */
  1851. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1852. alignment = 256 * 1024;
  1853. /*
  1854. * Global gtt pte registers are special registers which actually forward
  1855. * writes to a chunk of system memory. Which means that there is no risk
  1856. * that the register values disappear as soon as we call
  1857. * intel_runtime_pm_put(), so it is correct to wrap only the
  1858. * pin/unpin/fence and not more.
  1859. */
  1860. intel_runtime_pm_get(dev_priv);
  1861. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1862. if (IS_ERR(vma))
  1863. goto err;
  1864. if (i915_vma_is_map_and_fenceable(vma)) {
  1865. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1866. * fence, whereas 965+ only requires a fence if using
  1867. * framebuffer compression. For simplicity, we always, when
  1868. * possible, install a fence as the cost is not that onerous.
  1869. *
  1870. * If we fail to fence the tiled scanout, then either the
  1871. * modeset will reject the change (which is highly unlikely as
  1872. * the affected systems, all but one, do not have unmappable
  1873. * space) or we will not be able to enable full powersaving
  1874. * techniques (also likely not to apply due to various limits
  1875. * FBC and the like impose on the size of the buffer, which
  1876. * presumably we violated anyway with this unmappable buffer).
  1877. * Anyway, it is presumably better to stumble onwards with
  1878. * something and try to run the system in a "less than optimal"
  1879. * mode that matches the user configuration.
  1880. */
  1881. if (i915_vma_get_fence(vma) == 0)
  1882. i915_vma_pin_fence(vma);
  1883. }
  1884. i915_vma_get(vma);
  1885. err:
  1886. intel_runtime_pm_put(dev_priv);
  1887. return vma;
  1888. }
  1889. void intel_unpin_fb_vma(struct i915_vma *vma)
  1890. {
  1891. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1892. i915_vma_unpin_fence(vma);
  1893. i915_gem_object_unpin_from_display_plane(vma);
  1894. i915_vma_put(vma);
  1895. }
  1896. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1897. unsigned int rotation)
  1898. {
  1899. if (drm_rotation_90_or_270(rotation))
  1900. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1901. else
  1902. return fb->pitches[plane];
  1903. }
  1904. /*
  1905. * Convert the x/y offsets into a linear offset.
  1906. * Only valid with 0/180 degree rotation, which is fine since linear
  1907. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1908. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1909. */
  1910. u32 intel_fb_xy_to_linear(int x, int y,
  1911. const struct intel_plane_state *state,
  1912. int plane)
  1913. {
  1914. const struct drm_framebuffer *fb = state->base.fb;
  1915. unsigned int cpp = fb->format->cpp[plane];
  1916. unsigned int pitch = fb->pitches[plane];
  1917. return y * pitch + x * cpp;
  1918. }
  1919. /*
  1920. * Add the x/y offsets derived from fb->offsets[] to the user
  1921. * specified plane src x/y offsets. The resulting x/y offsets
  1922. * specify the start of scanout from the beginning of the gtt mapping.
  1923. */
  1924. void intel_add_fb_offsets(int *x, int *y,
  1925. const struct intel_plane_state *state,
  1926. int plane)
  1927. {
  1928. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1929. unsigned int rotation = state->base.rotation;
  1930. if (drm_rotation_90_or_270(rotation)) {
  1931. *x += intel_fb->rotated[plane].x;
  1932. *y += intel_fb->rotated[plane].y;
  1933. } else {
  1934. *x += intel_fb->normal[plane].x;
  1935. *y += intel_fb->normal[plane].y;
  1936. }
  1937. }
  1938. /*
  1939. * Input tile dimensions and pitch must already be
  1940. * rotated to match x and y, and in pixel units.
  1941. */
  1942. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1943. unsigned int tile_width,
  1944. unsigned int tile_height,
  1945. unsigned int tile_size,
  1946. unsigned int pitch_tiles,
  1947. u32 old_offset,
  1948. u32 new_offset)
  1949. {
  1950. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1951. unsigned int tiles;
  1952. WARN_ON(old_offset & (tile_size - 1));
  1953. WARN_ON(new_offset & (tile_size - 1));
  1954. WARN_ON(new_offset > old_offset);
  1955. tiles = (old_offset - new_offset) / tile_size;
  1956. *y += tiles / pitch_tiles * tile_height;
  1957. *x += tiles % pitch_tiles * tile_width;
  1958. /* minimize x in case it got needlessly big */
  1959. *y += *x / pitch_pixels * tile_height;
  1960. *x %= pitch_pixels;
  1961. return new_offset;
  1962. }
  1963. /*
  1964. * Adjust the tile offset by moving the difference into
  1965. * the x/y offsets.
  1966. */
  1967. static u32 intel_adjust_tile_offset(int *x, int *y,
  1968. const struct intel_plane_state *state, int plane,
  1969. u32 old_offset, u32 new_offset)
  1970. {
  1971. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  1972. const struct drm_framebuffer *fb = state->base.fb;
  1973. unsigned int cpp = fb->format->cpp[plane];
  1974. unsigned int rotation = state->base.rotation;
  1975. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  1976. WARN_ON(new_offset > old_offset);
  1977. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  1978. unsigned int tile_size, tile_width, tile_height;
  1979. unsigned int pitch_tiles;
  1980. tile_size = intel_tile_size(dev_priv);
  1981. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1982. fb->modifier, cpp);
  1983. if (drm_rotation_90_or_270(rotation)) {
  1984. pitch_tiles = pitch / tile_height;
  1985. swap(tile_width, tile_height);
  1986. } else {
  1987. pitch_tiles = pitch / (tile_width * cpp);
  1988. }
  1989. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  1990. tile_size, pitch_tiles,
  1991. old_offset, new_offset);
  1992. } else {
  1993. old_offset += *y * pitch + *x * cpp;
  1994. *y = (old_offset - new_offset) / pitch;
  1995. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  1996. }
  1997. return new_offset;
  1998. }
  1999. /*
  2000. * Computes the linear offset to the base tile and adjusts
  2001. * x, y. bytes per pixel is assumed to be a power-of-two.
  2002. *
  2003. * In the 90/270 rotated case, x and y are assumed
  2004. * to be already rotated to match the rotated GTT view, and
  2005. * pitch is the tile_height aligned framebuffer height.
  2006. *
  2007. * This function is used when computing the derived information
  2008. * under intel_framebuffer, so using any of that information
  2009. * here is not allowed. Anything under drm_framebuffer can be
  2010. * used. This is why the user has to pass in the pitch since it
  2011. * is specified in the rotated orientation.
  2012. */
  2013. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2014. int *x, int *y,
  2015. const struct drm_framebuffer *fb, int plane,
  2016. unsigned int pitch,
  2017. unsigned int rotation,
  2018. u32 alignment)
  2019. {
  2020. uint64_t fb_modifier = fb->modifier;
  2021. unsigned int cpp = fb->format->cpp[plane];
  2022. u32 offset, offset_aligned;
  2023. if (alignment)
  2024. alignment--;
  2025. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2026. unsigned int tile_size, tile_width, tile_height;
  2027. unsigned int tile_rows, tiles, pitch_tiles;
  2028. tile_size = intel_tile_size(dev_priv);
  2029. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2030. fb_modifier, cpp);
  2031. if (drm_rotation_90_or_270(rotation)) {
  2032. pitch_tiles = pitch / tile_height;
  2033. swap(tile_width, tile_height);
  2034. } else {
  2035. pitch_tiles = pitch / (tile_width * cpp);
  2036. }
  2037. tile_rows = *y / tile_height;
  2038. *y %= tile_height;
  2039. tiles = *x / tile_width;
  2040. *x %= tile_width;
  2041. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2042. offset_aligned = offset & ~alignment;
  2043. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2044. tile_size, pitch_tiles,
  2045. offset, offset_aligned);
  2046. } else {
  2047. offset = *y * pitch + *x * cpp;
  2048. offset_aligned = offset & ~alignment;
  2049. *y = (offset & alignment) / pitch;
  2050. *x = ((offset & alignment) - *y * pitch) / cpp;
  2051. }
  2052. return offset_aligned;
  2053. }
  2054. u32 intel_compute_tile_offset(int *x, int *y,
  2055. const struct intel_plane_state *state,
  2056. int plane)
  2057. {
  2058. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2059. const struct drm_framebuffer *fb = state->base.fb;
  2060. unsigned int rotation = state->base.rotation;
  2061. int pitch = intel_fb_pitch(fb, plane, rotation);
  2062. u32 alignment;
  2063. /* AUX_DIST needs only 4K alignment */
  2064. if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
  2065. alignment = 4096;
  2066. else
  2067. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2068. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2069. rotation, alignment);
  2070. }
  2071. /* Convert the fb->offset[] linear offset into x/y offsets */
  2072. static void intel_fb_offset_to_xy(int *x, int *y,
  2073. const struct drm_framebuffer *fb, int plane)
  2074. {
  2075. unsigned int cpp = fb->format->cpp[plane];
  2076. unsigned int pitch = fb->pitches[plane];
  2077. u32 linear_offset = fb->offsets[plane];
  2078. *y = linear_offset / pitch;
  2079. *x = linear_offset % pitch / cpp;
  2080. }
  2081. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2082. {
  2083. switch (fb_modifier) {
  2084. case I915_FORMAT_MOD_X_TILED:
  2085. return I915_TILING_X;
  2086. case I915_FORMAT_MOD_Y_TILED:
  2087. return I915_TILING_Y;
  2088. default:
  2089. return I915_TILING_NONE;
  2090. }
  2091. }
  2092. static int
  2093. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2094. struct drm_framebuffer *fb)
  2095. {
  2096. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2097. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2098. u32 gtt_offset_rotated = 0;
  2099. unsigned int max_size = 0;
  2100. int i, num_planes = fb->format->num_planes;
  2101. unsigned int tile_size = intel_tile_size(dev_priv);
  2102. for (i = 0; i < num_planes; i++) {
  2103. unsigned int width, height;
  2104. unsigned int cpp, size;
  2105. u32 offset;
  2106. int x, y;
  2107. cpp = fb->format->cpp[i];
  2108. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2109. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2110. intel_fb_offset_to_xy(&x, &y, fb, i);
  2111. /*
  2112. * The fence (if used) is aligned to the start of the object
  2113. * so having the framebuffer wrap around across the edge of the
  2114. * fenced region doesn't really work. We have no API to configure
  2115. * the fence start offset within the object (nor could we probably
  2116. * on gen2/3). So it's just easier if we just require that the
  2117. * fb layout agrees with the fence layout. We already check that the
  2118. * fb stride matches the fence stride elsewhere.
  2119. */
  2120. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2121. (x + width) * cpp > fb->pitches[i]) {
  2122. DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
  2123. i, fb->offsets[i]);
  2124. return -EINVAL;
  2125. }
  2126. /*
  2127. * First pixel of the framebuffer from
  2128. * the start of the normal gtt mapping.
  2129. */
  2130. intel_fb->normal[i].x = x;
  2131. intel_fb->normal[i].y = y;
  2132. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2133. fb, 0, fb->pitches[i],
  2134. DRM_ROTATE_0, tile_size);
  2135. offset /= tile_size;
  2136. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  2137. unsigned int tile_width, tile_height;
  2138. unsigned int pitch_tiles;
  2139. struct drm_rect r;
  2140. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2141. fb->modifier, cpp);
  2142. rot_info->plane[i].offset = offset;
  2143. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2144. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2145. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2146. intel_fb->rotated[i].pitch =
  2147. rot_info->plane[i].height * tile_height;
  2148. /* how many tiles does this plane need */
  2149. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2150. /*
  2151. * If the plane isn't horizontally tile aligned,
  2152. * we need one more tile.
  2153. */
  2154. if (x != 0)
  2155. size++;
  2156. /* rotate the x/y offsets to match the GTT view */
  2157. r.x1 = x;
  2158. r.y1 = y;
  2159. r.x2 = x + width;
  2160. r.y2 = y + height;
  2161. drm_rect_rotate(&r,
  2162. rot_info->plane[i].width * tile_width,
  2163. rot_info->plane[i].height * tile_height,
  2164. DRM_ROTATE_270);
  2165. x = r.x1;
  2166. y = r.y1;
  2167. /* rotate the tile dimensions to match the GTT view */
  2168. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2169. swap(tile_width, tile_height);
  2170. /*
  2171. * We only keep the x/y offsets, so push all of the
  2172. * gtt offset into the x/y offsets.
  2173. */
  2174. _intel_adjust_tile_offset(&x, &y,
  2175. tile_width, tile_height,
  2176. tile_size, pitch_tiles,
  2177. gtt_offset_rotated * tile_size, 0);
  2178. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2179. /*
  2180. * First pixel of the framebuffer from
  2181. * the start of the rotated gtt mapping.
  2182. */
  2183. intel_fb->rotated[i].x = x;
  2184. intel_fb->rotated[i].y = y;
  2185. } else {
  2186. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2187. x * cpp, tile_size);
  2188. }
  2189. /* how many tiles in total needed in the bo */
  2190. max_size = max(max_size, offset + size);
  2191. }
  2192. if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
  2193. DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2194. max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
  2195. return -EINVAL;
  2196. }
  2197. return 0;
  2198. }
  2199. static int i9xx_format_to_fourcc(int format)
  2200. {
  2201. switch (format) {
  2202. case DISPPLANE_8BPP:
  2203. return DRM_FORMAT_C8;
  2204. case DISPPLANE_BGRX555:
  2205. return DRM_FORMAT_XRGB1555;
  2206. case DISPPLANE_BGRX565:
  2207. return DRM_FORMAT_RGB565;
  2208. default:
  2209. case DISPPLANE_BGRX888:
  2210. return DRM_FORMAT_XRGB8888;
  2211. case DISPPLANE_RGBX888:
  2212. return DRM_FORMAT_XBGR8888;
  2213. case DISPPLANE_BGRX101010:
  2214. return DRM_FORMAT_XRGB2101010;
  2215. case DISPPLANE_RGBX101010:
  2216. return DRM_FORMAT_XBGR2101010;
  2217. }
  2218. }
  2219. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2220. {
  2221. switch (format) {
  2222. case PLANE_CTL_FORMAT_RGB_565:
  2223. return DRM_FORMAT_RGB565;
  2224. default:
  2225. case PLANE_CTL_FORMAT_XRGB_8888:
  2226. if (rgb_order) {
  2227. if (alpha)
  2228. return DRM_FORMAT_ABGR8888;
  2229. else
  2230. return DRM_FORMAT_XBGR8888;
  2231. } else {
  2232. if (alpha)
  2233. return DRM_FORMAT_ARGB8888;
  2234. else
  2235. return DRM_FORMAT_XRGB8888;
  2236. }
  2237. case PLANE_CTL_FORMAT_XRGB_2101010:
  2238. if (rgb_order)
  2239. return DRM_FORMAT_XBGR2101010;
  2240. else
  2241. return DRM_FORMAT_XRGB2101010;
  2242. }
  2243. }
  2244. static bool
  2245. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2246. struct intel_initial_plane_config *plane_config)
  2247. {
  2248. struct drm_device *dev = crtc->base.dev;
  2249. struct drm_i915_private *dev_priv = to_i915(dev);
  2250. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2251. struct drm_i915_gem_object *obj = NULL;
  2252. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2253. struct drm_framebuffer *fb = &plane_config->fb->base;
  2254. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2255. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2256. PAGE_SIZE);
  2257. size_aligned -= base_aligned;
  2258. if (plane_config->size == 0)
  2259. return false;
  2260. /* If the FB is too big, just don't use it since fbdev is not very
  2261. * important and we should probably use that space with FBC or other
  2262. * features. */
  2263. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2264. return false;
  2265. mutex_lock(&dev->struct_mutex);
  2266. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2267. base_aligned,
  2268. base_aligned,
  2269. size_aligned);
  2270. mutex_unlock(&dev->struct_mutex);
  2271. if (!obj)
  2272. return false;
  2273. if (plane_config->tiling == I915_TILING_X)
  2274. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2275. mode_cmd.pixel_format = fb->format->format;
  2276. mode_cmd.width = fb->width;
  2277. mode_cmd.height = fb->height;
  2278. mode_cmd.pitches[0] = fb->pitches[0];
  2279. mode_cmd.modifier[0] = fb->modifier;
  2280. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2281. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2282. DRM_DEBUG_KMS("intel fb init failed\n");
  2283. goto out_unref_obj;
  2284. }
  2285. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2286. return true;
  2287. out_unref_obj:
  2288. i915_gem_object_put(obj);
  2289. return false;
  2290. }
  2291. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2292. static void
  2293. update_state_fb(struct drm_plane *plane)
  2294. {
  2295. if (plane->fb == plane->state->fb)
  2296. return;
  2297. if (plane->state->fb)
  2298. drm_framebuffer_unreference(plane->state->fb);
  2299. plane->state->fb = plane->fb;
  2300. if (plane->state->fb)
  2301. drm_framebuffer_reference(plane->state->fb);
  2302. }
  2303. static void
  2304. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2305. struct intel_initial_plane_config *plane_config)
  2306. {
  2307. struct drm_device *dev = intel_crtc->base.dev;
  2308. struct drm_i915_private *dev_priv = to_i915(dev);
  2309. struct drm_crtc *c;
  2310. struct drm_i915_gem_object *obj;
  2311. struct drm_plane *primary = intel_crtc->base.primary;
  2312. struct drm_plane_state *plane_state = primary->state;
  2313. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2314. struct intel_plane *intel_plane = to_intel_plane(primary);
  2315. struct intel_plane_state *intel_state =
  2316. to_intel_plane_state(plane_state);
  2317. struct drm_framebuffer *fb;
  2318. if (!plane_config->fb)
  2319. return;
  2320. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2321. fb = &plane_config->fb->base;
  2322. goto valid_fb;
  2323. }
  2324. kfree(plane_config->fb);
  2325. /*
  2326. * Failed to alloc the obj, check to see if we should share
  2327. * an fb with another CRTC instead
  2328. */
  2329. for_each_crtc(dev, c) {
  2330. struct intel_plane_state *state;
  2331. if (c == &intel_crtc->base)
  2332. continue;
  2333. if (!to_intel_crtc(c)->active)
  2334. continue;
  2335. state = to_intel_plane_state(c->primary->state);
  2336. if (!state->vma)
  2337. continue;
  2338. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2339. fb = c->primary->fb;
  2340. drm_framebuffer_reference(fb);
  2341. goto valid_fb;
  2342. }
  2343. }
  2344. /*
  2345. * We've failed to reconstruct the BIOS FB. Current display state
  2346. * indicates that the primary plane is visible, but has a NULL FB,
  2347. * which will lead to problems later if we don't fix it up. The
  2348. * simplest solution is to just disable the primary plane now and
  2349. * pretend the BIOS never had it enabled.
  2350. */
  2351. plane_state->visible = false;
  2352. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2353. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2354. intel_plane->disable_plane(primary, &intel_crtc->base);
  2355. return;
  2356. valid_fb:
  2357. mutex_lock(&dev->struct_mutex);
  2358. intel_state->vma =
  2359. intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  2360. mutex_unlock(&dev->struct_mutex);
  2361. if (IS_ERR(intel_state->vma)) {
  2362. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2363. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2364. intel_state->vma = NULL;
  2365. drm_framebuffer_unreference(fb);
  2366. return;
  2367. }
  2368. plane_state->src_x = 0;
  2369. plane_state->src_y = 0;
  2370. plane_state->src_w = fb->width << 16;
  2371. plane_state->src_h = fb->height << 16;
  2372. plane_state->crtc_x = 0;
  2373. plane_state->crtc_y = 0;
  2374. plane_state->crtc_w = fb->width;
  2375. plane_state->crtc_h = fb->height;
  2376. intel_state->base.src = drm_plane_state_src(plane_state);
  2377. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2378. obj = intel_fb_obj(fb);
  2379. if (i915_gem_object_is_tiled(obj))
  2380. dev_priv->preserve_bios_swizzle = true;
  2381. drm_framebuffer_reference(fb);
  2382. primary->fb = primary->state->fb = fb;
  2383. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2384. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2385. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2386. &obj->frontbuffer_bits);
  2387. }
  2388. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2389. unsigned int rotation)
  2390. {
  2391. int cpp = fb->format->cpp[plane];
  2392. switch (fb->modifier) {
  2393. case DRM_FORMAT_MOD_NONE:
  2394. case I915_FORMAT_MOD_X_TILED:
  2395. switch (cpp) {
  2396. case 8:
  2397. return 4096;
  2398. case 4:
  2399. case 2:
  2400. case 1:
  2401. return 8192;
  2402. default:
  2403. MISSING_CASE(cpp);
  2404. break;
  2405. }
  2406. break;
  2407. case I915_FORMAT_MOD_Y_TILED:
  2408. case I915_FORMAT_MOD_Yf_TILED:
  2409. switch (cpp) {
  2410. case 8:
  2411. return 2048;
  2412. case 4:
  2413. return 4096;
  2414. case 2:
  2415. case 1:
  2416. return 8192;
  2417. default:
  2418. MISSING_CASE(cpp);
  2419. break;
  2420. }
  2421. break;
  2422. default:
  2423. MISSING_CASE(fb->modifier);
  2424. }
  2425. return 2048;
  2426. }
  2427. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2428. {
  2429. const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
  2430. const struct drm_framebuffer *fb = plane_state->base.fb;
  2431. unsigned int rotation = plane_state->base.rotation;
  2432. int x = plane_state->base.src.x1 >> 16;
  2433. int y = plane_state->base.src.y1 >> 16;
  2434. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2435. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2436. int max_width = skl_max_plane_width(fb, 0, rotation);
  2437. int max_height = 4096;
  2438. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2439. if (w > max_width || h > max_height) {
  2440. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2441. w, h, max_width, max_height);
  2442. return -EINVAL;
  2443. }
  2444. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2445. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2446. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2447. /*
  2448. * AUX surface offset is specified as the distance from the
  2449. * main surface offset, and it must be non-negative. Make
  2450. * sure that is what we will get.
  2451. */
  2452. if (offset > aux_offset)
  2453. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2454. offset, aux_offset & ~(alignment - 1));
  2455. /*
  2456. * When using an X-tiled surface, the plane blows up
  2457. * if the x offset + width exceed the stride.
  2458. *
  2459. * TODO: linear and Y-tiled seem fine, Yf untested,
  2460. */
  2461. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2462. int cpp = fb->format->cpp[0];
  2463. while ((x + w) * cpp > fb->pitches[0]) {
  2464. if (offset == 0) {
  2465. DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
  2466. return -EINVAL;
  2467. }
  2468. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2469. offset, offset - alignment);
  2470. }
  2471. }
  2472. plane_state->main.offset = offset;
  2473. plane_state->main.x = x;
  2474. plane_state->main.y = y;
  2475. return 0;
  2476. }
  2477. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2478. {
  2479. const struct drm_framebuffer *fb = plane_state->base.fb;
  2480. unsigned int rotation = plane_state->base.rotation;
  2481. int max_width = skl_max_plane_width(fb, 1, rotation);
  2482. int max_height = 4096;
  2483. int x = plane_state->base.src.x1 >> 17;
  2484. int y = plane_state->base.src.y1 >> 17;
  2485. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2486. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2487. u32 offset;
  2488. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2489. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2490. /* FIXME not quite sure how/if these apply to the chroma plane */
  2491. if (w > max_width || h > max_height) {
  2492. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2493. w, h, max_width, max_height);
  2494. return -EINVAL;
  2495. }
  2496. plane_state->aux.offset = offset;
  2497. plane_state->aux.x = x;
  2498. plane_state->aux.y = y;
  2499. return 0;
  2500. }
  2501. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2502. {
  2503. const struct drm_framebuffer *fb = plane_state->base.fb;
  2504. unsigned int rotation = plane_state->base.rotation;
  2505. int ret;
  2506. if (!plane_state->base.visible)
  2507. return 0;
  2508. /* Rotate src coordinates to match rotated GTT view */
  2509. if (drm_rotation_90_or_270(rotation))
  2510. drm_rect_rotate(&plane_state->base.src,
  2511. fb->width << 16, fb->height << 16,
  2512. DRM_ROTATE_270);
  2513. /*
  2514. * Handle the AUX surface first since
  2515. * the main surface setup depends on it.
  2516. */
  2517. if (fb->format->format == DRM_FORMAT_NV12) {
  2518. ret = skl_check_nv12_aux_surface(plane_state);
  2519. if (ret)
  2520. return ret;
  2521. } else {
  2522. plane_state->aux.offset = ~0xfff;
  2523. plane_state->aux.x = 0;
  2524. plane_state->aux.y = 0;
  2525. }
  2526. ret = skl_check_main_surface(plane_state);
  2527. if (ret)
  2528. return ret;
  2529. return 0;
  2530. }
  2531. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2532. const struct intel_crtc_state *crtc_state,
  2533. const struct intel_plane_state *plane_state)
  2534. {
  2535. struct drm_i915_private *dev_priv = to_i915(primary->dev);
  2536. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2537. struct drm_framebuffer *fb = plane_state->base.fb;
  2538. int plane = intel_crtc->plane;
  2539. u32 linear_offset;
  2540. u32 dspcntr;
  2541. i915_reg_t reg = DSPCNTR(plane);
  2542. unsigned int rotation = plane_state->base.rotation;
  2543. int x = plane_state->base.src.x1 >> 16;
  2544. int y = plane_state->base.src.y1 >> 16;
  2545. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2546. dspcntr |= DISPLAY_PLANE_ENABLE;
  2547. if (INTEL_GEN(dev_priv) < 4) {
  2548. if (intel_crtc->pipe == PIPE_B)
  2549. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2550. /* pipesrc and dspsize control the size that is scaled from,
  2551. * which should always be the user's requested size.
  2552. */
  2553. I915_WRITE(DSPSIZE(plane),
  2554. ((crtc_state->pipe_src_h - 1) << 16) |
  2555. (crtc_state->pipe_src_w - 1));
  2556. I915_WRITE(DSPPOS(plane), 0);
  2557. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2558. I915_WRITE(PRIMSIZE(plane),
  2559. ((crtc_state->pipe_src_h - 1) << 16) |
  2560. (crtc_state->pipe_src_w - 1));
  2561. I915_WRITE(PRIMPOS(plane), 0);
  2562. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2563. }
  2564. switch (fb->format->format) {
  2565. case DRM_FORMAT_C8:
  2566. dspcntr |= DISPPLANE_8BPP;
  2567. break;
  2568. case DRM_FORMAT_XRGB1555:
  2569. dspcntr |= DISPPLANE_BGRX555;
  2570. break;
  2571. case DRM_FORMAT_RGB565:
  2572. dspcntr |= DISPPLANE_BGRX565;
  2573. break;
  2574. case DRM_FORMAT_XRGB8888:
  2575. dspcntr |= DISPPLANE_BGRX888;
  2576. break;
  2577. case DRM_FORMAT_XBGR8888:
  2578. dspcntr |= DISPPLANE_RGBX888;
  2579. break;
  2580. case DRM_FORMAT_XRGB2101010:
  2581. dspcntr |= DISPPLANE_BGRX101010;
  2582. break;
  2583. case DRM_FORMAT_XBGR2101010:
  2584. dspcntr |= DISPPLANE_RGBX101010;
  2585. break;
  2586. default:
  2587. BUG();
  2588. }
  2589. if (INTEL_GEN(dev_priv) >= 4 &&
  2590. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2591. dspcntr |= DISPPLANE_TILED;
  2592. if (rotation & DRM_ROTATE_180)
  2593. dspcntr |= DISPPLANE_ROTATE_180;
  2594. if (rotation & DRM_REFLECT_X)
  2595. dspcntr |= DISPPLANE_MIRROR;
  2596. if (IS_G4X(dev_priv))
  2597. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2598. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2599. if (INTEL_GEN(dev_priv) >= 4)
  2600. intel_crtc->dspaddr_offset =
  2601. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2602. if (rotation & DRM_ROTATE_180) {
  2603. x += crtc_state->pipe_src_w - 1;
  2604. y += crtc_state->pipe_src_h - 1;
  2605. } else if (rotation & DRM_REFLECT_X) {
  2606. x += crtc_state->pipe_src_w - 1;
  2607. }
  2608. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2609. if (INTEL_GEN(dev_priv) < 4)
  2610. intel_crtc->dspaddr_offset = linear_offset;
  2611. intel_crtc->adjusted_x = x;
  2612. intel_crtc->adjusted_y = y;
  2613. I915_WRITE(reg, dspcntr);
  2614. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2615. if (INTEL_GEN(dev_priv) >= 4) {
  2616. I915_WRITE(DSPSURF(plane),
  2617. intel_plane_ggtt_offset(plane_state) +
  2618. intel_crtc->dspaddr_offset);
  2619. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2620. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2621. } else {
  2622. I915_WRITE(DSPADDR(plane),
  2623. intel_plane_ggtt_offset(plane_state) +
  2624. intel_crtc->dspaddr_offset);
  2625. }
  2626. POSTING_READ(reg);
  2627. }
  2628. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2629. struct drm_crtc *crtc)
  2630. {
  2631. struct drm_device *dev = crtc->dev;
  2632. struct drm_i915_private *dev_priv = to_i915(dev);
  2633. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2634. int plane = intel_crtc->plane;
  2635. I915_WRITE(DSPCNTR(plane), 0);
  2636. if (INTEL_INFO(dev_priv)->gen >= 4)
  2637. I915_WRITE(DSPSURF(plane), 0);
  2638. else
  2639. I915_WRITE(DSPADDR(plane), 0);
  2640. POSTING_READ(DSPCNTR(plane));
  2641. }
  2642. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2643. const struct intel_crtc_state *crtc_state,
  2644. const struct intel_plane_state *plane_state)
  2645. {
  2646. struct drm_device *dev = primary->dev;
  2647. struct drm_i915_private *dev_priv = to_i915(dev);
  2648. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2649. struct drm_framebuffer *fb = plane_state->base.fb;
  2650. int plane = intel_crtc->plane;
  2651. u32 linear_offset;
  2652. u32 dspcntr;
  2653. i915_reg_t reg = DSPCNTR(plane);
  2654. unsigned int rotation = plane_state->base.rotation;
  2655. int x = plane_state->base.src.x1 >> 16;
  2656. int y = plane_state->base.src.y1 >> 16;
  2657. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2658. dspcntr |= DISPLAY_PLANE_ENABLE;
  2659. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2660. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2661. switch (fb->format->format) {
  2662. case DRM_FORMAT_C8:
  2663. dspcntr |= DISPPLANE_8BPP;
  2664. break;
  2665. case DRM_FORMAT_RGB565:
  2666. dspcntr |= DISPPLANE_BGRX565;
  2667. break;
  2668. case DRM_FORMAT_XRGB8888:
  2669. dspcntr |= DISPPLANE_BGRX888;
  2670. break;
  2671. case DRM_FORMAT_XBGR8888:
  2672. dspcntr |= DISPPLANE_RGBX888;
  2673. break;
  2674. case DRM_FORMAT_XRGB2101010:
  2675. dspcntr |= DISPPLANE_BGRX101010;
  2676. break;
  2677. case DRM_FORMAT_XBGR2101010:
  2678. dspcntr |= DISPPLANE_RGBX101010;
  2679. break;
  2680. default:
  2681. BUG();
  2682. }
  2683. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  2684. dspcntr |= DISPPLANE_TILED;
  2685. if (rotation & DRM_ROTATE_180)
  2686. dspcntr |= DISPPLANE_ROTATE_180;
  2687. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
  2688. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2689. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2690. intel_crtc->dspaddr_offset =
  2691. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2692. /* HSW+ does this automagically in hardware */
  2693. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
  2694. rotation & DRM_ROTATE_180) {
  2695. x += crtc_state->pipe_src_w - 1;
  2696. y += crtc_state->pipe_src_h - 1;
  2697. }
  2698. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2699. intel_crtc->adjusted_x = x;
  2700. intel_crtc->adjusted_y = y;
  2701. I915_WRITE(reg, dspcntr);
  2702. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2703. I915_WRITE(DSPSURF(plane),
  2704. intel_plane_ggtt_offset(plane_state) +
  2705. intel_crtc->dspaddr_offset);
  2706. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2707. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2708. } else {
  2709. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2710. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2711. }
  2712. POSTING_READ(reg);
  2713. }
  2714. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2715. uint64_t fb_modifier, uint32_t pixel_format)
  2716. {
  2717. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2718. return 64;
  2719. } else {
  2720. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2721. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2722. }
  2723. }
  2724. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2725. {
  2726. struct drm_device *dev = intel_crtc->base.dev;
  2727. struct drm_i915_private *dev_priv = to_i915(dev);
  2728. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2729. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2730. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2731. }
  2732. /*
  2733. * This function detaches (aka. unbinds) unused scalers in hardware
  2734. */
  2735. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2736. {
  2737. struct intel_crtc_scaler_state *scaler_state;
  2738. int i;
  2739. scaler_state = &intel_crtc->config->scaler_state;
  2740. /* loop through and disable scalers that aren't in use */
  2741. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2742. if (!scaler_state->scalers[i].in_use)
  2743. skl_detach_scaler(intel_crtc, i);
  2744. }
  2745. }
  2746. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2747. unsigned int rotation)
  2748. {
  2749. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2750. u32 stride = intel_fb_pitch(fb, plane, rotation);
  2751. /*
  2752. * The stride is either expressed as a multiple of 64 bytes chunks for
  2753. * linear buffers or in number of tiles for tiled buffers.
  2754. */
  2755. if (drm_rotation_90_or_270(rotation)) {
  2756. int cpp = fb->format->cpp[plane];
  2757. stride /= intel_tile_height(dev_priv, fb->modifier, cpp);
  2758. } else {
  2759. stride /= intel_fb_stride_alignment(dev_priv, fb->modifier,
  2760. fb->format->format);
  2761. }
  2762. return stride;
  2763. }
  2764. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2765. {
  2766. switch (pixel_format) {
  2767. case DRM_FORMAT_C8:
  2768. return PLANE_CTL_FORMAT_INDEXED;
  2769. case DRM_FORMAT_RGB565:
  2770. return PLANE_CTL_FORMAT_RGB_565;
  2771. case DRM_FORMAT_XBGR8888:
  2772. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2773. case DRM_FORMAT_XRGB8888:
  2774. return PLANE_CTL_FORMAT_XRGB_8888;
  2775. /*
  2776. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2777. * to be already pre-multiplied. We need to add a knob (or a different
  2778. * DRM_FORMAT) for user-space to configure that.
  2779. */
  2780. case DRM_FORMAT_ABGR8888:
  2781. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2782. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2783. case DRM_FORMAT_ARGB8888:
  2784. return PLANE_CTL_FORMAT_XRGB_8888 |
  2785. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2786. case DRM_FORMAT_XRGB2101010:
  2787. return PLANE_CTL_FORMAT_XRGB_2101010;
  2788. case DRM_FORMAT_XBGR2101010:
  2789. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2790. case DRM_FORMAT_YUYV:
  2791. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2792. case DRM_FORMAT_YVYU:
  2793. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2794. case DRM_FORMAT_UYVY:
  2795. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2796. case DRM_FORMAT_VYUY:
  2797. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2798. default:
  2799. MISSING_CASE(pixel_format);
  2800. }
  2801. return 0;
  2802. }
  2803. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2804. {
  2805. switch (fb_modifier) {
  2806. case DRM_FORMAT_MOD_NONE:
  2807. break;
  2808. case I915_FORMAT_MOD_X_TILED:
  2809. return PLANE_CTL_TILED_X;
  2810. case I915_FORMAT_MOD_Y_TILED:
  2811. return PLANE_CTL_TILED_Y;
  2812. case I915_FORMAT_MOD_Yf_TILED:
  2813. return PLANE_CTL_TILED_YF;
  2814. default:
  2815. MISSING_CASE(fb_modifier);
  2816. }
  2817. return 0;
  2818. }
  2819. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2820. {
  2821. switch (rotation) {
  2822. case DRM_ROTATE_0:
  2823. break;
  2824. /*
  2825. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2826. * while i915 HW rotation is clockwise, thats why this swapping.
  2827. */
  2828. case DRM_ROTATE_90:
  2829. return PLANE_CTL_ROTATE_270;
  2830. case DRM_ROTATE_180:
  2831. return PLANE_CTL_ROTATE_180;
  2832. case DRM_ROTATE_270:
  2833. return PLANE_CTL_ROTATE_90;
  2834. default:
  2835. MISSING_CASE(rotation);
  2836. }
  2837. return 0;
  2838. }
  2839. static void skylake_update_primary_plane(struct drm_plane *plane,
  2840. const struct intel_crtc_state *crtc_state,
  2841. const struct intel_plane_state *plane_state)
  2842. {
  2843. struct drm_device *dev = plane->dev;
  2844. struct drm_i915_private *dev_priv = to_i915(dev);
  2845. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2846. struct drm_framebuffer *fb = plane_state->base.fb;
  2847. enum plane_id plane_id = to_intel_plane(plane)->id;
  2848. enum pipe pipe = to_intel_plane(plane)->pipe;
  2849. u32 plane_ctl;
  2850. unsigned int rotation = plane_state->base.rotation;
  2851. u32 stride = skl_plane_stride(fb, 0, rotation);
  2852. u32 surf_addr = plane_state->main.offset;
  2853. int scaler_id = plane_state->scaler_id;
  2854. int src_x = plane_state->main.x;
  2855. int src_y = plane_state->main.y;
  2856. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2857. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2858. int dst_x = plane_state->base.dst.x1;
  2859. int dst_y = plane_state->base.dst.y1;
  2860. int dst_w = drm_rect_width(&plane_state->base.dst);
  2861. int dst_h = drm_rect_height(&plane_state->base.dst);
  2862. plane_ctl = PLANE_CTL_ENABLE;
  2863. if (IS_GEMINILAKE(dev_priv)) {
  2864. I915_WRITE(PLANE_COLOR_CTL(pipe, plane_id),
  2865. PLANE_COLOR_PIPE_GAMMA_ENABLE |
  2866. PLANE_COLOR_PLANE_GAMMA_DISABLE);
  2867. } else {
  2868. plane_ctl |=
  2869. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2870. PLANE_CTL_PIPE_CSC_ENABLE |
  2871. PLANE_CTL_PLANE_GAMMA_DISABLE;
  2872. }
  2873. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  2874. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  2875. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2876. /* Sizes are 0 based */
  2877. src_w--;
  2878. src_h--;
  2879. dst_w--;
  2880. dst_h--;
  2881. intel_crtc->dspaddr_offset = surf_addr;
  2882. intel_crtc->adjusted_x = src_x;
  2883. intel_crtc->adjusted_y = src_y;
  2884. I915_WRITE(PLANE_CTL(pipe, plane_id), plane_ctl);
  2885. I915_WRITE(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
  2886. I915_WRITE(PLANE_STRIDE(pipe, plane_id), stride);
  2887. I915_WRITE(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  2888. if (scaler_id >= 0) {
  2889. uint32_t ps_ctrl = 0;
  2890. WARN_ON(!dst_w || !dst_h);
  2891. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
  2892. crtc_state->scaler_state.scalers[scaler_id].mode;
  2893. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2894. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2895. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2896. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2897. I915_WRITE(PLANE_POS(pipe, plane_id), 0);
  2898. } else {
  2899. I915_WRITE(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
  2900. }
  2901. I915_WRITE(PLANE_SURF(pipe, plane_id),
  2902. intel_plane_ggtt_offset(plane_state) + surf_addr);
  2903. POSTING_READ(PLANE_SURF(pipe, plane_id));
  2904. }
  2905. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2906. struct drm_crtc *crtc)
  2907. {
  2908. struct drm_device *dev = crtc->dev;
  2909. struct drm_i915_private *dev_priv = to_i915(dev);
  2910. enum plane_id plane_id = to_intel_plane(primary)->id;
  2911. enum pipe pipe = to_intel_plane(primary)->pipe;
  2912. I915_WRITE(PLANE_CTL(pipe, plane_id), 0);
  2913. I915_WRITE(PLANE_SURF(pipe, plane_id), 0);
  2914. POSTING_READ(PLANE_SURF(pipe, plane_id));
  2915. }
  2916. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2917. static int
  2918. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2919. int x, int y, enum mode_set_atomic state)
  2920. {
  2921. /* Support for kgdboc is disabled, this needs a major rework. */
  2922. DRM_ERROR("legacy panic handler not supported any more.\n");
  2923. return -ENODEV;
  2924. }
  2925. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2926. {
  2927. struct intel_crtc *crtc;
  2928. for_each_intel_crtc(&dev_priv->drm, crtc)
  2929. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2930. }
  2931. static void intel_update_primary_planes(struct drm_device *dev)
  2932. {
  2933. struct drm_crtc *crtc;
  2934. for_each_crtc(dev, crtc) {
  2935. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2936. struct intel_plane_state *plane_state =
  2937. to_intel_plane_state(plane->base.state);
  2938. if (plane_state->base.visible)
  2939. plane->update_plane(&plane->base,
  2940. to_intel_crtc_state(crtc->state),
  2941. plane_state);
  2942. }
  2943. }
  2944. static int
  2945. __intel_display_resume(struct drm_device *dev,
  2946. struct drm_atomic_state *state)
  2947. {
  2948. struct drm_crtc_state *crtc_state;
  2949. struct drm_crtc *crtc;
  2950. int i, ret;
  2951. intel_modeset_setup_hw_state(dev);
  2952. i915_redisable_vga(to_i915(dev));
  2953. if (!state)
  2954. return 0;
  2955. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  2956. /*
  2957. * Force recalculation even if we restore
  2958. * current state. With fast modeset this may not result
  2959. * in a modeset when the state is compatible.
  2960. */
  2961. crtc_state->mode_changed = true;
  2962. }
  2963. /* ignore any reset values/BIOS leftovers in the WM registers */
  2964. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  2965. ret = drm_atomic_commit(state);
  2966. WARN_ON(ret == -EDEADLK);
  2967. return ret;
  2968. }
  2969. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  2970. {
  2971. return intel_has_gpu_reset(dev_priv) &&
  2972. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  2973. }
  2974. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  2975. {
  2976. struct drm_device *dev = &dev_priv->drm;
  2977. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  2978. struct drm_atomic_state *state;
  2979. int ret;
  2980. /*
  2981. * Need mode_config.mutex so that we don't
  2982. * trample ongoing ->detect() and whatnot.
  2983. */
  2984. mutex_lock(&dev->mode_config.mutex);
  2985. drm_modeset_acquire_init(ctx, 0);
  2986. while (1) {
  2987. ret = drm_modeset_lock_all_ctx(dev, ctx);
  2988. if (ret != -EDEADLK)
  2989. break;
  2990. drm_modeset_backoff(ctx);
  2991. }
  2992. /* reset doesn't touch the display, but flips might get nuked anyway, */
  2993. if (!i915.force_reset_modeset_test &&
  2994. !gpu_reset_clobbers_display(dev_priv))
  2995. return;
  2996. /*
  2997. * Disabling the crtcs gracefully seems nicer. Also the
  2998. * g33 docs say we should at least disable all the planes.
  2999. */
  3000. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3001. if (IS_ERR(state)) {
  3002. ret = PTR_ERR(state);
  3003. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3004. return;
  3005. }
  3006. ret = drm_atomic_helper_disable_all(dev, ctx);
  3007. if (ret) {
  3008. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3009. drm_atomic_state_put(state);
  3010. return;
  3011. }
  3012. dev_priv->modeset_restore_state = state;
  3013. state->acquire_ctx = ctx;
  3014. }
  3015. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3016. {
  3017. struct drm_device *dev = &dev_priv->drm;
  3018. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3019. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3020. int ret;
  3021. /*
  3022. * Flips in the rings will be nuked by the reset,
  3023. * so complete all pending flips so that user space
  3024. * will get its events and not get stuck.
  3025. */
  3026. intel_complete_page_flips(dev_priv);
  3027. dev_priv->modeset_restore_state = NULL;
  3028. /* reset doesn't touch the display */
  3029. if (!gpu_reset_clobbers_display(dev_priv)) {
  3030. if (!state) {
  3031. /*
  3032. * Flips in the rings have been nuked by the reset,
  3033. * so update the base address of all primary
  3034. * planes to the the last fb to make sure we're
  3035. * showing the correct fb after a reset.
  3036. *
  3037. * FIXME: Atomic will make this obsolete since we won't schedule
  3038. * CS-based flips (which might get lost in gpu resets) any more.
  3039. */
  3040. intel_update_primary_planes(dev);
  3041. } else {
  3042. ret = __intel_display_resume(dev, state);
  3043. if (ret)
  3044. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3045. }
  3046. } else {
  3047. /*
  3048. * The display has been reset as well,
  3049. * so need a full re-initialization.
  3050. */
  3051. intel_runtime_pm_disable_interrupts(dev_priv);
  3052. intel_runtime_pm_enable_interrupts(dev_priv);
  3053. intel_pps_unlock_regs_wa(dev_priv);
  3054. intel_modeset_init_hw(dev);
  3055. spin_lock_irq(&dev_priv->irq_lock);
  3056. if (dev_priv->display.hpd_irq_setup)
  3057. dev_priv->display.hpd_irq_setup(dev_priv);
  3058. spin_unlock_irq(&dev_priv->irq_lock);
  3059. ret = __intel_display_resume(dev, state);
  3060. if (ret)
  3061. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3062. intel_hpd_init(dev_priv);
  3063. }
  3064. if (state)
  3065. drm_atomic_state_put(state);
  3066. drm_modeset_drop_locks(ctx);
  3067. drm_modeset_acquire_fini(ctx);
  3068. mutex_unlock(&dev->mode_config.mutex);
  3069. }
  3070. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3071. {
  3072. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3073. if (i915_reset_in_progress(error))
  3074. return true;
  3075. if (crtc->reset_count != i915_reset_count(error))
  3076. return true;
  3077. return false;
  3078. }
  3079. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3080. {
  3081. struct drm_device *dev = crtc->dev;
  3082. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3083. bool pending;
  3084. if (abort_flip_on_reset(intel_crtc))
  3085. return false;
  3086. spin_lock_irq(&dev->event_lock);
  3087. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3088. spin_unlock_irq(&dev->event_lock);
  3089. return pending;
  3090. }
  3091. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3092. struct intel_crtc_state *old_crtc_state)
  3093. {
  3094. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3095. struct intel_crtc_state *pipe_config =
  3096. to_intel_crtc_state(crtc->base.state);
  3097. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3098. crtc->base.mode = crtc->base.state->mode;
  3099. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  3100. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  3101. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  3102. /*
  3103. * Update pipe size and adjust fitter if needed: the reason for this is
  3104. * that in compute_mode_changes we check the native mode (not the pfit
  3105. * mode) to see if we can flip rather than do a full mode set. In the
  3106. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3107. * pfit state, we'll end up with a big fb scanned out into the wrong
  3108. * sized surface.
  3109. */
  3110. I915_WRITE(PIPESRC(crtc->pipe),
  3111. ((pipe_config->pipe_src_w - 1) << 16) |
  3112. (pipe_config->pipe_src_h - 1));
  3113. /* on skylake this is done by detaching scalers */
  3114. if (INTEL_GEN(dev_priv) >= 9) {
  3115. skl_detach_scalers(crtc);
  3116. if (pipe_config->pch_pfit.enabled)
  3117. skylake_pfit_enable(crtc);
  3118. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3119. if (pipe_config->pch_pfit.enabled)
  3120. ironlake_pfit_enable(crtc);
  3121. else if (old_crtc_state->pch_pfit.enabled)
  3122. ironlake_pfit_disable(crtc, true);
  3123. }
  3124. }
  3125. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  3126. {
  3127. struct drm_device *dev = crtc->dev;
  3128. struct drm_i915_private *dev_priv = to_i915(dev);
  3129. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3130. int pipe = intel_crtc->pipe;
  3131. i915_reg_t reg;
  3132. u32 temp;
  3133. /* enable normal train */
  3134. reg = FDI_TX_CTL(pipe);
  3135. temp = I915_READ(reg);
  3136. if (IS_IVYBRIDGE(dev_priv)) {
  3137. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3138. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3139. } else {
  3140. temp &= ~FDI_LINK_TRAIN_NONE;
  3141. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3142. }
  3143. I915_WRITE(reg, temp);
  3144. reg = FDI_RX_CTL(pipe);
  3145. temp = I915_READ(reg);
  3146. if (HAS_PCH_CPT(dev_priv)) {
  3147. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3148. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3149. } else {
  3150. temp &= ~FDI_LINK_TRAIN_NONE;
  3151. temp |= FDI_LINK_TRAIN_NONE;
  3152. }
  3153. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3154. /* wait one idle pattern time */
  3155. POSTING_READ(reg);
  3156. udelay(1000);
  3157. /* IVB wants error correction enabled */
  3158. if (IS_IVYBRIDGE(dev_priv))
  3159. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3160. FDI_FE_ERRC_ENABLE);
  3161. }
  3162. /* The FDI link training functions for ILK/Ibexpeak. */
  3163. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  3164. {
  3165. struct drm_device *dev = crtc->dev;
  3166. struct drm_i915_private *dev_priv = to_i915(dev);
  3167. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3168. int pipe = intel_crtc->pipe;
  3169. i915_reg_t reg;
  3170. u32 temp, tries;
  3171. /* FDI needs bits from pipe first */
  3172. assert_pipe_enabled(dev_priv, pipe);
  3173. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3174. for train result */
  3175. reg = FDI_RX_IMR(pipe);
  3176. temp = I915_READ(reg);
  3177. temp &= ~FDI_RX_SYMBOL_LOCK;
  3178. temp &= ~FDI_RX_BIT_LOCK;
  3179. I915_WRITE(reg, temp);
  3180. I915_READ(reg);
  3181. udelay(150);
  3182. /* enable CPU FDI TX and PCH FDI RX */
  3183. reg = FDI_TX_CTL(pipe);
  3184. temp = I915_READ(reg);
  3185. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3186. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3187. temp &= ~FDI_LINK_TRAIN_NONE;
  3188. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3189. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3190. reg = FDI_RX_CTL(pipe);
  3191. temp = I915_READ(reg);
  3192. temp &= ~FDI_LINK_TRAIN_NONE;
  3193. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3194. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3195. POSTING_READ(reg);
  3196. udelay(150);
  3197. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3198. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3199. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3200. FDI_RX_PHASE_SYNC_POINTER_EN);
  3201. reg = FDI_RX_IIR(pipe);
  3202. for (tries = 0; tries < 5; tries++) {
  3203. temp = I915_READ(reg);
  3204. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3205. if ((temp & FDI_RX_BIT_LOCK)) {
  3206. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3207. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3208. break;
  3209. }
  3210. }
  3211. if (tries == 5)
  3212. DRM_ERROR("FDI train 1 fail!\n");
  3213. /* Train 2 */
  3214. reg = FDI_TX_CTL(pipe);
  3215. temp = I915_READ(reg);
  3216. temp &= ~FDI_LINK_TRAIN_NONE;
  3217. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3218. I915_WRITE(reg, temp);
  3219. reg = FDI_RX_CTL(pipe);
  3220. temp = I915_READ(reg);
  3221. temp &= ~FDI_LINK_TRAIN_NONE;
  3222. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3223. I915_WRITE(reg, temp);
  3224. POSTING_READ(reg);
  3225. udelay(150);
  3226. reg = FDI_RX_IIR(pipe);
  3227. for (tries = 0; tries < 5; tries++) {
  3228. temp = I915_READ(reg);
  3229. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3230. if (temp & FDI_RX_SYMBOL_LOCK) {
  3231. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3232. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3233. break;
  3234. }
  3235. }
  3236. if (tries == 5)
  3237. DRM_ERROR("FDI train 2 fail!\n");
  3238. DRM_DEBUG_KMS("FDI train done\n");
  3239. }
  3240. static const int snb_b_fdi_train_param[] = {
  3241. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3242. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3243. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3244. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3245. };
  3246. /* The FDI link training functions for SNB/Cougarpoint. */
  3247. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3248. {
  3249. struct drm_device *dev = crtc->dev;
  3250. struct drm_i915_private *dev_priv = to_i915(dev);
  3251. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3252. int pipe = intel_crtc->pipe;
  3253. i915_reg_t reg;
  3254. u32 temp, i, retry;
  3255. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3256. for train result */
  3257. reg = FDI_RX_IMR(pipe);
  3258. temp = I915_READ(reg);
  3259. temp &= ~FDI_RX_SYMBOL_LOCK;
  3260. temp &= ~FDI_RX_BIT_LOCK;
  3261. I915_WRITE(reg, temp);
  3262. POSTING_READ(reg);
  3263. udelay(150);
  3264. /* enable CPU FDI TX and PCH FDI RX */
  3265. reg = FDI_TX_CTL(pipe);
  3266. temp = I915_READ(reg);
  3267. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3268. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3269. temp &= ~FDI_LINK_TRAIN_NONE;
  3270. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3271. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3272. /* SNB-B */
  3273. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3274. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3275. I915_WRITE(FDI_RX_MISC(pipe),
  3276. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3277. reg = FDI_RX_CTL(pipe);
  3278. temp = I915_READ(reg);
  3279. if (HAS_PCH_CPT(dev_priv)) {
  3280. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3281. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3282. } else {
  3283. temp &= ~FDI_LINK_TRAIN_NONE;
  3284. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3285. }
  3286. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3287. POSTING_READ(reg);
  3288. udelay(150);
  3289. for (i = 0; i < 4; i++) {
  3290. reg = FDI_TX_CTL(pipe);
  3291. temp = I915_READ(reg);
  3292. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3293. temp |= snb_b_fdi_train_param[i];
  3294. I915_WRITE(reg, temp);
  3295. POSTING_READ(reg);
  3296. udelay(500);
  3297. for (retry = 0; retry < 5; retry++) {
  3298. reg = FDI_RX_IIR(pipe);
  3299. temp = I915_READ(reg);
  3300. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3301. if (temp & FDI_RX_BIT_LOCK) {
  3302. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3303. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3304. break;
  3305. }
  3306. udelay(50);
  3307. }
  3308. if (retry < 5)
  3309. break;
  3310. }
  3311. if (i == 4)
  3312. DRM_ERROR("FDI train 1 fail!\n");
  3313. /* Train 2 */
  3314. reg = FDI_TX_CTL(pipe);
  3315. temp = I915_READ(reg);
  3316. temp &= ~FDI_LINK_TRAIN_NONE;
  3317. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3318. if (IS_GEN6(dev_priv)) {
  3319. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3320. /* SNB-B */
  3321. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3322. }
  3323. I915_WRITE(reg, temp);
  3324. reg = FDI_RX_CTL(pipe);
  3325. temp = I915_READ(reg);
  3326. if (HAS_PCH_CPT(dev_priv)) {
  3327. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3328. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3329. } else {
  3330. temp &= ~FDI_LINK_TRAIN_NONE;
  3331. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3332. }
  3333. I915_WRITE(reg, temp);
  3334. POSTING_READ(reg);
  3335. udelay(150);
  3336. for (i = 0; i < 4; i++) {
  3337. reg = FDI_TX_CTL(pipe);
  3338. temp = I915_READ(reg);
  3339. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3340. temp |= snb_b_fdi_train_param[i];
  3341. I915_WRITE(reg, temp);
  3342. POSTING_READ(reg);
  3343. udelay(500);
  3344. for (retry = 0; retry < 5; retry++) {
  3345. reg = FDI_RX_IIR(pipe);
  3346. temp = I915_READ(reg);
  3347. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3348. if (temp & FDI_RX_SYMBOL_LOCK) {
  3349. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3350. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3351. break;
  3352. }
  3353. udelay(50);
  3354. }
  3355. if (retry < 5)
  3356. break;
  3357. }
  3358. if (i == 4)
  3359. DRM_ERROR("FDI train 2 fail!\n");
  3360. DRM_DEBUG_KMS("FDI train done.\n");
  3361. }
  3362. /* Manual link training for Ivy Bridge A0 parts */
  3363. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3364. {
  3365. struct drm_device *dev = crtc->dev;
  3366. struct drm_i915_private *dev_priv = to_i915(dev);
  3367. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3368. int pipe = intel_crtc->pipe;
  3369. i915_reg_t reg;
  3370. u32 temp, i, j;
  3371. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3372. for train result */
  3373. reg = FDI_RX_IMR(pipe);
  3374. temp = I915_READ(reg);
  3375. temp &= ~FDI_RX_SYMBOL_LOCK;
  3376. temp &= ~FDI_RX_BIT_LOCK;
  3377. I915_WRITE(reg, temp);
  3378. POSTING_READ(reg);
  3379. udelay(150);
  3380. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3381. I915_READ(FDI_RX_IIR(pipe)));
  3382. /* Try each vswing and preemphasis setting twice before moving on */
  3383. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3384. /* disable first in case we need to retry */
  3385. reg = FDI_TX_CTL(pipe);
  3386. temp = I915_READ(reg);
  3387. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3388. temp &= ~FDI_TX_ENABLE;
  3389. I915_WRITE(reg, temp);
  3390. reg = FDI_RX_CTL(pipe);
  3391. temp = I915_READ(reg);
  3392. temp &= ~FDI_LINK_TRAIN_AUTO;
  3393. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3394. temp &= ~FDI_RX_ENABLE;
  3395. I915_WRITE(reg, temp);
  3396. /* enable CPU FDI TX and PCH FDI RX */
  3397. reg = FDI_TX_CTL(pipe);
  3398. temp = I915_READ(reg);
  3399. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3400. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3401. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3402. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3403. temp |= snb_b_fdi_train_param[j/2];
  3404. temp |= FDI_COMPOSITE_SYNC;
  3405. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3406. I915_WRITE(FDI_RX_MISC(pipe),
  3407. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3408. reg = FDI_RX_CTL(pipe);
  3409. temp = I915_READ(reg);
  3410. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3411. temp |= FDI_COMPOSITE_SYNC;
  3412. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3413. POSTING_READ(reg);
  3414. udelay(1); /* should be 0.5us */
  3415. for (i = 0; i < 4; i++) {
  3416. reg = FDI_RX_IIR(pipe);
  3417. temp = I915_READ(reg);
  3418. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3419. if (temp & FDI_RX_BIT_LOCK ||
  3420. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3421. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3422. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3423. i);
  3424. break;
  3425. }
  3426. udelay(1); /* should be 0.5us */
  3427. }
  3428. if (i == 4) {
  3429. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3430. continue;
  3431. }
  3432. /* Train 2 */
  3433. reg = FDI_TX_CTL(pipe);
  3434. temp = I915_READ(reg);
  3435. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3436. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3437. I915_WRITE(reg, temp);
  3438. reg = FDI_RX_CTL(pipe);
  3439. temp = I915_READ(reg);
  3440. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3441. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3442. I915_WRITE(reg, temp);
  3443. POSTING_READ(reg);
  3444. udelay(2); /* should be 1.5us */
  3445. for (i = 0; i < 4; i++) {
  3446. reg = FDI_RX_IIR(pipe);
  3447. temp = I915_READ(reg);
  3448. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3449. if (temp & FDI_RX_SYMBOL_LOCK ||
  3450. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3451. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3452. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3453. i);
  3454. goto train_done;
  3455. }
  3456. udelay(2); /* should be 1.5us */
  3457. }
  3458. if (i == 4)
  3459. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3460. }
  3461. train_done:
  3462. DRM_DEBUG_KMS("FDI train done.\n");
  3463. }
  3464. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3465. {
  3466. struct drm_device *dev = intel_crtc->base.dev;
  3467. struct drm_i915_private *dev_priv = to_i915(dev);
  3468. int pipe = intel_crtc->pipe;
  3469. i915_reg_t reg;
  3470. u32 temp;
  3471. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3472. reg = FDI_RX_CTL(pipe);
  3473. temp = I915_READ(reg);
  3474. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3475. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3476. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3477. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3478. POSTING_READ(reg);
  3479. udelay(200);
  3480. /* Switch from Rawclk to PCDclk */
  3481. temp = I915_READ(reg);
  3482. I915_WRITE(reg, temp | FDI_PCDCLK);
  3483. POSTING_READ(reg);
  3484. udelay(200);
  3485. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3486. reg = FDI_TX_CTL(pipe);
  3487. temp = I915_READ(reg);
  3488. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3489. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3490. POSTING_READ(reg);
  3491. udelay(100);
  3492. }
  3493. }
  3494. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3495. {
  3496. struct drm_device *dev = intel_crtc->base.dev;
  3497. struct drm_i915_private *dev_priv = to_i915(dev);
  3498. int pipe = intel_crtc->pipe;
  3499. i915_reg_t reg;
  3500. u32 temp;
  3501. /* Switch from PCDclk to Rawclk */
  3502. reg = FDI_RX_CTL(pipe);
  3503. temp = I915_READ(reg);
  3504. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3505. /* Disable CPU FDI TX PLL */
  3506. reg = FDI_TX_CTL(pipe);
  3507. temp = I915_READ(reg);
  3508. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3509. POSTING_READ(reg);
  3510. udelay(100);
  3511. reg = FDI_RX_CTL(pipe);
  3512. temp = I915_READ(reg);
  3513. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3514. /* Wait for the clocks to turn off. */
  3515. POSTING_READ(reg);
  3516. udelay(100);
  3517. }
  3518. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3519. {
  3520. struct drm_device *dev = crtc->dev;
  3521. struct drm_i915_private *dev_priv = to_i915(dev);
  3522. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3523. int pipe = intel_crtc->pipe;
  3524. i915_reg_t reg;
  3525. u32 temp;
  3526. /* disable CPU FDI tx and PCH FDI rx */
  3527. reg = FDI_TX_CTL(pipe);
  3528. temp = I915_READ(reg);
  3529. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3530. POSTING_READ(reg);
  3531. reg = FDI_RX_CTL(pipe);
  3532. temp = I915_READ(reg);
  3533. temp &= ~(0x7 << 16);
  3534. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3535. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3536. POSTING_READ(reg);
  3537. udelay(100);
  3538. /* Ironlake workaround, disable clock pointer after downing FDI */
  3539. if (HAS_PCH_IBX(dev_priv))
  3540. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3541. /* still set train pattern 1 */
  3542. reg = FDI_TX_CTL(pipe);
  3543. temp = I915_READ(reg);
  3544. temp &= ~FDI_LINK_TRAIN_NONE;
  3545. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3546. I915_WRITE(reg, temp);
  3547. reg = FDI_RX_CTL(pipe);
  3548. temp = I915_READ(reg);
  3549. if (HAS_PCH_CPT(dev_priv)) {
  3550. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3551. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3552. } else {
  3553. temp &= ~FDI_LINK_TRAIN_NONE;
  3554. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3555. }
  3556. /* BPC in FDI rx is consistent with that in PIPECONF */
  3557. temp &= ~(0x07 << 16);
  3558. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3559. I915_WRITE(reg, temp);
  3560. POSTING_READ(reg);
  3561. udelay(100);
  3562. }
  3563. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3564. {
  3565. struct intel_crtc *crtc;
  3566. /* Note that we don't need to be called with mode_config.lock here
  3567. * as our list of CRTC objects is static for the lifetime of the
  3568. * device and so cannot disappear as we iterate. Similarly, we can
  3569. * happily treat the predicates as racy, atomic checks as userspace
  3570. * cannot claim and pin a new fb without at least acquring the
  3571. * struct_mutex and so serialising with us.
  3572. */
  3573. for_each_intel_crtc(&dev_priv->drm, crtc) {
  3574. if (atomic_read(&crtc->unpin_work_count) == 0)
  3575. continue;
  3576. if (crtc->flip_work)
  3577. intel_wait_for_vblank(dev_priv, crtc->pipe);
  3578. return true;
  3579. }
  3580. return false;
  3581. }
  3582. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3583. {
  3584. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3585. struct intel_flip_work *work = intel_crtc->flip_work;
  3586. intel_crtc->flip_work = NULL;
  3587. if (work->event)
  3588. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3589. drm_crtc_vblank_put(&intel_crtc->base);
  3590. wake_up_all(&dev_priv->pending_flip_queue);
  3591. trace_i915_flip_complete(intel_crtc->plane,
  3592. work->pending_flip_obj);
  3593. queue_work(dev_priv->wq, &work->unpin_work);
  3594. }
  3595. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3596. {
  3597. struct drm_device *dev = crtc->dev;
  3598. struct drm_i915_private *dev_priv = to_i915(dev);
  3599. long ret;
  3600. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3601. ret = wait_event_interruptible_timeout(
  3602. dev_priv->pending_flip_queue,
  3603. !intel_crtc_has_pending_flip(crtc),
  3604. 60*HZ);
  3605. if (ret < 0)
  3606. return ret;
  3607. if (ret == 0) {
  3608. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3609. struct intel_flip_work *work;
  3610. spin_lock_irq(&dev->event_lock);
  3611. work = intel_crtc->flip_work;
  3612. if (work && !is_mmio_work(work)) {
  3613. WARN_ONCE(1, "Removing stuck page flip\n");
  3614. page_flip_completed(intel_crtc);
  3615. }
  3616. spin_unlock_irq(&dev->event_lock);
  3617. }
  3618. return 0;
  3619. }
  3620. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3621. {
  3622. u32 temp;
  3623. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3624. mutex_lock(&dev_priv->sb_lock);
  3625. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3626. temp |= SBI_SSCCTL_DISABLE;
  3627. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3628. mutex_unlock(&dev_priv->sb_lock);
  3629. }
  3630. /* Program iCLKIP clock to the desired frequency */
  3631. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3632. {
  3633. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3634. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3635. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3636. u32 temp;
  3637. lpt_disable_iclkip(dev_priv);
  3638. /* The iCLK virtual clock root frequency is in MHz,
  3639. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3640. * divisors, it is necessary to divide one by another, so we
  3641. * convert the virtual clock precision to KHz here for higher
  3642. * precision.
  3643. */
  3644. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3645. u32 iclk_virtual_root_freq = 172800 * 1000;
  3646. u32 iclk_pi_range = 64;
  3647. u32 desired_divisor;
  3648. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3649. clock << auxdiv);
  3650. divsel = (desired_divisor / iclk_pi_range) - 2;
  3651. phaseinc = desired_divisor % iclk_pi_range;
  3652. /*
  3653. * Near 20MHz is a corner case which is
  3654. * out of range for the 7-bit divisor
  3655. */
  3656. if (divsel <= 0x7f)
  3657. break;
  3658. }
  3659. /* This should not happen with any sane values */
  3660. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3661. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3662. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3663. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3664. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3665. clock,
  3666. auxdiv,
  3667. divsel,
  3668. phasedir,
  3669. phaseinc);
  3670. mutex_lock(&dev_priv->sb_lock);
  3671. /* Program SSCDIVINTPHASE6 */
  3672. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3673. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3674. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3675. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3676. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3677. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3678. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3679. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3680. /* Program SSCAUXDIV */
  3681. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3682. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3683. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3684. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3685. /* Enable modulator and associated divider */
  3686. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3687. temp &= ~SBI_SSCCTL_DISABLE;
  3688. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3689. mutex_unlock(&dev_priv->sb_lock);
  3690. /* Wait for initialization time */
  3691. udelay(24);
  3692. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3693. }
  3694. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3695. {
  3696. u32 divsel, phaseinc, auxdiv;
  3697. u32 iclk_virtual_root_freq = 172800 * 1000;
  3698. u32 iclk_pi_range = 64;
  3699. u32 desired_divisor;
  3700. u32 temp;
  3701. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3702. return 0;
  3703. mutex_lock(&dev_priv->sb_lock);
  3704. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3705. if (temp & SBI_SSCCTL_DISABLE) {
  3706. mutex_unlock(&dev_priv->sb_lock);
  3707. return 0;
  3708. }
  3709. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3710. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3711. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3712. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3713. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3714. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3715. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3716. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3717. mutex_unlock(&dev_priv->sb_lock);
  3718. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3719. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3720. desired_divisor << auxdiv);
  3721. }
  3722. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3723. enum pipe pch_transcoder)
  3724. {
  3725. struct drm_device *dev = crtc->base.dev;
  3726. struct drm_i915_private *dev_priv = to_i915(dev);
  3727. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3728. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3729. I915_READ(HTOTAL(cpu_transcoder)));
  3730. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3731. I915_READ(HBLANK(cpu_transcoder)));
  3732. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3733. I915_READ(HSYNC(cpu_transcoder)));
  3734. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3735. I915_READ(VTOTAL(cpu_transcoder)));
  3736. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3737. I915_READ(VBLANK(cpu_transcoder)));
  3738. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3739. I915_READ(VSYNC(cpu_transcoder)));
  3740. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3741. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3742. }
  3743. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3744. {
  3745. struct drm_i915_private *dev_priv = to_i915(dev);
  3746. uint32_t temp;
  3747. temp = I915_READ(SOUTH_CHICKEN1);
  3748. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3749. return;
  3750. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3751. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3752. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3753. if (enable)
  3754. temp |= FDI_BC_BIFURCATION_SELECT;
  3755. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3756. I915_WRITE(SOUTH_CHICKEN1, temp);
  3757. POSTING_READ(SOUTH_CHICKEN1);
  3758. }
  3759. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3760. {
  3761. struct drm_device *dev = intel_crtc->base.dev;
  3762. switch (intel_crtc->pipe) {
  3763. case PIPE_A:
  3764. break;
  3765. case PIPE_B:
  3766. if (intel_crtc->config->fdi_lanes > 2)
  3767. cpt_set_fdi_bc_bifurcation(dev, false);
  3768. else
  3769. cpt_set_fdi_bc_bifurcation(dev, true);
  3770. break;
  3771. case PIPE_C:
  3772. cpt_set_fdi_bc_bifurcation(dev, true);
  3773. break;
  3774. default:
  3775. BUG();
  3776. }
  3777. }
  3778. /* Return which DP Port should be selected for Transcoder DP control */
  3779. static enum port
  3780. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3781. {
  3782. struct drm_device *dev = crtc->dev;
  3783. struct intel_encoder *encoder;
  3784. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3785. if (encoder->type == INTEL_OUTPUT_DP ||
  3786. encoder->type == INTEL_OUTPUT_EDP)
  3787. return enc_to_dig_port(&encoder->base)->port;
  3788. }
  3789. return -1;
  3790. }
  3791. /*
  3792. * Enable PCH resources required for PCH ports:
  3793. * - PCH PLLs
  3794. * - FDI training & RX/TX
  3795. * - update transcoder timings
  3796. * - DP transcoding bits
  3797. * - transcoder
  3798. */
  3799. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3800. {
  3801. struct drm_device *dev = crtc->dev;
  3802. struct drm_i915_private *dev_priv = to_i915(dev);
  3803. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3804. int pipe = intel_crtc->pipe;
  3805. u32 temp;
  3806. assert_pch_transcoder_disabled(dev_priv, pipe);
  3807. if (IS_IVYBRIDGE(dev_priv))
  3808. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3809. /* Write the TU size bits before fdi link training, so that error
  3810. * detection works. */
  3811. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3812. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3813. /* For PCH output, training FDI link */
  3814. dev_priv->display.fdi_link_train(crtc);
  3815. /* We need to program the right clock selection before writing the pixel
  3816. * mutliplier into the DPLL. */
  3817. if (HAS_PCH_CPT(dev_priv)) {
  3818. u32 sel;
  3819. temp = I915_READ(PCH_DPLL_SEL);
  3820. temp |= TRANS_DPLL_ENABLE(pipe);
  3821. sel = TRANS_DPLLB_SEL(pipe);
  3822. if (intel_crtc->config->shared_dpll ==
  3823. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3824. temp |= sel;
  3825. else
  3826. temp &= ~sel;
  3827. I915_WRITE(PCH_DPLL_SEL, temp);
  3828. }
  3829. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3830. * transcoder, and we actually should do this to not upset any PCH
  3831. * transcoder that already use the clock when we share it.
  3832. *
  3833. * Note that enable_shared_dpll tries to do the right thing, but
  3834. * get_shared_dpll unconditionally resets the pll - we need that to have
  3835. * the right LVDS enable sequence. */
  3836. intel_enable_shared_dpll(intel_crtc);
  3837. /* set transcoder timing, panel must allow it */
  3838. assert_panel_unlocked(dev_priv, pipe);
  3839. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3840. intel_fdi_normal_train(crtc);
  3841. /* For PCH DP, enable TRANS_DP_CTL */
  3842. if (HAS_PCH_CPT(dev_priv) &&
  3843. intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3844. const struct drm_display_mode *adjusted_mode =
  3845. &intel_crtc->config->base.adjusted_mode;
  3846. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3847. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3848. temp = I915_READ(reg);
  3849. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3850. TRANS_DP_SYNC_MASK |
  3851. TRANS_DP_BPC_MASK);
  3852. temp |= TRANS_DP_OUTPUT_ENABLE;
  3853. temp |= bpc << 9; /* same format but at 11:9 */
  3854. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3855. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3856. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3857. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3858. switch (intel_trans_dp_port_sel(crtc)) {
  3859. case PORT_B:
  3860. temp |= TRANS_DP_PORT_SEL_B;
  3861. break;
  3862. case PORT_C:
  3863. temp |= TRANS_DP_PORT_SEL_C;
  3864. break;
  3865. case PORT_D:
  3866. temp |= TRANS_DP_PORT_SEL_D;
  3867. break;
  3868. default:
  3869. BUG();
  3870. }
  3871. I915_WRITE(reg, temp);
  3872. }
  3873. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3874. }
  3875. static void lpt_pch_enable(struct drm_crtc *crtc)
  3876. {
  3877. struct drm_device *dev = crtc->dev;
  3878. struct drm_i915_private *dev_priv = to_i915(dev);
  3879. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3880. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3881. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3882. lpt_program_iclkip(crtc);
  3883. /* Set transcoder timing. */
  3884. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3885. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3886. }
  3887. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3888. {
  3889. struct drm_i915_private *dev_priv = to_i915(dev);
  3890. i915_reg_t dslreg = PIPEDSL(pipe);
  3891. u32 temp;
  3892. temp = I915_READ(dslreg);
  3893. udelay(500);
  3894. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3895. if (wait_for(I915_READ(dslreg) != temp, 5))
  3896. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3897. }
  3898. }
  3899. static int
  3900. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3901. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3902. int src_w, int src_h, int dst_w, int dst_h)
  3903. {
  3904. struct intel_crtc_scaler_state *scaler_state =
  3905. &crtc_state->scaler_state;
  3906. struct intel_crtc *intel_crtc =
  3907. to_intel_crtc(crtc_state->base.crtc);
  3908. int need_scaling;
  3909. need_scaling = drm_rotation_90_or_270(rotation) ?
  3910. (src_h != dst_w || src_w != dst_h):
  3911. (src_w != dst_w || src_h != dst_h);
  3912. /*
  3913. * if plane is being disabled or scaler is no more required or force detach
  3914. * - free scaler binded to this plane/crtc
  3915. * - in order to do this, update crtc->scaler_usage
  3916. *
  3917. * Here scaler state in crtc_state is set free so that
  3918. * scaler can be assigned to other user. Actual register
  3919. * update to free the scaler is done in plane/panel-fit programming.
  3920. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3921. */
  3922. if (force_detach || !need_scaling) {
  3923. if (*scaler_id >= 0) {
  3924. scaler_state->scaler_users &= ~(1 << scaler_user);
  3925. scaler_state->scalers[*scaler_id].in_use = 0;
  3926. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3927. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3928. intel_crtc->pipe, scaler_user, *scaler_id,
  3929. scaler_state->scaler_users);
  3930. *scaler_id = -1;
  3931. }
  3932. return 0;
  3933. }
  3934. /* range checks */
  3935. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3936. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3937. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3938. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3939. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3940. "size is out of scaler range\n",
  3941. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3942. return -EINVAL;
  3943. }
  3944. /* mark this plane as a scaler user in crtc_state */
  3945. scaler_state->scaler_users |= (1 << scaler_user);
  3946. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3947. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3948. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3949. scaler_state->scaler_users);
  3950. return 0;
  3951. }
  3952. /**
  3953. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3954. *
  3955. * @state: crtc's scaler state
  3956. *
  3957. * Return
  3958. * 0 - scaler_usage updated successfully
  3959. * error - requested scaling cannot be supported or other error condition
  3960. */
  3961. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3962. {
  3963. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3964. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3965. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3966. state->pipe_src_w, state->pipe_src_h,
  3967. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3968. }
  3969. /**
  3970. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3971. *
  3972. * @state: crtc's scaler state
  3973. * @plane_state: atomic plane state to update
  3974. *
  3975. * Return
  3976. * 0 - scaler_usage updated successfully
  3977. * error - requested scaling cannot be supported or other error condition
  3978. */
  3979. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3980. struct intel_plane_state *plane_state)
  3981. {
  3982. struct intel_plane *intel_plane =
  3983. to_intel_plane(plane_state->base.plane);
  3984. struct drm_framebuffer *fb = plane_state->base.fb;
  3985. int ret;
  3986. bool force_detach = !fb || !plane_state->base.visible;
  3987. ret = skl_update_scaler(crtc_state, force_detach,
  3988. drm_plane_index(&intel_plane->base),
  3989. &plane_state->scaler_id,
  3990. plane_state->base.rotation,
  3991. drm_rect_width(&plane_state->base.src) >> 16,
  3992. drm_rect_height(&plane_state->base.src) >> 16,
  3993. drm_rect_width(&plane_state->base.dst),
  3994. drm_rect_height(&plane_state->base.dst));
  3995. if (ret || plane_state->scaler_id < 0)
  3996. return ret;
  3997. /* check colorkey */
  3998. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3999. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4000. intel_plane->base.base.id,
  4001. intel_plane->base.name);
  4002. return -EINVAL;
  4003. }
  4004. /* Check src format */
  4005. switch (fb->format->format) {
  4006. case DRM_FORMAT_RGB565:
  4007. case DRM_FORMAT_XBGR8888:
  4008. case DRM_FORMAT_XRGB8888:
  4009. case DRM_FORMAT_ABGR8888:
  4010. case DRM_FORMAT_ARGB8888:
  4011. case DRM_FORMAT_XRGB2101010:
  4012. case DRM_FORMAT_XBGR2101010:
  4013. case DRM_FORMAT_YUYV:
  4014. case DRM_FORMAT_YVYU:
  4015. case DRM_FORMAT_UYVY:
  4016. case DRM_FORMAT_VYUY:
  4017. break;
  4018. default:
  4019. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4020. intel_plane->base.base.id, intel_plane->base.name,
  4021. fb->base.id, fb->format->format);
  4022. return -EINVAL;
  4023. }
  4024. return 0;
  4025. }
  4026. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4027. {
  4028. int i;
  4029. for (i = 0; i < crtc->num_scalers; i++)
  4030. skl_detach_scaler(crtc, i);
  4031. }
  4032. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4033. {
  4034. struct drm_device *dev = crtc->base.dev;
  4035. struct drm_i915_private *dev_priv = to_i915(dev);
  4036. int pipe = crtc->pipe;
  4037. struct intel_crtc_scaler_state *scaler_state =
  4038. &crtc->config->scaler_state;
  4039. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  4040. if (crtc->config->pch_pfit.enabled) {
  4041. int id;
  4042. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  4043. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  4044. return;
  4045. }
  4046. id = scaler_state->scaler_id;
  4047. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4048. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4049. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4050. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4051. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  4052. }
  4053. }
  4054. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4055. {
  4056. struct drm_device *dev = crtc->base.dev;
  4057. struct drm_i915_private *dev_priv = to_i915(dev);
  4058. int pipe = crtc->pipe;
  4059. if (crtc->config->pch_pfit.enabled) {
  4060. /* Force use of hard-coded filter coefficients
  4061. * as some pre-programmed values are broken,
  4062. * e.g. x201.
  4063. */
  4064. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4065. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4066. PF_PIPE_SEL_IVB(pipe));
  4067. else
  4068. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4069. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4070. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4071. }
  4072. }
  4073. void hsw_enable_ips(struct intel_crtc *crtc)
  4074. {
  4075. struct drm_device *dev = crtc->base.dev;
  4076. struct drm_i915_private *dev_priv = to_i915(dev);
  4077. if (!crtc->config->ips_enabled)
  4078. return;
  4079. /*
  4080. * We can only enable IPS after we enable a plane and wait for a vblank
  4081. * This function is called from post_plane_update, which is run after
  4082. * a vblank wait.
  4083. */
  4084. assert_plane_enabled(dev_priv, crtc->plane);
  4085. if (IS_BROADWELL(dev_priv)) {
  4086. mutex_lock(&dev_priv->rps.hw_lock);
  4087. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4088. mutex_unlock(&dev_priv->rps.hw_lock);
  4089. /* Quoting Art Runyan: "its not safe to expect any particular
  4090. * value in IPS_CTL bit 31 after enabling IPS through the
  4091. * mailbox." Moreover, the mailbox may return a bogus state,
  4092. * so we need to just enable it and continue on.
  4093. */
  4094. } else {
  4095. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4096. /* The bit only becomes 1 in the next vblank, so this wait here
  4097. * is essentially intel_wait_for_vblank. If we don't have this
  4098. * and don't wait for vblanks until the end of crtc_enable, then
  4099. * the HW state readout code will complain that the expected
  4100. * IPS_CTL value is not the one we read. */
  4101. if (intel_wait_for_register(dev_priv,
  4102. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4103. 50))
  4104. DRM_ERROR("Timed out waiting for IPS enable\n");
  4105. }
  4106. }
  4107. void hsw_disable_ips(struct intel_crtc *crtc)
  4108. {
  4109. struct drm_device *dev = crtc->base.dev;
  4110. struct drm_i915_private *dev_priv = to_i915(dev);
  4111. if (!crtc->config->ips_enabled)
  4112. return;
  4113. assert_plane_enabled(dev_priv, crtc->plane);
  4114. if (IS_BROADWELL(dev_priv)) {
  4115. mutex_lock(&dev_priv->rps.hw_lock);
  4116. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4117. mutex_unlock(&dev_priv->rps.hw_lock);
  4118. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4119. if (intel_wait_for_register(dev_priv,
  4120. IPS_CTL, IPS_ENABLE, 0,
  4121. 42))
  4122. DRM_ERROR("Timed out waiting for IPS disable\n");
  4123. } else {
  4124. I915_WRITE(IPS_CTL, 0);
  4125. POSTING_READ(IPS_CTL);
  4126. }
  4127. /* We need to wait for a vblank before we can disable the plane. */
  4128. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4129. }
  4130. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4131. {
  4132. if (intel_crtc->overlay) {
  4133. struct drm_device *dev = intel_crtc->base.dev;
  4134. struct drm_i915_private *dev_priv = to_i915(dev);
  4135. mutex_lock(&dev->struct_mutex);
  4136. dev_priv->mm.interruptible = false;
  4137. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4138. dev_priv->mm.interruptible = true;
  4139. mutex_unlock(&dev->struct_mutex);
  4140. }
  4141. /* Let userspace switch the overlay on again. In most cases userspace
  4142. * has to recompute where to put it anyway.
  4143. */
  4144. }
  4145. /**
  4146. * intel_post_enable_primary - Perform operations after enabling primary plane
  4147. * @crtc: the CRTC whose primary plane was just enabled
  4148. *
  4149. * Performs potentially sleeping operations that must be done after the primary
  4150. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4151. * called due to an explicit primary plane update, or due to an implicit
  4152. * re-enable that is caused when a sprite plane is updated to no longer
  4153. * completely hide the primary plane.
  4154. */
  4155. static void
  4156. intel_post_enable_primary(struct drm_crtc *crtc)
  4157. {
  4158. struct drm_device *dev = crtc->dev;
  4159. struct drm_i915_private *dev_priv = to_i915(dev);
  4160. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4161. int pipe = intel_crtc->pipe;
  4162. /*
  4163. * FIXME IPS should be fine as long as one plane is
  4164. * enabled, but in practice it seems to have problems
  4165. * when going from primary only to sprite only and vice
  4166. * versa.
  4167. */
  4168. hsw_enable_ips(intel_crtc);
  4169. /*
  4170. * Gen2 reports pipe underruns whenever all planes are disabled.
  4171. * So don't enable underrun reporting before at least some planes
  4172. * are enabled.
  4173. * FIXME: Need to fix the logic to work when we turn off all planes
  4174. * but leave the pipe running.
  4175. */
  4176. if (IS_GEN2(dev_priv))
  4177. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4178. /* Underruns don't always raise interrupts, so check manually. */
  4179. intel_check_cpu_fifo_underruns(dev_priv);
  4180. intel_check_pch_fifo_underruns(dev_priv);
  4181. }
  4182. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4183. static void
  4184. intel_pre_disable_primary(struct drm_crtc *crtc)
  4185. {
  4186. struct drm_device *dev = crtc->dev;
  4187. struct drm_i915_private *dev_priv = to_i915(dev);
  4188. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4189. int pipe = intel_crtc->pipe;
  4190. /*
  4191. * Gen2 reports pipe underruns whenever all planes are disabled.
  4192. * So diasble underrun reporting before all the planes get disabled.
  4193. * FIXME: Need to fix the logic to work when we turn off all planes
  4194. * but leave the pipe running.
  4195. */
  4196. if (IS_GEN2(dev_priv))
  4197. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4198. /*
  4199. * FIXME IPS should be fine as long as one plane is
  4200. * enabled, but in practice it seems to have problems
  4201. * when going from primary only to sprite only and vice
  4202. * versa.
  4203. */
  4204. hsw_disable_ips(intel_crtc);
  4205. }
  4206. /* FIXME get rid of this and use pre_plane_update */
  4207. static void
  4208. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4209. {
  4210. struct drm_device *dev = crtc->dev;
  4211. struct drm_i915_private *dev_priv = to_i915(dev);
  4212. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4213. int pipe = intel_crtc->pipe;
  4214. intel_pre_disable_primary(crtc);
  4215. /*
  4216. * Vblank time updates from the shadow to live plane control register
  4217. * are blocked if the memory self-refresh mode is active at that
  4218. * moment. So to make sure the plane gets truly disabled, disable
  4219. * first the self-refresh mode. The self-refresh enable bit in turn
  4220. * will be checked/applied by the HW only at the next frame start
  4221. * event which is after the vblank start event, so we need to have a
  4222. * wait-for-vblank between disabling the plane and the pipe.
  4223. */
  4224. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4225. intel_set_memory_cxsr(dev_priv, false))
  4226. intel_wait_for_vblank(dev_priv, pipe);
  4227. }
  4228. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4229. {
  4230. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4231. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4232. struct intel_crtc_state *pipe_config =
  4233. to_intel_crtc_state(crtc->base.state);
  4234. struct drm_plane *primary = crtc->base.primary;
  4235. struct drm_plane_state *old_pri_state =
  4236. drm_atomic_get_existing_plane_state(old_state, primary);
  4237. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4238. crtc->wm.cxsr_allowed = true;
  4239. if (pipe_config->update_wm_post && pipe_config->base.active)
  4240. intel_update_watermarks(crtc);
  4241. if (old_pri_state) {
  4242. struct intel_plane_state *primary_state =
  4243. to_intel_plane_state(primary->state);
  4244. struct intel_plane_state *old_primary_state =
  4245. to_intel_plane_state(old_pri_state);
  4246. intel_fbc_post_update(crtc);
  4247. if (primary_state->base.visible &&
  4248. (needs_modeset(&pipe_config->base) ||
  4249. !old_primary_state->base.visible))
  4250. intel_post_enable_primary(&crtc->base);
  4251. }
  4252. }
  4253. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  4254. {
  4255. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4256. struct drm_device *dev = crtc->base.dev;
  4257. struct drm_i915_private *dev_priv = to_i915(dev);
  4258. struct intel_crtc_state *pipe_config =
  4259. to_intel_crtc_state(crtc->base.state);
  4260. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4261. struct drm_plane *primary = crtc->base.primary;
  4262. struct drm_plane_state *old_pri_state =
  4263. drm_atomic_get_existing_plane_state(old_state, primary);
  4264. bool modeset = needs_modeset(&pipe_config->base);
  4265. struct intel_atomic_state *old_intel_state =
  4266. to_intel_atomic_state(old_state);
  4267. if (old_pri_state) {
  4268. struct intel_plane_state *primary_state =
  4269. to_intel_plane_state(primary->state);
  4270. struct intel_plane_state *old_primary_state =
  4271. to_intel_plane_state(old_pri_state);
  4272. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4273. if (old_primary_state->base.visible &&
  4274. (modeset || !primary_state->base.visible))
  4275. intel_pre_disable_primary(&crtc->base);
  4276. }
  4277. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
  4278. crtc->wm.cxsr_allowed = false;
  4279. /*
  4280. * Vblank time updates from the shadow to live plane control register
  4281. * are blocked if the memory self-refresh mode is active at that
  4282. * moment. So to make sure the plane gets truly disabled, disable
  4283. * first the self-refresh mode. The self-refresh enable bit in turn
  4284. * will be checked/applied by the HW only at the next frame start
  4285. * event which is after the vblank start event, so we need to have a
  4286. * wait-for-vblank between disabling the plane and the pipe.
  4287. */
  4288. if (old_crtc_state->base.active &&
  4289. intel_set_memory_cxsr(dev_priv, false))
  4290. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4291. }
  4292. /*
  4293. * IVB workaround: must disable low power watermarks for at least
  4294. * one frame before enabling scaling. LP watermarks can be re-enabled
  4295. * when scaling is disabled.
  4296. *
  4297. * WaCxSRDisabledForSpriteScaling:ivb
  4298. */
  4299. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4300. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4301. /*
  4302. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4303. * watermark programming here.
  4304. */
  4305. if (needs_modeset(&pipe_config->base))
  4306. return;
  4307. /*
  4308. * For platforms that support atomic watermarks, program the
  4309. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4310. * will be the intermediate values that are safe for both pre- and
  4311. * post- vblank; when vblank happens, the 'active' values will be set
  4312. * to the final 'target' values and we'll do this again to get the
  4313. * optimal watermarks. For gen9+ platforms, the values we program here
  4314. * will be the final target values which will get automatically latched
  4315. * at vblank time; no further programming will be necessary.
  4316. *
  4317. * If a platform hasn't been transitioned to atomic watermarks yet,
  4318. * we'll continue to update watermarks the old way, if flags tell
  4319. * us to.
  4320. */
  4321. if (dev_priv->display.initial_watermarks != NULL)
  4322. dev_priv->display.initial_watermarks(old_intel_state,
  4323. pipe_config);
  4324. else if (pipe_config->update_wm_pre)
  4325. intel_update_watermarks(crtc);
  4326. }
  4327. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4328. {
  4329. struct drm_device *dev = crtc->dev;
  4330. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4331. struct drm_plane *p;
  4332. int pipe = intel_crtc->pipe;
  4333. intel_crtc_dpms_overlay_disable(intel_crtc);
  4334. drm_for_each_plane_mask(p, dev, plane_mask)
  4335. to_intel_plane(p)->disable_plane(p, crtc);
  4336. /*
  4337. * FIXME: Once we grow proper nuclear flip support out of this we need
  4338. * to compute the mask of flip planes precisely. For the time being
  4339. * consider this a flip to a NULL plane.
  4340. */
  4341. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4342. }
  4343. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4344. struct intel_crtc_state *crtc_state,
  4345. struct drm_atomic_state *old_state)
  4346. {
  4347. struct drm_connector_state *old_conn_state;
  4348. struct drm_connector *conn;
  4349. int i;
  4350. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4351. struct drm_connector_state *conn_state = conn->state;
  4352. struct intel_encoder *encoder =
  4353. to_intel_encoder(conn_state->best_encoder);
  4354. if (conn_state->crtc != crtc)
  4355. continue;
  4356. if (encoder->pre_pll_enable)
  4357. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4358. }
  4359. }
  4360. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4361. struct intel_crtc_state *crtc_state,
  4362. struct drm_atomic_state *old_state)
  4363. {
  4364. struct drm_connector_state *old_conn_state;
  4365. struct drm_connector *conn;
  4366. int i;
  4367. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4368. struct drm_connector_state *conn_state = conn->state;
  4369. struct intel_encoder *encoder =
  4370. to_intel_encoder(conn_state->best_encoder);
  4371. if (conn_state->crtc != crtc)
  4372. continue;
  4373. if (encoder->pre_enable)
  4374. encoder->pre_enable(encoder, crtc_state, conn_state);
  4375. }
  4376. }
  4377. static void intel_encoders_enable(struct drm_crtc *crtc,
  4378. struct intel_crtc_state *crtc_state,
  4379. struct drm_atomic_state *old_state)
  4380. {
  4381. struct drm_connector_state *old_conn_state;
  4382. struct drm_connector *conn;
  4383. int i;
  4384. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4385. struct drm_connector_state *conn_state = conn->state;
  4386. struct intel_encoder *encoder =
  4387. to_intel_encoder(conn_state->best_encoder);
  4388. if (conn_state->crtc != crtc)
  4389. continue;
  4390. encoder->enable(encoder, crtc_state, conn_state);
  4391. intel_opregion_notify_encoder(encoder, true);
  4392. }
  4393. }
  4394. static void intel_encoders_disable(struct drm_crtc *crtc,
  4395. struct intel_crtc_state *old_crtc_state,
  4396. struct drm_atomic_state *old_state)
  4397. {
  4398. struct drm_connector_state *old_conn_state;
  4399. struct drm_connector *conn;
  4400. int i;
  4401. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4402. struct intel_encoder *encoder =
  4403. to_intel_encoder(old_conn_state->best_encoder);
  4404. if (old_conn_state->crtc != crtc)
  4405. continue;
  4406. intel_opregion_notify_encoder(encoder, false);
  4407. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4408. }
  4409. }
  4410. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4411. struct intel_crtc_state *old_crtc_state,
  4412. struct drm_atomic_state *old_state)
  4413. {
  4414. struct drm_connector_state *old_conn_state;
  4415. struct drm_connector *conn;
  4416. int i;
  4417. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4418. struct intel_encoder *encoder =
  4419. to_intel_encoder(old_conn_state->best_encoder);
  4420. if (old_conn_state->crtc != crtc)
  4421. continue;
  4422. if (encoder->post_disable)
  4423. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4424. }
  4425. }
  4426. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4427. struct intel_crtc_state *old_crtc_state,
  4428. struct drm_atomic_state *old_state)
  4429. {
  4430. struct drm_connector_state *old_conn_state;
  4431. struct drm_connector *conn;
  4432. int i;
  4433. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4434. struct intel_encoder *encoder =
  4435. to_intel_encoder(old_conn_state->best_encoder);
  4436. if (old_conn_state->crtc != crtc)
  4437. continue;
  4438. if (encoder->post_pll_disable)
  4439. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4440. }
  4441. }
  4442. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4443. struct drm_atomic_state *old_state)
  4444. {
  4445. struct drm_crtc *crtc = pipe_config->base.crtc;
  4446. struct drm_device *dev = crtc->dev;
  4447. struct drm_i915_private *dev_priv = to_i915(dev);
  4448. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4449. int pipe = intel_crtc->pipe;
  4450. struct intel_atomic_state *old_intel_state =
  4451. to_intel_atomic_state(old_state);
  4452. if (WARN_ON(intel_crtc->active))
  4453. return;
  4454. /*
  4455. * Sometimes spurious CPU pipe underruns happen during FDI
  4456. * training, at least with VGA+HDMI cloning. Suppress them.
  4457. *
  4458. * On ILK we get an occasional spurious CPU pipe underruns
  4459. * between eDP port A enable and vdd enable. Also PCH port
  4460. * enable seems to result in the occasional CPU pipe underrun.
  4461. *
  4462. * Spurious PCH underruns also occur during PCH enabling.
  4463. */
  4464. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4465. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4466. if (intel_crtc->config->has_pch_encoder)
  4467. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4468. if (intel_crtc->config->has_pch_encoder)
  4469. intel_prepare_shared_dpll(intel_crtc);
  4470. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4471. intel_dp_set_m_n(intel_crtc, M1_N1);
  4472. intel_set_pipe_timings(intel_crtc);
  4473. intel_set_pipe_src_size(intel_crtc);
  4474. if (intel_crtc->config->has_pch_encoder) {
  4475. intel_cpu_transcoder_set_m_n(intel_crtc,
  4476. &intel_crtc->config->fdi_m_n, NULL);
  4477. }
  4478. ironlake_set_pipeconf(crtc);
  4479. intel_crtc->active = true;
  4480. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4481. if (intel_crtc->config->has_pch_encoder) {
  4482. /* Note: FDI PLL enabling _must_ be done before we enable the
  4483. * cpu pipes, hence this is separate from all the other fdi/pch
  4484. * enabling. */
  4485. ironlake_fdi_pll_enable(intel_crtc);
  4486. } else {
  4487. assert_fdi_tx_disabled(dev_priv, pipe);
  4488. assert_fdi_rx_disabled(dev_priv, pipe);
  4489. }
  4490. ironlake_pfit_enable(intel_crtc);
  4491. /*
  4492. * On ILK+ LUT must be loaded before the pipe is running but with
  4493. * clocks enabled
  4494. */
  4495. intel_color_load_luts(&pipe_config->base);
  4496. if (dev_priv->display.initial_watermarks != NULL)
  4497. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4498. intel_enable_pipe(intel_crtc);
  4499. if (intel_crtc->config->has_pch_encoder)
  4500. ironlake_pch_enable(crtc);
  4501. assert_vblank_disabled(crtc);
  4502. drm_crtc_vblank_on(crtc);
  4503. intel_encoders_enable(crtc, pipe_config, old_state);
  4504. if (HAS_PCH_CPT(dev_priv))
  4505. cpt_verify_modeset(dev, intel_crtc->pipe);
  4506. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4507. if (intel_crtc->config->has_pch_encoder)
  4508. intel_wait_for_vblank(dev_priv, pipe);
  4509. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4510. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4511. }
  4512. /* IPS only exists on ULT machines and is tied to pipe A. */
  4513. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4514. {
  4515. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4516. }
  4517. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4518. struct drm_atomic_state *old_state)
  4519. {
  4520. struct drm_crtc *crtc = pipe_config->base.crtc;
  4521. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4522. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4523. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4524. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4525. struct intel_atomic_state *old_intel_state =
  4526. to_intel_atomic_state(old_state);
  4527. if (WARN_ON(intel_crtc->active))
  4528. return;
  4529. if (intel_crtc->config->has_pch_encoder)
  4530. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4531. false);
  4532. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4533. if (intel_crtc->config->shared_dpll)
  4534. intel_enable_shared_dpll(intel_crtc);
  4535. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4536. intel_dp_set_m_n(intel_crtc, M1_N1);
  4537. if (!transcoder_is_dsi(cpu_transcoder))
  4538. intel_set_pipe_timings(intel_crtc);
  4539. intel_set_pipe_src_size(intel_crtc);
  4540. if (cpu_transcoder != TRANSCODER_EDP &&
  4541. !transcoder_is_dsi(cpu_transcoder)) {
  4542. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4543. intel_crtc->config->pixel_multiplier - 1);
  4544. }
  4545. if (intel_crtc->config->has_pch_encoder) {
  4546. intel_cpu_transcoder_set_m_n(intel_crtc,
  4547. &intel_crtc->config->fdi_m_n, NULL);
  4548. }
  4549. if (!transcoder_is_dsi(cpu_transcoder))
  4550. haswell_set_pipeconf(crtc);
  4551. haswell_set_pipemisc(crtc);
  4552. intel_color_set_csc(&pipe_config->base);
  4553. intel_crtc->active = true;
  4554. if (intel_crtc->config->has_pch_encoder)
  4555. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4556. else
  4557. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4558. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4559. if (intel_crtc->config->has_pch_encoder)
  4560. dev_priv->display.fdi_link_train(crtc);
  4561. if (!transcoder_is_dsi(cpu_transcoder))
  4562. intel_ddi_enable_pipe_clock(intel_crtc);
  4563. if (INTEL_GEN(dev_priv) >= 9)
  4564. skylake_pfit_enable(intel_crtc);
  4565. else
  4566. ironlake_pfit_enable(intel_crtc);
  4567. /*
  4568. * On ILK+ LUT must be loaded before the pipe is running but with
  4569. * clocks enabled
  4570. */
  4571. intel_color_load_luts(&pipe_config->base);
  4572. intel_ddi_set_pipe_settings(crtc);
  4573. if (!transcoder_is_dsi(cpu_transcoder))
  4574. intel_ddi_enable_transcoder_func(crtc);
  4575. if (dev_priv->display.initial_watermarks != NULL)
  4576. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4577. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4578. if (!transcoder_is_dsi(cpu_transcoder))
  4579. intel_enable_pipe(intel_crtc);
  4580. if (intel_crtc->config->has_pch_encoder)
  4581. lpt_pch_enable(crtc);
  4582. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4583. intel_ddi_set_vc_payload_alloc(crtc, true);
  4584. assert_vblank_disabled(crtc);
  4585. drm_crtc_vblank_on(crtc);
  4586. intel_encoders_enable(crtc, pipe_config, old_state);
  4587. if (intel_crtc->config->has_pch_encoder) {
  4588. intel_wait_for_vblank(dev_priv, pipe);
  4589. intel_wait_for_vblank(dev_priv, pipe);
  4590. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4591. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4592. true);
  4593. }
  4594. /* If we change the relative order between pipe/planes enabling, we need
  4595. * to change the workaround. */
  4596. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4597. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4598. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4599. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4600. }
  4601. }
  4602. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4603. {
  4604. struct drm_device *dev = crtc->base.dev;
  4605. struct drm_i915_private *dev_priv = to_i915(dev);
  4606. int pipe = crtc->pipe;
  4607. /* To avoid upsetting the power well on haswell only disable the pfit if
  4608. * it's in use. The hw state code will make sure we get this right. */
  4609. if (force || crtc->config->pch_pfit.enabled) {
  4610. I915_WRITE(PF_CTL(pipe), 0);
  4611. I915_WRITE(PF_WIN_POS(pipe), 0);
  4612. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4613. }
  4614. }
  4615. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4616. struct drm_atomic_state *old_state)
  4617. {
  4618. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4619. struct drm_device *dev = crtc->dev;
  4620. struct drm_i915_private *dev_priv = to_i915(dev);
  4621. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4622. int pipe = intel_crtc->pipe;
  4623. /*
  4624. * Sometimes spurious CPU pipe underruns happen when the
  4625. * pipe is already disabled, but FDI RX/TX is still enabled.
  4626. * Happens at least with VGA+HDMI cloning. Suppress them.
  4627. */
  4628. if (intel_crtc->config->has_pch_encoder) {
  4629. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4630. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4631. }
  4632. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4633. drm_crtc_vblank_off(crtc);
  4634. assert_vblank_disabled(crtc);
  4635. intel_disable_pipe(intel_crtc);
  4636. ironlake_pfit_disable(intel_crtc, false);
  4637. if (intel_crtc->config->has_pch_encoder)
  4638. ironlake_fdi_disable(crtc);
  4639. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4640. if (intel_crtc->config->has_pch_encoder) {
  4641. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4642. if (HAS_PCH_CPT(dev_priv)) {
  4643. i915_reg_t reg;
  4644. u32 temp;
  4645. /* disable TRANS_DP_CTL */
  4646. reg = TRANS_DP_CTL(pipe);
  4647. temp = I915_READ(reg);
  4648. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4649. TRANS_DP_PORT_SEL_MASK);
  4650. temp |= TRANS_DP_PORT_SEL_NONE;
  4651. I915_WRITE(reg, temp);
  4652. /* disable DPLL_SEL */
  4653. temp = I915_READ(PCH_DPLL_SEL);
  4654. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4655. I915_WRITE(PCH_DPLL_SEL, temp);
  4656. }
  4657. ironlake_fdi_pll_disable(intel_crtc);
  4658. }
  4659. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4660. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4661. }
  4662. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4663. struct drm_atomic_state *old_state)
  4664. {
  4665. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4666. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4667. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4668. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4669. if (intel_crtc->config->has_pch_encoder)
  4670. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4671. false);
  4672. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4673. drm_crtc_vblank_off(crtc);
  4674. assert_vblank_disabled(crtc);
  4675. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4676. if (!transcoder_is_dsi(cpu_transcoder))
  4677. intel_disable_pipe(intel_crtc);
  4678. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4679. intel_ddi_set_vc_payload_alloc(crtc, false);
  4680. if (!transcoder_is_dsi(cpu_transcoder))
  4681. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4682. if (INTEL_GEN(dev_priv) >= 9)
  4683. skylake_scaler_disable(intel_crtc);
  4684. else
  4685. ironlake_pfit_disable(intel_crtc, false);
  4686. if (!transcoder_is_dsi(cpu_transcoder))
  4687. intel_ddi_disable_pipe_clock(intel_crtc);
  4688. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4689. if (old_crtc_state->has_pch_encoder)
  4690. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4691. true);
  4692. }
  4693. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4694. {
  4695. struct drm_device *dev = crtc->base.dev;
  4696. struct drm_i915_private *dev_priv = to_i915(dev);
  4697. struct intel_crtc_state *pipe_config = crtc->config;
  4698. if (!pipe_config->gmch_pfit.control)
  4699. return;
  4700. /*
  4701. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4702. * according to register description and PRM.
  4703. */
  4704. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4705. assert_pipe_disabled(dev_priv, crtc->pipe);
  4706. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4707. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4708. /* Border color in case we don't scale up to the full screen. Black by
  4709. * default, change to something else for debugging. */
  4710. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4711. }
  4712. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4713. {
  4714. switch (port) {
  4715. case PORT_A:
  4716. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4717. case PORT_B:
  4718. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4719. case PORT_C:
  4720. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4721. case PORT_D:
  4722. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4723. case PORT_E:
  4724. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4725. default:
  4726. MISSING_CASE(port);
  4727. return POWER_DOMAIN_PORT_OTHER;
  4728. }
  4729. }
  4730. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4731. {
  4732. switch (port) {
  4733. case PORT_A:
  4734. return POWER_DOMAIN_AUX_A;
  4735. case PORT_B:
  4736. return POWER_DOMAIN_AUX_B;
  4737. case PORT_C:
  4738. return POWER_DOMAIN_AUX_C;
  4739. case PORT_D:
  4740. return POWER_DOMAIN_AUX_D;
  4741. case PORT_E:
  4742. /* FIXME: Check VBT for actual wiring of PORT E */
  4743. return POWER_DOMAIN_AUX_D;
  4744. default:
  4745. MISSING_CASE(port);
  4746. return POWER_DOMAIN_AUX_A;
  4747. }
  4748. }
  4749. enum intel_display_power_domain
  4750. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4751. {
  4752. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4753. struct intel_digital_port *intel_dig_port;
  4754. switch (intel_encoder->type) {
  4755. case INTEL_OUTPUT_UNKNOWN:
  4756. /* Only DDI platforms should ever use this output type */
  4757. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4758. case INTEL_OUTPUT_DP:
  4759. case INTEL_OUTPUT_HDMI:
  4760. case INTEL_OUTPUT_EDP:
  4761. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4762. return port_to_power_domain(intel_dig_port->port);
  4763. case INTEL_OUTPUT_DP_MST:
  4764. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4765. return port_to_power_domain(intel_dig_port->port);
  4766. case INTEL_OUTPUT_ANALOG:
  4767. return POWER_DOMAIN_PORT_CRT;
  4768. case INTEL_OUTPUT_DSI:
  4769. return POWER_DOMAIN_PORT_DSI;
  4770. default:
  4771. return POWER_DOMAIN_PORT_OTHER;
  4772. }
  4773. }
  4774. enum intel_display_power_domain
  4775. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4776. {
  4777. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4778. struct intel_digital_port *intel_dig_port;
  4779. switch (intel_encoder->type) {
  4780. case INTEL_OUTPUT_UNKNOWN:
  4781. case INTEL_OUTPUT_HDMI:
  4782. /*
  4783. * Only DDI platforms should ever use these output types.
  4784. * We can get here after the HDMI detect code has already set
  4785. * the type of the shared encoder. Since we can't be sure
  4786. * what's the status of the given connectors, play safe and
  4787. * run the DP detection too.
  4788. */
  4789. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4790. case INTEL_OUTPUT_DP:
  4791. case INTEL_OUTPUT_EDP:
  4792. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4793. return port_to_aux_power_domain(intel_dig_port->port);
  4794. case INTEL_OUTPUT_DP_MST:
  4795. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4796. return port_to_aux_power_domain(intel_dig_port->port);
  4797. default:
  4798. MISSING_CASE(intel_encoder->type);
  4799. return POWER_DOMAIN_AUX_A;
  4800. }
  4801. }
  4802. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4803. struct intel_crtc_state *crtc_state)
  4804. {
  4805. struct drm_device *dev = crtc->dev;
  4806. struct drm_i915_private *dev_priv = to_i915(dev);
  4807. struct drm_encoder *encoder;
  4808. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4809. enum pipe pipe = intel_crtc->pipe;
  4810. u64 mask;
  4811. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4812. if (!crtc_state->base.active)
  4813. return 0;
  4814. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4815. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4816. if (crtc_state->pch_pfit.enabled ||
  4817. crtc_state->pch_pfit.force_thru)
  4818. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4819. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4820. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4821. mask |= BIT_ULL(intel_display_port_power_domain(intel_encoder));
  4822. }
  4823. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4824. mask |= BIT(POWER_DOMAIN_AUDIO);
  4825. if (crtc_state->shared_dpll)
  4826. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4827. return mask;
  4828. }
  4829. static u64
  4830. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4831. struct intel_crtc_state *crtc_state)
  4832. {
  4833. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4834. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4835. enum intel_display_power_domain domain;
  4836. u64 domains, new_domains, old_domains;
  4837. old_domains = intel_crtc->enabled_power_domains;
  4838. intel_crtc->enabled_power_domains = new_domains =
  4839. get_crtc_power_domains(crtc, crtc_state);
  4840. domains = new_domains & ~old_domains;
  4841. for_each_power_domain(domain, domains)
  4842. intel_display_power_get(dev_priv, domain);
  4843. return old_domains & ~new_domains;
  4844. }
  4845. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4846. u64 domains)
  4847. {
  4848. enum intel_display_power_domain domain;
  4849. for_each_power_domain(domain, domains)
  4850. intel_display_power_put(dev_priv, domain);
  4851. }
  4852. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  4853. struct drm_atomic_state *old_state)
  4854. {
  4855. struct drm_crtc *crtc = pipe_config->base.crtc;
  4856. struct drm_device *dev = crtc->dev;
  4857. struct drm_i915_private *dev_priv = to_i915(dev);
  4858. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4859. int pipe = intel_crtc->pipe;
  4860. if (WARN_ON(intel_crtc->active))
  4861. return;
  4862. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4863. intel_dp_set_m_n(intel_crtc, M1_N1);
  4864. intel_set_pipe_timings(intel_crtc);
  4865. intel_set_pipe_src_size(intel_crtc);
  4866. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  4867. struct drm_i915_private *dev_priv = to_i915(dev);
  4868. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4869. I915_WRITE(CHV_CANVAS(pipe), 0);
  4870. }
  4871. i9xx_set_pipeconf(intel_crtc);
  4872. intel_crtc->active = true;
  4873. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4874. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4875. if (IS_CHERRYVIEW(dev_priv)) {
  4876. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4877. chv_enable_pll(intel_crtc, intel_crtc->config);
  4878. } else {
  4879. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4880. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4881. }
  4882. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4883. i9xx_pfit_enable(intel_crtc);
  4884. intel_color_load_luts(&pipe_config->base);
  4885. intel_update_watermarks(intel_crtc);
  4886. intel_enable_pipe(intel_crtc);
  4887. assert_vblank_disabled(crtc);
  4888. drm_crtc_vblank_on(crtc);
  4889. intel_encoders_enable(crtc, pipe_config, old_state);
  4890. }
  4891. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4892. {
  4893. struct drm_device *dev = crtc->base.dev;
  4894. struct drm_i915_private *dev_priv = to_i915(dev);
  4895. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4896. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4897. }
  4898. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  4899. struct drm_atomic_state *old_state)
  4900. {
  4901. struct drm_crtc *crtc = pipe_config->base.crtc;
  4902. struct drm_device *dev = crtc->dev;
  4903. struct drm_i915_private *dev_priv = to_i915(dev);
  4904. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4905. enum pipe pipe = intel_crtc->pipe;
  4906. if (WARN_ON(intel_crtc->active))
  4907. return;
  4908. i9xx_set_pll_dividers(intel_crtc);
  4909. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4910. intel_dp_set_m_n(intel_crtc, M1_N1);
  4911. intel_set_pipe_timings(intel_crtc);
  4912. intel_set_pipe_src_size(intel_crtc);
  4913. i9xx_set_pipeconf(intel_crtc);
  4914. intel_crtc->active = true;
  4915. if (!IS_GEN2(dev_priv))
  4916. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4917. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4918. i9xx_enable_pll(intel_crtc);
  4919. i9xx_pfit_enable(intel_crtc);
  4920. intel_color_load_luts(&pipe_config->base);
  4921. intel_update_watermarks(intel_crtc);
  4922. intel_enable_pipe(intel_crtc);
  4923. assert_vblank_disabled(crtc);
  4924. drm_crtc_vblank_on(crtc);
  4925. intel_encoders_enable(crtc, pipe_config, old_state);
  4926. }
  4927. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4928. {
  4929. struct drm_device *dev = crtc->base.dev;
  4930. struct drm_i915_private *dev_priv = to_i915(dev);
  4931. if (!crtc->config->gmch_pfit.control)
  4932. return;
  4933. assert_pipe_disabled(dev_priv, crtc->pipe);
  4934. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4935. I915_READ(PFIT_CONTROL));
  4936. I915_WRITE(PFIT_CONTROL, 0);
  4937. }
  4938. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4939. struct drm_atomic_state *old_state)
  4940. {
  4941. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4942. struct drm_device *dev = crtc->dev;
  4943. struct drm_i915_private *dev_priv = to_i915(dev);
  4944. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4945. int pipe = intel_crtc->pipe;
  4946. /*
  4947. * On gen2 planes are double buffered but the pipe isn't, so we must
  4948. * wait for planes to fully turn off before disabling the pipe.
  4949. */
  4950. if (IS_GEN2(dev_priv))
  4951. intel_wait_for_vblank(dev_priv, pipe);
  4952. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4953. drm_crtc_vblank_off(crtc);
  4954. assert_vblank_disabled(crtc);
  4955. intel_disable_pipe(intel_crtc);
  4956. i9xx_pfit_disable(intel_crtc);
  4957. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4958. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  4959. if (IS_CHERRYVIEW(dev_priv))
  4960. chv_disable_pll(dev_priv, pipe);
  4961. else if (IS_VALLEYVIEW(dev_priv))
  4962. vlv_disable_pll(dev_priv, pipe);
  4963. else
  4964. i9xx_disable_pll(intel_crtc);
  4965. }
  4966. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  4967. if (!IS_GEN2(dev_priv))
  4968. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4969. }
  4970. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  4971. {
  4972. struct intel_encoder *encoder;
  4973. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4974. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4975. enum intel_display_power_domain domain;
  4976. u64 domains;
  4977. struct drm_atomic_state *state;
  4978. struct intel_crtc_state *crtc_state;
  4979. int ret;
  4980. if (!intel_crtc->active)
  4981. return;
  4982. if (crtc->primary->state->visible) {
  4983. WARN_ON(intel_crtc->flip_work);
  4984. intel_pre_disable_primary_noatomic(crtc);
  4985. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  4986. crtc->primary->state->visible = false;
  4987. }
  4988. state = drm_atomic_state_alloc(crtc->dev);
  4989. if (!state) {
  4990. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  4991. crtc->base.id, crtc->name);
  4992. return;
  4993. }
  4994. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  4995. /* Everything's already locked, -EDEADLK can't happen. */
  4996. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4997. ret = drm_atomic_add_affected_connectors(state, crtc);
  4998. WARN_ON(IS_ERR(crtc_state) || ret);
  4999. dev_priv->display.crtc_disable(crtc_state, state);
  5000. drm_atomic_state_put(state);
  5001. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5002. crtc->base.id, crtc->name);
  5003. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5004. crtc->state->active = false;
  5005. intel_crtc->active = false;
  5006. crtc->enabled = false;
  5007. crtc->state->connector_mask = 0;
  5008. crtc->state->encoder_mask = 0;
  5009. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5010. encoder->base.crtc = NULL;
  5011. intel_fbc_disable(intel_crtc);
  5012. intel_update_watermarks(intel_crtc);
  5013. intel_disable_shared_dpll(intel_crtc);
  5014. domains = intel_crtc->enabled_power_domains;
  5015. for_each_power_domain(domain, domains)
  5016. intel_display_power_put(dev_priv, domain);
  5017. intel_crtc->enabled_power_domains = 0;
  5018. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5019. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5020. }
  5021. /*
  5022. * turn all crtc's off, but do not adjust state
  5023. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5024. */
  5025. int intel_display_suspend(struct drm_device *dev)
  5026. {
  5027. struct drm_i915_private *dev_priv = to_i915(dev);
  5028. struct drm_atomic_state *state;
  5029. int ret;
  5030. state = drm_atomic_helper_suspend(dev);
  5031. ret = PTR_ERR_OR_ZERO(state);
  5032. if (ret)
  5033. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5034. else
  5035. dev_priv->modeset_restore_state = state;
  5036. return ret;
  5037. }
  5038. void intel_encoder_destroy(struct drm_encoder *encoder)
  5039. {
  5040. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5041. drm_encoder_cleanup(encoder);
  5042. kfree(intel_encoder);
  5043. }
  5044. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5045. * internal consistency). */
  5046. static void intel_connector_verify_state(struct intel_connector *connector)
  5047. {
  5048. struct drm_crtc *crtc = connector->base.state->crtc;
  5049. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5050. connector->base.base.id,
  5051. connector->base.name);
  5052. if (connector->get_hw_state(connector)) {
  5053. struct intel_encoder *encoder = connector->encoder;
  5054. struct drm_connector_state *conn_state = connector->base.state;
  5055. I915_STATE_WARN(!crtc,
  5056. "connector enabled without attached crtc\n");
  5057. if (!crtc)
  5058. return;
  5059. I915_STATE_WARN(!crtc->state->active,
  5060. "connector is active, but attached crtc isn't\n");
  5061. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5062. return;
  5063. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5064. "atomic encoder doesn't match attached encoder\n");
  5065. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5066. "attached encoder crtc differs from connector crtc\n");
  5067. } else {
  5068. I915_STATE_WARN(crtc && crtc->state->active,
  5069. "attached crtc is active, but connector isn't\n");
  5070. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5071. "best encoder set without crtc!\n");
  5072. }
  5073. }
  5074. int intel_connector_init(struct intel_connector *connector)
  5075. {
  5076. drm_atomic_helper_connector_reset(&connector->base);
  5077. if (!connector->base.state)
  5078. return -ENOMEM;
  5079. return 0;
  5080. }
  5081. struct intel_connector *intel_connector_alloc(void)
  5082. {
  5083. struct intel_connector *connector;
  5084. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5085. if (!connector)
  5086. return NULL;
  5087. if (intel_connector_init(connector) < 0) {
  5088. kfree(connector);
  5089. return NULL;
  5090. }
  5091. return connector;
  5092. }
  5093. /* Simple connector->get_hw_state implementation for encoders that support only
  5094. * one connector and no cloning and hence the encoder state determines the state
  5095. * of the connector. */
  5096. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5097. {
  5098. enum pipe pipe = 0;
  5099. struct intel_encoder *encoder = connector->encoder;
  5100. return encoder->get_hw_state(encoder, &pipe);
  5101. }
  5102. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5103. {
  5104. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5105. return crtc_state->fdi_lanes;
  5106. return 0;
  5107. }
  5108. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5109. struct intel_crtc_state *pipe_config)
  5110. {
  5111. struct drm_i915_private *dev_priv = to_i915(dev);
  5112. struct drm_atomic_state *state = pipe_config->base.state;
  5113. struct intel_crtc *other_crtc;
  5114. struct intel_crtc_state *other_crtc_state;
  5115. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5116. pipe_name(pipe), pipe_config->fdi_lanes);
  5117. if (pipe_config->fdi_lanes > 4) {
  5118. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5119. pipe_name(pipe), pipe_config->fdi_lanes);
  5120. return -EINVAL;
  5121. }
  5122. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5123. if (pipe_config->fdi_lanes > 2) {
  5124. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5125. pipe_config->fdi_lanes);
  5126. return -EINVAL;
  5127. } else {
  5128. return 0;
  5129. }
  5130. }
  5131. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5132. return 0;
  5133. /* Ivybridge 3 pipe is really complicated */
  5134. switch (pipe) {
  5135. case PIPE_A:
  5136. return 0;
  5137. case PIPE_B:
  5138. if (pipe_config->fdi_lanes <= 2)
  5139. return 0;
  5140. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5141. other_crtc_state =
  5142. intel_atomic_get_crtc_state(state, other_crtc);
  5143. if (IS_ERR(other_crtc_state))
  5144. return PTR_ERR(other_crtc_state);
  5145. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5146. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5147. pipe_name(pipe), pipe_config->fdi_lanes);
  5148. return -EINVAL;
  5149. }
  5150. return 0;
  5151. case PIPE_C:
  5152. if (pipe_config->fdi_lanes > 2) {
  5153. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5154. pipe_name(pipe), pipe_config->fdi_lanes);
  5155. return -EINVAL;
  5156. }
  5157. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5158. other_crtc_state =
  5159. intel_atomic_get_crtc_state(state, other_crtc);
  5160. if (IS_ERR(other_crtc_state))
  5161. return PTR_ERR(other_crtc_state);
  5162. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5163. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5164. return -EINVAL;
  5165. }
  5166. return 0;
  5167. default:
  5168. BUG();
  5169. }
  5170. }
  5171. #define RETRY 1
  5172. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5173. struct intel_crtc_state *pipe_config)
  5174. {
  5175. struct drm_device *dev = intel_crtc->base.dev;
  5176. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5177. int lane, link_bw, fdi_dotclock, ret;
  5178. bool needs_recompute = false;
  5179. retry:
  5180. /* FDI is a binary signal running at ~2.7GHz, encoding
  5181. * each output octet as 10 bits. The actual frequency
  5182. * is stored as a divider into a 100MHz clock, and the
  5183. * mode pixel clock is stored in units of 1KHz.
  5184. * Hence the bw of each lane in terms of the mode signal
  5185. * is:
  5186. */
  5187. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5188. fdi_dotclock = adjusted_mode->crtc_clock;
  5189. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5190. pipe_config->pipe_bpp);
  5191. pipe_config->fdi_lanes = lane;
  5192. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5193. link_bw, &pipe_config->fdi_m_n);
  5194. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5195. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5196. pipe_config->pipe_bpp -= 2*3;
  5197. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5198. pipe_config->pipe_bpp);
  5199. needs_recompute = true;
  5200. pipe_config->bw_constrained = true;
  5201. goto retry;
  5202. }
  5203. if (needs_recompute)
  5204. return RETRY;
  5205. return ret;
  5206. }
  5207. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5208. struct intel_crtc_state *pipe_config)
  5209. {
  5210. if (pipe_config->pipe_bpp > 24)
  5211. return false;
  5212. /* HSW can handle pixel rate up to cdclk? */
  5213. if (IS_HASWELL(dev_priv))
  5214. return true;
  5215. /*
  5216. * We compare against max which means we must take
  5217. * the increased cdclk requirement into account when
  5218. * calculating the new cdclk.
  5219. *
  5220. * Should measure whether using a lower cdclk w/o IPS
  5221. */
  5222. return pipe_config->pixel_rate <=
  5223. dev_priv->max_cdclk_freq * 95 / 100;
  5224. }
  5225. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5226. struct intel_crtc_state *pipe_config)
  5227. {
  5228. struct drm_device *dev = crtc->base.dev;
  5229. struct drm_i915_private *dev_priv = to_i915(dev);
  5230. pipe_config->ips_enabled = i915.enable_ips &&
  5231. hsw_crtc_supports_ips(crtc) &&
  5232. pipe_config_supports_ips(dev_priv, pipe_config);
  5233. }
  5234. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5235. {
  5236. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5237. /* GDG double wide on either pipe, otherwise pipe A only */
  5238. return INTEL_INFO(dev_priv)->gen < 4 &&
  5239. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5240. }
  5241. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5242. {
  5243. uint32_t pixel_rate;
  5244. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5245. /*
  5246. * We only use IF-ID interlacing. If we ever use
  5247. * PF-ID we'll need to adjust the pixel_rate here.
  5248. */
  5249. if (pipe_config->pch_pfit.enabled) {
  5250. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5251. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5252. pipe_w = pipe_config->pipe_src_w;
  5253. pipe_h = pipe_config->pipe_src_h;
  5254. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5255. pfit_h = pfit_size & 0xFFFF;
  5256. if (pipe_w < pfit_w)
  5257. pipe_w = pfit_w;
  5258. if (pipe_h < pfit_h)
  5259. pipe_h = pfit_h;
  5260. if (WARN_ON(!pfit_w || !pfit_h))
  5261. return pixel_rate;
  5262. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5263. pfit_w * pfit_h);
  5264. }
  5265. return pixel_rate;
  5266. }
  5267. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5268. {
  5269. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5270. if (HAS_GMCH_DISPLAY(dev_priv))
  5271. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5272. crtc_state->pixel_rate =
  5273. crtc_state->base.adjusted_mode.crtc_clock;
  5274. else
  5275. crtc_state->pixel_rate =
  5276. ilk_pipe_pixel_rate(crtc_state);
  5277. }
  5278. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5279. struct intel_crtc_state *pipe_config)
  5280. {
  5281. struct drm_device *dev = crtc->base.dev;
  5282. struct drm_i915_private *dev_priv = to_i915(dev);
  5283. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5284. int clock_limit = dev_priv->max_dotclk_freq;
  5285. if (INTEL_GEN(dev_priv) < 4) {
  5286. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5287. /*
  5288. * Enable double wide mode when the dot clock
  5289. * is > 90% of the (display) core speed.
  5290. */
  5291. if (intel_crtc_supports_double_wide(crtc) &&
  5292. adjusted_mode->crtc_clock > clock_limit) {
  5293. clock_limit = dev_priv->max_dotclk_freq;
  5294. pipe_config->double_wide = true;
  5295. }
  5296. }
  5297. if (adjusted_mode->crtc_clock > clock_limit) {
  5298. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5299. adjusted_mode->crtc_clock, clock_limit,
  5300. yesno(pipe_config->double_wide));
  5301. return -EINVAL;
  5302. }
  5303. /*
  5304. * Pipe horizontal size must be even in:
  5305. * - DVO ganged mode
  5306. * - LVDS dual channel mode
  5307. * - Double wide pipe
  5308. */
  5309. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5310. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5311. pipe_config->pipe_src_w &= ~1;
  5312. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5313. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5314. */
  5315. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5316. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5317. return -EINVAL;
  5318. intel_crtc_compute_pixel_rate(pipe_config);
  5319. if (HAS_IPS(dev_priv))
  5320. hsw_compute_ips_config(crtc, pipe_config);
  5321. if (pipe_config->has_pch_encoder)
  5322. return ironlake_fdi_compute_config(crtc, pipe_config);
  5323. return 0;
  5324. }
  5325. static void
  5326. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5327. {
  5328. while (*num > DATA_LINK_M_N_MASK ||
  5329. *den > DATA_LINK_M_N_MASK) {
  5330. *num >>= 1;
  5331. *den >>= 1;
  5332. }
  5333. }
  5334. static void compute_m_n(unsigned int m, unsigned int n,
  5335. uint32_t *ret_m, uint32_t *ret_n)
  5336. {
  5337. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5338. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5339. intel_reduce_m_n_ratio(ret_m, ret_n);
  5340. }
  5341. void
  5342. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5343. int pixel_clock, int link_clock,
  5344. struct intel_link_m_n *m_n)
  5345. {
  5346. m_n->tu = 64;
  5347. compute_m_n(bits_per_pixel * pixel_clock,
  5348. link_clock * nlanes * 8,
  5349. &m_n->gmch_m, &m_n->gmch_n);
  5350. compute_m_n(pixel_clock, link_clock,
  5351. &m_n->link_m, &m_n->link_n);
  5352. }
  5353. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5354. {
  5355. if (i915.panel_use_ssc >= 0)
  5356. return i915.panel_use_ssc != 0;
  5357. return dev_priv->vbt.lvds_use_ssc
  5358. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5359. }
  5360. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5361. {
  5362. return (1 << dpll->n) << 16 | dpll->m2;
  5363. }
  5364. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5365. {
  5366. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5367. }
  5368. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5369. struct intel_crtc_state *crtc_state,
  5370. struct dpll *reduced_clock)
  5371. {
  5372. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5373. u32 fp, fp2 = 0;
  5374. if (IS_PINEVIEW(dev_priv)) {
  5375. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5376. if (reduced_clock)
  5377. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5378. } else {
  5379. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5380. if (reduced_clock)
  5381. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5382. }
  5383. crtc_state->dpll_hw_state.fp0 = fp;
  5384. crtc->lowfreq_avail = false;
  5385. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5386. reduced_clock) {
  5387. crtc_state->dpll_hw_state.fp1 = fp2;
  5388. crtc->lowfreq_avail = true;
  5389. } else {
  5390. crtc_state->dpll_hw_state.fp1 = fp;
  5391. }
  5392. }
  5393. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5394. pipe)
  5395. {
  5396. u32 reg_val;
  5397. /*
  5398. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5399. * and set it to a reasonable value instead.
  5400. */
  5401. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5402. reg_val &= 0xffffff00;
  5403. reg_val |= 0x00000030;
  5404. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5405. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5406. reg_val &= 0x8cffffff;
  5407. reg_val = 0x8c000000;
  5408. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5409. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5410. reg_val &= 0xffffff00;
  5411. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5412. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5413. reg_val &= 0x00ffffff;
  5414. reg_val |= 0xb0000000;
  5415. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5416. }
  5417. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5418. struct intel_link_m_n *m_n)
  5419. {
  5420. struct drm_device *dev = crtc->base.dev;
  5421. struct drm_i915_private *dev_priv = to_i915(dev);
  5422. int pipe = crtc->pipe;
  5423. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5424. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5425. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5426. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5427. }
  5428. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5429. struct intel_link_m_n *m_n,
  5430. struct intel_link_m_n *m2_n2)
  5431. {
  5432. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5433. int pipe = crtc->pipe;
  5434. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5435. if (INTEL_GEN(dev_priv) >= 5) {
  5436. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5437. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5438. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5439. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5440. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5441. * for gen < 8) and if DRRS is supported (to make sure the
  5442. * registers are not unnecessarily accessed).
  5443. */
  5444. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5445. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5446. I915_WRITE(PIPE_DATA_M2(transcoder),
  5447. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5448. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5449. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5450. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5451. }
  5452. } else {
  5453. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5454. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5455. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5456. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5457. }
  5458. }
  5459. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5460. {
  5461. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5462. if (m_n == M1_N1) {
  5463. dp_m_n = &crtc->config->dp_m_n;
  5464. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5465. } else if (m_n == M2_N2) {
  5466. /*
  5467. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5468. * needs to be programmed into M1_N1.
  5469. */
  5470. dp_m_n = &crtc->config->dp_m2_n2;
  5471. } else {
  5472. DRM_ERROR("Unsupported divider value\n");
  5473. return;
  5474. }
  5475. if (crtc->config->has_pch_encoder)
  5476. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5477. else
  5478. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5479. }
  5480. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5481. struct intel_crtc_state *pipe_config)
  5482. {
  5483. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5484. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5485. if (crtc->pipe != PIPE_A)
  5486. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5487. /* DPLL not used with DSI, but still need the rest set up */
  5488. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5489. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5490. DPLL_EXT_BUFFER_ENABLE_VLV;
  5491. pipe_config->dpll_hw_state.dpll_md =
  5492. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5493. }
  5494. static void chv_compute_dpll(struct intel_crtc *crtc,
  5495. struct intel_crtc_state *pipe_config)
  5496. {
  5497. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5498. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5499. if (crtc->pipe != PIPE_A)
  5500. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5501. /* DPLL not used with DSI, but still need the rest set up */
  5502. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5503. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5504. pipe_config->dpll_hw_state.dpll_md =
  5505. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5506. }
  5507. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5508. const struct intel_crtc_state *pipe_config)
  5509. {
  5510. struct drm_device *dev = crtc->base.dev;
  5511. struct drm_i915_private *dev_priv = to_i915(dev);
  5512. enum pipe pipe = crtc->pipe;
  5513. u32 mdiv;
  5514. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5515. u32 coreclk, reg_val;
  5516. /* Enable Refclk */
  5517. I915_WRITE(DPLL(pipe),
  5518. pipe_config->dpll_hw_state.dpll &
  5519. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5520. /* No need to actually set up the DPLL with DSI */
  5521. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5522. return;
  5523. mutex_lock(&dev_priv->sb_lock);
  5524. bestn = pipe_config->dpll.n;
  5525. bestm1 = pipe_config->dpll.m1;
  5526. bestm2 = pipe_config->dpll.m2;
  5527. bestp1 = pipe_config->dpll.p1;
  5528. bestp2 = pipe_config->dpll.p2;
  5529. /* See eDP HDMI DPIO driver vbios notes doc */
  5530. /* PLL B needs special handling */
  5531. if (pipe == PIPE_B)
  5532. vlv_pllb_recal_opamp(dev_priv, pipe);
  5533. /* Set up Tx target for periodic Rcomp update */
  5534. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5535. /* Disable target IRef on PLL */
  5536. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5537. reg_val &= 0x00ffffff;
  5538. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5539. /* Disable fast lock */
  5540. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5541. /* Set idtafcrecal before PLL is enabled */
  5542. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5543. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5544. mdiv |= ((bestn << DPIO_N_SHIFT));
  5545. mdiv |= (1 << DPIO_K_SHIFT);
  5546. /*
  5547. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5548. * but we don't support that).
  5549. * Note: don't use the DAC post divider as it seems unstable.
  5550. */
  5551. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5552. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5553. mdiv |= DPIO_ENABLE_CALIBRATION;
  5554. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5555. /* Set HBR and RBR LPF coefficients */
  5556. if (pipe_config->port_clock == 162000 ||
  5557. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5558. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5559. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5560. 0x009f0003);
  5561. else
  5562. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5563. 0x00d0000f);
  5564. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5565. /* Use SSC source */
  5566. if (pipe == PIPE_A)
  5567. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5568. 0x0df40000);
  5569. else
  5570. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5571. 0x0df70000);
  5572. } else { /* HDMI or VGA */
  5573. /* Use bend source */
  5574. if (pipe == PIPE_A)
  5575. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5576. 0x0df70000);
  5577. else
  5578. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5579. 0x0df40000);
  5580. }
  5581. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5582. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5583. if (intel_crtc_has_dp_encoder(crtc->config))
  5584. coreclk |= 0x01000000;
  5585. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5586. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5587. mutex_unlock(&dev_priv->sb_lock);
  5588. }
  5589. static void chv_prepare_pll(struct intel_crtc *crtc,
  5590. const struct intel_crtc_state *pipe_config)
  5591. {
  5592. struct drm_device *dev = crtc->base.dev;
  5593. struct drm_i915_private *dev_priv = to_i915(dev);
  5594. enum pipe pipe = crtc->pipe;
  5595. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5596. u32 loopfilter, tribuf_calcntr;
  5597. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5598. u32 dpio_val;
  5599. int vco;
  5600. /* Enable Refclk and SSC */
  5601. I915_WRITE(DPLL(pipe),
  5602. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5603. /* No need to actually set up the DPLL with DSI */
  5604. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5605. return;
  5606. bestn = pipe_config->dpll.n;
  5607. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5608. bestm1 = pipe_config->dpll.m1;
  5609. bestm2 = pipe_config->dpll.m2 >> 22;
  5610. bestp1 = pipe_config->dpll.p1;
  5611. bestp2 = pipe_config->dpll.p2;
  5612. vco = pipe_config->dpll.vco;
  5613. dpio_val = 0;
  5614. loopfilter = 0;
  5615. mutex_lock(&dev_priv->sb_lock);
  5616. /* p1 and p2 divider */
  5617. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5618. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5619. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5620. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5621. 1 << DPIO_CHV_K_DIV_SHIFT);
  5622. /* Feedback post-divider - m2 */
  5623. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5624. /* Feedback refclk divider - n and m1 */
  5625. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5626. DPIO_CHV_M1_DIV_BY_2 |
  5627. 1 << DPIO_CHV_N_DIV_SHIFT);
  5628. /* M2 fraction division */
  5629. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5630. /* M2 fraction division enable */
  5631. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5632. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5633. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5634. if (bestm2_frac)
  5635. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5636. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5637. /* Program digital lock detect threshold */
  5638. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5639. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5640. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5641. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5642. if (!bestm2_frac)
  5643. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5644. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5645. /* Loop filter */
  5646. if (vco == 5400000) {
  5647. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5648. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5649. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5650. tribuf_calcntr = 0x9;
  5651. } else if (vco <= 6200000) {
  5652. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5653. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5654. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5655. tribuf_calcntr = 0x9;
  5656. } else if (vco <= 6480000) {
  5657. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5658. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5659. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5660. tribuf_calcntr = 0x8;
  5661. } else {
  5662. /* Not supported. Apply the same limits as in the max case */
  5663. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5664. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5665. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5666. tribuf_calcntr = 0;
  5667. }
  5668. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5669. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5670. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5671. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5672. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5673. /* AFC Recal */
  5674. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5675. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5676. DPIO_AFC_RECAL);
  5677. mutex_unlock(&dev_priv->sb_lock);
  5678. }
  5679. /**
  5680. * vlv_force_pll_on - forcibly enable just the PLL
  5681. * @dev_priv: i915 private structure
  5682. * @pipe: pipe PLL to enable
  5683. * @dpll: PLL configuration
  5684. *
  5685. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5686. * in cases where we need the PLL enabled even when @pipe is not going to
  5687. * be enabled.
  5688. */
  5689. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5690. const struct dpll *dpll)
  5691. {
  5692. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5693. struct intel_crtc_state *pipe_config;
  5694. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5695. if (!pipe_config)
  5696. return -ENOMEM;
  5697. pipe_config->base.crtc = &crtc->base;
  5698. pipe_config->pixel_multiplier = 1;
  5699. pipe_config->dpll = *dpll;
  5700. if (IS_CHERRYVIEW(dev_priv)) {
  5701. chv_compute_dpll(crtc, pipe_config);
  5702. chv_prepare_pll(crtc, pipe_config);
  5703. chv_enable_pll(crtc, pipe_config);
  5704. } else {
  5705. vlv_compute_dpll(crtc, pipe_config);
  5706. vlv_prepare_pll(crtc, pipe_config);
  5707. vlv_enable_pll(crtc, pipe_config);
  5708. }
  5709. kfree(pipe_config);
  5710. return 0;
  5711. }
  5712. /**
  5713. * vlv_force_pll_off - forcibly disable just the PLL
  5714. * @dev_priv: i915 private structure
  5715. * @pipe: pipe PLL to disable
  5716. *
  5717. * Disable the PLL for @pipe. To be used in cases where we need
  5718. * the PLL enabled even when @pipe is not going to be enabled.
  5719. */
  5720. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5721. {
  5722. if (IS_CHERRYVIEW(dev_priv))
  5723. chv_disable_pll(dev_priv, pipe);
  5724. else
  5725. vlv_disable_pll(dev_priv, pipe);
  5726. }
  5727. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5728. struct intel_crtc_state *crtc_state,
  5729. struct dpll *reduced_clock)
  5730. {
  5731. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5732. u32 dpll;
  5733. struct dpll *clock = &crtc_state->dpll;
  5734. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5735. dpll = DPLL_VGA_MODE_DIS;
  5736. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5737. dpll |= DPLLB_MODE_LVDS;
  5738. else
  5739. dpll |= DPLLB_MODE_DAC_SERIAL;
  5740. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5741. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5742. dpll |= (crtc_state->pixel_multiplier - 1)
  5743. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5744. }
  5745. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5746. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5747. dpll |= DPLL_SDVO_HIGH_SPEED;
  5748. if (intel_crtc_has_dp_encoder(crtc_state))
  5749. dpll |= DPLL_SDVO_HIGH_SPEED;
  5750. /* compute bitmask from p1 value */
  5751. if (IS_PINEVIEW(dev_priv))
  5752. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5753. else {
  5754. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5755. if (IS_G4X(dev_priv) && reduced_clock)
  5756. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5757. }
  5758. switch (clock->p2) {
  5759. case 5:
  5760. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5761. break;
  5762. case 7:
  5763. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5764. break;
  5765. case 10:
  5766. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5767. break;
  5768. case 14:
  5769. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5770. break;
  5771. }
  5772. if (INTEL_GEN(dev_priv) >= 4)
  5773. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5774. if (crtc_state->sdvo_tv_clock)
  5775. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5776. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5777. intel_panel_use_ssc(dev_priv))
  5778. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5779. else
  5780. dpll |= PLL_REF_INPUT_DREFCLK;
  5781. dpll |= DPLL_VCO_ENABLE;
  5782. crtc_state->dpll_hw_state.dpll = dpll;
  5783. if (INTEL_GEN(dev_priv) >= 4) {
  5784. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5785. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5786. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5787. }
  5788. }
  5789. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5790. struct intel_crtc_state *crtc_state,
  5791. struct dpll *reduced_clock)
  5792. {
  5793. struct drm_device *dev = crtc->base.dev;
  5794. struct drm_i915_private *dev_priv = to_i915(dev);
  5795. u32 dpll;
  5796. struct dpll *clock = &crtc_state->dpll;
  5797. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5798. dpll = DPLL_VGA_MODE_DIS;
  5799. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5800. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5801. } else {
  5802. if (clock->p1 == 2)
  5803. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5804. else
  5805. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5806. if (clock->p2 == 4)
  5807. dpll |= PLL_P2_DIVIDE_BY_4;
  5808. }
  5809. if (!IS_I830(dev_priv) &&
  5810. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5811. dpll |= DPLL_DVO_2X_MODE;
  5812. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5813. intel_panel_use_ssc(dev_priv))
  5814. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5815. else
  5816. dpll |= PLL_REF_INPUT_DREFCLK;
  5817. dpll |= DPLL_VCO_ENABLE;
  5818. crtc_state->dpll_hw_state.dpll = dpll;
  5819. }
  5820. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5821. {
  5822. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5823. enum pipe pipe = intel_crtc->pipe;
  5824. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5825. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5826. uint32_t crtc_vtotal, crtc_vblank_end;
  5827. int vsyncshift = 0;
  5828. /* We need to be careful not to changed the adjusted mode, for otherwise
  5829. * the hw state checker will get angry at the mismatch. */
  5830. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5831. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5832. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5833. /* the chip adds 2 halflines automatically */
  5834. crtc_vtotal -= 1;
  5835. crtc_vblank_end -= 1;
  5836. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5837. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5838. else
  5839. vsyncshift = adjusted_mode->crtc_hsync_start -
  5840. adjusted_mode->crtc_htotal / 2;
  5841. if (vsyncshift < 0)
  5842. vsyncshift += adjusted_mode->crtc_htotal;
  5843. }
  5844. if (INTEL_GEN(dev_priv) > 3)
  5845. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5846. I915_WRITE(HTOTAL(cpu_transcoder),
  5847. (adjusted_mode->crtc_hdisplay - 1) |
  5848. ((adjusted_mode->crtc_htotal - 1) << 16));
  5849. I915_WRITE(HBLANK(cpu_transcoder),
  5850. (adjusted_mode->crtc_hblank_start - 1) |
  5851. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5852. I915_WRITE(HSYNC(cpu_transcoder),
  5853. (adjusted_mode->crtc_hsync_start - 1) |
  5854. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5855. I915_WRITE(VTOTAL(cpu_transcoder),
  5856. (adjusted_mode->crtc_vdisplay - 1) |
  5857. ((crtc_vtotal - 1) << 16));
  5858. I915_WRITE(VBLANK(cpu_transcoder),
  5859. (adjusted_mode->crtc_vblank_start - 1) |
  5860. ((crtc_vblank_end - 1) << 16));
  5861. I915_WRITE(VSYNC(cpu_transcoder),
  5862. (adjusted_mode->crtc_vsync_start - 1) |
  5863. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5864. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5865. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5866. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5867. * bits. */
  5868. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  5869. (pipe == PIPE_B || pipe == PIPE_C))
  5870. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5871. }
  5872. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  5873. {
  5874. struct drm_device *dev = intel_crtc->base.dev;
  5875. struct drm_i915_private *dev_priv = to_i915(dev);
  5876. enum pipe pipe = intel_crtc->pipe;
  5877. /* pipesrc controls the size that is scaled from, which should
  5878. * always be the user's requested size.
  5879. */
  5880. I915_WRITE(PIPESRC(pipe),
  5881. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5882. (intel_crtc->config->pipe_src_h - 1));
  5883. }
  5884. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5885. struct intel_crtc_state *pipe_config)
  5886. {
  5887. struct drm_device *dev = crtc->base.dev;
  5888. struct drm_i915_private *dev_priv = to_i915(dev);
  5889. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5890. uint32_t tmp;
  5891. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5892. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5893. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5894. tmp = I915_READ(HBLANK(cpu_transcoder));
  5895. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5896. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5897. tmp = I915_READ(HSYNC(cpu_transcoder));
  5898. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5899. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5900. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5901. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5902. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5903. tmp = I915_READ(VBLANK(cpu_transcoder));
  5904. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5905. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5906. tmp = I915_READ(VSYNC(cpu_transcoder));
  5907. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5908. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5909. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5910. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5911. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5912. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5913. }
  5914. }
  5915. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  5916. struct intel_crtc_state *pipe_config)
  5917. {
  5918. struct drm_device *dev = crtc->base.dev;
  5919. struct drm_i915_private *dev_priv = to_i915(dev);
  5920. u32 tmp;
  5921. tmp = I915_READ(PIPESRC(crtc->pipe));
  5922. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5923. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5924. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5925. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5926. }
  5927. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5928. struct intel_crtc_state *pipe_config)
  5929. {
  5930. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5931. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5932. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5933. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5934. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5935. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5936. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5937. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5938. mode->flags = pipe_config->base.adjusted_mode.flags;
  5939. mode->type = DRM_MODE_TYPE_DRIVER;
  5940. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5941. mode->hsync = drm_mode_hsync(mode);
  5942. mode->vrefresh = drm_mode_vrefresh(mode);
  5943. drm_mode_set_name(mode);
  5944. }
  5945. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5946. {
  5947. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5948. uint32_t pipeconf;
  5949. pipeconf = 0;
  5950. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5951. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5952. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5953. if (intel_crtc->config->double_wide)
  5954. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5955. /* only g4x and later have fancy bpc/dither controls */
  5956. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  5957. IS_CHERRYVIEW(dev_priv)) {
  5958. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5959. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5960. pipeconf |= PIPECONF_DITHER_EN |
  5961. PIPECONF_DITHER_TYPE_SP;
  5962. switch (intel_crtc->config->pipe_bpp) {
  5963. case 18:
  5964. pipeconf |= PIPECONF_6BPC;
  5965. break;
  5966. case 24:
  5967. pipeconf |= PIPECONF_8BPC;
  5968. break;
  5969. case 30:
  5970. pipeconf |= PIPECONF_10BPC;
  5971. break;
  5972. default:
  5973. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5974. BUG();
  5975. }
  5976. }
  5977. if (HAS_PIPE_CXSR(dev_priv)) {
  5978. if (intel_crtc->lowfreq_avail) {
  5979. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5980. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5981. } else {
  5982. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5983. }
  5984. }
  5985. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5986. if (INTEL_GEN(dev_priv) < 4 ||
  5987. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5988. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5989. else
  5990. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5991. } else
  5992. pipeconf |= PIPECONF_PROGRESSIVE;
  5993. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  5994. intel_crtc->config->limited_color_range)
  5995. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5996. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5997. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5998. }
  5999. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6000. struct intel_crtc_state *crtc_state)
  6001. {
  6002. struct drm_device *dev = crtc->base.dev;
  6003. struct drm_i915_private *dev_priv = to_i915(dev);
  6004. const struct intel_limit *limit;
  6005. int refclk = 48000;
  6006. memset(&crtc_state->dpll_hw_state, 0,
  6007. sizeof(crtc_state->dpll_hw_state));
  6008. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6009. if (intel_panel_use_ssc(dev_priv)) {
  6010. refclk = dev_priv->vbt.lvds_ssc_freq;
  6011. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6012. }
  6013. limit = &intel_limits_i8xx_lvds;
  6014. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6015. limit = &intel_limits_i8xx_dvo;
  6016. } else {
  6017. limit = &intel_limits_i8xx_dac;
  6018. }
  6019. if (!crtc_state->clock_set &&
  6020. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6021. refclk, NULL, &crtc_state->dpll)) {
  6022. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6023. return -EINVAL;
  6024. }
  6025. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6026. return 0;
  6027. }
  6028. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6029. struct intel_crtc_state *crtc_state)
  6030. {
  6031. struct drm_device *dev = crtc->base.dev;
  6032. struct drm_i915_private *dev_priv = to_i915(dev);
  6033. const struct intel_limit *limit;
  6034. int refclk = 96000;
  6035. memset(&crtc_state->dpll_hw_state, 0,
  6036. sizeof(crtc_state->dpll_hw_state));
  6037. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6038. if (intel_panel_use_ssc(dev_priv)) {
  6039. refclk = dev_priv->vbt.lvds_ssc_freq;
  6040. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6041. }
  6042. if (intel_is_dual_link_lvds(dev))
  6043. limit = &intel_limits_g4x_dual_channel_lvds;
  6044. else
  6045. limit = &intel_limits_g4x_single_channel_lvds;
  6046. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6047. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6048. limit = &intel_limits_g4x_hdmi;
  6049. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6050. limit = &intel_limits_g4x_sdvo;
  6051. } else {
  6052. /* The option is for other outputs */
  6053. limit = &intel_limits_i9xx_sdvo;
  6054. }
  6055. if (!crtc_state->clock_set &&
  6056. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6057. refclk, NULL, &crtc_state->dpll)) {
  6058. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6059. return -EINVAL;
  6060. }
  6061. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6062. return 0;
  6063. }
  6064. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6065. struct intel_crtc_state *crtc_state)
  6066. {
  6067. struct drm_device *dev = crtc->base.dev;
  6068. struct drm_i915_private *dev_priv = to_i915(dev);
  6069. const struct intel_limit *limit;
  6070. int refclk = 96000;
  6071. memset(&crtc_state->dpll_hw_state, 0,
  6072. sizeof(crtc_state->dpll_hw_state));
  6073. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6074. if (intel_panel_use_ssc(dev_priv)) {
  6075. refclk = dev_priv->vbt.lvds_ssc_freq;
  6076. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6077. }
  6078. limit = &intel_limits_pineview_lvds;
  6079. } else {
  6080. limit = &intel_limits_pineview_sdvo;
  6081. }
  6082. if (!crtc_state->clock_set &&
  6083. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6084. refclk, NULL, &crtc_state->dpll)) {
  6085. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6086. return -EINVAL;
  6087. }
  6088. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6089. return 0;
  6090. }
  6091. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6092. struct intel_crtc_state *crtc_state)
  6093. {
  6094. struct drm_device *dev = crtc->base.dev;
  6095. struct drm_i915_private *dev_priv = to_i915(dev);
  6096. const struct intel_limit *limit;
  6097. int refclk = 96000;
  6098. memset(&crtc_state->dpll_hw_state, 0,
  6099. sizeof(crtc_state->dpll_hw_state));
  6100. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6101. if (intel_panel_use_ssc(dev_priv)) {
  6102. refclk = dev_priv->vbt.lvds_ssc_freq;
  6103. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6104. }
  6105. limit = &intel_limits_i9xx_lvds;
  6106. } else {
  6107. limit = &intel_limits_i9xx_sdvo;
  6108. }
  6109. if (!crtc_state->clock_set &&
  6110. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6111. refclk, NULL, &crtc_state->dpll)) {
  6112. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6113. return -EINVAL;
  6114. }
  6115. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6116. return 0;
  6117. }
  6118. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6119. struct intel_crtc_state *crtc_state)
  6120. {
  6121. int refclk = 100000;
  6122. const struct intel_limit *limit = &intel_limits_chv;
  6123. memset(&crtc_state->dpll_hw_state, 0,
  6124. sizeof(crtc_state->dpll_hw_state));
  6125. if (!crtc_state->clock_set &&
  6126. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6127. refclk, NULL, &crtc_state->dpll)) {
  6128. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6129. return -EINVAL;
  6130. }
  6131. chv_compute_dpll(crtc, crtc_state);
  6132. return 0;
  6133. }
  6134. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6135. struct intel_crtc_state *crtc_state)
  6136. {
  6137. int refclk = 100000;
  6138. const struct intel_limit *limit = &intel_limits_vlv;
  6139. memset(&crtc_state->dpll_hw_state, 0,
  6140. sizeof(crtc_state->dpll_hw_state));
  6141. if (!crtc_state->clock_set &&
  6142. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6143. refclk, NULL, &crtc_state->dpll)) {
  6144. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6145. return -EINVAL;
  6146. }
  6147. vlv_compute_dpll(crtc, crtc_state);
  6148. return 0;
  6149. }
  6150. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6151. struct intel_crtc_state *pipe_config)
  6152. {
  6153. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6154. uint32_t tmp;
  6155. if (INTEL_GEN(dev_priv) <= 3 &&
  6156. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6157. return;
  6158. tmp = I915_READ(PFIT_CONTROL);
  6159. if (!(tmp & PFIT_ENABLE))
  6160. return;
  6161. /* Check whether the pfit is attached to our pipe. */
  6162. if (INTEL_GEN(dev_priv) < 4) {
  6163. if (crtc->pipe != PIPE_B)
  6164. return;
  6165. } else {
  6166. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6167. return;
  6168. }
  6169. pipe_config->gmch_pfit.control = tmp;
  6170. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6171. }
  6172. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6173. struct intel_crtc_state *pipe_config)
  6174. {
  6175. struct drm_device *dev = crtc->base.dev;
  6176. struct drm_i915_private *dev_priv = to_i915(dev);
  6177. int pipe = pipe_config->cpu_transcoder;
  6178. struct dpll clock;
  6179. u32 mdiv;
  6180. int refclk = 100000;
  6181. /* In case of DSI, DPLL will not be used */
  6182. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6183. return;
  6184. mutex_lock(&dev_priv->sb_lock);
  6185. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6186. mutex_unlock(&dev_priv->sb_lock);
  6187. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6188. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6189. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6190. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6191. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6192. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6193. }
  6194. static void
  6195. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6196. struct intel_initial_plane_config *plane_config)
  6197. {
  6198. struct drm_device *dev = crtc->base.dev;
  6199. struct drm_i915_private *dev_priv = to_i915(dev);
  6200. u32 val, base, offset;
  6201. int pipe = crtc->pipe, plane = crtc->plane;
  6202. int fourcc, pixel_format;
  6203. unsigned int aligned_height;
  6204. struct drm_framebuffer *fb;
  6205. struct intel_framebuffer *intel_fb;
  6206. val = I915_READ(DSPCNTR(plane));
  6207. if (!(val & DISPLAY_PLANE_ENABLE))
  6208. return;
  6209. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6210. if (!intel_fb) {
  6211. DRM_DEBUG_KMS("failed to alloc fb\n");
  6212. return;
  6213. }
  6214. fb = &intel_fb->base;
  6215. fb->dev = dev;
  6216. if (INTEL_GEN(dev_priv) >= 4) {
  6217. if (val & DISPPLANE_TILED) {
  6218. plane_config->tiling = I915_TILING_X;
  6219. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6220. }
  6221. }
  6222. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6223. fourcc = i9xx_format_to_fourcc(pixel_format);
  6224. fb->format = drm_format_info(fourcc);
  6225. if (INTEL_GEN(dev_priv) >= 4) {
  6226. if (plane_config->tiling)
  6227. offset = I915_READ(DSPTILEOFF(plane));
  6228. else
  6229. offset = I915_READ(DSPLINOFF(plane));
  6230. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6231. } else {
  6232. base = I915_READ(DSPADDR(plane));
  6233. }
  6234. plane_config->base = base;
  6235. val = I915_READ(PIPESRC(pipe));
  6236. fb->width = ((val >> 16) & 0xfff) + 1;
  6237. fb->height = ((val >> 0) & 0xfff) + 1;
  6238. val = I915_READ(DSPSTRIDE(pipe));
  6239. fb->pitches[0] = val & 0xffffffc0;
  6240. aligned_height = intel_fb_align_height(dev_priv,
  6241. fb->height,
  6242. fb->format->format,
  6243. fb->modifier);
  6244. plane_config->size = fb->pitches[0] * aligned_height;
  6245. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6246. pipe_name(pipe), plane, fb->width, fb->height,
  6247. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6248. plane_config->size);
  6249. plane_config->fb = intel_fb;
  6250. }
  6251. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6252. struct intel_crtc_state *pipe_config)
  6253. {
  6254. struct drm_device *dev = crtc->base.dev;
  6255. struct drm_i915_private *dev_priv = to_i915(dev);
  6256. int pipe = pipe_config->cpu_transcoder;
  6257. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6258. struct dpll clock;
  6259. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6260. int refclk = 100000;
  6261. /* In case of DSI, DPLL will not be used */
  6262. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6263. return;
  6264. mutex_lock(&dev_priv->sb_lock);
  6265. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6266. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6267. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6268. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6269. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6270. mutex_unlock(&dev_priv->sb_lock);
  6271. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6272. clock.m2 = (pll_dw0 & 0xff) << 22;
  6273. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6274. clock.m2 |= pll_dw2 & 0x3fffff;
  6275. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6276. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6277. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6278. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6279. }
  6280. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6281. struct intel_crtc_state *pipe_config)
  6282. {
  6283. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6284. enum intel_display_power_domain power_domain;
  6285. uint32_t tmp;
  6286. bool ret;
  6287. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6288. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6289. return false;
  6290. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6291. pipe_config->shared_dpll = NULL;
  6292. ret = false;
  6293. tmp = I915_READ(PIPECONF(crtc->pipe));
  6294. if (!(tmp & PIPECONF_ENABLE))
  6295. goto out;
  6296. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6297. IS_CHERRYVIEW(dev_priv)) {
  6298. switch (tmp & PIPECONF_BPC_MASK) {
  6299. case PIPECONF_6BPC:
  6300. pipe_config->pipe_bpp = 18;
  6301. break;
  6302. case PIPECONF_8BPC:
  6303. pipe_config->pipe_bpp = 24;
  6304. break;
  6305. case PIPECONF_10BPC:
  6306. pipe_config->pipe_bpp = 30;
  6307. break;
  6308. default:
  6309. break;
  6310. }
  6311. }
  6312. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6313. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6314. pipe_config->limited_color_range = true;
  6315. if (INTEL_GEN(dev_priv) < 4)
  6316. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6317. intel_get_pipe_timings(crtc, pipe_config);
  6318. intel_get_pipe_src_size(crtc, pipe_config);
  6319. i9xx_get_pfit_config(crtc, pipe_config);
  6320. if (INTEL_GEN(dev_priv) >= 4) {
  6321. /* No way to read it out on pipes B and C */
  6322. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6323. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6324. else
  6325. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6326. pipe_config->pixel_multiplier =
  6327. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6328. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6329. pipe_config->dpll_hw_state.dpll_md = tmp;
  6330. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6331. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6332. tmp = I915_READ(DPLL(crtc->pipe));
  6333. pipe_config->pixel_multiplier =
  6334. ((tmp & SDVO_MULTIPLIER_MASK)
  6335. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6336. } else {
  6337. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6338. * port and will be fixed up in the encoder->get_config
  6339. * function. */
  6340. pipe_config->pixel_multiplier = 1;
  6341. }
  6342. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6343. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6344. /*
  6345. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6346. * on 830. Filter it out here so that we don't
  6347. * report errors due to that.
  6348. */
  6349. if (IS_I830(dev_priv))
  6350. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6351. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6352. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6353. } else {
  6354. /* Mask out read-only status bits. */
  6355. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6356. DPLL_PORTC_READY_MASK |
  6357. DPLL_PORTB_READY_MASK);
  6358. }
  6359. if (IS_CHERRYVIEW(dev_priv))
  6360. chv_crtc_clock_get(crtc, pipe_config);
  6361. else if (IS_VALLEYVIEW(dev_priv))
  6362. vlv_crtc_clock_get(crtc, pipe_config);
  6363. else
  6364. i9xx_crtc_clock_get(crtc, pipe_config);
  6365. /*
  6366. * Normally the dotclock is filled in by the encoder .get_config()
  6367. * but in case the pipe is enabled w/o any ports we need a sane
  6368. * default.
  6369. */
  6370. pipe_config->base.adjusted_mode.crtc_clock =
  6371. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6372. ret = true;
  6373. out:
  6374. intel_display_power_put(dev_priv, power_domain);
  6375. return ret;
  6376. }
  6377. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6378. {
  6379. struct intel_encoder *encoder;
  6380. int i;
  6381. u32 val, final;
  6382. bool has_lvds = false;
  6383. bool has_cpu_edp = false;
  6384. bool has_panel = false;
  6385. bool has_ck505 = false;
  6386. bool can_ssc = false;
  6387. bool using_ssc_source = false;
  6388. /* We need to take the global config into account */
  6389. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6390. switch (encoder->type) {
  6391. case INTEL_OUTPUT_LVDS:
  6392. has_panel = true;
  6393. has_lvds = true;
  6394. break;
  6395. case INTEL_OUTPUT_EDP:
  6396. has_panel = true;
  6397. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6398. has_cpu_edp = true;
  6399. break;
  6400. default:
  6401. break;
  6402. }
  6403. }
  6404. if (HAS_PCH_IBX(dev_priv)) {
  6405. has_ck505 = dev_priv->vbt.display_clock_mode;
  6406. can_ssc = has_ck505;
  6407. } else {
  6408. has_ck505 = false;
  6409. can_ssc = true;
  6410. }
  6411. /* Check if any DPLLs are using the SSC source */
  6412. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6413. u32 temp = I915_READ(PCH_DPLL(i));
  6414. if (!(temp & DPLL_VCO_ENABLE))
  6415. continue;
  6416. if ((temp & PLL_REF_INPUT_MASK) ==
  6417. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6418. using_ssc_source = true;
  6419. break;
  6420. }
  6421. }
  6422. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6423. has_panel, has_lvds, has_ck505, using_ssc_source);
  6424. /* Ironlake: try to setup display ref clock before DPLL
  6425. * enabling. This is only under driver's control after
  6426. * PCH B stepping, previous chipset stepping should be
  6427. * ignoring this setting.
  6428. */
  6429. val = I915_READ(PCH_DREF_CONTROL);
  6430. /* As we must carefully and slowly disable/enable each source in turn,
  6431. * compute the final state we want first and check if we need to
  6432. * make any changes at all.
  6433. */
  6434. final = val;
  6435. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6436. if (has_ck505)
  6437. final |= DREF_NONSPREAD_CK505_ENABLE;
  6438. else
  6439. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6440. final &= ~DREF_SSC_SOURCE_MASK;
  6441. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6442. final &= ~DREF_SSC1_ENABLE;
  6443. if (has_panel) {
  6444. final |= DREF_SSC_SOURCE_ENABLE;
  6445. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6446. final |= DREF_SSC1_ENABLE;
  6447. if (has_cpu_edp) {
  6448. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6449. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6450. else
  6451. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6452. } else
  6453. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6454. } else if (using_ssc_source) {
  6455. final |= DREF_SSC_SOURCE_ENABLE;
  6456. final |= DREF_SSC1_ENABLE;
  6457. }
  6458. if (final == val)
  6459. return;
  6460. /* Always enable nonspread source */
  6461. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6462. if (has_ck505)
  6463. val |= DREF_NONSPREAD_CK505_ENABLE;
  6464. else
  6465. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6466. if (has_panel) {
  6467. val &= ~DREF_SSC_SOURCE_MASK;
  6468. val |= DREF_SSC_SOURCE_ENABLE;
  6469. /* SSC must be turned on before enabling the CPU output */
  6470. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6471. DRM_DEBUG_KMS("Using SSC on panel\n");
  6472. val |= DREF_SSC1_ENABLE;
  6473. } else
  6474. val &= ~DREF_SSC1_ENABLE;
  6475. /* Get SSC going before enabling the outputs */
  6476. I915_WRITE(PCH_DREF_CONTROL, val);
  6477. POSTING_READ(PCH_DREF_CONTROL);
  6478. udelay(200);
  6479. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6480. /* Enable CPU source on CPU attached eDP */
  6481. if (has_cpu_edp) {
  6482. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6483. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6484. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6485. } else
  6486. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6487. } else
  6488. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6489. I915_WRITE(PCH_DREF_CONTROL, val);
  6490. POSTING_READ(PCH_DREF_CONTROL);
  6491. udelay(200);
  6492. } else {
  6493. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6494. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6495. /* Turn off CPU output */
  6496. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6497. I915_WRITE(PCH_DREF_CONTROL, val);
  6498. POSTING_READ(PCH_DREF_CONTROL);
  6499. udelay(200);
  6500. if (!using_ssc_source) {
  6501. DRM_DEBUG_KMS("Disabling SSC source\n");
  6502. /* Turn off the SSC source */
  6503. val &= ~DREF_SSC_SOURCE_MASK;
  6504. val |= DREF_SSC_SOURCE_DISABLE;
  6505. /* Turn off SSC1 */
  6506. val &= ~DREF_SSC1_ENABLE;
  6507. I915_WRITE(PCH_DREF_CONTROL, val);
  6508. POSTING_READ(PCH_DREF_CONTROL);
  6509. udelay(200);
  6510. }
  6511. }
  6512. BUG_ON(val != final);
  6513. }
  6514. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6515. {
  6516. uint32_t tmp;
  6517. tmp = I915_READ(SOUTH_CHICKEN2);
  6518. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6519. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6520. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6521. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6522. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6523. tmp = I915_READ(SOUTH_CHICKEN2);
  6524. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6525. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6526. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6527. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6528. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6529. }
  6530. /* WaMPhyProgramming:hsw */
  6531. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6532. {
  6533. uint32_t tmp;
  6534. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6535. tmp &= ~(0xFF << 24);
  6536. tmp |= (0x12 << 24);
  6537. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6538. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6539. tmp |= (1 << 11);
  6540. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6541. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6542. tmp |= (1 << 11);
  6543. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6544. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6545. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6546. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6547. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6548. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6549. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6550. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6551. tmp &= ~(7 << 13);
  6552. tmp |= (5 << 13);
  6553. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6554. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6555. tmp &= ~(7 << 13);
  6556. tmp |= (5 << 13);
  6557. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6558. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6559. tmp &= ~0xFF;
  6560. tmp |= 0x1C;
  6561. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6562. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6563. tmp &= ~0xFF;
  6564. tmp |= 0x1C;
  6565. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6566. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6567. tmp &= ~(0xFF << 16);
  6568. tmp |= (0x1C << 16);
  6569. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6570. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6571. tmp &= ~(0xFF << 16);
  6572. tmp |= (0x1C << 16);
  6573. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6574. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6575. tmp |= (1 << 27);
  6576. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6577. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6578. tmp |= (1 << 27);
  6579. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6580. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6581. tmp &= ~(0xF << 28);
  6582. tmp |= (4 << 28);
  6583. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6584. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6585. tmp &= ~(0xF << 28);
  6586. tmp |= (4 << 28);
  6587. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6588. }
  6589. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6590. * Programming" based on the parameters passed:
  6591. * - Sequence to enable CLKOUT_DP
  6592. * - Sequence to enable CLKOUT_DP without spread
  6593. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6594. */
  6595. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6596. bool with_spread, bool with_fdi)
  6597. {
  6598. uint32_t reg, tmp;
  6599. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6600. with_spread = true;
  6601. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6602. with_fdi, "LP PCH doesn't have FDI\n"))
  6603. with_fdi = false;
  6604. mutex_lock(&dev_priv->sb_lock);
  6605. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6606. tmp &= ~SBI_SSCCTL_DISABLE;
  6607. tmp |= SBI_SSCCTL_PATHALT;
  6608. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6609. udelay(24);
  6610. if (with_spread) {
  6611. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6612. tmp &= ~SBI_SSCCTL_PATHALT;
  6613. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6614. if (with_fdi) {
  6615. lpt_reset_fdi_mphy(dev_priv);
  6616. lpt_program_fdi_mphy(dev_priv);
  6617. }
  6618. }
  6619. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6620. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6621. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6622. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6623. mutex_unlock(&dev_priv->sb_lock);
  6624. }
  6625. /* Sequence to disable CLKOUT_DP */
  6626. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6627. {
  6628. uint32_t reg, tmp;
  6629. mutex_lock(&dev_priv->sb_lock);
  6630. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6631. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6632. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6633. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6634. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6635. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6636. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6637. tmp |= SBI_SSCCTL_PATHALT;
  6638. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6639. udelay(32);
  6640. }
  6641. tmp |= SBI_SSCCTL_DISABLE;
  6642. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6643. }
  6644. mutex_unlock(&dev_priv->sb_lock);
  6645. }
  6646. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6647. static const uint16_t sscdivintphase[] = {
  6648. [BEND_IDX( 50)] = 0x3B23,
  6649. [BEND_IDX( 45)] = 0x3B23,
  6650. [BEND_IDX( 40)] = 0x3C23,
  6651. [BEND_IDX( 35)] = 0x3C23,
  6652. [BEND_IDX( 30)] = 0x3D23,
  6653. [BEND_IDX( 25)] = 0x3D23,
  6654. [BEND_IDX( 20)] = 0x3E23,
  6655. [BEND_IDX( 15)] = 0x3E23,
  6656. [BEND_IDX( 10)] = 0x3F23,
  6657. [BEND_IDX( 5)] = 0x3F23,
  6658. [BEND_IDX( 0)] = 0x0025,
  6659. [BEND_IDX( -5)] = 0x0025,
  6660. [BEND_IDX(-10)] = 0x0125,
  6661. [BEND_IDX(-15)] = 0x0125,
  6662. [BEND_IDX(-20)] = 0x0225,
  6663. [BEND_IDX(-25)] = 0x0225,
  6664. [BEND_IDX(-30)] = 0x0325,
  6665. [BEND_IDX(-35)] = 0x0325,
  6666. [BEND_IDX(-40)] = 0x0425,
  6667. [BEND_IDX(-45)] = 0x0425,
  6668. [BEND_IDX(-50)] = 0x0525,
  6669. };
  6670. /*
  6671. * Bend CLKOUT_DP
  6672. * steps -50 to 50 inclusive, in steps of 5
  6673. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6674. * change in clock period = -(steps / 10) * 5.787 ps
  6675. */
  6676. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6677. {
  6678. uint32_t tmp;
  6679. int idx = BEND_IDX(steps);
  6680. if (WARN_ON(steps % 5 != 0))
  6681. return;
  6682. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6683. return;
  6684. mutex_lock(&dev_priv->sb_lock);
  6685. if (steps % 10 != 0)
  6686. tmp = 0xAAAAAAAB;
  6687. else
  6688. tmp = 0x00000000;
  6689. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6690. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6691. tmp &= 0xffff0000;
  6692. tmp |= sscdivintphase[idx];
  6693. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6694. mutex_unlock(&dev_priv->sb_lock);
  6695. }
  6696. #undef BEND_IDX
  6697. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6698. {
  6699. struct intel_encoder *encoder;
  6700. bool has_vga = false;
  6701. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6702. switch (encoder->type) {
  6703. case INTEL_OUTPUT_ANALOG:
  6704. has_vga = true;
  6705. break;
  6706. default:
  6707. break;
  6708. }
  6709. }
  6710. if (has_vga) {
  6711. lpt_bend_clkout_dp(dev_priv, 0);
  6712. lpt_enable_clkout_dp(dev_priv, true, true);
  6713. } else {
  6714. lpt_disable_clkout_dp(dev_priv);
  6715. }
  6716. }
  6717. /*
  6718. * Initialize reference clocks when the driver loads
  6719. */
  6720. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6721. {
  6722. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6723. ironlake_init_pch_refclk(dev_priv);
  6724. else if (HAS_PCH_LPT(dev_priv))
  6725. lpt_init_pch_refclk(dev_priv);
  6726. }
  6727. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6728. {
  6729. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6730. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6731. int pipe = intel_crtc->pipe;
  6732. uint32_t val;
  6733. val = 0;
  6734. switch (intel_crtc->config->pipe_bpp) {
  6735. case 18:
  6736. val |= PIPECONF_6BPC;
  6737. break;
  6738. case 24:
  6739. val |= PIPECONF_8BPC;
  6740. break;
  6741. case 30:
  6742. val |= PIPECONF_10BPC;
  6743. break;
  6744. case 36:
  6745. val |= PIPECONF_12BPC;
  6746. break;
  6747. default:
  6748. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6749. BUG();
  6750. }
  6751. if (intel_crtc->config->dither)
  6752. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6753. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6754. val |= PIPECONF_INTERLACED_ILK;
  6755. else
  6756. val |= PIPECONF_PROGRESSIVE;
  6757. if (intel_crtc->config->limited_color_range)
  6758. val |= PIPECONF_COLOR_RANGE_SELECT;
  6759. I915_WRITE(PIPECONF(pipe), val);
  6760. POSTING_READ(PIPECONF(pipe));
  6761. }
  6762. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6763. {
  6764. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6765. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6766. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6767. u32 val = 0;
  6768. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6769. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6770. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6771. val |= PIPECONF_INTERLACED_ILK;
  6772. else
  6773. val |= PIPECONF_PROGRESSIVE;
  6774. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6775. POSTING_READ(PIPECONF(cpu_transcoder));
  6776. }
  6777. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6778. {
  6779. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6780. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6781. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  6782. u32 val = 0;
  6783. switch (intel_crtc->config->pipe_bpp) {
  6784. case 18:
  6785. val |= PIPEMISC_DITHER_6_BPC;
  6786. break;
  6787. case 24:
  6788. val |= PIPEMISC_DITHER_8_BPC;
  6789. break;
  6790. case 30:
  6791. val |= PIPEMISC_DITHER_10_BPC;
  6792. break;
  6793. case 36:
  6794. val |= PIPEMISC_DITHER_12_BPC;
  6795. break;
  6796. default:
  6797. /* Case prevented by pipe_config_set_bpp. */
  6798. BUG();
  6799. }
  6800. if (intel_crtc->config->dither)
  6801. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6802. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6803. }
  6804. }
  6805. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6806. {
  6807. /*
  6808. * Account for spread spectrum to avoid
  6809. * oversubscribing the link. Max center spread
  6810. * is 2.5%; use 5% for safety's sake.
  6811. */
  6812. u32 bps = target_clock * bpp * 21 / 20;
  6813. return DIV_ROUND_UP(bps, link_bw * 8);
  6814. }
  6815. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6816. {
  6817. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6818. }
  6819. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6820. struct intel_crtc_state *crtc_state,
  6821. struct dpll *reduced_clock)
  6822. {
  6823. struct drm_crtc *crtc = &intel_crtc->base;
  6824. struct drm_device *dev = crtc->dev;
  6825. struct drm_i915_private *dev_priv = to_i915(dev);
  6826. u32 dpll, fp, fp2;
  6827. int factor;
  6828. /* Enable autotuning of the PLL clock (if permissible) */
  6829. factor = 21;
  6830. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6831. if ((intel_panel_use_ssc(dev_priv) &&
  6832. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6833. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6834. factor = 25;
  6835. } else if (crtc_state->sdvo_tv_clock)
  6836. factor = 20;
  6837. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6838. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6839. fp |= FP_CB_TUNE;
  6840. if (reduced_clock) {
  6841. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6842. if (reduced_clock->m < factor * reduced_clock->n)
  6843. fp2 |= FP_CB_TUNE;
  6844. } else {
  6845. fp2 = fp;
  6846. }
  6847. dpll = 0;
  6848. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6849. dpll |= DPLLB_MODE_LVDS;
  6850. else
  6851. dpll |= DPLLB_MODE_DAC_SERIAL;
  6852. dpll |= (crtc_state->pixel_multiplier - 1)
  6853. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6854. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6855. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6856. dpll |= DPLL_SDVO_HIGH_SPEED;
  6857. if (intel_crtc_has_dp_encoder(crtc_state))
  6858. dpll |= DPLL_SDVO_HIGH_SPEED;
  6859. /*
  6860. * The high speed IO clock is only really required for
  6861. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6862. * possible to share the DPLL between CRT and HDMI. Enabling
  6863. * the clock needlessly does no real harm, except use up a
  6864. * bit of power potentially.
  6865. *
  6866. * We'll limit this to IVB with 3 pipes, since it has only two
  6867. * DPLLs and so DPLL sharing is the only way to get three pipes
  6868. * driving PCH ports at the same time. On SNB we could do this,
  6869. * and potentially avoid enabling the second DPLL, but it's not
  6870. * clear if it''s a win or loss power wise. No point in doing
  6871. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  6872. */
  6873. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  6874. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  6875. dpll |= DPLL_SDVO_HIGH_SPEED;
  6876. /* compute bitmask from p1 value */
  6877. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6878. /* also FPA1 */
  6879. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6880. switch (crtc_state->dpll.p2) {
  6881. case 5:
  6882. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6883. break;
  6884. case 7:
  6885. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6886. break;
  6887. case 10:
  6888. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6889. break;
  6890. case 14:
  6891. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6892. break;
  6893. }
  6894. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6895. intel_panel_use_ssc(dev_priv))
  6896. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6897. else
  6898. dpll |= PLL_REF_INPUT_DREFCLK;
  6899. dpll |= DPLL_VCO_ENABLE;
  6900. crtc_state->dpll_hw_state.dpll = dpll;
  6901. crtc_state->dpll_hw_state.fp0 = fp;
  6902. crtc_state->dpll_hw_state.fp1 = fp2;
  6903. }
  6904. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6905. struct intel_crtc_state *crtc_state)
  6906. {
  6907. struct drm_device *dev = crtc->base.dev;
  6908. struct drm_i915_private *dev_priv = to_i915(dev);
  6909. struct dpll reduced_clock;
  6910. bool has_reduced_clock = false;
  6911. struct intel_shared_dpll *pll;
  6912. const struct intel_limit *limit;
  6913. int refclk = 120000;
  6914. memset(&crtc_state->dpll_hw_state, 0,
  6915. sizeof(crtc_state->dpll_hw_state));
  6916. crtc->lowfreq_avail = false;
  6917. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6918. if (!crtc_state->has_pch_encoder)
  6919. return 0;
  6920. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6921. if (intel_panel_use_ssc(dev_priv)) {
  6922. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6923. dev_priv->vbt.lvds_ssc_freq);
  6924. refclk = dev_priv->vbt.lvds_ssc_freq;
  6925. }
  6926. if (intel_is_dual_link_lvds(dev)) {
  6927. if (refclk == 100000)
  6928. limit = &intel_limits_ironlake_dual_lvds_100m;
  6929. else
  6930. limit = &intel_limits_ironlake_dual_lvds;
  6931. } else {
  6932. if (refclk == 100000)
  6933. limit = &intel_limits_ironlake_single_lvds_100m;
  6934. else
  6935. limit = &intel_limits_ironlake_single_lvds;
  6936. }
  6937. } else {
  6938. limit = &intel_limits_ironlake_dac;
  6939. }
  6940. if (!crtc_state->clock_set &&
  6941. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6942. refclk, NULL, &crtc_state->dpll)) {
  6943. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6944. return -EINVAL;
  6945. }
  6946. ironlake_compute_dpll(crtc, crtc_state,
  6947. has_reduced_clock ? &reduced_clock : NULL);
  6948. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  6949. if (pll == NULL) {
  6950. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6951. pipe_name(crtc->pipe));
  6952. return -EINVAL;
  6953. }
  6954. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6955. has_reduced_clock)
  6956. crtc->lowfreq_avail = true;
  6957. return 0;
  6958. }
  6959. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6960. struct intel_link_m_n *m_n)
  6961. {
  6962. struct drm_device *dev = crtc->base.dev;
  6963. struct drm_i915_private *dev_priv = to_i915(dev);
  6964. enum pipe pipe = crtc->pipe;
  6965. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6966. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6967. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6968. & ~TU_SIZE_MASK;
  6969. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6970. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6971. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6972. }
  6973. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6974. enum transcoder transcoder,
  6975. struct intel_link_m_n *m_n,
  6976. struct intel_link_m_n *m2_n2)
  6977. {
  6978. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6979. enum pipe pipe = crtc->pipe;
  6980. if (INTEL_GEN(dev_priv) >= 5) {
  6981. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6982. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6983. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6984. & ~TU_SIZE_MASK;
  6985. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6986. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6987. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6988. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6989. * gen < 8) and if DRRS is supported (to make sure the
  6990. * registers are not unnecessarily read).
  6991. */
  6992. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  6993. crtc->config->has_drrs) {
  6994. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6995. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6996. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6997. & ~TU_SIZE_MASK;
  6998. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6999. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7000. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7001. }
  7002. } else {
  7003. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7004. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7005. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7006. & ~TU_SIZE_MASK;
  7007. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7008. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7009. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7010. }
  7011. }
  7012. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7013. struct intel_crtc_state *pipe_config)
  7014. {
  7015. if (pipe_config->has_pch_encoder)
  7016. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7017. else
  7018. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7019. &pipe_config->dp_m_n,
  7020. &pipe_config->dp_m2_n2);
  7021. }
  7022. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7023. struct intel_crtc_state *pipe_config)
  7024. {
  7025. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7026. &pipe_config->fdi_m_n, NULL);
  7027. }
  7028. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7029. struct intel_crtc_state *pipe_config)
  7030. {
  7031. struct drm_device *dev = crtc->base.dev;
  7032. struct drm_i915_private *dev_priv = to_i915(dev);
  7033. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7034. uint32_t ps_ctrl = 0;
  7035. int id = -1;
  7036. int i;
  7037. /* find scaler attached to this pipe */
  7038. for (i = 0; i < crtc->num_scalers; i++) {
  7039. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7040. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7041. id = i;
  7042. pipe_config->pch_pfit.enabled = true;
  7043. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7044. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7045. break;
  7046. }
  7047. }
  7048. scaler_state->scaler_id = id;
  7049. if (id >= 0) {
  7050. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7051. } else {
  7052. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7053. }
  7054. }
  7055. static void
  7056. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7057. struct intel_initial_plane_config *plane_config)
  7058. {
  7059. struct drm_device *dev = crtc->base.dev;
  7060. struct drm_i915_private *dev_priv = to_i915(dev);
  7061. u32 val, base, offset, stride_mult, tiling;
  7062. int pipe = crtc->pipe;
  7063. int fourcc, pixel_format;
  7064. unsigned int aligned_height;
  7065. struct drm_framebuffer *fb;
  7066. struct intel_framebuffer *intel_fb;
  7067. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7068. if (!intel_fb) {
  7069. DRM_DEBUG_KMS("failed to alloc fb\n");
  7070. return;
  7071. }
  7072. fb = &intel_fb->base;
  7073. fb->dev = dev;
  7074. val = I915_READ(PLANE_CTL(pipe, 0));
  7075. if (!(val & PLANE_CTL_ENABLE))
  7076. goto error;
  7077. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7078. fourcc = skl_format_to_fourcc(pixel_format,
  7079. val & PLANE_CTL_ORDER_RGBX,
  7080. val & PLANE_CTL_ALPHA_MASK);
  7081. fb->format = drm_format_info(fourcc);
  7082. tiling = val & PLANE_CTL_TILED_MASK;
  7083. switch (tiling) {
  7084. case PLANE_CTL_TILED_LINEAR:
  7085. fb->modifier = DRM_FORMAT_MOD_NONE;
  7086. break;
  7087. case PLANE_CTL_TILED_X:
  7088. plane_config->tiling = I915_TILING_X;
  7089. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7090. break;
  7091. case PLANE_CTL_TILED_Y:
  7092. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7093. break;
  7094. case PLANE_CTL_TILED_YF:
  7095. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7096. break;
  7097. default:
  7098. MISSING_CASE(tiling);
  7099. goto error;
  7100. }
  7101. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7102. plane_config->base = base;
  7103. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7104. val = I915_READ(PLANE_SIZE(pipe, 0));
  7105. fb->height = ((val >> 16) & 0xfff) + 1;
  7106. fb->width = ((val >> 0) & 0x1fff) + 1;
  7107. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7108. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
  7109. fb->format->format);
  7110. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7111. aligned_height = intel_fb_align_height(dev_priv,
  7112. fb->height,
  7113. fb->format->format,
  7114. fb->modifier);
  7115. plane_config->size = fb->pitches[0] * aligned_height;
  7116. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7117. pipe_name(pipe), fb->width, fb->height,
  7118. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7119. plane_config->size);
  7120. plane_config->fb = intel_fb;
  7121. return;
  7122. error:
  7123. kfree(intel_fb);
  7124. }
  7125. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7126. struct intel_crtc_state *pipe_config)
  7127. {
  7128. struct drm_device *dev = crtc->base.dev;
  7129. struct drm_i915_private *dev_priv = to_i915(dev);
  7130. uint32_t tmp;
  7131. tmp = I915_READ(PF_CTL(crtc->pipe));
  7132. if (tmp & PF_ENABLE) {
  7133. pipe_config->pch_pfit.enabled = true;
  7134. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7135. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7136. /* We currently do not free assignements of panel fitters on
  7137. * ivb/hsw (since we don't use the higher upscaling modes which
  7138. * differentiates them) so just WARN about this case for now. */
  7139. if (IS_GEN7(dev_priv)) {
  7140. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7141. PF_PIPE_SEL_IVB(crtc->pipe));
  7142. }
  7143. }
  7144. }
  7145. static void
  7146. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7147. struct intel_initial_plane_config *plane_config)
  7148. {
  7149. struct drm_device *dev = crtc->base.dev;
  7150. struct drm_i915_private *dev_priv = to_i915(dev);
  7151. u32 val, base, offset;
  7152. int pipe = crtc->pipe;
  7153. int fourcc, pixel_format;
  7154. unsigned int aligned_height;
  7155. struct drm_framebuffer *fb;
  7156. struct intel_framebuffer *intel_fb;
  7157. val = I915_READ(DSPCNTR(pipe));
  7158. if (!(val & DISPLAY_PLANE_ENABLE))
  7159. return;
  7160. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7161. if (!intel_fb) {
  7162. DRM_DEBUG_KMS("failed to alloc fb\n");
  7163. return;
  7164. }
  7165. fb = &intel_fb->base;
  7166. fb->dev = dev;
  7167. if (INTEL_GEN(dev_priv) >= 4) {
  7168. if (val & DISPPLANE_TILED) {
  7169. plane_config->tiling = I915_TILING_X;
  7170. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7171. }
  7172. }
  7173. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7174. fourcc = i9xx_format_to_fourcc(pixel_format);
  7175. fb->format = drm_format_info(fourcc);
  7176. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7177. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  7178. offset = I915_READ(DSPOFFSET(pipe));
  7179. } else {
  7180. if (plane_config->tiling)
  7181. offset = I915_READ(DSPTILEOFF(pipe));
  7182. else
  7183. offset = I915_READ(DSPLINOFF(pipe));
  7184. }
  7185. plane_config->base = base;
  7186. val = I915_READ(PIPESRC(pipe));
  7187. fb->width = ((val >> 16) & 0xfff) + 1;
  7188. fb->height = ((val >> 0) & 0xfff) + 1;
  7189. val = I915_READ(DSPSTRIDE(pipe));
  7190. fb->pitches[0] = val & 0xffffffc0;
  7191. aligned_height = intel_fb_align_height(dev_priv,
  7192. fb->height,
  7193. fb->format->format,
  7194. fb->modifier);
  7195. plane_config->size = fb->pitches[0] * aligned_height;
  7196. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7197. pipe_name(pipe), fb->width, fb->height,
  7198. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7199. plane_config->size);
  7200. plane_config->fb = intel_fb;
  7201. }
  7202. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7203. struct intel_crtc_state *pipe_config)
  7204. {
  7205. struct drm_device *dev = crtc->base.dev;
  7206. struct drm_i915_private *dev_priv = to_i915(dev);
  7207. enum intel_display_power_domain power_domain;
  7208. uint32_t tmp;
  7209. bool ret;
  7210. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7211. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7212. return false;
  7213. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7214. pipe_config->shared_dpll = NULL;
  7215. ret = false;
  7216. tmp = I915_READ(PIPECONF(crtc->pipe));
  7217. if (!(tmp & PIPECONF_ENABLE))
  7218. goto out;
  7219. switch (tmp & PIPECONF_BPC_MASK) {
  7220. case PIPECONF_6BPC:
  7221. pipe_config->pipe_bpp = 18;
  7222. break;
  7223. case PIPECONF_8BPC:
  7224. pipe_config->pipe_bpp = 24;
  7225. break;
  7226. case PIPECONF_10BPC:
  7227. pipe_config->pipe_bpp = 30;
  7228. break;
  7229. case PIPECONF_12BPC:
  7230. pipe_config->pipe_bpp = 36;
  7231. break;
  7232. default:
  7233. break;
  7234. }
  7235. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7236. pipe_config->limited_color_range = true;
  7237. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7238. struct intel_shared_dpll *pll;
  7239. enum intel_dpll_id pll_id;
  7240. pipe_config->has_pch_encoder = true;
  7241. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7242. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7243. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7244. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7245. if (HAS_PCH_IBX(dev_priv)) {
  7246. /*
  7247. * The pipe->pch transcoder and pch transcoder->pll
  7248. * mapping is fixed.
  7249. */
  7250. pll_id = (enum intel_dpll_id) crtc->pipe;
  7251. } else {
  7252. tmp = I915_READ(PCH_DPLL_SEL);
  7253. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7254. pll_id = DPLL_ID_PCH_PLL_B;
  7255. else
  7256. pll_id= DPLL_ID_PCH_PLL_A;
  7257. }
  7258. pipe_config->shared_dpll =
  7259. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7260. pll = pipe_config->shared_dpll;
  7261. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7262. &pipe_config->dpll_hw_state));
  7263. tmp = pipe_config->dpll_hw_state.dpll;
  7264. pipe_config->pixel_multiplier =
  7265. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7266. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7267. ironlake_pch_clock_get(crtc, pipe_config);
  7268. } else {
  7269. pipe_config->pixel_multiplier = 1;
  7270. }
  7271. intel_get_pipe_timings(crtc, pipe_config);
  7272. intel_get_pipe_src_size(crtc, pipe_config);
  7273. ironlake_get_pfit_config(crtc, pipe_config);
  7274. ret = true;
  7275. out:
  7276. intel_display_power_put(dev_priv, power_domain);
  7277. return ret;
  7278. }
  7279. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7280. {
  7281. struct drm_device *dev = &dev_priv->drm;
  7282. struct intel_crtc *crtc;
  7283. for_each_intel_crtc(dev, crtc)
  7284. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7285. pipe_name(crtc->pipe));
  7286. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7287. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7288. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7289. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7290. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7291. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7292. "CPU PWM1 enabled\n");
  7293. if (IS_HASWELL(dev_priv))
  7294. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7295. "CPU PWM2 enabled\n");
  7296. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7297. "PCH PWM1 enabled\n");
  7298. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7299. "Utility pin enabled\n");
  7300. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7301. /*
  7302. * In theory we can still leave IRQs enabled, as long as only the HPD
  7303. * interrupts remain enabled. We used to check for that, but since it's
  7304. * gen-specific and since we only disable LCPLL after we fully disable
  7305. * the interrupts, the check below should be enough.
  7306. */
  7307. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7308. }
  7309. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7310. {
  7311. if (IS_HASWELL(dev_priv))
  7312. return I915_READ(D_COMP_HSW);
  7313. else
  7314. return I915_READ(D_COMP_BDW);
  7315. }
  7316. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7317. {
  7318. if (IS_HASWELL(dev_priv)) {
  7319. mutex_lock(&dev_priv->rps.hw_lock);
  7320. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7321. val))
  7322. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7323. mutex_unlock(&dev_priv->rps.hw_lock);
  7324. } else {
  7325. I915_WRITE(D_COMP_BDW, val);
  7326. POSTING_READ(D_COMP_BDW);
  7327. }
  7328. }
  7329. /*
  7330. * This function implements pieces of two sequences from BSpec:
  7331. * - Sequence for display software to disable LCPLL
  7332. * - Sequence for display software to allow package C8+
  7333. * The steps implemented here are just the steps that actually touch the LCPLL
  7334. * register. Callers should take care of disabling all the display engine
  7335. * functions, doing the mode unset, fixing interrupts, etc.
  7336. */
  7337. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7338. bool switch_to_fclk, bool allow_power_down)
  7339. {
  7340. uint32_t val;
  7341. assert_can_disable_lcpll(dev_priv);
  7342. val = I915_READ(LCPLL_CTL);
  7343. if (switch_to_fclk) {
  7344. val |= LCPLL_CD_SOURCE_FCLK;
  7345. I915_WRITE(LCPLL_CTL, val);
  7346. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7347. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7348. DRM_ERROR("Switching to FCLK failed\n");
  7349. val = I915_READ(LCPLL_CTL);
  7350. }
  7351. val |= LCPLL_PLL_DISABLE;
  7352. I915_WRITE(LCPLL_CTL, val);
  7353. POSTING_READ(LCPLL_CTL);
  7354. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7355. DRM_ERROR("LCPLL still locked\n");
  7356. val = hsw_read_dcomp(dev_priv);
  7357. val |= D_COMP_COMP_DISABLE;
  7358. hsw_write_dcomp(dev_priv, val);
  7359. ndelay(100);
  7360. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7361. 1))
  7362. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7363. if (allow_power_down) {
  7364. val = I915_READ(LCPLL_CTL);
  7365. val |= LCPLL_POWER_DOWN_ALLOW;
  7366. I915_WRITE(LCPLL_CTL, val);
  7367. POSTING_READ(LCPLL_CTL);
  7368. }
  7369. }
  7370. /*
  7371. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7372. * source.
  7373. */
  7374. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7375. {
  7376. uint32_t val;
  7377. val = I915_READ(LCPLL_CTL);
  7378. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7379. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7380. return;
  7381. /*
  7382. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7383. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7384. */
  7385. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7386. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7387. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7388. I915_WRITE(LCPLL_CTL, val);
  7389. POSTING_READ(LCPLL_CTL);
  7390. }
  7391. val = hsw_read_dcomp(dev_priv);
  7392. val |= D_COMP_COMP_FORCE;
  7393. val &= ~D_COMP_COMP_DISABLE;
  7394. hsw_write_dcomp(dev_priv, val);
  7395. val = I915_READ(LCPLL_CTL);
  7396. val &= ~LCPLL_PLL_DISABLE;
  7397. I915_WRITE(LCPLL_CTL, val);
  7398. if (intel_wait_for_register(dev_priv,
  7399. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7400. 5))
  7401. DRM_ERROR("LCPLL not locked yet\n");
  7402. if (val & LCPLL_CD_SOURCE_FCLK) {
  7403. val = I915_READ(LCPLL_CTL);
  7404. val &= ~LCPLL_CD_SOURCE_FCLK;
  7405. I915_WRITE(LCPLL_CTL, val);
  7406. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7407. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7408. DRM_ERROR("Switching back to LCPLL failed\n");
  7409. }
  7410. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7411. intel_update_cdclk(dev_priv);
  7412. }
  7413. /*
  7414. * Package states C8 and deeper are really deep PC states that can only be
  7415. * reached when all the devices on the system allow it, so even if the graphics
  7416. * device allows PC8+, it doesn't mean the system will actually get to these
  7417. * states. Our driver only allows PC8+ when going into runtime PM.
  7418. *
  7419. * The requirements for PC8+ are that all the outputs are disabled, the power
  7420. * well is disabled and most interrupts are disabled, and these are also
  7421. * requirements for runtime PM. When these conditions are met, we manually do
  7422. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7423. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7424. * hang the machine.
  7425. *
  7426. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7427. * the state of some registers, so when we come back from PC8+ we need to
  7428. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7429. * need to take care of the registers kept by RC6. Notice that this happens even
  7430. * if we don't put the device in PCI D3 state (which is what currently happens
  7431. * because of the runtime PM support).
  7432. *
  7433. * For more, read "Display Sequences for Package C8" on the hardware
  7434. * documentation.
  7435. */
  7436. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7437. {
  7438. uint32_t val;
  7439. DRM_DEBUG_KMS("Enabling package C8+\n");
  7440. if (HAS_PCH_LPT_LP(dev_priv)) {
  7441. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7442. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7443. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7444. }
  7445. lpt_disable_clkout_dp(dev_priv);
  7446. hsw_disable_lcpll(dev_priv, true, true);
  7447. }
  7448. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7449. {
  7450. uint32_t val;
  7451. DRM_DEBUG_KMS("Disabling package C8+\n");
  7452. hsw_restore_lcpll(dev_priv);
  7453. lpt_init_pch_refclk(dev_priv);
  7454. if (HAS_PCH_LPT_LP(dev_priv)) {
  7455. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7456. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7457. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7458. }
  7459. }
  7460. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7461. struct intel_crtc_state *crtc_state)
  7462. {
  7463. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7464. if (!intel_ddi_pll_select(crtc, crtc_state))
  7465. return -EINVAL;
  7466. }
  7467. crtc->lowfreq_avail = false;
  7468. return 0;
  7469. }
  7470. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7471. enum port port,
  7472. struct intel_crtc_state *pipe_config)
  7473. {
  7474. enum intel_dpll_id id;
  7475. switch (port) {
  7476. case PORT_A:
  7477. id = DPLL_ID_SKL_DPLL0;
  7478. break;
  7479. case PORT_B:
  7480. id = DPLL_ID_SKL_DPLL1;
  7481. break;
  7482. case PORT_C:
  7483. id = DPLL_ID_SKL_DPLL2;
  7484. break;
  7485. default:
  7486. DRM_ERROR("Incorrect port type\n");
  7487. return;
  7488. }
  7489. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7490. }
  7491. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7492. enum port port,
  7493. struct intel_crtc_state *pipe_config)
  7494. {
  7495. enum intel_dpll_id id;
  7496. u32 temp;
  7497. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7498. id = temp >> (port * 3 + 1);
  7499. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7500. return;
  7501. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7502. }
  7503. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7504. enum port port,
  7505. struct intel_crtc_state *pipe_config)
  7506. {
  7507. enum intel_dpll_id id;
  7508. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7509. switch (ddi_pll_sel) {
  7510. case PORT_CLK_SEL_WRPLL1:
  7511. id = DPLL_ID_WRPLL1;
  7512. break;
  7513. case PORT_CLK_SEL_WRPLL2:
  7514. id = DPLL_ID_WRPLL2;
  7515. break;
  7516. case PORT_CLK_SEL_SPLL:
  7517. id = DPLL_ID_SPLL;
  7518. break;
  7519. case PORT_CLK_SEL_LCPLL_810:
  7520. id = DPLL_ID_LCPLL_810;
  7521. break;
  7522. case PORT_CLK_SEL_LCPLL_1350:
  7523. id = DPLL_ID_LCPLL_1350;
  7524. break;
  7525. case PORT_CLK_SEL_LCPLL_2700:
  7526. id = DPLL_ID_LCPLL_2700;
  7527. break;
  7528. default:
  7529. MISSING_CASE(ddi_pll_sel);
  7530. /* fall through */
  7531. case PORT_CLK_SEL_NONE:
  7532. return;
  7533. }
  7534. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7535. }
  7536. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7537. struct intel_crtc_state *pipe_config,
  7538. u64 *power_domain_mask)
  7539. {
  7540. struct drm_device *dev = crtc->base.dev;
  7541. struct drm_i915_private *dev_priv = to_i915(dev);
  7542. enum intel_display_power_domain power_domain;
  7543. u32 tmp;
  7544. /*
  7545. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7546. * transcoder handled below.
  7547. */
  7548. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7549. /*
  7550. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7551. * consistency and less surprising code; it's in always on power).
  7552. */
  7553. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7554. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7555. enum pipe trans_edp_pipe;
  7556. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7557. default:
  7558. WARN(1, "unknown pipe linked to edp transcoder\n");
  7559. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7560. case TRANS_DDI_EDP_INPUT_A_ON:
  7561. trans_edp_pipe = PIPE_A;
  7562. break;
  7563. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7564. trans_edp_pipe = PIPE_B;
  7565. break;
  7566. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7567. trans_edp_pipe = PIPE_C;
  7568. break;
  7569. }
  7570. if (trans_edp_pipe == crtc->pipe)
  7571. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7572. }
  7573. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7574. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7575. return false;
  7576. *power_domain_mask |= BIT_ULL(power_domain);
  7577. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7578. return tmp & PIPECONF_ENABLE;
  7579. }
  7580. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7581. struct intel_crtc_state *pipe_config,
  7582. u64 *power_domain_mask)
  7583. {
  7584. struct drm_device *dev = crtc->base.dev;
  7585. struct drm_i915_private *dev_priv = to_i915(dev);
  7586. enum intel_display_power_domain power_domain;
  7587. enum port port;
  7588. enum transcoder cpu_transcoder;
  7589. u32 tmp;
  7590. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7591. if (port == PORT_A)
  7592. cpu_transcoder = TRANSCODER_DSI_A;
  7593. else
  7594. cpu_transcoder = TRANSCODER_DSI_C;
  7595. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7596. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7597. continue;
  7598. *power_domain_mask |= BIT_ULL(power_domain);
  7599. /*
  7600. * The PLL needs to be enabled with a valid divider
  7601. * configuration, otherwise accessing DSI registers will hang
  7602. * the machine. See BSpec North Display Engine
  7603. * registers/MIPI[BXT]. We can break out here early, since we
  7604. * need the same DSI PLL to be enabled for both DSI ports.
  7605. */
  7606. if (!intel_dsi_pll_is_enabled(dev_priv))
  7607. break;
  7608. /* XXX: this works for video mode only */
  7609. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7610. if (!(tmp & DPI_ENABLE))
  7611. continue;
  7612. tmp = I915_READ(MIPI_CTRL(port));
  7613. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7614. continue;
  7615. pipe_config->cpu_transcoder = cpu_transcoder;
  7616. break;
  7617. }
  7618. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7619. }
  7620. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7621. struct intel_crtc_state *pipe_config)
  7622. {
  7623. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7624. struct intel_shared_dpll *pll;
  7625. enum port port;
  7626. uint32_t tmp;
  7627. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7628. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7629. if (IS_GEN9_BC(dev_priv))
  7630. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7631. else if (IS_GEN9_LP(dev_priv))
  7632. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7633. else
  7634. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7635. pll = pipe_config->shared_dpll;
  7636. if (pll) {
  7637. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7638. &pipe_config->dpll_hw_state));
  7639. }
  7640. /*
  7641. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7642. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7643. * the PCH transcoder is on.
  7644. */
  7645. if (INTEL_GEN(dev_priv) < 9 &&
  7646. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7647. pipe_config->has_pch_encoder = true;
  7648. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7649. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7650. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7651. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7652. }
  7653. }
  7654. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7655. struct intel_crtc_state *pipe_config)
  7656. {
  7657. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7658. enum intel_display_power_domain power_domain;
  7659. u64 power_domain_mask;
  7660. bool active;
  7661. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7662. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7663. return false;
  7664. power_domain_mask = BIT_ULL(power_domain);
  7665. pipe_config->shared_dpll = NULL;
  7666. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7667. if (IS_GEN9_LP(dev_priv) &&
  7668. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7669. WARN_ON(active);
  7670. active = true;
  7671. }
  7672. if (!active)
  7673. goto out;
  7674. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7675. haswell_get_ddi_port_state(crtc, pipe_config);
  7676. intel_get_pipe_timings(crtc, pipe_config);
  7677. }
  7678. intel_get_pipe_src_size(crtc, pipe_config);
  7679. pipe_config->gamma_mode =
  7680. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7681. if (INTEL_GEN(dev_priv) >= 9) {
  7682. intel_crtc_init_scalers(crtc, pipe_config);
  7683. pipe_config->scaler_state.scaler_id = -1;
  7684. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7685. }
  7686. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7687. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7688. power_domain_mask |= BIT_ULL(power_domain);
  7689. if (INTEL_GEN(dev_priv) >= 9)
  7690. skylake_get_pfit_config(crtc, pipe_config);
  7691. else
  7692. ironlake_get_pfit_config(crtc, pipe_config);
  7693. }
  7694. if (IS_HASWELL(dev_priv))
  7695. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7696. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7697. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7698. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7699. pipe_config->pixel_multiplier =
  7700. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7701. } else {
  7702. pipe_config->pixel_multiplier = 1;
  7703. }
  7704. out:
  7705. for_each_power_domain(power_domain, power_domain_mask)
  7706. intel_display_power_put(dev_priv, power_domain);
  7707. return active;
  7708. }
  7709. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  7710. const struct intel_plane_state *plane_state)
  7711. {
  7712. struct drm_device *dev = crtc->dev;
  7713. struct drm_i915_private *dev_priv = to_i915(dev);
  7714. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7715. uint32_t cntl = 0, size = 0;
  7716. if (plane_state && plane_state->base.visible) {
  7717. unsigned int width = plane_state->base.crtc_w;
  7718. unsigned int height = plane_state->base.crtc_h;
  7719. unsigned int stride = roundup_pow_of_two(width) * 4;
  7720. switch (stride) {
  7721. default:
  7722. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  7723. width, stride);
  7724. stride = 256;
  7725. /* fallthrough */
  7726. case 256:
  7727. case 512:
  7728. case 1024:
  7729. case 2048:
  7730. break;
  7731. }
  7732. cntl |= CURSOR_ENABLE |
  7733. CURSOR_GAMMA_ENABLE |
  7734. CURSOR_FORMAT_ARGB |
  7735. CURSOR_STRIDE(stride);
  7736. size = (height << 12) | width;
  7737. }
  7738. if (intel_crtc->cursor_cntl != 0 &&
  7739. (intel_crtc->cursor_base != base ||
  7740. intel_crtc->cursor_size != size ||
  7741. intel_crtc->cursor_cntl != cntl)) {
  7742. /* On these chipsets we can only modify the base/size/stride
  7743. * whilst the cursor is disabled.
  7744. */
  7745. I915_WRITE(CURCNTR(PIPE_A), 0);
  7746. POSTING_READ(CURCNTR(PIPE_A));
  7747. intel_crtc->cursor_cntl = 0;
  7748. }
  7749. if (intel_crtc->cursor_base != base) {
  7750. I915_WRITE(CURBASE(PIPE_A), base);
  7751. intel_crtc->cursor_base = base;
  7752. }
  7753. if (intel_crtc->cursor_size != size) {
  7754. I915_WRITE(CURSIZE, size);
  7755. intel_crtc->cursor_size = size;
  7756. }
  7757. if (intel_crtc->cursor_cntl != cntl) {
  7758. I915_WRITE(CURCNTR(PIPE_A), cntl);
  7759. POSTING_READ(CURCNTR(PIPE_A));
  7760. intel_crtc->cursor_cntl = cntl;
  7761. }
  7762. }
  7763. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  7764. const struct intel_plane_state *plane_state)
  7765. {
  7766. struct drm_device *dev = crtc->dev;
  7767. struct drm_i915_private *dev_priv = to_i915(dev);
  7768. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7769. int pipe = intel_crtc->pipe;
  7770. uint32_t cntl = 0;
  7771. if (plane_state && plane_state->base.visible) {
  7772. cntl = MCURSOR_GAMMA_ENABLE;
  7773. switch (plane_state->base.crtc_w) {
  7774. case 64:
  7775. cntl |= CURSOR_MODE_64_ARGB_AX;
  7776. break;
  7777. case 128:
  7778. cntl |= CURSOR_MODE_128_ARGB_AX;
  7779. break;
  7780. case 256:
  7781. cntl |= CURSOR_MODE_256_ARGB_AX;
  7782. break;
  7783. default:
  7784. MISSING_CASE(plane_state->base.crtc_w);
  7785. return;
  7786. }
  7787. cntl |= pipe << 28; /* Connect to correct pipe */
  7788. if (HAS_DDI(dev_priv))
  7789. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7790. if (plane_state->base.rotation & DRM_ROTATE_180)
  7791. cntl |= CURSOR_ROTATE_180;
  7792. }
  7793. if (intel_crtc->cursor_cntl != cntl) {
  7794. I915_WRITE(CURCNTR(pipe), cntl);
  7795. POSTING_READ(CURCNTR(pipe));
  7796. intel_crtc->cursor_cntl = cntl;
  7797. }
  7798. /* and commit changes on next vblank */
  7799. I915_WRITE(CURBASE(pipe), base);
  7800. POSTING_READ(CURBASE(pipe));
  7801. intel_crtc->cursor_base = base;
  7802. }
  7803. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7804. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7805. const struct intel_plane_state *plane_state)
  7806. {
  7807. struct drm_device *dev = crtc->dev;
  7808. struct drm_i915_private *dev_priv = to_i915(dev);
  7809. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7810. int pipe = intel_crtc->pipe;
  7811. u32 base = intel_crtc->cursor_addr;
  7812. u32 pos = 0;
  7813. if (plane_state) {
  7814. int x = plane_state->base.crtc_x;
  7815. int y = plane_state->base.crtc_y;
  7816. if (x < 0) {
  7817. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7818. x = -x;
  7819. }
  7820. pos |= x << CURSOR_X_SHIFT;
  7821. if (y < 0) {
  7822. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7823. y = -y;
  7824. }
  7825. pos |= y << CURSOR_Y_SHIFT;
  7826. /* ILK+ do this automagically */
  7827. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7828. plane_state->base.rotation & DRM_ROTATE_180) {
  7829. base += (plane_state->base.crtc_h *
  7830. plane_state->base.crtc_w - 1) * 4;
  7831. }
  7832. }
  7833. I915_WRITE(CURPOS(pipe), pos);
  7834. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  7835. i845_update_cursor(crtc, base, plane_state);
  7836. else
  7837. i9xx_update_cursor(crtc, base, plane_state);
  7838. }
  7839. static bool cursor_size_ok(struct drm_i915_private *dev_priv,
  7840. uint32_t width, uint32_t height)
  7841. {
  7842. if (width == 0 || height == 0)
  7843. return false;
  7844. /*
  7845. * 845g/865g are special in that they are only limited by
  7846. * the width of their cursors, the height is arbitrary up to
  7847. * the precision of the register. Everything else requires
  7848. * square cursors, limited to a few power-of-two sizes.
  7849. */
  7850. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  7851. if ((width & 63) != 0)
  7852. return false;
  7853. if (width > (IS_I845G(dev_priv) ? 64 : 512))
  7854. return false;
  7855. if (height > 1023)
  7856. return false;
  7857. } else {
  7858. switch (width | height) {
  7859. case 256:
  7860. case 128:
  7861. if (IS_GEN2(dev_priv))
  7862. return false;
  7863. case 64:
  7864. break;
  7865. default:
  7866. return false;
  7867. }
  7868. }
  7869. return true;
  7870. }
  7871. /* VESA 640x480x72Hz mode to set on the pipe */
  7872. static struct drm_display_mode load_detect_mode = {
  7873. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7874. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7875. };
  7876. struct drm_framebuffer *
  7877. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  7878. struct drm_mode_fb_cmd2 *mode_cmd)
  7879. {
  7880. struct intel_framebuffer *intel_fb;
  7881. int ret;
  7882. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7883. if (!intel_fb)
  7884. return ERR_PTR(-ENOMEM);
  7885. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  7886. if (ret)
  7887. goto err;
  7888. return &intel_fb->base;
  7889. err:
  7890. kfree(intel_fb);
  7891. return ERR_PTR(ret);
  7892. }
  7893. static u32
  7894. intel_framebuffer_pitch_for_width(int width, int bpp)
  7895. {
  7896. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7897. return ALIGN(pitch, 64);
  7898. }
  7899. static u32
  7900. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7901. {
  7902. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7903. return PAGE_ALIGN(pitch * mode->vdisplay);
  7904. }
  7905. static struct drm_framebuffer *
  7906. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7907. struct drm_display_mode *mode,
  7908. int depth, int bpp)
  7909. {
  7910. struct drm_framebuffer *fb;
  7911. struct drm_i915_gem_object *obj;
  7912. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7913. obj = i915_gem_object_create(to_i915(dev),
  7914. intel_framebuffer_size_for_mode(mode, bpp));
  7915. if (IS_ERR(obj))
  7916. return ERR_CAST(obj);
  7917. mode_cmd.width = mode->hdisplay;
  7918. mode_cmd.height = mode->vdisplay;
  7919. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7920. bpp);
  7921. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7922. fb = intel_framebuffer_create(obj, &mode_cmd);
  7923. if (IS_ERR(fb))
  7924. i915_gem_object_put(obj);
  7925. return fb;
  7926. }
  7927. static struct drm_framebuffer *
  7928. mode_fits_in_fbdev(struct drm_device *dev,
  7929. struct drm_display_mode *mode)
  7930. {
  7931. #ifdef CONFIG_DRM_FBDEV_EMULATION
  7932. struct drm_i915_private *dev_priv = to_i915(dev);
  7933. struct drm_i915_gem_object *obj;
  7934. struct drm_framebuffer *fb;
  7935. if (!dev_priv->fbdev)
  7936. return NULL;
  7937. if (!dev_priv->fbdev->fb)
  7938. return NULL;
  7939. obj = dev_priv->fbdev->fb->obj;
  7940. BUG_ON(!obj);
  7941. fb = &dev_priv->fbdev->fb->base;
  7942. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7943. fb->format->cpp[0] * 8))
  7944. return NULL;
  7945. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7946. return NULL;
  7947. drm_framebuffer_reference(fb);
  7948. return fb;
  7949. #else
  7950. return NULL;
  7951. #endif
  7952. }
  7953. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  7954. struct drm_crtc *crtc,
  7955. struct drm_display_mode *mode,
  7956. struct drm_framebuffer *fb,
  7957. int x, int y)
  7958. {
  7959. struct drm_plane_state *plane_state;
  7960. int hdisplay, vdisplay;
  7961. int ret;
  7962. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  7963. if (IS_ERR(plane_state))
  7964. return PTR_ERR(plane_state);
  7965. if (mode)
  7966. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  7967. else
  7968. hdisplay = vdisplay = 0;
  7969. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  7970. if (ret)
  7971. return ret;
  7972. drm_atomic_set_fb_for_plane(plane_state, fb);
  7973. plane_state->crtc_x = 0;
  7974. plane_state->crtc_y = 0;
  7975. plane_state->crtc_w = hdisplay;
  7976. plane_state->crtc_h = vdisplay;
  7977. plane_state->src_x = x << 16;
  7978. plane_state->src_y = y << 16;
  7979. plane_state->src_w = hdisplay << 16;
  7980. plane_state->src_h = vdisplay << 16;
  7981. return 0;
  7982. }
  7983. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7984. struct drm_display_mode *mode,
  7985. struct intel_load_detect_pipe *old,
  7986. struct drm_modeset_acquire_ctx *ctx)
  7987. {
  7988. struct intel_crtc *intel_crtc;
  7989. struct intel_encoder *intel_encoder =
  7990. intel_attached_encoder(connector);
  7991. struct drm_crtc *possible_crtc;
  7992. struct drm_encoder *encoder = &intel_encoder->base;
  7993. struct drm_crtc *crtc = NULL;
  7994. struct drm_device *dev = encoder->dev;
  7995. struct drm_i915_private *dev_priv = to_i915(dev);
  7996. struct drm_framebuffer *fb;
  7997. struct drm_mode_config *config = &dev->mode_config;
  7998. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  7999. struct drm_connector_state *connector_state;
  8000. struct intel_crtc_state *crtc_state;
  8001. int ret, i = -1;
  8002. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8003. connector->base.id, connector->name,
  8004. encoder->base.id, encoder->name);
  8005. old->restore_state = NULL;
  8006. retry:
  8007. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8008. if (ret)
  8009. goto fail;
  8010. /*
  8011. * Algorithm gets a little messy:
  8012. *
  8013. * - if the connector already has an assigned crtc, use it (but make
  8014. * sure it's on first)
  8015. *
  8016. * - try to find the first unused crtc that can drive this connector,
  8017. * and use that if we find one
  8018. */
  8019. /* See if we already have a CRTC for this connector */
  8020. if (connector->state->crtc) {
  8021. crtc = connector->state->crtc;
  8022. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8023. if (ret)
  8024. goto fail;
  8025. /* Make sure the crtc and connector are running */
  8026. goto found;
  8027. }
  8028. /* Find an unused one (if possible) */
  8029. for_each_crtc(dev, possible_crtc) {
  8030. i++;
  8031. if (!(encoder->possible_crtcs & (1 << i)))
  8032. continue;
  8033. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8034. if (ret)
  8035. goto fail;
  8036. if (possible_crtc->state->enable) {
  8037. drm_modeset_unlock(&possible_crtc->mutex);
  8038. continue;
  8039. }
  8040. crtc = possible_crtc;
  8041. break;
  8042. }
  8043. /*
  8044. * If we didn't find an unused CRTC, don't use any.
  8045. */
  8046. if (!crtc) {
  8047. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8048. goto fail;
  8049. }
  8050. found:
  8051. intel_crtc = to_intel_crtc(crtc);
  8052. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8053. if (ret)
  8054. goto fail;
  8055. state = drm_atomic_state_alloc(dev);
  8056. restore_state = drm_atomic_state_alloc(dev);
  8057. if (!state || !restore_state) {
  8058. ret = -ENOMEM;
  8059. goto fail;
  8060. }
  8061. state->acquire_ctx = ctx;
  8062. restore_state->acquire_ctx = ctx;
  8063. connector_state = drm_atomic_get_connector_state(state, connector);
  8064. if (IS_ERR(connector_state)) {
  8065. ret = PTR_ERR(connector_state);
  8066. goto fail;
  8067. }
  8068. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8069. if (ret)
  8070. goto fail;
  8071. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8072. if (IS_ERR(crtc_state)) {
  8073. ret = PTR_ERR(crtc_state);
  8074. goto fail;
  8075. }
  8076. crtc_state->base.active = crtc_state->base.enable = true;
  8077. if (!mode)
  8078. mode = &load_detect_mode;
  8079. /* We need a framebuffer large enough to accommodate all accesses
  8080. * that the plane may generate whilst we perform load detection.
  8081. * We can not rely on the fbcon either being present (we get called
  8082. * during its initialisation to detect all boot displays, or it may
  8083. * not even exist) or that it is large enough to satisfy the
  8084. * requested mode.
  8085. */
  8086. fb = mode_fits_in_fbdev(dev, mode);
  8087. if (fb == NULL) {
  8088. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8089. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8090. } else
  8091. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8092. if (IS_ERR(fb)) {
  8093. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8094. goto fail;
  8095. }
  8096. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8097. if (ret)
  8098. goto fail;
  8099. drm_framebuffer_unreference(fb);
  8100. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8101. if (ret)
  8102. goto fail;
  8103. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8104. if (!ret)
  8105. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8106. if (!ret)
  8107. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8108. if (ret) {
  8109. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8110. goto fail;
  8111. }
  8112. ret = drm_atomic_commit(state);
  8113. if (ret) {
  8114. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8115. goto fail;
  8116. }
  8117. old->restore_state = restore_state;
  8118. drm_atomic_state_put(state);
  8119. /* let the connector get through one full cycle before testing */
  8120. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8121. return true;
  8122. fail:
  8123. if (state) {
  8124. drm_atomic_state_put(state);
  8125. state = NULL;
  8126. }
  8127. if (restore_state) {
  8128. drm_atomic_state_put(restore_state);
  8129. restore_state = NULL;
  8130. }
  8131. if (ret == -EDEADLK) {
  8132. drm_modeset_backoff(ctx);
  8133. goto retry;
  8134. }
  8135. return false;
  8136. }
  8137. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8138. struct intel_load_detect_pipe *old,
  8139. struct drm_modeset_acquire_ctx *ctx)
  8140. {
  8141. struct intel_encoder *intel_encoder =
  8142. intel_attached_encoder(connector);
  8143. struct drm_encoder *encoder = &intel_encoder->base;
  8144. struct drm_atomic_state *state = old->restore_state;
  8145. int ret;
  8146. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8147. connector->base.id, connector->name,
  8148. encoder->base.id, encoder->name);
  8149. if (!state)
  8150. return;
  8151. ret = drm_atomic_commit(state);
  8152. if (ret)
  8153. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8154. drm_atomic_state_put(state);
  8155. }
  8156. static int i9xx_pll_refclk(struct drm_device *dev,
  8157. const struct intel_crtc_state *pipe_config)
  8158. {
  8159. struct drm_i915_private *dev_priv = to_i915(dev);
  8160. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8161. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8162. return dev_priv->vbt.lvds_ssc_freq;
  8163. else if (HAS_PCH_SPLIT(dev_priv))
  8164. return 120000;
  8165. else if (!IS_GEN2(dev_priv))
  8166. return 96000;
  8167. else
  8168. return 48000;
  8169. }
  8170. /* Returns the clock of the currently programmed mode of the given pipe. */
  8171. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8172. struct intel_crtc_state *pipe_config)
  8173. {
  8174. struct drm_device *dev = crtc->base.dev;
  8175. struct drm_i915_private *dev_priv = to_i915(dev);
  8176. int pipe = pipe_config->cpu_transcoder;
  8177. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8178. u32 fp;
  8179. struct dpll clock;
  8180. int port_clock;
  8181. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8182. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8183. fp = pipe_config->dpll_hw_state.fp0;
  8184. else
  8185. fp = pipe_config->dpll_hw_state.fp1;
  8186. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8187. if (IS_PINEVIEW(dev_priv)) {
  8188. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8189. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8190. } else {
  8191. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8192. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8193. }
  8194. if (!IS_GEN2(dev_priv)) {
  8195. if (IS_PINEVIEW(dev_priv))
  8196. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8197. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8198. else
  8199. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8200. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8201. switch (dpll & DPLL_MODE_MASK) {
  8202. case DPLLB_MODE_DAC_SERIAL:
  8203. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8204. 5 : 10;
  8205. break;
  8206. case DPLLB_MODE_LVDS:
  8207. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8208. 7 : 14;
  8209. break;
  8210. default:
  8211. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8212. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8213. return;
  8214. }
  8215. if (IS_PINEVIEW(dev_priv))
  8216. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8217. else
  8218. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8219. } else {
  8220. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8221. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8222. if (is_lvds) {
  8223. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8224. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8225. if (lvds & LVDS_CLKB_POWER_UP)
  8226. clock.p2 = 7;
  8227. else
  8228. clock.p2 = 14;
  8229. } else {
  8230. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8231. clock.p1 = 2;
  8232. else {
  8233. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8234. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8235. }
  8236. if (dpll & PLL_P2_DIVIDE_BY_4)
  8237. clock.p2 = 4;
  8238. else
  8239. clock.p2 = 2;
  8240. }
  8241. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8242. }
  8243. /*
  8244. * This value includes pixel_multiplier. We will use
  8245. * port_clock to compute adjusted_mode.crtc_clock in the
  8246. * encoder's get_config() function.
  8247. */
  8248. pipe_config->port_clock = port_clock;
  8249. }
  8250. int intel_dotclock_calculate(int link_freq,
  8251. const struct intel_link_m_n *m_n)
  8252. {
  8253. /*
  8254. * The calculation for the data clock is:
  8255. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8256. * But we want to avoid losing precison if possible, so:
  8257. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8258. *
  8259. * and the link clock is simpler:
  8260. * link_clock = (m * link_clock) / n
  8261. */
  8262. if (!m_n->link_n)
  8263. return 0;
  8264. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8265. }
  8266. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8267. struct intel_crtc_state *pipe_config)
  8268. {
  8269. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8270. /* read out port_clock from the DPLL */
  8271. i9xx_crtc_clock_get(crtc, pipe_config);
  8272. /*
  8273. * In case there is an active pipe without active ports,
  8274. * we may need some idea for the dotclock anyway.
  8275. * Calculate one based on the FDI configuration.
  8276. */
  8277. pipe_config->base.adjusted_mode.crtc_clock =
  8278. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8279. &pipe_config->fdi_m_n);
  8280. }
  8281. /** Returns the currently programmed mode of the given pipe. */
  8282. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8283. struct drm_crtc *crtc)
  8284. {
  8285. struct drm_i915_private *dev_priv = to_i915(dev);
  8286. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8287. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8288. struct drm_display_mode *mode;
  8289. struct intel_crtc_state *pipe_config;
  8290. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8291. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8292. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8293. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8294. enum pipe pipe = intel_crtc->pipe;
  8295. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8296. if (!mode)
  8297. return NULL;
  8298. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8299. if (!pipe_config) {
  8300. kfree(mode);
  8301. return NULL;
  8302. }
  8303. /*
  8304. * Construct a pipe_config sufficient for getting the clock info
  8305. * back out of crtc_clock_get.
  8306. *
  8307. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8308. * to use a real value here instead.
  8309. */
  8310. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  8311. pipe_config->pixel_multiplier = 1;
  8312. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8313. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8314. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8315. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  8316. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  8317. mode->hdisplay = (htot & 0xffff) + 1;
  8318. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8319. mode->hsync_start = (hsync & 0xffff) + 1;
  8320. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8321. mode->vdisplay = (vtot & 0xffff) + 1;
  8322. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8323. mode->vsync_start = (vsync & 0xffff) + 1;
  8324. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8325. drm_mode_set_name(mode);
  8326. kfree(pipe_config);
  8327. return mode;
  8328. }
  8329. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8330. {
  8331. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8332. struct drm_device *dev = crtc->dev;
  8333. struct intel_flip_work *work;
  8334. spin_lock_irq(&dev->event_lock);
  8335. work = intel_crtc->flip_work;
  8336. intel_crtc->flip_work = NULL;
  8337. spin_unlock_irq(&dev->event_lock);
  8338. if (work) {
  8339. cancel_work_sync(&work->mmio_work);
  8340. cancel_work_sync(&work->unpin_work);
  8341. kfree(work);
  8342. }
  8343. drm_crtc_cleanup(crtc);
  8344. kfree(intel_crtc);
  8345. }
  8346. static void intel_unpin_work_fn(struct work_struct *__work)
  8347. {
  8348. struct intel_flip_work *work =
  8349. container_of(__work, struct intel_flip_work, unpin_work);
  8350. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8351. struct drm_device *dev = crtc->base.dev;
  8352. struct drm_plane *primary = crtc->base.primary;
  8353. if (is_mmio_work(work))
  8354. flush_work(&work->mmio_work);
  8355. mutex_lock(&dev->struct_mutex);
  8356. intel_unpin_fb_vma(work->old_vma);
  8357. i915_gem_object_put(work->pending_flip_obj);
  8358. mutex_unlock(&dev->struct_mutex);
  8359. i915_gem_request_put(work->flip_queued_req);
  8360. intel_frontbuffer_flip_complete(to_i915(dev),
  8361. to_intel_plane(primary)->frontbuffer_bit);
  8362. intel_fbc_post_update(crtc);
  8363. drm_framebuffer_unreference(work->old_fb);
  8364. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8365. atomic_dec(&crtc->unpin_work_count);
  8366. kfree(work);
  8367. }
  8368. /* Is 'a' after or equal to 'b'? */
  8369. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8370. {
  8371. return !((a - b) & 0x80000000);
  8372. }
  8373. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  8374. struct intel_flip_work *work)
  8375. {
  8376. struct drm_device *dev = crtc->base.dev;
  8377. struct drm_i915_private *dev_priv = to_i915(dev);
  8378. if (abort_flip_on_reset(crtc))
  8379. return true;
  8380. /*
  8381. * The relevant registers doen't exist on pre-ctg.
  8382. * As the flip done interrupt doesn't trigger for mmio
  8383. * flips on gmch platforms, a flip count check isn't
  8384. * really needed there. But since ctg has the registers,
  8385. * include it in the check anyway.
  8386. */
  8387. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8388. return true;
  8389. /*
  8390. * BDW signals flip done immediately if the plane
  8391. * is disabled, even if the plane enable is already
  8392. * armed to occur at the next vblank :(
  8393. */
  8394. /*
  8395. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  8396. * used the same base address. In that case the mmio flip might
  8397. * have completed, but the CS hasn't even executed the flip yet.
  8398. *
  8399. * A flip count check isn't enough as the CS might have updated
  8400. * the base address just after start of vblank, but before we
  8401. * managed to process the interrupt. This means we'd complete the
  8402. * CS flip too soon.
  8403. *
  8404. * Combining both checks should get us a good enough result. It may
  8405. * still happen that the CS flip has been executed, but has not
  8406. * yet actually completed. But in case the base address is the same
  8407. * anyway, we don't really care.
  8408. */
  8409. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  8410. crtc->flip_work->gtt_offset &&
  8411. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  8412. crtc->flip_work->flip_count);
  8413. }
  8414. static bool
  8415. __pageflip_finished_mmio(struct intel_crtc *crtc,
  8416. struct intel_flip_work *work)
  8417. {
  8418. /*
  8419. * MMIO work completes when vblank is different from
  8420. * flip_queued_vblank.
  8421. *
  8422. * Reset counter value doesn't matter, this is handled by
  8423. * i915_wait_request finishing early, so no need to handle
  8424. * reset here.
  8425. */
  8426. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  8427. }
  8428. static bool pageflip_finished(struct intel_crtc *crtc,
  8429. struct intel_flip_work *work)
  8430. {
  8431. if (!atomic_read(&work->pending))
  8432. return false;
  8433. smp_rmb();
  8434. if (is_mmio_work(work))
  8435. return __pageflip_finished_mmio(crtc, work);
  8436. else
  8437. return __pageflip_finished_cs(crtc, work);
  8438. }
  8439. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  8440. {
  8441. struct drm_device *dev = &dev_priv->drm;
  8442. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8443. struct intel_flip_work *work;
  8444. unsigned long flags;
  8445. /* Ignore early vblank irqs */
  8446. if (!crtc)
  8447. return;
  8448. /*
  8449. * This is called both by irq handlers and the reset code (to complete
  8450. * lost pageflips) so needs the full irqsave spinlocks.
  8451. */
  8452. spin_lock_irqsave(&dev->event_lock, flags);
  8453. work = crtc->flip_work;
  8454. if (work != NULL &&
  8455. !is_mmio_work(work) &&
  8456. pageflip_finished(crtc, work))
  8457. page_flip_completed(crtc);
  8458. spin_unlock_irqrestore(&dev->event_lock, flags);
  8459. }
  8460. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  8461. {
  8462. struct drm_device *dev = &dev_priv->drm;
  8463. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8464. struct intel_flip_work *work;
  8465. unsigned long flags;
  8466. /* Ignore early vblank irqs */
  8467. if (!crtc)
  8468. return;
  8469. /*
  8470. * This is called both by irq handlers and the reset code (to complete
  8471. * lost pageflips) so needs the full irqsave spinlocks.
  8472. */
  8473. spin_lock_irqsave(&dev->event_lock, flags);
  8474. work = crtc->flip_work;
  8475. if (work != NULL &&
  8476. is_mmio_work(work) &&
  8477. pageflip_finished(crtc, work))
  8478. page_flip_completed(crtc);
  8479. spin_unlock_irqrestore(&dev->event_lock, flags);
  8480. }
  8481. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  8482. struct intel_flip_work *work)
  8483. {
  8484. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  8485. /* Ensure that the work item is consistent when activating it ... */
  8486. smp_mb__before_atomic();
  8487. atomic_set(&work->pending, 1);
  8488. }
  8489. static int intel_gen2_queue_flip(struct drm_device *dev,
  8490. struct drm_crtc *crtc,
  8491. struct drm_framebuffer *fb,
  8492. struct drm_i915_gem_object *obj,
  8493. struct drm_i915_gem_request *req,
  8494. uint32_t flags)
  8495. {
  8496. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8497. u32 flip_mask, *cs;
  8498. cs = intel_ring_begin(req, 6);
  8499. if (IS_ERR(cs))
  8500. return PTR_ERR(cs);
  8501. /* Can't queue multiple flips, so wait for the previous
  8502. * one to finish before executing the next.
  8503. */
  8504. if (intel_crtc->plane)
  8505. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8506. else
  8507. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8508. *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
  8509. *cs++ = MI_NOOP;
  8510. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8511. *cs++ = fb->pitches[0];
  8512. *cs++ = intel_crtc->flip_work->gtt_offset;
  8513. *cs++ = 0; /* aux display base address, unused */
  8514. return 0;
  8515. }
  8516. static int intel_gen3_queue_flip(struct drm_device *dev,
  8517. struct drm_crtc *crtc,
  8518. struct drm_framebuffer *fb,
  8519. struct drm_i915_gem_object *obj,
  8520. struct drm_i915_gem_request *req,
  8521. uint32_t flags)
  8522. {
  8523. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8524. u32 flip_mask, *cs;
  8525. cs = intel_ring_begin(req, 6);
  8526. if (IS_ERR(cs))
  8527. return PTR_ERR(cs);
  8528. if (intel_crtc->plane)
  8529. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8530. else
  8531. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8532. *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
  8533. *cs++ = MI_NOOP;
  8534. *cs++ = MI_DISPLAY_FLIP_I915 | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8535. *cs++ = fb->pitches[0];
  8536. *cs++ = intel_crtc->flip_work->gtt_offset;
  8537. *cs++ = MI_NOOP;
  8538. return 0;
  8539. }
  8540. static int intel_gen4_queue_flip(struct drm_device *dev,
  8541. struct drm_crtc *crtc,
  8542. struct drm_framebuffer *fb,
  8543. struct drm_i915_gem_object *obj,
  8544. struct drm_i915_gem_request *req,
  8545. uint32_t flags)
  8546. {
  8547. struct drm_i915_private *dev_priv = to_i915(dev);
  8548. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8549. u32 pf, pipesrc, *cs;
  8550. cs = intel_ring_begin(req, 4);
  8551. if (IS_ERR(cs))
  8552. return PTR_ERR(cs);
  8553. /* i965+ uses the linear or tiled offsets from the
  8554. * Display Registers (which do not change across a page-flip)
  8555. * so we need only reprogram the base address.
  8556. */
  8557. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8558. *cs++ = fb->pitches[0];
  8559. *cs++ = intel_crtc->flip_work->gtt_offset |
  8560. intel_fb_modifier_to_tiling(fb->modifier);
  8561. /* XXX Enabling the panel-fitter across page-flip is so far
  8562. * untested on non-native modes, so ignore it for now.
  8563. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  8564. */
  8565. pf = 0;
  8566. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8567. *cs++ = pf | pipesrc;
  8568. return 0;
  8569. }
  8570. static int intel_gen6_queue_flip(struct drm_device *dev,
  8571. struct drm_crtc *crtc,
  8572. struct drm_framebuffer *fb,
  8573. struct drm_i915_gem_object *obj,
  8574. struct drm_i915_gem_request *req,
  8575. uint32_t flags)
  8576. {
  8577. struct drm_i915_private *dev_priv = to_i915(dev);
  8578. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8579. u32 pf, pipesrc, *cs;
  8580. cs = intel_ring_begin(req, 4);
  8581. if (IS_ERR(cs))
  8582. return PTR_ERR(cs);
  8583. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8584. *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
  8585. *cs++ = intel_crtc->flip_work->gtt_offset;
  8586. /* Contrary to the suggestions in the documentation,
  8587. * "Enable Panel Fitter" does not seem to be required when page
  8588. * flipping with a non-native mode, and worse causes a normal
  8589. * modeset to fail.
  8590. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  8591. */
  8592. pf = 0;
  8593. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8594. *cs++ = pf | pipesrc;
  8595. return 0;
  8596. }
  8597. static int intel_gen7_queue_flip(struct drm_device *dev,
  8598. struct drm_crtc *crtc,
  8599. struct drm_framebuffer *fb,
  8600. struct drm_i915_gem_object *obj,
  8601. struct drm_i915_gem_request *req,
  8602. uint32_t flags)
  8603. {
  8604. struct drm_i915_private *dev_priv = to_i915(dev);
  8605. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8606. u32 *cs, plane_bit = 0;
  8607. int len, ret;
  8608. switch (intel_crtc->plane) {
  8609. case PLANE_A:
  8610. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  8611. break;
  8612. case PLANE_B:
  8613. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  8614. break;
  8615. case PLANE_C:
  8616. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  8617. break;
  8618. default:
  8619. WARN_ONCE(1, "unknown plane in flip command\n");
  8620. return -ENODEV;
  8621. }
  8622. len = 4;
  8623. if (req->engine->id == RCS) {
  8624. len += 6;
  8625. /*
  8626. * On Gen 8, SRM is now taking an extra dword to accommodate
  8627. * 48bits addresses, and we need a NOOP for the batch size to
  8628. * stay even.
  8629. */
  8630. if (IS_GEN8(dev_priv))
  8631. len += 2;
  8632. }
  8633. /*
  8634. * BSpec MI_DISPLAY_FLIP for IVB:
  8635. * "The full packet must be contained within the same cache line."
  8636. *
  8637. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8638. * cacheline, if we ever start emitting more commands before
  8639. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8640. * then do the cacheline alignment, and finally emit the
  8641. * MI_DISPLAY_FLIP.
  8642. */
  8643. ret = intel_ring_cacheline_align(req);
  8644. if (ret)
  8645. return ret;
  8646. cs = intel_ring_begin(req, len);
  8647. if (IS_ERR(cs))
  8648. return PTR_ERR(cs);
  8649. /* Unmask the flip-done completion message. Note that the bspec says that
  8650. * we should do this for both the BCS and RCS, and that we must not unmask
  8651. * more than one flip event at any time (or ensure that one flip message
  8652. * can be sent by waiting for flip-done prior to queueing new flips).
  8653. * Experimentation says that BCS works despite DERRMR masking all
  8654. * flip-done completion events and that unmasking all planes at once
  8655. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8656. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8657. */
  8658. if (req->engine->id == RCS) {
  8659. *cs++ = MI_LOAD_REGISTER_IMM(1);
  8660. *cs++ = i915_mmio_reg_offset(DERRMR);
  8661. *cs++ = ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8662. DERRMR_PIPEB_PRI_FLIP_DONE |
  8663. DERRMR_PIPEC_PRI_FLIP_DONE);
  8664. if (IS_GEN8(dev_priv))
  8665. *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
  8666. MI_SRM_LRM_GLOBAL_GTT;
  8667. else
  8668. *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
  8669. *cs++ = i915_mmio_reg_offset(DERRMR);
  8670. *cs++ = i915_ggtt_offset(req->engine->scratch) + 256;
  8671. if (IS_GEN8(dev_priv)) {
  8672. *cs++ = 0;
  8673. *cs++ = MI_NOOP;
  8674. }
  8675. }
  8676. *cs++ = MI_DISPLAY_FLIP_I915 | plane_bit;
  8677. *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
  8678. *cs++ = intel_crtc->flip_work->gtt_offset;
  8679. *cs++ = MI_NOOP;
  8680. return 0;
  8681. }
  8682. static bool use_mmio_flip(struct intel_engine_cs *engine,
  8683. struct drm_i915_gem_object *obj)
  8684. {
  8685. /*
  8686. * This is not being used for older platforms, because
  8687. * non-availability of flip done interrupt forces us to use
  8688. * CS flips. Older platforms derive flip done using some clever
  8689. * tricks involving the flip_pending status bits and vblank irqs.
  8690. * So using MMIO flips there would disrupt this mechanism.
  8691. */
  8692. if (engine == NULL)
  8693. return true;
  8694. if (INTEL_GEN(engine->i915) < 5)
  8695. return false;
  8696. if (i915.use_mmio_flip < 0)
  8697. return false;
  8698. else if (i915.use_mmio_flip > 0)
  8699. return true;
  8700. else if (i915.enable_execlists)
  8701. return true;
  8702. return engine != i915_gem_object_last_write_engine(obj);
  8703. }
  8704. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  8705. unsigned int rotation,
  8706. struct intel_flip_work *work)
  8707. {
  8708. struct drm_device *dev = intel_crtc->base.dev;
  8709. struct drm_i915_private *dev_priv = to_i915(dev);
  8710. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8711. const enum pipe pipe = intel_crtc->pipe;
  8712. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  8713. ctl = I915_READ(PLANE_CTL(pipe, 0));
  8714. ctl &= ~PLANE_CTL_TILED_MASK;
  8715. switch (fb->modifier) {
  8716. case DRM_FORMAT_MOD_NONE:
  8717. break;
  8718. case I915_FORMAT_MOD_X_TILED:
  8719. ctl |= PLANE_CTL_TILED_X;
  8720. break;
  8721. case I915_FORMAT_MOD_Y_TILED:
  8722. ctl |= PLANE_CTL_TILED_Y;
  8723. break;
  8724. case I915_FORMAT_MOD_Yf_TILED:
  8725. ctl |= PLANE_CTL_TILED_YF;
  8726. break;
  8727. default:
  8728. MISSING_CASE(fb->modifier);
  8729. }
  8730. /*
  8731. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  8732. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  8733. */
  8734. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  8735. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  8736. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  8737. POSTING_READ(PLANE_SURF(pipe, 0));
  8738. }
  8739. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  8740. struct intel_flip_work *work)
  8741. {
  8742. struct drm_device *dev = intel_crtc->base.dev;
  8743. struct drm_i915_private *dev_priv = to_i915(dev);
  8744. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8745. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  8746. u32 dspcntr;
  8747. dspcntr = I915_READ(reg);
  8748. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  8749. dspcntr |= DISPPLANE_TILED;
  8750. else
  8751. dspcntr &= ~DISPPLANE_TILED;
  8752. I915_WRITE(reg, dspcntr);
  8753. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  8754. POSTING_READ(DSPSURF(intel_crtc->plane));
  8755. }
  8756. static void intel_mmio_flip_work_func(struct work_struct *w)
  8757. {
  8758. struct intel_flip_work *work =
  8759. container_of(w, struct intel_flip_work, mmio_work);
  8760. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8761. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8762. struct intel_framebuffer *intel_fb =
  8763. to_intel_framebuffer(crtc->base.primary->fb);
  8764. struct drm_i915_gem_object *obj = intel_fb->obj;
  8765. WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
  8766. intel_pipe_update_start(crtc);
  8767. if (INTEL_GEN(dev_priv) >= 9)
  8768. skl_do_mmio_flip(crtc, work->rotation, work);
  8769. else
  8770. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  8771. ilk_do_mmio_flip(crtc, work);
  8772. intel_pipe_update_end(crtc, work);
  8773. }
  8774. static int intel_default_queue_flip(struct drm_device *dev,
  8775. struct drm_crtc *crtc,
  8776. struct drm_framebuffer *fb,
  8777. struct drm_i915_gem_object *obj,
  8778. struct drm_i915_gem_request *req,
  8779. uint32_t flags)
  8780. {
  8781. return -ENODEV;
  8782. }
  8783. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  8784. struct intel_crtc *intel_crtc,
  8785. struct intel_flip_work *work)
  8786. {
  8787. u32 addr, vblank;
  8788. if (!atomic_read(&work->pending))
  8789. return false;
  8790. smp_rmb();
  8791. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  8792. if (work->flip_ready_vblank == 0) {
  8793. if (work->flip_queued_req &&
  8794. !i915_gem_request_completed(work->flip_queued_req))
  8795. return false;
  8796. work->flip_ready_vblank = vblank;
  8797. }
  8798. if (vblank - work->flip_ready_vblank < 3)
  8799. return false;
  8800. /* Potential stall - if we see that the flip has happened,
  8801. * assume a missed interrupt. */
  8802. if (INTEL_GEN(dev_priv) >= 4)
  8803. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8804. else
  8805. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8806. /* There is a potential issue here with a false positive after a flip
  8807. * to the same address. We could address this by checking for a
  8808. * non-incrementing frame counter.
  8809. */
  8810. return addr == work->gtt_offset;
  8811. }
  8812. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  8813. {
  8814. struct drm_device *dev = &dev_priv->drm;
  8815. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8816. struct intel_flip_work *work;
  8817. WARN_ON(!in_interrupt());
  8818. if (crtc == NULL)
  8819. return;
  8820. spin_lock(&dev->event_lock);
  8821. work = crtc->flip_work;
  8822. if (work != NULL && !is_mmio_work(work) &&
  8823. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  8824. WARN_ONCE(1,
  8825. "Kicking stuck page flip: queued at %d, now %d\n",
  8826. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  8827. page_flip_completed(crtc);
  8828. work = NULL;
  8829. }
  8830. if (work != NULL && !is_mmio_work(work) &&
  8831. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  8832. intel_queue_rps_boost_for_request(work->flip_queued_req);
  8833. spin_unlock(&dev->event_lock);
  8834. }
  8835. __maybe_unused
  8836. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8837. struct drm_framebuffer *fb,
  8838. struct drm_pending_vblank_event *event,
  8839. uint32_t page_flip_flags)
  8840. {
  8841. struct drm_device *dev = crtc->dev;
  8842. struct drm_i915_private *dev_priv = to_i915(dev);
  8843. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8844. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8845. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8846. struct drm_plane *primary = crtc->primary;
  8847. enum pipe pipe = intel_crtc->pipe;
  8848. struct intel_flip_work *work;
  8849. struct intel_engine_cs *engine;
  8850. bool mmio_flip;
  8851. struct drm_i915_gem_request *request;
  8852. struct i915_vma *vma;
  8853. int ret;
  8854. /*
  8855. * drm_mode_page_flip_ioctl() should already catch this, but double
  8856. * check to be safe. In the future we may enable pageflipping from
  8857. * a disabled primary plane.
  8858. */
  8859. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8860. return -EBUSY;
  8861. /* Can't change pixel format via MI display flips. */
  8862. if (fb->format != crtc->primary->fb->format)
  8863. return -EINVAL;
  8864. /*
  8865. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8866. * Note that pitch changes could also affect these register.
  8867. */
  8868. if (INTEL_GEN(dev_priv) > 3 &&
  8869. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8870. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8871. return -EINVAL;
  8872. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8873. goto out_hang;
  8874. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8875. if (work == NULL)
  8876. return -ENOMEM;
  8877. work->event = event;
  8878. work->crtc = crtc;
  8879. work->old_fb = old_fb;
  8880. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  8881. ret = drm_crtc_vblank_get(crtc);
  8882. if (ret)
  8883. goto free_work;
  8884. /* We borrow the event spin lock for protecting flip_work */
  8885. spin_lock_irq(&dev->event_lock);
  8886. if (intel_crtc->flip_work) {
  8887. /* Before declaring the flip queue wedged, check if
  8888. * the hardware completed the operation behind our backs.
  8889. */
  8890. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  8891. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8892. page_flip_completed(intel_crtc);
  8893. } else {
  8894. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8895. spin_unlock_irq(&dev->event_lock);
  8896. drm_crtc_vblank_put(crtc);
  8897. kfree(work);
  8898. return -EBUSY;
  8899. }
  8900. }
  8901. intel_crtc->flip_work = work;
  8902. spin_unlock_irq(&dev->event_lock);
  8903. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8904. flush_workqueue(dev_priv->wq);
  8905. /* Reference the objects for the scheduled work. */
  8906. drm_framebuffer_reference(work->old_fb);
  8907. crtc->primary->fb = fb;
  8908. update_state_fb(crtc->primary);
  8909. work->pending_flip_obj = i915_gem_object_get(obj);
  8910. ret = i915_mutex_lock_interruptible(dev);
  8911. if (ret)
  8912. goto cleanup;
  8913. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  8914. if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
  8915. ret = -EIO;
  8916. goto unlock;
  8917. }
  8918. atomic_inc(&intel_crtc->unpin_work_count);
  8919. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  8920. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  8921. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  8922. engine = dev_priv->engine[BCS];
  8923. if (fb->modifier != old_fb->modifier)
  8924. /* vlv: DISPLAY_FLIP fails to change tiling */
  8925. engine = NULL;
  8926. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  8927. engine = dev_priv->engine[BCS];
  8928. } else if (INTEL_GEN(dev_priv) >= 7) {
  8929. engine = i915_gem_object_last_write_engine(obj);
  8930. if (engine == NULL || engine->id != RCS)
  8931. engine = dev_priv->engine[BCS];
  8932. } else {
  8933. engine = dev_priv->engine[RCS];
  8934. }
  8935. mmio_flip = use_mmio_flip(engine, obj);
  8936. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  8937. if (IS_ERR(vma)) {
  8938. ret = PTR_ERR(vma);
  8939. goto cleanup_pending;
  8940. }
  8941. work->old_vma = to_intel_plane_state(primary->state)->vma;
  8942. to_intel_plane_state(primary->state)->vma = vma;
  8943. work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
  8944. work->rotation = crtc->primary->state->rotation;
  8945. /*
  8946. * There's the potential that the next frame will not be compatible with
  8947. * FBC, so we want to call pre_update() before the actual page flip.
  8948. * The problem is that pre_update() caches some information about the fb
  8949. * object, so we want to do this only after the object is pinned. Let's
  8950. * be on the safe side and do this immediately before scheduling the
  8951. * flip.
  8952. */
  8953. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  8954. to_intel_plane_state(primary->state));
  8955. if (mmio_flip) {
  8956. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  8957. queue_work(system_unbound_wq, &work->mmio_work);
  8958. } else {
  8959. request = i915_gem_request_alloc(engine,
  8960. dev_priv->kernel_context);
  8961. if (IS_ERR(request)) {
  8962. ret = PTR_ERR(request);
  8963. goto cleanup_unpin;
  8964. }
  8965. ret = i915_gem_request_await_object(request, obj, false);
  8966. if (ret)
  8967. goto cleanup_request;
  8968. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  8969. page_flip_flags);
  8970. if (ret)
  8971. goto cleanup_request;
  8972. intel_mark_page_flip_active(intel_crtc, work);
  8973. work->flip_queued_req = i915_gem_request_get(request);
  8974. i915_add_request_no_flush(request);
  8975. }
  8976. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  8977. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  8978. to_intel_plane(primary)->frontbuffer_bit);
  8979. mutex_unlock(&dev->struct_mutex);
  8980. intel_frontbuffer_flip_prepare(to_i915(dev),
  8981. to_intel_plane(primary)->frontbuffer_bit);
  8982. trace_i915_flip_request(intel_crtc->plane, obj);
  8983. return 0;
  8984. cleanup_request:
  8985. i915_add_request_no_flush(request);
  8986. cleanup_unpin:
  8987. to_intel_plane_state(primary->state)->vma = work->old_vma;
  8988. intel_unpin_fb_vma(vma);
  8989. cleanup_pending:
  8990. atomic_dec(&intel_crtc->unpin_work_count);
  8991. unlock:
  8992. mutex_unlock(&dev->struct_mutex);
  8993. cleanup:
  8994. crtc->primary->fb = old_fb;
  8995. update_state_fb(crtc->primary);
  8996. i915_gem_object_put(obj);
  8997. drm_framebuffer_unreference(work->old_fb);
  8998. spin_lock_irq(&dev->event_lock);
  8999. intel_crtc->flip_work = NULL;
  9000. spin_unlock_irq(&dev->event_lock);
  9001. drm_crtc_vblank_put(crtc);
  9002. free_work:
  9003. kfree(work);
  9004. if (ret == -EIO) {
  9005. struct drm_atomic_state *state;
  9006. struct drm_plane_state *plane_state;
  9007. out_hang:
  9008. state = drm_atomic_state_alloc(dev);
  9009. if (!state)
  9010. return -ENOMEM;
  9011. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9012. retry:
  9013. plane_state = drm_atomic_get_plane_state(state, primary);
  9014. ret = PTR_ERR_OR_ZERO(plane_state);
  9015. if (!ret) {
  9016. drm_atomic_set_fb_for_plane(plane_state, fb);
  9017. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9018. if (!ret)
  9019. ret = drm_atomic_commit(state);
  9020. }
  9021. if (ret == -EDEADLK) {
  9022. drm_modeset_backoff(state->acquire_ctx);
  9023. drm_atomic_state_clear(state);
  9024. goto retry;
  9025. }
  9026. drm_atomic_state_put(state);
  9027. if (ret == 0 && event) {
  9028. spin_lock_irq(&dev->event_lock);
  9029. drm_crtc_send_vblank_event(crtc, event);
  9030. spin_unlock_irq(&dev->event_lock);
  9031. }
  9032. }
  9033. return ret;
  9034. }
  9035. /**
  9036. * intel_wm_need_update - Check whether watermarks need updating
  9037. * @plane: drm plane
  9038. * @state: new plane state
  9039. *
  9040. * Check current plane state versus the new one to determine whether
  9041. * watermarks need to be recalculated.
  9042. *
  9043. * Returns true or false.
  9044. */
  9045. static bool intel_wm_need_update(struct drm_plane *plane,
  9046. struct drm_plane_state *state)
  9047. {
  9048. struct intel_plane_state *new = to_intel_plane_state(state);
  9049. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9050. /* Update watermarks on tiling or size changes. */
  9051. if (new->base.visible != cur->base.visible)
  9052. return true;
  9053. if (!cur->base.fb || !new->base.fb)
  9054. return false;
  9055. if (cur->base.fb->modifier != new->base.fb->modifier ||
  9056. cur->base.rotation != new->base.rotation ||
  9057. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  9058. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  9059. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  9060. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  9061. return true;
  9062. return false;
  9063. }
  9064. static bool needs_scaling(struct intel_plane_state *state)
  9065. {
  9066. int src_w = drm_rect_width(&state->base.src) >> 16;
  9067. int src_h = drm_rect_height(&state->base.src) >> 16;
  9068. int dst_w = drm_rect_width(&state->base.dst);
  9069. int dst_h = drm_rect_height(&state->base.dst);
  9070. return (src_w != dst_w || src_h != dst_h);
  9071. }
  9072. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9073. struct drm_plane_state *plane_state)
  9074. {
  9075. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9076. struct drm_crtc *crtc = crtc_state->crtc;
  9077. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9078. struct drm_plane *plane = plane_state->plane;
  9079. struct drm_device *dev = crtc->dev;
  9080. struct drm_i915_private *dev_priv = to_i915(dev);
  9081. struct intel_plane_state *old_plane_state =
  9082. to_intel_plane_state(plane->state);
  9083. bool mode_changed = needs_modeset(crtc_state);
  9084. bool was_crtc_enabled = crtc->state->active;
  9085. bool is_crtc_enabled = crtc_state->active;
  9086. bool turn_off, turn_on, visible, was_visible;
  9087. struct drm_framebuffer *fb = plane_state->fb;
  9088. int ret;
  9089. if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  9090. ret = skl_update_scaler_plane(
  9091. to_intel_crtc_state(crtc_state),
  9092. to_intel_plane_state(plane_state));
  9093. if (ret)
  9094. return ret;
  9095. }
  9096. was_visible = old_plane_state->base.visible;
  9097. visible = plane_state->visible;
  9098. if (!was_crtc_enabled && WARN_ON(was_visible))
  9099. was_visible = false;
  9100. /*
  9101. * Visibility is calculated as if the crtc was on, but
  9102. * after scaler setup everything depends on it being off
  9103. * when the crtc isn't active.
  9104. *
  9105. * FIXME this is wrong for watermarks. Watermarks should also
  9106. * be computed as if the pipe would be active. Perhaps move
  9107. * per-plane wm computation to the .check_plane() hook, and
  9108. * only combine the results from all planes in the current place?
  9109. */
  9110. if (!is_crtc_enabled)
  9111. plane_state->visible = visible = false;
  9112. if (!was_visible && !visible)
  9113. return 0;
  9114. if (fb != old_plane_state->base.fb)
  9115. pipe_config->fb_changed = true;
  9116. turn_off = was_visible && (!visible || mode_changed);
  9117. turn_on = visible && (!was_visible || mode_changed);
  9118. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  9119. intel_crtc->base.base.id,
  9120. intel_crtc->base.name,
  9121. plane->base.id, plane->name,
  9122. fb ? fb->base.id : -1);
  9123. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  9124. plane->base.id, plane->name,
  9125. was_visible, visible,
  9126. turn_off, turn_on, mode_changed);
  9127. if (turn_on) {
  9128. pipe_config->update_wm_pre = true;
  9129. /* must disable cxsr around plane enable/disable */
  9130. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9131. pipe_config->disable_cxsr = true;
  9132. } else if (turn_off) {
  9133. pipe_config->update_wm_post = true;
  9134. /* must disable cxsr around plane enable/disable */
  9135. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9136. pipe_config->disable_cxsr = true;
  9137. } else if (intel_wm_need_update(plane, plane_state)) {
  9138. /* FIXME bollocks */
  9139. pipe_config->update_wm_pre = true;
  9140. pipe_config->update_wm_post = true;
  9141. }
  9142. /* Pre-gen9 platforms need two-step watermark updates */
  9143. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  9144. INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
  9145. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  9146. if (visible || was_visible)
  9147. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  9148. /*
  9149. * WaCxSRDisabledForSpriteScaling:ivb
  9150. *
  9151. * cstate->update_wm was already set above, so this flag will
  9152. * take effect when we commit and program watermarks.
  9153. */
  9154. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
  9155. needs_scaling(to_intel_plane_state(plane_state)) &&
  9156. !needs_scaling(old_plane_state))
  9157. pipe_config->disable_lp_wm = true;
  9158. return 0;
  9159. }
  9160. static bool encoders_cloneable(const struct intel_encoder *a,
  9161. const struct intel_encoder *b)
  9162. {
  9163. /* masks could be asymmetric, so check both ways */
  9164. return a == b || (a->cloneable & (1 << b->type) &&
  9165. b->cloneable & (1 << a->type));
  9166. }
  9167. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9168. struct intel_crtc *crtc,
  9169. struct intel_encoder *encoder)
  9170. {
  9171. struct intel_encoder *source_encoder;
  9172. struct drm_connector *connector;
  9173. struct drm_connector_state *connector_state;
  9174. int i;
  9175. for_each_connector_in_state(state, connector, connector_state, i) {
  9176. if (connector_state->crtc != &crtc->base)
  9177. continue;
  9178. source_encoder =
  9179. to_intel_encoder(connector_state->best_encoder);
  9180. if (!encoders_cloneable(encoder, source_encoder))
  9181. return false;
  9182. }
  9183. return true;
  9184. }
  9185. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9186. struct drm_crtc_state *crtc_state)
  9187. {
  9188. struct drm_device *dev = crtc->dev;
  9189. struct drm_i915_private *dev_priv = to_i915(dev);
  9190. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9191. struct intel_crtc_state *pipe_config =
  9192. to_intel_crtc_state(crtc_state);
  9193. struct drm_atomic_state *state = crtc_state->state;
  9194. int ret;
  9195. bool mode_changed = needs_modeset(crtc_state);
  9196. if (mode_changed && !crtc_state->active)
  9197. pipe_config->update_wm_post = true;
  9198. if (mode_changed && crtc_state->enable &&
  9199. dev_priv->display.crtc_compute_clock &&
  9200. !WARN_ON(pipe_config->shared_dpll)) {
  9201. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9202. pipe_config);
  9203. if (ret)
  9204. return ret;
  9205. }
  9206. if (crtc_state->color_mgmt_changed) {
  9207. ret = intel_color_check(crtc, crtc_state);
  9208. if (ret)
  9209. return ret;
  9210. /*
  9211. * Changing color management on Intel hardware is
  9212. * handled as part of planes update.
  9213. */
  9214. crtc_state->planes_changed = true;
  9215. }
  9216. ret = 0;
  9217. if (dev_priv->display.compute_pipe_wm) {
  9218. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  9219. if (ret) {
  9220. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  9221. return ret;
  9222. }
  9223. }
  9224. if (dev_priv->display.compute_intermediate_wm &&
  9225. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  9226. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  9227. return 0;
  9228. /*
  9229. * Calculate 'intermediate' watermarks that satisfy both the
  9230. * old state and the new state. We can program these
  9231. * immediately.
  9232. */
  9233. ret = dev_priv->display.compute_intermediate_wm(dev,
  9234. intel_crtc,
  9235. pipe_config);
  9236. if (ret) {
  9237. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  9238. return ret;
  9239. }
  9240. } else if (dev_priv->display.compute_intermediate_wm) {
  9241. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  9242. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  9243. }
  9244. if (INTEL_GEN(dev_priv) >= 9) {
  9245. if (mode_changed)
  9246. ret = skl_update_scaler_crtc(pipe_config);
  9247. if (!ret)
  9248. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  9249. pipe_config);
  9250. }
  9251. return ret;
  9252. }
  9253. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9254. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9255. .atomic_begin = intel_begin_crtc_commit,
  9256. .atomic_flush = intel_finish_crtc_commit,
  9257. .atomic_check = intel_crtc_atomic_check,
  9258. };
  9259. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9260. {
  9261. struct intel_connector *connector;
  9262. for_each_intel_connector(dev, connector) {
  9263. if (connector->base.state->crtc)
  9264. drm_connector_unreference(&connector->base);
  9265. if (connector->base.encoder) {
  9266. connector->base.state->best_encoder =
  9267. connector->base.encoder;
  9268. connector->base.state->crtc =
  9269. connector->base.encoder->crtc;
  9270. drm_connector_reference(&connector->base);
  9271. } else {
  9272. connector->base.state->best_encoder = NULL;
  9273. connector->base.state->crtc = NULL;
  9274. }
  9275. }
  9276. }
  9277. static void
  9278. connected_sink_compute_bpp(struct intel_connector *connector,
  9279. struct intel_crtc_state *pipe_config)
  9280. {
  9281. const struct drm_display_info *info = &connector->base.display_info;
  9282. int bpp = pipe_config->pipe_bpp;
  9283. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9284. connector->base.base.id,
  9285. connector->base.name);
  9286. /* Don't use an invalid EDID bpc value */
  9287. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  9288. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9289. bpp, info->bpc * 3);
  9290. pipe_config->pipe_bpp = info->bpc * 3;
  9291. }
  9292. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9293. if (info->bpc == 0 && bpp > 24) {
  9294. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9295. bpp);
  9296. pipe_config->pipe_bpp = 24;
  9297. }
  9298. }
  9299. static int
  9300. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9301. struct intel_crtc_state *pipe_config)
  9302. {
  9303. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9304. struct drm_atomic_state *state;
  9305. struct drm_connector *connector;
  9306. struct drm_connector_state *connector_state;
  9307. int bpp, i;
  9308. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  9309. IS_CHERRYVIEW(dev_priv)))
  9310. bpp = 10*3;
  9311. else if (INTEL_GEN(dev_priv) >= 5)
  9312. bpp = 12*3;
  9313. else
  9314. bpp = 8*3;
  9315. pipe_config->pipe_bpp = bpp;
  9316. state = pipe_config->base.state;
  9317. /* Clamp display bpp to EDID value */
  9318. for_each_connector_in_state(state, connector, connector_state, i) {
  9319. if (connector_state->crtc != &crtc->base)
  9320. continue;
  9321. connected_sink_compute_bpp(to_intel_connector(connector),
  9322. pipe_config);
  9323. }
  9324. return bpp;
  9325. }
  9326. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9327. {
  9328. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9329. "type: 0x%x flags: 0x%x\n",
  9330. mode->crtc_clock,
  9331. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9332. mode->crtc_hsync_end, mode->crtc_htotal,
  9333. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9334. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9335. }
  9336. static inline void
  9337. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  9338. unsigned int lane_count, struct intel_link_m_n *m_n)
  9339. {
  9340. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9341. id, lane_count,
  9342. m_n->gmch_m, m_n->gmch_n,
  9343. m_n->link_m, m_n->link_n, m_n->tu);
  9344. }
  9345. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9346. struct intel_crtc_state *pipe_config,
  9347. const char *context)
  9348. {
  9349. struct drm_device *dev = crtc->base.dev;
  9350. struct drm_i915_private *dev_priv = to_i915(dev);
  9351. struct drm_plane *plane;
  9352. struct intel_plane *intel_plane;
  9353. struct intel_plane_state *state;
  9354. struct drm_framebuffer *fb;
  9355. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  9356. crtc->base.base.id, crtc->base.name, context);
  9357. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  9358. transcoder_name(pipe_config->cpu_transcoder),
  9359. pipe_config->pipe_bpp, pipe_config->dither);
  9360. if (pipe_config->has_pch_encoder)
  9361. intel_dump_m_n_config(pipe_config, "fdi",
  9362. pipe_config->fdi_lanes,
  9363. &pipe_config->fdi_m_n);
  9364. if (intel_crtc_has_dp_encoder(pipe_config)) {
  9365. intel_dump_m_n_config(pipe_config, "dp m_n",
  9366. pipe_config->lane_count, &pipe_config->dp_m_n);
  9367. if (pipe_config->has_drrs)
  9368. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  9369. pipe_config->lane_count,
  9370. &pipe_config->dp_m2_n2);
  9371. }
  9372. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9373. pipe_config->has_audio, pipe_config->has_infoframe);
  9374. DRM_DEBUG_KMS("requested mode:\n");
  9375. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9376. DRM_DEBUG_KMS("adjusted mode:\n");
  9377. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9378. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9379. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  9380. pipe_config->port_clock,
  9381. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  9382. pipe_config->pixel_rate);
  9383. if (INTEL_GEN(dev_priv) >= 9)
  9384. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9385. crtc->num_scalers,
  9386. pipe_config->scaler_state.scaler_users,
  9387. pipe_config->scaler_state.scaler_id);
  9388. if (HAS_GMCH_DISPLAY(dev_priv))
  9389. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9390. pipe_config->gmch_pfit.control,
  9391. pipe_config->gmch_pfit.pgm_ratios,
  9392. pipe_config->gmch_pfit.lvds_border_bits);
  9393. else
  9394. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9395. pipe_config->pch_pfit.pos,
  9396. pipe_config->pch_pfit.size,
  9397. enableddisabled(pipe_config->pch_pfit.enabled));
  9398. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  9399. pipe_config->ips_enabled, pipe_config->double_wide);
  9400. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  9401. DRM_DEBUG_KMS("planes on this crtc\n");
  9402. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9403. struct drm_format_name_buf format_name;
  9404. intel_plane = to_intel_plane(plane);
  9405. if (intel_plane->pipe != crtc->pipe)
  9406. continue;
  9407. state = to_intel_plane_state(plane->state);
  9408. fb = state->base.fb;
  9409. if (!fb) {
  9410. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  9411. plane->base.id, plane->name, state->scaler_id);
  9412. continue;
  9413. }
  9414. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  9415. plane->base.id, plane->name,
  9416. fb->base.id, fb->width, fb->height,
  9417. drm_get_format_name(fb->format->format, &format_name));
  9418. if (INTEL_GEN(dev_priv) >= 9)
  9419. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  9420. state->scaler_id,
  9421. state->base.src.x1 >> 16,
  9422. state->base.src.y1 >> 16,
  9423. drm_rect_width(&state->base.src) >> 16,
  9424. drm_rect_height(&state->base.src) >> 16,
  9425. state->base.dst.x1, state->base.dst.y1,
  9426. drm_rect_width(&state->base.dst),
  9427. drm_rect_height(&state->base.dst));
  9428. }
  9429. }
  9430. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9431. {
  9432. struct drm_device *dev = state->dev;
  9433. struct drm_connector *connector;
  9434. unsigned int used_ports = 0;
  9435. unsigned int used_mst_ports = 0;
  9436. /*
  9437. * Walk the connector list instead of the encoder
  9438. * list to detect the problem on ddi platforms
  9439. * where there's just one encoder per digital port.
  9440. */
  9441. drm_for_each_connector(connector, dev) {
  9442. struct drm_connector_state *connector_state;
  9443. struct intel_encoder *encoder;
  9444. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  9445. if (!connector_state)
  9446. connector_state = connector->state;
  9447. if (!connector_state->best_encoder)
  9448. continue;
  9449. encoder = to_intel_encoder(connector_state->best_encoder);
  9450. WARN_ON(!connector_state->crtc);
  9451. switch (encoder->type) {
  9452. unsigned int port_mask;
  9453. case INTEL_OUTPUT_UNKNOWN:
  9454. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9455. break;
  9456. case INTEL_OUTPUT_DP:
  9457. case INTEL_OUTPUT_HDMI:
  9458. case INTEL_OUTPUT_EDP:
  9459. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9460. /* the same port mustn't appear more than once */
  9461. if (used_ports & port_mask)
  9462. return false;
  9463. used_ports |= port_mask;
  9464. break;
  9465. case INTEL_OUTPUT_DP_MST:
  9466. used_mst_ports |=
  9467. 1 << enc_to_mst(&encoder->base)->primary->port;
  9468. break;
  9469. default:
  9470. break;
  9471. }
  9472. }
  9473. /* can't mix MST and SST/HDMI on the same port */
  9474. if (used_ports & used_mst_ports)
  9475. return false;
  9476. return true;
  9477. }
  9478. static void
  9479. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9480. {
  9481. struct drm_crtc_state tmp_state;
  9482. struct intel_crtc_scaler_state scaler_state;
  9483. struct intel_dpll_hw_state dpll_hw_state;
  9484. struct intel_shared_dpll *shared_dpll;
  9485. bool force_thru;
  9486. /* FIXME: before the switch to atomic started, a new pipe_config was
  9487. * kzalloc'd. Code that depends on any field being zero should be
  9488. * fixed, so that the crtc_state can be safely duplicated. For now,
  9489. * only fields that are know to not cause problems are preserved. */
  9490. tmp_state = crtc_state->base;
  9491. scaler_state = crtc_state->scaler_state;
  9492. shared_dpll = crtc_state->shared_dpll;
  9493. dpll_hw_state = crtc_state->dpll_hw_state;
  9494. force_thru = crtc_state->pch_pfit.force_thru;
  9495. memset(crtc_state, 0, sizeof *crtc_state);
  9496. crtc_state->base = tmp_state;
  9497. crtc_state->scaler_state = scaler_state;
  9498. crtc_state->shared_dpll = shared_dpll;
  9499. crtc_state->dpll_hw_state = dpll_hw_state;
  9500. crtc_state->pch_pfit.force_thru = force_thru;
  9501. }
  9502. static int
  9503. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9504. struct intel_crtc_state *pipe_config)
  9505. {
  9506. struct drm_atomic_state *state = pipe_config->base.state;
  9507. struct intel_encoder *encoder;
  9508. struct drm_connector *connector;
  9509. struct drm_connector_state *connector_state;
  9510. int base_bpp, ret = -EINVAL;
  9511. int i;
  9512. bool retry = true;
  9513. clear_intel_crtc_state(pipe_config);
  9514. pipe_config->cpu_transcoder =
  9515. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9516. /*
  9517. * Sanitize sync polarity flags based on requested ones. If neither
  9518. * positive or negative polarity is requested, treat this as meaning
  9519. * negative polarity.
  9520. */
  9521. if (!(pipe_config->base.adjusted_mode.flags &
  9522. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9523. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9524. if (!(pipe_config->base.adjusted_mode.flags &
  9525. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9526. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9527. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9528. pipe_config);
  9529. if (base_bpp < 0)
  9530. goto fail;
  9531. /*
  9532. * Determine the real pipe dimensions. Note that stereo modes can
  9533. * increase the actual pipe size due to the frame doubling and
  9534. * insertion of additional space for blanks between the frame. This
  9535. * is stored in the crtc timings. We use the requested mode to do this
  9536. * computation to clearly distinguish it from the adjusted mode, which
  9537. * can be changed by the connectors in the below retry loop.
  9538. */
  9539. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9540. &pipe_config->pipe_src_w,
  9541. &pipe_config->pipe_src_h);
  9542. for_each_connector_in_state(state, connector, connector_state, i) {
  9543. if (connector_state->crtc != crtc)
  9544. continue;
  9545. encoder = to_intel_encoder(connector_state->best_encoder);
  9546. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9547. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9548. goto fail;
  9549. }
  9550. /*
  9551. * Determine output_types before calling the .compute_config()
  9552. * hooks so that the hooks can use this information safely.
  9553. */
  9554. pipe_config->output_types |= 1 << encoder->type;
  9555. }
  9556. encoder_retry:
  9557. /* Ensure the port clock defaults are reset when retrying. */
  9558. pipe_config->port_clock = 0;
  9559. pipe_config->pixel_multiplier = 1;
  9560. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9561. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9562. CRTC_STEREO_DOUBLE);
  9563. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9564. * adjust it according to limitations or connector properties, and also
  9565. * a chance to reject the mode entirely.
  9566. */
  9567. for_each_connector_in_state(state, connector, connector_state, i) {
  9568. if (connector_state->crtc != crtc)
  9569. continue;
  9570. encoder = to_intel_encoder(connector_state->best_encoder);
  9571. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9572. DRM_DEBUG_KMS("Encoder config failure\n");
  9573. goto fail;
  9574. }
  9575. }
  9576. /* Set default port clock if not overwritten by the encoder. Needs to be
  9577. * done afterwards in case the encoder adjusts the mode. */
  9578. if (!pipe_config->port_clock)
  9579. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9580. * pipe_config->pixel_multiplier;
  9581. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9582. if (ret < 0) {
  9583. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9584. goto fail;
  9585. }
  9586. if (ret == RETRY) {
  9587. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9588. ret = -EINVAL;
  9589. goto fail;
  9590. }
  9591. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9592. retry = false;
  9593. goto encoder_retry;
  9594. }
  9595. /* Dithering seems to not pass-through bits correctly when it should, so
  9596. * only enable it on 6bpc panels and when its not a compliance
  9597. * test requesting 6bpc video pattern.
  9598. */
  9599. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9600. !pipe_config->dither_force_disable;
  9601. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9602. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9603. fail:
  9604. return ret;
  9605. }
  9606. static void
  9607. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  9608. {
  9609. struct drm_crtc *crtc;
  9610. struct drm_crtc_state *crtc_state;
  9611. int i;
  9612. /* Double check state. */
  9613. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  9614. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  9615. /* Update hwmode for vblank functions */
  9616. if (crtc->state->active)
  9617. crtc->hwmode = crtc->state->adjusted_mode;
  9618. else
  9619. crtc->hwmode.crtc_clock = 0;
  9620. /*
  9621. * Update legacy state to satisfy fbc code. This can
  9622. * be removed when fbc uses the atomic state.
  9623. */
  9624. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  9625. struct drm_plane_state *plane_state = crtc->primary->state;
  9626. crtc->primary->fb = plane_state->fb;
  9627. crtc->x = plane_state->src_x >> 16;
  9628. crtc->y = plane_state->src_y >> 16;
  9629. }
  9630. }
  9631. }
  9632. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9633. {
  9634. int diff;
  9635. if (clock1 == clock2)
  9636. return true;
  9637. if (!clock1 || !clock2)
  9638. return false;
  9639. diff = abs(clock1 - clock2);
  9640. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9641. return true;
  9642. return false;
  9643. }
  9644. static bool
  9645. intel_compare_m_n(unsigned int m, unsigned int n,
  9646. unsigned int m2, unsigned int n2,
  9647. bool exact)
  9648. {
  9649. if (m == m2 && n == n2)
  9650. return true;
  9651. if (exact || !m || !n || !m2 || !n2)
  9652. return false;
  9653. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9654. if (n > n2) {
  9655. while (n > n2) {
  9656. m2 <<= 1;
  9657. n2 <<= 1;
  9658. }
  9659. } else if (n < n2) {
  9660. while (n < n2) {
  9661. m <<= 1;
  9662. n <<= 1;
  9663. }
  9664. }
  9665. if (n != n2)
  9666. return false;
  9667. return intel_fuzzy_clock_check(m, m2);
  9668. }
  9669. static bool
  9670. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9671. struct intel_link_m_n *m2_n2,
  9672. bool adjust)
  9673. {
  9674. if (m_n->tu == m2_n2->tu &&
  9675. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9676. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9677. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9678. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9679. if (adjust)
  9680. *m2_n2 = *m_n;
  9681. return true;
  9682. }
  9683. return false;
  9684. }
  9685. static void __printf(3, 4)
  9686. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9687. {
  9688. char *level;
  9689. unsigned int category;
  9690. struct va_format vaf;
  9691. va_list args;
  9692. if (adjust) {
  9693. level = KERN_DEBUG;
  9694. category = DRM_UT_KMS;
  9695. } else {
  9696. level = KERN_ERR;
  9697. category = DRM_UT_NONE;
  9698. }
  9699. va_start(args, format);
  9700. vaf.fmt = format;
  9701. vaf.va = &args;
  9702. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  9703. va_end(args);
  9704. }
  9705. static bool
  9706. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9707. struct intel_crtc_state *current_config,
  9708. struct intel_crtc_state *pipe_config,
  9709. bool adjust)
  9710. {
  9711. bool ret = true;
  9712. #define PIPE_CONF_CHECK_X(name) \
  9713. if (current_config->name != pipe_config->name) { \
  9714. pipe_config_err(adjust, __stringify(name), \
  9715. "(expected 0x%08x, found 0x%08x)\n", \
  9716. current_config->name, \
  9717. pipe_config->name); \
  9718. ret = false; \
  9719. }
  9720. #define PIPE_CONF_CHECK_I(name) \
  9721. if (current_config->name != pipe_config->name) { \
  9722. pipe_config_err(adjust, __stringify(name), \
  9723. "(expected %i, found %i)\n", \
  9724. current_config->name, \
  9725. pipe_config->name); \
  9726. ret = false; \
  9727. }
  9728. #define PIPE_CONF_CHECK_P(name) \
  9729. if (current_config->name != pipe_config->name) { \
  9730. pipe_config_err(adjust, __stringify(name), \
  9731. "(expected %p, found %p)\n", \
  9732. current_config->name, \
  9733. pipe_config->name); \
  9734. ret = false; \
  9735. }
  9736. #define PIPE_CONF_CHECK_M_N(name) \
  9737. if (!intel_compare_link_m_n(&current_config->name, \
  9738. &pipe_config->name,\
  9739. adjust)) { \
  9740. pipe_config_err(adjust, __stringify(name), \
  9741. "(expected tu %i gmch %i/%i link %i/%i, " \
  9742. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9743. current_config->name.tu, \
  9744. current_config->name.gmch_m, \
  9745. current_config->name.gmch_n, \
  9746. current_config->name.link_m, \
  9747. current_config->name.link_n, \
  9748. pipe_config->name.tu, \
  9749. pipe_config->name.gmch_m, \
  9750. pipe_config->name.gmch_n, \
  9751. pipe_config->name.link_m, \
  9752. pipe_config->name.link_n); \
  9753. ret = false; \
  9754. }
  9755. /* This is required for BDW+ where there is only one set of registers for
  9756. * switching between high and low RR.
  9757. * This macro can be used whenever a comparison has to be made between one
  9758. * hw state and multiple sw state variables.
  9759. */
  9760. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9761. if (!intel_compare_link_m_n(&current_config->name, \
  9762. &pipe_config->name, adjust) && \
  9763. !intel_compare_link_m_n(&current_config->alt_name, \
  9764. &pipe_config->name, adjust)) { \
  9765. pipe_config_err(adjust, __stringify(name), \
  9766. "(expected tu %i gmch %i/%i link %i/%i, " \
  9767. "or tu %i gmch %i/%i link %i/%i, " \
  9768. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9769. current_config->name.tu, \
  9770. current_config->name.gmch_m, \
  9771. current_config->name.gmch_n, \
  9772. current_config->name.link_m, \
  9773. current_config->name.link_n, \
  9774. current_config->alt_name.tu, \
  9775. current_config->alt_name.gmch_m, \
  9776. current_config->alt_name.gmch_n, \
  9777. current_config->alt_name.link_m, \
  9778. current_config->alt_name.link_n, \
  9779. pipe_config->name.tu, \
  9780. pipe_config->name.gmch_m, \
  9781. pipe_config->name.gmch_n, \
  9782. pipe_config->name.link_m, \
  9783. pipe_config->name.link_n); \
  9784. ret = false; \
  9785. }
  9786. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9787. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9788. pipe_config_err(adjust, __stringify(name), \
  9789. "(%x) (expected %i, found %i)\n", \
  9790. (mask), \
  9791. current_config->name & (mask), \
  9792. pipe_config->name & (mask)); \
  9793. ret = false; \
  9794. }
  9795. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9796. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9797. pipe_config_err(adjust, __stringify(name), \
  9798. "(expected %i, found %i)\n", \
  9799. current_config->name, \
  9800. pipe_config->name); \
  9801. ret = false; \
  9802. }
  9803. #define PIPE_CONF_QUIRK(quirk) \
  9804. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9805. PIPE_CONF_CHECK_I(cpu_transcoder);
  9806. PIPE_CONF_CHECK_I(has_pch_encoder);
  9807. PIPE_CONF_CHECK_I(fdi_lanes);
  9808. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9809. PIPE_CONF_CHECK_I(lane_count);
  9810. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9811. if (INTEL_GEN(dev_priv) < 8) {
  9812. PIPE_CONF_CHECK_M_N(dp_m_n);
  9813. if (current_config->has_drrs)
  9814. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9815. } else
  9816. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9817. PIPE_CONF_CHECK_X(output_types);
  9818. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9819. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9820. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9821. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9822. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9823. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9824. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9825. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9826. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9827. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9828. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9829. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9830. PIPE_CONF_CHECK_I(pixel_multiplier);
  9831. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9832. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9833. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9834. PIPE_CONF_CHECK_I(limited_color_range);
  9835. PIPE_CONF_CHECK_I(has_infoframe);
  9836. PIPE_CONF_CHECK_I(has_audio);
  9837. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9838. DRM_MODE_FLAG_INTERLACE);
  9839. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9840. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9841. DRM_MODE_FLAG_PHSYNC);
  9842. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9843. DRM_MODE_FLAG_NHSYNC);
  9844. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9845. DRM_MODE_FLAG_PVSYNC);
  9846. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9847. DRM_MODE_FLAG_NVSYNC);
  9848. }
  9849. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9850. /* pfit ratios are autocomputed by the hw on gen4+ */
  9851. if (INTEL_GEN(dev_priv) < 4)
  9852. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9853. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9854. if (!adjust) {
  9855. PIPE_CONF_CHECK_I(pipe_src_w);
  9856. PIPE_CONF_CHECK_I(pipe_src_h);
  9857. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9858. if (current_config->pch_pfit.enabled) {
  9859. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9860. PIPE_CONF_CHECK_X(pch_pfit.size);
  9861. }
  9862. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9863. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9864. }
  9865. /* BDW+ don't expose a synchronous way to read the state */
  9866. if (IS_HASWELL(dev_priv))
  9867. PIPE_CONF_CHECK_I(ips_enabled);
  9868. PIPE_CONF_CHECK_I(double_wide);
  9869. PIPE_CONF_CHECK_P(shared_dpll);
  9870. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9871. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9872. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9873. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9874. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9875. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9876. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9877. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9878. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9879. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9880. PIPE_CONF_CHECK_X(dsi_pll.div);
  9881. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9882. PIPE_CONF_CHECK_I(pipe_bpp);
  9883. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9884. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9885. #undef PIPE_CONF_CHECK_X
  9886. #undef PIPE_CONF_CHECK_I
  9887. #undef PIPE_CONF_CHECK_P
  9888. #undef PIPE_CONF_CHECK_FLAGS
  9889. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9890. #undef PIPE_CONF_QUIRK
  9891. return ret;
  9892. }
  9893. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9894. const struct intel_crtc_state *pipe_config)
  9895. {
  9896. if (pipe_config->has_pch_encoder) {
  9897. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9898. &pipe_config->fdi_m_n);
  9899. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9900. /*
  9901. * FDI already provided one idea for the dotclock.
  9902. * Yell if the encoder disagrees.
  9903. */
  9904. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9905. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9906. fdi_dotclock, dotclock);
  9907. }
  9908. }
  9909. static void verify_wm_state(struct drm_crtc *crtc,
  9910. struct drm_crtc_state *new_state)
  9911. {
  9912. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9913. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9914. struct skl_pipe_wm hw_wm, *sw_wm;
  9915. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9916. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9917. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9918. const enum pipe pipe = intel_crtc->pipe;
  9919. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9920. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9921. return;
  9922. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9923. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9924. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9925. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9926. /* planes */
  9927. for_each_universal_plane(dev_priv, pipe, plane) {
  9928. hw_plane_wm = &hw_wm.planes[plane];
  9929. sw_plane_wm = &sw_wm->planes[plane];
  9930. /* Watermarks */
  9931. for (level = 0; level <= max_level; level++) {
  9932. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9933. &sw_plane_wm->wm[level]))
  9934. continue;
  9935. 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",
  9936. pipe_name(pipe), plane + 1, level,
  9937. sw_plane_wm->wm[level].plane_en,
  9938. sw_plane_wm->wm[level].plane_res_b,
  9939. sw_plane_wm->wm[level].plane_res_l,
  9940. hw_plane_wm->wm[level].plane_en,
  9941. hw_plane_wm->wm[level].plane_res_b,
  9942. hw_plane_wm->wm[level].plane_res_l);
  9943. }
  9944. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9945. &sw_plane_wm->trans_wm)) {
  9946. 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",
  9947. pipe_name(pipe), plane + 1,
  9948. sw_plane_wm->trans_wm.plane_en,
  9949. sw_plane_wm->trans_wm.plane_res_b,
  9950. sw_plane_wm->trans_wm.plane_res_l,
  9951. hw_plane_wm->trans_wm.plane_en,
  9952. hw_plane_wm->trans_wm.plane_res_b,
  9953. hw_plane_wm->trans_wm.plane_res_l);
  9954. }
  9955. /* DDB */
  9956. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9957. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9958. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9959. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9960. pipe_name(pipe), plane + 1,
  9961. sw_ddb_entry->start, sw_ddb_entry->end,
  9962. hw_ddb_entry->start, hw_ddb_entry->end);
  9963. }
  9964. }
  9965. /*
  9966. * cursor
  9967. * If the cursor plane isn't active, we may not have updated it's ddb
  9968. * allocation. In that case since the ddb allocation will be updated
  9969. * once the plane becomes visible, we can skip this check
  9970. */
  9971. if (intel_crtc->cursor_addr) {
  9972. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9973. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9974. /* Watermarks */
  9975. for (level = 0; level <= max_level; level++) {
  9976. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9977. &sw_plane_wm->wm[level]))
  9978. continue;
  9979. 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",
  9980. pipe_name(pipe), level,
  9981. sw_plane_wm->wm[level].plane_en,
  9982. sw_plane_wm->wm[level].plane_res_b,
  9983. sw_plane_wm->wm[level].plane_res_l,
  9984. hw_plane_wm->wm[level].plane_en,
  9985. hw_plane_wm->wm[level].plane_res_b,
  9986. hw_plane_wm->wm[level].plane_res_l);
  9987. }
  9988. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9989. &sw_plane_wm->trans_wm)) {
  9990. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9991. pipe_name(pipe),
  9992. sw_plane_wm->trans_wm.plane_en,
  9993. sw_plane_wm->trans_wm.plane_res_b,
  9994. sw_plane_wm->trans_wm.plane_res_l,
  9995. hw_plane_wm->trans_wm.plane_en,
  9996. hw_plane_wm->trans_wm.plane_res_b,
  9997. hw_plane_wm->trans_wm.plane_res_l);
  9998. }
  9999. /* DDB */
  10000. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10001. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10002. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  10003. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  10004. pipe_name(pipe),
  10005. sw_ddb_entry->start, sw_ddb_entry->end,
  10006. hw_ddb_entry->start, hw_ddb_entry->end);
  10007. }
  10008. }
  10009. }
  10010. static void
  10011. verify_connector_state(struct drm_device *dev,
  10012. struct drm_atomic_state *state,
  10013. struct drm_crtc *crtc)
  10014. {
  10015. struct drm_connector *connector;
  10016. struct drm_connector_state *old_conn_state;
  10017. int i;
  10018. for_each_connector_in_state(state, connector, old_conn_state, i) {
  10019. struct drm_encoder *encoder = connector->encoder;
  10020. struct drm_connector_state *state = connector->state;
  10021. if (state->crtc != crtc)
  10022. continue;
  10023. intel_connector_verify_state(to_intel_connector(connector));
  10024. I915_STATE_WARN(state->best_encoder != encoder,
  10025. "connector's atomic encoder doesn't match legacy encoder\n");
  10026. }
  10027. }
  10028. static void
  10029. verify_encoder_state(struct drm_device *dev)
  10030. {
  10031. struct intel_encoder *encoder;
  10032. struct intel_connector *connector;
  10033. for_each_intel_encoder(dev, encoder) {
  10034. bool enabled = false;
  10035. enum pipe pipe;
  10036. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10037. encoder->base.base.id,
  10038. encoder->base.name);
  10039. for_each_intel_connector(dev, connector) {
  10040. if (connector->base.state->best_encoder != &encoder->base)
  10041. continue;
  10042. enabled = true;
  10043. I915_STATE_WARN(connector->base.state->crtc !=
  10044. encoder->base.crtc,
  10045. "connector's crtc doesn't match encoder crtc\n");
  10046. }
  10047. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10048. "encoder's enabled state mismatch "
  10049. "(expected %i, found %i)\n",
  10050. !!encoder->base.crtc, enabled);
  10051. if (!encoder->base.crtc) {
  10052. bool active;
  10053. active = encoder->get_hw_state(encoder, &pipe);
  10054. I915_STATE_WARN(active,
  10055. "encoder detached but still enabled on pipe %c.\n",
  10056. pipe_name(pipe));
  10057. }
  10058. }
  10059. }
  10060. static void
  10061. verify_crtc_state(struct drm_crtc *crtc,
  10062. struct drm_crtc_state *old_crtc_state,
  10063. struct drm_crtc_state *new_crtc_state)
  10064. {
  10065. struct drm_device *dev = crtc->dev;
  10066. struct drm_i915_private *dev_priv = to_i915(dev);
  10067. struct intel_encoder *encoder;
  10068. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10069. struct intel_crtc_state *pipe_config, *sw_config;
  10070. struct drm_atomic_state *old_state;
  10071. bool active;
  10072. old_state = old_crtc_state->state;
  10073. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10074. pipe_config = to_intel_crtc_state(old_crtc_state);
  10075. memset(pipe_config, 0, sizeof(*pipe_config));
  10076. pipe_config->base.crtc = crtc;
  10077. pipe_config->base.state = old_state;
  10078. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  10079. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10080. /* hw state is inconsistent with the pipe quirk */
  10081. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10082. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10083. active = new_crtc_state->active;
  10084. I915_STATE_WARN(new_crtc_state->active != active,
  10085. "crtc active state doesn't match with hw state "
  10086. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10087. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10088. "transitional active state does not match atomic hw state "
  10089. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10090. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10091. enum pipe pipe;
  10092. active = encoder->get_hw_state(encoder, &pipe);
  10093. I915_STATE_WARN(active != new_crtc_state->active,
  10094. "[ENCODER:%i] active %i with crtc active %i\n",
  10095. encoder->base.base.id, active, new_crtc_state->active);
  10096. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10097. "Encoder connected to wrong pipe %c\n",
  10098. pipe_name(pipe));
  10099. if (active) {
  10100. pipe_config->output_types |= 1 << encoder->type;
  10101. encoder->get_config(encoder, pipe_config);
  10102. }
  10103. }
  10104. intel_crtc_compute_pixel_rate(pipe_config);
  10105. if (!new_crtc_state->active)
  10106. return;
  10107. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10108. sw_config = to_intel_crtc_state(crtc->state);
  10109. if (!intel_pipe_config_compare(dev_priv, sw_config,
  10110. pipe_config, false)) {
  10111. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10112. intel_dump_pipe_config(intel_crtc, pipe_config,
  10113. "[hw state]");
  10114. intel_dump_pipe_config(intel_crtc, sw_config,
  10115. "[sw state]");
  10116. }
  10117. }
  10118. static void
  10119. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10120. struct intel_shared_dpll *pll,
  10121. struct drm_crtc *crtc,
  10122. struct drm_crtc_state *new_state)
  10123. {
  10124. struct intel_dpll_hw_state dpll_hw_state;
  10125. unsigned crtc_mask;
  10126. bool active;
  10127. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10128. DRM_DEBUG_KMS("%s\n", pll->name);
  10129. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10130. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10131. I915_STATE_WARN(!pll->on && pll->active_mask,
  10132. "pll in active use but not on in sw tracking\n");
  10133. I915_STATE_WARN(pll->on && !pll->active_mask,
  10134. "pll is on but not used by any active crtc\n");
  10135. I915_STATE_WARN(pll->on != active,
  10136. "pll on state mismatch (expected %i, found %i)\n",
  10137. pll->on, active);
  10138. }
  10139. if (!crtc) {
  10140. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  10141. "more active pll users than references: %x vs %x\n",
  10142. pll->active_mask, pll->state.crtc_mask);
  10143. return;
  10144. }
  10145. crtc_mask = 1 << drm_crtc_index(crtc);
  10146. if (new_state->active)
  10147. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10148. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10149. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10150. else
  10151. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10152. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10153. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10154. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  10155. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10156. crtc_mask, pll->state.crtc_mask);
  10157. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  10158. &dpll_hw_state,
  10159. sizeof(dpll_hw_state)),
  10160. "pll hw state mismatch\n");
  10161. }
  10162. static void
  10163. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10164. struct drm_crtc_state *old_crtc_state,
  10165. struct drm_crtc_state *new_crtc_state)
  10166. {
  10167. struct drm_i915_private *dev_priv = to_i915(dev);
  10168. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10169. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10170. if (new_state->shared_dpll)
  10171. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10172. if (old_state->shared_dpll &&
  10173. old_state->shared_dpll != new_state->shared_dpll) {
  10174. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  10175. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10176. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10177. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10178. pipe_name(drm_crtc_index(crtc)));
  10179. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  10180. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10181. pipe_name(drm_crtc_index(crtc)));
  10182. }
  10183. }
  10184. static void
  10185. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  10186. struct drm_atomic_state *state,
  10187. struct drm_crtc_state *old_state,
  10188. struct drm_crtc_state *new_state)
  10189. {
  10190. if (!needs_modeset(new_state) &&
  10191. !to_intel_crtc_state(new_state)->update_pipe)
  10192. return;
  10193. verify_wm_state(crtc, new_state);
  10194. verify_connector_state(crtc->dev, state, crtc);
  10195. verify_crtc_state(crtc, old_state, new_state);
  10196. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  10197. }
  10198. static void
  10199. verify_disabled_dpll_state(struct drm_device *dev)
  10200. {
  10201. struct drm_i915_private *dev_priv = to_i915(dev);
  10202. int i;
  10203. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10204. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  10205. }
  10206. static void
  10207. intel_modeset_verify_disabled(struct drm_device *dev,
  10208. struct drm_atomic_state *state)
  10209. {
  10210. verify_encoder_state(dev);
  10211. verify_connector_state(dev, state, NULL);
  10212. verify_disabled_dpll_state(dev);
  10213. }
  10214. static void update_scanline_offset(struct intel_crtc *crtc)
  10215. {
  10216. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10217. /*
  10218. * The scanline counter increments at the leading edge of hsync.
  10219. *
  10220. * On most platforms it starts counting from vtotal-1 on the
  10221. * first active line. That means the scanline counter value is
  10222. * always one less than what we would expect. Ie. just after
  10223. * start of vblank, which also occurs at start of hsync (on the
  10224. * last active line), the scanline counter will read vblank_start-1.
  10225. *
  10226. * On gen2 the scanline counter starts counting from 1 instead
  10227. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10228. * to keep the value positive), instead of adding one.
  10229. *
  10230. * On HSW+ the behaviour of the scanline counter depends on the output
  10231. * type. For DP ports it behaves like most other platforms, but on HDMI
  10232. * there's an extra 1 line difference. So we need to add two instead of
  10233. * one to the value.
  10234. */
  10235. if (IS_GEN2(dev_priv)) {
  10236. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10237. int vtotal;
  10238. vtotal = adjusted_mode->crtc_vtotal;
  10239. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10240. vtotal /= 2;
  10241. crtc->scanline_offset = vtotal - 1;
  10242. } else if (HAS_DDI(dev_priv) &&
  10243. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  10244. crtc->scanline_offset = 2;
  10245. } else
  10246. crtc->scanline_offset = 1;
  10247. }
  10248. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10249. {
  10250. struct drm_device *dev = state->dev;
  10251. struct drm_i915_private *dev_priv = to_i915(dev);
  10252. struct drm_crtc *crtc;
  10253. struct drm_crtc_state *crtc_state;
  10254. int i;
  10255. if (!dev_priv->display.crtc_compute_clock)
  10256. return;
  10257. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10258. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10259. struct intel_shared_dpll *old_dpll =
  10260. to_intel_crtc_state(crtc->state)->shared_dpll;
  10261. if (!needs_modeset(crtc_state))
  10262. continue;
  10263. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  10264. if (!old_dpll)
  10265. continue;
  10266. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  10267. }
  10268. }
  10269. /*
  10270. * This implements the workaround described in the "notes" section of the mode
  10271. * set sequence documentation. When going from no pipes or single pipe to
  10272. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10273. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10274. */
  10275. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10276. {
  10277. struct drm_crtc_state *crtc_state;
  10278. struct intel_crtc *intel_crtc;
  10279. struct drm_crtc *crtc;
  10280. struct intel_crtc_state *first_crtc_state = NULL;
  10281. struct intel_crtc_state *other_crtc_state = NULL;
  10282. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10283. int i;
  10284. /* look at all crtc's that are going to be enabled in during modeset */
  10285. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10286. intel_crtc = to_intel_crtc(crtc);
  10287. if (!crtc_state->active || !needs_modeset(crtc_state))
  10288. continue;
  10289. if (first_crtc_state) {
  10290. other_crtc_state = to_intel_crtc_state(crtc_state);
  10291. break;
  10292. } else {
  10293. first_crtc_state = to_intel_crtc_state(crtc_state);
  10294. first_pipe = intel_crtc->pipe;
  10295. }
  10296. }
  10297. /* No workaround needed? */
  10298. if (!first_crtc_state)
  10299. return 0;
  10300. /* w/a possibly needed, check how many crtc's are already enabled. */
  10301. for_each_intel_crtc(state->dev, intel_crtc) {
  10302. struct intel_crtc_state *pipe_config;
  10303. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10304. if (IS_ERR(pipe_config))
  10305. return PTR_ERR(pipe_config);
  10306. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10307. if (!pipe_config->base.active ||
  10308. needs_modeset(&pipe_config->base))
  10309. continue;
  10310. /* 2 or more enabled crtcs means no need for w/a */
  10311. if (enabled_pipe != INVALID_PIPE)
  10312. return 0;
  10313. enabled_pipe = intel_crtc->pipe;
  10314. }
  10315. if (enabled_pipe != INVALID_PIPE)
  10316. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10317. else if (other_crtc_state)
  10318. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10319. return 0;
  10320. }
  10321. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  10322. {
  10323. struct drm_crtc *crtc;
  10324. /* Add all pipes to the state */
  10325. for_each_crtc(state->dev, crtc) {
  10326. struct drm_crtc_state *crtc_state;
  10327. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10328. if (IS_ERR(crtc_state))
  10329. return PTR_ERR(crtc_state);
  10330. }
  10331. return 0;
  10332. }
  10333. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10334. {
  10335. struct drm_crtc *crtc;
  10336. /*
  10337. * Add all pipes to the state, and force
  10338. * a modeset on all the active ones.
  10339. */
  10340. for_each_crtc(state->dev, crtc) {
  10341. struct drm_crtc_state *crtc_state;
  10342. int ret;
  10343. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10344. if (IS_ERR(crtc_state))
  10345. return PTR_ERR(crtc_state);
  10346. if (!crtc_state->active || needs_modeset(crtc_state))
  10347. continue;
  10348. crtc_state->mode_changed = true;
  10349. ret = drm_atomic_add_affected_connectors(state, crtc);
  10350. if (ret)
  10351. return ret;
  10352. ret = drm_atomic_add_affected_planes(state, crtc);
  10353. if (ret)
  10354. return ret;
  10355. }
  10356. return 0;
  10357. }
  10358. static int intel_modeset_checks(struct drm_atomic_state *state)
  10359. {
  10360. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10361. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10362. struct drm_crtc *crtc;
  10363. struct drm_crtc_state *crtc_state;
  10364. int ret = 0, i;
  10365. if (!check_digital_port_conflicts(state)) {
  10366. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10367. return -EINVAL;
  10368. }
  10369. intel_state->modeset = true;
  10370. intel_state->active_crtcs = dev_priv->active_crtcs;
  10371. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10372. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  10373. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10374. if (crtc_state->active)
  10375. intel_state->active_crtcs |= 1 << i;
  10376. else
  10377. intel_state->active_crtcs &= ~(1 << i);
  10378. if (crtc_state->active != crtc->state->active)
  10379. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10380. }
  10381. /*
  10382. * See if the config requires any additional preparation, e.g.
  10383. * to adjust global state with pipes off. We need to do this
  10384. * here so we can get the modeset_pipe updated config for the new
  10385. * mode set on this crtc. For other crtcs we need to use the
  10386. * adjusted_mode bits in the crtc directly.
  10387. */
  10388. if (dev_priv->display.modeset_calc_cdclk) {
  10389. ret = dev_priv->display.modeset_calc_cdclk(state);
  10390. if (ret < 0)
  10391. return ret;
  10392. /*
  10393. * Writes to dev_priv->cdclk.logical must protected by
  10394. * holding all the crtc locks, even if we don't end up
  10395. * touching the hardware
  10396. */
  10397. if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
  10398. &intel_state->cdclk.logical)) {
  10399. ret = intel_lock_all_pipes(state);
  10400. if (ret < 0)
  10401. return ret;
  10402. }
  10403. /* All pipes must be switched off while we change the cdclk. */
  10404. if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
  10405. &intel_state->cdclk.actual)) {
  10406. ret = intel_modeset_all_pipes(state);
  10407. if (ret < 0)
  10408. return ret;
  10409. }
  10410. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10411. intel_state->cdclk.logical.cdclk,
  10412. intel_state->cdclk.actual.cdclk);
  10413. } else {
  10414. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10415. }
  10416. intel_modeset_clear_plls(state);
  10417. if (IS_HASWELL(dev_priv))
  10418. return haswell_mode_set_planes_workaround(state);
  10419. return 0;
  10420. }
  10421. /*
  10422. * Handle calculation of various watermark data at the end of the atomic check
  10423. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10424. * handlers to ensure that all derived state has been updated.
  10425. */
  10426. static int calc_watermark_data(struct drm_atomic_state *state)
  10427. {
  10428. struct drm_device *dev = state->dev;
  10429. struct drm_i915_private *dev_priv = to_i915(dev);
  10430. /* Is there platform-specific watermark information to calculate? */
  10431. if (dev_priv->display.compute_global_watermarks)
  10432. return dev_priv->display.compute_global_watermarks(state);
  10433. return 0;
  10434. }
  10435. /**
  10436. * intel_atomic_check - validate state object
  10437. * @dev: drm device
  10438. * @state: state to validate
  10439. */
  10440. static int intel_atomic_check(struct drm_device *dev,
  10441. struct drm_atomic_state *state)
  10442. {
  10443. struct drm_i915_private *dev_priv = to_i915(dev);
  10444. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10445. struct drm_crtc *crtc;
  10446. struct drm_crtc_state *crtc_state;
  10447. int ret, i;
  10448. bool any_ms = false;
  10449. ret = drm_atomic_helper_check_modeset(dev, state);
  10450. if (ret)
  10451. return ret;
  10452. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10453. struct intel_crtc_state *pipe_config =
  10454. to_intel_crtc_state(crtc_state);
  10455. /* Catch I915_MODE_FLAG_INHERITED */
  10456. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  10457. crtc_state->mode_changed = true;
  10458. if (!needs_modeset(crtc_state))
  10459. continue;
  10460. if (!crtc_state->enable) {
  10461. any_ms = true;
  10462. continue;
  10463. }
  10464. /* FIXME: For only active_changed we shouldn't need to do any
  10465. * state recomputation at all. */
  10466. ret = drm_atomic_add_affected_connectors(state, crtc);
  10467. if (ret)
  10468. return ret;
  10469. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10470. if (ret) {
  10471. intel_dump_pipe_config(to_intel_crtc(crtc),
  10472. pipe_config, "[failed]");
  10473. return ret;
  10474. }
  10475. if (i915.fastboot &&
  10476. intel_pipe_config_compare(dev_priv,
  10477. to_intel_crtc_state(crtc->state),
  10478. pipe_config, true)) {
  10479. crtc_state->mode_changed = false;
  10480. to_intel_crtc_state(crtc_state)->update_pipe = true;
  10481. }
  10482. if (needs_modeset(crtc_state))
  10483. any_ms = true;
  10484. ret = drm_atomic_add_affected_planes(state, crtc);
  10485. if (ret)
  10486. return ret;
  10487. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10488. needs_modeset(crtc_state) ?
  10489. "[modeset]" : "[fastset]");
  10490. }
  10491. if (any_ms) {
  10492. ret = intel_modeset_checks(state);
  10493. if (ret)
  10494. return ret;
  10495. } else {
  10496. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10497. }
  10498. ret = drm_atomic_helper_check_planes(dev, state);
  10499. if (ret)
  10500. return ret;
  10501. intel_fbc_choose_crtc(dev_priv, state);
  10502. return calc_watermark_data(state);
  10503. }
  10504. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10505. struct drm_atomic_state *state)
  10506. {
  10507. struct drm_i915_private *dev_priv = to_i915(dev);
  10508. struct drm_crtc_state *crtc_state;
  10509. struct drm_crtc *crtc;
  10510. int i, ret;
  10511. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10512. if (state->legacy_cursor_update)
  10513. continue;
  10514. ret = intel_crtc_wait_for_pending_flips(crtc);
  10515. if (ret)
  10516. return ret;
  10517. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  10518. flush_workqueue(dev_priv->wq);
  10519. }
  10520. ret = mutex_lock_interruptible(&dev->struct_mutex);
  10521. if (ret)
  10522. return ret;
  10523. ret = drm_atomic_helper_prepare_planes(dev, state);
  10524. mutex_unlock(&dev->struct_mutex);
  10525. return ret;
  10526. }
  10527. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10528. {
  10529. struct drm_device *dev = crtc->base.dev;
  10530. if (!dev->max_vblank_count)
  10531. return drm_accurate_vblank_count(&crtc->base);
  10532. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10533. }
  10534. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  10535. struct drm_i915_private *dev_priv,
  10536. unsigned crtc_mask)
  10537. {
  10538. unsigned last_vblank_count[I915_MAX_PIPES];
  10539. enum pipe pipe;
  10540. int ret;
  10541. if (!crtc_mask)
  10542. return;
  10543. for_each_pipe(dev_priv, pipe) {
  10544. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10545. pipe);
  10546. if (!((1 << pipe) & crtc_mask))
  10547. continue;
  10548. ret = drm_crtc_vblank_get(&crtc->base);
  10549. if (WARN_ON(ret != 0)) {
  10550. crtc_mask &= ~(1 << pipe);
  10551. continue;
  10552. }
  10553. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  10554. }
  10555. for_each_pipe(dev_priv, pipe) {
  10556. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10557. pipe);
  10558. long lret;
  10559. if (!((1 << pipe) & crtc_mask))
  10560. continue;
  10561. lret = wait_event_timeout(dev->vblank[pipe].queue,
  10562. last_vblank_count[pipe] !=
  10563. drm_crtc_vblank_count(&crtc->base),
  10564. msecs_to_jiffies(50));
  10565. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  10566. drm_crtc_vblank_put(&crtc->base);
  10567. }
  10568. }
  10569. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  10570. {
  10571. /* fb updated, need to unpin old fb */
  10572. if (crtc_state->fb_changed)
  10573. return true;
  10574. /* wm changes, need vblank before final wm's */
  10575. if (crtc_state->update_wm_post)
  10576. return true;
  10577. /*
  10578. * cxsr is re-enabled after vblank.
  10579. * This is already handled by crtc_state->update_wm_post,
  10580. * but added for clarity.
  10581. */
  10582. if (crtc_state->disable_cxsr)
  10583. return true;
  10584. return false;
  10585. }
  10586. static void intel_update_crtc(struct drm_crtc *crtc,
  10587. struct drm_atomic_state *state,
  10588. struct drm_crtc_state *old_crtc_state,
  10589. unsigned int *crtc_vblank_mask)
  10590. {
  10591. struct drm_device *dev = crtc->dev;
  10592. struct drm_i915_private *dev_priv = to_i915(dev);
  10593. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10594. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
  10595. bool modeset = needs_modeset(crtc->state);
  10596. if (modeset) {
  10597. update_scanline_offset(intel_crtc);
  10598. dev_priv->display.crtc_enable(pipe_config, state);
  10599. } else {
  10600. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  10601. }
  10602. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10603. intel_fbc_enable(
  10604. intel_crtc, pipe_config,
  10605. to_intel_plane_state(crtc->primary->state));
  10606. }
  10607. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10608. if (needs_vblank_wait(pipe_config))
  10609. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  10610. }
  10611. static void intel_update_crtcs(struct drm_atomic_state *state,
  10612. unsigned int *crtc_vblank_mask)
  10613. {
  10614. struct drm_crtc *crtc;
  10615. struct drm_crtc_state *old_crtc_state;
  10616. int i;
  10617. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  10618. if (!crtc->state->active)
  10619. continue;
  10620. intel_update_crtc(crtc, state, old_crtc_state,
  10621. crtc_vblank_mask);
  10622. }
  10623. }
  10624. static void skl_update_crtcs(struct drm_atomic_state *state,
  10625. unsigned int *crtc_vblank_mask)
  10626. {
  10627. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10628. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10629. struct drm_crtc *crtc;
  10630. struct intel_crtc *intel_crtc;
  10631. struct drm_crtc_state *old_crtc_state;
  10632. struct intel_crtc_state *cstate;
  10633. unsigned int updated = 0;
  10634. bool progress;
  10635. enum pipe pipe;
  10636. int i;
  10637. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10638. for_each_crtc_in_state(state, crtc, old_crtc_state, i)
  10639. /* ignore allocations for crtc's that have been turned off. */
  10640. if (crtc->state->active)
  10641. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10642. /*
  10643. * Whenever the number of active pipes changes, we need to make sure we
  10644. * update the pipes in the right order so that their ddb allocations
  10645. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10646. * cause pipe underruns and other bad stuff.
  10647. */
  10648. do {
  10649. progress = false;
  10650. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  10651. bool vbl_wait = false;
  10652. unsigned int cmask = drm_crtc_mask(crtc);
  10653. intel_crtc = to_intel_crtc(crtc);
  10654. cstate = to_intel_crtc_state(crtc->state);
  10655. pipe = intel_crtc->pipe;
  10656. if (updated & cmask || !cstate->base.active)
  10657. continue;
  10658. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  10659. continue;
  10660. updated |= cmask;
  10661. entries[i] = &cstate->wm.skl.ddb;
  10662. /*
  10663. * If this is an already active pipe, it's DDB changed,
  10664. * and this isn't the last pipe that needs updating
  10665. * then we need to wait for a vblank to pass for the
  10666. * new ddb allocation to take effect.
  10667. */
  10668. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10669. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10670. !crtc->state->active_changed &&
  10671. intel_state->wm_results.dirty_pipes != updated)
  10672. vbl_wait = true;
  10673. intel_update_crtc(crtc, state, old_crtc_state,
  10674. crtc_vblank_mask);
  10675. if (vbl_wait)
  10676. intel_wait_for_vblank(dev_priv, pipe);
  10677. progress = true;
  10678. }
  10679. } while (progress);
  10680. }
  10681. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10682. {
  10683. struct intel_atomic_state *state, *next;
  10684. struct llist_node *freed;
  10685. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10686. llist_for_each_entry_safe(state, next, freed, freed)
  10687. drm_atomic_state_put(&state->base);
  10688. }
  10689. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10690. {
  10691. struct drm_i915_private *dev_priv =
  10692. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10693. intel_atomic_helper_free_state(dev_priv);
  10694. }
  10695. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10696. {
  10697. struct drm_device *dev = state->dev;
  10698. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10699. struct drm_i915_private *dev_priv = to_i915(dev);
  10700. struct drm_crtc_state *old_crtc_state;
  10701. struct drm_crtc *crtc;
  10702. struct intel_crtc_state *intel_cstate;
  10703. bool hw_check = intel_state->modeset;
  10704. u64 put_domains[I915_MAX_PIPES] = {};
  10705. unsigned crtc_vblank_mask = 0;
  10706. int i;
  10707. drm_atomic_helper_wait_for_dependencies(state);
  10708. if (intel_state->modeset)
  10709. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10710. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  10711. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10712. if (needs_modeset(crtc->state) ||
  10713. to_intel_crtc_state(crtc->state)->update_pipe) {
  10714. hw_check = true;
  10715. put_domains[to_intel_crtc(crtc)->pipe] =
  10716. modeset_get_crtc_power_domains(crtc,
  10717. to_intel_crtc_state(crtc->state));
  10718. }
  10719. if (!needs_modeset(crtc->state))
  10720. continue;
  10721. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  10722. if (old_crtc_state->active) {
  10723. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10724. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10725. intel_crtc->active = false;
  10726. intel_fbc_disable(intel_crtc);
  10727. intel_disable_shared_dpll(intel_crtc);
  10728. /*
  10729. * Underruns don't always raise
  10730. * interrupts, so check manually.
  10731. */
  10732. intel_check_cpu_fifo_underruns(dev_priv);
  10733. intel_check_pch_fifo_underruns(dev_priv);
  10734. if (!crtc->state->active) {
  10735. /*
  10736. * Make sure we don't call initial_watermarks
  10737. * for ILK-style watermark updates.
  10738. */
  10739. if (dev_priv->display.atomic_update_watermarks)
  10740. dev_priv->display.initial_watermarks(intel_state,
  10741. to_intel_crtc_state(crtc->state));
  10742. else
  10743. intel_update_watermarks(intel_crtc);
  10744. }
  10745. }
  10746. }
  10747. /* Only after disabling all output pipelines that will be changed can we
  10748. * update the the output configuration. */
  10749. intel_modeset_update_crtc_state(state);
  10750. if (intel_state->modeset) {
  10751. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10752. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10753. /*
  10754. * SKL workaround: bspec recommends we disable the SAGV when we
  10755. * have more then one pipe enabled
  10756. */
  10757. if (!intel_can_enable_sagv(state))
  10758. intel_disable_sagv(dev_priv);
  10759. intel_modeset_verify_disabled(dev, state);
  10760. }
  10761. /* Complete the events for pipes that have now been disabled */
  10762. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  10763. bool modeset = needs_modeset(crtc->state);
  10764. /* Complete events for now disable pipes here. */
  10765. if (modeset && !crtc->state->active && crtc->state->event) {
  10766. spin_lock_irq(&dev->event_lock);
  10767. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  10768. spin_unlock_irq(&dev->event_lock);
  10769. crtc->state->event = NULL;
  10770. }
  10771. }
  10772. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10773. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  10774. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10775. * already, but still need the state for the delayed optimization. To
  10776. * fix this:
  10777. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10778. * - schedule that vblank worker _before_ calling hw_done
  10779. * - at the start of commit_tail, cancel it _synchrously
  10780. * - switch over to the vblank wait helper in the core after that since
  10781. * we don't need out special handling any more.
  10782. */
  10783. if (!state->legacy_cursor_update)
  10784. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  10785. /*
  10786. * Now that the vblank has passed, we can go ahead and program the
  10787. * optimal watermarks on platforms that need two-step watermark
  10788. * programming.
  10789. *
  10790. * TODO: Move this (and other cleanup) to an async worker eventually.
  10791. */
  10792. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  10793. intel_cstate = to_intel_crtc_state(crtc->state);
  10794. if (dev_priv->display.optimize_watermarks)
  10795. dev_priv->display.optimize_watermarks(intel_state,
  10796. intel_cstate);
  10797. }
  10798. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  10799. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10800. if (put_domains[i])
  10801. modeset_put_power_domains(dev_priv, put_domains[i]);
  10802. intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
  10803. }
  10804. if (intel_state->modeset && intel_can_enable_sagv(state))
  10805. intel_enable_sagv(dev_priv);
  10806. drm_atomic_helper_commit_hw_done(state);
  10807. if (intel_state->modeset)
  10808. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10809. mutex_lock(&dev->struct_mutex);
  10810. drm_atomic_helper_cleanup_planes(dev, state);
  10811. mutex_unlock(&dev->struct_mutex);
  10812. drm_atomic_helper_commit_cleanup_done(state);
  10813. drm_atomic_state_put(state);
  10814. /* As one of the primary mmio accessors, KMS has a high likelihood
  10815. * of triggering bugs in unclaimed access. After we finish
  10816. * modesetting, see if an error has been flagged, and if so
  10817. * enable debugging for the next modeset - and hope we catch
  10818. * the culprit.
  10819. *
  10820. * XXX note that we assume display power is on at this point.
  10821. * This might hold true now but we need to add pm helper to check
  10822. * unclaimed only when the hardware is on, as atomic commits
  10823. * can happen also when the device is completely off.
  10824. */
  10825. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10826. intel_atomic_helper_free_state(dev_priv);
  10827. }
  10828. static void intel_atomic_commit_work(struct work_struct *work)
  10829. {
  10830. struct drm_atomic_state *state =
  10831. container_of(work, struct drm_atomic_state, commit_work);
  10832. intel_atomic_commit_tail(state);
  10833. }
  10834. static int __i915_sw_fence_call
  10835. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10836. enum i915_sw_fence_notify notify)
  10837. {
  10838. struct intel_atomic_state *state =
  10839. container_of(fence, struct intel_atomic_state, commit_ready);
  10840. switch (notify) {
  10841. case FENCE_COMPLETE:
  10842. if (state->base.commit_work.func)
  10843. queue_work(system_unbound_wq, &state->base.commit_work);
  10844. break;
  10845. case FENCE_FREE:
  10846. {
  10847. struct intel_atomic_helper *helper =
  10848. &to_i915(state->base.dev)->atomic_helper;
  10849. if (llist_add(&state->freed, &helper->free_list))
  10850. schedule_work(&helper->free_work);
  10851. break;
  10852. }
  10853. }
  10854. return NOTIFY_DONE;
  10855. }
  10856. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10857. {
  10858. struct drm_plane_state *old_plane_state;
  10859. struct drm_plane *plane;
  10860. int i;
  10861. for_each_plane_in_state(state, plane, old_plane_state, i)
  10862. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10863. intel_fb_obj(plane->state->fb),
  10864. to_intel_plane(plane)->frontbuffer_bit);
  10865. }
  10866. /**
  10867. * intel_atomic_commit - commit validated state object
  10868. * @dev: DRM device
  10869. * @state: the top-level driver state object
  10870. * @nonblock: nonblocking commit
  10871. *
  10872. * This function commits a top-level state object that has been validated
  10873. * with drm_atomic_helper_check().
  10874. *
  10875. * RETURNS
  10876. * Zero for success or -errno.
  10877. */
  10878. static int intel_atomic_commit(struct drm_device *dev,
  10879. struct drm_atomic_state *state,
  10880. bool nonblock)
  10881. {
  10882. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10883. struct drm_i915_private *dev_priv = to_i915(dev);
  10884. int ret = 0;
  10885. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10886. if (ret)
  10887. return ret;
  10888. drm_atomic_state_get(state);
  10889. i915_sw_fence_init(&intel_state->commit_ready,
  10890. intel_atomic_commit_ready);
  10891. ret = intel_atomic_prepare_commit(dev, state);
  10892. if (ret) {
  10893. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10894. i915_sw_fence_commit(&intel_state->commit_ready);
  10895. return ret;
  10896. }
  10897. drm_atomic_helper_swap_state(state, true);
  10898. dev_priv->wm.distrust_bios_wm = false;
  10899. intel_shared_dpll_swap_state(state);
  10900. intel_atomic_track_fbs(state);
  10901. if (intel_state->modeset) {
  10902. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  10903. sizeof(intel_state->min_pixclk));
  10904. dev_priv->active_crtcs = intel_state->active_crtcs;
  10905. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10906. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10907. }
  10908. drm_atomic_state_get(state);
  10909. INIT_WORK(&state->commit_work,
  10910. nonblock ? intel_atomic_commit_work : NULL);
  10911. i915_sw_fence_commit(&intel_state->commit_ready);
  10912. if (!nonblock) {
  10913. i915_sw_fence_wait(&intel_state->commit_ready);
  10914. intel_atomic_commit_tail(state);
  10915. }
  10916. return 0;
  10917. }
  10918. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  10919. {
  10920. struct drm_device *dev = crtc->dev;
  10921. struct drm_atomic_state *state;
  10922. struct drm_crtc_state *crtc_state;
  10923. int ret;
  10924. state = drm_atomic_state_alloc(dev);
  10925. if (!state) {
  10926. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  10927. crtc->base.id, crtc->name);
  10928. return;
  10929. }
  10930. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  10931. retry:
  10932. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10933. ret = PTR_ERR_OR_ZERO(crtc_state);
  10934. if (!ret) {
  10935. if (!crtc_state->active)
  10936. goto out;
  10937. crtc_state->mode_changed = true;
  10938. ret = drm_atomic_commit(state);
  10939. }
  10940. if (ret == -EDEADLK) {
  10941. drm_atomic_state_clear(state);
  10942. drm_modeset_backoff(state->acquire_ctx);
  10943. goto retry;
  10944. }
  10945. out:
  10946. drm_atomic_state_put(state);
  10947. }
  10948. /*
  10949. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  10950. * drm_atomic_helper_legacy_gamma_set() directly.
  10951. */
  10952. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  10953. u16 *red, u16 *green, u16 *blue,
  10954. uint32_t size)
  10955. {
  10956. struct drm_device *dev = crtc->dev;
  10957. struct drm_mode_config *config = &dev->mode_config;
  10958. struct drm_crtc_state *state;
  10959. int ret;
  10960. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  10961. if (ret)
  10962. return ret;
  10963. /*
  10964. * Make sure we update the legacy properties so this works when
  10965. * atomic is not enabled.
  10966. */
  10967. state = crtc->state;
  10968. drm_object_property_set_value(&crtc->base,
  10969. config->degamma_lut_property,
  10970. (state->degamma_lut) ?
  10971. state->degamma_lut->base.id : 0);
  10972. drm_object_property_set_value(&crtc->base,
  10973. config->ctm_property,
  10974. (state->ctm) ?
  10975. state->ctm->base.id : 0);
  10976. drm_object_property_set_value(&crtc->base,
  10977. config->gamma_lut_property,
  10978. (state->gamma_lut) ?
  10979. state->gamma_lut->base.id : 0);
  10980. return 0;
  10981. }
  10982. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10983. .gamma_set = intel_atomic_legacy_gamma_set,
  10984. .set_config = drm_atomic_helper_set_config,
  10985. .set_property = drm_atomic_helper_crtc_set_property,
  10986. .destroy = intel_crtc_destroy,
  10987. .page_flip = drm_atomic_helper_page_flip,
  10988. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10989. .atomic_destroy_state = intel_crtc_destroy_state,
  10990. .set_crc_source = intel_crtc_set_crc_source,
  10991. };
  10992. /**
  10993. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10994. * @plane: drm plane to prepare for
  10995. * @fb: framebuffer to prepare for presentation
  10996. *
  10997. * Prepares a framebuffer for usage on a display plane. Generally this
  10998. * involves pinning the underlying object and updating the frontbuffer tracking
  10999. * bits. Some older platforms need special physical address handling for
  11000. * cursor planes.
  11001. *
  11002. * Must be called with struct_mutex held.
  11003. *
  11004. * Returns 0 on success, negative error code on failure.
  11005. */
  11006. int
  11007. intel_prepare_plane_fb(struct drm_plane *plane,
  11008. struct drm_plane_state *new_state)
  11009. {
  11010. struct intel_atomic_state *intel_state =
  11011. to_intel_atomic_state(new_state->state);
  11012. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11013. struct drm_framebuffer *fb = new_state->fb;
  11014. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11015. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11016. int ret;
  11017. if (!obj && !old_obj)
  11018. return 0;
  11019. if (old_obj) {
  11020. struct drm_crtc_state *crtc_state =
  11021. drm_atomic_get_existing_crtc_state(new_state->state,
  11022. plane->state->crtc);
  11023. /* Big Hammer, we also need to ensure that any pending
  11024. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11025. * current scanout is retired before unpinning the old
  11026. * framebuffer. Note that we rely on userspace rendering
  11027. * into the buffer attached to the pipe they are waiting
  11028. * on. If not, userspace generates a GPU hang with IPEHR
  11029. * point to the MI_WAIT_FOR_EVENT.
  11030. *
  11031. * This should only fail upon a hung GPU, in which case we
  11032. * can safely continue.
  11033. */
  11034. if (needs_modeset(crtc_state)) {
  11035. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  11036. old_obj->resv, NULL,
  11037. false, 0,
  11038. GFP_KERNEL);
  11039. if (ret < 0)
  11040. return ret;
  11041. }
  11042. }
  11043. if (new_state->fence) { /* explicit fencing */
  11044. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  11045. new_state->fence,
  11046. I915_FENCE_TIMEOUT,
  11047. GFP_KERNEL);
  11048. if (ret < 0)
  11049. return ret;
  11050. }
  11051. if (!obj)
  11052. return 0;
  11053. if (!new_state->fence) { /* implicit fencing */
  11054. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  11055. obj->resv, NULL,
  11056. false, I915_FENCE_TIMEOUT,
  11057. GFP_KERNEL);
  11058. if (ret < 0)
  11059. return ret;
  11060. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  11061. }
  11062. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11063. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  11064. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  11065. ret = i915_gem_object_attach_phys(obj, align);
  11066. if (ret) {
  11067. DRM_DEBUG_KMS("failed to attach phys object\n");
  11068. return ret;
  11069. }
  11070. } else {
  11071. struct i915_vma *vma;
  11072. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  11073. if (IS_ERR(vma)) {
  11074. DRM_DEBUG_KMS("failed to pin object\n");
  11075. return PTR_ERR(vma);
  11076. }
  11077. to_intel_plane_state(new_state)->vma = vma;
  11078. }
  11079. return 0;
  11080. }
  11081. /**
  11082. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11083. * @plane: drm plane to clean up for
  11084. * @fb: old framebuffer that was on plane
  11085. *
  11086. * Cleans up a framebuffer that has just been removed from a plane.
  11087. *
  11088. * Must be called with struct_mutex held.
  11089. */
  11090. void
  11091. intel_cleanup_plane_fb(struct drm_plane *plane,
  11092. struct drm_plane_state *old_state)
  11093. {
  11094. struct i915_vma *vma;
  11095. /* Should only be called after a successful intel_prepare_plane_fb()! */
  11096. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  11097. if (vma)
  11098. intel_unpin_fb_vma(vma);
  11099. }
  11100. int
  11101. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11102. {
  11103. int max_scale;
  11104. int crtc_clock, cdclk;
  11105. if (!intel_crtc || !crtc_state->base.enable)
  11106. return DRM_PLANE_HELPER_NO_SCALING;
  11107. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11108. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  11109. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11110. return DRM_PLANE_HELPER_NO_SCALING;
  11111. /*
  11112. * skl max scale is lower of:
  11113. * close to 3 but not 3, -1 is for that purpose
  11114. * or
  11115. * cdclk/crtc_clock
  11116. */
  11117. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11118. return max_scale;
  11119. }
  11120. static int
  11121. intel_check_primary_plane(struct drm_plane *plane,
  11122. struct intel_crtc_state *crtc_state,
  11123. struct intel_plane_state *state)
  11124. {
  11125. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11126. struct drm_crtc *crtc = state->base.crtc;
  11127. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11128. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11129. bool can_position = false;
  11130. int ret;
  11131. if (INTEL_GEN(dev_priv) >= 9) {
  11132. /* use scaler when colorkey is not required */
  11133. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11134. min_scale = 1;
  11135. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11136. }
  11137. can_position = true;
  11138. }
  11139. ret = drm_plane_helper_check_state(&state->base,
  11140. &state->clip,
  11141. min_scale, max_scale,
  11142. can_position, true);
  11143. if (ret)
  11144. return ret;
  11145. if (!state->base.fb)
  11146. return 0;
  11147. if (INTEL_GEN(dev_priv) >= 9) {
  11148. ret = skl_check_plane_surface(state);
  11149. if (ret)
  11150. return ret;
  11151. }
  11152. return 0;
  11153. }
  11154. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11155. struct drm_crtc_state *old_crtc_state)
  11156. {
  11157. struct drm_device *dev = crtc->dev;
  11158. struct drm_i915_private *dev_priv = to_i915(dev);
  11159. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11160. struct intel_crtc_state *intel_cstate =
  11161. to_intel_crtc_state(crtc->state);
  11162. struct intel_crtc_state *old_intel_cstate =
  11163. to_intel_crtc_state(old_crtc_state);
  11164. struct intel_atomic_state *old_intel_state =
  11165. to_intel_atomic_state(old_crtc_state->state);
  11166. bool modeset = needs_modeset(crtc->state);
  11167. /* Perform vblank evasion around commit operation */
  11168. intel_pipe_update_start(intel_crtc);
  11169. if (modeset)
  11170. goto out;
  11171. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  11172. intel_color_set_csc(crtc->state);
  11173. intel_color_load_luts(crtc->state);
  11174. }
  11175. if (intel_cstate->update_pipe)
  11176. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  11177. else if (INTEL_GEN(dev_priv) >= 9)
  11178. skl_detach_scalers(intel_crtc);
  11179. out:
  11180. if (dev_priv->display.atomic_update_watermarks)
  11181. dev_priv->display.atomic_update_watermarks(old_intel_state,
  11182. intel_cstate);
  11183. }
  11184. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11185. struct drm_crtc_state *old_crtc_state)
  11186. {
  11187. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11188. intel_pipe_update_end(intel_crtc, NULL);
  11189. }
  11190. /**
  11191. * intel_plane_destroy - destroy a plane
  11192. * @plane: plane to destroy
  11193. *
  11194. * Common destruction function for all types of planes (primary, cursor,
  11195. * sprite).
  11196. */
  11197. void intel_plane_destroy(struct drm_plane *plane)
  11198. {
  11199. drm_plane_cleanup(plane);
  11200. kfree(to_intel_plane(plane));
  11201. }
  11202. const struct drm_plane_funcs intel_plane_funcs = {
  11203. .update_plane = drm_atomic_helper_update_plane,
  11204. .disable_plane = drm_atomic_helper_disable_plane,
  11205. .destroy = intel_plane_destroy,
  11206. .set_property = drm_atomic_helper_plane_set_property,
  11207. .atomic_get_property = intel_plane_atomic_get_property,
  11208. .atomic_set_property = intel_plane_atomic_set_property,
  11209. .atomic_duplicate_state = intel_plane_duplicate_state,
  11210. .atomic_destroy_state = intel_plane_destroy_state,
  11211. };
  11212. static int
  11213. intel_legacy_cursor_update(struct drm_plane *plane,
  11214. struct drm_crtc *crtc,
  11215. struct drm_framebuffer *fb,
  11216. int crtc_x, int crtc_y,
  11217. unsigned int crtc_w, unsigned int crtc_h,
  11218. uint32_t src_x, uint32_t src_y,
  11219. uint32_t src_w, uint32_t src_h)
  11220. {
  11221. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11222. int ret;
  11223. struct drm_plane_state *old_plane_state, *new_plane_state;
  11224. struct intel_plane *intel_plane = to_intel_plane(plane);
  11225. struct drm_framebuffer *old_fb;
  11226. struct drm_crtc_state *crtc_state = crtc->state;
  11227. struct i915_vma *old_vma;
  11228. /*
  11229. * When crtc is inactive or there is a modeset pending,
  11230. * wait for it to complete in the slowpath
  11231. */
  11232. if (!crtc_state->active || needs_modeset(crtc_state) ||
  11233. to_intel_crtc_state(crtc_state)->update_pipe)
  11234. goto slow;
  11235. old_plane_state = plane->state;
  11236. /*
  11237. * If any parameters change that may affect watermarks,
  11238. * take the slowpath. Only changing fb or position should be
  11239. * in the fastpath.
  11240. */
  11241. if (old_plane_state->crtc != crtc ||
  11242. old_plane_state->src_w != src_w ||
  11243. old_plane_state->src_h != src_h ||
  11244. old_plane_state->crtc_w != crtc_w ||
  11245. old_plane_state->crtc_h != crtc_h ||
  11246. !old_plane_state->visible ||
  11247. old_plane_state->fb->modifier != fb->modifier)
  11248. goto slow;
  11249. new_plane_state = intel_plane_duplicate_state(plane);
  11250. if (!new_plane_state)
  11251. return -ENOMEM;
  11252. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  11253. new_plane_state->src_x = src_x;
  11254. new_plane_state->src_y = src_y;
  11255. new_plane_state->src_w = src_w;
  11256. new_plane_state->src_h = src_h;
  11257. new_plane_state->crtc_x = crtc_x;
  11258. new_plane_state->crtc_y = crtc_y;
  11259. new_plane_state->crtc_w = crtc_w;
  11260. new_plane_state->crtc_h = crtc_h;
  11261. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  11262. to_intel_plane_state(new_plane_state));
  11263. if (ret)
  11264. goto out_free;
  11265. /* Visibility changed, must take slowpath. */
  11266. if (!new_plane_state->visible)
  11267. goto slow_free;
  11268. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  11269. if (ret)
  11270. goto out_free;
  11271. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  11272. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  11273. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  11274. if (ret) {
  11275. DRM_DEBUG_KMS("failed to attach phys object\n");
  11276. goto out_unlock;
  11277. }
  11278. } else {
  11279. struct i915_vma *vma;
  11280. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  11281. if (IS_ERR(vma)) {
  11282. DRM_DEBUG_KMS("failed to pin object\n");
  11283. ret = PTR_ERR(vma);
  11284. goto out_unlock;
  11285. }
  11286. to_intel_plane_state(new_plane_state)->vma = vma;
  11287. }
  11288. old_fb = old_plane_state->fb;
  11289. old_vma = to_intel_plane_state(old_plane_state)->vma;
  11290. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  11291. intel_plane->frontbuffer_bit);
  11292. /* Swap plane state */
  11293. new_plane_state->fence = old_plane_state->fence;
  11294. *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
  11295. new_plane_state->fence = NULL;
  11296. new_plane_state->fb = old_fb;
  11297. to_intel_plane_state(new_plane_state)->vma = old_vma;
  11298. intel_plane->update_plane(plane,
  11299. to_intel_crtc_state(crtc->state),
  11300. to_intel_plane_state(plane->state));
  11301. intel_cleanup_plane_fb(plane, new_plane_state);
  11302. out_unlock:
  11303. mutex_unlock(&dev_priv->drm.struct_mutex);
  11304. out_free:
  11305. intel_plane_destroy_state(plane, new_plane_state);
  11306. return ret;
  11307. slow_free:
  11308. intel_plane_destroy_state(plane, new_plane_state);
  11309. slow:
  11310. return drm_atomic_helper_update_plane(plane, crtc, fb,
  11311. crtc_x, crtc_y, crtc_w, crtc_h,
  11312. src_x, src_y, src_w, src_h);
  11313. }
  11314. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  11315. .update_plane = intel_legacy_cursor_update,
  11316. .disable_plane = drm_atomic_helper_disable_plane,
  11317. .destroy = intel_plane_destroy,
  11318. .set_property = drm_atomic_helper_plane_set_property,
  11319. .atomic_get_property = intel_plane_atomic_get_property,
  11320. .atomic_set_property = intel_plane_atomic_set_property,
  11321. .atomic_duplicate_state = intel_plane_duplicate_state,
  11322. .atomic_destroy_state = intel_plane_destroy_state,
  11323. };
  11324. static struct intel_plane *
  11325. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11326. {
  11327. struct intel_plane *primary = NULL;
  11328. struct intel_plane_state *state = NULL;
  11329. const uint32_t *intel_primary_formats;
  11330. unsigned int supported_rotations;
  11331. unsigned int num_formats;
  11332. int ret;
  11333. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11334. if (!primary) {
  11335. ret = -ENOMEM;
  11336. goto fail;
  11337. }
  11338. state = intel_create_plane_state(&primary->base);
  11339. if (!state) {
  11340. ret = -ENOMEM;
  11341. goto fail;
  11342. }
  11343. primary->base.state = &state->base;
  11344. primary->can_scale = false;
  11345. primary->max_downscale = 1;
  11346. if (INTEL_GEN(dev_priv) >= 9) {
  11347. primary->can_scale = true;
  11348. state->scaler_id = -1;
  11349. }
  11350. primary->pipe = pipe;
  11351. /*
  11352. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11353. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11354. */
  11355. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11356. primary->plane = (enum plane) !pipe;
  11357. else
  11358. primary->plane = (enum plane) pipe;
  11359. primary->id = PLANE_PRIMARY;
  11360. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11361. primary->check_plane = intel_check_primary_plane;
  11362. if (INTEL_GEN(dev_priv) >= 9) {
  11363. intel_primary_formats = skl_primary_formats;
  11364. num_formats = ARRAY_SIZE(skl_primary_formats);
  11365. primary->update_plane = skylake_update_primary_plane;
  11366. primary->disable_plane = skylake_disable_primary_plane;
  11367. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11368. intel_primary_formats = i965_primary_formats;
  11369. num_formats = ARRAY_SIZE(i965_primary_formats);
  11370. primary->update_plane = ironlake_update_primary_plane;
  11371. primary->disable_plane = i9xx_disable_primary_plane;
  11372. } else if (INTEL_GEN(dev_priv) >= 4) {
  11373. intel_primary_formats = i965_primary_formats;
  11374. num_formats = ARRAY_SIZE(i965_primary_formats);
  11375. primary->update_plane = i9xx_update_primary_plane;
  11376. primary->disable_plane = i9xx_disable_primary_plane;
  11377. } else {
  11378. intel_primary_formats = i8xx_primary_formats;
  11379. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11380. primary->update_plane = i9xx_update_primary_plane;
  11381. primary->disable_plane = i9xx_disable_primary_plane;
  11382. }
  11383. if (INTEL_GEN(dev_priv) >= 9)
  11384. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11385. 0, &intel_plane_funcs,
  11386. intel_primary_formats, num_formats,
  11387. DRM_PLANE_TYPE_PRIMARY,
  11388. "plane 1%c", pipe_name(pipe));
  11389. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11390. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11391. 0, &intel_plane_funcs,
  11392. intel_primary_formats, num_formats,
  11393. DRM_PLANE_TYPE_PRIMARY,
  11394. "primary %c", pipe_name(pipe));
  11395. else
  11396. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11397. 0, &intel_plane_funcs,
  11398. intel_primary_formats, num_formats,
  11399. DRM_PLANE_TYPE_PRIMARY,
  11400. "plane %c", plane_name(primary->plane));
  11401. if (ret)
  11402. goto fail;
  11403. if (INTEL_GEN(dev_priv) >= 9) {
  11404. supported_rotations =
  11405. DRM_ROTATE_0 | DRM_ROTATE_90 |
  11406. DRM_ROTATE_180 | DRM_ROTATE_270;
  11407. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11408. supported_rotations =
  11409. DRM_ROTATE_0 | DRM_ROTATE_180 |
  11410. DRM_REFLECT_X;
  11411. } else if (INTEL_GEN(dev_priv) >= 4) {
  11412. supported_rotations =
  11413. DRM_ROTATE_0 | DRM_ROTATE_180;
  11414. } else {
  11415. supported_rotations = DRM_ROTATE_0;
  11416. }
  11417. if (INTEL_GEN(dev_priv) >= 4)
  11418. drm_plane_create_rotation_property(&primary->base,
  11419. DRM_ROTATE_0,
  11420. supported_rotations);
  11421. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11422. return primary;
  11423. fail:
  11424. kfree(state);
  11425. kfree(primary);
  11426. return ERR_PTR(ret);
  11427. }
  11428. static int
  11429. intel_check_cursor_plane(struct drm_plane *plane,
  11430. struct intel_crtc_state *crtc_state,
  11431. struct intel_plane_state *state)
  11432. {
  11433. struct drm_framebuffer *fb = state->base.fb;
  11434. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11435. enum pipe pipe = to_intel_plane(plane)->pipe;
  11436. unsigned stride;
  11437. int ret;
  11438. ret = drm_plane_helper_check_state(&state->base,
  11439. &state->clip,
  11440. DRM_PLANE_HELPER_NO_SCALING,
  11441. DRM_PLANE_HELPER_NO_SCALING,
  11442. true, true);
  11443. if (ret)
  11444. return ret;
  11445. /* if we want to turn off the cursor ignore width and height */
  11446. if (!obj)
  11447. return 0;
  11448. /* Check for which cursor types we support */
  11449. if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
  11450. state->base.crtc_h)) {
  11451. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11452. state->base.crtc_w, state->base.crtc_h);
  11453. return -EINVAL;
  11454. }
  11455. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11456. if (obj->base.size < stride * state->base.crtc_h) {
  11457. DRM_DEBUG_KMS("buffer is too small\n");
  11458. return -ENOMEM;
  11459. }
  11460. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  11461. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11462. return -EINVAL;
  11463. }
  11464. /*
  11465. * There's something wrong with the cursor on CHV pipe C.
  11466. * If it straddles the left edge of the screen then
  11467. * moving it away from the edge or disabling it often
  11468. * results in a pipe underrun, and often that can lead to
  11469. * dead pipe (constant underrun reported, and it scans
  11470. * out just a solid color). To recover from that, the
  11471. * display power well must be turned off and on again.
  11472. * Refuse the put the cursor into that compromised position.
  11473. */
  11474. if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
  11475. state->base.visible && state->base.crtc_x < 0) {
  11476. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  11477. return -EINVAL;
  11478. }
  11479. return 0;
  11480. }
  11481. static void
  11482. intel_disable_cursor_plane(struct drm_plane *plane,
  11483. struct drm_crtc *crtc)
  11484. {
  11485. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11486. intel_crtc->cursor_addr = 0;
  11487. intel_crtc_update_cursor(crtc, NULL);
  11488. }
  11489. static void
  11490. intel_update_cursor_plane(struct drm_plane *plane,
  11491. const struct intel_crtc_state *crtc_state,
  11492. const struct intel_plane_state *state)
  11493. {
  11494. struct drm_crtc *crtc = crtc_state->base.crtc;
  11495. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11496. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11497. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11498. uint32_t addr;
  11499. if (!obj)
  11500. addr = 0;
  11501. else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
  11502. addr = intel_plane_ggtt_offset(state);
  11503. else
  11504. addr = obj->phys_handle->busaddr;
  11505. intel_crtc->cursor_addr = addr;
  11506. intel_crtc_update_cursor(crtc, state);
  11507. }
  11508. static struct intel_plane *
  11509. intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11510. {
  11511. struct intel_plane *cursor = NULL;
  11512. struct intel_plane_state *state = NULL;
  11513. int ret;
  11514. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11515. if (!cursor) {
  11516. ret = -ENOMEM;
  11517. goto fail;
  11518. }
  11519. state = intel_create_plane_state(&cursor->base);
  11520. if (!state) {
  11521. ret = -ENOMEM;
  11522. goto fail;
  11523. }
  11524. cursor->base.state = &state->base;
  11525. cursor->can_scale = false;
  11526. cursor->max_downscale = 1;
  11527. cursor->pipe = pipe;
  11528. cursor->plane = pipe;
  11529. cursor->id = PLANE_CURSOR;
  11530. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11531. cursor->check_plane = intel_check_cursor_plane;
  11532. cursor->update_plane = intel_update_cursor_plane;
  11533. cursor->disable_plane = intel_disable_cursor_plane;
  11534. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11535. 0, &intel_cursor_plane_funcs,
  11536. intel_cursor_formats,
  11537. ARRAY_SIZE(intel_cursor_formats),
  11538. DRM_PLANE_TYPE_CURSOR,
  11539. "cursor %c", pipe_name(pipe));
  11540. if (ret)
  11541. goto fail;
  11542. if (INTEL_GEN(dev_priv) >= 4)
  11543. drm_plane_create_rotation_property(&cursor->base,
  11544. DRM_ROTATE_0,
  11545. DRM_ROTATE_0 |
  11546. DRM_ROTATE_180);
  11547. if (INTEL_GEN(dev_priv) >= 9)
  11548. state->scaler_id = -1;
  11549. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11550. return cursor;
  11551. fail:
  11552. kfree(state);
  11553. kfree(cursor);
  11554. return ERR_PTR(ret);
  11555. }
  11556. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11557. struct intel_crtc_state *crtc_state)
  11558. {
  11559. struct intel_crtc_scaler_state *scaler_state =
  11560. &crtc_state->scaler_state;
  11561. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11562. int i;
  11563. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11564. if (!crtc->num_scalers)
  11565. return;
  11566. for (i = 0; i < crtc->num_scalers; i++) {
  11567. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11568. scaler->in_use = 0;
  11569. scaler->mode = PS_SCALER_MODE_DYN;
  11570. }
  11571. scaler_state->scaler_id = -1;
  11572. }
  11573. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11574. {
  11575. struct intel_crtc *intel_crtc;
  11576. struct intel_crtc_state *crtc_state = NULL;
  11577. struct intel_plane *primary = NULL;
  11578. struct intel_plane *cursor = NULL;
  11579. int sprite, ret;
  11580. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11581. if (!intel_crtc)
  11582. return -ENOMEM;
  11583. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11584. if (!crtc_state) {
  11585. ret = -ENOMEM;
  11586. goto fail;
  11587. }
  11588. intel_crtc->config = crtc_state;
  11589. intel_crtc->base.state = &crtc_state->base;
  11590. crtc_state->base.crtc = &intel_crtc->base;
  11591. primary = intel_primary_plane_create(dev_priv, pipe);
  11592. if (IS_ERR(primary)) {
  11593. ret = PTR_ERR(primary);
  11594. goto fail;
  11595. }
  11596. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11597. for_each_sprite(dev_priv, pipe, sprite) {
  11598. struct intel_plane *plane;
  11599. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11600. if (IS_ERR(plane)) {
  11601. ret = PTR_ERR(plane);
  11602. goto fail;
  11603. }
  11604. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11605. }
  11606. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11607. if (IS_ERR(cursor)) {
  11608. ret = PTR_ERR(cursor);
  11609. goto fail;
  11610. }
  11611. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11612. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11613. &primary->base, &cursor->base,
  11614. &intel_crtc_funcs,
  11615. "pipe %c", pipe_name(pipe));
  11616. if (ret)
  11617. goto fail;
  11618. intel_crtc->pipe = pipe;
  11619. intel_crtc->plane = primary->plane;
  11620. intel_crtc->cursor_base = ~0;
  11621. intel_crtc->cursor_cntl = ~0;
  11622. intel_crtc->cursor_size = ~0;
  11623. intel_crtc->wm.cxsr_allowed = true;
  11624. /* initialize shared scalers */
  11625. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11626. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11627. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11628. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  11629. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11630. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11631. intel_color_init(&intel_crtc->base);
  11632. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11633. return 0;
  11634. fail:
  11635. /*
  11636. * drm_mode_config_cleanup() will free up any
  11637. * crtcs/planes already initialized.
  11638. */
  11639. kfree(crtc_state);
  11640. kfree(intel_crtc);
  11641. return ret;
  11642. }
  11643. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11644. {
  11645. struct drm_encoder *encoder = connector->base.encoder;
  11646. struct drm_device *dev = connector->base.dev;
  11647. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11648. if (!encoder || WARN_ON(!encoder->crtc))
  11649. return INVALID_PIPE;
  11650. return to_intel_crtc(encoder->crtc)->pipe;
  11651. }
  11652. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11653. struct drm_file *file)
  11654. {
  11655. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11656. struct drm_crtc *drmmode_crtc;
  11657. struct intel_crtc *crtc;
  11658. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11659. if (!drmmode_crtc)
  11660. return -ENOENT;
  11661. crtc = to_intel_crtc(drmmode_crtc);
  11662. pipe_from_crtc_id->pipe = crtc->pipe;
  11663. return 0;
  11664. }
  11665. static int intel_encoder_clones(struct intel_encoder *encoder)
  11666. {
  11667. struct drm_device *dev = encoder->base.dev;
  11668. struct intel_encoder *source_encoder;
  11669. int index_mask = 0;
  11670. int entry = 0;
  11671. for_each_intel_encoder(dev, source_encoder) {
  11672. if (encoders_cloneable(encoder, source_encoder))
  11673. index_mask |= (1 << entry);
  11674. entry++;
  11675. }
  11676. return index_mask;
  11677. }
  11678. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11679. {
  11680. if (!IS_MOBILE(dev_priv))
  11681. return false;
  11682. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11683. return false;
  11684. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11685. return false;
  11686. return true;
  11687. }
  11688. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11689. {
  11690. if (INTEL_GEN(dev_priv) >= 9)
  11691. return false;
  11692. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11693. return false;
  11694. if (IS_CHERRYVIEW(dev_priv))
  11695. return false;
  11696. if (HAS_PCH_LPT_H(dev_priv) &&
  11697. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11698. return false;
  11699. /* DDI E can't be used if DDI A requires 4 lanes */
  11700. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11701. return false;
  11702. if (!dev_priv->vbt.int_crt_support)
  11703. return false;
  11704. return true;
  11705. }
  11706. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11707. {
  11708. int pps_num;
  11709. int pps_idx;
  11710. if (HAS_DDI(dev_priv))
  11711. return;
  11712. /*
  11713. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11714. * everywhere where registers can be write protected.
  11715. */
  11716. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11717. pps_num = 2;
  11718. else
  11719. pps_num = 1;
  11720. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11721. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11722. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11723. I915_WRITE(PP_CONTROL(pps_idx), val);
  11724. }
  11725. }
  11726. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11727. {
  11728. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11729. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11730. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11731. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11732. else
  11733. dev_priv->pps_mmio_base = PPS_BASE;
  11734. intel_pps_unlock_regs_wa(dev_priv);
  11735. }
  11736. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11737. {
  11738. struct intel_encoder *encoder;
  11739. bool dpd_is_edp = false;
  11740. intel_pps_init(dev_priv);
  11741. /*
  11742. * intel_edp_init_connector() depends on this completing first, to
  11743. * prevent the registeration of both eDP and LVDS and the incorrect
  11744. * sharing of the PPS.
  11745. */
  11746. intel_lvds_init(dev_priv);
  11747. if (intel_crt_present(dev_priv))
  11748. intel_crt_init(dev_priv);
  11749. if (IS_GEN9_LP(dev_priv)) {
  11750. /*
  11751. * FIXME: Broxton doesn't support port detection via the
  11752. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11753. * detect the ports.
  11754. */
  11755. intel_ddi_init(dev_priv, PORT_A);
  11756. intel_ddi_init(dev_priv, PORT_B);
  11757. intel_ddi_init(dev_priv, PORT_C);
  11758. intel_dsi_init(dev_priv);
  11759. } else if (HAS_DDI(dev_priv)) {
  11760. int found;
  11761. /*
  11762. * Haswell uses DDI functions to detect digital outputs.
  11763. * On SKL pre-D0 the strap isn't connected, so we assume
  11764. * it's there.
  11765. */
  11766. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11767. /* WaIgnoreDDIAStrap: skl */
  11768. if (found || IS_GEN9_BC(dev_priv))
  11769. intel_ddi_init(dev_priv, PORT_A);
  11770. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11771. * register */
  11772. found = I915_READ(SFUSE_STRAP);
  11773. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11774. intel_ddi_init(dev_priv, PORT_B);
  11775. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11776. intel_ddi_init(dev_priv, PORT_C);
  11777. if (found & SFUSE_STRAP_DDID_DETECTED)
  11778. intel_ddi_init(dev_priv, PORT_D);
  11779. /*
  11780. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11781. */
  11782. if (IS_GEN9_BC(dev_priv) &&
  11783. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11784. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11785. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11786. intel_ddi_init(dev_priv, PORT_E);
  11787. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11788. int found;
  11789. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  11790. if (has_edp_a(dev_priv))
  11791. intel_dp_init(dev_priv, DP_A, PORT_A);
  11792. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11793. /* PCH SDVOB multiplex with HDMIB */
  11794. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11795. if (!found)
  11796. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11797. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11798. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11799. }
  11800. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11801. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11802. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11803. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11804. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11805. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11806. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11807. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11808. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11809. bool has_edp, has_port;
  11810. /*
  11811. * The DP_DETECTED bit is the latched state of the DDC
  11812. * SDA pin at boot. However since eDP doesn't require DDC
  11813. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11814. * eDP ports may have been muxed to an alternate function.
  11815. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11816. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11817. * detect eDP ports.
  11818. *
  11819. * Sadly the straps seem to be missing sometimes even for HDMI
  11820. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11821. * and VBT for the presence of the port. Additionally we can't
  11822. * trust the port type the VBT declares as we've seen at least
  11823. * HDMI ports that the VBT claim are DP or eDP.
  11824. */
  11825. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  11826. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11827. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11828. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11829. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11830. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11831. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  11832. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11833. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11834. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11835. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11836. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11837. if (IS_CHERRYVIEW(dev_priv)) {
  11838. /*
  11839. * eDP not supported on port D,
  11840. * so no need to worry about it
  11841. */
  11842. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11843. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11844. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11845. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11846. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11847. }
  11848. intel_dsi_init(dev_priv);
  11849. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11850. bool found = false;
  11851. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11852. DRM_DEBUG_KMS("probing SDVOB\n");
  11853. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11854. if (!found && IS_G4X(dev_priv)) {
  11855. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11856. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11857. }
  11858. if (!found && IS_G4X(dev_priv))
  11859. intel_dp_init(dev_priv, DP_B, PORT_B);
  11860. }
  11861. /* Before G4X SDVOC doesn't have its own detect register */
  11862. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11863. DRM_DEBUG_KMS("probing SDVOC\n");
  11864. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11865. }
  11866. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11867. if (IS_G4X(dev_priv)) {
  11868. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11869. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11870. }
  11871. if (IS_G4X(dev_priv))
  11872. intel_dp_init(dev_priv, DP_C, PORT_C);
  11873. }
  11874. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11875. intel_dp_init(dev_priv, DP_D, PORT_D);
  11876. } else if (IS_GEN2(dev_priv))
  11877. intel_dvo_init(dev_priv);
  11878. if (SUPPORTS_TV(dev_priv))
  11879. intel_tv_init(dev_priv);
  11880. intel_psr_init(dev_priv);
  11881. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11882. encoder->base.possible_crtcs = encoder->crtc_mask;
  11883. encoder->base.possible_clones =
  11884. intel_encoder_clones(encoder);
  11885. }
  11886. intel_init_pch_refclk(dev_priv);
  11887. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11888. }
  11889. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11890. {
  11891. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11892. drm_framebuffer_cleanup(fb);
  11893. WARN_ON(atomic_dec_return(&intel_fb->obj->framebuffer_references) < 0);
  11894. i915_gem_object_put(intel_fb->obj);
  11895. kfree(intel_fb);
  11896. }
  11897. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11898. struct drm_file *file,
  11899. unsigned int *handle)
  11900. {
  11901. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11902. struct drm_i915_gem_object *obj = intel_fb->obj;
  11903. if (obj->userptr.mm) {
  11904. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11905. return -EINVAL;
  11906. }
  11907. return drm_gem_handle_create(file, &obj->base, handle);
  11908. }
  11909. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11910. struct drm_file *file,
  11911. unsigned flags, unsigned color,
  11912. struct drm_clip_rect *clips,
  11913. unsigned num_clips)
  11914. {
  11915. struct drm_device *dev = fb->dev;
  11916. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11917. struct drm_i915_gem_object *obj = intel_fb->obj;
  11918. mutex_lock(&dev->struct_mutex);
  11919. if (obj->pin_display && obj->cache_dirty)
  11920. i915_gem_clflush_object(obj, true);
  11921. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  11922. mutex_unlock(&dev->struct_mutex);
  11923. return 0;
  11924. }
  11925. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11926. .destroy = intel_user_framebuffer_destroy,
  11927. .create_handle = intel_user_framebuffer_create_handle,
  11928. .dirty = intel_user_framebuffer_dirty,
  11929. };
  11930. static
  11931. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11932. uint64_t fb_modifier, uint32_t pixel_format)
  11933. {
  11934. u32 gen = INTEL_GEN(dev_priv);
  11935. if (gen >= 9) {
  11936. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11937. /* "The stride in bytes must not exceed the of the size of 8K
  11938. * pixels and 32K bytes."
  11939. */
  11940. return min(8192 * cpp, 32768);
  11941. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11942. return 32*1024;
  11943. } else if (gen >= 4) {
  11944. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11945. return 16*1024;
  11946. else
  11947. return 32*1024;
  11948. } else if (gen >= 3) {
  11949. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11950. return 8*1024;
  11951. else
  11952. return 16*1024;
  11953. } else {
  11954. /* XXX DSPC is limited to 4k tiled */
  11955. return 8*1024;
  11956. }
  11957. }
  11958. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11959. struct drm_i915_gem_object *obj,
  11960. struct drm_mode_fb_cmd2 *mode_cmd)
  11961. {
  11962. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11963. unsigned int tiling = i915_gem_object_get_tiling(obj);
  11964. u32 pitch_limit, stride_alignment;
  11965. struct drm_format_name_buf format_name;
  11966. int ret = -EINVAL;
  11967. atomic_inc(&obj->framebuffer_references);
  11968. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11969. /*
  11970. * If there's a fence, enforce that
  11971. * the fb modifier and tiling mode match.
  11972. */
  11973. if (tiling != I915_TILING_NONE &&
  11974. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11975. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11976. goto err;
  11977. }
  11978. } else {
  11979. if (tiling == I915_TILING_X) {
  11980. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11981. } else if (tiling == I915_TILING_Y) {
  11982. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11983. goto err;
  11984. }
  11985. }
  11986. /* Passed in modifier sanity checking. */
  11987. switch (mode_cmd->modifier[0]) {
  11988. case I915_FORMAT_MOD_Y_TILED:
  11989. case I915_FORMAT_MOD_Yf_TILED:
  11990. if (INTEL_GEN(dev_priv) < 9) {
  11991. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11992. mode_cmd->modifier[0]);
  11993. goto err;
  11994. }
  11995. case DRM_FORMAT_MOD_NONE:
  11996. case I915_FORMAT_MOD_X_TILED:
  11997. break;
  11998. default:
  11999. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12000. mode_cmd->modifier[0]);
  12001. goto err;
  12002. }
  12003. /*
  12004. * gen2/3 display engine uses the fence if present,
  12005. * so the tiling mode must match the fb modifier exactly.
  12006. */
  12007. if (INTEL_INFO(dev_priv)->gen < 4 &&
  12008. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  12009. DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
  12010. return -EINVAL;
  12011. }
  12012. stride_alignment = intel_fb_stride_alignment(dev_priv,
  12013. mode_cmd->modifier[0],
  12014. mode_cmd->pixel_format);
  12015. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12016. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12017. mode_cmd->pitches[0], stride_alignment);
  12018. goto err;
  12019. }
  12020. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  12021. mode_cmd->pixel_format);
  12022. if (mode_cmd->pitches[0] > pitch_limit) {
  12023. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12024. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12025. "tiled" : "linear",
  12026. mode_cmd->pitches[0], pitch_limit);
  12027. goto err;
  12028. }
  12029. /*
  12030. * If there's a fence, enforce that
  12031. * the fb pitch and fence stride match.
  12032. */
  12033. if (tiling != I915_TILING_NONE &&
  12034. mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
  12035. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12036. mode_cmd->pitches[0],
  12037. i915_gem_object_get_stride(obj));
  12038. goto err;
  12039. }
  12040. /* Reject formats not supported by any plane early. */
  12041. switch (mode_cmd->pixel_format) {
  12042. case DRM_FORMAT_C8:
  12043. case DRM_FORMAT_RGB565:
  12044. case DRM_FORMAT_XRGB8888:
  12045. case DRM_FORMAT_ARGB8888:
  12046. break;
  12047. case DRM_FORMAT_XRGB1555:
  12048. if (INTEL_GEN(dev_priv) > 3) {
  12049. DRM_DEBUG("unsupported pixel format: %s\n",
  12050. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12051. return -EINVAL;
  12052. }
  12053. break;
  12054. case DRM_FORMAT_ABGR8888:
  12055. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  12056. INTEL_GEN(dev_priv) < 9) {
  12057. DRM_DEBUG("unsupported pixel format: %s\n",
  12058. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12059. return -EINVAL;
  12060. }
  12061. break;
  12062. case DRM_FORMAT_XBGR8888:
  12063. case DRM_FORMAT_XRGB2101010:
  12064. case DRM_FORMAT_XBGR2101010:
  12065. if (INTEL_GEN(dev_priv) < 4) {
  12066. DRM_DEBUG("unsupported pixel format: %s\n",
  12067. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12068. return -EINVAL;
  12069. }
  12070. break;
  12071. case DRM_FORMAT_ABGR2101010:
  12072. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  12073. DRM_DEBUG("unsupported pixel format: %s\n",
  12074. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12075. return -EINVAL;
  12076. }
  12077. break;
  12078. case DRM_FORMAT_YUYV:
  12079. case DRM_FORMAT_UYVY:
  12080. case DRM_FORMAT_YVYU:
  12081. case DRM_FORMAT_VYUY:
  12082. if (INTEL_GEN(dev_priv) < 5) {
  12083. DRM_DEBUG("unsupported pixel format: %s\n",
  12084. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12085. return -EINVAL;
  12086. }
  12087. break;
  12088. default:
  12089. DRM_DEBUG("unsupported pixel format: %s\n",
  12090. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12091. return -EINVAL;
  12092. }
  12093. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12094. if (mode_cmd->offsets[0] != 0)
  12095. goto err;
  12096. drm_helper_mode_fill_fb_struct(&dev_priv->drm,
  12097. &intel_fb->base, mode_cmd);
  12098. intel_fb->obj = obj;
  12099. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  12100. if (ret)
  12101. return ret;
  12102. ret = drm_framebuffer_init(obj->base.dev,
  12103. &intel_fb->base,
  12104. &intel_fb_funcs);
  12105. if (ret) {
  12106. DRM_ERROR("framebuffer init failed %d\n", ret);
  12107. goto err;
  12108. }
  12109. return 0;
  12110. err:
  12111. atomic_dec(&obj->framebuffer_references);
  12112. return ret;
  12113. }
  12114. static struct drm_framebuffer *
  12115. intel_user_framebuffer_create(struct drm_device *dev,
  12116. struct drm_file *filp,
  12117. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12118. {
  12119. struct drm_framebuffer *fb;
  12120. struct drm_i915_gem_object *obj;
  12121. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12122. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  12123. if (!obj)
  12124. return ERR_PTR(-ENOENT);
  12125. fb = intel_framebuffer_create(obj, &mode_cmd);
  12126. if (IS_ERR(fb))
  12127. i915_gem_object_put(obj);
  12128. return fb;
  12129. }
  12130. static void intel_atomic_state_free(struct drm_atomic_state *state)
  12131. {
  12132. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12133. drm_atomic_state_default_release(state);
  12134. i915_sw_fence_fini(&intel_state->commit_ready);
  12135. kfree(state);
  12136. }
  12137. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12138. .fb_create = intel_user_framebuffer_create,
  12139. .output_poll_changed = intel_fbdev_output_poll_changed,
  12140. .atomic_check = intel_atomic_check,
  12141. .atomic_commit = intel_atomic_commit,
  12142. .atomic_state_alloc = intel_atomic_state_alloc,
  12143. .atomic_state_clear = intel_atomic_state_clear,
  12144. .atomic_state_free = intel_atomic_state_free,
  12145. };
  12146. /**
  12147. * intel_init_display_hooks - initialize the display modesetting hooks
  12148. * @dev_priv: device private
  12149. */
  12150. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12151. {
  12152. intel_init_cdclk_hooks(dev_priv);
  12153. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12154. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12155. dev_priv->display.get_initial_plane_config =
  12156. skylake_get_initial_plane_config;
  12157. dev_priv->display.crtc_compute_clock =
  12158. haswell_crtc_compute_clock;
  12159. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12160. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12161. } else if (HAS_DDI(dev_priv)) {
  12162. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12163. dev_priv->display.get_initial_plane_config =
  12164. ironlake_get_initial_plane_config;
  12165. dev_priv->display.crtc_compute_clock =
  12166. haswell_crtc_compute_clock;
  12167. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12168. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12169. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12170. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12171. dev_priv->display.get_initial_plane_config =
  12172. ironlake_get_initial_plane_config;
  12173. dev_priv->display.crtc_compute_clock =
  12174. ironlake_crtc_compute_clock;
  12175. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12176. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12177. } else if (IS_CHERRYVIEW(dev_priv)) {
  12178. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12179. dev_priv->display.get_initial_plane_config =
  12180. i9xx_get_initial_plane_config;
  12181. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12182. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12183. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12184. } else if (IS_VALLEYVIEW(dev_priv)) {
  12185. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12186. dev_priv->display.get_initial_plane_config =
  12187. i9xx_get_initial_plane_config;
  12188. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12189. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12190. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12191. } else if (IS_G4X(dev_priv)) {
  12192. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12193. dev_priv->display.get_initial_plane_config =
  12194. i9xx_get_initial_plane_config;
  12195. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12196. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12197. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12198. } else if (IS_PINEVIEW(dev_priv)) {
  12199. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12200. dev_priv->display.get_initial_plane_config =
  12201. i9xx_get_initial_plane_config;
  12202. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12203. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12204. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12205. } else if (!IS_GEN2(dev_priv)) {
  12206. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12207. dev_priv->display.get_initial_plane_config =
  12208. i9xx_get_initial_plane_config;
  12209. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12210. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12211. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12212. } else {
  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 = i8xx_crtc_compute_clock;
  12217. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12218. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12219. }
  12220. if (IS_GEN5(dev_priv)) {
  12221. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12222. } else if (IS_GEN6(dev_priv)) {
  12223. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12224. } else if (IS_IVYBRIDGE(dev_priv)) {
  12225. /* FIXME: detect B0+ stepping and use auto training */
  12226. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12227. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12228. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12229. }
  12230. if (dev_priv->info.gen >= 9)
  12231. dev_priv->display.update_crtcs = skl_update_crtcs;
  12232. else
  12233. dev_priv->display.update_crtcs = intel_update_crtcs;
  12234. switch (INTEL_INFO(dev_priv)->gen) {
  12235. case 2:
  12236. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12237. break;
  12238. case 3:
  12239. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12240. break;
  12241. case 4:
  12242. case 5:
  12243. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12244. break;
  12245. case 6:
  12246. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12247. break;
  12248. case 7:
  12249. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12250. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12251. break;
  12252. case 9:
  12253. /* Drop through - unsupported since execlist only. */
  12254. default:
  12255. /* Default just returns -ENODEV to indicate unsupported */
  12256. dev_priv->display.queue_flip = intel_default_queue_flip;
  12257. }
  12258. }
  12259. /*
  12260. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12261. * resume, or other times. This quirk makes sure that's the case for
  12262. * affected systems.
  12263. */
  12264. static void quirk_pipea_force(struct drm_device *dev)
  12265. {
  12266. struct drm_i915_private *dev_priv = to_i915(dev);
  12267. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12268. DRM_INFO("applying pipe a force quirk\n");
  12269. }
  12270. static void quirk_pipeb_force(struct drm_device *dev)
  12271. {
  12272. struct drm_i915_private *dev_priv = to_i915(dev);
  12273. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12274. DRM_INFO("applying pipe b force quirk\n");
  12275. }
  12276. /*
  12277. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12278. */
  12279. static void quirk_ssc_force_disable(struct drm_device *dev)
  12280. {
  12281. struct drm_i915_private *dev_priv = to_i915(dev);
  12282. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12283. DRM_INFO("applying lvds SSC disable quirk\n");
  12284. }
  12285. /*
  12286. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12287. * brightness value
  12288. */
  12289. static void quirk_invert_brightness(struct drm_device *dev)
  12290. {
  12291. struct drm_i915_private *dev_priv = to_i915(dev);
  12292. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12293. DRM_INFO("applying inverted panel brightness quirk\n");
  12294. }
  12295. /* Some VBT's incorrectly indicate no backlight is present */
  12296. static void quirk_backlight_present(struct drm_device *dev)
  12297. {
  12298. struct drm_i915_private *dev_priv = to_i915(dev);
  12299. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12300. DRM_INFO("applying backlight present quirk\n");
  12301. }
  12302. struct intel_quirk {
  12303. int device;
  12304. int subsystem_vendor;
  12305. int subsystem_device;
  12306. void (*hook)(struct drm_device *dev);
  12307. };
  12308. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12309. struct intel_dmi_quirk {
  12310. void (*hook)(struct drm_device *dev);
  12311. const struct dmi_system_id (*dmi_id_list)[];
  12312. };
  12313. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12314. {
  12315. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12316. return 1;
  12317. }
  12318. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12319. {
  12320. .dmi_id_list = &(const struct dmi_system_id[]) {
  12321. {
  12322. .callback = intel_dmi_reverse_brightness,
  12323. .ident = "NCR Corporation",
  12324. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12325. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12326. },
  12327. },
  12328. { } /* terminating entry */
  12329. },
  12330. .hook = quirk_invert_brightness,
  12331. },
  12332. };
  12333. static struct intel_quirk intel_quirks[] = {
  12334. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12335. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12336. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12337. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12338. /* 830 needs to leave pipe A & dpll A up */
  12339. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12340. /* 830 needs to leave pipe B & dpll B up */
  12341. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12342. /* Lenovo U160 cannot use SSC on LVDS */
  12343. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12344. /* Sony Vaio Y cannot use SSC on LVDS */
  12345. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12346. /* Acer Aspire 5734Z must invert backlight brightness */
  12347. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12348. /* Acer/eMachines G725 */
  12349. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12350. /* Acer/eMachines e725 */
  12351. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12352. /* Acer/Packard Bell NCL20 */
  12353. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12354. /* Acer Aspire 4736Z */
  12355. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12356. /* Acer Aspire 5336 */
  12357. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12358. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12359. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12360. /* Acer C720 Chromebook (Core i3 4005U) */
  12361. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12362. /* Apple Macbook 2,1 (Core 2 T7400) */
  12363. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12364. /* Apple Macbook 4,1 */
  12365. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12366. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12367. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12368. /* HP Chromebook 14 (Celeron 2955U) */
  12369. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12370. /* Dell Chromebook 11 */
  12371. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12372. /* Dell Chromebook 11 (2015 version) */
  12373. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12374. };
  12375. static void intel_init_quirks(struct drm_device *dev)
  12376. {
  12377. struct pci_dev *d = dev->pdev;
  12378. int i;
  12379. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12380. struct intel_quirk *q = &intel_quirks[i];
  12381. if (d->device == q->device &&
  12382. (d->subsystem_vendor == q->subsystem_vendor ||
  12383. q->subsystem_vendor == PCI_ANY_ID) &&
  12384. (d->subsystem_device == q->subsystem_device ||
  12385. q->subsystem_device == PCI_ANY_ID))
  12386. q->hook(dev);
  12387. }
  12388. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12389. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12390. intel_dmi_quirks[i].hook(dev);
  12391. }
  12392. }
  12393. /* Disable the VGA plane that we never use */
  12394. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12395. {
  12396. struct pci_dev *pdev = dev_priv->drm.pdev;
  12397. u8 sr1;
  12398. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12399. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12400. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12401. outb(SR01, VGA_SR_INDEX);
  12402. sr1 = inb(VGA_SR_DATA);
  12403. outb(sr1 | 1<<5, VGA_SR_DATA);
  12404. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12405. udelay(300);
  12406. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12407. POSTING_READ(vga_reg);
  12408. }
  12409. void intel_modeset_init_hw(struct drm_device *dev)
  12410. {
  12411. struct drm_i915_private *dev_priv = to_i915(dev);
  12412. intel_update_cdclk(dev_priv);
  12413. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12414. intel_init_clock_gating(dev_priv);
  12415. }
  12416. /*
  12417. * Calculate what we think the watermarks should be for the state we've read
  12418. * out of the hardware and then immediately program those watermarks so that
  12419. * we ensure the hardware settings match our internal state.
  12420. *
  12421. * We can calculate what we think WM's should be by creating a duplicate of the
  12422. * current state (which was constructed during hardware readout) and running it
  12423. * through the atomic check code to calculate new watermark values in the
  12424. * state object.
  12425. */
  12426. static void sanitize_watermarks(struct drm_device *dev)
  12427. {
  12428. struct drm_i915_private *dev_priv = to_i915(dev);
  12429. struct drm_atomic_state *state;
  12430. struct intel_atomic_state *intel_state;
  12431. struct drm_crtc *crtc;
  12432. struct drm_crtc_state *cstate;
  12433. struct drm_modeset_acquire_ctx ctx;
  12434. int ret;
  12435. int i;
  12436. /* Only supported on platforms that use atomic watermark design */
  12437. if (!dev_priv->display.optimize_watermarks)
  12438. return;
  12439. /*
  12440. * We need to hold connection_mutex before calling duplicate_state so
  12441. * that the connector loop is protected.
  12442. */
  12443. drm_modeset_acquire_init(&ctx, 0);
  12444. retry:
  12445. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12446. if (ret == -EDEADLK) {
  12447. drm_modeset_backoff(&ctx);
  12448. goto retry;
  12449. } else if (WARN_ON(ret)) {
  12450. goto fail;
  12451. }
  12452. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12453. if (WARN_ON(IS_ERR(state)))
  12454. goto fail;
  12455. intel_state = to_intel_atomic_state(state);
  12456. /*
  12457. * Hardware readout is the only time we don't want to calculate
  12458. * intermediate watermarks (since we don't trust the current
  12459. * watermarks).
  12460. */
  12461. intel_state->skip_intermediate_wm = true;
  12462. ret = intel_atomic_check(dev, state);
  12463. if (ret) {
  12464. /*
  12465. * If we fail here, it means that the hardware appears to be
  12466. * programmed in a way that shouldn't be possible, given our
  12467. * understanding of watermark requirements. This might mean a
  12468. * mistake in the hardware readout code or a mistake in the
  12469. * watermark calculations for a given platform. Raise a WARN
  12470. * so that this is noticeable.
  12471. *
  12472. * If this actually happens, we'll have to just leave the
  12473. * BIOS-programmed watermarks untouched and hope for the best.
  12474. */
  12475. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12476. goto put_state;
  12477. }
  12478. /* Write calculated watermark values back */
  12479. for_each_crtc_in_state(state, crtc, cstate, i) {
  12480. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12481. cs->wm.need_postvbl_update = true;
  12482. dev_priv->display.optimize_watermarks(intel_state, cs);
  12483. }
  12484. put_state:
  12485. drm_atomic_state_put(state);
  12486. fail:
  12487. drm_modeset_drop_locks(&ctx);
  12488. drm_modeset_acquire_fini(&ctx);
  12489. }
  12490. int intel_modeset_init(struct drm_device *dev)
  12491. {
  12492. struct drm_i915_private *dev_priv = to_i915(dev);
  12493. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12494. enum pipe pipe;
  12495. struct intel_crtc *crtc;
  12496. drm_mode_config_init(dev);
  12497. dev->mode_config.min_width = 0;
  12498. dev->mode_config.min_height = 0;
  12499. dev->mode_config.preferred_depth = 24;
  12500. dev->mode_config.prefer_shadow = 1;
  12501. dev->mode_config.allow_fb_modifiers = true;
  12502. dev->mode_config.funcs = &intel_mode_funcs;
  12503. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12504. intel_atomic_helper_free_state_worker);
  12505. intel_init_quirks(dev);
  12506. intel_init_pm(dev_priv);
  12507. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12508. return 0;
  12509. /*
  12510. * There may be no VBT; and if the BIOS enabled SSC we can
  12511. * just keep using it to avoid unnecessary flicker. Whereas if the
  12512. * BIOS isn't using it, don't assume it will work even if the VBT
  12513. * indicates as much.
  12514. */
  12515. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12516. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12517. DREF_SSC1_ENABLE);
  12518. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12519. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12520. bios_lvds_use_ssc ? "en" : "dis",
  12521. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12522. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12523. }
  12524. }
  12525. if (IS_GEN2(dev_priv)) {
  12526. dev->mode_config.max_width = 2048;
  12527. dev->mode_config.max_height = 2048;
  12528. } else if (IS_GEN3(dev_priv)) {
  12529. dev->mode_config.max_width = 4096;
  12530. dev->mode_config.max_height = 4096;
  12531. } else {
  12532. dev->mode_config.max_width = 8192;
  12533. dev->mode_config.max_height = 8192;
  12534. }
  12535. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12536. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12537. dev->mode_config.cursor_height = 1023;
  12538. } else if (IS_GEN2(dev_priv)) {
  12539. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12540. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12541. } else {
  12542. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12543. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12544. }
  12545. dev->mode_config.fb_base = ggtt->mappable_base;
  12546. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12547. INTEL_INFO(dev_priv)->num_pipes,
  12548. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12549. for_each_pipe(dev_priv, pipe) {
  12550. int ret;
  12551. ret = intel_crtc_init(dev_priv, pipe);
  12552. if (ret) {
  12553. drm_mode_config_cleanup(dev);
  12554. return ret;
  12555. }
  12556. }
  12557. intel_update_czclk(dev_priv);
  12558. intel_update_cdclk(dev_priv);
  12559. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12560. intel_shared_dpll_init(dev);
  12561. if (dev_priv->max_cdclk_freq == 0)
  12562. intel_update_max_cdclk(dev_priv);
  12563. /* Just disable it once at startup */
  12564. i915_disable_vga(dev_priv);
  12565. intel_setup_outputs(dev_priv);
  12566. drm_modeset_lock_all(dev);
  12567. intel_modeset_setup_hw_state(dev);
  12568. drm_modeset_unlock_all(dev);
  12569. for_each_intel_crtc(dev, crtc) {
  12570. struct intel_initial_plane_config plane_config = {};
  12571. if (!crtc->active)
  12572. continue;
  12573. /*
  12574. * Note that reserving the BIOS fb up front prevents us
  12575. * from stuffing other stolen allocations like the ring
  12576. * on top. This prevents some ugliness at boot time, and
  12577. * can even allow for smooth boot transitions if the BIOS
  12578. * fb is large enough for the active pipe configuration.
  12579. */
  12580. dev_priv->display.get_initial_plane_config(crtc,
  12581. &plane_config);
  12582. /*
  12583. * If the fb is shared between multiple heads, we'll
  12584. * just get the first one.
  12585. */
  12586. intel_find_initial_plane_obj(crtc, &plane_config);
  12587. }
  12588. /*
  12589. * Make sure hardware watermarks really match the state we read out.
  12590. * Note that we need to do this after reconstructing the BIOS fb's
  12591. * since the watermark calculation done here will use pstate->fb.
  12592. */
  12593. sanitize_watermarks(dev);
  12594. return 0;
  12595. }
  12596. static void intel_enable_pipe_a(struct drm_device *dev)
  12597. {
  12598. struct intel_connector *connector;
  12599. struct drm_connector *crt = NULL;
  12600. struct intel_load_detect_pipe load_detect_temp;
  12601. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12602. /* We can't just switch on the pipe A, we need to set things up with a
  12603. * proper mode and output configuration. As a gross hack, enable pipe A
  12604. * by enabling the load detect pipe once. */
  12605. for_each_intel_connector(dev, connector) {
  12606. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12607. crt = &connector->base;
  12608. break;
  12609. }
  12610. }
  12611. if (!crt)
  12612. return;
  12613. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12614. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12615. }
  12616. static bool
  12617. intel_check_plane_mapping(struct intel_crtc *crtc)
  12618. {
  12619. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12620. u32 val;
  12621. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  12622. return true;
  12623. val = I915_READ(DSPCNTR(!crtc->plane));
  12624. if ((val & DISPLAY_PLANE_ENABLE) &&
  12625. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12626. return false;
  12627. return true;
  12628. }
  12629. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12630. {
  12631. struct drm_device *dev = crtc->base.dev;
  12632. struct intel_encoder *encoder;
  12633. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12634. return true;
  12635. return false;
  12636. }
  12637. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12638. {
  12639. struct drm_device *dev = encoder->base.dev;
  12640. struct intel_connector *connector;
  12641. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12642. return connector;
  12643. return NULL;
  12644. }
  12645. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12646. enum transcoder pch_transcoder)
  12647. {
  12648. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12649. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  12650. }
  12651. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12652. {
  12653. struct drm_device *dev = crtc->base.dev;
  12654. struct drm_i915_private *dev_priv = to_i915(dev);
  12655. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12656. /* Clear any frame start delays used for debugging left by the BIOS */
  12657. if (!transcoder_is_dsi(cpu_transcoder)) {
  12658. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12659. I915_WRITE(reg,
  12660. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12661. }
  12662. /* restore vblank interrupts to correct state */
  12663. drm_crtc_vblank_reset(&crtc->base);
  12664. if (crtc->active) {
  12665. struct intel_plane *plane;
  12666. drm_crtc_vblank_on(&crtc->base);
  12667. /* Disable everything but the primary plane */
  12668. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12669. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12670. continue;
  12671. plane->disable_plane(&plane->base, &crtc->base);
  12672. }
  12673. }
  12674. /* We need to sanitize the plane -> pipe mapping first because this will
  12675. * disable the crtc (and hence change the state) if it is wrong. Note
  12676. * that gen4+ has a fixed plane -> pipe mapping. */
  12677. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  12678. bool plane;
  12679. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  12680. crtc->base.base.id, crtc->base.name);
  12681. /* Pipe has the wrong plane attached and the plane is active.
  12682. * Temporarily change the plane mapping and disable everything
  12683. * ... */
  12684. plane = crtc->plane;
  12685. crtc->base.primary->state->visible = true;
  12686. crtc->plane = !plane;
  12687. intel_crtc_disable_noatomic(&crtc->base);
  12688. crtc->plane = plane;
  12689. }
  12690. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12691. crtc->pipe == PIPE_A && !crtc->active) {
  12692. /* BIOS forgot to enable pipe A, this mostly happens after
  12693. * resume. Force-enable the pipe to fix this, the update_dpms
  12694. * call below we restore the pipe to the right state, but leave
  12695. * the required bits on. */
  12696. intel_enable_pipe_a(dev);
  12697. }
  12698. /* Adjust the state of the output pipe according to whether we
  12699. * have active connectors/encoders. */
  12700. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12701. intel_crtc_disable_noatomic(&crtc->base);
  12702. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12703. /*
  12704. * We start out with underrun reporting disabled to avoid races.
  12705. * For correct bookkeeping mark this on active crtcs.
  12706. *
  12707. * Also on gmch platforms we dont have any hardware bits to
  12708. * disable the underrun reporting. Which means we need to start
  12709. * out with underrun reporting disabled also on inactive pipes,
  12710. * since otherwise we'll complain about the garbage we read when
  12711. * e.g. coming up after runtime pm.
  12712. *
  12713. * No protection against concurrent access is required - at
  12714. * worst a fifo underrun happens which also sets this to false.
  12715. */
  12716. crtc->cpu_fifo_underrun_disabled = true;
  12717. /*
  12718. * We track the PCH trancoder underrun reporting state
  12719. * within the crtc. With crtc for pipe A housing the underrun
  12720. * reporting state for PCH transcoder A, crtc for pipe B housing
  12721. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12722. * and marking underrun reporting as disabled for the non-existing
  12723. * PCH transcoders B and C would prevent enabling the south
  12724. * error interrupt (see cpt_can_enable_serr_int()).
  12725. */
  12726. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  12727. crtc->pch_fifo_underrun_disabled = true;
  12728. }
  12729. }
  12730. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12731. {
  12732. struct intel_connector *connector;
  12733. /* We need to check both for a crtc link (meaning that the
  12734. * encoder is active and trying to read from a pipe) and the
  12735. * pipe itself being active. */
  12736. bool has_active_crtc = encoder->base.crtc &&
  12737. to_intel_crtc(encoder->base.crtc)->active;
  12738. connector = intel_encoder_find_connector(encoder);
  12739. if (connector && !has_active_crtc) {
  12740. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12741. encoder->base.base.id,
  12742. encoder->base.name);
  12743. /* Connector is active, but has no active pipe. This is
  12744. * fallout from our resume register restoring. Disable
  12745. * the encoder manually again. */
  12746. if (encoder->base.crtc) {
  12747. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12748. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12749. encoder->base.base.id,
  12750. encoder->base.name);
  12751. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12752. if (encoder->post_disable)
  12753. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12754. }
  12755. encoder->base.crtc = NULL;
  12756. /* Inconsistent output/port/pipe state happens presumably due to
  12757. * a bug in one of the get_hw_state functions. Or someplace else
  12758. * in our code, like the register restore mess on resume. Clamp
  12759. * things to off as a safer default. */
  12760. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12761. connector->base.encoder = NULL;
  12762. }
  12763. /* Enabled encoders without active connectors will be fixed in
  12764. * the crtc fixup. */
  12765. }
  12766. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12767. {
  12768. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12769. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12770. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12771. i915_disable_vga(dev_priv);
  12772. }
  12773. }
  12774. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12775. {
  12776. /* This function can be called both from intel_modeset_setup_hw_state or
  12777. * at a very early point in our resume sequence, where the power well
  12778. * structures are not yet restored. Since this function is at a very
  12779. * paranoid "someone might have enabled VGA while we were not looking"
  12780. * level, just check if the power well is enabled instead of trying to
  12781. * follow the "don't touch the power well if we don't need it" policy
  12782. * the rest of the driver uses. */
  12783. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12784. return;
  12785. i915_redisable_vga_power_on(dev_priv);
  12786. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12787. }
  12788. static bool primary_get_hw_state(struct intel_plane *plane)
  12789. {
  12790. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12791. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12792. }
  12793. /* FIXME read out full plane state for all planes */
  12794. static void readout_plane_state(struct intel_crtc *crtc)
  12795. {
  12796. struct drm_plane *primary = crtc->base.primary;
  12797. struct intel_plane_state *plane_state =
  12798. to_intel_plane_state(primary->state);
  12799. plane_state->base.visible = crtc->active &&
  12800. primary_get_hw_state(to_intel_plane(primary));
  12801. if (plane_state->base.visible)
  12802. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  12803. }
  12804. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12805. {
  12806. struct drm_i915_private *dev_priv = to_i915(dev);
  12807. enum pipe pipe;
  12808. struct intel_crtc *crtc;
  12809. struct intel_encoder *encoder;
  12810. struct intel_connector *connector;
  12811. int i;
  12812. dev_priv->active_crtcs = 0;
  12813. for_each_intel_crtc(dev, crtc) {
  12814. struct intel_crtc_state *crtc_state =
  12815. to_intel_crtc_state(crtc->base.state);
  12816. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12817. memset(crtc_state, 0, sizeof(*crtc_state));
  12818. crtc_state->base.crtc = &crtc->base;
  12819. crtc_state->base.active = crtc_state->base.enable =
  12820. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12821. crtc->base.enabled = crtc_state->base.enable;
  12822. crtc->active = crtc_state->base.active;
  12823. if (crtc_state->base.active)
  12824. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12825. readout_plane_state(crtc);
  12826. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12827. crtc->base.base.id, crtc->base.name,
  12828. enableddisabled(crtc_state->base.active));
  12829. }
  12830. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12831. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12832. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12833. &pll->state.hw_state);
  12834. pll->state.crtc_mask = 0;
  12835. for_each_intel_crtc(dev, crtc) {
  12836. struct intel_crtc_state *crtc_state =
  12837. to_intel_crtc_state(crtc->base.state);
  12838. if (crtc_state->base.active &&
  12839. crtc_state->shared_dpll == pll)
  12840. pll->state.crtc_mask |= 1 << crtc->pipe;
  12841. }
  12842. pll->active_mask = pll->state.crtc_mask;
  12843. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12844. pll->name, pll->state.crtc_mask, pll->on);
  12845. }
  12846. for_each_intel_encoder(dev, encoder) {
  12847. pipe = 0;
  12848. if (encoder->get_hw_state(encoder, &pipe)) {
  12849. struct intel_crtc_state *crtc_state;
  12850. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12851. crtc_state = to_intel_crtc_state(crtc->base.state);
  12852. encoder->base.crtc = &crtc->base;
  12853. crtc_state->output_types |= 1 << encoder->type;
  12854. encoder->get_config(encoder, crtc_state);
  12855. } else {
  12856. encoder->base.crtc = NULL;
  12857. }
  12858. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12859. encoder->base.base.id, encoder->base.name,
  12860. enableddisabled(encoder->base.crtc),
  12861. pipe_name(pipe));
  12862. }
  12863. for_each_intel_connector(dev, connector) {
  12864. if (connector->get_hw_state(connector)) {
  12865. connector->base.dpms = DRM_MODE_DPMS_ON;
  12866. encoder = connector->encoder;
  12867. connector->base.encoder = &encoder->base;
  12868. if (encoder->base.crtc &&
  12869. encoder->base.crtc->state->active) {
  12870. /*
  12871. * This has to be done during hardware readout
  12872. * because anything calling .crtc_disable may
  12873. * rely on the connector_mask being accurate.
  12874. */
  12875. encoder->base.crtc->state->connector_mask |=
  12876. 1 << drm_connector_index(&connector->base);
  12877. encoder->base.crtc->state->encoder_mask |=
  12878. 1 << drm_encoder_index(&encoder->base);
  12879. }
  12880. } else {
  12881. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12882. connector->base.encoder = NULL;
  12883. }
  12884. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12885. connector->base.base.id, connector->base.name,
  12886. enableddisabled(connector->base.encoder));
  12887. }
  12888. for_each_intel_crtc(dev, crtc) {
  12889. struct intel_crtc_state *crtc_state =
  12890. to_intel_crtc_state(crtc->base.state);
  12891. int pixclk = 0;
  12892. crtc->base.hwmode = crtc_state->base.adjusted_mode;
  12893. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12894. if (crtc_state->base.active) {
  12895. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12896. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12897. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12898. /*
  12899. * The initial mode needs to be set in order to keep
  12900. * the atomic core happy. It wants a valid mode if the
  12901. * crtc's enabled, so we do the above call.
  12902. *
  12903. * But we don't set all the derived state fully, hence
  12904. * set a flag to indicate that a full recalculation is
  12905. * needed on the next commit.
  12906. */
  12907. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12908. intel_crtc_compute_pixel_rate(crtc_state);
  12909. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
  12910. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12911. pixclk = crtc_state->pixel_rate;
  12912. else
  12913. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  12914. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  12915. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  12916. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  12917. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12918. update_scanline_offset(crtc);
  12919. }
  12920. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  12921. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12922. }
  12923. }
  12924. /* Scan out the current hw modeset state,
  12925. * and sanitizes it to the current state
  12926. */
  12927. static void
  12928. intel_modeset_setup_hw_state(struct drm_device *dev)
  12929. {
  12930. struct drm_i915_private *dev_priv = to_i915(dev);
  12931. enum pipe pipe;
  12932. struct intel_crtc *crtc;
  12933. struct intel_encoder *encoder;
  12934. int i;
  12935. intel_modeset_readout_hw_state(dev);
  12936. /* HW state is read out, now we need to sanitize this mess. */
  12937. for_each_intel_encoder(dev, encoder) {
  12938. intel_sanitize_encoder(encoder);
  12939. }
  12940. for_each_pipe(dev_priv, pipe) {
  12941. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12942. intel_sanitize_crtc(crtc);
  12943. intel_dump_pipe_config(crtc, crtc->config,
  12944. "[setup_hw_state]");
  12945. }
  12946. intel_modeset_update_connector_atomic_state(dev);
  12947. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12948. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12949. if (!pll->on || pll->active_mask)
  12950. continue;
  12951. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12952. pll->funcs.disable(dev_priv, pll);
  12953. pll->on = false;
  12954. }
  12955. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12956. vlv_wm_get_hw_state(dev);
  12957. else if (IS_GEN9(dev_priv))
  12958. skl_wm_get_hw_state(dev);
  12959. else if (HAS_PCH_SPLIT(dev_priv))
  12960. ilk_wm_get_hw_state(dev);
  12961. for_each_intel_crtc(dev, crtc) {
  12962. u64 put_domains;
  12963. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12964. if (WARN_ON(put_domains))
  12965. modeset_put_power_domains(dev_priv, put_domains);
  12966. }
  12967. intel_display_set_init_power(dev_priv, false);
  12968. intel_fbc_init_pipe_state(dev_priv);
  12969. }
  12970. void intel_display_resume(struct drm_device *dev)
  12971. {
  12972. struct drm_i915_private *dev_priv = to_i915(dev);
  12973. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12974. struct drm_modeset_acquire_ctx ctx;
  12975. int ret;
  12976. dev_priv->modeset_restore_state = NULL;
  12977. if (state)
  12978. state->acquire_ctx = &ctx;
  12979. /*
  12980. * This is a cludge because with real atomic modeset mode_config.mutex
  12981. * won't be taken. Unfortunately some probed state like
  12982. * audio_codec_enable is still protected by mode_config.mutex, so lock
  12983. * it here for now.
  12984. */
  12985. mutex_lock(&dev->mode_config.mutex);
  12986. drm_modeset_acquire_init(&ctx, 0);
  12987. while (1) {
  12988. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12989. if (ret != -EDEADLK)
  12990. break;
  12991. drm_modeset_backoff(&ctx);
  12992. }
  12993. if (!ret)
  12994. ret = __intel_display_resume(dev, state);
  12995. drm_modeset_drop_locks(&ctx);
  12996. drm_modeset_acquire_fini(&ctx);
  12997. mutex_unlock(&dev->mode_config.mutex);
  12998. if (ret)
  12999. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13000. if (state)
  13001. drm_atomic_state_put(state);
  13002. }
  13003. void intel_modeset_gem_init(struct drm_device *dev)
  13004. {
  13005. struct drm_i915_private *dev_priv = to_i915(dev);
  13006. intel_init_gt_powersave(dev_priv);
  13007. intel_modeset_init_hw(dev);
  13008. intel_setup_overlay(dev_priv);
  13009. }
  13010. int intel_connector_register(struct drm_connector *connector)
  13011. {
  13012. struct intel_connector *intel_connector = to_intel_connector(connector);
  13013. int ret;
  13014. ret = intel_backlight_device_register(intel_connector);
  13015. if (ret)
  13016. goto err;
  13017. return 0;
  13018. err:
  13019. return ret;
  13020. }
  13021. void intel_connector_unregister(struct drm_connector *connector)
  13022. {
  13023. struct intel_connector *intel_connector = to_intel_connector(connector);
  13024. intel_backlight_device_unregister(intel_connector);
  13025. intel_panel_destroy_backlight(connector);
  13026. }
  13027. void intel_modeset_cleanup(struct drm_device *dev)
  13028. {
  13029. struct drm_i915_private *dev_priv = to_i915(dev);
  13030. flush_work(&dev_priv->atomic_helper.free_work);
  13031. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  13032. intel_disable_gt_powersave(dev_priv);
  13033. /*
  13034. * Interrupts and polling as the first thing to avoid creating havoc.
  13035. * Too much stuff here (turning of connectors, ...) would
  13036. * experience fancy races otherwise.
  13037. */
  13038. intel_irq_uninstall(dev_priv);
  13039. /*
  13040. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13041. * poll handlers. Hence disable polling after hpd handling is shut down.
  13042. */
  13043. drm_kms_helper_poll_fini(dev);
  13044. intel_unregister_dsm_handler();
  13045. intel_fbc_global_disable(dev_priv);
  13046. /* flush any delayed tasks or pending work */
  13047. flush_scheduled_work();
  13048. drm_mode_config_cleanup(dev);
  13049. intel_cleanup_overlay(dev_priv);
  13050. intel_cleanup_gt_powersave(dev_priv);
  13051. intel_teardown_gmbus(dev_priv);
  13052. }
  13053. void intel_connector_attach_encoder(struct intel_connector *connector,
  13054. struct intel_encoder *encoder)
  13055. {
  13056. connector->encoder = encoder;
  13057. drm_mode_connector_attach_encoder(&connector->base,
  13058. &encoder->base);
  13059. }
  13060. /*
  13061. * set vga decode state - true == enable VGA decode
  13062. */
  13063. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  13064. {
  13065. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13066. u16 gmch_ctrl;
  13067. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13068. DRM_ERROR("failed to read control word\n");
  13069. return -EIO;
  13070. }
  13071. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13072. return 0;
  13073. if (state)
  13074. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13075. else
  13076. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13077. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13078. DRM_ERROR("failed to write control word\n");
  13079. return -EIO;
  13080. }
  13081. return 0;
  13082. }
  13083. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  13084. struct intel_display_error_state {
  13085. u32 power_well_driver;
  13086. int num_transcoders;
  13087. struct intel_cursor_error_state {
  13088. u32 control;
  13089. u32 position;
  13090. u32 base;
  13091. u32 size;
  13092. } cursor[I915_MAX_PIPES];
  13093. struct intel_pipe_error_state {
  13094. bool power_domain_on;
  13095. u32 source;
  13096. u32 stat;
  13097. } pipe[I915_MAX_PIPES];
  13098. struct intel_plane_error_state {
  13099. u32 control;
  13100. u32 stride;
  13101. u32 size;
  13102. u32 pos;
  13103. u32 addr;
  13104. u32 surface;
  13105. u32 tile_offset;
  13106. } plane[I915_MAX_PIPES];
  13107. struct intel_transcoder_error_state {
  13108. bool power_domain_on;
  13109. enum transcoder cpu_transcoder;
  13110. u32 conf;
  13111. u32 htotal;
  13112. u32 hblank;
  13113. u32 hsync;
  13114. u32 vtotal;
  13115. u32 vblank;
  13116. u32 vsync;
  13117. } transcoder[4];
  13118. };
  13119. struct intel_display_error_state *
  13120. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13121. {
  13122. struct intel_display_error_state *error;
  13123. int transcoders[] = {
  13124. TRANSCODER_A,
  13125. TRANSCODER_B,
  13126. TRANSCODER_C,
  13127. TRANSCODER_EDP,
  13128. };
  13129. int i;
  13130. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13131. return NULL;
  13132. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13133. if (error == NULL)
  13134. return NULL;
  13135. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13136. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13137. for_each_pipe(dev_priv, i) {
  13138. error->pipe[i].power_domain_on =
  13139. __intel_display_power_is_enabled(dev_priv,
  13140. POWER_DOMAIN_PIPE(i));
  13141. if (!error->pipe[i].power_domain_on)
  13142. continue;
  13143. error->cursor[i].control = I915_READ(CURCNTR(i));
  13144. error->cursor[i].position = I915_READ(CURPOS(i));
  13145. error->cursor[i].base = I915_READ(CURBASE(i));
  13146. error->plane[i].control = I915_READ(DSPCNTR(i));
  13147. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13148. if (INTEL_GEN(dev_priv) <= 3) {
  13149. error->plane[i].size = I915_READ(DSPSIZE(i));
  13150. error->plane[i].pos = I915_READ(DSPPOS(i));
  13151. }
  13152. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13153. error->plane[i].addr = I915_READ(DSPADDR(i));
  13154. if (INTEL_GEN(dev_priv) >= 4) {
  13155. error->plane[i].surface = I915_READ(DSPSURF(i));
  13156. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13157. }
  13158. error->pipe[i].source = I915_READ(PIPESRC(i));
  13159. if (HAS_GMCH_DISPLAY(dev_priv))
  13160. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13161. }
  13162. /* Note: this does not include DSI transcoders. */
  13163. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13164. if (HAS_DDI(dev_priv))
  13165. error->num_transcoders++; /* Account for eDP. */
  13166. for (i = 0; i < error->num_transcoders; i++) {
  13167. enum transcoder cpu_transcoder = transcoders[i];
  13168. error->transcoder[i].power_domain_on =
  13169. __intel_display_power_is_enabled(dev_priv,
  13170. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13171. if (!error->transcoder[i].power_domain_on)
  13172. continue;
  13173. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13174. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13175. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13176. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13177. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13178. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13179. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13180. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13181. }
  13182. return error;
  13183. }
  13184. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13185. void
  13186. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13187. struct intel_display_error_state *error)
  13188. {
  13189. struct drm_i915_private *dev_priv = m->i915;
  13190. int i;
  13191. if (!error)
  13192. return;
  13193. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  13194. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13195. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13196. error->power_well_driver);
  13197. for_each_pipe(dev_priv, i) {
  13198. err_printf(m, "Pipe [%d]:\n", i);
  13199. err_printf(m, " Power: %s\n",
  13200. onoff(error->pipe[i].power_domain_on));
  13201. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13202. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13203. err_printf(m, "Plane [%d]:\n", i);
  13204. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13205. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13206. if (INTEL_GEN(dev_priv) <= 3) {
  13207. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13208. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13209. }
  13210. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13211. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13212. if (INTEL_GEN(dev_priv) >= 4) {
  13213. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13214. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13215. }
  13216. err_printf(m, "Cursor [%d]:\n", i);
  13217. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13218. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13219. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13220. }
  13221. for (i = 0; i < error->num_transcoders; i++) {
  13222. err_printf(m, "CPU transcoder: %s\n",
  13223. transcoder_name(error->transcoder[i].cpu_transcoder));
  13224. err_printf(m, " Power: %s\n",
  13225. onoff(error->transcoder[i].power_domain_on));
  13226. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13227. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13228. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13229. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13230. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13231. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13232. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13233. }
  13234. }
  13235. #endif