intel_display.c 489 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "intel_dsi.h"
  40. #include "i915_trace.h"
  41. #include <drm/drm_atomic.h>
  42. #include <drm/drm_atomic_helper.h>
  43. #include <drm/drm_dp_helper.h>
  44. #include <drm/drm_crtc_helper.h>
  45. #include <drm/drm_plane_helper.h>
  46. #include <drm/drm_rect.h>
  47. #include <linux/dma_remapping.h>
  48. #include <linux/reservation.h>
  49. static bool is_mmio_work(struct intel_flip_work *work)
  50. {
  51. return work->mmio_work.func;
  52. }
  53. /* Primary plane formats for gen <= 3 */
  54. static const uint32_t i8xx_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB1555,
  58. DRM_FORMAT_XRGB8888,
  59. };
  60. /* Primary plane formats for gen >= 4 */
  61. static const uint32_t i965_primary_formats[] = {
  62. DRM_FORMAT_C8,
  63. DRM_FORMAT_RGB565,
  64. DRM_FORMAT_XRGB8888,
  65. DRM_FORMAT_XBGR8888,
  66. DRM_FORMAT_XRGB2101010,
  67. DRM_FORMAT_XBGR2101010,
  68. };
  69. static const uint32_t skl_primary_formats[] = {
  70. DRM_FORMAT_C8,
  71. DRM_FORMAT_RGB565,
  72. DRM_FORMAT_XRGB8888,
  73. DRM_FORMAT_XBGR8888,
  74. DRM_FORMAT_ARGB8888,
  75. DRM_FORMAT_ABGR8888,
  76. DRM_FORMAT_XRGB2101010,
  77. DRM_FORMAT_XBGR2101010,
  78. DRM_FORMAT_YUYV,
  79. DRM_FORMAT_YVYU,
  80. DRM_FORMAT_UYVY,
  81. DRM_FORMAT_VYUY,
  82. };
  83. /* Cursor formats */
  84. static const uint32_t intel_cursor_formats[] = {
  85. DRM_FORMAT_ARGB8888,
  86. };
  87. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  88. struct intel_crtc_state *pipe_config);
  89. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  90. struct intel_crtc_state *pipe_config);
  91. static int intel_framebuffer_init(struct drm_device *dev,
  92. struct intel_framebuffer *ifb,
  93. struct drm_mode_fb_cmd2 *mode_cmd,
  94. struct drm_i915_gem_object *obj);
  95. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  96. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  98. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  99. struct intel_link_m_n *m_n,
  100. struct intel_link_m_n *m2_n2);
  101. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  102. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  104. static void vlv_prepare_pll(struct intel_crtc *crtc,
  105. const struct intel_crtc_state *pipe_config);
  106. static void chv_prepare_pll(struct intel_crtc *crtc,
  107. const struct intel_crtc_state *pipe_config);
  108. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  109. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  111. struct intel_crtc_state *crtc_state);
  112. static void skylake_pfit_enable(struct intel_crtc *crtc);
  113. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  114. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  115. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  116. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  117. static int ilk_max_pixel_rate(struct drm_atomic_state *state);
  118. static int glk_calc_cdclk(int max_pixclk);
  119. static int bxt_calc_cdclk(int max_pixclk);
  120. struct intel_limit {
  121. struct {
  122. int min, max;
  123. } dot, vco, n, m, m1, m2, p, p1;
  124. struct {
  125. int dot_limit;
  126. int p2_slow, p2_fast;
  127. } p2;
  128. };
  129. /* returns HPLL frequency in kHz */
  130. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  131. {
  132. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  133. /* Obtain SKU information */
  134. mutex_lock(&dev_priv->sb_lock);
  135. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  136. CCK_FUSE_HPLL_FREQ_MASK;
  137. mutex_unlock(&dev_priv->sb_lock);
  138. return vco_freq[hpll_freq] * 1000;
  139. }
  140. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  141. const char *name, u32 reg, int ref_freq)
  142. {
  143. u32 val;
  144. int divider;
  145. mutex_lock(&dev_priv->sb_lock);
  146. val = vlv_cck_read(dev_priv, reg);
  147. mutex_unlock(&dev_priv->sb_lock);
  148. divider = val & CCK_FREQUENCY_VALUES;
  149. WARN((val & CCK_FREQUENCY_STATUS) !=
  150. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  151. "%s change in progress\n", name);
  152. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  153. }
  154. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  155. const char *name, u32 reg)
  156. {
  157. if (dev_priv->hpll_freq == 0)
  158. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  159. return vlv_get_cck_clock(dev_priv, name, reg,
  160. dev_priv->hpll_freq);
  161. }
  162. static int
  163. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  164. {
  165. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  166. }
  167. static int
  168. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  169. {
  170. /* RAWCLK_FREQ_VLV register updated from power well code */
  171. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  172. CCK_DISPLAY_REF_CLOCK_CONTROL);
  173. }
  174. static int
  175. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  176. {
  177. uint32_t clkcfg;
  178. /* hrawclock is 1/4 the FSB frequency */
  179. clkcfg = I915_READ(CLKCFG);
  180. switch (clkcfg & CLKCFG_FSB_MASK) {
  181. case CLKCFG_FSB_400:
  182. return 100000;
  183. case CLKCFG_FSB_533:
  184. return 133333;
  185. case CLKCFG_FSB_667:
  186. return 166667;
  187. case CLKCFG_FSB_800:
  188. return 200000;
  189. case CLKCFG_FSB_1067:
  190. return 266667;
  191. case CLKCFG_FSB_1333:
  192. return 333333;
  193. /* these two are just a guess; one of them might be right */
  194. case CLKCFG_FSB_1600:
  195. case CLKCFG_FSB_1600_ALT:
  196. return 400000;
  197. default:
  198. return 133333;
  199. }
  200. }
  201. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  202. {
  203. if (HAS_PCH_SPLIT(dev_priv))
  204. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  205. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  206. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  207. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  208. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  209. else
  210. return; /* no rawclk on other platforms, or no need to know it */
  211. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  212. }
  213. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  214. {
  215. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  216. return;
  217. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  218. CCK_CZ_CLOCK_CONTROL);
  219. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  220. }
  221. static inline u32 /* units of 100MHz */
  222. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  223. const struct intel_crtc_state *pipe_config)
  224. {
  225. if (HAS_DDI(dev_priv))
  226. return pipe_config->port_clock; /* SPLL */
  227. else if (IS_GEN5(dev_priv))
  228. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  229. else
  230. return 270000;
  231. }
  232. static const struct intel_limit intel_limits_i8xx_dac = {
  233. .dot = { .min = 25000, .max = 350000 },
  234. .vco = { .min = 908000, .max = 1512000 },
  235. .n = { .min = 2, .max = 16 },
  236. .m = { .min = 96, .max = 140 },
  237. .m1 = { .min = 18, .max = 26 },
  238. .m2 = { .min = 6, .max = 16 },
  239. .p = { .min = 4, .max = 128 },
  240. .p1 = { .min = 2, .max = 33 },
  241. .p2 = { .dot_limit = 165000,
  242. .p2_slow = 4, .p2_fast = 2 },
  243. };
  244. static const struct intel_limit intel_limits_i8xx_dvo = {
  245. .dot = { .min = 25000, .max = 350000 },
  246. .vco = { .min = 908000, .max = 1512000 },
  247. .n = { .min = 2, .max = 16 },
  248. .m = { .min = 96, .max = 140 },
  249. .m1 = { .min = 18, .max = 26 },
  250. .m2 = { .min = 6, .max = 16 },
  251. .p = { .min = 4, .max = 128 },
  252. .p1 = { .min = 2, .max = 33 },
  253. .p2 = { .dot_limit = 165000,
  254. .p2_slow = 4, .p2_fast = 4 },
  255. };
  256. static const struct intel_limit intel_limits_i8xx_lvds = {
  257. .dot = { .min = 25000, .max = 350000 },
  258. .vco = { .min = 908000, .max = 1512000 },
  259. .n = { .min = 2, .max = 16 },
  260. .m = { .min = 96, .max = 140 },
  261. .m1 = { .min = 18, .max = 26 },
  262. .m2 = { .min = 6, .max = 16 },
  263. .p = { .min = 4, .max = 128 },
  264. .p1 = { .min = 1, .max = 6 },
  265. .p2 = { .dot_limit = 165000,
  266. .p2_slow = 14, .p2_fast = 7 },
  267. };
  268. static const struct intel_limit intel_limits_i9xx_sdvo = {
  269. .dot = { .min = 20000, .max = 400000 },
  270. .vco = { .min = 1400000, .max = 2800000 },
  271. .n = { .min = 1, .max = 6 },
  272. .m = { .min = 70, .max = 120 },
  273. .m1 = { .min = 8, .max = 18 },
  274. .m2 = { .min = 3, .max = 7 },
  275. .p = { .min = 5, .max = 80 },
  276. .p1 = { .min = 1, .max = 8 },
  277. .p2 = { .dot_limit = 200000,
  278. .p2_slow = 10, .p2_fast = 5 },
  279. };
  280. static const struct intel_limit intel_limits_i9xx_lvds = {
  281. .dot = { .min = 20000, .max = 400000 },
  282. .vco = { .min = 1400000, .max = 2800000 },
  283. .n = { .min = 1, .max = 6 },
  284. .m = { .min = 70, .max = 120 },
  285. .m1 = { .min = 8, .max = 18 },
  286. .m2 = { .min = 3, .max = 7 },
  287. .p = { .min = 7, .max = 98 },
  288. .p1 = { .min = 1, .max = 8 },
  289. .p2 = { .dot_limit = 112000,
  290. .p2_slow = 14, .p2_fast = 7 },
  291. };
  292. static const struct intel_limit intel_limits_g4x_sdvo = {
  293. .dot = { .min = 25000, .max = 270000 },
  294. .vco = { .min = 1750000, .max = 3500000},
  295. .n = { .min = 1, .max = 4 },
  296. .m = { .min = 104, .max = 138 },
  297. .m1 = { .min = 17, .max = 23 },
  298. .m2 = { .min = 5, .max = 11 },
  299. .p = { .min = 10, .max = 30 },
  300. .p1 = { .min = 1, .max = 3},
  301. .p2 = { .dot_limit = 270000,
  302. .p2_slow = 10,
  303. .p2_fast = 10
  304. },
  305. };
  306. static const struct intel_limit intel_limits_g4x_hdmi = {
  307. .dot = { .min = 22000, .max = 400000 },
  308. .vco = { .min = 1750000, .max = 3500000},
  309. .n = { .min = 1, .max = 4 },
  310. .m = { .min = 104, .max = 138 },
  311. .m1 = { .min = 16, .max = 23 },
  312. .m2 = { .min = 5, .max = 11 },
  313. .p = { .min = 5, .max = 80 },
  314. .p1 = { .min = 1, .max = 8},
  315. .p2 = { .dot_limit = 165000,
  316. .p2_slow = 10, .p2_fast = 5 },
  317. };
  318. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  319. .dot = { .min = 20000, .max = 115000 },
  320. .vco = { .min = 1750000, .max = 3500000 },
  321. .n = { .min = 1, .max = 3 },
  322. .m = { .min = 104, .max = 138 },
  323. .m1 = { .min = 17, .max = 23 },
  324. .m2 = { .min = 5, .max = 11 },
  325. .p = { .min = 28, .max = 112 },
  326. .p1 = { .min = 2, .max = 8 },
  327. .p2 = { .dot_limit = 0,
  328. .p2_slow = 14, .p2_fast = 14
  329. },
  330. };
  331. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  332. .dot = { .min = 80000, .max = 224000 },
  333. .vco = { .min = 1750000, .max = 3500000 },
  334. .n = { .min = 1, .max = 3 },
  335. .m = { .min = 104, .max = 138 },
  336. .m1 = { .min = 17, .max = 23 },
  337. .m2 = { .min = 5, .max = 11 },
  338. .p = { .min = 14, .max = 42 },
  339. .p1 = { .min = 2, .max = 6 },
  340. .p2 = { .dot_limit = 0,
  341. .p2_slow = 7, .p2_fast = 7
  342. },
  343. };
  344. static const struct intel_limit intel_limits_pineview_sdvo = {
  345. .dot = { .min = 20000, .max = 400000},
  346. .vco = { .min = 1700000, .max = 3500000 },
  347. /* Pineview's Ncounter is a ring counter */
  348. .n = { .min = 3, .max = 6 },
  349. .m = { .min = 2, .max = 256 },
  350. /* Pineview only has one combined m divider, which we treat as m2. */
  351. .m1 = { .min = 0, .max = 0 },
  352. .m2 = { .min = 0, .max = 254 },
  353. .p = { .min = 5, .max = 80 },
  354. .p1 = { .min = 1, .max = 8 },
  355. .p2 = { .dot_limit = 200000,
  356. .p2_slow = 10, .p2_fast = 5 },
  357. };
  358. static const struct intel_limit intel_limits_pineview_lvds = {
  359. .dot = { .min = 20000, .max = 400000 },
  360. .vco = { .min = 1700000, .max = 3500000 },
  361. .n = { .min = 3, .max = 6 },
  362. .m = { .min = 2, .max = 256 },
  363. .m1 = { .min = 0, .max = 0 },
  364. .m2 = { .min = 0, .max = 254 },
  365. .p = { .min = 7, .max = 112 },
  366. .p1 = { .min = 1, .max = 8 },
  367. .p2 = { .dot_limit = 112000,
  368. .p2_slow = 14, .p2_fast = 14 },
  369. };
  370. /* Ironlake / Sandybridge
  371. *
  372. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  373. * the range value for them is (actual_value - 2).
  374. */
  375. static const struct intel_limit intel_limits_ironlake_dac = {
  376. .dot = { .min = 25000, .max = 350000 },
  377. .vco = { .min = 1760000, .max = 3510000 },
  378. .n = { .min = 1, .max = 5 },
  379. .m = { .min = 79, .max = 127 },
  380. .m1 = { .min = 12, .max = 22 },
  381. .m2 = { .min = 5, .max = 9 },
  382. .p = { .min = 5, .max = 80 },
  383. .p1 = { .min = 1, .max = 8 },
  384. .p2 = { .dot_limit = 225000,
  385. .p2_slow = 10, .p2_fast = 5 },
  386. };
  387. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  388. .dot = { .min = 25000, .max = 350000 },
  389. .vco = { .min = 1760000, .max = 3510000 },
  390. .n = { .min = 1, .max = 3 },
  391. .m = { .min = 79, .max = 118 },
  392. .m1 = { .min = 12, .max = 22 },
  393. .m2 = { .min = 5, .max = 9 },
  394. .p = { .min = 28, .max = 112 },
  395. .p1 = { .min = 2, .max = 8 },
  396. .p2 = { .dot_limit = 225000,
  397. .p2_slow = 14, .p2_fast = 14 },
  398. };
  399. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  400. .dot = { .min = 25000, .max = 350000 },
  401. .vco = { .min = 1760000, .max = 3510000 },
  402. .n = { .min = 1, .max = 3 },
  403. .m = { .min = 79, .max = 127 },
  404. .m1 = { .min = 12, .max = 22 },
  405. .m2 = { .min = 5, .max = 9 },
  406. .p = { .min = 14, .max = 56 },
  407. .p1 = { .min = 2, .max = 8 },
  408. .p2 = { .dot_limit = 225000,
  409. .p2_slow = 7, .p2_fast = 7 },
  410. };
  411. /* LVDS 100mhz refclk limits. */
  412. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  413. .dot = { .min = 25000, .max = 350000 },
  414. .vco = { .min = 1760000, .max = 3510000 },
  415. .n = { .min = 1, .max = 2 },
  416. .m = { .min = 79, .max = 126 },
  417. .m1 = { .min = 12, .max = 22 },
  418. .m2 = { .min = 5, .max = 9 },
  419. .p = { .min = 28, .max = 112 },
  420. .p1 = { .min = 2, .max = 8 },
  421. .p2 = { .dot_limit = 225000,
  422. .p2_slow = 14, .p2_fast = 14 },
  423. };
  424. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  425. .dot = { .min = 25000, .max = 350000 },
  426. .vco = { .min = 1760000, .max = 3510000 },
  427. .n = { .min = 1, .max = 3 },
  428. .m = { .min = 79, .max = 126 },
  429. .m1 = { .min = 12, .max = 22 },
  430. .m2 = { .min = 5, .max = 9 },
  431. .p = { .min = 14, .max = 42 },
  432. .p1 = { .min = 2, .max = 6 },
  433. .p2 = { .dot_limit = 225000,
  434. .p2_slow = 7, .p2_fast = 7 },
  435. };
  436. static const struct intel_limit intel_limits_vlv = {
  437. /*
  438. * These are the data rate limits (measured in fast clocks)
  439. * since those are the strictest limits we have. The fast
  440. * clock and actual rate limits are more relaxed, so checking
  441. * them would make no difference.
  442. */
  443. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  444. .vco = { .min = 4000000, .max = 6000000 },
  445. .n = { .min = 1, .max = 7 },
  446. .m1 = { .min = 2, .max = 3 },
  447. .m2 = { .min = 11, .max = 156 },
  448. .p1 = { .min = 2, .max = 3 },
  449. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  450. };
  451. static const struct intel_limit intel_limits_chv = {
  452. /*
  453. * These are the data rate limits (measured in fast clocks)
  454. * since those are the strictest limits we have. The fast
  455. * clock and actual rate limits are more relaxed, so checking
  456. * them would make no difference.
  457. */
  458. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  459. .vco = { .min = 4800000, .max = 6480000 },
  460. .n = { .min = 1, .max = 1 },
  461. .m1 = { .min = 2, .max = 2 },
  462. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  463. .p1 = { .min = 2, .max = 4 },
  464. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  465. };
  466. static const struct intel_limit intel_limits_bxt = {
  467. /* FIXME: find real dot limits */
  468. .dot = { .min = 0, .max = INT_MAX },
  469. .vco = { .min = 4800000, .max = 6700000 },
  470. .n = { .min = 1, .max = 1 },
  471. .m1 = { .min = 2, .max = 2 },
  472. /* FIXME: find real m2 limits */
  473. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  474. .p1 = { .min = 2, .max = 4 },
  475. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  476. };
  477. static bool
  478. needs_modeset(struct drm_crtc_state *state)
  479. {
  480. return drm_atomic_crtc_needs_modeset(state);
  481. }
  482. /*
  483. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  484. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  485. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  486. * The helpers' return value is the rate of the clock that is fed to the
  487. * display engine's pipe which can be the above fast dot clock rate or a
  488. * divided-down version of it.
  489. */
  490. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  491. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  492. {
  493. clock->m = clock->m2 + 2;
  494. clock->p = clock->p1 * clock->p2;
  495. if (WARN_ON(clock->n == 0 || clock->p == 0))
  496. return 0;
  497. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  498. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  499. return clock->dot;
  500. }
  501. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  502. {
  503. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  504. }
  505. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  506. {
  507. clock->m = i9xx_dpll_compute_m(clock);
  508. clock->p = clock->p1 * clock->p2;
  509. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  510. return 0;
  511. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  512. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  513. return clock->dot;
  514. }
  515. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  516. {
  517. clock->m = clock->m1 * clock->m2;
  518. clock->p = clock->p1 * clock->p2;
  519. if (WARN_ON(clock->n == 0 || clock->p == 0))
  520. return 0;
  521. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  522. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  523. return clock->dot / 5;
  524. }
  525. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  526. {
  527. clock->m = clock->m1 * clock->m2;
  528. clock->p = clock->p1 * clock->p2;
  529. if (WARN_ON(clock->n == 0 || clock->p == 0))
  530. return 0;
  531. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  532. clock->n << 22);
  533. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  534. return clock->dot / 5;
  535. }
  536. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  537. /**
  538. * Returns whether the given set of divisors are valid for a given refclk with
  539. * the given connectors.
  540. */
  541. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  542. const struct intel_limit *limit,
  543. const struct dpll *clock)
  544. {
  545. if (clock->n < limit->n.min || limit->n.max < clock->n)
  546. INTELPllInvalid("n out of range\n");
  547. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  548. INTELPllInvalid("p1 out of range\n");
  549. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  550. INTELPllInvalid("m2 out of range\n");
  551. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  552. INTELPllInvalid("m1 out of range\n");
  553. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  554. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  555. if (clock->m1 <= clock->m2)
  556. INTELPllInvalid("m1 <= m2\n");
  557. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  558. !IS_GEN9_LP(dev_priv)) {
  559. if (clock->p < limit->p.min || limit->p.max < clock->p)
  560. INTELPllInvalid("p out of range\n");
  561. if (clock->m < limit->m.min || limit->m.max < clock->m)
  562. INTELPllInvalid("m out of range\n");
  563. }
  564. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  565. INTELPllInvalid("vco out of range\n");
  566. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  567. * connector, etc., rather than just a single range.
  568. */
  569. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  570. INTELPllInvalid("dot out of range\n");
  571. return true;
  572. }
  573. static int
  574. i9xx_select_p2_div(const struct intel_limit *limit,
  575. const struct intel_crtc_state *crtc_state,
  576. int target)
  577. {
  578. struct drm_device *dev = crtc_state->base.crtc->dev;
  579. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  580. /*
  581. * For LVDS just rely on its current settings for dual-channel.
  582. * We haven't figured out how to reliably set up different
  583. * single/dual channel state, if we even can.
  584. */
  585. if (intel_is_dual_link_lvds(dev))
  586. return limit->p2.p2_fast;
  587. else
  588. return limit->p2.p2_slow;
  589. } else {
  590. if (target < limit->p2.dot_limit)
  591. return limit->p2.p2_slow;
  592. else
  593. return limit->p2.p2_fast;
  594. }
  595. }
  596. /*
  597. * Returns a set of divisors for the desired target clock with the given
  598. * refclk, or FALSE. The returned values represent the clock equation:
  599. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  600. *
  601. * Target and reference clocks are specified in kHz.
  602. *
  603. * If match_clock is provided, then best_clock P divider must match the P
  604. * divider from @match_clock used for LVDS downclocking.
  605. */
  606. static bool
  607. i9xx_find_best_dpll(const struct intel_limit *limit,
  608. struct intel_crtc_state *crtc_state,
  609. int target, int refclk, struct dpll *match_clock,
  610. struct dpll *best_clock)
  611. {
  612. struct drm_device *dev = crtc_state->base.crtc->dev;
  613. struct dpll clock;
  614. int err = target;
  615. memset(best_clock, 0, sizeof(*best_clock));
  616. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  617. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  618. clock.m1++) {
  619. for (clock.m2 = limit->m2.min;
  620. clock.m2 <= limit->m2.max; clock.m2++) {
  621. if (clock.m2 >= clock.m1)
  622. break;
  623. for (clock.n = limit->n.min;
  624. clock.n <= limit->n.max; clock.n++) {
  625. for (clock.p1 = limit->p1.min;
  626. clock.p1 <= limit->p1.max; clock.p1++) {
  627. int this_err;
  628. i9xx_calc_dpll_params(refclk, &clock);
  629. if (!intel_PLL_is_valid(to_i915(dev),
  630. limit,
  631. &clock))
  632. continue;
  633. if (match_clock &&
  634. clock.p != match_clock->p)
  635. continue;
  636. this_err = abs(clock.dot - target);
  637. if (this_err < err) {
  638. *best_clock = clock;
  639. err = this_err;
  640. }
  641. }
  642. }
  643. }
  644. }
  645. return (err != target);
  646. }
  647. /*
  648. * Returns a set of divisors for the desired target clock with the given
  649. * refclk, or FALSE. The returned values represent the clock equation:
  650. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  651. *
  652. * Target and reference clocks are specified in kHz.
  653. *
  654. * If match_clock is provided, then best_clock P divider must match the P
  655. * divider from @match_clock used for LVDS downclocking.
  656. */
  657. static bool
  658. pnv_find_best_dpll(const struct intel_limit *limit,
  659. struct intel_crtc_state *crtc_state,
  660. int target, int refclk, struct dpll *match_clock,
  661. struct dpll *best_clock)
  662. {
  663. struct drm_device *dev = crtc_state->base.crtc->dev;
  664. struct dpll clock;
  665. int err = target;
  666. memset(best_clock, 0, sizeof(*best_clock));
  667. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  668. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  669. clock.m1++) {
  670. for (clock.m2 = limit->m2.min;
  671. clock.m2 <= limit->m2.max; clock.m2++) {
  672. for (clock.n = limit->n.min;
  673. clock.n <= limit->n.max; clock.n++) {
  674. for (clock.p1 = limit->p1.min;
  675. clock.p1 <= limit->p1.max; clock.p1++) {
  676. int this_err;
  677. pnv_calc_dpll_params(refclk, &clock);
  678. if (!intel_PLL_is_valid(to_i915(dev),
  679. limit,
  680. &clock))
  681. continue;
  682. if (match_clock &&
  683. clock.p != match_clock->p)
  684. continue;
  685. this_err = abs(clock.dot - target);
  686. if (this_err < err) {
  687. *best_clock = clock;
  688. err = this_err;
  689. }
  690. }
  691. }
  692. }
  693. }
  694. return (err != target);
  695. }
  696. /*
  697. * Returns a set of divisors for the desired target clock with the given
  698. * refclk, or FALSE. The returned values represent the clock equation:
  699. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  700. *
  701. * Target and reference clocks are specified in kHz.
  702. *
  703. * If match_clock is provided, then best_clock P divider must match the P
  704. * divider from @match_clock used for LVDS downclocking.
  705. */
  706. static bool
  707. g4x_find_best_dpll(const struct intel_limit *limit,
  708. struct intel_crtc_state *crtc_state,
  709. int target, int refclk, struct dpll *match_clock,
  710. struct dpll *best_clock)
  711. {
  712. struct drm_device *dev = crtc_state->base.crtc->dev;
  713. struct dpll clock;
  714. int max_n;
  715. bool found = false;
  716. /* approximately equals target * 0.00585 */
  717. int err_most = (target >> 8) + (target >> 9);
  718. memset(best_clock, 0, sizeof(*best_clock));
  719. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  720. max_n = limit->n.max;
  721. /* based on hardware requirement, prefer smaller n to precision */
  722. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  723. /* based on hardware requirement, prefere larger m1,m2 */
  724. for (clock.m1 = limit->m1.max;
  725. clock.m1 >= limit->m1.min; clock.m1--) {
  726. for (clock.m2 = limit->m2.max;
  727. clock.m2 >= limit->m2.min; clock.m2--) {
  728. for (clock.p1 = limit->p1.max;
  729. clock.p1 >= limit->p1.min; clock.p1--) {
  730. int this_err;
  731. i9xx_calc_dpll_params(refclk, &clock);
  732. if (!intel_PLL_is_valid(to_i915(dev),
  733. limit,
  734. &clock))
  735. continue;
  736. this_err = abs(clock.dot - target);
  737. if (this_err < err_most) {
  738. *best_clock = clock;
  739. err_most = this_err;
  740. max_n = clock.n;
  741. found = true;
  742. }
  743. }
  744. }
  745. }
  746. }
  747. return found;
  748. }
  749. /*
  750. * Check if the calculated PLL configuration is more optimal compared to the
  751. * best configuration and error found so far. Return the calculated error.
  752. */
  753. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  754. const struct dpll *calculated_clock,
  755. const struct dpll *best_clock,
  756. unsigned int best_error_ppm,
  757. unsigned int *error_ppm)
  758. {
  759. /*
  760. * For CHV ignore the error and consider only the P value.
  761. * Prefer a bigger P value based on HW requirements.
  762. */
  763. if (IS_CHERRYVIEW(to_i915(dev))) {
  764. *error_ppm = 0;
  765. return calculated_clock->p > best_clock->p;
  766. }
  767. if (WARN_ON_ONCE(!target_freq))
  768. return false;
  769. *error_ppm = div_u64(1000000ULL *
  770. abs(target_freq - calculated_clock->dot),
  771. target_freq);
  772. /*
  773. * Prefer a better P value over a better (smaller) error if the error
  774. * is small. Ensure this preference for future configurations too by
  775. * setting the error to 0.
  776. */
  777. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  778. *error_ppm = 0;
  779. return true;
  780. }
  781. return *error_ppm + 10 < best_error_ppm;
  782. }
  783. /*
  784. * Returns a set of divisors for the desired target clock with the given
  785. * refclk, or FALSE. The returned values represent the clock equation:
  786. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  787. */
  788. static bool
  789. vlv_find_best_dpll(const struct intel_limit *limit,
  790. struct intel_crtc_state *crtc_state,
  791. int target, int refclk, struct dpll *match_clock,
  792. struct dpll *best_clock)
  793. {
  794. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  795. struct drm_device *dev = crtc->base.dev;
  796. struct dpll clock;
  797. unsigned int bestppm = 1000000;
  798. /* min update 19.2 MHz */
  799. int max_n = min(limit->n.max, refclk / 19200);
  800. bool found = false;
  801. target *= 5; /* fast clock */
  802. memset(best_clock, 0, sizeof(*best_clock));
  803. /* based on hardware requirement, prefer smaller n to precision */
  804. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  805. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  806. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  807. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  808. clock.p = clock.p1 * clock.p2;
  809. /* based on hardware requirement, prefer bigger m1,m2 values */
  810. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  811. unsigned int ppm;
  812. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  813. refclk * clock.m1);
  814. vlv_calc_dpll_params(refclk, &clock);
  815. if (!intel_PLL_is_valid(to_i915(dev),
  816. limit,
  817. &clock))
  818. continue;
  819. if (!vlv_PLL_is_optimal(dev, target,
  820. &clock,
  821. best_clock,
  822. bestppm, &ppm))
  823. continue;
  824. *best_clock = clock;
  825. bestppm = ppm;
  826. found = true;
  827. }
  828. }
  829. }
  830. }
  831. return found;
  832. }
  833. /*
  834. * Returns a set of divisors for the desired target clock with the given
  835. * refclk, or FALSE. The returned values represent the clock equation:
  836. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  837. */
  838. static bool
  839. chv_find_best_dpll(const struct intel_limit *limit,
  840. struct intel_crtc_state *crtc_state,
  841. int target, int refclk, struct dpll *match_clock,
  842. struct dpll *best_clock)
  843. {
  844. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  845. struct drm_device *dev = crtc->base.dev;
  846. unsigned int best_error_ppm;
  847. struct dpll clock;
  848. uint64_t m2;
  849. int found = false;
  850. memset(best_clock, 0, sizeof(*best_clock));
  851. best_error_ppm = 1000000;
  852. /*
  853. * Based on hardware doc, the n always set to 1, and m1 always
  854. * set to 2. If requires to support 200Mhz refclk, we need to
  855. * revisit this because n may not 1 anymore.
  856. */
  857. clock.n = 1, clock.m1 = 2;
  858. target *= 5; /* fast clock */
  859. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  860. for (clock.p2 = limit->p2.p2_fast;
  861. clock.p2 >= limit->p2.p2_slow;
  862. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  863. unsigned int error_ppm;
  864. clock.p = clock.p1 * clock.p2;
  865. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  866. clock.n) << 22, refclk * clock.m1);
  867. if (m2 > INT_MAX/clock.m1)
  868. continue;
  869. clock.m2 = m2;
  870. chv_calc_dpll_params(refclk, &clock);
  871. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  872. continue;
  873. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  874. best_error_ppm, &error_ppm))
  875. continue;
  876. *best_clock = clock;
  877. best_error_ppm = error_ppm;
  878. found = true;
  879. }
  880. }
  881. return found;
  882. }
  883. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  884. struct dpll *best_clock)
  885. {
  886. int refclk = 100000;
  887. const struct intel_limit *limit = &intel_limits_bxt;
  888. return chv_find_best_dpll(limit, crtc_state,
  889. target_clock, refclk, NULL, best_clock);
  890. }
  891. bool intel_crtc_active(struct intel_crtc *crtc)
  892. {
  893. /* Be paranoid as we can arrive here with only partial
  894. * state retrieved from the hardware during setup.
  895. *
  896. * We can ditch the adjusted_mode.crtc_clock check as soon
  897. * as Haswell has gained clock readout/fastboot support.
  898. *
  899. * We can ditch the crtc->primary->fb check as soon as we can
  900. * properly reconstruct framebuffers.
  901. *
  902. * FIXME: The intel_crtc->active here should be switched to
  903. * crtc->state->active once we have proper CRTC states wired up
  904. * for atomic.
  905. */
  906. return crtc->active && crtc->base.primary->state->fb &&
  907. crtc->config->base.adjusted_mode.crtc_clock;
  908. }
  909. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  910. enum pipe pipe)
  911. {
  912. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  913. return crtc->config->cpu_transcoder;
  914. }
  915. static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  916. {
  917. i915_reg_t reg = PIPEDSL(pipe);
  918. u32 line1, line2;
  919. u32 line_mask;
  920. if (IS_GEN2(dev_priv))
  921. line_mask = DSL_LINEMASK_GEN2;
  922. else
  923. line_mask = DSL_LINEMASK_GEN3;
  924. line1 = I915_READ(reg) & line_mask;
  925. msleep(5);
  926. line2 = I915_READ(reg) & line_mask;
  927. return line1 == line2;
  928. }
  929. /*
  930. * intel_wait_for_pipe_off - wait for pipe to turn off
  931. * @crtc: crtc whose pipe to wait for
  932. *
  933. * After disabling a pipe, we can't wait for vblank in the usual way,
  934. * spinning on the vblank interrupt status bit, since we won't actually
  935. * see an interrupt when the pipe is disabled.
  936. *
  937. * On Gen4 and above:
  938. * wait for the pipe register state bit to turn off
  939. *
  940. * Otherwise:
  941. * wait for the display line value to settle (it usually
  942. * ends up stopping at the start of the next frame).
  943. *
  944. */
  945. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  946. {
  947. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  948. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  949. enum pipe pipe = crtc->pipe;
  950. if (INTEL_GEN(dev_priv) >= 4) {
  951. i915_reg_t reg = PIPECONF(cpu_transcoder);
  952. /* Wait for the Pipe State to go off */
  953. if (intel_wait_for_register(dev_priv,
  954. reg, I965_PIPECONF_ACTIVE, 0,
  955. 100))
  956. WARN(1, "pipe_off wait timed out\n");
  957. } else {
  958. /* Wait for the display line to settle */
  959. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  960. WARN(1, "pipe_off wait timed out\n");
  961. }
  962. }
  963. /* Only for pre-ILK configs */
  964. void assert_pll(struct drm_i915_private *dev_priv,
  965. enum pipe pipe, bool state)
  966. {
  967. u32 val;
  968. bool cur_state;
  969. val = I915_READ(DPLL(pipe));
  970. cur_state = !!(val & DPLL_VCO_ENABLE);
  971. I915_STATE_WARN(cur_state != state,
  972. "PLL state assertion failure (expected %s, current %s)\n",
  973. onoff(state), onoff(cur_state));
  974. }
  975. /* XXX: the dsi pll is shared between MIPI DSI ports */
  976. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  977. {
  978. u32 val;
  979. bool cur_state;
  980. mutex_lock(&dev_priv->sb_lock);
  981. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  982. mutex_unlock(&dev_priv->sb_lock);
  983. cur_state = val & DSI_PLL_VCO_EN;
  984. I915_STATE_WARN(cur_state != state,
  985. "DSI PLL state assertion failure (expected %s, current %s)\n",
  986. onoff(state), onoff(cur_state));
  987. }
  988. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  989. enum pipe pipe, bool state)
  990. {
  991. bool cur_state;
  992. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  993. pipe);
  994. if (HAS_DDI(dev_priv)) {
  995. /* DDI does not have a specific FDI_TX register */
  996. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  997. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  998. } else {
  999. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1000. cur_state = !!(val & FDI_TX_ENABLE);
  1001. }
  1002. I915_STATE_WARN(cur_state != state,
  1003. "FDI TX state assertion failure (expected %s, current %s)\n",
  1004. onoff(state), onoff(cur_state));
  1005. }
  1006. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1007. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1008. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1009. enum pipe pipe, bool state)
  1010. {
  1011. u32 val;
  1012. bool cur_state;
  1013. val = I915_READ(FDI_RX_CTL(pipe));
  1014. cur_state = !!(val & FDI_RX_ENABLE);
  1015. I915_STATE_WARN(cur_state != state,
  1016. "FDI RX state assertion failure (expected %s, current %s)\n",
  1017. onoff(state), onoff(cur_state));
  1018. }
  1019. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1020. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1021. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1022. enum pipe pipe)
  1023. {
  1024. u32 val;
  1025. /* ILK FDI PLL is always enabled */
  1026. if (IS_GEN5(dev_priv))
  1027. return;
  1028. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1029. if (HAS_DDI(dev_priv))
  1030. return;
  1031. val = I915_READ(FDI_TX_CTL(pipe));
  1032. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1033. }
  1034. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1035. enum pipe pipe, bool state)
  1036. {
  1037. u32 val;
  1038. bool cur_state;
  1039. val = I915_READ(FDI_RX_CTL(pipe));
  1040. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1041. I915_STATE_WARN(cur_state != state,
  1042. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1043. onoff(state), onoff(cur_state));
  1044. }
  1045. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1046. {
  1047. i915_reg_t pp_reg;
  1048. u32 val;
  1049. enum pipe panel_pipe = PIPE_A;
  1050. bool locked = true;
  1051. if (WARN_ON(HAS_DDI(dev_priv)))
  1052. return;
  1053. if (HAS_PCH_SPLIT(dev_priv)) {
  1054. u32 port_sel;
  1055. pp_reg = PP_CONTROL(0);
  1056. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1057. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1058. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1059. panel_pipe = PIPE_B;
  1060. /* XXX: else fix for eDP */
  1061. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1062. /* presumably write lock depends on pipe, not port select */
  1063. pp_reg = PP_CONTROL(pipe);
  1064. panel_pipe = pipe;
  1065. } else {
  1066. pp_reg = PP_CONTROL(0);
  1067. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1068. panel_pipe = PIPE_B;
  1069. }
  1070. val = I915_READ(pp_reg);
  1071. if (!(val & PANEL_POWER_ON) ||
  1072. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1073. locked = false;
  1074. I915_STATE_WARN(panel_pipe == pipe && locked,
  1075. "panel assertion failure, pipe %c regs locked\n",
  1076. pipe_name(pipe));
  1077. }
  1078. static void assert_cursor(struct drm_i915_private *dev_priv,
  1079. enum pipe pipe, bool state)
  1080. {
  1081. bool cur_state;
  1082. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1083. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1084. else
  1085. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1086. I915_STATE_WARN(cur_state != state,
  1087. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1088. pipe_name(pipe), onoff(state), onoff(cur_state));
  1089. }
  1090. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1091. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1092. void assert_pipe(struct drm_i915_private *dev_priv,
  1093. enum pipe pipe, bool state)
  1094. {
  1095. bool cur_state;
  1096. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1097. pipe);
  1098. enum intel_display_power_domain power_domain;
  1099. /* if we need the pipe quirk it must be always on */
  1100. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1101. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1102. state = true;
  1103. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1104. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1105. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1106. cur_state = !!(val & PIPECONF_ENABLE);
  1107. intel_display_power_put(dev_priv, power_domain);
  1108. } else {
  1109. cur_state = false;
  1110. }
  1111. I915_STATE_WARN(cur_state != state,
  1112. "pipe %c assertion failure (expected %s, current %s)\n",
  1113. pipe_name(pipe), onoff(state), onoff(cur_state));
  1114. }
  1115. static void assert_plane(struct drm_i915_private *dev_priv,
  1116. enum plane plane, bool state)
  1117. {
  1118. u32 val;
  1119. bool cur_state;
  1120. val = I915_READ(DSPCNTR(plane));
  1121. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1122. I915_STATE_WARN(cur_state != state,
  1123. "plane %c assertion failure (expected %s, current %s)\n",
  1124. plane_name(plane), onoff(state), onoff(cur_state));
  1125. }
  1126. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1127. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1128. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1129. enum pipe pipe)
  1130. {
  1131. int i;
  1132. /* Primary planes are fixed to pipes on gen4+ */
  1133. if (INTEL_GEN(dev_priv) >= 4) {
  1134. u32 val = I915_READ(DSPCNTR(pipe));
  1135. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1136. "plane %c assertion failure, should be disabled but not\n",
  1137. plane_name(pipe));
  1138. return;
  1139. }
  1140. /* Need to check both planes against the pipe */
  1141. for_each_pipe(dev_priv, i) {
  1142. u32 val = I915_READ(DSPCNTR(i));
  1143. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1144. DISPPLANE_SEL_PIPE_SHIFT;
  1145. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1146. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1147. plane_name(i), pipe_name(pipe));
  1148. }
  1149. }
  1150. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1151. enum pipe pipe)
  1152. {
  1153. int sprite;
  1154. if (INTEL_GEN(dev_priv) >= 9) {
  1155. for_each_sprite(dev_priv, pipe, sprite) {
  1156. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1157. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1158. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1159. sprite, pipe_name(pipe));
  1160. }
  1161. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1162. for_each_sprite(dev_priv, pipe, sprite) {
  1163. u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
  1164. I915_STATE_WARN(val & SP_ENABLE,
  1165. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1166. sprite_name(pipe, sprite), pipe_name(pipe));
  1167. }
  1168. } else if (INTEL_GEN(dev_priv) >= 7) {
  1169. u32 val = I915_READ(SPRCTL(pipe));
  1170. I915_STATE_WARN(val & SPRITE_ENABLE,
  1171. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1172. plane_name(pipe), pipe_name(pipe));
  1173. } else if (INTEL_GEN(dev_priv) >= 5) {
  1174. u32 val = I915_READ(DVSCNTR(pipe));
  1175. I915_STATE_WARN(val & DVS_ENABLE,
  1176. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1177. plane_name(pipe), pipe_name(pipe));
  1178. }
  1179. }
  1180. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1181. {
  1182. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1183. drm_crtc_vblank_put(crtc);
  1184. }
  1185. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1186. enum pipe pipe)
  1187. {
  1188. u32 val;
  1189. bool enabled;
  1190. val = I915_READ(PCH_TRANSCONF(pipe));
  1191. enabled = !!(val & TRANS_ENABLE);
  1192. I915_STATE_WARN(enabled,
  1193. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1194. pipe_name(pipe));
  1195. }
  1196. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1197. enum pipe pipe, u32 port_sel, u32 val)
  1198. {
  1199. if ((val & DP_PORT_EN) == 0)
  1200. return false;
  1201. if (HAS_PCH_CPT(dev_priv)) {
  1202. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1203. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1204. return false;
  1205. } else if (IS_CHERRYVIEW(dev_priv)) {
  1206. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1207. return false;
  1208. } else {
  1209. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1210. return false;
  1211. }
  1212. return true;
  1213. }
  1214. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1215. enum pipe pipe, u32 val)
  1216. {
  1217. if ((val & SDVO_ENABLE) == 0)
  1218. return false;
  1219. if (HAS_PCH_CPT(dev_priv)) {
  1220. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1221. return false;
  1222. } else if (IS_CHERRYVIEW(dev_priv)) {
  1223. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1224. return false;
  1225. } else {
  1226. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1227. return false;
  1228. }
  1229. return true;
  1230. }
  1231. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1232. enum pipe pipe, u32 val)
  1233. {
  1234. if ((val & LVDS_PORT_EN) == 0)
  1235. return false;
  1236. if (HAS_PCH_CPT(dev_priv)) {
  1237. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1238. return false;
  1239. } else {
  1240. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1241. return false;
  1242. }
  1243. return true;
  1244. }
  1245. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1246. enum pipe pipe, u32 val)
  1247. {
  1248. if ((val & ADPA_DAC_ENABLE) == 0)
  1249. return false;
  1250. if (HAS_PCH_CPT(dev_priv)) {
  1251. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1252. return false;
  1253. } else {
  1254. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1255. return false;
  1256. }
  1257. return true;
  1258. }
  1259. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1260. enum pipe pipe, i915_reg_t reg,
  1261. u32 port_sel)
  1262. {
  1263. u32 val = I915_READ(reg);
  1264. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1265. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1266. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1267. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1268. && (val & DP_PIPEB_SELECT),
  1269. "IBX PCH dp port still using transcoder B\n");
  1270. }
  1271. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1272. enum pipe pipe, i915_reg_t reg)
  1273. {
  1274. u32 val = I915_READ(reg);
  1275. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1276. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1277. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1278. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1279. && (val & SDVO_PIPE_B_SELECT),
  1280. "IBX PCH hdmi port still using transcoder B\n");
  1281. }
  1282. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1283. enum pipe pipe)
  1284. {
  1285. u32 val;
  1286. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1287. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1288. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1289. val = I915_READ(PCH_ADPA);
  1290. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1291. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1292. pipe_name(pipe));
  1293. val = I915_READ(PCH_LVDS);
  1294. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1295. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1296. pipe_name(pipe));
  1297. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1298. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1299. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1300. }
  1301. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1302. const struct intel_crtc_state *pipe_config)
  1303. {
  1304. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1305. enum pipe pipe = crtc->pipe;
  1306. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1307. POSTING_READ(DPLL(pipe));
  1308. udelay(150);
  1309. if (intel_wait_for_register(dev_priv,
  1310. DPLL(pipe),
  1311. DPLL_LOCK_VLV,
  1312. DPLL_LOCK_VLV,
  1313. 1))
  1314. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1315. }
  1316. static void vlv_enable_pll(struct intel_crtc *crtc,
  1317. const struct intel_crtc_state *pipe_config)
  1318. {
  1319. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1320. enum pipe pipe = crtc->pipe;
  1321. assert_pipe_disabled(dev_priv, pipe);
  1322. /* PLL is protected by panel, make sure we can write it */
  1323. assert_panel_unlocked(dev_priv, pipe);
  1324. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1325. _vlv_enable_pll(crtc, pipe_config);
  1326. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1327. POSTING_READ(DPLL_MD(pipe));
  1328. }
  1329. static void _chv_enable_pll(struct intel_crtc *crtc,
  1330. const struct intel_crtc_state *pipe_config)
  1331. {
  1332. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1333. enum pipe pipe = crtc->pipe;
  1334. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1335. u32 tmp;
  1336. mutex_lock(&dev_priv->sb_lock);
  1337. /* Enable back the 10bit clock to display controller */
  1338. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1339. tmp |= DPIO_DCLKP_EN;
  1340. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1341. mutex_unlock(&dev_priv->sb_lock);
  1342. /*
  1343. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1344. */
  1345. udelay(1);
  1346. /* Enable PLL */
  1347. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1348. /* Check PLL is locked */
  1349. if (intel_wait_for_register(dev_priv,
  1350. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1351. 1))
  1352. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1353. }
  1354. static void chv_enable_pll(struct intel_crtc *crtc,
  1355. const struct intel_crtc_state *pipe_config)
  1356. {
  1357. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1358. enum pipe pipe = crtc->pipe;
  1359. assert_pipe_disabled(dev_priv, pipe);
  1360. /* PLL is protected by panel, make sure we can write it */
  1361. assert_panel_unlocked(dev_priv, pipe);
  1362. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1363. _chv_enable_pll(crtc, pipe_config);
  1364. if (pipe != PIPE_A) {
  1365. /*
  1366. * WaPixelRepeatModeFixForC0:chv
  1367. *
  1368. * DPLLCMD is AWOL. Use chicken bits to propagate
  1369. * the value from DPLLBMD to either pipe B or C.
  1370. */
  1371. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1372. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1373. I915_WRITE(CBR4_VLV, 0);
  1374. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1375. /*
  1376. * DPLLB VGA mode also seems to cause problems.
  1377. * We should always have it disabled.
  1378. */
  1379. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1380. } else {
  1381. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1382. POSTING_READ(DPLL_MD(pipe));
  1383. }
  1384. }
  1385. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1386. {
  1387. struct intel_crtc *crtc;
  1388. int count = 0;
  1389. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1390. count += crtc->base.state->active &&
  1391. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1392. }
  1393. return count;
  1394. }
  1395. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1396. {
  1397. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1398. i915_reg_t reg = DPLL(crtc->pipe);
  1399. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1400. assert_pipe_disabled(dev_priv, crtc->pipe);
  1401. /* PLL is protected by panel, make sure we can write it */
  1402. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1403. assert_panel_unlocked(dev_priv, crtc->pipe);
  1404. /* Enable DVO 2x clock on both PLLs if necessary */
  1405. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1406. /*
  1407. * It appears to be important that we don't enable this
  1408. * for the current pipe before otherwise configuring the
  1409. * PLL. No idea how this should be handled if multiple
  1410. * DVO outputs are enabled simultaneosly.
  1411. */
  1412. dpll |= DPLL_DVO_2X_MODE;
  1413. I915_WRITE(DPLL(!crtc->pipe),
  1414. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1415. }
  1416. /*
  1417. * Apparently we need to have VGA mode enabled prior to changing
  1418. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1419. * dividers, even though the register value does change.
  1420. */
  1421. I915_WRITE(reg, 0);
  1422. I915_WRITE(reg, dpll);
  1423. /* Wait for the clocks to stabilize. */
  1424. POSTING_READ(reg);
  1425. udelay(150);
  1426. if (INTEL_GEN(dev_priv) >= 4) {
  1427. I915_WRITE(DPLL_MD(crtc->pipe),
  1428. crtc->config->dpll_hw_state.dpll_md);
  1429. } else {
  1430. /* The pixel multiplier can only be updated once the
  1431. * DPLL is enabled and the clocks are stable.
  1432. *
  1433. * So write it again.
  1434. */
  1435. I915_WRITE(reg, dpll);
  1436. }
  1437. /* We do this three times for luck */
  1438. I915_WRITE(reg, dpll);
  1439. POSTING_READ(reg);
  1440. udelay(150); /* wait for warmup */
  1441. I915_WRITE(reg, dpll);
  1442. POSTING_READ(reg);
  1443. udelay(150); /* wait for warmup */
  1444. I915_WRITE(reg, dpll);
  1445. POSTING_READ(reg);
  1446. udelay(150); /* wait for warmup */
  1447. }
  1448. /**
  1449. * i9xx_disable_pll - disable a PLL
  1450. * @dev_priv: i915 private structure
  1451. * @pipe: pipe PLL to disable
  1452. *
  1453. * Disable the PLL for @pipe, making sure the pipe is off first.
  1454. *
  1455. * Note! This is for pre-ILK only.
  1456. */
  1457. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1458. {
  1459. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1460. enum pipe pipe = crtc->pipe;
  1461. /* Disable DVO 2x clock on both PLLs if necessary */
  1462. if (IS_I830(dev_priv) &&
  1463. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1464. !intel_num_dvo_pipes(dev_priv)) {
  1465. I915_WRITE(DPLL(PIPE_B),
  1466. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1467. I915_WRITE(DPLL(PIPE_A),
  1468. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1469. }
  1470. /* Don't disable pipe or pipe PLLs if needed */
  1471. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1472. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1473. return;
  1474. /* Make sure the pipe isn't still relying on us */
  1475. assert_pipe_disabled(dev_priv, pipe);
  1476. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1477. POSTING_READ(DPLL(pipe));
  1478. }
  1479. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1480. {
  1481. u32 val;
  1482. /* Make sure the pipe isn't still relying on us */
  1483. assert_pipe_disabled(dev_priv, pipe);
  1484. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1485. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1486. if (pipe != PIPE_A)
  1487. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1488. I915_WRITE(DPLL(pipe), val);
  1489. POSTING_READ(DPLL(pipe));
  1490. }
  1491. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1492. {
  1493. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1494. u32 val;
  1495. /* Make sure the pipe isn't still relying on us */
  1496. assert_pipe_disabled(dev_priv, pipe);
  1497. val = DPLL_SSC_REF_CLK_CHV |
  1498. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1499. if (pipe != PIPE_A)
  1500. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1501. I915_WRITE(DPLL(pipe), val);
  1502. POSTING_READ(DPLL(pipe));
  1503. mutex_lock(&dev_priv->sb_lock);
  1504. /* Disable 10bit clock to display controller */
  1505. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1506. val &= ~DPIO_DCLKP_EN;
  1507. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1508. mutex_unlock(&dev_priv->sb_lock);
  1509. }
  1510. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1511. struct intel_digital_port *dport,
  1512. unsigned int expected_mask)
  1513. {
  1514. u32 port_mask;
  1515. i915_reg_t dpll_reg;
  1516. switch (dport->port) {
  1517. case PORT_B:
  1518. port_mask = DPLL_PORTB_READY_MASK;
  1519. dpll_reg = DPLL(0);
  1520. break;
  1521. case PORT_C:
  1522. port_mask = DPLL_PORTC_READY_MASK;
  1523. dpll_reg = DPLL(0);
  1524. expected_mask <<= 4;
  1525. break;
  1526. case PORT_D:
  1527. port_mask = DPLL_PORTD_READY_MASK;
  1528. dpll_reg = DPIO_PHY_STATUS;
  1529. break;
  1530. default:
  1531. BUG();
  1532. }
  1533. if (intel_wait_for_register(dev_priv,
  1534. dpll_reg, port_mask, expected_mask,
  1535. 1000))
  1536. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1537. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1538. }
  1539. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1540. enum pipe pipe)
  1541. {
  1542. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1543. pipe);
  1544. i915_reg_t reg;
  1545. uint32_t val, pipeconf_val;
  1546. /* Make sure PCH DPLL is enabled */
  1547. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1548. /* FDI must be feeding us bits for PCH ports */
  1549. assert_fdi_tx_enabled(dev_priv, pipe);
  1550. assert_fdi_rx_enabled(dev_priv, pipe);
  1551. if (HAS_PCH_CPT(dev_priv)) {
  1552. /* Workaround: Set the timing override bit before enabling the
  1553. * pch transcoder. */
  1554. reg = TRANS_CHICKEN2(pipe);
  1555. val = I915_READ(reg);
  1556. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1557. I915_WRITE(reg, val);
  1558. }
  1559. reg = PCH_TRANSCONF(pipe);
  1560. val = I915_READ(reg);
  1561. pipeconf_val = I915_READ(PIPECONF(pipe));
  1562. if (HAS_PCH_IBX(dev_priv)) {
  1563. /*
  1564. * Make the BPC in transcoder be consistent with
  1565. * that in pipeconf reg. For HDMI we must use 8bpc
  1566. * here for both 8bpc and 12bpc.
  1567. */
  1568. val &= ~PIPECONF_BPC_MASK;
  1569. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1570. val |= PIPECONF_8BPC;
  1571. else
  1572. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1573. }
  1574. val &= ~TRANS_INTERLACE_MASK;
  1575. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1576. if (HAS_PCH_IBX(dev_priv) &&
  1577. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1578. val |= TRANS_LEGACY_INTERLACED_ILK;
  1579. else
  1580. val |= TRANS_INTERLACED;
  1581. else
  1582. val |= TRANS_PROGRESSIVE;
  1583. I915_WRITE(reg, val | TRANS_ENABLE);
  1584. if (intel_wait_for_register(dev_priv,
  1585. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1586. 100))
  1587. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1588. }
  1589. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1590. enum transcoder cpu_transcoder)
  1591. {
  1592. u32 val, pipeconf_val;
  1593. /* FDI must be feeding us bits for PCH ports */
  1594. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1595. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1596. /* Workaround: set timing override bit. */
  1597. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1598. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1599. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1600. val = TRANS_ENABLE;
  1601. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1602. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1603. PIPECONF_INTERLACED_ILK)
  1604. val |= TRANS_INTERLACED;
  1605. else
  1606. val |= TRANS_PROGRESSIVE;
  1607. I915_WRITE(LPT_TRANSCONF, val);
  1608. if (intel_wait_for_register(dev_priv,
  1609. LPT_TRANSCONF,
  1610. TRANS_STATE_ENABLE,
  1611. TRANS_STATE_ENABLE,
  1612. 100))
  1613. DRM_ERROR("Failed to enable PCH transcoder\n");
  1614. }
  1615. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1616. enum pipe pipe)
  1617. {
  1618. i915_reg_t reg;
  1619. uint32_t val;
  1620. /* FDI relies on the transcoder */
  1621. assert_fdi_tx_disabled(dev_priv, pipe);
  1622. assert_fdi_rx_disabled(dev_priv, pipe);
  1623. /* Ports must be off as well */
  1624. assert_pch_ports_disabled(dev_priv, pipe);
  1625. reg = PCH_TRANSCONF(pipe);
  1626. val = I915_READ(reg);
  1627. val &= ~TRANS_ENABLE;
  1628. I915_WRITE(reg, val);
  1629. /* wait for PCH transcoder off, transcoder state */
  1630. if (intel_wait_for_register(dev_priv,
  1631. reg, TRANS_STATE_ENABLE, 0,
  1632. 50))
  1633. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1634. if (HAS_PCH_CPT(dev_priv)) {
  1635. /* Workaround: Clear the timing override chicken bit again. */
  1636. reg = TRANS_CHICKEN2(pipe);
  1637. val = I915_READ(reg);
  1638. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1639. I915_WRITE(reg, val);
  1640. }
  1641. }
  1642. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1643. {
  1644. u32 val;
  1645. val = I915_READ(LPT_TRANSCONF);
  1646. val &= ~TRANS_ENABLE;
  1647. I915_WRITE(LPT_TRANSCONF, val);
  1648. /* wait for PCH transcoder off, transcoder state */
  1649. if (intel_wait_for_register(dev_priv,
  1650. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1651. 50))
  1652. DRM_ERROR("Failed to disable PCH transcoder\n");
  1653. /* Workaround: clear timing override bit. */
  1654. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1655. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1656. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1657. }
  1658. enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1659. {
  1660. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1661. WARN_ON(!crtc->config->has_pch_encoder);
  1662. if (HAS_PCH_LPT(dev_priv))
  1663. return TRANSCODER_A;
  1664. else
  1665. return (enum transcoder) crtc->pipe;
  1666. }
  1667. /**
  1668. * intel_enable_pipe - enable a pipe, asserting requirements
  1669. * @crtc: crtc responsible for the pipe
  1670. *
  1671. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1672. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1673. */
  1674. static void intel_enable_pipe(struct intel_crtc *crtc)
  1675. {
  1676. struct drm_device *dev = crtc->base.dev;
  1677. struct drm_i915_private *dev_priv = to_i915(dev);
  1678. enum pipe pipe = crtc->pipe;
  1679. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1680. i915_reg_t reg;
  1681. u32 val;
  1682. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1683. assert_planes_disabled(dev_priv, pipe);
  1684. assert_cursor_disabled(dev_priv, pipe);
  1685. assert_sprites_disabled(dev_priv, pipe);
  1686. /*
  1687. * A pipe without a PLL won't actually be able to drive bits from
  1688. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1689. * need the check.
  1690. */
  1691. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1692. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1693. assert_dsi_pll_enabled(dev_priv);
  1694. else
  1695. assert_pll_enabled(dev_priv, pipe);
  1696. } else {
  1697. if (crtc->config->has_pch_encoder) {
  1698. /* if driving the PCH, we need FDI enabled */
  1699. assert_fdi_rx_pll_enabled(dev_priv,
  1700. (enum pipe) intel_crtc_pch_transcoder(crtc));
  1701. assert_fdi_tx_pll_enabled(dev_priv,
  1702. (enum pipe) cpu_transcoder);
  1703. }
  1704. /* FIXME: assert CPU port conditions for SNB+ */
  1705. }
  1706. reg = PIPECONF(cpu_transcoder);
  1707. val = I915_READ(reg);
  1708. if (val & PIPECONF_ENABLE) {
  1709. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1710. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1711. return;
  1712. }
  1713. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1714. POSTING_READ(reg);
  1715. /*
  1716. * Until the pipe starts DSL will read as 0, which would cause
  1717. * an apparent vblank timestamp jump, which messes up also the
  1718. * frame count when it's derived from the timestamps. So let's
  1719. * wait for the pipe to start properly before we call
  1720. * drm_crtc_vblank_on()
  1721. */
  1722. if (dev->max_vblank_count == 0 &&
  1723. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1724. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1725. }
  1726. /**
  1727. * intel_disable_pipe - disable a pipe, asserting requirements
  1728. * @crtc: crtc whose pipes is to be disabled
  1729. *
  1730. * Disable the pipe of @crtc, making sure that various hardware
  1731. * specific requirements are met, if applicable, e.g. plane
  1732. * disabled, panel fitter off, etc.
  1733. *
  1734. * Will wait until the pipe has shut down before returning.
  1735. */
  1736. static void intel_disable_pipe(struct intel_crtc *crtc)
  1737. {
  1738. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1739. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1740. enum pipe pipe = crtc->pipe;
  1741. i915_reg_t reg;
  1742. u32 val;
  1743. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1744. /*
  1745. * Make sure planes won't keep trying to pump pixels to us,
  1746. * or we might hang the display.
  1747. */
  1748. assert_planes_disabled(dev_priv, pipe);
  1749. assert_cursor_disabled(dev_priv, pipe);
  1750. assert_sprites_disabled(dev_priv, pipe);
  1751. reg = PIPECONF(cpu_transcoder);
  1752. val = I915_READ(reg);
  1753. if ((val & PIPECONF_ENABLE) == 0)
  1754. return;
  1755. /*
  1756. * Double wide has implications for planes
  1757. * so best keep it disabled when not needed.
  1758. */
  1759. if (crtc->config->double_wide)
  1760. val &= ~PIPECONF_DOUBLE_WIDE;
  1761. /* Don't disable pipe or pipe PLLs if needed */
  1762. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1763. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1764. val &= ~PIPECONF_ENABLE;
  1765. I915_WRITE(reg, val);
  1766. if ((val & PIPECONF_ENABLE) == 0)
  1767. intel_wait_for_pipe_off(crtc);
  1768. }
  1769. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1770. {
  1771. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1772. }
  1773. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1774. uint64_t fb_modifier, unsigned int cpp)
  1775. {
  1776. switch (fb_modifier) {
  1777. case DRM_FORMAT_MOD_NONE:
  1778. return cpp;
  1779. case I915_FORMAT_MOD_X_TILED:
  1780. if (IS_GEN2(dev_priv))
  1781. return 128;
  1782. else
  1783. return 512;
  1784. case I915_FORMAT_MOD_Y_TILED:
  1785. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1786. return 128;
  1787. else
  1788. return 512;
  1789. case I915_FORMAT_MOD_Yf_TILED:
  1790. switch (cpp) {
  1791. case 1:
  1792. return 64;
  1793. case 2:
  1794. case 4:
  1795. return 128;
  1796. case 8:
  1797. case 16:
  1798. return 256;
  1799. default:
  1800. MISSING_CASE(cpp);
  1801. return cpp;
  1802. }
  1803. break;
  1804. default:
  1805. MISSING_CASE(fb_modifier);
  1806. return cpp;
  1807. }
  1808. }
  1809. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1810. uint64_t fb_modifier, unsigned int cpp)
  1811. {
  1812. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1813. return 1;
  1814. else
  1815. return intel_tile_size(dev_priv) /
  1816. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1817. }
  1818. /* Return the tile dimensions in pixel units */
  1819. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1820. unsigned int *tile_width,
  1821. unsigned int *tile_height,
  1822. uint64_t fb_modifier,
  1823. unsigned int cpp)
  1824. {
  1825. unsigned int tile_width_bytes =
  1826. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1827. *tile_width = tile_width_bytes / cpp;
  1828. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1829. }
  1830. unsigned int
  1831. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1832. uint32_t pixel_format, uint64_t fb_modifier)
  1833. {
  1834. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1835. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1836. return ALIGN(height, tile_height);
  1837. }
  1838. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1839. {
  1840. unsigned int size = 0;
  1841. int i;
  1842. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1843. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1844. return size;
  1845. }
  1846. static void
  1847. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1848. const struct drm_framebuffer *fb,
  1849. unsigned int rotation)
  1850. {
  1851. view->type = I915_GGTT_VIEW_NORMAL;
  1852. if (drm_rotation_90_or_270(rotation)) {
  1853. view->type = I915_GGTT_VIEW_ROTATED;
  1854. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1855. }
  1856. }
  1857. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1858. {
  1859. if (INTEL_INFO(dev_priv)->gen >= 9)
  1860. return 256 * 1024;
  1861. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1862. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1863. return 128 * 1024;
  1864. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1865. return 4 * 1024;
  1866. else
  1867. return 0;
  1868. }
  1869. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1870. uint64_t fb_modifier)
  1871. {
  1872. switch (fb_modifier) {
  1873. case DRM_FORMAT_MOD_NONE:
  1874. return intel_linear_alignment(dev_priv);
  1875. case I915_FORMAT_MOD_X_TILED:
  1876. if (INTEL_INFO(dev_priv)->gen >= 9)
  1877. return 256 * 1024;
  1878. return 0;
  1879. case I915_FORMAT_MOD_Y_TILED:
  1880. case I915_FORMAT_MOD_Yf_TILED:
  1881. return 1 * 1024 * 1024;
  1882. default:
  1883. MISSING_CASE(fb_modifier);
  1884. return 0;
  1885. }
  1886. }
  1887. struct i915_vma *
  1888. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1889. {
  1890. struct drm_device *dev = fb->dev;
  1891. struct drm_i915_private *dev_priv = to_i915(dev);
  1892. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1893. struct i915_ggtt_view view;
  1894. struct i915_vma *vma;
  1895. u32 alignment;
  1896. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1897. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  1898. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1899. /* Note that the w/a also requires 64 PTE of padding following the
  1900. * bo. We currently fill all unused PTE with the shadow page and so
  1901. * we should always have valid PTE following the scanout preventing
  1902. * the VT-d warning.
  1903. */
  1904. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1905. alignment = 256 * 1024;
  1906. /*
  1907. * Global gtt pte registers are special registers which actually forward
  1908. * writes to a chunk of system memory. Which means that there is no risk
  1909. * that the register values disappear as soon as we call
  1910. * intel_runtime_pm_put(), so it is correct to wrap only the
  1911. * pin/unpin/fence and not more.
  1912. */
  1913. intel_runtime_pm_get(dev_priv);
  1914. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1915. if (IS_ERR(vma))
  1916. goto err;
  1917. if (i915_vma_is_map_and_fenceable(vma)) {
  1918. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1919. * fence, whereas 965+ only requires a fence if using
  1920. * framebuffer compression. For simplicity, we always, when
  1921. * possible, install a fence as the cost is not that onerous.
  1922. *
  1923. * If we fail to fence the tiled scanout, then either the
  1924. * modeset will reject the change (which is highly unlikely as
  1925. * the affected systems, all but one, do not have unmappable
  1926. * space) or we will not be able to enable full powersaving
  1927. * techniques (also likely not to apply due to various limits
  1928. * FBC and the like impose on the size of the buffer, which
  1929. * presumably we violated anyway with this unmappable buffer).
  1930. * Anyway, it is presumably better to stumble onwards with
  1931. * something and try to run the system in a "less than optimal"
  1932. * mode that matches the user configuration.
  1933. */
  1934. if (i915_vma_get_fence(vma) == 0)
  1935. i915_vma_pin_fence(vma);
  1936. }
  1937. err:
  1938. intel_runtime_pm_put(dev_priv);
  1939. return vma;
  1940. }
  1941. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1942. {
  1943. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1944. struct i915_ggtt_view view;
  1945. struct i915_vma *vma;
  1946. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1947. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1948. vma = i915_gem_object_to_ggtt(obj, &view);
  1949. i915_vma_unpin_fence(vma);
  1950. i915_gem_object_unpin_from_display_plane(vma);
  1951. }
  1952. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1953. unsigned int rotation)
  1954. {
  1955. if (drm_rotation_90_or_270(rotation))
  1956. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1957. else
  1958. return fb->pitches[plane];
  1959. }
  1960. /*
  1961. * Convert the x/y offsets into a linear offset.
  1962. * Only valid with 0/180 degree rotation, which is fine since linear
  1963. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1964. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1965. */
  1966. u32 intel_fb_xy_to_linear(int x, int y,
  1967. const struct intel_plane_state *state,
  1968. int plane)
  1969. {
  1970. const struct drm_framebuffer *fb = state->base.fb;
  1971. unsigned int cpp = fb->format->cpp[plane];
  1972. unsigned int pitch = fb->pitches[plane];
  1973. return y * pitch + x * cpp;
  1974. }
  1975. /*
  1976. * Add the x/y offsets derived from fb->offsets[] to the user
  1977. * specified plane src x/y offsets. The resulting x/y offsets
  1978. * specify the start of scanout from the beginning of the gtt mapping.
  1979. */
  1980. void intel_add_fb_offsets(int *x, int *y,
  1981. const struct intel_plane_state *state,
  1982. int plane)
  1983. {
  1984. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1985. unsigned int rotation = state->base.rotation;
  1986. if (drm_rotation_90_or_270(rotation)) {
  1987. *x += intel_fb->rotated[plane].x;
  1988. *y += intel_fb->rotated[plane].y;
  1989. } else {
  1990. *x += intel_fb->normal[plane].x;
  1991. *y += intel_fb->normal[plane].y;
  1992. }
  1993. }
  1994. /*
  1995. * Input tile dimensions and pitch must already be
  1996. * rotated to match x and y, and in pixel units.
  1997. */
  1998. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1999. unsigned int tile_width,
  2000. unsigned int tile_height,
  2001. unsigned int tile_size,
  2002. unsigned int pitch_tiles,
  2003. u32 old_offset,
  2004. u32 new_offset)
  2005. {
  2006. unsigned int pitch_pixels = pitch_tiles * tile_width;
  2007. unsigned int tiles;
  2008. WARN_ON(old_offset & (tile_size - 1));
  2009. WARN_ON(new_offset & (tile_size - 1));
  2010. WARN_ON(new_offset > old_offset);
  2011. tiles = (old_offset - new_offset) / tile_size;
  2012. *y += tiles / pitch_tiles * tile_height;
  2013. *x += tiles % pitch_tiles * tile_width;
  2014. /* minimize x in case it got needlessly big */
  2015. *y += *x / pitch_pixels * tile_height;
  2016. *x %= pitch_pixels;
  2017. return new_offset;
  2018. }
  2019. /*
  2020. * Adjust the tile offset by moving the difference into
  2021. * the x/y offsets.
  2022. */
  2023. static u32 intel_adjust_tile_offset(int *x, int *y,
  2024. const struct intel_plane_state *state, int plane,
  2025. u32 old_offset, u32 new_offset)
  2026. {
  2027. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2028. const struct drm_framebuffer *fb = state->base.fb;
  2029. unsigned int cpp = fb->format->cpp[plane];
  2030. unsigned int rotation = state->base.rotation;
  2031. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  2032. WARN_ON(new_offset > old_offset);
  2033. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  2034. unsigned int tile_size, tile_width, tile_height;
  2035. unsigned int pitch_tiles;
  2036. tile_size = intel_tile_size(dev_priv);
  2037. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2038. fb->modifier, cpp);
  2039. if (drm_rotation_90_or_270(rotation)) {
  2040. pitch_tiles = pitch / tile_height;
  2041. swap(tile_width, tile_height);
  2042. } else {
  2043. pitch_tiles = pitch / (tile_width * cpp);
  2044. }
  2045. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2046. tile_size, pitch_tiles,
  2047. old_offset, new_offset);
  2048. } else {
  2049. old_offset += *y * pitch + *x * cpp;
  2050. *y = (old_offset - new_offset) / pitch;
  2051. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2052. }
  2053. return new_offset;
  2054. }
  2055. /*
  2056. * Computes the linear offset to the base tile and adjusts
  2057. * x, y. bytes per pixel is assumed to be a power-of-two.
  2058. *
  2059. * In the 90/270 rotated case, x and y are assumed
  2060. * to be already rotated to match the rotated GTT view, and
  2061. * pitch is the tile_height aligned framebuffer height.
  2062. *
  2063. * This function is used when computing the derived information
  2064. * under intel_framebuffer, so using any of that information
  2065. * here is not allowed. Anything under drm_framebuffer can be
  2066. * used. This is why the user has to pass in the pitch since it
  2067. * is specified in the rotated orientation.
  2068. */
  2069. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2070. int *x, int *y,
  2071. const struct drm_framebuffer *fb, int plane,
  2072. unsigned int pitch,
  2073. unsigned int rotation,
  2074. u32 alignment)
  2075. {
  2076. uint64_t fb_modifier = fb->modifier;
  2077. unsigned int cpp = fb->format->cpp[plane];
  2078. u32 offset, offset_aligned;
  2079. if (alignment)
  2080. alignment--;
  2081. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2082. unsigned int tile_size, tile_width, tile_height;
  2083. unsigned int tile_rows, tiles, pitch_tiles;
  2084. tile_size = intel_tile_size(dev_priv);
  2085. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2086. fb_modifier, cpp);
  2087. if (drm_rotation_90_or_270(rotation)) {
  2088. pitch_tiles = pitch / tile_height;
  2089. swap(tile_width, tile_height);
  2090. } else {
  2091. pitch_tiles = pitch / (tile_width * cpp);
  2092. }
  2093. tile_rows = *y / tile_height;
  2094. *y %= tile_height;
  2095. tiles = *x / tile_width;
  2096. *x %= tile_width;
  2097. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2098. offset_aligned = offset & ~alignment;
  2099. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2100. tile_size, pitch_tiles,
  2101. offset, offset_aligned);
  2102. } else {
  2103. offset = *y * pitch + *x * cpp;
  2104. offset_aligned = offset & ~alignment;
  2105. *y = (offset & alignment) / pitch;
  2106. *x = ((offset & alignment) - *y * pitch) / cpp;
  2107. }
  2108. return offset_aligned;
  2109. }
  2110. u32 intel_compute_tile_offset(int *x, int *y,
  2111. const struct intel_plane_state *state,
  2112. int plane)
  2113. {
  2114. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2115. const struct drm_framebuffer *fb = state->base.fb;
  2116. unsigned int rotation = state->base.rotation;
  2117. int pitch = intel_fb_pitch(fb, plane, rotation);
  2118. u32 alignment;
  2119. /* AUX_DIST needs only 4K alignment */
  2120. if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
  2121. alignment = 4096;
  2122. else
  2123. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2124. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2125. rotation, alignment);
  2126. }
  2127. /* Convert the fb->offset[] linear offset into x/y offsets */
  2128. static void intel_fb_offset_to_xy(int *x, int *y,
  2129. const struct drm_framebuffer *fb, int plane)
  2130. {
  2131. unsigned int cpp = fb->format->cpp[plane];
  2132. unsigned int pitch = fb->pitches[plane];
  2133. u32 linear_offset = fb->offsets[plane];
  2134. *y = linear_offset / pitch;
  2135. *x = linear_offset % pitch / cpp;
  2136. }
  2137. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2138. {
  2139. switch (fb_modifier) {
  2140. case I915_FORMAT_MOD_X_TILED:
  2141. return I915_TILING_X;
  2142. case I915_FORMAT_MOD_Y_TILED:
  2143. return I915_TILING_Y;
  2144. default:
  2145. return I915_TILING_NONE;
  2146. }
  2147. }
  2148. static int
  2149. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2150. struct drm_framebuffer *fb)
  2151. {
  2152. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2153. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2154. u32 gtt_offset_rotated = 0;
  2155. unsigned int max_size = 0;
  2156. int i, num_planes = fb->format->num_planes;
  2157. unsigned int tile_size = intel_tile_size(dev_priv);
  2158. for (i = 0; i < num_planes; i++) {
  2159. unsigned int width, height;
  2160. unsigned int cpp, size;
  2161. u32 offset;
  2162. int x, y;
  2163. cpp = fb->format->cpp[i];
  2164. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2165. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2166. intel_fb_offset_to_xy(&x, &y, fb, i);
  2167. /*
  2168. * The fence (if used) is aligned to the start of the object
  2169. * so having the framebuffer wrap around across the edge of the
  2170. * fenced region doesn't really work. We have no API to configure
  2171. * the fence start offset within the object (nor could we probably
  2172. * on gen2/3). So it's just easier if we just require that the
  2173. * fb layout agrees with the fence layout. We already check that the
  2174. * fb stride matches the fence stride elsewhere.
  2175. */
  2176. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2177. (x + width) * cpp > fb->pitches[i]) {
  2178. DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
  2179. i, fb->offsets[i]);
  2180. return -EINVAL;
  2181. }
  2182. /*
  2183. * First pixel of the framebuffer from
  2184. * the start of the normal gtt mapping.
  2185. */
  2186. intel_fb->normal[i].x = x;
  2187. intel_fb->normal[i].y = y;
  2188. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2189. fb, 0, fb->pitches[i],
  2190. DRM_ROTATE_0, tile_size);
  2191. offset /= tile_size;
  2192. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  2193. unsigned int tile_width, tile_height;
  2194. unsigned int pitch_tiles;
  2195. struct drm_rect r;
  2196. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2197. fb->modifier, cpp);
  2198. rot_info->plane[i].offset = offset;
  2199. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2200. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2201. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2202. intel_fb->rotated[i].pitch =
  2203. rot_info->plane[i].height * tile_height;
  2204. /* how many tiles does this plane need */
  2205. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2206. /*
  2207. * If the plane isn't horizontally tile aligned,
  2208. * we need one more tile.
  2209. */
  2210. if (x != 0)
  2211. size++;
  2212. /* rotate the x/y offsets to match the GTT view */
  2213. r.x1 = x;
  2214. r.y1 = y;
  2215. r.x2 = x + width;
  2216. r.y2 = y + height;
  2217. drm_rect_rotate(&r,
  2218. rot_info->plane[i].width * tile_width,
  2219. rot_info->plane[i].height * tile_height,
  2220. DRM_ROTATE_270);
  2221. x = r.x1;
  2222. y = r.y1;
  2223. /* rotate the tile dimensions to match the GTT view */
  2224. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2225. swap(tile_width, tile_height);
  2226. /*
  2227. * We only keep the x/y offsets, so push all of the
  2228. * gtt offset into the x/y offsets.
  2229. */
  2230. _intel_adjust_tile_offset(&x, &y, tile_size,
  2231. tile_width, tile_height, pitch_tiles,
  2232. gtt_offset_rotated * tile_size, 0);
  2233. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2234. /*
  2235. * First pixel of the framebuffer from
  2236. * the start of the rotated gtt mapping.
  2237. */
  2238. intel_fb->rotated[i].x = x;
  2239. intel_fb->rotated[i].y = y;
  2240. } else {
  2241. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2242. x * cpp, tile_size);
  2243. }
  2244. /* how many tiles in total needed in the bo */
  2245. max_size = max(max_size, offset + size);
  2246. }
  2247. if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
  2248. DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2249. max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
  2250. return -EINVAL;
  2251. }
  2252. return 0;
  2253. }
  2254. static int i9xx_format_to_fourcc(int format)
  2255. {
  2256. switch (format) {
  2257. case DISPPLANE_8BPP:
  2258. return DRM_FORMAT_C8;
  2259. case DISPPLANE_BGRX555:
  2260. return DRM_FORMAT_XRGB1555;
  2261. case DISPPLANE_BGRX565:
  2262. return DRM_FORMAT_RGB565;
  2263. default:
  2264. case DISPPLANE_BGRX888:
  2265. return DRM_FORMAT_XRGB8888;
  2266. case DISPPLANE_RGBX888:
  2267. return DRM_FORMAT_XBGR8888;
  2268. case DISPPLANE_BGRX101010:
  2269. return DRM_FORMAT_XRGB2101010;
  2270. case DISPPLANE_RGBX101010:
  2271. return DRM_FORMAT_XBGR2101010;
  2272. }
  2273. }
  2274. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2275. {
  2276. switch (format) {
  2277. case PLANE_CTL_FORMAT_RGB_565:
  2278. return DRM_FORMAT_RGB565;
  2279. default:
  2280. case PLANE_CTL_FORMAT_XRGB_8888:
  2281. if (rgb_order) {
  2282. if (alpha)
  2283. return DRM_FORMAT_ABGR8888;
  2284. else
  2285. return DRM_FORMAT_XBGR8888;
  2286. } else {
  2287. if (alpha)
  2288. return DRM_FORMAT_ARGB8888;
  2289. else
  2290. return DRM_FORMAT_XRGB8888;
  2291. }
  2292. case PLANE_CTL_FORMAT_XRGB_2101010:
  2293. if (rgb_order)
  2294. return DRM_FORMAT_XBGR2101010;
  2295. else
  2296. return DRM_FORMAT_XRGB2101010;
  2297. }
  2298. }
  2299. static bool
  2300. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2301. struct intel_initial_plane_config *plane_config)
  2302. {
  2303. struct drm_device *dev = crtc->base.dev;
  2304. struct drm_i915_private *dev_priv = to_i915(dev);
  2305. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2306. struct drm_i915_gem_object *obj = NULL;
  2307. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2308. struct drm_framebuffer *fb = &plane_config->fb->base;
  2309. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2310. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2311. PAGE_SIZE);
  2312. size_aligned -= base_aligned;
  2313. if (plane_config->size == 0)
  2314. return false;
  2315. /* If the FB is too big, just don't use it since fbdev is not very
  2316. * important and we should probably use that space with FBC or other
  2317. * features. */
  2318. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2319. return false;
  2320. mutex_lock(&dev->struct_mutex);
  2321. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2322. base_aligned,
  2323. base_aligned,
  2324. size_aligned);
  2325. if (!obj) {
  2326. mutex_unlock(&dev->struct_mutex);
  2327. return false;
  2328. }
  2329. if (plane_config->tiling == I915_TILING_X)
  2330. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2331. mode_cmd.pixel_format = fb->format->format;
  2332. mode_cmd.width = fb->width;
  2333. mode_cmd.height = fb->height;
  2334. mode_cmd.pitches[0] = fb->pitches[0];
  2335. mode_cmd.modifier[0] = fb->modifier;
  2336. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2337. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2338. &mode_cmd, obj)) {
  2339. DRM_DEBUG_KMS("intel fb init failed\n");
  2340. goto out_unref_obj;
  2341. }
  2342. mutex_unlock(&dev->struct_mutex);
  2343. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2344. return true;
  2345. out_unref_obj:
  2346. i915_gem_object_put(obj);
  2347. mutex_unlock(&dev->struct_mutex);
  2348. return false;
  2349. }
  2350. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2351. static void
  2352. update_state_fb(struct drm_plane *plane)
  2353. {
  2354. if (plane->fb == plane->state->fb)
  2355. return;
  2356. if (plane->state->fb)
  2357. drm_framebuffer_unreference(plane->state->fb);
  2358. plane->state->fb = plane->fb;
  2359. if (plane->state->fb)
  2360. drm_framebuffer_reference(plane->state->fb);
  2361. }
  2362. static void
  2363. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2364. struct intel_initial_plane_config *plane_config)
  2365. {
  2366. struct drm_device *dev = intel_crtc->base.dev;
  2367. struct drm_i915_private *dev_priv = to_i915(dev);
  2368. struct drm_crtc *c;
  2369. struct intel_crtc *i;
  2370. struct drm_i915_gem_object *obj;
  2371. struct drm_plane *primary = intel_crtc->base.primary;
  2372. struct drm_plane_state *plane_state = primary->state;
  2373. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2374. struct intel_plane *intel_plane = to_intel_plane(primary);
  2375. struct intel_plane_state *intel_state =
  2376. to_intel_plane_state(plane_state);
  2377. struct drm_framebuffer *fb;
  2378. if (!plane_config->fb)
  2379. return;
  2380. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2381. fb = &plane_config->fb->base;
  2382. goto valid_fb;
  2383. }
  2384. kfree(plane_config->fb);
  2385. /*
  2386. * Failed to alloc the obj, check to see if we should share
  2387. * an fb with another CRTC instead
  2388. */
  2389. for_each_crtc(dev, c) {
  2390. i = to_intel_crtc(c);
  2391. if (c == &intel_crtc->base)
  2392. continue;
  2393. if (!i->active)
  2394. continue;
  2395. fb = c->primary->fb;
  2396. if (!fb)
  2397. continue;
  2398. obj = intel_fb_obj(fb);
  2399. if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
  2400. drm_framebuffer_reference(fb);
  2401. goto valid_fb;
  2402. }
  2403. }
  2404. /*
  2405. * We've failed to reconstruct the BIOS FB. Current display state
  2406. * indicates that the primary plane is visible, but has a NULL FB,
  2407. * which will lead to problems later if we don't fix it up. The
  2408. * simplest solution is to just disable the primary plane now and
  2409. * pretend the BIOS never had it enabled.
  2410. */
  2411. plane_state->visible = false;
  2412. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2413. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2414. intel_plane->disable_plane(primary, &intel_crtc->base);
  2415. return;
  2416. valid_fb:
  2417. plane_state->src_x = 0;
  2418. plane_state->src_y = 0;
  2419. plane_state->src_w = fb->width << 16;
  2420. plane_state->src_h = fb->height << 16;
  2421. plane_state->crtc_x = 0;
  2422. plane_state->crtc_y = 0;
  2423. plane_state->crtc_w = fb->width;
  2424. plane_state->crtc_h = fb->height;
  2425. intel_state->base.src = drm_plane_state_src(plane_state);
  2426. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2427. obj = intel_fb_obj(fb);
  2428. if (i915_gem_object_is_tiled(obj))
  2429. dev_priv->preserve_bios_swizzle = true;
  2430. drm_framebuffer_reference(fb);
  2431. primary->fb = primary->state->fb = fb;
  2432. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2433. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2434. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2435. &obj->frontbuffer_bits);
  2436. }
  2437. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2438. unsigned int rotation)
  2439. {
  2440. int cpp = fb->format->cpp[plane];
  2441. switch (fb->modifier) {
  2442. case DRM_FORMAT_MOD_NONE:
  2443. case I915_FORMAT_MOD_X_TILED:
  2444. switch (cpp) {
  2445. case 8:
  2446. return 4096;
  2447. case 4:
  2448. case 2:
  2449. case 1:
  2450. return 8192;
  2451. default:
  2452. MISSING_CASE(cpp);
  2453. break;
  2454. }
  2455. break;
  2456. case I915_FORMAT_MOD_Y_TILED:
  2457. case I915_FORMAT_MOD_Yf_TILED:
  2458. switch (cpp) {
  2459. case 8:
  2460. return 2048;
  2461. case 4:
  2462. return 4096;
  2463. case 2:
  2464. case 1:
  2465. return 8192;
  2466. default:
  2467. MISSING_CASE(cpp);
  2468. break;
  2469. }
  2470. break;
  2471. default:
  2472. MISSING_CASE(fb->modifier);
  2473. }
  2474. return 2048;
  2475. }
  2476. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2477. {
  2478. const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
  2479. const struct drm_framebuffer *fb = plane_state->base.fb;
  2480. unsigned int rotation = plane_state->base.rotation;
  2481. int x = plane_state->base.src.x1 >> 16;
  2482. int y = plane_state->base.src.y1 >> 16;
  2483. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2484. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2485. int max_width = skl_max_plane_width(fb, 0, rotation);
  2486. int max_height = 4096;
  2487. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2488. if (w > max_width || h > max_height) {
  2489. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2490. w, h, max_width, max_height);
  2491. return -EINVAL;
  2492. }
  2493. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2494. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2495. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2496. /*
  2497. * AUX surface offset is specified as the distance from the
  2498. * main surface offset, and it must be non-negative. Make
  2499. * sure that is what we will get.
  2500. */
  2501. if (offset > aux_offset)
  2502. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2503. offset, aux_offset & ~(alignment - 1));
  2504. /*
  2505. * When using an X-tiled surface, the plane blows up
  2506. * if the x offset + width exceed the stride.
  2507. *
  2508. * TODO: linear and Y-tiled seem fine, Yf untested,
  2509. */
  2510. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2511. int cpp = fb->format->cpp[0];
  2512. while ((x + w) * cpp > fb->pitches[0]) {
  2513. if (offset == 0) {
  2514. DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
  2515. return -EINVAL;
  2516. }
  2517. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2518. offset, offset - alignment);
  2519. }
  2520. }
  2521. plane_state->main.offset = offset;
  2522. plane_state->main.x = x;
  2523. plane_state->main.y = y;
  2524. return 0;
  2525. }
  2526. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2527. {
  2528. const struct drm_framebuffer *fb = plane_state->base.fb;
  2529. unsigned int rotation = plane_state->base.rotation;
  2530. int max_width = skl_max_plane_width(fb, 1, rotation);
  2531. int max_height = 4096;
  2532. int x = plane_state->base.src.x1 >> 17;
  2533. int y = plane_state->base.src.y1 >> 17;
  2534. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2535. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2536. u32 offset;
  2537. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2538. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2539. /* FIXME not quite sure how/if these apply to the chroma plane */
  2540. if (w > max_width || h > max_height) {
  2541. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2542. w, h, max_width, max_height);
  2543. return -EINVAL;
  2544. }
  2545. plane_state->aux.offset = offset;
  2546. plane_state->aux.x = x;
  2547. plane_state->aux.y = y;
  2548. return 0;
  2549. }
  2550. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2551. {
  2552. const struct drm_framebuffer *fb = plane_state->base.fb;
  2553. unsigned int rotation = plane_state->base.rotation;
  2554. int ret;
  2555. if (!plane_state->base.visible)
  2556. return 0;
  2557. /* Rotate src coordinates to match rotated GTT view */
  2558. if (drm_rotation_90_or_270(rotation))
  2559. drm_rect_rotate(&plane_state->base.src,
  2560. fb->width << 16, fb->height << 16,
  2561. DRM_ROTATE_270);
  2562. /*
  2563. * Handle the AUX surface first since
  2564. * the main surface setup depends on it.
  2565. */
  2566. if (fb->format->format == DRM_FORMAT_NV12) {
  2567. ret = skl_check_nv12_aux_surface(plane_state);
  2568. if (ret)
  2569. return ret;
  2570. } else {
  2571. plane_state->aux.offset = ~0xfff;
  2572. plane_state->aux.x = 0;
  2573. plane_state->aux.y = 0;
  2574. }
  2575. ret = skl_check_main_surface(plane_state);
  2576. if (ret)
  2577. return ret;
  2578. return 0;
  2579. }
  2580. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2581. const struct intel_crtc_state *crtc_state,
  2582. const struct intel_plane_state *plane_state)
  2583. {
  2584. struct drm_i915_private *dev_priv = to_i915(primary->dev);
  2585. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2586. struct drm_framebuffer *fb = plane_state->base.fb;
  2587. int plane = intel_crtc->plane;
  2588. u32 linear_offset;
  2589. u32 dspcntr;
  2590. i915_reg_t reg = DSPCNTR(plane);
  2591. unsigned int rotation = plane_state->base.rotation;
  2592. int x = plane_state->base.src.x1 >> 16;
  2593. int y = plane_state->base.src.y1 >> 16;
  2594. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2595. dspcntr |= DISPLAY_PLANE_ENABLE;
  2596. if (INTEL_GEN(dev_priv) < 4) {
  2597. if (intel_crtc->pipe == PIPE_B)
  2598. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2599. /* pipesrc and dspsize control the size that is scaled from,
  2600. * which should always be the user's requested size.
  2601. */
  2602. I915_WRITE(DSPSIZE(plane),
  2603. ((crtc_state->pipe_src_h - 1) << 16) |
  2604. (crtc_state->pipe_src_w - 1));
  2605. I915_WRITE(DSPPOS(plane), 0);
  2606. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2607. I915_WRITE(PRIMSIZE(plane),
  2608. ((crtc_state->pipe_src_h - 1) << 16) |
  2609. (crtc_state->pipe_src_w - 1));
  2610. I915_WRITE(PRIMPOS(plane), 0);
  2611. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2612. }
  2613. switch (fb->format->format) {
  2614. case DRM_FORMAT_C8:
  2615. dspcntr |= DISPPLANE_8BPP;
  2616. break;
  2617. case DRM_FORMAT_XRGB1555:
  2618. dspcntr |= DISPPLANE_BGRX555;
  2619. break;
  2620. case DRM_FORMAT_RGB565:
  2621. dspcntr |= DISPPLANE_BGRX565;
  2622. break;
  2623. case DRM_FORMAT_XRGB8888:
  2624. dspcntr |= DISPPLANE_BGRX888;
  2625. break;
  2626. case DRM_FORMAT_XBGR8888:
  2627. dspcntr |= DISPPLANE_RGBX888;
  2628. break;
  2629. case DRM_FORMAT_XRGB2101010:
  2630. dspcntr |= DISPPLANE_BGRX101010;
  2631. break;
  2632. case DRM_FORMAT_XBGR2101010:
  2633. dspcntr |= DISPPLANE_RGBX101010;
  2634. break;
  2635. default:
  2636. BUG();
  2637. }
  2638. if (INTEL_GEN(dev_priv) >= 4 &&
  2639. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2640. dspcntr |= DISPPLANE_TILED;
  2641. if (rotation & DRM_ROTATE_180)
  2642. dspcntr |= DISPPLANE_ROTATE_180;
  2643. if (rotation & DRM_REFLECT_X)
  2644. dspcntr |= DISPPLANE_MIRROR;
  2645. if (IS_G4X(dev_priv))
  2646. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2647. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2648. if (INTEL_GEN(dev_priv) >= 4)
  2649. intel_crtc->dspaddr_offset =
  2650. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2651. if (rotation & DRM_ROTATE_180) {
  2652. x += crtc_state->pipe_src_w - 1;
  2653. y += crtc_state->pipe_src_h - 1;
  2654. } else if (rotation & DRM_REFLECT_X) {
  2655. x += crtc_state->pipe_src_w - 1;
  2656. }
  2657. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2658. if (INTEL_GEN(dev_priv) < 4)
  2659. intel_crtc->dspaddr_offset = linear_offset;
  2660. intel_crtc->adjusted_x = x;
  2661. intel_crtc->adjusted_y = y;
  2662. I915_WRITE(reg, dspcntr);
  2663. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2664. if (INTEL_GEN(dev_priv) >= 4) {
  2665. I915_WRITE(DSPSURF(plane),
  2666. intel_fb_gtt_offset(fb, rotation) +
  2667. intel_crtc->dspaddr_offset);
  2668. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2669. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2670. } else {
  2671. I915_WRITE(DSPADDR(plane),
  2672. intel_fb_gtt_offset(fb, rotation) +
  2673. intel_crtc->dspaddr_offset);
  2674. }
  2675. POSTING_READ(reg);
  2676. }
  2677. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2678. struct drm_crtc *crtc)
  2679. {
  2680. struct drm_device *dev = crtc->dev;
  2681. struct drm_i915_private *dev_priv = to_i915(dev);
  2682. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2683. int plane = intel_crtc->plane;
  2684. I915_WRITE(DSPCNTR(plane), 0);
  2685. if (INTEL_INFO(dev_priv)->gen >= 4)
  2686. I915_WRITE(DSPSURF(plane), 0);
  2687. else
  2688. I915_WRITE(DSPADDR(plane), 0);
  2689. POSTING_READ(DSPCNTR(plane));
  2690. }
  2691. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2692. const struct intel_crtc_state *crtc_state,
  2693. const struct intel_plane_state *plane_state)
  2694. {
  2695. struct drm_device *dev = primary->dev;
  2696. struct drm_i915_private *dev_priv = to_i915(dev);
  2697. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2698. struct drm_framebuffer *fb = plane_state->base.fb;
  2699. int plane = intel_crtc->plane;
  2700. u32 linear_offset;
  2701. u32 dspcntr;
  2702. i915_reg_t reg = DSPCNTR(plane);
  2703. unsigned int rotation = plane_state->base.rotation;
  2704. int x = plane_state->base.src.x1 >> 16;
  2705. int y = plane_state->base.src.y1 >> 16;
  2706. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2707. dspcntr |= DISPLAY_PLANE_ENABLE;
  2708. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2709. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2710. switch (fb->format->format) {
  2711. case DRM_FORMAT_C8:
  2712. dspcntr |= DISPPLANE_8BPP;
  2713. break;
  2714. case DRM_FORMAT_RGB565:
  2715. dspcntr |= DISPPLANE_BGRX565;
  2716. break;
  2717. case DRM_FORMAT_XRGB8888:
  2718. dspcntr |= DISPPLANE_BGRX888;
  2719. break;
  2720. case DRM_FORMAT_XBGR8888:
  2721. dspcntr |= DISPPLANE_RGBX888;
  2722. break;
  2723. case DRM_FORMAT_XRGB2101010:
  2724. dspcntr |= DISPPLANE_BGRX101010;
  2725. break;
  2726. case DRM_FORMAT_XBGR2101010:
  2727. dspcntr |= DISPPLANE_RGBX101010;
  2728. break;
  2729. default:
  2730. BUG();
  2731. }
  2732. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  2733. dspcntr |= DISPPLANE_TILED;
  2734. if (rotation & DRM_ROTATE_180)
  2735. dspcntr |= DISPPLANE_ROTATE_180;
  2736. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
  2737. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2738. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2739. intel_crtc->dspaddr_offset =
  2740. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2741. /* HSW+ does this automagically in hardware */
  2742. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
  2743. rotation & DRM_ROTATE_180) {
  2744. x += crtc_state->pipe_src_w - 1;
  2745. y += crtc_state->pipe_src_h - 1;
  2746. }
  2747. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2748. intel_crtc->adjusted_x = x;
  2749. intel_crtc->adjusted_y = y;
  2750. I915_WRITE(reg, dspcntr);
  2751. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2752. I915_WRITE(DSPSURF(plane),
  2753. intel_fb_gtt_offset(fb, rotation) +
  2754. intel_crtc->dspaddr_offset);
  2755. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2756. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2757. } else {
  2758. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2759. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2760. }
  2761. POSTING_READ(reg);
  2762. }
  2763. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2764. uint64_t fb_modifier, uint32_t pixel_format)
  2765. {
  2766. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2767. return 64;
  2768. } else {
  2769. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2770. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2771. }
  2772. }
  2773. u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
  2774. unsigned int rotation)
  2775. {
  2776. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2777. struct i915_ggtt_view view;
  2778. struct i915_vma *vma;
  2779. intel_fill_fb_ggtt_view(&view, fb, rotation);
  2780. vma = i915_gem_object_to_ggtt(obj, &view);
  2781. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2782. view.type))
  2783. return -1;
  2784. return i915_ggtt_offset(vma);
  2785. }
  2786. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2787. {
  2788. struct drm_device *dev = intel_crtc->base.dev;
  2789. struct drm_i915_private *dev_priv = to_i915(dev);
  2790. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2791. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2792. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2793. }
  2794. /*
  2795. * This function detaches (aka. unbinds) unused scalers in hardware
  2796. */
  2797. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2798. {
  2799. struct intel_crtc_scaler_state *scaler_state;
  2800. int i;
  2801. scaler_state = &intel_crtc->config->scaler_state;
  2802. /* loop through and disable scalers that aren't in use */
  2803. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2804. if (!scaler_state->scalers[i].in_use)
  2805. skl_detach_scaler(intel_crtc, i);
  2806. }
  2807. }
  2808. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2809. unsigned int rotation)
  2810. {
  2811. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2812. u32 stride = intel_fb_pitch(fb, plane, rotation);
  2813. /*
  2814. * The stride is either expressed as a multiple of 64 bytes chunks for
  2815. * linear buffers or in number of tiles for tiled buffers.
  2816. */
  2817. if (drm_rotation_90_or_270(rotation)) {
  2818. int cpp = fb->format->cpp[plane];
  2819. stride /= intel_tile_height(dev_priv, fb->modifier, cpp);
  2820. } else {
  2821. stride /= intel_fb_stride_alignment(dev_priv, fb->modifier,
  2822. fb->format->format);
  2823. }
  2824. return stride;
  2825. }
  2826. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2827. {
  2828. switch (pixel_format) {
  2829. case DRM_FORMAT_C8:
  2830. return PLANE_CTL_FORMAT_INDEXED;
  2831. case DRM_FORMAT_RGB565:
  2832. return PLANE_CTL_FORMAT_RGB_565;
  2833. case DRM_FORMAT_XBGR8888:
  2834. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2835. case DRM_FORMAT_XRGB8888:
  2836. return PLANE_CTL_FORMAT_XRGB_8888;
  2837. /*
  2838. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2839. * to be already pre-multiplied. We need to add a knob (or a different
  2840. * DRM_FORMAT) for user-space to configure that.
  2841. */
  2842. case DRM_FORMAT_ABGR8888:
  2843. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2844. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2845. case DRM_FORMAT_ARGB8888:
  2846. return PLANE_CTL_FORMAT_XRGB_8888 |
  2847. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2848. case DRM_FORMAT_XRGB2101010:
  2849. return PLANE_CTL_FORMAT_XRGB_2101010;
  2850. case DRM_FORMAT_XBGR2101010:
  2851. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2852. case DRM_FORMAT_YUYV:
  2853. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2854. case DRM_FORMAT_YVYU:
  2855. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2856. case DRM_FORMAT_UYVY:
  2857. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2858. case DRM_FORMAT_VYUY:
  2859. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2860. default:
  2861. MISSING_CASE(pixel_format);
  2862. }
  2863. return 0;
  2864. }
  2865. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2866. {
  2867. switch (fb_modifier) {
  2868. case DRM_FORMAT_MOD_NONE:
  2869. break;
  2870. case I915_FORMAT_MOD_X_TILED:
  2871. return PLANE_CTL_TILED_X;
  2872. case I915_FORMAT_MOD_Y_TILED:
  2873. return PLANE_CTL_TILED_Y;
  2874. case I915_FORMAT_MOD_Yf_TILED:
  2875. return PLANE_CTL_TILED_YF;
  2876. default:
  2877. MISSING_CASE(fb_modifier);
  2878. }
  2879. return 0;
  2880. }
  2881. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2882. {
  2883. switch (rotation) {
  2884. case DRM_ROTATE_0:
  2885. break;
  2886. /*
  2887. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2888. * while i915 HW rotation is clockwise, thats why this swapping.
  2889. */
  2890. case DRM_ROTATE_90:
  2891. return PLANE_CTL_ROTATE_270;
  2892. case DRM_ROTATE_180:
  2893. return PLANE_CTL_ROTATE_180;
  2894. case DRM_ROTATE_270:
  2895. return PLANE_CTL_ROTATE_90;
  2896. default:
  2897. MISSING_CASE(rotation);
  2898. }
  2899. return 0;
  2900. }
  2901. static void skylake_update_primary_plane(struct drm_plane *plane,
  2902. const struct intel_crtc_state *crtc_state,
  2903. const struct intel_plane_state *plane_state)
  2904. {
  2905. struct drm_device *dev = plane->dev;
  2906. struct drm_i915_private *dev_priv = to_i915(dev);
  2907. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2908. struct drm_framebuffer *fb = plane_state->base.fb;
  2909. enum plane_id plane_id = to_intel_plane(plane)->id;
  2910. enum pipe pipe = to_intel_plane(plane)->pipe;
  2911. u32 plane_ctl;
  2912. unsigned int rotation = plane_state->base.rotation;
  2913. u32 stride = skl_plane_stride(fb, 0, rotation);
  2914. u32 surf_addr = plane_state->main.offset;
  2915. int scaler_id = plane_state->scaler_id;
  2916. int src_x = plane_state->main.x;
  2917. int src_y = plane_state->main.y;
  2918. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2919. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2920. int dst_x = plane_state->base.dst.x1;
  2921. int dst_y = plane_state->base.dst.y1;
  2922. int dst_w = drm_rect_width(&plane_state->base.dst);
  2923. int dst_h = drm_rect_height(&plane_state->base.dst);
  2924. plane_ctl = PLANE_CTL_ENABLE |
  2925. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2926. PLANE_CTL_PIPE_CSC_ENABLE;
  2927. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  2928. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  2929. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2930. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2931. /* Sizes are 0 based */
  2932. src_w--;
  2933. src_h--;
  2934. dst_w--;
  2935. dst_h--;
  2936. intel_crtc->dspaddr_offset = surf_addr;
  2937. intel_crtc->adjusted_x = src_x;
  2938. intel_crtc->adjusted_y = src_y;
  2939. I915_WRITE(PLANE_CTL(pipe, plane_id), plane_ctl);
  2940. I915_WRITE(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
  2941. I915_WRITE(PLANE_STRIDE(pipe, plane_id), stride);
  2942. I915_WRITE(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  2943. if (scaler_id >= 0) {
  2944. uint32_t ps_ctrl = 0;
  2945. WARN_ON(!dst_w || !dst_h);
  2946. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
  2947. crtc_state->scaler_state.scalers[scaler_id].mode;
  2948. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2949. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2950. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2951. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2952. I915_WRITE(PLANE_POS(pipe, plane_id), 0);
  2953. } else {
  2954. I915_WRITE(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
  2955. }
  2956. I915_WRITE(PLANE_SURF(pipe, plane_id),
  2957. intel_fb_gtt_offset(fb, rotation) + surf_addr);
  2958. POSTING_READ(PLANE_SURF(pipe, plane_id));
  2959. }
  2960. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2961. struct drm_crtc *crtc)
  2962. {
  2963. struct drm_device *dev = crtc->dev;
  2964. struct drm_i915_private *dev_priv = to_i915(dev);
  2965. enum plane_id plane_id = to_intel_plane(primary)->id;
  2966. enum pipe pipe = to_intel_plane(primary)->pipe;
  2967. I915_WRITE(PLANE_CTL(pipe, plane_id), 0);
  2968. I915_WRITE(PLANE_SURF(pipe, plane_id), 0);
  2969. POSTING_READ(PLANE_SURF(pipe, plane_id));
  2970. }
  2971. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2972. static int
  2973. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2974. int x, int y, enum mode_set_atomic state)
  2975. {
  2976. /* Support for kgdboc is disabled, this needs a major rework. */
  2977. DRM_ERROR("legacy panic handler not supported any more.\n");
  2978. return -ENODEV;
  2979. }
  2980. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2981. {
  2982. struct intel_crtc *crtc;
  2983. for_each_intel_crtc(&dev_priv->drm, crtc)
  2984. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2985. }
  2986. static void intel_update_primary_planes(struct drm_device *dev)
  2987. {
  2988. struct drm_crtc *crtc;
  2989. for_each_crtc(dev, crtc) {
  2990. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2991. struct intel_plane_state *plane_state =
  2992. to_intel_plane_state(plane->base.state);
  2993. if (plane_state->base.visible)
  2994. plane->update_plane(&plane->base,
  2995. to_intel_crtc_state(crtc->state),
  2996. plane_state);
  2997. }
  2998. }
  2999. static int
  3000. __intel_display_resume(struct drm_device *dev,
  3001. struct drm_atomic_state *state)
  3002. {
  3003. struct drm_crtc_state *crtc_state;
  3004. struct drm_crtc *crtc;
  3005. int i, ret;
  3006. intel_modeset_setup_hw_state(dev);
  3007. i915_redisable_vga(to_i915(dev));
  3008. if (!state)
  3009. return 0;
  3010. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3011. /*
  3012. * Force recalculation even if we restore
  3013. * current state. With fast modeset this may not result
  3014. * in a modeset when the state is compatible.
  3015. */
  3016. crtc_state->mode_changed = true;
  3017. }
  3018. /* ignore any reset values/BIOS leftovers in the WM registers */
  3019. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3020. ret = drm_atomic_commit(state);
  3021. WARN_ON(ret == -EDEADLK);
  3022. return ret;
  3023. }
  3024. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3025. {
  3026. return intel_has_gpu_reset(dev_priv) &&
  3027. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3028. }
  3029. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3030. {
  3031. struct drm_device *dev = &dev_priv->drm;
  3032. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3033. struct drm_atomic_state *state;
  3034. int ret;
  3035. /*
  3036. * Need mode_config.mutex so that we don't
  3037. * trample ongoing ->detect() and whatnot.
  3038. */
  3039. mutex_lock(&dev->mode_config.mutex);
  3040. drm_modeset_acquire_init(ctx, 0);
  3041. while (1) {
  3042. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3043. if (ret != -EDEADLK)
  3044. break;
  3045. drm_modeset_backoff(ctx);
  3046. }
  3047. /* reset doesn't touch the display, but flips might get nuked anyway, */
  3048. if (!i915.force_reset_modeset_test &&
  3049. !gpu_reset_clobbers_display(dev_priv))
  3050. return;
  3051. /*
  3052. * Disabling the crtcs gracefully seems nicer. Also the
  3053. * g33 docs say we should at least disable all the planes.
  3054. */
  3055. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3056. if (IS_ERR(state)) {
  3057. ret = PTR_ERR(state);
  3058. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3059. return;
  3060. }
  3061. ret = drm_atomic_helper_disable_all(dev, ctx);
  3062. if (ret) {
  3063. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3064. drm_atomic_state_put(state);
  3065. return;
  3066. }
  3067. dev_priv->modeset_restore_state = state;
  3068. state->acquire_ctx = ctx;
  3069. }
  3070. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3071. {
  3072. struct drm_device *dev = &dev_priv->drm;
  3073. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3074. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3075. int ret;
  3076. /*
  3077. * Flips in the rings will be nuked by the reset,
  3078. * so complete all pending flips so that user space
  3079. * will get its events and not get stuck.
  3080. */
  3081. intel_complete_page_flips(dev_priv);
  3082. dev_priv->modeset_restore_state = NULL;
  3083. /* reset doesn't touch the display */
  3084. if (!gpu_reset_clobbers_display(dev_priv)) {
  3085. if (!state) {
  3086. /*
  3087. * Flips in the rings have been nuked by the reset,
  3088. * so update the base address of all primary
  3089. * planes to the the last fb to make sure we're
  3090. * showing the correct fb after a reset.
  3091. *
  3092. * FIXME: Atomic will make this obsolete since we won't schedule
  3093. * CS-based flips (which might get lost in gpu resets) any more.
  3094. */
  3095. intel_update_primary_planes(dev);
  3096. } else {
  3097. ret = __intel_display_resume(dev, state);
  3098. if (ret)
  3099. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3100. }
  3101. } else {
  3102. /*
  3103. * The display has been reset as well,
  3104. * so need a full re-initialization.
  3105. */
  3106. intel_runtime_pm_disable_interrupts(dev_priv);
  3107. intel_runtime_pm_enable_interrupts(dev_priv);
  3108. intel_pps_unlock_regs_wa(dev_priv);
  3109. intel_modeset_init_hw(dev);
  3110. spin_lock_irq(&dev_priv->irq_lock);
  3111. if (dev_priv->display.hpd_irq_setup)
  3112. dev_priv->display.hpd_irq_setup(dev_priv);
  3113. spin_unlock_irq(&dev_priv->irq_lock);
  3114. ret = __intel_display_resume(dev, state);
  3115. if (ret)
  3116. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3117. intel_hpd_init(dev_priv);
  3118. }
  3119. if (state)
  3120. drm_atomic_state_put(state);
  3121. drm_modeset_drop_locks(ctx);
  3122. drm_modeset_acquire_fini(ctx);
  3123. mutex_unlock(&dev->mode_config.mutex);
  3124. }
  3125. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3126. {
  3127. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3128. if (i915_reset_in_progress(error))
  3129. return true;
  3130. if (crtc->reset_count != i915_reset_count(error))
  3131. return true;
  3132. return false;
  3133. }
  3134. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3135. {
  3136. struct drm_device *dev = crtc->dev;
  3137. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3138. bool pending;
  3139. if (abort_flip_on_reset(intel_crtc))
  3140. return false;
  3141. spin_lock_irq(&dev->event_lock);
  3142. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3143. spin_unlock_irq(&dev->event_lock);
  3144. return pending;
  3145. }
  3146. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3147. struct intel_crtc_state *old_crtc_state)
  3148. {
  3149. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3150. struct intel_crtc_state *pipe_config =
  3151. to_intel_crtc_state(crtc->base.state);
  3152. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3153. crtc->base.mode = crtc->base.state->mode;
  3154. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  3155. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  3156. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  3157. /*
  3158. * Update pipe size and adjust fitter if needed: the reason for this is
  3159. * that in compute_mode_changes we check the native mode (not the pfit
  3160. * mode) to see if we can flip rather than do a full mode set. In the
  3161. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3162. * pfit state, we'll end up with a big fb scanned out into the wrong
  3163. * sized surface.
  3164. */
  3165. I915_WRITE(PIPESRC(crtc->pipe),
  3166. ((pipe_config->pipe_src_w - 1) << 16) |
  3167. (pipe_config->pipe_src_h - 1));
  3168. /* on skylake this is done by detaching scalers */
  3169. if (INTEL_GEN(dev_priv) >= 9) {
  3170. skl_detach_scalers(crtc);
  3171. if (pipe_config->pch_pfit.enabled)
  3172. skylake_pfit_enable(crtc);
  3173. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3174. if (pipe_config->pch_pfit.enabled)
  3175. ironlake_pfit_enable(crtc);
  3176. else if (old_crtc_state->pch_pfit.enabled)
  3177. ironlake_pfit_disable(crtc, true);
  3178. }
  3179. }
  3180. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  3181. {
  3182. struct drm_device *dev = crtc->dev;
  3183. struct drm_i915_private *dev_priv = to_i915(dev);
  3184. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3185. int pipe = intel_crtc->pipe;
  3186. i915_reg_t reg;
  3187. u32 temp;
  3188. /* enable normal train */
  3189. reg = FDI_TX_CTL(pipe);
  3190. temp = I915_READ(reg);
  3191. if (IS_IVYBRIDGE(dev_priv)) {
  3192. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3193. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3194. } else {
  3195. temp &= ~FDI_LINK_TRAIN_NONE;
  3196. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3197. }
  3198. I915_WRITE(reg, temp);
  3199. reg = FDI_RX_CTL(pipe);
  3200. temp = I915_READ(reg);
  3201. if (HAS_PCH_CPT(dev_priv)) {
  3202. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3203. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3204. } else {
  3205. temp &= ~FDI_LINK_TRAIN_NONE;
  3206. temp |= FDI_LINK_TRAIN_NONE;
  3207. }
  3208. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3209. /* wait one idle pattern time */
  3210. POSTING_READ(reg);
  3211. udelay(1000);
  3212. /* IVB wants error correction enabled */
  3213. if (IS_IVYBRIDGE(dev_priv))
  3214. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3215. FDI_FE_ERRC_ENABLE);
  3216. }
  3217. /* The FDI link training functions for ILK/Ibexpeak. */
  3218. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  3219. {
  3220. struct drm_device *dev = crtc->dev;
  3221. struct drm_i915_private *dev_priv = to_i915(dev);
  3222. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3223. int pipe = intel_crtc->pipe;
  3224. i915_reg_t reg;
  3225. u32 temp, tries;
  3226. /* FDI needs bits from pipe first */
  3227. assert_pipe_enabled(dev_priv, pipe);
  3228. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3229. for train result */
  3230. reg = FDI_RX_IMR(pipe);
  3231. temp = I915_READ(reg);
  3232. temp &= ~FDI_RX_SYMBOL_LOCK;
  3233. temp &= ~FDI_RX_BIT_LOCK;
  3234. I915_WRITE(reg, temp);
  3235. I915_READ(reg);
  3236. udelay(150);
  3237. /* enable CPU FDI TX and PCH FDI RX */
  3238. reg = FDI_TX_CTL(pipe);
  3239. temp = I915_READ(reg);
  3240. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3241. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3242. temp &= ~FDI_LINK_TRAIN_NONE;
  3243. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3244. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3245. reg = FDI_RX_CTL(pipe);
  3246. temp = I915_READ(reg);
  3247. temp &= ~FDI_LINK_TRAIN_NONE;
  3248. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3249. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3250. POSTING_READ(reg);
  3251. udelay(150);
  3252. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3253. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3254. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3255. FDI_RX_PHASE_SYNC_POINTER_EN);
  3256. reg = FDI_RX_IIR(pipe);
  3257. for (tries = 0; tries < 5; tries++) {
  3258. temp = I915_READ(reg);
  3259. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3260. if ((temp & FDI_RX_BIT_LOCK)) {
  3261. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3262. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3263. break;
  3264. }
  3265. }
  3266. if (tries == 5)
  3267. DRM_ERROR("FDI train 1 fail!\n");
  3268. /* Train 2 */
  3269. reg = FDI_TX_CTL(pipe);
  3270. temp = I915_READ(reg);
  3271. temp &= ~FDI_LINK_TRAIN_NONE;
  3272. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3273. I915_WRITE(reg, temp);
  3274. reg = FDI_RX_CTL(pipe);
  3275. temp = I915_READ(reg);
  3276. temp &= ~FDI_LINK_TRAIN_NONE;
  3277. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3278. I915_WRITE(reg, temp);
  3279. POSTING_READ(reg);
  3280. udelay(150);
  3281. reg = FDI_RX_IIR(pipe);
  3282. for (tries = 0; tries < 5; tries++) {
  3283. temp = I915_READ(reg);
  3284. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3285. if (temp & FDI_RX_SYMBOL_LOCK) {
  3286. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3287. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3288. break;
  3289. }
  3290. }
  3291. if (tries == 5)
  3292. DRM_ERROR("FDI train 2 fail!\n");
  3293. DRM_DEBUG_KMS("FDI train done\n");
  3294. }
  3295. static const int snb_b_fdi_train_param[] = {
  3296. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3297. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3298. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3299. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3300. };
  3301. /* The FDI link training functions for SNB/Cougarpoint. */
  3302. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3303. {
  3304. struct drm_device *dev = crtc->dev;
  3305. struct drm_i915_private *dev_priv = to_i915(dev);
  3306. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3307. int pipe = intel_crtc->pipe;
  3308. i915_reg_t reg;
  3309. u32 temp, i, retry;
  3310. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3311. for train result */
  3312. reg = FDI_RX_IMR(pipe);
  3313. temp = I915_READ(reg);
  3314. temp &= ~FDI_RX_SYMBOL_LOCK;
  3315. temp &= ~FDI_RX_BIT_LOCK;
  3316. I915_WRITE(reg, temp);
  3317. POSTING_READ(reg);
  3318. udelay(150);
  3319. /* enable CPU FDI TX and PCH FDI RX */
  3320. reg = FDI_TX_CTL(pipe);
  3321. temp = I915_READ(reg);
  3322. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3323. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3324. temp &= ~FDI_LINK_TRAIN_NONE;
  3325. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3326. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3327. /* SNB-B */
  3328. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3329. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3330. I915_WRITE(FDI_RX_MISC(pipe),
  3331. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3332. reg = FDI_RX_CTL(pipe);
  3333. temp = I915_READ(reg);
  3334. if (HAS_PCH_CPT(dev_priv)) {
  3335. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3336. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3337. } else {
  3338. temp &= ~FDI_LINK_TRAIN_NONE;
  3339. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3340. }
  3341. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3342. POSTING_READ(reg);
  3343. udelay(150);
  3344. for (i = 0; i < 4; i++) {
  3345. reg = FDI_TX_CTL(pipe);
  3346. temp = I915_READ(reg);
  3347. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3348. temp |= snb_b_fdi_train_param[i];
  3349. I915_WRITE(reg, temp);
  3350. POSTING_READ(reg);
  3351. udelay(500);
  3352. for (retry = 0; retry < 5; retry++) {
  3353. reg = FDI_RX_IIR(pipe);
  3354. temp = I915_READ(reg);
  3355. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3356. if (temp & FDI_RX_BIT_LOCK) {
  3357. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3358. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3359. break;
  3360. }
  3361. udelay(50);
  3362. }
  3363. if (retry < 5)
  3364. break;
  3365. }
  3366. if (i == 4)
  3367. DRM_ERROR("FDI train 1 fail!\n");
  3368. /* Train 2 */
  3369. reg = FDI_TX_CTL(pipe);
  3370. temp = I915_READ(reg);
  3371. temp &= ~FDI_LINK_TRAIN_NONE;
  3372. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3373. if (IS_GEN6(dev_priv)) {
  3374. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3375. /* SNB-B */
  3376. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3377. }
  3378. I915_WRITE(reg, temp);
  3379. reg = FDI_RX_CTL(pipe);
  3380. temp = I915_READ(reg);
  3381. if (HAS_PCH_CPT(dev_priv)) {
  3382. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3383. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3384. } else {
  3385. temp &= ~FDI_LINK_TRAIN_NONE;
  3386. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3387. }
  3388. I915_WRITE(reg, temp);
  3389. POSTING_READ(reg);
  3390. udelay(150);
  3391. for (i = 0; i < 4; i++) {
  3392. reg = FDI_TX_CTL(pipe);
  3393. temp = I915_READ(reg);
  3394. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3395. temp |= snb_b_fdi_train_param[i];
  3396. I915_WRITE(reg, temp);
  3397. POSTING_READ(reg);
  3398. udelay(500);
  3399. for (retry = 0; retry < 5; retry++) {
  3400. reg = FDI_RX_IIR(pipe);
  3401. temp = I915_READ(reg);
  3402. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3403. if (temp & FDI_RX_SYMBOL_LOCK) {
  3404. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3405. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3406. break;
  3407. }
  3408. udelay(50);
  3409. }
  3410. if (retry < 5)
  3411. break;
  3412. }
  3413. if (i == 4)
  3414. DRM_ERROR("FDI train 2 fail!\n");
  3415. DRM_DEBUG_KMS("FDI train done.\n");
  3416. }
  3417. /* Manual link training for Ivy Bridge A0 parts */
  3418. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3419. {
  3420. struct drm_device *dev = crtc->dev;
  3421. struct drm_i915_private *dev_priv = to_i915(dev);
  3422. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3423. int pipe = intel_crtc->pipe;
  3424. i915_reg_t reg;
  3425. u32 temp, i, j;
  3426. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3427. for train result */
  3428. reg = FDI_RX_IMR(pipe);
  3429. temp = I915_READ(reg);
  3430. temp &= ~FDI_RX_SYMBOL_LOCK;
  3431. temp &= ~FDI_RX_BIT_LOCK;
  3432. I915_WRITE(reg, temp);
  3433. POSTING_READ(reg);
  3434. udelay(150);
  3435. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3436. I915_READ(FDI_RX_IIR(pipe)));
  3437. /* Try each vswing and preemphasis setting twice before moving on */
  3438. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3439. /* disable first in case we need to retry */
  3440. reg = FDI_TX_CTL(pipe);
  3441. temp = I915_READ(reg);
  3442. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3443. temp &= ~FDI_TX_ENABLE;
  3444. I915_WRITE(reg, temp);
  3445. reg = FDI_RX_CTL(pipe);
  3446. temp = I915_READ(reg);
  3447. temp &= ~FDI_LINK_TRAIN_AUTO;
  3448. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3449. temp &= ~FDI_RX_ENABLE;
  3450. I915_WRITE(reg, temp);
  3451. /* enable CPU FDI TX and PCH FDI RX */
  3452. reg = FDI_TX_CTL(pipe);
  3453. temp = I915_READ(reg);
  3454. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3455. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3456. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3457. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3458. temp |= snb_b_fdi_train_param[j/2];
  3459. temp |= FDI_COMPOSITE_SYNC;
  3460. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3461. I915_WRITE(FDI_RX_MISC(pipe),
  3462. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3463. reg = FDI_RX_CTL(pipe);
  3464. temp = I915_READ(reg);
  3465. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3466. temp |= FDI_COMPOSITE_SYNC;
  3467. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3468. POSTING_READ(reg);
  3469. udelay(1); /* should be 0.5us */
  3470. for (i = 0; i < 4; i++) {
  3471. reg = FDI_RX_IIR(pipe);
  3472. temp = I915_READ(reg);
  3473. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3474. if (temp & FDI_RX_BIT_LOCK ||
  3475. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3476. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3477. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3478. i);
  3479. break;
  3480. }
  3481. udelay(1); /* should be 0.5us */
  3482. }
  3483. if (i == 4) {
  3484. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3485. continue;
  3486. }
  3487. /* Train 2 */
  3488. reg = FDI_TX_CTL(pipe);
  3489. temp = I915_READ(reg);
  3490. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3491. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3492. I915_WRITE(reg, temp);
  3493. reg = FDI_RX_CTL(pipe);
  3494. temp = I915_READ(reg);
  3495. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3496. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3497. I915_WRITE(reg, temp);
  3498. POSTING_READ(reg);
  3499. udelay(2); /* should be 1.5us */
  3500. for (i = 0; i < 4; i++) {
  3501. reg = FDI_RX_IIR(pipe);
  3502. temp = I915_READ(reg);
  3503. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3504. if (temp & FDI_RX_SYMBOL_LOCK ||
  3505. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3506. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3507. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3508. i);
  3509. goto train_done;
  3510. }
  3511. udelay(2); /* should be 1.5us */
  3512. }
  3513. if (i == 4)
  3514. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3515. }
  3516. train_done:
  3517. DRM_DEBUG_KMS("FDI train done.\n");
  3518. }
  3519. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3520. {
  3521. struct drm_device *dev = intel_crtc->base.dev;
  3522. struct drm_i915_private *dev_priv = to_i915(dev);
  3523. int pipe = intel_crtc->pipe;
  3524. i915_reg_t reg;
  3525. u32 temp;
  3526. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3527. reg = FDI_RX_CTL(pipe);
  3528. temp = I915_READ(reg);
  3529. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3530. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3531. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3532. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3533. POSTING_READ(reg);
  3534. udelay(200);
  3535. /* Switch from Rawclk to PCDclk */
  3536. temp = I915_READ(reg);
  3537. I915_WRITE(reg, temp | FDI_PCDCLK);
  3538. POSTING_READ(reg);
  3539. udelay(200);
  3540. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3541. reg = FDI_TX_CTL(pipe);
  3542. temp = I915_READ(reg);
  3543. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3544. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3545. POSTING_READ(reg);
  3546. udelay(100);
  3547. }
  3548. }
  3549. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3550. {
  3551. struct drm_device *dev = intel_crtc->base.dev;
  3552. struct drm_i915_private *dev_priv = to_i915(dev);
  3553. int pipe = intel_crtc->pipe;
  3554. i915_reg_t reg;
  3555. u32 temp;
  3556. /* Switch from PCDclk to Rawclk */
  3557. reg = FDI_RX_CTL(pipe);
  3558. temp = I915_READ(reg);
  3559. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3560. /* Disable CPU FDI TX PLL */
  3561. reg = FDI_TX_CTL(pipe);
  3562. temp = I915_READ(reg);
  3563. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3564. POSTING_READ(reg);
  3565. udelay(100);
  3566. reg = FDI_RX_CTL(pipe);
  3567. temp = I915_READ(reg);
  3568. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3569. /* Wait for the clocks to turn off. */
  3570. POSTING_READ(reg);
  3571. udelay(100);
  3572. }
  3573. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3574. {
  3575. struct drm_device *dev = crtc->dev;
  3576. struct drm_i915_private *dev_priv = to_i915(dev);
  3577. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3578. int pipe = intel_crtc->pipe;
  3579. i915_reg_t reg;
  3580. u32 temp;
  3581. /* disable CPU FDI tx and PCH FDI rx */
  3582. reg = FDI_TX_CTL(pipe);
  3583. temp = I915_READ(reg);
  3584. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3585. POSTING_READ(reg);
  3586. reg = FDI_RX_CTL(pipe);
  3587. temp = I915_READ(reg);
  3588. temp &= ~(0x7 << 16);
  3589. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3590. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3591. POSTING_READ(reg);
  3592. udelay(100);
  3593. /* Ironlake workaround, disable clock pointer after downing FDI */
  3594. if (HAS_PCH_IBX(dev_priv))
  3595. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3596. /* still set train pattern 1 */
  3597. reg = FDI_TX_CTL(pipe);
  3598. temp = I915_READ(reg);
  3599. temp &= ~FDI_LINK_TRAIN_NONE;
  3600. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3601. I915_WRITE(reg, temp);
  3602. reg = FDI_RX_CTL(pipe);
  3603. temp = I915_READ(reg);
  3604. if (HAS_PCH_CPT(dev_priv)) {
  3605. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3606. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3607. } else {
  3608. temp &= ~FDI_LINK_TRAIN_NONE;
  3609. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3610. }
  3611. /* BPC in FDI rx is consistent with that in PIPECONF */
  3612. temp &= ~(0x07 << 16);
  3613. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3614. I915_WRITE(reg, temp);
  3615. POSTING_READ(reg);
  3616. udelay(100);
  3617. }
  3618. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3619. {
  3620. struct intel_crtc *crtc;
  3621. /* Note that we don't need to be called with mode_config.lock here
  3622. * as our list of CRTC objects is static for the lifetime of the
  3623. * device and so cannot disappear as we iterate. Similarly, we can
  3624. * happily treat the predicates as racy, atomic checks as userspace
  3625. * cannot claim and pin a new fb without at least acquring the
  3626. * struct_mutex and so serialising with us.
  3627. */
  3628. for_each_intel_crtc(&dev_priv->drm, crtc) {
  3629. if (atomic_read(&crtc->unpin_work_count) == 0)
  3630. continue;
  3631. if (crtc->flip_work)
  3632. intel_wait_for_vblank(dev_priv, crtc->pipe);
  3633. return true;
  3634. }
  3635. return false;
  3636. }
  3637. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3638. {
  3639. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3640. struct intel_flip_work *work = intel_crtc->flip_work;
  3641. intel_crtc->flip_work = NULL;
  3642. if (work->event)
  3643. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3644. drm_crtc_vblank_put(&intel_crtc->base);
  3645. wake_up_all(&dev_priv->pending_flip_queue);
  3646. queue_work(dev_priv->wq, &work->unpin_work);
  3647. trace_i915_flip_complete(intel_crtc->plane,
  3648. work->pending_flip_obj);
  3649. }
  3650. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3651. {
  3652. struct drm_device *dev = crtc->dev;
  3653. struct drm_i915_private *dev_priv = to_i915(dev);
  3654. long ret;
  3655. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3656. ret = wait_event_interruptible_timeout(
  3657. dev_priv->pending_flip_queue,
  3658. !intel_crtc_has_pending_flip(crtc),
  3659. 60*HZ);
  3660. if (ret < 0)
  3661. return ret;
  3662. if (ret == 0) {
  3663. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3664. struct intel_flip_work *work;
  3665. spin_lock_irq(&dev->event_lock);
  3666. work = intel_crtc->flip_work;
  3667. if (work && !is_mmio_work(work)) {
  3668. WARN_ONCE(1, "Removing stuck page flip\n");
  3669. page_flip_completed(intel_crtc);
  3670. }
  3671. spin_unlock_irq(&dev->event_lock);
  3672. }
  3673. return 0;
  3674. }
  3675. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3676. {
  3677. u32 temp;
  3678. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3679. mutex_lock(&dev_priv->sb_lock);
  3680. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3681. temp |= SBI_SSCCTL_DISABLE;
  3682. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3683. mutex_unlock(&dev_priv->sb_lock);
  3684. }
  3685. /* Program iCLKIP clock to the desired frequency */
  3686. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3687. {
  3688. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3689. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3690. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3691. u32 temp;
  3692. lpt_disable_iclkip(dev_priv);
  3693. /* The iCLK virtual clock root frequency is in MHz,
  3694. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3695. * divisors, it is necessary to divide one by another, so we
  3696. * convert the virtual clock precision to KHz here for higher
  3697. * precision.
  3698. */
  3699. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3700. u32 iclk_virtual_root_freq = 172800 * 1000;
  3701. u32 iclk_pi_range = 64;
  3702. u32 desired_divisor;
  3703. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3704. clock << auxdiv);
  3705. divsel = (desired_divisor / iclk_pi_range) - 2;
  3706. phaseinc = desired_divisor % iclk_pi_range;
  3707. /*
  3708. * Near 20MHz is a corner case which is
  3709. * out of range for the 7-bit divisor
  3710. */
  3711. if (divsel <= 0x7f)
  3712. break;
  3713. }
  3714. /* This should not happen with any sane values */
  3715. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3716. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3717. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3718. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3719. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3720. clock,
  3721. auxdiv,
  3722. divsel,
  3723. phasedir,
  3724. phaseinc);
  3725. mutex_lock(&dev_priv->sb_lock);
  3726. /* Program SSCDIVINTPHASE6 */
  3727. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3728. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3729. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3730. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3731. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3732. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3733. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3734. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3735. /* Program SSCAUXDIV */
  3736. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3737. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3738. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3739. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3740. /* Enable modulator and associated divider */
  3741. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3742. temp &= ~SBI_SSCCTL_DISABLE;
  3743. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3744. mutex_unlock(&dev_priv->sb_lock);
  3745. /* Wait for initialization time */
  3746. udelay(24);
  3747. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3748. }
  3749. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3750. {
  3751. u32 divsel, phaseinc, auxdiv;
  3752. u32 iclk_virtual_root_freq = 172800 * 1000;
  3753. u32 iclk_pi_range = 64;
  3754. u32 desired_divisor;
  3755. u32 temp;
  3756. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3757. return 0;
  3758. mutex_lock(&dev_priv->sb_lock);
  3759. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3760. if (temp & SBI_SSCCTL_DISABLE) {
  3761. mutex_unlock(&dev_priv->sb_lock);
  3762. return 0;
  3763. }
  3764. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3765. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3766. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3767. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3768. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3769. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3770. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3771. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3772. mutex_unlock(&dev_priv->sb_lock);
  3773. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3774. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3775. desired_divisor << auxdiv);
  3776. }
  3777. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3778. enum pipe pch_transcoder)
  3779. {
  3780. struct drm_device *dev = crtc->base.dev;
  3781. struct drm_i915_private *dev_priv = to_i915(dev);
  3782. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3783. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3784. I915_READ(HTOTAL(cpu_transcoder)));
  3785. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3786. I915_READ(HBLANK(cpu_transcoder)));
  3787. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3788. I915_READ(HSYNC(cpu_transcoder)));
  3789. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3790. I915_READ(VTOTAL(cpu_transcoder)));
  3791. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3792. I915_READ(VBLANK(cpu_transcoder)));
  3793. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3794. I915_READ(VSYNC(cpu_transcoder)));
  3795. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3796. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3797. }
  3798. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3799. {
  3800. struct drm_i915_private *dev_priv = to_i915(dev);
  3801. uint32_t temp;
  3802. temp = I915_READ(SOUTH_CHICKEN1);
  3803. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3804. return;
  3805. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3806. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3807. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3808. if (enable)
  3809. temp |= FDI_BC_BIFURCATION_SELECT;
  3810. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3811. I915_WRITE(SOUTH_CHICKEN1, temp);
  3812. POSTING_READ(SOUTH_CHICKEN1);
  3813. }
  3814. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3815. {
  3816. struct drm_device *dev = intel_crtc->base.dev;
  3817. switch (intel_crtc->pipe) {
  3818. case PIPE_A:
  3819. break;
  3820. case PIPE_B:
  3821. if (intel_crtc->config->fdi_lanes > 2)
  3822. cpt_set_fdi_bc_bifurcation(dev, false);
  3823. else
  3824. cpt_set_fdi_bc_bifurcation(dev, true);
  3825. break;
  3826. case PIPE_C:
  3827. cpt_set_fdi_bc_bifurcation(dev, true);
  3828. break;
  3829. default:
  3830. BUG();
  3831. }
  3832. }
  3833. /* Return which DP Port should be selected for Transcoder DP control */
  3834. static enum port
  3835. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3836. {
  3837. struct drm_device *dev = crtc->dev;
  3838. struct intel_encoder *encoder;
  3839. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3840. if (encoder->type == INTEL_OUTPUT_DP ||
  3841. encoder->type == INTEL_OUTPUT_EDP)
  3842. return enc_to_dig_port(&encoder->base)->port;
  3843. }
  3844. return -1;
  3845. }
  3846. /*
  3847. * Enable PCH resources required for PCH ports:
  3848. * - PCH PLLs
  3849. * - FDI training & RX/TX
  3850. * - update transcoder timings
  3851. * - DP transcoding bits
  3852. * - transcoder
  3853. */
  3854. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3855. {
  3856. struct drm_device *dev = crtc->dev;
  3857. struct drm_i915_private *dev_priv = to_i915(dev);
  3858. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3859. int pipe = intel_crtc->pipe;
  3860. u32 temp;
  3861. assert_pch_transcoder_disabled(dev_priv, pipe);
  3862. if (IS_IVYBRIDGE(dev_priv))
  3863. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3864. /* Write the TU size bits before fdi link training, so that error
  3865. * detection works. */
  3866. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3867. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3868. /* For PCH output, training FDI link */
  3869. dev_priv->display.fdi_link_train(crtc);
  3870. /* We need to program the right clock selection before writing the pixel
  3871. * mutliplier into the DPLL. */
  3872. if (HAS_PCH_CPT(dev_priv)) {
  3873. u32 sel;
  3874. temp = I915_READ(PCH_DPLL_SEL);
  3875. temp |= TRANS_DPLL_ENABLE(pipe);
  3876. sel = TRANS_DPLLB_SEL(pipe);
  3877. if (intel_crtc->config->shared_dpll ==
  3878. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3879. temp |= sel;
  3880. else
  3881. temp &= ~sel;
  3882. I915_WRITE(PCH_DPLL_SEL, temp);
  3883. }
  3884. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3885. * transcoder, and we actually should do this to not upset any PCH
  3886. * transcoder that already use the clock when we share it.
  3887. *
  3888. * Note that enable_shared_dpll tries to do the right thing, but
  3889. * get_shared_dpll unconditionally resets the pll - we need that to have
  3890. * the right LVDS enable sequence. */
  3891. intel_enable_shared_dpll(intel_crtc);
  3892. /* set transcoder timing, panel must allow it */
  3893. assert_panel_unlocked(dev_priv, pipe);
  3894. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3895. intel_fdi_normal_train(crtc);
  3896. /* For PCH DP, enable TRANS_DP_CTL */
  3897. if (HAS_PCH_CPT(dev_priv) &&
  3898. intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3899. const struct drm_display_mode *adjusted_mode =
  3900. &intel_crtc->config->base.adjusted_mode;
  3901. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3902. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3903. temp = I915_READ(reg);
  3904. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3905. TRANS_DP_SYNC_MASK |
  3906. TRANS_DP_BPC_MASK);
  3907. temp |= TRANS_DP_OUTPUT_ENABLE;
  3908. temp |= bpc << 9; /* same format but at 11:9 */
  3909. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3910. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3911. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3912. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3913. switch (intel_trans_dp_port_sel(crtc)) {
  3914. case PORT_B:
  3915. temp |= TRANS_DP_PORT_SEL_B;
  3916. break;
  3917. case PORT_C:
  3918. temp |= TRANS_DP_PORT_SEL_C;
  3919. break;
  3920. case PORT_D:
  3921. temp |= TRANS_DP_PORT_SEL_D;
  3922. break;
  3923. default:
  3924. BUG();
  3925. }
  3926. I915_WRITE(reg, temp);
  3927. }
  3928. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3929. }
  3930. static void lpt_pch_enable(struct drm_crtc *crtc)
  3931. {
  3932. struct drm_device *dev = crtc->dev;
  3933. struct drm_i915_private *dev_priv = to_i915(dev);
  3934. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3935. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3936. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3937. lpt_program_iclkip(crtc);
  3938. /* Set transcoder timing. */
  3939. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3940. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3941. }
  3942. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3943. {
  3944. struct drm_i915_private *dev_priv = to_i915(dev);
  3945. i915_reg_t dslreg = PIPEDSL(pipe);
  3946. u32 temp;
  3947. temp = I915_READ(dslreg);
  3948. udelay(500);
  3949. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3950. if (wait_for(I915_READ(dslreg) != temp, 5))
  3951. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3952. }
  3953. }
  3954. static int
  3955. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3956. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3957. int src_w, int src_h, int dst_w, int dst_h)
  3958. {
  3959. struct intel_crtc_scaler_state *scaler_state =
  3960. &crtc_state->scaler_state;
  3961. struct intel_crtc *intel_crtc =
  3962. to_intel_crtc(crtc_state->base.crtc);
  3963. int need_scaling;
  3964. need_scaling = drm_rotation_90_or_270(rotation) ?
  3965. (src_h != dst_w || src_w != dst_h):
  3966. (src_w != dst_w || src_h != dst_h);
  3967. /*
  3968. * if plane is being disabled or scaler is no more required or force detach
  3969. * - free scaler binded to this plane/crtc
  3970. * - in order to do this, update crtc->scaler_usage
  3971. *
  3972. * Here scaler state in crtc_state is set free so that
  3973. * scaler can be assigned to other user. Actual register
  3974. * update to free the scaler is done in plane/panel-fit programming.
  3975. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3976. */
  3977. if (force_detach || !need_scaling) {
  3978. if (*scaler_id >= 0) {
  3979. scaler_state->scaler_users &= ~(1 << scaler_user);
  3980. scaler_state->scalers[*scaler_id].in_use = 0;
  3981. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3982. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3983. intel_crtc->pipe, scaler_user, *scaler_id,
  3984. scaler_state->scaler_users);
  3985. *scaler_id = -1;
  3986. }
  3987. return 0;
  3988. }
  3989. /* range checks */
  3990. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3991. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3992. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3993. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3994. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3995. "size is out of scaler range\n",
  3996. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3997. return -EINVAL;
  3998. }
  3999. /* mark this plane as a scaler user in crtc_state */
  4000. scaler_state->scaler_users |= (1 << scaler_user);
  4001. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4002. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4003. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4004. scaler_state->scaler_users);
  4005. return 0;
  4006. }
  4007. /**
  4008. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4009. *
  4010. * @state: crtc's scaler state
  4011. *
  4012. * Return
  4013. * 0 - scaler_usage updated successfully
  4014. * error - requested scaling cannot be supported or other error condition
  4015. */
  4016. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4017. {
  4018. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4019. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4020. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  4021. state->pipe_src_w, state->pipe_src_h,
  4022. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4023. }
  4024. /**
  4025. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4026. *
  4027. * @state: crtc's scaler state
  4028. * @plane_state: atomic plane state to update
  4029. *
  4030. * Return
  4031. * 0 - scaler_usage updated successfully
  4032. * error - requested scaling cannot be supported or other error condition
  4033. */
  4034. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4035. struct intel_plane_state *plane_state)
  4036. {
  4037. struct intel_plane *intel_plane =
  4038. to_intel_plane(plane_state->base.plane);
  4039. struct drm_framebuffer *fb = plane_state->base.fb;
  4040. int ret;
  4041. bool force_detach = !fb || !plane_state->base.visible;
  4042. ret = skl_update_scaler(crtc_state, force_detach,
  4043. drm_plane_index(&intel_plane->base),
  4044. &plane_state->scaler_id,
  4045. plane_state->base.rotation,
  4046. drm_rect_width(&plane_state->base.src) >> 16,
  4047. drm_rect_height(&plane_state->base.src) >> 16,
  4048. drm_rect_width(&plane_state->base.dst),
  4049. drm_rect_height(&plane_state->base.dst));
  4050. if (ret || plane_state->scaler_id < 0)
  4051. return ret;
  4052. /* check colorkey */
  4053. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4054. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4055. intel_plane->base.base.id,
  4056. intel_plane->base.name);
  4057. return -EINVAL;
  4058. }
  4059. /* Check src format */
  4060. switch (fb->format->format) {
  4061. case DRM_FORMAT_RGB565:
  4062. case DRM_FORMAT_XBGR8888:
  4063. case DRM_FORMAT_XRGB8888:
  4064. case DRM_FORMAT_ABGR8888:
  4065. case DRM_FORMAT_ARGB8888:
  4066. case DRM_FORMAT_XRGB2101010:
  4067. case DRM_FORMAT_XBGR2101010:
  4068. case DRM_FORMAT_YUYV:
  4069. case DRM_FORMAT_YVYU:
  4070. case DRM_FORMAT_UYVY:
  4071. case DRM_FORMAT_VYUY:
  4072. break;
  4073. default:
  4074. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4075. intel_plane->base.base.id, intel_plane->base.name,
  4076. fb->base.id, fb->format->format);
  4077. return -EINVAL;
  4078. }
  4079. return 0;
  4080. }
  4081. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4082. {
  4083. int i;
  4084. for (i = 0; i < crtc->num_scalers; i++)
  4085. skl_detach_scaler(crtc, i);
  4086. }
  4087. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4088. {
  4089. struct drm_device *dev = crtc->base.dev;
  4090. struct drm_i915_private *dev_priv = to_i915(dev);
  4091. int pipe = crtc->pipe;
  4092. struct intel_crtc_scaler_state *scaler_state =
  4093. &crtc->config->scaler_state;
  4094. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  4095. if (crtc->config->pch_pfit.enabled) {
  4096. int id;
  4097. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  4098. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  4099. return;
  4100. }
  4101. id = scaler_state->scaler_id;
  4102. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4103. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4104. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4105. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4106. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  4107. }
  4108. }
  4109. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4110. {
  4111. struct drm_device *dev = crtc->base.dev;
  4112. struct drm_i915_private *dev_priv = to_i915(dev);
  4113. int pipe = crtc->pipe;
  4114. if (crtc->config->pch_pfit.enabled) {
  4115. /* Force use of hard-coded filter coefficients
  4116. * as some pre-programmed values are broken,
  4117. * e.g. x201.
  4118. */
  4119. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4120. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4121. PF_PIPE_SEL_IVB(pipe));
  4122. else
  4123. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4124. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4125. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4126. }
  4127. }
  4128. void hsw_enable_ips(struct intel_crtc *crtc)
  4129. {
  4130. struct drm_device *dev = crtc->base.dev;
  4131. struct drm_i915_private *dev_priv = to_i915(dev);
  4132. if (!crtc->config->ips_enabled)
  4133. return;
  4134. /*
  4135. * We can only enable IPS after we enable a plane and wait for a vblank
  4136. * This function is called from post_plane_update, which is run after
  4137. * a vblank wait.
  4138. */
  4139. assert_plane_enabled(dev_priv, crtc->plane);
  4140. if (IS_BROADWELL(dev_priv)) {
  4141. mutex_lock(&dev_priv->rps.hw_lock);
  4142. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4143. mutex_unlock(&dev_priv->rps.hw_lock);
  4144. /* Quoting Art Runyan: "its not safe to expect any particular
  4145. * value in IPS_CTL bit 31 after enabling IPS through the
  4146. * mailbox." Moreover, the mailbox may return a bogus state,
  4147. * so we need to just enable it and continue on.
  4148. */
  4149. } else {
  4150. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4151. /* The bit only becomes 1 in the next vblank, so this wait here
  4152. * is essentially intel_wait_for_vblank. If we don't have this
  4153. * and don't wait for vblanks until the end of crtc_enable, then
  4154. * the HW state readout code will complain that the expected
  4155. * IPS_CTL value is not the one we read. */
  4156. if (intel_wait_for_register(dev_priv,
  4157. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4158. 50))
  4159. DRM_ERROR("Timed out waiting for IPS enable\n");
  4160. }
  4161. }
  4162. void hsw_disable_ips(struct intel_crtc *crtc)
  4163. {
  4164. struct drm_device *dev = crtc->base.dev;
  4165. struct drm_i915_private *dev_priv = to_i915(dev);
  4166. if (!crtc->config->ips_enabled)
  4167. return;
  4168. assert_plane_enabled(dev_priv, crtc->plane);
  4169. if (IS_BROADWELL(dev_priv)) {
  4170. mutex_lock(&dev_priv->rps.hw_lock);
  4171. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4172. mutex_unlock(&dev_priv->rps.hw_lock);
  4173. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4174. if (intel_wait_for_register(dev_priv,
  4175. IPS_CTL, IPS_ENABLE, 0,
  4176. 42))
  4177. DRM_ERROR("Timed out waiting for IPS disable\n");
  4178. } else {
  4179. I915_WRITE(IPS_CTL, 0);
  4180. POSTING_READ(IPS_CTL);
  4181. }
  4182. /* We need to wait for a vblank before we can disable the plane. */
  4183. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4184. }
  4185. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4186. {
  4187. if (intel_crtc->overlay) {
  4188. struct drm_device *dev = intel_crtc->base.dev;
  4189. struct drm_i915_private *dev_priv = to_i915(dev);
  4190. mutex_lock(&dev->struct_mutex);
  4191. dev_priv->mm.interruptible = false;
  4192. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4193. dev_priv->mm.interruptible = true;
  4194. mutex_unlock(&dev->struct_mutex);
  4195. }
  4196. /* Let userspace switch the overlay on again. In most cases userspace
  4197. * has to recompute where to put it anyway.
  4198. */
  4199. }
  4200. /**
  4201. * intel_post_enable_primary - Perform operations after enabling primary plane
  4202. * @crtc: the CRTC whose primary plane was just enabled
  4203. *
  4204. * Performs potentially sleeping operations that must be done after the primary
  4205. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4206. * called due to an explicit primary plane update, or due to an implicit
  4207. * re-enable that is caused when a sprite plane is updated to no longer
  4208. * completely hide the primary plane.
  4209. */
  4210. static void
  4211. intel_post_enable_primary(struct drm_crtc *crtc)
  4212. {
  4213. struct drm_device *dev = crtc->dev;
  4214. struct drm_i915_private *dev_priv = to_i915(dev);
  4215. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4216. int pipe = intel_crtc->pipe;
  4217. /*
  4218. * FIXME IPS should be fine as long as one plane is
  4219. * enabled, but in practice it seems to have problems
  4220. * when going from primary only to sprite only and vice
  4221. * versa.
  4222. */
  4223. hsw_enable_ips(intel_crtc);
  4224. /*
  4225. * Gen2 reports pipe underruns whenever all planes are disabled.
  4226. * So don't enable underrun reporting before at least some planes
  4227. * are enabled.
  4228. * FIXME: Need to fix the logic to work when we turn off all planes
  4229. * but leave the pipe running.
  4230. */
  4231. if (IS_GEN2(dev_priv))
  4232. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4233. /* Underruns don't always raise interrupts, so check manually. */
  4234. intel_check_cpu_fifo_underruns(dev_priv);
  4235. intel_check_pch_fifo_underruns(dev_priv);
  4236. }
  4237. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4238. static void
  4239. intel_pre_disable_primary(struct drm_crtc *crtc)
  4240. {
  4241. struct drm_device *dev = crtc->dev;
  4242. struct drm_i915_private *dev_priv = to_i915(dev);
  4243. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4244. int pipe = intel_crtc->pipe;
  4245. /*
  4246. * Gen2 reports pipe underruns whenever all planes are disabled.
  4247. * So diasble underrun reporting before all the planes get disabled.
  4248. * FIXME: Need to fix the logic to work when we turn off all planes
  4249. * but leave the pipe running.
  4250. */
  4251. if (IS_GEN2(dev_priv))
  4252. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4253. /*
  4254. * FIXME IPS should be fine as long as one plane is
  4255. * enabled, but in practice it seems to have problems
  4256. * when going from primary only to sprite only and vice
  4257. * versa.
  4258. */
  4259. hsw_disable_ips(intel_crtc);
  4260. }
  4261. /* FIXME get rid of this and use pre_plane_update */
  4262. static void
  4263. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4264. {
  4265. struct drm_device *dev = crtc->dev;
  4266. struct drm_i915_private *dev_priv = to_i915(dev);
  4267. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4268. int pipe = intel_crtc->pipe;
  4269. intel_pre_disable_primary(crtc);
  4270. /*
  4271. * Vblank time updates from the shadow to live plane control register
  4272. * are blocked if the memory self-refresh mode is active at that
  4273. * moment. So to make sure the plane gets truly disabled, disable
  4274. * first the self-refresh mode. The self-refresh enable bit in turn
  4275. * will be checked/applied by the HW only at the next frame start
  4276. * event which is after the vblank start event, so we need to have a
  4277. * wait-for-vblank between disabling the plane and the pipe.
  4278. */
  4279. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4280. intel_set_memory_cxsr(dev_priv, false))
  4281. intel_wait_for_vblank(dev_priv, pipe);
  4282. }
  4283. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4284. {
  4285. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4286. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4287. struct intel_crtc_state *pipe_config =
  4288. to_intel_crtc_state(crtc->base.state);
  4289. struct drm_plane *primary = crtc->base.primary;
  4290. struct drm_plane_state *old_pri_state =
  4291. drm_atomic_get_existing_plane_state(old_state, primary);
  4292. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4293. crtc->wm.cxsr_allowed = true;
  4294. if (pipe_config->update_wm_post && pipe_config->base.active)
  4295. intel_update_watermarks(crtc);
  4296. if (old_pri_state) {
  4297. struct intel_plane_state *primary_state =
  4298. to_intel_plane_state(primary->state);
  4299. struct intel_plane_state *old_primary_state =
  4300. to_intel_plane_state(old_pri_state);
  4301. intel_fbc_post_update(crtc);
  4302. if (primary_state->base.visible &&
  4303. (needs_modeset(&pipe_config->base) ||
  4304. !old_primary_state->base.visible))
  4305. intel_post_enable_primary(&crtc->base);
  4306. }
  4307. }
  4308. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  4309. {
  4310. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4311. struct drm_device *dev = crtc->base.dev;
  4312. struct drm_i915_private *dev_priv = to_i915(dev);
  4313. struct intel_crtc_state *pipe_config =
  4314. to_intel_crtc_state(crtc->base.state);
  4315. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4316. struct drm_plane *primary = crtc->base.primary;
  4317. struct drm_plane_state *old_pri_state =
  4318. drm_atomic_get_existing_plane_state(old_state, primary);
  4319. bool modeset = needs_modeset(&pipe_config->base);
  4320. struct intel_atomic_state *old_intel_state =
  4321. to_intel_atomic_state(old_state);
  4322. if (old_pri_state) {
  4323. struct intel_plane_state *primary_state =
  4324. to_intel_plane_state(primary->state);
  4325. struct intel_plane_state *old_primary_state =
  4326. to_intel_plane_state(old_pri_state);
  4327. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4328. if (old_primary_state->base.visible &&
  4329. (modeset || !primary_state->base.visible))
  4330. intel_pre_disable_primary(&crtc->base);
  4331. }
  4332. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
  4333. crtc->wm.cxsr_allowed = false;
  4334. /*
  4335. * Vblank time updates from the shadow to live plane control register
  4336. * are blocked if the memory self-refresh mode is active at that
  4337. * moment. So to make sure the plane gets truly disabled, disable
  4338. * first the self-refresh mode. The self-refresh enable bit in turn
  4339. * will be checked/applied by the HW only at the next frame start
  4340. * event which is after the vblank start event, so we need to have a
  4341. * wait-for-vblank between disabling the plane and the pipe.
  4342. */
  4343. if (old_crtc_state->base.active &&
  4344. intel_set_memory_cxsr(dev_priv, false))
  4345. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4346. }
  4347. /*
  4348. * IVB workaround: must disable low power watermarks for at least
  4349. * one frame before enabling scaling. LP watermarks can be re-enabled
  4350. * when scaling is disabled.
  4351. *
  4352. * WaCxSRDisabledForSpriteScaling:ivb
  4353. */
  4354. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4355. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4356. /*
  4357. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4358. * watermark programming here.
  4359. */
  4360. if (needs_modeset(&pipe_config->base))
  4361. return;
  4362. /*
  4363. * For platforms that support atomic watermarks, program the
  4364. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4365. * will be the intermediate values that are safe for both pre- and
  4366. * post- vblank; when vblank happens, the 'active' values will be set
  4367. * to the final 'target' values and we'll do this again to get the
  4368. * optimal watermarks. For gen9+ platforms, the values we program here
  4369. * will be the final target values which will get automatically latched
  4370. * at vblank time; no further programming will be necessary.
  4371. *
  4372. * If a platform hasn't been transitioned to atomic watermarks yet,
  4373. * we'll continue to update watermarks the old way, if flags tell
  4374. * us to.
  4375. */
  4376. if (dev_priv->display.initial_watermarks != NULL)
  4377. dev_priv->display.initial_watermarks(old_intel_state,
  4378. pipe_config);
  4379. else if (pipe_config->update_wm_pre)
  4380. intel_update_watermarks(crtc);
  4381. }
  4382. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4383. {
  4384. struct drm_device *dev = crtc->dev;
  4385. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4386. struct drm_plane *p;
  4387. int pipe = intel_crtc->pipe;
  4388. intel_crtc_dpms_overlay_disable(intel_crtc);
  4389. drm_for_each_plane_mask(p, dev, plane_mask)
  4390. to_intel_plane(p)->disable_plane(p, crtc);
  4391. /*
  4392. * FIXME: Once we grow proper nuclear flip support out of this we need
  4393. * to compute the mask of flip planes precisely. For the time being
  4394. * consider this a flip to a NULL plane.
  4395. */
  4396. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4397. }
  4398. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4399. struct intel_crtc_state *crtc_state,
  4400. struct drm_atomic_state *old_state)
  4401. {
  4402. struct drm_connector_state *old_conn_state;
  4403. struct drm_connector *conn;
  4404. int i;
  4405. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4406. struct drm_connector_state *conn_state = conn->state;
  4407. struct intel_encoder *encoder =
  4408. to_intel_encoder(conn_state->best_encoder);
  4409. if (conn_state->crtc != crtc)
  4410. continue;
  4411. if (encoder->pre_pll_enable)
  4412. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4413. }
  4414. }
  4415. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4416. struct intel_crtc_state *crtc_state,
  4417. struct drm_atomic_state *old_state)
  4418. {
  4419. struct drm_connector_state *old_conn_state;
  4420. struct drm_connector *conn;
  4421. int i;
  4422. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4423. struct drm_connector_state *conn_state = conn->state;
  4424. struct intel_encoder *encoder =
  4425. to_intel_encoder(conn_state->best_encoder);
  4426. if (conn_state->crtc != crtc)
  4427. continue;
  4428. if (encoder->pre_enable)
  4429. encoder->pre_enable(encoder, crtc_state, conn_state);
  4430. }
  4431. }
  4432. static void intel_encoders_enable(struct drm_crtc *crtc,
  4433. struct intel_crtc_state *crtc_state,
  4434. struct drm_atomic_state *old_state)
  4435. {
  4436. struct drm_connector_state *old_conn_state;
  4437. struct drm_connector *conn;
  4438. int i;
  4439. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4440. struct drm_connector_state *conn_state = conn->state;
  4441. struct intel_encoder *encoder =
  4442. to_intel_encoder(conn_state->best_encoder);
  4443. if (conn_state->crtc != crtc)
  4444. continue;
  4445. encoder->enable(encoder, crtc_state, conn_state);
  4446. intel_opregion_notify_encoder(encoder, true);
  4447. }
  4448. }
  4449. static void intel_encoders_disable(struct drm_crtc *crtc,
  4450. struct intel_crtc_state *old_crtc_state,
  4451. struct drm_atomic_state *old_state)
  4452. {
  4453. struct drm_connector_state *old_conn_state;
  4454. struct drm_connector *conn;
  4455. int i;
  4456. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4457. struct intel_encoder *encoder =
  4458. to_intel_encoder(old_conn_state->best_encoder);
  4459. if (old_conn_state->crtc != crtc)
  4460. continue;
  4461. intel_opregion_notify_encoder(encoder, false);
  4462. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4463. }
  4464. }
  4465. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4466. struct intel_crtc_state *old_crtc_state,
  4467. struct drm_atomic_state *old_state)
  4468. {
  4469. struct drm_connector_state *old_conn_state;
  4470. struct drm_connector *conn;
  4471. int i;
  4472. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4473. struct intel_encoder *encoder =
  4474. to_intel_encoder(old_conn_state->best_encoder);
  4475. if (old_conn_state->crtc != crtc)
  4476. continue;
  4477. if (encoder->post_disable)
  4478. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4479. }
  4480. }
  4481. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4482. struct intel_crtc_state *old_crtc_state,
  4483. struct drm_atomic_state *old_state)
  4484. {
  4485. struct drm_connector_state *old_conn_state;
  4486. struct drm_connector *conn;
  4487. int i;
  4488. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4489. struct intel_encoder *encoder =
  4490. to_intel_encoder(old_conn_state->best_encoder);
  4491. if (old_conn_state->crtc != crtc)
  4492. continue;
  4493. if (encoder->post_pll_disable)
  4494. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4495. }
  4496. }
  4497. static void ironlake_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_device *dev = crtc->dev;
  4502. struct drm_i915_private *dev_priv = to_i915(dev);
  4503. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4504. int pipe = intel_crtc->pipe;
  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. /*
  4510. * Sometimes spurious CPU pipe underruns happen during FDI
  4511. * training, at least with VGA+HDMI cloning. Suppress them.
  4512. *
  4513. * On ILK we get an occasional spurious CPU pipe underruns
  4514. * between eDP port A enable and vdd enable. Also PCH port
  4515. * enable seems to result in the occasional CPU pipe underrun.
  4516. *
  4517. * Spurious PCH underruns also occur during PCH enabling.
  4518. */
  4519. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4520. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4521. if (intel_crtc->config->has_pch_encoder)
  4522. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4523. if (intel_crtc->config->has_pch_encoder)
  4524. intel_prepare_shared_dpll(intel_crtc);
  4525. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4526. intel_dp_set_m_n(intel_crtc, M1_N1);
  4527. intel_set_pipe_timings(intel_crtc);
  4528. intel_set_pipe_src_size(intel_crtc);
  4529. if (intel_crtc->config->has_pch_encoder) {
  4530. intel_cpu_transcoder_set_m_n(intel_crtc,
  4531. &intel_crtc->config->fdi_m_n, NULL);
  4532. }
  4533. ironlake_set_pipeconf(crtc);
  4534. intel_crtc->active = true;
  4535. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4536. if (intel_crtc->config->has_pch_encoder) {
  4537. /* Note: FDI PLL enabling _must_ be done before we enable the
  4538. * cpu pipes, hence this is separate from all the other fdi/pch
  4539. * enabling. */
  4540. ironlake_fdi_pll_enable(intel_crtc);
  4541. } else {
  4542. assert_fdi_tx_disabled(dev_priv, pipe);
  4543. assert_fdi_rx_disabled(dev_priv, pipe);
  4544. }
  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. if (dev_priv->display.initial_watermarks != NULL)
  4552. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4553. intel_enable_pipe(intel_crtc);
  4554. if (intel_crtc->config->has_pch_encoder)
  4555. ironlake_pch_enable(crtc);
  4556. assert_vblank_disabled(crtc);
  4557. drm_crtc_vblank_on(crtc);
  4558. intel_encoders_enable(crtc, pipe_config, old_state);
  4559. if (HAS_PCH_CPT(dev_priv))
  4560. cpt_verify_modeset(dev, intel_crtc->pipe);
  4561. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4562. if (intel_crtc->config->has_pch_encoder)
  4563. intel_wait_for_vblank(dev_priv, pipe);
  4564. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4565. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4566. }
  4567. /* IPS only exists on ULT machines and is tied to pipe A. */
  4568. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4569. {
  4570. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4571. }
  4572. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4573. struct drm_atomic_state *old_state)
  4574. {
  4575. struct drm_crtc *crtc = pipe_config->base.crtc;
  4576. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4577. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4578. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4579. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4580. struct intel_atomic_state *old_intel_state =
  4581. to_intel_atomic_state(old_state);
  4582. if (WARN_ON(intel_crtc->active))
  4583. return;
  4584. if (intel_crtc->config->has_pch_encoder)
  4585. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4586. false);
  4587. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4588. if (intel_crtc->config->shared_dpll)
  4589. intel_enable_shared_dpll(intel_crtc);
  4590. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4591. intel_dp_set_m_n(intel_crtc, M1_N1);
  4592. if (!transcoder_is_dsi(cpu_transcoder))
  4593. intel_set_pipe_timings(intel_crtc);
  4594. intel_set_pipe_src_size(intel_crtc);
  4595. if (cpu_transcoder != TRANSCODER_EDP &&
  4596. !transcoder_is_dsi(cpu_transcoder)) {
  4597. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4598. intel_crtc->config->pixel_multiplier - 1);
  4599. }
  4600. if (intel_crtc->config->has_pch_encoder) {
  4601. intel_cpu_transcoder_set_m_n(intel_crtc,
  4602. &intel_crtc->config->fdi_m_n, NULL);
  4603. }
  4604. if (!transcoder_is_dsi(cpu_transcoder))
  4605. haswell_set_pipeconf(crtc);
  4606. haswell_set_pipemisc(crtc);
  4607. intel_color_set_csc(&pipe_config->base);
  4608. intel_crtc->active = true;
  4609. if (intel_crtc->config->has_pch_encoder)
  4610. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4611. else
  4612. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4613. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4614. if (intel_crtc->config->has_pch_encoder)
  4615. dev_priv->display.fdi_link_train(crtc);
  4616. if (!transcoder_is_dsi(cpu_transcoder))
  4617. intel_ddi_enable_pipe_clock(intel_crtc);
  4618. if (INTEL_GEN(dev_priv) >= 9)
  4619. skylake_pfit_enable(intel_crtc);
  4620. else
  4621. ironlake_pfit_enable(intel_crtc);
  4622. /*
  4623. * On ILK+ LUT must be loaded before the pipe is running but with
  4624. * clocks enabled
  4625. */
  4626. intel_color_load_luts(&pipe_config->base);
  4627. intel_ddi_set_pipe_settings(crtc);
  4628. if (!transcoder_is_dsi(cpu_transcoder))
  4629. intel_ddi_enable_transcoder_func(crtc);
  4630. if (dev_priv->display.initial_watermarks != NULL)
  4631. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4632. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4633. if (!transcoder_is_dsi(cpu_transcoder))
  4634. intel_enable_pipe(intel_crtc);
  4635. if (intel_crtc->config->has_pch_encoder)
  4636. lpt_pch_enable(crtc);
  4637. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4638. intel_ddi_set_vc_payload_alloc(crtc, true);
  4639. assert_vblank_disabled(crtc);
  4640. drm_crtc_vblank_on(crtc);
  4641. intel_encoders_enable(crtc, pipe_config, old_state);
  4642. if (intel_crtc->config->has_pch_encoder) {
  4643. intel_wait_for_vblank(dev_priv, pipe);
  4644. intel_wait_for_vblank(dev_priv, pipe);
  4645. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4646. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4647. true);
  4648. }
  4649. /* If we change the relative order between pipe/planes enabling, we need
  4650. * to change the workaround. */
  4651. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4652. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4653. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4654. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4655. }
  4656. }
  4657. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4658. {
  4659. struct drm_device *dev = crtc->base.dev;
  4660. struct drm_i915_private *dev_priv = to_i915(dev);
  4661. int pipe = crtc->pipe;
  4662. /* To avoid upsetting the power well on haswell only disable the pfit if
  4663. * it's in use. The hw state code will make sure we get this right. */
  4664. if (force || crtc->config->pch_pfit.enabled) {
  4665. I915_WRITE(PF_CTL(pipe), 0);
  4666. I915_WRITE(PF_WIN_POS(pipe), 0);
  4667. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4668. }
  4669. }
  4670. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4671. struct drm_atomic_state *old_state)
  4672. {
  4673. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4674. struct drm_device *dev = crtc->dev;
  4675. struct drm_i915_private *dev_priv = to_i915(dev);
  4676. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4677. int pipe = intel_crtc->pipe;
  4678. /*
  4679. * Sometimes spurious CPU pipe underruns happen when the
  4680. * pipe is already disabled, but FDI RX/TX is still enabled.
  4681. * Happens at least with VGA+HDMI cloning. Suppress them.
  4682. */
  4683. if (intel_crtc->config->has_pch_encoder) {
  4684. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4685. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4686. }
  4687. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4688. drm_crtc_vblank_off(crtc);
  4689. assert_vblank_disabled(crtc);
  4690. intel_disable_pipe(intel_crtc);
  4691. ironlake_pfit_disable(intel_crtc, false);
  4692. if (intel_crtc->config->has_pch_encoder)
  4693. ironlake_fdi_disable(crtc);
  4694. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4695. if (intel_crtc->config->has_pch_encoder) {
  4696. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4697. if (HAS_PCH_CPT(dev_priv)) {
  4698. i915_reg_t reg;
  4699. u32 temp;
  4700. /* disable TRANS_DP_CTL */
  4701. reg = TRANS_DP_CTL(pipe);
  4702. temp = I915_READ(reg);
  4703. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4704. TRANS_DP_PORT_SEL_MASK);
  4705. temp |= TRANS_DP_PORT_SEL_NONE;
  4706. I915_WRITE(reg, temp);
  4707. /* disable DPLL_SEL */
  4708. temp = I915_READ(PCH_DPLL_SEL);
  4709. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4710. I915_WRITE(PCH_DPLL_SEL, temp);
  4711. }
  4712. ironlake_fdi_pll_disable(intel_crtc);
  4713. }
  4714. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4715. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4716. }
  4717. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4718. struct drm_atomic_state *old_state)
  4719. {
  4720. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4721. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4722. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4723. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4724. if (intel_crtc->config->has_pch_encoder)
  4725. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4726. false);
  4727. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4728. drm_crtc_vblank_off(crtc);
  4729. assert_vblank_disabled(crtc);
  4730. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4731. if (!transcoder_is_dsi(cpu_transcoder))
  4732. intel_disable_pipe(intel_crtc);
  4733. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4734. intel_ddi_set_vc_payload_alloc(crtc, false);
  4735. if (!transcoder_is_dsi(cpu_transcoder))
  4736. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4737. if (INTEL_GEN(dev_priv) >= 9)
  4738. skylake_scaler_disable(intel_crtc);
  4739. else
  4740. ironlake_pfit_disable(intel_crtc, false);
  4741. if (!transcoder_is_dsi(cpu_transcoder))
  4742. intel_ddi_disable_pipe_clock(intel_crtc);
  4743. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4744. if (old_crtc_state->has_pch_encoder)
  4745. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4746. true);
  4747. }
  4748. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4749. {
  4750. struct drm_device *dev = crtc->base.dev;
  4751. struct drm_i915_private *dev_priv = to_i915(dev);
  4752. struct intel_crtc_state *pipe_config = crtc->config;
  4753. if (!pipe_config->gmch_pfit.control)
  4754. return;
  4755. /*
  4756. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4757. * according to register description and PRM.
  4758. */
  4759. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4760. assert_pipe_disabled(dev_priv, crtc->pipe);
  4761. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4762. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4763. /* Border color in case we don't scale up to the full screen. Black by
  4764. * default, change to something else for debugging. */
  4765. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4766. }
  4767. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4768. {
  4769. switch (port) {
  4770. case PORT_A:
  4771. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4772. case PORT_B:
  4773. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4774. case PORT_C:
  4775. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4776. case PORT_D:
  4777. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4778. case PORT_E:
  4779. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4780. default:
  4781. MISSING_CASE(port);
  4782. return POWER_DOMAIN_PORT_OTHER;
  4783. }
  4784. }
  4785. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4786. {
  4787. switch (port) {
  4788. case PORT_A:
  4789. return POWER_DOMAIN_AUX_A;
  4790. case PORT_B:
  4791. return POWER_DOMAIN_AUX_B;
  4792. case PORT_C:
  4793. return POWER_DOMAIN_AUX_C;
  4794. case PORT_D:
  4795. return POWER_DOMAIN_AUX_D;
  4796. case PORT_E:
  4797. /* FIXME: Check VBT for actual wiring of PORT E */
  4798. return POWER_DOMAIN_AUX_D;
  4799. default:
  4800. MISSING_CASE(port);
  4801. return POWER_DOMAIN_AUX_A;
  4802. }
  4803. }
  4804. enum intel_display_power_domain
  4805. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4806. {
  4807. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4808. struct intel_digital_port *intel_dig_port;
  4809. switch (intel_encoder->type) {
  4810. case INTEL_OUTPUT_UNKNOWN:
  4811. /* Only DDI platforms should ever use this output type */
  4812. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4813. case INTEL_OUTPUT_DP:
  4814. case INTEL_OUTPUT_HDMI:
  4815. case INTEL_OUTPUT_EDP:
  4816. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4817. return port_to_power_domain(intel_dig_port->port);
  4818. case INTEL_OUTPUT_DP_MST:
  4819. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4820. return port_to_power_domain(intel_dig_port->port);
  4821. case INTEL_OUTPUT_ANALOG:
  4822. return POWER_DOMAIN_PORT_CRT;
  4823. case INTEL_OUTPUT_DSI:
  4824. return POWER_DOMAIN_PORT_DSI;
  4825. default:
  4826. return POWER_DOMAIN_PORT_OTHER;
  4827. }
  4828. }
  4829. enum intel_display_power_domain
  4830. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4831. {
  4832. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4833. struct intel_digital_port *intel_dig_port;
  4834. switch (intel_encoder->type) {
  4835. case INTEL_OUTPUT_UNKNOWN:
  4836. case INTEL_OUTPUT_HDMI:
  4837. /*
  4838. * Only DDI platforms should ever use these output types.
  4839. * We can get here after the HDMI detect code has already set
  4840. * the type of the shared encoder. Since we can't be sure
  4841. * what's the status of the given connectors, play safe and
  4842. * run the DP detection too.
  4843. */
  4844. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4845. case INTEL_OUTPUT_DP:
  4846. case INTEL_OUTPUT_EDP:
  4847. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4848. return port_to_aux_power_domain(intel_dig_port->port);
  4849. case INTEL_OUTPUT_DP_MST:
  4850. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4851. return port_to_aux_power_domain(intel_dig_port->port);
  4852. default:
  4853. MISSING_CASE(intel_encoder->type);
  4854. return POWER_DOMAIN_AUX_A;
  4855. }
  4856. }
  4857. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4858. struct intel_crtc_state *crtc_state)
  4859. {
  4860. struct drm_device *dev = crtc->dev;
  4861. struct drm_encoder *encoder;
  4862. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4863. enum pipe pipe = intel_crtc->pipe;
  4864. unsigned long mask;
  4865. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4866. if (!crtc_state->base.active)
  4867. return 0;
  4868. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4869. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4870. if (crtc_state->pch_pfit.enabled ||
  4871. crtc_state->pch_pfit.force_thru)
  4872. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4873. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4874. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4875. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4876. }
  4877. if (crtc_state->shared_dpll)
  4878. mask |= BIT(POWER_DOMAIN_PLLS);
  4879. return mask;
  4880. }
  4881. static unsigned long
  4882. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4883. struct intel_crtc_state *crtc_state)
  4884. {
  4885. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4886. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4887. enum intel_display_power_domain domain;
  4888. unsigned long domains, new_domains, old_domains;
  4889. old_domains = intel_crtc->enabled_power_domains;
  4890. intel_crtc->enabled_power_domains = new_domains =
  4891. get_crtc_power_domains(crtc, crtc_state);
  4892. domains = new_domains & ~old_domains;
  4893. for_each_power_domain(domain, domains)
  4894. intel_display_power_get(dev_priv, domain);
  4895. return old_domains & ~new_domains;
  4896. }
  4897. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4898. unsigned long domains)
  4899. {
  4900. enum intel_display_power_domain domain;
  4901. for_each_power_domain(domain, domains)
  4902. intel_display_power_put(dev_priv, domain);
  4903. }
  4904. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4905. {
  4906. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4907. if (IS_GEMINILAKE(dev_priv))
  4908. return 2 * max_cdclk_freq;
  4909. else if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4910. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4911. return max_cdclk_freq;
  4912. else if (IS_CHERRYVIEW(dev_priv))
  4913. return max_cdclk_freq*95/100;
  4914. else if (INTEL_INFO(dev_priv)->gen < 4)
  4915. return 2*max_cdclk_freq*90/100;
  4916. else
  4917. return max_cdclk_freq*90/100;
  4918. }
  4919. static int skl_calc_cdclk(int max_pixclk, int vco);
  4920. static void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
  4921. {
  4922. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  4923. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4924. int max_cdclk, vco;
  4925. vco = dev_priv->skl_preferred_vco_freq;
  4926. WARN_ON(vco != 8100000 && vco != 8640000);
  4927. /*
  4928. * Use the lower (vco 8640) cdclk values as a
  4929. * first guess. skl_calc_cdclk() will correct it
  4930. * if the preferred vco is 8100 instead.
  4931. */
  4932. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4933. max_cdclk = 617143;
  4934. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4935. max_cdclk = 540000;
  4936. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4937. max_cdclk = 432000;
  4938. else
  4939. max_cdclk = 308571;
  4940. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4941. } else if (IS_GEMINILAKE(dev_priv)) {
  4942. dev_priv->max_cdclk_freq = 316800;
  4943. } else if (IS_BROXTON(dev_priv)) {
  4944. dev_priv->max_cdclk_freq = 624000;
  4945. } else if (IS_BROADWELL(dev_priv)) {
  4946. /*
  4947. * FIXME with extra cooling we can allow
  4948. * 540 MHz for ULX and 675 Mhz for ULT.
  4949. * How can we know if extra cooling is
  4950. * available? PCI ID, VTB, something else?
  4951. */
  4952. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4953. dev_priv->max_cdclk_freq = 450000;
  4954. else if (IS_BDW_ULX(dev_priv))
  4955. dev_priv->max_cdclk_freq = 450000;
  4956. else if (IS_BDW_ULT(dev_priv))
  4957. dev_priv->max_cdclk_freq = 540000;
  4958. else
  4959. dev_priv->max_cdclk_freq = 675000;
  4960. } else if (IS_CHERRYVIEW(dev_priv)) {
  4961. dev_priv->max_cdclk_freq = 320000;
  4962. } else if (IS_VALLEYVIEW(dev_priv)) {
  4963. dev_priv->max_cdclk_freq = 400000;
  4964. } else {
  4965. /* otherwise assume cdclk is fixed */
  4966. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4967. }
  4968. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4969. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4970. dev_priv->max_cdclk_freq);
  4971. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4972. dev_priv->max_dotclk_freq);
  4973. }
  4974. static void intel_update_cdclk(struct drm_i915_private *dev_priv)
  4975. {
  4976. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev_priv);
  4977. if (INTEL_GEN(dev_priv) >= 9)
  4978. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  4979. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  4980. dev_priv->cdclk_pll.ref);
  4981. else
  4982. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4983. dev_priv->cdclk_freq);
  4984. /*
  4985. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4986. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4987. * of cdclk that generates 4MHz reference clock freq which is used to
  4988. * generate GMBus clock. This will vary with the cdclk freq.
  4989. */
  4990. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4991. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4992. }
  4993. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4994. static int skl_cdclk_decimal(int cdclk)
  4995. {
  4996. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  4997. }
  4998. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  4999. {
  5000. int ratio;
  5001. if (cdclk == dev_priv->cdclk_pll.ref)
  5002. return 0;
  5003. switch (cdclk) {
  5004. default:
  5005. MISSING_CASE(cdclk);
  5006. case 144000:
  5007. case 288000:
  5008. case 384000:
  5009. case 576000:
  5010. ratio = 60;
  5011. break;
  5012. case 624000:
  5013. ratio = 65;
  5014. break;
  5015. }
  5016. return dev_priv->cdclk_pll.ref * ratio;
  5017. }
  5018. static int glk_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  5019. {
  5020. int ratio;
  5021. if (cdclk == dev_priv->cdclk_pll.ref)
  5022. return 0;
  5023. switch (cdclk) {
  5024. default:
  5025. MISSING_CASE(cdclk);
  5026. case 79200:
  5027. case 158400:
  5028. case 316800:
  5029. ratio = 33;
  5030. break;
  5031. }
  5032. return dev_priv->cdclk_pll.ref * ratio;
  5033. }
  5034. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  5035. {
  5036. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  5037. /* Timeout 200us */
  5038. if (intel_wait_for_register(dev_priv,
  5039. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  5040. 1))
  5041. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  5042. dev_priv->cdclk_pll.vco = 0;
  5043. }
  5044. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  5045. {
  5046. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  5047. u32 val;
  5048. val = I915_READ(BXT_DE_PLL_CTL);
  5049. val &= ~BXT_DE_PLL_RATIO_MASK;
  5050. val |= BXT_DE_PLL_RATIO(ratio);
  5051. I915_WRITE(BXT_DE_PLL_CTL, val);
  5052. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  5053. /* Timeout 200us */
  5054. if (intel_wait_for_register(dev_priv,
  5055. BXT_DE_PLL_ENABLE,
  5056. BXT_DE_PLL_LOCK,
  5057. BXT_DE_PLL_LOCK,
  5058. 1))
  5059. DRM_ERROR("timeout waiting for DE PLL lock\n");
  5060. dev_priv->cdclk_pll.vco = vco;
  5061. }
  5062. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  5063. {
  5064. u32 val, divider;
  5065. int vco, ret;
  5066. if (IS_GEMINILAKE(dev_priv))
  5067. vco = glk_de_pll_vco(dev_priv, cdclk);
  5068. else
  5069. vco = bxt_de_pll_vco(dev_priv, cdclk);
  5070. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5071. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  5072. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  5073. case 8:
  5074. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  5075. break;
  5076. case 4:
  5077. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  5078. break;
  5079. case 3:
  5080. WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
  5081. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  5082. break;
  5083. case 2:
  5084. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5085. break;
  5086. default:
  5087. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  5088. WARN_ON(vco != 0);
  5089. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5090. break;
  5091. }
  5092. /* Inform power controller of upcoming frequency change */
  5093. mutex_lock(&dev_priv->rps.hw_lock);
  5094. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5095. 0x80000000);
  5096. mutex_unlock(&dev_priv->rps.hw_lock);
  5097. if (ret) {
  5098. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  5099. ret, cdclk);
  5100. return;
  5101. }
  5102. if (dev_priv->cdclk_pll.vco != 0 &&
  5103. dev_priv->cdclk_pll.vco != vco)
  5104. bxt_de_pll_disable(dev_priv);
  5105. if (dev_priv->cdclk_pll.vco != vco)
  5106. bxt_de_pll_enable(dev_priv, vco);
  5107. val = divider | skl_cdclk_decimal(cdclk);
  5108. /*
  5109. * FIXME if only the cd2x divider needs changing, it could be done
  5110. * without shutting off the pipe (if only one pipe is active).
  5111. */
  5112. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  5113. /*
  5114. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5115. * enable otherwise.
  5116. */
  5117. if (cdclk >= 500000)
  5118. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5119. I915_WRITE(CDCLK_CTL, val);
  5120. mutex_lock(&dev_priv->rps.hw_lock);
  5121. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5122. DIV_ROUND_UP(cdclk, 25000));
  5123. mutex_unlock(&dev_priv->rps.hw_lock);
  5124. if (ret) {
  5125. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  5126. ret, cdclk);
  5127. return;
  5128. }
  5129. intel_update_cdclk(dev_priv);
  5130. }
  5131. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5132. {
  5133. u32 cdctl, expected;
  5134. intel_update_cdclk(dev_priv);
  5135. if (dev_priv->cdclk_pll.vco == 0 ||
  5136. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5137. goto sanitize;
  5138. /* DPLL okay; verify the cdclock
  5139. *
  5140. * Some BIOS versions leave an incorrect decimal frequency value and
  5141. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  5142. * so sanitize this register.
  5143. */
  5144. cdctl = I915_READ(CDCLK_CTL);
  5145. /*
  5146. * Let's ignore the pipe field, since BIOS could have configured the
  5147. * dividers both synching to an active pipe, or asynchronously
  5148. * (PIPE_NONE).
  5149. */
  5150. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  5151. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  5152. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5153. /*
  5154. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5155. * enable otherwise.
  5156. */
  5157. if (dev_priv->cdclk_freq >= 500000)
  5158. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5159. if (cdctl == expected)
  5160. /* All well; nothing to sanitize */
  5161. return;
  5162. sanitize:
  5163. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5164. /* force cdclk programming */
  5165. dev_priv->cdclk_freq = 0;
  5166. /* force full PLL disable + enable */
  5167. dev_priv->cdclk_pll.vco = -1;
  5168. }
  5169. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  5170. {
  5171. int cdclk;
  5172. bxt_sanitize_cdclk(dev_priv);
  5173. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  5174. return;
  5175. /*
  5176. * FIXME:
  5177. * - The initial CDCLK needs to be read from VBT.
  5178. * Need to make this change after VBT has changes for BXT.
  5179. */
  5180. if (IS_GEMINILAKE(dev_priv))
  5181. cdclk = glk_calc_cdclk(0);
  5182. else
  5183. cdclk = bxt_calc_cdclk(0);
  5184. bxt_set_cdclk(dev_priv, cdclk);
  5185. }
  5186. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  5187. {
  5188. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  5189. }
  5190. static int skl_calc_cdclk(int max_pixclk, int vco)
  5191. {
  5192. if (vco == 8640000) {
  5193. if (max_pixclk > 540000)
  5194. return 617143;
  5195. else if (max_pixclk > 432000)
  5196. return 540000;
  5197. else if (max_pixclk > 308571)
  5198. return 432000;
  5199. else
  5200. return 308571;
  5201. } else {
  5202. if (max_pixclk > 540000)
  5203. return 675000;
  5204. else if (max_pixclk > 450000)
  5205. return 540000;
  5206. else if (max_pixclk > 337500)
  5207. return 450000;
  5208. else
  5209. return 337500;
  5210. }
  5211. }
  5212. static void
  5213. skl_dpll0_update(struct drm_i915_private *dev_priv)
  5214. {
  5215. u32 val;
  5216. dev_priv->cdclk_pll.ref = 24000;
  5217. dev_priv->cdclk_pll.vco = 0;
  5218. val = I915_READ(LCPLL1_CTL);
  5219. if ((val & LCPLL_PLL_ENABLE) == 0)
  5220. return;
  5221. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  5222. return;
  5223. val = I915_READ(DPLL_CTRL1);
  5224. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  5225. DPLL_CTRL1_SSC(SKL_DPLL0) |
  5226. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  5227. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  5228. return;
  5229. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  5230. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  5231. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  5232. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  5233. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  5234. dev_priv->cdclk_pll.vco = 8100000;
  5235. break;
  5236. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  5237. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  5238. dev_priv->cdclk_pll.vco = 8640000;
  5239. break;
  5240. default:
  5241. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5242. break;
  5243. }
  5244. }
  5245. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  5246. {
  5247. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  5248. dev_priv->skl_preferred_vco_freq = vco;
  5249. if (changed)
  5250. intel_update_max_cdclk(dev_priv);
  5251. }
  5252. static void
  5253. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  5254. {
  5255. int min_cdclk = skl_calc_cdclk(0, vco);
  5256. u32 val;
  5257. WARN_ON(vco != 8100000 && vco != 8640000);
  5258. /* select the minimum CDCLK before enabling DPLL 0 */
  5259. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  5260. I915_WRITE(CDCLK_CTL, val);
  5261. POSTING_READ(CDCLK_CTL);
  5262. /*
  5263. * We always enable DPLL0 with the lowest link rate possible, but still
  5264. * taking into account the VCO required to operate the eDP panel at the
  5265. * desired frequency. The usual DP link rates operate with a VCO of
  5266. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  5267. * The modeset code is responsible for the selection of the exact link
  5268. * rate later on, with the constraint of choosing a frequency that
  5269. * works with vco.
  5270. */
  5271. val = I915_READ(DPLL_CTRL1);
  5272. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  5273. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5274. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  5275. if (vco == 8640000)
  5276. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  5277. SKL_DPLL0);
  5278. else
  5279. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  5280. SKL_DPLL0);
  5281. I915_WRITE(DPLL_CTRL1, val);
  5282. POSTING_READ(DPLL_CTRL1);
  5283. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  5284. if (intel_wait_for_register(dev_priv,
  5285. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  5286. 5))
  5287. DRM_ERROR("DPLL0 not locked\n");
  5288. dev_priv->cdclk_pll.vco = vco;
  5289. /* We'll want to keep using the current vco from now on. */
  5290. skl_set_preferred_cdclk_vco(dev_priv, vco);
  5291. }
  5292. static void
  5293. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  5294. {
  5295. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  5296. if (intel_wait_for_register(dev_priv,
  5297. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  5298. 1))
  5299. DRM_ERROR("Couldn't disable DPLL0\n");
  5300. dev_priv->cdclk_pll.vco = 0;
  5301. }
  5302. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  5303. {
  5304. u32 freq_select, pcu_ack;
  5305. int ret;
  5306. WARN_ON((cdclk == 24000) != (vco == 0));
  5307. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5308. mutex_lock(&dev_priv->rps.hw_lock);
  5309. ret = skl_pcode_request(dev_priv, SKL_PCODE_CDCLK_CONTROL,
  5310. SKL_CDCLK_PREPARE_FOR_CHANGE,
  5311. SKL_CDCLK_READY_FOR_CHANGE,
  5312. SKL_CDCLK_READY_FOR_CHANGE, 3);
  5313. mutex_unlock(&dev_priv->rps.hw_lock);
  5314. if (ret) {
  5315. DRM_ERROR("Failed to inform PCU about cdclk change (%d)\n",
  5316. ret);
  5317. return;
  5318. }
  5319. /* set CDCLK_CTL */
  5320. switch (cdclk) {
  5321. case 450000:
  5322. case 432000:
  5323. freq_select = CDCLK_FREQ_450_432;
  5324. pcu_ack = 1;
  5325. break;
  5326. case 540000:
  5327. freq_select = CDCLK_FREQ_540;
  5328. pcu_ack = 2;
  5329. break;
  5330. case 308571:
  5331. case 337500:
  5332. default:
  5333. freq_select = CDCLK_FREQ_337_308;
  5334. pcu_ack = 0;
  5335. break;
  5336. case 617143:
  5337. case 675000:
  5338. freq_select = CDCLK_FREQ_675_617;
  5339. pcu_ack = 3;
  5340. break;
  5341. }
  5342. if (dev_priv->cdclk_pll.vco != 0 &&
  5343. dev_priv->cdclk_pll.vco != vco)
  5344. skl_dpll0_disable(dev_priv);
  5345. if (dev_priv->cdclk_pll.vco != vco)
  5346. skl_dpll0_enable(dev_priv, vco);
  5347. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  5348. POSTING_READ(CDCLK_CTL);
  5349. /* inform PCU of the change */
  5350. mutex_lock(&dev_priv->rps.hw_lock);
  5351. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  5352. mutex_unlock(&dev_priv->rps.hw_lock);
  5353. intel_update_cdclk(dev_priv);
  5354. }
  5355. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  5356. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  5357. {
  5358. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  5359. }
  5360. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  5361. {
  5362. int cdclk, vco;
  5363. skl_sanitize_cdclk(dev_priv);
  5364. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  5365. /*
  5366. * Use the current vco as our initial
  5367. * guess as to what the preferred vco is.
  5368. */
  5369. if (dev_priv->skl_preferred_vco_freq == 0)
  5370. skl_set_preferred_cdclk_vco(dev_priv,
  5371. dev_priv->cdclk_pll.vco);
  5372. return;
  5373. }
  5374. vco = dev_priv->skl_preferred_vco_freq;
  5375. if (vco == 0)
  5376. vco = 8100000;
  5377. cdclk = skl_calc_cdclk(0, vco);
  5378. skl_set_cdclk(dev_priv, cdclk, vco);
  5379. }
  5380. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5381. {
  5382. uint32_t cdctl, expected;
  5383. /*
  5384. * check if the pre-os intialized the display
  5385. * There is SWF18 scratchpad register defined which is set by the
  5386. * pre-os which can be used by the OS drivers to check the status
  5387. */
  5388. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  5389. goto sanitize;
  5390. intel_update_cdclk(dev_priv);
  5391. /* Is PLL enabled and locked ? */
  5392. if (dev_priv->cdclk_pll.vco == 0 ||
  5393. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5394. goto sanitize;
  5395. /* DPLL okay; verify the cdclock
  5396. *
  5397. * Noticed in some instances that the freq selection is correct but
  5398. * decimal part is programmed wrong from BIOS where pre-os does not
  5399. * enable display. Verify the same as well.
  5400. */
  5401. cdctl = I915_READ(CDCLK_CTL);
  5402. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  5403. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5404. if (cdctl == expected)
  5405. /* All well; nothing to sanitize */
  5406. return;
  5407. sanitize:
  5408. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5409. /* force cdclk programming */
  5410. dev_priv->cdclk_freq = 0;
  5411. /* force full PLL disable + enable */
  5412. dev_priv->cdclk_pll.vco = -1;
  5413. }
  5414. /* Adjust CDclk dividers to allow high res or save power if possible */
  5415. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  5416. {
  5417. struct drm_i915_private *dev_priv = to_i915(dev);
  5418. u32 val, cmd;
  5419. WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
  5420. != dev_priv->cdclk_freq);
  5421. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  5422. cmd = 2;
  5423. else if (cdclk == 266667)
  5424. cmd = 1;
  5425. else
  5426. cmd = 0;
  5427. mutex_lock(&dev_priv->rps.hw_lock);
  5428. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5429. val &= ~DSPFREQGUAR_MASK;
  5430. val |= (cmd << DSPFREQGUAR_SHIFT);
  5431. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5432. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5433. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  5434. 50)) {
  5435. DRM_ERROR("timed out waiting for CDclk change\n");
  5436. }
  5437. mutex_unlock(&dev_priv->rps.hw_lock);
  5438. mutex_lock(&dev_priv->sb_lock);
  5439. if (cdclk == 400000) {
  5440. u32 divider;
  5441. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5442. /* adjust cdclk divider */
  5443. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5444. val &= ~CCK_FREQUENCY_VALUES;
  5445. val |= divider;
  5446. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  5447. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  5448. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  5449. 50))
  5450. DRM_ERROR("timed out waiting for CDclk change\n");
  5451. }
  5452. /* adjust self-refresh exit latency value */
  5453. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  5454. val &= ~0x7f;
  5455. /*
  5456. * For high bandwidth configs, we set a higher latency in the bunit
  5457. * so that the core display fetch happens in time to avoid underruns.
  5458. */
  5459. if (cdclk == 400000)
  5460. val |= 4500 / 250; /* 4.5 usec */
  5461. else
  5462. val |= 3000 / 250; /* 3.0 usec */
  5463. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  5464. mutex_unlock(&dev_priv->sb_lock);
  5465. intel_update_cdclk(dev_priv);
  5466. }
  5467. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  5468. {
  5469. struct drm_i915_private *dev_priv = to_i915(dev);
  5470. u32 val, cmd;
  5471. WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
  5472. != dev_priv->cdclk_freq);
  5473. switch (cdclk) {
  5474. case 333333:
  5475. case 320000:
  5476. case 266667:
  5477. case 200000:
  5478. break;
  5479. default:
  5480. MISSING_CASE(cdclk);
  5481. return;
  5482. }
  5483. /*
  5484. * Specs are full of misinformation, but testing on actual
  5485. * hardware has shown that we just need to write the desired
  5486. * CCK divider into the Punit register.
  5487. */
  5488. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5489. mutex_lock(&dev_priv->rps.hw_lock);
  5490. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5491. val &= ~DSPFREQGUAR_MASK_CHV;
  5492. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5493. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5494. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5495. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5496. 50)) {
  5497. DRM_ERROR("timed out waiting for CDclk change\n");
  5498. }
  5499. mutex_unlock(&dev_priv->rps.hw_lock);
  5500. intel_update_cdclk(dev_priv);
  5501. }
  5502. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5503. int max_pixclk)
  5504. {
  5505. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5506. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5507. /*
  5508. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5509. * 200MHz
  5510. * 267MHz
  5511. * 320/333MHz (depends on HPLL freq)
  5512. * 400MHz (VLV only)
  5513. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5514. * of the lower bin and adjust if needed.
  5515. *
  5516. * We seem to get an unstable or solid color picture at 200MHz.
  5517. * Not sure what's wrong. For now use 200MHz only when all pipes
  5518. * are off.
  5519. */
  5520. if (!IS_CHERRYVIEW(dev_priv) &&
  5521. max_pixclk > freq_320*limit/100)
  5522. return 400000;
  5523. else if (max_pixclk > 266667*limit/100)
  5524. return freq_320;
  5525. else if (max_pixclk > 0)
  5526. return 266667;
  5527. else
  5528. return 200000;
  5529. }
  5530. static int glk_calc_cdclk(int max_pixclk)
  5531. {
  5532. if (max_pixclk > 2 * 158400)
  5533. return 316800;
  5534. else if (max_pixclk > 2 * 79200)
  5535. return 158400;
  5536. else
  5537. return 79200;
  5538. }
  5539. static int bxt_calc_cdclk(int max_pixclk)
  5540. {
  5541. if (max_pixclk > 576000)
  5542. return 624000;
  5543. else if (max_pixclk > 384000)
  5544. return 576000;
  5545. else if (max_pixclk > 288000)
  5546. return 384000;
  5547. else if (max_pixclk > 144000)
  5548. return 288000;
  5549. else
  5550. return 144000;
  5551. }
  5552. /* Compute the max pixel clock for new configuration. */
  5553. static int intel_mode_max_pixclk(struct drm_device *dev,
  5554. struct drm_atomic_state *state)
  5555. {
  5556. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5557. struct drm_i915_private *dev_priv = to_i915(dev);
  5558. struct drm_crtc *crtc;
  5559. struct drm_crtc_state *crtc_state;
  5560. unsigned max_pixclk = 0, i;
  5561. enum pipe pipe;
  5562. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5563. sizeof(intel_state->min_pixclk));
  5564. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5565. int pixclk = 0;
  5566. if (crtc_state->enable)
  5567. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5568. intel_state->min_pixclk[i] = pixclk;
  5569. }
  5570. for_each_pipe(dev_priv, pipe)
  5571. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5572. return max_pixclk;
  5573. }
  5574. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5575. {
  5576. struct drm_device *dev = state->dev;
  5577. struct drm_i915_private *dev_priv = to_i915(dev);
  5578. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5579. struct intel_atomic_state *intel_state =
  5580. to_intel_atomic_state(state);
  5581. intel_state->cdclk = intel_state->dev_cdclk =
  5582. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5583. if (!intel_state->active_crtcs)
  5584. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5585. return 0;
  5586. }
  5587. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  5588. {
  5589. struct drm_i915_private *dev_priv = to_i915(state->dev);
  5590. int max_pixclk = ilk_max_pixel_rate(state);
  5591. struct intel_atomic_state *intel_state =
  5592. to_intel_atomic_state(state);
  5593. int cdclk;
  5594. if (IS_GEMINILAKE(dev_priv))
  5595. cdclk = glk_calc_cdclk(max_pixclk);
  5596. else
  5597. cdclk = bxt_calc_cdclk(max_pixclk);
  5598. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  5599. if (!intel_state->active_crtcs) {
  5600. if (IS_GEMINILAKE(dev_priv))
  5601. cdclk = glk_calc_cdclk(0);
  5602. else
  5603. cdclk = bxt_calc_cdclk(0);
  5604. intel_state->dev_cdclk = cdclk;
  5605. }
  5606. return 0;
  5607. }
  5608. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5609. {
  5610. unsigned int credits, default_credits;
  5611. if (IS_CHERRYVIEW(dev_priv))
  5612. default_credits = PFI_CREDIT(12);
  5613. else
  5614. default_credits = PFI_CREDIT(8);
  5615. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5616. /* CHV suggested value is 31 or 63 */
  5617. if (IS_CHERRYVIEW(dev_priv))
  5618. credits = PFI_CREDIT_63;
  5619. else
  5620. credits = PFI_CREDIT(15);
  5621. } else {
  5622. credits = default_credits;
  5623. }
  5624. /*
  5625. * WA - write default credits before re-programming
  5626. * FIXME: should we also set the resend bit here?
  5627. */
  5628. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5629. default_credits);
  5630. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5631. credits | PFI_CREDIT_RESEND);
  5632. /*
  5633. * FIXME is this guaranteed to clear
  5634. * immediately or should we poll for it?
  5635. */
  5636. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5637. }
  5638. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5639. {
  5640. struct drm_device *dev = old_state->dev;
  5641. struct drm_i915_private *dev_priv = to_i915(dev);
  5642. struct intel_atomic_state *old_intel_state =
  5643. to_intel_atomic_state(old_state);
  5644. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5645. /*
  5646. * FIXME: We can end up here with all power domains off, yet
  5647. * with a CDCLK frequency other than the minimum. To account
  5648. * for this take the PIPE-A power domain, which covers the HW
  5649. * blocks needed for the following programming. This can be
  5650. * removed once it's guaranteed that we get here either with
  5651. * the minimum CDCLK set, or the required power domains
  5652. * enabled.
  5653. */
  5654. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5655. if (IS_CHERRYVIEW(dev_priv))
  5656. cherryview_set_cdclk(dev, req_cdclk);
  5657. else
  5658. valleyview_set_cdclk(dev, req_cdclk);
  5659. vlv_program_pfi_credits(dev_priv);
  5660. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5661. }
  5662. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  5663. struct drm_atomic_state *old_state)
  5664. {
  5665. struct drm_crtc *crtc = pipe_config->base.crtc;
  5666. struct drm_device *dev = crtc->dev;
  5667. struct drm_i915_private *dev_priv = to_i915(dev);
  5668. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5669. int pipe = intel_crtc->pipe;
  5670. if (WARN_ON(intel_crtc->active))
  5671. return;
  5672. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5673. intel_dp_set_m_n(intel_crtc, M1_N1);
  5674. intel_set_pipe_timings(intel_crtc);
  5675. intel_set_pipe_src_size(intel_crtc);
  5676. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  5677. struct drm_i915_private *dev_priv = to_i915(dev);
  5678. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5679. I915_WRITE(CHV_CANVAS(pipe), 0);
  5680. }
  5681. i9xx_set_pipeconf(intel_crtc);
  5682. intel_crtc->active = true;
  5683. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5684. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  5685. if (IS_CHERRYVIEW(dev_priv)) {
  5686. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5687. chv_enable_pll(intel_crtc, intel_crtc->config);
  5688. } else {
  5689. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5690. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5691. }
  5692. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5693. i9xx_pfit_enable(intel_crtc);
  5694. intel_color_load_luts(&pipe_config->base);
  5695. intel_update_watermarks(intel_crtc);
  5696. intel_enable_pipe(intel_crtc);
  5697. assert_vblank_disabled(crtc);
  5698. drm_crtc_vblank_on(crtc);
  5699. intel_encoders_enable(crtc, pipe_config, old_state);
  5700. }
  5701. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5702. {
  5703. struct drm_device *dev = crtc->base.dev;
  5704. struct drm_i915_private *dev_priv = to_i915(dev);
  5705. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5706. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5707. }
  5708. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  5709. struct drm_atomic_state *old_state)
  5710. {
  5711. struct drm_crtc *crtc = pipe_config->base.crtc;
  5712. struct drm_device *dev = crtc->dev;
  5713. struct drm_i915_private *dev_priv = to_i915(dev);
  5714. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5715. enum pipe pipe = intel_crtc->pipe;
  5716. if (WARN_ON(intel_crtc->active))
  5717. return;
  5718. i9xx_set_pll_dividers(intel_crtc);
  5719. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5720. intel_dp_set_m_n(intel_crtc, M1_N1);
  5721. intel_set_pipe_timings(intel_crtc);
  5722. intel_set_pipe_src_size(intel_crtc);
  5723. i9xx_set_pipeconf(intel_crtc);
  5724. intel_crtc->active = true;
  5725. if (!IS_GEN2(dev_priv))
  5726. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5727. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5728. i9xx_enable_pll(intel_crtc);
  5729. i9xx_pfit_enable(intel_crtc);
  5730. intel_color_load_luts(&pipe_config->base);
  5731. intel_update_watermarks(intel_crtc);
  5732. intel_enable_pipe(intel_crtc);
  5733. assert_vblank_disabled(crtc);
  5734. drm_crtc_vblank_on(crtc);
  5735. intel_encoders_enable(crtc, pipe_config, old_state);
  5736. }
  5737. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5738. {
  5739. struct drm_device *dev = crtc->base.dev;
  5740. struct drm_i915_private *dev_priv = to_i915(dev);
  5741. if (!crtc->config->gmch_pfit.control)
  5742. return;
  5743. assert_pipe_disabled(dev_priv, crtc->pipe);
  5744. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5745. I915_READ(PFIT_CONTROL));
  5746. I915_WRITE(PFIT_CONTROL, 0);
  5747. }
  5748. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5749. struct drm_atomic_state *old_state)
  5750. {
  5751. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5752. struct drm_device *dev = crtc->dev;
  5753. struct drm_i915_private *dev_priv = to_i915(dev);
  5754. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5755. int pipe = intel_crtc->pipe;
  5756. /*
  5757. * On gen2 planes are double buffered but the pipe isn't, so we must
  5758. * wait for planes to fully turn off before disabling the pipe.
  5759. */
  5760. if (IS_GEN2(dev_priv))
  5761. intel_wait_for_vblank(dev_priv, pipe);
  5762. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5763. drm_crtc_vblank_off(crtc);
  5764. assert_vblank_disabled(crtc);
  5765. intel_disable_pipe(intel_crtc);
  5766. i9xx_pfit_disable(intel_crtc);
  5767. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5768. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5769. if (IS_CHERRYVIEW(dev_priv))
  5770. chv_disable_pll(dev_priv, pipe);
  5771. else if (IS_VALLEYVIEW(dev_priv))
  5772. vlv_disable_pll(dev_priv, pipe);
  5773. else
  5774. i9xx_disable_pll(intel_crtc);
  5775. }
  5776. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5777. if (!IS_GEN2(dev_priv))
  5778. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5779. }
  5780. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5781. {
  5782. struct intel_encoder *encoder;
  5783. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5784. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5785. enum intel_display_power_domain domain;
  5786. unsigned long domains;
  5787. struct drm_atomic_state *state;
  5788. struct intel_crtc_state *crtc_state;
  5789. int ret;
  5790. if (!intel_crtc->active)
  5791. return;
  5792. if (crtc->primary->state->visible) {
  5793. WARN_ON(intel_crtc->flip_work);
  5794. intel_pre_disable_primary_noatomic(crtc);
  5795. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5796. crtc->primary->state->visible = false;
  5797. }
  5798. state = drm_atomic_state_alloc(crtc->dev);
  5799. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  5800. /* Everything's already locked, -EDEADLK can't happen. */
  5801. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5802. ret = drm_atomic_add_affected_connectors(state, crtc);
  5803. WARN_ON(IS_ERR(crtc_state) || ret);
  5804. dev_priv->display.crtc_disable(crtc_state, state);
  5805. drm_atomic_state_put(state);
  5806. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5807. crtc->base.id, crtc->name);
  5808. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5809. crtc->state->active = false;
  5810. intel_crtc->active = false;
  5811. crtc->enabled = false;
  5812. crtc->state->connector_mask = 0;
  5813. crtc->state->encoder_mask = 0;
  5814. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5815. encoder->base.crtc = NULL;
  5816. intel_fbc_disable(intel_crtc);
  5817. intel_update_watermarks(intel_crtc);
  5818. intel_disable_shared_dpll(intel_crtc);
  5819. domains = intel_crtc->enabled_power_domains;
  5820. for_each_power_domain(domain, domains)
  5821. intel_display_power_put(dev_priv, domain);
  5822. intel_crtc->enabled_power_domains = 0;
  5823. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5824. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5825. }
  5826. /*
  5827. * turn all crtc's off, but do not adjust state
  5828. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5829. */
  5830. int intel_display_suspend(struct drm_device *dev)
  5831. {
  5832. struct drm_i915_private *dev_priv = to_i915(dev);
  5833. struct drm_atomic_state *state;
  5834. int ret;
  5835. state = drm_atomic_helper_suspend(dev);
  5836. ret = PTR_ERR_OR_ZERO(state);
  5837. if (ret)
  5838. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5839. else
  5840. dev_priv->modeset_restore_state = state;
  5841. return ret;
  5842. }
  5843. void intel_encoder_destroy(struct drm_encoder *encoder)
  5844. {
  5845. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5846. drm_encoder_cleanup(encoder);
  5847. kfree(intel_encoder);
  5848. }
  5849. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5850. * internal consistency). */
  5851. static void intel_connector_verify_state(struct intel_connector *connector)
  5852. {
  5853. struct drm_crtc *crtc = connector->base.state->crtc;
  5854. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5855. connector->base.base.id,
  5856. connector->base.name);
  5857. if (connector->get_hw_state(connector)) {
  5858. struct intel_encoder *encoder = connector->encoder;
  5859. struct drm_connector_state *conn_state = connector->base.state;
  5860. I915_STATE_WARN(!crtc,
  5861. "connector enabled without attached crtc\n");
  5862. if (!crtc)
  5863. return;
  5864. I915_STATE_WARN(!crtc->state->active,
  5865. "connector is active, but attached crtc isn't\n");
  5866. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5867. return;
  5868. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5869. "atomic encoder doesn't match attached encoder\n");
  5870. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5871. "attached encoder crtc differs from connector crtc\n");
  5872. } else {
  5873. I915_STATE_WARN(crtc && crtc->state->active,
  5874. "attached crtc is active, but connector isn't\n");
  5875. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5876. "best encoder set without crtc!\n");
  5877. }
  5878. }
  5879. int intel_connector_init(struct intel_connector *connector)
  5880. {
  5881. drm_atomic_helper_connector_reset(&connector->base);
  5882. if (!connector->base.state)
  5883. return -ENOMEM;
  5884. return 0;
  5885. }
  5886. struct intel_connector *intel_connector_alloc(void)
  5887. {
  5888. struct intel_connector *connector;
  5889. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5890. if (!connector)
  5891. return NULL;
  5892. if (intel_connector_init(connector) < 0) {
  5893. kfree(connector);
  5894. return NULL;
  5895. }
  5896. return connector;
  5897. }
  5898. /* Simple connector->get_hw_state implementation for encoders that support only
  5899. * one connector and no cloning and hence the encoder state determines the state
  5900. * of the connector. */
  5901. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5902. {
  5903. enum pipe pipe = 0;
  5904. struct intel_encoder *encoder = connector->encoder;
  5905. return encoder->get_hw_state(encoder, &pipe);
  5906. }
  5907. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5908. {
  5909. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5910. return crtc_state->fdi_lanes;
  5911. return 0;
  5912. }
  5913. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5914. struct intel_crtc_state *pipe_config)
  5915. {
  5916. struct drm_i915_private *dev_priv = to_i915(dev);
  5917. struct drm_atomic_state *state = pipe_config->base.state;
  5918. struct intel_crtc *other_crtc;
  5919. struct intel_crtc_state *other_crtc_state;
  5920. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5921. pipe_name(pipe), pipe_config->fdi_lanes);
  5922. if (pipe_config->fdi_lanes > 4) {
  5923. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5924. pipe_name(pipe), pipe_config->fdi_lanes);
  5925. return -EINVAL;
  5926. }
  5927. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5928. if (pipe_config->fdi_lanes > 2) {
  5929. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5930. pipe_config->fdi_lanes);
  5931. return -EINVAL;
  5932. } else {
  5933. return 0;
  5934. }
  5935. }
  5936. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5937. return 0;
  5938. /* Ivybridge 3 pipe is really complicated */
  5939. switch (pipe) {
  5940. case PIPE_A:
  5941. return 0;
  5942. case PIPE_B:
  5943. if (pipe_config->fdi_lanes <= 2)
  5944. return 0;
  5945. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5946. other_crtc_state =
  5947. intel_atomic_get_crtc_state(state, other_crtc);
  5948. if (IS_ERR(other_crtc_state))
  5949. return PTR_ERR(other_crtc_state);
  5950. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5951. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5952. pipe_name(pipe), pipe_config->fdi_lanes);
  5953. return -EINVAL;
  5954. }
  5955. return 0;
  5956. case PIPE_C:
  5957. if (pipe_config->fdi_lanes > 2) {
  5958. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5959. pipe_name(pipe), pipe_config->fdi_lanes);
  5960. return -EINVAL;
  5961. }
  5962. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5963. other_crtc_state =
  5964. intel_atomic_get_crtc_state(state, other_crtc);
  5965. if (IS_ERR(other_crtc_state))
  5966. return PTR_ERR(other_crtc_state);
  5967. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5968. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5969. return -EINVAL;
  5970. }
  5971. return 0;
  5972. default:
  5973. BUG();
  5974. }
  5975. }
  5976. #define RETRY 1
  5977. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5978. struct intel_crtc_state *pipe_config)
  5979. {
  5980. struct drm_device *dev = intel_crtc->base.dev;
  5981. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5982. int lane, link_bw, fdi_dotclock, ret;
  5983. bool needs_recompute = false;
  5984. retry:
  5985. /* FDI is a binary signal running at ~2.7GHz, encoding
  5986. * each output octet as 10 bits. The actual frequency
  5987. * is stored as a divider into a 100MHz clock, and the
  5988. * mode pixel clock is stored in units of 1KHz.
  5989. * Hence the bw of each lane in terms of the mode signal
  5990. * is:
  5991. */
  5992. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5993. fdi_dotclock = adjusted_mode->crtc_clock;
  5994. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5995. pipe_config->pipe_bpp);
  5996. pipe_config->fdi_lanes = lane;
  5997. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5998. link_bw, &pipe_config->fdi_m_n);
  5999. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  6000. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  6001. pipe_config->pipe_bpp -= 2*3;
  6002. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  6003. pipe_config->pipe_bpp);
  6004. needs_recompute = true;
  6005. pipe_config->bw_constrained = true;
  6006. goto retry;
  6007. }
  6008. if (needs_recompute)
  6009. return RETRY;
  6010. return ret;
  6011. }
  6012. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  6013. struct intel_crtc_state *pipe_config)
  6014. {
  6015. if (pipe_config->pipe_bpp > 24)
  6016. return false;
  6017. /* HSW can handle pixel rate up to cdclk? */
  6018. if (IS_HASWELL(dev_priv))
  6019. return true;
  6020. /*
  6021. * We compare against max which means we must take
  6022. * the increased cdclk requirement into account when
  6023. * calculating the new cdclk.
  6024. *
  6025. * Should measure whether using a lower cdclk w/o IPS
  6026. */
  6027. return ilk_pipe_pixel_rate(pipe_config) <=
  6028. dev_priv->max_cdclk_freq * 95 / 100;
  6029. }
  6030. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  6031. struct intel_crtc_state *pipe_config)
  6032. {
  6033. struct drm_device *dev = crtc->base.dev;
  6034. struct drm_i915_private *dev_priv = to_i915(dev);
  6035. pipe_config->ips_enabled = i915.enable_ips &&
  6036. hsw_crtc_supports_ips(crtc) &&
  6037. pipe_config_supports_ips(dev_priv, pipe_config);
  6038. }
  6039. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  6040. {
  6041. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6042. /* GDG double wide on either pipe, otherwise pipe A only */
  6043. return INTEL_INFO(dev_priv)->gen < 4 &&
  6044. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  6045. }
  6046. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  6047. struct intel_crtc_state *pipe_config)
  6048. {
  6049. struct drm_device *dev = crtc->base.dev;
  6050. struct drm_i915_private *dev_priv = to_i915(dev);
  6051. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  6052. int clock_limit = dev_priv->max_dotclk_freq;
  6053. if (INTEL_GEN(dev_priv) < 4) {
  6054. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  6055. /*
  6056. * Enable double wide mode when the dot clock
  6057. * is > 90% of the (display) core speed.
  6058. */
  6059. if (intel_crtc_supports_double_wide(crtc) &&
  6060. adjusted_mode->crtc_clock > clock_limit) {
  6061. clock_limit = dev_priv->max_dotclk_freq;
  6062. pipe_config->double_wide = true;
  6063. }
  6064. }
  6065. if (adjusted_mode->crtc_clock > clock_limit) {
  6066. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  6067. adjusted_mode->crtc_clock, clock_limit,
  6068. yesno(pipe_config->double_wide));
  6069. return -EINVAL;
  6070. }
  6071. /*
  6072. * Pipe horizontal size must be even in:
  6073. * - DVO ganged mode
  6074. * - LVDS dual channel mode
  6075. * - Double wide pipe
  6076. */
  6077. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  6078. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  6079. pipe_config->pipe_src_w &= ~1;
  6080. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  6081. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  6082. */
  6083. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  6084. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  6085. return -EINVAL;
  6086. if (HAS_IPS(dev_priv))
  6087. hsw_compute_ips_config(crtc, pipe_config);
  6088. if (pipe_config->has_pch_encoder)
  6089. return ironlake_fdi_compute_config(crtc, pipe_config);
  6090. return 0;
  6091. }
  6092. static int skylake_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6093. {
  6094. u32 cdctl;
  6095. skl_dpll0_update(dev_priv);
  6096. if (dev_priv->cdclk_pll.vco == 0)
  6097. return dev_priv->cdclk_pll.ref;
  6098. cdctl = I915_READ(CDCLK_CTL);
  6099. if (dev_priv->cdclk_pll.vco == 8640000) {
  6100. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6101. case CDCLK_FREQ_450_432:
  6102. return 432000;
  6103. case CDCLK_FREQ_337_308:
  6104. return 308571;
  6105. case CDCLK_FREQ_540:
  6106. return 540000;
  6107. case CDCLK_FREQ_675_617:
  6108. return 617143;
  6109. default:
  6110. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6111. }
  6112. } else {
  6113. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6114. case CDCLK_FREQ_450_432:
  6115. return 450000;
  6116. case CDCLK_FREQ_337_308:
  6117. return 337500;
  6118. case CDCLK_FREQ_540:
  6119. return 540000;
  6120. case CDCLK_FREQ_675_617:
  6121. return 675000;
  6122. default:
  6123. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6124. }
  6125. }
  6126. return dev_priv->cdclk_pll.ref;
  6127. }
  6128. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  6129. {
  6130. u32 val;
  6131. dev_priv->cdclk_pll.ref = 19200;
  6132. dev_priv->cdclk_pll.vco = 0;
  6133. val = I915_READ(BXT_DE_PLL_ENABLE);
  6134. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  6135. return;
  6136. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  6137. return;
  6138. val = I915_READ(BXT_DE_PLL_CTL);
  6139. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  6140. dev_priv->cdclk_pll.ref;
  6141. }
  6142. static int broxton_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6143. {
  6144. u32 divider;
  6145. int div, vco;
  6146. bxt_de_pll_update(dev_priv);
  6147. vco = dev_priv->cdclk_pll.vco;
  6148. if (vco == 0)
  6149. return dev_priv->cdclk_pll.ref;
  6150. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  6151. switch (divider) {
  6152. case BXT_CDCLK_CD2X_DIV_SEL_1:
  6153. div = 2;
  6154. break;
  6155. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  6156. WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
  6157. div = 3;
  6158. break;
  6159. case BXT_CDCLK_CD2X_DIV_SEL_2:
  6160. div = 4;
  6161. break;
  6162. case BXT_CDCLK_CD2X_DIV_SEL_4:
  6163. div = 8;
  6164. break;
  6165. default:
  6166. MISSING_CASE(divider);
  6167. return dev_priv->cdclk_pll.ref;
  6168. }
  6169. return DIV_ROUND_CLOSEST(vco, div);
  6170. }
  6171. static int broadwell_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6172. {
  6173. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6174. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6175. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6176. return 800000;
  6177. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6178. return 450000;
  6179. else if (freq == LCPLL_CLK_FREQ_450)
  6180. return 450000;
  6181. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  6182. return 540000;
  6183. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  6184. return 337500;
  6185. else
  6186. return 675000;
  6187. }
  6188. static int haswell_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6189. {
  6190. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6191. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6192. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6193. return 800000;
  6194. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6195. return 450000;
  6196. else if (freq == LCPLL_CLK_FREQ_450)
  6197. return 450000;
  6198. else if (IS_HSW_ULT(dev_priv))
  6199. return 337500;
  6200. else
  6201. return 540000;
  6202. }
  6203. static int valleyview_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6204. {
  6205. return vlv_get_cck_clock_hpll(dev_priv, "cdclk",
  6206. CCK_DISPLAY_CLOCK_CONTROL);
  6207. }
  6208. static int ilk_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6209. {
  6210. return 450000;
  6211. }
  6212. static int i945_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6213. {
  6214. return 400000;
  6215. }
  6216. static int i915_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6217. {
  6218. return 333333;
  6219. }
  6220. static int i9xx_misc_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6221. {
  6222. return 200000;
  6223. }
  6224. static int pnv_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6225. {
  6226. struct pci_dev *pdev = dev_priv->drm.pdev;
  6227. u16 gcfgc = 0;
  6228. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6229. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6230. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  6231. return 266667;
  6232. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  6233. return 333333;
  6234. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  6235. return 444444;
  6236. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  6237. return 200000;
  6238. default:
  6239. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  6240. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  6241. return 133333;
  6242. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  6243. return 166667;
  6244. }
  6245. }
  6246. static int i915gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6247. {
  6248. struct pci_dev *pdev = dev_priv->drm.pdev;
  6249. u16 gcfgc = 0;
  6250. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6251. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  6252. return 133333;
  6253. else {
  6254. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6255. case GC_DISPLAY_CLOCK_333_MHZ:
  6256. return 333333;
  6257. default:
  6258. case GC_DISPLAY_CLOCK_190_200_MHZ:
  6259. return 190000;
  6260. }
  6261. }
  6262. }
  6263. static int i865_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6264. {
  6265. return 266667;
  6266. }
  6267. static int i85x_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6268. {
  6269. struct pci_dev *pdev = dev_priv->drm.pdev;
  6270. u16 hpllcc = 0;
  6271. /*
  6272. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  6273. * encoding is different :(
  6274. * FIXME is this the right way to detect 852GM/852GMV?
  6275. */
  6276. if (pdev->revision == 0x1)
  6277. return 133333;
  6278. pci_bus_read_config_word(pdev->bus,
  6279. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  6280. /* Assume that the hardware is in the high speed state. This
  6281. * should be the default.
  6282. */
  6283. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  6284. case GC_CLOCK_133_200:
  6285. case GC_CLOCK_133_200_2:
  6286. case GC_CLOCK_100_200:
  6287. return 200000;
  6288. case GC_CLOCK_166_250:
  6289. return 250000;
  6290. case GC_CLOCK_100_133:
  6291. return 133333;
  6292. case GC_CLOCK_133_266:
  6293. case GC_CLOCK_133_266_2:
  6294. case GC_CLOCK_166_266:
  6295. return 266667;
  6296. }
  6297. /* Shouldn't happen */
  6298. return 0;
  6299. }
  6300. static int i830_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6301. {
  6302. return 133333;
  6303. }
  6304. static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
  6305. {
  6306. static const unsigned int blb_vco[8] = {
  6307. [0] = 3200000,
  6308. [1] = 4000000,
  6309. [2] = 5333333,
  6310. [3] = 4800000,
  6311. [4] = 6400000,
  6312. };
  6313. static const unsigned int pnv_vco[8] = {
  6314. [0] = 3200000,
  6315. [1] = 4000000,
  6316. [2] = 5333333,
  6317. [3] = 4800000,
  6318. [4] = 2666667,
  6319. };
  6320. static const unsigned int cl_vco[8] = {
  6321. [0] = 3200000,
  6322. [1] = 4000000,
  6323. [2] = 5333333,
  6324. [3] = 6400000,
  6325. [4] = 3333333,
  6326. [5] = 3566667,
  6327. [6] = 4266667,
  6328. };
  6329. static const unsigned int elk_vco[8] = {
  6330. [0] = 3200000,
  6331. [1] = 4000000,
  6332. [2] = 5333333,
  6333. [3] = 4800000,
  6334. };
  6335. static const unsigned int ctg_vco[8] = {
  6336. [0] = 3200000,
  6337. [1] = 4000000,
  6338. [2] = 5333333,
  6339. [3] = 6400000,
  6340. [4] = 2666667,
  6341. [5] = 4266667,
  6342. };
  6343. const unsigned int *vco_table;
  6344. unsigned int vco;
  6345. uint8_t tmp = 0;
  6346. /* FIXME other chipsets? */
  6347. if (IS_GM45(dev_priv))
  6348. vco_table = ctg_vco;
  6349. else if (IS_G4X(dev_priv))
  6350. vco_table = elk_vco;
  6351. else if (IS_I965GM(dev_priv))
  6352. vco_table = cl_vco;
  6353. else if (IS_PINEVIEW(dev_priv))
  6354. vco_table = pnv_vco;
  6355. else if (IS_G33(dev_priv))
  6356. vco_table = blb_vco;
  6357. else
  6358. return 0;
  6359. tmp = I915_READ(IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
  6360. vco = vco_table[tmp & 0x7];
  6361. if (vco == 0)
  6362. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  6363. else
  6364. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  6365. return vco;
  6366. }
  6367. static int gm45_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6368. {
  6369. struct pci_dev *pdev = dev_priv->drm.pdev;
  6370. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6371. uint16_t tmp = 0;
  6372. pci_read_config_word(pdev, GCFGC, &tmp);
  6373. cdclk_sel = (tmp >> 12) & 0x1;
  6374. switch (vco) {
  6375. case 2666667:
  6376. case 4000000:
  6377. case 5333333:
  6378. return cdclk_sel ? 333333 : 222222;
  6379. case 3200000:
  6380. return cdclk_sel ? 320000 : 228571;
  6381. default:
  6382. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  6383. return 222222;
  6384. }
  6385. }
  6386. static int i965gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6387. {
  6388. struct pci_dev *pdev = dev_priv->drm.pdev;
  6389. static const uint8_t div_3200[] = { 16, 10, 8 };
  6390. static const uint8_t div_4000[] = { 20, 12, 10 };
  6391. static const uint8_t div_5333[] = { 24, 16, 14 };
  6392. const uint8_t *div_table;
  6393. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6394. uint16_t tmp = 0;
  6395. pci_read_config_word(pdev, GCFGC, &tmp);
  6396. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  6397. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6398. goto fail;
  6399. switch (vco) {
  6400. case 3200000:
  6401. div_table = div_3200;
  6402. break;
  6403. case 4000000:
  6404. div_table = div_4000;
  6405. break;
  6406. case 5333333:
  6407. div_table = div_5333;
  6408. break;
  6409. default:
  6410. goto fail;
  6411. }
  6412. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6413. fail:
  6414. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  6415. return 200000;
  6416. }
  6417. static int g33_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6418. {
  6419. struct pci_dev *pdev = dev_priv->drm.pdev;
  6420. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  6421. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  6422. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  6423. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  6424. const uint8_t *div_table;
  6425. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6426. uint16_t tmp = 0;
  6427. pci_read_config_word(pdev, GCFGC, &tmp);
  6428. cdclk_sel = (tmp >> 4) & 0x7;
  6429. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6430. goto fail;
  6431. switch (vco) {
  6432. case 3200000:
  6433. div_table = div_3200;
  6434. break;
  6435. case 4000000:
  6436. div_table = div_4000;
  6437. break;
  6438. case 4800000:
  6439. div_table = div_4800;
  6440. break;
  6441. case 5333333:
  6442. div_table = div_5333;
  6443. break;
  6444. default:
  6445. goto fail;
  6446. }
  6447. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6448. fail:
  6449. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  6450. return 190476;
  6451. }
  6452. static void
  6453. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  6454. {
  6455. while (*num > DATA_LINK_M_N_MASK ||
  6456. *den > DATA_LINK_M_N_MASK) {
  6457. *num >>= 1;
  6458. *den >>= 1;
  6459. }
  6460. }
  6461. static void compute_m_n(unsigned int m, unsigned int n,
  6462. uint32_t *ret_m, uint32_t *ret_n)
  6463. {
  6464. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  6465. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  6466. intel_reduce_m_n_ratio(ret_m, ret_n);
  6467. }
  6468. void
  6469. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  6470. int pixel_clock, int link_clock,
  6471. struct intel_link_m_n *m_n)
  6472. {
  6473. m_n->tu = 64;
  6474. compute_m_n(bits_per_pixel * pixel_clock,
  6475. link_clock * nlanes * 8,
  6476. &m_n->gmch_m, &m_n->gmch_n);
  6477. compute_m_n(pixel_clock, link_clock,
  6478. &m_n->link_m, &m_n->link_n);
  6479. }
  6480. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  6481. {
  6482. if (i915.panel_use_ssc >= 0)
  6483. return i915.panel_use_ssc != 0;
  6484. return dev_priv->vbt.lvds_use_ssc
  6485. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  6486. }
  6487. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6488. {
  6489. return (1 << dpll->n) << 16 | dpll->m2;
  6490. }
  6491. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6492. {
  6493. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6494. }
  6495. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6496. struct intel_crtc_state *crtc_state,
  6497. struct dpll *reduced_clock)
  6498. {
  6499. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6500. u32 fp, fp2 = 0;
  6501. if (IS_PINEVIEW(dev_priv)) {
  6502. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6503. if (reduced_clock)
  6504. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6505. } else {
  6506. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6507. if (reduced_clock)
  6508. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6509. }
  6510. crtc_state->dpll_hw_state.fp0 = fp;
  6511. crtc->lowfreq_avail = false;
  6512. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6513. reduced_clock) {
  6514. crtc_state->dpll_hw_state.fp1 = fp2;
  6515. crtc->lowfreq_avail = true;
  6516. } else {
  6517. crtc_state->dpll_hw_state.fp1 = fp;
  6518. }
  6519. }
  6520. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6521. pipe)
  6522. {
  6523. u32 reg_val;
  6524. /*
  6525. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6526. * and set it to a reasonable value instead.
  6527. */
  6528. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6529. reg_val &= 0xffffff00;
  6530. reg_val |= 0x00000030;
  6531. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6532. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6533. reg_val &= 0x8cffffff;
  6534. reg_val = 0x8c000000;
  6535. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6536. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6537. reg_val &= 0xffffff00;
  6538. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6539. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6540. reg_val &= 0x00ffffff;
  6541. reg_val |= 0xb0000000;
  6542. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6543. }
  6544. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6545. struct intel_link_m_n *m_n)
  6546. {
  6547. struct drm_device *dev = crtc->base.dev;
  6548. struct drm_i915_private *dev_priv = to_i915(dev);
  6549. int pipe = crtc->pipe;
  6550. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6551. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6552. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6553. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6554. }
  6555. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6556. struct intel_link_m_n *m_n,
  6557. struct intel_link_m_n *m2_n2)
  6558. {
  6559. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6560. int pipe = crtc->pipe;
  6561. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6562. if (INTEL_GEN(dev_priv) >= 5) {
  6563. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6564. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6565. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6566. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6567. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6568. * for gen < 8) and if DRRS is supported (to make sure the
  6569. * registers are not unnecessarily accessed).
  6570. */
  6571. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  6572. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  6573. I915_WRITE(PIPE_DATA_M2(transcoder),
  6574. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6575. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6576. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6577. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6578. }
  6579. } else {
  6580. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6581. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6582. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6583. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6584. }
  6585. }
  6586. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6587. {
  6588. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6589. if (m_n == M1_N1) {
  6590. dp_m_n = &crtc->config->dp_m_n;
  6591. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6592. } else if (m_n == M2_N2) {
  6593. /*
  6594. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6595. * needs to be programmed into M1_N1.
  6596. */
  6597. dp_m_n = &crtc->config->dp_m2_n2;
  6598. } else {
  6599. DRM_ERROR("Unsupported divider value\n");
  6600. return;
  6601. }
  6602. if (crtc->config->has_pch_encoder)
  6603. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6604. else
  6605. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6606. }
  6607. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6608. struct intel_crtc_state *pipe_config)
  6609. {
  6610. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6611. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6612. if (crtc->pipe != PIPE_A)
  6613. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6614. /* DPLL not used with DSI, but still need the rest set up */
  6615. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6616. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6617. DPLL_EXT_BUFFER_ENABLE_VLV;
  6618. pipe_config->dpll_hw_state.dpll_md =
  6619. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6620. }
  6621. static void chv_compute_dpll(struct intel_crtc *crtc,
  6622. struct intel_crtc_state *pipe_config)
  6623. {
  6624. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6625. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6626. if (crtc->pipe != PIPE_A)
  6627. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6628. /* DPLL not used with DSI, but still need the rest set up */
  6629. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6630. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6631. pipe_config->dpll_hw_state.dpll_md =
  6632. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6633. }
  6634. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6635. const struct intel_crtc_state *pipe_config)
  6636. {
  6637. struct drm_device *dev = crtc->base.dev;
  6638. struct drm_i915_private *dev_priv = to_i915(dev);
  6639. enum pipe pipe = crtc->pipe;
  6640. u32 mdiv;
  6641. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6642. u32 coreclk, reg_val;
  6643. /* Enable Refclk */
  6644. I915_WRITE(DPLL(pipe),
  6645. pipe_config->dpll_hw_state.dpll &
  6646. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6647. /* No need to actually set up the DPLL with DSI */
  6648. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6649. return;
  6650. mutex_lock(&dev_priv->sb_lock);
  6651. bestn = pipe_config->dpll.n;
  6652. bestm1 = pipe_config->dpll.m1;
  6653. bestm2 = pipe_config->dpll.m2;
  6654. bestp1 = pipe_config->dpll.p1;
  6655. bestp2 = pipe_config->dpll.p2;
  6656. /* See eDP HDMI DPIO driver vbios notes doc */
  6657. /* PLL B needs special handling */
  6658. if (pipe == PIPE_B)
  6659. vlv_pllb_recal_opamp(dev_priv, pipe);
  6660. /* Set up Tx target for periodic Rcomp update */
  6661. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6662. /* Disable target IRef on PLL */
  6663. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6664. reg_val &= 0x00ffffff;
  6665. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6666. /* Disable fast lock */
  6667. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6668. /* Set idtafcrecal before PLL is enabled */
  6669. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6670. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6671. mdiv |= ((bestn << DPIO_N_SHIFT));
  6672. mdiv |= (1 << DPIO_K_SHIFT);
  6673. /*
  6674. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6675. * but we don't support that).
  6676. * Note: don't use the DAC post divider as it seems unstable.
  6677. */
  6678. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6679. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6680. mdiv |= DPIO_ENABLE_CALIBRATION;
  6681. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6682. /* Set HBR and RBR LPF coefficients */
  6683. if (pipe_config->port_clock == 162000 ||
  6684. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  6685. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  6686. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6687. 0x009f0003);
  6688. else
  6689. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6690. 0x00d0000f);
  6691. if (intel_crtc_has_dp_encoder(pipe_config)) {
  6692. /* Use SSC source */
  6693. if (pipe == PIPE_A)
  6694. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6695. 0x0df40000);
  6696. else
  6697. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6698. 0x0df70000);
  6699. } else { /* HDMI or VGA */
  6700. /* Use bend source */
  6701. if (pipe == PIPE_A)
  6702. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6703. 0x0df70000);
  6704. else
  6705. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6706. 0x0df40000);
  6707. }
  6708. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6709. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6710. if (intel_crtc_has_dp_encoder(crtc->config))
  6711. coreclk |= 0x01000000;
  6712. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6713. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6714. mutex_unlock(&dev_priv->sb_lock);
  6715. }
  6716. static void chv_prepare_pll(struct intel_crtc *crtc,
  6717. const struct intel_crtc_state *pipe_config)
  6718. {
  6719. struct drm_device *dev = crtc->base.dev;
  6720. struct drm_i915_private *dev_priv = to_i915(dev);
  6721. enum pipe pipe = crtc->pipe;
  6722. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6723. u32 loopfilter, tribuf_calcntr;
  6724. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6725. u32 dpio_val;
  6726. int vco;
  6727. /* Enable Refclk and SSC */
  6728. I915_WRITE(DPLL(pipe),
  6729. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6730. /* No need to actually set up the DPLL with DSI */
  6731. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6732. return;
  6733. bestn = pipe_config->dpll.n;
  6734. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6735. bestm1 = pipe_config->dpll.m1;
  6736. bestm2 = pipe_config->dpll.m2 >> 22;
  6737. bestp1 = pipe_config->dpll.p1;
  6738. bestp2 = pipe_config->dpll.p2;
  6739. vco = pipe_config->dpll.vco;
  6740. dpio_val = 0;
  6741. loopfilter = 0;
  6742. mutex_lock(&dev_priv->sb_lock);
  6743. /* p1 and p2 divider */
  6744. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6745. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6746. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6747. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6748. 1 << DPIO_CHV_K_DIV_SHIFT);
  6749. /* Feedback post-divider - m2 */
  6750. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6751. /* Feedback refclk divider - n and m1 */
  6752. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6753. DPIO_CHV_M1_DIV_BY_2 |
  6754. 1 << DPIO_CHV_N_DIV_SHIFT);
  6755. /* M2 fraction division */
  6756. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6757. /* M2 fraction division enable */
  6758. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6759. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6760. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6761. if (bestm2_frac)
  6762. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6763. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6764. /* Program digital lock detect threshold */
  6765. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6766. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6767. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6768. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6769. if (!bestm2_frac)
  6770. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6771. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6772. /* Loop filter */
  6773. if (vco == 5400000) {
  6774. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6775. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6776. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6777. tribuf_calcntr = 0x9;
  6778. } else if (vco <= 6200000) {
  6779. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6780. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6781. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6782. tribuf_calcntr = 0x9;
  6783. } else if (vco <= 6480000) {
  6784. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6785. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6786. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6787. tribuf_calcntr = 0x8;
  6788. } else {
  6789. /* Not supported. Apply the same limits as in the max case */
  6790. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6791. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6792. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6793. tribuf_calcntr = 0;
  6794. }
  6795. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6796. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6797. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6798. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6799. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6800. /* AFC Recal */
  6801. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6802. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6803. DPIO_AFC_RECAL);
  6804. mutex_unlock(&dev_priv->sb_lock);
  6805. }
  6806. /**
  6807. * vlv_force_pll_on - forcibly enable just the PLL
  6808. * @dev_priv: i915 private structure
  6809. * @pipe: pipe PLL to enable
  6810. * @dpll: PLL configuration
  6811. *
  6812. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6813. * in cases where we need the PLL enabled even when @pipe is not going to
  6814. * be enabled.
  6815. */
  6816. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  6817. const struct dpll *dpll)
  6818. {
  6819. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  6820. struct intel_crtc_state *pipe_config;
  6821. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6822. if (!pipe_config)
  6823. return -ENOMEM;
  6824. pipe_config->base.crtc = &crtc->base;
  6825. pipe_config->pixel_multiplier = 1;
  6826. pipe_config->dpll = *dpll;
  6827. if (IS_CHERRYVIEW(dev_priv)) {
  6828. chv_compute_dpll(crtc, pipe_config);
  6829. chv_prepare_pll(crtc, pipe_config);
  6830. chv_enable_pll(crtc, pipe_config);
  6831. } else {
  6832. vlv_compute_dpll(crtc, pipe_config);
  6833. vlv_prepare_pll(crtc, pipe_config);
  6834. vlv_enable_pll(crtc, pipe_config);
  6835. }
  6836. kfree(pipe_config);
  6837. return 0;
  6838. }
  6839. /**
  6840. * vlv_force_pll_off - forcibly disable just the PLL
  6841. * @dev_priv: i915 private structure
  6842. * @pipe: pipe PLL to disable
  6843. *
  6844. * Disable the PLL for @pipe. To be used in cases where we need
  6845. * the PLL enabled even when @pipe is not going to be enabled.
  6846. */
  6847. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  6848. {
  6849. if (IS_CHERRYVIEW(dev_priv))
  6850. chv_disable_pll(dev_priv, pipe);
  6851. else
  6852. vlv_disable_pll(dev_priv, pipe);
  6853. }
  6854. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6855. struct intel_crtc_state *crtc_state,
  6856. struct dpll *reduced_clock)
  6857. {
  6858. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6859. u32 dpll;
  6860. struct dpll *clock = &crtc_state->dpll;
  6861. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6862. dpll = DPLL_VGA_MODE_DIS;
  6863. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6864. dpll |= DPLLB_MODE_LVDS;
  6865. else
  6866. dpll |= DPLLB_MODE_DAC_SERIAL;
  6867. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6868. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6869. dpll |= (crtc_state->pixel_multiplier - 1)
  6870. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6871. }
  6872. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6873. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6874. dpll |= DPLL_SDVO_HIGH_SPEED;
  6875. if (intel_crtc_has_dp_encoder(crtc_state))
  6876. dpll |= DPLL_SDVO_HIGH_SPEED;
  6877. /* compute bitmask from p1 value */
  6878. if (IS_PINEVIEW(dev_priv))
  6879. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6880. else {
  6881. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6882. if (IS_G4X(dev_priv) && reduced_clock)
  6883. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6884. }
  6885. switch (clock->p2) {
  6886. case 5:
  6887. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6888. break;
  6889. case 7:
  6890. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6891. break;
  6892. case 10:
  6893. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6894. break;
  6895. case 14:
  6896. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6897. break;
  6898. }
  6899. if (INTEL_GEN(dev_priv) >= 4)
  6900. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6901. if (crtc_state->sdvo_tv_clock)
  6902. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6903. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6904. intel_panel_use_ssc(dev_priv))
  6905. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6906. else
  6907. dpll |= PLL_REF_INPUT_DREFCLK;
  6908. dpll |= DPLL_VCO_ENABLE;
  6909. crtc_state->dpll_hw_state.dpll = dpll;
  6910. if (INTEL_GEN(dev_priv) >= 4) {
  6911. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6912. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6913. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6914. }
  6915. }
  6916. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6917. struct intel_crtc_state *crtc_state,
  6918. struct dpll *reduced_clock)
  6919. {
  6920. struct drm_device *dev = crtc->base.dev;
  6921. struct drm_i915_private *dev_priv = to_i915(dev);
  6922. u32 dpll;
  6923. struct dpll *clock = &crtc_state->dpll;
  6924. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6925. dpll = DPLL_VGA_MODE_DIS;
  6926. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6927. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6928. } else {
  6929. if (clock->p1 == 2)
  6930. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6931. else
  6932. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6933. if (clock->p2 == 4)
  6934. dpll |= PLL_P2_DIVIDE_BY_4;
  6935. }
  6936. if (!IS_I830(dev_priv) &&
  6937. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6938. dpll |= DPLL_DVO_2X_MODE;
  6939. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6940. intel_panel_use_ssc(dev_priv))
  6941. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6942. else
  6943. dpll |= PLL_REF_INPUT_DREFCLK;
  6944. dpll |= DPLL_VCO_ENABLE;
  6945. crtc_state->dpll_hw_state.dpll = dpll;
  6946. }
  6947. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6948. {
  6949. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6950. enum pipe pipe = intel_crtc->pipe;
  6951. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6952. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6953. uint32_t crtc_vtotal, crtc_vblank_end;
  6954. int vsyncshift = 0;
  6955. /* We need to be careful not to changed the adjusted mode, for otherwise
  6956. * the hw state checker will get angry at the mismatch. */
  6957. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6958. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6959. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6960. /* the chip adds 2 halflines automatically */
  6961. crtc_vtotal -= 1;
  6962. crtc_vblank_end -= 1;
  6963. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6964. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6965. else
  6966. vsyncshift = adjusted_mode->crtc_hsync_start -
  6967. adjusted_mode->crtc_htotal / 2;
  6968. if (vsyncshift < 0)
  6969. vsyncshift += adjusted_mode->crtc_htotal;
  6970. }
  6971. if (INTEL_GEN(dev_priv) > 3)
  6972. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6973. I915_WRITE(HTOTAL(cpu_transcoder),
  6974. (adjusted_mode->crtc_hdisplay - 1) |
  6975. ((adjusted_mode->crtc_htotal - 1) << 16));
  6976. I915_WRITE(HBLANK(cpu_transcoder),
  6977. (adjusted_mode->crtc_hblank_start - 1) |
  6978. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6979. I915_WRITE(HSYNC(cpu_transcoder),
  6980. (adjusted_mode->crtc_hsync_start - 1) |
  6981. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6982. I915_WRITE(VTOTAL(cpu_transcoder),
  6983. (adjusted_mode->crtc_vdisplay - 1) |
  6984. ((crtc_vtotal - 1) << 16));
  6985. I915_WRITE(VBLANK(cpu_transcoder),
  6986. (adjusted_mode->crtc_vblank_start - 1) |
  6987. ((crtc_vblank_end - 1) << 16));
  6988. I915_WRITE(VSYNC(cpu_transcoder),
  6989. (adjusted_mode->crtc_vsync_start - 1) |
  6990. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6991. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6992. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6993. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6994. * bits. */
  6995. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  6996. (pipe == PIPE_B || pipe == PIPE_C))
  6997. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6998. }
  6999. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  7000. {
  7001. struct drm_device *dev = intel_crtc->base.dev;
  7002. struct drm_i915_private *dev_priv = to_i915(dev);
  7003. enum pipe pipe = intel_crtc->pipe;
  7004. /* pipesrc controls the size that is scaled from, which should
  7005. * always be the user's requested size.
  7006. */
  7007. I915_WRITE(PIPESRC(pipe),
  7008. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  7009. (intel_crtc->config->pipe_src_h - 1));
  7010. }
  7011. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  7012. struct intel_crtc_state *pipe_config)
  7013. {
  7014. struct drm_device *dev = crtc->base.dev;
  7015. struct drm_i915_private *dev_priv = to_i915(dev);
  7016. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  7017. uint32_t tmp;
  7018. tmp = I915_READ(HTOTAL(cpu_transcoder));
  7019. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  7020. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  7021. tmp = I915_READ(HBLANK(cpu_transcoder));
  7022. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  7023. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  7024. tmp = I915_READ(HSYNC(cpu_transcoder));
  7025. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  7026. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  7027. tmp = I915_READ(VTOTAL(cpu_transcoder));
  7028. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  7029. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  7030. tmp = I915_READ(VBLANK(cpu_transcoder));
  7031. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  7032. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  7033. tmp = I915_READ(VSYNC(cpu_transcoder));
  7034. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  7035. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  7036. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  7037. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  7038. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  7039. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  7040. }
  7041. }
  7042. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  7043. struct intel_crtc_state *pipe_config)
  7044. {
  7045. struct drm_device *dev = crtc->base.dev;
  7046. struct drm_i915_private *dev_priv = to_i915(dev);
  7047. u32 tmp;
  7048. tmp = I915_READ(PIPESRC(crtc->pipe));
  7049. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  7050. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  7051. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  7052. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  7053. }
  7054. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  7055. struct intel_crtc_state *pipe_config)
  7056. {
  7057. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  7058. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  7059. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  7060. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  7061. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  7062. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  7063. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  7064. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  7065. mode->flags = pipe_config->base.adjusted_mode.flags;
  7066. mode->type = DRM_MODE_TYPE_DRIVER;
  7067. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  7068. mode->flags |= pipe_config->base.adjusted_mode.flags;
  7069. mode->hsync = drm_mode_hsync(mode);
  7070. mode->vrefresh = drm_mode_vrefresh(mode);
  7071. drm_mode_set_name(mode);
  7072. }
  7073. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  7074. {
  7075. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  7076. uint32_t pipeconf;
  7077. pipeconf = 0;
  7078. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  7079. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  7080. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  7081. if (intel_crtc->config->double_wide)
  7082. pipeconf |= PIPECONF_DOUBLE_WIDE;
  7083. /* only g4x and later have fancy bpc/dither controls */
  7084. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7085. IS_CHERRYVIEW(dev_priv)) {
  7086. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  7087. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  7088. pipeconf |= PIPECONF_DITHER_EN |
  7089. PIPECONF_DITHER_TYPE_SP;
  7090. switch (intel_crtc->config->pipe_bpp) {
  7091. case 18:
  7092. pipeconf |= PIPECONF_6BPC;
  7093. break;
  7094. case 24:
  7095. pipeconf |= PIPECONF_8BPC;
  7096. break;
  7097. case 30:
  7098. pipeconf |= PIPECONF_10BPC;
  7099. break;
  7100. default:
  7101. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7102. BUG();
  7103. }
  7104. }
  7105. if (HAS_PIPE_CXSR(dev_priv)) {
  7106. if (intel_crtc->lowfreq_avail) {
  7107. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  7108. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  7109. } else {
  7110. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  7111. }
  7112. }
  7113. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  7114. if (INTEL_GEN(dev_priv) < 4 ||
  7115. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  7116. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  7117. else
  7118. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  7119. } else
  7120. pipeconf |= PIPECONF_PROGRESSIVE;
  7121. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7122. intel_crtc->config->limited_color_range)
  7123. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  7124. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  7125. POSTING_READ(PIPECONF(intel_crtc->pipe));
  7126. }
  7127. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  7128. struct intel_crtc_state *crtc_state)
  7129. {
  7130. struct drm_device *dev = crtc->base.dev;
  7131. struct drm_i915_private *dev_priv = to_i915(dev);
  7132. const struct intel_limit *limit;
  7133. int refclk = 48000;
  7134. memset(&crtc_state->dpll_hw_state, 0,
  7135. sizeof(crtc_state->dpll_hw_state));
  7136. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7137. if (intel_panel_use_ssc(dev_priv)) {
  7138. refclk = dev_priv->vbt.lvds_ssc_freq;
  7139. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7140. }
  7141. limit = &intel_limits_i8xx_lvds;
  7142. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  7143. limit = &intel_limits_i8xx_dvo;
  7144. } else {
  7145. limit = &intel_limits_i8xx_dac;
  7146. }
  7147. if (!crtc_state->clock_set &&
  7148. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7149. refclk, NULL, &crtc_state->dpll)) {
  7150. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7151. return -EINVAL;
  7152. }
  7153. i8xx_compute_dpll(crtc, crtc_state, NULL);
  7154. return 0;
  7155. }
  7156. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  7157. struct intel_crtc_state *crtc_state)
  7158. {
  7159. struct drm_device *dev = crtc->base.dev;
  7160. struct drm_i915_private *dev_priv = to_i915(dev);
  7161. const struct intel_limit *limit;
  7162. int refclk = 96000;
  7163. memset(&crtc_state->dpll_hw_state, 0,
  7164. sizeof(crtc_state->dpll_hw_state));
  7165. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7166. if (intel_panel_use_ssc(dev_priv)) {
  7167. refclk = dev_priv->vbt.lvds_ssc_freq;
  7168. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7169. }
  7170. if (intel_is_dual_link_lvds(dev))
  7171. limit = &intel_limits_g4x_dual_channel_lvds;
  7172. else
  7173. limit = &intel_limits_g4x_single_channel_lvds;
  7174. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  7175. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  7176. limit = &intel_limits_g4x_hdmi;
  7177. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  7178. limit = &intel_limits_g4x_sdvo;
  7179. } else {
  7180. /* The option is for other outputs */
  7181. limit = &intel_limits_i9xx_sdvo;
  7182. }
  7183. if (!crtc_state->clock_set &&
  7184. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7185. refclk, NULL, &crtc_state->dpll)) {
  7186. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7187. return -EINVAL;
  7188. }
  7189. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7190. return 0;
  7191. }
  7192. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  7193. struct intel_crtc_state *crtc_state)
  7194. {
  7195. struct drm_device *dev = crtc->base.dev;
  7196. struct drm_i915_private *dev_priv = to_i915(dev);
  7197. const struct intel_limit *limit;
  7198. int refclk = 96000;
  7199. memset(&crtc_state->dpll_hw_state, 0,
  7200. sizeof(crtc_state->dpll_hw_state));
  7201. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7202. if (intel_panel_use_ssc(dev_priv)) {
  7203. refclk = dev_priv->vbt.lvds_ssc_freq;
  7204. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7205. }
  7206. limit = &intel_limits_pineview_lvds;
  7207. } else {
  7208. limit = &intel_limits_pineview_sdvo;
  7209. }
  7210. if (!crtc_state->clock_set &&
  7211. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7212. refclk, NULL, &crtc_state->dpll)) {
  7213. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7214. return -EINVAL;
  7215. }
  7216. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7217. return 0;
  7218. }
  7219. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  7220. struct intel_crtc_state *crtc_state)
  7221. {
  7222. struct drm_device *dev = crtc->base.dev;
  7223. struct drm_i915_private *dev_priv = to_i915(dev);
  7224. const struct intel_limit *limit;
  7225. int refclk = 96000;
  7226. memset(&crtc_state->dpll_hw_state, 0,
  7227. sizeof(crtc_state->dpll_hw_state));
  7228. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7229. if (intel_panel_use_ssc(dev_priv)) {
  7230. refclk = dev_priv->vbt.lvds_ssc_freq;
  7231. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7232. }
  7233. limit = &intel_limits_i9xx_lvds;
  7234. } else {
  7235. limit = &intel_limits_i9xx_sdvo;
  7236. }
  7237. if (!crtc_state->clock_set &&
  7238. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7239. refclk, NULL, &crtc_state->dpll)) {
  7240. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7241. return -EINVAL;
  7242. }
  7243. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7244. return 0;
  7245. }
  7246. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  7247. struct intel_crtc_state *crtc_state)
  7248. {
  7249. int refclk = 100000;
  7250. const struct intel_limit *limit = &intel_limits_chv;
  7251. memset(&crtc_state->dpll_hw_state, 0,
  7252. sizeof(crtc_state->dpll_hw_state));
  7253. if (!crtc_state->clock_set &&
  7254. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7255. refclk, NULL, &crtc_state->dpll)) {
  7256. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7257. return -EINVAL;
  7258. }
  7259. chv_compute_dpll(crtc, crtc_state);
  7260. return 0;
  7261. }
  7262. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  7263. struct intel_crtc_state *crtc_state)
  7264. {
  7265. int refclk = 100000;
  7266. const struct intel_limit *limit = &intel_limits_vlv;
  7267. memset(&crtc_state->dpll_hw_state, 0,
  7268. sizeof(crtc_state->dpll_hw_state));
  7269. if (!crtc_state->clock_set &&
  7270. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7271. refclk, NULL, &crtc_state->dpll)) {
  7272. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7273. return -EINVAL;
  7274. }
  7275. vlv_compute_dpll(crtc, crtc_state);
  7276. return 0;
  7277. }
  7278. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  7279. struct intel_crtc_state *pipe_config)
  7280. {
  7281. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7282. uint32_t tmp;
  7283. if (INTEL_GEN(dev_priv) <= 3 &&
  7284. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  7285. return;
  7286. tmp = I915_READ(PFIT_CONTROL);
  7287. if (!(tmp & PFIT_ENABLE))
  7288. return;
  7289. /* Check whether the pfit is attached to our pipe. */
  7290. if (INTEL_GEN(dev_priv) < 4) {
  7291. if (crtc->pipe != PIPE_B)
  7292. return;
  7293. } else {
  7294. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  7295. return;
  7296. }
  7297. pipe_config->gmch_pfit.control = tmp;
  7298. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  7299. }
  7300. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  7301. struct intel_crtc_state *pipe_config)
  7302. {
  7303. struct drm_device *dev = crtc->base.dev;
  7304. struct drm_i915_private *dev_priv = to_i915(dev);
  7305. int pipe = pipe_config->cpu_transcoder;
  7306. struct dpll clock;
  7307. u32 mdiv;
  7308. int refclk = 100000;
  7309. /* In case of DSI, DPLL will not be used */
  7310. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7311. return;
  7312. mutex_lock(&dev_priv->sb_lock);
  7313. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  7314. mutex_unlock(&dev_priv->sb_lock);
  7315. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  7316. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  7317. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  7318. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  7319. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  7320. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  7321. }
  7322. static void
  7323. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  7324. struct intel_initial_plane_config *plane_config)
  7325. {
  7326. struct drm_device *dev = crtc->base.dev;
  7327. struct drm_i915_private *dev_priv = to_i915(dev);
  7328. u32 val, base, offset;
  7329. int pipe = crtc->pipe, plane = crtc->plane;
  7330. int fourcc, pixel_format;
  7331. unsigned int aligned_height;
  7332. struct drm_framebuffer *fb;
  7333. struct intel_framebuffer *intel_fb;
  7334. val = I915_READ(DSPCNTR(plane));
  7335. if (!(val & DISPLAY_PLANE_ENABLE))
  7336. return;
  7337. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7338. if (!intel_fb) {
  7339. DRM_DEBUG_KMS("failed to alloc fb\n");
  7340. return;
  7341. }
  7342. fb = &intel_fb->base;
  7343. fb->dev = dev;
  7344. if (INTEL_GEN(dev_priv) >= 4) {
  7345. if (val & DISPPLANE_TILED) {
  7346. plane_config->tiling = I915_TILING_X;
  7347. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7348. }
  7349. }
  7350. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7351. fourcc = i9xx_format_to_fourcc(pixel_format);
  7352. fb->format = drm_format_info(fourcc);
  7353. if (INTEL_GEN(dev_priv) >= 4) {
  7354. if (plane_config->tiling)
  7355. offset = I915_READ(DSPTILEOFF(plane));
  7356. else
  7357. offset = I915_READ(DSPLINOFF(plane));
  7358. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  7359. } else {
  7360. base = I915_READ(DSPADDR(plane));
  7361. }
  7362. plane_config->base = base;
  7363. val = I915_READ(PIPESRC(pipe));
  7364. fb->width = ((val >> 16) & 0xfff) + 1;
  7365. fb->height = ((val >> 0) & 0xfff) + 1;
  7366. val = I915_READ(DSPSTRIDE(pipe));
  7367. fb->pitches[0] = val & 0xffffffc0;
  7368. aligned_height = intel_fb_align_height(dev, fb->height,
  7369. fb->format->format,
  7370. fb->modifier);
  7371. plane_config->size = fb->pitches[0] * aligned_height;
  7372. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7373. pipe_name(pipe), plane, fb->width, fb->height,
  7374. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7375. plane_config->size);
  7376. plane_config->fb = intel_fb;
  7377. }
  7378. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  7379. struct intel_crtc_state *pipe_config)
  7380. {
  7381. struct drm_device *dev = crtc->base.dev;
  7382. struct drm_i915_private *dev_priv = to_i915(dev);
  7383. int pipe = pipe_config->cpu_transcoder;
  7384. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  7385. struct dpll clock;
  7386. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  7387. int refclk = 100000;
  7388. /* In case of DSI, DPLL will not be used */
  7389. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7390. return;
  7391. mutex_lock(&dev_priv->sb_lock);
  7392. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  7393. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  7394. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  7395. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  7396. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  7397. mutex_unlock(&dev_priv->sb_lock);
  7398. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  7399. clock.m2 = (pll_dw0 & 0xff) << 22;
  7400. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  7401. clock.m2 |= pll_dw2 & 0x3fffff;
  7402. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  7403. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  7404. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  7405. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  7406. }
  7407. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  7408. struct intel_crtc_state *pipe_config)
  7409. {
  7410. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7411. enum intel_display_power_domain power_domain;
  7412. uint32_t tmp;
  7413. bool ret;
  7414. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7415. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7416. return false;
  7417. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7418. pipe_config->shared_dpll = NULL;
  7419. ret = false;
  7420. tmp = I915_READ(PIPECONF(crtc->pipe));
  7421. if (!(tmp & PIPECONF_ENABLE))
  7422. goto out;
  7423. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7424. IS_CHERRYVIEW(dev_priv)) {
  7425. switch (tmp & PIPECONF_BPC_MASK) {
  7426. case PIPECONF_6BPC:
  7427. pipe_config->pipe_bpp = 18;
  7428. break;
  7429. case PIPECONF_8BPC:
  7430. pipe_config->pipe_bpp = 24;
  7431. break;
  7432. case PIPECONF_10BPC:
  7433. pipe_config->pipe_bpp = 30;
  7434. break;
  7435. default:
  7436. break;
  7437. }
  7438. }
  7439. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7440. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  7441. pipe_config->limited_color_range = true;
  7442. if (INTEL_GEN(dev_priv) < 4)
  7443. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  7444. intel_get_pipe_timings(crtc, pipe_config);
  7445. intel_get_pipe_src_size(crtc, pipe_config);
  7446. i9xx_get_pfit_config(crtc, pipe_config);
  7447. if (INTEL_GEN(dev_priv) >= 4) {
  7448. /* No way to read it out on pipes B and C */
  7449. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  7450. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  7451. else
  7452. tmp = I915_READ(DPLL_MD(crtc->pipe));
  7453. pipe_config->pixel_multiplier =
  7454. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  7455. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  7456. pipe_config->dpll_hw_state.dpll_md = tmp;
  7457. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  7458. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  7459. tmp = I915_READ(DPLL(crtc->pipe));
  7460. pipe_config->pixel_multiplier =
  7461. ((tmp & SDVO_MULTIPLIER_MASK)
  7462. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  7463. } else {
  7464. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  7465. * port and will be fixed up in the encoder->get_config
  7466. * function. */
  7467. pipe_config->pixel_multiplier = 1;
  7468. }
  7469. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  7470. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  7471. /*
  7472. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  7473. * on 830. Filter it out here so that we don't
  7474. * report errors due to that.
  7475. */
  7476. if (IS_I830(dev_priv))
  7477. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  7478. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  7479. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  7480. } else {
  7481. /* Mask out read-only status bits. */
  7482. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  7483. DPLL_PORTC_READY_MASK |
  7484. DPLL_PORTB_READY_MASK);
  7485. }
  7486. if (IS_CHERRYVIEW(dev_priv))
  7487. chv_crtc_clock_get(crtc, pipe_config);
  7488. else if (IS_VALLEYVIEW(dev_priv))
  7489. vlv_crtc_clock_get(crtc, pipe_config);
  7490. else
  7491. i9xx_crtc_clock_get(crtc, pipe_config);
  7492. /*
  7493. * Normally the dotclock is filled in by the encoder .get_config()
  7494. * but in case the pipe is enabled w/o any ports we need a sane
  7495. * default.
  7496. */
  7497. pipe_config->base.adjusted_mode.crtc_clock =
  7498. pipe_config->port_clock / pipe_config->pixel_multiplier;
  7499. ret = true;
  7500. out:
  7501. intel_display_power_put(dev_priv, power_domain);
  7502. return ret;
  7503. }
  7504. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  7505. {
  7506. struct intel_encoder *encoder;
  7507. int i;
  7508. u32 val, final;
  7509. bool has_lvds = false;
  7510. bool has_cpu_edp = false;
  7511. bool has_panel = false;
  7512. bool has_ck505 = false;
  7513. bool can_ssc = false;
  7514. bool using_ssc_source = false;
  7515. /* We need to take the global config into account */
  7516. for_each_intel_encoder(&dev_priv->drm, encoder) {
  7517. switch (encoder->type) {
  7518. case INTEL_OUTPUT_LVDS:
  7519. has_panel = true;
  7520. has_lvds = true;
  7521. break;
  7522. case INTEL_OUTPUT_EDP:
  7523. has_panel = true;
  7524. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7525. has_cpu_edp = true;
  7526. break;
  7527. default:
  7528. break;
  7529. }
  7530. }
  7531. if (HAS_PCH_IBX(dev_priv)) {
  7532. has_ck505 = dev_priv->vbt.display_clock_mode;
  7533. can_ssc = has_ck505;
  7534. } else {
  7535. has_ck505 = false;
  7536. can_ssc = true;
  7537. }
  7538. /* Check if any DPLLs are using the SSC source */
  7539. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7540. u32 temp = I915_READ(PCH_DPLL(i));
  7541. if (!(temp & DPLL_VCO_ENABLE))
  7542. continue;
  7543. if ((temp & PLL_REF_INPUT_MASK) ==
  7544. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7545. using_ssc_source = true;
  7546. break;
  7547. }
  7548. }
  7549. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7550. has_panel, has_lvds, has_ck505, using_ssc_source);
  7551. /* Ironlake: try to setup display ref clock before DPLL
  7552. * enabling. This is only under driver's control after
  7553. * PCH B stepping, previous chipset stepping should be
  7554. * ignoring this setting.
  7555. */
  7556. val = I915_READ(PCH_DREF_CONTROL);
  7557. /* As we must carefully and slowly disable/enable each source in turn,
  7558. * compute the final state we want first and check if we need to
  7559. * make any changes at all.
  7560. */
  7561. final = val;
  7562. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7563. if (has_ck505)
  7564. final |= DREF_NONSPREAD_CK505_ENABLE;
  7565. else
  7566. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7567. final &= ~DREF_SSC_SOURCE_MASK;
  7568. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7569. final &= ~DREF_SSC1_ENABLE;
  7570. if (has_panel) {
  7571. final |= DREF_SSC_SOURCE_ENABLE;
  7572. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7573. final |= DREF_SSC1_ENABLE;
  7574. if (has_cpu_edp) {
  7575. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7576. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7577. else
  7578. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7579. } else
  7580. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7581. } else if (using_ssc_source) {
  7582. final |= DREF_SSC_SOURCE_ENABLE;
  7583. final |= DREF_SSC1_ENABLE;
  7584. }
  7585. if (final == val)
  7586. return;
  7587. /* Always enable nonspread source */
  7588. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7589. if (has_ck505)
  7590. val |= DREF_NONSPREAD_CK505_ENABLE;
  7591. else
  7592. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7593. if (has_panel) {
  7594. val &= ~DREF_SSC_SOURCE_MASK;
  7595. val |= DREF_SSC_SOURCE_ENABLE;
  7596. /* SSC must be turned on before enabling the CPU output */
  7597. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7598. DRM_DEBUG_KMS("Using SSC on panel\n");
  7599. val |= DREF_SSC1_ENABLE;
  7600. } else
  7601. val &= ~DREF_SSC1_ENABLE;
  7602. /* Get SSC going before enabling the outputs */
  7603. I915_WRITE(PCH_DREF_CONTROL, val);
  7604. POSTING_READ(PCH_DREF_CONTROL);
  7605. udelay(200);
  7606. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7607. /* Enable CPU source on CPU attached eDP */
  7608. if (has_cpu_edp) {
  7609. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7610. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7611. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7612. } else
  7613. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7614. } else
  7615. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7616. I915_WRITE(PCH_DREF_CONTROL, val);
  7617. POSTING_READ(PCH_DREF_CONTROL);
  7618. udelay(200);
  7619. } else {
  7620. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7621. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7622. /* Turn off CPU output */
  7623. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7624. I915_WRITE(PCH_DREF_CONTROL, val);
  7625. POSTING_READ(PCH_DREF_CONTROL);
  7626. udelay(200);
  7627. if (!using_ssc_source) {
  7628. DRM_DEBUG_KMS("Disabling SSC source\n");
  7629. /* Turn off the SSC source */
  7630. val &= ~DREF_SSC_SOURCE_MASK;
  7631. val |= DREF_SSC_SOURCE_DISABLE;
  7632. /* Turn off SSC1 */
  7633. val &= ~DREF_SSC1_ENABLE;
  7634. I915_WRITE(PCH_DREF_CONTROL, val);
  7635. POSTING_READ(PCH_DREF_CONTROL);
  7636. udelay(200);
  7637. }
  7638. }
  7639. BUG_ON(val != final);
  7640. }
  7641. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7642. {
  7643. uint32_t tmp;
  7644. tmp = I915_READ(SOUTH_CHICKEN2);
  7645. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7646. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7647. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  7648. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7649. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7650. tmp = I915_READ(SOUTH_CHICKEN2);
  7651. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7652. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7653. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  7654. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7655. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7656. }
  7657. /* WaMPhyProgramming:hsw */
  7658. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7659. {
  7660. uint32_t tmp;
  7661. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7662. tmp &= ~(0xFF << 24);
  7663. tmp |= (0x12 << 24);
  7664. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7665. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7666. tmp |= (1 << 11);
  7667. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7668. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7669. tmp |= (1 << 11);
  7670. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7671. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7672. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7673. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7674. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7675. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7676. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7677. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7678. tmp &= ~(7 << 13);
  7679. tmp |= (5 << 13);
  7680. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7681. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7682. tmp &= ~(7 << 13);
  7683. tmp |= (5 << 13);
  7684. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7685. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7686. tmp &= ~0xFF;
  7687. tmp |= 0x1C;
  7688. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7689. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7690. tmp &= ~0xFF;
  7691. tmp |= 0x1C;
  7692. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7693. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7694. tmp &= ~(0xFF << 16);
  7695. tmp |= (0x1C << 16);
  7696. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7697. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7698. tmp &= ~(0xFF << 16);
  7699. tmp |= (0x1C << 16);
  7700. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7701. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7702. tmp |= (1 << 27);
  7703. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7704. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7705. tmp |= (1 << 27);
  7706. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7707. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7708. tmp &= ~(0xF << 28);
  7709. tmp |= (4 << 28);
  7710. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7711. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7712. tmp &= ~(0xF << 28);
  7713. tmp |= (4 << 28);
  7714. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7715. }
  7716. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7717. * Programming" based on the parameters passed:
  7718. * - Sequence to enable CLKOUT_DP
  7719. * - Sequence to enable CLKOUT_DP without spread
  7720. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7721. */
  7722. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  7723. bool with_spread, bool with_fdi)
  7724. {
  7725. uint32_t reg, tmp;
  7726. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7727. with_spread = true;
  7728. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  7729. with_fdi, "LP PCH doesn't have FDI\n"))
  7730. with_fdi = false;
  7731. mutex_lock(&dev_priv->sb_lock);
  7732. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7733. tmp &= ~SBI_SSCCTL_DISABLE;
  7734. tmp |= SBI_SSCCTL_PATHALT;
  7735. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7736. udelay(24);
  7737. if (with_spread) {
  7738. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7739. tmp &= ~SBI_SSCCTL_PATHALT;
  7740. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7741. if (with_fdi) {
  7742. lpt_reset_fdi_mphy(dev_priv);
  7743. lpt_program_fdi_mphy(dev_priv);
  7744. }
  7745. }
  7746. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7747. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7748. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7749. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7750. mutex_unlock(&dev_priv->sb_lock);
  7751. }
  7752. /* Sequence to disable CLKOUT_DP */
  7753. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  7754. {
  7755. uint32_t reg, tmp;
  7756. mutex_lock(&dev_priv->sb_lock);
  7757. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7758. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7759. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7760. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7761. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7762. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7763. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7764. tmp |= SBI_SSCCTL_PATHALT;
  7765. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7766. udelay(32);
  7767. }
  7768. tmp |= SBI_SSCCTL_DISABLE;
  7769. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7770. }
  7771. mutex_unlock(&dev_priv->sb_lock);
  7772. }
  7773. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7774. static const uint16_t sscdivintphase[] = {
  7775. [BEND_IDX( 50)] = 0x3B23,
  7776. [BEND_IDX( 45)] = 0x3B23,
  7777. [BEND_IDX( 40)] = 0x3C23,
  7778. [BEND_IDX( 35)] = 0x3C23,
  7779. [BEND_IDX( 30)] = 0x3D23,
  7780. [BEND_IDX( 25)] = 0x3D23,
  7781. [BEND_IDX( 20)] = 0x3E23,
  7782. [BEND_IDX( 15)] = 0x3E23,
  7783. [BEND_IDX( 10)] = 0x3F23,
  7784. [BEND_IDX( 5)] = 0x3F23,
  7785. [BEND_IDX( 0)] = 0x0025,
  7786. [BEND_IDX( -5)] = 0x0025,
  7787. [BEND_IDX(-10)] = 0x0125,
  7788. [BEND_IDX(-15)] = 0x0125,
  7789. [BEND_IDX(-20)] = 0x0225,
  7790. [BEND_IDX(-25)] = 0x0225,
  7791. [BEND_IDX(-30)] = 0x0325,
  7792. [BEND_IDX(-35)] = 0x0325,
  7793. [BEND_IDX(-40)] = 0x0425,
  7794. [BEND_IDX(-45)] = 0x0425,
  7795. [BEND_IDX(-50)] = 0x0525,
  7796. };
  7797. /*
  7798. * Bend CLKOUT_DP
  7799. * steps -50 to 50 inclusive, in steps of 5
  7800. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7801. * change in clock period = -(steps / 10) * 5.787 ps
  7802. */
  7803. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7804. {
  7805. uint32_t tmp;
  7806. int idx = BEND_IDX(steps);
  7807. if (WARN_ON(steps % 5 != 0))
  7808. return;
  7809. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7810. return;
  7811. mutex_lock(&dev_priv->sb_lock);
  7812. if (steps % 10 != 0)
  7813. tmp = 0xAAAAAAAB;
  7814. else
  7815. tmp = 0x00000000;
  7816. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7817. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7818. tmp &= 0xffff0000;
  7819. tmp |= sscdivintphase[idx];
  7820. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7821. mutex_unlock(&dev_priv->sb_lock);
  7822. }
  7823. #undef BEND_IDX
  7824. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  7825. {
  7826. struct intel_encoder *encoder;
  7827. bool has_vga = false;
  7828. for_each_intel_encoder(&dev_priv->drm, encoder) {
  7829. switch (encoder->type) {
  7830. case INTEL_OUTPUT_ANALOG:
  7831. has_vga = true;
  7832. break;
  7833. default:
  7834. break;
  7835. }
  7836. }
  7837. if (has_vga) {
  7838. lpt_bend_clkout_dp(dev_priv, 0);
  7839. lpt_enable_clkout_dp(dev_priv, true, true);
  7840. } else {
  7841. lpt_disable_clkout_dp(dev_priv);
  7842. }
  7843. }
  7844. /*
  7845. * Initialize reference clocks when the driver loads
  7846. */
  7847. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  7848. {
  7849. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  7850. ironlake_init_pch_refclk(dev_priv);
  7851. else if (HAS_PCH_LPT(dev_priv))
  7852. lpt_init_pch_refclk(dev_priv);
  7853. }
  7854. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7855. {
  7856. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7857. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7858. int pipe = intel_crtc->pipe;
  7859. uint32_t val;
  7860. val = 0;
  7861. switch (intel_crtc->config->pipe_bpp) {
  7862. case 18:
  7863. val |= PIPECONF_6BPC;
  7864. break;
  7865. case 24:
  7866. val |= PIPECONF_8BPC;
  7867. break;
  7868. case 30:
  7869. val |= PIPECONF_10BPC;
  7870. break;
  7871. case 36:
  7872. val |= PIPECONF_12BPC;
  7873. break;
  7874. default:
  7875. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7876. BUG();
  7877. }
  7878. if (intel_crtc->config->dither)
  7879. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7880. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7881. val |= PIPECONF_INTERLACED_ILK;
  7882. else
  7883. val |= PIPECONF_PROGRESSIVE;
  7884. if (intel_crtc->config->limited_color_range)
  7885. val |= PIPECONF_COLOR_RANGE_SELECT;
  7886. I915_WRITE(PIPECONF(pipe), val);
  7887. POSTING_READ(PIPECONF(pipe));
  7888. }
  7889. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7890. {
  7891. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7892. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7893. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7894. u32 val = 0;
  7895. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7896. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7897. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7898. val |= PIPECONF_INTERLACED_ILK;
  7899. else
  7900. val |= PIPECONF_PROGRESSIVE;
  7901. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7902. POSTING_READ(PIPECONF(cpu_transcoder));
  7903. }
  7904. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7905. {
  7906. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7907. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7908. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7909. u32 val = 0;
  7910. switch (intel_crtc->config->pipe_bpp) {
  7911. case 18:
  7912. val |= PIPEMISC_DITHER_6_BPC;
  7913. break;
  7914. case 24:
  7915. val |= PIPEMISC_DITHER_8_BPC;
  7916. break;
  7917. case 30:
  7918. val |= PIPEMISC_DITHER_10_BPC;
  7919. break;
  7920. case 36:
  7921. val |= PIPEMISC_DITHER_12_BPC;
  7922. break;
  7923. default:
  7924. /* Case prevented by pipe_config_set_bpp. */
  7925. BUG();
  7926. }
  7927. if (intel_crtc->config->dither)
  7928. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7929. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7930. }
  7931. }
  7932. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7933. {
  7934. /*
  7935. * Account for spread spectrum to avoid
  7936. * oversubscribing the link. Max center spread
  7937. * is 2.5%; use 5% for safety's sake.
  7938. */
  7939. u32 bps = target_clock * bpp * 21 / 20;
  7940. return DIV_ROUND_UP(bps, link_bw * 8);
  7941. }
  7942. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7943. {
  7944. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7945. }
  7946. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7947. struct intel_crtc_state *crtc_state,
  7948. struct dpll *reduced_clock)
  7949. {
  7950. struct drm_crtc *crtc = &intel_crtc->base;
  7951. struct drm_device *dev = crtc->dev;
  7952. struct drm_i915_private *dev_priv = to_i915(dev);
  7953. u32 dpll, fp, fp2;
  7954. int factor;
  7955. /* Enable autotuning of the PLL clock (if permissible) */
  7956. factor = 21;
  7957. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7958. if ((intel_panel_use_ssc(dev_priv) &&
  7959. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7960. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  7961. factor = 25;
  7962. } else if (crtc_state->sdvo_tv_clock)
  7963. factor = 20;
  7964. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7965. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7966. fp |= FP_CB_TUNE;
  7967. if (reduced_clock) {
  7968. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7969. if (reduced_clock->m < factor * reduced_clock->n)
  7970. fp2 |= FP_CB_TUNE;
  7971. } else {
  7972. fp2 = fp;
  7973. }
  7974. dpll = 0;
  7975. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7976. dpll |= DPLLB_MODE_LVDS;
  7977. else
  7978. dpll |= DPLLB_MODE_DAC_SERIAL;
  7979. dpll |= (crtc_state->pixel_multiplier - 1)
  7980. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7981. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7982. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7983. dpll |= DPLL_SDVO_HIGH_SPEED;
  7984. if (intel_crtc_has_dp_encoder(crtc_state))
  7985. dpll |= DPLL_SDVO_HIGH_SPEED;
  7986. /*
  7987. * The high speed IO clock is only really required for
  7988. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  7989. * possible to share the DPLL between CRT and HDMI. Enabling
  7990. * the clock needlessly does no real harm, except use up a
  7991. * bit of power potentially.
  7992. *
  7993. * We'll limit this to IVB with 3 pipes, since it has only two
  7994. * DPLLs and so DPLL sharing is the only way to get three pipes
  7995. * driving PCH ports at the same time. On SNB we could do this,
  7996. * and potentially avoid enabling the second DPLL, but it's not
  7997. * clear if it''s a win or loss power wise. No point in doing
  7998. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  7999. */
  8000. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  8001. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  8002. dpll |= DPLL_SDVO_HIGH_SPEED;
  8003. /* compute bitmask from p1 value */
  8004. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  8005. /* also FPA1 */
  8006. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  8007. switch (crtc_state->dpll.p2) {
  8008. case 5:
  8009. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  8010. break;
  8011. case 7:
  8012. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  8013. break;
  8014. case 10:
  8015. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  8016. break;
  8017. case 14:
  8018. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  8019. break;
  8020. }
  8021. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8022. intel_panel_use_ssc(dev_priv))
  8023. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  8024. else
  8025. dpll |= PLL_REF_INPUT_DREFCLK;
  8026. dpll |= DPLL_VCO_ENABLE;
  8027. crtc_state->dpll_hw_state.dpll = dpll;
  8028. crtc_state->dpll_hw_state.fp0 = fp;
  8029. crtc_state->dpll_hw_state.fp1 = fp2;
  8030. }
  8031. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  8032. struct intel_crtc_state *crtc_state)
  8033. {
  8034. struct drm_device *dev = crtc->base.dev;
  8035. struct drm_i915_private *dev_priv = to_i915(dev);
  8036. struct dpll reduced_clock;
  8037. bool has_reduced_clock = false;
  8038. struct intel_shared_dpll *pll;
  8039. const struct intel_limit *limit;
  8040. int refclk = 120000;
  8041. memset(&crtc_state->dpll_hw_state, 0,
  8042. sizeof(crtc_state->dpll_hw_state));
  8043. crtc->lowfreq_avail = false;
  8044. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  8045. if (!crtc_state->has_pch_encoder)
  8046. return 0;
  8047. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  8048. if (intel_panel_use_ssc(dev_priv)) {
  8049. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  8050. dev_priv->vbt.lvds_ssc_freq);
  8051. refclk = dev_priv->vbt.lvds_ssc_freq;
  8052. }
  8053. if (intel_is_dual_link_lvds(dev)) {
  8054. if (refclk == 100000)
  8055. limit = &intel_limits_ironlake_dual_lvds_100m;
  8056. else
  8057. limit = &intel_limits_ironlake_dual_lvds;
  8058. } else {
  8059. if (refclk == 100000)
  8060. limit = &intel_limits_ironlake_single_lvds_100m;
  8061. else
  8062. limit = &intel_limits_ironlake_single_lvds;
  8063. }
  8064. } else {
  8065. limit = &intel_limits_ironlake_dac;
  8066. }
  8067. if (!crtc_state->clock_set &&
  8068. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  8069. refclk, NULL, &crtc_state->dpll)) {
  8070. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  8071. return -EINVAL;
  8072. }
  8073. ironlake_compute_dpll(crtc, crtc_state,
  8074. has_reduced_clock ? &reduced_clock : NULL);
  8075. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  8076. if (pll == NULL) {
  8077. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  8078. pipe_name(crtc->pipe));
  8079. return -EINVAL;
  8080. }
  8081. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8082. has_reduced_clock)
  8083. crtc->lowfreq_avail = true;
  8084. return 0;
  8085. }
  8086. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  8087. struct intel_link_m_n *m_n)
  8088. {
  8089. struct drm_device *dev = crtc->base.dev;
  8090. struct drm_i915_private *dev_priv = to_i915(dev);
  8091. enum pipe pipe = crtc->pipe;
  8092. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  8093. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  8094. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  8095. & ~TU_SIZE_MASK;
  8096. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  8097. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  8098. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8099. }
  8100. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  8101. enum transcoder transcoder,
  8102. struct intel_link_m_n *m_n,
  8103. struct intel_link_m_n *m2_n2)
  8104. {
  8105. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8106. enum pipe pipe = crtc->pipe;
  8107. if (INTEL_GEN(dev_priv) >= 5) {
  8108. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  8109. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  8110. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  8111. & ~TU_SIZE_MASK;
  8112. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  8113. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  8114. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8115. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  8116. * gen < 8) and if DRRS is supported (to make sure the
  8117. * registers are not unnecessarily read).
  8118. */
  8119. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  8120. crtc->config->has_drrs) {
  8121. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  8122. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  8123. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  8124. & ~TU_SIZE_MASK;
  8125. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  8126. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  8127. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8128. }
  8129. } else {
  8130. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  8131. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  8132. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  8133. & ~TU_SIZE_MASK;
  8134. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  8135. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  8136. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8137. }
  8138. }
  8139. void intel_dp_get_m_n(struct intel_crtc *crtc,
  8140. struct intel_crtc_state *pipe_config)
  8141. {
  8142. if (pipe_config->has_pch_encoder)
  8143. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  8144. else
  8145. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8146. &pipe_config->dp_m_n,
  8147. &pipe_config->dp_m2_n2);
  8148. }
  8149. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  8150. struct intel_crtc_state *pipe_config)
  8151. {
  8152. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8153. &pipe_config->fdi_m_n, NULL);
  8154. }
  8155. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  8156. struct intel_crtc_state *pipe_config)
  8157. {
  8158. struct drm_device *dev = crtc->base.dev;
  8159. struct drm_i915_private *dev_priv = to_i915(dev);
  8160. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  8161. uint32_t ps_ctrl = 0;
  8162. int id = -1;
  8163. int i;
  8164. /* find scaler attached to this pipe */
  8165. for (i = 0; i < crtc->num_scalers; i++) {
  8166. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  8167. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  8168. id = i;
  8169. pipe_config->pch_pfit.enabled = true;
  8170. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  8171. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  8172. break;
  8173. }
  8174. }
  8175. scaler_state->scaler_id = id;
  8176. if (id >= 0) {
  8177. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  8178. } else {
  8179. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8180. }
  8181. }
  8182. static void
  8183. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  8184. struct intel_initial_plane_config *plane_config)
  8185. {
  8186. struct drm_device *dev = crtc->base.dev;
  8187. struct drm_i915_private *dev_priv = to_i915(dev);
  8188. u32 val, base, offset, stride_mult, tiling;
  8189. int pipe = crtc->pipe;
  8190. int fourcc, pixel_format;
  8191. unsigned int aligned_height;
  8192. struct drm_framebuffer *fb;
  8193. struct intel_framebuffer *intel_fb;
  8194. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8195. if (!intel_fb) {
  8196. DRM_DEBUG_KMS("failed to alloc fb\n");
  8197. return;
  8198. }
  8199. fb = &intel_fb->base;
  8200. fb->dev = dev;
  8201. val = I915_READ(PLANE_CTL(pipe, 0));
  8202. if (!(val & PLANE_CTL_ENABLE))
  8203. goto error;
  8204. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  8205. fourcc = skl_format_to_fourcc(pixel_format,
  8206. val & PLANE_CTL_ORDER_RGBX,
  8207. val & PLANE_CTL_ALPHA_MASK);
  8208. fb->format = drm_format_info(fourcc);
  8209. tiling = val & PLANE_CTL_TILED_MASK;
  8210. switch (tiling) {
  8211. case PLANE_CTL_TILED_LINEAR:
  8212. fb->modifier = DRM_FORMAT_MOD_NONE;
  8213. break;
  8214. case PLANE_CTL_TILED_X:
  8215. plane_config->tiling = I915_TILING_X;
  8216. fb->modifier = I915_FORMAT_MOD_X_TILED;
  8217. break;
  8218. case PLANE_CTL_TILED_Y:
  8219. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  8220. break;
  8221. case PLANE_CTL_TILED_YF:
  8222. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  8223. break;
  8224. default:
  8225. MISSING_CASE(tiling);
  8226. goto error;
  8227. }
  8228. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  8229. plane_config->base = base;
  8230. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  8231. val = I915_READ(PLANE_SIZE(pipe, 0));
  8232. fb->height = ((val >> 16) & 0xfff) + 1;
  8233. fb->width = ((val >> 0) & 0x1fff) + 1;
  8234. val = I915_READ(PLANE_STRIDE(pipe, 0));
  8235. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
  8236. fb->format->format);
  8237. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  8238. aligned_height = intel_fb_align_height(dev, fb->height,
  8239. fb->format->format,
  8240. fb->modifier);
  8241. plane_config->size = fb->pitches[0] * aligned_height;
  8242. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8243. pipe_name(pipe), fb->width, fb->height,
  8244. fb->format->cpp[0] * 8, base, fb->pitches[0],
  8245. plane_config->size);
  8246. plane_config->fb = intel_fb;
  8247. return;
  8248. error:
  8249. kfree(intel_fb);
  8250. }
  8251. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  8252. struct intel_crtc_state *pipe_config)
  8253. {
  8254. struct drm_device *dev = crtc->base.dev;
  8255. struct drm_i915_private *dev_priv = to_i915(dev);
  8256. uint32_t tmp;
  8257. tmp = I915_READ(PF_CTL(crtc->pipe));
  8258. if (tmp & PF_ENABLE) {
  8259. pipe_config->pch_pfit.enabled = true;
  8260. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  8261. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  8262. /* We currently do not free assignements of panel fitters on
  8263. * ivb/hsw (since we don't use the higher upscaling modes which
  8264. * differentiates them) so just WARN about this case for now. */
  8265. if (IS_GEN7(dev_priv)) {
  8266. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  8267. PF_PIPE_SEL_IVB(crtc->pipe));
  8268. }
  8269. }
  8270. }
  8271. static void
  8272. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  8273. struct intel_initial_plane_config *plane_config)
  8274. {
  8275. struct drm_device *dev = crtc->base.dev;
  8276. struct drm_i915_private *dev_priv = to_i915(dev);
  8277. u32 val, base, offset;
  8278. int pipe = crtc->pipe;
  8279. int fourcc, pixel_format;
  8280. unsigned int aligned_height;
  8281. struct drm_framebuffer *fb;
  8282. struct intel_framebuffer *intel_fb;
  8283. val = I915_READ(DSPCNTR(pipe));
  8284. if (!(val & DISPLAY_PLANE_ENABLE))
  8285. return;
  8286. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8287. if (!intel_fb) {
  8288. DRM_DEBUG_KMS("failed to alloc fb\n");
  8289. return;
  8290. }
  8291. fb = &intel_fb->base;
  8292. fb->dev = dev;
  8293. if (INTEL_GEN(dev_priv) >= 4) {
  8294. if (val & DISPPLANE_TILED) {
  8295. plane_config->tiling = I915_TILING_X;
  8296. fb->modifier = I915_FORMAT_MOD_X_TILED;
  8297. }
  8298. }
  8299. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  8300. fourcc = i9xx_format_to_fourcc(pixel_format);
  8301. fb->format = drm_format_info(fourcc);
  8302. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  8303. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  8304. offset = I915_READ(DSPOFFSET(pipe));
  8305. } else {
  8306. if (plane_config->tiling)
  8307. offset = I915_READ(DSPTILEOFF(pipe));
  8308. else
  8309. offset = I915_READ(DSPLINOFF(pipe));
  8310. }
  8311. plane_config->base = base;
  8312. val = I915_READ(PIPESRC(pipe));
  8313. fb->width = ((val >> 16) & 0xfff) + 1;
  8314. fb->height = ((val >> 0) & 0xfff) + 1;
  8315. val = I915_READ(DSPSTRIDE(pipe));
  8316. fb->pitches[0] = val & 0xffffffc0;
  8317. aligned_height = intel_fb_align_height(dev, fb->height,
  8318. fb->format->format,
  8319. fb->modifier);
  8320. plane_config->size = fb->pitches[0] * aligned_height;
  8321. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8322. pipe_name(pipe), fb->width, fb->height,
  8323. fb->format->cpp[0] * 8, base, fb->pitches[0],
  8324. plane_config->size);
  8325. plane_config->fb = intel_fb;
  8326. }
  8327. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  8328. struct intel_crtc_state *pipe_config)
  8329. {
  8330. struct drm_device *dev = crtc->base.dev;
  8331. struct drm_i915_private *dev_priv = to_i915(dev);
  8332. enum intel_display_power_domain power_domain;
  8333. uint32_t tmp;
  8334. bool ret;
  8335. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8336. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8337. return false;
  8338. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8339. pipe_config->shared_dpll = NULL;
  8340. ret = false;
  8341. tmp = I915_READ(PIPECONF(crtc->pipe));
  8342. if (!(tmp & PIPECONF_ENABLE))
  8343. goto out;
  8344. switch (tmp & PIPECONF_BPC_MASK) {
  8345. case PIPECONF_6BPC:
  8346. pipe_config->pipe_bpp = 18;
  8347. break;
  8348. case PIPECONF_8BPC:
  8349. pipe_config->pipe_bpp = 24;
  8350. break;
  8351. case PIPECONF_10BPC:
  8352. pipe_config->pipe_bpp = 30;
  8353. break;
  8354. case PIPECONF_12BPC:
  8355. pipe_config->pipe_bpp = 36;
  8356. break;
  8357. default:
  8358. break;
  8359. }
  8360. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  8361. pipe_config->limited_color_range = true;
  8362. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  8363. struct intel_shared_dpll *pll;
  8364. enum intel_dpll_id pll_id;
  8365. pipe_config->has_pch_encoder = true;
  8366. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  8367. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8368. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8369. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8370. if (HAS_PCH_IBX(dev_priv)) {
  8371. /*
  8372. * The pipe->pch transcoder and pch transcoder->pll
  8373. * mapping is fixed.
  8374. */
  8375. pll_id = (enum intel_dpll_id) crtc->pipe;
  8376. } else {
  8377. tmp = I915_READ(PCH_DPLL_SEL);
  8378. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  8379. pll_id = DPLL_ID_PCH_PLL_B;
  8380. else
  8381. pll_id= DPLL_ID_PCH_PLL_A;
  8382. }
  8383. pipe_config->shared_dpll =
  8384. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  8385. pll = pipe_config->shared_dpll;
  8386. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8387. &pipe_config->dpll_hw_state));
  8388. tmp = pipe_config->dpll_hw_state.dpll;
  8389. pipe_config->pixel_multiplier =
  8390. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  8391. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  8392. ironlake_pch_clock_get(crtc, pipe_config);
  8393. } else {
  8394. pipe_config->pixel_multiplier = 1;
  8395. }
  8396. intel_get_pipe_timings(crtc, pipe_config);
  8397. intel_get_pipe_src_size(crtc, pipe_config);
  8398. ironlake_get_pfit_config(crtc, pipe_config);
  8399. ret = true;
  8400. out:
  8401. intel_display_power_put(dev_priv, power_domain);
  8402. return ret;
  8403. }
  8404. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  8405. {
  8406. struct drm_device *dev = &dev_priv->drm;
  8407. struct intel_crtc *crtc;
  8408. for_each_intel_crtc(dev, crtc)
  8409. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  8410. pipe_name(crtc->pipe));
  8411. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  8412. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  8413. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  8414. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  8415. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  8416. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  8417. "CPU PWM1 enabled\n");
  8418. if (IS_HASWELL(dev_priv))
  8419. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  8420. "CPU PWM2 enabled\n");
  8421. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  8422. "PCH PWM1 enabled\n");
  8423. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  8424. "Utility pin enabled\n");
  8425. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  8426. /*
  8427. * In theory we can still leave IRQs enabled, as long as only the HPD
  8428. * interrupts remain enabled. We used to check for that, but since it's
  8429. * gen-specific and since we only disable LCPLL after we fully disable
  8430. * the interrupts, the check below should be enough.
  8431. */
  8432. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  8433. }
  8434. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  8435. {
  8436. if (IS_HASWELL(dev_priv))
  8437. return I915_READ(D_COMP_HSW);
  8438. else
  8439. return I915_READ(D_COMP_BDW);
  8440. }
  8441. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  8442. {
  8443. if (IS_HASWELL(dev_priv)) {
  8444. mutex_lock(&dev_priv->rps.hw_lock);
  8445. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  8446. val))
  8447. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  8448. mutex_unlock(&dev_priv->rps.hw_lock);
  8449. } else {
  8450. I915_WRITE(D_COMP_BDW, val);
  8451. POSTING_READ(D_COMP_BDW);
  8452. }
  8453. }
  8454. /*
  8455. * This function implements pieces of two sequences from BSpec:
  8456. * - Sequence for display software to disable LCPLL
  8457. * - Sequence for display software to allow package C8+
  8458. * The steps implemented here are just the steps that actually touch the LCPLL
  8459. * register. Callers should take care of disabling all the display engine
  8460. * functions, doing the mode unset, fixing interrupts, etc.
  8461. */
  8462. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  8463. bool switch_to_fclk, bool allow_power_down)
  8464. {
  8465. uint32_t val;
  8466. assert_can_disable_lcpll(dev_priv);
  8467. val = I915_READ(LCPLL_CTL);
  8468. if (switch_to_fclk) {
  8469. val |= LCPLL_CD_SOURCE_FCLK;
  8470. I915_WRITE(LCPLL_CTL, val);
  8471. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8472. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8473. DRM_ERROR("Switching to FCLK failed\n");
  8474. val = I915_READ(LCPLL_CTL);
  8475. }
  8476. val |= LCPLL_PLL_DISABLE;
  8477. I915_WRITE(LCPLL_CTL, val);
  8478. POSTING_READ(LCPLL_CTL);
  8479. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  8480. DRM_ERROR("LCPLL still locked\n");
  8481. val = hsw_read_dcomp(dev_priv);
  8482. val |= D_COMP_COMP_DISABLE;
  8483. hsw_write_dcomp(dev_priv, val);
  8484. ndelay(100);
  8485. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  8486. 1))
  8487. DRM_ERROR("D_COMP RCOMP still in progress\n");
  8488. if (allow_power_down) {
  8489. val = I915_READ(LCPLL_CTL);
  8490. val |= LCPLL_POWER_DOWN_ALLOW;
  8491. I915_WRITE(LCPLL_CTL, val);
  8492. POSTING_READ(LCPLL_CTL);
  8493. }
  8494. }
  8495. /*
  8496. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  8497. * source.
  8498. */
  8499. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8500. {
  8501. uint32_t val;
  8502. val = I915_READ(LCPLL_CTL);
  8503. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8504. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8505. return;
  8506. /*
  8507. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8508. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8509. */
  8510. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8511. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8512. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8513. I915_WRITE(LCPLL_CTL, val);
  8514. POSTING_READ(LCPLL_CTL);
  8515. }
  8516. val = hsw_read_dcomp(dev_priv);
  8517. val |= D_COMP_COMP_FORCE;
  8518. val &= ~D_COMP_COMP_DISABLE;
  8519. hsw_write_dcomp(dev_priv, val);
  8520. val = I915_READ(LCPLL_CTL);
  8521. val &= ~LCPLL_PLL_DISABLE;
  8522. I915_WRITE(LCPLL_CTL, val);
  8523. if (intel_wait_for_register(dev_priv,
  8524. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  8525. 5))
  8526. DRM_ERROR("LCPLL not locked yet\n");
  8527. if (val & LCPLL_CD_SOURCE_FCLK) {
  8528. val = I915_READ(LCPLL_CTL);
  8529. val &= ~LCPLL_CD_SOURCE_FCLK;
  8530. I915_WRITE(LCPLL_CTL, val);
  8531. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8532. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8533. DRM_ERROR("Switching back to LCPLL failed\n");
  8534. }
  8535. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8536. intel_update_cdclk(dev_priv);
  8537. }
  8538. /*
  8539. * Package states C8 and deeper are really deep PC states that can only be
  8540. * reached when all the devices on the system allow it, so even if the graphics
  8541. * device allows PC8+, it doesn't mean the system will actually get to these
  8542. * states. Our driver only allows PC8+ when going into runtime PM.
  8543. *
  8544. * The requirements for PC8+ are that all the outputs are disabled, the power
  8545. * well is disabled and most interrupts are disabled, and these are also
  8546. * requirements for runtime PM. When these conditions are met, we manually do
  8547. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8548. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8549. * hang the machine.
  8550. *
  8551. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8552. * the state of some registers, so when we come back from PC8+ we need to
  8553. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8554. * need to take care of the registers kept by RC6. Notice that this happens even
  8555. * if we don't put the device in PCI D3 state (which is what currently happens
  8556. * because of the runtime PM support).
  8557. *
  8558. * For more, read "Display Sequences for Package C8" on the hardware
  8559. * documentation.
  8560. */
  8561. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8562. {
  8563. uint32_t val;
  8564. DRM_DEBUG_KMS("Enabling package C8+\n");
  8565. if (HAS_PCH_LPT_LP(dev_priv)) {
  8566. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8567. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8568. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8569. }
  8570. lpt_disable_clkout_dp(dev_priv);
  8571. hsw_disable_lcpll(dev_priv, true, true);
  8572. }
  8573. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8574. {
  8575. uint32_t val;
  8576. DRM_DEBUG_KMS("Disabling package C8+\n");
  8577. hsw_restore_lcpll(dev_priv);
  8578. lpt_init_pch_refclk(dev_priv);
  8579. if (HAS_PCH_LPT_LP(dev_priv)) {
  8580. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8581. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8582. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8583. }
  8584. }
  8585. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8586. {
  8587. struct drm_device *dev = old_state->dev;
  8588. struct intel_atomic_state *old_intel_state =
  8589. to_intel_atomic_state(old_state);
  8590. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8591. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8592. }
  8593. static int bdw_adjust_min_pipe_pixel_rate(struct intel_crtc_state *crtc_state,
  8594. int pixel_rate)
  8595. {
  8596. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  8597. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8598. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8599. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8600. /* BSpec says "Do not use DisplayPort with CDCLK less than
  8601. * 432 MHz, audio enabled, port width x4, and link rate
  8602. * HBR2 (5.4 GHz), or else there may be audio corruption or
  8603. * screen corruption."
  8604. */
  8605. if (intel_crtc_has_dp_encoder(crtc_state) &&
  8606. crtc_state->has_audio &&
  8607. crtc_state->port_clock >= 540000 &&
  8608. crtc_state->lane_count == 4)
  8609. pixel_rate = max(432000, pixel_rate);
  8610. return pixel_rate;
  8611. }
  8612. /* compute the max rate for new configuration */
  8613. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8614. {
  8615. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8616. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8617. struct drm_crtc *crtc;
  8618. struct drm_crtc_state *cstate;
  8619. struct intel_crtc_state *crtc_state;
  8620. unsigned max_pixel_rate = 0, i;
  8621. enum pipe pipe;
  8622. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8623. sizeof(intel_state->min_pixclk));
  8624. for_each_crtc_in_state(state, crtc, cstate, i) {
  8625. int pixel_rate;
  8626. crtc_state = to_intel_crtc_state(cstate);
  8627. if (!crtc_state->base.enable) {
  8628. intel_state->min_pixclk[i] = 0;
  8629. continue;
  8630. }
  8631. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8632. if (IS_BROADWELL(dev_priv) || IS_GEN9(dev_priv))
  8633. pixel_rate = bdw_adjust_min_pipe_pixel_rate(crtc_state,
  8634. pixel_rate);
  8635. intel_state->min_pixclk[i] = pixel_rate;
  8636. }
  8637. for_each_pipe(dev_priv, pipe)
  8638. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8639. return max_pixel_rate;
  8640. }
  8641. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8642. {
  8643. struct drm_i915_private *dev_priv = to_i915(dev);
  8644. uint32_t val, data;
  8645. int ret;
  8646. if (WARN((I915_READ(LCPLL_CTL) &
  8647. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8648. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8649. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8650. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8651. "trying to change cdclk frequency with cdclk not enabled\n"))
  8652. return;
  8653. mutex_lock(&dev_priv->rps.hw_lock);
  8654. ret = sandybridge_pcode_write(dev_priv,
  8655. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8656. mutex_unlock(&dev_priv->rps.hw_lock);
  8657. if (ret) {
  8658. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8659. return;
  8660. }
  8661. val = I915_READ(LCPLL_CTL);
  8662. val |= LCPLL_CD_SOURCE_FCLK;
  8663. I915_WRITE(LCPLL_CTL, val);
  8664. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8665. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8666. DRM_ERROR("Switching to FCLK failed\n");
  8667. val = I915_READ(LCPLL_CTL);
  8668. val &= ~LCPLL_CLK_FREQ_MASK;
  8669. switch (cdclk) {
  8670. case 450000:
  8671. val |= LCPLL_CLK_FREQ_450;
  8672. data = 0;
  8673. break;
  8674. case 540000:
  8675. val |= LCPLL_CLK_FREQ_54O_BDW;
  8676. data = 1;
  8677. break;
  8678. case 337500:
  8679. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8680. data = 2;
  8681. break;
  8682. case 675000:
  8683. val |= LCPLL_CLK_FREQ_675_BDW;
  8684. data = 3;
  8685. break;
  8686. default:
  8687. WARN(1, "invalid cdclk frequency\n");
  8688. return;
  8689. }
  8690. I915_WRITE(LCPLL_CTL, val);
  8691. val = I915_READ(LCPLL_CTL);
  8692. val &= ~LCPLL_CD_SOURCE_FCLK;
  8693. I915_WRITE(LCPLL_CTL, val);
  8694. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8695. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8696. DRM_ERROR("Switching back to LCPLL failed\n");
  8697. mutex_lock(&dev_priv->rps.hw_lock);
  8698. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8699. mutex_unlock(&dev_priv->rps.hw_lock);
  8700. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8701. intel_update_cdclk(dev_priv);
  8702. WARN(cdclk != dev_priv->cdclk_freq,
  8703. "cdclk requested %d kHz but got %d kHz\n",
  8704. cdclk, dev_priv->cdclk_freq);
  8705. }
  8706. static int broadwell_calc_cdclk(int max_pixclk)
  8707. {
  8708. if (max_pixclk > 540000)
  8709. return 675000;
  8710. else if (max_pixclk > 450000)
  8711. return 540000;
  8712. else if (max_pixclk > 337500)
  8713. return 450000;
  8714. else
  8715. return 337500;
  8716. }
  8717. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8718. {
  8719. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8720. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8721. int max_pixclk = ilk_max_pixel_rate(state);
  8722. int cdclk;
  8723. /*
  8724. * FIXME should also account for plane ratio
  8725. * once 64bpp pixel formats are supported.
  8726. */
  8727. cdclk = broadwell_calc_cdclk(max_pixclk);
  8728. if (cdclk > dev_priv->max_cdclk_freq) {
  8729. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8730. cdclk, dev_priv->max_cdclk_freq);
  8731. return -EINVAL;
  8732. }
  8733. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8734. if (!intel_state->active_crtcs)
  8735. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8736. return 0;
  8737. }
  8738. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8739. {
  8740. struct drm_device *dev = old_state->dev;
  8741. struct intel_atomic_state *old_intel_state =
  8742. to_intel_atomic_state(old_state);
  8743. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8744. broadwell_set_cdclk(dev, req_cdclk);
  8745. }
  8746. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8747. {
  8748. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8749. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8750. const int max_pixclk = ilk_max_pixel_rate(state);
  8751. int vco = intel_state->cdclk_pll_vco;
  8752. int cdclk;
  8753. /*
  8754. * FIXME should also account for plane ratio
  8755. * once 64bpp pixel formats are supported.
  8756. */
  8757. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8758. /*
  8759. * FIXME move the cdclk caclulation to
  8760. * compute_config() so we can fail gracegully.
  8761. */
  8762. if (cdclk > dev_priv->max_cdclk_freq) {
  8763. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8764. cdclk, dev_priv->max_cdclk_freq);
  8765. cdclk = dev_priv->max_cdclk_freq;
  8766. }
  8767. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8768. if (!intel_state->active_crtcs)
  8769. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8770. return 0;
  8771. }
  8772. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8773. {
  8774. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8775. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8776. unsigned int req_cdclk = intel_state->dev_cdclk;
  8777. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8778. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8779. }
  8780. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8781. struct intel_crtc_state *crtc_state)
  8782. {
  8783. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  8784. if (!intel_ddi_pll_select(crtc, crtc_state))
  8785. return -EINVAL;
  8786. }
  8787. crtc->lowfreq_avail = false;
  8788. return 0;
  8789. }
  8790. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8791. enum port port,
  8792. struct intel_crtc_state *pipe_config)
  8793. {
  8794. enum intel_dpll_id id;
  8795. switch (port) {
  8796. case PORT_A:
  8797. id = DPLL_ID_SKL_DPLL0;
  8798. break;
  8799. case PORT_B:
  8800. id = DPLL_ID_SKL_DPLL1;
  8801. break;
  8802. case PORT_C:
  8803. id = DPLL_ID_SKL_DPLL2;
  8804. break;
  8805. default:
  8806. DRM_ERROR("Incorrect port type\n");
  8807. return;
  8808. }
  8809. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8810. }
  8811. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8812. enum port port,
  8813. struct intel_crtc_state *pipe_config)
  8814. {
  8815. enum intel_dpll_id id;
  8816. u32 temp;
  8817. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8818. id = temp >> (port * 3 + 1);
  8819. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  8820. return;
  8821. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8822. }
  8823. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8824. enum port port,
  8825. struct intel_crtc_state *pipe_config)
  8826. {
  8827. enum intel_dpll_id id;
  8828. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8829. switch (ddi_pll_sel) {
  8830. case PORT_CLK_SEL_WRPLL1:
  8831. id = DPLL_ID_WRPLL1;
  8832. break;
  8833. case PORT_CLK_SEL_WRPLL2:
  8834. id = DPLL_ID_WRPLL2;
  8835. break;
  8836. case PORT_CLK_SEL_SPLL:
  8837. id = DPLL_ID_SPLL;
  8838. break;
  8839. case PORT_CLK_SEL_LCPLL_810:
  8840. id = DPLL_ID_LCPLL_810;
  8841. break;
  8842. case PORT_CLK_SEL_LCPLL_1350:
  8843. id = DPLL_ID_LCPLL_1350;
  8844. break;
  8845. case PORT_CLK_SEL_LCPLL_2700:
  8846. id = DPLL_ID_LCPLL_2700;
  8847. break;
  8848. default:
  8849. MISSING_CASE(ddi_pll_sel);
  8850. /* fall through */
  8851. case PORT_CLK_SEL_NONE:
  8852. return;
  8853. }
  8854. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8855. }
  8856. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8857. struct intel_crtc_state *pipe_config,
  8858. unsigned long *power_domain_mask)
  8859. {
  8860. struct drm_device *dev = crtc->base.dev;
  8861. struct drm_i915_private *dev_priv = to_i915(dev);
  8862. enum intel_display_power_domain power_domain;
  8863. u32 tmp;
  8864. /*
  8865. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8866. * transcoder handled below.
  8867. */
  8868. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8869. /*
  8870. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8871. * consistency and less surprising code; it's in always on power).
  8872. */
  8873. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8874. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8875. enum pipe trans_edp_pipe;
  8876. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8877. default:
  8878. WARN(1, "unknown pipe linked to edp transcoder\n");
  8879. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8880. case TRANS_DDI_EDP_INPUT_A_ON:
  8881. trans_edp_pipe = PIPE_A;
  8882. break;
  8883. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8884. trans_edp_pipe = PIPE_B;
  8885. break;
  8886. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8887. trans_edp_pipe = PIPE_C;
  8888. break;
  8889. }
  8890. if (trans_edp_pipe == crtc->pipe)
  8891. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8892. }
  8893. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8894. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8895. return false;
  8896. *power_domain_mask |= BIT(power_domain);
  8897. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8898. return tmp & PIPECONF_ENABLE;
  8899. }
  8900. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8901. struct intel_crtc_state *pipe_config,
  8902. unsigned long *power_domain_mask)
  8903. {
  8904. struct drm_device *dev = crtc->base.dev;
  8905. struct drm_i915_private *dev_priv = to_i915(dev);
  8906. enum intel_display_power_domain power_domain;
  8907. enum port port;
  8908. enum transcoder cpu_transcoder;
  8909. u32 tmp;
  8910. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8911. if (port == PORT_A)
  8912. cpu_transcoder = TRANSCODER_DSI_A;
  8913. else
  8914. cpu_transcoder = TRANSCODER_DSI_C;
  8915. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8916. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8917. continue;
  8918. *power_domain_mask |= BIT(power_domain);
  8919. /*
  8920. * The PLL needs to be enabled with a valid divider
  8921. * configuration, otherwise accessing DSI registers will hang
  8922. * the machine. See BSpec North Display Engine
  8923. * registers/MIPI[BXT]. We can break out here early, since we
  8924. * need the same DSI PLL to be enabled for both DSI ports.
  8925. */
  8926. if (!intel_dsi_pll_is_enabled(dev_priv))
  8927. break;
  8928. /* XXX: this works for video mode only */
  8929. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8930. if (!(tmp & DPI_ENABLE))
  8931. continue;
  8932. tmp = I915_READ(MIPI_CTRL(port));
  8933. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8934. continue;
  8935. pipe_config->cpu_transcoder = cpu_transcoder;
  8936. break;
  8937. }
  8938. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  8939. }
  8940. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8941. struct intel_crtc_state *pipe_config)
  8942. {
  8943. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8944. struct intel_shared_dpll *pll;
  8945. enum port port;
  8946. uint32_t tmp;
  8947. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8948. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8949. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  8950. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8951. else if (IS_GEN9_LP(dev_priv))
  8952. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8953. else
  8954. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8955. pll = pipe_config->shared_dpll;
  8956. if (pll) {
  8957. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8958. &pipe_config->dpll_hw_state));
  8959. }
  8960. /*
  8961. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8962. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8963. * the PCH transcoder is on.
  8964. */
  8965. if (INTEL_GEN(dev_priv) < 9 &&
  8966. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8967. pipe_config->has_pch_encoder = true;
  8968. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8969. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8970. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8971. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8972. }
  8973. }
  8974. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8975. struct intel_crtc_state *pipe_config)
  8976. {
  8977. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8978. enum intel_display_power_domain power_domain;
  8979. unsigned long power_domain_mask;
  8980. bool active;
  8981. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8982. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8983. return false;
  8984. power_domain_mask = BIT(power_domain);
  8985. pipe_config->shared_dpll = NULL;
  8986. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8987. if (IS_GEN9_LP(dev_priv) &&
  8988. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  8989. WARN_ON(active);
  8990. active = true;
  8991. }
  8992. if (!active)
  8993. goto out;
  8994. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8995. haswell_get_ddi_port_state(crtc, pipe_config);
  8996. intel_get_pipe_timings(crtc, pipe_config);
  8997. }
  8998. intel_get_pipe_src_size(crtc, pipe_config);
  8999. pipe_config->gamma_mode =
  9000. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  9001. if (INTEL_GEN(dev_priv) >= 9) {
  9002. intel_crtc_init_scalers(crtc, pipe_config);
  9003. pipe_config->scaler_state.scaler_id = -1;
  9004. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  9005. }
  9006. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  9007. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  9008. power_domain_mask |= BIT(power_domain);
  9009. if (INTEL_GEN(dev_priv) >= 9)
  9010. skylake_get_pfit_config(crtc, pipe_config);
  9011. else
  9012. ironlake_get_pfit_config(crtc, pipe_config);
  9013. }
  9014. if (IS_HASWELL(dev_priv))
  9015. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  9016. (I915_READ(IPS_CTL) & IPS_ENABLE);
  9017. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  9018. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  9019. pipe_config->pixel_multiplier =
  9020. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  9021. } else {
  9022. pipe_config->pixel_multiplier = 1;
  9023. }
  9024. out:
  9025. for_each_power_domain(power_domain, power_domain_mask)
  9026. intel_display_power_put(dev_priv, power_domain);
  9027. return active;
  9028. }
  9029. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  9030. const struct intel_plane_state *plane_state)
  9031. {
  9032. struct drm_device *dev = crtc->dev;
  9033. struct drm_i915_private *dev_priv = to_i915(dev);
  9034. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9035. uint32_t cntl = 0, size = 0;
  9036. if (plane_state && plane_state->base.visible) {
  9037. unsigned int width = plane_state->base.crtc_w;
  9038. unsigned int height = plane_state->base.crtc_h;
  9039. unsigned int stride = roundup_pow_of_two(width) * 4;
  9040. switch (stride) {
  9041. default:
  9042. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  9043. width, stride);
  9044. stride = 256;
  9045. /* fallthrough */
  9046. case 256:
  9047. case 512:
  9048. case 1024:
  9049. case 2048:
  9050. break;
  9051. }
  9052. cntl |= CURSOR_ENABLE |
  9053. CURSOR_GAMMA_ENABLE |
  9054. CURSOR_FORMAT_ARGB |
  9055. CURSOR_STRIDE(stride);
  9056. size = (height << 12) | width;
  9057. }
  9058. if (intel_crtc->cursor_cntl != 0 &&
  9059. (intel_crtc->cursor_base != base ||
  9060. intel_crtc->cursor_size != size ||
  9061. intel_crtc->cursor_cntl != cntl)) {
  9062. /* On these chipsets we can only modify the base/size/stride
  9063. * whilst the cursor is disabled.
  9064. */
  9065. I915_WRITE(CURCNTR(PIPE_A), 0);
  9066. POSTING_READ(CURCNTR(PIPE_A));
  9067. intel_crtc->cursor_cntl = 0;
  9068. }
  9069. if (intel_crtc->cursor_base != base) {
  9070. I915_WRITE(CURBASE(PIPE_A), base);
  9071. intel_crtc->cursor_base = base;
  9072. }
  9073. if (intel_crtc->cursor_size != size) {
  9074. I915_WRITE(CURSIZE, size);
  9075. intel_crtc->cursor_size = size;
  9076. }
  9077. if (intel_crtc->cursor_cntl != cntl) {
  9078. I915_WRITE(CURCNTR(PIPE_A), cntl);
  9079. POSTING_READ(CURCNTR(PIPE_A));
  9080. intel_crtc->cursor_cntl = cntl;
  9081. }
  9082. }
  9083. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  9084. const struct intel_plane_state *plane_state)
  9085. {
  9086. struct drm_device *dev = crtc->dev;
  9087. struct drm_i915_private *dev_priv = to_i915(dev);
  9088. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9089. int pipe = intel_crtc->pipe;
  9090. uint32_t cntl = 0;
  9091. if (plane_state && plane_state->base.visible) {
  9092. cntl = MCURSOR_GAMMA_ENABLE;
  9093. switch (plane_state->base.crtc_w) {
  9094. case 64:
  9095. cntl |= CURSOR_MODE_64_ARGB_AX;
  9096. break;
  9097. case 128:
  9098. cntl |= CURSOR_MODE_128_ARGB_AX;
  9099. break;
  9100. case 256:
  9101. cntl |= CURSOR_MODE_256_ARGB_AX;
  9102. break;
  9103. default:
  9104. MISSING_CASE(plane_state->base.crtc_w);
  9105. return;
  9106. }
  9107. cntl |= pipe << 28; /* Connect to correct pipe */
  9108. if (HAS_DDI(dev_priv))
  9109. cntl |= CURSOR_PIPE_CSC_ENABLE;
  9110. if (plane_state->base.rotation & DRM_ROTATE_180)
  9111. cntl |= CURSOR_ROTATE_180;
  9112. }
  9113. if (intel_crtc->cursor_cntl != cntl) {
  9114. I915_WRITE(CURCNTR(pipe), cntl);
  9115. POSTING_READ(CURCNTR(pipe));
  9116. intel_crtc->cursor_cntl = cntl;
  9117. }
  9118. /* and commit changes on next vblank */
  9119. I915_WRITE(CURBASE(pipe), base);
  9120. POSTING_READ(CURBASE(pipe));
  9121. intel_crtc->cursor_base = base;
  9122. }
  9123. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  9124. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  9125. const struct intel_plane_state *plane_state)
  9126. {
  9127. struct drm_device *dev = crtc->dev;
  9128. struct drm_i915_private *dev_priv = to_i915(dev);
  9129. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9130. int pipe = intel_crtc->pipe;
  9131. u32 base = intel_crtc->cursor_addr;
  9132. u32 pos = 0;
  9133. if (plane_state) {
  9134. int x = plane_state->base.crtc_x;
  9135. int y = plane_state->base.crtc_y;
  9136. if (x < 0) {
  9137. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  9138. x = -x;
  9139. }
  9140. pos |= x << CURSOR_X_SHIFT;
  9141. if (y < 0) {
  9142. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  9143. y = -y;
  9144. }
  9145. pos |= y << CURSOR_Y_SHIFT;
  9146. /* ILK+ do this automagically */
  9147. if (HAS_GMCH_DISPLAY(dev_priv) &&
  9148. plane_state->base.rotation & DRM_ROTATE_180) {
  9149. base += (plane_state->base.crtc_h *
  9150. plane_state->base.crtc_w - 1) * 4;
  9151. }
  9152. }
  9153. I915_WRITE(CURPOS(pipe), pos);
  9154. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  9155. i845_update_cursor(crtc, base, plane_state);
  9156. else
  9157. i9xx_update_cursor(crtc, base, plane_state);
  9158. }
  9159. static bool cursor_size_ok(struct drm_i915_private *dev_priv,
  9160. uint32_t width, uint32_t height)
  9161. {
  9162. if (width == 0 || height == 0)
  9163. return false;
  9164. /*
  9165. * 845g/865g are special in that they are only limited by
  9166. * the width of their cursors, the height is arbitrary up to
  9167. * the precision of the register. Everything else requires
  9168. * square cursors, limited to a few power-of-two sizes.
  9169. */
  9170. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  9171. if ((width & 63) != 0)
  9172. return false;
  9173. if (width > (IS_I845G(dev_priv) ? 64 : 512))
  9174. return false;
  9175. if (height > 1023)
  9176. return false;
  9177. } else {
  9178. switch (width | height) {
  9179. case 256:
  9180. case 128:
  9181. if (IS_GEN2(dev_priv))
  9182. return false;
  9183. case 64:
  9184. break;
  9185. default:
  9186. return false;
  9187. }
  9188. }
  9189. return true;
  9190. }
  9191. /* VESA 640x480x72Hz mode to set on the pipe */
  9192. static struct drm_display_mode load_detect_mode = {
  9193. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  9194. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  9195. };
  9196. struct drm_framebuffer *
  9197. __intel_framebuffer_create(struct drm_device *dev,
  9198. struct drm_mode_fb_cmd2 *mode_cmd,
  9199. struct drm_i915_gem_object *obj)
  9200. {
  9201. struct intel_framebuffer *intel_fb;
  9202. int ret;
  9203. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  9204. if (!intel_fb)
  9205. return ERR_PTR(-ENOMEM);
  9206. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  9207. if (ret)
  9208. goto err;
  9209. return &intel_fb->base;
  9210. err:
  9211. kfree(intel_fb);
  9212. return ERR_PTR(ret);
  9213. }
  9214. static struct drm_framebuffer *
  9215. intel_framebuffer_create(struct drm_device *dev,
  9216. struct drm_mode_fb_cmd2 *mode_cmd,
  9217. struct drm_i915_gem_object *obj)
  9218. {
  9219. struct drm_framebuffer *fb;
  9220. int ret;
  9221. ret = i915_mutex_lock_interruptible(dev);
  9222. if (ret)
  9223. return ERR_PTR(ret);
  9224. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  9225. mutex_unlock(&dev->struct_mutex);
  9226. return fb;
  9227. }
  9228. static u32
  9229. intel_framebuffer_pitch_for_width(int width, int bpp)
  9230. {
  9231. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  9232. return ALIGN(pitch, 64);
  9233. }
  9234. static u32
  9235. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  9236. {
  9237. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  9238. return PAGE_ALIGN(pitch * mode->vdisplay);
  9239. }
  9240. static struct drm_framebuffer *
  9241. intel_framebuffer_create_for_mode(struct drm_device *dev,
  9242. struct drm_display_mode *mode,
  9243. int depth, int bpp)
  9244. {
  9245. struct drm_framebuffer *fb;
  9246. struct drm_i915_gem_object *obj;
  9247. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  9248. obj = i915_gem_object_create(to_i915(dev),
  9249. intel_framebuffer_size_for_mode(mode, bpp));
  9250. if (IS_ERR(obj))
  9251. return ERR_CAST(obj);
  9252. mode_cmd.width = mode->hdisplay;
  9253. mode_cmd.height = mode->vdisplay;
  9254. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  9255. bpp);
  9256. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  9257. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  9258. if (IS_ERR(fb))
  9259. i915_gem_object_put(obj);
  9260. return fb;
  9261. }
  9262. static struct drm_framebuffer *
  9263. mode_fits_in_fbdev(struct drm_device *dev,
  9264. struct drm_display_mode *mode)
  9265. {
  9266. #ifdef CONFIG_DRM_FBDEV_EMULATION
  9267. struct drm_i915_private *dev_priv = to_i915(dev);
  9268. struct drm_i915_gem_object *obj;
  9269. struct drm_framebuffer *fb;
  9270. if (!dev_priv->fbdev)
  9271. return NULL;
  9272. if (!dev_priv->fbdev->fb)
  9273. return NULL;
  9274. obj = dev_priv->fbdev->fb->obj;
  9275. BUG_ON(!obj);
  9276. fb = &dev_priv->fbdev->fb->base;
  9277. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  9278. fb->format->cpp[0] * 8))
  9279. return NULL;
  9280. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  9281. return NULL;
  9282. drm_framebuffer_reference(fb);
  9283. return fb;
  9284. #else
  9285. return NULL;
  9286. #endif
  9287. }
  9288. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  9289. struct drm_crtc *crtc,
  9290. struct drm_display_mode *mode,
  9291. struct drm_framebuffer *fb,
  9292. int x, int y)
  9293. {
  9294. struct drm_plane_state *plane_state;
  9295. int hdisplay, vdisplay;
  9296. int ret;
  9297. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  9298. if (IS_ERR(plane_state))
  9299. return PTR_ERR(plane_state);
  9300. if (mode)
  9301. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9302. else
  9303. hdisplay = vdisplay = 0;
  9304. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  9305. if (ret)
  9306. return ret;
  9307. drm_atomic_set_fb_for_plane(plane_state, fb);
  9308. plane_state->crtc_x = 0;
  9309. plane_state->crtc_y = 0;
  9310. plane_state->crtc_w = hdisplay;
  9311. plane_state->crtc_h = vdisplay;
  9312. plane_state->src_x = x << 16;
  9313. plane_state->src_y = y << 16;
  9314. plane_state->src_w = hdisplay << 16;
  9315. plane_state->src_h = vdisplay << 16;
  9316. return 0;
  9317. }
  9318. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  9319. struct drm_display_mode *mode,
  9320. struct intel_load_detect_pipe *old,
  9321. struct drm_modeset_acquire_ctx *ctx)
  9322. {
  9323. struct intel_crtc *intel_crtc;
  9324. struct intel_encoder *intel_encoder =
  9325. intel_attached_encoder(connector);
  9326. struct drm_crtc *possible_crtc;
  9327. struct drm_encoder *encoder = &intel_encoder->base;
  9328. struct drm_crtc *crtc = NULL;
  9329. struct drm_device *dev = encoder->dev;
  9330. struct drm_i915_private *dev_priv = to_i915(dev);
  9331. struct drm_framebuffer *fb;
  9332. struct drm_mode_config *config = &dev->mode_config;
  9333. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  9334. struct drm_connector_state *connector_state;
  9335. struct intel_crtc_state *crtc_state;
  9336. int ret, i = -1;
  9337. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9338. connector->base.id, connector->name,
  9339. encoder->base.id, encoder->name);
  9340. old->restore_state = NULL;
  9341. retry:
  9342. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  9343. if (ret)
  9344. goto fail;
  9345. /*
  9346. * Algorithm gets a little messy:
  9347. *
  9348. * - if the connector already has an assigned crtc, use it (but make
  9349. * sure it's on first)
  9350. *
  9351. * - try to find the first unused crtc that can drive this connector,
  9352. * and use that if we find one
  9353. */
  9354. /* See if we already have a CRTC for this connector */
  9355. if (connector->state->crtc) {
  9356. crtc = connector->state->crtc;
  9357. ret = drm_modeset_lock(&crtc->mutex, ctx);
  9358. if (ret)
  9359. goto fail;
  9360. /* Make sure the crtc and connector are running */
  9361. goto found;
  9362. }
  9363. /* Find an unused one (if possible) */
  9364. for_each_crtc(dev, possible_crtc) {
  9365. i++;
  9366. if (!(encoder->possible_crtcs & (1 << i)))
  9367. continue;
  9368. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  9369. if (ret)
  9370. goto fail;
  9371. if (possible_crtc->state->enable) {
  9372. drm_modeset_unlock(&possible_crtc->mutex);
  9373. continue;
  9374. }
  9375. crtc = possible_crtc;
  9376. break;
  9377. }
  9378. /*
  9379. * If we didn't find an unused CRTC, don't use any.
  9380. */
  9381. if (!crtc) {
  9382. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  9383. goto fail;
  9384. }
  9385. found:
  9386. intel_crtc = to_intel_crtc(crtc);
  9387. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  9388. if (ret)
  9389. goto fail;
  9390. state = drm_atomic_state_alloc(dev);
  9391. restore_state = drm_atomic_state_alloc(dev);
  9392. if (!state || !restore_state) {
  9393. ret = -ENOMEM;
  9394. goto fail;
  9395. }
  9396. state->acquire_ctx = ctx;
  9397. restore_state->acquire_ctx = ctx;
  9398. connector_state = drm_atomic_get_connector_state(state, connector);
  9399. if (IS_ERR(connector_state)) {
  9400. ret = PTR_ERR(connector_state);
  9401. goto fail;
  9402. }
  9403. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  9404. if (ret)
  9405. goto fail;
  9406. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  9407. if (IS_ERR(crtc_state)) {
  9408. ret = PTR_ERR(crtc_state);
  9409. goto fail;
  9410. }
  9411. crtc_state->base.active = crtc_state->base.enable = true;
  9412. if (!mode)
  9413. mode = &load_detect_mode;
  9414. /* We need a framebuffer large enough to accommodate all accesses
  9415. * that the plane may generate whilst we perform load detection.
  9416. * We can not rely on the fbcon either being present (we get called
  9417. * during its initialisation to detect all boot displays, or it may
  9418. * not even exist) or that it is large enough to satisfy the
  9419. * requested mode.
  9420. */
  9421. fb = mode_fits_in_fbdev(dev, mode);
  9422. if (fb == NULL) {
  9423. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  9424. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  9425. } else
  9426. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  9427. if (IS_ERR(fb)) {
  9428. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  9429. goto fail;
  9430. }
  9431. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  9432. if (ret)
  9433. goto fail;
  9434. drm_framebuffer_unreference(fb);
  9435. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  9436. if (ret)
  9437. goto fail;
  9438. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  9439. if (!ret)
  9440. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  9441. if (!ret)
  9442. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  9443. if (ret) {
  9444. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  9445. goto fail;
  9446. }
  9447. ret = drm_atomic_commit(state);
  9448. if (ret) {
  9449. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  9450. goto fail;
  9451. }
  9452. old->restore_state = restore_state;
  9453. /* let the connector get through one full cycle before testing */
  9454. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  9455. return true;
  9456. fail:
  9457. if (state) {
  9458. drm_atomic_state_put(state);
  9459. state = NULL;
  9460. }
  9461. if (restore_state) {
  9462. drm_atomic_state_put(restore_state);
  9463. restore_state = NULL;
  9464. }
  9465. if (ret == -EDEADLK) {
  9466. drm_modeset_backoff(ctx);
  9467. goto retry;
  9468. }
  9469. return false;
  9470. }
  9471. void intel_release_load_detect_pipe(struct drm_connector *connector,
  9472. struct intel_load_detect_pipe *old,
  9473. struct drm_modeset_acquire_ctx *ctx)
  9474. {
  9475. struct intel_encoder *intel_encoder =
  9476. intel_attached_encoder(connector);
  9477. struct drm_encoder *encoder = &intel_encoder->base;
  9478. struct drm_atomic_state *state = old->restore_state;
  9479. int ret;
  9480. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9481. connector->base.id, connector->name,
  9482. encoder->base.id, encoder->name);
  9483. if (!state)
  9484. return;
  9485. ret = drm_atomic_commit(state);
  9486. if (ret)
  9487. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  9488. drm_atomic_state_put(state);
  9489. }
  9490. static int i9xx_pll_refclk(struct drm_device *dev,
  9491. const struct intel_crtc_state *pipe_config)
  9492. {
  9493. struct drm_i915_private *dev_priv = to_i915(dev);
  9494. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9495. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  9496. return dev_priv->vbt.lvds_ssc_freq;
  9497. else if (HAS_PCH_SPLIT(dev_priv))
  9498. return 120000;
  9499. else if (!IS_GEN2(dev_priv))
  9500. return 96000;
  9501. else
  9502. return 48000;
  9503. }
  9504. /* Returns the clock of the currently programmed mode of the given pipe. */
  9505. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  9506. struct intel_crtc_state *pipe_config)
  9507. {
  9508. struct drm_device *dev = crtc->base.dev;
  9509. struct drm_i915_private *dev_priv = to_i915(dev);
  9510. int pipe = pipe_config->cpu_transcoder;
  9511. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9512. u32 fp;
  9513. struct dpll clock;
  9514. int port_clock;
  9515. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9516. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9517. fp = pipe_config->dpll_hw_state.fp0;
  9518. else
  9519. fp = pipe_config->dpll_hw_state.fp1;
  9520. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9521. if (IS_PINEVIEW(dev_priv)) {
  9522. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9523. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9524. } else {
  9525. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9526. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9527. }
  9528. if (!IS_GEN2(dev_priv)) {
  9529. if (IS_PINEVIEW(dev_priv))
  9530. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9531. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9532. else
  9533. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9534. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9535. switch (dpll & DPLL_MODE_MASK) {
  9536. case DPLLB_MODE_DAC_SERIAL:
  9537. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9538. 5 : 10;
  9539. break;
  9540. case DPLLB_MODE_LVDS:
  9541. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9542. 7 : 14;
  9543. break;
  9544. default:
  9545. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9546. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9547. return;
  9548. }
  9549. if (IS_PINEVIEW(dev_priv))
  9550. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9551. else
  9552. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9553. } else {
  9554. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  9555. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9556. if (is_lvds) {
  9557. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9558. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9559. if (lvds & LVDS_CLKB_POWER_UP)
  9560. clock.p2 = 7;
  9561. else
  9562. clock.p2 = 14;
  9563. } else {
  9564. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9565. clock.p1 = 2;
  9566. else {
  9567. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9568. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9569. }
  9570. if (dpll & PLL_P2_DIVIDE_BY_4)
  9571. clock.p2 = 4;
  9572. else
  9573. clock.p2 = 2;
  9574. }
  9575. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9576. }
  9577. /*
  9578. * This value includes pixel_multiplier. We will use
  9579. * port_clock to compute adjusted_mode.crtc_clock in the
  9580. * encoder's get_config() function.
  9581. */
  9582. pipe_config->port_clock = port_clock;
  9583. }
  9584. int intel_dotclock_calculate(int link_freq,
  9585. const struct intel_link_m_n *m_n)
  9586. {
  9587. /*
  9588. * The calculation for the data clock is:
  9589. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9590. * But we want to avoid losing precison if possible, so:
  9591. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9592. *
  9593. * and the link clock is simpler:
  9594. * link_clock = (m * link_clock) / n
  9595. */
  9596. if (!m_n->link_n)
  9597. return 0;
  9598. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9599. }
  9600. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9601. struct intel_crtc_state *pipe_config)
  9602. {
  9603. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9604. /* read out port_clock from the DPLL */
  9605. i9xx_crtc_clock_get(crtc, pipe_config);
  9606. /*
  9607. * In case there is an active pipe without active ports,
  9608. * we may need some idea for the dotclock anyway.
  9609. * Calculate one based on the FDI configuration.
  9610. */
  9611. pipe_config->base.adjusted_mode.crtc_clock =
  9612. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9613. &pipe_config->fdi_m_n);
  9614. }
  9615. /** Returns the currently programmed mode of the given pipe. */
  9616. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9617. struct drm_crtc *crtc)
  9618. {
  9619. struct drm_i915_private *dev_priv = to_i915(dev);
  9620. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9621. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9622. struct drm_display_mode *mode;
  9623. struct intel_crtc_state *pipe_config;
  9624. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9625. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9626. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9627. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9628. enum pipe pipe = intel_crtc->pipe;
  9629. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9630. if (!mode)
  9631. return NULL;
  9632. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9633. if (!pipe_config) {
  9634. kfree(mode);
  9635. return NULL;
  9636. }
  9637. /*
  9638. * Construct a pipe_config sufficient for getting the clock info
  9639. * back out of crtc_clock_get.
  9640. *
  9641. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9642. * to use a real value here instead.
  9643. */
  9644. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9645. pipe_config->pixel_multiplier = 1;
  9646. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9647. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9648. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9649. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9650. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9651. mode->hdisplay = (htot & 0xffff) + 1;
  9652. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9653. mode->hsync_start = (hsync & 0xffff) + 1;
  9654. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9655. mode->vdisplay = (vtot & 0xffff) + 1;
  9656. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9657. mode->vsync_start = (vsync & 0xffff) + 1;
  9658. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9659. drm_mode_set_name(mode);
  9660. kfree(pipe_config);
  9661. return mode;
  9662. }
  9663. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9664. {
  9665. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9666. struct drm_device *dev = crtc->dev;
  9667. struct intel_flip_work *work;
  9668. spin_lock_irq(&dev->event_lock);
  9669. work = intel_crtc->flip_work;
  9670. intel_crtc->flip_work = NULL;
  9671. spin_unlock_irq(&dev->event_lock);
  9672. if (work) {
  9673. cancel_work_sync(&work->mmio_work);
  9674. cancel_work_sync(&work->unpin_work);
  9675. kfree(work);
  9676. }
  9677. drm_crtc_cleanup(crtc);
  9678. kfree(intel_crtc);
  9679. }
  9680. static void intel_unpin_work_fn(struct work_struct *__work)
  9681. {
  9682. struct intel_flip_work *work =
  9683. container_of(__work, struct intel_flip_work, unpin_work);
  9684. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9685. struct drm_device *dev = crtc->base.dev;
  9686. struct drm_plane *primary = crtc->base.primary;
  9687. if (is_mmio_work(work))
  9688. flush_work(&work->mmio_work);
  9689. mutex_lock(&dev->struct_mutex);
  9690. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9691. i915_gem_object_put(work->pending_flip_obj);
  9692. mutex_unlock(&dev->struct_mutex);
  9693. i915_gem_request_put(work->flip_queued_req);
  9694. intel_frontbuffer_flip_complete(to_i915(dev),
  9695. to_intel_plane(primary)->frontbuffer_bit);
  9696. intel_fbc_post_update(crtc);
  9697. drm_framebuffer_unreference(work->old_fb);
  9698. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9699. atomic_dec(&crtc->unpin_work_count);
  9700. kfree(work);
  9701. }
  9702. /* Is 'a' after or equal to 'b'? */
  9703. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9704. {
  9705. return !((a - b) & 0x80000000);
  9706. }
  9707. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9708. struct intel_flip_work *work)
  9709. {
  9710. struct drm_device *dev = crtc->base.dev;
  9711. struct drm_i915_private *dev_priv = to_i915(dev);
  9712. if (abort_flip_on_reset(crtc))
  9713. return true;
  9714. /*
  9715. * The relevant registers doen't exist on pre-ctg.
  9716. * As the flip done interrupt doesn't trigger for mmio
  9717. * flips on gmch platforms, a flip count check isn't
  9718. * really needed there. But since ctg has the registers,
  9719. * include it in the check anyway.
  9720. */
  9721. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  9722. return true;
  9723. /*
  9724. * BDW signals flip done immediately if the plane
  9725. * is disabled, even if the plane enable is already
  9726. * armed to occur at the next vblank :(
  9727. */
  9728. /*
  9729. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9730. * used the same base address. In that case the mmio flip might
  9731. * have completed, but the CS hasn't even executed the flip yet.
  9732. *
  9733. * A flip count check isn't enough as the CS might have updated
  9734. * the base address just after start of vblank, but before we
  9735. * managed to process the interrupt. This means we'd complete the
  9736. * CS flip too soon.
  9737. *
  9738. * Combining both checks should get us a good enough result. It may
  9739. * still happen that the CS flip has been executed, but has not
  9740. * yet actually completed. But in case the base address is the same
  9741. * anyway, we don't really care.
  9742. */
  9743. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9744. crtc->flip_work->gtt_offset &&
  9745. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9746. crtc->flip_work->flip_count);
  9747. }
  9748. static bool
  9749. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9750. struct intel_flip_work *work)
  9751. {
  9752. /*
  9753. * MMIO work completes when vblank is different from
  9754. * flip_queued_vblank.
  9755. *
  9756. * Reset counter value doesn't matter, this is handled by
  9757. * i915_wait_request finishing early, so no need to handle
  9758. * reset here.
  9759. */
  9760. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9761. }
  9762. static bool pageflip_finished(struct intel_crtc *crtc,
  9763. struct intel_flip_work *work)
  9764. {
  9765. if (!atomic_read(&work->pending))
  9766. return false;
  9767. smp_rmb();
  9768. if (is_mmio_work(work))
  9769. return __pageflip_finished_mmio(crtc, work);
  9770. else
  9771. return __pageflip_finished_cs(crtc, work);
  9772. }
  9773. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9774. {
  9775. struct drm_device *dev = &dev_priv->drm;
  9776. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9777. struct intel_flip_work *work;
  9778. unsigned long flags;
  9779. /* Ignore early vblank irqs */
  9780. if (!crtc)
  9781. return;
  9782. /*
  9783. * This is called both by irq handlers and the reset code (to complete
  9784. * lost pageflips) so needs the full irqsave spinlocks.
  9785. */
  9786. spin_lock_irqsave(&dev->event_lock, flags);
  9787. work = crtc->flip_work;
  9788. if (work != NULL &&
  9789. !is_mmio_work(work) &&
  9790. pageflip_finished(crtc, work))
  9791. page_flip_completed(crtc);
  9792. spin_unlock_irqrestore(&dev->event_lock, flags);
  9793. }
  9794. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9795. {
  9796. struct drm_device *dev = &dev_priv->drm;
  9797. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9798. struct intel_flip_work *work;
  9799. unsigned long flags;
  9800. /* Ignore early vblank irqs */
  9801. if (!crtc)
  9802. return;
  9803. /*
  9804. * This is called both by irq handlers and the reset code (to complete
  9805. * lost pageflips) so needs the full irqsave spinlocks.
  9806. */
  9807. spin_lock_irqsave(&dev->event_lock, flags);
  9808. work = crtc->flip_work;
  9809. if (work != NULL &&
  9810. is_mmio_work(work) &&
  9811. pageflip_finished(crtc, work))
  9812. page_flip_completed(crtc);
  9813. spin_unlock_irqrestore(&dev->event_lock, flags);
  9814. }
  9815. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9816. struct intel_flip_work *work)
  9817. {
  9818. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9819. /* Ensure that the work item is consistent when activating it ... */
  9820. smp_mb__before_atomic();
  9821. atomic_set(&work->pending, 1);
  9822. }
  9823. static int intel_gen2_queue_flip(struct drm_device *dev,
  9824. struct drm_crtc *crtc,
  9825. struct drm_framebuffer *fb,
  9826. struct drm_i915_gem_object *obj,
  9827. struct drm_i915_gem_request *req,
  9828. uint32_t flags)
  9829. {
  9830. struct intel_ring *ring = req->ring;
  9831. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9832. u32 flip_mask;
  9833. int ret;
  9834. ret = intel_ring_begin(req, 6);
  9835. if (ret)
  9836. return ret;
  9837. /* Can't queue multiple flips, so wait for the previous
  9838. * one to finish before executing the next.
  9839. */
  9840. if (intel_crtc->plane)
  9841. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9842. else
  9843. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9844. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9845. intel_ring_emit(ring, MI_NOOP);
  9846. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9847. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9848. intel_ring_emit(ring, fb->pitches[0]);
  9849. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9850. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9851. return 0;
  9852. }
  9853. static int intel_gen3_queue_flip(struct drm_device *dev,
  9854. struct drm_crtc *crtc,
  9855. struct drm_framebuffer *fb,
  9856. struct drm_i915_gem_object *obj,
  9857. struct drm_i915_gem_request *req,
  9858. uint32_t flags)
  9859. {
  9860. struct intel_ring *ring = req->ring;
  9861. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9862. u32 flip_mask;
  9863. int ret;
  9864. ret = intel_ring_begin(req, 6);
  9865. if (ret)
  9866. return ret;
  9867. if (intel_crtc->plane)
  9868. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9869. else
  9870. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9871. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9872. intel_ring_emit(ring, MI_NOOP);
  9873. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9874. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9875. intel_ring_emit(ring, fb->pitches[0]);
  9876. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9877. intel_ring_emit(ring, MI_NOOP);
  9878. return 0;
  9879. }
  9880. static int intel_gen4_queue_flip(struct drm_device *dev,
  9881. struct drm_crtc *crtc,
  9882. struct drm_framebuffer *fb,
  9883. struct drm_i915_gem_object *obj,
  9884. struct drm_i915_gem_request *req,
  9885. uint32_t flags)
  9886. {
  9887. struct intel_ring *ring = req->ring;
  9888. struct drm_i915_private *dev_priv = to_i915(dev);
  9889. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9890. uint32_t pf, pipesrc;
  9891. int ret;
  9892. ret = intel_ring_begin(req, 4);
  9893. if (ret)
  9894. return ret;
  9895. /* i965+ uses the linear or tiled offsets from the
  9896. * Display Registers (which do not change across a page-flip)
  9897. * so we need only reprogram the base address.
  9898. */
  9899. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9900. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9901. intel_ring_emit(ring, fb->pitches[0]);
  9902. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
  9903. intel_fb_modifier_to_tiling(fb->modifier));
  9904. /* XXX Enabling the panel-fitter across page-flip is so far
  9905. * untested on non-native modes, so ignore it for now.
  9906. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9907. */
  9908. pf = 0;
  9909. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9910. intel_ring_emit(ring, pf | pipesrc);
  9911. return 0;
  9912. }
  9913. static int intel_gen6_queue_flip(struct drm_device *dev,
  9914. struct drm_crtc *crtc,
  9915. struct drm_framebuffer *fb,
  9916. struct drm_i915_gem_object *obj,
  9917. struct drm_i915_gem_request *req,
  9918. uint32_t flags)
  9919. {
  9920. struct intel_ring *ring = req->ring;
  9921. struct drm_i915_private *dev_priv = to_i915(dev);
  9922. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9923. uint32_t pf, pipesrc;
  9924. int ret;
  9925. ret = intel_ring_begin(req, 4);
  9926. if (ret)
  9927. return ret;
  9928. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9929. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9930. intel_ring_emit(ring, fb->pitches[0] |
  9931. intel_fb_modifier_to_tiling(fb->modifier));
  9932. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9933. /* Contrary to the suggestions in the documentation,
  9934. * "Enable Panel Fitter" does not seem to be required when page
  9935. * flipping with a non-native mode, and worse causes a normal
  9936. * modeset to fail.
  9937. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9938. */
  9939. pf = 0;
  9940. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9941. intel_ring_emit(ring, pf | pipesrc);
  9942. return 0;
  9943. }
  9944. static int intel_gen7_queue_flip(struct drm_device *dev,
  9945. struct drm_crtc *crtc,
  9946. struct drm_framebuffer *fb,
  9947. struct drm_i915_gem_object *obj,
  9948. struct drm_i915_gem_request *req,
  9949. uint32_t flags)
  9950. {
  9951. struct drm_i915_private *dev_priv = to_i915(dev);
  9952. struct intel_ring *ring = req->ring;
  9953. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9954. uint32_t plane_bit = 0;
  9955. int len, ret;
  9956. switch (intel_crtc->plane) {
  9957. case PLANE_A:
  9958. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9959. break;
  9960. case PLANE_B:
  9961. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9962. break;
  9963. case PLANE_C:
  9964. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9965. break;
  9966. default:
  9967. WARN_ONCE(1, "unknown plane in flip command\n");
  9968. return -ENODEV;
  9969. }
  9970. len = 4;
  9971. if (req->engine->id == RCS) {
  9972. len += 6;
  9973. /*
  9974. * On Gen 8, SRM is now taking an extra dword to accommodate
  9975. * 48bits addresses, and we need a NOOP for the batch size to
  9976. * stay even.
  9977. */
  9978. if (IS_GEN8(dev_priv))
  9979. len += 2;
  9980. }
  9981. /*
  9982. * BSpec MI_DISPLAY_FLIP for IVB:
  9983. * "The full packet must be contained within the same cache line."
  9984. *
  9985. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9986. * cacheline, if we ever start emitting more commands before
  9987. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9988. * then do the cacheline alignment, and finally emit the
  9989. * MI_DISPLAY_FLIP.
  9990. */
  9991. ret = intel_ring_cacheline_align(req);
  9992. if (ret)
  9993. return ret;
  9994. ret = intel_ring_begin(req, len);
  9995. if (ret)
  9996. return ret;
  9997. /* Unmask the flip-done completion message. Note that the bspec says that
  9998. * we should do this for both the BCS and RCS, and that we must not unmask
  9999. * more than one flip event at any time (or ensure that one flip message
  10000. * can be sent by waiting for flip-done prior to queueing new flips).
  10001. * Experimentation says that BCS works despite DERRMR masking all
  10002. * flip-done completion events and that unmasking all planes at once
  10003. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  10004. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  10005. */
  10006. if (req->engine->id == RCS) {
  10007. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  10008. intel_ring_emit_reg(ring, DERRMR);
  10009. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  10010. DERRMR_PIPEB_PRI_FLIP_DONE |
  10011. DERRMR_PIPEC_PRI_FLIP_DONE));
  10012. if (IS_GEN8(dev_priv))
  10013. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  10014. MI_SRM_LRM_GLOBAL_GTT);
  10015. else
  10016. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  10017. MI_SRM_LRM_GLOBAL_GTT);
  10018. intel_ring_emit_reg(ring, DERRMR);
  10019. intel_ring_emit(ring,
  10020. i915_ggtt_offset(req->engine->scratch) + 256);
  10021. if (IS_GEN8(dev_priv)) {
  10022. intel_ring_emit(ring, 0);
  10023. intel_ring_emit(ring, MI_NOOP);
  10024. }
  10025. }
  10026. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  10027. intel_ring_emit(ring, fb->pitches[0] |
  10028. intel_fb_modifier_to_tiling(fb->modifier));
  10029. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  10030. intel_ring_emit(ring, (MI_NOOP));
  10031. return 0;
  10032. }
  10033. static bool use_mmio_flip(struct intel_engine_cs *engine,
  10034. struct drm_i915_gem_object *obj)
  10035. {
  10036. /*
  10037. * This is not being used for older platforms, because
  10038. * non-availability of flip done interrupt forces us to use
  10039. * CS flips. Older platforms derive flip done using some clever
  10040. * tricks involving the flip_pending status bits and vblank irqs.
  10041. * So using MMIO flips there would disrupt this mechanism.
  10042. */
  10043. if (engine == NULL)
  10044. return true;
  10045. if (INTEL_GEN(engine->i915) < 5)
  10046. return false;
  10047. if (i915.use_mmio_flip < 0)
  10048. return false;
  10049. else if (i915.use_mmio_flip > 0)
  10050. return true;
  10051. else if (i915.enable_execlists)
  10052. return true;
  10053. return engine != i915_gem_object_last_write_engine(obj);
  10054. }
  10055. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  10056. unsigned int rotation,
  10057. struct intel_flip_work *work)
  10058. {
  10059. struct drm_device *dev = intel_crtc->base.dev;
  10060. struct drm_i915_private *dev_priv = to_i915(dev);
  10061. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10062. const enum pipe pipe = intel_crtc->pipe;
  10063. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  10064. ctl = I915_READ(PLANE_CTL(pipe, 0));
  10065. ctl &= ~PLANE_CTL_TILED_MASK;
  10066. switch (fb->modifier) {
  10067. case DRM_FORMAT_MOD_NONE:
  10068. break;
  10069. case I915_FORMAT_MOD_X_TILED:
  10070. ctl |= PLANE_CTL_TILED_X;
  10071. break;
  10072. case I915_FORMAT_MOD_Y_TILED:
  10073. ctl |= PLANE_CTL_TILED_Y;
  10074. break;
  10075. case I915_FORMAT_MOD_Yf_TILED:
  10076. ctl |= PLANE_CTL_TILED_YF;
  10077. break;
  10078. default:
  10079. MISSING_CASE(fb->modifier);
  10080. }
  10081. /*
  10082. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  10083. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  10084. */
  10085. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  10086. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  10087. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  10088. POSTING_READ(PLANE_SURF(pipe, 0));
  10089. }
  10090. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  10091. struct intel_flip_work *work)
  10092. {
  10093. struct drm_device *dev = intel_crtc->base.dev;
  10094. struct drm_i915_private *dev_priv = to_i915(dev);
  10095. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10096. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  10097. u32 dspcntr;
  10098. dspcntr = I915_READ(reg);
  10099. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  10100. dspcntr |= DISPPLANE_TILED;
  10101. else
  10102. dspcntr &= ~DISPPLANE_TILED;
  10103. I915_WRITE(reg, dspcntr);
  10104. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  10105. POSTING_READ(DSPSURF(intel_crtc->plane));
  10106. }
  10107. static void intel_mmio_flip_work_func(struct work_struct *w)
  10108. {
  10109. struct intel_flip_work *work =
  10110. container_of(w, struct intel_flip_work, mmio_work);
  10111. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  10112. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10113. struct intel_framebuffer *intel_fb =
  10114. to_intel_framebuffer(crtc->base.primary->fb);
  10115. struct drm_i915_gem_object *obj = intel_fb->obj;
  10116. WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
  10117. intel_pipe_update_start(crtc);
  10118. if (INTEL_GEN(dev_priv) >= 9)
  10119. skl_do_mmio_flip(crtc, work->rotation, work);
  10120. else
  10121. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  10122. ilk_do_mmio_flip(crtc, work);
  10123. intel_pipe_update_end(crtc, work);
  10124. }
  10125. static int intel_default_queue_flip(struct drm_device *dev,
  10126. struct drm_crtc *crtc,
  10127. struct drm_framebuffer *fb,
  10128. struct drm_i915_gem_object *obj,
  10129. struct drm_i915_gem_request *req,
  10130. uint32_t flags)
  10131. {
  10132. return -ENODEV;
  10133. }
  10134. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  10135. struct intel_crtc *intel_crtc,
  10136. struct intel_flip_work *work)
  10137. {
  10138. u32 addr, vblank;
  10139. if (!atomic_read(&work->pending))
  10140. return false;
  10141. smp_rmb();
  10142. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  10143. if (work->flip_ready_vblank == 0) {
  10144. if (work->flip_queued_req &&
  10145. !i915_gem_request_completed(work->flip_queued_req))
  10146. return false;
  10147. work->flip_ready_vblank = vblank;
  10148. }
  10149. if (vblank - work->flip_ready_vblank < 3)
  10150. return false;
  10151. /* Potential stall - if we see that the flip has happened,
  10152. * assume a missed interrupt. */
  10153. if (INTEL_GEN(dev_priv) >= 4)
  10154. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  10155. else
  10156. addr = I915_READ(DSPADDR(intel_crtc->plane));
  10157. /* There is a potential issue here with a false positive after a flip
  10158. * to the same address. We could address this by checking for a
  10159. * non-incrementing frame counter.
  10160. */
  10161. return addr == work->gtt_offset;
  10162. }
  10163. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  10164. {
  10165. struct drm_device *dev = &dev_priv->drm;
  10166. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  10167. struct intel_flip_work *work;
  10168. WARN_ON(!in_interrupt());
  10169. if (crtc == NULL)
  10170. return;
  10171. spin_lock(&dev->event_lock);
  10172. work = crtc->flip_work;
  10173. if (work != NULL && !is_mmio_work(work) &&
  10174. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  10175. WARN_ONCE(1,
  10176. "Kicking stuck page flip: queued at %d, now %d\n",
  10177. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  10178. page_flip_completed(crtc);
  10179. work = NULL;
  10180. }
  10181. if (work != NULL && !is_mmio_work(work) &&
  10182. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  10183. intel_queue_rps_boost_for_request(work->flip_queued_req);
  10184. spin_unlock(&dev->event_lock);
  10185. }
  10186. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  10187. struct drm_framebuffer *fb,
  10188. struct drm_pending_vblank_event *event,
  10189. uint32_t page_flip_flags)
  10190. {
  10191. struct drm_device *dev = crtc->dev;
  10192. struct drm_i915_private *dev_priv = to_i915(dev);
  10193. struct drm_framebuffer *old_fb = crtc->primary->fb;
  10194. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10195. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10196. struct drm_plane *primary = crtc->primary;
  10197. enum pipe pipe = intel_crtc->pipe;
  10198. struct intel_flip_work *work;
  10199. struct intel_engine_cs *engine;
  10200. bool mmio_flip;
  10201. struct drm_i915_gem_request *request;
  10202. struct i915_vma *vma;
  10203. int ret;
  10204. /*
  10205. * drm_mode_page_flip_ioctl() should already catch this, but double
  10206. * check to be safe. In the future we may enable pageflipping from
  10207. * a disabled primary plane.
  10208. */
  10209. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  10210. return -EBUSY;
  10211. /* Can't change pixel format via MI display flips. */
  10212. if (fb->format != crtc->primary->fb->format)
  10213. return -EINVAL;
  10214. /*
  10215. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  10216. * Note that pitch changes could also affect these register.
  10217. */
  10218. if (INTEL_GEN(dev_priv) > 3 &&
  10219. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  10220. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  10221. return -EINVAL;
  10222. if (i915_terminally_wedged(&dev_priv->gpu_error))
  10223. goto out_hang;
  10224. work = kzalloc(sizeof(*work), GFP_KERNEL);
  10225. if (work == NULL)
  10226. return -ENOMEM;
  10227. work->event = event;
  10228. work->crtc = crtc;
  10229. work->old_fb = old_fb;
  10230. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  10231. ret = drm_crtc_vblank_get(crtc);
  10232. if (ret)
  10233. goto free_work;
  10234. /* We borrow the event spin lock for protecting flip_work */
  10235. spin_lock_irq(&dev->event_lock);
  10236. if (intel_crtc->flip_work) {
  10237. /* Before declaring the flip queue wedged, check if
  10238. * the hardware completed the operation behind our backs.
  10239. */
  10240. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  10241. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  10242. page_flip_completed(intel_crtc);
  10243. } else {
  10244. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  10245. spin_unlock_irq(&dev->event_lock);
  10246. drm_crtc_vblank_put(crtc);
  10247. kfree(work);
  10248. return -EBUSY;
  10249. }
  10250. }
  10251. intel_crtc->flip_work = work;
  10252. spin_unlock_irq(&dev->event_lock);
  10253. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  10254. flush_workqueue(dev_priv->wq);
  10255. /* Reference the objects for the scheduled work. */
  10256. drm_framebuffer_reference(work->old_fb);
  10257. crtc->primary->fb = fb;
  10258. update_state_fb(crtc->primary);
  10259. work->pending_flip_obj = i915_gem_object_get(obj);
  10260. ret = i915_mutex_lock_interruptible(dev);
  10261. if (ret)
  10262. goto cleanup;
  10263. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  10264. if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
  10265. ret = -EIO;
  10266. goto unlock;
  10267. }
  10268. atomic_inc(&intel_crtc->unpin_work_count);
  10269. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  10270. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  10271. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  10272. engine = dev_priv->engine[BCS];
  10273. if (fb->modifier != old_fb->modifier)
  10274. /* vlv: DISPLAY_FLIP fails to change tiling */
  10275. engine = NULL;
  10276. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  10277. engine = dev_priv->engine[BCS];
  10278. } else if (INTEL_GEN(dev_priv) >= 7) {
  10279. engine = i915_gem_object_last_write_engine(obj);
  10280. if (engine == NULL || engine->id != RCS)
  10281. engine = dev_priv->engine[BCS];
  10282. } else {
  10283. engine = dev_priv->engine[RCS];
  10284. }
  10285. mmio_flip = use_mmio_flip(engine, obj);
  10286. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  10287. if (IS_ERR(vma)) {
  10288. ret = PTR_ERR(vma);
  10289. goto cleanup_pending;
  10290. }
  10291. work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
  10292. work->gtt_offset += intel_crtc->dspaddr_offset;
  10293. work->rotation = crtc->primary->state->rotation;
  10294. /*
  10295. * There's the potential that the next frame will not be compatible with
  10296. * FBC, so we want to call pre_update() before the actual page flip.
  10297. * The problem is that pre_update() caches some information about the fb
  10298. * object, so we want to do this only after the object is pinned. Let's
  10299. * be on the safe side and do this immediately before scheduling the
  10300. * flip.
  10301. */
  10302. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  10303. to_intel_plane_state(primary->state));
  10304. if (mmio_flip) {
  10305. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  10306. queue_work(system_unbound_wq, &work->mmio_work);
  10307. } else {
  10308. request = i915_gem_request_alloc(engine,
  10309. dev_priv->kernel_context);
  10310. if (IS_ERR(request)) {
  10311. ret = PTR_ERR(request);
  10312. goto cleanup_unpin;
  10313. }
  10314. ret = i915_gem_request_await_object(request, obj, false);
  10315. if (ret)
  10316. goto cleanup_request;
  10317. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  10318. page_flip_flags);
  10319. if (ret)
  10320. goto cleanup_request;
  10321. intel_mark_page_flip_active(intel_crtc, work);
  10322. work->flip_queued_req = i915_gem_request_get(request);
  10323. i915_add_request_no_flush(request);
  10324. }
  10325. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10326. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  10327. to_intel_plane(primary)->frontbuffer_bit);
  10328. mutex_unlock(&dev->struct_mutex);
  10329. intel_frontbuffer_flip_prepare(to_i915(dev),
  10330. to_intel_plane(primary)->frontbuffer_bit);
  10331. trace_i915_flip_request(intel_crtc->plane, obj);
  10332. return 0;
  10333. cleanup_request:
  10334. i915_add_request_no_flush(request);
  10335. cleanup_unpin:
  10336. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  10337. cleanup_pending:
  10338. atomic_dec(&intel_crtc->unpin_work_count);
  10339. unlock:
  10340. mutex_unlock(&dev->struct_mutex);
  10341. cleanup:
  10342. crtc->primary->fb = old_fb;
  10343. update_state_fb(crtc->primary);
  10344. i915_gem_object_put(obj);
  10345. drm_framebuffer_unreference(work->old_fb);
  10346. spin_lock_irq(&dev->event_lock);
  10347. intel_crtc->flip_work = NULL;
  10348. spin_unlock_irq(&dev->event_lock);
  10349. drm_crtc_vblank_put(crtc);
  10350. free_work:
  10351. kfree(work);
  10352. if (ret == -EIO) {
  10353. struct drm_atomic_state *state;
  10354. struct drm_plane_state *plane_state;
  10355. out_hang:
  10356. state = drm_atomic_state_alloc(dev);
  10357. if (!state)
  10358. return -ENOMEM;
  10359. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  10360. retry:
  10361. plane_state = drm_atomic_get_plane_state(state, primary);
  10362. ret = PTR_ERR_OR_ZERO(plane_state);
  10363. if (!ret) {
  10364. drm_atomic_set_fb_for_plane(plane_state, fb);
  10365. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  10366. if (!ret)
  10367. ret = drm_atomic_commit(state);
  10368. }
  10369. if (ret == -EDEADLK) {
  10370. drm_modeset_backoff(state->acquire_ctx);
  10371. drm_atomic_state_clear(state);
  10372. goto retry;
  10373. }
  10374. drm_atomic_state_put(state);
  10375. if (ret == 0 && event) {
  10376. spin_lock_irq(&dev->event_lock);
  10377. drm_crtc_send_vblank_event(crtc, event);
  10378. spin_unlock_irq(&dev->event_lock);
  10379. }
  10380. }
  10381. return ret;
  10382. }
  10383. /**
  10384. * intel_wm_need_update - Check whether watermarks need updating
  10385. * @plane: drm plane
  10386. * @state: new plane state
  10387. *
  10388. * Check current plane state versus the new one to determine whether
  10389. * watermarks need to be recalculated.
  10390. *
  10391. * Returns true or false.
  10392. */
  10393. static bool intel_wm_need_update(struct drm_plane *plane,
  10394. struct drm_plane_state *state)
  10395. {
  10396. struct intel_plane_state *new = to_intel_plane_state(state);
  10397. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  10398. /* Update watermarks on tiling or size changes. */
  10399. if (new->base.visible != cur->base.visible)
  10400. return true;
  10401. if (!cur->base.fb || !new->base.fb)
  10402. return false;
  10403. if (cur->base.fb->modifier != new->base.fb->modifier ||
  10404. cur->base.rotation != new->base.rotation ||
  10405. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  10406. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  10407. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  10408. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  10409. return true;
  10410. return false;
  10411. }
  10412. static bool needs_scaling(struct intel_plane_state *state)
  10413. {
  10414. int src_w = drm_rect_width(&state->base.src) >> 16;
  10415. int src_h = drm_rect_height(&state->base.src) >> 16;
  10416. int dst_w = drm_rect_width(&state->base.dst);
  10417. int dst_h = drm_rect_height(&state->base.dst);
  10418. return (src_w != dst_w || src_h != dst_h);
  10419. }
  10420. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  10421. struct drm_plane_state *plane_state)
  10422. {
  10423. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  10424. struct drm_crtc *crtc = crtc_state->crtc;
  10425. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10426. struct drm_plane *plane = plane_state->plane;
  10427. struct drm_device *dev = crtc->dev;
  10428. struct drm_i915_private *dev_priv = to_i915(dev);
  10429. struct intel_plane_state *old_plane_state =
  10430. to_intel_plane_state(plane->state);
  10431. bool mode_changed = needs_modeset(crtc_state);
  10432. bool was_crtc_enabled = crtc->state->active;
  10433. bool is_crtc_enabled = crtc_state->active;
  10434. bool turn_off, turn_on, visible, was_visible;
  10435. struct drm_framebuffer *fb = plane_state->fb;
  10436. int ret;
  10437. if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  10438. ret = skl_update_scaler_plane(
  10439. to_intel_crtc_state(crtc_state),
  10440. to_intel_plane_state(plane_state));
  10441. if (ret)
  10442. return ret;
  10443. }
  10444. was_visible = old_plane_state->base.visible;
  10445. visible = plane_state->visible;
  10446. if (!was_crtc_enabled && WARN_ON(was_visible))
  10447. was_visible = false;
  10448. /*
  10449. * Visibility is calculated as if the crtc was on, but
  10450. * after scaler setup everything depends on it being off
  10451. * when the crtc isn't active.
  10452. *
  10453. * FIXME this is wrong for watermarks. Watermarks should also
  10454. * be computed as if the pipe would be active. Perhaps move
  10455. * per-plane wm computation to the .check_plane() hook, and
  10456. * only combine the results from all planes in the current place?
  10457. */
  10458. if (!is_crtc_enabled)
  10459. plane_state->visible = visible = false;
  10460. if (!was_visible && !visible)
  10461. return 0;
  10462. if (fb != old_plane_state->base.fb)
  10463. pipe_config->fb_changed = true;
  10464. turn_off = was_visible && (!visible || mode_changed);
  10465. turn_on = visible && (!was_visible || mode_changed);
  10466. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  10467. intel_crtc->base.base.id,
  10468. intel_crtc->base.name,
  10469. plane->base.id, plane->name,
  10470. fb ? fb->base.id : -1);
  10471. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  10472. plane->base.id, plane->name,
  10473. was_visible, visible,
  10474. turn_off, turn_on, mode_changed);
  10475. if (turn_on) {
  10476. pipe_config->update_wm_pre = true;
  10477. /* must disable cxsr around plane enable/disable */
  10478. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10479. pipe_config->disable_cxsr = true;
  10480. } else if (turn_off) {
  10481. pipe_config->update_wm_post = true;
  10482. /* must disable cxsr around plane enable/disable */
  10483. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10484. pipe_config->disable_cxsr = true;
  10485. } else if (intel_wm_need_update(plane, plane_state)) {
  10486. /* FIXME bollocks */
  10487. pipe_config->update_wm_pre = true;
  10488. pipe_config->update_wm_post = true;
  10489. }
  10490. /* Pre-gen9 platforms need two-step watermark updates */
  10491. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10492. INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
  10493. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10494. if (visible || was_visible)
  10495. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10496. /*
  10497. * WaCxSRDisabledForSpriteScaling:ivb
  10498. *
  10499. * cstate->update_wm was already set above, so this flag will
  10500. * take effect when we commit and program watermarks.
  10501. */
  10502. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
  10503. needs_scaling(to_intel_plane_state(plane_state)) &&
  10504. !needs_scaling(old_plane_state))
  10505. pipe_config->disable_lp_wm = true;
  10506. return 0;
  10507. }
  10508. static bool encoders_cloneable(const struct intel_encoder *a,
  10509. const struct intel_encoder *b)
  10510. {
  10511. /* masks could be asymmetric, so check both ways */
  10512. return a == b || (a->cloneable & (1 << b->type) &&
  10513. b->cloneable & (1 << a->type));
  10514. }
  10515. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10516. struct intel_crtc *crtc,
  10517. struct intel_encoder *encoder)
  10518. {
  10519. struct intel_encoder *source_encoder;
  10520. struct drm_connector *connector;
  10521. struct drm_connector_state *connector_state;
  10522. int i;
  10523. for_each_connector_in_state(state, connector, connector_state, i) {
  10524. if (connector_state->crtc != &crtc->base)
  10525. continue;
  10526. source_encoder =
  10527. to_intel_encoder(connector_state->best_encoder);
  10528. if (!encoders_cloneable(encoder, source_encoder))
  10529. return false;
  10530. }
  10531. return true;
  10532. }
  10533. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10534. struct drm_crtc_state *crtc_state)
  10535. {
  10536. struct drm_device *dev = crtc->dev;
  10537. struct drm_i915_private *dev_priv = to_i915(dev);
  10538. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10539. struct intel_crtc_state *pipe_config =
  10540. to_intel_crtc_state(crtc_state);
  10541. struct drm_atomic_state *state = crtc_state->state;
  10542. int ret;
  10543. bool mode_changed = needs_modeset(crtc_state);
  10544. if (mode_changed && !crtc_state->active)
  10545. pipe_config->update_wm_post = true;
  10546. if (mode_changed && crtc_state->enable &&
  10547. dev_priv->display.crtc_compute_clock &&
  10548. !WARN_ON(pipe_config->shared_dpll)) {
  10549. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10550. pipe_config);
  10551. if (ret)
  10552. return ret;
  10553. }
  10554. if (crtc_state->color_mgmt_changed) {
  10555. ret = intel_color_check(crtc, crtc_state);
  10556. if (ret)
  10557. return ret;
  10558. /*
  10559. * Changing color management on Intel hardware is
  10560. * handled as part of planes update.
  10561. */
  10562. crtc_state->planes_changed = true;
  10563. }
  10564. ret = 0;
  10565. if (dev_priv->display.compute_pipe_wm) {
  10566. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10567. if (ret) {
  10568. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10569. return ret;
  10570. }
  10571. }
  10572. if (dev_priv->display.compute_intermediate_wm &&
  10573. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10574. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10575. return 0;
  10576. /*
  10577. * Calculate 'intermediate' watermarks that satisfy both the
  10578. * old state and the new state. We can program these
  10579. * immediately.
  10580. */
  10581. ret = dev_priv->display.compute_intermediate_wm(dev,
  10582. intel_crtc,
  10583. pipe_config);
  10584. if (ret) {
  10585. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10586. return ret;
  10587. }
  10588. } else if (dev_priv->display.compute_intermediate_wm) {
  10589. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10590. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10591. }
  10592. if (INTEL_GEN(dev_priv) >= 9) {
  10593. if (mode_changed)
  10594. ret = skl_update_scaler_crtc(pipe_config);
  10595. if (!ret)
  10596. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10597. pipe_config);
  10598. }
  10599. return ret;
  10600. }
  10601. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10602. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10603. .atomic_begin = intel_begin_crtc_commit,
  10604. .atomic_flush = intel_finish_crtc_commit,
  10605. .atomic_check = intel_crtc_atomic_check,
  10606. };
  10607. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10608. {
  10609. struct intel_connector *connector;
  10610. for_each_intel_connector(dev, connector) {
  10611. if (connector->base.state->crtc)
  10612. drm_connector_unreference(&connector->base);
  10613. if (connector->base.encoder) {
  10614. connector->base.state->best_encoder =
  10615. connector->base.encoder;
  10616. connector->base.state->crtc =
  10617. connector->base.encoder->crtc;
  10618. drm_connector_reference(&connector->base);
  10619. } else {
  10620. connector->base.state->best_encoder = NULL;
  10621. connector->base.state->crtc = NULL;
  10622. }
  10623. }
  10624. }
  10625. static void
  10626. connected_sink_compute_bpp(struct intel_connector *connector,
  10627. struct intel_crtc_state *pipe_config)
  10628. {
  10629. const struct drm_display_info *info = &connector->base.display_info;
  10630. int bpp = pipe_config->pipe_bpp;
  10631. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10632. connector->base.base.id,
  10633. connector->base.name);
  10634. /* Don't use an invalid EDID bpc value */
  10635. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  10636. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10637. bpp, info->bpc * 3);
  10638. pipe_config->pipe_bpp = info->bpc * 3;
  10639. }
  10640. /* Clamp bpp to 8 on screens without EDID 1.4 */
  10641. if (info->bpc == 0 && bpp > 24) {
  10642. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  10643. bpp);
  10644. pipe_config->pipe_bpp = 24;
  10645. }
  10646. }
  10647. static int
  10648. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10649. struct intel_crtc_state *pipe_config)
  10650. {
  10651. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10652. struct drm_atomic_state *state;
  10653. struct drm_connector *connector;
  10654. struct drm_connector_state *connector_state;
  10655. int bpp, i;
  10656. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  10657. IS_CHERRYVIEW(dev_priv)))
  10658. bpp = 10*3;
  10659. else if (INTEL_GEN(dev_priv) >= 5)
  10660. bpp = 12*3;
  10661. else
  10662. bpp = 8*3;
  10663. pipe_config->pipe_bpp = bpp;
  10664. state = pipe_config->base.state;
  10665. /* Clamp display bpp to EDID value */
  10666. for_each_connector_in_state(state, connector, connector_state, i) {
  10667. if (connector_state->crtc != &crtc->base)
  10668. continue;
  10669. connected_sink_compute_bpp(to_intel_connector(connector),
  10670. pipe_config);
  10671. }
  10672. return bpp;
  10673. }
  10674. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10675. {
  10676. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10677. "type: 0x%x flags: 0x%x\n",
  10678. mode->crtc_clock,
  10679. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10680. mode->crtc_hsync_end, mode->crtc_htotal,
  10681. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10682. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10683. }
  10684. static inline void
  10685. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  10686. unsigned int lane_count, struct intel_link_m_n *m_n)
  10687. {
  10688. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10689. id, lane_count,
  10690. m_n->gmch_m, m_n->gmch_n,
  10691. m_n->link_m, m_n->link_n, m_n->tu);
  10692. }
  10693. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10694. struct intel_crtc_state *pipe_config,
  10695. const char *context)
  10696. {
  10697. struct drm_device *dev = crtc->base.dev;
  10698. struct drm_i915_private *dev_priv = to_i915(dev);
  10699. struct drm_plane *plane;
  10700. struct intel_plane *intel_plane;
  10701. struct intel_plane_state *state;
  10702. struct drm_framebuffer *fb;
  10703. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  10704. crtc->base.base.id, crtc->base.name, context);
  10705. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  10706. transcoder_name(pipe_config->cpu_transcoder),
  10707. pipe_config->pipe_bpp, pipe_config->dither);
  10708. if (pipe_config->has_pch_encoder)
  10709. intel_dump_m_n_config(pipe_config, "fdi",
  10710. pipe_config->fdi_lanes,
  10711. &pipe_config->fdi_m_n);
  10712. if (intel_crtc_has_dp_encoder(pipe_config)) {
  10713. intel_dump_m_n_config(pipe_config, "dp m_n",
  10714. pipe_config->lane_count, &pipe_config->dp_m_n);
  10715. if (pipe_config->has_drrs)
  10716. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  10717. pipe_config->lane_count,
  10718. &pipe_config->dp_m2_n2);
  10719. }
  10720. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10721. pipe_config->has_audio, pipe_config->has_infoframe);
  10722. DRM_DEBUG_KMS("requested mode:\n");
  10723. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10724. DRM_DEBUG_KMS("adjusted mode:\n");
  10725. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10726. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10727. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d\n",
  10728. pipe_config->port_clock,
  10729. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10730. if (INTEL_GEN(dev_priv) >= 9)
  10731. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10732. crtc->num_scalers,
  10733. pipe_config->scaler_state.scaler_users,
  10734. pipe_config->scaler_state.scaler_id);
  10735. if (HAS_GMCH_DISPLAY(dev_priv))
  10736. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10737. pipe_config->gmch_pfit.control,
  10738. pipe_config->gmch_pfit.pgm_ratios,
  10739. pipe_config->gmch_pfit.lvds_border_bits);
  10740. else
  10741. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10742. pipe_config->pch_pfit.pos,
  10743. pipe_config->pch_pfit.size,
  10744. enableddisabled(pipe_config->pch_pfit.enabled));
  10745. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  10746. pipe_config->ips_enabled, pipe_config->double_wide);
  10747. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  10748. DRM_DEBUG_KMS("planes on this crtc\n");
  10749. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10750. struct drm_format_name_buf format_name;
  10751. intel_plane = to_intel_plane(plane);
  10752. if (intel_plane->pipe != crtc->pipe)
  10753. continue;
  10754. state = to_intel_plane_state(plane->state);
  10755. fb = state->base.fb;
  10756. if (!fb) {
  10757. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10758. plane->base.id, plane->name, state->scaler_id);
  10759. continue;
  10760. }
  10761. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  10762. plane->base.id, plane->name,
  10763. fb->base.id, fb->width, fb->height,
  10764. drm_get_format_name(fb->format->format, &format_name));
  10765. if (INTEL_GEN(dev_priv) >= 9)
  10766. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10767. state->scaler_id,
  10768. state->base.src.x1 >> 16,
  10769. state->base.src.y1 >> 16,
  10770. drm_rect_width(&state->base.src) >> 16,
  10771. drm_rect_height(&state->base.src) >> 16,
  10772. state->base.dst.x1, state->base.dst.y1,
  10773. drm_rect_width(&state->base.dst),
  10774. drm_rect_height(&state->base.dst));
  10775. }
  10776. }
  10777. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10778. {
  10779. struct drm_device *dev = state->dev;
  10780. struct drm_connector *connector;
  10781. unsigned int used_ports = 0;
  10782. unsigned int used_mst_ports = 0;
  10783. /*
  10784. * Walk the connector list instead of the encoder
  10785. * list to detect the problem on ddi platforms
  10786. * where there's just one encoder per digital port.
  10787. */
  10788. drm_for_each_connector(connector, dev) {
  10789. struct drm_connector_state *connector_state;
  10790. struct intel_encoder *encoder;
  10791. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10792. if (!connector_state)
  10793. connector_state = connector->state;
  10794. if (!connector_state->best_encoder)
  10795. continue;
  10796. encoder = to_intel_encoder(connector_state->best_encoder);
  10797. WARN_ON(!connector_state->crtc);
  10798. switch (encoder->type) {
  10799. unsigned int port_mask;
  10800. case INTEL_OUTPUT_UNKNOWN:
  10801. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  10802. break;
  10803. case INTEL_OUTPUT_DP:
  10804. case INTEL_OUTPUT_HDMI:
  10805. case INTEL_OUTPUT_EDP:
  10806. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10807. /* the same port mustn't appear more than once */
  10808. if (used_ports & port_mask)
  10809. return false;
  10810. used_ports |= port_mask;
  10811. break;
  10812. case INTEL_OUTPUT_DP_MST:
  10813. used_mst_ports |=
  10814. 1 << enc_to_mst(&encoder->base)->primary->port;
  10815. break;
  10816. default:
  10817. break;
  10818. }
  10819. }
  10820. /* can't mix MST and SST/HDMI on the same port */
  10821. if (used_ports & used_mst_ports)
  10822. return false;
  10823. return true;
  10824. }
  10825. static void
  10826. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10827. {
  10828. struct drm_crtc_state tmp_state;
  10829. struct intel_crtc_scaler_state scaler_state;
  10830. struct intel_dpll_hw_state dpll_hw_state;
  10831. struct intel_shared_dpll *shared_dpll;
  10832. bool force_thru;
  10833. /* FIXME: before the switch to atomic started, a new pipe_config was
  10834. * kzalloc'd. Code that depends on any field being zero should be
  10835. * fixed, so that the crtc_state can be safely duplicated. For now,
  10836. * only fields that are know to not cause problems are preserved. */
  10837. tmp_state = crtc_state->base;
  10838. scaler_state = crtc_state->scaler_state;
  10839. shared_dpll = crtc_state->shared_dpll;
  10840. dpll_hw_state = crtc_state->dpll_hw_state;
  10841. force_thru = crtc_state->pch_pfit.force_thru;
  10842. memset(crtc_state, 0, sizeof *crtc_state);
  10843. crtc_state->base = tmp_state;
  10844. crtc_state->scaler_state = scaler_state;
  10845. crtc_state->shared_dpll = shared_dpll;
  10846. crtc_state->dpll_hw_state = dpll_hw_state;
  10847. crtc_state->pch_pfit.force_thru = force_thru;
  10848. }
  10849. static int
  10850. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10851. struct intel_crtc_state *pipe_config)
  10852. {
  10853. struct drm_atomic_state *state = pipe_config->base.state;
  10854. struct intel_encoder *encoder;
  10855. struct drm_connector *connector;
  10856. struct drm_connector_state *connector_state;
  10857. int base_bpp, ret = -EINVAL;
  10858. int i;
  10859. bool retry = true;
  10860. clear_intel_crtc_state(pipe_config);
  10861. pipe_config->cpu_transcoder =
  10862. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10863. /*
  10864. * Sanitize sync polarity flags based on requested ones. If neither
  10865. * positive or negative polarity is requested, treat this as meaning
  10866. * negative polarity.
  10867. */
  10868. if (!(pipe_config->base.adjusted_mode.flags &
  10869. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10870. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10871. if (!(pipe_config->base.adjusted_mode.flags &
  10872. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10873. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10874. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10875. pipe_config);
  10876. if (base_bpp < 0)
  10877. goto fail;
  10878. /*
  10879. * Determine the real pipe dimensions. Note that stereo modes can
  10880. * increase the actual pipe size due to the frame doubling and
  10881. * insertion of additional space for blanks between the frame. This
  10882. * is stored in the crtc timings. We use the requested mode to do this
  10883. * computation to clearly distinguish it from the adjusted mode, which
  10884. * can be changed by the connectors in the below retry loop.
  10885. */
  10886. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10887. &pipe_config->pipe_src_w,
  10888. &pipe_config->pipe_src_h);
  10889. for_each_connector_in_state(state, connector, connector_state, i) {
  10890. if (connector_state->crtc != crtc)
  10891. continue;
  10892. encoder = to_intel_encoder(connector_state->best_encoder);
  10893. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  10894. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10895. goto fail;
  10896. }
  10897. /*
  10898. * Determine output_types before calling the .compute_config()
  10899. * hooks so that the hooks can use this information safely.
  10900. */
  10901. pipe_config->output_types |= 1 << encoder->type;
  10902. }
  10903. encoder_retry:
  10904. /* Ensure the port clock defaults are reset when retrying. */
  10905. pipe_config->port_clock = 0;
  10906. pipe_config->pixel_multiplier = 1;
  10907. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10908. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10909. CRTC_STEREO_DOUBLE);
  10910. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10911. * adjust it according to limitations or connector properties, and also
  10912. * a chance to reject the mode entirely.
  10913. */
  10914. for_each_connector_in_state(state, connector, connector_state, i) {
  10915. if (connector_state->crtc != crtc)
  10916. continue;
  10917. encoder = to_intel_encoder(connector_state->best_encoder);
  10918. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  10919. DRM_DEBUG_KMS("Encoder config failure\n");
  10920. goto fail;
  10921. }
  10922. }
  10923. /* Set default port clock if not overwritten by the encoder. Needs to be
  10924. * done afterwards in case the encoder adjusts the mode. */
  10925. if (!pipe_config->port_clock)
  10926. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10927. * pipe_config->pixel_multiplier;
  10928. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10929. if (ret < 0) {
  10930. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10931. goto fail;
  10932. }
  10933. if (ret == RETRY) {
  10934. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10935. ret = -EINVAL;
  10936. goto fail;
  10937. }
  10938. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10939. retry = false;
  10940. goto encoder_retry;
  10941. }
  10942. /* Dithering seems to not pass-through bits correctly when it should, so
  10943. * only enable it on 6bpc panels. */
  10944. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10945. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10946. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10947. fail:
  10948. return ret;
  10949. }
  10950. static void
  10951. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10952. {
  10953. struct drm_crtc *crtc;
  10954. struct drm_crtc_state *crtc_state;
  10955. int i;
  10956. /* Double check state. */
  10957. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10958. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10959. /* Update hwmode for vblank functions */
  10960. if (crtc->state->active)
  10961. crtc->hwmode = crtc->state->adjusted_mode;
  10962. else
  10963. crtc->hwmode.crtc_clock = 0;
  10964. /*
  10965. * Update legacy state to satisfy fbc code. This can
  10966. * be removed when fbc uses the atomic state.
  10967. */
  10968. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10969. struct drm_plane_state *plane_state = crtc->primary->state;
  10970. crtc->primary->fb = plane_state->fb;
  10971. crtc->x = plane_state->src_x >> 16;
  10972. crtc->y = plane_state->src_y >> 16;
  10973. }
  10974. }
  10975. }
  10976. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10977. {
  10978. int diff;
  10979. if (clock1 == clock2)
  10980. return true;
  10981. if (!clock1 || !clock2)
  10982. return false;
  10983. diff = abs(clock1 - clock2);
  10984. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10985. return true;
  10986. return false;
  10987. }
  10988. static bool
  10989. intel_compare_m_n(unsigned int m, unsigned int n,
  10990. unsigned int m2, unsigned int n2,
  10991. bool exact)
  10992. {
  10993. if (m == m2 && n == n2)
  10994. return true;
  10995. if (exact || !m || !n || !m2 || !n2)
  10996. return false;
  10997. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10998. if (n > n2) {
  10999. while (n > n2) {
  11000. m2 <<= 1;
  11001. n2 <<= 1;
  11002. }
  11003. } else if (n < n2) {
  11004. while (n < n2) {
  11005. m <<= 1;
  11006. n <<= 1;
  11007. }
  11008. }
  11009. if (n != n2)
  11010. return false;
  11011. return intel_fuzzy_clock_check(m, m2);
  11012. }
  11013. static bool
  11014. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  11015. struct intel_link_m_n *m2_n2,
  11016. bool adjust)
  11017. {
  11018. if (m_n->tu == m2_n2->tu &&
  11019. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  11020. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  11021. intel_compare_m_n(m_n->link_m, m_n->link_n,
  11022. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  11023. if (adjust)
  11024. *m2_n2 = *m_n;
  11025. return true;
  11026. }
  11027. return false;
  11028. }
  11029. static void __printf(3, 4)
  11030. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  11031. {
  11032. char *level;
  11033. unsigned int category;
  11034. struct va_format vaf;
  11035. va_list args;
  11036. if (adjust) {
  11037. level = KERN_DEBUG;
  11038. category = DRM_UT_KMS;
  11039. } else {
  11040. level = KERN_ERR;
  11041. category = DRM_UT_NONE;
  11042. }
  11043. va_start(args, format);
  11044. vaf.fmt = format;
  11045. vaf.va = &args;
  11046. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  11047. va_end(args);
  11048. }
  11049. static bool
  11050. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  11051. struct intel_crtc_state *current_config,
  11052. struct intel_crtc_state *pipe_config,
  11053. bool adjust)
  11054. {
  11055. bool ret = true;
  11056. #define PIPE_CONF_CHECK_X(name) \
  11057. if (current_config->name != pipe_config->name) { \
  11058. pipe_config_err(adjust, __stringify(name), \
  11059. "(expected 0x%08x, found 0x%08x)\n", \
  11060. current_config->name, \
  11061. pipe_config->name); \
  11062. ret = false; \
  11063. }
  11064. #define PIPE_CONF_CHECK_I(name) \
  11065. if (current_config->name != pipe_config->name) { \
  11066. pipe_config_err(adjust, __stringify(name), \
  11067. "(expected %i, found %i)\n", \
  11068. current_config->name, \
  11069. pipe_config->name); \
  11070. ret = false; \
  11071. }
  11072. #define PIPE_CONF_CHECK_P(name) \
  11073. if (current_config->name != pipe_config->name) { \
  11074. pipe_config_err(adjust, __stringify(name), \
  11075. "(expected %p, found %p)\n", \
  11076. current_config->name, \
  11077. pipe_config->name); \
  11078. ret = false; \
  11079. }
  11080. #define PIPE_CONF_CHECK_M_N(name) \
  11081. if (!intel_compare_link_m_n(&current_config->name, \
  11082. &pipe_config->name,\
  11083. adjust)) { \
  11084. pipe_config_err(adjust, __stringify(name), \
  11085. "(expected tu %i gmch %i/%i link %i/%i, " \
  11086. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11087. current_config->name.tu, \
  11088. current_config->name.gmch_m, \
  11089. current_config->name.gmch_n, \
  11090. current_config->name.link_m, \
  11091. current_config->name.link_n, \
  11092. pipe_config->name.tu, \
  11093. pipe_config->name.gmch_m, \
  11094. pipe_config->name.gmch_n, \
  11095. pipe_config->name.link_m, \
  11096. pipe_config->name.link_n); \
  11097. ret = false; \
  11098. }
  11099. /* This is required for BDW+ where there is only one set of registers for
  11100. * switching between high and low RR.
  11101. * This macro can be used whenever a comparison has to be made between one
  11102. * hw state and multiple sw state variables.
  11103. */
  11104. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  11105. if (!intel_compare_link_m_n(&current_config->name, \
  11106. &pipe_config->name, adjust) && \
  11107. !intel_compare_link_m_n(&current_config->alt_name, \
  11108. &pipe_config->name, adjust)) { \
  11109. pipe_config_err(adjust, __stringify(name), \
  11110. "(expected tu %i gmch %i/%i link %i/%i, " \
  11111. "or tu %i gmch %i/%i link %i/%i, " \
  11112. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11113. current_config->name.tu, \
  11114. current_config->name.gmch_m, \
  11115. current_config->name.gmch_n, \
  11116. current_config->name.link_m, \
  11117. current_config->name.link_n, \
  11118. current_config->alt_name.tu, \
  11119. current_config->alt_name.gmch_m, \
  11120. current_config->alt_name.gmch_n, \
  11121. current_config->alt_name.link_m, \
  11122. current_config->alt_name.link_n, \
  11123. pipe_config->name.tu, \
  11124. pipe_config->name.gmch_m, \
  11125. pipe_config->name.gmch_n, \
  11126. pipe_config->name.link_m, \
  11127. pipe_config->name.link_n); \
  11128. ret = false; \
  11129. }
  11130. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  11131. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  11132. pipe_config_err(adjust, __stringify(name), \
  11133. "(%x) (expected %i, found %i)\n", \
  11134. (mask), \
  11135. current_config->name & (mask), \
  11136. pipe_config->name & (mask)); \
  11137. ret = false; \
  11138. }
  11139. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  11140. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  11141. pipe_config_err(adjust, __stringify(name), \
  11142. "(expected %i, found %i)\n", \
  11143. current_config->name, \
  11144. pipe_config->name); \
  11145. ret = false; \
  11146. }
  11147. #define PIPE_CONF_QUIRK(quirk) \
  11148. ((current_config->quirks | pipe_config->quirks) & (quirk))
  11149. PIPE_CONF_CHECK_I(cpu_transcoder);
  11150. PIPE_CONF_CHECK_I(has_pch_encoder);
  11151. PIPE_CONF_CHECK_I(fdi_lanes);
  11152. PIPE_CONF_CHECK_M_N(fdi_m_n);
  11153. PIPE_CONF_CHECK_I(lane_count);
  11154. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  11155. if (INTEL_GEN(dev_priv) < 8) {
  11156. PIPE_CONF_CHECK_M_N(dp_m_n);
  11157. if (current_config->has_drrs)
  11158. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  11159. } else
  11160. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  11161. PIPE_CONF_CHECK_X(output_types);
  11162. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  11163. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  11164. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  11165. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  11166. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  11167. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  11168. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  11169. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  11170. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  11171. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  11172. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  11173. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  11174. PIPE_CONF_CHECK_I(pixel_multiplier);
  11175. PIPE_CONF_CHECK_I(has_hdmi_sink);
  11176. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  11177. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11178. PIPE_CONF_CHECK_I(limited_color_range);
  11179. PIPE_CONF_CHECK_I(has_infoframe);
  11180. PIPE_CONF_CHECK_I(has_audio);
  11181. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11182. DRM_MODE_FLAG_INTERLACE);
  11183. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  11184. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11185. DRM_MODE_FLAG_PHSYNC);
  11186. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11187. DRM_MODE_FLAG_NHSYNC);
  11188. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11189. DRM_MODE_FLAG_PVSYNC);
  11190. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11191. DRM_MODE_FLAG_NVSYNC);
  11192. }
  11193. PIPE_CONF_CHECK_X(gmch_pfit.control);
  11194. /* pfit ratios are autocomputed by the hw on gen4+ */
  11195. if (INTEL_GEN(dev_priv) < 4)
  11196. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  11197. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  11198. if (!adjust) {
  11199. PIPE_CONF_CHECK_I(pipe_src_w);
  11200. PIPE_CONF_CHECK_I(pipe_src_h);
  11201. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  11202. if (current_config->pch_pfit.enabled) {
  11203. PIPE_CONF_CHECK_X(pch_pfit.pos);
  11204. PIPE_CONF_CHECK_X(pch_pfit.size);
  11205. }
  11206. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  11207. }
  11208. /* BDW+ don't expose a synchronous way to read the state */
  11209. if (IS_HASWELL(dev_priv))
  11210. PIPE_CONF_CHECK_I(ips_enabled);
  11211. PIPE_CONF_CHECK_I(double_wide);
  11212. PIPE_CONF_CHECK_P(shared_dpll);
  11213. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  11214. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  11215. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  11216. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  11217. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  11218. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  11219. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  11220. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  11221. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  11222. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  11223. PIPE_CONF_CHECK_X(dsi_pll.div);
  11224. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  11225. PIPE_CONF_CHECK_I(pipe_bpp);
  11226. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  11227. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  11228. #undef PIPE_CONF_CHECK_X
  11229. #undef PIPE_CONF_CHECK_I
  11230. #undef PIPE_CONF_CHECK_P
  11231. #undef PIPE_CONF_CHECK_FLAGS
  11232. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  11233. #undef PIPE_CONF_QUIRK
  11234. return ret;
  11235. }
  11236. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  11237. const struct intel_crtc_state *pipe_config)
  11238. {
  11239. if (pipe_config->has_pch_encoder) {
  11240. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  11241. &pipe_config->fdi_m_n);
  11242. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  11243. /*
  11244. * FDI already provided one idea for the dotclock.
  11245. * Yell if the encoder disagrees.
  11246. */
  11247. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  11248. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  11249. fdi_dotclock, dotclock);
  11250. }
  11251. }
  11252. static void verify_wm_state(struct drm_crtc *crtc,
  11253. struct drm_crtc_state *new_state)
  11254. {
  11255. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11256. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  11257. struct skl_pipe_wm hw_wm, *sw_wm;
  11258. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  11259. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  11260. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11261. const enum pipe pipe = intel_crtc->pipe;
  11262. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  11263. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  11264. return;
  11265. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  11266. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  11267. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  11268. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  11269. /* planes */
  11270. for_each_universal_plane(dev_priv, pipe, plane) {
  11271. hw_plane_wm = &hw_wm.planes[plane];
  11272. sw_plane_wm = &sw_wm->planes[plane];
  11273. /* Watermarks */
  11274. for (level = 0; level <= max_level; level++) {
  11275. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11276. &sw_plane_wm->wm[level]))
  11277. continue;
  11278. 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",
  11279. pipe_name(pipe), plane + 1, level,
  11280. sw_plane_wm->wm[level].plane_en,
  11281. sw_plane_wm->wm[level].plane_res_b,
  11282. sw_plane_wm->wm[level].plane_res_l,
  11283. hw_plane_wm->wm[level].plane_en,
  11284. hw_plane_wm->wm[level].plane_res_b,
  11285. hw_plane_wm->wm[level].plane_res_l);
  11286. }
  11287. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11288. &sw_plane_wm->trans_wm)) {
  11289. 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",
  11290. pipe_name(pipe), plane + 1,
  11291. sw_plane_wm->trans_wm.plane_en,
  11292. sw_plane_wm->trans_wm.plane_res_b,
  11293. sw_plane_wm->trans_wm.plane_res_l,
  11294. hw_plane_wm->trans_wm.plane_en,
  11295. hw_plane_wm->trans_wm.plane_res_b,
  11296. hw_plane_wm->trans_wm.plane_res_l);
  11297. }
  11298. /* DDB */
  11299. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  11300. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  11301. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11302. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  11303. pipe_name(pipe), plane + 1,
  11304. sw_ddb_entry->start, sw_ddb_entry->end,
  11305. hw_ddb_entry->start, hw_ddb_entry->end);
  11306. }
  11307. }
  11308. /*
  11309. * cursor
  11310. * If the cursor plane isn't active, we may not have updated it's ddb
  11311. * allocation. In that case since the ddb allocation will be updated
  11312. * once the plane becomes visible, we can skip this check
  11313. */
  11314. if (intel_crtc->cursor_addr) {
  11315. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  11316. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  11317. /* Watermarks */
  11318. for (level = 0; level <= max_level; level++) {
  11319. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11320. &sw_plane_wm->wm[level]))
  11321. continue;
  11322. 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",
  11323. pipe_name(pipe), level,
  11324. sw_plane_wm->wm[level].plane_en,
  11325. sw_plane_wm->wm[level].plane_res_b,
  11326. sw_plane_wm->wm[level].plane_res_l,
  11327. hw_plane_wm->wm[level].plane_en,
  11328. hw_plane_wm->wm[level].plane_res_b,
  11329. hw_plane_wm->wm[level].plane_res_l);
  11330. }
  11331. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11332. &sw_plane_wm->trans_wm)) {
  11333. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  11334. pipe_name(pipe),
  11335. sw_plane_wm->trans_wm.plane_en,
  11336. sw_plane_wm->trans_wm.plane_res_b,
  11337. sw_plane_wm->trans_wm.plane_res_l,
  11338. hw_plane_wm->trans_wm.plane_en,
  11339. hw_plane_wm->trans_wm.plane_res_b,
  11340. hw_plane_wm->trans_wm.plane_res_l);
  11341. }
  11342. /* DDB */
  11343. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  11344. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  11345. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11346. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  11347. pipe_name(pipe),
  11348. sw_ddb_entry->start, sw_ddb_entry->end,
  11349. hw_ddb_entry->start, hw_ddb_entry->end);
  11350. }
  11351. }
  11352. }
  11353. static void
  11354. verify_connector_state(struct drm_device *dev,
  11355. struct drm_atomic_state *state,
  11356. struct drm_crtc *crtc)
  11357. {
  11358. struct drm_connector *connector;
  11359. struct drm_connector_state *old_conn_state;
  11360. int i;
  11361. for_each_connector_in_state(state, connector, old_conn_state, i) {
  11362. struct drm_encoder *encoder = connector->encoder;
  11363. struct drm_connector_state *state = connector->state;
  11364. if (state->crtc != crtc)
  11365. continue;
  11366. intel_connector_verify_state(to_intel_connector(connector));
  11367. I915_STATE_WARN(state->best_encoder != encoder,
  11368. "connector's atomic encoder doesn't match legacy encoder\n");
  11369. }
  11370. }
  11371. static void
  11372. verify_encoder_state(struct drm_device *dev)
  11373. {
  11374. struct intel_encoder *encoder;
  11375. struct intel_connector *connector;
  11376. for_each_intel_encoder(dev, encoder) {
  11377. bool enabled = false;
  11378. enum pipe pipe;
  11379. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  11380. encoder->base.base.id,
  11381. encoder->base.name);
  11382. for_each_intel_connector(dev, connector) {
  11383. if (connector->base.state->best_encoder != &encoder->base)
  11384. continue;
  11385. enabled = true;
  11386. I915_STATE_WARN(connector->base.state->crtc !=
  11387. encoder->base.crtc,
  11388. "connector's crtc doesn't match encoder crtc\n");
  11389. }
  11390. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  11391. "encoder's enabled state mismatch "
  11392. "(expected %i, found %i)\n",
  11393. !!encoder->base.crtc, enabled);
  11394. if (!encoder->base.crtc) {
  11395. bool active;
  11396. active = encoder->get_hw_state(encoder, &pipe);
  11397. I915_STATE_WARN(active,
  11398. "encoder detached but still enabled on pipe %c.\n",
  11399. pipe_name(pipe));
  11400. }
  11401. }
  11402. }
  11403. static void
  11404. verify_crtc_state(struct drm_crtc *crtc,
  11405. struct drm_crtc_state *old_crtc_state,
  11406. struct drm_crtc_state *new_crtc_state)
  11407. {
  11408. struct drm_device *dev = crtc->dev;
  11409. struct drm_i915_private *dev_priv = to_i915(dev);
  11410. struct intel_encoder *encoder;
  11411. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11412. struct intel_crtc_state *pipe_config, *sw_config;
  11413. struct drm_atomic_state *old_state;
  11414. bool active;
  11415. old_state = old_crtc_state->state;
  11416. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  11417. pipe_config = to_intel_crtc_state(old_crtc_state);
  11418. memset(pipe_config, 0, sizeof(*pipe_config));
  11419. pipe_config->base.crtc = crtc;
  11420. pipe_config->base.state = old_state;
  11421. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  11422. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  11423. /* hw state is inconsistent with the pipe quirk */
  11424. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  11425. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  11426. active = new_crtc_state->active;
  11427. I915_STATE_WARN(new_crtc_state->active != active,
  11428. "crtc active state doesn't match with hw state "
  11429. "(expected %i, found %i)\n", new_crtc_state->active, active);
  11430. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  11431. "transitional active state does not match atomic hw state "
  11432. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  11433. for_each_encoder_on_crtc(dev, crtc, encoder) {
  11434. enum pipe pipe;
  11435. active = encoder->get_hw_state(encoder, &pipe);
  11436. I915_STATE_WARN(active != new_crtc_state->active,
  11437. "[ENCODER:%i] active %i with crtc active %i\n",
  11438. encoder->base.base.id, active, new_crtc_state->active);
  11439. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  11440. "Encoder connected to wrong pipe %c\n",
  11441. pipe_name(pipe));
  11442. if (active) {
  11443. pipe_config->output_types |= 1 << encoder->type;
  11444. encoder->get_config(encoder, pipe_config);
  11445. }
  11446. }
  11447. if (!new_crtc_state->active)
  11448. return;
  11449. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  11450. sw_config = to_intel_crtc_state(crtc->state);
  11451. if (!intel_pipe_config_compare(dev_priv, sw_config,
  11452. pipe_config, false)) {
  11453. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  11454. intel_dump_pipe_config(intel_crtc, pipe_config,
  11455. "[hw state]");
  11456. intel_dump_pipe_config(intel_crtc, sw_config,
  11457. "[sw state]");
  11458. }
  11459. }
  11460. static void
  11461. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  11462. struct intel_shared_dpll *pll,
  11463. struct drm_crtc *crtc,
  11464. struct drm_crtc_state *new_state)
  11465. {
  11466. struct intel_dpll_hw_state dpll_hw_state;
  11467. unsigned crtc_mask;
  11468. bool active;
  11469. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  11470. DRM_DEBUG_KMS("%s\n", pll->name);
  11471. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  11472. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  11473. I915_STATE_WARN(!pll->on && pll->active_mask,
  11474. "pll in active use but not on in sw tracking\n");
  11475. I915_STATE_WARN(pll->on && !pll->active_mask,
  11476. "pll is on but not used by any active crtc\n");
  11477. I915_STATE_WARN(pll->on != active,
  11478. "pll on state mismatch (expected %i, found %i)\n",
  11479. pll->on, active);
  11480. }
  11481. if (!crtc) {
  11482. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  11483. "more active pll users than references: %x vs %x\n",
  11484. pll->active_mask, pll->state.crtc_mask);
  11485. return;
  11486. }
  11487. crtc_mask = 1 << drm_crtc_index(crtc);
  11488. if (new_state->active)
  11489. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  11490. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  11491. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11492. else
  11493. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11494. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  11495. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11496. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  11497. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  11498. crtc_mask, pll->state.crtc_mask);
  11499. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  11500. &dpll_hw_state,
  11501. sizeof(dpll_hw_state)),
  11502. "pll hw state mismatch\n");
  11503. }
  11504. static void
  11505. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  11506. struct drm_crtc_state *old_crtc_state,
  11507. struct drm_crtc_state *new_crtc_state)
  11508. {
  11509. struct drm_i915_private *dev_priv = to_i915(dev);
  11510. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  11511. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  11512. if (new_state->shared_dpll)
  11513. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  11514. if (old_state->shared_dpll &&
  11515. old_state->shared_dpll != new_state->shared_dpll) {
  11516. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  11517. struct intel_shared_dpll *pll = old_state->shared_dpll;
  11518. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11519. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  11520. pipe_name(drm_crtc_index(crtc)));
  11521. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  11522. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  11523. pipe_name(drm_crtc_index(crtc)));
  11524. }
  11525. }
  11526. static void
  11527. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11528. struct drm_atomic_state *state,
  11529. struct drm_crtc_state *old_state,
  11530. struct drm_crtc_state *new_state)
  11531. {
  11532. if (!needs_modeset(new_state) &&
  11533. !to_intel_crtc_state(new_state)->update_pipe)
  11534. return;
  11535. verify_wm_state(crtc, new_state);
  11536. verify_connector_state(crtc->dev, state, crtc);
  11537. verify_crtc_state(crtc, old_state, new_state);
  11538. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11539. }
  11540. static void
  11541. verify_disabled_dpll_state(struct drm_device *dev)
  11542. {
  11543. struct drm_i915_private *dev_priv = to_i915(dev);
  11544. int i;
  11545. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11546. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11547. }
  11548. static void
  11549. intel_modeset_verify_disabled(struct drm_device *dev,
  11550. struct drm_atomic_state *state)
  11551. {
  11552. verify_encoder_state(dev);
  11553. verify_connector_state(dev, state, NULL);
  11554. verify_disabled_dpll_state(dev);
  11555. }
  11556. static void update_scanline_offset(struct intel_crtc *crtc)
  11557. {
  11558. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11559. /*
  11560. * The scanline counter increments at the leading edge of hsync.
  11561. *
  11562. * On most platforms it starts counting from vtotal-1 on the
  11563. * first active line. That means the scanline counter value is
  11564. * always one less than what we would expect. Ie. just after
  11565. * start of vblank, which also occurs at start of hsync (on the
  11566. * last active line), the scanline counter will read vblank_start-1.
  11567. *
  11568. * On gen2 the scanline counter starts counting from 1 instead
  11569. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11570. * to keep the value positive), instead of adding one.
  11571. *
  11572. * On HSW+ the behaviour of the scanline counter depends on the output
  11573. * type. For DP ports it behaves like most other platforms, but on HDMI
  11574. * there's an extra 1 line difference. So we need to add two instead of
  11575. * one to the value.
  11576. */
  11577. if (IS_GEN2(dev_priv)) {
  11578. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11579. int vtotal;
  11580. vtotal = adjusted_mode->crtc_vtotal;
  11581. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11582. vtotal /= 2;
  11583. crtc->scanline_offset = vtotal - 1;
  11584. } else if (HAS_DDI(dev_priv) &&
  11585. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  11586. crtc->scanline_offset = 2;
  11587. } else
  11588. crtc->scanline_offset = 1;
  11589. }
  11590. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11591. {
  11592. struct drm_device *dev = state->dev;
  11593. struct drm_i915_private *dev_priv = to_i915(dev);
  11594. struct drm_crtc *crtc;
  11595. struct drm_crtc_state *crtc_state;
  11596. int i;
  11597. if (!dev_priv->display.crtc_compute_clock)
  11598. return;
  11599. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11600. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11601. struct intel_shared_dpll *old_dpll =
  11602. to_intel_crtc_state(crtc->state)->shared_dpll;
  11603. if (!needs_modeset(crtc_state))
  11604. continue;
  11605. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11606. if (!old_dpll)
  11607. continue;
  11608. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  11609. }
  11610. }
  11611. /*
  11612. * This implements the workaround described in the "notes" section of the mode
  11613. * set sequence documentation. When going from no pipes or single pipe to
  11614. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11615. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11616. */
  11617. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11618. {
  11619. struct drm_crtc_state *crtc_state;
  11620. struct intel_crtc *intel_crtc;
  11621. struct drm_crtc *crtc;
  11622. struct intel_crtc_state *first_crtc_state = NULL;
  11623. struct intel_crtc_state *other_crtc_state = NULL;
  11624. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11625. int i;
  11626. /* look at all crtc's that are going to be enabled in during modeset */
  11627. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11628. intel_crtc = to_intel_crtc(crtc);
  11629. if (!crtc_state->active || !needs_modeset(crtc_state))
  11630. continue;
  11631. if (first_crtc_state) {
  11632. other_crtc_state = to_intel_crtc_state(crtc_state);
  11633. break;
  11634. } else {
  11635. first_crtc_state = to_intel_crtc_state(crtc_state);
  11636. first_pipe = intel_crtc->pipe;
  11637. }
  11638. }
  11639. /* No workaround needed? */
  11640. if (!first_crtc_state)
  11641. return 0;
  11642. /* w/a possibly needed, check how many crtc's are already enabled. */
  11643. for_each_intel_crtc(state->dev, intel_crtc) {
  11644. struct intel_crtc_state *pipe_config;
  11645. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11646. if (IS_ERR(pipe_config))
  11647. return PTR_ERR(pipe_config);
  11648. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11649. if (!pipe_config->base.active ||
  11650. needs_modeset(&pipe_config->base))
  11651. continue;
  11652. /* 2 or more enabled crtcs means no need for w/a */
  11653. if (enabled_pipe != INVALID_PIPE)
  11654. return 0;
  11655. enabled_pipe = intel_crtc->pipe;
  11656. }
  11657. if (enabled_pipe != INVALID_PIPE)
  11658. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11659. else if (other_crtc_state)
  11660. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11661. return 0;
  11662. }
  11663. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  11664. {
  11665. struct drm_crtc *crtc;
  11666. /* Add all pipes to the state */
  11667. for_each_crtc(state->dev, crtc) {
  11668. struct drm_crtc_state *crtc_state;
  11669. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11670. if (IS_ERR(crtc_state))
  11671. return PTR_ERR(crtc_state);
  11672. }
  11673. return 0;
  11674. }
  11675. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11676. {
  11677. struct drm_crtc *crtc;
  11678. /*
  11679. * Add all pipes to the state, and force
  11680. * a modeset on all the active ones.
  11681. */
  11682. for_each_crtc(state->dev, crtc) {
  11683. struct drm_crtc_state *crtc_state;
  11684. int ret;
  11685. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11686. if (IS_ERR(crtc_state))
  11687. return PTR_ERR(crtc_state);
  11688. if (!crtc_state->active || needs_modeset(crtc_state))
  11689. continue;
  11690. crtc_state->mode_changed = true;
  11691. ret = drm_atomic_add_affected_connectors(state, crtc);
  11692. if (ret)
  11693. return ret;
  11694. ret = drm_atomic_add_affected_planes(state, crtc);
  11695. if (ret)
  11696. return ret;
  11697. }
  11698. return 0;
  11699. }
  11700. static int intel_modeset_checks(struct drm_atomic_state *state)
  11701. {
  11702. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11703. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11704. struct drm_crtc *crtc;
  11705. struct drm_crtc_state *crtc_state;
  11706. int ret = 0, i;
  11707. if (!check_digital_port_conflicts(state)) {
  11708. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11709. return -EINVAL;
  11710. }
  11711. intel_state->modeset = true;
  11712. intel_state->active_crtcs = dev_priv->active_crtcs;
  11713. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11714. if (crtc_state->active)
  11715. intel_state->active_crtcs |= 1 << i;
  11716. else
  11717. intel_state->active_crtcs &= ~(1 << i);
  11718. if (crtc_state->active != crtc->state->active)
  11719. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11720. }
  11721. /*
  11722. * See if the config requires any additional preparation, e.g.
  11723. * to adjust global state with pipes off. We need to do this
  11724. * here so we can get the modeset_pipe updated config for the new
  11725. * mode set on this crtc. For other crtcs we need to use the
  11726. * adjusted_mode bits in the crtc directly.
  11727. */
  11728. if (dev_priv->display.modeset_calc_cdclk) {
  11729. if (!intel_state->cdclk_pll_vco)
  11730. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11731. if (!intel_state->cdclk_pll_vco)
  11732. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11733. ret = dev_priv->display.modeset_calc_cdclk(state);
  11734. if (ret < 0)
  11735. return ret;
  11736. /*
  11737. * Writes to dev_priv->atomic_cdclk_freq must protected by
  11738. * holding all the crtc locks, even if we don't end up
  11739. * touching the hardware
  11740. */
  11741. if (intel_state->cdclk != dev_priv->atomic_cdclk_freq) {
  11742. ret = intel_lock_all_pipes(state);
  11743. if (ret < 0)
  11744. return ret;
  11745. }
  11746. /* All pipes must be switched off while we change the cdclk. */
  11747. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11748. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco) {
  11749. ret = intel_modeset_all_pipes(state);
  11750. if (ret < 0)
  11751. return ret;
  11752. }
  11753. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11754. intel_state->cdclk, intel_state->dev_cdclk);
  11755. } else {
  11756. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11757. }
  11758. intel_modeset_clear_plls(state);
  11759. if (IS_HASWELL(dev_priv))
  11760. return haswell_mode_set_planes_workaround(state);
  11761. return 0;
  11762. }
  11763. /*
  11764. * Handle calculation of various watermark data at the end of the atomic check
  11765. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11766. * handlers to ensure that all derived state has been updated.
  11767. */
  11768. static int calc_watermark_data(struct drm_atomic_state *state)
  11769. {
  11770. struct drm_device *dev = state->dev;
  11771. struct drm_i915_private *dev_priv = to_i915(dev);
  11772. /* Is there platform-specific watermark information to calculate? */
  11773. if (dev_priv->display.compute_global_watermarks)
  11774. return dev_priv->display.compute_global_watermarks(state);
  11775. return 0;
  11776. }
  11777. /**
  11778. * intel_atomic_check - validate state object
  11779. * @dev: drm device
  11780. * @state: state to validate
  11781. */
  11782. static int intel_atomic_check(struct drm_device *dev,
  11783. struct drm_atomic_state *state)
  11784. {
  11785. struct drm_i915_private *dev_priv = to_i915(dev);
  11786. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11787. struct drm_crtc *crtc;
  11788. struct drm_crtc_state *crtc_state;
  11789. int ret, i;
  11790. bool any_ms = false;
  11791. ret = drm_atomic_helper_check_modeset(dev, state);
  11792. if (ret)
  11793. return ret;
  11794. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11795. struct intel_crtc_state *pipe_config =
  11796. to_intel_crtc_state(crtc_state);
  11797. /* Catch I915_MODE_FLAG_INHERITED */
  11798. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11799. crtc_state->mode_changed = true;
  11800. if (!needs_modeset(crtc_state))
  11801. continue;
  11802. if (!crtc_state->enable) {
  11803. any_ms = true;
  11804. continue;
  11805. }
  11806. /* FIXME: For only active_changed we shouldn't need to do any
  11807. * state recomputation at all. */
  11808. ret = drm_atomic_add_affected_connectors(state, crtc);
  11809. if (ret)
  11810. return ret;
  11811. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11812. if (ret) {
  11813. intel_dump_pipe_config(to_intel_crtc(crtc),
  11814. pipe_config, "[failed]");
  11815. return ret;
  11816. }
  11817. if (i915.fastboot &&
  11818. intel_pipe_config_compare(dev_priv,
  11819. to_intel_crtc_state(crtc->state),
  11820. pipe_config, true)) {
  11821. crtc_state->mode_changed = false;
  11822. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11823. }
  11824. if (needs_modeset(crtc_state))
  11825. any_ms = true;
  11826. ret = drm_atomic_add_affected_planes(state, crtc);
  11827. if (ret)
  11828. return ret;
  11829. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11830. needs_modeset(crtc_state) ?
  11831. "[modeset]" : "[fastset]");
  11832. }
  11833. if (any_ms) {
  11834. ret = intel_modeset_checks(state);
  11835. if (ret)
  11836. return ret;
  11837. } else {
  11838. intel_state->cdclk = dev_priv->atomic_cdclk_freq;
  11839. }
  11840. ret = drm_atomic_helper_check_planes(dev, state);
  11841. if (ret)
  11842. return ret;
  11843. intel_fbc_choose_crtc(dev_priv, state);
  11844. return calc_watermark_data(state);
  11845. }
  11846. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11847. struct drm_atomic_state *state)
  11848. {
  11849. struct drm_i915_private *dev_priv = to_i915(dev);
  11850. struct drm_crtc_state *crtc_state;
  11851. struct drm_crtc *crtc;
  11852. int i, ret;
  11853. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11854. if (state->legacy_cursor_update)
  11855. continue;
  11856. ret = intel_crtc_wait_for_pending_flips(crtc);
  11857. if (ret)
  11858. return ret;
  11859. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11860. flush_workqueue(dev_priv->wq);
  11861. }
  11862. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11863. if (ret)
  11864. return ret;
  11865. ret = drm_atomic_helper_prepare_planes(dev, state);
  11866. mutex_unlock(&dev->struct_mutex);
  11867. return ret;
  11868. }
  11869. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11870. {
  11871. struct drm_device *dev = crtc->base.dev;
  11872. if (!dev->max_vblank_count)
  11873. return drm_accurate_vblank_count(&crtc->base);
  11874. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11875. }
  11876. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11877. struct drm_i915_private *dev_priv,
  11878. unsigned crtc_mask)
  11879. {
  11880. unsigned last_vblank_count[I915_MAX_PIPES];
  11881. enum pipe pipe;
  11882. int ret;
  11883. if (!crtc_mask)
  11884. return;
  11885. for_each_pipe(dev_priv, pipe) {
  11886. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11887. pipe);
  11888. if (!((1 << pipe) & crtc_mask))
  11889. continue;
  11890. ret = drm_crtc_vblank_get(&crtc->base);
  11891. if (WARN_ON(ret != 0)) {
  11892. crtc_mask &= ~(1 << pipe);
  11893. continue;
  11894. }
  11895. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  11896. }
  11897. for_each_pipe(dev_priv, pipe) {
  11898. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11899. pipe);
  11900. long lret;
  11901. if (!((1 << pipe) & crtc_mask))
  11902. continue;
  11903. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11904. last_vblank_count[pipe] !=
  11905. drm_crtc_vblank_count(&crtc->base),
  11906. msecs_to_jiffies(50));
  11907. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11908. drm_crtc_vblank_put(&crtc->base);
  11909. }
  11910. }
  11911. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11912. {
  11913. /* fb updated, need to unpin old fb */
  11914. if (crtc_state->fb_changed)
  11915. return true;
  11916. /* wm changes, need vblank before final wm's */
  11917. if (crtc_state->update_wm_post)
  11918. return true;
  11919. /*
  11920. * cxsr is re-enabled after vblank.
  11921. * This is already handled by crtc_state->update_wm_post,
  11922. * but added for clarity.
  11923. */
  11924. if (crtc_state->disable_cxsr)
  11925. return true;
  11926. return false;
  11927. }
  11928. static void intel_update_crtc(struct drm_crtc *crtc,
  11929. struct drm_atomic_state *state,
  11930. struct drm_crtc_state *old_crtc_state,
  11931. unsigned int *crtc_vblank_mask)
  11932. {
  11933. struct drm_device *dev = crtc->dev;
  11934. struct drm_i915_private *dev_priv = to_i915(dev);
  11935. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11936. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
  11937. bool modeset = needs_modeset(crtc->state);
  11938. if (modeset) {
  11939. update_scanline_offset(intel_crtc);
  11940. dev_priv->display.crtc_enable(pipe_config, state);
  11941. } else {
  11942. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11943. }
  11944. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  11945. intel_fbc_enable(
  11946. intel_crtc, pipe_config,
  11947. to_intel_plane_state(crtc->primary->state));
  11948. }
  11949. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11950. if (needs_vblank_wait(pipe_config))
  11951. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  11952. }
  11953. static void intel_update_crtcs(struct drm_atomic_state *state,
  11954. unsigned int *crtc_vblank_mask)
  11955. {
  11956. struct drm_crtc *crtc;
  11957. struct drm_crtc_state *old_crtc_state;
  11958. int i;
  11959. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11960. if (!crtc->state->active)
  11961. continue;
  11962. intel_update_crtc(crtc, state, old_crtc_state,
  11963. crtc_vblank_mask);
  11964. }
  11965. }
  11966. static void skl_update_crtcs(struct drm_atomic_state *state,
  11967. unsigned int *crtc_vblank_mask)
  11968. {
  11969. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11970. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11971. struct drm_crtc *crtc;
  11972. struct intel_crtc *intel_crtc;
  11973. struct drm_crtc_state *old_crtc_state;
  11974. struct intel_crtc_state *cstate;
  11975. unsigned int updated = 0;
  11976. bool progress;
  11977. enum pipe pipe;
  11978. int i;
  11979. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  11980. for_each_crtc_in_state(state, crtc, old_crtc_state, i)
  11981. /* ignore allocations for crtc's that have been turned off. */
  11982. if (crtc->state->active)
  11983. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  11984. /*
  11985. * Whenever the number of active pipes changes, we need to make sure we
  11986. * update the pipes in the right order so that their ddb allocations
  11987. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  11988. * cause pipe underruns and other bad stuff.
  11989. */
  11990. do {
  11991. progress = false;
  11992. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11993. bool vbl_wait = false;
  11994. unsigned int cmask = drm_crtc_mask(crtc);
  11995. intel_crtc = to_intel_crtc(crtc);
  11996. cstate = to_intel_crtc_state(crtc->state);
  11997. pipe = intel_crtc->pipe;
  11998. if (updated & cmask || !cstate->base.active)
  11999. continue;
  12000. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  12001. continue;
  12002. updated |= cmask;
  12003. entries[i] = &cstate->wm.skl.ddb;
  12004. /*
  12005. * If this is an already active pipe, it's DDB changed,
  12006. * and this isn't the last pipe that needs updating
  12007. * then we need to wait for a vblank to pass for the
  12008. * new ddb allocation to take effect.
  12009. */
  12010. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  12011. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  12012. !crtc->state->active_changed &&
  12013. intel_state->wm_results.dirty_pipes != updated)
  12014. vbl_wait = true;
  12015. intel_update_crtc(crtc, state, old_crtc_state,
  12016. crtc_vblank_mask);
  12017. if (vbl_wait)
  12018. intel_wait_for_vblank(dev_priv, pipe);
  12019. progress = true;
  12020. }
  12021. } while (progress);
  12022. }
  12023. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  12024. {
  12025. struct drm_device *dev = state->dev;
  12026. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12027. struct drm_i915_private *dev_priv = to_i915(dev);
  12028. struct drm_crtc_state *old_crtc_state;
  12029. struct drm_crtc *crtc;
  12030. struct intel_crtc_state *intel_cstate;
  12031. bool hw_check = intel_state->modeset;
  12032. unsigned long put_domains[I915_MAX_PIPES] = {};
  12033. unsigned crtc_vblank_mask = 0;
  12034. int i;
  12035. drm_atomic_helper_wait_for_dependencies(state);
  12036. if (intel_state->modeset)
  12037. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  12038. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12039. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12040. if (needs_modeset(crtc->state) ||
  12041. to_intel_crtc_state(crtc->state)->update_pipe) {
  12042. hw_check = true;
  12043. put_domains[to_intel_crtc(crtc)->pipe] =
  12044. modeset_get_crtc_power_domains(crtc,
  12045. to_intel_crtc_state(crtc->state));
  12046. }
  12047. if (!needs_modeset(crtc->state))
  12048. continue;
  12049. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  12050. if (old_crtc_state->active) {
  12051. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  12052. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  12053. intel_crtc->active = false;
  12054. intel_fbc_disable(intel_crtc);
  12055. intel_disable_shared_dpll(intel_crtc);
  12056. /*
  12057. * Underruns don't always raise
  12058. * interrupts, so check manually.
  12059. */
  12060. intel_check_cpu_fifo_underruns(dev_priv);
  12061. intel_check_pch_fifo_underruns(dev_priv);
  12062. if (!crtc->state->active) {
  12063. /*
  12064. * Make sure we don't call initial_watermarks
  12065. * for ILK-style watermark updates.
  12066. */
  12067. if (dev_priv->display.atomic_update_watermarks)
  12068. dev_priv->display.initial_watermarks(intel_state,
  12069. to_intel_crtc_state(crtc->state));
  12070. else
  12071. intel_update_watermarks(intel_crtc);
  12072. }
  12073. }
  12074. }
  12075. /* Only after disabling all output pipelines that will be changed can we
  12076. * update the the output configuration. */
  12077. intel_modeset_update_crtc_state(state);
  12078. if (intel_state->modeset) {
  12079. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  12080. if (dev_priv->display.modeset_commit_cdclk &&
  12081. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  12082. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  12083. dev_priv->display.modeset_commit_cdclk(state);
  12084. /*
  12085. * SKL workaround: bspec recommends we disable the SAGV when we
  12086. * have more then one pipe enabled
  12087. */
  12088. if (!intel_can_enable_sagv(state))
  12089. intel_disable_sagv(dev_priv);
  12090. intel_modeset_verify_disabled(dev, state);
  12091. }
  12092. /* Complete the events for pipes that have now been disabled */
  12093. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12094. bool modeset = needs_modeset(crtc->state);
  12095. /* Complete events for now disable pipes here. */
  12096. if (modeset && !crtc->state->active && crtc->state->event) {
  12097. spin_lock_irq(&dev->event_lock);
  12098. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  12099. spin_unlock_irq(&dev->event_lock);
  12100. crtc->state->event = NULL;
  12101. }
  12102. }
  12103. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  12104. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  12105. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  12106. * already, but still need the state for the delayed optimization. To
  12107. * fix this:
  12108. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  12109. * - schedule that vblank worker _before_ calling hw_done
  12110. * - at the start of commit_tail, cancel it _synchrously
  12111. * - switch over to the vblank wait helper in the core after that since
  12112. * we don't need out special handling any more.
  12113. */
  12114. if (!state->legacy_cursor_update)
  12115. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  12116. /*
  12117. * Now that the vblank has passed, we can go ahead and program the
  12118. * optimal watermarks on platforms that need two-step watermark
  12119. * programming.
  12120. *
  12121. * TODO: Move this (and other cleanup) to an async worker eventually.
  12122. */
  12123. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12124. intel_cstate = to_intel_crtc_state(crtc->state);
  12125. if (dev_priv->display.optimize_watermarks)
  12126. dev_priv->display.optimize_watermarks(intel_state,
  12127. intel_cstate);
  12128. }
  12129. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12130. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  12131. if (put_domains[i])
  12132. modeset_put_power_domains(dev_priv, put_domains[i]);
  12133. intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
  12134. }
  12135. if (intel_state->modeset && intel_can_enable_sagv(state))
  12136. intel_enable_sagv(dev_priv);
  12137. drm_atomic_helper_commit_hw_done(state);
  12138. if (intel_state->modeset)
  12139. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  12140. mutex_lock(&dev->struct_mutex);
  12141. drm_atomic_helper_cleanup_planes(dev, state);
  12142. mutex_unlock(&dev->struct_mutex);
  12143. drm_atomic_helper_commit_cleanup_done(state);
  12144. drm_atomic_state_put(state);
  12145. /* As one of the primary mmio accessors, KMS has a high likelihood
  12146. * of triggering bugs in unclaimed access. After we finish
  12147. * modesetting, see if an error has been flagged, and if so
  12148. * enable debugging for the next modeset - and hope we catch
  12149. * the culprit.
  12150. *
  12151. * XXX note that we assume display power is on at this point.
  12152. * This might hold true now but we need to add pm helper to check
  12153. * unclaimed only when the hardware is on, as atomic commits
  12154. * can happen also when the device is completely off.
  12155. */
  12156. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  12157. }
  12158. static void intel_atomic_commit_work(struct work_struct *work)
  12159. {
  12160. struct drm_atomic_state *state =
  12161. container_of(work, struct drm_atomic_state, commit_work);
  12162. intel_atomic_commit_tail(state);
  12163. }
  12164. static int __i915_sw_fence_call
  12165. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  12166. enum i915_sw_fence_notify notify)
  12167. {
  12168. struct intel_atomic_state *state =
  12169. container_of(fence, struct intel_atomic_state, commit_ready);
  12170. switch (notify) {
  12171. case FENCE_COMPLETE:
  12172. if (state->base.commit_work.func)
  12173. queue_work(system_unbound_wq, &state->base.commit_work);
  12174. break;
  12175. case FENCE_FREE:
  12176. drm_atomic_state_put(&state->base);
  12177. break;
  12178. }
  12179. return NOTIFY_DONE;
  12180. }
  12181. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  12182. {
  12183. struct drm_plane_state *old_plane_state;
  12184. struct drm_plane *plane;
  12185. int i;
  12186. for_each_plane_in_state(state, plane, old_plane_state, i)
  12187. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  12188. intel_fb_obj(plane->state->fb),
  12189. to_intel_plane(plane)->frontbuffer_bit);
  12190. }
  12191. /**
  12192. * intel_atomic_commit - commit validated state object
  12193. * @dev: DRM device
  12194. * @state: the top-level driver state object
  12195. * @nonblock: nonblocking commit
  12196. *
  12197. * This function commits a top-level state object that has been validated
  12198. * with drm_atomic_helper_check().
  12199. *
  12200. * RETURNS
  12201. * Zero for success or -errno.
  12202. */
  12203. static int intel_atomic_commit(struct drm_device *dev,
  12204. struct drm_atomic_state *state,
  12205. bool nonblock)
  12206. {
  12207. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12208. struct drm_i915_private *dev_priv = to_i915(dev);
  12209. int ret = 0;
  12210. ret = drm_atomic_helper_setup_commit(state, nonblock);
  12211. if (ret)
  12212. return ret;
  12213. drm_atomic_state_get(state);
  12214. i915_sw_fence_init(&intel_state->commit_ready,
  12215. intel_atomic_commit_ready);
  12216. ret = intel_atomic_prepare_commit(dev, state);
  12217. if (ret) {
  12218. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  12219. i915_sw_fence_commit(&intel_state->commit_ready);
  12220. return ret;
  12221. }
  12222. drm_atomic_helper_swap_state(state, true);
  12223. dev_priv->wm.distrust_bios_wm = false;
  12224. intel_shared_dpll_swap_state(state);
  12225. intel_atomic_track_fbs(state);
  12226. if (intel_state->modeset) {
  12227. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  12228. sizeof(intel_state->min_pixclk));
  12229. dev_priv->active_crtcs = intel_state->active_crtcs;
  12230. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  12231. }
  12232. drm_atomic_state_get(state);
  12233. INIT_WORK(&state->commit_work,
  12234. nonblock ? intel_atomic_commit_work : NULL);
  12235. i915_sw_fence_commit(&intel_state->commit_ready);
  12236. if (!nonblock) {
  12237. i915_sw_fence_wait(&intel_state->commit_ready);
  12238. intel_atomic_commit_tail(state);
  12239. }
  12240. return 0;
  12241. }
  12242. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  12243. {
  12244. struct drm_device *dev = crtc->dev;
  12245. struct drm_atomic_state *state;
  12246. struct drm_crtc_state *crtc_state;
  12247. int ret;
  12248. state = drm_atomic_state_alloc(dev);
  12249. if (!state) {
  12250. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  12251. crtc->base.id, crtc->name);
  12252. return;
  12253. }
  12254. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  12255. retry:
  12256. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  12257. ret = PTR_ERR_OR_ZERO(crtc_state);
  12258. if (!ret) {
  12259. if (!crtc_state->active)
  12260. goto out;
  12261. crtc_state->mode_changed = true;
  12262. ret = drm_atomic_commit(state);
  12263. }
  12264. if (ret == -EDEADLK) {
  12265. drm_atomic_state_clear(state);
  12266. drm_modeset_backoff(state->acquire_ctx);
  12267. goto retry;
  12268. }
  12269. out:
  12270. drm_atomic_state_put(state);
  12271. }
  12272. /*
  12273. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  12274. * drm_atomic_helper_legacy_gamma_set() directly.
  12275. */
  12276. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  12277. u16 *red, u16 *green, u16 *blue,
  12278. uint32_t size)
  12279. {
  12280. struct drm_device *dev = crtc->dev;
  12281. struct drm_mode_config *config = &dev->mode_config;
  12282. struct drm_crtc_state *state;
  12283. int ret;
  12284. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  12285. if (ret)
  12286. return ret;
  12287. /*
  12288. * Make sure we update the legacy properties so this works when
  12289. * atomic is not enabled.
  12290. */
  12291. state = crtc->state;
  12292. drm_object_property_set_value(&crtc->base,
  12293. config->degamma_lut_property,
  12294. (state->degamma_lut) ?
  12295. state->degamma_lut->base.id : 0);
  12296. drm_object_property_set_value(&crtc->base,
  12297. config->ctm_property,
  12298. (state->ctm) ?
  12299. state->ctm->base.id : 0);
  12300. drm_object_property_set_value(&crtc->base,
  12301. config->gamma_lut_property,
  12302. (state->gamma_lut) ?
  12303. state->gamma_lut->base.id : 0);
  12304. return 0;
  12305. }
  12306. static const struct drm_crtc_funcs intel_crtc_funcs = {
  12307. .gamma_set = intel_atomic_legacy_gamma_set,
  12308. .set_config = drm_atomic_helper_set_config,
  12309. .set_property = drm_atomic_helper_crtc_set_property,
  12310. .destroy = intel_crtc_destroy,
  12311. .page_flip = intel_crtc_page_flip,
  12312. .atomic_duplicate_state = intel_crtc_duplicate_state,
  12313. .atomic_destroy_state = intel_crtc_destroy_state,
  12314. .set_crc_source = intel_crtc_set_crc_source,
  12315. };
  12316. /**
  12317. * intel_prepare_plane_fb - Prepare fb for usage on plane
  12318. * @plane: drm plane to prepare for
  12319. * @fb: framebuffer to prepare for presentation
  12320. *
  12321. * Prepares a framebuffer for usage on a display plane. Generally this
  12322. * involves pinning the underlying object and updating the frontbuffer tracking
  12323. * bits. Some older platforms need special physical address handling for
  12324. * cursor planes.
  12325. *
  12326. * Must be called with struct_mutex held.
  12327. *
  12328. * Returns 0 on success, negative error code on failure.
  12329. */
  12330. int
  12331. intel_prepare_plane_fb(struct drm_plane *plane,
  12332. struct drm_plane_state *new_state)
  12333. {
  12334. struct intel_atomic_state *intel_state =
  12335. to_intel_atomic_state(new_state->state);
  12336. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12337. struct drm_framebuffer *fb = new_state->fb;
  12338. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12339. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  12340. int ret;
  12341. if (!obj && !old_obj)
  12342. return 0;
  12343. if (old_obj) {
  12344. struct drm_crtc_state *crtc_state =
  12345. drm_atomic_get_existing_crtc_state(new_state->state,
  12346. plane->state->crtc);
  12347. /* Big Hammer, we also need to ensure that any pending
  12348. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  12349. * current scanout is retired before unpinning the old
  12350. * framebuffer. Note that we rely on userspace rendering
  12351. * into the buffer attached to the pipe they are waiting
  12352. * on. If not, userspace generates a GPU hang with IPEHR
  12353. * point to the MI_WAIT_FOR_EVENT.
  12354. *
  12355. * This should only fail upon a hung GPU, in which case we
  12356. * can safely continue.
  12357. */
  12358. if (needs_modeset(crtc_state)) {
  12359. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12360. old_obj->resv, NULL,
  12361. false, 0,
  12362. GFP_KERNEL);
  12363. if (ret < 0)
  12364. return ret;
  12365. }
  12366. }
  12367. if (new_state->fence) { /* explicit fencing */
  12368. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  12369. new_state->fence,
  12370. I915_FENCE_TIMEOUT,
  12371. GFP_KERNEL);
  12372. if (ret < 0)
  12373. return ret;
  12374. }
  12375. if (!obj)
  12376. return 0;
  12377. if (!new_state->fence) { /* implicit fencing */
  12378. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12379. obj->resv, NULL,
  12380. false, I915_FENCE_TIMEOUT,
  12381. GFP_KERNEL);
  12382. if (ret < 0)
  12383. return ret;
  12384. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  12385. }
  12386. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  12387. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  12388. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  12389. ret = i915_gem_object_attach_phys(obj, align);
  12390. if (ret) {
  12391. DRM_DEBUG_KMS("failed to attach phys object\n");
  12392. return ret;
  12393. }
  12394. } else {
  12395. struct i915_vma *vma;
  12396. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  12397. if (IS_ERR(vma)) {
  12398. DRM_DEBUG_KMS("failed to pin object\n");
  12399. return PTR_ERR(vma);
  12400. }
  12401. }
  12402. return 0;
  12403. }
  12404. /**
  12405. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  12406. * @plane: drm plane to clean up for
  12407. * @fb: old framebuffer that was on plane
  12408. *
  12409. * Cleans up a framebuffer that has just been removed from a plane.
  12410. *
  12411. * Must be called with struct_mutex held.
  12412. */
  12413. void
  12414. intel_cleanup_plane_fb(struct drm_plane *plane,
  12415. struct drm_plane_state *old_state)
  12416. {
  12417. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12418. struct intel_plane_state *old_intel_state;
  12419. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  12420. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  12421. old_intel_state = to_intel_plane_state(old_state);
  12422. if (!obj && !old_obj)
  12423. return;
  12424. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  12425. !INTEL_INFO(dev_priv)->cursor_needs_physical))
  12426. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  12427. }
  12428. int
  12429. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  12430. {
  12431. int max_scale;
  12432. int crtc_clock, cdclk;
  12433. if (!intel_crtc || !crtc_state->base.enable)
  12434. return DRM_PLANE_HELPER_NO_SCALING;
  12435. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  12436. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  12437. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  12438. return DRM_PLANE_HELPER_NO_SCALING;
  12439. /*
  12440. * skl max scale is lower of:
  12441. * close to 3 but not 3, -1 is for that purpose
  12442. * or
  12443. * cdclk/crtc_clock
  12444. */
  12445. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  12446. return max_scale;
  12447. }
  12448. static int
  12449. intel_check_primary_plane(struct drm_plane *plane,
  12450. struct intel_crtc_state *crtc_state,
  12451. struct intel_plane_state *state)
  12452. {
  12453. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12454. struct drm_crtc *crtc = state->base.crtc;
  12455. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  12456. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  12457. bool can_position = false;
  12458. int ret;
  12459. if (INTEL_GEN(dev_priv) >= 9) {
  12460. /* use scaler when colorkey is not required */
  12461. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  12462. min_scale = 1;
  12463. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  12464. }
  12465. can_position = true;
  12466. }
  12467. ret = drm_plane_helper_check_state(&state->base,
  12468. &state->clip,
  12469. min_scale, max_scale,
  12470. can_position, true);
  12471. if (ret)
  12472. return ret;
  12473. if (!state->base.fb)
  12474. return 0;
  12475. if (INTEL_GEN(dev_priv) >= 9) {
  12476. ret = skl_check_plane_surface(state);
  12477. if (ret)
  12478. return ret;
  12479. }
  12480. return 0;
  12481. }
  12482. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  12483. struct drm_crtc_state *old_crtc_state)
  12484. {
  12485. struct drm_device *dev = crtc->dev;
  12486. struct drm_i915_private *dev_priv = to_i915(dev);
  12487. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12488. struct intel_crtc_state *intel_cstate =
  12489. to_intel_crtc_state(crtc->state);
  12490. struct intel_crtc_state *old_intel_cstate =
  12491. to_intel_crtc_state(old_crtc_state);
  12492. struct intel_atomic_state *old_intel_state =
  12493. to_intel_atomic_state(old_crtc_state->state);
  12494. bool modeset = needs_modeset(crtc->state);
  12495. /* Perform vblank evasion around commit operation */
  12496. intel_pipe_update_start(intel_crtc);
  12497. if (modeset)
  12498. goto out;
  12499. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  12500. intel_color_set_csc(crtc->state);
  12501. intel_color_load_luts(crtc->state);
  12502. }
  12503. if (intel_cstate->update_pipe)
  12504. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  12505. else if (INTEL_GEN(dev_priv) >= 9)
  12506. skl_detach_scalers(intel_crtc);
  12507. out:
  12508. if (dev_priv->display.atomic_update_watermarks)
  12509. dev_priv->display.atomic_update_watermarks(old_intel_state,
  12510. intel_cstate);
  12511. }
  12512. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  12513. struct drm_crtc_state *old_crtc_state)
  12514. {
  12515. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12516. intel_pipe_update_end(intel_crtc, NULL);
  12517. }
  12518. /**
  12519. * intel_plane_destroy - destroy a plane
  12520. * @plane: plane to destroy
  12521. *
  12522. * Common destruction function for all types of planes (primary, cursor,
  12523. * sprite).
  12524. */
  12525. void intel_plane_destroy(struct drm_plane *plane)
  12526. {
  12527. drm_plane_cleanup(plane);
  12528. kfree(to_intel_plane(plane));
  12529. }
  12530. const struct drm_plane_funcs intel_plane_funcs = {
  12531. .update_plane = drm_atomic_helper_update_plane,
  12532. .disable_plane = drm_atomic_helper_disable_plane,
  12533. .destroy = intel_plane_destroy,
  12534. .set_property = drm_atomic_helper_plane_set_property,
  12535. .atomic_get_property = intel_plane_atomic_get_property,
  12536. .atomic_set_property = intel_plane_atomic_set_property,
  12537. .atomic_duplicate_state = intel_plane_duplicate_state,
  12538. .atomic_destroy_state = intel_plane_destroy_state,
  12539. };
  12540. static int
  12541. intel_legacy_cursor_update(struct drm_plane *plane,
  12542. struct drm_crtc *crtc,
  12543. struct drm_framebuffer *fb,
  12544. int crtc_x, int crtc_y,
  12545. unsigned int crtc_w, unsigned int crtc_h,
  12546. uint32_t src_x, uint32_t src_y,
  12547. uint32_t src_w, uint32_t src_h)
  12548. {
  12549. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  12550. int ret;
  12551. struct drm_plane_state *old_plane_state, *new_plane_state;
  12552. struct intel_plane *intel_plane = to_intel_plane(plane);
  12553. struct drm_framebuffer *old_fb;
  12554. struct drm_crtc_state *crtc_state = crtc->state;
  12555. /*
  12556. * When crtc is inactive or there is a modeset pending,
  12557. * wait for it to complete in the slowpath
  12558. */
  12559. if (!crtc_state->active || needs_modeset(crtc_state) ||
  12560. to_intel_crtc_state(crtc_state)->update_pipe)
  12561. goto slow;
  12562. old_plane_state = plane->state;
  12563. /*
  12564. * If any parameters change that may affect watermarks,
  12565. * take the slowpath. Only changing fb or position should be
  12566. * in the fastpath.
  12567. */
  12568. if (old_plane_state->crtc != crtc ||
  12569. old_plane_state->src_w != src_w ||
  12570. old_plane_state->src_h != src_h ||
  12571. old_plane_state->crtc_w != crtc_w ||
  12572. old_plane_state->crtc_h != crtc_h ||
  12573. !old_plane_state->visible ||
  12574. old_plane_state->fb->modifier != fb->modifier)
  12575. goto slow;
  12576. new_plane_state = intel_plane_duplicate_state(plane);
  12577. if (!new_plane_state)
  12578. return -ENOMEM;
  12579. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  12580. new_plane_state->src_x = src_x;
  12581. new_plane_state->src_y = src_y;
  12582. new_plane_state->src_w = src_w;
  12583. new_plane_state->src_h = src_h;
  12584. new_plane_state->crtc_x = crtc_x;
  12585. new_plane_state->crtc_y = crtc_y;
  12586. new_plane_state->crtc_w = crtc_w;
  12587. new_plane_state->crtc_h = crtc_h;
  12588. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  12589. to_intel_plane_state(new_plane_state));
  12590. if (ret)
  12591. goto out_free;
  12592. /* Visibility changed, must take slowpath. */
  12593. if (!new_plane_state->visible)
  12594. goto slow_free;
  12595. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  12596. if (ret)
  12597. goto out_free;
  12598. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  12599. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  12600. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  12601. if (ret) {
  12602. DRM_DEBUG_KMS("failed to attach phys object\n");
  12603. goto out_unlock;
  12604. }
  12605. } else {
  12606. struct i915_vma *vma;
  12607. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  12608. if (IS_ERR(vma)) {
  12609. DRM_DEBUG_KMS("failed to pin object\n");
  12610. ret = PTR_ERR(vma);
  12611. goto out_unlock;
  12612. }
  12613. }
  12614. old_fb = old_plane_state->fb;
  12615. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  12616. intel_plane->frontbuffer_bit);
  12617. /* Swap plane state */
  12618. new_plane_state->fence = old_plane_state->fence;
  12619. *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
  12620. new_plane_state->fence = NULL;
  12621. new_plane_state->fb = old_fb;
  12622. intel_plane->update_plane(plane,
  12623. to_intel_crtc_state(crtc->state),
  12624. to_intel_plane_state(plane->state));
  12625. intel_cleanup_plane_fb(plane, new_plane_state);
  12626. out_unlock:
  12627. mutex_unlock(&dev_priv->drm.struct_mutex);
  12628. out_free:
  12629. intel_plane_destroy_state(plane, new_plane_state);
  12630. return ret;
  12631. slow_free:
  12632. intel_plane_destroy_state(plane, new_plane_state);
  12633. slow:
  12634. return drm_atomic_helper_update_plane(plane, crtc, fb,
  12635. crtc_x, crtc_y, crtc_w, crtc_h,
  12636. src_x, src_y, src_w, src_h);
  12637. }
  12638. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  12639. .update_plane = intel_legacy_cursor_update,
  12640. .disable_plane = drm_atomic_helper_disable_plane,
  12641. .destroy = intel_plane_destroy,
  12642. .set_property = drm_atomic_helper_plane_set_property,
  12643. .atomic_get_property = intel_plane_atomic_get_property,
  12644. .atomic_set_property = intel_plane_atomic_set_property,
  12645. .atomic_duplicate_state = intel_plane_duplicate_state,
  12646. .atomic_destroy_state = intel_plane_destroy_state,
  12647. };
  12648. static struct intel_plane *
  12649. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12650. {
  12651. struct intel_plane *primary = NULL;
  12652. struct intel_plane_state *state = NULL;
  12653. const uint32_t *intel_primary_formats;
  12654. unsigned int supported_rotations;
  12655. unsigned int num_formats;
  12656. int ret;
  12657. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  12658. if (!primary) {
  12659. ret = -ENOMEM;
  12660. goto fail;
  12661. }
  12662. state = intel_create_plane_state(&primary->base);
  12663. if (!state) {
  12664. ret = -ENOMEM;
  12665. goto fail;
  12666. }
  12667. primary->base.state = &state->base;
  12668. primary->can_scale = false;
  12669. primary->max_downscale = 1;
  12670. if (INTEL_GEN(dev_priv) >= 9) {
  12671. primary->can_scale = true;
  12672. state->scaler_id = -1;
  12673. }
  12674. primary->pipe = pipe;
  12675. /*
  12676. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  12677. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  12678. */
  12679. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  12680. primary->plane = (enum plane) !pipe;
  12681. else
  12682. primary->plane = (enum plane) pipe;
  12683. primary->id = PLANE_PRIMARY;
  12684. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  12685. primary->check_plane = intel_check_primary_plane;
  12686. if (INTEL_GEN(dev_priv) >= 9) {
  12687. intel_primary_formats = skl_primary_formats;
  12688. num_formats = ARRAY_SIZE(skl_primary_formats);
  12689. primary->update_plane = skylake_update_primary_plane;
  12690. primary->disable_plane = skylake_disable_primary_plane;
  12691. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12692. intel_primary_formats = i965_primary_formats;
  12693. num_formats = ARRAY_SIZE(i965_primary_formats);
  12694. primary->update_plane = ironlake_update_primary_plane;
  12695. primary->disable_plane = i9xx_disable_primary_plane;
  12696. } else if (INTEL_GEN(dev_priv) >= 4) {
  12697. intel_primary_formats = i965_primary_formats;
  12698. num_formats = ARRAY_SIZE(i965_primary_formats);
  12699. primary->update_plane = i9xx_update_primary_plane;
  12700. primary->disable_plane = i9xx_disable_primary_plane;
  12701. } else {
  12702. intel_primary_formats = i8xx_primary_formats;
  12703. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  12704. primary->update_plane = i9xx_update_primary_plane;
  12705. primary->disable_plane = i9xx_disable_primary_plane;
  12706. }
  12707. if (INTEL_GEN(dev_priv) >= 9)
  12708. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12709. 0, &intel_plane_funcs,
  12710. intel_primary_formats, num_formats,
  12711. DRM_PLANE_TYPE_PRIMARY,
  12712. "plane 1%c", pipe_name(pipe));
  12713. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  12714. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12715. 0, &intel_plane_funcs,
  12716. intel_primary_formats, num_formats,
  12717. DRM_PLANE_TYPE_PRIMARY,
  12718. "primary %c", pipe_name(pipe));
  12719. else
  12720. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12721. 0, &intel_plane_funcs,
  12722. intel_primary_formats, num_formats,
  12723. DRM_PLANE_TYPE_PRIMARY,
  12724. "plane %c", plane_name(primary->plane));
  12725. if (ret)
  12726. goto fail;
  12727. if (INTEL_GEN(dev_priv) >= 9) {
  12728. supported_rotations =
  12729. DRM_ROTATE_0 | DRM_ROTATE_90 |
  12730. DRM_ROTATE_180 | DRM_ROTATE_270;
  12731. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  12732. supported_rotations =
  12733. DRM_ROTATE_0 | DRM_ROTATE_180 |
  12734. DRM_REFLECT_X;
  12735. } else if (INTEL_GEN(dev_priv) >= 4) {
  12736. supported_rotations =
  12737. DRM_ROTATE_0 | DRM_ROTATE_180;
  12738. } else {
  12739. supported_rotations = DRM_ROTATE_0;
  12740. }
  12741. if (INTEL_GEN(dev_priv) >= 4)
  12742. drm_plane_create_rotation_property(&primary->base,
  12743. DRM_ROTATE_0,
  12744. supported_rotations);
  12745. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  12746. return primary;
  12747. fail:
  12748. kfree(state);
  12749. kfree(primary);
  12750. return ERR_PTR(ret);
  12751. }
  12752. static int
  12753. intel_check_cursor_plane(struct drm_plane *plane,
  12754. struct intel_crtc_state *crtc_state,
  12755. struct intel_plane_state *state)
  12756. {
  12757. struct drm_framebuffer *fb = state->base.fb;
  12758. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12759. enum pipe pipe = to_intel_plane(plane)->pipe;
  12760. unsigned stride;
  12761. int ret;
  12762. ret = drm_plane_helper_check_state(&state->base,
  12763. &state->clip,
  12764. DRM_PLANE_HELPER_NO_SCALING,
  12765. DRM_PLANE_HELPER_NO_SCALING,
  12766. true, true);
  12767. if (ret)
  12768. return ret;
  12769. /* if we want to turn off the cursor ignore width and height */
  12770. if (!obj)
  12771. return 0;
  12772. /* Check for which cursor types we support */
  12773. if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
  12774. state->base.crtc_h)) {
  12775. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  12776. state->base.crtc_w, state->base.crtc_h);
  12777. return -EINVAL;
  12778. }
  12779. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  12780. if (obj->base.size < stride * state->base.crtc_h) {
  12781. DRM_DEBUG_KMS("buffer is too small\n");
  12782. return -ENOMEM;
  12783. }
  12784. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  12785. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  12786. return -EINVAL;
  12787. }
  12788. /*
  12789. * There's something wrong with the cursor on CHV pipe C.
  12790. * If it straddles the left edge of the screen then
  12791. * moving it away from the edge or disabling it often
  12792. * results in a pipe underrun, and often that can lead to
  12793. * dead pipe (constant underrun reported, and it scans
  12794. * out just a solid color). To recover from that, the
  12795. * display power well must be turned off and on again.
  12796. * Refuse the put the cursor into that compromised position.
  12797. */
  12798. if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
  12799. state->base.visible && state->base.crtc_x < 0) {
  12800. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12801. return -EINVAL;
  12802. }
  12803. return 0;
  12804. }
  12805. static void
  12806. intel_disable_cursor_plane(struct drm_plane *plane,
  12807. struct drm_crtc *crtc)
  12808. {
  12809. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12810. intel_crtc->cursor_addr = 0;
  12811. intel_crtc_update_cursor(crtc, NULL);
  12812. }
  12813. static void
  12814. intel_update_cursor_plane(struct drm_plane *plane,
  12815. const struct intel_crtc_state *crtc_state,
  12816. const struct intel_plane_state *state)
  12817. {
  12818. struct drm_crtc *crtc = crtc_state->base.crtc;
  12819. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12820. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12821. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12822. uint32_t addr;
  12823. if (!obj)
  12824. addr = 0;
  12825. else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
  12826. addr = i915_gem_object_ggtt_offset(obj, NULL);
  12827. else
  12828. addr = obj->phys_handle->busaddr;
  12829. intel_crtc->cursor_addr = addr;
  12830. intel_crtc_update_cursor(crtc, state);
  12831. }
  12832. static struct intel_plane *
  12833. intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12834. {
  12835. struct intel_plane *cursor = NULL;
  12836. struct intel_plane_state *state = NULL;
  12837. int ret;
  12838. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12839. if (!cursor) {
  12840. ret = -ENOMEM;
  12841. goto fail;
  12842. }
  12843. state = intel_create_plane_state(&cursor->base);
  12844. if (!state) {
  12845. ret = -ENOMEM;
  12846. goto fail;
  12847. }
  12848. cursor->base.state = &state->base;
  12849. cursor->can_scale = false;
  12850. cursor->max_downscale = 1;
  12851. cursor->pipe = pipe;
  12852. cursor->plane = pipe;
  12853. cursor->id = PLANE_CURSOR;
  12854. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12855. cursor->check_plane = intel_check_cursor_plane;
  12856. cursor->update_plane = intel_update_cursor_plane;
  12857. cursor->disable_plane = intel_disable_cursor_plane;
  12858. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  12859. 0, &intel_cursor_plane_funcs,
  12860. intel_cursor_formats,
  12861. ARRAY_SIZE(intel_cursor_formats),
  12862. DRM_PLANE_TYPE_CURSOR,
  12863. "cursor %c", pipe_name(pipe));
  12864. if (ret)
  12865. goto fail;
  12866. if (INTEL_GEN(dev_priv) >= 4)
  12867. drm_plane_create_rotation_property(&cursor->base,
  12868. DRM_ROTATE_0,
  12869. DRM_ROTATE_0 |
  12870. DRM_ROTATE_180);
  12871. if (INTEL_GEN(dev_priv) >= 9)
  12872. state->scaler_id = -1;
  12873. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12874. return cursor;
  12875. fail:
  12876. kfree(state);
  12877. kfree(cursor);
  12878. return ERR_PTR(ret);
  12879. }
  12880. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  12881. struct intel_crtc_state *crtc_state)
  12882. {
  12883. struct intel_crtc_scaler_state *scaler_state =
  12884. &crtc_state->scaler_state;
  12885. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12886. int i;
  12887. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  12888. if (!crtc->num_scalers)
  12889. return;
  12890. for (i = 0; i < crtc->num_scalers; i++) {
  12891. struct intel_scaler *scaler = &scaler_state->scalers[i];
  12892. scaler->in_use = 0;
  12893. scaler->mode = PS_SCALER_MODE_DYN;
  12894. }
  12895. scaler_state->scaler_id = -1;
  12896. }
  12897. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  12898. {
  12899. struct intel_crtc *intel_crtc;
  12900. struct intel_crtc_state *crtc_state = NULL;
  12901. struct intel_plane *primary = NULL;
  12902. struct intel_plane *cursor = NULL;
  12903. int sprite, ret;
  12904. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12905. if (!intel_crtc)
  12906. return -ENOMEM;
  12907. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12908. if (!crtc_state) {
  12909. ret = -ENOMEM;
  12910. goto fail;
  12911. }
  12912. intel_crtc->config = crtc_state;
  12913. intel_crtc->base.state = &crtc_state->base;
  12914. crtc_state->base.crtc = &intel_crtc->base;
  12915. primary = intel_primary_plane_create(dev_priv, pipe);
  12916. if (IS_ERR(primary)) {
  12917. ret = PTR_ERR(primary);
  12918. goto fail;
  12919. }
  12920. intel_crtc->plane_ids_mask |= BIT(primary->id);
  12921. for_each_sprite(dev_priv, pipe, sprite) {
  12922. struct intel_plane *plane;
  12923. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  12924. if (IS_ERR(plane)) {
  12925. ret = PTR_ERR(plane);
  12926. goto fail;
  12927. }
  12928. intel_crtc->plane_ids_mask |= BIT(plane->id);
  12929. }
  12930. cursor = intel_cursor_plane_create(dev_priv, pipe);
  12931. if (IS_ERR(cursor)) {
  12932. ret = PTR_ERR(cursor);
  12933. goto fail;
  12934. }
  12935. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  12936. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  12937. &primary->base, &cursor->base,
  12938. &intel_crtc_funcs,
  12939. "pipe %c", pipe_name(pipe));
  12940. if (ret)
  12941. goto fail;
  12942. intel_crtc->pipe = pipe;
  12943. intel_crtc->plane = primary->plane;
  12944. intel_crtc->cursor_base = ~0;
  12945. intel_crtc->cursor_cntl = ~0;
  12946. intel_crtc->cursor_size = ~0;
  12947. intel_crtc->wm.cxsr_allowed = true;
  12948. /* initialize shared scalers */
  12949. intel_crtc_init_scalers(intel_crtc, crtc_state);
  12950. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12951. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12952. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  12953. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  12954. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12955. intel_color_init(&intel_crtc->base);
  12956. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12957. return 0;
  12958. fail:
  12959. /*
  12960. * drm_mode_config_cleanup() will free up any
  12961. * crtcs/planes already initialized.
  12962. */
  12963. kfree(crtc_state);
  12964. kfree(intel_crtc);
  12965. return ret;
  12966. }
  12967. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12968. {
  12969. struct drm_encoder *encoder = connector->base.encoder;
  12970. struct drm_device *dev = connector->base.dev;
  12971. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12972. if (!encoder || WARN_ON(!encoder->crtc))
  12973. return INVALID_PIPE;
  12974. return to_intel_crtc(encoder->crtc)->pipe;
  12975. }
  12976. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12977. struct drm_file *file)
  12978. {
  12979. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12980. struct drm_crtc *drmmode_crtc;
  12981. struct intel_crtc *crtc;
  12982. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12983. if (!drmmode_crtc)
  12984. return -ENOENT;
  12985. crtc = to_intel_crtc(drmmode_crtc);
  12986. pipe_from_crtc_id->pipe = crtc->pipe;
  12987. return 0;
  12988. }
  12989. static int intel_encoder_clones(struct intel_encoder *encoder)
  12990. {
  12991. struct drm_device *dev = encoder->base.dev;
  12992. struct intel_encoder *source_encoder;
  12993. int index_mask = 0;
  12994. int entry = 0;
  12995. for_each_intel_encoder(dev, source_encoder) {
  12996. if (encoders_cloneable(encoder, source_encoder))
  12997. index_mask |= (1 << entry);
  12998. entry++;
  12999. }
  13000. return index_mask;
  13001. }
  13002. static bool has_edp_a(struct drm_i915_private *dev_priv)
  13003. {
  13004. if (!IS_MOBILE(dev_priv))
  13005. return false;
  13006. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  13007. return false;
  13008. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  13009. return false;
  13010. return true;
  13011. }
  13012. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  13013. {
  13014. if (INTEL_GEN(dev_priv) >= 9)
  13015. return false;
  13016. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  13017. return false;
  13018. if (IS_CHERRYVIEW(dev_priv))
  13019. return false;
  13020. if (HAS_PCH_LPT_H(dev_priv) &&
  13021. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  13022. return false;
  13023. /* DDI E can't be used if DDI A requires 4 lanes */
  13024. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  13025. return false;
  13026. if (!dev_priv->vbt.int_crt_support)
  13027. return false;
  13028. return true;
  13029. }
  13030. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  13031. {
  13032. int pps_num;
  13033. int pps_idx;
  13034. if (HAS_DDI(dev_priv))
  13035. return;
  13036. /*
  13037. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  13038. * everywhere where registers can be write protected.
  13039. */
  13040. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13041. pps_num = 2;
  13042. else
  13043. pps_num = 1;
  13044. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  13045. u32 val = I915_READ(PP_CONTROL(pps_idx));
  13046. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  13047. I915_WRITE(PP_CONTROL(pps_idx), val);
  13048. }
  13049. }
  13050. static void intel_pps_init(struct drm_i915_private *dev_priv)
  13051. {
  13052. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  13053. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  13054. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13055. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  13056. else
  13057. dev_priv->pps_mmio_base = PPS_BASE;
  13058. intel_pps_unlock_regs_wa(dev_priv);
  13059. }
  13060. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  13061. {
  13062. struct intel_encoder *encoder;
  13063. bool dpd_is_edp = false;
  13064. intel_pps_init(dev_priv);
  13065. /*
  13066. * intel_edp_init_connector() depends on this completing first, to
  13067. * prevent the registeration of both eDP and LVDS and the incorrect
  13068. * sharing of the PPS.
  13069. */
  13070. intel_lvds_init(dev_priv);
  13071. if (intel_crt_present(dev_priv))
  13072. intel_crt_init(dev_priv);
  13073. if (IS_GEN9_LP(dev_priv)) {
  13074. /*
  13075. * FIXME: Broxton doesn't support port detection via the
  13076. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  13077. * detect the ports.
  13078. */
  13079. intel_ddi_init(dev_priv, PORT_A);
  13080. intel_ddi_init(dev_priv, PORT_B);
  13081. intel_ddi_init(dev_priv, PORT_C);
  13082. intel_dsi_init(dev_priv);
  13083. } else if (HAS_DDI(dev_priv)) {
  13084. int found;
  13085. /*
  13086. * Haswell uses DDI functions to detect digital outputs.
  13087. * On SKL pre-D0 the strap isn't connected, so we assume
  13088. * it's there.
  13089. */
  13090. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  13091. /* WaIgnoreDDIAStrap: skl */
  13092. if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  13093. intel_ddi_init(dev_priv, PORT_A);
  13094. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  13095. * register */
  13096. found = I915_READ(SFUSE_STRAP);
  13097. if (found & SFUSE_STRAP_DDIB_DETECTED)
  13098. intel_ddi_init(dev_priv, PORT_B);
  13099. if (found & SFUSE_STRAP_DDIC_DETECTED)
  13100. intel_ddi_init(dev_priv, PORT_C);
  13101. if (found & SFUSE_STRAP_DDID_DETECTED)
  13102. intel_ddi_init(dev_priv, PORT_D);
  13103. /*
  13104. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  13105. */
  13106. if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
  13107. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  13108. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  13109. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  13110. intel_ddi_init(dev_priv, PORT_E);
  13111. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13112. int found;
  13113. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  13114. if (has_edp_a(dev_priv))
  13115. intel_dp_init(dev_priv, DP_A, PORT_A);
  13116. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  13117. /* PCH SDVOB multiplex with HDMIB */
  13118. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  13119. if (!found)
  13120. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  13121. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  13122. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  13123. }
  13124. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  13125. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  13126. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  13127. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  13128. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  13129. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  13130. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  13131. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  13132. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13133. bool has_edp, has_port;
  13134. /*
  13135. * The DP_DETECTED bit is the latched state of the DDC
  13136. * SDA pin at boot. However since eDP doesn't require DDC
  13137. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  13138. * eDP ports may have been muxed to an alternate function.
  13139. * Thus we can't rely on the DP_DETECTED bit alone to detect
  13140. * eDP ports. Consult the VBT as well as DP_DETECTED to
  13141. * detect eDP ports.
  13142. *
  13143. * Sadly the straps seem to be missing sometimes even for HDMI
  13144. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  13145. * and VBT for the presence of the port. Additionally we can't
  13146. * trust the port type the VBT declares as we've seen at least
  13147. * HDMI ports that the VBT claim are DP or eDP.
  13148. */
  13149. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  13150. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  13151. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  13152. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  13153. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  13154. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  13155. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  13156. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  13157. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  13158. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  13159. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  13160. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  13161. if (IS_CHERRYVIEW(dev_priv)) {
  13162. /*
  13163. * eDP not supported on port D,
  13164. * so no need to worry about it
  13165. */
  13166. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  13167. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  13168. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  13169. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  13170. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  13171. }
  13172. intel_dsi_init(dev_priv);
  13173. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  13174. bool found = false;
  13175. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13176. DRM_DEBUG_KMS("probing SDVOB\n");
  13177. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  13178. if (!found && IS_G4X(dev_priv)) {
  13179. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  13180. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  13181. }
  13182. if (!found && IS_G4X(dev_priv))
  13183. intel_dp_init(dev_priv, DP_B, PORT_B);
  13184. }
  13185. /* Before G4X SDVOC doesn't have its own detect register */
  13186. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13187. DRM_DEBUG_KMS("probing SDVOC\n");
  13188. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  13189. }
  13190. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  13191. if (IS_G4X(dev_priv)) {
  13192. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  13193. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  13194. }
  13195. if (IS_G4X(dev_priv))
  13196. intel_dp_init(dev_priv, DP_C, PORT_C);
  13197. }
  13198. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  13199. intel_dp_init(dev_priv, DP_D, PORT_D);
  13200. } else if (IS_GEN2(dev_priv))
  13201. intel_dvo_init(dev_priv);
  13202. if (SUPPORTS_TV(dev_priv))
  13203. intel_tv_init(dev_priv);
  13204. intel_psr_init(dev_priv);
  13205. for_each_intel_encoder(&dev_priv->drm, encoder) {
  13206. encoder->base.possible_crtcs = encoder->crtc_mask;
  13207. encoder->base.possible_clones =
  13208. intel_encoder_clones(encoder);
  13209. }
  13210. intel_init_pch_refclk(dev_priv);
  13211. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  13212. }
  13213. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  13214. {
  13215. struct drm_device *dev = fb->dev;
  13216. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13217. drm_framebuffer_cleanup(fb);
  13218. mutex_lock(&dev->struct_mutex);
  13219. WARN_ON(!intel_fb->obj->framebuffer_references--);
  13220. i915_gem_object_put(intel_fb->obj);
  13221. mutex_unlock(&dev->struct_mutex);
  13222. kfree(intel_fb);
  13223. }
  13224. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  13225. struct drm_file *file,
  13226. unsigned int *handle)
  13227. {
  13228. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13229. struct drm_i915_gem_object *obj = intel_fb->obj;
  13230. if (obj->userptr.mm) {
  13231. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  13232. return -EINVAL;
  13233. }
  13234. return drm_gem_handle_create(file, &obj->base, handle);
  13235. }
  13236. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  13237. struct drm_file *file,
  13238. unsigned flags, unsigned color,
  13239. struct drm_clip_rect *clips,
  13240. unsigned num_clips)
  13241. {
  13242. struct drm_device *dev = fb->dev;
  13243. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13244. struct drm_i915_gem_object *obj = intel_fb->obj;
  13245. mutex_lock(&dev->struct_mutex);
  13246. if (obj->pin_display && obj->cache_dirty)
  13247. i915_gem_clflush_object(obj, true);
  13248. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  13249. mutex_unlock(&dev->struct_mutex);
  13250. return 0;
  13251. }
  13252. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  13253. .destroy = intel_user_framebuffer_destroy,
  13254. .create_handle = intel_user_framebuffer_create_handle,
  13255. .dirty = intel_user_framebuffer_dirty,
  13256. };
  13257. static
  13258. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  13259. uint64_t fb_modifier, uint32_t pixel_format)
  13260. {
  13261. u32 gen = INTEL_INFO(dev_priv)->gen;
  13262. if (gen >= 9) {
  13263. int cpp = drm_format_plane_cpp(pixel_format, 0);
  13264. /* "The stride in bytes must not exceed the of the size of 8K
  13265. * pixels and 32K bytes."
  13266. */
  13267. return min(8192 * cpp, 32768);
  13268. } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
  13269. !IS_CHERRYVIEW(dev_priv)) {
  13270. return 32*1024;
  13271. } else if (gen >= 4) {
  13272. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13273. return 16*1024;
  13274. else
  13275. return 32*1024;
  13276. } else if (gen >= 3) {
  13277. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13278. return 8*1024;
  13279. else
  13280. return 16*1024;
  13281. } else {
  13282. /* XXX DSPC is limited to 4k tiled */
  13283. return 8*1024;
  13284. }
  13285. }
  13286. static int intel_framebuffer_init(struct drm_device *dev,
  13287. struct intel_framebuffer *intel_fb,
  13288. struct drm_mode_fb_cmd2 *mode_cmd,
  13289. struct drm_i915_gem_object *obj)
  13290. {
  13291. struct drm_i915_private *dev_priv = to_i915(dev);
  13292. unsigned int tiling = i915_gem_object_get_tiling(obj);
  13293. int ret;
  13294. u32 pitch_limit, stride_alignment;
  13295. struct drm_format_name_buf format_name;
  13296. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  13297. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  13298. /*
  13299. * If there's a fence, enforce that
  13300. * the fb modifier and tiling mode match.
  13301. */
  13302. if (tiling != I915_TILING_NONE &&
  13303. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13304. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  13305. return -EINVAL;
  13306. }
  13307. } else {
  13308. if (tiling == I915_TILING_X) {
  13309. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  13310. } else if (tiling == I915_TILING_Y) {
  13311. DRM_DEBUG("No Y tiling for legacy addfb\n");
  13312. return -EINVAL;
  13313. }
  13314. }
  13315. /* Passed in modifier sanity checking. */
  13316. switch (mode_cmd->modifier[0]) {
  13317. case I915_FORMAT_MOD_Y_TILED:
  13318. case I915_FORMAT_MOD_Yf_TILED:
  13319. if (INTEL_GEN(dev_priv) < 9) {
  13320. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  13321. mode_cmd->modifier[0]);
  13322. return -EINVAL;
  13323. }
  13324. case DRM_FORMAT_MOD_NONE:
  13325. case I915_FORMAT_MOD_X_TILED:
  13326. break;
  13327. default:
  13328. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  13329. mode_cmd->modifier[0]);
  13330. return -EINVAL;
  13331. }
  13332. /*
  13333. * gen2/3 display engine uses the fence if present,
  13334. * so the tiling mode must match the fb modifier exactly.
  13335. */
  13336. if (INTEL_INFO(dev_priv)->gen < 4 &&
  13337. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13338. DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
  13339. return -EINVAL;
  13340. }
  13341. stride_alignment = intel_fb_stride_alignment(dev_priv,
  13342. mode_cmd->modifier[0],
  13343. mode_cmd->pixel_format);
  13344. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  13345. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  13346. mode_cmd->pitches[0], stride_alignment);
  13347. return -EINVAL;
  13348. }
  13349. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  13350. mode_cmd->pixel_format);
  13351. if (mode_cmd->pitches[0] > pitch_limit) {
  13352. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  13353. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  13354. "tiled" : "linear",
  13355. mode_cmd->pitches[0], pitch_limit);
  13356. return -EINVAL;
  13357. }
  13358. /*
  13359. * If there's a fence, enforce that
  13360. * the fb pitch and fence stride match.
  13361. */
  13362. if (tiling != I915_TILING_NONE &&
  13363. mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
  13364. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  13365. mode_cmd->pitches[0],
  13366. i915_gem_object_get_stride(obj));
  13367. return -EINVAL;
  13368. }
  13369. /* Reject formats not supported by any plane early. */
  13370. switch (mode_cmd->pixel_format) {
  13371. case DRM_FORMAT_C8:
  13372. case DRM_FORMAT_RGB565:
  13373. case DRM_FORMAT_XRGB8888:
  13374. case DRM_FORMAT_ARGB8888:
  13375. break;
  13376. case DRM_FORMAT_XRGB1555:
  13377. if (INTEL_GEN(dev_priv) > 3) {
  13378. DRM_DEBUG("unsupported pixel format: %s\n",
  13379. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13380. return -EINVAL;
  13381. }
  13382. break;
  13383. case DRM_FORMAT_ABGR8888:
  13384. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  13385. INTEL_GEN(dev_priv) < 9) {
  13386. DRM_DEBUG("unsupported pixel format: %s\n",
  13387. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13388. return -EINVAL;
  13389. }
  13390. break;
  13391. case DRM_FORMAT_XBGR8888:
  13392. case DRM_FORMAT_XRGB2101010:
  13393. case DRM_FORMAT_XBGR2101010:
  13394. if (INTEL_GEN(dev_priv) < 4) {
  13395. DRM_DEBUG("unsupported pixel format: %s\n",
  13396. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13397. return -EINVAL;
  13398. }
  13399. break;
  13400. case DRM_FORMAT_ABGR2101010:
  13401. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  13402. DRM_DEBUG("unsupported pixel format: %s\n",
  13403. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13404. return -EINVAL;
  13405. }
  13406. break;
  13407. case DRM_FORMAT_YUYV:
  13408. case DRM_FORMAT_UYVY:
  13409. case DRM_FORMAT_YVYU:
  13410. case DRM_FORMAT_VYUY:
  13411. if (INTEL_GEN(dev_priv) < 5) {
  13412. DRM_DEBUG("unsupported pixel format: %s\n",
  13413. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13414. return -EINVAL;
  13415. }
  13416. break;
  13417. default:
  13418. DRM_DEBUG("unsupported pixel format: %s\n",
  13419. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13420. return -EINVAL;
  13421. }
  13422. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  13423. if (mode_cmd->offsets[0] != 0)
  13424. return -EINVAL;
  13425. drm_helper_mode_fill_fb_struct(dev, &intel_fb->base, mode_cmd);
  13426. intel_fb->obj = obj;
  13427. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  13428. if (ret)
  13429. return ret;
  13430. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  13431. if (ret) {
  13432. DRM_ERROR("framebuffer init failed %d\n", ret);
  13433. return ret;
  13434. }
  13435. intel_fb->obj->framebuffer_references++;
  13436. return 0;
  13437. }
  13438. static struct drm_framebuffer *
  13439. intel_user_framebuffer_create(struct drm_device *dev,
  13440. struct drm_file *filp,
  13441. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  13442. {
  13443. struct drm_framebuffer *fb;
  13444. struct drm_i915_gem_object *obj;
  13445. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  13446. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  13447. if (!obj)
  13448. return ERR_PTR(-ENOENT);
  13449. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  13450. if (IS_ERR(fb))
  13451. i915_gem_object_put(obj);
  13452. return fb;
  13453. }
  13454. static void intel_atomic_state_free(struct drm_atomic_state *state)
  13455. {
  13456. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  13457. drm_atomic_state_default_release(state);
  13458. i915_sw_fence_fini(&intel_state->commit_ready);
  13459. kfree(state);
  13460. }
  13461. static const struct drm_mode_config_funcs intel_mode_funcs = {
  13462. .fb_create = intel_user_framebuffer_create,
  13463. .output_poll_changed = intel_fbdev_output_poll_changed,
  13464. .atomic_check = intel_atomic_check,
  13465. .atomic_commit = intel_atomic_commit,
  13466. .atomic_state_alloc = intel_atomic_state_alloc,
  13467. .atomic_state_clear = intel_atomic_state_clear,
  13468. .atomic_state_free = intel_atomic_state_free,
  13469. };
  13470. /**
  13471. * intel_init_display_hooks - initialize the display modesetting hooks
  13472. * @dev_priv: device private
  13473. */
  13474. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  13475. {
  13476. if (INTEL_INFO(dev_priv)->gen >= 9) {
  13477. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13478. dev_priv->display.get_initial_plane_config =
  13479. skylake_get_initial_plane_config;
  13480. dev_priv->display.crtc_compute_clock =
  13481. haswell_crtc_compute_clock;
  13482. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13483. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13484. } else if (HAS_DDI(dev_priv)) {
  13485. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13486. dev_priv->display.get_initial_plane_config =
  13487. ironlake_get_initial_plane_config;
  13488. dev_priv->display.crtc_compute_clock =
  13489. haswell_crtc_compute_clock;
  13490. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13491. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13492. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13493. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  13494. dev_priv->display.get_initial_plane_config =
  13495. ironlake_get_initial_plane_config;
  13496. dev_priv->display.crtc_compute_clock =
  13497. ironlake_crtc_compute_clock;
  13498. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  13499. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  13500. } else if (IS_CHERRYVIEW(dev_priv)) {
  13501. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13502. dev_priv->display.get_initial_plane_config =
  13503. i9xx_get_initial_plane_config;
  13504. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  13505. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13506. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13507. } else if (IS_VALLEYVIEW(dev_priv)) {
  13508. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13509. dev_priv->display.get_initial_plane_config =
  13510. i9xx_get_initial_plane_config;
  13511. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  13512. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13513. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13514. } else if (IS_G4X(dev_priv)) {
  13515. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13516. dev_priv->display.get_initial_plane_config =
  13517. i9xx_get_initial_plane_config;
  13518. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  13519. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13520. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13521. } else if (IS_PINEVIEW(dev_priv)) {
  13522. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13523. dev_priv->display.get_initial_plane_config =
  13524. i9xx_get_initial_plane_config;
  13525. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  13526. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13527. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13528. } else if (!IS_GEN2(dev_priv)) {
  13529. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13530. dev_priv->display.get_initial_plane_config =
  13531. i9xx_get_initial_plane_config;
  13532. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  13533. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13534. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13535. } else {
  13536. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13537. dev_priv->display.get_initial_plane_config =
  13538. i9xx_get_initial_plane_config;
  13539. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  13540. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13541. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13542. }
  13543. /* Returns the core display clock speed */
  13544. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  13545. dev_priv->display.get_display_clock_speed =
  13546. skylake_get_display_clock_speed;
  13547. else if (IS_GEN9_LP(dev_priv))
  13548. dev_priv->display.get_display_clock_speed =
  13549. broxton_get_display_clock_speed;
  13550. else if (IS_BROADWELL(dev_priv))
  13551. dev_priv->display.get_display_clock_speed =
  13552. broadwell_get_display_clock_speed;
  13553. else if (IS_HASWELL(dev_priv))
  13554. dev_priv->display.get_display_clock_speed =
  13555. haswell_get_display_clock_speed;
  13556. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13557. dev_priv->display.get_display_clock_speed =
  13558. valleyview_get_display_clock_speed;
  13559. else if (IS_GEN5(dev_priv))
  13560. dev_priv->display.get_display_clock_speed =
  13561. ilk_get_display_clock_speed;
  13562. else if (IS_I945G(dev_priv) || IS_I965G(dev_priv) ||
  13563. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  13564. dev_priv->display.get_display_clock_speed =
  13565. i945_get_display_clock_speed;
  13566. else if (IS_GM45(dev_priv))
  13567. dev_priv->display.get_display_clock_speed =
  13568. gm45_get_display_clock_speed;
  13569. else if (IS_I965GM(dev_priv))
  13570. dev_priv->display.get_display_clock_speed =
  13571. i965gm_get_display_clock_speed;
  13572. else if (IS_PINEVIEW(dev_priv))
  13573. dev_priv->display.get_display_clock_speed =
  13574. pnv_get_display_clock_speed;
  13575. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  13576. dev_priv->display.get_display_clock_speed =
  13577. g33_get_display_clock_speed;
  13578. else if (IS_I915G(dev_priv))
  13579. dev_priv->display.get_display_clock_speed =
  13580. i915_get_display_clock_speed;
  13581. else if (IS_I945GM(dev_priv) || IS_I845G(dev_priv))
  13582. dev_priv->display.get_display_clock_speed =
  13583. i9xx_misc_get_display_clock_speed;
  13584. else if (IS_I915GM(dev_priv))
  13585. dev_priv->display.get_display_clock_speed =
  13586. i915gm_get_display_clock_speed;
  13587. else if (IS_I865G(dev_priv))
  13588. dev_priv->display.get_display_clock_speed =
  13589. i865_get_display_clock_speed;
  13590. else if (IS_I85X(dev_priv))
  13591. dev_priv->display.get_display_clock_speed =
  13592. i85x_get_display_clock_speed;
  13593. else { /* 830 */
  13594. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  13595. dev_priv->display.get_display_clock_speed =
  13596. i830_get_display_clock_speed;
  13597. }
  13598. if (IS_GEN5(dev_priv)) {
  13599. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  13600. } else if (IS_GEN6(dev_priv)) {
  13601. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  13602. } else if (IS_IVYBRIDGE(dev_priv)) {
  13603. /* FIXME: detect B0+ stepping and use auto training */
  13604. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  13605. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  13606. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  13607. }
  13608. if (IS_BROADWELL(dev_priv)) {
  13609. dev_priv->display.modeset_commit_cdclk =
  13610. broadwell_modeset_commit_cdclk;
  13611. dev_priv->display.modeset_calc_cdclk =
  13612. broadwell_modeset_calc_cdclk;
  13613. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13614. dev_priv->display.modeset_commit_cdclk =
  13615. valleyview_modeset_commit_cdclk;
  13616. dev_priv->display.modeset_calc_cdclk =
  13617. valleyview_modeset_calc_cdclk;
  13618. } else if (IS_GEN9_LP(dev_priv)) {
  13619. dev_priv->display.modeset_commit_cdclk =
  13620. bxt_modeset_commit_cdclk;
  13621. dev_priv->display.modeset_calc_cdclk =
  13622. bxt_modeset_calc_cdclk;
  13623. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  13624. dev_priv->display.modeset_commit_cdclk =
  13625. skl_modeset_commit_cdclk;
  13626. dev_priv->display.modeset_calc_cdclk =
  13627. skl_modeset_calc_cdclk;
  13628. }
  13629. if (dev_priv->info.gen >= 9)
  13630. dev_priv->display.update_crtcs = skl_update_crtcs;
  13631. else
  13632. dev_priv->display.update_crtcs = intel_update_crtcs;
  13633. switch (INTEL_INFO(dev_priv)->gen) {
  13634. case 2:
  13635. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  13636. break;
  13637. case 3:
  13638. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  13639. break;
  13640. case 4:
  13641. case 5:
  13642. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  13643. break;
  13644. case 6:
  13645. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  13646. break;
  13647. case 7:
  13648. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  13649. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  13650. break;
  13651. case 9:
  13652. /* Drop through - unsupported since execlist only. */
  13653. default:
  13654. /* Default just returns -ENODEV to indicate unsupported */
  13655. dev_priv->display.queue_flip = intel_default_queue_flip;
  13656. }
  13657. }
  13658. /*
  13659. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  13660. * resume, or other times. This quirk makes sure that's the case for
  13661. * affected systems.
  13662. */
  13663. static void quirk_pipea_force(struct drm_device *dev)
  13664. {
  13665. struct drm_i915_private *dev_priv = to_i915(dev);
  13666. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  13667. DRM_INFO("applying pipe a force quirk\n");
  13668. }
  13669. static void quirk_pipeb_force(struct drm_device *dev)
  13670. {
  13671. struct drm_i915_private *dev_priv = to_i915(dev);
  13672. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  13673. DRM_INFO("applying pipe b force quirk\n");
  13674. }
  13675. /*
  13676. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  13677. */
  13678. static void quirk_ssc_force_disable(struct drm_device *dev)
  13679. {
  13680. struct drm_i915_private *dev_priv = to_i915(dev);
  13681. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  13682. DRM_INFO("applying lvds SSC disable quirk\n");
  13683. }
  13684. /*
  13685. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  13686. * brightness value
  13687. */
  13688. static void quirk_invert_brightness(struct drm_device *dev)
  13689. {
  13690. struct drm_i915_private *dev_priv = to_i915(dev);
  13691. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  13692. DRM_INFO("applying inverted panel brightness quirk\n");
  13693. }
  13694. /* Some VBT's incorrectly indicate no backlight is present */
  13695. static void quirk_backlight_present(struct drm_device *dev)
  13696. {
  13697. struct drm_i915_private *dev_priv = to_i915(dev);
  13698. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  13699. DRM_INFO("applying backlight present quirk\n");
  13700. }
  13701. struct intel_quirk {
  13702. int device;
  13703. int subsystem_vendor;
  13704. int subsystem_device;
  13705. void (*hook)(struct drm_device *dev);
  13706. };
  13707. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  13708. struct intel_dmi_quirk {
  13709. void (*hook)(struct drm_device *dev);
  13710. const struct dmi_system_id (*dmi_id_list)[];
  13711. };
  13712. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  13713. {
  13714. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  13715. return 1;
  13716. }
  13717. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  13718. {
  13719. .dmi_id_list = &(const struct dmi_system_id[]) {
  13720. {
  13721. .callback = intel_dmi_reverse_brightness,
  13722. .ident = "NCR Corporation",
  13723. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  13724. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  13725. },
  13726. },
  13727. { } /* terminating entry */
  13728. },
  13729. .hook = quirk_invert_brightness,
  13730. },
  13731. };
  13732. static struct intel_quirk intel_quirks[] = {
  13733. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  13734. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  13735. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  13736. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  13737. /* 830 needs to leave pipe A & dpll A up */
  13738. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  13739. /* 830 needs to leave pipe B & dpll B up */
  13740. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  13741. /* Lenovo U160 cannot use SSC on LVDS */
  13742. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  13743. /* Sony Vaio Y cannot use SSC on LVDS */
  13744. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  13745. /* Acer Aspire 5734Z must invert backlight brightness */
  13746. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  13747. /* Acer/eMachines G725 */
  13748. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  13749. /* Acer/eMachines e725 */
  13750. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  13751. /* Acer/Packard Bell NCL20 */
  13752. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  13753. /* Acer Aspire 4736Z */
  13754. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  13755. /* Acer Aspire 5336 */
  13756. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  13757. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  13758. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  13759. /* Acer C720 Chromebook (Core i3 4005U) */
  13760. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  13761. /* Apple Macbook 2,1 (Core 2 T7400) */
  13762. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  13763. /* Apple Macbook 4,1 */
  13764. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  13765. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  13766. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  13767. /* HP Chromebook 14 (Celeron 2955U) */
  13768. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  13769. /* Dell Chromebook 11 */
  13770. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  13771. /* Dell Chromebook 11 (2015 version) */
  13772. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  13773. };
  13774. static void intel_init_quirks(struct drm_device *dev)
  13775. {
  13776. struct pci_dev *d = dev->pdev;
  13777. int i;
  13778. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  13779. struct intel_quirk *q = &intel_quirks[i];
  13780. if (d->device == q->device &&
  13781. (d->subsystem_vendor == q->subsystem_vendor ||
  13782. q->subsystem_vendor == PCI_ANY_ID) &&
  13783. (d->subsystem_device == q->subsystem_device ||
  13784. q->subsystem_device == PCI_ANY_ID))
  13785. q->hook(dev);
  13786. }
  13787. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  13788. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  13789. intel_dmi_quirks[i].hook(dev);
  13790. }
  13791. }
  13792. /* Disable the VGA plane that we never use */
  13793. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  13794. {
  13795. struct pci_dev *pdev = dev_priv->drm.pdev;
  13796. u8 sr1;
  13797. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  13798. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  13799. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  13800. outb(SR01, VGA_SR_INDEX);
  13801. sr1 = inb(VGA_SR_DATA);
  13802. outb(sr1 | 1<<5, VGA_SR_DATA);
  13803. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  13804. udelay(300);
  13805. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  13806. POSTING_READ(vga_reg);
  13807. }
  13808. void intel_modeset_init_hw(struct drm_device *dev)
  13809. {
  13810. struct drm_i915_private *dev_priv = to_i915(dev);
  13811. intel_update_cdclk(dev_priv);
  13812. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13813. intel_init_clock_gating(dev_priv);
  13814. }
  13815. /*
  13816. * Calculate what we think the watermarks should be for the state we've read
  13817. * out of the hardware and then immediately program those watermarks so that
  13818. * we ensure the hardware settings match our internal state.
  13819. *
  13820. * We can calculate what we think WM's should be by creating a duplicate of the
  13821. * current state (which was constructed during hardware readout) and running it
  13822. * through the atomic check code to calculate new watermark values in the
  13823. * state object.
  13824. */
  13825. static void sanitize_watermarks(struct drm_device *dev)
  13826. {
  13827. struct drm_i915_private *dev_priv = to_i915(dev);
  13828. struct drm_atomic_state *state;
  13829. struct intel_atomic_state *intel_state;
  13830. struct drm_crtc *crtc;
  13831. struct drm_crtc_state *cstate;
  13832. struct drm_modeset_acquire_ctx ctx;
  13833. int ret;
  13834. int i;
  13835. /* Only supported on platforms that use atomic watermark design */
  13836. if (!dev_priv->display.optimize_watermarks)
  13837. return;
  13838. /*
  13839. * We need to hold connection_mutex before calling duplicate_state so
  13840. * that the connector loop is protected.
  13841. */
  13842. drm_modeset_acquire_init(&ctx, 0);
  13843. retry:
  13844. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13845. if (ret == -EDEADLK) {
  13846. drm_modeset_backoff(&ctx);
  13847. goto retry;
  13848. } else if (WARN_ON(ret)) {
  13849. goto fail;
  13850. }
  13851. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13852. if (WARN_ON(IS_ERR(state)))
  13853. goto fail;
  13854. intel_state = to_intel_atomic_state(state);
  13855. /*
  13856. * Hardware readout is the only time we don't want to calculate
  13857. * intermediate watermarks (since we don't trust the current
  13858. * watermarks).
  13859. */
  13860. intel_state->skip_intermediate_wm = true;
  13861. ret = intel_atomic_check(dev, state);
  13862. if (ret) {
  13863. /*
  13864. * If we fail here, it means that the hardware appears to be
  13865. * programmed in a way that shouldn't be possible, given our
  13866. * understanding of watermark requirements. This might mean a
  13867. * mistake in the hardware readout code or a mistake in the
  13868. * watermark calculations for a given platform. Raise a WARN
  13869. * so that this is noticeable.
  13870. *
  13871. * If this actually happens, we'll have to just leave the
  13872. * BIOS-programmed watermarks untouched and hope for the best.
  13873. */
  13874. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13875. goto put_state;
  13876. }
  13877. /* Write calculated watermark values back */
  13878. for_each_crtc_in_state(state, crtc, cstate, i) {
  13879. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13880. cs->wm.need_postvbl_update = true;
  13881. dev_priv->display.optimize_watermarks(intel_state, cs);
  13882. }
  13883. put_state:
  13884. drm_atomic_state_put(state);
  13885. fail:
  13886. drm_modeset_drop_locks(&ctx);
  13887. drm_modeset_acquire_fini(&ctx);
  13888. }
  13889. int intel_modeset_init(struct drm_device *dev)
  13890. {
  13891. struct drm_i915_private *dev_priv = to_i915(dev);
  13892. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13893. enum pipe pipe;
  13894. struct intel_crtc *crtc;
  13895. drm_mode_config_init(dev);
  13896. dev->mode_config.min_width = 0;
  13897. dev->mode_config.min_height = 0;
  13898. dev->mode_config.preferred_depth = 24;
  13899. dev->mode_config.prefer_shadow = 1;
  13900. dev->mode_config.allow_fb_modifiers = true;
  13901. dev->mode_config.funcs = &intel_mode_funcs;
  13902. intel_init_quirks(dev);
  13903. intel_init_pm(dev_priv);
  13904. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13905. return 0;
  13906. /*
  13907. * There may be no VBT; and if the BIOS enabled SSC we can
  13908. * just keep using it to avoid unnecessary flicker. Whereas if the
  13909. * BIOS isn't using it, don't assume it will work even if the VBT
  13910. * indicates as much.
  13911. */
  13912. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  13913. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13914. DREF_SSC1_ENABLE);
  13915. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13916. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13917. bios_lvds_use_ssc ? "en" : "dis",
  13918. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13919. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13920. }
  13921. }
  13922. if (IS_GEN2(dev_priv)) {
  13923. dev->mode_config.max_width = 2048;
  13924. dev->mode_config.max_height = 2048;
  13925. } else if (IS_GEN3(dev_priv)) {
  13926. dev->mode_config.max_width = 4096;
  13927. dev->mode_config.max_height = 4096;
  13928. } else {
  13929. dev->mode_config.max_width = 8192;
  13930. dev->mode_config.max_height = 8192;
  13931. }
  13932. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  13933. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  13934. dev->mode_config.cursor_height = 1023;
  13935. } else if (IS_GEN2(dev_priv)) {
  13936. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13937. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13938. } else {
  13939. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13940. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13941. }
  13942. dev->mode_config.fb_base = ggtt->mappable_base;
  13943. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13944. INTEL_INFO(dev_priv)->num_pipes,
  13945. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  13946. for_each_pipe(dev_priv, pipe) {
  13947. int ret;
  13948. ret = intel_crtc_init(dev_priv, pipe);
  13949. if (ret) {
  13950. drm_mode_config_cleanup(dev);
  13951. return ret;
  13952. }
  13953. }
  13954. intel_update_czclk(dev_priv);
  13955. intel_update_cdclk(dev_priv);
  13956. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13957. intel_shared_dpll_init(dev);
  13958. if (dev_priv->max_cdclk_freq == 0)
  13959. intel_update_max_cdclk(dev_priv);
  13960. /* Just disable it once at startup */
  13961. i915_disable_vga(dev_priv);
  13962. intel_setup_outputs(dev_priv);
  13963. drm_modeset_lock_all(dev);
  13964. intel_modeset_setup_hw_state(dev);
  13965. drm_modeset_unlock_all(dev);
  13966. for_each_intel_crtc(dev, crtc) {
  13967. struct intel_initial_plane_config plane_config = {};
  13968. if (!crtc->active)
  13969. continue;
  13970. /*
  13971. * Note that reserving the BIOS fb up front prevents us
  13972. * from stuffing other stolen allocations like the ring
  13973. * on top. This prevents some ugliness at boot time, and
  13974. * can even allow for smooth boot transitions if the BIOS
  13975. * fb is large enough for the active pipe configuration.
  13976. */
  13977. dev_priv->display.get_initial_plane_config(crtc,
  13978. &plane_config);
  13979. /*
  13980. * If the fb is shared between multiple heads, we'll
  13981. * just get the first one.
  13982. */
  13983. intel_find_initial_plane_obj(crtc, &plane_config);
  13984. }
  13985. /*
  13986. * Make sure hardware watermarks really match the state we read out.
  13987. * Note that we need to do this after reconstructing the BIOS fb's
  13988. * since the watermark calculation done here will use pstate->fb.
  13989. */
  13990. sanitize_watermarks(dev);
  13991. return 0;
  13992. }
  13993. static void intel_enable_pipe_a(struct drm_device *dev)
  13994. {
  13995. struct intel_connector *connector;
  13996. struct drm_connector *crt = NULL;
  13997. struct intel_load_detect_pipe load_detect_temp;
  13998. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13999. /* We can't just switch on the pipe A, we need to set things up with a
  14000. * proper mode and output configuration. As a gross hack, enable pipe A
  14001. * by enabling the load detect pipe once. */
  14002. for_each_intel_connector(dev, connector) {
  14003. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  14004. crt = &connector->base;
  14005. break;
  14006. }
  14007. }
  14008. if (!crt)
  14009. return;
  14010. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  14011. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  14012. }
  14013. static bool
  14014. intel_check_plane_mapping(struct intel_crtc *crtc)
  14015. {
  14016. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  14017. u32 val;
  14018. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  14019. return true;
  14020. val = I915_READ(DSPCNTR(!crtc->plane));
  14021. if ((val & DISPLAY_PLANE_ENABLE) &&
  14022. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  14023. return false;
  14024. return true;
  14025. }
  14026. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  14027. {
  14028. struct drm_device *dev = crtc->base.dev;
  14029. struct intel_encoder *encoder;
  14030. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  14031. return true;
  14032. return false;
  14033. }
  14034. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  14035. {
  14036. struct drm_device *dev = encoder->base.dev;
  14037. struct intel_connector *connector;
  14038. for_each_connector_on_encoder(dev, &encoder->base, connector)
  14039. return connector;
  14040. return NULL;
  14041. }
  14042. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  14043. enum transcoder pch_transcoder)
  14044. {
  14045. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  14046. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  14047. }
  14048. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  14049. {
  14050. struct drm_device *dev = crtc->base.dev;
  14051. struct drm_i915_private *dev_priv = to_i915(dev);
  14052. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  14053. /* Clear any frame start delays used for debugging left by the BIOS */
  14054. if (!transcoder_is_dsi(cpu_transcoder)) {
  14055. i915_reg_t reg = PIPECONF(cpu_transcoder);
  14056. I915_WRITE(reg,
  14057. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  14058. }
  14059. /* restore vblank interrupts to correct state */
  14060. drm_crtc_vblank_reset(&crtc->base);
  14061. if (crtc->active) {
  14062. struct intel_plane *plane;
  14063. drm_crtc_vblank_on(&crtc->base);
  14064. /* Disable everything but the primary plane */
  14065. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  14066. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  14067. continue;
  14068. plane->disable_plane(&plane->base, &crtc->base);
  14069. }
  14070. }
  14071. /* We need to sanitize the plane -> pipe mapping first because this will
  14072. * disable the crtc (and hence change the state) if it is wrong. Note
  14073. * that gen4+ has a fixed plane -> pipe mapping. */
  14074. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  14075. bool plane;
  14076. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  14077. crtc->base.base.id, crtc->base.name);
  14078. /* Pipe has the wrong plane attached and the plane is active.
  14079. * Temporarily change the plane mapping and disable everything
  14080. * ... */
  14081. plane = crtc->plane;
  14082. crtc->base.primary->state->visible = true;
  14083. crtc->plane = !plane;
  14084. intel_crtc_disable_noatomic(&crtc->base);
  14085. crtc->plane = plane;
  14086. }
  14087. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  14088. crtc->pipe == PIPE_A && !crtc->active) {
  14089. /* BIOS forgot to enable pipe A, this mostly happens after
  14090. * resume. Force-enable the pipe to fix this, the update_dpms
  14091. * call below we restore the pipe to the right state, but leave
  14092. * the required bits on. */
  14093. intel_enable_pipe_a(dev);
  14094. }
  14095. /* Adjust the state of the output pipe according to whether we
  14096. * have active connectors/encoders. */
  14097. if (crtc->active && !intel_crtc_has_encoders(crtc))
  14098. intel_crtc_disable_noatomic(&crtc->base);
  14099. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  14100. /*
  14101. * We start out with underrun reporting disabled to avoid races.
  14102. * For correct bookkeeping mark this on active crtcs.
  14103. *
  14104. * Also on gmch platforms we dont have any hardware bits to
  14105. * disable the underrun reporting. Which means we need to start
  14106. * out with underrun reporting disabled also on inactive pipes,
  14107. * since otherwise we'll complain about the garbage we read when
  14108. * e.g. coming up after runtime pm.
  14109. *
  14110. * No protection against concurrent access is required - at
  14111. * worst a fifo underrun happens which also sets this to false.
  14112. */
  14113. crtc->cpu_fifo_underrun_disabled = true;
  14114. /*
  14115. * We track the PCH trancoder underrun reporting state
  14116. * within the crtc. With crtc for pipe A housing the underrun
  14117. * reporting state for PCH transcoder A, crtc for pipe B housing
  14118. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  14119. * and marking underrun reporting as disabled for the non-existing
  14120. * PCH transcoders B and C would prevent enabling the south
  14121. * error interrupt (see cpt_can_enable_serr_int()).
  14122. */
  14123. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  14124. crtc->pch_fifo_underrun_disabled = true;
  14125. }
  14126. }
  14127. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  14128. {
  14129. struct intel_connector *connector;
  14130. /* We need to check both for a crtc link (meaning that the
  14131. * encoder is active and trying to read from a pipe) and the
  14132. * pipe itself being active. */
  14133. bool has_active_crtc = encoder->base.crtc &&
  14134. to_intel_crtc(encoder->base.crtc)->active;
  14135. connector = intel_encoder_find_connector(encoder);
  14136. if (connector && !has_active_crtc) {
  14137. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  14138. encoder->base.base.id,
  14139. encoder->base.name);
  14140. /* Connector is active, but has no active pipe. This is
  14141. * fallout from our resume register restoring. Disable
  14142. * the encoder manually again. */
  14143. if (encoder->base.crtc) {
  14144. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  14145. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  14146. encoder->base.base.id,
  14147. encoder->base.name);
  14148. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14149. if (encoder->post_disable)
  14150. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14151. }
  14152. encoder->base.crtc = NULL;
  14153. /* Inconsistent output/port/pipe state happens presumably due to
  14154. * a bug in one of the get_hw_state functions. Or someplace else
  14155. * in our code, like the register restore mess on resume. Clamp
  14156. * things to off as a safer default. */
  14157. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14158. connector->base.encoder = NULL;
  14159. }
  14160. /* Enabled encoders without active connectors will be fixed in
  14161. * the crtc fixup. */
  14162. }
  14163. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  14164. {
  14165. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  14166. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  14167. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  14168. i915_disable_vga(dev_priv);
  14169. }
  14170. }
  14171. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  14172. {
  14173. /* This function can be called both from intel_modeset_setup_hw_state or
  14174. * at a very early point in our resume sequence, where the power well
  14175. * structures are not yet restored. Since this function is at a very
  14176. * paranoid "someone might have enabled VGA while we were not looking"
  14177. * level, just check if the power well is enabled instead of trying to
  14178. * follow the "don't touch the power well if we don't need it" policy
  14179. * the rest of the driver uses. */
  14180. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  14181. return;
  14182. i915_redisable_vga_power_on(dev_priv);
  14183. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  14184. }
  14185. static bool primary_get_hw_state(struct intel_plane *plane)
  14186. {
  14187. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  14188. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  14189. }
  14190. /* FIXME read out full plane state for all planes */
  14191. static void readout_plane_state(struct intel_crtc *crtc)
  14192. {
  14193. struct drm_plane *primary = crtc->base.primary;
  14194. struct intel_plane_state *plane_state =
  14195. to_intel_plane_state(primary->state);
  14196. plane_state->base.visible = crtc->active &&
  14197. primary_get_hw_state(to_intel_plane(primary));
  14198. if (plane_state->base.visible)
  14199. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  14200. }
  14201. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  14202. {
  14203. struct drm_i915_private *dev_priv = to_i915(dev);
  14204. enum pipe pipe;
  14205. struct intel_crtc *crtc;
  14206. struct intel_encoder *encoder;
  14207. struct intel_connector *connector;
  14208. int i;
  14209. dev_priv->active_crtcs = 0;
  14210. for_each_intel_crtc(dev, crtc) {
  14211. struct intel_crtc_state *crtc_state = crtc->config;
  14212. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  14213. memset(crtc_state, 0, sizeof(*crtc_state));
  14214. crtc_state->base.crtc = &crtc->base;
  14215. crtc_state->base.active = crtc_state->base.enable =
  14216. dev_priv->display.get_pipe_config(crtc, crtc_state);
  14217. crtc->base.enabled = crtc_state->base.enable;
  14218. crtc->active = crtc_state->base.active;
  14219. if (crtc_state->base.active)
  14220. dev_priv->active_crtcs |= 1 << crtc->pipe;
  14221. readout_plane_state(crtc);
  14222. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  14223. crtc->base.base.id, crtc->base.name,
  14224. enableddisabled(crtc->active));
  14225. }
  14226. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14227. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14228. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  14229. &pll->state.hw_state);
  14230. pll->state.crtc_mask = 0;
  14231. for_each_intel_crtc(dev, crtc) {
  14232. if (crtc->active && crtc->config->shared_dpll == pll)
  14233. pll->state.crtc_mask |= 1 << crtc->pipe;
  14234. }
  14235. pll->active_mask = pll->state.crtc_mask;
  14236. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  14237. pll->name, pll->state.crtc_mask, pll->on);
  14238. }
  14239. for_each_intel_encoder(dev, encoder) {
  14240. pipe = 0;
  14241. if (encoder->get_hw_state(encoder, &pipe)) {
  14242. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14243. encoder->base.crtc = &crtc->base;
  14244. crtc->config->output_types |= 1 << encoder->type;
  14245. encoder->get_config(encoder, crtc->config);
  14246. } else {
  14247. encoder->base.crtc = NULL;
  14248. }
  14249. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  14250. encoder->base.base.id, encoder->base.name,
  14251. enableddisabled(encoder->base.crtc),
  14252. pipe_name(pipe));
  14253. }
  14254. for_each_intel_connector(dev, connector) {
  14255. if (connector->get_hw_state(connector)) {
  14256. connector->base.dpms = DRM_MODE_DPMS_ON;
  14257. encoder = connector->encoder;
  14258. connector->base.encoder = &encoder->base;
  14259. if (encoder->base.crtc &&
  14260. encoder->base.crtc->state->active) {
  14261. /*
  14262. * This has to be done during hardware readout
  14263. * because anything calling .crtc_disable may
  14264. * rely on the connector_mask being accurate.
  14265. */
  14266. encoder->base.crtc->state->connector_mask |=
  14267. 1 << drm_connector_index(&connector->base);
  14268. encoder->base.crtc->state->encoder_mask |=
  14269. 1 << drm_encoder_index(&encoder->base);
  14270. }
  14271. } else {
  14272. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14273. connector->base.encoder = NULL;
  14274. }
  14275. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  14276. connector->base.base.id, connector->base.name,
  14277. enableddisabled(connector->base.encoder));
  14278. }
  14279. for_each_intel_crtc(dev, crtc) {
  14280. int pixclk = 0;
  14281. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  14282. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  14283. if (crtc->base.state->active) {
  14284. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  14285. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  14286. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  14287. /*
  14288. * The initial mode needs to be set in order to keep
  14289. * the atomic core happy. It wants a valid mode if the
  14290. * crtc's enabled, so we do the above call.
  14291. *
  14292. * But we don't set all the derived state fully, hence
  14293. * set a flag to indicate that a full recalculation is
  14294. * needed on the next commit.
  14295. */
  14296. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  14297. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  14298. pixclk = ilk_pipe_pixel_rate(crtc->config);
  14299. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14300. pixclk = crtc->config->base.adjusted_mode.crtc_clock;
  14301. else
  14302. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  14303. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  14304. if (IS_BROADWELL(dev_priv) && crtc->config->ips_enabled)
  14305. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  14306. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  14307. update_scanline_offset(crtc);
  14308. }
  14309. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  14310. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  14311. }
  14312. }
  14313. /* Scan out the current hw modeset state,
  14314. * and sanitizes it to the current state
  14315. */
  14316. static void
  14317. intel_modeset_setup_hw_state(struct drm_device *dev)
  14318. {
  14319. struct drm_i915_private *dev_priv = to_i915(dev);
  14320. enum pipe pipe;
  14321. struct intel_crtc *crtc;
  14322. struct intel_encoder *encoder;
  14323. int i;
  14324. intel_modeset_readout_hw_state(dev);
  14325. /* HW state is read out, now we need to sanitize this mess. */
  14326. for_each_intel_encoder(dev, encoder) {
  14327. intel_sanitize_encoder(encoder);
  14328. }
  14329. for_each_pipe(dev_priv, pipe) {
  14330. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14331. intel_sanitize_crtc(crtc);
  14332. intel_dump_pipe_config(crtc, crtc->config,
  14333. "[setup_hw_state]");
  14334. }
  14335. intel_modeset_update_connector_atomic_state(dev);
  14336. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14337. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14338. if (!pll->on || pll->active_mask)
  14339. continue;
  14340. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  14341. pll->funcs.disable(dev_priv, pll);
  14342. pll->on = false;
  14343. }
  14344. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14345. vlv_wm_get_hw_state(dev);
  14346. else if (IS_GEN9(dev_priv))
  14347. skl_wm_get_hw_state(dev);
  14348. else if (HAS_PCH_SPLIT(dev_priv))
  14349. ilk_wm_get_hw_state(dev);
  14350. for_each_intel_crtc(dev, crtc) {
  14351. unsigned long put_domains;
  14352. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  14353. if (WARN_ON(put_domains))
  14354. modeset_put_power_domains(dev_priv, put_domains);
  14355. }
  14356. intel_display_set_init_power(dev_priv, false);
  14357. intel_fbc_init_pipe_state(dev_priv);
  14358. }
  14359. void intel_display_resume(struct drm_device *dev)
  14360. {
  14361. struct drm_i915_private *dev_priv = to_i915(dev);
  14362. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  14363. struct drm_modeset_acquire_ctx ctx;
  14364. int ret;
  14365. dev_priv->modeset_restore_state = NULL;
  14366. if (state)
  14367. state->acquire_ctx = &ctx;
  14368. /*
  14369. * This is a cludge because with real atomic modeset mode_config.mutex
  14370. * won't be taken. Unfortunately some probed state like
  14371. * audio_codec_enable is still protected by mode_config.mutex, so lock
  14372. * it here for now.
  14373. */
  14374. mutex_lock(&dev->mode_config.mutex);
  14375. drm_modeset_acquire_init(&ctx, 0);
  14376. while (1) {
  14377. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  14378. if (ret != -EDEADLK)
  14379. break;
  14380. drm_modeset_backoff(&ctx);
  14381. }
  14382. if (!ret)
  14383. ret = __intel_display_resume(dev, state);
  14384. drm_modeset_drop_locks(&ctx);
  14385. drm_modeset_acquire_fini(&ctx);
  14386. mutex_unlock(&dev->mode_config.mutex);
  14387. if (ret)
  14388. DRM_ERROR("Restoring old state failed with %i\n", ret);
  14389. if (state)
  14390. drm_atomic_state_put(state);
  14391. }
  14392. void intel_modeset_gem_init(struct drm_device *dev)
  14393. {
  14394. struct drm_i915_private *dev_priv = to_i915(dev);
  14395. struct drm_crtc *c;
  14396. struct drm_i915_gem_object *obj;
  14397. intel_init_gt_powersave(dev_priv);
  14398. intel_modeset_init_hw(dev);
  14399. intel_setup_overlay(dev_priv);
  14400. /*
  14401. * Make sure any fbs we allocated at startup are properly
  14402. * pinned & fenced. When we do the allocation it's too early
  14403. * for this.
  14404. */
  14405. for_each_crtc(dev, c) {
  14406. struct i915_vma *vma;
  14407. obj = intel_fb_obj(c->primary->fb);
  14408. if (obj == NULL)
  14409. continue;
  14410. mutex_lock(&dev->struct_mutex);
  14411. vma = intel_pin_and_fence_fb_obj(c->primary->fb,
  14412. c->primary->state->rotation);
  14413. mutex_unlock(&dev->struct_mutex);
  14414. if (IS_ERR(vma)) {
  14415. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  14416. to_intel_crtc(c)->pipe);
  14417. drm_framebuffer_unreference(c->primary->fb);
  14418. c->primary->fb = NULL;
  14419. c->primary->crtc = c->primary->state->crtc = NULL;
  14420. update_state_fb(c->primary);
  14421. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  14422. }
  14423. }
  14424. }
  14425. int intel_connector_register(struct drm_connector *connector)
  14426. {
  14427. struct intel_connector *intel_connector = to_intel_connector(connector);
  14428. int ret;
  14429. ret = intel_backlight_device_register(intel_connector);
  14430. if (ret)
  14431. goto err;
  14432. return 0;
  14433. err:
  14434. return ret;
  14435. }
  14436. void intel_connector_unregister(struct drm_connector *connector)
  14437. {
  14438. struct intel_connector *intel_connector = to_intel_connector(connector);
  14439. intel_backlight_device_unregister(intel_connector);
  14440. intel_panel_destroy_backlight(connector);
  14441. }
  14442. void intel_modeset_cleanup(struct drm_device *dev)
  14443. {
  14444. struct drm_i915_private *dev_priv = to_i915(dev);
  14445. intel_disable_gt_powersave(dev_priv);
  14446. /*
  14447. * Interrupts and polling as the first thing to avoid creating havoc.
  14448. * Too much stuff here (turning of connectors, ...) would
  14449. * experience fancy races otherwise.
  14450. */
  14451. intel_irq_uninstall(dev_priv);
  14452. /*
  14453. * Due to the hpd irq storm handling the hotplug work can re-arm the
  14454. * poll handlers. Hence disable polling after hpd handling is shut down.
  14455. */
  14456. drm_kms_helper_poll_fini(dev);
  14457. intel_unregister_dsm_handler();
  14458. intel_fbc_global_disable(dev_priv);
  14459. /* flush any delayed tasks or pending work */
  14460. flush_scheduled_work();
  14461. drm_mode_config_cleanup(dev);
  14462. intel_cleanup_overlay(dev_priv);
  14463. intel_cleanup_gt_powersave(dev_priv);
  14464. intel_teardown_gmbus(dev_priv);
  14465. }
  14466. void intel_connector_attach_encoder(struct intel_connector *connector,
  14467. struct intel_encoder *encoder)
  14468. {
  14469. connector->encoder = encoder;
  14470. drm_mode_connector_attach_encoder(&connector->base,
  14471. &encoder->base);
  14472. }
  14473. /*
  14474. * set vga decode state - true == enable VGA decode
  14475. */
  14476. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  14477. {
  14478. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  14479. u16 gmch_ctrl;
  14480. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  14481. DRM_ERROR("failed to read control word\n");
  14482. return -EIO;
  14483. }
  14484. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  14485. return 0;
  14486. if (state)
  14487. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  14488. else
  14489. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  14490. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  14491. DRM_ERROR("failed to write control word\n");
  14492. return -EIO;
  14493. }
  14494. return 0;
  14495. }
  14496. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  14497. struct intel_display_error_state {
  14498. u32 power_well_driver;
  14499. int num_transcoders;
  14500. struct intel_cursor_error_state {
  14501. u32 control;
  14502. u32 position;
  14503. u32 base;
  14504. u32 size;
  14505. } cursor[I915_MAX_PIPES];
  14506. struct intel_pipe_error_state {
  14507. bool power_domain_on;
  14508. u32 source;
  14509. u32 stat;
  14510. } pipe[I915_MAX_PIPES];
  14511. struct intel_plane_error_state {
  14512. u32 control;
  14513. u32 stride;
  14514. u32 size;
  14515. u32 pos;
  14516. u32 addr;
  14517. u32 surface;
  14518. u32 tile_offset;
  14519. } plane[I915_MAX_PIPES];
  14520. struct intel_transcoder_error_state {
  14521. bool power_domain_on;
  14522. enum transcoder cpu_transcoder;
  14523. u32 conf;
  14524. u32 htotal;
  14525. u32 hblank;
  14526. u32 hsync;
  14527. u32 vtotal;
  14528. u32 vblank;
  14529. u32 vsync;
  14530. } transcoder[4];
  14531. };
  14532. struct intel_display_error_state *
  14533. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  14534. {
  14535. struct intel_display_error_state *error;
  14536. int transcoders[] = {
  14537. TRANSCODER_A,
  14538. TRANSCODER_B,
  14539. TRANSCODER_C,
  14540. TRANSCODER_EDP,
  14541. };
  14542. int i;
  14543. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  14544. return NULL;
  14545. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  14546. if (error == NULL)
  14547. return NULL;
  14548. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14549. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  14550. for_each_pipe(dev_priv, i) {
  14551. error->pipe[i].power_domain_on =
  14552. __intel_display_power_is_enabled(dev_priv,
  14553. POWER_DOMAIN_PIPE(i));
  14554. if (!error->pipe[i].power_domain_on)
  14555. continue;
  14556. error->cursor[i].control = I915_READ(CURCNTR(i));
  14557. error->cursor[i].position = I915_READ(CURPOS(i));
  14558. error->cursor[i].base = I915_READ(CURBASE(i));
  14559. error->plane[i].control = I915_READ(DSPCNTR(i));
  14560. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  14561. if (INTEL_GEN(dev_priv) <= 3) {
  14562. error->plane[i].size = I915_READ(DSPSIZE(i));
  14563. error->plane[i].pos = I915_READ(DSPPOS(i));
  14564. }
  14565. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14566. error->plane[i].addr = I915_READ(DSPADDR(i));
  14567. if (INTEL_GEN(dev_priv) >= 4) {
  14568. error->plane[i].surface = I915_READ(DSPSURF(i));
  14569. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  14570. }
  14571. error->pipe[i].source = I915_READ(PIPESRC(i));
  14572. if (HAS_GMCH_DISPLAY(dev_priv))
  14573. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  14574. }
  14575. /* Note: this does not include DSI transcoders. */
  14576. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  14577. if (HAS_DDI(dev_priv))
  14578. error->num_transcoders++; /* Account for eDP. */
  14579. for (i = 0; i < error->num_transcoders; i++) {
  14580. enum transcoder cpu_transcoder = transcoders[i];
  14581. error->transcoder[i].power_domain_on =
  14582. __intel_display_power_is_enabled(dev_priv,
  14583. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  14584. if (!error->transcoder[i].power_domain_on)
  14585. continue;
  14586. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  14587. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  14588. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  14589. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  14590. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  14591. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  14592. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  14593. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  14594. }
  14595. return error;
  14596. }
  14597. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  14598. void
  14599. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  14600. struct drm_i915_private *dev_priv,
  14601. struct intel_display_error_state *error)
  14602. {
  14603. int i;
  14604. if (!error)
  14605. return;
  14606. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  14607. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14608. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  14609. error->power_well_driver);
  14610. for_each_pipe(dev_priv, i) {
  14611. err_printf(m, "Pipe [%d]:\n", i);
  14612. err_printf(m, " Power: %s\n",
  14613. onoff(error->pipe[i].power_domain_on));
  14614. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  14615. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  14616. err_printf(m, "Plane [%d]:\n", i);
  14617. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  14618. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  14619. if (INTEL_GEN(dev_priv) <= 3) {
  14620. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  14621. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  14622. }
  14623. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14624. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  14625. if (INTEL_GEN(dev_priv) >= 4) {
  14626. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  14627. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  14628. }
  14629. err_printf(m, "Cursor [%d]:\n", i);
  14630. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  14631. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  14632. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  14633. }
  14634. for (i = 0; i < error->num_transcoders; i++) {
  14635. err_printf(m, "CPU transcoder: %s\n",
  14636. transcoder_name(error->transcoder[i].cpu_transcoder));
  14637. err_printf(m, " Power: %s\n",
  14638. onoff(error->transcoder[i].power_domain_on));
  14639. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  14640. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  14641. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  14642. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  14643. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  14644. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  14645. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  14646. }
  14647. }
  14648. #endif