intel_display.c 432 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "i915_gem_clflush.h"
  40. #include "intel_dsi.h"
  41. #include "i915_trace.h"
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_dp_helper.h>
  45. #include <drm/drm_crtc_helper.h>
  46. #include <drm/drm_plane_helper.h>
  47. #include <drm/drm_rect.h>
  48. #include <linux/dma_remapping.h>
  49. #include <linux/reservation.h>
  50. static bool is_mmio_work(struct intel_flip_work *work)
  51. {
  52. return work->mmio_work.func;
  53. }
  54. /* Primary plane formats for gen <= 3 */
  55. static const uint32_t i8xx_primary_formats[] = {
  56. DRM_FORMAT_C8,
  57. DRM_FORMAT_RGB565,
  58. DRM_FORMAT_XRGB1555,
  59. DRM_FORMAT_XRGB8888,
  60. };
  61. /* Primary plane formats for gen >= 4 */
  62. static const uint32_t i965_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_XRGB2101010,
  68. DRM_FORMAT_XBGR2101010,
  69. };
  70. static const uint32_t skl_primary_formats[] = {
  71. DRM_FORMAT_C8,
  72. DRM_FORMAT_RGB565,
  73. DRM_FORMAT_XRGB8888,
  74. DRM_FORMAT_XBGR8888,
  75. DRM_FORMAT_ARGB8888,
  76. DRM_FORMAT_ABGR8888,
  77. DRM_FORMAT_XRGB2101010,
  78. DRM_FORMAT_XBGR2101010,
  79. DRM_FORMAT_YUYV,
  80. DRM_FORMAT_YVYU,
  81. DRM_FORMAT_UYVY,
  82. DRM_FORMAT_VYUY,
  83. };
  84. /* Cursor formats */
  85. static const uint32_t intel_cursor_formats[] = {
  86. DRM_FORMAT_ARGB8888,
  87. };
  88. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  89. struct intel_crtc_state *pipe_config);
  90. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  91. struct intel_crtc_state *pipe_config);
  92. static int intel_framebuffer_init(struct intel_framebuffer *ifb,
  93. struct drm_i915_gem_object *obj,
  94. struct drm_mode_fb_cmd2 *mode_cmd);
  95. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  96. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  98. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  99. struct intel_link_m_n *m_n,
  100. struct intel_link_m_n *m2_n2);
  101. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  102. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  104. static void vlv_prepare_pll(struct intel_crtc *crtc,
  105. const struct intel_crtc_state *pipe_config);
  106. static void chv_prepare_pll(struct intel_crtc *crtc,
  107. const struct intel_crtc_state *pipe_config);
  108. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  109. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  111. struct intel_crtc_state *crtc_state);
  112. static void skylake_pfit_enable(struct intel_crtc *crtc);
  113. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  114. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  115. static void intel_modeset_setup_hw_state(struct drm_device *dev,
  116. struct drm_modeset_acquire_ctx *ctx);
  117. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  118. struct intel_limit {
  119. struct {
  120. int min, max;
  121. } dot, vco, n, m, m1, m2, p, p1;
  122. struct {
  123. int dot_limit;
  124. int p2_slow, p2_fast;
  125. } p2;
  126. };
  127. /* returns HPLL frequency in kHz */
  128. int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
  129. {
  130. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  131. /* Obtain SKU information */
  132. mutex_lock(&dev_priv->sb_lock);
  133. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  134. CCK_FUSE_HPLL_FREQ_MASK;
  135. mutex_unlock(&dev_priv->sb_lock);
  136. return vco_freq[hpll_freq] * 1000;
  137. }
  138. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  139. const char *name, u32 reg, int ref_freq)
  140. {
  141. u32 val;
  142. int divider;
  143. mutex_lock(&dev_priv->sb_lock);
  144. val = vlv_cck_read(dev_priv, reg);
  145. mutex_unlock(&dev_priv->sb_lock);
  146. divider = val & CCK_FREQUENCY_VALUES;
  147. WARN((val & CCK_FREQUENCY_STATUS) !=
  148. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  149. "%s change in progress\n", name);
  150. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  151. }
  152. int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  153. const char *name, u32 reg)
  154. {
  155. if (dev_priv->hpll_freq == 0)
  156. dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
  157. return vlv_get_cck_clock(dev_priv, name, reg,
  158. dev_priv->hpll_freq);
  159. }
  160. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  161. {
  162. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  163. return;
  164. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  165. CCK_CZ_CLOCK_CONTROL);
  166. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  167. }
  168. static inline u32 /* units of 100MHz */
  169. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  170. const struct intel_crtc_state *pipe_config)
  171. {
  172. if (HAS_DDI(dev_priv))
  173. return pipe_config->port_clock; /* SPLL */
  174. else if (IS_GEN5(dev_priv))
  175. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  176. else
  177. return 270000;
  178. }
  179. static const struct intel_limit intel_limits_i8xx_dac = {
  180. .dot = { .min = 25000, .max = 350000 },
  181. .vco = { .min = 908000, .max = 1512000 },
  182. .n = { .min = 2, .max = 16 },
  183. .m = { .min = 96, .max = 140 },
  184. .m1 = { .min = 18, .max = 26 },
  185. .m2 = { .min = 6, .max = 16 },
  186. .p = { .min = 4, .max = 128 },
  187. .p1 = { .min = 2, .max = 33 },
  188. .p2 = { .dot_limit = 165000,
  189. .p2_slow = 4, .p2_fast = 2 },
  190. };
  191. static const struct intel_limit intel_limits_i8xx_dvo = {
  192. .dot = { .min = 25000, .max = 350000 },
  193. .vco = { .min = 908000, .max = 1512000 },
  194. .n = { .min = 2, .max = 16 },
  195. .m = { .min = 96, .max = 140 },
  196. .m1 = { .min = 18, .max = 26 },
  197. .m2 = { .min = 6, .max = 16 },
  198. .p = { .min = 4, .max = 128 },
  199. .p1 = { .min = 2, .max = 33 },
  200. .p2 = { .dot_limit = 165000,
  201. .p2_slow = 4, .p2_fast = 4 },
  202. };
  203. static const struct intel_limit intel_limits_i8xx_lvds = {
  204. .dot = { .min = 25000, .max = 350000 },
  205. .vco = { .min = 908000, .max = 1512000 },
  206. .n = { .min = 2, .max = 16 },
  207. .m = { .min = 96, .max = 140 },
  208. .m1 = { .min = 18, .max = 26 },
  209. .m2 = { .min = 6, .max = 16 },
  210. .p = { .min = 4, .max = 128 },
  211. .p1 = { .min = 1, .max = 6 },
  212. .p2 = { .dot_limit = 165000,
  213. .p2_slow = 14, .p2_fast = 7 },
  214. };
  215. static const struct intel_limit intel_limits_i9xx_sdvo = {
  216. .dot = { .min = 20000, .max = 400000 },
  217. .vco = { .min = 1400000, .max = 2800000 },
  218. .n = { .min = 1, .max = 6 },
  219. .m = { .min = 70, .max = 120 },
  220. .m1 = { .min = 8, .max = 18 },
  221. .m2 = { .min = 3, .max = 7 },
  222. .p = { .min = 5, .max = 80 },
  223. .p1 = { .min = 1, .max = 8 },
  224. .p2 = { .dot_limit = 200000,
  225. .p2_slow = 10, .p2_fast = 5 },
  226. };
  227. static const struct intel_limit intel_limits_i9xx_lvds = {
  228. .dot = { .min = 20000, .max = 400000 },
  229. .vco = { .min = 1400000, .max = 2800000 },
  230. .n = { .min = 1, .max = 6 },
  231. .m = { .min = 70, .max = 120 },
  232. .m1 = { .min = 8, .max = 18 },
  233. .m2 = { .min = 3, .max = 7 },
  234. .p = { .min = 7, .max = 98 },
  235. .p1 = { .min = 1, .max = 8 },
  236. .p2 = { .dot_limit = 112000,
  237. .p2_slow = 14, .p2_fast = 7 },
  238. };
  239. static const struct intel_limit intel_limits_g4x_sdvo = {
  240. .dot = { .min = 25000, .max = 270000 },
  241. .vco = { .min = 1750000, .max = 3500000},
  242. .n = { .min = 1, .max = 4 },
  243. .m = { .min = 104, .max = 138 },
  244. .m1 = { .min = 17, .max = 23 },
  245. .m2 = { .min = 5, .max = 11 },
  246. .p = { .min = 10, .max = 30 },
  247. .p1 = { .min = 1, .max = 3},
  248. .p2 = { .dot_limit = 270000,
  249. .p2_slow = 10,
  250. .p2_fast = 10
  251. },
  252. };
  253. static const struct intel_limit intel_limits_g4x_hdmi = {
  254. .dot = { .min = 22000, .max = 400000 },
  255. .vco = { .min = 1750000, .max = 3500000},
  256. .n = { .min = 1, .max = 4 },
  257. .m = { .min = 104, .max = 138 },
  258. .m1 = { .min = 16, .max = 23 },
  259. .m2 = { .min = 5, .max = 11 },
  260. .p = { .min = 5, .max = 80 },
  261. .p1 = { .min = 1, .max = 8},
  262. .p2 = { .dot_limit = 165000,
  263. .p2_slow = 10, .p2_fast = 5 },
  264. };
  265. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  266. .dot = { .min = 20000, .max = 115000 },
  267. .vco = { .min = 1750000, .max = 3500000 },
  268. .n = { .min = 1, .max = 3 },
  269. .m = { .min = 104, .max = 138 },
  270. .m1 = { .min = 17, .max = 23 },
  271. .m2 = { .min = 5, .max = 11 },
  272. .p = { .min = 28, .max = 112 },
  273. .p1 = { .min = 2, .max = 8 },
  274. .p2 = { .dot_limit = 0,
  275. .p2_slow = 14, .p2_fast = 14
  276. },
  277. };
  278. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  279. .dot = { .min = 80000, .max = 224000 },
  280. .vco = { .min = 1750000, .max = 3500000 },
  281. .n = { .min = 1, .max = 3 },
  282. .m = { .min = 104, .max = 138 },
  283. .m1 = { .min = 17, .max = 23 },
  284. .m2 = { .min = 5, .max = 11 },
  285. .p = { .min = 14, .max = 42 },
  286. .p1 = { .min = 2, .max = 6 },
  287. .p2 = { .dot_limit = 0,
  288. .p2_slow = 7, .p2_fast = 7
  289. },
  290. };
  291. static const struct intel_limit intel_limits_pineview_sdvo = {
  292. .dot = { .min = 20000, .max = 400000},
  293. .vco = { .min = 1700000, .max = 3500000 },
  294. /* Pineview's Ncounter is a ring counter */
  295. .n = { .min = 3, .max = 6 },
  296. .m = { .min = 2, .max = 256 },
  297. /* Pineview only has one combined m divider, which we treat as m2. */
  298. .m1 = { .min = 0, .max = 0 },
  299. .m2 = { .min = 0, .max = 254 },
  300. .p = { .min = 5, .max = 80 },
  301. .p1 = { .min = 1, .max = 8 },
  302. .p2 = { .dot_limit = 200000,
  303. .p2_slow = 10, .p2_fast = 5 },
  304. };
  305. static const struct intel_limit intel_limits_pineview_lvds = {
  306. .dot = { .min = 20000, .max = 400000 },
  307. .vco = { .min = 1700000, .max = 3500000 },
  308. .n = { .min = 3, .max = 6 },
  309. .m = { .min = 2, .max = 256 },
  310. .m1 = { .min = 0, .max = 0 },
  311. .m2 = { .min = 0, .max = 254 },
  312. .p = { .min = 7, .max = 112 },
  313. .p1 = { .min = 1, .max = 8 },
  314. .p2 = { .dot_limit = 112000,
  315. .p2_slow = 14, .p2_fast = 14 },
  316. };
  317. /* Ironlake / Sandybridge
  318. *
  319. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  320. * the range value for them is (actual_value - 2).
  321. */
  322. static const struct intel_limit intel_limits_ironlake_dac = {
  323. .dot = { .min = 25000, .max = 350000 },
  324. .vco = { .min = 1760000, .max = 3510000 },
  325. .n = { .min = 1, .max = 5 },
  326. .m = { .min = 79, .max = 127 },
  327. .m1 = { .min = 12, .max = 22 },
  328. .m2 = { .min = 5, .max = 9 },
  329. .p = { .min = 5, .max = 80 },
  330. .p1 = { .min = 1, .max = 8 },
  331. .p2 = { .dot_limit = 225000,
  332. .p2_slow = 10, .p2_fast = 5 },
  333. };
  334. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  335. .dot = { .min = 25000, .max = 350000 },
  336. .vco = { .min = 1760000, .max = 3510000 },
  337. .n = { .min = 1, .max = 3 },
  338. .m = { .min = 79, .max = 118 },
  339. .m1 = { .min = 12, .max = 22 },
  340. .m2 = { .min = 5, .max = 9 },
  341. .p = { .min = 28, .max = 112 },
  342. .p1 = { .min = 2, .max = 8 },
  343. .p2 = { .dot_limit = 225000,
  344. .p2_slow = 14, .p2_fast = 14 },
  345. };
  346. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  347. .dot = { .min = 25000, .max = 350000 },
  348. .vco = { .min = 1760000, .max = 3510000 },
  349. .n = { .min = 1, .max = 3 },
  350. .m = { .min = 79, .max = 127 },
  351. .m1 = { .min = 12, .max = 22 },
  352. .m2 = { .min = 5, .max = 9 },
  353. .p = { .min = 14, .max = 56 },
  354. .p1 = { .min = 2, .max = 8 },
  355. .p2 = { .dot_limit = 225000,
  356. .p2_slow = 7, .p2_fast = 7 },
  357. };
  358. /* LVDS 100mhz refclk limits. */
  359. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  360. .dot = { .min = 25000, .max = 350000 },
  361. .vco = { .min = 1760000, .max = 3510000 },
  362. .n = { .min = 1, .max = 2 },
  363. .m = { .min = 79, .max = 126 },
  364. .m1 = { .min = 12, .max = 22 },
  365. .m2 = { .min = 5, .max = 9 },
  366. .p = { .min = 28, .max = 112 },
  367. .p1 = { .min = 2, .max = 8 },
  368. .p2 = { .dot_limit = 225000,
  369. .p2_slow = 14, .p2_fast = 14 },
  370. };
  371. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  372. .dot = { .min = 25000, .max = 350000 },
  373. .vco = { .min = 1760000, .max = 3510000 },
  374. .n = { .min = 1, .max = 3 },
  375. .m = { .min = 79, .max = 126 },
  376. .m1 = { .min = 12, .max = 22 },
  377. .m2 = { .min = 5, .max = 9 },
  378. .p = { .min = 14, .max = 42 },
  379. .p1 = { .min = 2, .max = 6 },
  380. .p2 = { .dot_limit = 225000,
  381. .p2_slow = 7, .p2_fast = 7 },
  382. };
  383. static const struct intel_limit intel_limits_vlv = {
  384. /*
  385. * These are the data rate limits (measured in fast clocks)
  386. * since those are the strictest limits we have. The fast
  387. * clock and actual rate limits are more relaxed, so checking
  388. * them would make no difference.
  389. */
  390. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  391. .vco = { .min = 4000000, .max = 6000000 },
  392. .n = { .min = 1, .max = 7 },
  393. .m1 = { .min = 2, .max = 3 },
  394. .m2 = { .min = 11, .max = 156 },
  395. .p1 = { .min = 2, .max = 3 },
  396. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  397. };
  398. static const struct intel_limit intel_limits_chv = {
  399. /*
  400. * These are the data rate limits (measured in fast clocks)
  401. * since those are the strictest limits we have. The fast
  402. * clock and actual rate limits are more relaxed, so checking
  403. * them would make no difference.
  404. */
  405. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  406. .vco = { .min = 4800000, .max = 6480000 },
  407. .n = { .min = 1, .max = 1 },
  408. .m1 = { .min = 2, .max = 2 },
  409. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  410. .p1 = { .min = 2, .max = 4 },
  411. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  412. };
  413. static const struct intel_limit intel_limits_bxt = {
  414. /* FIXME: find real dot limits */
  415. .dot = { .min = 0, .max = INT_MAX },
  416. .vco = { .min = 4800000, .max = 6700000 },
  417. .n = { .min = 1, .max = 1 },
  418. .m1 = { .min = 2, .max = 2 },
  419. /* FIXME: find real m2 limits */
  420. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  421. .p1 = { .min = 2, .max = 4 },
  422. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  423. };
  424. static bool
  425. needs_modeset(struct drm_crtc_state *state)
  426. {
  427. return drm_atomic_crtc_needs_modeset(state);
  428. }
  429. /*
  430. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  431. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  432. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  433. * The helpers' return value is the rate of the clock that is fed to the
  434. * display engine's pipe which can be the above fast dot clock rate or a
  435. * divided-down version of it.
  436. */
  437. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  438. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  439. {
  440. clock->m = clock->m2 + 2;
  441. clock->p = clock->p1 * clock->p2;
  442. if (WARN_ON(clock->n == 0 || clock->p == 0))
  443. return 0;
  444. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  445. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  446. return clock->dot;
  447. }
  448. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  449. {
  450. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  451. }
  452. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  453. {
  454. clock->m = i9xx_dpll_compute_m(clock);
  455. clock->p = clock->p1 * clock->p2;
  456. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  457. return 0;
  458. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  459. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  460. return clock->dot;
  461. }
  462. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  463. {
  464. clock->m = clock->m1 * clock->m2;
  465. clock->p = clock->p1 * clock->p2;
  466. if (WARN_ON(clock->n == 0 || clock->p == 0))
  467. return 0;
  468. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  469. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  470. return clock->dot / 5;
  471. }
  472. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  473. {
  474. clock->m = clock->m1 * clock->m2;
  475. clock->p = clock->p1 * clock->p2;
  476. if (WARN_ON(clock->n == 0 || clock->p == 0))
  477. return 0;
  478. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  479. clock->n << 22);
  480. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  481. return clock->dot / 5;
  482. }
  483. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  484. /**
  485. * Returns whether the given set of divisors are valid for a given refclk with
  486. * the given connectors.
  487. */
  488. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  489. const struct intel_limit *limit,
  490. const struct dpll *clock)
  491. {
  492. if (clock->n < limit->n.min || limit->n.max < clock->n)
  493. INTELPllInvalid("n out of range\n");
  494. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  495. INTELPllInvalid("p1 out of range\n");
  496. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  497. INTELPllInvalid("m2 out of range\n");
  498. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  499. INTELPllInvalid("m1 out of range\n");
  500. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  501. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  502. if (clock->m1 <= clock->m2)
  503. INTELPllInvalid("m1 <= m2\n");
  504. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  505. !IS_GEN9_LP(dev_priv)) {
  506. if (clock->p < limit->p.min || limit->p.max < clock->p)
  507. INTELPllInvalid("p out of range\n");
  508. if (clock->m < limit->m.min || limit->m.max < clock->m)
  509. INTELPllInvalid("m out of range\n");
  510. }
  511. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  512. INTELPllInvalid("vco out of range\n");
  513. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  514. * connector, etc., rather than just a single range.
  515. */
  516. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  517. INTELPllInvalid("dot out of range\n");
  518. return true;
  519. }
  520. static int
  521. i9xx_select_p2_div(const struct intel_limit *limit,
  522. const struct intel_crtc_state *crtc_state,
  523. int target)
  524. {
  525. struct drm_device *dev = crtc_state->base.crtc->dev;
  526. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  527. /*
  528. * For LVDS just rely on its current settings for dual-channel.
  529. * We haven't figured out how to reliably set up different
  530. * single/dual channel state, if we even can.
  531. */
  532. if (intel_is_dual_link_lvds(dev))
  533. return limit->p2.p2_fast;
  534. else
  535. return limit->p2.p2_slow;
  536. } else {
  537. if (target < limit->p2.dot_limit)
  538. return limit->p2.p2_slow;
  539. else
  540. return limit->p2.p2_fast;
  541. }
  542. }
  543. /*
  544. * Returns a set of divisors for the desired target clock with the given
  545. * refclk, or FALSE. The returned values represent the clock equation:
  546. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  547. *
  548. * Target and reference clocks are specified in kHz.
  549. *
  550. * If match_clock is provided, then best_clock P divider must match the P
  551. * divider from @match_clock used for LVDS downclocking.
  552. */
  553. static bool
  554. i9xx_find_best_dpll(const struct intel_limit *limit,
  555. struct intel_crtc_state *crtc_state,
  556. int target, int refclk, struct dpll *match_clock,
  557. struct dpll *best_clock)
  558. {
  559. struct drm_device *dev = crtc_state->base.crtc->dev;
  560. struct dpll clock;
  561. int err = target;
  562. memset(best_clock, 0, sizeof(*best_clock));
  563. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  564. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  565. clock.m1++) {
  566. for (clock.m2 = limit->m2.min;
  567. clock.m2 <= limit->m2.max; clock.m2++) {
  568. if (clock.m2 >= clock.m1)
  569. break;
  570. for (clock.n = limit->n.min;
  571. clock.n <= limit->n.max; clock.n++) {
  572. for (clock.p1 = limit->p1.min;
  573. clock.p1 <= limit->p1.max; clock.p1++) {
  574. int this_err;
  575. i9xx_calc_dpll_params(refclk, &clock);
  576. if (!intel_PLL_is_valid(to_i915(dev),
  577. limit,
  578. &clock))
  579. continue;
  580. if (match_clock &&
  581. clock.p != match_clock->p)
  582. continue;
  583. this_err = abs(clock.dot - target);
  584. if (this_err < err) {
  585. *best_clock = clock;
  586. err = this_err;
  587. }
  588. }
  589. }
  590. }
  591. }
  592. return (err != target);
  593. }
  594. /*
  595. * Returns a set of divisors for the desired target clock with the given
  596. * refclk, or FALSE. The returned values represent the clock equation:
  597. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  598. *
  599. * Target and reference clocks are specified in kHz.
  600. *
  601. * If match_clock is provided, then best_clock P divider must match the P
  602. * divider from @match_clock used for LVDS downclocking.
  603. */
  604. static bool
  605. pnv_find_best_dpll(const struct intel_limit *limit,
  606. struct intel_crtc_state *crtc_state,
  607. int target, int refclk, struct dpll *match_clock,
  608. struct dpll *best_clock)
  609. {
  610. struct drm_device *dev = crtc_state->base.crtc->dev;
  611. struct dpll clock;
  612. int err = target;
  613. memset(best_clock, 0, sizeof(*best_clock));
  614. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  615. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  616. clock.m1++) {
  617. for (clock.m2 = limit->m2.min;
  618. clock.m2 <= limit->m2.max; clock.m2++) {
  619. for (clock.n = limit->n.min;
  620. clock.n <= limit->n.max; clock.n++) {
  621. for (clock.p1 = limit->p1.min;
  622. clock.p1 <= limit->p1.max; clock.p1++) {
  623. int this_err;
  624. pnv_calc_dpll_params(refclk, &clock);
  625. if (!intel_PLL_is_valid(to_i915(dev),
  626. limit,
  627. &clock))
  628. continue;
  629. if (match_clock &&
  630. clock.p != match_clock->p)
  631. continue;
  632. this_err = abs(clock.dot - target);
  633. if (this_err < err) {
  634. *best_clock = clock;
  635. err = this_err;
  636. }
  637. }
  638. }
  639. }
  640. }
  641. return (err != target);
  642. }
  643. /*
  644. * Returns a set of divisors for the desired target clock with the given
  645. * refclk, or FALSE. The returned values represent the clock equation:
  646. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  647. *
  648. * Target and reference clocks are specified in kHz.
  649. *
  650. * If match_clock is provided, then best_clock P divider must match the P
  651. * divider from @match_clock used for LVDS downclocking.
  652. */
  653. static bool
  654. g4x_find_best_dpll(const struct intel_limit *limit,
  655. struct intel_crtc_state *crtc_state,
  656. int target, int refclk, struct dpll *match_clock,
  657. struct dpll *best_clock)
  658. {
  659. struct drm_device *dev = crtc_state->base.crtc->dev;
  660. struct dpll clock;
  661. int max_n;
  662. bool found = false;
  663. /* approximately equals target * 0.00585 */
  664. int err_most = (target >> 8) + (target >> 9);
  665. memset(best_clock, 0, sizeof(*best_clock));
  666. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  667. max_n = limit->n.max;
  668. /* based on hardware requirement, prefer smaller n to precision */
  669. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  670. /* based on hardware requirement, prefere larger m1,m2 */
  671. for (clock.m1 = limit->m1.max;
  672. clock.m1 >= limit->m1.min; clock.m1--) {
  673. for (clock.m2 = limit->m2.max;
  674. clock.m2 >= limit->m2.min; clock.m2--) {
  675. for (clock.p1 = limit->p1.max;
  676. clock.p1 >= limit->p1.min; clock.p1--) {
  677. int this_err;
  678. i9xx_calc_dpll_params(refclk, &clock);
  679. if (!intel_PLL_is_valid(to_i915(dev),
  680. limit,
  681. &clock))
  682. continue;
  683. this_err = abs(clock.dot - target);
  684. if (this_err < err_most) {
  685. *best_clock = clock;
  686. err_most = this_err;
  687. max_n = clock.n;
  688. found = true;
  689. }
  690. }
  691. }
  692. }
  693. }
  694. return found;
  695. }
  696. /*
  697. * Check if the calculated PLL configuration is more optimal compared to the
  698. * best configuration and error found so far. Return the calculated error.
  699. */
  700. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  701. const struct dpll *calculated_clock,
  702. const struct dpll *best_clock,
  703. unsigned int best_error_ppm,
  704. unsigned int *error_ppm)
  705. {
  706. /*
  707. * For CHV ignore the error and consider only the P value.
  708. * Prefer a bigger P value based on HW requirements.
  709. */
  710. if (IS_CHERRYVIEW(to_i915(dev))) {
  711. *error_ppm = 0;
  712. return calculated_clock->p > best_clock->p;
  713. }
  714. if (WARN_ON_ONCE(!target_freq))
  715. return false;
  716. *error_ppm = div_u64(1000000ULL *
  717. abs(target_freq - calculated_clock->dot),
  718. target_freq);
  719. /*
  720. * Prefer a better P value over a better (smaller) error if the error
  721. * is small. Ensure this preference for future configurations too by
  722. * setting the error to 0.
  723. */
  724. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  725. *error_ppm = 0;
  726. return true;
  727. }
  728. return *error_ppm + 10 < best_error_ppm;
  729. }
  730. /*
  731. * Returns a set of divisors for the desired target clock with the given
  732. * refclk, or FALSE. The returned values represent the clock equation:
  733. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  734. */
  735. static bool
  736. vlv_find_best_dpll(const struct intel_limit *limit,
  737. struct intel_crtc_state *crtc_state,
  738. int target, int refclk, struct dpll *match_clock,
  739. struct dpll *best_clock)
  740. {
  741. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  742. struct drm_device *dev = crtc->base.dev;
  743. struct dpll clock;
  744. unsigned int bestppm = 1000000;
  745. /* min update 19.2 MHz */
  746. int max_n = min(limit->n.max, refclk / 19200);
  747. bool found = false;
  748. target *= 5; /* fast clock */
  749. memset(best_clock, 0, sizeof(*best_clock));
  750. /* based on hardware requirement, prefer smaller n to precision */
  751. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  752. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  753. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  754. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  755. clock.p = clock.p1 * clock.p2;
  756. /* based on hardware requirement, prefer bigger m1,m2 values */
  757. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  758. unsigned int ppm;
  759. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  760. refclk * clock.m1);
  761. vlv_calc_dpll_params(refclk, &clock);
  762. if (!intel_PLL_is_valid(to_i915(dev),
  763. limit,
  764. &clock))
  765. continue;
  766. if (!vlv_PLL_is_optimal(dev, target,
  767. &clock,
  768. best_clock,
  769. bestppm, &ppm))
  770. continue;
  771. *best_clock = clock;
  772. bestppm = ppm;
  773. found = true;
  774. }
  775. }
  776. }
  777. }
  778. return found;
  779. }
  780. /*
  781. * Returns a set of divisors for the desired target clock with the given
  782. * refclk, or FALSE. The returned values represent the clock equation:
  783. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  784. */
  785. static bool
  786. chv_find_best_dpll(const struct intel_limit *limit,
  787. struct intel_crtc_state *crtc_state,
  788. int target, int refclk, struct dpll *match_clock,
  789. struct dpll *best_clock)
  790. {
  791. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  792. struct drm_device *dev = crtc->base.dev;
  793. unsigned int best_error_ppm;
  794. struct dpll clock;
  795. uint64_t m2;
  796. int found = false;
  797. memset(best_clock, 0, sizeof(*best_clock));
  798. best_error_ppm = 1000000;
  799. /*
  800. * Based on hardware doc, the n always set to 1, and m1 always
  801. * set to 2. If requires to support 200Mhz refclk, we need to
  802. * revisit this because n may not 1 anymore.
  803. */
  804. clock.n = 1, clock.m1 = 2;
  805. target *= 5; /* fast clock */
  806. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  807. for (clock.p2 = limit->p2.p2_fast;
  808. clock.p2 >= limit->p2.p2_slow;
  809. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  810. unsigned int error_ppm;
  811. clock.p = clock.p1 * clock.p2;
  812. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  813. clock.n) << 22, refclk * clock.m1);
  814. if (m2 > INT_MAX/clock.m1)
  815. continue;
  816. clock.m2 = m2;
  817. chv_calc_dpll_params(refclk, &clock);
  818. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  819. continue;
  820. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  821. best_error_ppm, &error_ppm))
  822. continue;
  823. *best_clock = clock;
  824. best_error_ppm = error_ppm;
  825. found = true;
  826. }
  827. }
  828. return found;
  829. }
  830. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  831. struct dpll *best_clock)
  832. {
  833. int refclk = 100000;
  834. const struct intel_limit *limit = &intel_limits_bxt;
  835. return chv_find_best_dpll(limit, crtc_state,
  836. target_clock, refclk, NULL, best_clock);
  837. }
  838. bool intel_crtc_active(struct intel_crtc *crtc)
  839. {
  840. /* Be paranoid as we can arrive here with only partial
  841. * state retrieved from the hardware during setup.
  842. *
  843. * We can ditch the adjusted_mode.crtc_clock check as soon
  844. * as Haswell has gained clock readout/fastboot support.
  845. *
  846. * We can ditch the crtc->primary->fb check as soon as we can
  847. * properly reconstruct framebuffers.
  848. *
  849. * FIXME: The intel_crtc->active here should be switched to
  850. * crtc->state->active once we have proper CRTC states wired up
  851. * for atomic.
  852. */
  853. return crtc->active && crtc->base.primary->state->fb &&
  854. crtc->config->base.adjusted_mode.crtc_clock;
  855. }
  856. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  857. enum pipe pipe)
  858. {
  859. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  860. return crtc->config->cpu_transcoder;
  861. }
  862. static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  863. {
  864. i915_reg_t reg = PIPEDSL(pipe);
  865. u32 line1, line2;
  866. u32 line_mask;
  867. if (IS_GEN2(dev_priv))
  868. line_mask = DSL_LINEMASK_GEN2;
  869. else
  870. line_mask = DSL_LINEMASK_GEN3;
  871. line1 = I915_READ(reg) & line_mask;
  872. msleep(5);
  873. line2 = I915_READ(reg) & line_mask;
  874. return line1 == line2;
  875. }
  876. /*
  877. * intel_wait_for_pipe_off - wait for pipe to turn off
  878. * @crtc: crtc whose pipe to wait for
  879. *
  880. * After disabling a pipe, we can't wait for vblank in the usual way,
  881. * spinning on the vblank interrupt status bit, since we won't actually
  882. * see an interrupt when the pipe is disabled.
  883. *
  884. * On Gen4 and above:
  885. * wait for the pipe register state bit to turn off
  886. *
  887. * Otherwise:
  888. * wait for the display line value to settle (it usually
  889. * ends up stopping at the start of the next frame).
  890. *
  891. */
  892. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  893. {
  894. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  895. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  896. enum pipe pipe = crtc->pipe;
  897. if (INTEL_GEN(dev_priv) >= 4) {
  898. i915_reg_t reg = PIPECONF(cpu_transcoder);
  899. /* Wait for the Pipe State to go off */
  900. if (intel_wait_for_register(dev_priv,
  901. reg, I965_PIPECONF_ACTIVE, 0,
  902. 100))
  903. WARN(1, "pipe_off wait timed out\n");
  904. } else {
  905. /* Wait for the display line to settle */
  906. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  907. WARN(1, "pipe_off wait timed out\n");
  908. }
  909. }
  910. /* Only for pre-ILK configs */
  911. void assert_pll(struct drm_i915_private *dev_priv,
  912. enum pipe pipe, bool state)
  913. {
  914. u32 val;
  915. bool cur_state;
  916. val = I915_READ(DPLL(pipe));
  917. cur_state = !!(val & DPLL_VCO_ENABLE);
  918. I915_STATE_WARN(cur_state != state,
  919. "PLL state assertion failure (expected %s, current %s)\n",
  920. onoff(state), onoff(cur_state));
  921. }
  922. /* XXX: the dsi pll is shared between MIPI DSI ports */
  923. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  924. {
  925. u32 val;
  926. bool cur_state;
  927. mutex_lock(&dev_priv->sb_lock);
  928. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  929. mutex_unlock(&dev_priv->sb_lock);
  930. cur_state = val & DSI_PLL_VCO_EN;
  931. I915_STATE_WARN(cur_state != state,
  932. "DSI PLL state assertion failure (expected %s, current %s)\n",
  933. onoff(state), onoff(cur_state));
  934. }
  935. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  936. enum pipe pipe, bool state)
  937. {
  938. bool cur_state;
  939. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  940. pipe);
  941. if (HAS_DDI(dev_priv)) {
  942. /* DDI does not have a specific FDI_TX register */
  943. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  944. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  945. } else {
  946. u32 val = I915_READ(FDI_TX_CTL(pipe));
  947. cur_state = !!(val & FDI_TX_ENABLE);
  948. }
  949. I915_STATE_WARN(cur_state != state,
  950. "FDI TX state assertion failure (expected %s, current %s)\n",
  951. onoff(state), onoff(cur_state));
  952. }
  953. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  954. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  955. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  956. enum pipe pipe, bool state)
  957. {
  958. u32 val;
  959. bool cur_state;
  960. val = I915_READ(FDI_RX_CTL(pipe));
  961. cur_state = !!(val & FDI_RX_ENABLE);
  962. I915_STATE_WARN(cur_state != state,
  963. "FDI RX state assertion failure (expected %s, current %s)\n",
  964. onoff(state), onoff(cur_state));
  965. }
  966. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  967. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  968. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  969. enum pipe pipe)
  970. {
  971. u32 val;
  972. /* ILK FDI PLL is always enabled */
  973. if (IS_GEN5(dev_priv))
  974. return;
  975. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  976. if (HAS_DDI(dev_priv))
  977. return;
  978. val = I915_READ(FDI_TX_CTL(pipe));
  979. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  980. }
  981. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  982. enum pipe pipe, bool state)
  983. {
  984. u32 val;
  985. bool cur_state;
  986. val = I915_READ(FDI_RX_CTL(pipe));
  987. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  988. I915_STATE_WARN(cur_state != state,
  989. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  990. onoff(state), onoff(cur_state));
  991. }
  992. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  993. {
  994. i915_reg_t pp_reg;
  995. u32 val;
  996. enum pipe panel_pipe = PIPE_A;
  997. bool locked = true;
  998. if (WARN_ON(HAS_DDI(dev_priv)))
  999. return;
  1000. if (HAS_PCH_SPLIT(dev_priv)) {
  1001. u32 port_sel;
  1002. pp_reg = PP_CONTROL(0);
  1003. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1004. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1005. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1006. panel_pipe = PIPE_B;
  1007. /* XXX: else fix for eDP */
  1008. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1009. /* presumably write lock depends on pipe, not port select */
  1010. pp_reg = PP_CONTROL(pipe);
  1011. panel_pipe = pipe;
  1012. } else {
  1013. pp_reg = PP_CONTROL(0);
  1014. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1015. panel_pipe = PIPE_B;
  1016. }
  1017. val = I915_READ(pp_reg);
  1018. if (!(val & PANEL_POWER_ON) ||
  1019. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1020. locked = false;
  1021. I915_STATE_WARN(panel_pipe == pipe && locked,
  1022. "panel assertion failure, pipe %c regs locked\n",
  1023. pipe_name(pipe));
  1024. }
  1025. static void assert_cursor(struct drm_i915_private *dev_priv,
  1026. enum pipe pipe, bool state)
  1027. {
  1028. bool cur_state;
  1029. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1030. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1031. else
  1032. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1033. I915_STATE_WARN(cur_state != state,
  1034. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1035. pipe_name(pipe), onoff(state), onoff(cur_state));
  1036. }
  1037. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1038. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1039. void assert_pipe(struct drm_i915_private *dev_priv,
  1040. enum pipe pipe, bool state)
  1041. {
  1042. bool cur_state;
  1043. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1044. pipe);
  1045. enum intel_display_power_domain power_domain;
  1046. /* we keep both pipes enabled on 830 */
  1047. if (IS_I830(dev_priv))
  1048. state = true;
  1049. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1050. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1051. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1052. cur_state = !!(val & PIPECONF_ENABLE);
  1053. intel_display_power_put(dev_priv, power_domain);
  1054. } else {
  1055. cur_state = false;
  1056. }
  1057. I915_STATE_WARN(cur_state != state,
  1058. "pipe %c assertion failure (expected %s, current %s)\n",
  1059. pipe_name(pipe), onoff(state), onoff(cur_state));
  1060. }
  1061. static void assert_plane(struct drm_i915_private *dev_priv,
  1062. enum plane plane, bool state)
  1063. {
  1064. u32 val;
  1065. bool cur_state;
  1066. val = I915_READ(DSPCNTR(plane));
  1067. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1068. I915_STATE_WARN(cur_state != state,
  1069. "plane %c assertion failure (expected %s, current %s)\n",
  1070. plane_name(plane), onoff(state), onoff(cur_state));
  1071. }
  1072. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1073. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1074. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1075. enum pipe pipe)
  1076. {
  1077. int i;
  1078. /* Primary planes are fixed to pipes on gen4+ */
  1079. if (INTEL_GEN(dev_priv) >= 4) {
  1080. u32 val = I915_READ(DSPCNTR(pipe));
  1081. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1082. "plane %c assertion failure, should be disabled but not\n",
  1083. plane_name(pipe));
  1084. return;
  1085. }
  1086. /* Need to check both planes against the pipe */
  1087. for_each_pipe(dev_priv, i) {
  1088. u32 val = I915_READ(DSPCNTR(i));
  1089. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1090. DISPPLANE_SEL_PIPE_SHIFT;
  1091. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1092. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1093. plane_name(i), pipe_name(pipe));
  1094. }
  1095. }
  1096. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1097. enum pipe pipe)
  1098. {
  1099. int sprite;
  1100. if (INTEL_GEN(dev_priv) >= 9) {
  1101. for_each_sprite(dev_priv, pipe, sprite) {
  1102. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1103. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1104. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1105. sprite, pipe_name(pipe));
  1106. }
  1107. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1108. for_each_sprite(dev_priv, pipe, sprite) {
  1109. u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
  1110. I915_STATE_WARN(val & SP_ENABLE,
  1111. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1112. sprite_name(pipe, sprite), pipe_name(pipe));
  1113. }
  1114. } else if (INTEL_GEN(dev_priv) >= 7) {
  1115. u32 val = I915_READ(SPRCTL(pipe));
  1116. I915_STATE_WARN(val & SPRITE_ENABLE,
  1117. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1118. plane_name(pipe), pipe_name(pipe));
  1119. } else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
  1120. u32 val = I915_READ(DVSCNTR(pipe));
  1121. I915_STATE_WARN(val & DVS_ENABLE,
  1122. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1123. plane_name(pipe), pipe_name(pipe));
  1124. }
  1125. }
  1126. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1127. {
  1128. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1129. drm_crtc_vblank_put(crtc);
  1130. }
  1131. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1132. enum pipe pipe)
  1133. {
  1134. u32 val;
  1135. bool enabled;
  1136. val = I915_READ(PCH_TRANSCONF(pipe));
  1137. enabled = !!(val & TRANS_ENABLE);
  1138. I915_STATE_WARN(enabled,
  1139. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1140. pipe_name(pipe));
  1141. }
  1142. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1143. enum pipe pipe, u32 port_sel, u32 val)
  1144. {
  1145. if ((val & DP_PORT_EN) == 0)
  1146. return false;
  1147. if (HAS_PCH_CPT(dev_priv)) {
  1148. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1149. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1150. return false;
  1151. } else if (IS_CHERRYVIEW(dev_priv)) {
  1152. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1153. return false;
  1154. } else {
  1155. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1156. return false;
  1157. }
  1158. return true;
  1159. }
  1160. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1161. enum pipe pipe, u32 val)
  1162. {
  1163. if ((val & SDVO_ENABLE) == 0)
  1164. return false;
  1165. if (HAS_PCH_CPT(dev_priv)) {
  1166. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1167. return false;
  1168. } else if (IS_CHERRYVIEW(dev_priv)) {
  1169. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1170. return false;
  1171. } else {
  1172. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1173. return false;
  1174. }
  1175. return true;
  1176. }
  1177. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1178. enum pipe pipe, u32 val)
  1179. {
  1180. if ((val & LVDS_PORT_EN) == 0)
  1181. return false;
  1182. if (HAS_PCH_CPT(dev_priv)) {
  1183. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1184. return false;
  1185. } else {
  1186. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1187. return false;
  1188. }
  1189. return true;
  1190. }
  1191. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1192. enum pipe pipe, u32 val)
  1193. {
  1194. if ((val & ADPA_DAC_ENABLE) == 0)
  1195. return false;
  1196. if (HAS_PCH_CPT(dev_priv)) {
  1197. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1198. return false;
  1199. } else {
  1200. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1201. return false;
  1202. }
  1203. return true;
  1204. }
  1205. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1206. enum pipe pipe, i915_reg_t reg,
  1207. u32 port_sel)
  1208. {
  1209. u32 val = I915_READ(reg);
  1210. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1211. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1212. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1213. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1214. && (val & DP_PIPEB_SELECT),
  1215. "IBX PCH dp port still using transcoder B\n");
  1216. }
  1217. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1218. enum pipe pipe, i915_reg_t reg)
  1219. {
  1220. u32 val = I915_READ(reg);
  1221. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1222. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1223. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1224. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1225. && (val & SDVO_PIPE_B_SELECT),
  1226. "IBX PCH hdmi port still using transcoder B\n");
  1227. }
  1228. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1229. enum pipe pipe)
  1230. {
  1231. u32 val;
  1232. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1233. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1234. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1235. val = I915_READ(PCH_ADPA);
  1236. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1237. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1238. pipe_name(pipe));
  1239. val = I915_READ(PCH_LVDS);
  1240. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1241. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1242. pipe_name(pipe));
  1243. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1244. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1245. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1246. }
  1247. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1248. const struct intel_crtc_state *pipe_config)
  1249. {
  1250. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1251. enum pipe pipe = crtc->pipe;
  1252. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1253. POSTING_READ(DPLL(pipe));
  1254. udelay(150);
  1255. if (intel_wait_for_register(dev_priv,
  1256. DPLL(pipe),
  1257. DPLL_LOCK_VLV,
  1258. DPLL_LOCK_VLV,
  1259. 1))
  1260. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1261. }
  1262. static void vlv_enable_pll(struct intel_crtc *crtc,
  1263. const struct intel_crtc_state *pipe_config)
  1264. {
  1265. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1266. enum pipe pipe = crtc->pipe;
  1267. assert_pipe_disabled(dev_priv, pipe);
  1268. /* PLL is protected by panel, make sure we can write it */
  1269. assert_panel_unlocked(dev_priv, pipe);
  1270. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1271. _vlv_enable_pll(crtc, pipe_config);
  1272. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1273. POSTING_READ(DPLL_MD(pipe));
  1274. }
  1275. static void _chv_enable_pll(struct intel_crtc *crtc,
  1276. const struct intel_crtc_state *pipe_config)
  1277. {
  1278. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1279. enum pipe pipe = crtc->pipe;
  1280. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1281. u32 tmp;
  1282. mutex_lock(&dev_priv->sb_lock);
  1283. /* Enable back the 10bit clock to display controller */
  1284. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1285. tmp |= DPIO_DCLKP_EN;
  1286. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1287. mutex_unlock(&dev_priv->sb_lock);
  1288. /*
  1289. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1290. */
  1291. udelay(1);
  1292. /* Enable PLL */
  1293. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1294. /* Check PLL is locked */
  1295. if (intel_wait_for_register(dev_priv,
  1296. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1297. 1))
  1298. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1299. }
  1300. static void chv_enable_pll(struct intel_crtc *crtc,
  1301. const struct intel_crtc_state *pipe_config)
  1302. {
  1303. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1304. enum pipe pipe = crtc->pipe;
  1305. assert_pipe_disabled(dev_priv, pipe);
  1306. /* PLL is protected by panel, make sure we can write it */
  1307. assert_panel_unlocked(dev_priv, pipe);
  1308. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1309. _chv_enable_pll(crtc, pipe_config);
  1310. if (pipe != PIPE_A) {
  1311. /*
  1312. * WaPixelRepeatModeFixForC0:chv
  1313. *
  1314. * DPLLCMD is AWOL. Use chicken bits to propagate
  1315. * the value from DPLLBMD to either pipe B or C.
  1316. */
  1317. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1318. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1319. I915_WRITE(CBR4_VLV, 0);
  1320. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1321. /*
  1322. * DPLLB VGA mode also seems to cause problems.
  1323. * We should always have it disabled.
  1324. */
  1325. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1326. } else {
  1327. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1328. POSTING_READ(DPLL_MD(pipe));
  1329. }
  1330. }
  1331. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1332. {
  1333. struct intel_crtc *crtc;
  1334. int count = 0;
  1335. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1336. count += crtc->base.state->active &&
  1337. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1338. }
  1339. return count;
  1340. }
  1341. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1342. {
  1343. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1344. i915_reg_t reg = DPLL(crtc->pipe);
  1345. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1346. int i;
  1347. assert_pipe_disabled(dev_priv, crtc->pipe);
  1348. /* PLL is protected by panel, make sure we can write it */
  1349. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1350. assert_panel_unlocked(dev_priv, crtc->pipe);
  1351. /* Enable DVO 2x clock on both PLLs if necessary */
  1352. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1353. /*
  1354. * It appears to be important that we don't enable this
  1355. * for the current pipe before otherwise configuring the
  1356. * PLL. No idea how this should be handled if multiple
  1357. * DVO outputs are enabled simultaneosly.
  1358. */
  1359. dpll |= DPLL_DVO_2X_MODE;
  1360. I915_WRITE(DPLL(!crtc->pipe),
  1361. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1362. }
  1363. /*
  1364. * Apparently we need to have VGA mode enabled prior to changing
  1365. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1366. * dividers, even though the register value does change.
  1367. */
  1368. I915_WRITE(reg, 0);
  1369. I915_WRITE(reg, dpll);
  1370. /* Wait for the clocks to stabilize. */
  1371. POSTING_READ(reg);
  1372. udelay(150);
  1373. if (INTEL_GEN(dev_priv) >= 4) {
  1374. I915_WRITE(DPLL_MD(crtc->pipe),
  1375. crtc->config->dpll_hw_state.dpll_md);
  1376. } else {
  1377. /* The pixel multiplier can only be updated once the
  1378. * DPLL is enabled and the clocks are stable.
  1379. *
  1380. * So write it again.
  1381. */
  1382. I915_WRITE(reg, dpll);
  1383. }
  1384. /* We do this three times for luck */
  1385. for (i = 0; i < 3; i++) {
  1386. I915_WRITE(reg, dpll);
  1387. POSTING_READ(reg);
  1388. udelay(150); /* wait for warmup */
  1389. }
  1390. }
  1391. /**
  1392. * i9xx_disable_pll - disable a PLL
  1393. * @dev_priv: i915 private structure
  1394. * @pipe: pipe PLL to disable
  1395. *
  1396. * Disable the PLL for @pipe, making sure the pipe is off first.
  1397. *
  1398. * Note! This is for pre-ILK only.
  1399. */
  1400. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1401. {
  1402. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1403. enum pipe pipe = crtc->pipe;
  1404. /* Disable DVO 2x clock on both PLLs if necessary */
  1405. if (IS_I830(dev_priv) &&
  1406. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1407. !intel_num_dvo_pipes(dev_priv)) {
  1408. I915_WRITE(DPLL(PIPE_B),
  1409. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1410. I915_WRITE(DPLL(PIPE_A),
  1411. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1412. }
  1413. /* Don't disable pipe or pipe PLLs if needed */
  1414. if (IS_I830(dev_priv))
  1415. return;
  1416. /* Make sure the pipe isn't still relying on us */
  1417. assert_pipe_disabled(dev_priv, pipe);
  1418. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1419. POSTING_READ(DPLL(pipe));
  1420. }
  1421. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1422. {
  1423. u32 val;
  1424. /* Make sure the pipe isn't still relying on us */
  1425. assert_pipe_disabled(dev_priv, pipe);
  1426. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1427. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1428. if (pipe != PIPE_A)
  1429. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1430. I915_WRITE(DPLL(pipe), val);
  1431. POSTING_READ(DPLL(pipe));
  1432. }
  1433. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1434. {
  1435. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1436. u32 val;
  1437. /* Make sure the pipe isn't still relying on us */
  1438. assert_pipe_disabled(dev_priv, pipe);
  1439. val = DPLL_SSC_REF_CLK_CHV |
  1440. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1441. if (pipe != PIPE_A)
  1442. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1443. I915_WRITE(DPLL(pipe), val);
  1444. POSTING_READ(DPLL(pipe));
  1445. mutex_lock(&dev_priv->sb_lock);
  1446. /* Disable 10bit clock to display controller */
  1447. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1448. val &= ~DPIO_DCLKP_EN;
  1449. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1450. mutex_unlock(&dev_priv->sb_lock);
  1451. }
  1452. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1453. struct intel_digital_port *dport,
  1454. unsigned int expected_mask)
  1455. {
  1456. u32 port_mask;
  1457. i915_reg_t dpll_reg;
  1458. switch (dport->port) {
  1459. case PORT_B:
  1460. port_mask = DPLL_PORTB_READY_MASK;
  1461. dpll_reg = DPLL(0);
  1462. break;
  1463. case PORT_C:
  1464. port_mask = DPLL_PORTC_READY_MASK;
  1465. dpll_reg = DPLL(0);
  1466. expected_mask <<= 4;
  1467. break;
  1468. case PORT_D:
  1469. port_mask = DPLL_PORTD_READY_MASK;
  1470. dpll_reg = DPIO_PHY_STATUS;
  1471. break;
  1472. default:
  1473. BUG();
  1474. }
  1475. if (intel_wait_for_register(dev_priv,
  1476. dpll_reg, port_mask, expected_mask,
  1477. 1000))
  1478. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1479. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1480. }
  1481. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1482. enum pipe pipe)
  1483. {
  1484. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1485. pipe);
  1486. i915_reg_t reg;
  1487. uint32_t val, pipeconf_val;
  1488. /* Make sure PCH DPLL is enabled */
  1489. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1490. /* FDI must be feeding us bits for PCH ports */
  1491. assert_fdi_tx_enabled(dev_priv, pipe);
  1492. assert_fdi_rx_enabled(dev_priv, pipe);
  1493. if (HAS_PCH_CPT(dev_priv)) {
  1494. /* Workaround: Set the timing override bit before enabling the
  1495. * pch transcoder. */
  1496. reg = TRANS_CHICKEN2(pipe);
  1497. val = I915_READ(reg);
  1498. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1499. I915_WRITE(reg, val);
  1500. }
  1501. reg = PCH_TRANSCONF(pipe);
  1502. val = I915_READ(reg);
  1503. pipeconf_val = I915_READ(PIPECONF(pipe));
  1504. if (HAS_PCH_IBX(dev_priv)) {
  1505. /*
  1506. * Make the BPC in transcoder be consistent with
  1507. * that in pipeconf reg. For HDMI we must use 8bpc
  1508. * here for both 8bpc and 12bpc.
  1509. */
  1510. val &= ~PIPECONF_BPC_MASK;
  1511. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1512. val |= PIPECONF_8BPC;
  1513. else
  1514. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1515. }
  1516. val &= ~TRANS_INTERLACE_MASK;
  1517. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1518. if (HAS_PCH_IBX(dev_priv) &&
  1519. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1520. val |= TRANS_LEGACY_INTERLACED_ILK;
  1521. else
  1522. val |= TRANS_INTERLACED;
  1523. else
  1524. val |= TRANS_PROGRESSIVE;
  1525. I915_WRITE(reg, val | TRANS_ENABLE);
  1526. if (intel_wait_for_register(dev_priv,
  1527. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1528. 100))
  1529. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1530. }
  1531. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1532. enum transcoder cpu_transcoder)
  1533. {
  1534. u32 val, pipeconf_val;
  1535. /* FDI must be feeding us bits for PCH ports */
  1536. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1537. assert_fdi_rx_enabled(dev_priv, PIPE_A);
  1538. /* Workaround: set timing override bit. */
  1539. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1540. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1541. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1542. val = TRANS_ENABLE;
  1543. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1544. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1545. PIPECONF_INTERLACED_ILK)
  1546. val |= TRANS_INTERLACED;
  1547. else
  1548. val |= TRANS_PROGRESSIVE;
  1549. I915_WRITE(LPT_TRANSCONF, val);
  1550. if (intel_wait_for_register(dev_priv,
  1551. LPT_TRANSCONF,
  1552. TRANS_STATE_ENABLE,
  1553. TRANS_STATE_ENABLE,
  1554. 100))
  1555. DRM_ERROR("Failed to enable PCH transcoder\n");
  1556. }
  1557. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1558. enum pipe pipe)
  1559. {
  1560. i915_reg_t reg;
  1561. uint32_t val;
  1562. /* FDI relies on the transcoder */
  1563. assert_fdi_tx_disabled(dev_priv, pipe);
  1564. assert_fdi_rx_disabled(dev_priv, pipe);
  1565. /* Ports must be off as well */
  1566. assert_pch_ports_disabled(dev_priv, pipe);
  1567. reg = PCH_TRANSCONF(pipe);
  1568. val = I915_READ(reg);
  1569. val &= ~TRANS_ENABLE;
  1570. I915_WRITE(reg, val);
  1571. /* wait for PCH transcoder off, transcoder state */
  1572. if (intel_wait_for_register(dev_priv,
  1573. reg, TRANS_STATE_ENABLE, 0,
  1574. 50))
  1575. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1576. if (HAS_PCH_CPT(dev_priv)) {
  1577. /* Workaround: Clear the timing override chicken bit again. */
  1578. reg = TRANS_CHICKEN2(pipe);
  1579. val = I915_READ(reg);
  1580. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1581. I915_WRITE(reg, val);
  1582. }
  1583. }
  1584. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1585. {
  1586. u32 val;
  1587. val = I915_READ(LPT_TRANSCONF);
  1588. val &= ~TRANS_ENABLE;
  1589. I915_WRITE(LPT_TRANSCONF, val);
  1590. /* wait for PCH transcoder off, transcoder state */
  1591. if (intel_wait_for_register(dev_priv,
  1592. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1593. 50))
  1594. DRM_ERROR("Failed to disable PCH transcoder\n");
  1595. /* Workaround: clear timing override bit. */
  1596. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1597. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1598. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1599. }
  1600. enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1601. {
  1602. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1603. WARN_ON(!crtc->config->has_pch_encoder);
  1604. if (HAS_PCH_LPT(dev_priv))
  1605. return PIPE_A;
  1606. else
  1607. return crtc->pipe;
  1608. }
  1609. /**
  1610. * intel_enable_pipe - enable a pipe, asserting requirements
  1611. * @crtc: crtc responsible for the pipe
  1612. *
  1613. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1614. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1615. */
  1616. static void intel_enable_pipe(struct intel_crtc *crtc)
  1617. {
  1618. struct drm_device *dev = crtc->base.dev;
  1619. struct drm_i915_private *dev_priv = to_i915(dev);
  1620. enum pipe pipe = crtc->pipe;
  1621. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1622. i915_reg_t reg;
  1623. u32 val;
  1624. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1625. assert_planes_disabled(dev_priv, pipe);
  1626. assert_cursor_disabled(dev_priv, pipe);
  1627. assert_sprites_disabled(dev_priv, pipe);
  1628. /*
  1629. * A pipe without a PLL won't actually be able to drive bits from
  1630. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1631. * need the check.
  1632. */
  1633. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1634. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1635. assert_dsi_pll_enabled(dev_priv);
  1636. else
  1637. assert_pll_enabled(dev_priv, pipe);
  1638. } else {
  1639. if (crtc->config->has_pch_encoder) {
  1640. /* if driving the PCH, we need FDI enabled */
  1641. assert_fdi_rx_pll_enabled(dev_priv,
  1642. intel_crtc_pch_transcoder(crtc));
  1643. assert_fdi_tx_pll_enabled(dev_priv,
  1644. (enum pipe) cpu_transcoder);
  1645. }
  1646. /* FIXME: assert CPU port conditions for SNB+ */
  1647. }
  1648. reg = PIPECONF(cpu_transcoder);
  1649. val = I915_READ(reg);
  1650. if (val & PIPECONF_ENABLE) {
  1651. /* we keep both pipes enabled on 830 */
  1652. WARN_ON(!IS_I830(dev_priv));
  1653. return;
  1654. }
  1655. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1656. POSTING_READ(reg);
  1657. /*
  1658. * Until the pipe starts DSL will read as 0, which would cause
  1659. * an apparent vblank timestamp jump, which messes up also the
  1660. * frame count when it's derived from the timestamps. So let's
  1661. * wait for the pipe to start properly before we call
  1662. * drm_crtc_vblank_on()
  1663. */
  1664. if (dev->max_vblank_count == 0 &&
  1665. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1666. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1667. }
  1668. /**
  1669. * intel_disable_pipe - disable a pipe, asserting requirements
  1670. * @crtc: crtc whose pipes is to be disabled
  1671. *
  1672. * Disable the pipe of @crtc, making sure that various hardware
  1673. * specific requirements are met, if applicable, e.g. plane
  1674. * disabled, panel fitter off, etc.
  1675. *
  1676. * Will wait until the pipe has shut down before returning.
  1677. */
  1678. static void intel_disable_pipe(struct intel_crtc *crtc)
  1679. {
  1680. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1681. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1682. enum pipe pipe = crtc->pipe;
  1683. i915_reg_t reg;
  1684. u32 val;
  1685. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1686. /*
  1687. * Make sure planes won't keep trying to pump pixels to us,
  1688. * or we might hang the display.
  1689. */
  1690. assert_planes_disabled(dev_priv, pipe);
  1691. assert_cursor_disabled(dev_priv, pipe);
  1692. assert_sprites_disabled(dev_priv, pipe);
  1693. reg = PIPECONF(cpu_transcoder);
  1694. val = I915_READ(reg);
  1695. if ((val & PIPECONF_ENABLE) == 0)
  1696. return;
  1697. /*
  1698. * Double wide has implications for planes
  1699. * so best keep it disabled when not needed.
  1700. */
  1701. if (crtc->config->double_wide)
  1702. val &= ~PIPECONF_DOUBLE_WIDE;
  1703. /* Don't disable pipe or pipe PLLs if needed */
  1704. if (!IS_I830(dev_priv))
  1705. val &= ~PIPECONF_ENABLE;
  1706. I915_WRITE(reg, val);
  1707. if ((val & PIPECONF_ENABLE) == 0)
  1708. intel_wait_for_pipe_off(crtc);
  1709. }
  1710. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1711. {
  1712. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1713. }
  1714. static unsigned int
  1715. intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
  1716. {
  1717. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1718. unsigned int cpp = fb->format->cpp[plane];
  1719. switch (fb->modifier) {
  1720. case DRM_FORMAT_MOD_LINEAR:
  1721. return cpp;
  1722. case I915_FORMAT_MOD_X_TILED:
  1723. if (IS_GEN2(dev_priv))
  1724. return 128;
  1725. else
  1726. return 512;
  1727. case I915_FORMAT_MOD_Y_TILED:
  1728. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1729. return 128;
  1730. else
  1731. return 512;
  1732. case I915_FORMAT_MOD_Yf_TILED:
  1733. switch (cpp) {
  1734. case 1:
  1735. return 64;
  1736. case 2:
  1737. case 4:
  1738. return 128;
  1739. case 8:
  1740. case 16:
  1741. return 256;
  1742. default:
  1743. MISSING_CASE(cpp);
  1744. return cpp;
  1745. }
  1746. break;
  1747. default:
  1748. MISSING_CASE(fb->modifier);
  1749. return cpp;
  1750. }
  1751. }
  1752. static unsigned int
  1753. intel_tile_height(const struct drm_framebuffer *fb, int plane)
  1754. {
  1755. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  1756. return 1;
  1757. else
  1758. return intel_tile_size(to_i915(fb->dev)) /
  1759. intel_tile_width_bytes(fb, plane);
  1760. }
  1761. /* Return the tile dimensions in pixel units */
  1762. static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
  1763. unsigned int *tile_width,
  1764. unsigned int *tile_height)
  1765. {
  1766. unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
  1767. unsigned int cpp = fb->format->cpp[plane];
  1768. *tile_width = tile_width_bytes / cpp;
  1769. *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
  1770. }
  1771. unsigned int
  1772. intel_fb_align_height(const struct drm_framebuffer *fb,
  1773. int plane, unsigned int height)
  1774. {
  1775. unsigned int tile_height = intel_tile_height(fb, plane);
  1776. return ALIGN(height, tile_height);
  1777. }
  1778. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1779. {
  1780. unsigned int size = 0;
  1781. int i;
  1782. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1783. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1784. return size;
  1785. }
  1786. static void
  1787. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1788. const struct drm_framebuffer *fb,
  1789. unsigned int rotation)
  1790. {
  1791. view->type = I915_GGTT_VIEW_NORMAL;
  1792. if (drm_rotation_90_or_270(rotation)) {
  1793. view->type = I915_GGTT_VIEW_ROTATED;
  1794. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1795. }
  1796. }
  1797. static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
  1798. {
  1799. if (IS_I830(dev_priv))
  1800. return 16 * 1024;
  1801. else if (IS_I85X(dev_priv))
  1802. return 256;
  1803. else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1804. return 32;
  1805. else
  1806. return 4 * 1024;
  1807. }
  1808. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1809. {
  1810. if (INTEL_INFO(dev_priv)->gen >= 9)
  1811. return 256 * 1024;
  1812. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1813. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1814. return 128 * 1024;
  1815. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1816. return 4 * 1024;
  1817. else
  1818. return 0;
  1819. }
  1820. static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
  1821. int plane)
  1822. {
  1823. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1824. /* AUX_DIST needs only 4K alignment */
  1825. if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
  1826. return 4096;
  1827. switch (fb->modifier) {
  1828. case DRM_FORMAT_MOD_LINEAR:
  1829. return intel_linear_alignment(dev_priv);
  1830. case I915_FORMAT_MOD_X_TILED:
  1831. if (INTEL_GEN(dev_priv) >= 9)
  1832. return 256 * 1024;
  1833. return 0;
  1834. case I915_FORMAT_MOD_Y_TILED:
  1835. case I915_FORMAT_MOD_Yf_TILED:
  1836. return 1 * 1024 * 1024;
  1837. default:
  1838. MISSING_CASE(fb->modifier);
  1839. return 0;
  1840. }
  1841. }
  1842. struct i915_vma *
  1843. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1844. {
  1845. struct drm_device *dev = fb->dev;
  1846. struct drm_i915_private *dev_priv = to_i915(dev);
  1847. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1848. struct i915_ggtt_view view;
  1849. struct i915_vma *vma;
  1850. u32 alignment;
  1851. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1852. alignment = intel_surf_alignment(fb, 0);
  1853. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1854. /* Note that the w/a also requires 64 PTE of padding following the
  1855. * bo. We currently fill all unused PTE with the shadow page and so
  1856. * we should always have valid PTE following the scanout preventing
  1857. * the VT-d warning.
  1858. */
  1859. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1860. alignment = 256 * 1024;
  1861. /*
  1862. * Global gtt pte registers are special registers which actually forward
  1863. * writes to a chunk of system memory. Which means that there is no risk
  1864. * that the register values disappear as soon as we call
  1865. * intel_runtime_pm_put(), so it is correct to wrap only the
  1866. * pin/unpin/fence and not more.
  1867. */
  1868. intel_runtime_pm_get(dev_priv);
  1869. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1870. if (IS_ERR(vma))
  1871. goto err;
  1872. if (i915_vma_is_map_and_fenceable(vma)) {
  1873. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1874. * fence, whereas 965+ only requires a fence if using
  1875. * framebuffer compression. For simplicity, we always, when
  1876. * possible, install a fence as the cost is not that onerous.
  1877. *
  1878. * If we fail to fence the tiled scanout, then either the
  1879. * modeset will reject the change (which is highly unlikely as
  1880. * the affected systems, all but one, do not have unmappable
  1881. * space) or we will not be able to enable full powersaving
  1882. * techniques (also likely not to apply due to various limits
  1883. * FBC and the like impose on the size of the buffer, which
  1884. * presumably we violated anyway with this unmappable buffer).
  1885. * Anyway, it is presumably better to stumble onwards with
  1886. * something and try to run the system in a "less than optimal"
  1887. * mode that matches the user configuration.
  1888. */
  1889. if (i915_vma_get_fence(vma) == 0)
  1890. i915_vma_pin_fence(vma);
  1891. }
  1892. i915_vma_get(vma);
  1893. err:
  1894. intel_runtime_pm_put(dev_priv);
  1895. return vma;
  1896. }
  1897. void intel_unpin_fb_vma(struct i915_vma *vma)
  1898. {
  1899. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1900. i915_vma_unpin_fence(vma);
  1901. i915_gem_object_unpin_from_display_plane(vma);
  1902. i915_vma_put(vma);
  1903. }
  1904. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1905. unsigned int rotation)
  1906. {
  1907. if (drm_rotation_90_or_270(rotation))
  1908. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1909. else
  1910. return fb->pitches[plane];
  1911. }
  1912. /*
  1913. * Convert the x/y offsets into a linear offset.
  1914. * Only valid with 0/180 degree rotation, which is fine since linear
  1915. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1916. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1917. */
  1918. u32 intel_fb_xy_to_linear(int x, int y,
  1919. const struct intel_plane_state *state,
  1920. int plane)
  1921. {
  1922. const struct drm_framebuffer *fb = state->base.fb;
  1923. unsigned int cpp = fb->format->cpp[plane];
  1924. unsigned int pitch = fb->pitches[plane];
  1925. return y * pitch + x * cpp;
  1926. }
  1927. /*
  1928. * Add the x/y offsets derived from fb->offsets[] to the user
  1929. * specified plane src x/y offsets. The resulting x/y offsets
  1930. * specify the start of scanout from the beginning of the gtt mapping.
  1931. */
  1932. void intel_add_fb_offsets(int *x, int *y,
  1933. const struct intel_plane_state *state,
  1934. int plane)
  1935. {
  1936. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1937. unsigned int rotation = state->base.rotation;
  1938. if (drm_rotation_90_or_270(rotation)) {
  1939. *x += intel_fb->rotated[plane].x;
  1940. *y += intel_fb->rotated[plane].y;
  1941. } else {
  1942. *x += intel_fb->normal[plane].x;
  1943. *y += intel_fb->normal[plane].y;
  1944. }
  1945. }
  1946. /*
  1947. * Input tile dimensions and pitch must already be
  1948. * rotated to match x and y, and in pixel units.
  1949. */
  1950. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1951. unsigned int tile_width,
  1952. unsigned int tile_height,
  1953. unsigned int tile_size,
  1954. unsigned int pitch_tiles,
  1955. u32 old_offset,
  1956. u32 new_offset)
  1957. {
  1958. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1959. unsigned int tiles;
  1960. WARN_ON(old_offset & (tile_size - 1));
  1961. WARN_ON(new_offset & (tile_size - 1));
  1962. WARN_ON(new_offset > old_offset);
  1963. tiles = (old_offset - new_offset) / tile_size;
  1964. *y += tiles / pitch_tiles * tile_height;
  1965. *x += tiles % pitch_tiles * tile_width;
  1966. /* minimize x in case it got needlessly big */
  1967. *y += *x / pitch_pixels * tile_height;
  1968. *x %= pitch_pixels;
  1969. return new_offset;
  1970. }
  1971. /*
  1972. * Adjust the tile offset by moving the difference into
  1973. * the x/y offsets.
  1974. */
  1975. static u32 intel_adjust_tile_offset(int *x, int *y,
  1976. const struct intel_plane_state *state, int plane,
  1977. u32 old_offset, u32 new_offset)
  1978. {
  1979. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  1980. const struct drm_framebuffer *fb = state->base.fb;
  1981. unsigned int cpp = fb->format->cpp[plane];
  1982. unsigned int rotation = state->base.rotation;
  1983. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  1984. WARN_ON(new_offset > old_offset);
  1985. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  1986. unsigned int tile_size, tile_width, tile_height;
  1987. unsigned int pitch_tiles;
  1988. tile_size = intel_tile_size(dev_priv);
  1989. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  1990. if (drm_rotation_90_or_270(rotation)) {
  1991. pitch_tiles = pitch / tile_height;
  1992. swap(tile_width, tile_height);
  1993. } else {
  1994. pitch_tiles = pitch / (tile_width * cpp);
  1995. }
  1996. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  1997. tile_size, pitch_tiles,
  1998. old_offset, new_offset);
  1999. } else {
  2000. old_offset += *y * pitch + *x * cpp;
  2001. *y = (old_offset - new_offset) / pitch;
  2002. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2003. }
  2004. return new_offset;
  2005. }
  2006. /*
  2007. * Computes the linear offset to the base tile and adjusts
  2008. * x, y. bytes per pixel is assumed to be a power-of-two.
  2009. *
  2010. * In the 90/270 rotated case, x and y are assumed
  2011. * to be already rotated to match the rotated GTT view, and
  2012. * pitch is the tile_height aligned framebuffer height.
  2013. *
  2014. * This function is used when computing the derived information
  2015. * under intel_framebuffer, so using any of that information
  2016. * here is not allowed. Anything under drm_framebuffer can be
  2017. * used. This is why the user has to pass in the pitch since it
  2018. * is specified in the rotated orientation.
  2019. */
  2020. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2021. int *x, int *y,
  2022. const struct drm_framebuffer *fb, int plane,
  2023. unsigned int pitch,
  2024. unsigned int rotation,
  2025. u32 alignment)
  2026. {
  2027. uint64_t fb_modifier = fb->modifier;
  2028. unsigned int cpp = fb->format->cpp[plane];
  2029. u32 offset, offset_aligned;
  2030. if (alignment)
  2031. alignment--;
  2032. if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
  2033. unsigned int tile_size, tile_width, tile_height;
  2034. unsigned int tile_rows, tiles, pitch_tiles;
  2035. tile_size = intel_tile_size(dev_priv);
  2036. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2037. if (drm_rotation_90_or_270(rotation)) {
  2038. pitch_tiles = pitch / tile_height;
  2039. swap(tile_width, tile_height);
  2040. } else {
  2041. pitch_tiles = pitch / (tile_width * cpp);
  2042. }
  2043. tile_rows = *y / tile_height;
  2044. *y %= tile_height;
  2045. tiles = *x / tile_width;
  2046. *x %= tile_width;
  2047. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2048. offset_aligned = offset & ~alignment;
  2049. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2050. tile_size, pitch_tiles,
  2051. offset, offset_aligned);
  2052. } else {
  2053. offset = *y * pitch + *x * cpp;
  2054. offset_aligned = offset & ~alignment;
  2055. *y = (offset & alignment) / pitch;
  2056. *x = ((offset & alignment) - *y * pitch) / cpp;
  2057. }
  2058. return offset_aligned;
  2059. }
  2060. u32 intel_compute_tile_offset(int *x, int *y,
  2061. const struct intel_plane_state *state,
  2062. int plane)
  2063. {
  2064. struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
  2065. struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
  2066. const struct drm_framebuffer *fb = state->base.fb;
  2067. unsigned int rotation = state->base.rotation;
  2068. int pitch = intel_fb_pitch(fb, plane, rotation);
  2069. u32 alignment;
  2070. if (intel_plane->id == PLANE_CURSOR)
  2071. alignment = intel_cursor_alignment(dev_priv);
  2072. else
  2073. alignment = intel_surf_alignment(fb, plane);
  2074. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2075. rotation, alignment);
  2076. }
  2077. /* Convert the fb->offset[] linear offset into x/y offsets */
  2078. static void intel_fb_offset_to_xy(int *x, int *y,
  2079. const struct drm_framebuffer *fb, int plane)
  2080. {
  2081. unsigned int cpp = fb->format->cpp[plane];
  2082. unsigned int pitch = fb->pitches[plane];
  2083. u32 linear_offset = fb->offsets[plane];
  2084. *y = linear_offset / pitch;
  2085. *x = linear_offset % pitch / cpp;
  2086. }
  2087. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2088. {
  2089. switch (fb_modifier) {
  2090. case I915_FORMAT_MOD_X_TILED:
  2091. return I915_TILING_X;
  2092. case I915_FORMAT_MOD_Y_TILED:
  2093. return I915_TILING_Y;
  2094. default:
  2095. return I915_TILING_NONE;
  2096. }
  2097. }
  2098. static int
  2099. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2100. struct drm_framebuffer *fb)
  2101. {
  2102. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2103. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2104. u32 gtt_offset_rotated = 0;
  2105. unsigned int max_size = 0;
  2106. int i, num_planes = fb->format->num_planes;
  2107. unsigned int tile_size = intel_tile_size(dev_priv);
  2108. for (i = 0; i < num_planes; i++) {
  2109. unsigned int width, height;
  2110. unsigned int cpp, size;
  2111. u32 offset;
  2112. int x, y;
  2113. cpp = fb->format->cpp[i];
  2114. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2115. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2116. intel_fb_offset_to_xy(&x, &y, fb, i);
  2117. /*
  2118. * The fence (if used) is aligned to the start of the object
  2119. * so having the framebuffer wrap around across the edge of the
  2120. * fenced region doesn't really work. We have no API to configure
  2121. * the fence start offset within the object (nor could we probably
  2122. * on gen2/3). So it's just easier if we just require that the
  2123. * fb layout agrees with the fence layout. We already check that the
  2124. * fb stride matches the fence stride elsewhere.
  2125. */
  2126. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2127. (x + width) * cpp > fb->pitches[i]) {
  2128. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2129. i, fb->offsets[i]);
  2130. return -EINVAL;
  2131. }
  2132. /*
  2133. * First pixel of the framebuffer from
  2134. * the start of the normal gtt mapping.
  2135. */
  2136. intel_fb->normal[i].x = x;
  2137. intel_fb->normal[i].y = y;
  2138. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2139. fb, i, fb->pitches[i],
  2140. DRM_MODE_ROTATE_0, tile_size);
  2141. offset /= tile_size;
  2142. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2143. unsigned int tile_width, tile_height;
  2144. unsigned int pitch_tiles;
  2145. struct drm_rect r;
  2146. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2147. rot_info->plane[i].offset = offset;
  2148. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2149. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2150. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2151. intel_fb->rotated[i].pitch =
  2152. rot_info->plane[i].height * tile_height;
  2153. /* how many tiles does this plane need */
  2154. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2155. /*
  2156. * If the plane isn't horizontally tile aligned,
  2157. * we need one more tile.
  2158. */
  2159. if (x != 0)
  2160. size++;
  2161. /* rotate the x/y offsets to match the GTT view */
  2162. r.x1 = x;
  2163. r.y1 = y;
  2164. r.x2 = x + width;
  2165. r.y2 = y + height;
  2166. drm_rect_rotate(&r,
  2167. rot_info->plane[i].width * tile_width,
  2168. rot_info->plane[i].height * tile_height,
  2169. DRM_MODE_ROTATE_270);
  2170. x = r.x1;
  2171. y = r.y1;
  2172. /* rotate the tile dimensions to match the GTT view */
  2173. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2174. swap(tile_width, tile_height);
  2175. /*
  2176. * We only keep the x/y offsets, so push all of the
  2177. * gtt offset into the x/y offsets.
  2178. */
  2179. _intel_adjust_tile_offset(&x, &y,
  2180. tile_width, tile_height,
  2181. tile_size, pitch_tiles,
  2182. gtt_offset_rotated * tile_size, 0);
  2183. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2184. /*
  2185. * First pixel of the framebuffer from
  2186. * the start of the rotated gtt mapping.
  2187. */
  2188. intel_fb->rotated[i].x = x;
  2189. intel_fb->rotated[i].y = y;
  2190. } else {
  2191. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2192. x * cpp, tile_size);
  2193. }
  2194. /* how many tiles in total needed in the bo */
  2195. max_size = max(max_size, offset + size);
  2196. }
  2197. if (max_size * tile_size > intel_fb->obj->base.size) {
  2198. DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2199. max_size * tile_size, intel_fb->obj->base.size);
  2200. return -EINVAL;
  2201. }
  2202. return 0;
  2203. }
  2204. static int i9xx_format_to_fourcc(int format)
  2205. {
  2206. switch (format) {
  2207. case DISPPLANE_8BPP:
  2208. return DRM_FORMAT_C8;
  2209. case DISPPLANE_BGRX555:
  2210. return DRM_FORMAT_XRGB1555;
  2211. case DISPPLANE_BGRX565:
  2212. return DRM_FORMAT_RGB565;
  2213. default:
  2214. case DISPPLANE_BGRX888:
  2215. return DRM_FORMAT_XRGB8888;
  2216. case DISPPLANE_RGBX888:
  2217. return DRM_FORMAT_XBGR8888;
  2218. case DISPPLANE_BGRX101010:
  2219. return DRM_FORMAT_XRGB2101010;
  2220. case DISPPLANE_RGBX101010:
  2221. return DRM_FORMAT_XBGR2101010;
  2222. }
  2223. }
  2224. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2225. {
  2226. switch (format) {
  2227. case PLANE_CTL_FORMAT_RGB_565:
  2228. return DRM_FORMAT_RGB565;
  2229. default:
  2230. case PLANE_CTL_FORMAT_XRGB_8888:
  2231. if (rgb_order) {
  2232. if (alpha)
  2233. return DRM_FORMAT_ABGR8888;
  2234. else
  2235. return DRM_FORMAT_XBGR8888;
  2236. } else {
  2237. if (alpha)
  2238. return DRM_FORMAT_ARGB8888;
  2239. else
  2240. return DRM_FORMAT_XRGB8888;
  2241. }
  2242. case PLANE_CTL_FORMAT_XRGB_2101010:
  2243. if (rgb_order)
  2244. return DRM_FORMAT_XBGR2101010;
  2245. else
  2246. return DRM_FORMAT_XRGB2101010;
  2247. }
  2248. }
  2249. static bool
  2250. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2251. struct intel_initial_plane_config *plane_config)
  2252. {
  2253. struct drm_device *dev = crtc->base.dev;
  2254. struct drm_i915_private *dev_priv = to_i915(dev);
  2255. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2256. struct drm_i915_gem_object *obj = NULL;
  2257. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2258. struct drm_framebuffer *fb = &plane_config->fb->base;
  2259. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2260. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2261. PAGE_SIZE);
  2262. size_aligned -= base_aligned;
  2263. if (plane_config->size == 0)
  2264. return false;
  2265. /* If the FB is too big, just don't use it since fbdev is not very
  2266. * important and we should probably use that space with FBC or other
  2267. * features. */
  2268. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2269. return false;
  2270. mutex_lock(&dev->struct_mutex);
  2271. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2272. base_aligned,
  2273. base_aligned,
  2274. size_aligned);
  2275. mutex_unlock(&dev->struct_mutex);
  2276. if (!obj)
  2277. return false;
  2278. if (plane_config->tiling == I915_TILING_X)
  2279. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2280. mode_cmd.pixel_format = fb->format->format;
  2281. mode_cmd.width = fb->width;
  2282. mode_cmd.height = fb->height;
  2283. mode_cmd.pitches[0] = fb->pitches[0];
  2284. mode_cmd.modifier[0] = fb->modifier;
  2285. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2286. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2287. DRM_DEBUG_KMS("intel fb init failed\n");
  2288. goto out_unref_obj;
  2289. }
  2290. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2291. return true;
  2292. out_unref_obj:
  2293. i915_gem_object_put(obj);
  2294. return false;
  2295. }
  2296. static void
  2297. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2298. struct intel_plane_state *plane_state,
  2299. bool visible)
  2300. {
  2301. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2302. plane_state->base.visible = visible;
  2303. /* FIXME pre-g4x don't work like this */
  2304. if (visible) {
  2305. crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
  2306. crtc_state->active_planes |= BIT(plane->id);
  2307. } else {
  2308. crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
  2309. crtc_state->active_planes &= ~BIT(plane->id);
  2310. }
  2311. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2312. crtc_state->base.crtc->name,
  2313. crtc_state->active_planes);
  2314. }
  2315. static void
  2316. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2317. struct intel_initial_plane_config *plane_config)
  2318. {
  2319. struct drm_device *dev = intel_crtc->base.dev;
  2320. struct drm_i915_private *dev_priv = to_i915(dev);
  2321. struct drm_crtc *c;
  2322. struct drm_i915_gem_object *obj;
  2323. struct drm_plane *primary = intel_crtc->base.primary;
  2324. struct drm_plane_state *plane_state = primary->state;
  2325. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2326. struct intel_plane *intel_plane = to_intel_plane(primary);
  2327. struct intel_plane_state *intel_state =
  2328. to_intel_plane_state(plane_state);
  2329. struct drm_framebuffer *fb;
  2330. if (!plane_config->fb)
  2331. return;
  2332. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2333. fb = &plane_config->fb->base;
  2334. goto valid_fb;
  2335. }
  2336. kfree(plane_config->fb);
  2337. /*
  2338. * Failed to alloc the obj, check to see if we should share
  2339. * an fb with another CRTC instead
  2340. */
  2341. for_each_crtc(dev, c) {
  2342. struct intel_plane_state *state;
  2343. if (c == &intel_crtc->base)
  2344. continue;
  2345. if (!to_intel_crtc(c)->active)
  2346. continue;
  2347. state = to_intel_plane_state(c->primary->state);
  2348. if (!state->vma)
  2349. continue;
  2350. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2351. fb = c->primary->fb;
  2352. drm_framebuffer_reference(fb);
  2353. goto valid_fb;
  2354. }
  2355. }
  2356. /*
  2357. * We've failed to reconstruct the BIOS FB. Current display state
  2358. * indicates that the primary plane is visible, but has a NULL FB,
  2359. * which will lead to problems later if we don't fix it up. The
  2360. * simplest solution is to just disable the primary plane now and
  2361. * pretend the BIOS never had it enabled.
  2362. */
  2363. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2364. to_intel_plane_state(plane_state),
  2365. false);
  2366. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2367. trace_intel_disable_plane(primary, intel_crtc);
  2368. intel_plane->disable_plane(intel_plane, intel_crtc);
  2369. return;
  2370. valid_fb:
  2371. mutex_lock(&dev->struct_mutex);
  2372. intel_state->vma =
  2373. intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  2374. mutex_unlock(&dev->struct_mutex);
  2375. if (IS_ERR(intel_state->vma)) {
  2376. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2377. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2378. intel_state->vma = NULL;
  2379. drm_framebuffer_unreference(fb);
  2380. return;
  2381. }
  2382. plane_state->src_x = 0;
  2383. plane_state->src_y = 0;
  2384. plane_state->src_w = fb->width << 16;
  2385. plane_state->src_h = fb->height << 16;
  2386. plane_state->crtc_x = 0;
  2387. plane_state->crtc_y = 0;
  2388. plane_state->crtc_w = fb->width;
  2389. plane_state->crtc_h = fb->height;
  2390. intel_state->base.src = drm_plane_state_src(plane_state);
  2391. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2392. obj = intel_fb_obj(fb);
  2393. if (i915_gem_object_is_tiled(obj))
  2394. dev_priv->preserve_bios_swizzle = true;
  2395. drm_framebuffer_reference(fb);
  2396. primary->fb = primary->state->fb = fb;
  2397. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2398. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2399. to_intel_plane_state(plane_state),
  2400. true);
  2401. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2402. &obj->frontbuffer_bits);
  2403. }
  2404. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2405. unsigned int rotation)
  2406. {
  2407. int cpp = fb->format->cpp[plane];
  2408. switch (fb->modifier) {
  2409. case DRM_FORMAT_MOD_LINEAR:
  2410. case I915_FORMAT_MOD_X_TILED:
  2411. switch (cpp) {
  2412. case 8:
  2413. return 4096;
  2414. case 4:
  2415. case 2:
  2416. case 1:
  2417. return 8192;
  2418. default:
  2419. MISSING_CASE(cpp);
  2420. break;
  2421. }
  2422. break;
  2423. case I915_FORMAT_MOD_Y_TILED:
  2424. case I915_FORMAT_MOD_Yf_TILED:
  2425. switch (cpp) {
  2426. case 8:
  2427. return 2048;
  2428. case 4:
  2429. return 4096;
  2430. case 2:
  2431. case 1:
  2432. return 8192;
  2433. default:
  2434. MISSING_CASE(cpp);
  2435. break;
  2436. }
  2437. break;
  2438. default:
  2439. MISSING_CASE(fb->modifier);
  2440. }
  2441. return 2048;
  2442. }
  2443. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2444. {
  2445. const struct drm_framebuffer *fb = plane_state->base.fb;
  2446. unsigned int rotation = plane_state->base.rotation;
  2447. int x = plane_state->base.src.x1 >> 16;
  2448. int y = plane_state->base.src.y1 >> 16;
  2449. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2450. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2451. int max_width = skl_max_plane_width(fb, 0, rotation);
  2452. int max_height = 4096;
  2453. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2454. if (w > max_width || h > max_height) {
  2455. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2456. w, h, max_width, max_height);
  2457. return -EINVAL;
  2458. }
  2459. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2460. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2461. alignment = intel_surf_alignment(fb, 0);
  2462. /*
  2463. * AUX surface offset is specified as the distance from the
  2464. * main surface offset, and it must be non-negative. Make
  2465. * sure that is what we will get.
  2466. */
  2467. if (offset > aux_offset)
  2468. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2469. offset, aux_offset & ~(alignment - 1));
  2470. /*
  2471. * When using an X-tiled surface, the plane blows up
  2472. * if the x offset + width exceed the stride.
  2473. *
  2474. * TODO: linear and Y-tiled seem fine, Yf untested,
  2475. */
  2476. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2477. int cpp = fb->format->cpp[0];
  2478. while ((x + w) * cpp > fb->pitches[0]) {
  2479. if (offset == 0) {
  2480. DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
  2481. return -EINVAL;
  2482. }
  2483. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2484. offset, offset - alignment);
  2485. }
  2486. }
  2487. plane_state->main.offset = offset;
  2488. plane_state->main.x = x;
  2489. plane_state->main.y = y;
  2490. return 0;
  2491. }
  2492. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2493. {
  2494. const struct drm_framebuffer *fb = plane_state->base.fb;
  2495. unsigned int rotation = plane_state->base.rotation;
  2496. int max_width = skl_max_plane_width(fb, 1, rotation);
  2497. int max_height = 4096;
  2498. int x = plane_state->base.src.x1 >> 17;
  2499. int y = plane_state->base.src.y1 >> 17;
  2500. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2501. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2502. u32 offset;
  2503. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2504. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2505. /* FIXME not quite sure how/if these apply to the chroma plane */
  2506. if (w > max_width || h > max_height) {
  2507. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2508. w, h, max_width, max_height);
  2509. return -EINVAL;
  2510. }
  2511. plane_state->aux.offset = offset;
  2512. plane_state->aux.x = x;
  2513. plane_state->aux.y = y;
  2514. return 0;
  2515. }
  2516. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2517. {
  2518. const struct drm_framebuffer *fb = plane_state->base.fb;
  2519. unsigned int rotation = plane_state->base.rotation;
  2520. int ret;
  2521. if (!plane_state->base.visible)
  2522. return 0;
  2523. /* Rotate src coordinates to match rotated GTT view */
  2524. if (drm_rotation_90_or_270(rotation))
  2525. drm_rect_rotate(&plane_state->base.src,
  2526. fb->width << 16, fb->height << 16,
  2527. DRM_MODE_ROTATE_270);
  2528. /*
  2529. * Handle the AUX surface first since
  2530. * the main surface setup depends on it.
  2531. */
  2532. if (fb->format->format == DRM_FORMAT_NV12) {
  2533. ret = skl_check_nv12_aux_surface(plane_state);
  2534. if (ret)
  2535. return ret;
  2536. } else {
  2537. plane_state->aux.offset = ~0xfff;
  2538. plane_state->aux.x = 0;
  2539. plane_state->aux.y = 0;
  2540. }
  2541. ret = skl_check_main_surface(plane_state);
  2542. if (ret)
  2543. return ret;
  2544. return 0;
  2545. }
  2546. static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2547. const struct intel_plane_state *plane_state)
  2548. {
  2549. struct drm_i915_private *dev_priv =
  2550. to_i915(plane_state->base.plane->dev);
  2551. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2552. const struct drm_framebuffer *fb = plane_state->base.fb;
  2553. unsigned int rotation = plane_state->base.rotation;
  2554. u32 dspcntr;
  2555. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2556. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2557. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2558. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2559. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2560. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2561. if (INTEL_GEN(dev_priv) < 4)
  2562. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2563. switch (fb->format->format) {
  2564. case DRM_FORMAT_C8:
  2565. dspcntr |= DISPPLANE_8BPP;
  2566. break;
  2567. case DRM_FORMAT_XRGB1555:
  2568. dspcntr |= DISPPLANE_BGRX555;
  2569. break;
  2570. case DRM_FORMAT_RGB565:
  2571. dspcntr |= DISPPLANE_BGRX565;
  2572. break;
  2573. case DRM_FORMAT_XRGB8888:
  2574. dspcntr |= DISPPLANE_BGRX888;
  2575. break;
  2576. case DRM_FORMAT_XBGR8888:
  2577. dspcntr |= DISPPLANE_RGBX888;
  2578. break;
  2579. case DRM_FORMAT_XRGB2101010:
  2580. dspcntr |= DISPPLANE_BGRX101010;
  2581. break;
  2582. case DRM_FORMAT_XBGR2101010:
  2583. dspcntr |= DISPPLANE_RGBX101010;
  2584. break;
  2585. default:
  2586. MISSING_CASE(fb->format->format);
  2587. return 0;
  2588. }
  2589. if (INTEL_GEN(dev_priv) >= 4 &&
  2590. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2591. dspcntr |= DISPPLANE_TILED;
  2592. if (rotation & DRM_MODE_ROTATE_180)
  2593. dspcntr |= DISPPLANE_ROTATE_180;
  2594. if (rotation & DRM_MODE_REFLECT_X)
  2595. dspcntr |= DISPPLANE_MIRROR;
  2596. return dspcntr;
  2597. }
  2598. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2599. {
  2600. struct drm_i915_private *dev_priv =
  2601. to_i915(plane_state->base.plane->dev);
  2602. int src_x = plane_state->base.src.x1 >> 16;
  2603. int src_y = plane_state->base.src.y1 >> 16;
  2604. u32 offset;
  2605. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2606. if (INTEL_GEN(dev_priv) >= 4)
  2607. offset = intel_compute_tile_offset(&src_x, &src_y,
  2608. plane_state, 0);
  2609. else
  2610. offset = 0;
  2611. /* HSW/BDW do this automagically in hardware */
  2612. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2613. unsigned int rotation = plane_state->base.rotation;
  2614. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2615. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2616. if (rotation & DRM_MODE_ROTATE_180) {
  2617. src_x += src_w - 1;
  2618. src_y += src_h - 1;
  2619. } else if (rotation & DRM_MODE_REFLECT_X) {
  2620. src_x += src_w - 1;
  2621. }
  2622. }
  2623. plane_state->main.offset = offset;
  2624. plane_state->main.x = src_x;
  2625. plane_state->main.y = src_y;
  2626. return 0;
  2627. }
  2628. static void i9xx_update_primary_plane(struct intel_plane *primary,
  2629. const struct intel_crtc_state *crtc_state,
  2630. const struct intel_plane_state *plane_state)
  2631. {
  2632. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2633. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2634. const struct drm_framebuffer *fb = plane_state->base.fb;
  2635. enum plane plane = primary->plane;
  2636. u32 linear_offset;
  2637. u32 dspcntr = plane_state->ctl;
  2638. i915_reg_t reg = DSPCNTR(plane);
  2639. int x = plane_state->main.x;
  2640. int y = plane_state->main.y;
  2641. unsigned long irqflags;
  2642. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2643. if (INTEL_GEN(dev_priv) >= 4)
  2644. crtc->dspaddr_offset = plane_state->main.offset;
  2645. else
  2646. crtc->dspaddr_offset = linear_offset;
  2647. crtc->adjusted_x = x;
  2648. crtc->adjusted_y = y;
  2649. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2650. if (INTEL_GEN(dev_priv) < 4) {
  2651. /* pipesrc and dspsize control the size that is scaled from,
  2652. * which should always be the user's requested size.
  2653. */
  2654. I915_WRITE_FW(DSPSIZE(plane),
  2655. ((crtc_state->pipe_src_h - 1) << 16) |
  2656. (crtc_state->pipe_src_w - 1));
  2657. I915_WRITE_FW(DSPPOS(plane), 0);
  2658. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2659. I915_WRITE_FW(PRIMSIZE(plane),
  2660. ((crtc_state->pipe_src_h - 1) << 16) |
  2661. (crtc_state->pipe_src_w - 1));
  2662. I915_WRITE_FW(PRIMPOS(plane), 0);
  2663. I915_WRITE_FW(PRIMCNSTALPHA(plane), 0);
  2664. }
  2665. I915_WRITE_FW(reg, dspcntr);
  2666. I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
  2667. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2668. I915_WRITE_FW(DSPSURF(plane),
  2669. intel_plane_ggtt_offset(plane_state) +
  2670. crtc->dspaddr_offset);
  2671. I915_WRITE_FW(DSPOFFSET(plane), (y << 16) | x);
  2672. } else if (INTEL_GEN(dev_priv) >= 4) {
  2673. I915_WRITE_FW(DSPSURF(plane),
  2674. intel_plane_ggtt_offset(plane_state) +
  2675. crtc->dspaddr_offset);
  2676. I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
  2677. I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
  2678. } else {
  2679. I915_WRITE_FW(DSPADDR(plane),
  2680. intel_plane_ggtt_offset(plane_state) +
  2681. crtc->dspaddr_offset);
  2682. }
  2683. POSTING_READ_FW(reg);
  2684. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2685. }
  2686. static void i9xx_disable_primary_plane(struct intel_plane *primary,
  2687. struct intel_crtc *crtc)
  2688. {
  2689. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2690. enum plane plane = primary->plane;
  2691. unsigned long irqflags;
  2692. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2693. I915_WRITE_FW(DSPCNTR(plane), 0);
  2694. if (INTEL_INFO(dev_priv)->gen >= 4)
  2695. I915_WRITE_FW(DSPSURF(plane), 0);
  2696. else
  2697. I915_WRITE_FW(DSPADDR(plane), 0);
  2698. POSTING_READ_FW(DSPCNTR(plane));
  2699. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2700. }
  2701. static u32
  2702. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2703. {
  2704. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2705. return 64;
  2706. else
  2707. return intel_tile_width_bytes(fb, plane);
  2708. }
  2709. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2710. {
  2711. struct drm_device *dev = intel_crtc->base.dev;
  2712. struct drm_i915_private *dev_priv = to_i915(dev);
  2713. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2714. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2715. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2716. }
  2717. /*
  2718. * This function detaches (aka. unbinds) unused scalers in hardware
  2719. */
  2720. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2721. {
  2722. struct intel_crtc_scaler_state *scaler_state;
  2723. int i;
  2724. scaler_state = &intel_crtc->config->scaler_state;
  2725. /* loop through and disable scalers that aren't in use */
  2726. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2727. if (!scaler_state->scalers[i].in_use)
  2728. skl_detach_scaler(intel_crtc, i);
  2729. }
  2730. }
  2731. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2732. unsigned int rotation)
  2733. {
  2734. u32 stride;
  2735. if (plane >= fb->format->num_planes)
  2736. return 0;
  2737. stride = intel_fb_pitch(fb, plane, rotation);
  2738. /*
  2739. * The stride is either expressed as a multiple of 64 bytes chunks for
  2740. * linear buffers or in number of tiles for tiled buffers.
  2741. */
  2742. if (drm_rotation_90_or_270(rotation))
  2743. stride /= intel_tile_height(fb, plane);
  2744. else
  2745. stride /= intel_fb_stride_alignment(fb, plane);
  2746. return stride;
  2747. }
  2748. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  2749. {
  2750. switch (pixel_format) {
  2751. case DRM_FORMAT_C8:
  2752. return PLANE_CTL_FORMAT_INDEXED;
  2753. case DRM_FORMAT_RGB565:
  2754. return PLANE_CTL_FORMAT_RGB_565;
  2755. case DRM_FORMAT_XBGR8888:
  2756. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2757. case DRM_FORMAT_XRGB8888:
  2758. return PLANE_CTL_FORMAT_XRGB_8888;
  2759. /*
  2760. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2761. * to be already pre-multiplied. We need to add a knob (or a different
  2762. * DRM_FORMAT) for user-space to configure that.
  2763. */
  2764. case DRM_FORMAT_ABGR8888:
  2765. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2766. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2767. case DRM_FORMAT_ARGB8888:
  2768. return PLANE_CTL_FORMAT_XRGB_8888 |
  2769. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2770. case DRM_FORMAT_XRGB2101010:
  2771. return PLANE_CTL_FORMAT_XRGB_2101010;
  2772. case DRM_FORMAT_XBGR2101010:
  2773. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2774. case DRM_FORMAT_YUYV:
  2775. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2776. case DRM_FORMAT_YVYU:
  2777. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2778. case DRM_FORMAT_UYVY:
  2779. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2780. case DRM_FORMAT_VYUY:
  2781. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2782. default:
  2783. MISSING_CASE(pixel_format);
  2784. }
  2785. return 0;
  2786. }
  2787. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2788. {
  2789. switch (fb_modifier) {
  2790. case DRM_FORMAT_MOD_LINEAR:
  2791. break;
  2792. case I915_FORMAT_MOD_X_TILED:
  2793. return PLANE_CTL_TILED_X;
  2794. case I915_FORMAT_MOD_Y_TILED:
  2795. return PLANE_CTL_TILED_Y;
  2796. case I915_FORMAT_MOD_Yf_TILED:
  2797. return PLANE_CTL_TILED_YF;
  2798. default:
  2799. MISSING_CASE(fb_modifier);
  2800. }
  2801. return 0;
  2802. }
  2803. static u32 skl_plane_ctl_rotation(unsigned int rotation)
  2804. {
  2805. switch (rotation) {
  2806. case DRM_MODE_ROTATE_0:
  2807. break;
  2808. /*
  2809. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2810. * while i915 HW rotation is clockwise, thats why this swapping.
  2811. */
  2812. case DRM_MODE_ROTATE_90:
  2813. return PLANE_CTL_ROTATE_270;
  2814. case DRM_MODE_ROTATE_180:
  2815. return PLANE_CTL_ROTATE_180;
  2816. case DRM_MODE_ROTATE_270:
  2817. return PLANE_CTL_ROTATE_90;
  2818. default:
  2819. MISSING_CASE(rotation);
  2820. }
  2821. return 0;
  2822. }
  2823. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  2824. const struct intel_plane_state *plane_state)
  2825. {
  2826. struct drm_i915_private *dev_priv =
  2827. to_i915(plane_state->base.plane->dev);
  2828. const struct drm_framebuffer *fb = plane_state->base.fb;
  2829. unsigned int rotation = plane_state->base.rotation;
  2830. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  2831. u32 plane_ctl;
  2832. plane_ctl = PLANE_CTL_ENABLE;
  2833. if (!IS_GEMINILAKE(dev_priv) && !IS_CANNONLAKE(dev_priv)) {
  2834. plane_ctl |=
  2835. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2836. PLANE_CTL_PIPE_CSC_ENABLE |
  2837. PLANE_CTL_PLANE_GAMMA_DISABLE;
  2838. }
  2839. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  2840. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  2841. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2842. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  2843. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  2844. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  2845. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  2846. return plane_ctl;
  2847. }
  2848. static void skylake_update_primary_plane(struct intel_plane *plane,
  2849. const struct intel_crtc_state *crtc_state,
  2850. const struct intel_plane_state *plane_state)
  2851. {
  2852. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2853. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2854. const struct drm_framebuffer *fb = plane_state->base.fb;
  2855. enum plane_id plane_id = plane->id;
  2856. enum pipe pipe = plane->pipe;
  2857. u32 plane_ctl = plane_state->ctl;
  2858. unsigned int rotation = plane_state->base.rotation;
  2859. u32 stride = skl_plane_stride(fb, 0, rotation);
  2860. u32 surf_addr = plane_state->main.offset;
  2861. int scaler_id = plane_state->scaler_id;
  2862. int src_x = plane_state->main.x;
  2863. int src_y = plane_state->main.y;
  2864. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2865. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2866. int dst_x = plane_state->base.dst.x1;
  2867. int dst_y = plane_state->base.dst.y1;
  2868. int dst_w = drm_rect_width(&plane_state->base.dst);
  2869. int dst_h = drm_rect_height(&plane_state->base.dst);
  2870. unsigned long irqflags;
  2871. /* Sizes are 0 based */
  2872. src_w--;
  2873. src_h--;
  2874. dst_w--;
  2875. dst_h--;
  2876. crtc->dspaddr_offset = surf_addr;
  2877. crtc->adjusted_x = src_x;
  2878. crtc->adjusted_y = src_y;
  2879. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2880. if (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) {
  2881. I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
  2882. PLANE_COLOR_PIPE_GAMMA_ENABLE |
  2883. PLANE_COLOR_PIPE_CSC_ENABLE |
  2884. PLANE_COLOR_PLANE_GAMMA_DISABLE);
  2885. }
  2886. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
  2887. I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
  2888. I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
  2889. I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  2890. if (scaler_id >= 0) {
  2891. uint32_t ps_ctrl = 0;
  2892. WARN_ON(!dst_w || !dst_h);
  2893. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
  2894. crtc_state->scaler_state.scalers[scaler_id].mode;
  2895. I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2896. I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2897. I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2898. I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2899. I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
  2900. } else {
  2901. I915_WRITE_FW(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
  2902. }
  2903. I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
  2904. intel_plane_ggtt_offset(plane_state) + surf_addr);
  2905. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  2906. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2907. }
  2908. static void skylake_disable_primary_plane(struct intel_plane *primary,
  2909. struct intel_crtc *crtc)
  2910. {
  2911. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2912. enum plane_id plane_id = primary->id;
  2913. enum pipe pipe = primary->pipe;
  2914. unsigned long irqflags;
  2915. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2916. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
  2917. I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
  2918. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  2919. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2920. }
  2921. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2922. {
  2923. struct intel_crtc *crtc;
  2924. for_each_intel_crtc(&dev_priv->drm, crtc)
  2925. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2926. }
  2927. static int
  2928. __intel_display_resume(struct drm_device *dev,
  2929. struct drm_atomic_state *state,
  2930. struct drm_modeset_acquire_ctx *ctx)
  2931. {
  2932. struct drm_crtc_state *crtc_state;
  2933. struct drm_crtc *crtc;
  2934. int i, ret;
  2935. intel_modeset_setup_hw_state(dev, ctx);
  2936. i915_redisable_vga(to_i915(dev));
  2937. if (!state)
  2938. return 0;
  2939. /*
  2940. * We've duplicated the state, pointers to the old state are invalid.
  2941. *
  2942. * Don't attempt to use the old state until we commit the duplicated state.
  2943. */
  2944. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  2945. /*
  2946. * Force recalculation even if we restore
  2947. * current state. With fast modeset this may not result
  2948. * in a modeset when the state is compatible.
  2949. */
  2950. crtc_state->mode_changed = true;
  2951. }
  2952. /* ignore any reset values/BIOS leftovers in the WM registers */
  2953. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  2954. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  2955. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  2956. WARN_ON(ret == -EDEADLK);
  2957. return ret;
  2958. }
  2959. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  2960. {
  2961. return intel_has_gpu_reset(dev_priv) &&
  2962. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  2963. }
  2964. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  2965. {
  2966. struct drm_device *dev = &dev_priv->drm;
  2967. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  2968. struct drm_atomic_state *state;
  2969. int ret;
  2970. /* reset doesn't touch the display */
  2971. if (!i915.force_reset_modeset_test &&
  2972. !gpu_reset_clobbers_display(dev_priv))
  2973. return;
  2974. /*
  2975. * Need mode_config.mutex so that we don't
  2976. * trample ongoing ->detect() and whatnot.
  2977. */
  2978. mutex_lock(&dev->mode_config.mutex);
  2979. drm_modeset_acquire_init(ctx, 0);
  2980. while (1) {
  2981. ret = drm_modeset_lock_all_ctx(dev, ctx);
  2982. if (ret != -EDEADLK)
  2983. break;
  2984. drm_modeset_backoff(ctx);
  2985. }
  2986. /*
  2987. * Disabling the crtcs gracefully seems nicer. Also the
  2988. * g33 docs say we should at least disable all the planes.
  2989. */
  2990. state = drm_atomic_helper_duplicate_state(dev, ctx);
  2991. if (IS_ERR(state)) {
  2992. ret = PTR_ERR(state);
  2993. DRM_ERROR("Duplicating state failed with %i\n", ret);
  2994. return;
  2995. }
  2996. ret = drm_atomic_helper_disable_all(dev, ctx);
  2997. if (ret) {
  2998. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  2999. drm_atomic_state_put(state);
  3000. return;
  3001. }
  3002. dev_priv->modeset_restore_state = state;
  3003. state->acquire_ctx = ctx;
  3004. }
  3005. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3006. {
  3007. struct drm_device *dev = &dev_priv->drm;
  3008. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3009. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3010. int ret;
  3011. /* reset doesn't touch the display */
  3012. if (!i915.force_reset_modeset_test &&
  3013. !gpu_reset_clobbers_display(dev_priv))
  3014. return;
  3015. if (!state)
  3016. goto unlock;
  3017. /*
  3018. * Flips in the rings will be nuked by the reset,
  3019. * so complete all pending flips so that user space
  3020. * will get its events and not get stuck.
  3021. */
  3022. intel_complete_page_flips(dev_priv);
  3023. dev_priv->modeset_restore_state = NULL;
  3024. /* reset doesn't touch the display */
  3025. if (!gpu_reset_clobbers_display(dev_priv)) {
  3026. /* for testing only restore the display */
  3027. ret = __intel_display_resume(dev, state, ctx);
  3028. if (ret)
  3029. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3030. } else {
  3031. /*
  3032. * The display has been reset as well,
  3033. * so need a full re-initialization.
  3034. */
  3035. intel_runtime_pm_disable_interrupts(dev_priv);
  3036. intel_runtime_pm_enable_interrupts(dev_priv);
  3037. intel_pps_unlock_regs_wa(dev_priv);
  3038. intel_modeset_init_hw(dev);
  3039. spin_lock_irq(&dev_priv->irq_lock);
  3040. if (dev_priv->display.hpd_irq_setup)
  3041. dev_priv->display.hpd_irq_setup(dev_priv);
  3042. spin_unlock_irq(&dev_priv->irq_lock);
  3043. ret = __intel_display_resume(dev, state, ctx);
  3044. if (ret)
  3045. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3046. intel_hpd_init(dev_priv);
  3047. }
  3048. drm_atomic_state_put(state);
  3049. unlock:
  3050. drm_modeset_drop_locks(ctx);
  3051. drm_modeset_acquire_fini(ctx);
  3052. mutex_unlock(&dev->mode_config.mutex);
  3053. }
  3054. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3055. {
  3056. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3057. if (i915_reset_backoff(error))
  3058. return true;
  3059. if (crtc->reset_count != i915_reset_count(error))
  3060. return true;
  3061. return false;
  3062. }
  3063. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3064. {
  3065. struct drm_device *dev = crtc->dev;
  3066. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3067. bool pending;
  3068. if (abort_flip_on_reset(intel_crtc))
  3069. return false;
  3070. spin_lock_irq(&dev->event_lock);
  3071. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3072. spin_unlock_irq(&dev->event_lock);
  3073. return pending;
  3074. }
  3075. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3076. struct intel_crtc_state *old_crtc_state)
  3077. {
  3078. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3079. struct intel_crtc_state *pipe_config =
  3080. to_intel_crtc_state(crtc->base.state);
  3081. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3082. crtc->base.mode = crtc->base.state->mode;
  3083. /*
  3084. * Update pipe size and adjust fitter if needed: the reason for this is
  3085. * that in compute_mode_changes we check the native mode (not the pfit
  3086. * mode) to see if we can flip rather than do a full mode set. In the
  3087. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3088. * pfit state, we'll end up with a big fb scanned out into the wrong
  3089. * sized surface.
  3090. */
  3091. I915_WRITE(PIPESRC(crtc->pipe),
  3092. ((pipe_config->pipe_src_w - 1) << 16) |
  3093. (pipe_config->pipe_src_h - 1));
  3094. /* on skylake this is done by detaching scalers */
  3095. if (INTEL_GEN(dev_priv) >= 9) {
  3096. skl_detach_scalers(crtc);
  3097. if (pipe_config->pch_pfit.enabled)
  3098. skylake_pfit_enable(crtc);
  3099. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3100. if (pipe_config->pch_pfit.enabled)
  3101. ironlake_pfit_enable(crtc);
  3102. else if (old_crtc_state->pch_pfit.enabled)
  3103. ironlake_pfit_disable(crtc, true);
  3104. }
  3105. }
  3106. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3107. {
  3108. struct drm_device *dev = crtc->base.dev;
  3109. struct drm_i915_private *dev_priv = to_i915(dev);
  3110. int pipe = crtc->pipe;
  3111. i915_reg_t reg;
  3112. u32 temp;
  3113. /* enable normal train */
  3114. reg = FDI_TX_CTL(pipe);
  3115. temp = I915_READ(reg);
  3116. if (IS_IVYBRIDGE(dev_priv)) {
  3117. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3118. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3119. } else {
  3120. temp &= ~FDI_LINK_TRAIN_NONE;
  3121. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3122. }
  3123. I915_WRITE(reg, temp);
  3124. reg = FDI_RX_CTL(pipe);
  3125. temp = I915_READ(reg);
  3126. if (HAS_PCH_CPT(dev_priv)) {
  3127. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3128. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3129. } else {
  3130. temp &= ~FDI_LINK_TRAIN_NONE;
  3131. temp |= FDI_LINK_TRAIN_NONE;
  3132. }
  3133. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3134. /* wait one idle pattern time */
  3135. POSTING_READ(reg);
  3136. udelay(1000);
  3137. /* IVB wants error correction enabled */
  3138. if (IS_IVYBRIDGE(dev_priv))
  3139. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3140. FDI_FE_ERRC_ENABLE);
  3141. }
  3142. /* The FDI link training functions for ILK/Ibexpeak. */
  3143. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3144. const struct intel_crtc_state *crtc_state)
  3145. {
  3146. struct drm_device *dev = crtc->base.dev;
  3147. struct drm_i915_private *dev_priv = to_i915(dev);
  3148. int pipe = crtc->pipe;
  3149. i915_reg_t reg;
  3150. u32 temp, tries;
  3151. /* FDI needs bits from pipe first */
  3152. assert_pipe_enabled(dev_priv, pipe);
  3153. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3154. for train result */
  3155. reg = FDI_RX_IMR(pipe);
  3156. temp = I915_READ(reg);
  3157. temp &= ~FDI_RX_SYMBOL_LOCK;
  3158. temp &= ~FDI_RX_BIT_LOCK;
  3159. I915_WRITE(reg, temp);
  3160. I915_READ(reg);
  3161. udelay(150);
  3162. /* enable CPU FDI TX and PCH FDI RX */
  3163. reg = FDI_TX_CTL(pipe);
  3164. temp = I915_READ(reg);
  3165. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3166. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3167. temp &= ~FDI_LINK_TRAIN_NONE;
  3168. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3169. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3170. reg = FDI_RX_CTL(pipe);
  3171. temp = I915_READ(reg);
  3172. temp &= ~FDI_LINK_TRAIN_NONE;
  3173. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3174. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3175. POSTING_READ(reg);
  3176. udelay(150);
  3177. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3178. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3179. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3180. FDI_RX_PHASE_SYNC_POINTER_EN);
  3181. reg = FDI_RX_IIR(pipe);
  3182. for (tries = 0; tries < 5; tries++) {
  3183. temp = I915_READ(reg);
  3184. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3185. if ((temp & FDI_RX_BIT_LOCK)) {
  3186. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3187. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3188. break;
  3189. }
  3190. }
  3191. if (tries == 5)
  3192. DRM_ERROR("FDI train 1 fail!\n");
  3193. /* Train 2 */
  3194. reg = FDI_TX_CTL(pipe);
  3195. temp = I915_READ(reg);
  3196. temp &= ~FDI_LINK_TRAIN_NONE;
  3197. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3198. I915_WRITE(reg, temp);
  3199. reg = FDI_RX_CTL(pipe);
  3200. temp = I915_READ(reg);
  3201. temp &= ~FDI_LINK_TRAIN_NONE;
  3202. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3203. I915_WRITE(reg, temp);
  3204. POSTING_READ(reg);
  3205. udelay(150);
  3206. reg = FDI_RX_IIR(pipe);
  3207. for (tries = 0; tries < 5; tries++) {
  3208. temp = I915_READ(reg);
  3209. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3210. if (temp & FDI_RX_SYMBOL_LOCK) {
  3211. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3212. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3213. break;
  3214. }
  3215. }
  3216. if (tries == 5)
  3217. DRM_ERROR("FDI train 2 fail!\n");
  3218. DRM_DEBUG_KMS("FDI train done\n");
  3219. }
  3220. static const int snb_b_fdi_train_param[] = {
  3221. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3222. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3223. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3224. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3225. };
  3226. /* The FDI link training functions for SNB/Cougarpoint. */
  3227. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3228. const struct intel_crtc_state *crtc_state)
  3229. {
  3230. struct drm_device *dev = crtc->base.dev;
  3231. struct drm_i915_private *dev_priv = to_i915(dev);
  3232. int pipe = crtc->pipe;
  3233. i915_reg_t reg;
  3234. u32 temp, i, retry;
  3235. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3236. for train result */
  3237. reg = FDI_RX_IMR(pipe);
  3238. temp = I915_READ(reg);
  3239. temp &= ~FDI_RX_SYMBOL_LOCK;
  3240. temp &= ~FDI_RX_BIT_LOCK;
  3241. I915_WRITE(reg, temp);
  3242. POSTING_READ(reg);
  3243. udelay(150);
  3244. /* enable CPU FDI TX and PCH FDI RX */
  3245. reg = FDI_TX_CTL(pipe);
  3246. temp = I915_READ(reg);
  3247. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3248. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3249. temp &= ~FDI_LINK_TRAIN_NONE;
  3250. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3251. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3252. /* SNB-B */
  3253. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3254. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3255. I915_WRITE(FDI_RX_MISC(pipe),
  3256. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3257. reg = FDI_RX_CTL(pipe);
  3258. temp = I915_READ(reg);
  3259. if (HAS_PCH_CPT(dev_priv)) {
  3260. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3261. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3262. } else {
  3263. temp &= ~FDI_LINK_TRAIN_NONE;
  3264. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3265. }
  3266. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3267. POSTING_READ(reg);
  3268. udelay(150);
  3269. for (i = 0; i < 4; i++) {
  3270. reg = FDI_TX_CTL(pipe);
  3271. temp = I915_READ(reg);
  3272. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3273. temp |= snb_b_fdi_train_param[i];
  3274. I915_WRITE(reg, temp);
  3275. POSTING_READ(reg);
  3276. udelay(500);
  3277. for (retry = 0; retry < 5; retry++) {
  3278. reg = FDI_RX_IIR(pipe);
  3279. temp = I915_READ(reg);
  3280. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3281. if (temp & FDI_RX_BIT_LOCK) {
  3282. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3283. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3284. break;
  3285. }
  3286. udelay(50);
  3287. }
  3288. if (retry < 5)
  3289. break;
  3290. }
  3291. if (i == 4)
  3292. DRM_ERROR("FDI train 1 fail!\n");
  3293. /* Train 2 */
  3294. reg = FDI_TX_CTL(pipe);
  3295. temp = I915_READ(reg);
  3296. temp &= ~FDI_LINK_TRAIN_NONE;
  3297. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3298. if (IS_GEN6(dev_priv)) {
  3299. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3300. /* SNB-B */
  3301. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3302. }
  3303. I915_WRITE(reg, temp);
  3304. reg = FDI_RX_CTL(pipe);
  3305. temp = I915_READ(reg);
  3306. if (HAS_PCH_CPT(dev_priv)) {
  3307. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3308. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3309. } else {
  3310. temp &= ~FDI_LINK_TRAIN_NONE;
  3311. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3312. }
  3313. I915_WRITE(reg, temp);
  3314. POSTING_READ(reg);
  3315. udelay(150);
  3316. for (i = 0; i < 4; i++) {
  3317. reg = FDI_TX_CTL(pipe);
  3318. temp = I915_READ(reg);
  3319. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3320. temp |= snb_b_fdi_train_param[i];
  3321. I915_WRITE(reg, temp);
  3322. POSTING_READ(reg);
  3323. udelay(500);
  3324. for (retry = 0; retry < 5; retry++) {
  3325. reg = FDI_RX_IIR(pipe);
  3326. temp = I915_READ(reg);
  3327. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3328. if (temp & FDI_RX_SYMBOL_LOCK) {
  3329. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3330. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3331. break;
  3332. }
  3333. udelay(50);
  3334. }
  3335. if (retry < 5)
  3336. break;
  3337. }
  3338. if (i == 4)
  3339. DRM_ERROR("FDI train 2 fail!\n");
  3340. DRM_DEBUG_KMS("FDI train done.\n");
  3341. }
  3342. /* Manual link training for Ivy Bridge A0 parts */
  3343. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3344. const struct intel_crtc_state *crtc_state)
  3345. {
  3346. struct drm_device *dev = crtc->base.dev;
  3347. struct drm_i915_private *dev_priv = to_i915(dev);
  3348. int pipe = crtc->pipe;
  3349. i915_reg_t reg;
  3350. u32 temp, i, j;
  3351. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3352. for train result */
  3353. reg = FDI_RX_IMR(pipe);
  3354. temp = I915_READ(reg);
  3355. temp &= ~FDI_RX_SYMBOL_LOCK;
  3356. temp &= ~FDI_RX_BIT_LOCK;
  3357. I915_WRITE(reg, temp);
  3358. POSTING_READ(reg);
  3359. udelay(150);
  3360. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3361. I915_READ(FDI_RX_IIR(pipe)));
  3362. /* Try each vswing and preemphasis setting twice before moving on */
  3363. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3364. /* disable first in case we need to retry */
  3365. reg = FDI_TX_CTL(pipe);
  3366. temp = I915_READ(reg);
  3367. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3368. temp &= ~FDI_TX_ENABLE;
  3369. I915_WRITE(reg, temp);
  3370. reg = FDI_RX_CTL(pipe);
  3371. temp = I915_READ(reg);
  3372. temp &= ~FDI_LINK_TRAIN_AUTO;
  3373. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3374. temp &= ~FDI_RX_ENABLE;
  3375. I915_WRITE(reg, temp);
  3376. /* enable CPU FDI TX and PCH FDI RX */
  3377. reg = FDI_TX_CTL(pipe);
  3378. temp = I915_READ(reg);
  3379. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3380. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3381. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3382. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3383. temp |= snb_b_fdi_train_param[j/2];
  3384. temp |= FDI_COMPOSITE_SYNC;
  3385. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3386. I915_WRITE(FDI_RX_MISC(pipe),
  3387. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3388. reg = FDI_RX_CTL(pipe);
  3389. temp = I915_READ(reg);
  3390. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3391. temp |= FDI_COMPOSITE_SYNC;
  3392. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3393. POSTING_READ(reg);
  3394. udelay(1); /* should be 0.5us */
  3395. for (i = 0; i < 4; i++) {
  3396. reg = FDI_RX_IIR(pipe);
  3397. temp = I915_READ(reg);
  3398. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3399. if (temp & FDI_RX_BIT_LOCK ||
  3400. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3401. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3402. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3403. i);
  3404. break;
  3405. }
  3406. udelay(1); /* should be 0.5us */
  3407. }
  3408. if (i == 4) {
  3409. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3410. continue;
  3411. }
  3412. /* Train 2 */
  3413. reg = FDI_TX_CTL(pipe);
  3414. temp = I915_READ(reg);
  3415. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3416. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3417. I915_WRITE(reg, temp);
  3418. reg = FDI_RX_CTL(pipe);
  3419. temp = I915_READ(reg);
  3420. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3421. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3422. I915_WRITE(reg, temp);
  3423. POSTING_READ(reg);
  3424. udelay(2); /* should be 1.5us */
  3425. for (i = 0; i < 4; i++) {
  3426. reg = FDI_RX_IIR(pipe);
  3427. temp = I915_READ(reg);
  3428. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3429. if (temp & FDI_RX_SYMBOL_LOCK ||
  3430. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3431. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3432. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3433. i);
  3434. goto train_done;
  3435. }
  3436. udelay(2); /* should be 1.5us */
  3437. }
  3438. if (i == 4)
  3439. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3440. }
  3441. train_done:
  3442. DRM_DEBUG_KMS("FDI train done.\n");
  3443. }
  3444. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3445. {
  3446. struct drm_device *dev = intel_crtc->base.dev;
  3447. struct drm_i915_private *dev_priv = to_i915(dev);
  3448. int pipe = intel_crtc->pipe;
  3449. i915_reg_t reg;
  3450. u32 temp;
  3451. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3452. reg = FDI_RX_CTL(pipe);
  3453. temp = I915_READ(reg);
  3454. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3455. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3456. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3457. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3458. POSTING_READ(reg);
  3459. udelay(200);
  3460. /* Switch from Rawclk to PCDclk */
  3461. temp = I915_READ(reg);
  3462. I915_WRITE(reg, temp | FDI_PCDCLK);
  3463. POSTING_READ(reg);
  3464. udelay(200);
  3465. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3466. reg = FDI_TX_CTL(pipe);
  3467. temp = I915_READ(reg);
  3468. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3469. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3470. POSTING_READ(reg);
  3471. udelay(100);
  3472. }
  3473. }
  3474. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3475. {
  3476. struct drm_device *dev = intel_crtc->base.dev;
  3477. struct drm_i915_private *dev_priv = to_i915(dev);
  3478. int pipe = intel_crtc->pipe;
  3479. i915_reg_t reg;
  3480. u32 temp;
  3481. /* Switch from PCDclk to Rawclk */
  3482. reg = FDI_RX_CTL(pipe);
  3483. temp = I915_READ(reg);
  3484. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3485. /* Disable CPU FDI TX PLL */
  3486. reg = FDI_TX_CTL(pipe);
  3487. temp = I915_READ(reg);
  3488. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3489. POSTING_READ(reg);
  3490. udelay(100);
  3491. reg = FDI_RX_CTL(pipe);
  3492. temp = I915_READ(reg);
  3493. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3494. /* Wait for the clocks to turn off. */
  3495. POSTING_READ(reg);
  3496. udelay(100);
  3497. }
  3498. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3499. {
  3500. struct drm_device *dev = crtc->dev;
  3501. struct drm_i915_private *dev_priv = to_i915(dev);
  3502. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3503. int pipe = intel_crtc->pipe;
  3504. i915_reg_t reg;
  3505. u32 temp;
  3506. /* disable CPU FDI tx and PCH FDI rx */
  3507. reg = FDI_TX_CTL(pipe);
  3508. temp = I915_READ(reg);
  3509. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3510. POSTING_READ(reg);
  3511. reg = FDI_RX_CTL(pipe);
  3512. temp = I915_READ(reg);
  3513. temp &= ~(0x7 << 16);
  3514. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3515. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3516. POSTING_READ(reg);
  3517. udelay(100);
  3518. /* Ironlake workaround, disable clock pointer after downing FDI */
  3519. if (HAS_PCH_IBX(dev_priv))
  3520. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3521. /* still set train pattern 1 */
  3522. reg = FDI_TX_CTL(pipe);
  3523. temp = I915_READ(reg);
  3524. temp &= ~FDI_LINK_TRAIN_NONE;
  3525. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3526. I915_WRITE(reg, temp);
  3527. reg = FDI_RX_CTL(pipe);
  3528. temp = I915_READ(reg);
  3529. if (HAS_PCH_CPT(dev_priv)) {
  3530. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3531. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3532. } else {
  3533. temp &= ~FDI_LINK_TRAIN_NONE;
  3534. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3535. }
  3536. /* BPC in FDI rx is consistent with that in PIPECONF */
  3537. temp &= ~(0x07 << 16);
  3538. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3539. I915_WRITE(reg, temp);
  3540. POSTING_READ(reg);
  3541. udelay(100);
  3542. }
  3543. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3544. {
  3545. struct intel_crtc *crtc;
  3546. /* Note that we don't need to be called with mode_config.lock here
  3547. * as our list of CRTC objects is static for the lifetime of the
  3548. * device and so cannot disappear as we iterate. Similarly, we can
  3549. * happily treat the predicates as racy, atomic checks as userspace
  3550. * cannot claim and pin a new fb without at least acquring the
  3551. * struct_mutex and so serialising with us.
  3552. */
  3553. for_each_intel_crtc(&dev_priv->drm, crtc) {
  3554. if (atomic_read(&crtc->unpin_work_count) == 0)
  3555. continue;
  3556. if (crtc->flip_work)
  3557. intel_wait_for_vblank(dev_priv, crtc->pipe);
  3558. return true;
  3559. }
  3560. return false;
  3561. }
  3562. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3563. {
  3564. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3565. struct intel_flip_work *work = intel_crtc->flip_work;
  3566. intel_crtc->flip_work = NULL;
  3567. if (work->event)
  3568. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3569. drm_crtc_vblank_put(&intel_crtc->base);
  3570. wake_up_all(&dev_priv->pending_flip_queue);
  3571. trace_i915_flip_complete(intel_crtc->plane,
  3572. work->pending_flip_obj);
  3573. queue_work(dev_priv->wq, &work->unpin_work);
  3574. }
  3575. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3576. {
  3577. struct drm_device *dev = crtc->dev;
  3578. struct drm_i915_private *dev_priv = to_i915(dev);
  3579. long ret;
  3580. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3581. ret = wait_event_interruptible_timeout(
  3582. dev_priv->pending_flip_queue,
  3583. !intel_crtc_has_pending_flip(crtc),
  3584. 60*HZ);
  3585. if (ret < 0)
  3586. return ret;
  3587. if (ret == 0) {
  3588. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3589. struct intel_flip_work *work;
  3590. spin_lock_irq(&dev->event_lock);
  3591. work = intel_crtc->flip_work;
  3592. if (work && !is_mmio_work(work)) {
  3593. WARN_ONCE(1, "Removing stuck page flip\n");
  3594. page_flip_completed(intel_crtc);
  3595. }
  3596. spin_unlock_irq(&dev->event_lock);
  3597. }
  3598. return 0;
  3599. }
  3600. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3601. {
  3602. u32 temp;
  3603. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3604. mutex_lock(&dev_priv->sb_lock);
  3605. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3606. temp |= SBI_SSCCTL_DISABLE;
  3607. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3608. mutex_unlock(&dev_priv->sb_lock);
  3609. }
  3610. /* Program iCLKIP clock to the desired frequency */
  3611. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3612. {
  3613. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3614. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3615. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3616. u32 temp;
  3617. lpt_disable_iclkip(dev_priv);
  3618. /* The iCLK virtual clock root frequency is in MHz,
  3619. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3620. * divisors, it is necessary to divide one by another, so we
  3621. * convert the virtual clock precision to KHz here for higher
  3622. * precision.
  3623. */
  3624. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3625. u32 iclk_virtual_root_freq = 172800 * 1000;
  3626. u32 iclk_pi_range = 64;
  3627. u32 desired_divisor;
  3628. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3629. clock << auxdiv);
  3630. divsel = (desired_divisor / iclk_pi_range) - 2;
  3631. phaseinc = desired_divisor % iclk_pi_range;
  3632. /*
  3633. * Near 20MHz is a corner case which is
  3634. * out of range for the 7-bit divisor
  3635. */
  3636. if (divsel <= 0x7f)
  3637. break;
  3638. }
  3639. /* This should not happen with any sane values */
  3640. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3641. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3642. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3643. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3644. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3645. clock,
  3646. auxdiv,
  3647. divsel,
  3648. phasedir,
  3649. phaseinc);
  3650. mutex_lock(&dev_priv->sb_lock);
  3651. /* Program SSCDIVINTPHASE6 */
  3652. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3653. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3654. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3655. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3656. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3657. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3658. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3659. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3660. /* Program SSCAUXDIV */
  3661. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3662. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3663. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3664. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3665. /* Enable modulator and associated divider */
  3666. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3667. temp &= ~SBI_SSCCTL_DISABLE;
  3668. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3669. mutex_unlock(&dev_priv->sb_lock);
  3670. /* Wait for initialization time */
  3671. udelay(24);
  3672. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3673. }
  3674. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3675. {
  3676. u32 divsel, phaseinc, auxdiv;
  3677. u32 iclk_virtual_root_freq = 172800 * 1000;
  3678. u32 iclk_pi_range = 64;
  3679. u32 desired_divisor;
  3680. u32 temp;
  3681. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3682. return 0;
  3683. mutex_lock(&dev_priv->sb_lock);
  3684. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3685. if (temp & SBI_SSCCTL_DISABLE) {
  3686. mutex_unlock(&dev_priv->sb_lock);
  3687. return 0;
  3688. }
  3689. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3690. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3691. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3692. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3693. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3694. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3695. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3696. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3697. mutex_unlock(&dev_priv->sb_lock);
  3698. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3699. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3700. desired_divisor << auxdiv);
  3701. }
  3702. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3703. enum pipe pch_transcoder)
  3704. {
  3705. struct drm_device *dev = crtc->base.dev;
  3706. struct drm_i915_private *dev_priv = to_i915(dev);
  3707. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3708. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3709. I915_READ(HTOTAL(cpu_transcoder)));
  3710. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3711. I915_READ(HBLANK(cpu_transcoder)));
  3712. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3713. I915_READ(HSYNC(cpu_transcoder)));
  3714. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3715. I915_READ(VTOTAL(cpu_transcoder)));
  3716. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3717. I915_READ(VBLANK(cpu_transcoder)));
  3718. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3719. I915_READ(VSYNC(cpu_transcoder)));
  3720. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3721. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3722. }
  3723. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3724. {
  3725. struct drm_i915_private *dev_priv = to_i915(dev);
  3726. uint32_t temp;
  3727. temp = I915_READ(SOUTH_CHICKEN1);
  3728. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3729. return;
  3730. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3731. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3732. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3733. if (enable)
  3734. temp |= FDI_BC_BIFURCATION_SELECT;
  3735. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3736. I915_WRITE(SOUTH_CHICKEN1, temp);
  3737. POSTING_READ(SOUTH_CHICKEN1);
  3738. }
  3739. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3740. {
  3741. struct drm_device *dev = intel_crtc->base.dev;
  3742. switch (intel_crtc->pipe) {
  3743. case PIPE_A:
  3744. break;
  3745. case PIPE_B:
  3746. if (intel_crtc->config->fdi_lanes > 2)
  3747. cpt_set_fdi_bc_bifurcation(dev, false);
  3748. else
  3749. cpt_set_fdi_bc_bifurcation(dev, true);
  3750. break;
  3751. case PIPE_C:
  3752. cpt_set_fdi_bc_bifurcation(dev, true);
  3753. break;
  3754. default:
  3755. BUG();
  3756. }
  3757. }
  3758. /* Return which DP Port should be selected for Transcoder DP control */
  3759. static enum port
  3760. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3761. {
  3762. struct drm_device *dev = crtc->base.dev;
  3763. struct intel_encoder *encoder;
  3764. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3765. if (encoder->type == INTEL_OUTPUT_DP ||
  3766. encoder->type == INTEL_OUTPUT_EDP)
  3767. return enc_to_dig_port(&encoder->base)->port;
  3768. }
  3769. return -1;
  3770. }
  3771. /*
  3772. * Enable PCH resources required for PCH ports:
  3773. * - PCH PLLs
  3774. * - FDI training & RX/TX
  3775. * - update transcoder timings
  3776. * - DP transcoding bits
  3777. * - transcoder
  3778. */
  3779. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3780. {
  3781. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3782. struct drm_device *dev = crtc->base.dev;
  3783. struct drm_i915_private *dev_priv = to_i915(dev);
  3784. int pipe = crtc->pipe;
  3785. u32 temp;
  3786. assert_pch_transcoder_disabled(dev_priv, pipe);
  3787. if (IS_IVYBRIDGE(dev_priv))
  3788. ivybridge_update_fdi_bc_bifurcation(crtc);
  3789. /* Write the TU size bits before fdi link training, so that error
  3790. * detection works. */
  3791. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3792. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3793. /* For PCH output, training FDI link */
  3794. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3795. /* We need to program the right clock selection before writing the pixel
  3796. * mutliplier into the DPLL. */
  3797. if (HAS_PCH_CPT(dev_priv)) {
  3798. u32 sel;
  3799. temp = I915_READ(PCH_DPLL_SEL);
  3800. temp |= TRANS_DPLL_ENABLE(pipe);
  3801. sel = TRANS_DPLLB_SEL(pipe);
  3802. if (crtc_state->shared_dpll ==
  3803. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3804. temp |= sel;
  3805. else
  3806. temp &= ~sel;
  3807. I915_WRITE(PCH_DPLL_SEL, temp);
  3808. }
  3809. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3810. * transcoder, and we actually should do this to not upset any PCH
  3811. * transcoder that already use the clock when we share it.
  3812. *
  3813. * Note that enable_shared_dpll tries to do the right thing, but
  3814. * get_shared_dpll unconditionally resets the pll - we need that to have
  3815. * the right LVDS enable sequence. */
  3816. intel_enable_shared_dpll(crtc);
  3817. /* set transcoder timing, panel must allow it */
  3818. assert_panel_unlocked(dev_priv, pipe);
  3819. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3820. intel_fdi_normal_train(crtc);
  3821. /* For PCH DP, enable TRANS_DP_CTL */
  3822. if (HAS_PCH_CPT(dev_priv) &&
  3823. intel_crtc_has_dp_encoder(crtc_state)) {
  3824. const struct drm_display_mode *adjusted_mode =
  3825. &crtc_state->base.adjusted_mode;
  3826. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3827. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3828. temp = I915_READ(reg);
  3829. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3830. TRANS_DP_SYNC_MASK |
  3831. TRANS_DP_BPC_MASK);
  3832. temp |= TRANS_DP_OUTPUT_ENABLE;
  3833. temp |= bpc << 9; /* same format but at 11:9 */
  3834. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3835. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3836. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3837. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3838. switch (intel_trans_dp_port_sel(crtc)) {
  3839. case PORT_B:
  3840. temp |= TRANS_DP_PORT_SEL_B;
  3841. break;
  3842. case PORT_C:
  3843. temp |= TRANS_DP_PORT_SEL_C;
  3844. break;
  3845. case PORT_D:
  3846. temp |= TRANS_DP_PORT_SEL_D;
  3847. break;
  3848. default:
  3849. BUG();
  3850. }
  3851. I915_WRITE(reg, temp);
  3852. }
  3853. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3854. }
  3855. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  3856. {
  3857. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3858. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3859. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  3860. assert_pch_transcoder_disabled(dev_priv, PIPE_A);
  3861. lpt_program_iclkip(crtc);
  3862. /* Set transcoder timing. */
  3863. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  3864. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3865. }
  3866. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3867. {
  3868. struct drm_i915_private *dev_priv = to_i915(dev);
  3869. i915_reg_t dslreg = PIPEDSL(pipe);
  3870. u32 temp;
  3871. temp = I915_READ(dslreg);
  3872. udelay(500);
  3873. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3874. if (wait_for(I915_READ(dslreg) != temp, 5))
  3875. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3876. }
  3877. }
  3878. static int
  3879. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3880. unsigned int scaler_user, int *scaler_id,
  3881. int src_w, int src_h, int dst_w, int dst_h)
  3882. {
  3883. struct intel_crtc_scaler_state *scaler_state =
  3884. &crtc_state->scaler_state;
  3885. struct intel_crtc *intel_crtc =
  3886. to_intel_crtc(crtc_state->base.crtc);
  3887. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3888. const struct drm_display_mode *adjusted_mode =
  3889. &crtc_state->base.adjusted_mode;
  3890. int need_scaling;
  3891. /*
  3892. * Src coordinates are already rotated by 270 degrees for
  3893. * the 90/270 degree plane rotation cases (to match the
  3894. * GTT mapping), hence no need to account for rotation here.
  3895. */
  3896. need_scaling = src_w != dst_w || src_h != dst_h;
  3897. /*
  3898. * Scaling/fitting not supported in IF-ID mode in GEN9+
  3899. * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
  3900. * Once NV12 is enabled, handle it here while allocating scaler
  3901. * for NV12.
  3902. */
  3903. if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
  3904. need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3905. DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
  3906. return -EINVAL;
  3907. }
  3908. /*
  3909. * if plane is being disabled or scaler is no more required or force detach
  3910. * - free scaler binded to this plane/crtc
  3911. * - in order to do this, update crtc->scaler_usage
  3912. *
  3913. * Here scaler state in crtc_state is set free so that
  3914. * scaler can be assigned to other user. Actual register
  3915. * update to free the scaler is done in plane/panel-fit programming.
  3916. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3917. */
  3918. if (force_detach || !need_scaling) {
  3919. if (*scaler_id >= 0) {
  3920. scaler_state->scaler_users &= ~(1 << scaler_user);
  3921. scaler_state->scalers[*scaler_id].in_use = 0;
  3922. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3923. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3924. intel_crtc->pipe, scaler_user, *scaler_id,
  3925. scaler_state->scaler_users);
  3926. *scaler_id = -1;
  3927. }
  3928. return 0;
  3929. }
  3930. /* range checks */
  3931. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3932. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3933. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3934. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3935. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3936. "size is out of scaler range\n",
  3937. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3938. return -EINVAL;
  3939. }
  3940. /* mark this plane as a scaler user in crtc_state */
  3941. scaler_state->scaler_users |= (1 << scaler_user);
  3942. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3943. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3944. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3945. scaler_state->scaler_users);
  3946. return 0;
  3947. }
  3948. /**
  3949. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3950. *
  3951. * @state: crtc's scaler state
  3952. *
  3953. * Return
  3954. * 0 - scaler_usage updated successfully
  3955. * error - requested scaling cannot be supported or other error condition
  3956. */
  3957. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3958. {
  3959. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3960. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3961. &state->scaler_state.scaler_id,
  3962. state->pipe_src_w, state->pipe_src_h,
  3963. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3964. }
  3965. /**
  3966. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3967. *
  3968. * @state: crtc's scaler state
  3969. * @plane_state: atomic plane state to update
  3970. *
  3971. * Return
  3972. * 0 - scaler_usage updated successfully
  3973. * error - requested scaling cannot be supported or other error condition
  3974. */
  3975. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3976. struct intel_plane_state *plane_state)
  3977. {
  3978. struct intel_plane *intel_plane =
  3979. to_intel_plane(plane_state->base.plane);
  3980. struct drm_framebuffer *fb = plane_state->base.fb;
  3981. int ret;
  3982. bool force_detach = !fb || !plane_state->base.visible;
  3983. ret = skl_update_scaler(crtc_state, force_detach,
  3984. drm_plane_index(&intel_plane->base),
  3985. &plane_state->scaler_id,
  3986. drm_rect_width(&plane_state->base.src) >> 16,
  3987. drm_rect_height(&plane_state->base.src) >> 16,
  3988. drm_rect_width(&plane_state->base.dst),
  3989. drm_rect_height(&plane_state->base.dst));
  3990. if (ret || plane_state->scaler_id < 0)
  3991. return ret;
  3992. /* check colorkey */
  3993. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3994. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  3995. intel_plane->base.base.id,
  3996. intel_plane->base.name);
  3997. return -EINVAL;
  3998. }
  3999. /* Check src format */
  4000. switch (fb->format->format) {
  4001. case DRM_FORMAT_RGB565:
  4002. case DRM_FORMAT_XBGR8888:
  4003. case DRM_FORMAT_XRGB8888:
  4004. case DRM_FORMAT_ABGR8888:
  4005. case DRM_FORMAT_ARGB8888:
  4006. case DRM_FORMAT_XRGB2101010:
  4007. case DRM_FORMAT_XBGR2101010:
  4008. case DRM_FORMAT_YUYV:
  4009. case DRM_FORMAT_YVYU:
  4010. case DRM_FORMAT_UYVY:
  4011. case DRM_FORMAT_VYUY:
  4012. break;
  4013. default:
  4014. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4015. intel_plane->base.base.id, intel_plane->base.name,
  4016. fb->base.id, fb->format->format);
  4017. return -EINVAL;
  4018. }
  4019. return 0;
  4020. }
  4021. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4022. {
  4023. int i;
  4024. for (i = 0; i < crtc->num_scalers; i++)
  4025. skl_detach_scaler(crtc, i);
  4026. }
  4027. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4028. {
  4029. struct drm_device *dev = crtc->base.dev;
  4030. struct drm_i915_private *dev_priv = to_i915(dev);
  4031. int pipe = crtc->pipe;
  4032. struct intel_crtc_scaler_state *scaler_state =
  4033. &crtc->config->scaler_state;
  4034. if (crtc->config->pch_pfit.enabled) {
  4035. int id;
  4036. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4037. return;
  4038. id = scaler_state->scaler_id;
  4039. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4040. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4041. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4042. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4043. }
  4044. }
  4045. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4046. {
  4047. struct drm_device *dev = crtc->base.dev;
  4048. struct drm_i915_private *dev_priv = to_i915(dev);
  4049. int pipe = crtc->pipe;
  4050. if (crtc->config->pch_pfit.enabled) {
  4051. /* Force use of hard-coded filter coefficients
  4052. * as some pre-programmed values are broken,
  4053. * e.g. x201.
  4054. */
  4055. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4056. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4057. PF_PIPE_SEL_IVB(pipe));
  4058. else
  4059. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4060. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4061. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4062. }
  4063. }
  4064. void hsw_enable_ips(struct intel_crtc *crtc)
  4065. {
  4066. struct drm_device *dev = crtc->base.dev;
  4067. struct drm_i915_private *dev_priv = to_i915(dev);
  4068. if (!crtc->config->ips_enabled)
  4069. return;
  4070. /*
  4071. * We can only enable IPS after we enable a plane and wait for a vblank
  4072. * This function is called from post_plane_update, which is run after
  4073. * a vblank wait.
  4074. */
  4075. assert_plane_enabled(dev_priv, crtc->plane);
  4076. if (IS_BROADWELL(dev_priv)) {
  4077. mutex_lock(&dev_priv->rps.hw_lock);
  4078. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4079. mutex_unlock(&dev_priv->rps.hw_lock);
  4080. /* Quoting Art Runyan: "its not safe to expect any particular
  4081. * value in IPS_CTL bit 31 after enabling IPS through the
  4082. * mailbox." Moreover, the mailbox may return a bogus state,
  4083. * so we need to just enable it and continue on.
  4084. */
  4085. } else {
  4086. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4087. /* The bit only becomes 1 in the next vblank, so this wait here
  4088. * is essentially intel_wait_for_vblank. If we don't have this
  4089. * and don't wait for vblanks until the end of crtc_enable, then
  4090. * the HW state readout code will complain that the expected
  4091. * IPS_CTL value is not the one we read. */
  4092. if (intel_wait_for_register(dev_priv,
  4093. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4094. 50))
  4095. DRM_ERROR("Timed out waiting for IPS enable\n");
  4096. }
  4097. }
  4098. void hsw_disable_ips(struct intel_crtc *crtc)
  4099. {
  4100. struct drm_device *dev = crtc->base.dev;
  4101. struct drm_i915_private *dev_priv = to_i915(dev);
  4102. if (!crtc->config->ips_enabled)
  4103. return;
  4104. assert_plane_enabled(dev_priv, crtc->plane);
  4105. if (IS_BROADWELL(dev_priv)) {
  4106. mutex_lock(&dev_priv->rps.hw_lock);
  4107. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4108. mutex_unlock(&dev_priv->rps.hw_lock);
  4109. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4110. if (intel_wait_for_register(dev_priv,
  4111. IPS_CTL, IPS_ENABLE, 0,
  4112. 42))
  4113. DRM_ERROR("Timed out waiting for IPS disable\n");
  4114. } else {
  4115. I915_WRITE(IPS_CTL, 0);
  4116. POSTING_READ(IPS_CTL);
  4117. }
  4118. /* We need to wait for a vblank before we can disable the plane. */
  4119. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4120. }
  4121. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4122. {
  4123. if (intel_crtc->overlay) {
  4124. struct drm_device *dev = intel_crtc->base.dev;
  4125. mutex_lock(&dev->struct_mutex);
  4126. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4127. mutex_unlock(&dev->struct_mutex);
  4128. }
  4129. /* Let userspace switch the overlay on again. In most cases userspace
  4130. * has to recompute where to put it anyway.
  4131. */
  4132. }
  4133. /**
  4134. * intel_post_enable_primary - Perform operations after enabling primary plane
  4135. * @crtc: the CRTC whose primary plane was just enabled
  4136. *
  4137. * Performs potentially sleeping operations that must be done after the primary
  4138. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4139. * called due to an explicit primary plane update, or due to an implicit
  4140. * re-enable that is caused when a sprite plane is updated to no longer
  4141. * completely hide the primary plane.
  4142. */
  4143. static void
  4144. intel_post_enable_primary(struct drm_crtc *crtc)
  4145. {
  4146. struct drm_device *dev = crtc->dev;
  4147. struct drm_i915_private *dev_priv = to_i915(dev);
  4148. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4149. int pipe = intel_crtc->pipe;
  4150. /*
  4151. * FIXME IPS should be fine as long as one plane is
  4152. * enabled, but in practice it seems to have problems
  4153. * when going from primary only to sprite only and vice
  4154. * versa.
  4155. */
  4156. hsw_enable_ips(intel_crtc);
  4157. /*
  4158. * Gen2 reports pipe underruns whenever all planes are disabled.
  4159. * So don't enable underrun reporting before at least some planes
  4160. * are enabled.
  4161. * FIXME: Need to fix the logic to work when we turn off all planes
  4162. * but leave the pipe running.
  4163. */
  4164. if (IS_GEN2(dev_priv))
  4165. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4166. /* Underruns don't always raise interrupts, so check manually. */
  4167. intel_check_cpu_fifo_underruns(dev_priv);
  4168. intel_check_pch_fifo_underruns(dev_priv);
  4169. }
  4170. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4171. static void
  4172. intel_pre_disable_primary(struct drm_crtc *crtc)
  4173. {
  4174. struct drm_device *dev = crtc->dev;
  4175. struct drm_i915_private *dev_priv = to_i915(dev);
  4176. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4177. int pipe = intel_crtc->pipe;
  4178. /*
  4179. * Gen2 reports pipe underruns whenever all planes are disabled.
  4180. * So diasble underrun reporting before all the planes get disabled.
  4181. * FIXME: Need to fix the logic to work when we turn off all planes
  4182. * but leave the pipe running.
  4183. */
  4184. if (IS_GEN2(dev_priv))
  4185. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4186. /*
  4187. * FIXME IPS should be fine as long as one plane is
  4188. * enabled, but in practice it seems to have problems
  4189. * when going from primary only to sprite only and vice
  4190. * versa.
  4191. */
  4192. hsw_disable_ips(intel_crtc);
  4193. }
  4194. /* FIXME get rid of this and use pre_plane_update */
  4195. static void
  4196. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4197. {
  4198. struct drm_device *dev = crtc->dev;
  4199. struct drm_i915_private *dev_priv = to_i915(dev);
  4200. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4201. int pipe = intel_crtc->pipe;
  4202. intel_pre_disable_primary(crtc);
  4203. /*
  4204. * Vblank time updates from the shadow to live plane control register
  4205. * are blocked if the memory self-refresh mode is active at that
  4206. * moment. So to make sure the plane gets truly disabled, disable
  4207. * first the self-refresh mode. The self-refresh enable bit in turn
  4208. * will be checked/applied by the HW only at the next frame start
  4209. * event which is after the vblank start event, so we need to have a
  4210. * wait-for-vblank between disabling the plane and the pipe.
  4211. */
  4212. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4213. intel_set_memory_cxsr(dev_priv, false))
  4214. intel_wait_for_vblank(dev_priv, pipe);
  4215. }
  4216. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4217. {
  4218. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4219. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4220. struct intel_crtc_state *pipe_config =
  4221. to_intel_crtc_state(crtc->base.state);
  4222. struct drm_plane *primary = crtc->base.primary;
  4223. struct drm_plane_state *old_pri_state =
  4224. drm_atomic_get_existing_plane_state(old_state, primary);
  4225. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4226. if (pipe_config->update_wm_post && pipe_config->base.active)
  4227. intel_update_watermarks(crtc);
  4228. if (old_pri_state) {
  4229. struct intel_plane_state *primary_state =
  4230. to_intel_plane_state(primary->state);
  4231. struct intel_plane_state *old_primary_state =
  4232. to_intel_plane_state(old_pri_state);
  4233. intel_fbc_post_update(crtc);
  4234. if (primary_state->base.visible &&
  4235. (needs_modeset(&pipe_config->base) ||
  4236. !old_primary_state->base.visible))
  4237. intel_post_enable_primary(&crtc->base);
  4238. }
  4239. }
  4240. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4241. struct intel_crtc_state *pipe_config)
  4242. {
  4243. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4244. struct drm_device *dev = crtc->base.dev;
  4245. struct drm_i915_private *dev_priv = to_i915(dev);
  4246. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4247. struct drm_plane *primary = crtc->base.primary;
  4248. struct drm_plane_state *old_pri_state =
  4249. drm_atomic_get_existing_plane_state(old_state, primary);
  4250. bool modeset = needs_modeset(&pipe_config->base);
  4251. struct intel_atomic_state *old_intel_state =
  4252. to_intel_atomic_state(old_state);
  4253. if (old_pri_state) {
  4254. struct intel_plane_state *primary_state =
  4255. to_intel_plane_state(primary->state);
  4256. struct intel_plane_state *old_primary_state =
  4257. to_intel_plane_state(old_pri_state);
  4258. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4259. if (old_primary_state->base.visible &&
  4260. (modeset || !primary_state->base.visible))
  4261. intel_pre_disable_primary(&crtc->base);
  4262. }
  4263. /*
  4264. * Vblank time updates from the shadow to live plane control register
  4265. * are blocked if the memory self-refresh mode is active at that
  4266. * moment. So to make sure the plane gets truly disabled, disable
  4267. * first the self-refresh mode. The self-refresh enable bit in turn
  4268. * will be checked/applied by the HW only at the next frame start
  4269. * event which is after the vblank start event, so we need to have a
  4270. * wait-for-vblank between disabling the plane and the pipe.
  4271. */
  4272. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4273. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4274. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4275. /*
  4276. * IVB workaround: must disable low power watermarks for at least
  4277. * one frame before enabling scaling. LP watermarks can be re-enabled
  4278. * when scaling is disabled.
  4279. *
  4280. * WaCxSRDisabledForSpriteScaling:ivb
  4281. */
  4282. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4283. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4284. /*
  4285. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4286. * watermark programming here.
  4287. */
  4288. if (needs_modeset(&pipe_config->base))
  4289. return;
  4290. /*
  4291. * For platforms that support atomic watermarks, program the
  4292. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4293. * will be the intermediate values that are safe for both pre- and
  4294. * post- vblank; when vblank happens, the 'active' values will be set
  4295. * to the final 'target' values and we'll do this again to get the
  4296. * optimal watermarks. For gen9+ platforms, the values we program here
  4297. * will be the final target values which will get automatically latched
  4298. * at vblank time; no further programming will be necessary.
  4299. *
  4300. * If a platform hasn't been transitioned to atomic watermarks yet,
  4301. * we'll continue to update watermarks the old way, if flags tell
  4302. * us to.
  4303. */
  4304. if (dev_priv->display.initial_watermarks != NULL)
  4305. dev_priv->display.initial_watermarks(old_intel_state,
  4306. pipe_config);
  4307. else if (pipe_config->update_wm_pre)
  4308. intel_update_watermarks(crtc);
  4309. }
  4310. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4311. {
  4312. struct drm_device *dev = crtc->dev;
  4313. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4314. struct drm_plane *p;
  4315. int pipe = intel_crtc->pipe;
  4316. intel_crtc_dpms_overlay_disable(intel_crtc);
  4317. drm_for_each_plane_mask(p, dev, plane_mask)
  4318. to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
  4319. /*
  4320. * FIXME: Once we grow proper nuclear flip support out of this we need
  4321. * to compute the mask of flip planes precisely. For the time being
  4322. * consider this a flip to a NULL plane.
  4323. */
  4324. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4325. }
  4326. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4327. struct intel_crtc_state *crtc_state,
  4328. struct drm_atomic_state *old_state)
  4329. {
  4330. struct drm_connector_state *conn_state;
  4331. struct drm_connector *conn;
  4332. int i;
  4333. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4334. struct intel_encoder *encoder =
  4335. to_intel_encoder(conn_state->best_encoder);
  4336. if (conn_state->crtc != crtc)
  4337. continue;
  4338. if (encoder->pre_pll_enable)
  4339. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4340. }
  4341. }
  4342. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4343. struct intel_crtc_state *crtc_state,
  4344. struct drm_atomic_state *old_state)
  4345. {
  4346. struct drm_connector_state *conn_state;
  4347. struct drm_connector *conn;
  4348. int i;
  4349. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4350. struct intel_encoder *encoder =
  4351. to_intel_encoder(conn_state->best_encoder);
  4352. if (conn_state->crtc != crtc)
  4353. continue;
  4354. if (encoder->pre_enable)
  4355. encoder->pre_enable(encoder, crtc_state, conn_state);
  4356. }
  4357. }
  4358. static void intel_encoders_enable(struct drm_crtc *crtc,
  4359. struct intel_crtc_state *crtc_state,
  4360. struct drm_atomic_state *old_state)
  4361. {
  4362. struct drm_connector_state *conn_state;
  4363. struct drm_connector *conn;
  4364. int i;
  4365. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4366. struct intel_encoder *encoder =
  4367. to_intel_encoder(conn_state->best_encoder);
  4368. if (conn_state->crtc != crtc)
  4369. continue;
  4370. encoder->enable(encoder, crtc_state, conn_state);
  4371. intel_opregion_notify_encoder(encoder, true);
  4372. }
  4373. }
  4374. static void intel_encoders_disable(struct drm_crtc *crtc,
  4375. struct intel_crtc_state *old_crtc_state,
  4376. struct drm_atomic_state *old_state)
  4377. {
  4378. struct drm_connector_state *old_conn_state;
  4379. struct drm_connector *conn;
  4380. int i;
  4381. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4382. struct intel_encoder *encoder =
  4383. to_intel_encoder(old_conn_state->best_encoder);
  4384. if (old_conn_state->crtc != crtc)
  4385. continue;
  4386. intel_opregion_notify_encoder(encoder, false);
  4387. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4388. }
  4389. }
  4390. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4391. struct intel_crtc_state *old_crtc_state,
  4392. struct drm_atomic_state *old_state)
  4393. {
  4394. struct drm_connector_state *old_conn_state;
  4395. struct drm_connector *conn;
  4396. int i;
  4397. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4398. struct intel_encoder *encoder =
  4399. to_intel_encoder(old_conn_state->best_encoder);
  4400. if (old_conn_state->crtc != crtc)
  4401. continue;
  4402. if (encoder->post_disable)
  4403. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4404. }
  4405. }
  4406. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4407. struct intel_crtc_state *old_crtc_state,
  4408. struct drm_atomic_state *old_state)
  4409. {
  4410. struct drm_connector_state *old_conn_state;
  4411. struct drm_connector *conn;
  4412. int i;
  4413. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4414. struct intel_encoder *encoder =
  4415. to_intel_encoder(old_conn_state->best_encoder);
  4416. if (old_conn_state->crtc != crtc)
  4417. continue;
  4418. if (encoder->post_pll_disable)
  4419. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4420. }
  4421. }
  4422. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4423. struct drm_atomic_state *old_state)
  4424. {
  4425. struct drm_crtc *crtc = pipe_config->base.crtc;
  4426. struct drm_device *dev = crtc->dev;
  4427. struct drm_i915_private *dev_priv = to_i915(dev);
  4428. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4429. int pipe = intel_crtc->pipe;
  4430. struct intel_atomic_state *old_intel_state =
  4431. to_intel_atomic_state(old_state);
  4432. if (WARN_ON(intel_crtc->active))
  4433. return;
  4434. /*
  4435. * Sometimes spurious CPU pipe underruns happen during FDI
  4436. * training, at least with VGA+HDMI cloning. Suppress them.
  4437. *
  4438. * On ILK we get an occasional spurious CPU pipe underruns
  4439. * between eDP port A enable and vdd enable. Also PCH port
  4440. * enable seems to result in the occasional CPU pipe underrun.
  4441. *
  4442. * Spurious PCH underruns also occur during PCH enabling.
  4443. */
  4444. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4445. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4446. if (intel_crtc->config->has_pch_encoder)
  4447. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4448. if (intel_crtc->config->has_pch_encoder)
  4449. intel_prepare_shared_dpll(intel_crtc);
  4450. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4451. intel_dp_set_m_n(intel_crtc, M1_N1);
  4452. intel_set_pipe_timings(intel_crtc);
  4453. intel_set_pipe_src_size(intel_crtc);
  4454. if (intel_crtc->config->has_pch_encoder) {
  4455. intel_cpu_transcoder_set_m_n(intel_crtc,
  4456. &intel_crtc->config->fdi_m_n, NULL);
  4457. }
  4458. ironlake_set_pipeconf(crtc);
  4459. intel_crtc->active = true;
  4460. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4461. if (intel_crtc->config->has_pch_encoder) {
  4462. /* Note: FDI PLL enabling _must_ be done before we enable the
  4463. * cpu pipes, hence this is separate from all the other fdi/pch
  4464. * enabling. */
  4465. ironlake_fdi_pll_enable(intel_crtc);
  4466. } else {
  4467. assert_fdi_tx_disabled(dev_priv, pipe);
  4468. assert_fdi_rx_disabled(dev_priv, pipe);
  4469. }
  4470. ironlake_pfit_enable(intel_crtc);
  4471. /*
  4472. * On ILK+ LUT must be loaded before the pipe is running but with
  4473. * clocks enabled
  4474. */
  4475. intel_color_load_luts(&pipe_config->base);
  4476. if (dev_priv->display.initial_watermarks != NULL)
  4477. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4478. intel_enable_pipe(intel_crtc);
  4479. if (intel_crtc->config->has_pch_encoder)
  4480. ironlake_pch_enable(pipe_config);
  4481. assert_vblank_disabled(crtc);
  4482. drm_crtc_vblank_on(crtc);
  4483. intel_encoders_enable(crtc, pipe_config, old_state);
  4484. if (HAS_PCH_CPT(dev_priv))
  4485. cpt_verify_modeset(dev, intel_crtc->pipe);
  4486. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4487. if (intel_crtc->config->has_pch_encoder)
  4488. intel_wait_for_vblank(dev_priv, pipe);
  4489. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4490. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4491. }
  4492. /* IPS only exists on ULT machines and is tied to pipe A. */
  4493. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4494. {
  4495. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4496. }
  4497. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4498. struct drm_atomic_state *old_state)
  4499. {
  4500. struct drm_crtc *crtc = pipe_config->base.crtc;
  4501. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4502. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4503. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4504. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4505. struct intel_atomic_state *old_intel_state =
  4506. to_intel_atomic_state(old_state);
  4507. if (WARN_ON(intel_crtc->active))
  4508. return;
  4509. if (intel_crtc->config->has_pch_encoder)
  4510. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
  4511. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4512. if (intel_crtc->config->shared_dpll)
  4513. intel_enable_shared_dpll(intel_crtc);
  4514. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4515. intel_dp_set_m_n(intel_crtc, M1_N1);
  4516. if (!transcoder_is_dsi(cpu_transcoder))
  4517. intel_set_pipe_timings(intel_crtc);
  4518. intel_set_pipe_src_size(intel_crtc);
  4519. if (cpu_transcoder != TRANSCODER_EDP &&
  4520. !transcoder_is_dsi(cpu_transcoder)) {
  4521. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4522. intel_crtc->config->pixel_multiplier - 1);
  4523. }
  4524. if (intel_crtc->config->has_pch_encoder) {
  4525. intel_cpu_transcoder_set_m_n(intel_crtc,
  4526. &intel_crtc->config->fdi_m_n, NULL);
  4527. }
  4528. if (!transcoder_is_dsi(cpu_transcoder))
  4529. haswell_set_pipeconf(crtc);
  4530. haswell_set_pipemisc(crtc);
  4531. intel_color_set_csc(&pipe_config->base);
  4532. intel_crtc->active = true;
  4533. if (intel_crtc->config->has_pch_encoder)
  4534. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4535. else
  4536. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4537. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4538. if (intel_crtc->config->has_pch_encoder)
  4539. dev_priv->display.fdi_link_train(intel_crtc, pipe_config);
  4540. if (!transcoder_is_dsi(cpu_transcoder))
  4541. intel_ddi_enable_pipe_clock(pipe_config);
  4542. if (INTEL_GEN(dev_priv) >= 9)
  4543. skylake_pfit_enable(intel_crtc);
  4544. else
  4545. ironlake_pfit_enable(intel_crtc);
  4546. /*
  4547. * On ILK+ LUT must be loaded before the pipe is running but with
  4548. * clocks enabled
  4549. */
  4550. intel_color_load_luts(&pipe_config->base);
  4551. intel_ddi_set_pipe_settings(pipe_config);
  4552. if (!transcoder_is_dsi(cpu_transcoder))
  4553. intel_ddi_enable_transcoder_func(pipe_config);
  4554. if (dev_priv->display.initial_watermarks != NULL)
  4555. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4556. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4557. if (!transcoder_is_dsi(cpu_transcoder))
  4558. intel_enable_pipe(intel_crtc);
  4559. if (intel_crtc->config->has_pch_encoder)
  4560. lpt_pch_enable(pipe_config);
  4561. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4562. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4563. assert_vblank_disabled(crtc);
  4564. drm_crtc_vblank_on(crtc);
  4565. intel_encoders_enable(crtc, pipe_config, old_state);
  4566. if (intel_crtc->config->has_pch_encoder) {
  4567. intel_wait_for_vblank(dev_priv, pipe);
  4568. intel_wait_for_vblank(dev_priv, pipe);
  4569. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4570. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
  4571. }
  4572. /* If we change the relative order between pipe/planes enabling, we need
  4573. * to change the workaround. */
  4574. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4575. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4576. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4577. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4578. }
  4579. }
  4580. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4581. {
  4582. struct drm_device *dev = crtc->base.dev;
  4583. struct drm_i915_private *dev_priv = to_i915(dev);
  4584. int pipe = crtc->pipe;
  4585. /* To avoid upsetting the power well on haswell only disable the pfit if
  4586. * it's in use. The hw state code will make sure we get this right. */
  4587. if (force || crtc->config->pch_pfit.enabled) {
  4588. I915_WRITE(PF_CTL(pipe), 0);
  4589. I915_WRITE(PF_WIN_POS(pipe), 0);
  4590. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4591. }
  4592. }
  4593. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4594. struct drm_atomic_state *old_state)
  4595. {
  4596. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4597. struct drm_device *dev = crtc->dev;
  4598. struct drm_i915_private *dev_priv = to_i915(dev);
  4599. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4600. int pipe = intel_crtc->pipe;
  4601. /*
  4602. * Sometimes spurious CPU pipe underruns happen when the
  4603. * pipe is already disabled, but FDI RX/TX is still enabled.
  4604. * Happens at least with VGA+HDMI cloning. Suppress them.
  4605. */
  4606. if (intel_crtc->config->has_pch_encoder) {
  4607. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4608. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4609. }
  4610. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4611. drm_crtc_vblank_off(crtc);
  4612. assert_vblank_disabled(crtc);
  4613. intel_disable_pipe(intel_crtc);
  4614. ironlake_pfit_disable(intel_crtc, false);
  4615. if (intel_crtc->config->has_pch_encoder)
  4616. ironlake_fdi_disable(crtc);
  4617. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4618. if (intel_crtc->config->has_pch_encoder) {
  4619. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4620. if (HAS_PCH_CPT(dev_priv)) {
  4621. i915_reg_t reg;
  4622. u32 temp;
  4623. /* disable TRANS_DP_CTL */
  4624. reg = TRANS_DP_CTL(pipe);
  4625. temp = I915_READ(reg);
  4626. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4627. TRANS_DP_PORT_SEL_MASK);
  4628. temp |= TRANS_DP_PORT_SEL_NONE;
  4629. I915_WRITE(reg, temp);
  4630. /* disable DPLL_SEL */
  4631. temp = I915_READ(PCH_DPLL_SEL);
  4632. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4633. I915_WRITE(PCH_DPLL_SEL, temp);
  4634. }
  4635. ironlake_fdi_pll_disable(intel_crtc);
  4636. }
  4637. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4638. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4639. }
  4640. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4641. struct drm_atomic_state *old_state)
  4642. {
  4643. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4644. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4645. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4646. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4647. if (intel_crtc->config->has_pch_encoder)
  4648. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
  4649. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4650. drm_crtc_vblank_off(crtc);
  4651. assert_vblank_disabled(crtc);
  4652. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4653. if (!transcoder_is_dsi(cpu_transcoder))
  4654. intel_disable_pipe(intel_crtc);
  4655. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4656. intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
  4657. if (!transcoder_is_dsi(cpu_transcoder))
  4658. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4659. if (INTEL_GEN(dev_priv) >= 9)
  4660. skylake_scaler_disable(intel_crtc);
  4661. else
  4662. ironlake_pfit_disable(intel_crtc, false);
  4663. if (!transcoder_is_dsi(cpu_transcoder))
  4664. intel_ddi_disable_pipe_clock(intel_crtc->config);
  4665. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4666. if (old_crtc_state->has_pch_encoder)
  4667. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
  4668. }
  4669. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4670. {
  4671. struct drm_device *dev = crtc->base.dev;
  4672. struct drm_i915_private *dev_priv = to_i915(dev);
  4673. struct intel_crtc_state *pipe_config = crtc->config;
  4674. if (!pipe_config->gmch_pfit.control)
  4675. return;
  4676. /*
  4677. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4678. * according to register description and PRM.
  4679. */
  4680. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4681. assert_pipe_disabled(dev_priv, crtc->pipe);
  4682. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4683. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4684. /* Border color in case we don't scale up to the full screen. Black by
  4685. * default, change to something else for debugging. */
  4686. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4687. }
  4688. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4689. {
  4690. switch (port) {
  4691. case PORT_A:
  4692. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4693. case PORT_B:
  4694. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4695. case PORT_C:
  4696. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4697. case PORT_D:
  4698. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4699. case PORT_E:
  4700. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4701. default:
  4702. MISSING_CASE(port);
  4703. return POWER_DOMAIN_PORT_OTHER;
  4704. }
  4705. }
  4706. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4707. struct intel_crtc_state *crtc_state)
  4708. {
  4709. struct drm_device *dev = crtc->dev;
  4710. struct drm_i915_private *dev_priv = to_i915(dev);
  4711. struct drm_encoder *encoder;
  4712. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4713. enum pipe pipe = intel_crtc->pipe;
  4714. u64 mask;
  4715. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4716. if (!crtc_state->base.active)
  4717. return 0;
  4718. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4719. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4720. if (crtc_state->pch_pfit.enabled ||
  4721. crtc_state->pch_pfit.force_thru)
  4722. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4723. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4724. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4725. mask |= BIT_ULL(intel_encoder->power_domain);
  4726. }
  4727. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4728. mask |= BIT(POWER_DOMAIN_AUDIO);
  4729. if (crtc_state->shared_dpll)
  4730. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4731. return mask;
  4732. }
  4733. static u64
  4734. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4735. struct intel_crtc_state *crtc_state)
  4736. {
  4737. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4738. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4739. enum intel_display_power_domain domain;
  4740. u64 domains, new_domains, old_domains;
  4741. old_domains = intel_crtc->enabled_power_domains;
  4742. intel_crtc->enabled_power_domains = new_domains =
  4743. get_crtc_power_domains(crtc, crtc_state);
  4744. domains = new_domains & ~old_domains;
  4745. for_each_power_domain(domain, domains)
  4746. intel_display_power_get(dev_priv, domain);
  4747. return old_domains & ~new_domains;
  4748. }
  4749. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4750. u64 domains)
  4751. {
  4752. enum intel_display_power_domain domain;
  4753. for_each_power_domain(domain, domains)
  4754. intel_display_power_put(dev_priv, domain);
  4755. }
  4756. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  4757. struct drm_atomic_state *old_state)
  4758. {
  4759. struct intel_atomic_state *old_intel_state =
  4760. to_intel_atomic_state(old_state);
  4761. struct drm_crtc *crtc = pipe_config->base.crtc;
  4762. struct drm_device *dev = crtc->dev;
  4763. struct drm_i915_private *dev_priv = to_i915(dev);
  4764. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4765. int pipe = intel_crtc->pipe;
  4766. if (WARN_ON(intel_crtc->active))
  4767. return;
  4768. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4769. intel_dp_set_m_n(intel_crtc, M1_N1);
  4770. intel_set_pipe_timings(intel_crtc);
  4771. intel_set_pipe_src_size(intel_crtc);
  4772. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  4773. struct drm_i915_private *dev_priv = to_i915(dev);
  4774. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4775. I915_WRITE(CHV_CANVAS(pipe), 0);
  4776. }
  4777. i9xx_set_pipeconf(intel_crtc);
  4778. intel_crtc->active = true;
  4779. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4780. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4781. if (IS_CHERRYVIEW(dev_priv)) {
  4782. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4783. chv_enable_pll(intel_crtc, intel_crtc->config);
  4784. } else {
  4785. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4786. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4787. }
  4788. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4789. i9xx_pfit_enable(intel_crtc);
  4790. intel_color_load_luts(&pipe_config->base);
  4791. dev_priv->display.initial_watermarks(old_intel_state,
  4792. pipe_config);
  4793. intel_enable_pipe(intel_crtc);
  4794. assert_vblank_disabled(crtc);
  4795. drm_crtc_vblank_on(crtc);
  4796. intel_encoders_enable(crtc, pipe_config, old_state);
  4797. }
  4798. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4799. {
  4800. struct drm_device *dev = crtc->base.dev;
  4801. struct drm_i915_private *dev_priv = to_i915(dev);
  4802. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4803. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4804. }
  4805. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  4806. struct drm_atomic_state *old_state)
  4807. {
  4808. struct intel_atomic_state *old_intel_state =
  4809. to_intel_atomic_state(old_state);
  4810. struct drm_crtc *crtc = pipe_config->base.crtc;
  4811. struct drm_device *dev = crtc->dev;
  4812. struct drm_i915_private *dev_priv = to_i915(dev);
  4813. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4814. enum pipe pipe = intel_crtc->pipe;
  4815. if (WARN_ON(intel_crtc->active))
  4816. return;
  4817. i9xx_set_pll_dividers(intel_crtc);
  4818. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4819. intel_dp_set_m_n(intel_crtc, M1_N1);
  4820. intel_set_pipe_timings(intel_crtc);
  4821. intel_set_pipe_src_size(intel_crtc);
  4822. i9xx_set_pipeconf(intel_crtc);
  4823. intel_crtc->active = true;
  4824. if (!IS_GEN2(dev_priv))
  4825. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4826. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4827. i9xx_enable_pll(intel_crtc);
  4828. i9xx_pfit_enable(intel_crtc);
  4829. intel_color_load_luts(&pipe_config->base);
  4830. if (dev_priv->display.initial_watermarks != NULL)
  4831. dev_priv->display.initial_watermarks(old_intel_state,
  4832. intel_crtc->config);
  4833. else
  4834. intel_update_watermarks(intel_crtc);
  4835. intel_enable_pipe(intel_crtc);
  4836. assert_vblank_disabled(crtc);
  4837. drm_crtc_vblank_on(crtc);
  4838. intel_encoders_enable(crtc, pipe_config, old_state);
  4839. }
  4840. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4841. {
  4842. struct drm_device *dev = crtc->base.dev;
  4843. struct drm_i915_private *dev_priv = to_i915(dev);
  4844. if (!crtc->config->gmch_pfit.control)
  4845. return;
  4846. assert_pipe_disabled(dev_priv, crtc->pipe);
  4847. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4848. I915_READ(PFIT_CONTROL));
  4849. I915_WRITE(PFIT_CONTROL, 0);
  4850. }
  4851. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4852. struct drm_atomic_state *old_state)
  4853. {
  4854. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4855. struct drm_device *dev = crtc->dev;
  4856. struct drm_i915_private *dev_priv = to_i915(dev);
  4857. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4858. int pipe = intel_crtc->pipe;
  4859. /*
  4860. * On gen2 planes are double buffered but the pipe isn't, so we must
  4861. * wait for planes to fully turn off before disabling the pipe.
  4862. */
  4863. if (IS_GEN2(dev_priv))
  4864. intel_wait_for_vblank(dev_priv, pipe);
  4865. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4866. drm_crtc_vblank_off(crtc);
  4867. assert_vblank_disabled(crtc);
  4868. intel_disable_pipe(intel_crtc);
  4869. i9xx_pfit_disable(intel_crtc);
  4870. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4871. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  4872. if (IS_CHERRYVIEW(dev_priv))
  4873. chv_disable_pll(dev_priv, pipe);
  4874. else if (IS_VALLEYVIEW(dev_priv))
  4875. vlv_disable_pll(dev_priv, pipe);
  4876. else
  4877. i9xx_disable_pll(intel_crtc);
  4878. }
  4879. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  4880. if (!IS_GEN2(dev_priv))
  4881. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4882. if (!dev_priv->display.initial_watermarks)
  4883. intel_update_watermarks(intel_crtc);
  4884. /* clock the pipe down to 640x480@60 to potentially save power */
  4885. if (IS_I830(dev_priv))
  4886. i830_enable_pipe(dev_priv, pipe);
  4887. }
  4888. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
  4889. struct drm_modeset_acquire_ctx *ctx)
  4890. {
  4891. struct intel_encoder *encoder;
  4892. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4893. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4894. enum intel_display_power_domain domain;
  4895. u64 domains;
  4896. struct drm_atomic_state *state;
  4897. struct intel_crtc_state *crtc_state;
  4898. int ret;
  4899. if (!intel_crtc->active)
  4900. return;
  4901. if (crtc->primary->state->visible) {
  4902. WARN_ON(intel_crtc->flip_work);
  4903. intel_pre_disable_primary_noatomic(crtc);
  4904. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  4905. crtc->primary->state->visible = false;
  4906. }
  4907. state = drm_atomic_state_alloc(crtc->dev);
  4908. if (!state) {
  4909. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  4910. crtc->base.id, crtc->name);
  4911. return;
  4912. }
  4913. state->acquire_ctx = ctx;
  4914. /* Everything's already locked, -EDEADLK can't happen. */
  4915. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4916. ret = drm_atomic_add_affected_connectors(state, crtc);
  4917. WARN_ON(IS_ERR(crtc_state) || ret);
  4918. dev_priv->display.crtc_disable(crtc_state, state);
  4919. drm_atomic_state_put(state);
  4920. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  4921. crtc->base.id, crtc->name);
  4922. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  4923. crtc->state->active = false;
  4924. intel_crtc->active = false;
  4925. crtc->enabled = false;
  4926. crtc->state->connector_mask = 0;
  4927. crtc->state->encoder_mask = 0;
  4928. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  4929. encoder->base.crtc = NULL;
  4930. intel_fbc_disable(intel_crtc);
  4931. intel_update_watermarks(intel_crtc);
  4932. intel_disable_shared_dpll(intel_crtc);
  4933. domains = intel_crtc->enabled_power_domains;
  4934. for_each_power_domain(domain, domains)
  4935. intel_display_power_put(dev_priv, domain);
  4936. intel_crtc->enabled_power_domains = 0;
  4937. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  4938. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  4939. }
  4940. /*
  4941. * turn all crtc's off, but do not adjust state
  4942. * This has to be paired with a call to intel_modeset_setup_hw_state.
  4943. */
  4944. int intel_display_suspend(struct drm_device *dev)
  4945. {
  4946. struct drm_i915_private *dev_priv = to_i915(dev);
  4947. struct drm_atomic_state *state;
  4948. int ret;
  4949. state = drm_atomic_helper_suspend(dev);
  4950. ret = PTR_ERR_OR_ZERO(state);
  4951. if (ret)
  4952. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  4953. else
  4954. dev_priv->modeset_restore_state = state;
  4955. return ret;
  4956. }
  4957. void intel_encoder_destroy(struct drm_encoder *encoder)
  4958. {
  4959. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4960. drm_encoder_cleanup(encoder);
  4961. kfree(intel_encoder);
  4962. }
  4963. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4964. * internal consistency). */
  4965. static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
  4966. struct drm_connector_state *conn_state)
  4967. {
  4968. struct intel_connector *connector = to_intel_connector(conn_state->connector);
  4969. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4970. connector->base.base.id,
  4971. connector->base.name);
  4972. if (connector->get_hw_state(connector)) {
  4973. struct intel_encoder *encoder = connector->encoder;
  4974. I915_STATE_WARN(!crtc_state,
  4975. "connector enabled without attached crtc\n");
  4976. if (!crtc_state)
  4977. return;
  4978. I915_STATE_WARN(!crtc_state->active,
  4979. "connector is active, but attached crtc isn't\n");
  4980. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  4981. return;
  4982. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  4983. "atomic encoder doesn't match attached encoder\n");
  4984. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  4985. "attached encoder crtc differs from connector crtc\n");
  4986. } else {
  4987. I915_STATE_WARN(crtc_state && crtc_state->active,
  4988. "attached crtc is active, but connector isn't\n");
  4989. I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
  4990. "best encoder set without crtc!\n");
  4991. }
  4992. }
  4993. int intel_connector_init(struct intel_connector *connector)
  4994. {
  4995. struct intel_digital_connector_state *conn_state;
  4996. /*
  4997. * Allocate enough memory to hold intel_digital_connector_state,
  4998. * This might be a few bytes too many, but for connectors that don't
  4999. * need it we'll free the state and allocate a smaller one on the first
  5000. * succesful commit anyway.
  5001. */
  5002. conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
  5003. if (!conn_state)
  5004. return -ENOMEM;
  5005. __drm_atomic_helper_connector_reset(&connector->base,
  5006. &conn_state->base);
  5007. return 0;
  5008. }
  5009. struct intel_connector *intel_connector_alloc(void)
  5010. {
  5011. struct intel_connector *connector;
  5012. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5013. if (!connector)
  5014. return NULL;
  5015. if (intel_connector_init(connector) < 0) {
  5016. kfree(connector);
  5017. return NULL;
  5018. }
  5019. return connector;
  5020. }
  5021. /* Simple connector->get_hw_state implementation for encoders that support only
  5022. * one connector and no cloning and hence the encoder state determines the state
  5023. * of the connector. */
  5024. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5025. {
  5026. enum pipe pipe = 0;
  5027. struct intel_encoder *encoder = connector->encoder;
  5028. return encoder->get_hw_state(encoder, &pipe);
  5029. }
  5030. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5031. {
  5032. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5033. return crtc_state->fdi_lanes;
  5034. return 0;
  5035. }
  5036. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5037. struct intel_crtc_state *pipe_config)
  5038. {
  5039. struct drm_i915_private *dev_priv = to_i915(dev);
  5040. struct drm_atomic_state *state = pipe_config->base.state;
  5041. struct intel_crtc *other_crtc;
  5042. struct intel_crtc_state *other_crtc_state;
  5043. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5044. pipe_name(pipe), pipe_config->fdi_lanes);
  5045. if (pipe_config->fdi_lanes > 4) {
  5046. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5047. pipe_name(pipe), pipe_config->fdi_lanes);
  5048. return -EINVAL;
  5049. }
  5050. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5051. if (pipe_config->fdi_lanes > 2) {
  5052. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5053. pipe_config->fdi_lanes);
  5054. return -EINVAL;
  5055. } else {
  5056. return 0;
  5057. }
  5058. }
  5059. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5060. return 0;
  5061. /* Ivybridge 3 pipe is really complicated */
  5062. switch (pipe) {
  5063. case PIPE_A:
  5064. return 0;
  5065. case PIPE_B:
  5066. if (pipe_config->fdi_lanes <= 2)
  5067. return 0;
  5068. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5069. other_crtc_state =
  5070. intel_atomic_get_crtc_state(state, other_crtc);
  5071. if (IS_ERR(other_crtc_state))
  5072. return PTR_ERR(other_crtc_state);
  5073. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5074. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5075. pipe_name(pipe), pipe_config->fdi_lanes);
  5076. return -EINVAL;
  5077. }
  5078. return 0;
  5079. case PIPE_C:
  5080. if (pipe_config->fdi_lanes > 2) {
  5081. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5082. pipe_name(pipe), pipe_config->fdi_lanes);
  5083. return -EINVAL;
  5084. }
  5085. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5086. other_crtc_state =
  5087. intel_atomic_get_crtc_state(state, other_crtc);
  5088. if (IS_ERR(other_crtc_state))
  5089. return PTR_ERR(other_crtc_state);
  5090. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5091. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5092. return -EINVAL;
  5093. }
  5094. return 0;
  5095. default:
  5096. BUG();
  5097. }
  5098. }
  5099. #define RETRY 1
  5100. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5101. struct intel_crtc_state *pipe_config)
  5102. {
  5103. struct drm_device *dev = intel_crtc->base.dev;
  5104. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5105. int lane, link_bw, fdi_dotclock, ret;
  5106. bool needs_recompute = false;
  5107. retry:
  5108. /* FDI is a binary signal running at ~2.7GHz, encoding
  5109. * each output octet as 10 bits. The actual frequency
  5110. * is stored as a divider into a 100MHz clock, and the
  5111. * mode pixel clock is stored in units of 1KHz.
  5112. * Hence the bw of each lane in terms of the mode signal
  5113. * is:
  5114. */
  5115. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5116. fdi_dotclock = adjusted_mode->crtc_clock;
  5117. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5118. pipe_config->pipe_bpp);
  5119. pipe_config->fdi_lanes = lane;
  5120. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5121. link_bw, &pipe_config->fdi_m_n, false);
  5122. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5123. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5124. pipe_config->pipe_bpp -= 2*3;
  5125. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5126. pipe_config->pipe_bpp);
  5127. needs_recompute = true;
  5128. pipe_config->bw_constrained = true;
  5129. goto retry;
  5130. }
  5131. if (needs_recompute)
  5132. return RETRY;
  5133. return ret;
  5134. }
  5135. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5136. struct intel_crtc_state *pipe_config)
  5137. {
  5138. if (pipe_config->pipe_bpp > 24)
  5139. return false;
  5140. /* HSW can handle pixel rate up to cdclk? */
  5141. if (IS_HASWELL(dev_priv))
  5142. return true;
  5143. /*
  5144. * We compare against max which means we must take
  5145. * the increased cdclk requirement into account when
  5146. * calculating the new cdclk.
  5147. *
  5148. * Should measure whether using a lower cdclk w/o IPS
  5149. */
  5150. return pipe_config->pixel_rate <=
  5151. dev_priv->max_cdclk_freq * 95 / 100;
  5152. }
  5153. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5154. struct intel_crtc_state *pipe_config)
  5155. {
  5156. struct drm_device *dev = crtc->base.dev;
  5157. struct drm_i915_private *dev_priv = to_i915(dev);
  5158. pipe_config->ips_enabled = i915.enable_ips &&
  5159. hsw_crtc_supports_ips(crtc) &&
  5160. pipe_config_supports_ips(dev_priv, pipe_config);
  5161. }
  5162. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5163. {
  5164. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5165. /* GDG double wide on either pipe, otherwise pipe A only */
  5166. return INTEL_INFO(dev_priv)->gen < 4 &&
  5167. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5168. }
  5169. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5170. {
  5171. uint32_t pixel_rate;
  5172. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5173. /*
  5174. * We only use IF-ID interlacing. If we ever use
  5175. * PF-ID we'll need to adjust the pixel_rate here.
  5176. */
  5177. if (pipe_config->pch_pfit.enabled) {
  5178. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5179. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5180. pipe_w = pipe_config->pipe_src_w;
  5181. pipe_h = pipe_config->pipe_src_h;
  5182. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5183. pfit_h = pfit_size & 0xFFFF;
  5184. if (pipe_w < pfit_w)
  5185. pipe_w = pfit_w;
  5186. if (pipe_h < pfit_h)
  5187. pipe_h = pfit_h;
  5188. if (WARN_ON(!pfit_w || !pfit_h))
  5189. return pixel_rate;
  5190. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5191. pfit_w * pfit_h);
  5192. }
  5193. return pixel_rate;
  5194. }
  5195. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5196. {
  5197. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5198. if (HAS_GMCH_DISPLAY(dev_priv))
  5199. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5200. crtc_state->pixel_rate =
  5201. crtc_state->base.adjusted_mode.crtc_clock;
  5202. else
  5203. crtc_state->pixel_rate =
  5204. ilk_pipe_pixel_rate(crtc_state);
  5205. }
  5206. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5207. struct intel_crtc_state *pipe_config)
  5208. {
  5209. struct drm_device *dev = crtc->base.dev;
  5210. struct drm_i915_private *dev_priv = to_i915(dev);
  5211. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5212. int clock_limit = dev_priv->max_dotclk_freq;
  5213. if (INTEL_GEN(dev_priv) < 4) {
  5214. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5215. /*
  5216. * Enable double wide mode when the dot clock
  5217. * is > 90% of the (display) core speed.
  5218. */
  5219. if (intel_crtc_supports_double_wide(crtc) &&
  5220. adjusted_mode->crtc_clock > clock_limit) {
  5221. clock_limit = dev_priv->max_dotclk_freq;
  5222. pipe_config->double_wide = true;
  5223. }
  5224. }
  5225. if (adjusted_mode->crtc_clock > clock_limit) {
  5226. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5227. adjusted_mode->crtc_clock, clock_limit,
  5228. yesno(pipe_config->double_wide));
  5229. return -EINVAL;
  5230. }
  5231. /*
  5232. * Pipe horizontal size must be even in:
  5233. * - DVO ganged mode
  5234. * - LVDS dual channel mode
  5235. * - Double wide pipe
  5236. */
  5237. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5238. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5239. pipe_config->pipe_src_w &= ~1;
  5240. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5241. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5242. */
  5243. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5244. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5245. return -EINVAL;
  5246. intel_crtc_compute_pixel_rate(pipe_config);
  5247. if (HAS_IPS(dev_priv))
  5248. hsw_compute_ips_config(crtc, pipe_config);
  5249. if (pipe_config->has_pch_encoder)
  5250. return ironlake_fdi_compute_config(crtc, pipe_config);
  5251. return 0;
  5252. }
  5253. static void
  5254. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5255. {
  5256. while (*num > DATA_LINK_M_N_MASK ||
  5257. *den > DATA_LINK_M_N_MASK) {
  5258. *num >>= 1;
  5259. *den >>= 1;
  5260. }
  5261. }
  5262. static void compute_m_n(unsigned int m, unsigned int n,
  5263. uint32_t *ret_m, uint32_t *ret_n,
  5264. bool reduce_m_n)
  5265. {
  5266. /*
  5267. * Reduce M/N as much as possible without loss in precision. Several DP
  5268. * dongles in particular seem to be fussy about too large *link* M/N
  5269. * values. The passed in values are more likely to have the least
  5270. * significant bits zero than M after rounding below, so do this first.
  5271. */
  5272. if (reduce_m_n) {
  5273. while ((m & 1) == 0 && (n & 1) == 0) {
  5274. m >>= 1;
  5275. n >>= 1;
  5276. }
  5277. }
  5278. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5279. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5280. intel_reduce_m_n_ratio(ret_m, ret_n);
  5281. }
  5282. void
  5283. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5284. int pixel_clock, int link_clock,
  5285. struct intel_link_m_n *m_n,
  5286. bool reduce_m_n)
  5287. {
  5288. m_n->tu = 64;
  5289. compute_m_n(bits_per_pixel * pixel_clock,
  5290. link_clock * nlanes * 8,
  5291. &m_n->gmch_m, &m_n->gmch_n,
  5292. reduce_m_n);
  5293. compute_m_n(pixel_clock, link_clock,
  5294. &m_n->link_m, &m_n->link_n,
  5295. reduce_m_n);
  5296. }
  5297. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5298. {
  5299. if (i915.panel_use_ssc >= 0)
  5300. return i915.panel_use_ssc != 0;
  5301. return dev_priv->vbt.lvds_use_ssc
  5302. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5303. }
  5304. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5305. {
  5306. return (1 << dpll->n) << 16 | dpll->m2;
  5307. }
  5308. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5309. {
  5310. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5311. }
  5312. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5313. struct intel_crtc_state *crtc_state,
  5314. struct dpll *reduced_clock)
  5315. {
  5316. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5317. u32 fp, fp2 = 0;
  5318. if (IS_PINEVIEW(dev_priv)) {
  5319. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5320. if (reduced_clock)
  5321. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5322. } else {
  5323. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5324. if (reduced_clock)
  5325. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5326. }
  5327. crtc_state->dpll_hw_state.fp0 = fp;
  5328. crtc->lowfreq_avail = false;
  5329. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5330. reduced_clock) {
  5331. crtc_state->dpll_hw_state.fp1 = fp2;
  5332. crtc->lowfreq_avail = true;
  5333. } else {
  5334. crtc_state->dpll_hw_state.fp1 = fp;
  5335. }
  5336. }
  5337. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5338. pipe)
  5339. {
  5340. u32 reg_val;
  5341. /*
  5342. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5343. * and set it to a reasonable value instead.
  5344. */
  5345. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5346. reg_val &= 0xffffff00;
  5347. reg_val |= 0x00000030;
  5348. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5349. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5350. reg_val &= 0x00ffffff;
  5351. reg_val |= 0x8c000000;
  5352. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5353. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5354. reg_val &= 0xffffff00;
  5355. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5356. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5357. reg_val &= 0x00ffffff;
  5358. reg_val |= 0xb0000000;
  5359. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5360. }
  5361. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5362. struct intel_link_m_n *m_n)
  5363. {
  5364. struct drm_device *dev = crtc->base.dev;
  5365. struct drm_i915_private *dev_priv = to_i915(dev);
  5366. int pipe = crtc->pipe;
  5367. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5368. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5369. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5370. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5371. }
  5372. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5373. struct intel_link_m_n *m_n,
  5374. struct intel_link_m_n *m2_n2)
  5375. {
  5376. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5377. int pipe = crtc->pipe;
  5378. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5379. if (INTEL_GEN(dev_priv) >= 5) {
  5380. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5381. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5382. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5383. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5384. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5385. * for gen < 8) and if DRRS is supported (to make sure the
  5386. * registers are not unnecessarily accessed).
  5387. */
  5388. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5389. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5390. I915_WRITE(PIPE_DATA_M2(transcoder),
  5391. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5392. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5393. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5394. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5395. }
  5396. } else {
  5397. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5398. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5399. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5400. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5401. }
  5402. }
  5403. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5404. {
  5405. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5406. if (m_n == M1_N1) {
  5407. dp_m_n = &crtc->config->dp_m_n;
  5408. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5409. } else if (m_n == M2_N2) {
  5410. /*
  5411. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5412. * needs to be programmed into M1_N1.
  5413. */
  5414. dp_m_n = &crtc->config->dp_m2_n2;
  5415. } else {
  5416. DRM_ERROR("Unsupported divider value\n");
  5417. return;
  5418. }
  5419. if (crtc->config->has_pch_encoder)
  5420. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5421. else
  5422. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5423. }
  5424. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5425. struct intel_crtc_state *pipe_config)
  5426. {
  5427. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5428. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5429. if (crtc->pipe != PIPE_A)
  5430. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5431. /* DPLL not used with DSI, but still need the rest set up */
  5432. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5433. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5434. DPLL_EXT_BUFFER_ENABLE_VLV;
  5435. pipe_config->dpll_hw_state.dpll_md =
  5436. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5437. }
  5438. static void chv_compute_dpll(struct intel_crtc *crtc,
  5439. struct intel_crtc_state *pipe_config)
  5440. {
  5441. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5442. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5443. if (crtc->pipe != PIPE_A)
  5444. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5445. /* DPLL not used with DSI, but still need the rest set up */
  5446. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5447. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5448. pipe_config->dpll_hw_state.dpll_md =
  5449. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5450. }
  5451. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5452. const struct intel_crtc_state *pipe_config)
  5453. {
  5454. struct drm_device *dev = crtc->base.dev;
  5455. struct drm_i915_private *dev_priv = to_i915(dev);
  5456. enum pipe pipe = crtc->pipe;
  5457. u32 mdiv;
  5458. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5459. u32 coreclk, reg_val;
  5460. /* Enable Refclk */
  5461. I915_WRITE(DPLL(pipe),
  5462. pipe_config->dpll_hw_state.dpll &
  5463. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5464. /* No need to actually set up the DPLL with DSI */
  5465. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5466. return;
  5467. mutex_lock(&dev_priv->sb_lock);
  5468. bestn = pipe_config->dpll.n;
  5469. bestm1 = pipe_config->dpll.m1;
  5470. bestm2 = pipe_config->dpll.m2;
  5471. bestp1 = pipe_config->dpll.p1;
  5472. bestp2 = pipe_config->dpll.p2;
  5473. /* See eDP HDMI DPIO driver vbios notes doc */
  5474. /* PLL B needs special handling */
  5475. if (pipe == PIPE_B)
  5476. vlv_pllb_recal_opamp(dev_priv, pipe);
  5477. /* Set up Tx target for periodic Rcomp update */
  5478. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5479. /* Disable target IRef on PLL */
  5480. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5481. reg_val &= 0x00ffffff;
  5482. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5483. /* Disable fast lock */
  5484. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5485. /* Set idtafcrecal before PLL is enabled */
  5486. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5487. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5488. mdiv |= ((bestn << DPIO_N_SHIFT));
  5489. mdiv |= (1 << DPIO_K_SHIFT);
  5490. /*
  5491. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5492. * but we don't support that).
  5493. * Note: don't use the DAC post divider as it seems unstable.
  5494. */
  5495. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5496. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5497. mdiv |= DPIO_ENABLE_CALIBRATION;
  5498. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5499. /* Set HBR and RBR LPF coefficients */
  5500. if (pipe_config->port_clock == 162000 ||
  5501. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5502. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5503. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5504. 0x009f0003);
  5505. else
  5506. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5507. 0x00d0000f);
  5508. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5509. /* Use SSC source */
  5510. if (pipe == PIPE_A)
  5511. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5512. 0x0df40000);
  5513. else
  5514. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5515. 0x0df70000);
  5516. } else { /* HDMI or VGA */
  5517. /* Use bend source */
  5518. if (pipe == PIPE_A)
  5519. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5520. 0x0df70000);
  5521. else
  5522. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5523. 0x0df40000);
  5524. }
  5525. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5526. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5527. if (intel_crtc_has_dp_encoder(crtc->config))
  5528. coreclk |= 0x01000000;
  5529. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5530. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5531. mutex_unlock(&dev_priv->sb_lock);
  5532. }
  5533. static void chv_prepare_pll(struct intel_crtc *crtc,
  5534. const struct intel_crtc_state *pipe_config)
  5535. {
  5536. struct drm_device *dev = crtc->base.dev;
  5537. struct drm_i915_private *dev_priv = to_i915(dev);
  5538. enum pipe pipe = crtc->pipe;
  5539. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5540. u32 loopfilter, tribuf_calcntr;
  5541. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5542. u32 dpio_val;
  5543. int vco;
  5544. /* Enable Refclk and SSC */
  5545. I915_WRITE(DPLL(pipe),
  5546. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5547. /* No need to actually set up the DPLL with DSI */
  5548. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5549. return;
  5550. bestn = pipe_config->dpll.n;
  5551. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5552. bestm1 = pipe_config->dpll.m1;
  5553. bestm2 = pipe_config->dpll.m2 >> 22;
  5554. bestp1 = pipe_config->dpll.p1;
  5555. bestp2 = pipe_config->dpll.p2;
  5556. vco = pipe_config->dpll.vco;
  5557. dpio_val = 0;
  5558. loopfilter = 0;
  5559. mutex_lock(&dev_priv->sb_lock);
  5560. /* p1 and p2 divider */
  5561. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5562. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5563. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5564. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5565. 1 << DPIO_CHV_K_DIV_SHIFT);
  5566. /* Feedback post-divider - m2 */
  5567. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5568. /* Feedback refclk divider - n and m1 */
  5569. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5570. DPIO_CHV_M1_DIV_BY_2 |
  5571. 1 << DPIO_CHV_N_DIV_SHIFT);
  5572. /* M2 fraction division */
  5573. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5574. /* M2 fraction division enable */
  5575. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5576. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5577. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5578. if (bestm2_frac)
  5579. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5580. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5581. /* Program digital lock detect threshold */
  5582. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5583. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5584. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5585. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5586. if (!bestm2_frac)
  5587. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5588. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5589. /* Loop filter */
  5590. if (vco == 5400000) {
  5591. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5592. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5593. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5594. tribuf_calcntr = 0x9;
  5595. } else if (vco <= 6200000) {
  5596. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5597. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5598. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5599. tribuf_calcntr = 0x9;
  5600. } else if (vco <= 6480000) {
  5601. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5602. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5603. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5604. tribuf_calcntr = 0x8;
  5605. } else {
  5606. /* Not supported. Apply the same limits as in the max case */
  5607. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5608. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5609. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5610. tribuf_calcntr = 0;
  5611. }
  5612. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5613. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5614. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5615. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5616. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5617. /* AFC Recal */
  5618. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5619. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5620. DPIO_AFC_RECAL);
  5621. mutex_unlock(&dev_priv->sb_lock);
  5622. }
  5623. /**
  5624. * vlv_force_pll_on - forcibly enable just the PLL
  5625. * @dev_priv: i915 private structure
  5626. * @pipe: pipe PLL to enable
  5627. * @dpll: PLL configuration
  5628. *
  5629. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5630. * in cases where we need the PLL enabled even when @pipe is not going to
  5631. * be enabled.
  5632. */
  5633. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5634. const struct dpll *dpll)
  5635. {
  5636. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5637. struct intel_crtc_state *pipe_config;
  5638. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5639. if (!pipe_config)
  5640. return -ENOMEM;
  5641. pipe_config->base.crtc = &crtc->base;
  5642. pipe_config->pixel_multiplier = 1;
  5643. pipe_config->dpll = *dpll;
  5644. if (IS_CHERRYVIEW(dev_priv)) {
  5645. chv_compute_dpll(crtc, pipe_config);
  5646. chv_prepare_pll(crtc, pipe_config);
  5647. chv_enable_pll(crtc, pipe_config);
  5648. } else {
  5649. vlv_compute_dpll(crtc, pipe_config);
  5650. vlv_prepare_pll(crtc, pipe_config);
  5651. vlv_enable_pll(crtc, pipe_config);
  5652. }
  5653. kfree(pipe_config);
  5654. return 0;
  5655. }
  5656. /**
  5657. * vlv_force_pll_off - forcibly disable just the PLL
  5658. * @dev_priv: i915 private structure
  5659. * @pipe: pipe PLL to disable
  5660. *
  5661. * Disable the PLL for @pipe. To be used in cases where we need
  5662. * the PLL enabled even when @pipe is not going to be enabled.
  5663. */
  5664. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5665. {
  5666. if (IS_CHERRYVIEW(dev_priv))
  5667. chv_disable_pll(dev_priv, pipe);
  5668. else
  5669. vlv_disable_pll(dev_priv, pipe);
  5670. }
  5671. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5672. struct intel_crtc_state *crtc_state,
  5673. struct dpll *reduced_clock)
  5674. {
  5675. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5676. u32 dpll;
  5677. struct dpll *clock = &crtc_state->dpll;
  5678. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5679. dpll = DPLL_VGA_MODE_DIS;
  5680. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5681. dpll |= DPLLB_MODE_LVDS;
  5682. else
  5683. dpll |= DPLLB_MODE_DAC_SERIAL;
  5684. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5685. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5686. dpll |= (crtc_state->pixel_multiplier - 1)
  5687. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5688. }
  5689. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5690. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5691. dpll |= DPLL_SDVO_HIGH_SPEED;
  5692. if (intel_crtc_has_dp_encoder(crtc_state))
  5693. dpll |= DPLL_SDVO_HIGH_SPEED;
  5694. /* compute bitmask from p1 value */
  5695. if (IS_PINEVIEW(dev_priv))
  5696. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5697. else {
  5698. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5699. if (IS_G4X(dev_priv) && reduced_clock)
  5700. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5701. }
  5702. switch (clock->p2) {
  5703. case 5:
  5704. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5705. break;
  5706. case 7:
  5707. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5708. break;
  5709. case 10:
  5710. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5711. break;
  5712. case 14:
  5713. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5714. break;
  5715. }
  5716. if (INTEL_GEN(dev_priv) >= 4)
  5717. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5718. if (crtc_state->sdvo_tv_clock)
  5719. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5720. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5721. intel_panel_use_ssc(dev_priv))
  5722. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5723. else
  5724. dpll |= PLL_REF_INPUT_DREFCLK;
  5725. dpll |= DPLL_VCO_ENABLE;
  5726. crtc_state->dpll_hw_state.dpll = dpll;
  5727. if (INTEL_GEN(dev_priv) >= 4) {
  5728. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5729. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5730. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5731. }
  5732. }
  5733. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5734. struct intel_crtc_state *crtc_state,
  5735. struct dpll *reduced_clock)
  5736. {
  5737. struct drm_device *dev = crtc->base.dev;
  5738. struct drm_i915_private *dev_priv = to_i915(dev);
  5739. u32 dpll;
  5740. struct dpll *clock = &crtc_state->dpll;
  5741. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5742. dpll = DPLL_VGA_MODE_DIS;
  5743. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5744. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5745. } else {
  5746. if (clock->p1 == 2)
  5747. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5748. else
  5749. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5750. if (clock->p2 == 4)
  5751. dpll |= PLL_P2_DIVIDE_BY_4;
  5752. }
  5753. if (!IS_I830(dev_priv) &&
  5754. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5755. dpll |= DPLL_DVO_2X_MODE;
  5756. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5757. intel_panel_use_ssc(dev_priv))
  5758. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5759. else
  5760. dpll |= PLL_REF_INPUT_DREFCLK;
  5761. dpll |= DPLL_VCO_ENABLE;
  5762. crtc_state->dpll_hw_state.dpll = dpll;
  5763. }
  5764. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5765. {
  5766. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5767. enum pipe pipe = intel_crtc->pipe;
  5768. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5769. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5770. uint32_t crtc_vtotal, crtc_vblank_end;
  5771. int vsyncshift = 0;
  5772. /* We need to be careful not to changed the adjusted mode, for otherwise
  5773. * the hw state checker will get angry at the mismatch. */
  5774. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5775. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5776. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5777. /* the chip adds 2 halflines automatically */
  5778. crtc_vtotal -= 1;
  5779. crtc_vblank_end -= 1;
  5780. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5781. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5782. else
  5783. vsyncshift = adjusted_mode->crtc_hsync_start -
  5784. adjusted_mode->crtc_htotal / 2;
  5785. if (vsyncshift < 0)
  5786. vsyncshift += adjusted_mode->crtc_htotal;
  5787. }
  5788. if (INTEL_GEN(dev_priv) > 3)
  5789. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5790. I915_WRITE(HTOTAL(cpu_transcoder),
  5791. (adjusted_mode->crtc_hdisplay - 1) |
  5792. ((adjusted_mode->crtc_htotal - 1) << 16));
  5793. I915_WRITE(HBLANK(cpu_transcoder),
  5794. (adjusted_mode->crtc_hblank_start - 1) |
  5795. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5796. I915_WRITE(HSYNC(cpu_transcoder),
  5797. (adjusted_mode->crtc_hsync_start - 1) |
  5798. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5799. I915_WRITE(VTOTAL(cpu_transcoder),
  5800. (adjusted_mode->crtc_vdisplay - 1) |
  5801. ((crtc_vtotal - 1) << 16));
  5802. I915_WRITE(VBLANK(cpu_transcoder),
  5803. (adjusted_mode->crtc_vblank_start - 1) |
  5804. ((crtc_vblank_end - 1) << 16));
  5805. I915_WRITE(VSYNC(cpu_transcoder),
  5806. (adjusted_mode->crtc_vsync_start - 1) |
  5807. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5808. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5809. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5810. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5811. * bits. */
  5812. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  5813. (pipe == PIPE_B || pipe == PIPE_C))
  5814. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5815. }
  5816. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  5817. {
  5818. struct drm_device *dev = intel_crtc->base.dev;
  5819. struct drm_i915_private *dev_priv = to_i915(dev);
  5820. enum pipe pipe = intel_crtc->pipe;
  5821. /* pipesrc controls the size that is scaled from, which should
  5822. * always be the user's requested size.
  5823. */
  5824. I915_WRITE(PIPESRC(pipe),
  5825. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5826. (intel_crtc->config->pipe_src_h - 1));
  5827. }
  5828. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5829. struct intel_crtc_state *pipe_config)
  5830. {
  5831. struct drm_device *dev = crtc->base.dev;
  5832. struct drm_i915_private *dev_priv = to_i915(dev);
  5833. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5834. uint32_t tmp;
  5835. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5836. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5837. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5838. tmp = I915_READ(HBLANK(cpu_transcoder));
  5839. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5840. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5841. tmp = I915_READ(HSYNC(cpu_transcoder));
  5842. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5843. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5844. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5845. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5846. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5847. tmp = I915_READ(VBLANK(cpu_transcoder));
  5848. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5849. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5850. tmp = I915_READ(VSYNC(cpu_transcoder));
  5851. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5852. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5853. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5854. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5855. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5856. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5857. }
  5858. }
  5859. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  5860. struct intel_crtc_state *pipe_config)
  5861. {
  5862. struct drm_device *dev = crtc->base.dev;
  5863. struct drm_i915_private *dev_priv = to_i915(dev);
  5864. u32 tmp;
  5865. tmp = I915_READ(PIPESRC(crtc->pipe));
  5866. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5867. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5868. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5869. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5870. }
  5871. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5872. struct intel_crtc_state *pipe_config)
  5873. {
  5874. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5875. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5876. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5877. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5878. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5879. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5880. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5881. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5882. mode->flags = pipe_config->base.adjusted_mode.flags;
  5883. mode->type = DRM_MODE_TYPE_DRIVER;
  5884. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5885. mode->hsync = drm_mode_hsync(mode);
  5886. mode->vrefresh = drm_mode_vrefresh(mode);
  5887. drm_mode_set_name(mode);
  5888. }
  5889. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5890. {
  5891. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5892. uint32_t pipeconf;
  5893. pipeconf = 0;
  5894. /* we keep both pipes enabled on 830 */
  5895. if (IS_I830(dev_priv))
  5896. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5897. if (intel_crtc->config->double_wide)
  5898. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5899. /* only g4x and later have fancy bpc/dither controls */
  5900. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  5901. IS_CHERRYVIEW(dev_priv)) {
  5902. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5903. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5904. pipeconf |= PIPECONF_DITHER_EN |
  5905. PIPECONF_DITHER_TYPE_SP;
  5906. switch (intel_crtc->config->pipe_bpp) {
  5907. case 18:
  5908. pipeconf |= PIPECONF_6BPC;
  5909. break;
  5910. case 24:
  5911. pipeconf |= PIPECONF_8BPC;
  5912. break;
  5913. case 30:
  5914. pipeconf |= PIPECONF_10BPC;
  5915. break;
  5916. default:
  5917. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5918. BUG();
  5919. }
  5920. }
  5921. if (HAS_PIPE_CXSR(dev_priv)) {
  5922. if (intel_crtc->lowfreq_avail) {
  5923. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5924. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5925. } else {
  5926. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5927. }
  5928. }
  5929. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5930. if (INTEL_GEN(dev_priv) < 4 ||
  5931. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5932. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5933. else
  5934. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5935. } else
  5936. pipeconf |= PIPECONF_PROGRESSIVE;
  5937. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  5938. intel_crtc->config->limited_color_range)
  5939. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5940. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5941. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5942. }
  5943. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  5944. struct intel_crtc_state *crtc_state)
  5945. {
  5946. struct drm_device *dev = crtc->base.dev;
  5947. struct drm_i915_private *dev_priv = to_i915(dev);
  5948. const struct intel_limit *limit;
  5949. int refclk = 48000;
  5950. memset(&crtc_state->dpll_hw_state, 0,
  5951. sizeof(crtc_state->dpll_hw_state));
  5952. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5953. if (intel_panel_use_ssc(dev_priv)) {
  5954. refclk = dev_priv->vbt.lvds_ssc_freq;
  5955. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5956. }
  5957. limit = &intel_limits_i8xx_lvds;
  5958. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  5959. limit = &intel_limits_i8xx_dvo;
  5960. } else {
  5961. limit = &intel_limits_i8xx_dac;
  5962. }
  5963. if (!crtc_state->clock_set &&
  5964. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  5965. refclk, NULL, &crtc_state->dpll)) {
  5966. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5967. return -EINVAL;
  5968. }
  5969. i8xx_compute_dpll(crtc, crtc_state, NULL);
  5970. return 0;
  5971. }
  5972. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  5973. struct intel_crtc_state *crtc_state)
  5974. {
  5975. struct drm_device *dev = crtc->base.dev;
  5976. struct drm_i915_private *dev_priv = to_i915(dev);
  5977. const struct intel_limit *limit;
  5978. int refclk = 96000;
  5979. memset(&crtc_state->dpll_hw_state, 0,
  5980. sizeof(crtc_state->dpll_hw_state));
  5981. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5982. if (intel_panel_use_ssc(dev_priv)) {
  5983. refclk = dev_priv->vbt.lvds_ssc_freq;
  5984. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5985. }
  5986. if (intel_is_dual_link_lvds(dev))
  5987. limit = &intel_limits_g4x_dual_channel_lvds;
  5988. else
  5989. limit = &intel_limits_g4x_single_channel_lvds;
  5990. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  5991. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  5992. limit = &intel_limits_g4x_hdmi;
  5993. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  5994. limit = &intel_limits_g4x_sdvo;
  5995. } else {
  5996. /* The option is for other outputs */
  5997. limit = &intel_limits_i9xx_sdvo;
  5998. }
  5999. if (!crtc_state->clock_set &&
  6000. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6001. refclk, NULL, &crtc_state->dpll)) {
  6002. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6003. return -EINVAL;
  6004. }
  6005. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6006. return 0;
  6007. }
  6008. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6009. struct intel_crtc_state *crtc_state)
  6010. {
  6011. struct drm_device *dev = crtc->base.dev;
  6012. struct drm_i915_private *dev_priv = to_i915(dev);
  6013. const struct intel_limit *limit;
  6014. int refclk = 96000;
  6015. memset(&crtc_state->dpll_hw_state, 0,
  6016. sizeof(crtc_state->dpll_hw_state));
  6017. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6018. if (intel_panel_use_ssc(dev_priv)) {
  6019. refclk = dev_priv->vbt.lvds_ssc_freq;
  6020. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6021. }
  6022. limit = &intel_limits_pineview_lvds;
  6023. } else {
  6024. limit = &intel_limits_pineview_sdvo;
  6025. }
  6026. if (!crtc_state->clock_set &&
  6027. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6028. refclk, NULL, &crtc_state->dpll)) {
  6029. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6030. return -EINVAL;
  6031. }
  6032. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6033. return 0;
  6034. }
  6035. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6036. struct intel_crtc_state *crtc_state)
  6037. {
  6038. struct drm_device *dev = crtc->base.dev;
  6039. struct drm_i915_private *dev_priv = to_i915(dev);
  6040. const struct intel_limit *limit;
  6041. int refclk = 96000;
  6042. memset(&crtc_state->dpll_hw_state, 0,
  6043. sizeof(crtc_state->dpll_hw_state));
  6044. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6045. if (intel_panel_use_ssc(dev_priv)) {
  6046. refclk = dev_priv->vbt.lvds_ssc_freq;
  6047. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6048. }
  6049. limit = &intel_limits_i9xx_lvds;
  6050. } else {
  6051. limit = &intel_limits_i9xx_sdvo;
  6052. }
  6053. if (!crtc_state->clock_set &&
  6054. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6055. refclk, NULL, &crtc_state->dpll)) {
  6056. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6057. return -EINVAL;
  6058. }
  6059. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6060. return 0;
  6061. }
  6062. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6063. struct intel_crtc_state *crtc_state)
  6064. {
  6065. int refclk = 100000;
  6066. const struct intel_limit *limit = &intel_limits_chv;
  6067. memset(&crtc_state->dpll_hw_state, 0,
  6068. sizeof(crtc_state->dpll_hw_state));
  6069. if (!crtc_state->clock_set &&
  6070. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6071. refclk, NULL, &crtc_state->dpll)) {
  6072. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6073. return -EINVAL;
  6074. }
  6075. chv_compute_dpll(crtc, crtc_state);
  6076. return 0;
  6077. }
  6078. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6079. struct intel_crtc_state *crtc_state)
  6080. {
  6081. int refclk = 100000;
  6082. const struct intel_limit *limit = &intel_limits_vlv;
  6083. memset(&crtc_state->dpll_hw_state, 0,
  6084. sizeof(crtc_state->dpll_hw_state));
  6085. if (!crtc_state->clock_set &&
  6086. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6087. refclk, NULL, &crtc_state->dpll)) {
  6088. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6089. return -EINVAL;
  6090. }
  6091. vlv_compute_dpll(crtc, crtc_state);
  6092. return 0;
  6093. }
  6094. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6095. struct intel_crtc_state *pipe_config)
  6096. {
  6097. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6098. uint32_t tmp;
  6099. if (INTEL_GEN(dev_priv) <= 3 &&
  6100. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6101. return;
  6102. tmp = I915_READ(PFIT_CONTROL);
  6103. if (!(tmp & PFIT_ENABLE))
  6104. return;
  6105. /* Check whether the pfit is attached to our pipe. */
  6106. if (INTEL_GEN(dev_priv) < 4) {
  6107. if (crtc->pipe != PIPE_B)
  6108. return;
  6109. } else {
  6110. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6111. return;
  6112. }
  6113. pipe_config->gmch_pfit.control = tmp;
  6114. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6115. }
  6116. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6117. struct intel_crtc_state *pipe_config)
  6118. {
  6119. struct drm_device *dev = crtc->base.dev;
  6120. struct drm_i915_private *dev_priv = to_i915(dev);
  6121. int pipe = pipe_config->cpu_transcoder;
  6122. struct dpll clock;
  6123. u32 mdiv;
  6124. int refclk = 100000;
  6125. /* In case of DSI, DPLL will not be used */
  6126. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6127. return;
  6128. mutex_lock(&dev_priv->sb_lock);
  6129. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6130. mutex_unlock(&dev_priv->sb_lock);
  6131. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6132. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6133. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6134. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6135. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6136. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6137. }
  6138. static void
  6139. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6140. struct intel_initial_plane_config *plane_config)
  6141. {
  6142. struct drm_device *dev = crtc->base.dev;
  6143. struct drm_i915_private *dev_priv = to_i915(dev);
  6144. u32 val, base, offset;
  6145. int pipe = crtc->pipe, plane = crtc->plane;
  6146. int fourcc, pixel_format;
  6147. unsigned int aligned_height;
  6148. struct drm_framebuffer *fb;
  6149. struct intel_framebuffer *intel_fb;
  6150. val = I915_READ(DSPCNTR(plane));
  6151. if (!(val & DISPLAY_PLANE_ENABLE))
  6152. return;
  6153. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6154. if (!intel_fb) {
  6155. DRM_DEBUG_KMS("failed to alloc fb\n");
  6156. return;
  6157. }
  6158. fb = &intel_fb->base;
  6159. fb->dev = dev;
  6160. if (INTEL_GEN(dev_priv) >= 4) {
  6161. if (val & DISPPLANE_TILED) {
  6162. plane_config->tiling = I915_TILING_X;
  6163. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6164. }
  6165. }
  6166. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6167. fourcc = i9xx_format_to_fourcc(pixel_format);
  6168. fb->format = drm_format_info(fourcc);
  6169. if (INTEL_GEN(dev_priv) >= 4) {
  6170. if (plane_config->tiling)
  6171. offset = I915_READ(DSPTILEOFF(plane));
  6172. else
  6173. offset = I915_READ(DSPLINOFF(plane));
  6174. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6175. } else {
  6176. base = I915_READ(DSPADDR(plane));
  6177. }
  6178. plane_config->base = base;
  6179. val = I915_READ(PIPESRC(pipe));
  6180. fb->width = ((val >> 16) & 0xfff) + 1;
  6181. fb->height = ((val >> 0) & 0xfff) + 1;
  6182. val = I915_READ(DSPSTRIDE(pipe));
  6183. fb->pitches[0] = val & 0xffffffc0;
  6184. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6185. plane_config->size = fb->pitches[0] * aligned_height;
  6186. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6187. pipe_name(pipe), plane, fb->width, fb->height,
  6188. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6189. plane_config->size);
  6190. plane_config->fb = intel_fb;
  6191. }
  6192. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6193. struct intel_crtc_state *pipe_config)
  6194. {
  6195. struct drm_device *dev = crtc->base.dev;
  6196. struct drm_i915_private *dev_priv = to_i915(dev);
  6197. int pipe = pipe_config->cpu_transcoder;
  6198. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6199. struct dpll clock;
  6200. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6201. int refclk = 100000;
  6202. /* In case of DSI, DPLL will not be used */
  6203. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6204. return;
  6205. mutex_lock(&dev_priv->sb_lock);
  6206. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6207. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6208. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6209. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6210. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6211. mutex_unlock(&dev_priv->sb_lock);
  6212. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6213. clock.m2 = (pll_dw0 & 0xff) << 22;
  6214. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6215. clock.m2 |= pll_dw2 & 0x3fffff;
  6216. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6217. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6218. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6219. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6220. }
  6221. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6222. struct intel_crtc_state *pipe_config)
  6223. {
  6224. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6225. enum intel_display_power_domain power_domain;
  6226. uint32_t tmp;
  6227. bool ret;
  6228. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6229. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6230. return false;
  6231. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6232. pipe_config->shared_dpll = NULL;
  6233. ret = false;
  6234. tmp = I915_READ(PIPECONF(crtc->pipe));
  6235. if (!(tmp & PIPECONF_ENABLE))
  6236. goto out;
  6237. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6238. IS_CHERRYVIEW(dev_priv)) {
  6239. switch (tmp & PIPECONF_BPC_MASK) {
  6240. case PIPECONF_6BPC:
  6241. pipe_config->pipe_bpp = 18;
  6242. break;
  6243. case PIPECONF_8BPC:
  6244. pipe_config->pipe_bpp = 24;
  6245. break;
  6246. case PIPECONF_10BPC:
  6247. pipe_config->pipe_bpp = 30;
  6248. break;
  6249. default:
  6250. break;
  6251. }
  6252. }
  6253. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6254. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6255. pipe_config->limited_color_range = true;
  6256. if (INTEL_GEN(dev_priv) < 4)
  6257. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6258. intel_get_pipe_timings(crtc, pipe_config);
  6259. intel_get_pipe_src_size(crtc, pipe_config);
  6260. i9xx_get_pfit_config(crtc, pipe_config);
  6261. if (INTEL_GEN(dev_priv) >= 4) {
  6262. /* No way to read it out on pipes B and C */
  6263. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6264. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6265. else
  6266. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6267. pipe_config->pixel_multiplier =
  6268. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6269. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6270. pipe_config->dpll_hw_state.dpll_md = tmp;
  6271. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6272. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6273. tmp = I915_READ(DPLL(crtc->pipe));
  6274. pipe_config->pixel_multiplier =
  6275. ((tmp & SDVO_MULTIPLIER_MASK)
  6276. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6277. } else {
  6278. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6279. * port and will be fixed up in the encoder->get_config
  6280. * function. */
  6281. pipe_config->pixel_multiplier = 1;
  6282. }
  6283. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6284. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6285. /*
  6286. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6287. * on 830. Filter it out here so that we don't
  6288. * report errors due to that.
  6289. */
  6290. if (IS_I830(dev_priv))
  6291. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6292. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6293. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6294. } else {
  6295. /* Mask out read-only status bits. */
  6296. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6297. DPLL_PORTC_READY_MASK |
  6298. DPLL_PORTB_READY_MASK);
  6299. }
  6300. if (IS_CHERRYVIEW(dev_priv))
  6301. chv_crtc_clock_get(crtc, pipe_config);
  6302. else if (IS_VALLEYVIEW(dev_priv))
  6303. vlv_crtc_clock_get(crtc, pipe_config);
  6304. else
  6305. i9xx_crtc_clock_get(crtc, pipe_config);
  6306. /*
  6307. * Normally the dotclock is filled in by the encoder .get_config()
  6308. * but in case the pipe is enabled w/o any ports we need a sane
  6309. * default.
  6310. */
  6311. pipe_config->base.adjusted_mode.crtc_clock =
  6312. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6313. ret = true;
  6314. out:
  6315. intel_display_power_put(dev_priv, power_domain);
  6316. return ret;
  6317. }
  6318. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6319. {
  6320. struct intel_encoder *encoder;
  6321. int i;
  6322. u32 val, final;
  6323. bool has_lvds = false;
  6324. bool has_cpu_edp = false;
  6325. bool has_panel = false;
  6326. bool has_ck505 = false;
  6327. bool can_ssc = false;
  6328. bool using_ssc_source = false;
  6329. /* We need to take the global config into account */
  6330. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6331. switch (encoder->type) {
  6332. case INTEL_OUTPUT_LVDS:
  6333. has_panel = true;
  6334. has_lvds = true;
  6335. break;
  6336. case INTEL_OUTPUT_EDP:
  6337. has_panel = true;
  6338. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6339. has_cpu_edp = true;
  6340. break;
  6341. default:
  6342. break;
  6343. }
  6344. }
  6345. if (HAS_PCH_IBX(dev_priv)) {
  6346. has_ck505 = dev_priv->vbt.display_clock_mode;
  6347. can_ssc = has_ck505;
  6348. } else {
  6349. has_ck505 = false;
  6350. can_ssc = true;
  6351. }
  6352. /* Check if any DPLLs are using the SSC source */
  6353. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6354. u32 temp = I915_READ(PCH_DPLL(i));
  6355. if (!(temp & DPLL_VCO_ENABLE))
  6356. continue;
  6357. if ((temp & PLL_REF_INPUT_MASK) ==
  6358. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6359. using_ssc_source = true;
  6360. break;
  6361. }
  6362. }
  6363. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6364. has_panel, has_lvds, has_ck505, using_ssc_source);
  6365. /* Ironlake: try to setup display ref clock before DPLL
  6366. * enabling. This is only under driver's control after
  6367. * PCH B stepping, previous chipset stepping should be
  6368. * ignoring this setting.
  6369. */
  6370. val = I915_READ(PCH_DREF_CONTROL);
  6371. /* As we must carefully and slowly disable/enable each source in turn,
  6372. * compute the final state we want first and check if we need to
  6373. * make any changes at all.
  6374. */
  6375. final = val;
  6376. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6377. if (has_ck505)
  6378. final |= DREF_NONSPREAD_CK505_ENABLE;
  6379. else
  6380. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6381. final &= ~DREF_SSC_SOURCE_MASK;
  6382. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6383. final &= ~DREF_SSC1_ENABLE;
  6384. if (has_panel) {
  6385. final |= DREF_SSC_SOURCE_ENABLE;
  6386. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6387. final |= DREF_SSC1_ENABLE;
  6388. if (has_cpu_edp) {
  6389. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6390. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6391. else
  6392. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6393. } else
  6394. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6395. } else if (using_ssc_source) {
  6396. final |= DREF_SSC_SOURCE_ENABLE;
  6397. final |= DREF_SSC1_ENABLE;
  6398. }
  6399. if (final == val)
  6400. return;
  6401. /* Always enable nonspread source */
  6402. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6403. if (has_ck505)
  6404. val |= DREF_NONSPREAD_CK505_ENABLE;
  6405. else
  6406. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6407. if (has_panel) {
  6408. val &= ~DREF_SSC_SOURCE_MASK;
  6409. val |= DREF_SSC_SOURCE_ENABLE;
  6410. /* SSC must be turned on before enabling the CPU output */
  6411. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6412. DRM_DEBUG_KMS("Using SSC on panel\n");
  6413. val |= DREF_SSC1_ENABLE;
  6414. } else
  6415. val &= ~DREF_SSC1_ENABLE;
  6416. /* Get SSC going before enabling the outputs */
  6417. I915_WRITE(PCH_DREF_CONTROL, val);
  6418. POSTING_READ(PCH_DREF_CONTROL);
  6419. udelay(200);
  6420. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6421. /* Enable CPU source on CPU attached eDP */
  6422. if (has_cpu_edp) {
  6423. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6424. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6425. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6426. } else
  6427. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6428. } else
  6429. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6430. I915_WRITE(PCH_DREF_CONTROL, val);
  6431. POSTING_READ(PCH_DREF_CONTROL);
  6432. udelay(200);
  6433. } else {
  6434. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6435. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6436. /* Turn off CPU output */
  6437. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6438. I915_WRITE(PCH_DREF_CONTROL, val);
  6439. POSTING_READ(PCH_DREF_CONTROL);
  6440. udelay(200);
  6441. if (!using_ssc_source) {
  6442. DRM_DEBUG_KMS("Disabling SSC source\n");
  6443. /* Turn off the SSC source */
  6444. val &= ~DREF_SSC_SOURCE_MASK;
  6445. val |= DREF_SSC_SOURCE_DISABLE;
  6446. /* Turn off SSC1 */
  6447. val &= ~DREF_SSC1_ENABLE;
  6448. I915_WRITE(PCH_DREF_CONTROL, val);
  6449. POSTING_READ(PCH_DREF_CONTROL);
  6450. udelay(200);
  6451. }
  6452. }
  6453. BUG_ON(val != final);
  6454. }
  6455. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6456. {
  6457. uint32_t tmp;
  6458. tmp = I915_READ(SOUTH_CHICKEN2);
  6459. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6460. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6461. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6462. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6463. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6464. tmp = I915_READ(SOUTH_CHICKEN2);
  6465. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6466. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6467. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6468. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6469. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6470. }
  6471. /* WaMPhyProgramming:hsw */
  6472. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6473. {
  6474. uint32_t tmp;
  6475. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6476. tmp &= ~(0xFF << 24);
  6477. tmp |= (0x12 << 24);
  6478. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6479. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6480. tmp |= (1 << 11);
  6481. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6482. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6483. tmp |= (1 << 11);
  6484. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6485. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6486. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6487. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6488. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6489. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6490. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6491. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6492. tmp &= ~(7 << 13);
  6493. tmp |= (5 << 13);
  6494. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6495. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6496. tmp &= ~(7 << 13);
  6497. tmp |= (5 << 13);
  6498. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6499. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6500. tmp &= ~0xFF;
  6501. tmp |= 0x1C;
  6502. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6503. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6504. tmp &= ~0xFF;
  6505. tmp |= 0x1C;
  6506. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6507. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6508. tmp &= ~(0xFF << 16);
  6509. tmp |= (0x1C << 16);
  6510. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6511. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6512. tmp &= ~(0xFF << 16);
  6513. tmp |= (0x1C << 16);
  6514. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6515. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6516. tmp |= (1 << 27);
  6517. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6518. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6519. tmp |= (1 << 27);
  6520. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6521. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6522. tmp &= ~(0xF << 28);
  6523. tmp |= (4 << 28);
  6524. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6525. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6526. tmp &= ~(0xF << 28);
  6527. tmp |= (4 << 28);
  6528. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6529. }
  6530. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6531. * Programming" based on the parameters passed:
  6532. * - Sequence to enable CLKOUT_DP
  6533. * - Sequence to enable CLKOUT_DP without spread
  6534. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6535. */
  6536. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6537. bool with_spread, bool with_fdi)
  6538. {
  6539. uint32_t reg, tmp;
  6540. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6541. with_spread = true;
  6542. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6543. with_fdi, "LP PCH doesn't have FDI\n"))
  6544. with_fdi = false;
  6545. mutex_lock(&dev_priv->sb_lock);
  6546. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6547. tmp &= ~SBI_SSCCTL_DISABLE;
  6548. tmp |= SBI_SSCCTL_PATHALT;
  6549. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6550. udelay(24);
  6551. if (with_spread) {
  6552. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6553. tmp &= ~SBI_SSCCTL_PATHALT;
  6554. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6555. if (with_fdi) {
  6556. lpt_reset_fdi_mphy(dev_priv);
  6557. lpt_program_fdi_mphy(dev_priv);
  6558. }
  6559. }
  6560. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6561. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6562. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6563. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6564. mutex_unlock(&dev_priv->sb_lock);
  6565. }
  6566. /* Sequence to disable CLKOUT_DP */
  6567. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6568. {
  6569. uint32_t reg, tmp;
  6570. mutex_lock(&dev_priv->sb_lock);
  6571. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6572. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6573. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6574. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6575. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6576. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6577. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6578. tmp |= SBI_SSCCTL_PATHALT;
  6579. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6580. udelay(32);
  6581. }
  6582. tmp |= SBI_SSCCTL_DISABLE;
  6583. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6584. }
  6585. mutex_unlock(&dev_priv->sb_lock);
  6586. }
  6587. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6588. static const uint16_t sscdivintphase[] = {
  6589. [BEND_IDX( 50)] = 0x3B23,
  6590. [BEND_IDX( 45)] = 0x3B23,
  6591. [BEND_IDX( 40)] = 0x3C23,
  6592. [BEND_IDX( 35)] = 0x3C23,
  6593. [BEND_IDX( 30)] = 0x3D23,
  6594. [BEND_IDX( 25)] = 0x3D23,
  6595. [BEND_IDX( 20)] = 0x3E23,
  6596. [BEND_IDX( 15)] = 0x3E23,
  6597. [BEND_IDX( 10)] = 0x3F23,
  6598. [BEND_IDX( 5)] = 0x3F23,
  6599. [BEND_IDX( 0)] = 0x0025,
  6600. [BEND_IDX( -5)] = 0x0025,
  6601. [BEND_IDX(-10)] = 0x0125,
  6602. [BEND_IDX(-15)] = 0x0125,
  6603. [BEND_IDX(-20)] = 0x0225,
  6604. [BEND_IDX(-25)] = 0x0225,
  6605. [BEND_IDX(-30)] = 0x0325,
  6606. [BEND_IDX(-35)] = 0x0325,
  6607. [BEND_IDX(-40)] = 0x0425,
  6608. [BEND_IDX(-45)] = 0x0425,
  6609. [BEND_IDX(-50)] = 0x0525,
  6610. };
  6611. /*
  6612. * Bend CLKOUT_DP
  6613. * steps -50 to 50 inclusive, in steps of 5
  6614. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6615. * change in clock period = -(steps / 10) * 5.787 ps
  6616. */
  6617. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6618. {
  6619. uint32_t tmp;
  6620. int idx = BEND_IDX(steps);
  6621. if (WARN_ON(steps % 5 != 0))
  6622. return;
  6623. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6624. return;
  6625. mutex_lock(&dev_priv->sb_lock);
  6626. if (steps % 10 != 0)
  6627. tmp = 0xAAAAAAAB;
  6628. else
  6629. tmp = 0x00000000;
  6630. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6631. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6632. tmp &= 0xffff0000;
  6633. tmp |= sscdivintphase[idx];
  6634. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6635. mutex_unlock(&dev_priv->sb_lock);
  6636. }
  6637. #undef BEND_IDX
  6638. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6639. {
  6640. struct intel_encoder *encoder;
  6641. bool has_vga = false;
  6642. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6643. switch (encoder->type) {
  6644. case INTEL_OUTPUT_ANALOG:
  6645. has_vga = true;
  6646. break;
  6647. default:
  6648. break;
  6649. }
  6650. }
  6651. if (has_vga) {
  6652. lpt_bend_clkout_dp(dev_priv, 0);
  6653. lpt_enable_clkout_dp(dev_priv, true, true);
  6654. } else {
  6655. lpt_disable_clkout_dp(dev_priv);
  6656. }
  6657. }
  6658. /*
  6659. * Initialize reference clocks when the driver loads
  6660. */
  6661. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6662. {
  6663. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6664. ironlake_init_pch_refclk(dev_priv);
  6665. else if (HAS_PCH_LPT(dev_priv))
  6666. lpt_init_pch_refclk(dev_priv);
  6667. }
  6668. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6669. {
  6670. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6671. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6672. int pipe = intel_crtc->pipe;
  6673. uint32_t val;
  6674. val = 0;
  6675. switch (intel_crtc->config->pipe_bpp) {
  6676. case 18:
  6677. val |= PIPECONF_6BPC;
  6678. break;
  6679. case 24:
  6680. val |= PIPECONF_8BPC;
  6681. break;
  6682. case 30:
  6683. val |= PIPECONF_10BPC;
  6684. break;
  6685. case 36:
  6686. val |= PIPECONF_12BPC;
  6687. break;
  6688. default:
  6689. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6690. BUG();
  6691. }
  6692. if (intel_crtc->config->dither)
  6693. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6694. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6695. val |= PIPECONF_INTERLACED_ILK;
  6696. else
  6697. val |= PIPECONF_PROGRESSIVE;
  6698. if (intel_crtc->config->limited_color_range)
  6699. val |= PIPECONF_COLOR_RANGE_SELECT;
  6700. I915_WRITE(PIPECONF(pipe), val);
  6701. POSTING_READ(PIPECONF(pipe));
  6702. }
  6703. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6704. {
  6705. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6706. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6707. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6708. u32 val = 0;
  6709. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6710. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6711. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6712. val |= PIPECONF_INTERLACED_ILK;
  6713. else
  6714. val |= PIPECONF_PROGRESSIVE;
  6715. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6716. POSTING_READ(PIPECONF(cpu_transcoder));
  6717. }
  6718. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6719. {
  6720. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6721. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6722. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  6723. u32 val = 0;
  6724. switch (intel_crtc->config->pipe_bpp) {
  6725. case 18:
  6726. val |= PIPEMISC_DITHER_6_BPC;
  6727. break;
  6728. case 24:
  6729. val |= PIPEMISC_DITHER_8_BPC;
  6730. break;
  6731. case 30:
  6732. val |= PIPEMISC_DITHER_10_BPC;
  6733. break;
  6734. case 36:
  6735. val |= PIPEMISC_DITHER_12_BPC;
  6736. break;
  6737. default:
  6738. /* Case prevented by pipe_config_set_bpp. */
  6739. BUG();
  6740. }
  6741. if (intel_crtc->config->dither)
  6742. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6743. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6744. }
  6745. }
  6746. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6747. {
  6748. /*
  6749. * Account for spread spectrum to avoid
  6750. * oversubscribing the link. Max center spread
  6751. * is 2.5%; use 5% for safety's sake.
  6752. */
  6753. u32 bps = target_clock * bpp * 21 / 20;
  6754. return DIV_ROUND_UP(bps, link_bw * 8);
  6755. }
  6756. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6757. {
  6758. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6759. }
  6760. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6761. struct intel_crtc_state *crtc_state,
  6762. struct dpll *reduced_clock)
  6763. {
  6764. struct drm_crtc *crtc = &intel_crtc->base;
  6765. struct drm_device *dev = crtc->dev;
  6766. struct drm_i915_private *dev_priv = to_i915(dev);
  6767. u32 dpll, fp, fp2;
  6768. int factor;
  6769. /* Enable autotuning of the PLL clock (if permissible) */
  6770. factor = 21;
  6771. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6772. if ((intel_panel_use_ssc(dev_priv) &&
  6773. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6774. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6775. factor = 25;
  6776. } else if (crtc_state->sdvo_tv_clock)
  6777. factor = 20;
  6778. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6779. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6780. fp |= FP_CB_TUNE;
  6781. if (reduced_clock) {
  6782. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6783. if (reduced_clock->m < factor * reduced_clock->n)
  6784. fp2 |= FP_CB_TUNE;
  6785. } else {
  6786. fp2 = fp;
  6787. }
  6788. dpll = 0;
  6789. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6790. dpll |= DPLLB_MODE_LVDS;
  6791. else
  6792. dpll |= DPLLB_MODE_DAC_SERIAL;
  6793. dpll |= (crtc_state->pixel_multiplier - 1)
  6794. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6795. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6796. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6797. dpll |= DPLL_SDVO_HIGH_SPEED;
  6798. if (intel_crtc_has_dp_encoder(crtc_state))
  6799. dpll |= DPLL_SDVO_HIGH_SPEED;
  6800. /*
  6801. * The high speed IO clock is only really required for
  6802. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6803. * possible to share the DPLL between CRT and HDMI. Enabling
  6804. * the clock needlessly does no real harm, except use up a
  6805. * bit of power potentially.
  6806. *
  6807. * We'll limit this to IVB with 3 pipes, since it has only two
  6808. * DPLLs and so DPLL sharing is the only way to get three pipes
  6809. * driving PCH ports at the same time. On SNB we could do this,
  6810. * and potentially avoid enabling the second DPLL, but it's not
  6811. * clear if it''s a win or loss power wise. No point in doing
  6812. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  6813. */
  6814. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  6815. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  6816. dpll |= DPLL_SDVO_HIGH_SPEED;
  6817. /* compute bitmask from p1 value */
  6818. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6819. /* also FPA1 */
  6820. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6821. switch (crtc_state->dpll.p2) {
  6822. case 5:
  6823. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6824. break;
  6825. case 7:
  6826. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6827. break;
  6828. case 10:
  6829. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6830. break;
  6831. case 14:
  6832. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6833. break;
  6834. }
  6835. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6836. intel_panel_use_ssc(dev_priv))
  6837. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6838. else
  6839. dpll |= PLL_REF_INPUT_DREFCLK;
  6840. dpll |= DPLL_VCO_ENABLE;
  6841. crtc_state->dpll_hw_state.dpll = dpll;
  6842. crtc_state->dpll_hw_state.fp0 = fp;
  6843. crtc_state->dpll_hw_state.fp1 = fp2;
  6844. }
  6845. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6846. struct intel_crtc_state *crtc_state)
  6847. {
  6848. struct drm_device *dev = crtc->base.dev;
  6849. struct drm_i915_private *dev_priv = to_i915(dev);
  6850. const struct intel_limit *limit;
  6851. int refclk = 120000;
  6852. memset(&crtc_state->dpll_hw_state, 0,
  6853. sizeof(crtc_state->dpll_hw_state));
  6854. crtc->lowfreq_avail = false;
  6855. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6856. if (!crtc_state->has_pch_encoder)
  6857. return 0;
  6858. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6859. if (intel_panel_use_ssc(dev_priv)) {
  6860. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6861. dev_priv->vbt.lvds_ssc_freq);
  6862. refclk = dev_priv->vbt.lvds_ssc_freq;
  6863. }
  6864. if (intel_is_dual_link_lvds(dev)) {
  6865. if (refclk == 100000)
  6866. limit = &intel_limits_ironlake_dual_lvds_100m;
  6867. else
  6868. limit = &intel_limits_ironlake_dual_lvds;
  6869. } else {
  6870. if (refclk == 100000)
  6871. limit = &intel_limits_ironlake_single_lvds_100m;
  6872. else
  6873. limit = &intel_limits_ironlake_single_lvds;
  6874. }
  6875. } else {
  6876. limit = &intel_limits_ironlake_dac;
  6877. }
  6878. if (!crtc_state->clock_set &&
  6879. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6880. refclk, NULL, &crtc_state->dpll)) {
  6881. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6882. return -EINVAL;
  6883. }
  6884. ironlake_compute_dpll(crtc, crtc_state, NULL);
  6885. if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
  6886. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6887. pipe_name(crtc->pipe));
  6888. return -EINVAL;
  6889. }
  6890. return 0;
  6891. }
  6892. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6893. struct intel_link_m_n *m_n)
  6894. {
  6895. struct drm_device *dev = crtc->base.dev;
  6896. struct drm_i915_private *dev_priv = to_i915(dev);
  6897. enum pipe pipe = crtc->pipe;
  6898. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6899. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6900. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6901. & ~TU_SIZE_MASK;
  6902. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6903. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6904. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6905. }
  6906. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6907. enum transcoder transcoder,
  6908. struct intel_link_m_n *m_n,
  6909. struct intel_link_m_n *m2_n2)
  6910. {
  6911. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6912. enum pipe pipe = crtc->pipe;
  6913. if (INTEL_GEN(dev_priv) >= 5) {
  6914. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6915. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6916. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6917. & ~TU_SIZE_MASK;
  6918. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6919. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6920. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6921. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6922. * gen < 8) and if DRRS is supported (to make sure the
  6923. * registers are not unnecessarily read).
  6924. */
  6925. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  6926. crtc->config->has_drrs) {
  6927. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6928. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6929. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6930. & ~TU_SIZE_MASK;
  6931. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6932. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6933. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6934. }
  6935. } else {
  6936. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6937. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6938. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6939. & ~TU_SIZE_MASK;
  6940. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6941. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6942. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6943. }
  6944. }
  6945. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6946. struct intel_crtc_state *pipe_config)
  6947. {
  6948. if (pipe_config->has_pch_encoder)
  6949. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6950. else
  6951. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6952. &pipe_config->dp_m_n,
  6953. &pipe_config->dp_m2_n2);
  6954. }
  6955. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6956. struct intel_crtc_state *pipe_config)
  6957. {
  6958. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6959. &pipe_config->fdi_m_n, NULL);
  6960. }
  6961. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6962. struct intel_crtc_state *pipe_config)
  6963. {
  6964. struct drm_device *dev = crtc->base.dev;
  6965. struct drm_i915_private *dev_priv = to_i915(dev);
  6966. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  6967. uint32_t ps_ctrl = 0;
  6968. int id = -1;
  6969. int i;
  6970. /* find scaler attached to this pipe */
  6971. for (i = 0; i < crtc->num_scalers; i++) {
  6972. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  6973. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  6974. id = i;
  6975. pipe_config->pch_pfit.enabled = true;
  6976. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  6977. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  6978. break;
  6979. }
  6980. }
  6981. scaler_state->scaler_id = id;
  6982. if (id >= 0) {
  6983. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  6984. } else {
  6985. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  6986. }
  6987. }
  6988. static void
  6989. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  6990. struct intel_initial_plane_config *plane_config)
  6991. {
  6992. struct drm_device *dev = crtc->base.dev;
  6993. struct drm_i915_private *dev_priv = to_i915(dev);
  6994. u32 val, base, offset, stride_mult, tiling;
  6995. int pipe = crtc->pipe;
  6996. int fourcc, pixel_format;
  6997. unsigned int aligned_height;
  6998. struct drm_framebuffer *fb;
  6999. struct intel_framebuffer *intel_fb;
  7000. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7001. if (!intel_fb) {
  7002. DRM_DEBUG_KMS("failed to alloc fb\n");
  7003. return;
  7004. }
  7005. fb = &intel_fb->base;
  7006. fb->dev = dev;
  7007. val = I915_READ(PLANE_CTL(pipe, 0));
  7008. if (!(val & PLANE_CTL_ENABLE))
  7009. goto error;
  7010. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7011. fourcc = skl_format_to_fourcc(pixel_format,
  7012. val & PLANE_CTL_ORDER_RGBX,
  7013. val & PLANE_CTL_ALPHA_MASK);
  7014. fb->format = drm_format_info(fourcc);
  7015. tiling = val & PLANE_CTL_TILED_MASK;
  7016. switch (tiling) {
  7017. case PLANE_CTL_TILED_LINEAR:
  7018. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7019. break;
  7020. case PLANE_CTL_TILED_X:
  7021. plane_config->tiling = I915_TILING_X;
  7022. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7023. break;
  7024. case PLANE_CTL_TILED_Y:
  7025. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7026. break;
  7027. case PLANE_CTL_TILED_YF:
  7028. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7029. break;
  7030. default:
  7031. MISSING_CASE(tiling);
  7032. goto error;
  7033. }
  7034. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7035. plane_config->base = base;
  7036. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7037. val = I915_READ(PLANE_SIZE(pipe, 0));
  7038. fb->height = ((val >> 16) & 0xfff) + 1;
  7039. fb->width = ((val >> 0) & 0x1fff) + 1;
  7040. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7041. stride_mult = intel_fb_stride_alignment(fb, 0);
  7042. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7043. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7044. plane_config->size = fb->pitches[0] * aligned_height;
  7045. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7046. pipe_name(pipe), fb->width, fb->height,
  7047. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7048. plane_config->size);
  7049. plane_config->fb = intel_fb;
  7050. return;
  7051. error:
  7052. kfree(intel_fb);
  7053. }
  7054. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7055. struct intel_crtc_state *pipe_config)
  7056. {
  7057. struct drm_device *dev = crtc->base.dev;
  7058. struct drm_i915_private *dev_priv = to_i915(dev);
  7059. uint32_t tmp;
  7060. tmp = I915_READ(PF_CTL(crtc->pipe));
  7061. if (tmp & PF_ENABLE) {
  7062. pipe_config->pch_pfit.enabled = true;
  7063. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7064. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7065. /* We currently do not free assignements of panel fitters on
  7066. * ivb/hsw (since we don't use the higher upscaling modes which
  7067. * differentiates them) so just WARN about this case for now. */
  7068. if (IS_GEN7(dev_priv)) {
  7069. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7070. PF_PIPE_SEL_IVB(crtc->pipe));
  7071. }
  7072. }
  7073. }
  7074. static void
  7075. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7076. struct intel_initial_plane_config *plane_config)
  7077. {
  7078. struct drm_device *dev = crtc->base.dev;
  7079. struct drm_i915_private *dev_priv = to_i915(dev);
  7080. u32 val, base, offset;
  7081. int pipe = crtc->pipe;
  7082. int fourcc, pixel_format;
  7083. unsigned int aligned_height;
  7084. struct drm_framebuffer *fb;
  7085. struct intel_framebuffer *intel_fb;
  7086. val = I915_READ(DSPCNTR(pipe));
  7087. if (!(val & DISPLAY_PLANE_ENABLE))
  7088. return;
  7089. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7090. if (!intel_fb) {
  7091. DRM_DEBUG_KMS("failed to alloc fb\n");
  7092. return;
  7093. }
  7094. fb = &intel_fb->base;
  7095. fb->dev = dev;
  7096. if (INTEL_GEN(dev_priv) >= 4) {
  7097. if (val & DISPPLANE_TILED) {
  7098. plane_config->tiling = I915_TILING_X;
  7099. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7100. }
  7101. }
  7102. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7103. fourcc = i9xx_format_to_fourcc(pixel_format);
  7104. fb->format = drm_format_info(fourcc);
  7105. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7106. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  7107. offset = I915_READ(DSPOFFSET(pipe));
  7108. } else {
  7109. if (plane_config->tiling)
  7110. offset = I915_READ(DSPTILEOFF(pipe));
  7111. else
  7112. offset = I915_READ(DSPLINOFF(pipe));
  7113. }
  7114. plane_config->base = base;
  7115. val = I915_READ(PIPESRC(pipe));
  7116. fb->width = ((val >> 16) & 0xfff) + 1;
  7117. fb->height = ((val >> 0) & 0xfff) + 1;
  7118. val = I915_READ(DSPSTRIDE(pipe));
  7119. fb->pitches[0] = val & 0xffffffc0;
  7120. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7121. plane_config->size = fb->pitches[0] * aligned_height;
  7122. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7123. pipe_name(pipe), fb->width, fb->height,
  7124. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7125. plane_config->size);
  7126. plane_config->fb = intel_fb;
  7127. }
  7128. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7129. struct intel_crtc_state *pipe_config)
  7130. {
  7131. struct drm_device *dev = crtc->base.dev;
  7132. struct drm_i915_private *dev_priv = to_i915(dev);
  7133. enum intel_display_power_domain power_domain;
  7134. uint32_t tmp;
  7135. bool ret;
  7136. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7137. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7138. return false;
  7139. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7140. pipe_config->shared_dpll = NULL;
  7141. ret = false;
  7142. tmp = I915_READ(PIPECONF(crtc->pipe));
  7143. if (!(tmp & PIPECONF_ENABLE))
  7144. goto out;
  7145. switch (tmp & PIPECONF_BPC_MASK) {
  7146. case PIPECONF_6BPC:
  7147. pipe_config->pipe_bpp = 18;
  7148. break;
  7149. case PIPECONF_8BPC:
  7150. pipe_config->pipe_bpp = 24;
  7151. break;
  7152. case PIPECONF_10BPC:
  7153. pipe_config->pipe_bpp = 30;
  7154. break;
  7155. case PIPECONF_12BPC:
  7156. pipe_config->pipe_bpp = 36;
  7157. break;
  7158. default:
  7159. break;
  7160. }
  7161. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7162. pipe_config->limited_color_range = true;
  7163. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7164. struct intel_shared_dpll *pll;
  7165. enum intel_dpll_id pll_id;
  7166. pipe_config->has_pch_encoder = true;
  7167. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7168. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7169. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7170. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7171. if (HAS_PCH_IBX(dev_priv)) {
  7172. /*
  7173. * The pipe->pch transcoder and pch transcoder->pll
  7174. * mapping is fixed.
  7175. */
  7176. pll_id = (enum intel_dpll_id) crtc->pipe;
  7177. } else {
  7178. tmp = I915_READ(PCH_DPLL_SEL);
  7179. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7180. pll_id = DPLL_ID_PCH_PLL_B;
  7181. else
  7182. pll_id= DPLL_ID_PCH_PLL_A;
  7183. }
  7184. pipe_config->shared_dpll =
  7185. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7186. pll = pipe_config->shared_dpll;
  7187. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7188. &pipe_config->dpll_hw_state));
  7189. tmp = pipe_config->dpll_hw_state.dpll;
  7190. pipe_config->pixel_multiplier =
  7191. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7192. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7193. ironlake_pch_clock_get(crtc, pipe_config);
  7194. } else {
  7195. pipe_config->pixel_multiplier = 1;
  7196. }
  7197. intel_get_pipe_timings(crtc, pipe_config);
  7198. intel_get_pipe_src_size(crtc, pipe_config);
  7199. ironlake_get_pfit_config(crtc, pipe_config);
  7200. ret = true;
  7201. out:
  7202. intel_display_power_put(dev_priv, power_domain);
  7203. return ret;
  7204. }
  7205. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7206. {
  7207. struct drm_device *dev = &dev_priv->drm;
  7208. struct intel_crtc *crtc;
  7209. for_each_intel_crtc(dev, crtc)
  7210. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7211. pipe_name(crtc->pipe));
  7212. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7213. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7214. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7215. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7216. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7217. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7218. "CPU PWM1 enabled\n");
  7219. if (IS_HASWELL(dev_priv))
  7220. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7221. "CPU PWM2 enabled\n");
  7222. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7223. "PCH PWM1 enabled\n");
  7224. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7225. "Utility pin enabled\n");
  7226. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7227. /*
  7228. * In theory we can still leave IRQs enabled, as long as only the HPD
  7229. * interrupts remain enabled. We used to check for that, but since it's
  7230. * gen-specific and since we only disable LCPLL after we fully disable
  7231. * the interrupts, the check below should be enough.
  7232. */
  7233. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7234. }
  7235. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7236. {
  7237. if (IS_HASWELL(dev_priv))
  7238. return I915_READ(D_COMP_HSW);
  7239. else
  7240. return I915_READ(D_COMP_BDW);
  7241. }
  7242. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7243. {
  7244. if (IS_HASWELL(dev_priv)) {
  7245. mutex_lock(&dev_priv->rps.hw_lock);
  7246. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7247. val))
  7248. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7249. mutex_unlock(&dev_priv->rps.hw_lock);
  7250. } else {
  7251. I915_WRITE(D_COMP_BDW, val);
  7252. POSTING_READ(D_COMP_BDW);
  7253. }
  7254. }
  7255. /*
  7256. * This function implements pieces of two sequences from BSpec:
  7257. * - Sequence for display software to disable LCPLL
  7258. * - Sequence for display software to allow package C8+
  7259. * The steps implemented here are just the steps that actually touch the LCPLL
  7260. * register. Callers should take care of disabling all the display engine
  7261. * functions, doing the mode unset, fixing interrupts, etc.
  7262. */
  7263. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7264. bool switch_to_fclk, bool allow_power_down)
  7265. {
  7266. uint32_t val;
  7267. assert_can_disable_lcpll(dev_priv);
  7268. val = I915_READ(LCPLL_CTL);
  7269. if (switch_to_fclk) {
  7270. val |= LCPLL_CD_SOURCE_FCLK;
  7271. I915_WRITE(LCPLL_CTL, val);
  7272. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7273. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7274. DRM_ERROR("Switching to FCLK failed\n");
  7275. val = I915_READ(LCPLL_CTL);
  7276. }
  7277. val |= LCPLL_PLL_DISABLE;
  7278. I915_WRITE(LCPLL_CTL, val);
  7279. POSTING_READ(LCPLL_CTL);
  7280. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7281. DRM_ERROR("LCPLL still locked\n");
  7282. val = hsw_read_dcomp(dev_priv);
  7283. val |= D_COMP_COMP_DISABLE;
  7284. hsw_write_dcomp(dev_priv, val);
  7285. ndelay(100);
  7286. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7287. 1))
  7288. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7289. if (allow_power_down) {
  7290. val = I915_READ(LCPLL_CTL);
  7291. val |= LCPLL_POWER_DOWN_ALLOW;
  7292. I915_WRITE(LCPLL_CTL, val);
  7293. POSTING_READ(LCPLL_CTL);
  7294. }
  7295. }
  7296. /*
  7297. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7298. * source.
  7299. */
  7300. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7301. {
  7302. uint32_t val;
  7303. val = I915_READ(LCPLL_CTL);
  7304. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7305. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7306. return;
  7307. /*
  7308. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7309. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7310. */
  7311. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7312. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7313. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7314. I915_WRITE(LCPLL_CTL, val);
  7315. POSTING_READ(LCPLL_CTL);
  7316. }
  7317. val = hsw_read_dcomp(dev_priv);
  7318. val |= D_COMP_COMP_FORCE;
  7319. val &= ~D_COMP_COMP_DISABLE;
  7320. hsw_write_dcomp(dev_priv, val);
  7321. val = I915_READ(LCPLL_CTL);
  7322. val &= ~LCPLL_PLL_DISABLE;
  7323. I915_WRITE(LCPLL_CTL, val);
  7324. if (intel_wait_for_register(dev_priv,
  7325. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7326. 5))
  7327. DRM_ERROR("LCPLL not locked yet\n");
  7328. if (val & LCPLL_CD_SOURCE_FCLK) {
  7329. val = I915_READ(LCPLL_CTL);
  7330. val &= ~LCPLL_CD_SOURCE_FCLK;
  7331. I915_WRITE(LCPLL_CTL, val);
  7332. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7333. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7334. DRM_ERROR("Switching back to LCPLL failed\n");
  7335. }
  7336. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7337. intel_update_cdclk(dev_priv);
  7338. }
  7339. /*
  7340. * Package states C8 and deeper are really deep PC states that can only be
  7341. * reached when all the devices on the system allow it, so even if the graphics
  7342. * device allows PC8+, it doesn't mean the system will actually get to these
  7343. * states. Our driver only allows PC8+ when going into runtime PM.
  7344. *
  7345. * The requirements for PC8+ are that all the outputs are disabled, the power
  7346. * well is disabled and most interrupts are disabled, and these are also
  7347. * requirements for runtime PM. When these conditions are met, we manually do
  7348. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7349. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7350. * hang the machine.
  7351. *
  7352. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7353. * the state of some registers, so when we come back from PC8+ we need to
  7354. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7355. * need to take care of the registers kept by RC6. Notice that this happens even
  7356. * if we don't put the device in PCI D3 state (which is what currently happens
  7357. * because of the runtime PM support).
  7358. *
  7359. * For more, read "Display Sequences for Package C8" on the hardware
  7360. * documentation.
  7361. */
  7362. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7363. {
  7364. uint32_t val;
  7365. DRM_DEBUG_KMS("Enabling package C8+\n");
  7366. if (HAS_PCH_LPT_LP(dev_priv)) {
  7367. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7368. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7369. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7370. }
  7371. lpt_disable_clkout_dp(dev_priv);
  7372. hsw_disable_lcpll(dev_priv, true, true);
  7373. }
  7374. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7375. {
  7376. uint32_t val;
  7377. DRM_DEBUG_KMS("Disabling package C8+\n");
  7378. hsw_restore_lcpll(dev_priv);
  7379. lpt_init_pch_refclk(dev_priv);
  7380. if (HAS_PCH_LPT_LP(dev_priv)) {
  7381. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7382. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7383. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7384. }
  7385. }
  7386. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7387. struct intel_crtc_state *crtc_state)
  7388. {
  7389. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7390. struct intel_encoder *encoder =
  7391. intel_ddi_get_crtc_new_encoder(crtc_state);
  7392. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7393. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7394. pipe_name(crtc->pipe));
  7395. return -EINVAL;
  7396. }
  7397. }
  7398. crtc->lowfreq_avail = false;
  7399. return 0;
  7400. }
  7401. static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7402. enum port port,
  7403. struct intel_crtc_state *pipe_config)
  7404. {
  7405. enum intel_dpll_id id;
  7406. u32 temp;
  7407. temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7408. id = temp >> (port * 2);
  7409. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
  7410. return;
  7411. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7412. }
  7413. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7414. enum port port,
  7415. struct intel_crtc_state *pipe_config)
  7416. {
  7417. enum intel_dpll_id id;
  7418. switch (port) {
  7419. case PORT_A:
  7420. id = DPLL_ID_SKL_DPLL0;
  7421. break;
  7422. case PORT_B:
  7423. id = DPLL_ID_SKL_DPLL1;
  7424. break;
  7425. case PORT_C:
  7426. id = DPLL_ID_SKL_DPLL2;
  7427. break;
  7428. default:
  7429. DRM_ERROR("Incorrect port type\n");
  7430. return;
  7431. }
  7432. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7433. }
  7434. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7435. enum port port,
  7436. struct intel_crtc_state *pipe_config)
  7437. {
  7438. enum intel_dpll_id id;
  7439. u32 temp;
  7440. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7441. id = temp >> (port * 3 + 1);
  7442. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7443. return;
  7444. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7445. }
  7446. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7447. enum port port,
  7448. struct intel_crtc_state *pipe_config)
  7449. {
  7450. enum intel_dpll_id id;
  7451. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7452. switch (ddi_pll_sel) {
  7453. case PORT_CLK_SEL_WRPLL1:
  7454. id = DPLL_ID_WRPLL1;
  7455. break;
  7456. case PORT_CLK_SEL_WRPLL2:
  7457. id = DPLL_ID_WRPLL2;
  7458. break;
  7459. case PORT_CLK_SEL_SPLL:
  7460. id = DPLL_ID_SPLL;
  7461. break;
  7462. case PORT_CLK_SEL_LCPLL_810:
  7463. id = DPLL_ID_LCPLL_810;
  7464. break;
  7465. case PORT_CLK_SEL_LCPLL_1350:
  7466. id = DPLL_ID_LCPLL_1350;
  7467. break;
  7468. case PORT_CLK_SEL_LCPLL_2700:
  7469. id = DPLL_ID_LCPLL_2700;
  7470. break;
  7471. default:
  7472. MISSING_CASE(ddi_pll_sel);
  7473. /* fall through */
  7474. case PORT_CLK_SEL_NONE:
  7475. return;
  7476. }
  7477. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7478. }
  7479. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7480. struct intel_crtc_state *pipe_config,
  7481. u64 *power_domain_mask)
  7482. {
  7483. struct drm_device *dev = crtc->base.dev;
  7484. struct drm_i915_private *dev_priv = to_i915(dev);
  7485. enum intel_display_power_domain power_domain;
  7486. u32 tmp;
  7487. /*
  7488. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7489. * transcoder handled below.
  7490. */
  7491. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7492. /*
  7493. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7494. * consistency and less surprising code; it's in always on power).
  7495. */
  7496. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7497. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7498. enum pipe trans_edp_pipe;
  7499. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7500. default:
  7501. WARN(1, "unknown pipe linked to edp transcoder\n");
  7502. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7503. case TRANS_DDI_EDP_INPUT_A_ON:
  7504. trans_edp_pipe = PIPE_A;
  7505. break;
  7506. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7507. trans_edp_pipe = PIPE_B;
  7508. break;
  7509. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7510. trans_edp_pipe = PIPE_C;
  7511. break;
  7512. }
  7513. if (trans_edp_pipe == crtc->pipe)
  7514. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7515. }
  7516. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7517. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7518. return false;
  7519. *power_domain_mask |= BIT_ULL(power_domain);
  7520. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7521. return tmp & PIPECONF_ENABLE;
  7522. }
  7523. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7524. struct intel_crtc_state *pipe_config,
  7525. u64 *power_domain_mask)
  7526. {
  7527. struct drm_device *dev = crtc->base.dev;
  7528. struct drm_i915_private *dev_priv = to_i915(dev);
  7529. enum intel_display_power_domain power_domain;
  7530. enum port port;
  7531. enum transcoder cpu_transcoder;
  7532. u32 tmp;
  7533. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7534. if (port == PORT_A)
  7535. cpu_transcoder = TRANSCODER_DSI_A;
  7536. else
  7537. cpu_transcoder = TRANSCODER_DSI_C;
  7538. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7539. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7540. continue;
  7541. *power_domain_mask |= BIT_ULL(power_domain);
  7542. /*
  7543. * The PLL needs to be enabled with a valid divider
  7544. * configuration, otherwise accessing DSI registers will hang
  7545. * the machine. See BSpec North Display Engine
  7546. * registers/MIPI[BXT]. We can break out here early, since we
  7547. * need the same DSI PLL to be enabled for both DSI ports.
  7548. */
  7549. if (!intel_dsi_pll_is_enabled(dev_priv))
  7550. break;
  7551. /* XXX: this works for video mode only */
  7552. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7553. if (!(tmp & DPI_ENABLE))
  7554. continue;
  7555. tmp = I915_READ(MIPI_CTRL(port));
  7556. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7557. continue;
  7558. pipe_config->cpu_transcoder = cpu_transcoder;
  7559. break;
  7560. }
  7561. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7562. }
  7563. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7564. struct intel_crtc_state *pipe_config)
  7565. {
  7566. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7567. struct intel_shared_dpll *pll;
  7568. enum port port;
  7569. uint32_t tmp;
  7570. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7571. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7572. if (IS_CANNONLAKE(dev_priv))
  7573. cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
  7574. else if (IS_GEN9_BC(dev_priv))
  7575. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7576. else if (IS_GEN9_LP(dev_priv))
  7577. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7578. else
  7579. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7580. pll = pipe_config->shared_dpll;
  7581. if (pll) {
  7582. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7583. &pipe_config->dpll_hw_state));
  7584. }
  7585. /*
  7586. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7587. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7588. * the PCH transcoder is on.
  7589. */
  7590. if (INTEL_GEN(dev_priv) < 9 &&
  7591. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7592. pipe_config->has_pch_encoder = true;
  7593. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7594. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7595. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7596. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7597. }
  7598. }
  7599. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7600. struct intel_crtc_state *pipe_config)
  7601. {
  7602. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7603. enum intel_display_power_domain power_domain;
  7604. u64 power_domain_mask;
  7605. bool active;
  7606. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7607. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7608. return false;
  7609. power_domain_mask = BIT_ULL(power_domain);
  7610. pipe_config->shared_dpll = NULL;
  7611. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7612. if (IS_GEN9_LP(dev_priv) &&
  7613. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7614. WARN_ON(active);
  7615. active = true;
  7616. }
  7617. if (!active)
  7618. goto out;
  7619. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7620. haswell_get_ddi_port_state(crtc, pipe_config);
  7621. intel_get_pipe_timings(crtc, pipe_config);
  7622. }
  7623. intel_get_pipe_src_size(crtc, pipe_config);
  7624. pipe_config->gamma_mode =
  7625. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7626. if (INTEL_GEN(dev_priv) >= 9) {
  7627. intel_crtc_init_scalers(crtc, pipe_config);
  7628. pipe_config->scaler_state.scaler_id = -1;
  7629. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7630. }
  7631. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7632. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7633. power_domain_mask |= BIT_ULL(power_domain);
  7634. if (INTEL_GEN(dev_priv) >= 9)
  7635. skylake_get_pfit_config(crtc, pipe_config);
  7636. else
  7637. ironlake_get_pfit_config(crtc, pipe_config);
  7638. }
  7639. if (IS_HASWELL(dev_priv))
  7640. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7641. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7642. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7643. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7644. pipe_config->pixel_multiplier =
  7645. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7646. } else {
  7647. pipe_config->pixel_multiplier = 1;
  7648. }
  7649. out:
  7650. for_each_power_domain(power_domain, power_domain_mask)
  7651. intel_display_power_put(dev_priv, power_domain);
  7652. return active;
  7653. }
  7654. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  7655. {
  7656. struct drm_i915_private *dev_priv =
  7657. to_i915(plane_state->base.plane->dev);
  7658. const struct drm_framebuffer *fb = plane_state->base.fb;
  7659. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  7660. u32 base;
  7661. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  7662. base = obj->phys_handle->busaddr;
  7663. else
  7664. base = intel_plane_ggtt_offset(plane_state);
  7665. base += plane_state->main.offset;
  7666. /* ILK+ do this automagically */
  7667. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7668. plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7669. base += (plane_state->base.crtc_h *
  7670. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  7671. return base;
  7672. }
  7673. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  7674. {
  7675. int x = plane_state->base.crtc_x;
  7676. int y = plane_state->base.crtc_y;
  7677. u32 pos = 0;
  7678. if (x < 0) {
  7679. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7680. x = -x;
  7681. }
  7682. pos |= x << CURSOR_X_SHIFT;
  7683. if (y < 0) {
  7684. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7685. y = -y;
  7686. }
  7687. pos |= y << CURSOR_Y_SHIFT;
  7688. return pos;
  7689. }
  7690. static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
  7691. {
  7692. const struct drm_mode_config *config =
  7693. &plane_state->base.plane->dev->mode_config;
  7694. int width = plane_state->base.crtc_w;
  7695. int height = plane_state->base.crtc_h;
  7696. return width > 0 && width <= config->cursor_width &&
  7697. height > 0 && height <= config->cursor_height;
  7698. }
  7699. static int intel_check_cursor(struct intel_crtc_state *crtc_state,
  7700. struct intel_plane_state *plane_state)
  7701. {
  7702. const struct drm_framebuffer *fb = plane_state->base.fb;
  7703. int src_x, src_y;
  7704. u32 offset;
  7705. int ret;
  7706. ret = drm_plane_helper_check_state(&plane_state->base,
  7707. &plane_state->clip,
  7708. DRM_PLANE_HELPER_NO_SCALING,
  7709. DRM_PLANE_HELPER_NO_SCALING,
  7710. true, true);
  7711. if (ret)
  7712. return ret;
  7713. if (!fb)
  7714. return 0;
  7715. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  7716. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  7717. return -EINVAL;
  7718. }
  7719. src_x = plane_state->base.src_x >> 16;
  7720. src_y = plane_state->base.src_y >> 16;
  7721. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  7722. offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
  7723. if (src_x != 0 || src_y != 0) {
  7724. DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
  7725. return -EINVAL;
  7726. }
  7727. plane_state->main.offset = offset;
  7728. return 0;
  7729. }
  7730. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7731. const struct intel_plane_state *plane_state)
  7732. {
  7733. const struct drm_framebuffer *fb = plane_state->base.fb;
  7734. return CURSOR_ENABLE |
  7735. CURSOR_GAMMA_ENABLE |
  7736. CURSOR_FORMAT_ARGB |
  7737. CURSOR_STRIDE(fb->pitches[0]);
  7738. }
  7739. static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
  7740. {
  7741. int width = plane_state->base.crtc_w;
  7742. /*
  7743. * 845g/865g are only limited by the width of their cursors,
  7744. * the height is arbitrary up to the precision of the register.
  7745. */
  7746. return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
  7747. }
  7748. static int i845_check_cursor(struct intel_plane *plane,
  7749. struct intel_crtc_state *crtc_state,
  7750. struct intel_plane_state *plane_state)
  7751. {
  7752. const struct drm_framebuffer *fb = plane_state->base.fb;
  7753. int ret;
  7754. ret = intel_check_cursor(crtc_state, plane_state);
  7755. if (ret)
  7756. return ret;
  7757. /* if we want to turn off the cursor ignore width and height */
  7758. if (!fb)
  7759. return 0;
  7760. /* Check for which cursor types we support */
  7761. if (!i845_cursor_size_ok(plane_state)) {
  7762. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  7763. plane_state->base.crtc_w,
  7764. plane_state->base.crtc_h);
  7765. return -EINVAL;
  7766. }
  7767. switch (fb->pitches[0]) {
  7768. case 256:
  7769. case 512:
  7770. case 1024:
  7771. case 2048:
  7772. break;
  7773. default:
  7774. DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
  7775. fb->pitches[0]);
  7776. return -EINVAL;
  7777. }
  7778. plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
  7779. return 0;
  7780. }
  7781. static void i845_update_cursor(struct intel_plane *plane,
  7782. const struct intel_crtc_state *crtc_state,
  7783. const struct intel_plane_state *plane_state)
  7784. {
  7785. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7786. u32 cntl = 0, base = 0, pos = 0, size = 0;
  7787. unsigned long irqflags;
  7788. if (plane_state && plane_state->base.visible) {
  7789. unsigned int width = plane_state->base.crtc_w;
  7790. unsigned int height = plane_state->base.crtc_h;
  7791. cntl = plane_state->ctl;
  7792. size = (height << 12) | width;
  7793. base = intel_cursor_base(plane_state);
  7794. pos = intel_cursor_position(plane_state);
  7795. }
  7796. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7797. /* On these chipsets we can only modify the base/size/stride
  7798. * whilst the cursor is disabled.
  7799. */
  7800. if (plane->cursor.base != base ||
  7801. plane->cursor.size != size ||
  7802. plane->cursor.cntl != cntl) {
  7803. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  7804. I915_WRITE_FW(CURBASE(PIPE_A), base);
  7805. I915_WRITE_FW(CURSIZE, size);
  7806. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7807. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  7808. plane->cursor.base = base;
  7809. plane->cursor.size = size;
  7810. plane->cursor.cntl = cntl;
  7811. } else {
  7812. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7813. }
  7814. POSTING_READ_FW(CURCNTR(PIPE_A));
  7815. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7816. }
  7817. static void i845_disable_cursor(struct intel_plane *plane,
  7818. struct intel_crtc *crtc)
  7819. {
  7820. i845_update_cursor(plane, NULL, NULL);
  7821. }
  7822. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7823. const struct intel_plane_state *plane_state)
  7824. {
  7825. struct drm_i915_private *dev_priv =
  7826. to_i915(plane_state->base.plane->dev);
  7827. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  7828. u32 cntl;
  7829. cntl = MCURSOR_GAMMA_ENABLE;
  7830. if (HAS_DDI(dev_priv))
  7831. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7832. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  7833. switch (plane_state->base.crtc_w) {
  7834. case 64:
  7835. cntl |= CURSOR_MODE_64_ARGB_AX;
  7836. break;
  7837. case 128:
  7838. cntl |= CURSOR_MODE_128_ARGB_AX;
  7839. break;
  7840. case 256:
  7841. cntl |= CURSOR_MODE_256_ARGB_AX;
  7842. break;
  7843. default:
  7844. MISSING_CASE(plane_state->base.crtc_w);
  7845. return 0;
  7846. }
  7847. if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
  7848. cntl |= CURSOR_ROTATE_180;
  7849. return cntl;
  7850. }
  7851. static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
  7852. {
  7853. struct drm_i915_private *dev_priv =
  7854. to_i915(plane_state->base.plane->dev);
  7855. int width = plane_state->base.crtc_w;
  7856. int height = plane_state->base.crtc_h;
  7857. if (!intel_cursor_size_ok(plane_state))
  7858. return false;
  7859. /* Cursor width is limited to a few power-of-two sizes */
  7860. switch (width) {
  7861. case 256:
  7862. case 128:
  7863. case 64:
  7864. break;
  7865. default:
  7866. return false;
  7867. }
  7868. /*
  7869. * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
  7870. * height from 8 lines up to the cursor width, when the
  7871. * cursor is not rotated. Everything else requires square
  7872. * cursors.
  7873. */
  7874. if (HAS_CUR_FBC(dev_priv) &&
  7875. plane_state->base.rotation & DRM_MODE_ROTATE_0) {
  7876. if (height < 8 || height > width)
  7877. return false;
  7878. } else {
  7879. if (height != width)
  7880. return false;
  7881. }
  7882. return true;
  7883. }
  7884. static int i9xx_check_cursor(struct intel_plane *plane,
  7885. struct intel_crtc_state *crtc_state,
  7886. struct intel_plane_state *plane_state)
  7887. {
  7888. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7889. const struct drm_framebuffer *fb = plane_state->base.fb;
  7890. enum pipe pipe = plane->pipe;
  7891. int ret;
  7892. ret = intel_check_cursor(crtc_state, plane_state);
  7893. if (ret)
  7894. return ret;
  7895. /* if we want to turn off the cursor ignore width and height */
  7896. if (!fb)
  7897. return 0;
  7898. /* Check for which cursor types we support */
  7899. if (!i9xx_cursor_size_ok(plane_state)) {
  7900. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  7901. plane_state->base.crtc_w,
  7902. plane_state->base.crtc_h);
  7903. return -EINVAL;
  7904. }
  7905. if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
  7906. DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
  7907. fb->pitches[0], plane_state->base.crtc_w);
  7908. return -EINVAL;
  7909. }
  7910. /*
  7911. * There's something wrong with the cursor on CHV pipe C.
  7912. * If it straddles the left edge of the screen then
  7913. * moving it away from the edge or disabling it often
  7914. * results in a pipe underrun, and often that can lead to
  7915. * dead pipe (constant underrun reported, and it scans
  7916. * out just a solid color). To recover from that, the
  7917. * display power well must be turned off and on again.
  7918. * Refuse the put the cursor into that compromised position.
  7919. */
  7920. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  7921. plane_state->base.visible && plane_state->base.crtc_x < 0) {
  7922. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  7923. return -EINVAL;
  7924. }
  7925. plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
  7926. return 0;
  7927. }
  7928. static void i9xx_update_cursor(struct intel_plane *plane,
  7929. const struct intel_crtc_state *crtc_state,
  7930. const struct intel_plane_state *plane_state)
  7931. {
  7932. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7933. enum pipe pipe = plane->pipe;
  7934. u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
  7935. unsigned long irqflags;
  7936. if (plane_state && plane_state->base.visible) {
  7937. cntl = plane_state->ctl;
  7938. if (plane_state->base.crtc_h != plane_state->base.crtc_w)
  7939. fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
  7940. base = intel_cursor_base(plane_state);
  7941. pos = intel_cursor_position(plane_state);
  7942. }
  7943. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7944. /*
  7945. * On some platforms writing CURCNTR first will also
  7946. * cause CURPOS to be armed by the CURBASE write.
  7947. * Without the CURCNTR write the CURPOS write would
  7948. * arm itself. Thus we always start the full update
  7949. * with a CURCNTR write.
  7950. *
  7951. * On other platforms CURPOS always requires the
  7952. * CURBASE write to arm the update. Additonally
  7953. * a write to any of the cursor register will cancel
  7954. * an already armed cursor update. Thus leaving out
  7955. * the CURBASE write after CURPOS could lead to a
  7956. * cursor that doesn't appear to move, or even change
  7957. * shape. Thus we always write CURBASE.
  7958. *
  7959. * CURCNTR and CUR_FBC_CTL are always
  7960. * armed by the CURBASE write only.
  7961. */
  7962. if (plane->cursor.base != base ||
  7963. plane->cursor.size != fbc_ctl ||
  7964. plane->cursor.cntl != cntl) {
  7965. I915_WRITE_FW(CURCNTR(pipe), cntl);
  7966. if (HAS_CUR_FBC(dev_priv))
  7967. I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
  7968. I915_WRITE_FW(CURPOS(pipe), pos);
  7969. I915_WRITE_FW(CURBASE(pipe), base);
  7970. plane->cursor.base = base;
  7971. plane->cursor.size = fbc_ctl;
  7972. plane->cursor.cntl = cntl;
  7973. } else {
  7974. I915_WRITE_FW(CURPOS(pipe), pos);
  7975. I915_WRITE_FW(CURBASE(pipe), base);
  7976. }
  7977. POSTING_READ_FW(CURBASE(pipe));
  7978. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7979. }
  7980. static void i9xx_disable_cursor(struct intel_plane *plane,
  7981. struct intel_crtc *crtc)
  7982. {
  7983. i9xx_update_cursor(plane, NULL, NULL);
  7984. }
  7985. /* VESA 640x480x72Hz mode to set on the pipe */
  7986. static struct drm_display_mode load_detect_mode = {
  7987. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7988. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7989. };
  7990. struct drm_framebuffer *
  7991. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  7992. struct drm_mode_fb_cmd2 *mode_cmd)
  7993. {
  7994. struct intel_framebuffer *intel_fb;
  7995. int ret;
  7996. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7997. if (!intel_fb)
  7998. return ERR_PTR(-ENOMEM);
  7999. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  8000. if (ret)
  8001. goto err;
  8002. return &intel_fb->base;
  8003. err:
  8004. kfree(intel_fb);
  8005. return ERR_PTR(ret);
  8006. }
  8007. static u32
  8008. intel_framebuffer_pitch_for_width(int width, int bpp)
  8009. {
  8010. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8011. return ALIGN(pitch, 64);
  8012. }
  8013. static u32
  8014. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8015. {
  8016. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8017. return PAGE_ALIGN(pitch * mode->vdisplay);
  8018. }
  8019. static struct drm_framebuffer *
  8020. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8021. struct drm_display_mode *mode,
  8022. int depth, int bpp)
  8023. {
  8024. struct drm_framebuffer *fb;
  8025. struct drm_i915_gem_object *obj;
  8026. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8027. obj = i915_gem_object_create(to_i915(dev),
  8028. intel_framebuffer_size_for_mode(mode, bpp));
  8029. if (IS_ERR(obj))
  8030. return ERR_CAST(obj);
  8031. mode_cmd.width = mode->hdisplay;
  8032. mode_cmd.height = mode->vdisplay;
  8033. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8034. bpp);
  8035. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8036. fb = intel_framebuffer_create(obj, &mode_cmd);
  8037. if (IS_ERR(fb))
  8038. i915_gem_object_put(obj);
  8039. return fb;
  8040. }
  8041. static struct drm_framebuffer *
  8042. mode_fits_in_fbdev(struct drm_device *dev,
  8043. struct drm_display_mode *mode)
  8044. {
  8045. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8046. struct drm_i915_private *dev_priv = to_i915(dev);
  8047. struct drm_i915_gem_object *obj;
  8048. struct drm_framebuffer *fb;
  8049. if (!dev_priv->fbdev)
  8050. return NULL;
  8051. if (!dev_priv->fbdev->fb)
  8052. return NULL;
  8053. obj = dev_priv->fbdev->fb->obj;
  8054. BUG_ON(!obj);
  8055. fb = &dev_priv->fbdev->fb->base;
  8056. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8057. fb->format->cpp[0] * 8))
  8058. return NULL;
  8059. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8060. return NULL;
  8061. drm_framebuffer_reference(fb);
  8062. return fb;
  8063. #else
  8064. return NULL;
  8065. #endif
  8066. }
  8067. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8068. struct drm_crtc *crtc,
  8069. struct drm_display_mode *mode,
  8070. struct drm_framebuffer *fb,
  8071. int x, int y)
  8072. {
  8073. struct drm_plane_state *plane_state;
  8074. int hdisplay, vdisplay;
  8075. int ret;
  8076. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8077. if (IS_ERR(plane_state))
  8078. return PTR_ERR(plane_state);
  8079. if (mode)
  8080. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  8081. else
  8082. hdisplay = vdisplay = 0;
  8083. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8084. if (ret)
  8085. return ret;
  8086. drm_atomic_set_fb_for_plane(plane_state, fb);
  8087. plane_state->crtc_x = 0;
  8088. plane_state->crtc_y = 0;
  8089. plane_state->crtc_w = hdisplay;
  8090. plane_state->crtc_h = vdisplay;
  8091. plane_state->src_x = x << 16;
  8092. plane_state->src_y = y << 16;
  8093. plane_state->src_w = hdisplay << 16;
  8094. plane_state->src_h = vdisplay << 16;
  8095. return 0;
  8096. }
  8097. int intel_get_load_detect_pipe(struct drm_connector *connector,
  8098. struct drm_display_mode *mode,
  8099. struct intel_load_detect_pipe *old,
  8100. struct drm_modeset_acquire_ctx *ctx)
  8101. {
  8102. struct intel_crtc *intel_crtc;
  8103. struct intel_encoder *intel_encoder =
  8104. intel_attached_encoder(connector);
  8105. struct drm_crtc *possible_crtc;
  8106. struct drm_encoder *encoder = &intel_encoder->base;
  8107. struct drm_crtc *crtc = NULL;
  8108. struct drm_device *dev = encoder->dev;
  8109. struct drm_i915_private *dev_priv = to_i915(dev);
  8110. struct drm_framebuffer *fb;
  8111. struct drm_mode_config *config = &dev->mode_config;
  8112. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8113. struct drm_connector_state *connector_state;
  8114. struct intel_crtc_state *crtc_state;
  8115. int ret, i = -1;
  8116. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8117. connector->base.id, connector->name,
  8118. encoder->base.id, encoder->name);
  8119. old->restore_state = NULL;
  8120. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  8121. /*
  8122. * Algorithm gets a little messy:
  8123. *
  8124. * - if the connector already has an assigned crtc, use it (but make
  8125. * sure it's on first)
  8126. *
  8127. * - try to find the first unused crtc that can drive this connector,
  8128. * and use that if we find one
  8129. */
  8130. /* See if we already have a CRTC for this connector */
  8131. if (connector->state->crtc) {
  8132. crtc = connector->state->crtc;
  8133. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8134. if (ret)
  8135. goto fail;
  8136. /* Make sure the crtc and connector are running */
  8137. goto found;
  8138. }
  8139. /* Find an unused one (if possible) */
  8140. for_each_crtc(dev, possible_crtc) {
  8141. i++;
  8142. if (!(encoder->possible_crtcs & (1 << i)))
  8143. continue;
  8144. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8145. if (ret)
  8146. goto fail;
  8147. if (possible_crtc->state->enable) {
  8148. drm_modeset_unlock(&possible_crtc->mutex);
  8149. continue;
  8150. }
  8151. crtc = possible_crtc;
  8152. break;
  8153. }
  8154. /*
  8155. * If we didn't find an unused CRTC, don't use any.
  8156. */
  8157. if (!crtc) {
  8158. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8159. ret = -ENODEV;
  8160. goto fail;
  8161. }
  8162. found:
  8163. intel_crtc = to_intel_crtc(crtc);
  8164. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8165. if (ret)
  8166. goto fail;
  8167. state = drm_atomic_state_alloc(dev);
  8168. restore_state = drm_atomic_state_alloc(dev);
  8169. if (!state || !restore_state) {
  8170. ret = -ENOMEM;
  8171. goto fail;
  8172. }
  8173. state->acquire_ctx = ctx;
  8174. restore_state->acquire_ctx = ctx;
  8175. connector_state = drm_atomic_get_connector_state(state, connector);
  8176. if (IS_ERR(connector_state)) {
  8177. ret = PTR_ERR(connector_state);
  8178. goto fail;
  8179. }
  8180. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8181. if (ret)
  8182. goto fail;
  8183. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8184. if (IS_ERR(crtc_state)) {
  8185. ret = PTR_ERR(crtc_state);
  8186. goto fail;
  8187. }
  8188. crtc_state->base.active = crtc_state->base.enable = true;
  8189. if (!mode)
  8190. mode = &load_detect_mode;
  8191. /* We need a framebuffer large enough to accommodate all accesses
  8192. * that the plane may generate whilst we perform load detection.
  8193. * We can not rely on the fbcon either being present (we get called
  8194. * during its initialisation to detect all boot displays, or it may
  8195. * not even exist) or that it is large enough to satisfy the
  8196. * requested mode.
  8197. */
  8198. fb = mode_fits_in_fbdev(dev, mode);
  8199. if (fb == NULL) {
  8200. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8201. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8202. } else
  8203. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8204. if (IS_ERR(fb)) {
  8205. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8206. ret = PTR_ERR(fb);
  8207. goto fail;
  8208. }
  8209. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8210. if (ret)
  8211. goto fail;
  8212. drm_framebuffer_unreference(fb);
  8213. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8214. if (ret)
  8215. goto fail;
  8216. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8217. if (!ret)
  8218. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8219. if (!ret)
  8220. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8221. if (ret) {
  8222. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8223. goto fail;
  8224. }
  8225. ret = drm_atomic_commit(state);
  8226. if (ret) {
  8227. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8228. goto fail;
  8229. }
  8230. old->restore_state = restore_state;
  8231. drm_atomic_state_put(state);
  8232. /* let the connector get through one full cycle before testing */
  8233. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8234. return true;
  8235. fail:
  8236. if (state) {
  8237. drm_atomic_state_put(state);
  8238. state = NULL;
  8239. }
  8240. if (restore_state) {
  8241. drm_atomic_state_put(restore_state);
  8242. restore_state = NULL;
  8243. }
  8244. if (ret == -EDEADLK)
  8245. return ret;
  8246. return false;
  8247. }
  8248. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8249. struct intel_load_detect_pipe *old,
  8250. struct drm_modeset_acquire_ctx *ctx)
  8251. {
  8252. struct intel_encoder *intel_encoder =
  8253. intel_attached_encoder(connector);
  8254. struct drm_encoder *encoder = &intel_encoder->base;
  8255. struct drm_atomic_state *state = old->restore_state;
  8256. int ret;
  8257. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8258. connector->base.id, connector->name,
  8259. encoder->base.id, encoder->name);
  8260. if (!state)
  8261. return;
  8262. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8263. if (ret)
  8264. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8265. drm_atomic_state_put(state);
  8266. }
  8267. static int i9xx_pll_refclk(struct drm_device *dev,
  8268. const struct intel_crtc_state *pipe_config)
  8269. {
  8270. struct drm_i915_private *dev_priv = to_i915(dev);
  8271. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8272. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8273. return dev_priv->vbt.lvds_ssc_freq;
  8274. else if (HAS_PCH_SPLIT(dev_priv))
  8275. return 120000;
  8276. else if (!IS_GEN2(dev_priv))
  8277. return 96000;
  8278. else
  8279. return 48000;
  8280. }
  8281. /* Returns the clock of the currently programmed mode of the given pipe. */
  8282. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8283. struct intel_crtc_state *pipe_config)
  8284. {
  8285. struct drm_device *dev = crtc->base.dev;
  8286. struct drm_i915_private *dev_priv = to_i915(dev);
  8287. int pipe = pipe_config->cpu_transcoder;
  8288. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8289. u32 fp;
  8290. struct dpll clock;
  8291. int port_clock;
  8292. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8293. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8294. fp = pipe_config->dpll_hw_state.fp0;
  8295. else
  8296. fp = pipe_config->dpll_hw_state.fp1;
  8297. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8298. if (IS_PINEVIEW(dev_priv)) {
  8299. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8300. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8301. } else {
  8302. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8303. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8304. }
  8305. if (!IS_GEN2(dev_priv)) {
  8306. if (IS_PINEVIEW(dev_priv))
  8307. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8308. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8309. else
  8310. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8311. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8312. switch (dpll & DPLL_MODE_MASK) {
  8313. case DPLLB_MODE_DAC_SERIAL:
  8314. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8315. 5 : 10;
  8316. break;
  8317. case DPLLB_MODE_LVDS:
  8318. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8319. 7 : 14;
  8320. break;
  8321. default:
  8322. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8323. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8324. return;
  8325. }
  8326. if (IS_PINEVIEW(dev_priv))
  8327. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8328. else
  8329. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8330. } else {
  8331. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8332. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8333. if (is_lvds) {
  8334. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8335. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8336. if (lvds & LVDS_CLKB_POWER_UP)
  8337. clock.p2 = 7;
  8338. else
  8339. clock.p2 = 14;
  8340. } else {
  8341. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8342. clock.p1 = 2;
  8343. else {
  8344. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8345. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8346. }
  8347. if (dpll & PLL_P2_DIVIDE_BY_4)
  8348. clock.p2 = 4;
  8349. else
  8350. clock.p2 = 2;
  8351. }
  8352. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8353. }
  8354. /*
  8355. * This value includes pixel_multiplier. We will use
  8356. * port_clock to compute adjusted_mode.crtc_clock in the
  8357. * encoder's get_config() function.
  8358. */
  8359. pipe_config->port_clock = port_clock;
  8360. }
  8361. int intel_dotclock_calculate(int link_freq,
  8362. const struct intel_link_m_n *m_n)
  8363. {
  8364. /*
  8365. * The calculation for the data clock is:
  8366. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8367. * But we want to avoid losing precison if possible, so:
  8368. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8369. *
  8370. * and the link clock is simpler:
  8371. * link_clock = (m * link_clock) / n
  8372. */
  8373. if (!m_n->link_n)
  8374. return 0;
  8375. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8376. }
  8377. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8378. struct intel_crtc_state *pipe_config)
  8379. {
  8380. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8381. /* read out port_clock from the DPLL */
  8382. i9xx_crtc_clock_get(crtc, pipe_config);
  8383. /*
  8384. * In case there is an active pipe without active ports,
  8385. * we may need some idea for the dotclock anyway.
  8386. * Calculate one based on the FDI configuration.
  8387. */
  8388. pipe_config->base.adjusted_mode.crtc_clock =
  8389. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8390. &pipe_config->fdi_m_n);
  8391. }
  8392. /** Returns the currently programmed mode of the given pipe. */
  8393. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8394. struct drm_crtc *crtc)
  8395. {
  8396. struct drm_i915_private *dev_priv = to_i915(dev);
  8397. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8398. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8399. struct drm_display_mode *mode;
  8400. struct intel_crtc_state *pipe_config;
  8401. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8402. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8403. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8404. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8405. enum pipe pipe = intel_crtc->pipe;
  8406. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8407. if (!mode)
  8408. return NULL;
  8409. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8410. if (!pipe_config) {
  8411. kfree(mode);
  8412. return NULL;
  8413. }
  8414. /*
  8415. * Construct a pipe_config sufficient for getting the clock info
  8416. * back out of crtc_clock_get.
  8417. *
  8418. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8419. * to use a real value here instead.
  8420. */
  8421. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  8422. pipe_config->pixel_multiplier = 1;
  8423. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8424. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8425. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8426. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  8427. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  8428. mode->hdisplay = (htot & 0xffff) + 1;
  8429. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8430. mode->hsync_start = (hsync & 0xffff) + 1;
  8431. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8432. mode->vdisplay = (vtot & 0xffff) + 1;
  8433. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8434. mode->vsync_start = (vsync & 0xffff) + 1;
  8435. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8436. drm_mode_set_name(mode);
  8437. kfree(pipe_config);
  8438. return mode;
  8439. }
  8440. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8441. {
  8442. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8443. struct drm_device *dev = crtc->dev;
  8444. struct intel_flip_work *work;
  8445. spin_lock_irq(&dev->event_lock);
  8446. work = intel_crtc->flip_work;
  8447. intel_crtc->flip_work = NULL;
  8448. spin_unlock_irq(&dev->event_lock);
  8449. if (work) {
  8450. cancel_work_sync(&work->mmio_work);
  8451. cancel_work_sync(&work->unpin_work);
  8452. kfree(work);
  8453. }
  8454. drm_crtc_cleanup(crtc);
  8455. kfree(intel_crtc);
  8456. }
  8457. /* Is 'a' after or equal to 'b'? */
  8458. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8459. {
  8460. return !((a - b) & 0x80000000);
  8461. }
  8462. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  8463. struct intel_flip_work *work)
  8464. {
  8465. struct drm_device *dev = crtc->base.dev;
  8466. struct drm_i915_private *dev_priv = to_i915(dev);
  8467. if (abort_flip_on_reset(crtc))
  8468. return true;
  8469. /*
  8470. * The relevant registers doen't exist on pre-ctg.
  8471. * As the flip done interrupt doesn't trigger for mmio
  8472. * flips on gmch platforms, a flip count check isn't
  8473. * really needed there. But since ctg has the registers,
  8474. * include it in the check anyway.
  8475. */
  8476. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8477. return true;
  8478. /*
  8479. * BDW signals flip done immediately if the plane
  8480. * is disabled, even if the plane enable is already
  8481. * armed to occur at the next vblank :(
  8482. */
  8483. /*
  8484. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  8485. * used the same base address. In that case the mmio flip might
  8486. * have completed, but the CS hasn't even executed the flip yet.
  8487. *
  8488. * A flip count check isn't enough as the CS might have updated
  8489. * the base address just after start of vblank, but before we
  8490. * managed to process the interrupt. This means we'd complete the
  8491. * CS flip too soon.
  8492. *
  8493. * Combining both checks should get us a good enough result. It may
  8494. * still happen that the CS flip has been executed, but has not
  8495. * yet actually completed. But in case the base address is the same
  8496. * anyway, we don't really care.
  8497. */
  8498. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  8499. crtc->flip_work->gtt_offset &&
  8500. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  8501. crtc->flip_work->flip_count);
  8502. }
  8503. static bool
  8504. __pageflip_finished_mmio(struct intel_crtc *crtc,
  8505. struct intel_flip_work *work)
  8506. {
  8507. /*
  8508. * MMIO work completes when vblank is different from
  8509. * flip_queued_vblank.
  8510. *
  8511. * Reset counter value doesn't matter, this is handled by
  8512. * i915_wait_request finishing early, so no need to handle
  8513. * reset here.
  8514. */
  8515. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  8516. }
  8517. static bool pageflip_finished(struct intel_crtc *crtc,
  8518. struct intel_flip_work *work)
  8519. {
  8520. if (!atomic_read(&work->pending))
  8521. return false;
  8522. smp_rmb();
  8523. if (is_mmio_work(work))
  8524. return __pageflip_finished_mmio(crtc, work);
  8525. else
  8526. return __pageflip_finished_cs(crtc, work);
  8527. }
  8528. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  8529. {
  8530. struct drm_device *dev = &dev_priv->drm;
  8531. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8532. struct intel_flip_work *work;
  8533. unsigned long flags;
  8534. /* Ignore early vblank irqs */
  8535. if (!crtc)
  8536. return;
  8537. /*
  8538. * This is called both by irq handlers and the reset code (to complete
  8539. * lost pageflips) so needs the full irqsave spinlocks.
  8540. */
  8541. spin_lock_irqsave(&dev->event_lock, flags);
  8542. work = crtc->flip_work;
  8543. if (work != NULL &&
  8544. !is_mmio_work(work) &&
  8545. pageflip_finished(crtc, work))
  8546. page_flip_completed(crtc);
  8547. spin_unlock_irqrestore(&dev->event_lock, flags);
  8548. }
  8549. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  8550. {
  8551. struct drm_device *dev = &dev_priv->drm;
  8552. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8553. struct intel_flip_work *work;
  8554. unsigned long flags;
  8555. /* Ignore early vblank irqs */
  8556. if (!crtc)
  8557. return;
  8558. /*
  8559. * This is called both by irq handlers and the reset code (to complete
  8560. * lost pageflips) so needs the full irqsave spinlocks.
  8561. */
  8562. spin_lock_irqsave(&dev->event_lock, flags);
  8563. work = crtc->flip_work;
  8564. if (work != NULL &&
  8565. is_mmio_work(work) &&
  8566. pageflip_finished(crtc, work))
  8567. page_flip_completed(crtc);
  8568. spin_unlock_irqrestore(&dev->event_lock, flags);
  8569. }
  8570. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  8571. struct intel_flip_work *work)
  8572. {
  8573. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  8574. /* Ensure that the work item is consistent when activating it ... */
  8575. smp_mb__before_atomic();
  8576. atomic_set(&work->pending, 1);
  8577. }
  8578. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  8579. struct intel_crtc *intel_crtc,
  8580. struct intel_flip_work *work)
  8581. {
  8582. u32 addr, vblank;
  8583. if (!atomic_read(&work->pending))
  8584. return false;
  8585. smp_rmb();
  8586. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  8587. if (work->flip_ready_vblank == 0) {
  8588. if (work->flip_queued_req &&
  8589. !i915_gem_request_completed(work->flip_queued_req))
  8590. return false;
  8591. work->flip_ready_vblank = vblank;
  8592. }
  8593. if (vblank - work->flip_ready_vblank < 3)
  8594. return false;
  8595. /* Potential stall - if we see that the flip has happened,
  8596. * assume a missed interrupt. */
  8597. if (INTEL_GEN(dev_priv) >= 4)
  8598. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8599. else
  8600. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8601. /* There is a potential issue here with a false positive after a flip
  8602. * to the same address. We could address this by checking for a
  8603. * non-incrementing frame counter.
  8604. */
  8605. return addr == work->gtt_offset;
  8606. }
  8607. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  8608. {
  8609. struct drm_device *dev = &dev_priv->drm;
  8610. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8611. struct intel_flip_work *work;
  8612. WARN_ON(!in_interrupt());
  8613. if (crtc == NULL)
  8614. return;
  8615. spin_lock(&dev->event_lock);
  8616. work = crtc->flip_work;
  8617. if (work != NULL && !is_mmio_work(work) &&
  8618. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  8619. WARN_ONCE(1,
  8620. "Kicking stuck page flip: queued at %d, now %d\n",
  8621. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  8622. page_flip_completed(crtc);
  8623. work = NULL;
  8624. }
  8625. if (work != NULL && !is_mmio_work(work) &&
  8626. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  8627. intel_queue_rps_boost_for_request(work->flip_queued_req);
  8628. spin_unlock(&dev->event_lock);
  8629. }
  8630. /**
  8631. * intel_wm_need_update - Check whether watermarks need updating
  8632. * @plane: drm plane
  8633. * @state: new plane state
  8634. *
  8635. * Check current plane state versus the new one to determine whether
  8636. * watermarks need to be recalculated.
  8637. *
  8638. * Returns true or false.
  8639. */
  8640. static bool intel_wm_need_update(struct drm_plane *plane,
  8641. struct drm_plane_state *state)
  8642. {
  8643. struct intel_plane_state *new = to_intel_plane_state(state);
  8644. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8645. /* Update watermarks on tiling or size changes. */
  8646. if (new->base.visible != cur->base.visible)
  8647. return true;
  8648. if (!cur->base.fb || !new->base.fb)
  8649. return false;
  8650. if (cur->base.fb->modifier != new->base.fb->modifier ||
  8651. cur->base.rotation != new->base.rotation ||
  8652. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  8653. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  8654. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  8655. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  8656. return true;
  8657. return false;
  8658. }
  8659. static bool needs_scaling(struct intel_plane_state *state)
  8660. {
  8661. int src_w = drm_rect_width(&state->base.src) >> 16;
  8662. int src_h = drm_rect_height(&state->base.src) >> 16;
  8663. int dst_w = drm_rect_width(&state->base.dst);
  8664. int dst_h = drm_rect_height(&state->base.dst);
  8665. return (src_w != dst_w || src_h != dst_h);
  8666. }
  8667. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  8668. struct drm_plane_state *plane_state)
  8669. {
  8670. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  8671. struct drm_crtc *crtc = crtc_state->crtc;
  8672. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8673. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  8674. struct drm_device *dev = crtc->dev;
  8675. struct drm_i915_private *dev_priv = to_i915(dev);
  8676. struct intel_plane_state *old_plane_state =
  8677. to_intel_plane_state(plane->base.state);
  8678. bool mode_changed = needs_modeset(crtc_state);
  8679. bool was_crtc_enabled = crtc->state->active;
  8680. bool is_crtc_enabled = crtc_state->active;
  8681. bool turn_off, turn_on, visible, was_visible;
  8682. struct drm_framebuffer *fb = plane_state->fb;
  8683. int ret;
  8684. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  8685. ret = skl_update_scaler_plane(
  8686. to_intel_crtc_state(crtc_state),
  8687. to_intel_plane_state(plane_state));
  8688. if (ret)
  8689. return ret;
  8690. }
  8691. was_visible = old_plane_state->base.visible;
  8692. visible = plane_state->visible;
  8693. if (!was_crtc_enabled && WARN_ON(was_visible))
  8694. was_visible = false;
  8695. /*
  8696. * Visibility is calculated as if the crtc was on, but
  8697. * after scaler setup everything depends on it being off
  8698. * when the crtc isn't active.
  8699. *
  8700. * FIXME this is wrong for watermarks. Watermarks should also
  8701. * be computed as if the pipe would be active. Perhaps move
  8702. * per-plane wm computation to the .check_plane() hook, and
  8703. * only combine the results from all planes in the current place?
  8704. */
  8705. if (!is_crtc_enabled) {
  8706. plane_state->visible = visible = false;
  8707. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  8708. }
  8709. if (!was_visible && !visible)
  8710. return 0;
  8711. if (fb != old_plane_state->base.fb)
  8712. pipe_config->fb_changed = true;
  8713. turn_off = was_visible && (!visible || mode_changed);
  8714. turn_on = visible && (!was_visible || mode_changed);
  8715. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  8716. intel_crtc->base.base.id, intel_crtc->base.name,
  8717. plane->base.base.id, plane->base.name,
  8718. fb ? fb->base.id : -1);
  8719. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  8720. plane->base.base.id, plane->base.name,
  8721. was_visible, visible,
  8722. turn_off, turn_on, mode_changed);
  8723. if (turn_on) {
  8724. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8725. pipe_config->update_wm_pre = true;
  8726. /* must disable cxsr around plane enable/disable */
  8727. if (plane->id != PLANE_CURSOR)
  8728. pipe_config->disable_cxsr = true;
  8729. } else if (turn_off) {
  8730. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8731. pipe_config->update_wm_post = true;
  8732. /* must disable cxsr around plane enable/disable */
  8733. if (plane->id != PLANE_CURSOR)
  8734. pipe_config->disable_cxsr = true;
  8735. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  8736. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  8737. /* FIXME bollocks */
  8738. pipe_config->update_wm_pre = true;
  8739. pipe_config->update_wm_post = true;
  8740. }
  8741. }
  8742. if (visible || was_visible)
  8743. pipe_config->fb_bits |= plane->frontbuffer_bit;
  8744. /*
  8745. * WaCxSRDisabledForSpriteScaling:ivb
  8746. *
  8747. * cstate->update_wm was already set above, so this flag will
  8748. * take effect when we commit and program watermarks.
  8749. */
  8750. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  8751. needs_scaling(to_intel_plane_state(plane_state)) &&
  8752. !needs_scaling(old_plane_state))
  8753. pipe_config->disable_lp_wm = true;
  8754. return 0;
  8755. }
  8756. static bool encoders_cloneable(const struct intel_encoder *a,
  8757. const struct intel_encoder *b)
  8758. {
  8759. /* masks could be asymmetric, so check both ways */
  8760. return a == b || (a->cloneable & (1 << b->type) &&
  8761. b->cloneable & (1 << a->type));
  8762. }
  8763. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  8764. struct intel_crtc *crtc,
  8765. struct intel_encoder *encoder)
  8766. {
  8767. struct intel_encoder *source_encoder;
  8768. struct drm_connector *connector;
  8769. struct drm_connector_state *connector_state;
  8770. int i;
  8771. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8772. if (connector_state->crtc != &crtc->base)
  8773. continue;
  8774. source_encoder =
  8775. to_intel_encoder(connector_state->best_encoder);
  8776. if (!encoders_cloneable(encoder, source_encoder))
  8777. return false;
  8778. }
  8779. return true;
  8780. }
  8781. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  8782. struct drm_crtc_state *crtc_state)
  8783. {
  8784. struct drm_device *dev = crtc->dev;
  8785. struct drm_i915_private *dev_priv = to_i915(dev);
  8786. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8787. struct intel_crtc_state *pipe_config =
  8788. to_intel_crtc_state(crtc_state);
  8789. struct drm_atomic_state *state = crtc_state->state;
  8790. int ret;
  8791. bool mode_changed = needs_modeset(crtc_state);
  8792. if (mode_changed && !crtc_state->active)
  8793. pipe_config->update_wm_post = true;
  8794. if (mode_changed && crtc_state->enable &&
  8795. dev_priv->display.crtc_compute_clock &&
  8796. !WARN_ON(pipe_config->shared_dpll)) {
  8797. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  8798. pipe_config);
  8799. if (ret)
  8800. return ret;
  8801. }
  8802. if (crtc_state->color_mgmt_changed) {
  8803. ret = intel_color_check(crtc, crtc_state);
  8804. if (ret)
  8805. return ret;
  8806. /*
  8807. * Changing color management on Intel hardware is
  8808. * handled as part of planes update.
  8809. */
  8810. crtc_state->planes_changed = true;
  8811. }
  8812. ret = 0;
  8813. if (dev_priv->display.compute_pipe_wm) {
  8814. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  8815. if (ret) {
  8816. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  8817. return ret;
  8818. }
  8819. }
  8820. if (dev_priv->display.compute_intermediate_wm &&
  8821. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  8822. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  8823. return 0;
  8824. /*
  8825. * Calculate 'intermediate' watermarks that satisfy both the
  8826. * old state and the new state. We can program these
  8827. * immediately.
  8828. */
  8829. ret = dev_priv->display.compute_intermediate_wm(dev,
  8830. intel_crtc,
  8831. pipe_config);
  8832. if (ret) {
  8833. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  8834. return ret;
  8835. }
  8836. } else if (dev_priv->display.compute_intermediate_wm) {
  8837. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  8838. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  8839. }
  8840. if (INTEL_GEN(dev_priv) >= 9) {
  8841. if (mode_changed)
  8842. ret = skl_update_scaler_crtc(pipe_config);
  8843. if (!ret)
  8844. ret = skl_check_pipe_max_pixel_rate(intel_crtc,
  8845. pipe_config);
  8846. if (!ret)
  8847. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  8848. pipe_config);
  8849. }
  8850. return ret;
  8851. }
  8852. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  8853. .atomic_begin = intel_begin_crtc_commit,
  8854. .atomic_flush = intel_finish_crtc_commit,
  8855. .atomic_check = intel_crtc_atomic_check,
  8856. };
  8857. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  8858. {
  8859. struct intel_connector *connector;
  8860. struct drm_connector_list_iter conn_iter;
  8861. drm_connector_list_iter_begin(dev, &conn_iter);
  8862. for_each_intel_connector_iter(connector, &conn_iter) {
  8863. if (connector->base.state->crtc)
  8864. drm_connector_unreference(&connector->base);
  8865. if (connector->base.encoder) {
  8866. connector->base.state->best_encoder =
  8867. connector->base.encoder;
  8868. connector->base.state->crtc =
  8869. connector->base.encoder->crtc;
  8870. drm_connector_reference(&connector->base);
  8871. } else {
  8872. connector->base.state->best_encoder = NULL;
  8873. connector->base.state->crtc = NULL;
  8874. }
  8875. }
  8876. drm_connector_list_iter_end(&conn_iter);
  8877. }
  8878. static void
  8879. connected_sink_compute_bpp(struct intel_connector *connector,
  8880. struct intel_crtc_state *pipe_config)
  8881. {
  8882. const struct drm_display_info *info = &connector->base.display_info;
  8883. int bpp = pipe_config->pipe_bpp;
  8884. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8885. connector->base.base.id,
  8886. connector->base.name);
  8887. /* Don't use an invalid EDID bpc value */
  8888. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  8889. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8890. bpp, info->bpc * 3);
  8891. pipe_config->pipe_bpp = info->bpc * 3;
  8892. }
  8893. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8894. if (info->bpc == 0 && bpp > 24) {
  8895. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8896. bpp);
  8897. pipe_config->pipe_bpp = 24;
  8898. }
  8899. }
  8900. static int
  8901. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8902. struct intel_crtc_state *pipe_config)
  8903. {
  8904. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8905. struct drm_atomic_state *state;
  8906. struct drm_connector *connector;
  8907. struct drm_connector_state *connector_state;
  8908. int bpp, i;
  8909. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  8910. IS_CHERRYVIEW(dev_priv)))
  8911. bpp = 10*3;
  8912. else if (INTEL_GEN(dev_priv) >= 5)
  8913. bpp = 12*3;
  8914. else
  8915. bpp = 8*3;
  8916. pipe_config->pipe_bpp = bpp;
  8917. state = pipe_config->base.state;
  8918. /* Clamp display bpp to EDID value */
  8919. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8920. if (connector_state->crtc != &crtc->base)
  8921. continue;
  8922. connected_sink_compute_bpp(to_intel_connector(connector),
  8923. pipe_config);
  8924. }
  8925. return bpp;
  8926. }
  8927. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8928. {
  8929. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8930. "type: 0x%x flags: 0x%x\n",
  8931. mode->crtc_clock,
  8932. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8933. mode->crtc_hsync_end, mode->crtc_htotal,
  8934. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8935. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8936. }
  8937. static inline void
  8938. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  8939. unsigned int lane_count, struct intel_link_m_n *m_n)
  8940. {
  8941. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8942. id, lane_count,
  8943. m_n->gmch_m, m_n->gmch_n,
  8944. m_n->link_m, m_n->link_n, m_n->tu);
  8945. }
  8946. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8947. struct intel_crtc_state *pipe_config,
  8948. const char *context)
  8949. {
  8950. struct drm_device *dev = crtc->base.dev;
  8951. struct drm_i915_private *dev_priv = to_i915(dev);
  8952. struct drm_plane *plane;
  8953. struct intel_plane *intel_plane;
  8954. struct intel_plane_state *state;
  8955. struct drm_framebuffer *fb;
  8956. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  8957. crtc->base.base.id, crtc->base.name, context);
  8958. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  8959. transcoder_name(pipe_config->cpu_transcoder),
  8960. pipe_config->pipe_bpp, pipe_config->dither);
  8961. if (pipe_config->has_pch_encoder)
  8962. intel_dump_m_n_config(pipe_config, "fdi",
  8963. pipe_config->fdi_lanes,
  8964. &pipe_config->fdi_m_n);
  8965. if (intel_crtc_has_dp_encoder(pipe_config)) {
  8966. intel_dump_m_n_config(pipe_config, "dp m_n",
  8967. pipe_config->lane_count, &pipe_config->dp_m_n);
  8968. if (pipe_config->has_drrs)
  8969. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  8970. pipe_config->lane_count,
  8971. &pipe_config->dp_m2_n2);
  8972. }
  8973. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8974. pipe_config->has_audio, pipe_config->has_infoframe);
  8975. DRM_DEBUG_KMS("requested mode:\n");
  8976. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8977. DRM_DEBUG_KMS("adjusted mode:\n");
  8978. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8979. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8980. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  8981. pipe_config->port_clock,
  8982. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  8983. pipe_config->pixel_rate);
  8984. if (INTEL_GEN(dev_priv) >= 9)
  8985. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  8986. crtc->num_scalers,
  8987. pipe_config->scaler_state.scaler_users,
  8988. pipe_config->scaler_state.scaler_id);
  8989. if (HAS_GMCH_DISPLAY(dev_priv))
  8990. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8991. pipe_config->gmch_pfit.control,
  8992. pipe_config->gmch_pfit.pgm_ratios,
  8993. pipe_config->gmch_pfit.lvds_border_bits);
  8994. else
  8995. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8996. pipe_config->pch_pfit.pos,
  8997. pipe_config->pch_pfit.size,
  8998. enableddisabled(pipe_config->pch_pfit.enabled));
  8999. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  9000. pipe_config->ips_enabled, pipe_config->double_wide);
  9001. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  9002. DRM_DEBUG_KMS("planes on this crtc\n");
  9003. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9004. struct drm_format_name_buf format_name;
  9005. intel_plane = to_intel_plane(plane);
  9006. if (intel_plane->pipe != crtc->pipe)
  9007. continue;
  9008. state = to_intel_plane_state(plane->state);
  9009. fb = state->base.fb;
  9010. if (!fb) {
  9011. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  9012. plane->base.id, plane->name, state->scaler_id);
  9013. continue;
  9014. }
  9015. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  9016. plane->base.id, plane->name,
  9017. fb->base.id, fb->width, fb->height,
  9018. drm_get_format_name(fb->format->format, &format_name));
  9019. if (INTEL_GEN(dev_priv) >= 9)
  9020. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  9021. state->scaler_id,
  9022. state->base.src.x1 >> 16,
  9023. state->base.src.y1 >> 16,
  9024. drm_rect_width(&state->base.src) >> 16,
  9025. drm_rect_height(&state->base.src) >> 16,
  9026. state->base.dst.x1, state->base.dst.y1,
  9027. drm_rect_width(&state->base.dst),
  9028. drm_rect_height(&state->base.dst));
  9029. }
  9030. }
  9031. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9032. {
  9033. struct drm_device *dev = state->dev;
  9034. struct drm_connector *connector;
  9035. struct drm_connector_list_iter conn_iter;
  9036. unsigned int used_ports = 0;
  9037. unsigned int used_mst_ports = 0;
  9038. /*
  9039. * Walk the connector list instead of the encoder
  9040. * list to detect the problem on ddi platforms
  9041. * where there's just one encoder per digital port.
  9042. */
  9043. drm_connector_list_iter_begin(dev, &conn_iter);
  9044. drm_for_each_connector_iter(connector, &conn_iter) {
  9045. struct drm_connector_state *connector_state;
  9046. struct intel_encoder *encoder;
  9047. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  9048. if (!connector_state)
  9049. connector_state = connector->state;
  9050. if (!connector_state->best_encoder)
  9051. continue;
  9052. encoder = to_intel_encoder(connector_state->best_encoder);
  9053. WARN_ON(!connector_state->crtc);
  9054. switch (encoder->type) {
  9055. unsigned int port_mask;
  9056. case INTEL_OUTPUT_UNKNOWN:
  9057. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9058. break;
  9059. case INTEL_OUTPUT_DP:
  9060. case INTEL_OUTPUT_HDMI:
  9061. case INTEL_OUTPUT_EDP:
  9062. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9063. /* the same port mustn't appear more than once */
  9064. if (used_ports & port_mask)
  9065. return false;
  9066. used_ports |= port_mask;
  9067. break;
  9068. case INTEL_OUTPUT_DP_MST:
  9069. used_mst_ports |=
  9070. 1 << enc_to_mst(&encoder->base)->primary->port;
  9071. break;
  9072. default:
  9073. break;
  9074. }
  9075. }
  9076. drm_connector_list_iter_end(&conn_iter);
  9077. /* can't mix MST and SST/HDMI on the same port */
  9078. if (used_ports & used_mst_ports)
  9079. return false;
  9080. return true;
  9081. }
  9082. static void
  9083. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9084. {
  9085. struct drm_i915_private *dev_priv =
  9086. to_i915(crtc_state->base.crtc->dev);
  9087. struct intel_crtc_scaler_state scaler_state;
  9088. struct intel_dpll_hw_state dpll_hw_state;
  9089. struct intel_shared_dpll *shared_dpll;
  9090. struct intel_crtc_wm_state wm_state;
  9091. bool force_thru;
  9092. /* FIXME: before the switch to atomic started, a new pipe_config was
  9093. * kzalloc'd. Code that depends on any field being zero should be
  9094. * fixed, so that the crtc_state can be safely duplicated. For now,
  9095. * only fields that are know to not cause problems are preserved. */
  9096. scaler_state = crtc_state->scaler_state;
  9097. shared_dpll = crtc_state->shared_dpll;
  9098. dpll_hw_state = crtc_state->dpll_hw_state;
  9099. force_thru = crtc_state->pch_pfit.force_thru;
  9100. if (IS_G4X(dev_priv) ||
  9101. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9102. wm_state = crtc_state->wm;
  9103. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9104. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9105. memset(&crtc_state->base + 1, 0,
  9106. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9107. crtc_state->scaler_state = scaler_state;
  9108. crtc_state->shared_dpll = shared_dpll;
  9109. crtc_state->dpll_hw_state = dpll_hw_state;
  9110. crtc_state->pch_pfit.force_thru = force_thru;
  9111. if (IS_G4X(dev_priv) ||
  9112. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9113. crtc_state->wm = wm_state;
  9114. }
  9115. static int
  9116. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9117. struct intel_crtc_state *pipe_config)
  9118. {
  9119. struct drm_atomic_state *state = pipe_config->base.state;
  9120. struct intel_encoder *encoder;
  9121. struct drm_connector *connector;
  9122. struct drm_connector_state *connector_state;
  9123. int base_bpp, ret = -EINVAL;
  9124. int i;
  9125. bool retry = true;
  9126. clear_intel_crtc_state(pipe_config);
  9127. pipe_config->cpu_transcoder =
  9128. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9129. /*
  9130. * Sanitize sync polarity flags based on requested ones. If neither
  9131. * positive or negative polarity is requested, treat this as meaning
  9132. * negative polarity.
  9133. */
  9134. if (!(pipe_config->base.adjusted_mode.flags &
  9135. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9136. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9137. if (!(pipe_config->base.adjusted_mode.flags &
  9138. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9139. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9140. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9141. pipe_config);
  9142. if (base_bpp < 0)
  9143. goto fail;
  9144. /*
  9145. * Determine the real pipe dimensions. Note that stereo modes can
  9146. * increase the actual pipe size due to the frame doubling and
  9147. * insertion of additional space for blanks between the frame. This
  9148. * is stored in the crtc timings. We use the requested mode to do this
  9149. * computation to clearly distinguish it from the adjusted mode, which
  9150. * can be changed by the connectors in the below retry loop.
  9151. */
  9152. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9153. &pipe_config->pipe_src_w,
  9154. &pipe_config->pipe_src_h);
  9155. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9156. if (connector_state->crtc != crtc)
  9157. continue;
  9158. encoder = to_intel_encoder(connector_state->best_encoder);
  9159. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9160. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9161. goto fail;
  9162. }
  9163. /*
  9164. * Determine output_types before calling the .compute_config()
  9165. * hooks so that the hooks can use this information safely.
  9166. */
  9167. pipe_config->output_types |= 1 << encoder->type;
  9168. }
  9169. encoder_retry:
  9170. /* Ensure the port clock defaults are reset when retrying. */
  9171. pipe_config->port_clock = 0;
  9172. pipe_config->pixel_multiplier = 1;
  9173. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9174. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9175. CRTC_STEREO_DOUBLE);
  9176. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9177. * adjust it according to limitations or connector properties, and also
  9178. * a chance to reject the mode entirely.
  9179. */
  9180. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9181. if (connector_state->crtc != crtc)
  9182. continue;
  9183. encoder = to_intel_encoder(connector_state->best_encoder);
  9184. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9185. DRM_DEBUG_KMS("Encoder config failure\n");
  9186. goto fail;
  9187. }
  9188. }
  9189. /* Set default port clock if not overwritten by the encoder. Needs to be
  9190. * done afterwards in case the encoder adjusts the mode. */
  9191. if (!pipe_config->port_clock)
  9192. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9193. * pipe_config->pixel_multiplier;
  9194. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9195. if (ret < 0) {
  9196. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9197. goto fail;
  9198. }
  9199. if (ret == RETRY) {
  9200. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9201. ret = -EINVAL;
  9202. goto fail;
  9203. }
  9204. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9205. retry = false;
  9206. goto encoder_retry;
  9207. }
  9208. /* Dithering seems to not pass-through bits correctly when it should, so
  9209. * only enable it on 6bpc panels and when its not a compliance
  9210. * test requesting 6bpc video pattern.
  9211. */
  9212. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9213. !pipe_config->dither_force_disable;
  9214. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9215. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9216. fail:
  9217. return ret;
  9218. }
  9219. static void
  9220. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  9221. {
  9222. struct drm_crtc *crtc;
  9223. struct drm_crtc_state *new_crtc_state;
  9224. int i;
  9225. /* Double check state. */
  9226. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  9227. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  9228. /*
  9229. * Update legacy state to satisfy fbc code. This can
  9230. * be removed when fbc uses the atomic state.
  9231. */
  9232. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  9233. struct drm_plane_state *plane_state = crtc->primary->state;
  9234. crtc->primary->fb = plane_state->fb;
  9235. crtc->x = plane_state->src_x >> 16;
  9236. crtc->y = plane_state->src_y >> 16;
  9237. }
  9238. }
  9239. }
  9240. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9241. {
  9242. int diff;
  9243. if (clock1 == clock2)
  9244. return true;
  9245. if (!clock1 || !clock2)
  9246. return false;
  9247. diff = abs(clock1 - clock2);
  9248. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9249. return true;
  9250. return false;
  9251. }
  9252. static bool
  9253. intel_compare_m_n(unsigned int m, unsigned int n,
  9254. unsigned int m2, unsigned int n2,
  9255. bool exact)
  9256. {
  9257. if (m == m2 && n == n2)
  9258. return true;
  9259. if (exact || !m || !n || !m2 || !n2)
  9260. return false;
  9261. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9262. if (n > n2) {
  9263. while (n > n2) {
  9264. m2 <<= 1;
  9265. n2 <<= 1;
  9266. }
  9267. } else if (n < n2) {
  9268. while (n < n2) {
  9269. m <<= 1;
  9270. n <<= 1;
  9271. }
  9272. }
  9273. if (n != n2)
  9274. return false;
  9275. return intel_fuzzy_clock_check(m, m2);
  9276. }
  9277. static bool
  9278. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9279. struct intel_link_m_n *m2_n2,
  9280. bool adjust)
  9281. {
  9282. if (m_n->tu == m2_n2->tu &&
  9283. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9284. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9285. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9286. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9287. if (adjust)
  9288. *m2_n2 = *m_n;
  9289. return true;
  9290. }
  9291. return false;
  9292. }
  9293. static void __printf(3, 4)
  9294. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9295. {
  9296. char *level;
  9297. unsigned int category;
  9298. struct va_format vaf;
  9299. va_list args;
  9300. if (adjust) {
  9301. level = KERN_DEBUG;
  9302. category = DRM_UT_KMS;
  9303. } else {
  9304. level = KERN_ERR;
  9305. category = DRM_UT_NONE;
  9306. }
  9307. va_start(args, format);
  9308. vaf.fmt = format;
  9309. vaf.va = &args;
  9310. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  9311. va_end(args);
  9312. }
  9313. static bool
  9314. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9315. struct intel_crtc_state *current_config,
  9316. struct intel_crtc_state *pipe_config,
  9317. bool adjust)
  9318. {
  9319. bool ret = true;
  9320. #define PIPE_CONF_CHECK_X(name) \
  9321. if (current_config->name != pipe_config->name) { \
  9322. pipe_config_err(adjust, __stringify(name), \
  9323. "(expected 0x%08x, found 0x%08x)\n", \
  9324. current_config->name, \
  9325. pipe_config->name); \
  9326. ret = false; \
  9327. }
  9328. #define PIPE_CONF_CHECK_I(name) \
  9329. if (current_config->name != pipe_config->name) { \
  9330. pipe_config_err(adjust, __stringify(name), \
  9331. "(expected %i, found %i)\n", \
  9332. current_config->name, \
  9333. pipe_config->name); \
  9334. ret = false; \
  9335. }
  9336. #define PIPE_CONF_CHECK_P(name) \
  9337. if (current_config->name != pipe_config->name) { \
  9338. pipe_config_err(adjust, __stringify(name), \
  9339. "(expected %p, found %p)\n", \
  9340. current_config->name, \
  9341. pipe_config->name); \
  9342. ret = false; \
  9343. }
  9344. #define PIPE_CONF_CHECK_M_N(name) \
  9345. if (!intel_compare_link_m_n(&current_config->name, \
  9346. &pipe_config->name,\
  9347. adjust)) { \
  9348. pipe_config_err(adjust, __stringify(name), \
  9349. "(expected tu %i gmch %i/%i link %i/%i, " \
  9350. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9351. current_config->name.tu, \
  9352. current_config->name.gmch_m, \
  9353. current_config->name.gmch_n, \
  9354. current_config->name.link_m, \
  9355. current_config->name.link_n, \
  9356. pipe_config->name.tu, \
  9357. pipe_config->name.gmch_m, \
  9358. pipe_config->name.gmch_n, \
  9359. pipe_config->name.link_m, \
  9360. pipe_config->name.link_n); \
  9361. ret = false; \
  9362. }
  9363. /* This is required for BDW+ where there is only one set of registers for
  9364. * switching between high and low RR.
  9365. * This macro can be used whenever a comparison has to be made between one
  9366. * hw state and multiple sw state variables.
  9367. */
  9368. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9369. if (!intel_compare_link_m_n(&current_config->name, \
  9370. &pipe_config->name, adjust) && \
  9371. !intel_compare_link_m_n(&current_config->alt_name, \
  9372. &pipe_config->name, adjust)) { \
  9373. pipe_config_err(adjust, __stringify(name), \
  9374. "(expected tu %i gmch %i/%i link %i/%i, " \
  9375. "or tu %i gmch %i/%i link %i/%i, " \
  9376. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9377. current_config->name.tu, \
  9378. current_config->name.gmch_m, \
  9379. current_config->name.gmch_n, \
  9380. current_config->name.link_m, \
  9381. current_config->name.link_n, \
  9382. current_config->alt_name.tu, \
  9383. current_config->alt_name.gmch_m, \
  9384. current_config->alt_name.gmch_n, \
  9385. current_config->alt_name.link_m, \
  9386. current_config->alt_name.link_n, \
  9387. pipe_config->name.tu, \
  9388. pipe_config->name.gmch_m, \
  9389. pipe_config->name.gmch_n, \
  9390. pipe_config->name.link_m, \
  9391. pipe_config->name.link_n); \
  9392. ret = false; \
  9393. }
  9394. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9395. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9396. pipe_config_err(adjust, __stringify(name), \
  9397. "(%x) (expected %i, found %i)\n", \
  9398. (mask), \
  9399. current_config->name & (mask), \
  9400. pipe_config->name & (mask)); \
  9401. ret = false; \
  9402. }
  9403. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9404. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9405. pipe_config_err(adjust, __stringify(name), \
  9406. "(expected %i, found %i)\n", \
  9407. current_config->name, \
  9408. pipe_config->name); \
  9409. ret = false; \
  9410. }
  9411. #define PIPE_CONF_QUIRK(quirk) \
  9412. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9413. PIPE_CONF_CHECK_I(cpu_transcoder);
  9414. PIPE_CONF_CHECK_I(has_pch_encoder);
  9415. PIPE_CONF_CHECK_I(fdi_lanes);
  9416. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9417. PIPE_CONF_CHECK_I(lane_count);
  9418. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9419. if (INTEL_GEN(dev_priv) < 8) {
  9420. PIPE_CONF_CHECK_M_N(dp_m_n);
  9421. if (current_config->has_drrs)
  9422. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9423. } else
  9424. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9425. PIPE_CONF_CHECK_X(output_types);
  9426. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9427. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9428. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9429. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9430. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9431. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9432. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9433. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9434. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9435. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9436. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9437. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9438. PIPE_CONF_CHECK_I(pixel_multiplier);
  9439. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9440. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9441. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9442. PIPE_CONF_CHECK_I(limited_color_range);
  9443. PIPE_CONF_CHECK_I(hdmi_scrambling);
  9444. PIPE_CONF_CHECK_I(hdmi_high_tmds_clock_ratio);
  9445. PIPE_CONF_CHECK_I(has_infoframe);
  9446. PIPE_CONF_CHECK_I(has_audio);
  9447. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9448. DRM_MODE_FLAG_INTERLACE);
  9449. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9450. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9451. DRM_MODE_FLAG_PHSYNC);
  9452. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9453. DRM_MODE_FLAG_NHSYNC);
  9454. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9455. DRM_MODE_FLAG_PVSYNC);
  9456. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9457. DRM_MODE_FLAG_NVSYNC);
  9458. }
  9459. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9460. /* pfit ratios are autocomputed by the hw on gen4+ */
  9461. if (INTEL_GEN(dev_priv) < 4)
  9462. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9463. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9464. if (!adjust) {
  9465. PIPE_CONF_CHECK_I(pipe_src_w);
  9466. PIPE_CONF_CHECK_I(pipe_src_h);
  9467. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9468. if (current_config->pch_pfit.enabled) {
  9469. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9470. PIPE_CONF_CHECK_X(pch_pfit.size);
  9471. }
  9472. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9473. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9474. }
  9475. /* BDW+ don't expose a synchronous way to read the state */
  9476. if (IS_HASWELL(dev_priv))
  9477. PIPE_CONF_CHECK_I(ips_enabled);
  9478. PIPE_CONF_CHECK_I(double_wide);
  9479. PIPE_CONF_CHECK_P(shared_dpll);
  9480. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9481. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9482. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9483. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9484. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9485. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9486. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9487. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9488. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9489. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9490. PIPE_CONF_CHECK_X(dsi_pll.div);
  9491. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9492. PIPE_CONF_CHECK_I(pipe_bpp);
  9493. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9494. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9495. #undef PIPE_CONF_CHECK_X
  9496. #undef PIPE_CONF_CHECK_I
  9497. #undef PIPE_CONF_CHECK_P
  9498. #undef PIPE_CONF_CHECK_FLAGS
  9499. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9500. #undef PIPE_CONF_QUIRK
  9501. return ret;
  9502. }
  9503. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9504. const struct intel_crtc_state *pipe_config)
  9505. {
  9506. if (pipe_config->has_pch_encoder) {
  9507. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9508. &pipe_config->fdi_m_n);
  9509. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9510. /*
  9511. * FDI already provided one idea for the dotclock.
  9512. * Yell if the encoder disagrees.
  9513. */
  9514. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9515. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9516. fdi_dotclock, dotclock);
  9517. }
  9518. }
  9519. static void verify_wm_state(struct drm_crtc *crtc,
  9520. struct drm_crtc_state *new_state)
  9521. {
  9522. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9523. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9524. struct skl_pipe_wm hw_wm, *sw_wm;
  9525. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9526. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9527. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9528. const enum pipe pipe = intel_crtc->pipe;
  9529. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9530. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9531. return;
  9532. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9533. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9534. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9535. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9536. /* planes */
  9537. for_each_universal_plane(dev_priv, pipe, plane) {
  9538. hw_plane_wm = &hw_wm.planes[plane];
  9539. sw_plane_wm = &sw_wm->planes[plane];
  9540. /* Watermarks */
  9541. for (level = 0; level <= max_level; level++) {
  9542. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9543. &sw_plane_wm->wm[level]))
  9544. continue;
  9545. 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",
  9546. pipe_name(pipe), plane + 1, level,
  9547. sw_plane_wm->wm[level].plane_en,
  9548. sw_plane_wm->wm[level].plane_res_b,
  9549. sw_plane_wm->wm[level].plane_res_l,
  9550. hw_plane_wm->wm[level].plane_en,
  9551. hw_plane_wm->wm[level].plane_res_b,
  9552. hw_plane_wm->wm[level].plane_res_l);
  9553. }
  9554. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9555. &sw_plane_wm->trans_wm)) {
  9556. 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",
  9557. pipe_name(pipe), plane + 1,
  9558. sw_plane_wm->trans_wm.plane_en,
  9559. sw_plane_wm->trans_wm.plane_res_b,
  9560. sw_plane_wm->trans_wm.plane_res_l,
  9561. hw_plane_wm->trans_wm.plane_en,
  9562. hw_plane_wm->trans_wm.plane_res_b,
  9563. hw_plane_wm->trans_wm.plane_res_l);
  9564. }
  9565. /* DDB */
  9566. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9567. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9568. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9569. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9570. pipe_name(pipe), plane + 1,
  9571. sw_ddb_entry->start, sw_ddb_entry->end,
  9572. hw_ddb_entry->start, hw_ddb_entry->end);
  9573. }
  9574. }
  9575. /*
  9576. * cursor
  9577. * If the cursor plane isn't active, we may not have updated it's ddb
  9578. * allocation. In that case since the ddb allocation will be updated
  9579. * once the plane becomes visible, we can skip this check
  9580. */
  9581. if (1) {
  9582. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9583. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9584. /* Watermarks */
  9585. for (level = 0; level <= max_level; level++) {
  9586. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9587. &sw_plane_wm->wm[level]))
  9588. continue;
  9589. 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",
  9590. pipe_name(pipe), level,
  9591. sw_plane_wm->wm[level].plane_en,
  9592. sw_plane_wm->wm[level].plane_res_b,
  9593. sw_plane_wm->wm[level].plane_res_l,
  9594. hw_plane_wm->wm[level].plane_en,
  9595. hw_plane_wm->wm[level].plane_res_b,
  9596. hw_plane_wm->wm[level].plane_res_l);
  9597. }
  9598. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9599. &sw_plane_wm->trans_wm)) {
  9600. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9601. pipe_name(pipe),
  9602. sw_plane_wm->trans_wm.plane_en,
  9603. sw_plane_wm->trans_wm.plane_res_b,
  9604. sw_plane_wm->trans_wm.plane_res_l,
  9605. hw_plane_wm->trans_wm.plane_en,
  9606. hw_plane_wm->trans_wm.plane_res_b,
  9607. hw_plane_wm->trans_wm.plane_res_l);
  9608. }
  9609. /* DDB */
  9610. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9611. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9612. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9613. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9614. pipe_name(pipe),
  9615. sw_ddb_entry->start, sw_ddb_entry->end,
  9616. hw_ddb_entry->start, hw_ddb_entry->end);
  9617. }
  9618. }
  9619. }
  9620. static void
  9621. verify_connector_state(struct drm_device *dev,
  9622. struct drm_atomic_state *state,
  9623. struct drm_crtc *crtc)
  9624. {
  9625. struct drm_connector *connector;
  9626. struct drm_connector_state *new_conn_state;
  9627. int i;
  9628. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9629. struct drm_encoder *encoder = connector->encoder;
  9630. struct drm_crtc_state *crtc_state = NULL;
  9631. if (new_conn_state->crtc != crtc)
  9632. continue;
  9633. if (crtc)
  9634. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  9635. intel_connector_verify_state(crtc_state, new_conn_state);
  9636. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9637. "connector's atomic encoder doesn't match legacy encoder\n");
  9638. }
  9639. }
  9640. static void
  9641. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9642. {
  9643. struct intel_encoder *encoder;
  9644. struct drm_connector *connector;
  9645. struct drm_connector_state *old_conn_state, *new_conn_state;
  9646. int i;
  9647. for_each_intel_encoder(dev, encoder) {
  9648. bool enabled = false, found = false;
  9649. enum pipe pipe;
  9650. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9651. encoder->base.base.id,
  9652. encoder->base.name);
  9653. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  9654. new_conn_state, i) {
  9655. if (old_conn_state->best_encoder == &encoder->base)
  9656. found = true;
  9657. if (new_conn_state->best_encoder != &encoder->base)
  9658. continue;
  9659. found = enabled = true;
  9660. I915_STATE_WARN(new_conn_state->crtc !=
  9661. encoder->base.crtc,
  9662. "connector's crtc doesn't match encoder crtc\n");
  9663. }
  9664. if (!found)
  9665. continue;
  9666. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9667. "encoder's enabled state mismatch "
  9668. "(expected %i, found %i)\n",
  9669. !!encoder->base.crtc, enabled);
  9670. if (!encoder->base.crtc) {
  9671. bool active;
  9672. active = encoder->get_hw_state(encoder, &pipe);
  9673. I915_STATE_WARN(active,
  9674. "encoder detached but still enabled on pipe %c.\n",
  9675. pipe_name(pipe));
  9676. }
  9677. }
  9678. }
  9679. static void
  9680. verify_crtc_state(struct drm_crtc *crtc,
  9681. struct drm_crtc_state *old_crtc_state,
  9682. struct drm_crtc_state *new_crtc_state)
  9683. {
  9684. struct drm_device *dev = crtc->dev;
  9685. struct drm_i915_private *dev_priv = to_i915(dev);
  9686. struct intel_encoder *encoder;
  9687. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9688. struct intel_crtc_state *pipe_config, *sw_config;
  9689. struct drm_atomic_state *old_state;
  9690. bool active;
  9691. old_state = old_crtc_state->state;
  9692. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  9693. pipe_config = to_intel_crtc_state(old_crtc_state);
  9694. memset(pipe_config, 0, sizeof(*pipe_config));
  9695. pipe_config->base.crtc = crtc;
  9696. pipe_config->base.state = old_state;
  9697. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  9698. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  9699. /* we keep both pipes enabled on 830 */
  9700. if (IS_I830(dev_priv))
  9701. active = new_crtc_state->active;
  9702. I915_STATE_WARN(new_crtc_state->active != active,
  9703. "crtc active state doesn't match with hw state "
  9704. "(expected %i, found %i)\n", new_crtc_state->active, active);
  9705. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  9706. "transitional active state does not match atomic hw state "
  9707. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  9708. for_each_encoder_on_crtc(dev, crtc, encoder) {
  9709. enum pipe pipe;
  9710. active = encoder->get_hw_state(encoder, &pipe);
  9711. I915_STATE_WARN(active != new_crtc_state->active,
  9712. "[ENCODER:%i] active %i with crtc active %i\n",
  9713. encoder->base.base.id, active, new_crtc_state->active);
  9714. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  9715. "Encoder connected to wrong pipe %c\n",
  9716. pipe_name(pipe));
  9717. if (active) {
  9718. pipe_config->output_types |= 1 << encoder->type;
  9719. encoder->get_config(encoder, pipe_config);
  9720. }
  9721. }
  9722. intel_crtc_compute_pixel_rate(pipe_config);
  9723. if (!new_crtc_state->active)
  9724. return;
  9725. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  9726. sw_config = to_intel_crtc_state(new_crtc_state);
  9727. if (!intel_pipe_config_compare(dev_priv, sw_config,
  9728. pipe_config, false)) {
  9729. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9730. intel_dump_pipe_config(intel_crtc, pipe_config,
  9731. "[hw state]");
  9732. intel_dump_pipe_config(intel_crtc, sw_config,
  9733. "[sw state]");
  9734. }
  9735. }
  9736. static void
  9737. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  9738. struct intel_shared_dpll *pll,
  9739. struct drm_crtc *crtc,
  9740. struct drm_crtc_state *new_state)
  9741. {
  9742. struct intel_dpll_hw_state dpll_hw_state;
  9743. unsigned crtc_mask;
  9744. bool active;
  9745. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9746. DRM_DEBUG_KMS("%s\n", pll->name);
  9747. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  9748. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  9749. I915_STATE_WARN(!pll->on && pll->active_mask,
  9750. "pll in active use but not on in sw tracking\n");
  9751. I915_STATE_WARN(pll->on && !pll->active_mask,
  9752. "pll is on but not used by any active crtc\n");
  9753. I915_STATE_WARN(pll->on != active,
  9754. "pll on state mismatch (expected %i, found %i)\n",
  9755. pll->on, active);
  9756. }
  9757. if (!crtc) {
  9758. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  9759. "more active pll users than references: %x vs %x\n",
  9760. pll->active_mask, pll->state.crtc_mask);
  9761. return;
  9762. }
  9763. crtc_mask = 1 << drm_crtc_index(crtc);
  9764. if (new_state->active)
  9765. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  9766. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  9767. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9768. else
  9769. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9770. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  9771. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9772. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  9773. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  9774. crtc_mask, pll->state.crtc_mask);
  9775. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  9776. &dpll_hw_state,
  9777. sizeof(dpll_hw_state)),
  9778. "pll hw state mismatch\n");
  9779. }
  9780. static void
  9781. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  9782. struct drm_crtc_state *old_crtc_state,
  9783. struct drm_crtc_state *new_crtc_state)
  9784. {
  9785. struct drm_i915_private *dev_priv = to_i915(dev);
  9786. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  9787. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  9788. if (new_state->shared_dpll)
  9789. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  9790. if (old_state->shared_dpll &&
  9791. old_state->shared_dpll != new_state->shared_dpll) {
  9792. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  9793. struct intel_shared_dpll *pll = old_state->shared_dpll;
  9794. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9795. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  9796. pipe_name(drm_crtc_index(crtc)));
  9797. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  9798. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  9799. pipe_name(drm_crtc_index(crtc)));
  9800. }
  9801. }
  9802. static void
  9803. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  9804. struct drm_atomic_state *state,
  9805. struct drm_crtc_state *old_state,
  9806. struct drm_crtc_state *new_state)
  9807. {
  9808. if (!needs_modeset(new_state) &&
  9809. !to_intel_crtc_state(new_state)->update_pipe)
  9810. return;
  9811. verify_wm_state(crtc, new_state);
  9812. verify_connector_state(crtc->dev, state, crtc);
  9813. verify_crtc_state(crtc, old_state, new_state);
  9814. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  9815. }
  9816. static void
  9817. verify_disabled_dpll_state(struct drm_device *dev)
  9818. {
  9819. struct drm_i915_private *dev_priv = to_i915(dev);
  9820. int i;
  9821. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  9822. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  9823. }
  9824. static void
  9825. intel_modeset_verify_disabled(struct drm_device *dev,
  9826. struct drm_atomic_state *state)
  9827. {
  9828. verify_encoder_state(dev, state);
  9829. verify_connector_state(dev, state, NULL);
  9830. verify_disabled_dpll_state(dev);
  9831. }
  9832. static void update_scanline_offset(struct intel_crtc *crtc)
  9833. {
  9834. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9835. /*
  9836. * The scanline counter increments at the leading edge of hsync.
  9837. *
  9838. * On most platforms it starts counting from vtotal-1 on the
  9839. * first active line. That means the scanline counter value is
  9840. * always one less than what we would expect. Ie. just after
  9841. * start of vblank, which also occurs at start of hsync (on the
  9842. * last active line), the scanline counter will read vblank_start-1.
  9843. *
  9844. * On gen2 the scanline counter starts counting from 1 instead
  9845. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9846. * to keep the value positive), instead of adding one.
  9847. *
  9848. * On HSW+ the behaviour of the scanline counter depends on the output
  9849. * type. For DP ports it behaves like most other platforms, but on HDMI
  9850. * there's an extra 1 line difference. So we need to add two instead of
  9851. * one to the value.
  9852. *
  9853. * On VLV/CHV DSI the scanline counter would appear to increment
  9854. * approx. 1/3 of a scanline before start of vblank. Unfortunately
  9855. * that means we can't tell whether we're in vblank or not while
  9856. * we're on that particular line. We must still set scanline_offset
  9857. * to 1 so that the vblank timestamps come out correct when we query
  9858. * the scanline counter from within the vblank interrupt handler.
  9859. * However if queried just before the start of vblank we'll get an
  9860. * answer that's slightly in the future.
  9861. */
  9862. if (IS_GEN2(dev_priv)) {
  9863. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  9864. int vtotal;
  9865. vtotal = adjusted_mode->crtc_vtotal;
  9866. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  9867. vtotal /= 2;
  9868. crtc->scanline_offset = vtotal - 1;
  9869. } else if (HAS_DDI(dev_priv) &&
  9870. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  9871. crtc->scanline_offset = 2;
  9872. } else
  9873. crtc->scanline_offset = 1;
  9874. }
  9875. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  9876. {
  9877. struct drm_device *dev = state->dev;
  9878. struct drm_i915_private *dev_priv = to_i915(dev);
  9879. struct drm_crtc *crtc;
  9880. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9881. int i;
  9882. if (!dev_priv->display.crtc_compute_clock)
  9883. return;
  9884. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  9885. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9886. struct intel_shared_dpll *old_dpll =
  9887. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  9888. if (!needs_modeset(new_crtc_state))
  9889. continue;
  9890. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  9891. if (!old_dpll)
  9892. continue;
  9893. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  9894. }
  9895. }
  9896. /*
  9897. * This implements the workaround described in the "notes" section of the mode
  9898. * set sequence documentation. When going from no pipes or single pipe to
  9899. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  9900. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  9901. */
  9902. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  9903. {
  9904. struct drm_crtc_state *crtc_state;
  9905. struct intel_crtc *intel_crtc;
  9906. struct drm_crtc *crtc;
  9907. struct intel_crtc_state *first_crtc_state = NULL;
  9908. struct intel_crtc_state *other_crtc_state = NULL;
  9909. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  9910. int i;
  9911. /* look at all crtc's that are going to be enabled in during modeset */
  9912. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  9913. intel_crtc = to_intel_crtc(crtc);
  9914. if (!crtc_state->active || !needs_modeset(crtc_state))
  9915. continue;
  9916. if (first_crtc_state) {
  9917. other_crtc_state = to_intel_crtc_state(crtc_state);
  9918. break;
  9919. } else {
  9920. first_crtc_state = to_intel_crtc_state(crtc_state);
  9921. first_pipe = intel_crtc->pipe;
  9922. }
  9923. }
  9924. /* No workaround needed? */
  9925. if (!first_crtc_state)
  9926. return 0;
  9927. /* w/a possibly needed, check how many crtc's are already enabled. */
  9928. for_each_intel_crtc(state->dev, intel_crtc) {
  9929. struct intel_crtc_state *pipe_config;
  9930. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  9931. if (IS_ERR(pipe_config))
  9932. return PTR_ERR(pipe_config);
  9933. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  9934. if (!pipe_config->base.active ||
  9935. needs_modeset(&pipe_config->base))
  9936. continue;
  9937. /* 2 or more enabled crtcs means no need for w/a */
  9938. if (enabled_pipe != INVALID_PIPE)
  9939. return 0;
  9940. enabled_pipe = intel_crtc->pipe;
  9941. }
  9942. if (enabled_pipe != INVALID_PIPE)
  9943. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  9944. else if (other_crtc_state)
  9945. other_crtc_state->hsw_workaround_pipe = first_pipe;
  9946. return 0;
  9947. }
  9948. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  9949. {
  9950. struct drm_crtc *crtc;
  9951. /* Add all pipes to the state */
  9952. for_each_crtc(state->dev, crtc) {
  9953. struct drm_crtc_state *crtc_state;
  9954. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9955. if (IS_ERR(crtc_state))
  9956. return PTR_ERR(crtc_state);
  9957. }
  9958. return 0;
  9959. }
  9960. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  9961. {
  9962. struct drm_crtc *crtc;
  9963. /*
  9964. * Add all pipes to the state, and force
  9965. * a modeset on all the active ones.
  9966. */
  9967. for_each_crtc(state->dev, crtc) {
  9968. struct drm_crtc_state *crtc_state;
  9969. int ret;
  9970. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  9971. if (IS_ERR(crtc_state))
  9972. return PTR_ERR(crtc_state);
  9973. if (!crtc_state->active || needs_modeset(crtc_state))
  9974. continue;
  9975. crtc_state->mode_changed = true;
  9976. ret = drm_atomic_add_affected_connectors(state, crtc);
  9977. if (ret)
  9978. return ret;
  9979. ret = drm_atomic_add_affected_planes(state, crtc);
  9980. if (ret)
  9981. return ret;
  9982. }
  9983. return 0;
  9984. }
  9985. static int intel_modeset_checks(struct drm_atomic_state *state)
  9986. {
  9987. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  9988. struct drm_i915_private *dev_priv = to_i915(state->dev);
  9989. struct drm_crtc *crtc;
  9990. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  9991. int ret = 0, i;
  9992. if (!check_digital_port_conflicts(state)) {
  9993. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  9994. return -EINVAL;
  9995. }
  9996. intel_state->modeset = true;
  9997. intel_state->active_crtcs = dev_priv->active_crtcs;
  9998. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  9999. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  10000. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10001. if (new_crtc_state->active)
  10002. intel_state->active_crtcs |= 1 << i;
  10003. else
  10004. intel_state->active_crtcs &= ~(1 << i);
  10005. if (old_crtc_state->active != new_crtc_state->active)
  10006. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10007. }
  10008. /*
  10009. * See if the config requires any additional preparation, e.g.
  10010. * to adjust global state with pipes off. We need to do this
  10011. * here so we can get the modeset_pipe updated config for the new
  10012. * mode set on this crtc. For other crtcs we need to use the
  10013. * adjusted_mode bits in the crtc directly.
  10014. */
  10015. if (dev_priv->display.modeset_calc_cdclk) {
  10016. ret = dev_priv->display.modeset_calc_cdclk(state);
  10017. if (ret < 0)
  10018. return ret;
  10019. /*
  10020. * Writes to dev_priv->cdclk.logical must protected by
  10021. * holding all the crtc locks, even if we don't end up
  10022. * touching the hardware
  10023. */
  10024. if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
  10025. &intel_state->cdclk.logical)) {
  10026. ret = intel_lock_all_pipes(state);
  10027. if (ret < 0)
  10028. return ret;
  10029. }
  10030. /* All pipes must be switched off while we change the cdclk. */
  10031. if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
  10032. &intel_state->cdclk.actual)) {
  10033. ret = intel_modeset_all_pipes(state);
  10034. if (ret < 0)
  10035. return ret;
  10036. }
  10037. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10038. intel_state->cdclk.logical.cdclk,
  10039. intel_state->cdclk.actual.cdclk);
  10040. } else {
  10041. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10042. }
  10043. intel_modeset_clear_plls(state);
  10044. if (IS_HASWELL(dev_priv))
  10045. return haswell_mode_set_planes_workaround(state);
  10046. return 0;
  10047. }
  10048. /*
  10049. * Handle calculation of various watermark data at the end of the atomic check
  10050. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10051. * handlers to ensure that all derived state has been updated.
  10052. */
  10053. static int calc_watermark_data(struct drm_atomic_state *state)
  10054. {
  10055. struct drm_device *dev = state->dev;
  10056. struct drm_i915_private *dev_priv = to_i915(dev);
  10057. /* Is there platform-specific watermark information to calculate? */
  10058. if (dev_priv->display.compute_global_watermarks)
  10059. return dev_priv->display.compute_global_watermarks(state);
  10060. return 0;
  10061. }
  10062. /**
  10063. * intel_atomic_check - validate state object
  10064. * @dev: drm device
  10065. * @state: state to validate
  10066. */
  10067. static int intel_atomic_check(struct drm_device *dev,
  10068. struct drm_atomic_state *state)
  10069. {
  10070. struct drm_i915_private *dev_priv = to_i915(dev);
  10071. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10072. struct drm_crtc *crtc;
  10073. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10074. int ret, i;
  10075. bool any_ms = false;
  10076. ret = drm_atomic_helper_check_modeset(dev, state);
  10077. if (ret)
  10078. return ret;
  10079. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10080. struct intel_crtc_state *pipe_config =
  10081. to_intel_crtc_state(crtc_state);
  10082. /* Catch I915_MODE_FLAG_INHERITED */
  10083. if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
  10084. crtc_state->mode_changed = true;
  10085. if (!needs_modeset(crtc_state))
  10086. continue;
  10087. if (!crtc_state->enable) {
  10088. any_ms = true;
  10089. continue;
  10090. }
  10091. /* FIXME: For only active_changed we shouldn't need to do any
  10092. * state recomputation at all. */
  10093. ret = drm_atomic_add_affected_connectors(state, crtc);
  10094. if (ret)
  10095. return ret;
  10096. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10097. if (ret) {
  10098. intel_dump_pipe_config(to_intel_crtc(crtc),
  10099. pipe_config, "[failed]");
  10100. return ret;
  10101. }
  10102. if (i915.fastboot &&
  10103. intel_pipe_config_compare(dev_priv,
  10104. to_intel_crtc_state(old_crtc_state),
  10105. pipe_config, true)) {
  10106. crtc_state->mode_changed = false;
  10107. pipe_config->update_pipe = true;
  10108. }
  10109. if (needs_modeset(crtc_state))
  10110. any_ms = true;
  10111. ret = drm_atomic_add_affected_planes(state, crtc);
  10112. if (ret)
  10113. return ret;
  10114. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10115. needs_modeset(crtc_state) ?
  10116. "[modeset]" : "[fastset]");
  10117. }
  10118. if (any_ms) {
  10119. ret = intel_modeset_checks(state);
  10120. if (ret)
  10121. return ret;
  10122. } else {
  10123. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10124. }
  10125. ret = drm_atomic_helper_check_planes(dev, state);
  10126. if (ret)
  10127. return ret;
  10128. intel_fbc_choose_crtc(dev_priv, state);
  10129. return calc_watermark_data(state);
  10130. }
  10131. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10132. struct drm_atomic_state *state)
  10133. {
  10134. struct drm_i915_private *dev_priv = to_i915(dev);
  10135. struct drm_crtc_state *crtc_state;
  10136. struct drm_crtc *crtc;
  10137. int i, ret;
  10138. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  10139. if (state->legacy_cursor_update)
  10140. continue;
  10141. ret = intel_crtc_wait_for_pending_flips(crtc);
  10142. if (ret)
  10143. return ret;
  10144. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  10145. flush_workqueue(dev_priv->wq);
  10146. }
  10147. ret = mutex_lock_interruptible(&dev->struct_mutex);
  10148. if (ret)
  10149. return ret;
  10150. ret = drm_atomic_helper_prepare_planes(dev, state);
  10151. mutex_unlock(&dev->struct_mutex);
  10152. return ret;
  10153. }
  10154. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10155. {
  10156. struct drm_device *dev = crtc->base.dev;
  10157. if (!dev->max_vblank_count)
  10158. return drm_accurate_vblank_count(&crtc->base);
  10159. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10160. }
  10161. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  10162. struct drm_i915_private *dev_priv,
  10163. unsigned crtc_mask)
  10164. {
  10165. unsigned last_vblank_count[I915_MAX_PIPES];
  10166. enum pipe pipe;
  10167. int ret;
  10168. if (!crtc_mask)
  10169. return;
  10170. for_each_pipe(dev_priv, pipe) {
  10171. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10172. pipe);
  10173. if (!((1 << pipe) & crtc_mask))
  10174. continue;
  10175. ret = drm_crtc_vblank_get(&crtc->base);
  10176. if (WARN_ON(ret != 0)) {
  10177. crtc_mask &= ~(1 << pipe);
  10178. continue;
  10179. }
  10180. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  10181. }
  10182. for_each_pipe(dev_priv, pipe) {
  10183. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10184. pipe);
  10185. long lret;
  10186. if (!((1 << pipe) & crtc_mask))
  10187. continue;
  10188. lret = wait_event_timeout(dev->vblank[pipe].queue,
  10189. last_vblank_count[pipe] !=
  10190. drm_crtc_vblank_count(&crtc->base),
  10191. msecs_to_jiffies(50));
  10192. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  10193. drm_crtc_vblank_put(&crtc->base);
  10194. }
  10195. }
  10196. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  10197. {
  10198. /* fb updated, need to unpin old fb */
  10199. if (crtc_state->fb_changed)
  10200. return true;
  10201. /* wm changes, need vblank before final wm's */
  10202. if (crtc_state->update_wm_post)
  10203. return true;
  10204. if (crtc_state->wm.need_postvbl_update)
  10205. return true;
  10206. return false;
  10207. }
  10208. static void intel_update_crtc(struct drm_crtc *crtc,
  10209. struct drm_atomic_state *state,
  10210. struct drm_crtc_state *old_crtc_state,
  10211. struct drm_crtc_state *new_crtc_state,
  10212. unsigned int *crtc_vblank_mask)
  10213. {
  10214. struct drm_device *dev = crtc->dev;
  10215. struct drm_i915_private *dev_priv = to_i915(dev);
  10216. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10217. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10218. bool modeset = needs_modeset(new_crtc_state);
  10219. if (modeset) {
  10220. update_scanline_offset(intel_crtc);
  10221. dev_priv->display.crtc_enable(pipe_config, state);
  10222. } else {
  10223. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10224. pipe_config);
  10225. }
  10226. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10227. intel_fbc_enable(
  10228. intel_crtc, pipe_config,
  10229. to_intel_plane_state(crtc->primary->state));
  10230. }
  10231. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10232. if (needs_vblank_wait(pipe_config))
  10233. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  10234. }
  10235. static void intel_update_crtcs(struct drm_atomic_state *state,
  10236. unsigned int *crtc_vblank_mask)
  10237. {
  10238. struct drm_crtc *crtc;
  10239. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10240. int i;
  10241. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10242. if (!new_crtc_state->active)
  10243. continue;
  10244. intel_update_crtc(crtc, state, old_crtc_state,
  10245. new_crtc_state, crtc_vblank_mask);
  10246. }
  10247. }
  10248. static void skl_update_crtcs(struct drm_atomic_state *state,
  10249. unsigned int *crtc_vblank_mask)
  10250. {
  10251. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10252. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10253. struct drm_crtc *crtc;
  10254. struct intel_crtc *intel_crtc;
  10255. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10256. struct intel_crtc_state *cstate;
  10257. unsigned int updated = 0;
  10258. bool progress;
  10259. enum pipe pipe;
  10260. int i;
  10261. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10262. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10263. /* ignore allocations for crtc's that have been turned off. */
  10264. if (new_crtc_state->active)
  10265. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10266. /*
  10267. * Whenever the number of active pipes changes, we need to make sure we
  10268. * update the pipes in the right order so that their ddb allocations
  10269. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10270. * cause pipe underruns and other bad stuff.
  10271. */
  10272. do {
  10273. progress = false;
  10274. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10275. bool vbl_wait = false;
  10276. unsigned int cmask = drm_crtc_mask(crtc);
  10277. intel_crtc = to_intel_crtc(crtc);
  10278. cstate = to_intel_crtc_state(crtc->state);
  10279. pipe = intel_crtc->pipe;
  10280. if (updated & cmask || !cstate->base.active)
  10281. continue;
  10282. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  10283. continue;
  10284. updated |= cmask;
  10285. entries[i] = &cstate->wm.skl.ddb;
  10286. /*
  10287. * If this is an already active pipe, it's DDB changed,
  10288. * and this isn't the last pipe that needs updating
  10289. * then we need to wait for a vblank to pass for the
  10290. * new ddb allocation to take effect.
  10291. */
  10292. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10293. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10294. !new_crtc_state->active_changed &&
  10295. intel_state->wm_results.dirty_pipes != updated)
  10296. vbl_wait = true;
  10297. intel_update_crtc(crtc, state, old_crtc_state,
  10298. new_crtc_state, crtc_vblank_mask);
  10299. if (vbl_wait)
  10300. intel_wait_for_vblank(dev_priv, pipe);
  10301. progress = true;
  10302. }
  10303. } while (progress);
  10304. }
  10305. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10306. {
  10307. struct intel_atomic_state *state, *next;
  10308. struct llist_node *freed;
  10309. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10310. llist_for_each_entry_safe(state, next, freed, freed)
  10311. drm_atomic_state_put(&state->base);
  10312. }
  10313. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10314. {
  10315. struct drm_i915_private *dev_priv =
  10316. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10317. intel_atomic_helper_free_state(dev_priv);
  10318. }
  10319. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10320. {
  10321. struct drm_device *dev = state->dev;
  10322. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10323. struct drm_i915_private *dev_priv = to_i915(dev);
  10324. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10325. struct drm_crtc *crtc;
  10326. struct intel_crtc_state *intel_cstate;
  10327. bool hw_check = intel_state->modeset;
  10328. u64 put_domains[I915_MAX_PIPES] = {};
  10329. unsigned crtc_vblank_mask = 0;
  10330. int i;
  10331. drm_atomic_helper_wait_for_dependencies(state);
  10332. if (intel_state->modeset)
  10333. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10334. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10335. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10336. if (needs_modeset(new_crtc_state) ||
  10337. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10338. hw_check = true;
  10339. put_domains[to_intel_crtc(crtc)->pipe] =
  10340. modeset_get_crtc_power_domains(crtc,
  10341. to_intel_crtc_state(new_crtc_state));
  10342. }
  10343. if (!needs_modeset(new_crtc_state))
  10344. continue;
  10345. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10346. to_intel_crtc_state(new_crtc_state));
  10347. if (old_crtc_state->active) {
  10348. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10349. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10350. intel_crtc->active = false;
  10351. intel_fbc_disable(intel_crtc);
  10352. intel_disable_shared_dpll(intel_crtc);
  10353. /*
  10354. * Underruns don't always raise
  10355. * interrupts, so check manually.
  10356. */
  10357. intel_check_cpu_fifo_underruns(dev_priv);
  10358. intel_check_pch_fifo_underruns(dev_priv);
  10359. if (!crtc->state->active) {
  10360. /*
  10361. * Make sure we don't call initial_watermarks
  10362. * for ILK-style watermark updates.
  10363. *
  10364. * No clue what this is supposed to achieve.
  10365. */
  10366. if (INTEL_GEN(dev_priv) >= 9)
  10367. dev_priv->display.initial_watermarks(intel_state,
  10368. to_intel_crtc_state(crtc->state));
  10369. }
  10370. }
  10371. }
  10372. /* Only after disabling all output pipelines that will be changed can we
  10373. * update the the output configuration. */
  10374. intel_modeset_update_crtc_state(state);
  10375. if (intel_state->modeset) {
  10376. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10377. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10378. /*
  10379. * SKL workaround: bspec recommends we disable the SAGV when we
  10380. * have more then one pipe enabled
  10381. */
  10382. if (!intel_can_enable_sagv(state))
  10383. intel_disable_sagv(dev_priv);
  10384. intel_modeset_verify_disabled(dev, state);
  10385. }
  10386. /* Complete the events for pipes that have now been disabled */
  10387. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10388. bool modeset = needs_modeset(new_crtc_state);
  10389. /* Complete events for now disable pipes here. */
  10390. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10391. spin_lock_irq(&dev->event_lock);
  10392. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10393. spin_unlock_irq(&dev->event_lock);
  10394. new_crtc_state->event = NULL;
  10395. }
  10396. }
  10397. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10398. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  10399. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10400. * already, but still need the state for the delayed optimization. To
  10401. * fix this:
  10402. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10403. * - schedule that vblank worker _before_ calling hw_done
  10404. * - at the start of commit_tail, cancel it _synchrously
  10405. * - switch over to the vblank wait helper in the core after that since
  10406. * we don't need out special handling any more.
  10407. */
  10408. if (!state->legacy_cursor_update)
  10409. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  10410. /*
  10411. * Now that the vblank has passed, we can go ahead and program the
  10412. * optimal watermarks on platforms that need two-step watermark
  10413. * programming.
  10414. *
  10415. * TODO: Move this (and other cleanup) to an async worker eventually.
  10416. */
  10417. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10418. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10419. if (dev_priv->display.optimize_watermarks)
  10420. dev_priv->display.optimize_watermarks(intel_state,
  10421. intel_cstate);
  10422. }
  10423. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10424. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10425. if (put_domains[i])
  10426. modeset_put_power_domains(dev_priv, put_domains[i]);
  10427. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10428. }
  10429. if (intel_state->modeset && intel_can_enable_sagv(state))
  10430. intel_enable_sagv(dev_priv);
  10431. drm_atomic_helper_commit_hw_done(state);
  10432. if (intel_state->modeset) {
  10433. /* As one of the primary mmio accessors, KMS has a high
  10434. * likelihood of triggering bugs in unclaimed access. After we
  10435. * finish modesetting, see if an error has been flagged, and if
  10436. * so enable debugging for the next modeset - and hope we catch
  10437. * the culprit.
  10438. */
  10439. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10440. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10441. }
  10442. mutex_lock(&dev->struct_mutex);
  10443. drm_atomic_helper_cleanup_planes(dev, state);
  10444. mutex_unlock(&dev->struct_mutex);
  10445. drm_atomic_helper_commit_cleanup_done(state);
  10446. drm_atomic_state_put(state);
  10447. intel_atomic_helper_free_state(dev_priv);
  10448. }
  10449. static void intel_atomic_commit_work(struct work_struct *work)
  10450. {
  10451. struct drm_atomic_state *state =
  10452. container_of(work, struct drm_atomic_state, commit_work);
  10453. intel_atomic_commit_tail(state);
  10454. }
  10455. static int __i915_sw_fence_call
  10456. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10457. enum i915_sw_fence_notify notify)
  10458. {
  10459. struct intel_atomic_state *state =
  10460. container_of(fence, struct intel_atomic_state, commit_ready);
  10461. switch (notify) {
  10462. case FENCE_COMPLETE:
  10463. if (state->base.commit_work.func)
  10464. queue_work(system_unbound_wq, &state->base.commit_work);
  10465. break;
  10466. case FENCE_FREE:
  10467. {
  10468. struct intel_atomic_helper *helper =
  10469. &to_i915(state->base.dev)->atomic_helper;
  10470. if (llist_add(&state->freed, &helper->free_list))
  10471. schedule_work(&helper->free_work);
  10472. break;
  10473. }
  10474. }
  10475. return NOTIFY_DONE;
  10476. }
  10477. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10478. {
  10479. struct drm_plane_state *old_plane_state, *new_plane_state;
  10480. struct drm_plane *plane;
  10481. int i;
  10482. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10483. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10484. intel_fb_obj(new_plane_state->fb),
  10485. to_intel_plane(plane)->frontbuffer_bit);
  10486. }
  10487. /**
  10488. * intel_atomic_commit - commit validated state object
  10489. * @dev: DRM device
  10490. * @state: the top-level driver state object
  10491. * @nonblock: nonblocking commit
  10492. *
  10493. * This function commits a top-level state object that has been validated
  10494. * with drm_atomic_helper_check().
  10495. *
  10496. * RETURNS
  10497. * Zero for success or -errno.
  10498. */
  10499. static int intel_atomic_commit(struct drm_device *dev,
  10500. struct drm_atomic_state *state,
  10501. bool nonblock)
  10502. {
  10503. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10504. struct drm_i915_private *dev_priv = to_i915(dev);
  10505. int ret = 0;
  10506. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10507. if (ret)
  10508. return ret;
  10509. drm_atomic_state_get(state);
  10510. i915_sw_fence_init(&intel_state->commit_ready,
  10511. intel_atomic_commit_ready);
  10512. ret = intel_atomic_prepare_commit(dev, state);
  10513. if (ret) {
  10514. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10515. i915_sw_fence_commit(&intel_state->commit_ready);
  10516. return ret;
  10517. }
  10518. /*
  10519. * The intel_legacy_cursor_update() fast path takes care
  10520. * of avoiding the vblank waits for simple cursor
  10521. * movement and flips. For cursor on/off and size changes,
  10522. * we want to perform the vblank waits so that watermark
  10523. * updates happen during the correct frames. Gen9+ have
  10524. * double buffered watermarks and so shouldn't need this.
  10525. *
  10526. * Do this after drm_atomic_helper_setup_commit() and
  10527. * intel_atomic_prepare_commit() because we still want
  10528. * to skip the flip and fb cleanup waits. Although that
  10529. * does risk yanking the mapping from under the display
  10530. * engine.
  10531. *
  10532. * FIXME doing watermarks and fb cleanup from a vblank worker
  10533. * (assuming we had any) would solve these problems.
  10534. */
  10535. if (INTEL_GEN(dev_priv) < 9)
  10536. state->legacy_cursor_update = false;
  10537. drm_atomic_helper_swap_state(state, true);
  10538. dev_priv->wm.distrust_bios_wm = false;
  10539. intel_shared_dpll_swap_state(state);
  10540. intel_atomic_track_fbs(state);
  10541. if (intel_state->modeset) {
  10542. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  10543. sizeof(intel_state->min_pixclk));
  10544. dev_priv->active_crtcs = intel_state->active_crtcs;
  10545. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10546. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10547. }
  10548. drm_atomic_state_get(state);
  10549. INIT_WORK(&state->commit_work,
  10550. nonblock ? intel_atomic_commit_work : NULL);
  10551. i915_sw_fence_commit(&intel_state->commit_ready);
  10552. if (!nonblock) {
  10553. i915_sw_fence_wait(&intel_state->commit_ready);
  10554. intel_atomic_commit_tail(state);
  10555. }
  10556. return 0;
  10557. }
  10558. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10559. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10560. .set_config = drm_atomic_helper_set_config,
  10561. .set_property = drm_atomic_helper_crtc_set_property,
  10562. .destroy = intel_crtc_destroy,
  10563. .page_flip = drm_atomic_helper_page_flip,
  10564. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10565. .atomic_destroy_state = intel_crtc_destroy_state,
  10566. .set_crc_source = intel_crtc_set_crc_source,
  10567. };
  10568. /**
  10569. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10570. * @plane: drm plane to prepare for
  10571. * @fb: framebuffer to prepare for presentation
  10572. *
  10573. * Prepares a framebuffer for usage on a display plane. Generally this
  10574. * involves pinning the underlying object and updating the frontbuffer tracking
  10575. * bits. Some older platforms need special physical address handling for
  10576. * cursor planes.
  10577. *
  10578. * Must be called with struct_mutex held.
  10579. *
  10580. * Returns 0 on success, negative error code on failure.
  10581. */
  10582. int
  10583. intel_prepare_plane_fb(struct drm_plane *plane,
  10584. struct drm_plane_state *new_state)
  10585. {
  10586. struct intel_atomic_state *intel_state =
  10587. to_intel_atomic_state(new_state->state);
  10588. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10589. struct drm_framebuffer *fb = new_state->fb;
  10590. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10591. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10592. int ret;
  10593. if (obj) {
  10594. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10595. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10596. const int align = intel_cursor_alignment(dev_priv);
  10597. ret = i915_gem_object_attach_phys(obj, align);
  10598. if (ret) {
  10599. DRM_DEBUG_KMS("failed to attach phys object\n");
  10600. return ret;
  10601. }
  10602. } else {
  10603. struct i915_vma *vma;
  10604. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  10605. if (IS_ERR(vma)) {
  10606. DRM_DEBUG_KMS("failed to pin object\n");
  10607. return PTR_ERR(vma);
  10608. }
  10609. to_intel_plane_state(new_state)->vma = vma;
  10610. }
  10611. }
  10612. if (!obj && !old_obj)
  10613. return 0;
  10614. if (old_obj) {
  10615. struct drm_crtc_state *crtc_state =
  10616. drm_atomic_get_existing_crtc_state(new_state->state,
  10617. plane->state->crtc);
  10618. /* Big Hammer, we also need to ensure that any pending
  10619. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10620. * current scanout is retired before unpinning the old
  10621. * framebuffer. Note that we rely on userspace rendering
  10622. * into the buffer attached to the pipe they are waiting
  10623. * on. If not, userspace generates a GPU hang with IPEHR
  10624. * point to the MI_WAIT_FOR_EVENT.
  10625. *
  10626. * This should only fail upon a hung GPU, in which case we
  10627. * can safely continue.
  10628. */
  10629. if (needs_modeset(crtc_state)) {
  10630. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10631. old_obj->resv, NULL,
  10632. false, 0,
  10633. GFP_KERNEL);
  10634. if (ret < 0)
  10635. return ret;
  10636. }
  10637. }
  10638. if (new_state->fence) { /* explicit fencing */
  10639. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  10640. new_state->fence,
  10641. I915_FENCE_TIMEOUT,
  10642. GFP_KERNEL);
  10643. if (ret < 0)
  10644. return ret;
  10645. }
  10646. if (!obj)
  10647. return 0;
  10648. if (!new_state->fence) { /* implicit fencing */
  10649. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10650. obj->resv, NULL,
  10651. false, I915_FENCE_TIMEOUT,
  10652. GFP_KERNEL);
  10653. if (ret < 0)
  10654. return ret;
  10655. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10656. }
  10657. return 0;
  10658. }
  10659. /**
  10660. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10661. * @plane: drm plane to clean up for
  10662. * @fb: old framebuffer that was on plane
  10663. *
  10664. * Cleans up a framebuffer that has just been removed from a plane.
  10665. *
  10666. * Must be called with struct_mutex held.
  10667. */
  10668. void
  10669. intel_cleanup_plane_fb(struct drm_plane *plane,
  10670. struct drm_plane_state *old_state)
  10671. {
  10672. struct i915_vma *vma;
  10673. /* Should only be called after a successful intel_prepare_plane_fb()! */
  10674. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  10675. if (vma)
  10676. intel_unpin_fb_vma(vma);
  10677. }
  10678. int
  10679. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  10680. {
  10681. struct drm_i915_private *dev_priv;
  10682. int max_scale;
  10683. int crtc_clock, max_dotclk;
  10684. if (!intel_crtc || !crtc_state->base.enable)
  10685. return DRM_PLANE_HELPER_NO_SCALING;
  10686. dev_priv = to_i915(intel_crtc->base.dev);
  10687. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  10688. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  10689. if (IS_GEMINILAKE(dev_priv))
  10690. max_dotclk *= 2;
  10691. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  10692. return DRM_PLANE_HELPER_NO_SCALING;
  10693. /*
  10694. * skl max scale is lower of:
  10695. * close to 3 but not 3, -1 is for that purpose
  10696. * or
  10697. * cdclk/crtc_clock
  10698. */
  10699. max_scale = min((1 << 16) * 3 - 1,
  10700. (1 << 8) * ((max_dotclk << 8) / crtc_clock));
  10701. return max_scale;
  10702. }
  10703. static int
  10704. intel_check_primary_plane(struct intel_plane *plane,
  10705. struct intel_crtc_state *crtc_state,
  10706. struct intel_plane_state *state)
  10707. {
  10708. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10709. struct drm_crtc *crtc = state->base.crtc;
  10710. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  10711. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  10712. bool can_position = false;
  10713. int ret;
  10714. if (INTEL_GEN(dev_priv) >= 9) {
  10715. /* use scaler when colorkey is not required */
  10716. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  10717. min_scale = 1;
  10718. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  10719. }
  10720. can_position = true;
  10721. }
  10722. ret = drm_plane_helper_check_state(&state->base,
  10723. &state->clip,
  10724. min_scale, max_scale,
  10725. can_position, true);
  10726. if (ret)
  10727. return ret;
  10728. if (!state->base.fb)
  10729. return 0;
  10730. if (INTEL_GEN(dev_priv) >= 9) {
  10731. ret = skl_check_plane_surface(state);
  10732. if (ret)
  10733. return ret;
  10734. state->ctl = skl_plane_ctl(crtc_state, state);
  10735. } else {
  10736. ret = i9xx_check_plane_surface(state);
  10737. if (ret)
  10738. return ret;
  10739. state->ctl = i9xx_plane_ctl(crtc_state, state);
  10740. }
  10741. return 0;
  10742. }
  10743. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  10744. struct drm_crtc_state *old_crtc_state)
  10745. {
  10746. struct drm_device *dev = crtc->dev;
  10747. struct drm_i915_private *dev_priv = to_i915(dev);
  10748. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10749. struct intel_crtc_state *intel_cstate =
  10750. to_intel_crtc_state(crtc->state);
  10751. struct intel_crtc_state *old_intel_cstate =
  10752. to_intel_crtc_state(old_crtc_state);
  10753. struct intel_atomic_state *old_intel_state =
  10754. to_intel_atomic_state(old_crtc_state->state);
  10755. bool modeset = needs_modeset(crtc->state);
  10756. if (!modeset &&
  10757. (intel_cstate->base.color_mgmt_changed ||
  10758. intel_cstate->update_pipe)) {
  10759. intel_color_set_csc(crtc->state);
  10760. intel_color_load_luts(crtc->state);
  10761. }
  10762. /* Perform vblank evasion around commit operation */
  10763. intel_pipe_update_start(intel_crtc);
  10764. if (modeset)
  10765. goto out;
  10766. if (intel_cstate->update_pipe)
  10767. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  10768. else if (INTEL_GEN(dev_priv) >= 9)
  10769. skl_detach_scalers(intel_crtc);
  10770. out:
  10771. if (dev_priv->display.atomic_update_watermarks)
  10772. dev_priv->display.atomic_update_watermarks(old_intel_state,
  10773. intel_cstate);
  10774. }
  10775. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  10776. struct drm_crtc_state *old_crtc_state)
  10777. {
  10778. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10779. intel_pipe_update_end(intel_crtc, NULL);
  10780. }
  10781. /**
  10782. * intel_plane_destroy - destroy a plane
  10783. * @plane: plane to destroy
  10784. *
  10785. * Common destruction function for all types of planes (primary, cursor,
  10786. * sprite).
  10787. */
  10788. void intel_plane_destroy(struct drm_plane *plane)
  10789. {
  10790. drm_plane_cleanup(plane);
  10791. kfree(to_intel_plane(plane));
  10792. }
  10793. const struct drm_plane_funcs intel_plane_funcs = {
  10794. .update_plane = drm_atomic_helper_update_plane,
  10795. .disable_plane = drm_atomic_helper_disable_plane,
  10796. .destroy = intel_plane_destroy,
  10797. .set_property = drm_atomic_helper_plane_set_property,
  10798. .atomic_get_property = intel_plane_atomic_get_property,
  10799. .atomic_set_property = intel_plane_atomic_set_property,
  10800. .atomic_duplicate_state = intel_plane_duplicate_state,
  10801. .atomic_destroy_state = intel_plane_destroy_state,
  10802. };
  10803. static int
  10804. intel_legacy_cursor_update(struct drm_plane *plane,
  10805. struct drm_crtc *crtc,
  10806. struct drm_framebuffer *fb,
  10807. int crtc_x, int crtc_y,
  10808. unsigned int crtc_w, unsigned int crtc_h,
  10809. uint32_t src_x, uint32_t src_y,
  10810. uint32_t src_w, uint32_t src_h,
  10811. struct drm_modeset_acquire_ctx *ctx)
  10812. {
  10813. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  10814. int ret;
  10815. struct drm_plane_state *old_plane_state, *new_plane_state;
  10816. struct intel_plane *intel_plane = to_intel_plane(plane);
  10817. struct drm_framebuffer *old_fb;
  10818. struct drm_crtc_state *crtc_state = crtc->state;
  10819. struct i915_vma *old_vma;
  10820. /*
  10821. * When crtc is inactive or there is a modeset pending,
  10822. * wait for it to complete in the slowpath
  10823. */
  10824. if (!crtc_state->active || needs_modeset(crtc_state) ||
  10825. to_intel_crtc_state(crtc_state)->update_pipe)
  10826. goto slow;
  10827. old_plane_state = plane->state;
  10828. /*
  10829. * If any parameters change that may affect watermarks,
  10830. * take the slowpath. Only changing fb or position should be
  10831. * in the fastpath.
  10832. */
  10833. if (old_plane_state->crtc != crtc ||
  10834. old_plane_state->src_w != src_w ||
  10835. old_plane_state->src_h != src_h ||
  10836. old_plane_state->crtc_w != crtc_w ||
  10837. old_plane_state->crtc_h != crtc_h ||
  10838. !old_plane_state->fb != !fb)
  10839. goto slow;
  10840. new_plane_state = intel_plane_duplicate_state(plane);
  10841. if (!new_plane_state)
  10842. return -ENOMEM;
  10843. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  10844. new_plane_state->src_x = src_x;
  10845. new_plane_state->src_y = src_y;
  10846. new_plane_state->src_w = src_w;
  10847. new_plane_state->src_h = src_h;
  10848. new_plane_state->crtc_x = crtc_x;
  10849. new_plane_state->crtc_y = crtc_y;
  10850. new_plane_state->crtc_w = crtc_w;
  10851. new_plane_state->crtc_h = crtc_h;
  10852. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  10853. to_intel_plane_state(new_plane_state));
  10854. if (ret)
  10855. goto out_free;
  10856. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10857. if (ret)
  10858. goto out_free;
  10859. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10860. int align = intel_cursor_alignment(dev_priv);
  10861. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  10862. if (ret) {
  10863. DRM_DEBUG_KMS("failed to attach phys object\n");
  10864. goto out_unlock;
  10865. }
  10866. } else {
  10867. struct i915_vma *vma;
  10868. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  10869. if (IS_ERR(vma)) {
  10870. DRM_DEBUG_KMS("failed to pin object\n");
  10871. ret = PTR_ERR(vma);
  10872. goto out_unlock;
  10873. }
  10874. to_intel_plane_state(new_plane_state)->vma = vma;
  10875. }
  10876. old_fb = old_plane_state->fb;
  10877. old_vma = to_intel_plane_state(old_plane_state)->vma;
  10878. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  10879. intel_plane->frontbuffer_bit);
  10880. /* Swap plane state */
  10881. new_plane_state->fence = old_plane_state->fence;
  10882. *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
  10883. new_plane_state->fence = NULL;
  10884. new_plane_state->fb = old_fb;
  10885. to_intel_plane_state(new_plane_state)->vma = old_vma;
  10886. if (plane->state->visible) {
  10887. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  10888. intel_plane->update_plane(intel_plane,
  10889. to_intel_crtc_state(crtc->state),
  10890. to_intel_plane_state(plane->state));
  10891. } else {
  10892. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  10893. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  10894. }
  10895. intel_cleanup_plane_fb(plane, new_plane_state);
  10896. out_unlock:
  10897. mutex_unlock(&dev_priv->drm.struct_mutex);
  10898. out_free:
  10899. intel_plane_destroy_state(plane, new_plane_state);
  10900. return ret;
  10901. slow:
  10902. return drm_atomic_helper_update_plane(plane, crtc, fb,
  10903. crtc_x, crtc_y, crtc_w, crtc_h,
  10904. src_x, src_y, src_w, src_h, ctx);
  10905. }
  10906. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  10907. .update_plane = intel_legacy_cursor_update,
  10908. .disable_plane = drm_atomic_helper_disable_plane,
  10909. .destroy = intel_plane_destroy,
  10910. .set_property = drm_atomic_helper_plane_set_property,
  10911. .atomic_get_property = intel_plane_atomic_get_property,
  10912. .atomic_set_property = intel_plane_atomic_set_property,
  10913. .atomic_duplicate_state = intel_plane_duplicate_state,
  10914. .atomic_destroy_state = intel_plane_destroy_state,
  10915. };
  10916. static struct intel_plane *
  10917. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  10918. {
  10919. struct intel_plane *primary = NULL;
  10920. struct intel_plane_state *state = NULL;
  10921. const uint32_t *intel_primary_formats;
  10922. unsigned int supported_rotations;
  10923. unsigned int num_formats;
  10924. int ret;
  10925. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10926. if (!primary) {
  10927. ret = -ENOMEM;
  10928. goto fail;
  10929. }
  10930. state = intel_create_plane_state(&primary->base);
  10931. if (!state) {
  10932. ret = -ENOMEM;
  10933. goto fail;
  10934. }
  10935. primary->base.state = &state->base;
  10936. primary->can_scale = false;
  10937. primary->max_downscale = 1;
  10938. if (INTEL_GEN(dev_priv) >= 9) {
  10939. primary->can_scale = true;
  10940. state->scaler_id = -1;
  10941. }
  10942. primary->pipe = pipe;
  10943. /*
  10944. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  10945. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  10946. */
  10947. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  10948. primary->plane = (enum plane) !pipe;
  10949. else
  10950. primary->plane = (enum plane) pipe;
  10951. primary->id = PLANE_PRIMARY;
  10952. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  10953. primary->check_plane = intel_check_primary_plane;
  10954. if (INTEL_GEN(dev_priv) >= 9) {
  10955. intel_primary_formats = skl_primary_formats;
  10956. num_formats = ARRAY_SIZE(skl_primary_formats);
  10957. primary->update_plane = skylake_update_primary_plane;
  10958. primary->disable_plane = skylake_disable_primary_plane;
  10959. } else if (INTEL_GEN(dev_priv) >= 4) {
  10960. intel_primary_formats = i965_primary_formats;
  10961. num_formats = ARRAY_SIZE(i965_primary_formats);
  10962. primary->update_plane = i9xx_update_primary_plane;
  10963. primary->disable_plane = i9xx_disable_primary_plane;
  10964. } else {
  10965. intel_primary_formats = i8xx_primary_formats;
  10966. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  10967. primary->update_plane = i9xx_update_primary_plane;
  10968. primary->disable_plane = i9xx_disable_primary_plane;
  10969. }
  10970. if (INTEL_GEN(dev_priv) >= 9)
  10971. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  10972. 0, &intel_plane_funcs,
  10973. intel_primary_formats, num_formats,
  10974. DRM_PLANE_TYPE_PRIMARY,
  10975. "plane 1%c", pipe_name(pipe));
  10976. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  10977. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  10978. 0, &intel_plane_funcs,
  10979. intel_primary_formats, num_formats,
  10980. DRM_PLANE_TYPE_PRIMARY,
  10981. "primary %c", pipe_name(pipe));
  10982. else
  10983. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  10984. 0, &intel_plane_funcs,
  10985. intel_primary_formats, num_formats,
  10986. DRM_PLANE_TYPE_PRIMARY,
  10987. "plane %c", plane_name(primary->plane));
  10988. if (ret)
  10989. goto fail;
  10990. if (INTEL_GEN(dev_priv) >= 9) {
  10991. supported_rotations =
  10992. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  10993. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  10994. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  10995. supported_rotations =
  10996. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  10997. DRM_MODE_REFLECT_X;
  10998. } else if (INTEL_GEN(dev_priv) >= 4) {
  10999. supported_rotations =
  11000. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  11001. } else {
  11002. supported_rotations = DRM_MODE_ROTATE_0;
  11003. }
  11004. if (INTEL_GEN(dev_priv) >= 4)
  11005. drm_plane_create_rotation_property(&primary->base,
  11006. DRM_MODE_ROTATE_0,
  11007. supported_rotations);
  11008. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11009. return primary;
  11010. fail:
  11011. kfree(state);
  11012. kfree(primary);
  11013. return ERR_PTR(ret);
  11014. }
  11015. static struct intel_plane *
  11016. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11017. enum pipe pipe)
  11018. {
  11019. struct intel_plane *cursor = NULL;
  11020. struct intel_plane_state *state = NULL;
  11021. int ret;
  11022. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11023. if (!cursor) {
  11024. ret = -ENOMEM;
  11025. goto fail;
  11026. }
  11027. state = intel_create_plane_state(&cursor->base);
  11028. if (!state) {
  11029. ret = -ENOMEM;
  11030. goto fail;
  11031. }
  11032. cursor->base.state = &state->base;
  11033. cursor->can_scale = false;
  11034. cursor->max_downscale = 1;
  11035. cursor->pipe = pipe;
  11036. cursor->plane = pipe;
  11037. cursor->id = PLANE_CURSOR;
  11038. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11039. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11040. cursor->update_plane = i845_update_cursor;
  11041. cursor->disable_plane = i845_disable_cursor;
  11042. cursor->check_plane = i845_check_cursor;
  11043. } else {
  11044. cursor->update_plane = i9xx_update_cursor;
  11045. cursor->disable_plane = i9xx_disable_cursor;
  11046. cursor->check_plane = i9xx_check_cursor;
  11047. }
  11048. cursor->cursor.base = ~0;
  11049. cursor->cursor.cntl = ~0;
  11050. if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
  11051. cursor->cursor.size = ~0;
  11052. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11053. 0, &intel_cursor_plane_funcs,
  11054. intel_cursor_formats,
  11055. ARRAY_SIZE(intel_cursor_formats),
  11056. DRM_PLANE_TYPE_CURSOR,
  11057. "cursor %c", pipe_name(pipe));
  11058. if (ret)
  11059. goto fail;
  11060. if (INTEL_GEN(dev_priv) >= 4)
  11061. drm_plane_create_rotation_property(&cursor->base,
  11062. DRM_MODE_ROTATE_0,
  11063. DRM_MODE_ROTATE_0 |
  11064. DRM_MODE_ROTATE_180);
  11065. if (INTEL_GEN(dev_priv) >= 9)
  11066. state->scaler_id = -1;
  11067. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11068. return cursor;
  11069. fail:
  11070. kfree(state);
  11071. kfree(cursor);
  11072. return ERR_PTR(ret);
  11073. }
  11074. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11075. struct intel_crtc_state *crtc_state)
  11076. {
  11077. struct intel_crtc_scaler_state *scaler_state =
  11078. &crtc_state->scaler_state;
  11079. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11080. int i;
  11081. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11082. if (!crtc->num_scalers)
  11083. return;
  11084. for (i = 0; i < crtc->num_scalers; i++) {
  11085. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11086. scaler->in_use = 0;
  11087. scaler->mode = PS_SCALER_MODE_DYN;
  11088. }
  11089. scaler_state->scaler_id = -1;
  11090. }
  11091. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11092. {
  11093. struct intel_crtc *intel_crtc;
  11094. struct intel_crtc_state *crtc_state = NULL;
  11095. struct intel_plane *primary = NULL;
  11096. struct intel_plane *cursor = NULL;
  11097. int sprite, ret;
  11098. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11099. if (!intel_crtc)
  11100. return -ENOMEM;
  11101. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11102. if (!crtc_state) {
  11103. ret = -ENOMEM;
  11104. goto fail;
  11105. }
  11106. intel_crtc->config = crtc_state;
  11107. intel_crtc->base.state = &crtc_state->base;
  11108. crtc_state->base.crtc = &intel_crtc->base;
  11109. primary = intel_primary_plane_create(dev_priv, pipe);
  11110. if (IS_ERR(primary)) {
  11111. ret = PTR_ERR(primary);
  11112. goto fail;
  11113. }
  11114. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11115. for_each_sprite(dev_priv, pipe, sprite) {
  11116. struct intel_plane *plane;
  11117. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11118. if (IS_ERR(plane)) {
  11119. ret = PTR_ERR(plane);
  11120. goto fail;
  11121. }
  11122. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11123. }
  11124. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11125. if (IS_ERR(cursor)) {
  11126. ret = PTR_ERR(cursor);
  11127. goto fail;
  11128. }
  11129. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11130. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11131. &primary->base, &cursor->base,
  11132. &intel_crtc_funcs,
  11133. "pipe %c", pipe_name(pipe));
  11134. if (ret)
  11135. goto fail;
  11136. intel_crtc->pipe = pipe;
  11137. intel_crtc->plane = primary->plane;
  11138. /* initialize shared scalers */
  11139. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11140. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11141. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11142. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  11143. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11144. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11145. intel_color_init(&intel_crtc->base);
  11146. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11147. return 0;
  11148. fail:
  11149. /*
  11150. * drm_mode_config_cleanup() will free up any
  11151. * crtcs/planes already initialized.
  11152. */
  11153. kfree(crtc_state);
  11154. kfree(intel_crtc);
  11155. return ret;
  11156. }
  11157. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11158. {
  11159. struct drm_device *dev = connector->base.dev;
  11160. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11161. if (!connector->base.state->crtc)
  11162. return INVALID_PIPE;
  11163. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11164. }
  11165. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11166. struct drm_file *file)
  11167. {
  11168. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11169. struct drm_crtc *drmmode_crtc;
  11170. struct intel_crtc *crtc;
  11171. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11172. if (!drmmode_crtc)
  11173. return -ENOENT;
  11174. crtc = to_intel_crtc(drmmode_crtc);
  11175. pipe_from_crtc_id->pipe = crtc->pipe;
  11176. return 0;
  11177. }
  11178. static int intel_encoder_clones(struct intel_encoder *encoder)
  11179. {
  11180. struct drm_device *dev = encoder->base.dev;
  11181. struct intel_encoder *source_encoder;
  11182. int index_mask = 0;
  11183. int entry = 0;
  11184. for_each_intel_encoder(dev, source_encoder) {
  11185. if (encoders_cloneable(encoder, source_encoder))
  11186. index_mask |= (1 << entry);
  11187. entry++;
  11188. }
  11189. return index_mask;
  11190. }
  11191. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11192. {
  11193. if (!IS_MOBILE(dev_priv))
  11194. return false;
  11195. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11196. return false;
  11197. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11198. return false;
  11199. return true;
  11200. }
  11201. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11202. {
  11203. if (INTEL_GEN(dev_priv) >= 9)
  11204. return false;
  11205. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11206. return false;
  11207. if (IS_CHERRYVIEW(dev_priv))
  11208. return false;
  11209. if (HAS_PCH_LPT_H(dev_priv) &&
  11210. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11211. return false;
  11212. /* DDI E can't be used if DDI A requires 4 lanes */
  11213. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11214. return false;
  11215. if (!dev_priv->vbt.int_crt_support)
  11216. return false;
  11217. return true;
  11218. }
  11219. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11220. {
  11221. int pps_num;
  11222. int pps_idx;
  11223. if (HAS_DDI(dev_priv))
  11224. return;
  11225. /*
  11226. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11227. * everywhere where registers can be write protected.
  11228. */
  11229. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11230. pps_num = 2;
  11231. else
  11232. pps_num = 1;
  11233. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11234. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11235. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11236. I915_WRITE(PP_CONTROL(pps_idx), val);
  11237. }
  11238. }
  11239. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11240. {
  11241. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11242. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11243. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11244. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11245. else
  11246. dev_priv->pps_mmio_base = PPS_BASE;
  11247. intel_pps_unlock_regs_wa(dev_priv);
  11248. }
  11249. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11250. {
  11251. struct intel_encoder *encoder;
  11252. bool dpd_is_edp = false;
  11253. intel_pps_init(dev_priv);
  11254. /*
  11255. * intel_edp_init_connector() depends on this completing first, to
  11256. * prevent the registeration of both eDP and LVDS and the incorrect
  11257. * sharing of the PPS.
  11258. */
  11259. intel_lvds_init(dev_priv);
  11260. if (intel_crt_present(dev_priv))
  11261. intel_crt_init(dev_priv);
  11262. if (IS_GEN9_LP(dev_priv)) {
  11263. /*
  11264. * FIXME: Broxton doesn't support port detection via the
  11265. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11266. * detect the ports.
  11267. */
  11268. intel_ddi_init(dev_priv, PORT_A);
  11269. intel_ddi_init(dev_priv, PORT_B);
  11270. intel_ddi_init(dev_priv, PORT_C);
  11271. intel_dsi_init(dev_priv);
  11272. } else if (HAS_DDI(dev_priv)) {
  11273. int found;
  11274. /*
  11275. * Haswell uses DDI functions to detect digital outputs.
  11276. * On SKL pre-D0 the strap isn't connected, so we assume
  11277. * it's there.
  11278. */
  11279. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11280. /* WaIgnoreDDIAStrap: skl */
  11281. if (found || IS_GEN9_BC(dev_priv))
  11282. intel_ddi_init(dev_priv, PORT_A);
  11283. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11284. * register */
  11285. found = I915_READ(SFUSE_STRAP);
  11286. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11287. intel_ddi_init(dev_priv, PORT_B);
  11288. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11289. intel_ddi_init(dev_priv, PORT_C);
  11290. if (found & SFUSE_STRAP_DDID_DETECTED)
  11291. intel_ddi_init(dev_priv, PORT_D);
  11292. /*
  11293. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11294. */
  11295. if (IS_GEN9_BC(dev_priv) &&
  11296. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11297. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11298. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11299. intel_ddi_init(dev_priv, PORT_E);
  11300. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11301. int found;
  11302. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  11303. if (has_edp_a(dev_priv))
  11304. intel_dp_init(dev_priv, DP_A, PORT_A);
  11305. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11306. /* PCH SDVOB multiplex with HDMIB */
  11307. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11308. if (!found)
  11309. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11310. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11311. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11312. }
  11313. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11314. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11315. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11316. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11317. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11318. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11319. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11320. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11321. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11322. bool has_edp, has_port;
  11323. /*
  11324. * The DP_DETECTED bit is the latched state of the DDC
  11325. * SDA pin at boot. However since eDP doesn't require DDC
  11326. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11327. * eDP ports may have been muxed to an alternate function.
  11328. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11329. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11330. * detect eDP ports.
  11331. *
  11332. * Sadly the straps seem to be missing sometimes even for HDMI
  11333. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11334. * and VBT for the presence of the port. Additionally we can't
  11335. * trust the port type the VBT declares as we've seen at least
  11336. * HDMI ports that the VBT claim are DP or eDP.
  11337. */
  11338. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  11339. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11340. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11341. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11342. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11343. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11344. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  11345. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11346. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11347. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11348. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11349. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11350. if (IS_CHERRYVIEW(dev_priv)) {
  11351. /*
  11352. * eDP not supported on port D,
  11353. * so no need to worry about it
  11354. */
  11355. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11356. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11357. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11358. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11359. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11360. }
  11361. intel_dsi_init(dev_priv);
  11362. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11363. bool found = false;
  11364. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11365. DRM_DEBUG_KMS("probing SDVOB\n");
  11366. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11367. if (!found && IS_G4X(dev_priv)) {
  11368. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11369. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11370. }
  11371. if (!found && IS_G4X(dev_priv))
  11372. intel_dp_init(dev_priv, DP_B, PORT_B);
  11373. }
  11374. /* Before G4X SDVOC doesn't have its own detect register */
  11375. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11376. DRM_DEBUG_KMS("probing SDVOC\n");
  11377. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11378. }
  11379. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11380. if (IS_G4X(dev_priv)) {
  11381. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11382. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11383. }
  11384. if (IS_G4X(dev_priv))
  11385. intel_dp_init(dev_priv, DP_C, PORT_C);
  11386. }
  11387. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11388. intel_dp_init(dev_priv, DP_D, PORT_D);
  11389. } else if (IS_GEN2(dev_priv))
  11390. intel_dvo_init(dev_priv);
  11391. if (SUPPORTS_TV(dev_priv))
  11392. intel_tv_init(dev_priv);
  11393. intel_psr_init(dev_priv);
  11394. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11395. encoder->base.possible_crtcs = encoder->crtc_mask;
  11396. encoder->base.possible_clones =
  11397. intel_encoder_clones(encoder);
  11398. }
  11399. intel_init_pch_refclk(dev_priv);
  11400. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11401. }
  11402. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11403. {
  11404. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11405. drm_framebuffer_cleanup(fb);
  11406. i915_gem_object_lock(intel_fb->obj);
  11407. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11408. i915_gem_object_unlock(intel_fb->obj);
  11409. i915_gem_object_put(intel_fb->obj);
  11410. kfree(intel_fb);
  11411. }
  11412. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11413. struct drm_file *file,
  11414. unsigned int *handle)
  11415. {
  11416. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11417. struct drm_i915_gem_object *obj = intel_fb->obj;
  11418. if (obj->userptr.mm) {
  11419. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11420. return -EINVAL;
  11421. }
  11422. return drm_gem_handle_create(file, &obj->base, handle);
  11423. }
  11424. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11425. struct drm_file *file,
  11426. unsigned flags, unsigned color,
  11427. struct drm_clip_rect *clips,
  11428. unsigned num_clips)
  11429. {
  11430. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11431. i915_gem_object_flush_if_display(obj);
  11432. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11433. return 0;
  11434. }
  11435. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11436. .destroy = intel_user_framebuffer_destroy,
  11437. .create_handle = intel_user_framebuffer_create_handle,
  11438. .dirty = intel_user_framebuffer_dirty,
  11439. };
  11440. static
  11441. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11442. uint64_t fb_modifier, uint32_t pixel_format)
  11443. {
  11444. u32 gen = INTEL_GEN(dev_priv);
  11445. if (gen >= 9) {
  11446. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11447. /* "The stride in bytes must not exceed the of the size of 8K
  11448. * pixels and 32K bytes."
  11449. */
  11450. return min(8192 * cpp, 32768);
  11451. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11452. return 32*1024;
  11453. } else if (gen >= 4) {
  11454. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11455. return 16*1024;
  11456. else
  11457. return 32*1024;
  11458. } else if (gen >= 3) {
  11459. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11460. return 8*1024;
  11461. else
  11462. return 16*1024;
  11463. } else {
  11464. /* XXX DSPC is limited to 4k tiled */
  11465. return 8*1024;
  11466. }
  11467. }
  11468. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11469. struct drm_i915_gem_object *obj,
  11470. struct drm_mode_fb_cmd2 *mode_cmd)
  11471. {
  11472. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11473. struct drm_format_name_buf format_name;
  11474. u32 pitch_limit, stride_alignment;
  11475. unsigned int tiling, stride;
  11476. int ret = -EINVAL;
  11477. i915_gem_object_lock(obj);
  11478. obj->framebuffer_references++;
  11479. tiling = i915_gem_object_get_tiling(obj);
  11480. stride = i915_gem_object_get_stride(obj);
  11481. i915_gem_object_unlock(obj);
  11482. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11483. /*
  11484. * If there's a fence, enforce that
  11485. * the fb modifier and tiling mode match.
  11486. */
  11487. if (tiling != I915_TILING_NONE &&
  11488. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11489. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11490. goto err;
  11491. }
  11492. } else {
  11493. if (tiling == I915_TILING_X) {
  11494. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11495. } else if (tiling == I915_TILING_Y) {
  11496. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11497. goto err;
  11498. }
  11499. }
  11500. /* Passed in modifier sanity checking. */
  11501. switch (mode_cmd->modifier[0]) {
  11502. case I915_FORMAT_MOD_Y_TILED:
  11503. case I915_FORMAT_MOD_Yf_TILED:
  11504. if (INTEL_GEN(dev_priv) < 9) {
  11505. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  11506. mode_cmd->modifier[0]);
  11507. goto err;
  11508. }
  11509. case DRM_FORMAT_MOD_LINEAR:
  11510. case I915_FORMAT_MOD_X_TILED:
  11511. break;
  11512. default:
  11513. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  11514. mode_cmd->modifier[0]);
  11515. goto err;
  11516. }
  11517. /*
  11518. * gen2/3 display engine uses the fence if present,
  11519. * so the tiling mode must match the fb modifier exactly.
  11520. */
  11521. if (INTEL_INFO(dev_priv)->gen < 4 &&
  11522. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11523. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  11524. goto err;
  11525. }
  11526. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11527. mode_cmd->pixel_format);
  11528. if (mode_cmd->pitches[0] > pitch_limit) {
  11529. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  11530. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  11531. "tiled" : "linear",
  11532. mode_cmd->pitches[0], pitch_limit);
  11533. goto err;
  11534. }
  11535. /*
  11536. * If there's a fence, enforce that
  11537. * the fb pitch and fence stride match.
  11538. */
  11539. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  11540. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  11541. mode_cmd->pitches[0], stride);
  11542. goto err;
  11543. }
  11544. /* Reject formats not supported by any plane early. */
  11545. switch (mode_cmd->pixel_format) {
  11546. case DRM_FORMAT_C8:
  11547. case DRM_FORMAT_RGB565:
  11548. case DRM_FORMAT_XRGB8888:
  11549. case DRM_FORMAT_ARGB8888:
  11550. break;
  11551. case DRM_FORMAT_XRGB1555:
  11552. if (INTEL_GEN(dev_priv) > 3) {
  11553. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11554. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11555. goto err;
  11556. }
  11557. break;
  11558. case DRM_FORMAT_ABGR8888:
  11559. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  11560. INTEL_GEN(dev_priv) < 9) {
  11561. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11562. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11563. goto err;
  11564. }
  11565. break;
  11566. case DRM_FORMAT_XBGR8888:
  11567. case DRM_FORMAT_XRGB2101010:
  11568. case DRM_FORMAT_XBGR2101010:
  11569. if (INTEL_GEN(dev_priv) < 4) {
  11570. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11571. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11572. goto err;
  11573. }
  11574. break;
  11575. case DRM_FORMAT_ABGR2101010:
  11576. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  11577. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11578. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11579. goto err;
  11580. }
  11581. break;
  11582. case DRM_FORMAT_YUYV:
  11583. case DRM_FORMAT_UYVY:
  11584. case DRM_FORMAT_YVYU:
  11585. case DRM_FORMAT_VYUY:
  11586. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  11587. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11588. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11589. goto err;
  11590. }
  11591. break;
  11592. default:
  11593. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11594. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11595. goto err;
  11596. }
  11597. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11598. if (mode_cmd->offsets[0] != 0)
  11599. goto err;
  11600. drm_helper_mode_fill_fb_struct(&dev_priv->drm,
  11601. &intel_fb->base, mode_cmd);
  11602. stride_alignment = intel_fb_stride_alignment(&intel_fb->base, 0);
  11603. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11604. DRM_DEBUG_KMS("pitch (%d) must be at least %u byte aligned\n",
  11605. mode_cmd->pitches[0], stride_alignment);
  11606. goto err;
  11607. }
  11608. intel_fb->obj = obj;
  11609. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  11610. if (ret)
  11611. goto err;
  11612. ret = drm_framebuffer_init(obj->base.dev,
  11613. &intel_fb->base,
  11614. &intel_fb_funcs);
  11615. if (ret) {
  11616. DRM_ERROR("framebuffer init failed %d\n", ret);
  11617. goto err;
  11618. }
  11619. return 0;
  11620. err:
  11621. i915_gem_object_lock(obj);
  11622. obj->framebuffer_references--;
  11623. i915_gem_object_unlock(obj);
  11624. return ret;
  11625. }
  11626. static struct drm_framebuffer *
  11627. intel_user_framebuffer_create(struct drm_device *dev,
  11628. struct drm_file *filp,
  11629. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  11630. {
  11631. struct drm_framebuffer *fb;
  11632. struct drm_i915_gem_object *obj;
  11633. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  11634. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  11635. if (!obj)
  11636. return ERR_PTR(-ENOENT);
  11637. fb = intel_framebuffer_create(obj, &mode_cmd);
  11638. if (IS_ERR(fb))
  11639. i915_gem_object_put(obj);
  11640. return fb;
  11641. }
  11642. static void intel_atomic_state_free(struct drm_atomic_state *state)
  11643. {
  11644. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11645. drm_atomic_state_default_release(state);
  11646. i915_sw_fence_fini(&intel_state->commit_ready);
  11647. kfree(state);
  11648. }
  11649. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11650. .fb_create = intel_user_framebuffer_create,
  11651. .output_poll_changed = intel_fbdev_output_poll_changed,
  11652. .atomic_check = intel_atomic_check,
  11653. .atomic_commit = intel_atomic_commit,
  11654. .atomic_state_alloc = intel_atomic_state_alloc,
  11655. .atomic_state_clear = intel_atomic_state_clear,
  11656. .atomic_state_free = intel_atomic_state_free,
  11657. };
  11658. /**
  11659. * intel_init_display_hooks - initialize the display modesetting hooks
  11660. * @dev_priv: device private
  11661. */
  11662. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  11663. {
  11664. intel_init_cdclk_hooks(dev_priv);
  11665. if (INTEL_INFO(dev_priv)->gen >= 9) {
  11666. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11667. dev_priv->display.get_initial_plane_config =
  11668. skylake_get_initial_plane_config;
  11669. dev_priv->display.crtc_compute_clock =
  11670. haswell_crtc_compute_clock;
  11671. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11672. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11673. } else if (HAS_DDI(dev_priv)) {
  11674. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11675. dev_priv->display.get_initial_plane_config =
  11676. ironlake_get_initial_plane_config;
  11677. dev_priv->display.crtc_compute_clock =
  11678. haswell_crtc_compute_clock;
  11679. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11680. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11681. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11682. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  11683. dev_priv->display.get_initial_plane_config =
  11684. ironlake_get_initial_plane_config;
  11685. dev_priv->display.crtc_compute_clock =
  11686. ironlake_crtc_compute_clock;
  11687. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  11688. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  11689. } else if (IS_CHERRYVIEW(dev_priv)) {
  11690. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11691. dev_priv->display.get_initial_plane_config =
  11692. i9xx_get_initial_plane_config;
  11693. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  11694. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11695. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11696. } else if (IS_VALLEYVIEW(dev_priv)) {
  11697. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11698. dev_priv->display.get_initial_plane_config =
  11699. i9xx_get_initial_plane_config;
  11700. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  11701. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11702. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11703. } else if (IS_G4X(dev_priv)) {
  11704. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11705. dev_priv->display.get_initial_plane_config =
  11706. i9xx_get_initial_plane_config;
  11707. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  11708. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11709. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11710. } else if (IS_PINEVIEW(dev_priv)) {
  11711. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11712. dev_priv->display.get_initial_plane_config =
  11713. i9xx_get_initial_plane_config;
  11714. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  11715. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11716. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11717. } else if (!IS_GEN2(dev_priv)) {
  11718. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11719. dev_priv->display.get_initial_plane_config =
  11720. i9xx_get_initial_plane_config;
  11721. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  11722. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11723. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11724. } else {
  11725. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11726. dev_priv->display.get_initial_plane_config =
  11727. i9xx_get_initial_plane_config;
  11728. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  11729. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11730. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11731. }
  11732. if (IS_GEN5(dev_priv)) {
  11733. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  11734. } else if (IS_GEN6(dev_priv)) {
  11735. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  11736. } else if (IS_IVYBRIDGE(dev_priv)) {
  11737. /* FIXME: detect B0+ stepping and use auto training */
  11738. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  11739. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  11740. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  11741. }
  11742. if (dev_priv->info.gen >= 9)
  11743. dev_priv->display.update_crtcs = skl_update_crtcs;
  11744. else
  11745. dev_priv->display.update_crtcs = intel_update_crtcs;
  11746. }
  11747. /*
  11748. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  11749. */
  11750. static void quirk_ssc_force_disable(struct drm_device *dev)
  11751. {
  11752. struct drm_i915_private *dev_priv = to_i915(dev);
  11753. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  11754. DRM_INFO("applying lvds SSC disable quirk\n");
  11755. }
  11756. /*
  11757. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  11758. * brightness value
  11759. */
  11760. static void quirk_invert_brightness(struct drm_device *dev)
  11761. {
  11762. struct drm_i915_private *dev_priv = to_i915(dev);
  11763. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  11764. DRM_INFO("applying inverted panel brightness quirk\n");
  11765. }
  11766. /* Some VBT's incorrectly indicate no backlight is present */
  11767. static void quirk_backlight_present(struct drm_device *dev)
  11768. {
  11769. struct drm_i915_private *dev_priv = to_i915(dev);
  11770. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  11771. DRM_INFO("applying backlight present quirk\n");
  11772. }
  11773. /* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
  11774. * which is 300 ms greater than eDP spec T12 min.
  11775. */
  11776. static void quirk_increase_t12_delay(struct drm_device *dev)
  11777. {
  11778. struct drm_i915_private *dev_priv = to_i915(dev);
  11779. dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
  11780. DRM_INFO("Applying T12 delay quirk\n");
  11781. }
  11782. struct intel_quirk {
  11783. int device;
  11784. int subsystem_vendor;
  11785. int subsystem_device;
  11786. void (*hook)(struct drm_device *dev);
  11787. };
  11788. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  11789. struct intel_dmi_quirk {
  11790. void (*hook)(struct drm_device *dev);
  11791. const struct dmi_system_id (*dmi_id_list)[];
  11792. };
  11793. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  11794. {
  11795. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  11796. return 1;
  11797. }
  11798. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  11799. {
  11800. .dmi_id_list = &(const struct dmi_system_id[]) {
  11801. {
  11802. .callback = intel_dmi_reverse_brightness,
  11803. .ident = "NCR Corporation",
  11804. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  11805. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  11806. },
  11807. },
  11808. { } /* terminating entry */
  11809. },
  11810. .hook = quirk_invert_brightness,
  11811. },
  11812. };
  11813. static struct intel_quirk intel_quirks[] = {
  11814. /* Lenovo U160 cannot use SSC on LVDS */
  11815. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  11816. /* Sony Vaio Y cannot use SSC on LVDS */
  11817. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  11818. /* Acer Aspire 5734Z must invert backlight brightness */
  11819. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  11820. /* Acer/eMachines G725 */
  11821. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  11822. /* Acer/eMachines e725 */
  11823. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  11824. /* Acer/Packard Bell NCL20 */
  11825. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  11826. /* Acer Aspire 4736Z */
  11827. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  11828. /* Acer Aspire 5336 */
  11829. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  11830. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  11831. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  11832. /* Acer C720 Chromebook (Core i3 4005U) */
  11833. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  11834. /* Apple Macbook 2,1 (Core 2 T7400) */
  11835. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  11836. /* Apple Macbook 4,1 */
  11837. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  11838. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  11839. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  11840. /* HP Chromebook 14 (Celeron 2955U) */
  11841. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  11842. /* Dell Chromebook 11 */
  11843. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  11844. /* Dell Chromebook 11 (2015 version) */
  11845. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  11846. /* Toshiba Satellite P50-C-18C */
  11847. { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
  11848. };
  11849. static void intel_init_quirks(struct drm_device *dev)
  11850. {
  11851. struct pci_dev *d = dev->pdev;
  11852. int i;
  11853. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  11854. struct intel_quirk *q = &intel_quirks[i];
  11855. if (d->device == q->device &&
  11856. (d->subsystem_vendor == q->subsystem_vendor ||
  11857. q->subsystem_vendor == PCI_ANY_ID) &&
  11858. (d->subsystem_device == q->subsystem_device ||
  11859. q->subsystem_device == PCI_ANY_ID))
  11860. q->hook(dev);
  11861. }
  11862. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  11863. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  11864. intel_dmi_quirks[i].hook(dev);
  11865. }
  11866. }
  11867. /* Disable the VGA plane that we never use */
  11868. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  11869. {
  11870. struct pci_dev *pdev = dev_priv->drm.pdev;
  11871. u8 sr1;
  11872. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  11873. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  11874. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  11875. outb(SR01, VGA_SR_INDEX);
  11876. sr1 = inb(VGA_SR_DATA);
  11877. outb(sr1 | 1<<5, VGA_SR_DATA);
  11878. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  11879. udelay(300);
  11880. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  11881. POSTING_READ(vga_reg);
  11882. }
  11883. void intel_modeset_init_hw(struct drm_device *dev)
  11884. {
  11885. struct drm_i915_private *dev_priv = to_i915(dev);
  11886. intel_update_cdclk(dev_priv);
  11887. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  11888. intel_init_clock_gating(dev_priv);
  11889. }
  11890. /*
  11891. * Calculate what we think the watermarks should be for the state we've read
  11892. * out of the hardware and then immediately program those watermarks so that
  11893. * we ensure the hardware settings match our internal state.
  11894. *
  11895. * We can calculate what we think WM's should be by creating a duplicate of the
  11896. * current state (which was constructed during hardware readout) and running it
  11897. * through the atomic check code to calculate new watermark values in the
  11898. * state object.
  11899. */
  11900. static void sanitize_watermarks(struct drm_device *dev)
  11901. {
  11902. struct drm_i915_private *dev_priv = to_i915(dev);
  11903. struct drm_atomic_state *state;
  11904. struct intel_atomic_state *intel_state;
  11905. struct drm_crtc *crtc;
  11906. struct drm_crtc_state *cstate;
  11907. struct drm_modeset_acquire_ctx ctx;
  11908. int ret;
  11909. int i;
  11910. /* Only supported on platforms that use atomic watermark design */
  11911. if (!dev_priv->display.optimize_watermarks)
  11912. return;
  11913. /*
  11914. * We need to hold connection_mutex before calling duplicate_state so
  11915. * that the connector loop is protected.
  11916. */
  11917. drm_modeset_acquire_init(&ctx, 0);
  11918. retry:
  11919. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  11920. if (ret == -EDEADLK) {
  11921. drm_modeset_backoff(&ctx);
  11922. goto retry;
  11923. } else if (WARN_ON(ret)) {
  11924. goto fail;
  11925. }
  11926. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  11927. if (WARN_ON(IS_ERR(state)))
  11928. goto fail;
  11929. intel_state = to_intel_atomic_state(state);
  11930. /*
  11931. * Hardware readout is the only time we don't want to calculate
  11932. * intermediate watermarks (since we don't trust the current
  11933. * watermarks).
  11934. */
  11935. if (!HAS_GMCH_DISPLAY(dev_priv))
  11936. intel_state->skip_intermediate_wm = true;
  11937. ret = intel_atomic_check(dev, state);
  11938. if (ret) {
  11939. /*
  11940. * If we fail here, it means that the hardware appears to be
  11941. * programmed in a way that shouldn't be possible, given our
  11942. * understanding of watermark requirements. This might mean a
  11943. * mistake in the hardware readout code or a mistake in the
  11944. * watermark calculations for a given platform. Raise a WARN
  11945. * so that this is noticeable.
  11946. *
  11947. * If this actually happens, we'll have to just leave the
  11948. * BIOS-programmed watermarks untouched and hope for the best.
  11949. */
  11950. WARN(true, "Could not determine valid watermarks for inherited state\n");
  11951. goto put_state;
  11952. }
  11953. /* Write calculated watermark values back */
  11954. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  11955. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  11956. cs->wm.need_postvbl_update = true;
  11957. dev_priv->display.optimize_watermarks(intel_state, cs);
  11958. }
  11959. put_state:
  11960. drm_atomic_state_put(state);
  11961. fail:
  11962. drm_modeset_drop_locks(&ctx);
  11963. drm_modeset_acquire_fini(&ctx);
  11964. }
  11965. int intel_modeset_init(struct drm_device *dev)
  11966. {
  11967. struct drm_i915_private *dev_priv = to_i915(dev);
  11968. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  11969. enum pipe pipe;
  11970. struct intel_crtc *crtc;
  11971. drm_mode_config_init(dev);
  11972. dev->mode_config.min_width = 0;
  11973. dev->mode_config.min_height = 0;
  11974. dev->mode_config.preferred_depth = 24;
  11975. dev->mode_config.prefer_shadow = 1;
  11976. dev->mode_config.allow_fb_modifiers = true;
  11977. dev->mode_config.funcs = &intel_mode_funcs;
  11978. init_llist_head(&dev_priv->atomic_helper.free_list);
  11979. INIT_WORK(&dev_priv->atomic_helper.free_work,
  11980. intel_atomic_helper_free_state_worker);
  11981. intel_init_quirks(dev);
  11982. intel_init_pm(dev_priv);
  11983. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  11984. return 0;
  11985. /*
  11986. * There may be no VBT; and if the BIOS enabled SSC we can
  11987. * just keep using it to avoid unnecessary flicker. Whereas if the
  11988. * BIOS isn't using it, don't assume it will work even if the VBT
  11989. * indicates as much.
  11990. */
  11991. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  11992. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  11993. DREF_SSC1_ENABLE);
  11994. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  11995. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  11996. bios_lvds_use_ssc ? "en" : "dis",
  11997. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  11998. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  11999. }
  12000. }
  12001. if (IS_GEN2(dev_priv)) {
  12002. dev->mode_config.max_width = 2048;
  12003. dev->mode_config.max_height = 2048;
  12004. } else if (IS_GEN3(dev_priv)) {
  12005. dev->mode_config.max_width = 4096;
  12006. dev->mode_config.max_height = 4096;
  12007. } else {
  12008. dev->mode_config.max_width = 8192;
  12009. dev->mode_config.max_height = 8192;
  12010. }
  12011. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12012. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12013. dev->mode_config.cursor_height = 1023;
  12014. } else if (IS_GEN2(dev_priv)) {
  12015. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12016. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12017. } else {
  12018. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12019. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12020. }
  12021. dev->mode_config.fb_base = ggtt->mappable_base;
  12022. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12023. INTEL_INFO(dev_priv)->num_pipes,
  12024. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12025. for_each_pipe(dev_priv, pipe) {
  12026. int ret;
  12027. ret = intel_crtc_init(dev_priv, pipe);
  12028. if (ret) {
  12029. drm_mode_config_cleanup(dev);
  12030. return ret;
  12031. }
  12032. }
  12033. intel_shared_dpll_init(dev);
  12034. intel_update_czclk(dev_priv);
  12035. intel_modeset_init_hw(dev);
  12036. if (dev_priv->max_cdclk_freq == 0)
  12037. intel_update_max_cdclk(dev_priv);
  12038. /* Just disable it once at startup */
  12039. i915_disable_vga(dev_priv);
  12040. intel_setup_outputs(dev_priv);
  12041. drm_modeset_lock_all(dev);
  12042. intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
  12043. drm_modeset_unlock_all(dev);
  12044. for_each_intel_crtc(dev, crtc) {
  12045. struct intel_initial_plane_config plane_config = {};
  12046. if (!crtc->active)
  12047. continue;
  12048. /*
  12049. * Note that reserving the BIOS fb up front prevents us
  12050. * from stuffing other stolen allocations like the ring
  12051. * on top. This prevents some ugliness at boot time, and
  12052. * can even allow for smooth boot transitions if the BIOS
  12053. * fb is large enough for the active pipe configuration.
  12054. */
  12055. dev_priv->display.get_initial_plane_config(crtc,
  12056. &plane_config);
  12057. /*
  12058. * If the fb is shared between multiple heads, we'll
  12059. * just get the first one.
  12060. */
  12061. intel_find_initial_plane_obj(crtc, &plane_config);
  12062. }
  12063. /*
  12064. * Make sure hardware watermarks really match the state we read out.
  12065. * Note that we need to do this after reconstructing the BIOS fb's
  12066. * since the watermark calculation done here will use pstate->fb.
  12067. */
  12068. if (!HAS_GMCH_DISPLAY(dev_priv))
  12069. sanitize_watermarks(dev);
  12070. return 0;
  12071. }
  12072. void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12073. {
  12074. /* 640x480@60Hz, ~25175 kHz */
  12075. struct dpll clock = {
  12076. .m1 = 18,
  12077. .m2 = 7,
  12078. .p1 = 13,
  12079. .p2 = 4,
  12080. .n = 2,
  12081. };
  12082. u32 dpll, fp;
  12083. int i;
  12084. WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
  12085. DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
  12086. pipe_name(pipe), clock.vco, clock.dot);
  12087. fp = i9xx_dpll_compute_fp(&clock);
  12088. dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
  12089. DPLL_VGA_MODE_DIS |
  12090. ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
  12091. PLL_P2_DIVIDE_BY_4 |
  12092. PLL_REF_INPUT_DREFCLK |
  12093. DPLL_VCO_ENABLE;
  12094. I915_WRITE(FP0(pipe), fp);
  12095. I915_WRITE(FP1(pipe), fp);
  12096. I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
  12097. I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
  12098. I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
  12099. I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
  12100. I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
  12101. I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
  12102. I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
  12103. /*
  12104. * Apparently we need to have VGA mode enabled prior to changing
  12105. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  12106. * dividers, even though the register value does change.
  12107. */
  12108. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
  12109. I915_WRITE(DPLL(pipe), dpll);
  12110. /* Wait for the clocks to stabilize. */
  12111. POSTING_READ(DPLL(pipe));
  12112. udelay(150);
  12113. /* The pixel multiplier can only be updated once the
  12114. * DPLL is enabled and the clocks are stable.
  12115. *
  12116. * So write it again.
  12117. */
  12118. I915_WRITE(DPLL(pipe), dpll);
  12119. /* We do this three times for luck */
  12120. for (i = 0; i < 3 ; i++) {
  12121. I915_WRITE(DPLL(pipe), dpll);
  12122. POSTING_READ(DPLL(pipe));
  12123. udelay(150); /* wait for warmup */
  12124. }
  12125. I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
  12126. POSTING_READ(PIPECONF(pipe));
  12127. }
  12128. void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12129. {
  12130. DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
  12131. pipe_name(pipe));
  12132. assert_plane_disabled(dev_priv, PLANE_A);
  12133. assert_plane_disabled(dev_priv, PLANE_B);
  12134. I915_WRITE(PIPECONF(pipe), 0);
  12135. POSTING_READ(PIPECONF(pipe));
  12136. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  12137. DRM_ERROR("pipe %c off wait timed out\n", pipe_name(pipe));
  12138. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  12139. POSTING_READ(DPLL(pipe));
  12140. }
  12141. static bool
  12142. intel_check_plane_mapping(struct intel_crtc *crtc)
  12143. {
  12144. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12145. u32 val;
  12146. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  12147. return true;
  12148. val = I915_READ(DSPCNTR(!crtc->plane));
  12149. if ((val & DISPLAY_PLANE_ENABLE) &&
  12150. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12151. return false;
  12152. return true;
  12153. }
  12154. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12155. {
  12156. struct drm_device *dev = crtc->base.dev;
  12157. struct intel_encoder *encoder;
  12158. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12159. return true;
  12160. return false;
  12161. }
  12162. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12163. {
  12164. struct drm_device *dev = encoder->base.dev;
  12165. struct intel_connector *connector;
  12166. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12167. return connector;
  12168. return NULL;
  12169. }
  12170. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12171. enum transcoder pch_transcoder)
  12172. {
  12173. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12174. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  12175. }
  12176. static void intel_sanitize_crtc(struct intel_crtc *crtc,
  12177. struct drm_modeset_acquire_ctx *ctx)
  12178. {
  12179. struct drm_device *dev = crtc->base.dev;
  12180. struct drm_i915_private *dev_priv = to_i915(dev);
  12181. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12182. /* Clear any frame start delays used for debugging left by the BIOS */
  12183. if (!transcoder_is_dsi(cpu_transcoder)) {
  12184. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12185. I915_WRITE(reg,
  12186. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12187. }
  12188. /* restore vblank interrupts to correct state */
  12189. drm_crtc_vblank_reset(&crtc->base);
  12190. if (crtc->active) {
  12191. struct intel_plane *plane;
  12192. drm_crtc_vblank_on(&crtc->base);
  12193. /* Disable everything but the primary plane */
  12194. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12195. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12196. continue;
  12197. trace_intel_disable_plane(&plane->base, crtc);
  12198. plane->disable_plane(plane, crtc);
  12199. }
  12200. }
  12201. /* We need to sanitize the plane -> pipe mapping first because this will
  12202. * disable the crtc (and hence change the state) if it is wrong. Note
  12203. * that gen4+ has a fixed plane -> pipe mapping. */
  12204. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  12205. bool plane;
  12206. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  12207. crtc->base.base.id, crtc->base.name);
  12208. /* Pipe has the wrong plane attached and the plane is active.
  12209. * Temporarily change the plane mapping and disable everything
  12210. * ... */
  12211. plane = crtc->plane;
  12212. crtc->base.primary->state->visible = true;
  12213. crtc->plane = !plane;
  12214. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12215. crtc->plane = plane;
  12216. }
  12217. /* Adjust the state of the output pipe according to whether we
  12218. * have active connectors/encoders. */
  12219. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12220. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12221. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12222. /*
  12223. * We start out with underrun reporting disabled to avoid races.
  12224. * For correct bookkeeping mark this on active crtcs.
  12225. *
  12226. * Also on gmch platforms we dont have any hardware bits to
  12227. * disable the underrun reporting. Which means we need to start
  12228. * out with underrun reporting disabled also on inactive pipes,
  12229. * since otherwise we'll complain about the garbage we read when
  12230. * e.g. coming up after runtime pm.
  12231. *
  12232. * No protection against concurrent access is required - at
  12233. * worst a fifo underrun happens which also sets this to false.
  12234. */
  12235. crtc->cpu_fifo_underrun_disabled = true;
  12236. /*
  12237. * We track the PCH trancoder underrun reporting state
  12238. * within the crtc. With crtc for pipe A housing the underrun
  12239. * reporting state for PCH transcoder A, crtc for pipe B housing
  12240. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12241. * and marking underrun reporting as disabled for the non-existing
  12242. * PCH transcoders B and C would prevent enabling the south
  12243. * error interrupt (see cpt_can_enable_serr_int()).
  12244. */
  12245. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  12246. crtc->pch_fifo_underrun_disabled = true;
  12247. }
  12248. }
  12249. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12250. {
  12251. struct intel_connector *connector;
  12252. /* We need to check both for a crtc link (meaning that the
  12253. * encoder is active and trying to read from a pipe) and the
  12254. * pipe itself being active. */
  12255. bool has_active_crtc = encoder->base.crtc &&
  12256. to_intel_crtc(encoder->base.crtc)->active;
  12257. connector = intel_encoder_find_connector(encoder);
  12258. if (connector && !has_active_crtc) {
  12259. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12260. encoder->base.base.id,
  12261. encoder->base.name);
  12262. /* Connector is active, but has no active pipe. This is
  12263. * fallout from our resume register restoring. Disable
  12264. * the encoder manually again. */
  12265. if (encoder->base.crtc) {
  12266. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12267. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12268. encoder->base.base.id,
  12269. encoder->base.name);
  12270. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12271. if (encoder->post_disable)
  12272. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12273. }
  12274. encoder->base.crtc = NULL;
  12275. /* Inconsistent output/port/pipe state happens presumably due to
  12276. * a bug in one of the get_hw_state functions. Or someplace else
  12277. * in our code, like the register restore mess on resume. Clamp
  12278. * things to off as a safer default. */
  12279. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12280. connector->base.encoder = NULL;
  12281. }
  12282. /* Enabled encoders without active connectors will be fixed in
  12283. * the crtc fixup. */
  12284. }
  12285. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12286. {
  12287. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12288. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12289. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12290. i915_disable_vga(dev_priv);
  12291. }
  12292. }
  12293. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12294. {
  12295. /* This function can be called both from intel_modeset_setup_hw_state or
  12296. * at a very early point in our resume sequence, where the power well
  12297. * structures are not yet restored. Since this function is at a very
  12298. * paranoid "someone might have enabled VGA while we were not looking"
  12299. * level, just check if the power well is enabled instead of trying to
  12300. * follow the "don't touch the power well if we don't need it" policy
  12301. * the rest of the driver uses. */
  12302. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12303. return;
  12304. i915_redisable_vga_power_on(dev_priv);
  12305. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12306. }
  12307. static bool primary_get_hw_state(struct intel_plane *plane)
  12308. {
  12309. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12310. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12311. }
  12312. /* FIXME read out full plane state for all planes */
  12313. static void readout_plane_state(struct intel_crtc *crtc)
  12314. {
  12315. struct intel_plane *primary = to_intel_plane(crtc->base.primary);
  12316. bool visible;
  12317. visible = crtc->active && primary_get_hw_state(primary);
  12318. intel_set_plane_visible(to_intel_crtc_state(crtc->base.state),
  12319. to_intel_plane_state(primary->base.state),
  12320. visible);
  12321. }
  12322. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12323. {
  12324. struct drm_i915_private *dev_priv = to_i915(dev);
  12325. enum pipe pipe;
  12326. struct intel_crtc *crtc;
  12327. struct intel_encoder *encoder;
  12328. struct intel_connector *connector;
  12329. struct drm_connector_list_iter conn_iter;
  12330. int i;
  12331. dev_priv->active_crtcs = 0;
  12332. for_each_intel_crtc(dev, crtc) {
  12333. struct intel_crtc_state *crtc_state =
  12334. to_intel_crtc_state(crtc->base.state);
  12335. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12336. memset(crtc_state, 0, sizeof(*crtc_state));
  12337. crtc_state->base.crtc = &crtc->base;
  12338. crtc_state->base.active = crtc_state->base.enable =
  12339. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12340. crtc->base.enabled = crtc_state->base.enable;
  12341. crtc->active = crtc_state->base.active;
  12342. if (crtc_state->base.active)
  12343. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12344. readout_plane_state(crtc);
  12345. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12346. crtc->base.base.id, crtc->base.name,
  12347. enableddisabled(crtc_state->base.active));
  12348. }
  12349. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12350. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12351. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12352. &pll->state.hw_state);
  12353. pll->state.crtc_mask = 0;
  12354. for_each_intel_crtc(dev, crtc) {
  12355. struct intel_crtc_state *crtc_state =
  12356. to_intel_crtc_state(crtc->base.state);
  12357. if (crtc_state->base.active &&
  12358. crtc_state->shared_dpll == pll)
  12359. pll->state.crtc_mask |= 1 << crtc->pipe;
  12360. }
  12361. pll->active_mask = pll->state.crtc_mask;
  12362. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12363. pll->name, pll->state.crtc_mask, pll->on);
  12364. }
  12365. for_each_intel_encoder(dev, encoder) {
  12366. pipe = 0;
  12367. if (encoder->get_hw_state(encoder, &pipe)) {
  12368. struct intel_crtc_state *crtc_state;
  12369. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12370. crtc_state = to_intel_crtc_state(crtc->base.state);
  12371. encoder->base.crtc = &crtc->base;
  12372. crtc_state->output_types |= 1 << encoder->type;
  12373. encoder->get_config(encoder, crtc_state);
  12374. } else {
  12375. encoder->base.crtc = NULL;
  12376. }
  12377. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12378. encoder->base.base.id, encoder->base.name,
  12379. enableddisabled(encoder->base.crtc),
  12380. pipe_name(pipe));
  12381. }
  12382. drm_connector_list_iter_begin(dev, &conn_iter);
  12383. for_each_intel_connector_iter(connector, &conn_iter) {
  12384. if (connector->get_hw_state(connector)) {
  12385. connector->base.dpms = DRM_MODE_DPMS_ON;
  12386. encoder = connector->encoder;
  12387. connector->base.encoder = &encoder->base;
  12388. if (encoder->base.crtc &&
  12389. encoder->base.crtc->state->active) {
  12390. /*
  12391. * This has to be done during hardware readout
  12392. * because anything calling .crtc_disable may
  12393. * rely on the connector_mask being accurate.
  12394. */
  12395. encoder->base.crtc->state->connector_mask |=
  12396. 1 << drm_connector_index(&connector->base);
  12397. encoder->base.crtc->state->encoder_mask |=
  12398. 1 << drm_encoder_index(&encoder->base);
  12399. }
  12400. } else {
  12401. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12402. connector->base.encoder = NULL;
  12403. }
  12404. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12405. connector->base.base.id, connector->base.name,
  12406. enableddisabled(connector->base.encoder));
  12407. }
  12408. drm_connector_list_iter_end(&conn_iter);
  12409. for_each_intel_crtc(dev, crtc) {
  12410. struct intel_crtc_state *crtc_state =
  12411. to_intel_crtc_state(crtc->base.state);
  12412. int pixclk = 0;
  12413. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12414. if (crtc_state->base.active) {
  12415. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12416. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12417. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12418. /*
  12419. * The initial mode needs to be set in order to keep
  12420. * the atomic core happy. It wants a valid mode if the
  12421. * crtc's enabled, so we do the above call.
  12422. *
  12423. * But we don't set all the derived state fully, hence
  12424. * set a flag to indicate that a full recalculation is
  12425. * needed on the next commit.
  12426. */
  12427. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12428. intel_crtc_compute_pixel_rate(crtc_state);
  12429. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
  12430. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12431. pixclk = crtc_state->pixel_rate;
  12432. else
  12433. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  12434. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  12435. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  12436. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  12437. drm_calc_timestamping_constants(&crtc->base,
  12438. &crtc_state->base.adjusted_mode);
  12439. update_scanline_offset(crtc);
  12440. }
  12441. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  12442. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12443. }
  12444. }
  12445. static void
  12446. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12447. {
  12448. struct intel_encoder *encoder;
  12449. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12450. u64 get_domains;
  12451. enum intel_display_power_domain domain;
  12452. if (!encoder->get_power_domains)
  12453. continue;
  12454. get_domains = encoder->get_power_domains(encoder);
  12455. for_each_power_domain(domain, get_domains)
  12456. intel_display_power_get(dev_priv, domain);
  12457. }
  12458. }
  12459. /* Scan out the current hw modeset state,
  12460. * and sanitizes it to the current state
  12461. */
  12462. static void
  12463. intel_modeset_setup_hw_state(struct drm_device *dev,
  12464. struct drm_modeset_acquire_ctx *ctx)
  12465. {
  12466. struct drm_i915_private *dev_priv = to_i915(dev);
  12467. enum pipe pipe;
  12468. struct intel_crtc *crtc;
  12469. struct intel_encoder *encoder;
  12470. int i;
  12471. intel_modeset_readout_hw_state(dev);
  12472. /* HW state is read out, now we need to sanitize this mess. */
  12473. get_encoder_power_domains(dev_priv);
  12474. for_each_intel_encoder(dev, encoder) {
  12475. intel_sanitize_encoder(encoder);
  12476. }
  12477. for_each_pipe(dev_priv, pipe) {
  12478. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12479. intel_sanitize_crtc(crtc, ctx);
  12480. intel_dump_pipe_config(crtc, crtc->config,
  12481. "[setup_hw_state]");
  12482. }
  12483. intel_modeset_update_connector_atomic_state(dev);
  12484. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12485. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12486. if (!pll->on || pll->active_mask)
  12487. continue;
  12488. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12489. pll->funcs.disable(dev_priv, pll);
  12490. pll->on = false;
  12491. }
  12492. if (IS_G4X(dev_priv)) {
  12493. g4x_wm_get_hw_state(dev);
  12494. g4x_wm_sanitize(dev_priv);
  12495. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12496. vlv_wm_get_hw_state(dev);
  12497. vlv_wm_sanitize(dev_priv);
  12498. } else if (IS_GEN9(dev_priv)) {
  12499. skl_wm_get_hw_state(dev);
  12500. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12501. ilk_wm_get_hw_state(dev);
  12502. }
  12503. for_each_intel_crtc(dev, crtc) {
  12504. u64 put_domains;
  12505. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12506. if (WARN_ON(put_domains))
  12507. modeset_put_power_domains(dev_priv, put_domains);
  12508. }
  12509. intel_display_set_init_power(dev_priv, false);
  12510. intel_power_domains_verify_state(dev_priv);
  12511. intel_fbc_init_pipe_state(dev_priv);
  12512. }
  12513. void intel_display_resume(struct drm_device *dev)
  12514. {
  12515. struct drm_i915_private *dev_priv = to_i915(dev);
  12516. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12517. struct drm_modeset_acquire_ctx ctx;
  12518. int ret;
  12519. dev_priv->modeset_restore_state = NULL;
  12520. if (state)
  12521. state->acquire_ctx = &ctx;
  12522. drm_modeset_acquire_init(&ctx, 0);
  12523. while (1) {
  12524. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12525. if (ret != -EDEADLK)
  12526. break;
  12527. drm_modeset_backoff(&ctx);
  12528. }
  12529. if (!ret)
  12530. ret = __intel_display_resume(dev, state, &ctx);
  12531. drm_modeset_drop_locks(&ctx);
  12532. drm_modeset_acquire_fini(&ctx);
  12533. if (ret)
  12534. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12535. if (state)
  12536. drm_atomic_state_put(state);
  12537. }
  12538. void intel_modeset_gem_init(struct drm_device *dev)
  12539. {
  12540. struct drm_i915_private *dev_priv = to_i915(dev);
  12541. intel_init_gt_powersave(dev_priv);
  12542. intel_setup_overlay(dev_priv);
  12543. }
  12544. int intel_connector_register(struct drm_connector *connector)
  12545. {
  12546. struct intel_connector *intel_connector = to_intel_connector(connector);
  12547. int ret;
  12548. ret = intel_backlight_device_register(intel_connector);
  12549. if (ret)
  12550. goto err;
  12551. return 0;
  12552. err:
  12553. return ret;
  12554. }
  12555. void intel_connector_unregister(struct drm_connector *connector)
  12556. {
  12557. struct intel_connector *intel_connector = to_intel_connector(connector);
  12558. intel_backlight_device_unregister(intel_connector);
  12559. intel_panel_destroy_backlight(connector);
  12560. }
  12561. void intel_modeset_cleanup(struct drm_device *dev)
  12562. {
  12563. struct drm_i915_private *dev_priv = to_i915(dev);
  12564. flush_work(&dev_priv->atomic_helper.free_work);
  12565. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  12566. intel_disable_gt_powersave(dev_priv);
  12567. /*
  12568. * Interrupts and polling as the first thing to avoid creating havoc.
  12569. * Too much stuff here (turning of connectors, ...) would
  12570. * experience fancy races otherwise.
  12571. */
  12572. intel_irq_uninstall(dev_priv);
  12573. /*
  12574. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12575. * poll handlers. Hence disable polling after hpd handling is shut down.
  12576. */
  12577. drm_kms_helper_poll_fini(dev);
  12578. /* poll work can call into fbdev, hence clean that up afterwards */
  12579. intel_fbdev_fini(dev_priv);
  12580. intel_unregister_dsm_handler();
  12581. intel_fbc_global_disable(dev_priv);
  12582. /* flush any delayed tasks or pending work */
  12583. flush_scheduled_work();
  12584. drm_mode_config_cleanup(dev);
  12585. intel_cleanup_overlay(dev_priv);
  12586. intel_cleanup_gt_powersave(dev_priv);
  12587. intel_teardown_gmbus(dev_priv);
  12588. }
  12589. void intel_connector_attach_encoder(struct intel_connector *connector,
  12590. struct intel_encoder *encoder)
  12591. {
  12592. connector->encoder = encoder;
  12593. drm_mode_connector_attach_encoder(&connector->base,
  12594. &encoder->base);
  12595. }
  12596. /*
  12597. * set vga decode state - true == enable VGA decode
  12598. */
  12599. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  12600. {
  12601. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12602. u16 gmch_ctrl;
  12603. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12604. DRM_ERROR("failed to read control word\n");
  12605. return -EIO;
  12606. }
  12607. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12608. return 0;
  12609. if (state)
  12610. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12611. else
  12612. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12613. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12614. DRM_ERROR("failed to write control word\n");
  12615. return -EIO;
  12616. }
  12617. return 0;
  12618. }
  12619. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  12620. struct intel_display_error_state {
  12621. u32 power_well_driver;
  12622. int num_transcoders;
  12623. struct intel_cursor_error_state {
  12624. u32 control;
  12625. u32 position;
  12626. u32 base;
  12627. u32 size;
  12628. } cursor[I915_MAX_PIPES];
  12629. struct intel_pipe_error_state {
  12630. bool power_domain_on;
  12631. u32 source;
  12632. u32 stat;
  12633. } pipe[I915_MAX_PIPES];
  12634. struct intel_plane_error_state {
  12635. u32 control;
  12636. u32 stride;
  12637. u32 size;
  12638. u32 pos;
  12639. u32 addr;
  12640. u32 surface;
  12641. u32 tile_offset;
  12642. } plane[I915_MAX_PIPES];
  12643. struct intel_transcoder_error_state {
  12644. bool power_domain_on;
  12645. enum transcoder cpu_transcoder;
  12646. u32 conf;
  12647. u32 htotal;
  12648. u32 hblank;
  12649. u32 hsync;
  12650. u32 vtotal;
  12651. u32 vblank;
  12652. u32 vsync;
  12653. } transcoder[4];
  12654. };
  12655. struct intel_display_error_state *
  12656. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  12657. {
  12658. struct intel_display_error_state *error;
  12659. int transcoders[] = {
  12660. TRANSCODER_A,
  12661. TRANSCODER_B,
  12662. TRANSCODER_C,
  12663. TRANSCODER_EDP,
  12664. };
  12665. int i;
  12666. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12667. return NULL;
  12668. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12669. if (error == NULL)
  12670. return NULL;
  12671. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12672. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  12673. for_each_pipe(dev_priv, i) {
  12674. error->pipe[i].power_domain_on =
  12675. __intel_display_power_is_enabled(dev_priv,
  12676. POWER_DOMAIN_PIPE(i));
  12677. if (!error->pipe[i].power_domain_on)
  12678. continue;
  12679. error->cursor[i].control = I915_READ(CURCNTR(i));
  12680. error->cursor[i].position = I915_READ(CURPOS(i));
  12681. error->cursor[i].base = I915_READ(CURBASE(i));
  12682. error->plane[i].control = I915_READ(DSPCNTR(i));
  12683. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12684. if (INTEL_GEN(dev_priv) <= 3) {
  12685. error->plane[i].size = I915_READ(DSPSIZE(i));
  12686. error->plane[i].pos = I915_READ(DSPPOS(i));
  12687. }
  12688. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12689. error->plane[i].addr = I915_READ(DSPADDR(i));
  12690. if (INTEL_GEN(dev_priv) >= 4) {
  12691. error->plane[i].surface = I915_READ(DSPSURF(i));
  12692. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12693. }
  12694. error->pipe[i].source = I915_READ(PIPESRC(i));
  12695. if (HAS_GMCH_DISPLAY(dev_priv))
  12696. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12697. }
  12698. /* Note: this does not include DSI transcoders. */
  12699. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  12700. if (HAS_DDI(dev_priv))
  12701. error->num_transcoders++; /* Account for eDP. */
  12702. for (i = 0; i < error->num_transcoders; i++) {
  12703. enum transcoder cpu_transcoder = transcoders[i];
  12704. error->transcoder[i].power_domain_on =
  12705. __intel_display_power_is_enabled(dev_priv,
  12706. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  12707. if (!error->transcoder[i].power_domain_on)
  12708. continue;
  12709. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  12710. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  12711. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  12712. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  12713. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  12714. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  12715. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  12716. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  12717. }
  12718. return error;
  12719. }
  12720. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  12721. void
  12722. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  12723. struct intel_display_error_state *error)
  12724. {
  12725. struct drm_i915_private *dev_priv = m->i915;
  12726. int i;
  12727. if (!error)
  12728. return;
  12729. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  12730. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  12731. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  12732. error->power_well_driver);
  12733. for_each_pipe(dev_priv, i) {
  12734. err_printf(m, "Pipe [%d]:\n", i);
  12735. err_printf(m, " Power: %s\n",
  12736. onoff(error->pipe[i].power_domain_on));
  12737. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  12738. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  12739. err_printf(m, "Plane [%d]:\n", i);
  12740. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  12741. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  12742. if (INTEL_GEN(dev_priv) <= 3) {
  12743. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  12744. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  12745. }
  12746. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  12747. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  12748. if (INTEL_GEN(dev_priv) >= 4) {
  12749. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  12750. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  12751. }
  12752. err_printf(m, "Cursor [%d]:\n", i);
  12753. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  12754. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  12755. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  12756. }
  12757. for (i = 0; i < error->num_transcoders; i++) {
  12758. err_printf(m, "CPU transcoder: %s\n",
  12759. transcoder_name(error->transcoder[i].cpu_transcoder));
  12760. err_printf(m, " Power: %s\n",
  12761. onoff(error->transcoder[i].power_domain_on));
  12762. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  12763. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  12764. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  12765. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  12766. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  12767. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  12768. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  12769. }
  12770. }
  12771. #endif