intel_display.c 456 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329
  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 <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "intel_dsi.h"
  39. #include "i915_trace.h"
  40. #include <drm/drm_atomic.h>
  41. #include <drm/drm_atomic_helper.h>
  42. #include <drm/drm_dp_helper.h>
  43. #include <drm/drm_crtc_helper.h>
  44. #include <drm/drm_plane_helper.h>
  45. #include <drm/drm_rect.h>
  46. #include <linux/dma_remapping.h>
  47. #include <linux/reservation.h>
  48. #include <linux/dma-buf.h>
  49. /* Primary plane formats for gen <= 3 */
  50. static const uint32_t i8xx_primary_formats[] = {
  51. DRM_FORMAT_C8,
  52. DRM_FORMAT_RGB565,
  53. DRM_FORMAT_XRGB1555,
  54. DRM_FORMAT_XRGB8888,
  55. };
  56. /* Primary plane formats for gen >= 4 */
  57. static const uint32_t i965_primary_formats[] = {
  58. DRM_FORMAT_C8,
  59. DRM_FORMAT_RGB565,
  60. DRM_FORMAT_XRGB8888,
  61. DRM_FORMAT_XBGR8888,
  62. DRM_FORMAT_XRGB2101010,
  63. DRM_FORMAT_XBGR2101010,
  64. };
  65. static const uint32_t skl_primary_formats[] = {
  66. DRM_FORMAT_C8,
  67. DRM_FORMAT_RGB565,
  68. DRM_FORMAT_XRGB8888,
  69. DRM_FORMAT_XBGR8888,
  70. DRM_FORMAT_ARGB8888,
  71. DRM_FORMAT_ABGR8888,
  72. DRM_FORMAT_XRGB2101010,
  73. DRM_FORMAT_XBGR2101010,
  74. DRM_FORMAT_YUYV,
  75. DRM_FORMAT_YVYU,
  76. DRM_FORMAT_UYVY,
  77. DRM_FORMAT_VYUY,
  78. };
  79. /* Cursor formats */
  80. static const uint32_t intel_cursor_formats[] = {
  81. DRM_FORMAT_ARGB8888,
  82. };
  83. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  84. struct intel_crtc_state *pipe_config);
  85. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  86. struct intel_crtc_state *pipe_config);
  87. static int intel_framebuffer_init(struct drm_device *dev,
  88. struct intel_framebuffer *ifb,
  89. struct drm_mode_fb_cmd2 *mode_cmd,
  90. struct drm_i915_gem_object *obj);
  91. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  92. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  93. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  94. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  95. struct intel_link_m_n *m_n,
  96. struct intel_link_m_n *m2_n2);
  97. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  98. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  99. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  100. static void vlv_prepare_pll(struct intel_crtc *crtc,
  101. const struct intel_crtc_state *pipe_config);
  102. static void chv_prepare_pll(struct intel_crtc *crtc,
  103. const struct intel_crtc_state *pipe_config);
  104. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  105. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  106. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  107. struct intel_crtc_state *crtc_state);
  108. static void skylake_pfit_enable(struct intel_crtc *crtc);
  109. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  110. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  111. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  112. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  113. typedef struct {
  114. int min, max;
  115. } intel_range_t;
  116. typedef struct {
  117. int dot_limit;
  118. int p2_slow, p2_fast;
  119. } intel_p2_t;
  120. typedef struct intel_limit intel_limit_t;
  121. struct intel_limit {
  122. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  123. intel_p2_t p2;
  124. };
  125. /* returns HPLL frequency in kHz */
  126. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  127. {
  128. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  129. /* Obtain SKU information */
  130. mutex_lock(&dev_priv->sb_lock);
  131. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  132. CCK_FUSE_HPLL_FREQ_MASK;
  133. mutex_unlock(&dev_priv->sb_lock);
  134. return vco_freq[hpll_freq] * 1000;
  135. }
  136. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  137. const char *name, u32 reg, int ref_freq)
  138. {
  139. u32 val;
  140. int divider;
  141. mutex_lock(&dev_priv->sb_lock);
  142. val = vlv_cck_read(dev_priv, reg);
  143. mutex_unlock(&dev_priv->sb_lock);
  144. divider = val & CCK_FREQUENCY_VALUES;
  145. WARN((val & CCK_FREQUENCY_STATUS) !=
  146. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  147. "%s change in progress\n", name);
  148. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  149. }
  150. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  151. const char *name, u32 reg)
  152. {
  153. if (dev_priv->hpll_freq == 0)
  154. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  155. return vlv_get_cck_clock(dev_priv, name, reg,
  156. dev_priv->hpll_freq);
  157. }
  158. static int
  159. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  160. {
  161. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  162. }
  163. static int
  164. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  165. {
  166. /* RAWCLK_FREQ_VLV register updated from power well code */
  167. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  168. CCK_DISPLAY_REF_CLOCK_CONTROL);
  169. }
  170. static int
  171. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  172. {
  173. uint32_t clkcfg;
  174. /* hrawclock is 1/4 the FSB frequency */
  175. clkcfg = I915_READ(CLKCFG);
  176. switch (clkcfg & CLKCFG_FSB_MASK) {
  177. case CLKCFG_FSB_400:
  178. return 100000;
  179. case CLKCFG_FSB_533:
  180. return 133333;
  181. case CLKCFG_FSB_667:
  182. return 166667;
  183. case CLKCFG_FSB_800:
  184. return 200000;
  185. case CLKCFG_FSB_1067:
  186. return 266667;
  187. case CLKCFG_FSB_1333:
  188. return 333333;
  189. /* these two are just a guess; one of them might be right */
  190. case CLKCFG_FSB_1600:
  191. case CLKCFG_FSB_1600_ALT:
  192. return 400000;
  193. default:
  194. return 133333;
  195. }
  196. }
  197. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  198. {
  199. if (HAS_PCH_SPLIT(dev_priv))
  200. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  201. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  202. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  203. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  204. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  205. else
  206. return; /* no rawclk on other platforms, or no need to know it */
  207. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  208. }
  209. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  210. {
  211. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  212. return;
  213. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  214. CCK_CZ_CLOCK_CONTROL);
  215. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  216. }
  217. static inline u32 /* units of 100MHz */
  218. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  219. const struct intel_crtc_state *pipe_config)
  220. {
  221. if (HAS_DDI(dev_priv))
  222. return pipe_config->port_clock; /* SPLL */
  223. else if (IS_GEN5(dev_priv))
  224. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  225. else
  226. return 270000;
  227. }
  228. static const intel_limit_t intel_limits_i8xx_dac = {
  229. .dot = { .min = 25000, .max = 350000 },
  230. .vco = { .min = 908000, .max = 1512000 },
  231. .n = { .min = 2, .max = 16 },
  232. .m = { .min = 96, .max = 140 },
  233. .m1 = { .min = 18, .max = 26 },
  234. .m2 = { .min = 6, .max = 16 },
  235. .p = { .min = 4, .max = 128 },
  236. .p1 = { .min = 2, .max = 33 },
  237. .p2 = { .dot_limit = 165000,
  238. .p2_slow = 4, .p2_fast = 2 },
  239. };
  240. static const intel_limit_t intel_limits_i8xx_dvo = {
  241. .dot = { .min = 25000, .max = 350000 },
  242. .vco = { .min = 908000, .max = 1512000 },
  243. .n = { .min = 2, .max = 16 },
  244. .m = { .min = 96, .max = 140 },
  245. .m1 = { .min = 18, .max = 26 },
  246. .m2 = { .min = 6, .max = 16 },
  247. .p = { .min = 4, .max = 128 },
  248. .p1 = { .min = 2, .max = 33 },
  249. .p2 = { .dot_limit = 165000,
  250. .p2_slow = 4, .p2_fast = 4 },
  251. };
  252. static const intel_limit_t intel_limits_i8xx_lvds = {
  253. .dot = { .min = 25000, .max = 350000 },
  254. .vco = { .min = 908000, .max = 1512000 },
  255. .n = { .min = 2, .max = 16 },
  256. .m = { .min = 96, .max = 140 },
  257. .m1 = { .min = 18, .max = 26 },
  258. .m2 = { .min = 6, .max = 16 },
  259. .p = { .min = 4, .max = 128 },
  260. .p1 = { .min = 1, .max = 6 },
  261. .p2 = { .dot_limit = 165000,
  262. .p2_slow = 14, .p2_fast = 7 },
  263. };
  264. static const intel_limit_t intel_limits_i9xx_sdvo = {
  265. .dot = { .min = 20000, .max = 400000 },
  266. .vco = { .min = 1400000, .max = 2800000 },
  267. .n = { .min = 1, .max = 6 },
  268. .m = { .min = 70, .max = 120 },
  269. .m1 = { .min = 8, .max = 18 },
  270. .m2 = { .min = 3, .max = 7 },
  271. .p = { .min = 5, .max = 80 },
  272. .p1 = { .min = 1, .max = 8 },
  273. .p2 = { .dot_limit = 200000,
  274. .p2_slow = 10, .p2_fast = 5 },
  275. };
  276. static const intel_limit_t intel_limits_i9xx_lvds = {
  277. .dot = { .min = 20000, .max = 400000 },
  278. .vco = { .min = 1400000, .max = 2800000 },
  279. .n = { .min = 1, .max = 6 },
  280. .m = { .min = 70, .max = 120 },
  281. .m1 = { .min = 8, .max = 18 },
  282. .m2 = { .min = 3, .max = 7 },
  283. .p = { .min = 7, .max = 98 },
  284. .p1 = { .min = 1, .max = 8 },
  285. .p2 = { .dot_limit = 112000,
  286. .p2_slow = 14, .p2_fast = 7 },
  287. };
  288. static const intel_limit_t intel_limits_g4x_sdvo = {
  289. .dot = { .min = 25000, .max = 270000 },
  290. .vco = { .min = 1750000, .max = 3500000},
  291. .n = { .min = 1, .max = 4 },
  292. .m = { .min = 104, .max = 138 },
  293. .m1 = { .min = 17, .max = 23 },
  294. .m2 = { .min = 5, .max = 11 },
  295. .p = { .min = 10, .max = 30 },
  296. .p1 = { .min = 1, .max = 3},
  297. .p2 = { .dot_limit = 270000,
  298. .p2_slow = 10,
  299. .p2_fast = 10
  300. },
  301. };
  302. static const intel_limit_t intel_limits_g4x_hdmi = {
  303. .dot = { .min = 22000, .max = 400000 },
  304. .vco = { .min = 1750000, .max = 3500000},
  305. .n = { .min = 1, .max = 4 },
  306. .m = { .min = 104, .max = 138 },
  307. .m1 = { .min = 16, .max = 23 },
  308. .m2 = { .min = 5, .max = 11 },
  309. .p = { .min = 5, .max = 80 },
  310. .p1 = { .min = 1, .max = 8},
  311. .p2 = { .dot_limit = 165000,
  312. .p2_slow = 10, .p2_fast = 5 },
  313. };
  314. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  315. .dot = { .min = 20000, .max = 115000 },
  316. .vco = { .min = 1750000, .max = 3500000 },
  317. .n = { .min = 1, .max = 3 },
  318. .m = { .min = 104, .max = 138 },
  319. .m1 = { .min = 17, .max = 23 },
  320. .m2 = { .min = 5, .max = 11 },
  321. .p = { .min = 28, .max = 112 },
  322. .p1 = { .min = 2, .max = 8 },
  323. .p2 = { .dot_limit = 0,
  324. .p2_slow = 14, .p2_fast = 14
  325. },
  326. };
  327. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  328. .dot = { .min = 80000, .max = 224000 },
  329. .vco = { .min = 1750000, .max = 3500000 },
  330. .n = { .min = 1, .max = 3 },
  331. .m = { .min = 104, .max = 138 },
  332. .m1 = { .min = 17, .max = 23 },
  333. .m2 = { .min = 5, .max = 11 },
  334. .p = { .min = 14, .max = 42 },
  335. .p1 = { .min = 2, .max = 6 },
  336. .p2 = { .dot_limit = 0,
  337. .p2_slow = 7, .p2_fast = 7
  338. },
  339. };
  340. static const intel_limit_t intel_limits_pineview_sdvo = {
  341. .dot = { .min = 20000, .max = 400000},
  342. .vco = { .min = 1700000, .max = 3500000 },
  343. /* Pineview's Ncounter is a ring counter */
  344. .n = { .min = 3, .max = 6 },
  345. .m = { .min = 2, .max = 256 },
  346. /* Pineview only has one combined m divider, which we treat as m2. */
  347. .m1 = { .min = 0, .max = 0 },
  348. .m2 = { .min = 0, .max = 254 },
  349. .p = { .min = 5, .max = 80 },
  350. .p1 = { .min = 1, .max = 8 },
  351. .p2 = { .dot_limit = 200000,
  352. .p2_slow = 10, .p2_fast = 5 },
  353. };
  354. static const intel_limit_t intel_limits_pineview_lvds = {
  355. .dot = { .min = 20000, .max = 400000 },
  356. .vco = { .min = 1700000, .max = 3500000 },
  357. .n = { .min = 3, .max = 6 },
  358. .m = { .min = 2, .max = 256 },
  359. .m1 = { .min = 0, .max = 0 },
  360. .m2 = { .min = 0, .max = 254 },
  361. .p = { .min = 7, .max = 112 },
  362. .p1 = { .min = 1, .max = 8 },
  363. .p2 = { .dot_limit = 112000,
  364. .p2_slow = 14, .p2_fast = 14 },
  365. };
  366. /* Ironlake / Sandybridge
  367. *
  368. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  369. * the range value for them is (actual_value - 2).
  370. */
  371. static const intel_limit_t intel_limits_ironlake_dac = {
  372. .dot = { .min = 25000, .max = 350000 },
  373. .vco = { .min = 1760000, .max = 3510000 },
  374. .n = { .min = 1, .max = 5 },
  375. .m = { .min = 79, .max = 127 },
  376. .m1 = { .min = 12, .max = 22 },
  377. .m2 = { .min = 5, .max = 9 },
  378. .p = { .min = 5, .max = 80 },
  379. .p1 = { .min = 1, .max = 8 },
  380. .p2 = { .dot_limit = 225000,
  381. .p2_slow = 10, .p2_fast = 5 },
  382. };
  383. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  384. .dot = { .min = 25000, .max = 350000 },
  385. .vco = { .min = 1760000, .max = 3510000 },
  386. .n = { .min = 1, .max = 3 },
  387. .m = { .min = 79, .max = 118 },
  388. .m1 = { .min = 12, .max = 22 },
  389. .m2 = { .min = 5, .max = 9 },
  390. .p = { .min = 28, .max = 112 },
  391. .p1 = { .min = 2, .max = 8 },
  392. .p2 = { .dot_limit = 225000,
  393. .p2_slow = 14, .p2_fast = 14 },
  394. };
  395. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  396. .dot = { .min = 25000, .max = 350000 },
  397. .vco = { .min = 1760000, .max = 3510000 },
  398. .n = { .min = 1, .max = 3 },
  399. .m = { .min = 79, .max = 127 },
  400. .m1 = { .min = 12, .max = 22 },
  401. .m2 = { .min = 5, .max = 9 },
  402. .p = { .min = 14, .max = 56 },
  403. .p1 = { .min = 2, .max = 8 },
  404. .p2 = { .dot_limit = 225000,
  405. .p2_slow = 7, .p2_fast = 7 },
  406. };
  407. /* LVDS 100mhz refclk limits. */
  408. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  409. .dot = { .min = 25000, .max = 350000 },
  410. .vco = { .min = 1760000, .max = 3510000 },
  411. .n = { .min = 1, .max = 2 },
  412. .m = { .min = 79, .max = 126 },
  413. .m1 = { .min = 12, .max = 22 },
  414. .m2 = { .min = 5, .max = 9 },
  415. .p = { .min = 28, .max = 112 },
  416. .p1 = { .min = 2, .max = 8 },
  417. .p2 = { .dot_limit = 225000,
  418. .p2_slow = 14, .p2_fast = 14 },
  419. };
  420. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  421. .dot = { .min = 25000, .max = 350000 },
  422. .vco = { .min = 1760000, .max = 3510000 },
  423. .n = { .min = 1, .max = 3 },
  424. .m = { .min = 79, .max = 126 },
  425. .m1 = { .min = 12, .max = 22 },
  426. .m2 = { .min = 5, .max = 9 },
  427. .p = { .min = 14, .max = 42 },
  428. .p1 = { .min = 2, .max = 6 },
  429. .p2 = { .dot_limit = 225000,
  430. .p2_slow = 7, .p2_fast = 7 },
  431. };
  432. static const intel_limit_t intel_limits_vlv = {
  433. /*
  434. * These are the data rate limits (measured in fast clocks)
  435. * since those are the strictest limits we have. The fast
  436. * clock and actual rate limits are more relaxed, so checking
  437. * them would make no difference.
  438. */
  439. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  440. .vco = { .min = 4000000, .max = 6000000 },
  441. .n = { .min = 1, .max = 7 },
  442. .m1 = { .min = 2, .max = 3 },
  443. .m2 = { .min = 11, .max = 156 },
  444. .p1 = { .min = 2, .max = 3 },
  445. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  446. };
  447. static const intel_limit_t intel_limits_chv = {
  448. /*
  449. * These are the data rate limits (measured in fast clocks)
  450. * since those are the strictest limits we have. The fast
  451. * clock and actual rate limits are more relaxed, so checking
  452. * them would make no difference.
  453. */
  454. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  455. .vco = { .min = 4800000, .max = 6480000 },
  456. .n = { .min = 1, .max = 1 },
  457. .m1 = { .min = 2, .max = 2 },
  458. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  459. .p1 = { .min = 2, .max = 4 },
  460. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  461. };
  462. static const intel_limit_t intel_limits_bxt = {
  463. /* FIXME: find real dot limits */
  464. .dot = { .min = 0, .max = INT_MAX },
  465. .vco = { .min = 4800000, .max = 6700000 },
  466. .n = { .min = 1, .max = 1 },
  467. .m1 = { .min = 2, .max = 2 },
  468. /* FIXME: find real m2 limits */
  469. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  470. .p1 = { .min = 2, .max = 4 },
  471. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  472. };
  473. static bool
  474. needs_modeset(struct drm_crtc_state *state)
  475. {
  476. return drm_atomic_crtc_needs_modeset(state);
  477. }
  478. /**
  479. * Returns whether any output on the specified pipe is of the specified type
  480. */
  481. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  482. {
  483. struct drm_device *dev = crtc->base.dev;
  484. struct intel_encoder *encoder;
  485. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  486. if (encoder->type == type)
  487. return true;
  488. return false;
  489. }
  490. /**
  491. * Returns whether any output on the specified pipe will have the specified
  492. * type after a staged modeset is complete, i.e., the same as
  493. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  494. * encoder->crtc.
  495. */
  496. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  497. int type)
  498. {
  499. struct drm_atomic_state *state = crtc_state->base.state;
  500. struct drm_connector *connector;
  501. struct drm_connector_state *connector_state;
  502. struct intel_encoder *encoder;
  503. int i, num_connectors = 0;
  504. for_each_connector_in_state(state, connector, connector_state, i) {
  505. if (connector_state->crtc != crtc_state->base.crtc)
  506. continue;
  507. num_connectors++;
  508. encoder = to_intel_encoder(connector_state->best_encoder);
  509. if (encoder->type == type)
  510. return true;
  511. }
  512. WARN_ON(num_connectors == 0);
  513. return false;
  514. }
  515. /*
  516. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  517. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  518. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  519. * The helpers' return value is the rate of the clock that is fed to the
  520. * display engine's pipe which can be the above fast dot clock rate or a
  521. * divided-down version of it.
  522. */
  523. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  524. static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
  525. {
  526. clock->m = clock->m2 + 2;
  527. clock->p = clock->p1 * clock->p2;
  528. if (WARN_ON(clock->n == 0 || clock->p == 0))
  529. return 0;
  530. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  531. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  532. return clock->dot;
  533. }
  534. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  535. {
  536. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  537. }
  538. static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
  539. {
  540. clock->m = i9xx_dpll_compute_m(clock);
  541. clock->p = clock->p1 * clock->p2;
  542. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  543. return 0;
  544. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  545. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  546. return clock->dot;
  547. }
  548. static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
  549. {
  550. clock->m = clock->m1 * clock->m2;
  551. clock->p = clock->p1 * clock->p2;
  552. if (WARN_ON(clock->n == 0 || clock->p == 0))
  553. return 0;
  554. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  555. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  556. return clock->dot / 5;
  557. }
  558. int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
  559. {
  560. clock->m = clock->m1 * clock->m2;
  561. clock->p = clock->p1 * clock->p2;
  562. if (WARN_ON(clock->n == 0 || clock->p == 0))
  563. return 0;
  564. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  565. clock->n << 22);
  566. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  567. return clock->dot / 5;
  568. }
  569. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  570. /**
  571. * Returns whether the given set of divisors are valid for a given refclk with
  572. * the given connectors.
  573. */
  574. static bool intel_PLL_is_valid(struct drm_device *dev,
  575. const intel_limit_t *limit,
  576. const intel_clock_t *clock)
  577. {
  578. if (clock->n < limit->n.min || limit->n.max < clock->n)
  579. INTELPllInvalid("n out of range\n");
  580. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  581. INTELPllInvalid("p1 out of range\n");
  582. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  583. INTELPllInvalid("m2 out of range\n");
  584. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  585. INTELPllInvalid("m1 out of range\n");
  586. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
  587. !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
  588. if (clock->m1 <= clock->m2)
  589. INTELPllInvalid("m1 <= m2\n");
  590. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
  591. if (clock->p < limit->p.min || limit->p.max < clock->p)
  592. INTELPllInvalid("p out of range\n");
  593. if (clock->m < limit->m.min || limit->m.max < clock->m)
  594. INTELPllInvalid("m out of range\n");
  595. }
  596. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  597. INTELPllInvalid("vco out of range\n");
  598. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  599. * connector, etc., rather than just a single range.
  600. */
  601. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  602. INTELPllInvalid("dot out of range\n");
  603. return true;
  604. }
  605. static int
  606. i9xx_select_p2_div(const intel_limit_t *limit,
  607. const struct intel_crtc_state *crtc_state,
  608. int target)
  609. {
  610. struct drm_device *dev = crtc_state->base.crtc->dev;
  611. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  612. /*
  613. * For LVDS just rely on its current settings for dual-channel.
  614. * We haven't figured out how to reliably set up different
  615. * single/dual channel state, if we even can.
  616. */
  617. if (intel_is_dual_link_lvds(dev))
  618. return limit->p2.p2_fast;
  619. else
  620. return limit->p2.p2_slow;
  621. } else {
  622. if (target < limit->p2.dot_limit)
  623. return limit->p2.p2_slow;
  624. else
  625. return limit->p2.p2_fast;
  626. }
  627. }
  628. /*
  629. * Returns a set of divisors for the desired target clock with the given
  630. * refclk, or FALSE. The returned values represent the clock equation:
  631. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  632. *
  633. * Target and reference clocks are specified in kHz.
  634. *
  635. * If match_clock is provided, then best_clock P divider must match the P
  636. * divider from @match_clock used for LVDS downclocking.
  637. */
  638. static bool
  639. i9xx_find_best_dpll(const intel_limit_t *limit,
  640. struct intel_crtc_state *crtc_state,
  641. int target, int refclk, intel_clock_t *match_clock,
  642. intel_clock_t *best_clock)
  643. {
  644. struct drm_device *dev = crtc_state->base.crtc->dev;
  645. intel_clock_t clock;
  646. int err = target;
  647. memset(best_clock, 0, sizeof(*best_clock));
  648. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  649. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  650. clock.m1++) {
  651. for (clock.m2 = limit->m2.min;
  652. clock.m2 <= limit->m2.max; clock.m2++) {
  653. if (clock.m2 >= clock.m1)
  654. break;
  655. for (clock.n = limit->n.min;
  656. clock.n <= limit->n.max; clock.n++) {
  657. for (clock.p1 = limit->p1.min;
  658. clock.p1 <= limit->p1.max; clock.p1++) {
  659. int this_err;
  660. i9xx_calc_dpll_params(refclk, &clock);
  661. if (!intel_PLL_is_valid(dev, limit,
  662. &clock))
  663. continue;
  664. if (match_clock &&
  665. clock.p != match_clock->p)
  666. continue;
  667. this_err = abs(clock.dot - target);
  668. if (this_err < err) {
  669. *best_clock = clock;
  670. err = this_err;
  671. }
  672. }
  673. }
  674. }
  675. }
  676. return (err != target);
  677. }
  678. /*
  679. * Returns a set of divisors for the desired target clock with the given
  680. * refclk, or FALSE. The returned values represent the clock equation:
  681. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  682. *
  683. * Target and reference clocks are specified in kHz.
  684. *
  685. * If match_clock is provided, then best_clock P divider must match the P
  686. * divider from @match_clock used for LVDS downclocking.
  687. */
  688. static bool
  689. pnv_find_best_dpll(const intel_limit_t *limit,
  690. struct intel_crtc_state *crtc_state,
  691. int target, int refclk, intel_clock_t *match_clock,
  692. intel_clock_t *best_clock)
  693. {
  694. struct drm_device *dev = crtc_state->base.crtc->dev;
  695. intel_clock_t clock;
  696. int err = target;
  697. memset(best_clock, 0, sizeof(*best_clock));
  698. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  699. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  700. clock.m1++) {
  701. for (clock.m2 = limit->m2.min;
  702. clock.m2 <= limit->m2.max; clock.m2++) {
  703. for (clock.n = limit->n.min;
  704. clock.n <= limit->n.max; clock.n++) {
  705. for (clock.p1 = limit->p1.min;
  706. clock.p1 <= limit->p1.max; clock.p1++) {
  707. int this_err;
  708. pnv_calc_dpll_params(refclk, &clock);
  709. if (!intel_PLL_is_valid(dev, limit,
  710. &clock))
  711. continue;
  712. if (match_clock &&
  713. clock.p != match_clock->p)
  714. continue;
  715. this_err = abs(clock.dot - target);
  716. if (this_err < err) {
  717. *best_clock = clock;
  718. err = this_err;
  719. }
  720. }
  721. }
  722. }
  723. }
  724. return (err != target);
  725. }
  726. /*
  727. * Returns a set of divisors for the desired target clock with the given
  728. * refclk, or FALSE. The returned values represent the clock equation:
  729. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  730. *
  731. * Target and reference clocks are specified in kHz.
  732. *
  733. * If match_clock is provided, then best_clock P divider must match the P
  734. * divider from @match_clock used for LVDS downclocking.
  735. */
  736. static bool
  737. g4x_find_best_dpll(const intel_limit_t *limit,
  738. struct intel_crtc_state *crtc_state,
  739. int target, int refclk, intel_clock_t *match_clock,
  740. intel_clock_t *best_clock)
  741. {
  742. struct drm_device *dev = crtc_state->base.crtc->dev;
  743. intel_clock_t clock;
  744. int max_n;
  745. bool found = false;
  746. /* approximately equals target * 0.00585 */
  747. int err_most = (target >> 8) + (target >> 9);
  748. memset(best_clock, 0, sizeof(*best_clock));
  749. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  750. max_n = limit->n.max;
  751. /* based on hardware requirement, prefer smaller n to precision */
  752. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  753. /* based on hardware requirement, prefere larger m1,m2 */
  754. for (clock.m1 = limit->m1.max;
  755. clock.m1 >= limit->m1.min; clock.m1--) {
  756. for (clock.m2 = limit->m2.max;
  757. clock.m2 >= limit->m2.min; clock.m2--) {
  758. for (clock.p1 = limit->p1.max;
  759. clock.p1 >= limit->p1.min; clock.p1--) {
  760. int this_err;
  761. i9xx_calc_dpll_params(refclk, &clock);
  762. if (!intel_PLL_is_valid(dev, limit,
  763. &clock))
  764. continue;
  765. this_err = abs(clock.dot - target);
  766. if (this_err < err_most) {
  767. *best_clock = clock;
  768. err_most = this_err;
  769. max_n = clock.n;
  770. found = true;
  771. }
  772. }
  773. }
  774. }
  775. }
  776. return found;
  777. }
  778. /*
  779. * Check if the calculated PLL configuration is more optimal compared to the
  780. * best configuration and error found so far. Return the calculated error.
  781. */
  782. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  783. const intel_clock_t *calculated_clock,
  784. const intel_clock_t *best_clock,
  785. unsigned int best_error_ppm,
  786. unsigned int *error_ppm)
  787. {
  788. /*
  789. * For CHV ignore the error and consider only the P value.
  790. * Prefer a bigger P value based on HW requirements.
  791. */
  792. if (IS_CHERRYVIEW(dev)) {
  793. *error_ppm = 0;
  794. return calculated_clock->p > best_clock->p;
  795. }
  796. if (WARN_ON_ONCE(!target_freq))
  797. return false;
  798. *error_ppm = div_u64(1000000ULL *
  799. abs(target_freq - calculated_clock->dot),
  800. target_freq);
  801. /*
  802. * Prefer a better P value over a better (smaller) error if the error
  803. * is small. Ensure this preference for future configurations too by
  804. * setting the error to 0.
  805. */
  806. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  807. *error_ppm = 0;
  808. return true;
  809. }
  810. return *error_ppm + 10 < best_error_ppm;
  811. }
  812. /*
  813. * Returns a set of divisors for the desired target clock with the given
  814. * refclk, or FALSE. The returned values represent the clock equation:
  815. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  816. */
  817. static bool
  818. vlv_find_best_dpll(const intel_limit_t *limit,
  819. struct intel_crtc_state *crtc_state,
  820. int target, int refclk, intel_clock_t *match_clock,
  821. intel_clock_t *best_clock)
  822. {
  823. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  824. struct drm_device *dev = crtc->base.dev;
  825. intel_clock_t clock;
  826. unsigned int bestppm = 1000000;
  827. /* min update 19.2 MHz */
  828. int max_n = min(limit->n.max, refclk / 19200);
  829. bool found = false;
  830. target *= 5; /* fast clock */
  831. memset(best_clock, 0, sizeof(*best_clock));
  832. /* based on hardware requirement, prefer smaller n to precision */
  833. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  834. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  835. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  836. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  837. clock.p = clock.p1 * clock.p2;
  838. /* based on hardware requirement, prefer bigger m1,m2 values */
  839. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  840. unsigned int ppm;
  841. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  842. refclk * clock.m1);
  843. vlv_calc_dpll_params(refclk, &clock);
  844. if (!intel_PLL_is_valid(dev, limit,
  845. &clock))
  846. continue;
  847. if (!vlv_PLL_is_optimal(dev, target,
  848. &clock,
  849. best_clock,
  850. bestppm, &ppm))
  851. continue;
  852. *best_clock = clock;
  853. bestppm = ppm;
  854. found = true;
  855. }
  856. }
  857. }
  858. }
  859. return found;
  860. }
  861. /*
  862. * Returns a set of divisors for the desired target clock with the given
  863. * refclk, or FALSE. The returned values represent the clock equation:
  864. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  865. */
  866. static bool
  867. chv_find_best_dpll(const intel_limit_t *limit,
  868. struct intel_crtc_state *crtc_state,
  869. int target, int refclk, intel_clock_t *match_clock,
  870. intel_clock_t *best_clock)
  871. {
  872. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  873. struct drm_device *dev = crtc->base.dev;
  874. unsigned int best_error_ppm;
  875. intel_clock_t clock;
  876. uint64_t m2;
  877. int found = false;
  878. memset(best_clock, 0, sizeof(*best_clock));
  879. best_error_ppm = 1000000;
  880. /*
  881. * Based on hardware doc, the n always set to 1, and m1 always
  882. * set to 2. If requires to support 200Mhz refclk, we need to
  883. * revisit this because n may not 1 anymore.
  884. */
  885. clock.n = 1, clock.m1 = 2;
  886. target *= 5; /* fast clock */
  887. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  888. for (clock.p2 = limit->p2.p2_fast;
  889. clock.p2 >= limit->p2.p2_slow;
  890. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  891. unsigned int error_ppm;
  892. clock.p = clock.p1 * clock.p2;
  893. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  894. clock.n) << 22, refclk * clock.m1);
  895. if (m2 > INT_MAX/clock.m1)
  896. continue;
  897. clock.m2 = m2;
  898. chv_calc_dpll_params(refclk, &clock);
  899. if (!intel_PLL_is_valid(dev, limit, &clock))
  900. continue;
  901. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  902. best_error_ppm, &error_ppm))
  903. continue;
  904. *best_clock = clock;
  905. best_error_ppm = error_ppm;
  906. found = true;
  907. }
  908. }
  909. return found;
  910. }
  911. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  912. intel_clock_t *best_clock)
  913. {
  914. int refclk = 100000;
  915. const intel_limit_t *limit = &intel_limits_bxt;
  916. return chv_find_best_dpll(limit, crtc_state,
  917. target_clock, refclk, NULL, best_clock);
  918. }
  919. bool intel_crtc_active(struct drm_crtc *crtc)
  920. {
  921. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  922. /* Be paranoid as we can arrive here with only partial
  923. * state retrieved from the hardware during setup.
  924. *
  925. * We can ditch the adjusted_mode.crtc_clock check as soon
  926. * as Haswell has gained clock readout/fastboot support.
  927. *
  928. * We can ditch the crtc->primary->fb check as soon as we can
  929. * properly reconstruct framebuffers.
  930. *
  931. * FIXME: The intel_crtc->active here should be switched to
  932. * crtc->state->active once we have proper CRTC states wired up
  933. * for atomic.
  934. */
  935. return intel_crtc->active && crtc->primary->state->fb &&
  936. intel_crtc->config->base.adjusted_mode.crtc_clock;
  937. }
  938. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  939. enum pipe pipe)
  940. {
  941. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  942. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  943. return intel_crtc->config->cpu_transcoder;
  944. }
  945. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  946. {
  947. struct drm_i915_private *dev_priv = dev->dev_private;
  948. i915_reg_t reg = PIPEDSL(pipe);
  949. u32 line1, line2;
  950. u32 line_mask;
  951. if (IS_GEN2(dev))
  952. line_mask = DSL_LINEMASK_GEN2;
  953. else
  954. line_mask = DSL_LINEMASK_GEN3;
  955. line1 = I915_READ(reg) & line_mask;
  956. msleep(5);
  957. line2 = I915_READ(reg) & line_mask;
  958. return line1 == line2;
  959. }
  960. /*
  961. * intel_wait_for_pipe_off - wait for pipe to turn off
  962. * @crtc: crtc whose pipe to wait for
  963. *
  964. * After disabling a pipe, we can't wait for vblank in the usual way,
  965. * spinning on the vblank interrupt status bit, since we won't actually
  966. * see an interrupt when the pipe is disabled.
  967. *
  968. * On Gen4 and above:
  969. * wait for the pipe register state bit to turn off
  970. *
  971. * Otherwise:
  972. * wait for the display line value to settle (it usually
  973. * ends up stopping at the start of the next frame).
  974. *
  975. */
  976. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  977. {
  978. struct drm_device *dev = crtc->base.dev;
  979. struct drm_i915_private *dev_priv = dev->dev_private;
  980. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  981. enum pipe pipe = crtc->pipe;
  982. if (INTEL_INFO(dev)->gen >= 4) {
  983. i915_reg_t reg = PIPECONF(cpu_transcoder);
  984. /* Wait for the Pipe State to go off */
  985. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  986. 100))
  987. WARN(1, "pipe_off wait timed out\n");
  988. } else {
  989. /* Wait for the display line to settle */
  990. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  991. WARN(1, "pipe_off wait timed out\n");
  992. }
  993. }
  994. /* Only for pre-ILK configs */
  995. void assert_pll(struct drm_i915_private *dev_priv,
  996. enum pipe pipe, bool state)
  997. {
  998. u32 val;
  999. bool cur_state;
  1000. val = I915_READ(DPLL(pipe));
  1001. cur_state = !!(val & DPLL_VCO_ENABLE);
  1002. I915_STATE_WARN(cur_state != state,
  1003. "PLL state assertion failure (expected %s, current %s)\n",
  1004. onoff(state), onoff(cur_state));
  1005. }
  1006. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1007. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1008. {
  1009. u32 val;
  1010. bool cur_state;
  1011. mutex_lock(&dev_priv->sb_lock);
  1012. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1013. mutex_unlock(&dev_priv->sb_lock);
  1014. cur_state = val & DSI_PLL_VCO_EN;
  1015. I915_STATE_WARN(cur_state != state,
  1016. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1017. onoff(state), onoff(cur_state));
  1018. }
  1019. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1020. enum pipe pipe, bool state)
  1021. {
  1022. bool cur_state;
  1023. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1024. pipe);
  1025. if (HAS_DDI(dev_priv)) {
  1026. /* DDI does not have a specific FDI_TX register */
  1027. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1028. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1029. } else {
  1030. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1031. cur_state = !!(val & FDI_TX_ENABLE);
  1032. }
  1033. I915_STATE_WARN(cur_state != state,
  1034. "FDI TX state assertion failure (expected %s, current %s)\n",
  1035. onoff(state), onoff(cur_state));
  1036. }
  1037. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1038. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1039. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1040. enum pipe pipe, bool state)
  1041. {
  1042. u32 val;
  1043. bool cur_state;
  1044. val = I915_READ(FDI_RX_CTL(pipe));
  1045. cur_state = !!(val & FDI_RX_ENABLE);
  1046. I915_STATE_WARN(cur_state != state,
  1047. "FDI RX state assertion failure (expected %s, current %s)\n",
  1048. onoff(state), onoff(cur_state));
  1049. }
  1050. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1051. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1052. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1053. enum pipe pipe)
  1054. {
  1055. u32 val;
  1056. /* ILK FDI PLL is always enabled */
  1057. if (INTEL_INFO(dev_priv)->gen == 5)
  1058. return;
  1059. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1060. if (HAS_DDI(dev_priv))
  1061. return;
  1062. val = I915_READ(FDI_TX_CTL(pipe));
  1063. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1064. }
  1065. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1066. enum pipe pipe, bool state)
  1067. {
  1068. u32 val;
  1069. bool cur_state;
  1070. val = I915_READ(FDI_RX_CTL(pipe));
  1071. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1072. I915_STATE_WARN(cur_state != state,
  1073. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1074. onoff(state), onoff(cur_state));
  1075. }
  1076. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1077. enum pipe pipe)
  1078. {
  1079. struct drm_device *dev = dev_priv->dev;
  1080. i915_reg_t pp_reg;
  1081. u32 val;
  1082. enum pipe panel_pipe = PIPE_A;
  1083. bool locked = true;
  1084. if (WARN_ON(HAS_DDI(dev)))
  1085. return;
  1086. if (HAS_PCH_SPLIT(dev)) {
  1087. u32 port_sel;
  1088. pp_reg = PCH_PP_CONTROL;
  1089. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1090. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1091. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1092. panel_pipe = PIPE_B;
  1093. /* XXX: else fix for eDP */
  1094. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1095. /* presumably write lock depends on pipe, not port select */
  1096. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1097. panel_pipe = pipe;
  1098. } else {
  1099. pp_reg = PP_CONTROL;
  1100. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1101. panel_pipe = PIPE_B;
  1102. }
  1103. val = I915_READ(pp_reg);
  1104. if (!(val & PANEL_POWER_ON) ||
  1105. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1106. locked = false;
  1107. I915_STATE_WARN(panel_pipe == pipe && locked,
  1108. "panel assertion failure, pipe %c regs locked\n",
  1109. pipe_name(pipe));
  1110. }
  1111. static void assert_cursor(struct drm_i915_private *dev_priv,
  1112. enum pipe pipe, bool state)
  1113. {
  1114. struct drm_device *dev = dev_priv->dev;
  1115. bool cur_state;
  1116. if (IS_845G(dev) || IS_I865G(dev))
  1117. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1118. else
  1119. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1120. I915_STATE_WARN(cur_state != state,
  1121. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1122. pipe_name(pipe), onoff(state), onoff(cur_state));
  1123. }
  1124. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1125. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1126. void assert_pipe(struct drm_i915_private *dev_priv,
  1127. enum pipe pipe, bool state)
  1128. {
  1129. bool cur_state;
  1130. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1131. pipe);
  1132. enum intel_display_power_domain power_domain;
  1133. /* if we need the pipe quirk it must be always on */
  1134. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1135. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1136. state = true;
  1137. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1138. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1139. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1140. cur_state = !!(val & PIPECONF_ENABLE);
  1141. intel_display_power_put(dev_priv, power_domain);
  1142. } else {
  1143. cur_state = false;
  1144. }
  1145. I915_STATE_WARN(cur_state != state,
  1146. "pipe %c assertion failure (expected %s, current %s)\n",
  1147. pipe_name(pipe), onoff(state), onoff(cur_state));
  1148. }
  1149. static void assert_plane(struct drm_i915_private *dev_priv,
  1150. enum plane plane, bool state)
  1151. {
  1152. u32 val;
  1153. bool cur_state;
  1154. val = I915_READ(DSPCNTR(plane));
  1155. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1156. I915_STATE_WARN(cur_state != state,
  1157. "plane %c assertion failure (expected %s, current %s)\n",
  1158. plane_name(plane), onoff(state), onoff(cur_state));
  1159. }
  1160. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1161. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1162. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1163. enum pipe pipe)
  1164. {
  1165. struct drm_device *dev = dev_priv->dev;
  1166. int i;
  1167. /* Primary planes are fixed to pipes on gen4+ */
  1168. if (INTEL_INFO(dev)->gen >= 4) {
  1169. u32 val = I915_READ(DSPCNTR(pipe));
  1170. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1171. "plane %c assertion failure, should be disabled but not\n",
  1172. plane_name(pipe));
  1173. return;
  1174. }
  1175. /* Need to check both planes against the pipe */
  1176. for_each_pipe(dev_priv, i) {
  1177. u32 val = I915_READ(DSPCNTR(i));
  1178. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1179. DISPPLANE_SEL_PIPE_SHIFT;
  1180. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1181. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1182. plane_name(i), pipe_name(pipe));
  1183. }
  1184. }
  1185. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1186. enum pipe pipe)
  1187. {
  1188. struct drm_device *dev = dev_priv->dev;
  1189. int sprite;
  1190. if (INTEL_INFO(dev)->gen >= 9) {
  1191. for_each_sprite(dev_priv, pipe, sprite) {
  1192. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1193. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1194. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1195. sprite, pipe_name(pipe));
  1196. }
  1197. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1198. for_each_sprite(dev_priv, pipe, sprite) {
  1199. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1200. I915_STATE_WARN(val & SP_ENABLE,
  1201. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1202. sprite_name(pipe, sprite), pipe_name(pipe));
  1203. }
  1204. } else if (INTEL_INFO(dev)->gen >= 7) {
  1205. u32 val = I915_READ(SPRCTL(pipe));
  1206. I915_STATE_WARN(val & SPRITE_ENABLE,
  1207. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1208. plane_name(pipe), pipe_name(pipe));
  1209. } else if (INTEL_INFO(dev)->gen >= 5) {
  1210. u32 val = I915_READ(DVSCNTR(pipe));
  1211. I915_STATE_WARN(val & DVS_ENABLE,
  1212. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1213. plane_name(pipe), pipe_name(pipe));
  1214. }
  1215. }
  1216. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1217. {
  1218. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1219. drm_crtc_vblank_put(crtc);
  1220. }
  1221. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1222. enum pipe pipe)
  1223. {
  1224. u32 val;
  1225. bool enabled;
  1226. val = I915_READ(PCH_TRANSCONF(pipe));
  1227. enabled = !!(val & TRANS_ENABLE);
  1228. I915_STATE_WARN(enabled,
  1229. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1230. pipe_name(pipe));
  1231. }
  1232. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1233. enum pipe pipe, u32 port_sel, u32 val)
  1234. {
  1235. if ((val & DP_PORT_EN) == 0)
  1236. return false;
  1237. if (HAS_PCH_CPT(dev_priv)) {
  1238. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1239. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1240. return false;
  1241. } else if (IS_CHERRYVIEW(dev_priv)) {
  1242. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1243. return false;
  1244. } else {
  1245. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1246. return false;
  1247. }
  1248. return true;
  1249. }
  1250. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1251. enum pipe pipe, u32 val)
  1252. {
  1253. if ((val & SDVO_ENABLE) == 0)
  1254. return false;
  1255. if (HAS_PCH_CPT(dev_priv)) {
  1256. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1257. return false;
  1258. } else if (IS_CHERRYVIEW(dev_priv)) {
  1259. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1260. return false;
  1261. } else {
  1262. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1263. return false;
  1264. }
  1265. return true;
  1266. }
  1267. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1268. enum pipe pipe, u32 val)
  1269. {
  1270. if ((val & LVDS_PORT_EN) == 0)
  1271. return false;
  1272. if (HAS_PCH_CPT(dev_priv)) {
  1273. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1274. return false;
  1275. } else {
  1276. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1277. return false;
  1278. }
  1279. return true;
  1280. }
  1281. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1282. enum pipe pipe, u32 val)
  1283. {
  1284. if ((val & ADPA_DAC_ENABLE) == 0)
  1285. return false;
  1286. if (HAS_PCH_CPT(dev_priv)) {
  1287. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1288. return false;
  1289. } else {
  1290. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1291. return false;
  1292. }
  1293. return true;
  1294. }
  1295. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1296. enum pipe pipe, i915_reg_t reg,
  1297. u32 port_sel)
  1298. {
  1299. u32 val = I915_READ(reg);
  1300. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1301. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1302. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1303. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1304. && (val & DP_PIPEB_SELECT),
  1305. "IBX PCH dp port still using transcoder B\n");
  1306. }
  1307. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1308. enum pipe pipe, i915_reg_t reg)
  1309. {
  1310. u32 val = I915_READ(reg);
  1311. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1312. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1313. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1314. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1315. && (val & SDVO_PIPE_B_SELECT),
  1316. "IBX PCH hdmi port still using transcoder B\n");
  1317. }
  1318. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1319. enum pipe pipe)
  1320. {
  1321. u32 val;
  1322. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1323. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1324. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1325. val = I915_READ(PCH_ADPA);
  1326. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1327. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1328. pipe_name(pipe));
  1329. val = I915_READ(PCH_LVDS);
  1330. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1331. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1332. pipe_name(pipe));
  1333. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1334. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1335. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1336. }
  1337. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1338. const struct intel_crtc_state *pipe_config)
  1339. {
  1340. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1341. enum pipe pipe = crtc->pipe;
  1342. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1343. POSTING_READ(DPLL(pipe));
  1344. udelay(150);
  1345. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1346. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1347. }
  1348. static void vlv_enable_pll(struct intel_crtc *crtc,
  1349. const struct intel_crtc_state *pipe_config)
  1350. {
  1351. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1352. enum pipe pipe = crtc->pipe;
  1353. assert_pipe_disabled(dev_priv, pipe);
  1354. /* PLL is protected by panel, make sure we can write it */
  1355. assert_panel_unlocked(dev_priv, pipe);
  1356. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1357. _vlv_enable_pll(crtc, pipe_config);
  1358. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1359. POSTING_READ(DPLL_MD(pipe));
  1360. }
  1361. static void _chv_enable_pll(struct intel_crtc *crtc,
  1362. const struct intel_crtc_state *pipe_config)
  1363. {
  1364. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1365. enum pipe pipe = crtc->pipe;
  1366. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1367. u32 tmp;
  1368. mutex_lock(&dev_priv->sb_lock);
  1369. /* Enable back the 10bit clock to display controller */
  1370. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1371. tmp |= DPIO_DCLKP_EN;
  1372. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1373. mutex_unlock(&dev_priv->sb_lock);
  1374. /*
  1375. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1376. */
  1377. udelay(1);
  1378. /* Enable PLL */
  1379. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1380. /* Check PLL is locked */
  1381. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1382. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1383. }
  1384. static void chv_enable_pll(struct intel_crtc *crtc,
  1385. const struct intel_crtc_state *pipe_config)
  1386. {
  1387. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1388. enum pipe pipe = crtc->pipe;
  1389. assert_pipe_disabled(dev_priv, pipe);
  1390. /* PLL is protected by panel, make sure we can write it */
  1391. assert_panel_unlocked(dev_priv, pipe);
  1392. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1393. _chv_enable_pll(crtc, pipe_config);
  1394. if (pipe != PIPE_A) {
  1395. /*
  1396. * WaPixelRepeatModeFixForC0:chv
  1397. *
  1398. * DPLLCMD is AWOL. Use chicken bits to propagate
  1399. * the value from DPLLBMD to either pipe B or C.
  1400. */
  1401. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1402. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1403. I915_WRITE(CBR4_VLV, 0);
  1404. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1405. /*
  1406. * DPLLB VGA mode also seems to cause problems.
  1407. * We should always have it disabled.
  1408. */
  1409. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1410. } else {
  1411. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1412. POSTING_READ(DPLL_MD(pipe));
  1413. }
  1414. }
  1415. static int intel_num_dvo_pipes(struct drm_device *dev)
  1416. {
  1417. struct intel_crtc *crtc;
  1418. int count = 0;
  1419. for_each_intel_crtc(dev, crtc)
  1420. count += crtc->base.state->active &&
  1421. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1422. return count;
  1423. }
  1424. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1425. {
  1426. struct drm_device *dev = crtc->base.dev;
  1427. struct drm_i915_private *dev_priv = dev->dev_private;
  1428. i915_reg_t reg = DPLL(crtc->pipe);
  1429. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1430. assert_pipe_disabled(dev_priv, crtc->pipe);
  1431. /* PLL is protected by panel, make sure we can write it */
  1432. if (IS_MOBILE(dev) && !IS_I830(dev))
  1433. assert_panel_unlocked(dev_priv, crtc->pipe);
  1434. /* Enable DVO 2x clock on both PLLs if necessary */
  1435. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1436. /*
  1437. * It appears to be important that we don't enable this
  1438. * for the current pipe before otherwise configuring the
  1439. * PLL. No idea how this should be handled if multiple
  1440. * DVO outputs are enabled simultaneosly.
  1441. */
  1442. dpll |= DPLL_DVO_2X_MODE;
  1443. I915_WRITE(DPLL(!crtc->pipe),
  1444. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1445. }
  1446. /*
  1447. * Apparently we need to have VGA mode enabled prior to changing
  1448. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1449. * dividers, even though the register value does change.
  1450. */
  1451. I915_WRITE(reg, 0);
  1452. I915_WRITE(reg, dpll);
  1453. /* Wait for the clocks to stabilize. */
  1454. POSTING_READ(reg);
  1455. udelay(150);
  1456. if (INTEL_INFO(dev)->gen >= 4) {
  1457. I915_WRITE(DPLL_MD(crtc->pipe),
  1458. crtc->config->dpll_hw_state.dpll_md);
  1459. } else {
  1460. /* The pixel multiplier can only be updated once the
  1461. * DPLL is enabled and the clocks are stable.
  1462. *
  1463. * So write it again.
  1464. */
  1465. I915_WRITE(reg, dpll);
  1466. }
  1467. /* We do this three times for luck */
  1468. I915_WRITE(reg, dpll);
  1469. POSTING_READ(reg);
  1470. udelay(150); /* wait for warmup */
  1471. I915_WRITE(reg, dpll);
  1472. POSTING_READ(reg);
  1473. udelay(150); /* wait for warmup */
  1474. I915_WRITE(reg, dpll);
  1475. POSTING_READ(reg);
  1476. udelay(150); /* wait for warmup */
  1477. }
  1478. /**
  1479. * i9xx_disable_pll - disable a PLL
  1480. * @dev_priv: i915 private structure
  1481. * @pipe: pipe PLL to disable
  1482. *
  1483. * Disable the PLL for @pipe, making sure the pipe is off first.
  1484. *
  1485. * Note! This is for pre-ILK only.
  1486. */
  1487. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1488. {
  1489. struct drm_device *dev = crtc->base.dev;
  1490. struct drm_i915_private *dev_priv = dev->dev_private;
  1491. enum pipe pipe = crtc->pipe;
  1492. /* Disable DVO 2x clock on both PLLs if necessary */
  1493. if (IS_I830(dev) &&
  1494. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1495. !intel_num_dvo_pipes(dev)) {
  1496. I915_WRITE(DPLL(PIPE_B),
  1497. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1498. I915_WRITE(DPLL(PIPE_A),
  1499. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1500. }
  1501. /* Don't disable pipe or pipe PLLs if needed */
  1502. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1503. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1504. return;
  1505. /* Make sure the pipe isn't still relying on us */
  1506. assert_pipe_disabled(dev_priv, pipe);
  1507. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1508. POSTING_READ(DPLL(pipe));
  1509. }
  1510. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1511. {
  1512. u32 val;
  1513. /* Make sure the pipe isn't still relying on us */
  1514. assert_pipe_disabled(dev_priv, pipe);
  1515. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1516. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1517. if (pipe != PIPE_A)
  1518. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1519. I915_WRITE(DPLL(pipe), val);
  1520. POSTING_READ(DPLL(pipe));
  1521. }
  1522. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1523. {
  1524. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1525. u32 val;
  1526. /* Make sure the pipe isn't still relying on us */
  1527. assert_pipe_disabled(dev_priv, pipe);
  1528. val = DPLL_SSC_REF_CLK_CHV |
  1529. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1530. if (pipe != PIPE_A)
  1531. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1532. I915_WRITE(DPLL(pipe), val);
  1533. POSTING_READ(DPLL(pipe));
  1534. mutex_lock(&dev_priv->sb_lock);
  1535. /* Disable 10bit clock to display controller */
  1536. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1537. val &= ~DPIO_DCLKP_EN;
  1538. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1539. mutex_unlock(&dev_priv->sb_lock);
  1540. }
  1541. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1542. struct intel_digital_port *dport,
  1543. unsigned int expected_mask)
  1544. {
  1545. u32 port_mask;
  1546. i915_reg_t dpll_reg;
  1547. switch (dport->port) {
  1548. case PORT_B:
  1549. port_mask = DPLL_PORTB_READY_MASK;
  1550. dpll_reg = DPLL(0);
  1551. break;
  1552. case PORT_C:
  1553. port_mask = DPLL_PORTC_READY_MASK;
  1554. dpll_reg = DPLL(0);
  1555. expected_mask <<= 4;
  1556. break;
  1557. case PORT_D:
  1558. port_mask = DPLL_PORTD_READY_MASK;
  1559. dpll_reg = DPIO_PHY_STATUS;
  1560. break;
  1561. default:
  1562. BUG();
  1563. }
  1564. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1565. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1566. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1567. }
  1568. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1569. enum pipe pipe)
  1570. {
  1571. struct drm_device *dev = dev_priv->dev;
  1572. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1573. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1574. i915_reg_t reg;
  1575. uint32_t val, pipeconf_val;
  1576. /* Make sure PCH DPLL is enabled */
  1577. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1578. /* FDI must be feeding us bits for PCH ports */
  1579. assert_fdi_tx_enabled(dev_priv, pipe);
  1580. assert_fdi_rx_enabled(dev_priv, pipe);
  1581. if (HAS_PCH_CPT(dev)) {
  1582. /* Workaround: Set the timing override bit before enabling the
  1583. * pch transcoder. */
  1584. reg = TRANS_CHICKEN2(pipe);
  1585. val = I915_READ(reg);
  1586. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1587. I915_WRITE(reg, val);
  1588. }
  1589. reg = PCH_TRANSCONF(pipe);
  1590. val = I915_READ(reg);
  1591. pipeconf_val = I915_READ(PIPECONF(pipe));
  1592. if (HAS_PCH_IBX(dev_priv)) {
  1593. /*
  1594. * Make the BPC in transcoder be consistent with
  1595. * that in pipeconf reg. For HDMI we must use 8bpc
  1596. * here for both 8bpc and 12bpc.
  1597. */
  1598. val &= ~PIPECONF_BPC_MASK;
  1599. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1600. val |= PIPECONF_8BPC;
  1601. else
  1602. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1603. }
  1604. val &= ~TRANS_INTERLACE_MASK;
  1605. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1606. if (HAS_PCH_IBX(dev_priv) &&
  1607. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1608. val |= TRANS_LEGACY_INTERLACED_ILK;
  1609. else
  1610. val |= TRANS_INTERLACED;
  1611. else
  1612. val |= TRANS_PROGRESSIVE;
  1613. I915_WRITE(reg, val | TRANS_ENABLE);
  1614. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1615. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1616. }
  1617. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1618. enum transcoder cpu_transcoder)
  1619. {
  1620. u32 val, pipeconf_val;
  1621. /* FDI must be feeding us bits for PCH ports */
  1622. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1623. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1624. /* Workaround: set timing override bit. */
  1625. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1626. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1627. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1628. val = TRANS_ENABLE;
  1629. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1630. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1631. PIPECONF_INTERLACED_ILK)
  1632. val |= TRANS_INTERLACED;
  1633. else
  1634. val |= TRANS_PROGRESSIVE;
  1635. I915_WRITE(LPT_TRANSCONF, val);
  1636. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1637. DRM_ERROR("Failed to enable PCH transcoder\n");
  1638. }
  1639. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1640. enum pipe pipe)
  1641. {
  1642. struct drm_device *dev = dev_priv->dev;
  1643. i915_reg_t reg;
  1644. uint32_t val;
  1645. /* FDI relies on the transcoder */
  1646. assert_fdi_tx_disabled(dev_priv, pipe);
  1647. assert_fdi_rx_disabled(dev_priv, pipe);
  1648. /* Ports must be off as well */
  1649. assert_pch_ports_disabled(dev_priv, pipe);
  1650. reg = PCH_TRANSCONF(pipe);
  1651. val = I915_READ(reg);
  1652. val &= ~TRANS_ENABLE;
  1653. I915_WRITE(reg, val);
  1654. /* wait for PCH transcoder off, transcoder state */
  1655. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1656. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1657. if (HAS_PCH_CPT(dev)) {
  1658. /* Workaround: Clear the timing override chicken bit again. */
  1659. reg = TRANS_CHICKEN2(pipe);
  1660. val = I915_READ(reg);
  1661. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1662. I915_WRITE(reg, val);
  1663. }
  1664. }
  1665. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1666. {
  1667. u32 val;
  1668. val = I915_READ(LPT_TRANSCONF);
  1669. val &= ~TRANS_ENABLE;
  1670. I915_WRITE(LPT_TRANSCONF, val);
  1671. /* wait for PCH transcoder off, transcoder state */
  1672. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1673. DRM_ERROR("Failed to disable PCH transcoder\n");
  1674. /* Workaround: clear timing override bit. */
  1675. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1676. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1677. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1678. }
  1679. /**
  1680. * intel_enable_pipe - enable a pipe, asserting requirements
  1681. * @crtc: crtc responsible for the pipe
  1682. *
  1683. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1684. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1685. */
  1686. static void intel_enable_pipe(struct intel_crtc *crtc)
  1687. {
  1688. struct drm_device *dev = crtc->base.dev;
  1689. struct drm_i915_private *dev_priv = dev->dev_private;
  1690. enum pipe pipe = crtc->pipe;
  1691. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1692. enum pipe pch_transcoder;
  1693. i915_reg_t reg;
  1694. u32 val;
  1695. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1696. assert_planes_disabled(dev_priv, pipe);
  1697. assert_cursor_disabled(dev_priv, pipe);
  1698. assert_sprites_disabled(dev_priv, pipe);
  1699. if (HAS_PCH_LPT(dev_priv))
  1700. pch_transcoder = TRANSCODER_A;
  1701. else
  1702. pch_transcoder = pipe;
  1703. /*
  1704. * A pipe without a PLL won't actually be able to drive bits from
  1705. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1706. * need the check.
  1707. */
  1708. if (HAS_GMCH_DISPLAY(dev_priv))
  1709. if (crtc->config->has_dsi_encoder)
  1710. assert_dsi_pll_enabled(dev_priv);
  1711. else
  1712. assert_pll_enabled(dev_priv, pipe);
  1713. else {
  1714. if (crtc->config->has_pch_encoder) {
  1715. /* if driving the PCH, we need FDI enabled */
  1716. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1717. assert_fdi_tx_pll_enabled(dev_priv,
  1718. (enum pipe) cpu_transcoder);
  1719. }
  1720. /* FIXME: assert CPU port conditions for SNB+ */
  1721. }
  1722. reg = PIPECONF(cpu_transcoder);
  1723. val = I915_READ(reg);
  1724. if (val & PIPECONF_ENABLE) {
  1725. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1726. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1727. return;
  1728. }
  1729. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1730. POSTING_READ(reg);
  1731. /*
  1732. * Until the pipe starts DSL will read as 0, which would cause
  1733. * an apparent vblank timestamp jump, which messes up also the
  1734. * frame count when it's derived from the timestamps. So let's
  1735. * wait for the pipe to start properly before we call
  1736. * drm_crtc_vblank_on()
  1737. */
  1738. if (dev->max_vblank_count == 0 &&
  1739. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1740. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1741. }
  1742. /**
  1743. * intel_disable_pipe - disable a pipe, asserting requirements
  1744. * @crtc: crtc whose pipes is to be disabled
  1745. *
  1746. * Disable the pipe of @crtc, making sure that various hardware
  1747. * specific requirements are met, if applicable, e.g. plane
  1748. * disabled, panel fitter off, etc.
  1749. *
  1750. * Will wait until the pipe has shut down before returning.
  1751. */
  1752. static void intel_disable_pipe(struct intel_crtc *crtc)
  1753. {
  1754. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1755. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1756. enum pipe pipe = crtc->pipe;
  1757. i915_reg_t reg;
  1758. u32 val;
  1759. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1760. /*
  1761. * Make sure planes won't keep trying to pump pixels to us,
  1762. * or we might hang the display.
  1763. */
  1764. assert_planes_disabled(dev_priv, pipe);
  1765. assert_cursor_disabled(dev_priv, pipe);
  1766. assert_sprites_disabled(dev_priv, pipe);
  1767. reg = PIPECONF(cpu_transcoder);
  1768. val = I915_READ(reg);
  1769. if ((val & PIPECONF_ENABLE) == 0)
  1770. return;
  1771. /*
  1772. * Double wide has implications for planes
  1773. * so best keep it disabled when not needed.
  1774. */
  1775. if (crtc->config->double_wide)
  1776. val &= ~PIPECONF_DOUBLE_WIDE;
  1777. /* Don't disable pipe or pipe PLLs if needed */
  1778. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1779. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1780. val &= ~PIPECONF_ENABLE;
  1781. I915_WRITE(reg, val);
  1782. if ((val & PIPECONF_ENABLE) == 0)
  1783. intel_wait_for_pipe_off(crtc);
  1784. }
  1785. static bool need_vtd_wa(struct drm_device *dev)
  1786. {
  1787. #ifdef CONFIG_INTEL_IOMMU
  1788. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1789. return true;
  1790. #endif
  1791. return false;
  1792. }
  1793. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1794. {
  1795. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1796. }
  1797. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1798. uint64_t fb_modifier, unsigned int cpp)
  1799. {
  1800. switch (fb_modifier) {
  1801. case DRM_FORMAT_MOD_NONE:
  1802. return cpp;
  1803. case I915_FORMAT_MOD_X_TILED:
  1804. if (IS_GEN2(dev_priv))
  1805. return 128;
  1806. else
  1807. return 512;
  1808. case I915_FORMAT_MOD_Y_TILED:
  1809. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1810. return 128;
  1811. else
  1812. return 512;
  1813. case I915_FORMAT_MOD_Yf_TILED:
  1814. switch (cpp) {
  1815. case 1:
  1816. return 64;
  1817. case 2:
  1818. case 4:
  1819. return 128;
  1820. case 8:
  1821. case 16:
  1822. return 256;
  1823. default:
  1824. MISSING_CASE(cpp);
  1825. return cpp;
  1826. }
  1827. break;
  1828. default:
  1829. MISSING_CASE(fb_modifier);
  1830. return cpp;
  1831. }
  1832. }
  1833. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1834. uint64_t fb_modifier, unsigned int cpp)
  1835. {
  1836. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1837. return 1;
  1838. else
  1839. return intel_tile_size(dev_priv) /
  1840. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1841. }
  1842. /* Return the tile dimensions in pixel units */
  1843. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1844. unsigned int *tile_width,
  1845. unsigned int *tile_height,
  1846. uint64_t fb_modifier,
  1847. unsigned int cpp)
  1848. {
  1849. unsigned int tile_width_bytes =
  1850. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1851. *tile_width = tile_width_bytes / cpp;
  1852. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1853. }
  1854. unsigned int
  1855. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1856. uint32_t pixel_format, uint64_t fb_modifier)
  1857. {
  1858. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1859. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1860. return ALIGN(height, tile_height);
  1861. }
  1862. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1863. {
  1864. unsigned int size = 0;
  1865. int i;
  1866. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1867. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1868. return size;
  1869. }
  1870. static void
  1871. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1872. const struct drm_framebuffer *fb,
  1873. unsigned int rotation)
  1874. {
  1875. if (intel_rotation_90_or_270(rotation)) {
  1876. *view = i915_ggtt_view_rotated;
  1877. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1878. } else {
  1879. *view = i915_ggtt_view_normal;
  1880. }
  1881. }
  1882. static void
  1883. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  1884. struct drm_framebuffer *fb)
  1885. {
  1886. struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
  1887. unsigned int tile_size, tile_width, tile_height, cpp;
  1888. tile_size = intel_tile_size(dev_priv);
  1889. cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  1890. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1891. fb->modifier[0], cpp);
  1892. info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
  1893. info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
  1894. if (info->pixel_format == DRM_FORMAT_NV12) {
  1895. cpp = drm_format_plane_cpp(fb->pixel_format, 1);
  1896. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1897. fb->modifier[1], cpp);
  1898. info->uv_offset = fb->offsets[1];
  1899. info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
  1900. info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
  1901. }
  1902. }
  1903. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1904. {
  1905. if (INTEL_INFO(dev_priv)->gen >= 9)
  1906. return 256 * 1024;
  1907. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1908. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1909. return 128 * 1024;
  1910. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1911. return 4 * 1024;
  1912. else
  1913. return 0;
  1914. }
  1915. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1916. uint64_t fb_modifier)
  1917. {
  1918. switch (fb_modifier) {
  1919. case DRM_FORMAT_MOD_NONE:
  1920. return intel_linear_alignment(dev_priv);
  1921. case I915_FORMAT_MOD_X_TILED:
  1922. if (INTEL_INFO(dev_priv)->gen >= 9)
  1923. return 256 * 1024;
  1924. return 0;
  1925. case I915_FORMAT_MOD_Y_TILED:
  1926. case I915_FORMAT_MOD_Yf_TILED:
  1927. return 1 * 1024 * 1024;
  1928. default:
  1929. MISSING_CASE(fb_modifier);
  1930. return 0;
  1931. }
  1932. }
  1933. int
  1934. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1935. unsigned int rotation)
  1936. {
  1937. struct drm_device *dev = fb->dev;
  1938. struct drm_i915_private *dev_priv = dev->dev_private;
  1939. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1940. struct i915_ggtt_view view;
  1941. u32 alignment;
  1942. int ret;
  1943. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1944. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  1945. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1946. /* Note that the w/a also requires 64 PTE of padding following the
  1947. * bo. We currently fill all unused PTE with the shadow page and so
  1948. * we should always have valid PTE following the scanout preventing
  1949. * the VT-d warning.
  1950. */
  1951. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1952. alignment = 256 * 1024;
  1953. /*
  1954. * Global gtt pte registers are special registers which actually forward
  1955. * writes to a chunk of system memory. Which means that there is no risk
  1956. * that the register values disappear as soon as we call
  1957. * intel_runtime_pm_put(), so it is correct to wrap only the
  1958. * pin/unpin/fence and not more.
  1959. */
  1960. intel_runtime_pm_get(dev_priv);
  1961. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  1962. &view);
  1963. if (ret)
  1964. goto err_pm;
  1965. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1966. * fence, whereas 965+ only requires a fence if using
  1967. * framebuffer compression. For simplicity, we always install
  1968. * a fence as the cost is not that onerous.
  1969. */
  1970. if (view.type == I915_GGTT_VIEW_NORMAL) {
  1971. ret = i915_gem_object_get_fence(obj);
  1972. if (ret == -EDEADLK) {
  1973. /*
  1974. * -EDEADLK means there are no free fences
  1975. * no pending flips.
  1976. *
  1977. * This is propagated to atomic, but it uses
  1978. * -EDEADLK to force a locking recovery, so
  1979. * change the returned error to -EBUSY.
  1980. */
  1981. ret = -EBUSY;
  1982. goto err_unpin;
  1983. } else if (ret)
  1984. goto err_unpin;
  1985. i915_gem_object_pin_fence(obj);
  1986. }
  1987. intel_runtime_pm_put(dev_priv);
  1988. return 0;
  1989. err_unpin:
  1990. i915_gem_object_unpin_from_display_plane(obj, &view);
  1991. err_pm:
  1992. intel_runtime_pm_put(dev_priv);
  1993. return ret;
  1994. }
  1995. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1996. {
  1997. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1998. struct i915_ggtt_view view;
  1999. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2000. intel_fill_fb_ggtt_view(&view, fb, rotation);
  2001. if (view.type == I915_GGTT_VIEW_NORMAL)
  2002. i915_gem_object_unpin_fence(obj);
  2003. i915_gem_object_unpin_from_display_plane(obj, &view);
  2004. }
  2005. /*
  2006. * Adjust the tile offset by moving the difference into
  2007. * the x/y offsets.
  2008. *
  2009. * Input tile dimensions and pitch must already be
  2010. * rotated to match x and y, and in pixel units.
  2011. */
  2012. static u32 intel_adjust_tile_offset(int *x, int *y,
  2013. unsigned int tile_width,
  2014. unsigned int tile_height,
  2015. unsigned int tile_size,
  2016. unsigned int pitch_tiles,
  2017. u32 old_offset,
  2018. u32 new_offset)
  2019. {
  2020. unsigned int tiles;
  2021. WARN_ON(old_offset & (tile_size - 1));
  2022. WARN_ON(new_offset & (tile_size - 1));
  2023. WARN_ON(new_offset > old_offset);
  2024. tiles = (old_offset - new_offset) / tile_size;
  2025. *y += tiles / pitch_tiles * tile_height;
  2026. *x += tiles % pitch_tiles * tile_width;
  2027. return new_offset;
  2028. }
  2029. /*
  2030. * Computes the linear offset to the base tile and adjusts
  2031. * x, y. bytes per pixel is assumed to be a power-of-two.
  2032. *
  2033. * In the 90/270 rotated case, x and y are assumed
  2034. * to be already rotated to match the rotated GTT view, and
  2035. * pitch is the tile_height aligned framebuffer height.
  2036. */
  2037. u32 intel_compute_tile_offset(int *x, int *y,
  2038. const struct drm_framebuffer *fb, int plane,
  2039. unsigned int pitch,
  2040. unsigned int rotation)
  2041. {
  2042. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2043. uint64_t fb_modifier = fb->modifier[plane];
  2044. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2045. u32 offset, offset_aligned, alignment;
  2046. alignment = intel_surf_alignment(dev_priv, fb_modifier);
  2047. if (alignment)
  2048. alignment--;
  2049. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2050. unsigned int tile_size, tile_width, tile_height;
  2051. unsigned int tile_rows, tiles, pitch_tiles;
  2052. tile_size = intel_tile_size(dev_priv);
  2053. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2054. fb_modifier, cpp);
  2055. if (intel_rotation_90_or_270(rotation)) {
  2056. pitch_tiles = pitch / tile_height;
  2057. swap(tile_width, tile_height);
  2058. } else {
  2059. pitch_tiles = pitch / (tile_width * cpp);
  2060. }
  2061. tile_rows = *y / tile_height;
  2062. *y %= tile_height;
  2063. tiles = *x / tile_width;
  2064. *x %= tile_width;
  2065. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2066. offset_aligned = offset & ~alignment;
  2067. intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2068. tile_size, pitch_tiles,
  2069. offset, offset_aligned);
  2070. } else {
  2071. offset = *y * pitch + *x * cpp;
  2072. offset_aligned = offset & ~alignment;
  2073. *y = (offset & alignment) / pitch;
  2074. *x = ((offset & alignment) - *y * pitch) / cpp;
  2075. }
  2076. return offset_aligned;
  2077. }
  2078. static int i9xx_format_to_fourcc(int format)
  2079. {
  2080. switch (format) {
  2081. case DISPPLANE_8BPP:
  2082. return DRM_FORMAT_C8;
  2083. case DISPPLANE_BGRX555:
  2084. return DRM_FORMAT_XRGB1555;
  2085. case DISPPLANE_BGRX565:
  2086. return DRM_FORMAT_RGB565;
  2087. default:
  2088. case DISPPLANE_BGRX888:
  2089. return DRM_FORMAT_XRGB8888;
  2090. case DISPPLANE_RGBX888:
  2091. return DRM_FORMAT_XBGR8888;
  2092. case DISPPLANE_BGRX101010:
  2093. return DRM_FORMAT_XRGB2101010;
  2094. case DISPPLANE_RGBX101010:
  2095. return DRM_FORMAT_XBGR2101010;
  2096. }
  2097. }
  2098. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2099. {
  2100. switch (format) {
  2101. case PLANE_CTL_FORMAT_RGB_565:
  2102. return DRM_FORMAT_RGB565;
  2103. default:
  2104. case PLANE_CTL_FORMAT_XRGB_8888:
  2105. if (rgb_order) {
  2106. if (alpha)
  2107. return DRM_FORMAT_ABGR8888;
  2108. else
  2109. return DRM_FORMAT_XBGR8888;
  2110. } else {
  2111. if (alpha)
  2112. return DRM_FORMAT_ARGB8888;
  2113. else
  2114. return DRM_FORMAT_XRGB8888;
  2115. }
  2116. case PLANE_CTL_FORMAT_XRGB_2101010:
  2117. if (rgb_order)
  2118. return DRM_FORMAT_XBGR2101010;
  2119. else
  2120. return DRM_FORMAT_XRGB2101010;
  2121. }
  2122. }
  2123. static bool
  2124. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2125. struct intel_initial_plane_config *plane_config)
  2126. {
  2127. struct drm_device *dev = crtc->base.dev;
  2128. struct drm_i915_private *dev_priv = to_i915(dev);
  2129. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2130. struct drm_i915_gem_object *obj = NULL;
  2131. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2132. struct drm_framebuffer *fb = &plane_config->fb->base;
  2133. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2134. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2135. PAGE_SIZE);
  2136. size_aligned -= base_aligned;
  2137. if (plane_config->size == 0)
  2138. return false;
  2139. /* If the FB is too big, just don't use it since fbdev is not very
  2140. * important and we should probably use that space with FBC or other
  2141. * features. */
  2142. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2143. return false;
  2144. mutex_lock(&dev->struct_mutex);
  2145. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2146. base_aligned,
  2147. base_aligned,
  2148. size_aligned);
  2149. if (!obj) {
  2150. mutex_unlock(&dev->struct_mutex);
  2151. return false;
  2152. }
  2153. obj->tiling_mode = plane_config->tiling;
  2154. if (obj->tiling_mode == I915_TILING_X)
  2155. obj->stride = fb->pitches[0];
  2156. mode_cmd.pixel_format = fb->pixel_format;
  2157. mode_cmd.width = fb->width;
  2158. mode_cmd.height = fb->height;
  2159. mode_cmd.pitches[0] = fb->pitches[0];
  2160. mode_cmd.modifier[0] = fb->modifier[0];
  2161. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2162. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2163. &mode_cmd, obj)) {
  2164. DRM_DEBUG_KMS("intel fb init failed\n");
  2165. goto out_unref_obj;
  2166. }
  2167. mutex_unlock(&dev->struct_mutex);
  2168. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2169. return true;
  2170. out_unref_obj:
  2171. drm_gem_object_unreference(&obj->base);
  2172. mutex_unlock(&dev->struct_mutex);
  2173. return false;
  2174. }
  2175. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2176. static void
  2177. update_state_fb(struct drm_plane *plane)
  2178. {
  2179. if (plane->fb == plane->state->fb)
  2180. return;
  2181. if (plane->state->fb)
  2182. drm_framebuffer_unreference(plane->state->fb);
  2183. plane->state->fb = plane->fb;
  2184. if (plane->state->fb)
  2185. drm_framebuffer_reference(plane->state->fb);
  2186. }
  2187. static void
  2188. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2189. struct intel_initial_plane_config *plane_config)
  2190. {
  2191. struct drm_device *dev = intel_crtc->base.dev;
  2192. struct drm_i915_private *dev_priv = dev->dev_private;
  2193. struct drm_crtc *c;
  2194. struct intel_crtc *i;
  2195. struct drm_i915_gem_object *obj;
  2196. struct drm_plane *primary = intel_crtc->base.primary;
  2197. struct drm_plane_state *plane_state = primary->state;
  2198. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2199. struct intel_plane *intel_plane = to_intel_plane(primary);
  2200. struct intel_plane_state *intel_state =
  2201. to_intel_plane_state(plane_state);
  2202. struct drm_framebuffer *fb;
  2203. if (!plane_config->fb)
  2204. return;
  2205. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2206. fb = &plane_config->fb->base;
  2207. goto valid_fb;
  2208. }
  2209. kfree(plane_config->fb);
  2210. /*
  2211. * Failed to alloc the obj, check to see if we should share
  2212. * an fb with another CRTC instead
  2213. */
  2214. for_each_crtc(dev, c) {
  2215. i = to_intel_crtc(c);
  2216. if (c == &intel_crtc->base)
  2217. continue;
  2218. if (!i->active)
  2219. continue;
  2220. fb = c->primary->fb;
  2221. if (!fb)
  2222. continue;
  2223. obj = intel_fb_obj(fb);
  2224. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2225. drm_framebuffer_reference(fb);
  2226. goto valid_fb;
  2227. }
  2228. }
  2229. /*
  2230. * We've failed to reconstruct the BIOS FB. Current display state
  2231. * indicates that the primary plane is visible, but has a NULL FB,
  2232. * which will lead to problems later if we don't fix it up. The
  2233. * simplest solution is to just disable the primary plane now and
  2234. * pretend the BIOS never had it enabled.
  2235. */
  2236. to_intel_plane_state(plane_state)->visible = false;
  2237. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2238. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2239. intel_plane->disable_plane(primary, &intel_crtc->base);
  2240. return;
  2241. valid_fb:
  2242. plane_state->src_x = 0;
  2243. plane_state->src_y = 0;
  2244. plane_state->src_w = fb->width << 16;
  2245. plane_state->src_h = fb->height << 16;
  2246. plane_state->crtc_x = 0;
  2247. plane_state->crtc_y = 0;
  2248. plane_state->crtc_w = fb->width;
  2249. plane_state->crtc_h = fb->height;
  2250. intel_state->src.x1 = plane_state->src_x;
  2251. intel_state->src.y1 = plane_state->src_y;
  2252. intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
  2253. intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
  2254. intel_state->dst.x1 = plane_state->crtc_x;
  2255. intel_state->dst.y1 = plane_state->crtc_y;
  2256. intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2257. intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2258. obj = intel_fb_obj(fb);
  2259. if (obj->tiling_mode != I915_TILING_NONE)
  2260. dev_priv->preserve_bios_swizzle = true;
  2261. drm_framebuffer_reference(fb);
  2262. primary->fb = primary->state->fb = fb;
  2263. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2264. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2265. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2266. }
  2267. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2268. const struct intel_crtc_state *crtc_state,
  2269. const struct intel_plane_state *plane_state)
  2270. {
  2271. struct drm_device *dev = primary->dev;
  2272. struct drm_i915_private *dev_priv = dev->dev_private;
  2273. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2274. struct drm_framebuffer *fb = plane_state->base.fb;
  2275. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2276. int plane = intel_crtc->plane;
  2277. u32 linear_offset;
  2278. u32 dspcntr;
  2279. i915_reg_t reg = DSPCNTR(plane);
  2280. unsigned int rotation = plane_state->base.rotation;
  2281. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2282. int x = plane_state->src.x1 >> 16;
  2283. int y = plane_state->src.y1 >> 16;
  2284. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2285. dspcntr |= DISPLAY_PLANE_ENABLE;
  2286. if (INTEL_INFO(dev)->gen < 4) {
  2287. if (intel_crtc->pipe == PIPE_B)
  2288. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2289. /* pipesrc and dspsize control the size that is scaled from,
  2290. * which should always be the user's requested size.
  2291. */
  2292. I915_WRITE(DSPSIZE(plane),
  2293. ((crtc_state->pipe_src_h - 1) << 16) |
  2294. (crtc_state->pipe_src_w - 1));
  2295. I915_WRITE(DSPPOS(plane), 0);
  2296. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2297. I915_WRITE(PRIMSIZE(plane),
  2298. ((crtc_state->pipe_src_h - 1) << 16) |
  2299. (crtc_state->pipe_src_w - 1));
  2300. I915_WRITE(PRIMPOS(plane), 0);
  2301. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2302. }
  2303. switch (fb->pixel_format) {
  2304. case DRM_FORMAT_C8:
  2305. dspcntr |= DISPPLANE_8BPP;
  2306. break;
  2307. case DRM_FORMAT_XRGB1555:
  2308. dspcntr |= DISPPLANE_BGRX555;
  2309. break;
  2310. case DRM_FORMAT_RGB565:
  2311. dspcntr |= DISPPLANE_BGRX565;
  2312. break;
  2313. case DRM_FORMAT_XRGB8888:
  2314. dspcntr |= DISPPLANE_BGRX888;
  2315. break;
  2316. case DRM_FORMAT_XBGR8888:
  2317. dspcntr |= DISPPLANE_RGBX888;
  2318. break;
  2319. case DRM_FORMAT_XRGB2101010:
  2320. dspcntr |= DISPPLANE_BGRX101010;
  2321. break;
  2322. case DRM_FORMAT_XBGR2101010:
  2323. dspcntr |= DISPPLANE_RGBX101010;
  2324. break;
  2325. default:
  2326. BUG();
  2327. }
  2328. if (INTEL_INFO(dev)->gen >= 4 &&
  2329. obj->tiling_mode != I915_TILING_NONE)
  2330. dspcntr |= DISPPLANE_TILED;
  2331. if (IS_G4X(dev))
  2332. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2333. linear_offset = y * fb->pitches[0] + x * cpp;
  2334. if (INTEL_INFO(dev)->gen >= 4) {
  2335. intel_crtc->dspaddr_offset =
  2336. intel_compute_tile_offset(&x, &y, fb, 0,
  2337. fb->pitches[0], rotation);
  2338. linear_offset -= intel_crtc->dspaddr_offset;
  2339. } else {
  2340. intel_crtc->dspaddr_offset = linear_offset;
  2341. }
  2342. if (rotation == BIT(DRM_ROTATE_180)) {
  2343. dspcntr |= DISPPLANE_ROTATE_180;
  2344. x += (crtc_state->pipe_src_w - 1);
  2345. y += (crtc_state->pipe_src_h - 1);
  2346. /* Finding the last pixel of the last line of the display
  2347. data and adding to linear_offset*/
  2348. linear_offset +=
  2349. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2350. (crtc_state->pipe_src_w - 1) * cpp;
  2351. }
  2352. intel_crtc->adjusted_x = x;
  2353. intel_crtc->adjusted_y = y;
  2354. I915_WRITE(reg, dspcntr);
  2355. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2356. if (INTEL_INFO(dev)->gen >= 4) {
  2357. I915_WRITE(DSPSURF(plane),
  2358. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2359. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2360. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2361. } else
  2362. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2363. POSTING_READ(reg);
  2364. }
  2365. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2366. struct drm_crtc *crtc)
  2367. {
  2368. struct drm_device *dev = crtc->dev;
  2369. struct drm_i915_private *dev_priv = dev->dev_private;
  2370. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2371. int plane = intel_crtc->plane;
  2372. I915_WRITE(DSPCNTR(plane), 0);
  2373. if (INTEL_INFO(dev_priv)->gen >= 4)
  2374. I915_WRITE(DSPSURF(plane), 0);
  2375. else
  2376. I915_WRITE(DSPADDR(plane), 0);
  2377. POSTING_READ(DSPCNTR(plane));
  2378. }
  2379. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2380. const struct intel_crtc_state *crtc_state,
  2381. const struct intel_plane_state *plane_state)
  2382. {
  2383. struct drm_device *dev = primary->dev;
  2384. struct drm_i915_private *dev_priv = dev->dev_private;
  2385. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2386. struct drm_framebuffer *fb = plane_state->base.fb;
  2387. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2388. int plane = intel_crtc->plane;
  2389. u32 linear_offset;
  2390. u32 dspcntr;
  2391. i915_reg_t reg = DSPCNTR(plane);
  2392. unsigned int rotation = plane_state->base.rotation;
  2393. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2394. int x = plane_state->src.x1 >> 16;
  2395. int y = plane_state->src.y1 >> 16;
  2396. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2397. dspcntr |= DISPLAY_PLANE_ENABLE;
  2398. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2399. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2400. switch (fb->pixel_format) {
  2401. case DRM_FORMAT_C8:
  2402. dspcntr |= DISPPLANE_8BPP;
  2403. break;
  2404. case DRM_FORMAT_RGB565:
  2405. dspcntr |= DISPPLANE_BGRX565;
  2406. break;
  2407. case DRM_FORMAT_XRGB8888:
  2408. dspcntr |= DISPPLANE_BGRX888;
  2409. break;
  2410. case DRM_FORMAT_XBGR8888:
  2411. dspcntr |= DISPPLANE_RGBX888;
  2412. break;
  2413. case DRM_FORMAT_XRGB2101010:
  2414. dspcntr |= DISPPLANE_BGRX101010;
  2415. break;
  2416. case DRM_FORMAT_XBGR2101010:
  2417. dspcntr |= DISPPLANE_RGBX101010;
  2418. break;
  2419. default:
  2420. BUG();
  2421. }
  2422. if (obj->tiling_mode != I915_TILING_NONE)
  2423. dspcntr |= DISPPLANE_TILED;
  2424. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2425. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2426. linear_offset = y * fb->pitches[0] + x * cpp;
  2427. intel_crtc->dspaddr_offset =
  2428. intel_compute_tile_offset(&x, &y, fb, 0,
  2429. fb->pitches[0], rotation);
  2430. linear_offset -= intel_crtc->dspaddr_offset;
  2431. if (rotation == BIT(DRM_ROTATE_180)) {
  2432. dspcntr |= DISPPLANE_ROTATE_180;
  2433. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2434. x += (crtc_state->pipe_src_w - 1);
  2435. y += (crtc_state->pipe_src_h - 1);
  2436. /* Finding the last pixel of the last line of the display
  2437. data and adding to linear_offset*/
  2438. linear_offset +=
  2439. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2440. (crtc_state->pipe_src_w - 1) * cpp;
  2441. }
  2442. }
  2443. intel_crtc->adjusted_x = x;
  2444. intel_crtc->adjusted_y = y;
  2445. I915_WRITE(reg, dspcntr);
  2446. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2447. I915_WRITE(DSPSURF(plane),
  2448. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2449. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2450. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2451. } else {
  2452. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2453. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2454. }
  2455. POSTING_READ(reg);
  2456. }
  2457. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2458. uint64_t fb_modifier, uint32_t pixel_format)
  2459. {
  2460. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2461. return 64;
  2462. } else {
  2463. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2464. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2465. }
  2466. }
  2467. u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
  2468. struct drm_i915_gem_object *obj,
  2469. unsigned int plane)
  2470. {
  2471. struct i915_ggtt_view view;
  2472. struct i915_vma *vma;
  2473. u64 offset;
  2474. intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
  2475. intel_plane->base.state->rotation);
  2476. vma = i915_gem_obj_to_ggtt_view(obj, &view);
  2477. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2478. view.type))
  2479. return -1;
  2480. offset = vma->node.start;
  2481. if (plane == 1) {
  2482. offset += vma->ggtt_view.params.rotated.uv_start_page *
  2483. PAGE_SIZE;
  2484. }
  2485. WARN_ON(upper_32_bits(offset));
  2486. return lower_32_bits(offset);
  2487. }
  2488. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2489. {
  2490. struct drm_device *dev = intel_crtc->base.dev;
  2491. struct drm_i915_private *dev_priv = dev->dev_private;
  2492. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2493. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2494. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2495. }
  2496. /*
  2497. * This function detaches (aka. unbinds) unused scalers in hardware
  2498. */
  2499. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2500. {
  2501. struct intel_crtc_scaler_state *scaler_state;
  2502. int i;
  2503. scaler_state = &intel_crtc->config->scaler_state;
  2504. /* loop through and disable scalers that aren't in use */
  2505. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2506. if (!scaler_state->scalers[i].in_use)
  2507. skl_detach_scaler(intel_crtc, i);
  2508. }
  2509. }
  2510. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2511. {
  2512. switch (pixel_format) {
  2513. case DRM_FORMAT_C8:
  2514. return PLANE_CTL_FORMAT_INDEXED;
  2515. case DRM_FORMAT_RGB565:
  2516. return PLANE_CTL_FORMAT_RGB_565;
  2517. case DRM_FORMAT_XBGR8888:
  2518. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2519. case DRM_FORMAT_XRGB8888:
  2520. return PLANE_CTL_FORMAT_XRGB_8888;
  2521. /*
  2522. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2523. * to be already pre-multiplied. We need to add a knob (or a different
  2524. * DRM_FORMAT) for user-space to configure that.
  2525. */
  2526. case DRM_FORMAT_ABGR8888:
  2527. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2528. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2529. case DRM_FORMAT_ARGB8888:
  2530. return PLANE_CTL_FORMAT_XRGB_8888 |
  2531. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2532. case DRM_FORMAT_XRGB2101010:
  2533. return PLANE_CTL_FORMAT_XRGB_2101010;
  2534. case DRM_FORMAT_XBGR2101010:
  2535. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2536. case DRM_FORMAT_YUYV:
  2537. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2538. case DRM_FORMAT_YVYU:
  2539. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2540. case DRM_FORMAT_UYVY:
  2541. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2542. case DRM_FORMAT_VYUY:
  2543. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2544. default:
  2545. MISSING_CASE(pixel_format);
  2546. }
  2547. return 0;
  2548. }
  2549. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2550. {
  2551. switch (fb_modifier) {
  2552. case DRM_FORMAT_MOD_NONE:
  2553. break;
  2554. case I915_FORMAT_MOD_X_TILED:
  2555. return PLANE_CTL_TILED_X;
  2556. case I915_FORMAT_MOD_Y_TILED:
  2557. return PLANE_CTL_TILED_Y;
  2558. case I915_FORMAT_MOD_Yf_TILED:
  2559. return PLANE_CTL_TILED_YF;
  2560. default:
  2561. MISSING_CASE(fb_modifier);
  2562. }
  2563. return 0;
  2564. }
  2565. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2566. {
  2567. switch (rotation) {
  2568. case BIT(DRM_ROTATE_0):
  2569. break;
  2570. /*
  2571. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2572. * while i915 HW rotation is clockwise, thats why this swapping.
  2573. */
  2574. case BIT(DRM_ROTATE_90):
  2575. return PLANE_CTL_ROTATE_270;
  2576. case BIT(DRM_ROTATE_180):
  2577. return PLANE_CTL_ROTATE_180;
  2578. case BIT(DRM_ROTATE_270):
  2579. return PLANE_CTL_ROTATE_90;
  2580. default:
  2581. MISSING_CASE(rotation);
  2582. }
  2583. return 0;
  2584. }
  2585. static void skylake_update_primary_plane(struct drm_plane *plane,
  2586. const struct intel_crtc_state *crtc_state,
  2587. const struct intel_plane_state *plane_state)
  2588. {
  2589. struct drm_device *dev = plane->dev;
  2590. struct drm_i915_private *dev_priv = dev->dev_private;
  2591. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2592. struct drm_framebuffer *fb = plane_state->base.fb;
  2593. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2594. int pipe = intel_crtc->pipe;
  2595. u32 plane_ctl, stride_div, stride;
  2596. u32 tile_height, plane_offset, plane_size;
  2597. unsigned int rotation = plane_state->base.rotation;
  2598. int x_offset, y_offset;
  2599. u32 surf_addr;
  2600. int scaler_id = plane_state->scaler_id;
  2601. int src_x = plane_state->src.x1 >> 16;
  2602. int src_y = plane_state->src.y1 >> 16;
  2603. int src_w = drm_rect_width(&plane_state->src) >> 16;
  2604. int src_h = drm_rect_height(&plane_state->src) >> 16;
  2605. int dst_x = plane_state->dst.x1;
  2606. int dst_y = plane_state->dst.y1;
  2607. int dst_w = drm_rect_width(&plane_state->dst);
  2608. int dst_h = drm_rect_height(&plane_state->dst);
  2609. plane_ctl = PLANE_CTL_ENABLE |
  2610. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2611. PLANE_CTL_PIPE_CSC_ENABLE;
  2612. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2613. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2614. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2615. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2616. stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2617. fb->pixel_format);
  2618. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2619. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2620. if (intel_rotation_90_or_270(rotation)) {
  2621. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2622. /* stride = Surface height in tiles */
  2623. tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2624. stride = DIV_ROUND_UP(fb->height, tile_height);
  2625. x_offset = stride * tile_height - src_y - src_h;
  2626. y_offset = src_x;
  2627. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2628. } else {
  2629. stride = fb->pitches[0] / stride_div;
  2630. x_offset = src_x;
  2631. y_offset = src_y;
  2632. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2633. }
  2634. plane_offset = y_offset << 16 | x_offset;
  2635. intel_crtc->adjusted_x = x_offset;
  2636. intel_crtc->adjusted_y = y_offset;
  2637. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2638. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2639. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2640. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2641. if (scaler_id >= 0) {
  2642. uint32_t ps_ctrl = 0;
  2643. WARN_ON(!dst_w || !dst_h);
  2644. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2645. crtc_state->scaler_state.scalers[scaler_id].mode;
  2646. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2647. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2648. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2649. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2650. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2651. } else {
  2652. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2653. }
  2654. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2655. POSTING_READ(PLANE_SURF(pipe, 0));
  2656. }
  2657. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2658. struct drm_crtc *crtc)
  2659. {
  2660. struct drm_device *dev = crtc->dev;
  2661. struct drm_i915_private *dev_priv = dev->dev_private;
  2662. int pipe = to_intel_crtc(crtc)->pipe;
  2663. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2664. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2665. POSTING_READ(PLANE_SURF(pipe, 0));
  2666. }
  2667. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2668. static int
  2669. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2670. int x, int y, enum mode_set_atomic state)
  2671. {
  2672. /* Support for kgdboc is disabled, this needs a major rework. */
  2673. DRM_ERROR("legacy panic handler not supported any more.\n");
  2674. return -ENODEV;
  2675. }
  2676. static void intel_complete_page_flips(struct drm_device *dev)
  2677. {
  2678. struct drm_crtc *crtc;
  2679. for_each_crtc(dev, crtc) {
  2680. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2681. enum plane plane = intel_crtc->plane;
  2682. intel_prepare_page_flip(dev, plane);
  2683. intel_finish_page_flip_plane(dev, plane);
  2684. }
  2685. }
  2686. static void intel_update_primary_planes(struct drm_device *dev)
  2687. {
  2688. struct drm_crtc *crtc;
  2689. for_each_crtc(dev, crtc) {
  2690. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2691. struct intel_plane_state *plane_state;
  2692. drm_modeset_lock_crtc(crtc, &plane->base);
  2693. plane_state = to_intel_plane_state(plane->base.state);
  2694. if (plane_state->visible)
  2695. plane->update_plane(&plane->base,
  2696. to_intel_crtc_state(crtc->state),
  2697. plane_state);
  2698. drm_modeset_unlock_crtc(crtc);
  2699. }
  2700. }
  2701. void intel_prepare_reset(struct drm_device *dev)
  2702. {
  2703. /* no reset support for gen2 */
  2704. if (IS_GEN2(dev))
  2705. return;
  2706. /* reset doesn't touch the display */
  2707. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2708. return;
  2709. drm_modeset_lock_all(dev);
  2710. /*
  2711. * Disabling the crtcs gracefully seems nicer. Also the
  2712. * g33 docs say we should at least disable all the planes.
  2713. */
  2714. intel_display_suspend(dev);
  2715. }
  2716. void intel_finish_reset(struct drm_device *dev)
  2717. {
  2718. struct drm_i915_private *dev_priv = to_i915(dev);
  2719. /*
  2720. * Flips in the rings will be nuked by the reset,
  2721. * so complete all pending flips so that user space
  2722. * will get its events and not get stuck.
  2723. */
  2724. intel_complete_page_flips(dev);
  2725. /* no reset support for gen2 */
  2726. if (IS_GEN2(dev))
  2727. return;
  2728. /* reset doesn't touch the display */
  2729. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2730. /*
  2731. * Flips in the rings have been nuked by the reset,
  2732. * so update the base address of all primary
  2733. * planes to the the last fb to make sure we're
  2734. * showing the correct fb after a reset.
  2735. *
  2736. * FIXME: Atomic will make this obsolete since we won't schedule
  2737. * CS-based flips (which might get lost in gpu resets) any more.
  2738. */
  2739. intel_update_primary_planes(dev);
  2740. return;
  2741. }
  2742. /*
  2743. * The display has been reset as well,
  2744. * so need a full re-initialization.
  2745. */
  2746. intel_runtime_pm_disable_interrupts(dev_priv);
  2747. intel_runtime_pm_enable_interrupts(dev_priv);
  2748. intel_modeset_init_hw(dev);
  2749. spin_lock_irq(&dev_priv->irq_lock);
  2750. if (dev_priv->display.hpd_irq_setup)
  2751. dev_priv->display.hpd_irq_setup(dev);
  2752. spin_unlock_irq(&dev_priv->irq_lock);
  2753. intel_display_resume(dev);
  2754. intel_hpd_init(dev_priv);
  2755. drm_modeset_unlock_all(dev);
  2756. }
  2757. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2758. {
  2759. struct drm_device *dev = crtc->dev;
  2760. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2761. unsigned reset_counter;
  2762. bool pending;
  2763. reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
  2764. if (intel_crtc->reset_counter != reset_counter)
  2765. return false;
  2766. spin_lock_irq(&dev->event_lock);
  2767. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2768. spin_unlock_irq(&dev->event_lock);
  2769. return pending;
  2770. }
  2771. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2772. struct intel_crtc_state *old_crtc_state)
  2773. {
  2774. struct drm_device *dev = crtc->base.dev;
  2775. struct drm_i915_private *dev_priv = dev->dev_private;
  2776. struct intel_crtc_state *pipe_config =
  2777. to_intel_crtc_state(crtc->base.state);
  2778. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2779. crtc->base.mode = crtc->base.state->mode;
  2780. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2781. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2782. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2783. /*
  2784. * Update pipe size and adjust fitter if needed: the reason for this is
  2785. * that in compute_mode_changes we check the native mode (not the pfit
  2786. * mode) to see if we can flip rather than do a full mode set. In the
  2787. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2788. * pfit state, we'll end up with a big fb scanned out into the wrong
  2789. * sized surface.
  2790. */
  2791. I915_WRITE(PIPESRC(crtc->pipe),
  2792. ((pipe_config->pipe_src_w - 1) << 16) |
  2793. (pipe_config->pipe_src_h - 1));
  2794. /* on skylake this is done by detaching scalers */
  2795. if (INTEL_INFO(dev)->gen >= 9) {
  2796. skl_detach_scalers(crtc);
  2797. if (pipe_config->pch_pfit.enabled)
  2798. skylake_pfit_enable(crtc);
  2799. } else if (HAS_PCH_SPLIT(dev)) {
  2800. if (pipe_config->pch_pfit.enabled)
  2801. ironlake_pfit_enable(crtc);
  2802. else if (old_crtc_state->pch_pfit.enabled)
  2803. ironlake_pfit_disable(crtc, true);
  2804. }
  2805. }
  2806. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2807. {
  2808. struct drm_device *dev = crtc->dev;
  2809. struct drm_i915_private *dev_priv = dev->dev_private;
  2810. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2811. int pipe = intel_crtc->pipe;
  2812. i915_reg_t reg;
  2813. u32 temp;
  2814. /* enable normal train */
  2815. reg = FDI_TX_CTL(pipe);
  2816. temp = I915_READ(reg);
  2817. if (IS_IVYBRIDGE(dev)) {
  2818. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2819. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2820. } else {
  2821. temp &= ~FDI_LINK_TRAIN_NONE;
  2822. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2823. }
  2824. I915_WRITE(reg, temp);
  2825. reg = FDI_RX_CTL(pipe);
  2826. temp = I915_READ(reg);
  2827. if (HAS_PCH_CPT(dev)) {
  2828. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2829. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2830. } else {
  2831. temp &= ~FDI_LINK_TRAIN_NONE;
  2832. temp |= FDI_LINK_TRAIN_NONE;
  2833. }
  2834. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2835. /* wait one idle pattern time */
  2836. POSTING_READ(reg);
  2837. udelay(1000);
  2838. /* IVB wants error correction enabled */
  2839. if (IS_IVYBRIDGE(dev))
  2840. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2841. FDI_FE_ERRC_ENABLE);
  2842. }
  2843. /* The FDI link training functions for ILK/Ibexpeak. */
  2844. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2845. {
  2846. struct drm_device *dev = crtc->dev;
  2847. struct drm_i915_private *dev_priv = dev->dev_private;
  2848. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2849. int pipe = intel_crtc->pipe;
  2850. i915_reg_t reg;
  2851. u32 temp, tries;
  2852. /* FDI needs bits from pipe first */
  2853. assert_pipe_enabled(dev_priv, pipe);
  2854. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2855. for train result */
  2856. reg = FDI_RX_IMR(pipe);
  2857. temp = I915_READ(reg);
  2858. temp &= ~FDI_RX_SYMBOL_LOCK;
  2859. temp &= ~FDI_RX_BIT_LOCK;
  2860. I915_WRITE(reg, temp);
  2861. I915_READ(reg);
  2862. udelay(150);
  2863. /* enable CPU FDI TX and PCH FDI RX */
  2864. reg = FDI_TX_CTL(pipe);
  2865. temp = I915_READ(reg);
  2866. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2867. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2868. temp &= ~FDI_LINK_TRAIN_NONE;
  2869. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2870. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2871. reg = FDI_RX_CTL(pipe);
  2872. temp = I915_READ(reg);
  2873. temp &= ~FDI_LINK_TRAIN_NONE;
  2874. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2875. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2876. POSTING_READ(reg);
  2877. udelay(150);
  2878. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2879. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2880. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2881. FDI_RX_PHASE_SYNC_POINTER_EN);
  2882. reg = FDI_RX_IIR(pipe);
  2883. for (tries = 0; tries < 5; tries++) {
  2884. temp = I915_READ(reg);
  2885. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2886. if ((temp & FDI_RX_BIT_LOCK)) {
  2887. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2888. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2889. break;
  2890. }
  2891. }
  2892. if (tries == 5)
  2893. DRM_ERROR("FDI train 1 fail!\n");
  2894. /* Train 2 */
  2895. reg = FDI_TX_CTL(pipe);
  2896. temp = I915_READ(reg);
  2897. temp &= ~FDI_LINK_TRAIN_NONE;
  2898. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2899. I915_WRITE(reg, temp);
  2900. reg = FDI_RX_CTL(pipe);
  2901. temp = I915_READ(reg);
  2902. temp &= ~FDI_LINK_TRAIN_NONE;
  2903. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2904. I915_WRITE(reg, temp);
  2905. POSTING_READ(reg);
  2906. udelay(150);
  2907. reg = FDI_RX_IIR(pipe);
  2908. for (tries = 0; tries < 5; tries++) {
  2909. temp = I915_READ(reg);
  2910. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2911. if (temp & FDI_RX_SYMBOL_LOCK) {
  2912. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2913. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2914. break;
  2915. }
  2916. }
  2917. if (tries == 5)
  2918. DRM_ERROR("FDI train 2 fail!\n");
  2919. DRM_DEBUG_KMS("FDI train done\n");
  2920. }
  2921. static const int snb_b_fdi_train_param[] = {
  2922. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2923. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2924. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2925. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2926. };
  2927. /* The FDI link training functions for SNB/Cougarpoint. */
  2928. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2929. {
  2930. struct drm_device *dev = crtc->dev;
  2931. struct drm_i915_private *dev_priv = dev->dev_private;
  2932. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2933. int pipe = intel_crtc->pipe;
  2934. i915_reg_t reg;
  2935. u32 temp, i, retry;
  2936. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2937. for train result */
  2938. reg = FDI_RX_IMR(pipe);
  2939. temp = I915_READ(reg);
  2940. temp &= ~FDI_RX_SYMBOL_LOCK;
  2941. temp &= ~FDI_RX_BIT_LOCK;
  2942. I915_WRITE(reg, temp);
  2943. POSTING_READ(reg);
  2944. udelay(150);
  2945. /* enable CPU FDI TX and PCH FDI RX */
  2946. reg = FDI_TX_CTL(pipe);
  2947. temp = I915_READ(reg);
  2948. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2949. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2950. temp &= ~FDI_LINK_TRAIN_NONE;
  2951. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2952. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2953. /* SNB-B */
  2954. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2955. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2956. I915_WRITE(FDI_RX_MISC(pipe),
  2957. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2958. reg = FDI_RX_CTL(pipe);
  2959. temp = I915_READ(reg);
  2960. if (HAS_PCH_CPT(dev)) {
  2961. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2962. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2963. } else {
  2964. temp &= ~FDI_LINK_TRAIN_NONE;
  2965. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2966. }
  2967. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2968. POSTING_READ(reg);
  2969. udelay(150);
  2970. for (i = 0; i < 4; i++) {
  2971. reg = FDI_TX_CTL(pipe);
  2972. temp = I915_READ(reg);
  2973. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2974. temp |= snb_b_fdi_train_param[i];
  2975. I915_WRITE(reg, temp);
  2976. POSTING_READ(reg);
  2977. udelay(500);
  2978. for (retry = 0; retry < 5; retry++) {
  2979. reg = FDI_RX_IIR(pipe);
  2980. temp = I915_READ(reg);
  2981. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2982. if (temp & FDI_RX_BIT_LOCK) {
  2983. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2984. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2985. break;
  2986. }
  2987. udelay(50);
  2988. }
  2989. if (retry < 5)
  2990. break;
  2991. }
  2992. if (i == 4)
  2993. DRM_ERROR("FDI train 1 fail!\n");
  2994. /* Train 2 */
  2995. reg = FDI_TX_CTL(pipe);
  2996. temp = I915_READ(reg);
  2997. temp &= ~FDI_LINK_TRAIN_NONE;
  2998. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2999. if (IS_GEN6(dev)) {
  3000. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3001. /* SNB-B */
  3002. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3003. }
  3004. I915_WRITE(reg, temp);
  3005. reg = FDI_RX_CTL(pipe);
  3006. temp = I915_READ(reg);
  3007. if (HAS_PCH_CPT(dev)) {
  3008. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3009. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3010. } else {
  3011. temp &= ~FDI_LINK_TRAIN_NONE;
  3012. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3013. }
  3014. I915_WRITE(reg, temp);
  3015. POSTING_READ(reg);
  3016. udelay(150);
  3017. for (i = 0; i < 4; i++) {
  3018. reg = FDI_TX_CTL(pipe);
  3019. temp = I915_READ(reg);
  3020. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3021. temp |= snb_b_fdi_train_param[i];
  3022. I915_WRITE(reg, temp);
  3023. POSTING_READ(reg);
  3024. udelay(500);
  3025. for (retry = 0; retry < 5; retry++) {
  3026. reg = FDI_RX_IIR(pipe);
  3027. temp = I915_READ(reg);
  3028. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3029. if (temp & FDI_RX_SYMBOL_LOCK) {
  3030. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3031. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3032. break;
  3033. }
  3034. udelay(50);
  3035. }
  3036. if (retry < 5)
  3037. break;
  3038. }
  3039. if (i == 4)
  3040. DRM_ERROR("FDI train 2 fail!\n");
  3041. DRM_DEBUG_KMS("FDI train done.\n");
  3042. }
  3043. /* Manual link training for Ivy Bridge A0 parts */
  3044. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3045. {
  3046. struct drm_device *dev = crtc->dev;
  3047. struct drm_i915_private *dev_priv = dev->dev_private;
  3048. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3049. int pipe = intel_crtc->pipe;
  3050. i915_reg_t reg;
  3051. u32 temp, i, j;
  3052. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3053. for train result */
  3054. reg = FDI_RX_IMR(pipe);
  3055. temp = I915_READ(reg);
  3056. temp &= ~FDI_RX_SYMBOL_LOCK;
  3057. temp &= ~FDI_RX_BIT_LOCK;
  3058. I915_WRITE(reg, temp);
  3059. POSTING_READ(reg);
  3060. udelay(150);
  3061. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3062. I915_READ(FDI_RX_IIR(pipe)));
  3063. /* Try each vswing and preemphasis setting twice before moving on */
  3064. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3065. /* disable first in case we need to retry */
  3066. reg = FDI_TX_CTL(pipe);
  3067. temp = I915_READ(reg);
  3068. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3069. temp &= ~FDI_TX_ENABLE;
  3070. I915_WRITE(reg, temp);
  3071. reg = FDI_RX_CTL(pipe);
  3072. temp = I915_READ(reg);
  3073. temp &= ~FDI_LINK_TRAIN_AUTO;
  3074. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3075. temp &= ~FDI_RX_ENABLE;
  3076. I915_WRITE(reg, temp);
  3077. /* enable CPU FDI TX and PCH FDI RX */
  3078. reg = FDI_TX_CTL(pipe);
  3079. temp = I915_READ(reg);
  3080. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3081. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3082. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3083. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3084. temp |= snb_b_fdi_train_param[j/2];
  3085. temp |= FDI_COMPOSITE_SYNC;
  3086. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3087. I915_WRITE(FDI_RX_MISC(pipe),
  3088. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3089. reg = FDI_RX_CTL(pipe);
  3090. temp = I915_READ(reg);
  3091. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3092. temp |= FDI_COMPOSITE_SYNC;
  3093. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3094. POSTING_READ(reg);
  3095. udelay(1); /* should be 0.5us */
  3096. for (i = 0; i < 4; i++) {
  3097. reg = FDI_RX_IIR(pipe);
  3098. temp = I915_READ(reg);
  3099. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3100. if (temp & FDI_RX_BIT_LOCK ||
  3101. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3102. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3103. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3104. i);
  3105. break;
  3106. }
  3107. udelay(1); /* should be 0.5us */
  3108. }
  3109. if (i == 4) {
  3110. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3111. continue;
  3112. }
  3113. /* Train 2 */
  3114. reg = FDI_TX_CTL(pipe);
  3115. temp = I915_READ(reg);
  3116. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3117. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3118. I915_WRITE(reg, temp);
  3119. reg = FDI_RX_CTL(pipe);
  3120. temp = I915_READ(reg);
  3121. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3122. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3123. I915_WRITE(reg, temp);
  3124. POSTING_READ(reg);
  3125. udelay(2); /* should be 1.5us */
  3126. for (i = 0; i < 4; i++) {
  3127. reg = FDI_RX_IIR(pipe);
  3128. temp = I915_READ(reg);
  3129. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3130. if (temp & FDI_RX_SYMBOL_LOCK ||
  3131. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3132. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3133. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3134. i);
  3135. goto train_done;
  3136. }
  3137. udelay(2); /* should be 1.5us */
  3138. }
  3139. if (i == 4)
  3140. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3141. }
  3142. train_done:
  3143. DRM_DEBUG_KMS("FDI train done.\n");
  3144. }
  3145. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3146. {
  3147. struct drm_device *dev = intel_crtc->base.dev;
  3148. struct drm_i915_private *dev_priv = dev->dev_private;
  3149. int pipe = intel_crtc->pipe;
  3150. i915_reg_t reg;
  3151. u32 temp;
  3152. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3153. reg = FDI_RX_CTL(pipe);
  3154. temp = I915_READ(reg);
  3155. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3156. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3157. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3158. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3159. POSTING_READ(reg);
  3160. udelay(200);
  3161. /* Switch from Rawclk to PCDclk */
  3162. temp = I915_READ(reg);
  3163. I915_WRITE(reg, temp | FDI_PCDCLK);
  3164. POSTING_READ(reg);
  3165. udelay(200);
  3166. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3167. reg = FDI_TX_CTL(pipe);
  3168. temp = I915_READ(reg);
  3169. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3170. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3171. POSTING_READ(reg);
  3172. udelay(100);
  3173. }
  3174. }
  3175. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3176. {
  3177. struct drm_device *dev = intel_crtc->base.dev;
  3178. struct drm_i915_private *dev_priv = dev->dev_private;
  3179. int pipe = intel_crtc->pipe;
  3180. i915_reg_t reg;
  3181. u32 temp;
  3182. /* Switch from PCDclk to Rawclk */
  3183. reg = FDI_RX_CTL(pipe);
  3184. temp = I915_READ(reg);
  3185. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3186. /* Disable CPU FDI TX PLL */
  3187. reg = FDI_TX_CTL(pipe);
  3188. temp = I915_READ(reg);
  3189. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3190. POSTING_READ(reg);
  3191. udelay(100);
  3192. reg = FDI_RX_CTL(pipe);
  3193. temp = I915_READ(reg);
  3194. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3195. /* Wait for the clocks to turn off. */
  3196. POSTING_READ(reg);
  3197. udelay(100);
  3198. }
  3199. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3200. {
  3201. struct drm_device *dev = crtc->dev;
  3202. struct drm_i915_private *dev_priv = dev->dev_private;
  3203. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3204. int pipe = intel_crtc->pipe;
  3205. i915_reg_t reg;
  3206. u32 temp;
  3207. /* disable CPU FDI tx and PCH FDI rx */
  3208. reg = FDI_TX_CTL(pipe);
  3209. temp = I915_READ(reg);
  3210. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3211. POSTING_READ(reg);
  3212. reg = FDI_RX_CTL(pipe);
  3213. temp = I915_READ(reg);
  3214. temp &= ~(0x7 << 16);
  3215. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3216. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3217. POSTING_READ(reg);
  3218. udelay(100);
  3219. /* Ironlake workaround, disable clock pointer after downing FDI */
  3220. if (HAS_PCH_IBX(dev))
  3221. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3222. /* still set train pattern 1 */
  3223. reg = FDI_TX_CTL(pipe);
  3224. temp = I915_READ(reg);
  3225. temp &= ~FDI_LINK_TRAIN_NONE;
  3226. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3227. I915_WRITE(reg, temp);
  3228. reg = FDI_RX_CTL(pipe);
  3229. temp = I915_READ(reg);
  3230. if (HAS_PCH_CPT(dev)) {
  3231. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3232. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3233. } else {
  3234. temp &= ~FDI_LINK_TRAIN_NONE;
  3235. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3236. }
  3237. /* BPC in FDI rx is consistent with that in PIPECONF */
  3238. temp &= ~(0x07 << 16);
  3239. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3240. I915_WRITE(reg, temp);
  3241. POSTING_READ(reg);
  3242. udelay(100);
  3243. }
  3244. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3245. {
  3246. struct intel_crtc *crtc;
  3247. /* Note that we don't need to be called with mode_config.lock here
  3248. * as our list of CRTC objects is static for the lifetime of the
  3249. * device and so cannot disappear as we iterate. Similarly, we can
  3250. * happily treat the predicates as racy, atomic checks as userspace
  3251. * cannot claim and pin a new fb without at least acquring the
  3252. * struct_mutex and so serialising with us.
  3253. */
  3254. for_each_intel_crtc(dev, crtc) {
  3255. if (atomic_read(&crtc->unpin_work_count) == 0)
  3256. continue;
  3257. if (crtc->unpin_work)
  3258. intel_wait_for_vblank(dev, crtc->pipe);
  3259. return true;
  3260. }
  3261. return false;
  3262. }
  3263. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3264. {
  3265. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3266. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3267. /* ensure that the unpin work is consistent wrt ->pending. */
  3268. smp_rmb();
  3269. intel_crtc->unpin_work = NULL;
  3270. if (work->event)
  3271. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3272. drm_crtc_vblank_put(&intel_crtc->base);
  3273. wake_up_all(&dev_priv->pending_flip_queue);
  3274. queue_work(dev_priv->wq, &work->work);
  3275. trace_i915_flip_complete(intel_crtc->plane,
  3276. work->pending_flip_obj);
  3277. }
  3278. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3279. {
  3280. struct drm_device *dev = crtc->dev;
  3281. struct drm_i915_private *dev_priv = dev->dev_private;
  3282. long ret;
  3283. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3284. ret = wait_event_interruptible_timeout(
  3285. dev_priv->pending_flip_queue,
  3286. !intel_crtc_has_pending_flip(crtc),
  3287. 60*HZ);
  3288. if (ret < 0)
  3289. return ret;
  3290. if (ret == 0) {
  3291. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3292. spin_lock_irq(&dev->event_lock);
  3293. if (intel_crtc->unpin_work) {
  3294. WARN_ONCE(1, "Removing stuck page flip\n");
  3295. page_flip_completed(intel_crtc);
  3296. }
  3297. spin_unlock_irq(&dev->event_lock);
  3298. }
  3299. return 0;
  3300. }
  3301. static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3302. {
  3303. u32 temp;
  3304. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3305. mutex_lock(&dev_priv->sb_lock);
  3306. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3307. temp |= SBI_SSCCTL_DISABLE;
  3308. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3309. mutex_unlock(&dev_priv->sb_lock);
  3310. }
  3311. /* Program iCLKIP clock to the desired frequency */
  3312. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3313. {
  3314. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3315. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3316. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3317. u32 temp;
  3318. lpt_disable_iclkip(dev_priv);
  3319. /* The iCLK virtual clock root frequency is in MHz,
  3320. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3321. * divisors, it is necessary to divide one by another, so we
  3322. * convert the virtual clock precision to KHz here for higher
  3323. * precision.
  3324. */
  3325. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3326. u32 iclk_virtual_root_freq = 172800 * 1000;
  3327. u32 iclk_pi_range = 64;
  3328. u32 desired_divisor;
  3329. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3330. clock << auxdiv);
  3331. divsel = (desired_divisor / iclk_pi_range) - 2;
  3332. phaseinc = desired_divisor % iclk_pi_range;
  3333. /*
  3334. * Near 20MHz is a corner case which is
  3335. * out of range for the 7-bit divisor
  3336. */
  3337. if (divsel <= 0x7f)
  3338. break;
  3339. }
  3340. /* This should not happen with any sane values */
  3341. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3342. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3343. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3344. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3345. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3346. clock,
  3347. auxdiv,
  3348. divsel,
  3349. phasedir,
  3350. phaseinc);
  3351. mutex_lock(&dev_priv->sb_lock);
  3352. /* Program SSCDIVINTPHASE6 */
  3353. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3354. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3355. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3356. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3357. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3358. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3359. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3360. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3361. /* Program SSCAUXDIV */
  3362. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3363. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3364. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3365. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3366. /* Enable modulator and associated divider */
  3367. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3368. temp &= ~SBI_SSCCTL_DISABLE;
  3369. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3370. mutex_unlock(&dev_priv->sb_lock);
  3371. /* Wait for initialization time */
  3372. udelay(24);
  3373. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3374. }
  3375. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3376. {
  3377. u32 divsel, phaseinc, auxdiv;
  3378. u32 iclk_virtual_root_freq = 172800 * 1000;
  3379. u32 iclk_pi_range = 64;
  3380. u32 desired_divisor;
  3381. u32 temp;
  3382. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3383. return 0;
  3384. mutex_lock(&dev_priv->sb_lock);
  3385. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3386. if (temp & SBI_SSCCTL_DISABLE) {
  3387. mutex_unlock(&dev_priv->sb_lock);
  3388. return 0;
  3389. }
  3390. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3391. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3392. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3393. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3394. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3395. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3396. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3397. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3398. mutex_unlock(&dev_priv->sb_lock);
  3399. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3400. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3401. desired_divisor << auxdiv);
  3402. }
  3403. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3404. enum pipe pch_transcoder)
  3405. {
  3406. struct drm_device *dev = crtc->base.dev;
  3407. struct drm_i915_private *dev_priv = dev->dev_private;
  3408. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3409. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3410. I915_READ(HTOTAL(cpu_transcoder)));
  3411. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3412. I915_READ(HBLANK(cpu_transcoder)));
  3413. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3414. I915_READ(HSYNC(cpu_transcoder)));
  3415. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3416. I915_READ(VTOTAL(cpu_transcoder)));
  3417. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3418. I915_READ(VBLANK(cpu_transcoder)));
  3419. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3420. I915_READ(VSYNC(cpu_transcoder)));
  3421. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3422. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3423. }
  3424. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3425. {
  3426. struct drm_i915_private *dev_priv = dev->dev_private;
  3427. uint32_t temp;
  3428. temp = I915_READ(SOUTH_CHICKEN1);
  3429. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3430. return;
  3431. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3432. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3433. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3434. if (enable)
  3435. temp |= FDI_BC_BIFURCATION_SELECT;
  3436. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3437. I915_WRITE(SOUTH_CHICKEN1, temp);
  3438. POSTING_READ(SOUTH_CHICKEN1);
  3439. }
  3440. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3441. {
  3442. struct drm_device *dev = intel_crtc->base.dev;
  3443. switch (intel_crtc->pipe) {
  3444. case PIPE_A:
  3445. break;
  3446. case PIPE_B:
  3447. if (intel_crtc->config->fdi_lanes > 2)
  3448. cpt_set_fdi_bc_bifurcation(dev, false);
  3449. else
  3450. cpt_set_fdi_bc_bifurcation(dev, true);
  3451. break;
  3452. case PIPE_C:
  3453. cpt_set_fdi_bc_bifurcation(dev, true);
  3454. break;
  3455. default:
  3456. BUG();
  3457. }
  3458. }
  3459. /* Return which DP Port should be selected for Transcoder DP control */
  3460. static enum port
  3461. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3462. {
  3463. struct drm_device *dev = crtc->dev;
  3464. struct intel_encoder *encoder;
  3465. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3466. if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
  3467. encoder->type == INTEL_OUTPUT_EDP)
  3468. return enc_to_dig_port(&encoder->base)->port;
  3469. }
  3470. return -1;
  3471. }
  3472. /*
  3473. * Enable PCH resources required for PCH ports:
  3474. * - PCH PLLs
  3475. * - FDI training & RX/TX
  3476. * - update transcoder timings
  3477. * - DP transcoding bits
  3478. * - transcoder
  3479. */
  3480. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3481. {
  3482. struct drm_device *dev = crtc->dev;
  3483. struct drm_i915_private *dev_priv = dev->dev_private;
  3484. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3485. int pipe = intel_crtc->pipe;
  3486. u32 temp;
  3487. assert_pch_transcoder_disabled(dev_priv, pipe);
  3488. if (IS_IVYBRIDGE(dev))
  3489. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3490. /* Write the TU size bits before fdi link training, so that error
  3491. * detection works. */
  3492. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3493. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3494. /* For PCH output, training FDI link */
  3495. dev_priv->display.fdi_link_train(crtc);
  3496. /* We need to program the right clock selection before writing the pixel
  3497. * mutliplier into the DPLL. */
  3498. if (HAS_PCH_CPT(dev)) {
  3499. u32 sel;
  3500. temp = I915_READ(PCH_DPLL_SEL);
  3501. temp |= TRANS_DPLL_ENABLE(pipe);
  3502. sel = TRANS_DPLLB_SEL(pipe);
  3503. if (intel_crtc->config->shared_dpll ==
  3504. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3505. temp |= sel;
  3506. else
  3507. temp &= ~sel;
  3508. I915_WRITE(PCH_DPLL_SEL, temp);
  3509. }
  3510. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3511. * transcoder, and we actually should do this to not upset any PCH
  3512. * transcoder that already use the clock when we share it.
  3513. *
  3514. * Note that enable_shared_dpll tries to do the right thing, but
  3515. * get_shared_dpll unconditionally resets the pll - we need that to have
  3516. * the right LVDS enable sequence. */
  3517. intel_enable_shared_dpll(intel_crtc);
  3518. /* set transcoder timing, panel must allow it */
  3519. assert_panel_unlocked(dev_priv, pipe);
  3520. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3521. intel_fdi_normal_train(crtc);
  3522. /* For PCH DP, enable TRANS_DP_CTL */
  3523. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3524. const struct drm_display_mode *adjusted_mode =
  3525. &intel_crtc->config->base.adjusted_mode;
  3526. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3527. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3528. temp = I915_READ(reg);
  3529. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3530. TRANS_DP_SYNC_MASK |
  3531. TRANS_DP_BPC_MASK);
  3532. temp |= TRANS_DP_OUTPUT_ENABLE;
  3533. temp |= bpc << 9; /* same format but at 11:9 */
  3534. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3535. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3536. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3537. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3538. switch (intel_trans_dp_port_sel(crtc)) {
  3539. case PORT_B:
  3540. temp |= TRANS_DP_PORT_SEL_B;
  3541. break;
  3542. case PORT_C:
  3543. temp |= TRANS_DP_PORT_SEL_C;
  3544. break;
  3545. case PORT_D:
  3546. temp |= TRANS_DP_PORT_SEL_D;
  3547. break;
  3548. default:
  3549. BUG();
  3550. }
  3551. I915_WRITE(reg, temp);
  3552. }
  3553. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3554. }
  3555. static void lpt_pch_enable(struct drm_crtc *crtc)
  3556. {
  3557. struct drm_device *dev = crtc->dev;
  3558. struct drm_i915_private *dev_priv = dev->dev_private;
  3559. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3560. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3561. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3562. lpt_program_iclkip(crtc);
  3563. /* Set transcoder timing. */
  3564. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3565. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3566. }
  3567. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3568. {
  3569. struct drm_i915_private *dev_priv = dev->dev_private;
  3570. i915_reg_t dslreg = PIPEDSL(pipe);
  3571. u32 temp;
  3572. temp = I915_READ(dslreg);
  3573. udelay(500);
  3574. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3575. if (wait_for(I915_READ(dslreg) != temp, 5))
  3576. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3577. }
  3578. }
  3579. static int
  3580. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3581. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3582. int src_w, int src_h, int dst_w, int dst_h)
  3583. {
  3584. struct intel_crtc_scaler_state *scaler_state =
  3585. &crtc_state->scaler_state;
  3586. struct intel_crtc *intel_crtc =
  3587. to_intel_crtc(crtc_state->base.crtc);
  3588. int need_scaling;
  3589. need_scaling = intel_rotation_90_or_270(rotation) ?
  3590. (src_h != dst_w || src_w != dst_h):
  3591. (src_w != dst_w || src_h != dst_h);
  3592. /*
  3593. * if plane is being disabled or scaler is no more required or force detach
  3594. * - free scaler binded to this plane/crtc
  3595. * - in order to do this, update crtc->scaler_usage
  3596. *
  3597. * Here scaler state in crtc_state is set free so that
  3598. * scaler can be assigned to other user. Actual register
  3599. * update to free the scaler is done in plane/panel-fit programming.
  3600. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3601. */
  3602. if (force_detach || !need_scaling) {
  3603. if (*scaler_id >= 0) {
  3604. scaler_state->scaler_users &= ~(1 << scaler_user);
  3605. scaler_state->scalers[*scaler_id].in_use = 0;
  3606. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3607. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3608. intel_crtc->pipe, scaler_user, *scaler_id,
  3609. scaler_state->scaler_users);
  3610. *scaler_id = -1;
  3611. }
  3612. return 0;
  3613. }
  3614. /* range checks */
  3615. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3616. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3617. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3618. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3619. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3620. "size is out of scaler range\n",
  3621. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3622. return -EINVAL;
  3623. }
  3624. /* mark this plane as a scaler user in crtc_state */
  3625. scaler_state->scaler_users |= (1 << scaler_user);
  3626. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3627. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3628. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3629. scaler_state->scaler_users);
  3630. return 0;
  3631. }
  3632. /**
  3633. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3634. *
  3635. * @state: crtc's scaler state
  3636. *
  3637. * Return
  3638. * 0 - scaler_usage updated successfully
  3639. * error - requested scaling cannot be supported or other error condition
  3640. */
  3641. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3642. {
  3643. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3644. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3645. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3646. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3647. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3648. &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
  3649. state->pipe_src_w, state->pipe_src_h,
  3650. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3651. }
  3652. /**
  3653. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3654. *
  3655. * @state: crtc's scaler state
  3656. * @plane_state: atomic plane state to update
  3657. *
  3658. * Return
  3659. * 0 - scaler_usage updated successfully
  3660. * error - requested scaling cannot be supported or other error condition
  3661. */
  3662. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3663. struct intel_plane_state *plane_state)
  3664. {
  3665. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3666. struct intel_plane *intel_plane =
  3667. to_intel_plane(plane_state->base.plane);
  3668. struct drm_framebuffer *fb = plane_state->base.fb;
  3669. int ret;
  3670. bool force_detach = !fb || !plane_state->visible;
  3671. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3672. intel_plane->base.base.id, intel_crtc->pipe,
  3673. drm_plane_index(&intel_plane->base));
  3674. ret = skl_update_scaler(crtc_state, force_detach,
  3675. drm_plane_index(&intel_plane->base),
  3676. &plane_state->scaler_id,
  3677. plane_state->base.rotation,
  3678. drm_rect_width(&plane_state->src) >> 16,
  3679. drm_rect_height(&plane_state->src) >> 16,
  3680. drm_rect_width(&plane_state->dst),
  3681. drm_rect_height(&plane_state->dst));
  3682. if (ret || plane_state->scaler_id < 0)
  3683. return ret;
  3684. /* check colorkey */
  3685. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3686. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3687. intel_plane->base.base.id);
  3688. return -EINVAL;
  3689. }
  3690. /* Check src format */
  3691. switch (fb->pixel_format) {
  3692. case DRM_FORMAT_RGB565:
  3693. case DRM_FORMAT_XBGR8888:
  3694. case DRM_FORMAT_XRGB8888:
  3695. case DRM_FORMAT_ABGR8888:
  3696. case DRM_FORMAT_ARGB8888:
  3697. case DRM_FORMAT_XRGB2101010:
  3698. case DRM_FORMAT_XBGR2101010:
  3699. case DRM_FORMAT_YUYV:
  3700. case DRM_FORMAT_YVYU:
  3701. case DRM_FORMAT_UYVY:
  3702. case DRM_FORMAT_VYUY:
  3703. break;
  3704. default:
  3705. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3706. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3707. return -EINVAL;
  3708. }
  3709. return 0;
  3710. }
  3711. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3712. {
  3713. int i;
  3714. for (i = 0; i < crtc->num_scalers; i++)
  3715. skl_detach_scaler(crtc, i);
  3716. }
  3717. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3718. {
  3719. struct drm_device *dev = crtc->base.dev;
  3720. struct drm_i915_private *dev_priv = dev->dev_private;
  3721. int pipe = crtc->pipe;
  3722. struct intel_crtc_scaler_state *scaler_state =
  3723. &crtc->config->scaler_state;
  3724. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3725. if (crtc->config->pch_pfit.enabled) {
  3726. int id;
  3727. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3728. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3729. return;
  3730. }
  3731. id = scaler_state->scaler_id;
  3732. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3733. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3734. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3735. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3736. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3737. }
  3738. }
  3739. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3740. {
  3741. struct drm_device *dev = crtc->base.dev;
  3742. struct drm_i915_private *dev_priv = dev->dev_private;
  3743. int pipe = crtc->pipe;
  3744. if (crtc->config->pch_pfit.enabled) {
  3745. /* Force use of hard-coded filter coefficients
  3746. * as some pre-programmed values are broken,
  3747. * e.g. x201.
  3748. */
  3749. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3750. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3751. PF_PIPE_SEL_IVB(pipe));
  3752. else
  3753. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3754. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3755. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3756. }
  3757. }
  3758. void hsw_enable_ips(struct intel_crtc *crtc)
  3759. {
  3760. struct drm_device *dev = crtc->base.dev;
  3761. struct drm_i915_private *dev_priv = dev->dev_private;
  3762. if (!crtc->config->ips_enabled)
  3763. return;
  3764. /*
  3765. * We can only enable IPS after we enable a plane and wait for a vblank
  3766. * This function is called from post_plane_update, which is run after
  3767. * a vblank wait.
  3768. */
  3769. assert_plane_enabled(dev_priv, crtc->plane);
  3770. if (IS_BROADWELL(dev)) {
  3771. mutex_lock(&dev_priv->rps.hw_lock);
  3772. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3773. mutex_unlock(&dev_priv->rps.hw_lock);
  3774. /* Quoting Art Runyan: "its not safe to expect any particular
  3775. * value in IPS_CTL bit 31 after enabling IPS through the
  3776. * mailbox." Moreover, the mailbox may return a bogus state,
  3777. * so we need to just enable it and continue on.
  3778. */
  3779. } else {
  3780. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3781. /* The bit only becomes 1 in the next vblank, so this wait here
  3782. * is essentially intel_wait_for_vblank. If we don't have this
  3783. * and don't wait for vblanks until the end of crtc_enable, then
  3784. * the HW state readout code will complain that the expected
  3785. * IPS_CTL value is not the one we read. */
  3786. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3787. DRM_ERROR("Timed out waiting for IPS enable\n");
  3788. }
  3789. }
  3790. void hsw_disable_ips(struct intel_crtc *crtc)
  3791. {
  3792. struct drm_device *dev = crtc->base.dev;
  3793. struct drm_i915_private *dev_priv = dev->dev_private;
  3794. if (!crtc->config->ips_enabled)
  3795. return;
  3796. assert_plane_enabled(dev_priv, crtc->plane);
  3797. if (IS_BROADWELL(dev)) {
  3798. mutex_lock(&dev_priv->rps.hw_lock);
  3799. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3800. mutex_unlock(&dev_priv->rps.hw_lock);
  3801. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3802. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3803. DRM_ERROR("Timed out waiting for IPS disable\n");
  3804. } else {
  3805. I915_WRITE(IPS_CTL, 0);
  3806. POSTING_READ(IPS_CTL);
  3807. }
  3808. /* We need to wait for a vblank before we can disable the plane. */
  3809. intel_wait_for_vblank(dev, crtc->pipe);
  3810. }
  3811. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3812. {
  3813. if (intel_crtc->overlay) {
  3814. struct drm_device *dev = intel_crtc->base.dev;
  3815. struct drm_i915_private *dev_priv = dev->dev_private;
  3816. mutex_lock(&dev->struct_mutex);
  3817. dev_priv->mm.interruptible = false;
  3818. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3819. dev_priv->mm.interruptible = true;
  3820. mutex_unlock(&dev->struct_mutex);
  3821. }
  3822. /* Let userspace switch the overlay on again. In most cases userspace
  3823. * has to recompute where to put it anyway.
  3824. */
  3825. }
  3826. /**
  3827. * intel_post_enable_primary - Perform operations after enabling primary plane
  3828. * @crtc: the CRTC whose primary plane was just enabled
  3829. *
  3830. * Performs potentially sleeping operations that must be done after the primary
  3831. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3832. * called due to an explicit primary plane update, or due to an implicit
  3833. * re-enable that is caused when a sprite plane is updated to no longer
  3834. * completely hide the primary plane.
  3835. */
  3836. static void
  3837. intel_post_enable_primary(struct drm_crtc *crtc)
  3838. {
  3839. struct drm_device *dev = crtc->dev;
  3840. struct drm_i915_private *dev_priv = dev->dev_private;
  3841. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3842. int pipe = intel_crtc->pipe;
  3843. /*
  3844. * FIXME IPS should be fine as long as one plane is
  3845. * enabled, but in practice it seems to have problems
  3846. * when going from primary only to sprite only and vice
  3847. * versa.
  3848. */
  3849. hsw_enable_ips(intel_crtc);
  3850. /*
  3851. * Gen2 reports pipe underruns whenever all planes are disabled.
  3852. * So don't enable underrun reporting before at least some planes
  3853. * are enabled.
  3854. * FIXME: Need to fix the logic to work when we turn off all planes
  3855. * but leave the pipe running.
  3856. */
  3857. if (IS_GEN2(dev))
  3858. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3859. /* Underruns don't always raise interrupts, so check manually. */
  3860. intel_check_cpu_fifo_underruns(dev_priv);
  3861. intel_check_pch_fifo_underruns(dev_priv);
  3862. }
  3863. /* FIXME move all this to pre_plane_update() with proper state tracking */
  3864. static void
  3865. intel_pre_disable_primary(struct drm_crtc *crtc)
  3866. {
  3867. struct drm_device *dev = crtc->dev;
  3868. struct drm_i915_private *dev_priv = dev->dev_private;
  3869. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3870. int pipe = intel_crtc->pipe;
  3871. /*
  3872. * Gen2 reports pipe underruns whenever all planes are disabled.
  3873. * So diasble underrun reporting before all the planes get disabled.
  3874. * FIXME: Need to fix the logic to work when we turn off all planes
  3875. * but leave the pipe running.
  3876. */
  3877. if (IS_GEN2(dev))
  3878. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3879. /*
  3880. * FIXME IPS should be fine as long as one plane is
  3881. * enabled, but in practice it seems to have problems
  3882. * when going from primary only to sprite only and vice
  3883. * versa.
  3884. */
  3885. hsw_disable_ips(intel_crtc);
  3886. }
  3887. /* FIXME get rid of this and use pre_plane_update */
  3888. static void
  3889. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  3890. {
  3891. struct drm_device *dev = crtc->dev;
  3892. struct drm_i915_private *dev_priv = dev->dev_private;
  3893. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3894. int pipe = intel_crtc->pipe;
  3895. intel_pre_disable_primary(crtc);
  3896. /*
  3897. * Vblank time updates from the shadow to live plane control register
  3898. * are blocked if the memory self-refresh mode is active at that
  3899. * moment. So to make sure the plane gets truly disabled, disable
  3900. * first the self-refresh mode. The self-refresh enable bit in turn
  3901. * will be checked/applied by the HW only at the next frame start
  3902. * event which is after the vblank start event, so we need to have a
  3903. * wait-for-vblank between disabling the plane and the pipe.
  3904. */
  3905. if (HAS_GMCH_DISPLAY(dev)) {
  3906. intel_set_memory_cxsr(dev_priv, false);
  3907. dev_priv->wm.vlv.cxsr = false;
  3908. intel_wait_for_vblank(dev, pipe);
  3909. }
  3910. }
  3911. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  3912. {
  3913. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3914. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3915. struct intel_crtc_state *pipe_config =
  3916. to_intel_crtc_state(crtc->base.state);
  3917. struct drm_device *dev = crtc->base.dev;
  3918. struct drm_plane *primary = crtc->base.primary;
  3919. struct drm_plane_state *old_pri_state =
  3920. drm_atomic_get_existing_plane_state(old_state, primary);
  3921. intel_frontbuffer_flip(dev, pipe_config->fb_bits);
  3922. crtc->wm.cxsr_allowed = true;
  3923. if (pipe_config->update_wm_post && pipe_config->base.active)
  3924. intel_update_watermarks(&crtc->base);
  3925. if (old_pri_state) {
  3926. struct intel_plane_state *primary_state =
  3927. to_intel_plane_state(primary->state);
  3928. struct intel_plane_state *old_primary_state =
  3929. to_intel_plane_state(old_pri_state);
  3930. intel_fbc_post_update(crtc);
  3931. if (primary_state->visible &&
  3932. (needs_modeset(&pipe_config->base) ||
  3933. !old_primary_state->visible))
  3934. intel_post_enable_primary(&crtc->base);
  3935. }
  3936. }
  3937. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  3938. {
  3939. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3940. struct drm_device *dev = crtc->base.dev;
  3941. struct drm_i915_private *dev_priv = dev->dev_private;
  3942. struct intel_crtc_state *pipe_config =
  3943. to_intel_crtc_state(crtc->base.state);
  3944. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3945. struct drm_plane *primary = crtc->base.primary;
  3946. struct drm_plane_state *old_pri_state =
  3947. drm_atomic_get_existing_plane_state(old_state, primary);
  3948. bool modeset = needs_modeset(&pipe_config->base);
  3949. if (old_pri_state) {
  3950. struct intel_plane_state *primary_state =
  3951. to_intel_plane_state(primary->state);
  3952. struct intel_plane_state *old_primary_state =
  3953. to_intel_plane_state(old_pri_state);
  3954. intel_fbc_pre_update(crtc);
  3955. if (old_primary_state->visible &&
  3956. (modeset || !primary_state->visible))
  3957. intel_pre_disable_primary(&crtc->base);
  3958. }
  3959. if (pipe_config->disable_cxsr) {
  3960. crtc->wm.cxsr_allowed = false;
  3961. /*
  3962. * Vblank time updates from the shadow to live plane control register
  3963. * are blocked if the memory self-refresh mode is active at that
  3964. * moment. So to make sure the plane gets truly disabled, disable
  3965. * first the self-refresh mode. The self-refresh enable bit in turn
  3966. * will be checked/applied by the HW only at the next frame start
  3967. * event which is after the vblank start event, so we need to have a
  3968. * wait-for-vblank between disabling the plane and the pipe.
  3969. */
  3970. if (old_crtc_state->base.active) {
  3971. intel_set_memory_cxsr(dev_priv, false);
  3972. dev_priv->wm.vlv.cxsr = false;
  3973. intel_wait_for_vblank(dev, crtc->pipe);
  3974. }
  3975. }
  3976. /*
  3977. * IVB workaround: must disable low power watermarks for at least
  3978. * one frame before enabling scaling. LP watermarks can be re-enabled
  3979. * when scaling is disabled.
  3980. *
  3981. * WaCxSRDisabledForSpriteScaling:ivb
  3982. */
  3983. if (pipe_config->disable_lp_wm) {
  3984. ilk_disable_lp_wm(dev);
  3985. intel_wait_for_vblank(dev, crtc->pipe);
  3986. }
  3987. /*
  3988. * If we're doing a modeset, we're done. No need to do any pre-vblank
  3989. * watermark programming here.
  3990. */
  3991. if (needs_modeset(&pipe_config->base))
  3992. return;
  3993. /*
  3994. * For platforms that support atomic watermarks, program the
  3995. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  3996. * will be the intermediate values that are safe for both pre- and
  3997. * post- vblank; when vblank happens, the 'active' values will be set
  3998. * to the final 'target' values and we'll do this again to get the
  3999. * optimal watermarks. For gen9+ platforms, the values we program here
  4000. * will be the final target values which will get automatically latched
  4001. * at vblank time; no further programming will be necessary.
  4002. *
  4003. * If a platform hasn't been transitioned to atomic watermarks yet,
  4004. * we'll continue to update watermarks the old way, if flags tell
  4005. * us to.
  4006. */
  4007. if (dev_priv->display.initial_watermarks != NULL)
  4008. dev_priv->display.initial_watermarks(pipe_config);
  4009. else if (pipe_config->update_wm_pre)
  4010. intel_update_watermarks(&crtc->base);
  4011. }
  4012. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4013. {
  4014. struct drm_device *dev = crtc->dev;
  4015. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4016. struct drm_plane *p;
  4017. int pipe = intel_crtc->pipe;
  4018. intel_crtc_dpms_overlay_disable(intel_crtc);
  4019. drm_for_each_plane_mask(p, dev, plane_mask)
  4020. to_intel_plane(p)->disable_plane(p, crtc);
  4021. /*
  4022. * FIXME: Once we grow proper nuclear flip support out of this we need
  4023. * to compute the mask of flip planes precisely. For the time being
  4024. * consider this a flip to a NULL plane.
  4025. */
  4026. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4027. }
  4028. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4029. {
  4030. struct drm_device *dev = crtc->dev;
  4031. struct drm_i915_private *dev_priv = dev->dev_private;
  4032. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4033. struct intel_encoder *encoder;
  4034. int pipe = intel_crtc->pipe;
  4035. struct intel_crtc_state *pipe_config =
  4036. to_intel_crtc_state(crtc->state);
  4037. if (WARN_ON(intel_crtc->active))
  4038. return;
  4039. /*
  4040. * Sometimes spurious CPU pipe underruns happen during FDI
  4041. * training, at least with VGA+HDMI cloning. Suppress them.
  4042. *
  4043. * On ILK we get an occasional spurious CPU pipe underruns
  4044. * between eDP port A enable and vdd enable. Also PCH port
  4045. * enable seems to result in the occasional CPU pipe underrun.
  4046. *
  4047. * Spurious PCH underruns also occur during PCH enabling.
  4048. */
  4049. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4050. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4051. if (intel_crtc->config->has_pch_encoder)
  4052. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4053. if (intel_crtc->config->has_pch_encoder)
  4054. intel_prepare_shared_dpll(intel_crtc);
  4055. if (intel_crtc->config->has_dp_encoder)
  4056. intel_dp_set_m_n(intel_crtc, M1_N1);
  4057. intel_set_pipe_timings(intel_crtc);
  4058. intel_set_pipe_src_size(intel_crtc);
  4059. if (intel_crtc->config->has_pch_encoder) {
  4060. intel_cpu_transcoder_set_m_n(intel_crtc,
  4061. &intel_crtc->config->fdi_m_n, NULL);
  4062. }
  4063. ironlake_set_pipeconf(crtc);
  4064. intel_crtc->active = true;
  4065. for_each_encoder_on_crtc(dev, crtc, encoder)
  4066. if (encoder->pre_enable)
  4067. encoder->pre_enable(encoder);
  4068. if (intel_crtc->config->has_pch_encoder) {
  4069. /* Note: FDI PLL enabling _must_ be done before we enable the
  4070. * cpu pipes, hence this is separate from all the other fdi/pch
  4071. * enabling. */
  4072. ironlake_fdi_pll_enable(intel_crtc);
  4073. } else {
  4074. assert_fdi_tx_disabled(dev_priv, pipe);
  4075. assert_fdi_rx_disabled(dev_priv, pipe);
  4076. }
  4077. ironlake_pfit_enable(intel_crtc);
  4078. /*
  4079. * On ILK+ LUT must be loaded before the pipe is running but with
  4080. * clocks enabled
  4081. */
  4082. intel_color_load_luts(&pipe_config->base);
  4083. if (dev_priv->display.initial_watermarks != NULL)
  4084. dev_priv->display.initial_watermarks(intel_crtc->config);
  4085. intel_enable_pipe(intel_crtc);
  4086. if (intel_crtc->config->has_pch_encoder)
  4087. ironlake_pch_enable(crtc);
  4088. assert_vblank_disabled(crtc);
  4089. drm_crtc_vblank_on(crtc);
  4090. for_each_encoder_on_crtc(dev, crtc, encoder)
  4091. encoder->enable(encoder);
  4092. if (HAS_PCH_CPT(dev))
  4093. cpt_verify_modeset(dev, intel_crtc->pipe);
  4094. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4095. if (intel_crtc->config->has_pch_encoder)
  4096. intel_wait_for_vblank(dev, pipe);
  4097. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4098. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4099. }
  4100. /* IPS only exists on ULT machines and is tied to pipe A. */
  4101. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4102. {
  4103. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4104. }
  4105. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4106. {
  4107. struct drm_device *dev = crtc->dev;
  4108. struct drm_i915_private *dev_priv = dev->dev_private;
  4109. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4110. struct intel_encoder *encoder;
  4111. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4112. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4113. struct intel_crtc_state *pipe_config =
  4114. to_intel_crtc_state(crtc->state);
  4115. if (WARN_ON(intel_crtc->active))
  4116. return;
  4117. if (intel_crtc->config->has_pch_encoder)
  4118. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4119. false);
  4120. if (intel_crtc->config->shared_dpll)
  4121. intel_enable_shared_dpll(intel_crtc);
  4122. if (intel_crtc->config->has_dp_encoder)
  4123. intel_dp_set_m_n(intel_crtc, M1_N1);
  4124. if (!intel_crtc->config->has_dsi_encoder)
  4125. intel_set_pipe_timings(intel_crtc);
  4126. intel_set_pipe_src_size(intel_crtc);
  4127. if (cpu_transcoder != TRANSCODER_EDP &&
  4128. !transcoder_is_dsi(cpu_transcoder)) {
  4129. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4130. intel_crtc->config->pixel_multiplier - 1);
  4131. }
  4132. if (intel_crtc->config->has_pch_encoder) {
  4133. intel_cpu_transcoder_set_m_n(intel_crtc,
  4134. &intel_crtc->config->fdi_m_n, NULL);
  4135. }
  4136. if (!intel_crtc->config->has_dsi_encoder)
  4137. haswell_set_pipeconf(crtc);
  4138. haswell_set_pipemisc(crtc);
  4139. intel_color_set_csc(&pipe_config->base);
  4140. intel_crtc->active = true;
  4141. if (intel_crtc->config->has_pch_encoder)
  4142. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4143. else
  4144. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4145. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4146. if (encoder->pre_enable)
  4147. encoder->pre_enable(encoder);
  4148. }
  4149. if (intel_crtc->config->has_pch_encoder)
  4150. dev_priv->display.fdi_link_train(crtc);
  4151. if (!intel_crtc->config->has_dsi_encoder)
  4152. intel_ddi_enable_pipe_clock(intel_crtc);
  4153. if (INTEL_INFO(dev)->gen >= 9)
  4154. skylake_pfit_enable(intel_crtc);
  4155. else
  4156. ironlake_pfit_enable(intel_crtc);
  4157. /*
  4158. * On ILK+ LUT must be loaded before the pipe is running but with
  4159. * clocks enabled
  4160. */
  4161. intel_color_load_luts(&pipe_config->base);
  4162. intel_ddi_set_pipe_settings(crtc);
  4163. if (!intel_crtc->config->has_dsi_encoder)
  4164. intel_ddi_enable_transcoder_func(crtc);
  4165. if (dev_priv->display.initial_watermarks != NULL)
  4166. dev_priv->display.initial_watermarks(pipe_config);
  4167. else
  4168. intel_update_watermarks(crtc);
  4169. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4170. if (!intel_crtc->config->has_dsi_encoder)
  4171. intel_enable_pipe(intel_crtc);
  4172. if (intel_crtc->config->has_pch_encoder)
  4173. lpt_pch_enable(crtc);
  4174. if (intel_crtc->config->dp_encoder_is_mst)
  4175. intel_ddi_set_vc_payload_alloc(crtc, true);
  4176. assert_vblank_disabled(crtc);
  4177. drm_crtc_vblank_on(crtc);
  4178. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4179. encoder->enable(encoder);
  4180. intel_opregion_notify_encoder(encoder, true);
  4181. }
  4182. if (intel_crtc->config->has_pch_encoder) {
  4183. intel_wait_for_vblank(dev, pipe);
  4184. intel_wait_for_vblank(dev, pipe);
  4185. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4186. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4187. true);
  4188. }
  4189. /* If we change the relative order between pipe/planes enabling, we need
  4190. * to change the workaround. */
  4191. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4192. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4193. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4194. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4195. }
  4196. }
  4197. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4198. {
  4199. struct drm_device *dev = crtc->base.dev;
  4200. struct drm_i915_private *dev_priv = dev->dev_private;
  4201. int pipe = crtc->pipe;
  4202. /* To avoid upsetting the power well on haswell only disable the pfit if
  4203. * it's in use. The hw state code will make sure we get this right. */
  4204. if (force || crtc->config->pch_pfit.enabled) {
  4205. I915_WRITE(PF_CTL(pipe), 0);
  4206. I915_WRITE(PF_WIN_POS(pipe), 0);
  4207. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4208. }
  4209. }
  4210. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4211. {
  4212. struct drm_device *dev = crtc->dev;
  4213. struct drm_i915_private *dev_priv = dev->dev_private;
  4214. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4215. struct intel_encoder *encoder;
  4216. int pipe = intel_crtc->pipe;
  4217. /*
  4218. * Sometimes spurious CPU pipe underruns happen when the
  4219. * pipe is already disabled, but FDI RX/TX is still enabled.
  4220. * Happens at least with VGA+HDMI cloning. Suppress them.
  4221. */
  4222. if (intel_crtc->config->has_pch_encoder) {
  4223. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4224. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4225. }
  4226. for_each_encoder_on_crtc(dev, crtc, encoder)
  4227. encoder->disable(encoder);
  4228. drm_crtc_vblank_off(crtc);
  4229. assert_vblank_disabled(crtc);
  4230. intel_disable_pipe(intel_crtc);
  4231. ironlake_pfit_disable(intel_crtc, false);
  4232. if (intel_crtc->config->has_pch_encoder)
  4233. ironlake_fdi_disable(crtc);
  4234. for_each_encoder_on_crtc(dev, crtc, encoder)
  4235. if (encoder->post_disable)
  4236. encoder->post_disable(encoder);
  4237. if (intel_crtc->config->has_pch_encoder) {
  4238. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4239. if (HAS_PCH_CPT(dev)) {
  4240. i915_reg_t reg;
  4241. u32 temp;
  4242. /* disable TRANS_DP_CTL */
  4243. reg = TRANS_DP_CTL(pipe);
  4244. temp = I915_READ(reg);
  4245. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4246. TRANS_DP_PORT_SEL_MASK);
  4247. temp |= TRANS_DP_PORT_SEL_NONE;
  4248. I915_WRITE(reg, temp);
  4249. /* disable DPLL_SEL */
  4250. temp = I915_READ(PCH_DPLL_SEL);
  4251. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4252. I915_WRITE(PCH_DPLL_SEL, temp);
  4253. }
  4254. ironlake_fdi_pll_disable(intel_crtc);
  4255. }
  4256. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4257. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4258. }
  4259. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4260. {
  4261. struct drm_device *dev = crtc->dev;
  4262. struct drm_i915_private *dev_priv = dev->dev_private;
  4263. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4264. struct intel_encoder *encoder;
  4265. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4266. if (intel_crtc->config->has_pch_encoder)
  4267. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4268. false);
  4269. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4270. intel_opregion_notify_encoder(encoder, false);
  4271. encoder->disable(encoder);
  4272. }
  4273. drm_crtc_vblank_off(crtc);
  4274. assert_vblank_disabled(crtc);
  4275. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4276. if (!intel_crtc->config->has_dsi_encoder)
  4277. intel_disable_pipe(intel_crtc);
  4278. if (intel_crtc->config->dp_encoder_is_mst)
  4279. intel_ddi_set_vc_payload_alloc(crtc, false);
  4280. if (!intel_crtc->config->has_dsi_encoder)
  4281. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4282. if (INTEL_INFO(dev)->gen >= 9)
  4283. skylake_scaler_disable(intel_crtc);
  4284. else
  4285. ironlake_pfit_disable(intel_crtc, false);
  4286. if (!intel_crtc->config->has_dsi_encoder)
  4287. intel_ddi_disable_pipe_clock(intel_crtc);
  4288. for_each_encoder_on_crtc(dev, crtc, encoder)
  4289. if (encoder->post_disable)
  4290. encoder->post_disable(encoder);
  4291. if (intel_crtc->config->has_pch_encoder) {
  4292. lpt_disable_pch_transcoder(dev_priv);
  4293. lpt_disable_iclkip(dev_priv);
  4294. intel_ddi_fdi_disable(crtc);
  4295. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4296. true);
  4297. }
  4298. }
  4299. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4300. {
  4301. struct drm_device *dev = crtc->base.dev;
  4302. struct drm_i915_private *dev_priv = dev->dev_private;
  4303. struct intel_crtc_state *pipe_config = crtc->config;
  4304. if (!pipe_config->gmch_pfit.control)
  4305. return;
  4306. /*
  4307. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4308. * according to register description and PRM.
  4309. */
  4310. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4311. assert_pipe_disabled(dev_priv, crtc->pipe);
  4312. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4313. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4314. /* Border color in case we don't scale up to the full screen. Black by
  4315. * default, change to something else for debugging. */
  4316. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4317. }
  4318. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4319. {
  4320. switch (port) {
  4321. case PORT_A:
  4322. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4323. case PORT_B:
  4324. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4325. case PORT_C:
  4326. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4327. case PORT_D:
  4328. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4329. case PORT_E:
  4330. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4331. default:
  4332. MISSING_CASE(port);
  4333. return POWER_DOMAIN_PORT_OTHER;
  4334. }
  4335. }
  4336. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4337. {
  4338. switch (port) {
  4339. case PORT_A:
  4340. return POWER_DOMAIN_AUX_A;
  4341. case PORT_B:
  4342. return POWER_DOMAIN_AUX_B;
  4343. case PORT_C:
  4344. return POWER_DOMAIN_AUX_C;
  4345. case PORT_D:
  4346. return POWER_DOMAIN_AUX_D;
  4347. case PORT_E:
  4348. /* FIXME: Check VBT for actual wiring of PORT E */
  4349. return POWER_DOMAIN_AUX_D;
  4350. default:
  4351. MISSING_CASE(port);
  4352. return POWER_DOMAIN_AUX_A;
  4353. }
  4354. }
  4355. enum intel_display_power_domain
  4356. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4357. {
  4358. struct drm_device *dev = intel_encoder->base.dev;
  4359. struct intel_digital_port *intel_dig_port;
  4360. switch (intel_encoder->type) {
  4361. case INTEL_OUTPUT_UNKNOWN:
  4362. /* Only DDI platforms should ever use this output type */
  4363. WARN_ON_ONCE(!HAS_DDI(dev));
  4364. case INTEL_OUTPUT_DISPLAYPORT:
  4365. case INTEL_OUTPUT_HDMI:
  4366. case INTEL_OUTPUT_EDP:
  4367. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4368. return port_to_power_domain(intel_dig_port->port);
  4369. case INTEL_OUTPUT_DP_MST:
  4370. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4371. return port_to_power_domain(intel_dig_port->port);
  4372. case INTEL_OUTPUT_ANALOG:
  4373. return POWER_DOMAIN_PORT_CRT;
  4374. case INTEL_OUTPUT_DSI:
  4375. return POWER_DOMAIN_PORT_DSI;
  4376. default:
  4377. return POWER_DOMAIN_PORT_OTHER;
  4378. }
  4379. }
  4380. enum intel_display_power_domain
  4381. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4382. {
  4383. struct drm_device *dev = intel_encoder->base.dev;
  4384. struct intel_digital_port *intel_dig_port;
  4385. switch (intel_encoder->type) {
  4386. case INTEL_OUTPUT_UNKNOWN:
  4387. case INTEL_OUTPUT_HDMI:
  4388. /*
  4389. * Only DDI platforms should ever use these output types.
  4390. * We can get here after the HDMI detect code has already set
  4391. * the type of the shared encoder. Since we can't be sure
  4392. * what's the status of the given connectors, play safe and
  4393. * run the DP detection too.
  4394. */
  4395. WARN_ON_ONCE(!HAS_DDI(dev));
  4396. case INTEL_OUTPUT_DISPLAYPORT:
  4397. case INTEL_OUTPUT_EDP:
  4398. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4399. return port_to_aux_power_domain(intel_dig_port->port);
  4400. case INTEL_OUTPUT_DP_MST:
  4401. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4402. return port_to_aux_power_domain(intel_dig_port->port);
  4403. default:
  4404. MISSING_CASE(intel_encoder->type);
  4405. return POWER_DOMAIN_AUX_A;
  4406. }
  4407. }
  4408. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4409. struct intel_crtc_state *crtc_state)
  4410. {
  4411. struct drm_device *dev = crtc->dev;
  4412. struct drm_encoder *encoder;
  4413. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4414. enum pipe pipe = intel_crtc->pipe;
  4415. unsigned long mask;
  4416. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4417. if (!crtc_state->base.active)
  4418. return 0;
  4419. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4420. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4421. if (crtc_state->pch_pfit.enabled ||
  4422. crtc_state->pch_pfit.force_thru)
  4423. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4424. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4425. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4426. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4427. }
  4428. if (crtc_state->shared_dpll)
  4429. mask |= BIT(POWER_DOMAIN_PLLS);
  4430. return mask;
  4431. }
  4432. static unsigned long
  4433. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4434. struct intel_crtc_state *crtc_state)
  4435. {
  4436. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4437. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4438. enum intel_display_power_domain domain;
  4439. unsigned long domains, new_domains, old_domains;
  4440. old_domains = intel_crtc->enabled_power_domains;
  4441. intel_crtc->enabled_power_domains = new_domains =
  4442. get_crtc_power_domains(crtc, crtc_state);
  4443. domains = new_domains & ~old_domains;
  4444. for_each_power_domain(domain, domains)
  4445. intel_display_power_get(dev_priv, domain);
  4446. return old_domains & ~new_domains;
  4447. }
  4448. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4449. unsigned long domains)
  4450. {
  4451. enum intel_display_power_domain domain;
  4452. for_each_power_domain(domain, domains)
  4453. intel_display_power_put(dev_priv, domain);
  4454. }
  4455. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4456. {
  4457. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4458. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4459. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4460. return max_cdclk_freq;
  4461. else if (IS_CHERRYVIEW(dev_priv))
  4462. return max_cdclk_freq*95/100;
  4463. else if (INTEL_INFO(dev_priv)->gen < 4)
  4464. return 2*max_cdclk_freq*90/100;
  4465. else
  4466. return max_cdclk_freq*90/100;
  4467. }
  4468. static void intel_update_max_cdclk(struct drm_device *dev)
  4469. {
  4470. struct drm_i915_private *dev_priv = dev->dev_private;
  4471. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4472. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4473. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4474. dev_priv->max_cdclk_freq = 675000;
  4475. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4476. dev_priv->max_cdclk_freq = 540000;
  4477. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4478. dev_priv->max_cdclk_freq = 450000;
  4479. else
  4480. dev_priv->max_cdclk_freq = 337500;
  4481. } else if (IS_BROXTON(dev)) {
  4482. dev_priv->max_cdclk_freq = 624000;
  4483. } else if (IS_BROADWELL(dev)) {
  4484. /*
  4485. * FIXME with extra cooling we can allow
  4486. * 540 MHz for ULX and 675 Mhz for ULT.
  4487. * How can we know if extra cooling is
  4488. * available? PCI ID, VTB, something else?
  4489. */
  4490. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4491. dev_priv->max_cdclk_freq = 450000;
  4492. else if (IS_BDW_ULX(dev))
  4493. dev_priv->max_cdclk_freq = 450000;
  4494. else if (IS_BDW_ULT(dev))
  4495. dev_priv->max_cdclk_freq = 540000;
  4496. else
  4497. dev_priv->max_cdclk_freq = 675000;
  4498. } else if (IS_CHERRYVIEW(dev)) {
  4499. dev_priv->max_cdclk_freq = 320000;
  4500. } else if (IS_VALLEYVIEW(dev)) {
  4501. dev_priv->max_cdclk_freq = 400000;
  4502. } else {
  4503. /* otherwise assume cdclk is fixed */
  4504. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4505. }
  4506. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4507. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4508. dev_priv->max_cdclk_freq);
  4509. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4510. dev_priv->max_dotclk_freq);
  4511. }
  4512. static void intel_update_cdclk(struct drm_device *dev)
  4513. {
  4514. struct drm_i915_private *dev_priv = dev->dev_private;
  4515. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4516. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4517. dev_priv->cdclk_freq);
  4518. /*
  4519. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4520. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4521. * of cdclk that generates 4MHz reference clock freq which is used to
  4522. * generate GMBus clock. This will vary with the cdclk freq.
  4523. */
  4524. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4525. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4526. if (dev_priv->max_cdclk_freq == 0)
  4527. intel_update_max_cdclk(dev);
  4528. }
  4529. static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int frequency)
  4530. {
  4531. uint32_t divider;
  4532. uint32_t ratio;
  4533. uint32_t current_freq;
  4534. int ret;
  4535. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4536. switch (frequency) {
  4537. case 144000:
  4538. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4539. ratio = BXT_DE_PLL_RATIO(60);
  4540. break;
  4541. case 288000:
  4542. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4543. ratio = BXT_DE_PLL_RATIO(60);
  4544. break;
  4545. case 384000:
  4546. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4547. ratio = BXT_DE_PLL_RATIO(60);
  4548. break;
  4549. case 576000:
  4550. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4551. ratio = BXT_DE_PLL_RATIO(60);
  4552. break;
  4553. case 624000:
  4554. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4555. ratio = BXT_DE_PLL_RATIO(65);
  4556. break;
  4557. case 19200:
  4558. /*
  4559. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4560. * to suppress GCC warning.
  4561. */
  4562. ratio = 0;
  4563. divider = 0;
  4564. break;
  4565. default:
  4566. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4567. return;
  4568. }
  4569. mutex_lock(&dev_priv->rps.hw_lock);
  4570. /* Inform power controller of upcoming frequency change */
  4571. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4572. 0x80000000);
  4573. mutex_unlock(&dev_priv->rps.hw_lock);
  4574. if (ret) {
  4575. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4576. ret, frequency);
  4577. return;
  4578. }
  4579. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4580. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4581. current_freq = current_freq * 500 + 1000;
  4582. /*
  4583. * DE PLL has to be disabled when
  4584. * - setting to 19.2MHz (bypass, PLL isn't used)
  4585. * - before setting to 624MHz (PLL needs toggling)
  4586. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4587. */
  4588. if (frequency == 19200 || frequency == 624000 ||
  4589. current_freq == 624000) {
  4590. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4591. /* Timeout 200us */
  4592. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4593. 1))
  4594. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4595. }
  4596. if (frequency != 19200) {
  4597. uint32_t val;
  4598. val = I915_READ(BXT_DE_PLL_CTL);
  4599. val &= ~BXT_DE_PLL_RATIO_MASK;
  4600. val |= ratio;
  4601. I915_WRITE(BXT_DE_PLL_CTL, val);
  4602. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4603. /* Timeout 200us */
  4604. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4605. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4606. val = I915_READ(CDCLK_CTL);
  4607. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4608. val |= divider;
  4609. /*
  4610. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4611. * enable otherwise.
  4612. */
  4613. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4614. if (frequency >= 500000)
  4615. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4616. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4617. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4618. val |= (frequency - 1000) / 500;
  4619. I915_WRITE(CDCLK_CTL, val);
  4620. }
  4621. mutex_lock(&dev_priv->rps.hw_lock);
  4622. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4623. DIV_ROUND_UP(frequency, 25000));
  4624. mutex_unlock(&dev_priv->rps.hw_lock);
  4625. if (ret) {
  4626. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4627. ret, frequency);
  4628. return;
  4629. }
  4630. intel_update_cdclk(dev_priv->dev);
  4631. }
  4632. static bool broxton_cdclk_is_enabled(struct drm_i915_private *dev_priv)
  4633. {
  4634. if (!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE))
  4635. return false;
  4636. /* TODO: Check for a valid CDCLK rate */
  4637. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_REQUEST)) {
  4638. DRM_DEBUG_DRIVER("CDCLK enabled, but DBUF power not requested\n");
  4639. return false;
  4640. }
  4641. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE)) {
  4642. DRM_DEBUG_DRIVER("CDCLK enabled, but DBUF power hasn't settled\n");
  4643. return false;
  4644. }
  4645. return true;
  4646. }
  4647. bool broxton_cdclk_verify_state(struct drm_i915_private *dev_priv)
  4648. {
  4649. return broxton_cdclk_is_enabled(dev_priv);
  4650. }
  4651. void broxton_init_cdclk(struct drm_i915_private *dev_priv)
  4652. {
  4653. /* check if cd clock is enabled */
  4654. if (broxton_cdclk_is_enabled(dev_priv)) {
  4655. DRM_DEBUG_KMS("CDCLK already enabled, won't reprogram it\n");
  4656. return;
  4657. }
  4658. DRM_DEBUG_KMS("CDCLK not enabled, enabling it\n");
  4659. /*
  4660. * FIXME:
  4661. * - The initial CDCLK needs to be read from VBT.
  4662. * Need to make this change after VBT has changes for BXT.
  4663. * - check if setting the max (or any) cdclk freq is really necessary
  4664. * here, it belongs to modeset time
  4665. */
  4666. broxton_set_cdclk(dev_priv, 624000);
  4667. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4668. POSTING_READ(DBUF_CTL);
  4669. udelay(10);
  4670. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4671. DRM_ERROR("DBuf power enable timeout!\n");
  4672. }
  4673. void broxton_uninit_cdclk(struct drm_i915_private *dev_priv)
  4674. {
  4675. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4676. POSTING_READ(DBUF_CTL);
  4677. udelay(10);
  4678. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4679. DRM_ERROR("DBuf power disable timeout!\n");
  4680. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4681. broxton_set_cdclk(dev_priv, 19200);
  4682. }
  4683. static const struct skl_cdclk_entry {
  4684. unsigned int freq;
  4685. unsigned int vco;
  4686. } skl_cdclk_frequencies[] = {
  4687. { .freq = 308570, .vco = 8640 },
  4688. { .freq = 337500, .vco = 8100 },
  4689. { .freq = 432000, .vco = 8640 },
  4690. { .freq = 450000, .vco = 8100 },
  4691. { .freq = 540000, .vco = 8100 },
  4692. { .freq = 617140, .vco = 8640 },
  4693. { .freq = 675000, .vco = 8100 },
  4694. };
  4695. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4696. {
  4697. return (freq - 1000) / 500;
  4698. }
  4699. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4700. {
  4701. unsigned int i;
  4702. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4703. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4704. if (e->freq == freq)
  4705. return e->vco;
  4706. }
  4707. return 8100;
  4708. }
  4709. static void
  4710. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4711. {
  4712. unsigned int min_freq;
  4713. u32 val;
  4714. /* select the minimum CDCLK before enabling DPLL 0 */
  4715. val = I915_READ(CDCLK_CTL);
  4716. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4717. val |= CDCLK_FREQ_337_308;
  4718. if (required_vco == 8640)
  4719. min_freq = 308570;
  4720. else
  4721. min_freq = 337500;
  4722. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4723. I915_WRITE(CDCLK_CTL, val);
  4724. POSTING_READ(CDCLK_CTL);
  4725. /*
  4726. * We always enable DPLL0 with the lowest link rate possible, but still
  4727. * taking into account the VCO required to operate the eDP panel at the
  4728. * desired frequency. The usual DP link rates operate with a VCO of
  4729. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4730. * The modeset code is responsible for the selection of the exact link
  4731. * rate later on, with the constraint of choosing a frequency that
  4732. * works with required_vco.
  4733. */
  4734. val = I915_READ(DPLL_CTRL1);
  4735. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4736. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4737. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4738. if (required_vco == 8640)
  4739. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4740. SKL_DPLL0);
  4741. else
  4742. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4743. SKL_DPLL0);
  4744. I915_WRITE(DPLL_CTRL1, val);
  4745. POSTING_READ(DPLL_CTRL1);
  4746. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4747. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4748. DRM_ERROR("DPLL0 not locked\n");
  4749. }
  4750. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4751. {
  4752. int ret;
  4753. u32 val;
  4754. /* inform PCU we want to change CDCLK */
  4755. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4756. mutex_lock(&dev_priv->rps.hw_lock);
  4757. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4758. mutex_unlock(&dev_priv->rps.hw_lock);
  4759. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4760. }
  4761. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4762. {
  4763. unsigned int i;
  4764. for (i = 0; i < 15; i++) {
  4765. if (skl_cdclk_pcu_ready(dev_priv))
  4766. return true;
  4767. udelay(10);
  4768. }
  4769. return false;
  4770. }
  4771. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4772. {
  4773. struct drm_device *dev = dev_priv->dev;
  4774. u32 freq_select, pcu_ack;
  4775. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4776. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4777. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4778. return;
  4779. }
  4780. /* set CDCLK_CTL */
  4781. switch(freq) {
  4782. case 450000:
  4783. case 432000:
  4784. freq_select = CDCLK_FREQ_450_432;
  4785. pcu_ack = 1;
  4786. break;
  4787. case 540000:
  4788. freq_select = CDCLK_FREQ_540;
  4789. pcu_ack = 2;
  4790. break;
  4791. case 308570:
  4792. case 337500:
  4793. default:
  4794. freq_select = CDCLK_FREQ_337_308;
  4795. pcu_ack = 0;
  4796. break;
  4797. case 617140:
  4798. case 675000:
  4799. freq_select = CDCLK_FREQ_675_617;
  4800. pcu_ack = 3;
  4801. break;
  4802. }
  4803. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4804. POSTING_READ(CDCLK_CTL);
  4805. /* inform PCU of the change */
  4806. mutex_lock(&dev_priv->rps.hw_lock);
  4807. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4808. mutex_unlock(&dev_priv->rps.hw_lock);
  4809. intel_update_cdclk(dev);
  4810. }
  4811. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4812. {
  4813. /* disable DBUF power */
  4814. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4815. POSTING_READ(DBUF_CTL);
  4816. udelay(10);
  4817. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4818. DRM_ERROR("DBuf power disable timeout\n");
  4819. /* disable DPLL0 */
  4820. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4821. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4822. DRM_ERROR("Couldn't disable DPLL0\n");
  4823. }
  4824. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4825. {
  4826. unsigned int required_vco;
  4827. /* DPLL0 not enabled (happens on early BIOS versions) */
  4828. if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
  4829. /* enable DPLL0 */
  4830. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4831. skl_dpll0_enable(dev_priv, required_vco);
  4832. }
  4833. /* set CDCLK to the frequency the BIOS chose */
  4834. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4835. /* enable DBUF power */
  4836. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4837. POSTING_READ(DBUF_CTL);
  4838. udelay(10);
  4839. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4840. DRM_ERROR("DBuf power enable timeout\n");
  4841. }
  4842. int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4843. {
  4844. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  4845. uint32_t cdctl = I915_READ(CDCLK_CTL);
  4846. int freq = dev_priv->skl_boot_cdclk;
  4847. /*
  4848. * check if the pre-os intialized the display
  4849. * There is SWF18 scratchpad register defined which is set by the
  4850. * pre-os which can be used by the OS drivers to check the status
  4851. */
  4852. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4853. goto sanitize;
  4854. /* Is PLL enabled and locked ? */
  4855. if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
  4856. goto sanitize;
  4857. /* DPLL okay; verify the cdclock
  4858. *
  4859. * Noticed in some instances that the freq selection is correct but
  4860. * decimal part is programmed wrong from BIOS where pre-os does not
  4861. * enable display. Verify the same as well.
  4862. */
  4863. if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
  4864. /* All well; nothing to sanitize */
  4865. return false;
  4866. sanitize:
  4867. /*
  4868. * As of now initialize with max cdclk till
  4869. * we get dynamic cdclk support
  4870. * */
  4871. dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
  4872. skl_init_cdclk(dev_priv);
  4873. /* we did have to sanitize */
  4874. return true;
  4875. }
  4876. /* Adjust CDclk dividers to allow high res or save power if possible */
  4877. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4878. {
  4879. struct drm_i915_private *dev_priv = dev->dev_private;
  4880. u32 val, cmd;
  4881. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4882. != dev_priv->cdclk_freq);
  4883. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4884. cmd = 2;
  4885. else if (cdclk == 266667)
  4886. cmd = 1;
  4887. else
  4888. cmd = 0;
  4889. mutex_lock(&dev_priv->rps.hw_lock);
  4890. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4891. val &= ~DSPFREQGUAR_MASK;
  4892. val |= (cmd << DSPFREQGUAR_SHIFT);
  4893. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4894. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4895. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4896. 50)) {
  4897. DRM_ERROR("timed out waiting for CDclk change\n");
  4898. }
  4899. mutex_unlock(&dev_priv->rps.hw_lock);
  4900. mutex_lock(&dev_priv->sb_lock);
  4901. if (cdclk == 400000) {
  4902. u32 divider;
  4903. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4904. /* adjust cdclk divider */
  4905. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4906. val &= ~CCK_FREQUENCY_VALUES;
  4907. val |= divider;
  4908. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4909. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4910. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4911. 50))
  4912. DRM_ERROR("timed out waiting for CDclk change\n");
  4913. }
  4914. /* adjust self-refresh exit latency value */
  4915. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4916. val &= ~0x7f;
  4917. /*
  4918. * For high bandwidth configs, we set a higher latency in the bunit
  4919. * so that the core display fetch happens in time to avoid underruns.
  4920. */
  4921. if (cdclk == 400000)
  4922. val |= 4500 / 250; /* 4.5 usec */
  4923. else
  4924. val |= 3000 / 250; /* 3.0 usec */
  4925. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4926. mutex_unlock(&dev_priv->sb_lock);
  4927. intel_update_cdclk(dev);
  4928. }
  4929. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4930. {
  4931. struct drm_i915_private *dev_priv = dev->dev_private;
  4932. u32 val, cmd;
  4933. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4934. != dev_priv->cdclk_freq);
  4935. switch (cdclk) {
  4936. case 333333:
  4937. case 320000:
  4938. case 266667:
  4939. case 200000:
  4940. break;
  4941. default:
  4942. MISSING_CASE(cdclk);
  4943. return;
  4944. }
  4945. /*
  4946. * Specs are full of misinformation, but testing on actual
  4947. * hardware has shown that we just need to write the desired
  4948. * CCK divider into the Punit register.
  4949. */
  4950. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4951. mutex_lock(&dev_priv->rps.hw_lock);
  4952. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4953. val &= ~DSPFREQGUAR_MASK_CHV;
  4954. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4955. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4956. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4957. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4958. 50)) {
  4959. DRM_ERROR("timed out waiting for CDclk change\n");
  4960. }
  4961. mutex_unlock(&dev_priv->rps.hw_lock);
  4962. intel_update_cdclk(dev);
  4963. }
  4964. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4965. int max_pixclk)
  4966. {
  4967. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4968. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4969. /*
  4970. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4971. * 200MHz
  4972. * 267MHz
  4973. * 320/333MHz (depends on HPLL freq)
  4974. * 400MHz (VLV only)
  4975. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4976. * of the lower bin and adjust if needed.
  4977. *
  4978. * We seem to get an unstable or solid color picture at 200MHz.
  4979. * Not sure what's wrong. For now use 200MHz only when all pipes
  4980. * are off.
  4981. */
  4982. if (!IS_CHERRYVIEW(dev_priv) &&
  4983. max_pixclk > freq_320*limit/100)
  4984. return 400000;
  4985. else if (max_pixclk > 266667*limit/100)
  4986. return freq_320;
  4987. else if (max_pixclk > 0)
  4988. return 266667;
  4989. else
  4990. return 200000;
  4991. }
  4992. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4993. int max_pixclk)
  4994. {
  4995. /*
  4996. * FIXME:
  4997. * - remove the guardband, it's not needed on BXT
  4998. * - set 19.2MHz bypass frequency if there are no active pipes
  4999. */
  5000. if (max_pixclk > 576000*9/10)
  5001. return 624000;
  5002. else if (max_pixclk > 384000*9/10)
  5003. return 576000;
  5004. else if (max_pixclk > 288000*9/10)
  5005. return 384000;
  5006. else if (max_pixclk > 144000*9/10)
  5007. return 288000;
  5008. else
  5009. return 144000;
  5010. }
  5011. /* Compute the max pixel clock for new configuration. */
  5012. static int intel_mode_max_pixclk(struct drm_device *dev,
  5013. struct drm_atomic_state *state)
  5014. {
  5015. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5016. struct drm_i915_private *dev_priv = dev->dev_private;
  5017. struct drm_crtc *crtc;
  5018. struct drm_crtc_state *crtc_state;
  5019. unsigned max_pixclk = 0, i;
  5020. enum pipe pipe;
  5021. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5022. sizeof(intel_state->min_pixclk));
  5023. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5024. int pixclk = 0;
  5025. if (crtc_state->enable)
  5026. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5027. intel_state->min_pixclk[i] = pixclk;
  5028. }
  5029. for_each_pipe(dev_priv, pipe)
  5030. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5031. return max_pixclk;
  5032. }
  5033. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5034. {
  5035. struct drm_device *dev = state->dev;
  5036. struct drm_i915_private *dev_priv = dev->dev_private;
  5037. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5038. struct intel_atomic_state *intel_state =
  5039. to_intel_atomic_state(state);
  5040. if (max_pixclk < 0)
  5041. return max_pixclk;
  5042. intel_state->cdclk = intel_state->dev_cdclk =
  5043. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5044. if (!intel_state->active_crtcs)
  5045. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5046. return 0;
  5047. }
  5048. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5049. {
  5050. struct drm_device *dev = state->dev;
  5051. struct drm_i915_private *dev_priv = dev->dev_private;
  5052. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5053. struct intel_atomic_state *intel_state =
  5054. to_intel_atomic_state(state);
  5055. if (max_pixclk < 0)
  5056. return max_pixclk;
  5057. intel_state->cdclk = intel_state->dev_cdclk =
  5058. broxton_calc_cdclk(dev_priv, max_pixclk);
  5059. if (!intel_state->active_crtcs)
  5060. intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
  5061. return 0;
  5062. }
  5063. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5064. {
  5065. unsigned int credits, default_credits;
  5066. if (IS_CHERRYVIEW(dev_priv))
  5067. default_credits = PFI_CREDIT(12);
  5068. else
  5069. default_credits = PFI_CREDIT(8);
  5070. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5071. /* CHV suggested value is 31 or 63 */
  5072. if (IS_CHERRYVIEW(dev_priv))
  5073. credits = PFI_CREDIT_63;
  5074. else
  5075. credits = PFI_CREDIT(15);
  5076. } else {
  5077. credits = default_credits;
  5078. }
  5079. /*
  5080. * WA - write default credits before re-programming
  5081. * FIXME: should we also set the resend bit here?
  5082. */
  5083. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5084. default_credits);
  5085. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5086. credits | PFI_CREDIT_RESEND);
  5087. /*
  5088. * FIXME is this guaranteed to clear
  5089. * immediately or should we poll for it?
  5090. */
  5091. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5092. }
  5093. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5094. {
  5095. struct drm_device *dev = old_state->dev;
  5096. struct drm_i915_private *dev_priv = dev->dev_private;
  5097. struct intel_atomic_state *old_intel_state =
  5098. to_intel_atomic_state(old_state);
  5099. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5100. /*
  5101. * FIXME: We can end up here with all power domains off, yet
  5102. * with a CDCLK frequency other than the minimum. To account
  5103. * for this take the PIPE-A power domain, which covers the HW
  5104. * blocks needed for the following programming. This can be
  5105. * removed once it's guaranteed that we get here either with
  5106. * the minimum CDCLK set, or the required power domains
  5107. * enabled.
  5108. */
  5109. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5110. if (IS_CHERRYVIEW(dev))
  5111. cherryview_set_cdclk(dev, req_cdclk);
  5112. else
  5113. valleyview_set_cdclk(dev, req_cdclk);
  5114. vlv_program_pfi_credits(dev_priv);
  5115. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5116. }
  5117. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5118. {
  5119. struct drm_device *dev = crtc->dev;
  5120. struct drm_i915_private *dev_priv = to_i915(dev);
  5121. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5122. struct intel_encoder *encoder;
  5123. struct intel_crtc_state *pipe_config =
  5124. to_intel_crtc_state(crtc->state);
  5125. int pipe = intel_crtc->pipe;
  5126. if (WARN_ON(intel_crtc->active))
  5127. return;
  5128. if (intel_crtc->config->has_dp_encoder)
  5129. intel_dp_set_m_n(intel_crtc, M1_N1);
  5130. intel_set_pipe_timings(intel_crtc);
  5131. intel_set_pipe_src_size(intel_crtc);
  5132. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5133. struct drm_i915_private *dev_priv = dev->dev_private;
  5134. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5135. I915_WRITE(CHV_CANVAS(pipe), 0);
  5136. }
  5137. i9xx_set_pipeconf(intel_crtc);
  5138. intel_crtc->active = true;
  5139. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5140. for_each_encoder_on_crtc(dev, crtc, encoder)
  5141. if (encoder->pre_pll_enable)
  5142. encoder->pre_pll_enable(encoder);
  5143. if (IS_CHERRYVIEW(dev)) {
  5144. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5145. chv_enable_pll(intel_crtc, intel_crtc->config);
  5146. } else {
  5147. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5148. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5149. }
  5150. for_each_encoder_on_crtc(dev, crtc, encoder)
  5151. if (encoder->pre_enable)
  5152. encoder->pre_enable(encoder);
  5153. i9xx_pfit_enable(intel_crtc);
  5154. intel_color_load_luts(&pipe_config->base);
  5155. intel_update_watermarks(crtc);
  5156. intel_enable_pipe(intel_crtc);
  5157. assert_vblank_disabled(crtc);
  5158. drm_crtc_vblank_on(crtc);
  5159. for_each_encoder_on_crtc(dev, crtc, encoder)
  5160. encoder->enable(encoder);
  5161. }
  5162. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5163. {
  5164. struct drm_device *dev = crtc->base.dev;
  5165. struct drm_i915_private *dev_priv = dev->dev_private;
  5166. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5167. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5168. }
  5169. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5170. {
  5171. struct drm_device *dev = crtc->dev;
  5172. struct drm_i915_private *dev_priv = to_i915(dev);
  5173. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5174. struct intel_encoder *encoder;
  5175. struct intel_crtc_state *pipe_config =
  5176. to_intel_crtc_state(crtc->state);
  5177. enum pipe pipe = intel_crtc->pipe;
  5178. if (WARN_ON(intel_crtc->active))
  5179. return;
  5180. i9xx_set_pll_dividers(intel_crtc);
  5181. if (intel_crtc->config->has_dp_encoder)
  5182. intel_dp_set_m_n(intel_crtc, M1_N1);
  5183. intel_set_pipe_timings(intel_crtc);
  5184. intel_set_pipe_src_size(intel_crtc);
  5185. i9xx_set_pipeconf(intel_crtc);
  5186. intel_crtc->active = true;
  5187. if (!IS_GEN2(dev))
  5188. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5189. for_each_encoder_on_crtc(dev, crtc, encoder)
  5190. if (encoder->pre_enable)
  5191. encoder->pre_enable(encoder);
  5192. i9xx_enable_pll(intel_crtc);
  5193. i9xx_pfit_enable(intel_crtc);
  5194. intel_color_load_luts(&pipe_config->base);
  5195. intel_update_watermarks(crtc);
  5196. intel_enable_pipe(intel_crtc);
  5197. assert_vblank_disabled(crtc);
  5198. drm_crtc_vblank_on(crtc);
  5199. for_each_encoder_on_crtc(dev, crtc, encoder)
  5200. encoder->enable(encoder);
  5201. }
  5202. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5203. {
  5204. struct drm_device *dev = crtc->base.dev;
  5205. struct drm_i915_private *dev_priv = dev->dev_private;
  5206. if (!crtc->config->gmch_pfit.control)
  5207. return;
  5208. assert_pipe_disabled(dev_priv, crtc->pipe);
  5209. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5210. I915_READ(PFIT_CONTROL));
  5211. I915_WRITE(PFIT_CONTROL, 0);
  5212. }
  5213. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5214. {
  5215. struct drm_device *dev = crtc->dev;
  5216. struct drm_i915_private *dev_priv = dev->dev_private;
  5217. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5218. struct intel_encoder *encoder;
  5219. int pipe = intel_crtc->pipe;
  5220. /*
  5221. * On gen2 planes are double buffered but the pipe isn't, so we must
  5222. * wait for planes to fully turn off before disabling the pipe.
  5223. */
  5224. if (IS_GEN2(dev))
  5225. intel_wait_for_vblank(dev, pipe);
  5226. for_each_encoder_on_crtc(dev, crtc, encoder)
  5227. encoder->disable(encoder);
  5228. drm_crtc_vblank_off(crtc);
  5229. assert_vblank_disabled(crtc);
  5230. intel_disable_pipe(intel_crtc);
  5231. i9xx_pfit_disable(intel_crtc);
  5232. for_each_encoder_on_crtc(dev, crtc, encoder)
  5233. if (encoder->post_disable)
  5234. encoder->post_disable(encoder);
  5235. if (!intel_crtc->config->has_dsi_encoder) {
  5236. if (IS_CHERRYVIEW(dev))
  5237. chv_disable_pll(dev_priv, pipe);
  5238. else if (IS_VALLEYVIEW(dev))
  5239. vlv_disable_pll(dev_priv, pipe);
  5240. else
  5241. i9xx_disable_pll(intel_crtc);
  5242. }
  5243. for_each_encoder_on_crtc(dev, crtc, encoder)
  5244. if (encoder->post_pll_disable)
  5245. encoder->post_pll_disable(encoder);
  5246. if (!IS_GEN2(dev))
  5247. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5248. }
  5249. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5250. {
  5251. struct intel_encoder *encoder;
  5252. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5253. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5254. enum intel_display_power_domain domain;
  5255. unsigned long domains;
  5256. if (!intel_crtc->active)
  5257. return;
  5258. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5259. WARN_ON(intel_crtc->unpin_work);
  5260. intel_pre_disable_primary_noatomic(crtc);
  5261. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5262. to_intel_plane_state(crtc->primary->state)->visible = false;
  5263. }
  5264. dev_priv->display.crtc_disable(crtc);
  5265. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
  5266. crtc->base.id);
  5267. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5268. crtc->state->active = false;
  5269. intel_crtc->active = false;
  5270. crtc->enabled = false;
  5271. crtc->state->connector_mask = 0;
  5272. crtc->state->encoder_mask = 0;
  5273. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5274. encoder->base.crtc = NULL;
  5275. intel_fbc_disable(intel_crtc);
  5276. intel_update_watermarks(crtc);
  5277. intel_disable_shared_dpll(intel_crtc);
  5278. domains = intel_crtc->enabled_power_domains;
  5279. for_each_power_domain(domain, domains)
  5280. intel_display_power_put(dev_priv, domain);
  5281. intel_crtc->enabled_power_domains = 0;
  5282. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5283. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5284. }
  5285. /*
  5286. * turn all crtc's off, but do not adjust state
  5287. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5288. */
  5289. int intel_display_suspend(struct drm_device *dev)
  5290. {
  5291. struct drm_i915_private *dev_priv = to_i915(dev);
  5292. struct drm_atomic_state *state;
  5293. int ret;
  5294. state = drm_atomic_helper_suspend(dev);
  5295. ret = PTR_ERR_OR_ZERO(state);
  5296. if (ret)
  5297. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5298. else
  5299. dev_priv->modeset_restore_state = state;
  5300. return ret;
  5301. }
  5302. void intel_encoder_destroy(struct drm_encoder *encoder)
  5303. {
  5304. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5305. drm_encoder_cleanup(encoder);
  5306. kfree(intel_encoder);
  5307. }
  5308. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5309. * internal consistency). */
  5310. static void intel_connector_verify_state(struct intel_connector *connector)
  5311. {
  5312. struct drm_crtc *crtc = connector->base.state->crtc;
  5313. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5314. connector->base.base.id,
  5315. connector->base.name);
  5316. if (connector->get_hw_state(connector)) {
  5317. struct intel_encoder *encoder = connector->encoder;
  5318. struct drm_connector_state *conn_state = connector->base.state;
  5319. I915_STATE_WARN(!crtc,
  5320. "connector enabled without attached crtc\n");
  5321. if (!crtc)
  5322. return;
  5323. I915_STATE_WARN(!crtc->state->active,
  5324. "connector is active, but attached crtc isn't\n");
  5325. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5326. return;
  5327. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5328. "atomic encoder doesn't match attached encoder\n");
  5329. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5330. "attached encoder crtc differs from connector crtc\n");
  5331. } else {
  5332. I915_STATE_WARN(crtc && crtc->state->active,
  5333. "attached crtc is active, but connector isn't\n");
  5334. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5335. "best encoder set without crtc!\n");
  5336. }
  5337. }
  5338. int intel_connector_init(struct intel_connector *connector)
  5339. {
  5340. drm_atomic_helper_connector_reset(&connector->base);
  5341. if (!connector->base.state)
  5342. return -ENOMEM;
  5343. return 0;
  5344. }
  5345. struct intel_connector *intel_connector_alloc(void)
  5346. {
  5347. struct intel_connector *connector;
  5348. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5349. if (!connector)
  5350. return NULL;
  5351. if (intel_connector_init(connector) < 0) {
  5352. kfree(connector);
  5353. return NULL;
  5354. }
  5355. return connector;
  5356. }
  5357. /* Simple connector->get_hw_state implementation for encoders that support only
  5358. * one connector and no cloning and hence the encoder state determines the state
  5359. * of the connector. */
  5360. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5361. {
  5362. enum pipe pipe = 0;
  5363. struct intel_encoder *encoder = connector->encoder;
  5364. return encoder->get_hw_state(encoder, &pipe);
  5365. }
  5366. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5367. {
  5368. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5369. return crtc_state->fdi_lanes;
  5370. return 0;
  5371. }
  5372. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5373. struct intel_crtc_state *pipe_config)
  5374. {
  5375. struct drm_atomic_state *state = pipe_config->base.state;
  5376. struct intel_crtc *other_crtc;
  5377. struct intel_crtc_state *other_crtc_state;
  5378. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5379. pipe_name(pipe), pipe_config->fdi_lanes);
  5380. if (pipe_config->fdi_lanes > 4) {
  5381. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5382. pipe_name(pipe), pipe_config->fdi_lanes);
  5383. return -EINVAL;
  5384. }
  5385. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5386. if (pipe_config->fdi_lanes > 2) {
  5387. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5388. pipe_config->fdi_lanes);
  5389. return -EINVAL;
  5390. } else {
  5391. return 0;
  5392. }
  5393. }
  5394. if (INTEL_INFO(dev)->num_pipes == 2)
  5395. return 0;
  5396. /* Ivybridge 3 pipe is really complicated */
  5397. switch (pipe) {
  5398. case PIPE_A:
  5399. return 0;
  5400. case PIPE_B:
  5401. if (pipe_config->fdi_lanes <= 2)
  5402. return 0;
  5403. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5404. other_crtc_state =
  5405. intel_atomic_get_crtc_state(state, other_crtc);
  5406. if (IS_ERR(other_crtc_state))
  5407. return PTR_ERR(other_crtc_state);
  5408. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5409. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5410. pipe_name(pipe), pipe_config->fdi_lanes);
  5411. return -EINVAL;
  5412. }
  5413. return 0;
  5414. case PIPE_C:
  5415. if (pipe_config->fdi_lanes > 2) {
  5416. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5417. pipe_name(pipe), pipe_config->fdi_lanes);
  5418. return -EINVAL;
  5419. }
  5420. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5421. other_crtc_state =
  5422. intel_atomic_get_crtc_state(state, other_crtc);
  5423. if (IS_ERR(other_crtc_state))
  5424. return PTR_ERR(other_crtc_state);
  5425. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5426. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5427. return -EINVAL;
  5428. }
  5429. return 0;
  5430. default:
  5431. BUG();
  5432. }
  5433. }
  5434. #define RETRY 1
  5435. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5436. struct intel_crtc_state *pipe_config)
  5437. {
  5438. struct drm_device *dev = intel_crtc->base.dev;
  5439. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5440. int lane, link_bw, fdi_dotclock, ret;
  5441. bool needs_recompute = false;
  5442. retry:
  5443. /* FDI is a binary signal running at ~2.7GHz, encoding
  5444. * each output octet as 10 bits. The actual frequency
  5445. * is stored as a divider into a 100MHz clock, and the
  5446. * mode pixel clock is stored in units of 1KHz.
  5447. * Hence the bw of each lane in terms of the mode signal
  5448. * is:
  5449. */
  5450. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5451. fdi_dotclock = adjusted_mode->crtc_clock;
  5452. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5453. pipe_config->pipe_bpp);
  5454. pipe_config->fdi_lanes = lane;
  5455. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5456. link_bw, &pipe_config->fdi_m_n);
  5457. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5458. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5459. pipe_config->pipe_bpp -= 2*3;
  5460. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5461. pipe_config->pipe_bpp);
  5462. needs_recompute = true;
  5463. pipe_config->bw_constrained = true;
  5464. goto retry;
  5465. }
  5466. if (needs_recompute)
  5467. return RETRY;
  5468. return ret;
  5469. }
  5470. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5471. struct intel_crtc_state *pipe_config)
  5472. {
  5473. if (pipe_config->pipe_bpp > 24)
  5474. return false;
  5475. /* HSW can handle pixel rate up to cdclk? */
  5476. if (IS_HASWELL(dev_priv))
  5477. return true;
  5478. /*
  5479. * We compare against max which means we must take
  5480. * the increased cdclk requirement into account when
  5481. * calculating the new cdclk.
  5482. *
  5483. * Should measure whether using a lower cdclk w/o IPS
  5484. */
  5485. return ilk_pipe_pixel_rate(pipe_config) <=
  5486. dev_priv->max_cdclk_freq * 95 / 100;
  5487. }
  5488. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5489. struct intel_crtc_state *pipe_config)
  5490. {
  5491. struct drm_device *dev = crtc->base.dev;
  5492. struct drm_i915_private *dev_priv = dev->dev_private;
  5493. pipe_config->ips_enabled = i915.enable_ips &&
  5494. hsw_crtc_supports_ips(crtc) &&
  5495. pipe_config_supports_ips(dev_priv, pipe_config);
  5496. }
  5497. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5498. {
  5499. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5500. /* GDG double wide on either pipe, otherwise pipe A only */
  5501. return INTEL_INFO(dev_priv)->gen < 4 &&
  5502. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5503. }
  5504. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5505. struct intel_crtc_state *pipe_config)
  5506. {
  5507. struct drm_device *dev = crtc->base.dev;
  5508. struct drm_i915_private *dev_priv = dev->dev_private;
  5509. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5510. /* FIXME should check pixel clock limits on all platforms */
  5511. if (INTEL_INFO(dev)->gen < 4) {
  5512. int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5513. /*
  5514. * Enable double wide mode when the dot clock
  5515. * is > 90% of the (display) core speed.
  5516. */
  5517. if (intel_crtc_supports_double_wide(crtc) &&
  5518. adjusted_mode->crtc_clock > clock_limit) {
  5519. clock_limit *= 2;
  5520. pipe_config->double_wide = true;
  5521. }
  5522. if (adjusted_mode->crtc_clock > clock_limit) {
  5523. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5524. adjusted_mode->crtc_clock, clock_limit,
  5525. yesno(pipe_config->double_wide));
  5526. return -EINVAL;
  5527. }
  5528. }
  5529. /*
  5530. * Pipe horizontal size must be even in:
  5531. * - DVO ganged mode
  5532. * - LVDS dual channel mode
  5533. * - Double wide pipe
  5534. */
  5535. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5536. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5537. pipe_config->pipe_src_w &= ~1;
  5538. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5539. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5540. */
  5541. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5542. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5543. return -EINVAL;
  5544. if (HAS_IPS(dev))
  5545. hsw_compute_ips_config(crtc, pipe_config);
  5546. if (pipe_config->has_pch_encoder)
  5547. return ironlake_fdi_compute_config(crtc, pipe_config);
  5548. return 0;
  5549. }
  5550. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5551. {
  5552. struct drm_i915_private *dev_priv = to_i915(dev);
  5553. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5554. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5555. uint32_t linkrate;
  5556. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5557. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5558. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5559. return 540000;
  5560. linkrate = (I915_READ(DPLL_CTRL1) &
  5561. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5562. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5563. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5564. /* vco 8640 */
  5565. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5566. case CDCLK_FREQ_450_432:
  5567. return 432000;
  5568. case CDCLK_FREQ_337_308:
  5569. return 308570;
  5570. case CDCLK_FREQ_675_617:
  5571. return 617140;
  5572. default:
  5573. WARN(1, "Unknown cd freq selection\n");
  5574. }
  5575. } else {
  5576. /* vco 8100 */
  5577. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5578. case CDCLK_FREQ_450_432:
  5579. return 450000;
  5580. case CDCLK_FREQ_337_308:
  5581. return 337500;
  5582. case CDCLK_FREQ_675_617:
  5583. return 675000;
  5584. default:
  5585. WARN(1, "Unknown cd freq selection\n");
  5586. }
  5587. }
  5588. /* error case, do as if DPLL0 isn't enabled */
  5589. return 24000;
  5590. }
  5591. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5592. {
  5593. struct drm_i915_private *dev_priv = to_i915(dev);
  5594. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5595. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5596. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5597. int cdclk;
  5598. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5599. return 19200;
  5600. cdclk = 19200 * pll_ratio / 2;
  5601. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5602. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5603. return cdclk; /* 576MHz or 624MHz */
  5604. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5605. return cdclk * 2 / 3; /* 384MHz */
  5606. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5607. return cdclk / 2; /* 288MHz */
  5608. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5609. return cdclk / 4; /* 144MHz */
  5610. }
  5611. /* error case, do as if DE PLL isn't enabled */
  5612. return 19200;
  5613. }
  5614. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5615. {
  5616. struct drm_i915_private *dev_priv = dev->dev_private;
  5617. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5618. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5619. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5620. return 800000;
  5621. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5622. return 450000;
  5623. else if (freq == LCPLL_CLK_FREQ_450)
  5624. return 450000;
  5625. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5626. return 540000;
  5627. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5628. return 337500;
  5629. else
  5630. return 675000;
  5631. }
  5632. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5633. {
  5634. struct drm_i915_private *dev_priv = dev->dev_private;
  5635. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5636. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5637. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5638. return 800000;
  5639. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5640. return 450000;
  5641. else if (freq == LCPLL_CLK_FREQ_450)
  5642. return 450000;
  5643. else if (IS_HSW_ULT(dev))
  5644. return 337500;
  5645. else
  5646. return 540000;
  5647. }
  5648. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5649. {
  5650. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5651. CCK_DISPLAY_CLOCK_CONTROL);
  5652. }
  5653. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5654. {
  5655. return 450000;
  5656. }
  5657. static int i945_get_display_clock_speed(struct drm_device *dev)
  5658. {
  5659. return 400000;
  5660. }
  5661. static int i915_get_display_clock_speed(struct drm_device *dev)
  5662. {
  5663. return 333333;
  5664. }
  5665. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5666. {
  5667. return 200000;
  5668. }
  5669. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5670. {
  5671. u16 gcfgc = 0;
  5672. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5673. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5674. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5675. return 266667;
  5676. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5677. return 333333;
  5678. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5679. return 444444;
  5680. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5681. return 200000;
  5682. default:
  5683. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5684. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5685. return 133333;
  5686. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5687. return 166667;
  5688. }
  5689. }
  5690. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5691. {
  5692. u16 gcfgc = 0;
  5693. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5694. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5695. return 133333;
  5696. else {
  5697. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5698. case GC_DISPLAY_CLOCK_333_MHZ:
  5699. return 333333;
  5700. default:
  5701. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5702. return 190000;
  5703. }
  5704. }
  5705. }
  5706. static int i865_get_display_clock_speed(struct drm_device *dev)
  5707. {
  5708. return 266667;
  5709. }
  5710. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5711. {
  5712. u16 hpllcc = 0;
  5713. /*
  5714. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5715. * encoding is different :(
  5716. * FIXME is this the right way to detect 852GM/852GMV?
  5717. */
  5718. if (dev->pdev->revision == 0x1)
  5719. return 133333;
  5720. pci_bus_read_config_word(dev->pdev->bus,
  5721. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5722. /* Assume that the hardware is in the high speed state. This
  5723. * should be the default.
  5724. */
  5725. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5726. case GC_CLOCK_133_200:
  5727. case GC_CLOCK_133_200_2:
  5728. case GC_CLOCK_100_200:
  5729. return 200000;
  5730. case GC_CLOCK_166_250:
  5731. return 250000;
  5732. case GC_CLOCK_100_133:
  5733. return 133333;
  5734. case GC_CLOCK_133_266:
  5735. case GC_CLOCK_133_266_2:
  5736. case GC_CLOCK_166_266:
  5737. return 266667;
  5738. }
  5739. /* Shouldn't happen */
  5740. return 0;
  5741. }
  5742. static int i830_get_display_clock_speed(struct drm_device *dev)
  5743. {
  5744. return 133333;
  5745. }
  5746. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5747. {
  5748. struct drm_i915_private *dev_priv = dev->dev_private;
  5749. static const unsigned int blb_vco[8] = {
  5750. [0] = 3200000,
  5751. [1] = 4000000,
  5752. [2] = 5333333,
  5753. [3] = 4800000,
  5754. [4] = 6400000,
  5755. };
  5756. static const unsigned int pnv_vco[8] = {
  5757. [0] = 3200000,
  5758. [1] = 4000000,
  5759. [2] = 5333333,
  5760. [3] = 4800000,
  5761. [4] = 2666667,
  5762. };
  5763. static const unsigned int cl_vco[8] = {
  5764. [0] = 3200000,
  5765. [1] = 4000000,
  5766. [2] = 5333333,
  5767. [3] = 6400000,
  5768. [4] = 3333333,
  5769. [5] = 3566667,
  5770. [6] = 4266667,
  5771. };
  5772. static const unsigned int elk_vco[8] = {
  5773. [0] = 3200000,
  5774. [1] = 4000000,
  5775. [2] = 5333333,
  5776. [3] = 4800000,
  5777. };
  5778. static const unsigned int ctg_vco[8] = {
  5779. [0] = 3200000,
  5780. [1] = 4000000,
  5781. [2] = 5333333,
  5782. [3] = 6400000,
  5783. [4] = 2666667,
  5784. [5] = 4266667,
  5785. };
  5786. const unsigned int *vco_table;
  5787. unsigned int vco;
  5788. uint8_t tmp = 0;
  5789. /* FIXME other chipsets? */
  5790. if (IS_GM45(dev))
  5791. vco_table = ctg_vco;
  5792. else if (IS_G4X(dev))
  5793. vco_table = elk_vco;
  5794. else if (IS_CRESTLINE(dev))
  5795. vco_table = cl_vco;
  5796. else if (IS_PINEVIEW(dev))
  5797. vco_table = pnv_vco;
  5798. else if (IS_G33(dev))
  5799. vco_table = blb_vco;
  5800. else
  5801. return 0;
  5802. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5803. vco = vco_table[tmp & 0x7];
  5804. if (vco == 0)
  5805. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5806. else
  5807. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5808. return vco;
  5809. }
  5810. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5811. {
  5812. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5813. uint16_t tmp = 0;
  5814. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5815. cdclk_sel = (tmp >> 12) & 0x1;
  5816. switch (vco) {
  5817. case 2666667:
  5818. case 4000000:
  5819. case 5333333:
  5820. return cdclk_sel ? 333333 : 222222;
  5821. case 3200000:
  5822. return cdclk_sel ? 320000 : 228571;
  5823. default:
  5824. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5825. return 222222;
  5826. }
  5827. }
  5828. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5829. {
  5830. static const uint8_t div_3200[] = { 16, 10, 8 };
  5831. static const uint8_t div_4000[] = { 20, 12, 10 };
  5832. static const uint8_t div_5333[] = { 24, 16, 14 };
  5833. const uint8_t *div_table;
  5834. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5835. uint16_t tmp = 0;
  5836. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5837. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5838. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5839. goto fail;
  5840. switch (vco) {
  5841. case 3200000:
  5842. div_table = div_3200;
  5843. break;
  5844. case 4000000:
  5845. div_table = div_4000;
  5846. break;
  5847. case 5333333:
  5848. div_table = div_5333;
  5849. break;
  5850. default:
  5851. goto fail;
  5852. }
  5853. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5854. fail:
  5855. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5856. return 200000;
  5857. }
  5858. static int g33_get_display_clock_speed(struct drm_device *dev)
  5859. {
  5860. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5861. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5862. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5863. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5864. const uint8_t *div_table;
  5865. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5866. uint16_t tmp = 0;
  5867. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5868. cdclk_sel = (tmp >> 4) & 0x7;
  5869. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5870. goto fail;
  5871. switch (vco) {
  5872. case 3200000:
  5873. div_table = div_3200;
  5874. break;
  5875. case 4000000:
  5876. div_table = div_4000;
  5877. break;
  5878. case 4800000:
  5879. div_table = div_4800;
  5880. break;
  5881. case 5333333:
  5882. div_table = div_5333;
  5883. break;
  5884. default:
  5885. goto fail;
  5886. }
  5887. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5888. fail:
  5889. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5890. return 190476;
  5891. }
  5892. static void
  5893. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5894. {
  5895. while (*num > DATA_LINK_M_N_MASK ||
  5896. *den > DATA_LINK_M_N_MASK) {
  5897. *num >>= 1;
  5898. *den >>= 1;
  5899. }
  5900. }
  5901. static void compute_m_n(unsigned int m, unsigned int n,
  5902. uint32_t *ret_m, uint32_t *ret_n)
  5903. {
  5904. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5905. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5906. intel_reduce_m_n_ratio(ret_m, ret_n);
  5907. }
  5908. void
  5909. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5910. int pixel_clock, int link_clock,
  5911. struct intel_link_m_n *m_n)
  5912. {
  5913. m_n->tu = 64;
  5914. compute_m_n(bits_per_pixel * pixel_clock,
  5915. link_clock * nlanes * 8,
  5916. &m_n->gmch_m, &m_n->gmch_n);
  5917. compute_m_n(pixel_clock, link_clock,
  5918. &m_n->link_m, &m_n->link_n);
  5919. }
  5920. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5921. {
  5922. if (i915.panel_use_ssc >= 0)
  5923. return i915.panel_use_ssc != 0;
  5924. return dev_priv->vbt.lvds_use_ssc
  5925. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5926. }
  5927. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5928. {
  5929. return (1 << dpll->n) << 16 | dpll->m2;
  5930. }
  5931. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5932. {
  5933. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5934. }
  5935. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5936. struct intel_crtc_state *crtc_state,
  5937. intel_clock_t *reduced_clock)
  5938. {
  5939. struct drm_device *dev = crtc->base.dev;
  5940. u32 fp, fp2 = 0;
  5941. if (IS_PINEVIEW(dev)) {
  5942. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5943. if (reduced_clock)
  5944. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5945. } else {
  5946. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5947. if (reduced_clock)
  5948. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5949. }
  5950. crtc_state->dpll_hw_state.fp0 = fp;
  5951. crtc->lowfreq_avail = false;
  5952. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5953. reduced_clock) {
  5954. crtc_state->dpll_hw_state.fp1 = fp2;
  5955. crtc->lowfreq_avail = true;
  5956. } else {
  5957. crtc_state->dpll_hw_state.fp1 = fp;
  5958. }
  5959. }
  5960. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5961. pipe)
  5962. {
  5963. u32 reg_val;
  5964. /*
  5965. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5966. * and set it to a reasonable value instead.
  5967. */
  5968. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5969. reg_val &= 0xffffff00;
  5970. reg_val |= 0x00000030;
  5971. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5972. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5973. reg_val &= 0x8cffffff;
  5974. reg_val = 0x8c000000;
  5975. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5976. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5977. reg_val &= 0xffffff00;
  5978. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5979. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5980. reg_val &= 0x00ffffff;
  5981. reg_val |= 0xb0000000;
  5982. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5983. }
  5984. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5985. struct intel_link_m_n *m_n)
  5986. {
  5987. struct drm_device *dev = crtc->base.dev;
  5988. struct drm_i915_private *dev_priv = dev->dev_private;
  5989. int pipe = crtc->pipe;
  5990. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5991. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5992. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5993. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5994. }
  5995. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5996. struct intel_link_m_n *m_n,
  5997. struct intel_link_m_n *m2_n2)
  5998. {
  5999. struct drm_device *dev = crtc->base.dev;
  6000. struct drm_i915_private *dev_priv = dev->dev_private;
  6001. int pipe = crtc->pipe;
  6002. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6003. if (INTEL_INFO(dev)->gen >= 5) {
  6004. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6005. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6006. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6007. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6008. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6009. * for gen < 8) and if DRRS is supported (to make sure the
  6010. * registers are not unnecessarily accessed).
  6011. */
  6012. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6013. crtc->config->has_drrs) {
  6014. I915_WRITE(PIPE_DATA_M2(transcoder),
  6015. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6016. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6017. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6018. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6019. }
  6020. } else {
  6021. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6022. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6023. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6024. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6025. }
  6026. }
  6027. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6028. {
  6029. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6030. if (m_n == M1_N1) {
  6031. dp_m_n = &crtc->config->dp_m_n;
  6032. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6033. } else if (m_n == M2_N2) {
  6034. /*
  6035. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6036. * needs to be programmed into M1_N1.
  6037. */
  6038. dp_m_n = &crtc->config->dp_m2_n2;
  6039. } else {
  6040. DRM_ERROR("Unsupported divider value\n");
  6041. return;
  6042. }
  6043. if (crtc->config->has_pch_encoder)
  6044. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6045. else
  6046. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6047. }
  6048. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6049. struct intel_crtc_state *pipe_config)
  6050. {
  6051. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6052. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6053. if (crtc->pipe != PIPE_A)
  6054. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6055. /* DPLL not used with DSI, but still need the rest set up */
  6056. if (!pipe_config->has_dsi_encoder)
  6057. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6058. DPLL_EXT_BUFFER_ENABLE_VLV;
  6059. pipe_config->dpll_hw_state.dpll_md =
  6060. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6061. }
  6062. static void chv_compute_dpll(struct intel_crtc *crtc,
  6063. struct intel_crtc_state *pipe_config)
  6064. {
  6065. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6066. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6067. if (crtc->pipe != PIPE_A)
  6068. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6069. /* DPLL not used with DSI, but still need the rest set up */
  6070. if (!pipe_config->has_dsi_encoder)
  6071. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6072. pipe_config->dpll_hw_state.dpll_md =
  6073. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6074. }
  6075. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6076. const struct intel_crtc_state *pipe_config)
  6077. {
  6078. struct drm_device *dev = crtc->base.dev;
  6079. struct drm_i915_private *dev_priv = dev->dev_private;
  6080. enum pipe pipe = crtc->pipe;
  6081. u32 mdiv;
  6082. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6083. u32 coreclk, reg_val;
  6084. /* Enable Refclk */
  6085. I915_WRITE(DPLL(pipe),
  6086. pipe_config->dpll_hw_state.dpll &
  6087. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6088. /* No need to actually set up the DPLL with DSI */
  6089. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6090. return;
  6091. mutex_lock(&dev_priv->sb_lock);
  6092. bestn = pipe_config->dpll.n;
  6093. bestm1 = pipe_config->dpll.m1;
  6094. bestm2 = pipe_config->dpll.m2;
  6095. bestp1 = pipe_config->dpll.p1;
  6096. bestp2 = pipe_config->dpll.p2;
  6097. /* See eDP HDMI DPIO driver vbios notes doc */
  6098. /* PLL B needs special handling */
  6099. if (pipe == PIPE_B)
  6100. vlv_pllb_recal_opamp(dev_priv, pipe);
  6101. /* Set up Tx target for periodic Rcomp update */
  6102. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6103. /* Disable target IRef on PLL */
  6104. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6105. reg_val &= 0x00ffffff;
  6106. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6107. /* Disable fast lock */
  6108. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6109. /* Set idtafcrecal before PLL is enabled */
  6110. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6111. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6112. mdiv |= ((bestn << DPIO_N_SHIFT));
  6113. mdiv |= (1 << DPIO_K_SHIFT);
  6114. /*
  6115. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6116. * but we don't support that).
  6117. * Note: don't use the DAC post divider as it seems unstable.
  6118. */
  6119. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6120. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6121. mdiv |= DPIO_ENABLE_CALIBRATION;
  6122. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6123. /* Set HBR and RBR LPF coefficients */
  6124. if (pipe_config->port_clock == 162000 ||
  6125. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6126. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6127. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6128. 0x009f0003);
  6129. else
  6130. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6131. 0x00d0000f);
  6132. if (pipe_config->has_dp_encoder) {
  6133. /* Use SSC source */
  6134. if (pipe == PIPE_A)
  6135. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6136. 0x0df40000);
  6137. else
  6138. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6139. 0x0df70000);
  6140. } else { /* HDMI or VGA */
  6141. /* Use bend source */
  6142. if (pipe == PIPE_A)
  6143. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6144. 0x0df70000);
  6145. else
  6146. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6147. 0x0df40000);
  6148. }
  6149. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6150. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6151. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6152. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6153. coreclk |= 0x01000000;
  6154. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6155. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6156. mutex_unlock(&dev_priv->sb_lock);
  6157. }
  6158. static void chv_prepare_pll(struct intel_crtc *crtc,
  6159. const struct intel_crtc_state *pipe_config)
  6160. {
  6161. struct drm_device *dev = crtc->base.dev;
  6162. struct drm_i915_private *dev_priv = dev->dev_private;
  6163. enum pipe pipe = crtc->pipe;
  6164. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6165. u32 loopfilter, tribuf_calcntr;
  6166. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6167. u32 dpio_val;
  6168. int vco;
  6169. /* Enable Refclk and SSC */
  6170. I915_WRITE(DPLL(pipe),
  6171. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6172. /* No need to actually set up the DPLL with DSI */
  6173. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6174. return;
  6175. bestn = pipe_config->dpll.n;
  6176. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6177. bestm1 = pipe_config->dpll.m1;
  6178. bestm2 = pipe_config->dpll.m2 >> 22;
  6179. bestp1 = pipe_config->dpll.p1;
  6180. bestp2 = pipe_config->dpll.p2;
  6181. vco = pipe_config->dpll.vco;
  6182. dpio_val = 0;
  6183. loopfilter = 0;
  6184. mutex_lock(&dev_priv->sb_lock);
  6185. /* p1 and p2 divider */
  6186. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6187. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6188. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6189. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6190. 1 << DPIO_CHV_K_DIV_SHIFT);
  6191. /* Feedback post-divider - m2 */
  6192. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6193. /* Feedback refclk divider - n and m1 */
  6194. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6195. DPIO_CHV_M1_DIV_BY_2 |
  6196. 1 << DPIO_CHV_N_DIV_SHIFT);
  6197. /* M2 fraction division */
  6198. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6199. /* M2 fraction division enable */
  6200. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6201. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6202. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6203. if (bestm2_frac)
  6204. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6205. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6206. /* Program digital lock detect threshold */
  6207. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6208. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6209. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6210. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6211. if (!bestm2_frac)
  6212. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6213. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6214. /* Loop filter */
  6215. if (vco == 5400000) {
  6216. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6217. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6218. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6219. tribuf_calcntr = 0x9;
  6220. } else if (vco <= 6200000) {
  6221. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6222. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6223. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6224. tribuf_calcntr = 0x9;
  6225. } else if (vco <= 6480000) {
  6226. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6227. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6228. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6229. tribuf_calcntr = 0x8;
  6230. } else {
  6231. /* Not supported. Apply the same limits as in the max case */
  6232. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6233. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6234. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6235. tribuf_calcntr = 0;
  6236. }
  6237. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6238. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6239. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6240. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6241. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6242. /* AFC Recal */
  6243. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6244. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6245. DPIO_AFC_RECAL);
  6246. mutex_unlock(&dev_priv->sb_lock);
  6247. }
  6248. /**
  6249. * vlv_force_pll_on - forcibly enable just the PLL
  6250. * @dev_priv: i915 private structure
  6251. * @pipe: pipe PLL to enable
  6252. * @dpll: PLL configuration
  6253. *
  6254. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6255. * in cases where we need the PLL enabled even when @pipe is not going to
  6256. * be enabled.
  6257. */
  6258. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6259. const struct dpll *dpll)
  6260. {
  6261. struct intel_crtc *crtc =
  6262. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6263. struct intel_crtc_state *pipe_config;
  6264. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6265. if (!pipe_config)
  6266. return -ENOMEM;
  6267. pipe_config->base.crtc = &crtc->base;
  6268. pipe_config->pixel_multiplier = 1;
  6269. pipe_config->dpll = *dpll;
  6270. if (IS_CHERRYVIEW(dev)) {
  6271. chv_compute_dpll(crtc, pipe_config);
  6272. chv_prepare_pll(crtc, pipe_config);
  6273. chv_enable_pll(crtc, pipe_config);
  6274. } else {
  6275. vlv_compute_dpll(crtc, pipe_config);
  6276. vlv_prepare_pll(crtc, pipe_config);
  6277. vlv_enable_pll(crtc, pipe_config);
  6278. }
  6279. kfree(pipe_config);
  6280. return 0;
  6281. }
  6282. /**
  6283. * vlv_force_pll_off - forcibly disable just the PLL
  6284. * @dev_priv: i915 private structure
  6285. * @pipe: pipe PLL to disable
  6286. *
  6287. * Disable the PLL for @pipe. To be used in cases where we need
  6288. * the PLL enabled even when @pipe is not going to be enabled.
  6289. */
  6290. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6291. {
  6292. if (IS_CHERRYVIEW(dev))
  6293. chv_disable_pll(to_i915(dev), pipe);
  6294. else
  6295. vlv_disable_pll(to_i915(dev), pipe);
  6296. }
  6297. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6298. struct intel_crtc_state *crtc_state,
  6299. intel_clock_t *reduced_clock)
  6300. {
  6301. struct drm_device *dev = crtc->base.dev;
  6302. struct drm_i915_private *dev_priv = dev->dev_private;
  6303. u32 dpll;
  6304. bool is_sdvo;
  6305. struct dpll *clock = &crtc_state->dpll;
  6306. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6307. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6308. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6309. dpll = DPLL_VGA_MODE_DIS;
  6310. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6311. dpll |= DPLLB_MODE_LVDS;
  6312. else
  6313. dpll |= DPLLB_MODE_DAC_SERIAL;
  6314. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6315. dpll |= (crtc_state->pixel_multiplier - 1)
  6316. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6317. }
  6318. if (is_sdvo)
  6319. dpll |= DPLL_SDVO_HIGH_SPEED;
  6320. if (crtc_state->has_dp_encoder)
  6321. dpll |= DPLL_SDVO_HIGH_SPEED;
  6322. /* compute bitmask from p1 value */
  6323. if (IS_PINEVIEW(dev))
  6324. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6325. else {
  6326. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6327. if (IS_G4X(dev) && reduced_clock)
  6328. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6329. }
  6330. switch (clock->p2) {
  6331. case 5:
  6332. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6333. break;
  6334. case 7:
  6335. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6336. break;
  6337. case 10:
  6338. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6339. break;
  6340. case 14:
  6341. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6342. break;
  6343. }
  6344. if (INTEL_INFO(dev)->gen >= 4)
  6345. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6346. if (crtc_state->sdvo_tv_clock)
  6347. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6348. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6349. intel_panel_use_ssc(dev_priv))
  6350. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6351. else
  6352. dpll |= PLL_REF_INPUT_DREFCLK;
  6353. dpll |= DPLL_VCO_ENABLE;
  6354. crtc_state->dpll_hw_state.dpll = dpll;
  6355. if (INTEL_INFO(dev)->gen >= 4) {
  6356. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6357. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6358. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6359. }
  6360. }
  6361. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6362. struct intel_crtc_state *crtc_state,
  6363. intel_clock_t *reduced_clock)
  6364. {
  6365. struct drm_device *dev = crtc->base.dev;
  6366. struct drm_i915_private *dev_priv = dev->dev_private;
  6367. u32 dpll;
  6368. struct dpll *clock = &crtc_state->dpll;
  6369. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6370. dpll = DPLL_VGA_MODE_DIS;
  6371. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6372. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6373. } else {
  6374. if (clock->p1 == 2)
  6375. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6376. else
  6377. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6378. if (clock->p2 == 4)
  6379. dpll |= PLL_P2_DIVIDE_BY_4;
  6380. }
  6381. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6382. dpll |= DPLL_DVO_2X_MODE;
  6383. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6384. intel_panel_use_ssc(dev_priv))
  6385. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6386. else
  6387. dpll |= PLL_REF_INPUT_DREFCLK;
  6388. dpll |= DPLL_VCO_ENABLE;
  6389. crtc_state->dpll_hw_state.dpll = dpll;
  6390. }
  6391. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6392. {
  6393. struct drm_device *dev = intel_crtc->base.dev;
  6394. struct drm_i915_private *dev_priv = dev->dev_private;
  6395. enum pipe pipe = intel_crtc->pipe;
  6396. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6397. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6398. uint32_t crtc_vtotal, crtc_vblank_end;
  6399. int vsyncshift = 0;
  6400. /* We need to be careful not to changed the adjusted mode, for otherwise
  6401. * the hw state checker will get angry at the mismatch. */
  6402. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6403. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6404. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6405. /* the chip adds 2 halflines automatically */
  6406. crtc_vtotal -= 1;
  6407. crtc_vblank_end -= 1;
  6408. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6409. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6410. else
  6411. vsyncshift = adjusted_mode->crtc_hsync_start -
  6412. adjusted_mode->crtc_htotal / 2;
  6413. if (vsyncshift < 0)
  6414. vsyncshift += adjusted_mode->crtc_htotal;
  6415. }
  6416. if (INTEL_INFO(dev)->gen > 3)
  6417. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6418. I915_WRITE(HTOTAL(cpu_transcoder),
  6419. (adjusted_mode->crtc_hdisplay - 1) |
  6420. ((adjusted_mode->crtc_htotal - 1) << 16));
  6421. I915_WRITE(HBLANK(cpu_transcoder),
  6422. (adjusted_mode->crtc_hblank_start - 1) |
  6423. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6424. I915_WRITE(HSYNC(cpu_transcoder),
  6425. (adjusted_mode->crtc_hsync_start - 1) |
  6426. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6427. I915_WRITE(VTOTAL(cpu_transcoder),
  6428. (adjusted_mode->crtc_vdisplay - 1) |
  6429. ((crtc_vtotal - 1) << 16));
  6430. I915_WRITE(VBLANK(cpu_transcoder),
  6431. (adjusted_mode->crtc_vblank_start - 1) |
  6432. ((crtc_vblank_end - 1) << 16));
  6433. I915_WRITE(VSYNC(cpu_transcoder),
  6434. (adjusted_mode->crtc_vsync_start - 1) |
  6435. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6436. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6437. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6438. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6439. * bits. */
  6440. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6441. (pipe == PIPE_B || pipe == PIPE_C))
  6442. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6443. }
  6444. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6445. {
  6446. struct drm_device *dev = intel_crtc->base.dev;
  6447. struct drm_i915_private *dev_priv = dev->dev_private;
  6448. enum pipe pipe = intel_crtc->pipe;
  6449. /* pipesrc controls the size that is scaled from, which should
  6450. * always be the user's requested size.
  6451. */
  6452. I915_WRITE(PIPESRC(pipe),
  6453. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6454. (intel_crtc->config->pipe_src_h - 1));
  6455. }
  6456. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6457. struct intel_crtc_state *pipe_config)
  6458. {
  6459. struct drm_device *dev = crtc->base.dev;
  6460. struct drm_i915_private *dev_priv = dev->dev_private;
  6461. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6462. uint32_t tmp;
  6463. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6464. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6465. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6466. tmp = I915_READ(HBLANK(cpu_transcoder));
  6467. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6468. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6469. tmp = I915_READ(HSYNC(cpu_transcoder));
  6470. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6471. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6472. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6473. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6474. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6475. tmp = I915_READ(VBLANK(cpu_transcoder));
  6476. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6477. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6478. tmp = I915_READ(VSYNC(cpu_transcoder));
  6479. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6480. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6481. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6482. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6483. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6484. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6485. }
  6486. }
  6487. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6488. struct intel_crtc_state *pipe_config)
  6489. {
  6490. struct drm_device *dev = crtc->base.dev;
  6491. struct drm_i915_private *dev_priv = dev->dev_private;
  6492. u32 tmp;
  6493. tmp = I915_READ(PIPESRC(crtc->pipe));
  6494. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6495. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6496. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6497. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6498. }
  6499. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6500. struct intel_crtc_state *pipe_config)
  6501. {
  6502. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6503. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6504. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6505. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6506. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6507. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6508. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6509. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6510. mode->flags = pipe_config->base.adjusted_mode.flags;
  6511. mode->type = DRM_MODE_TYPE_DRIVER;
  6512. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6513. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6514. mode->hsync = drm_mode_hsync(mode);
  6515. mode->vrefresh = drm_mode_vrefresh(mode);
  6516. drm_mode_set_name(mode);
  6517. }
  6518. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6519. {
  6520. struct drm_device *dev = intel_crtc->base.dev;
  6521. struct drm_i915_private *dev_priv = dev->dev_private;
  6522. uint32_t pipeconf;
  6523. pipeconf = 0;
  6524. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6525. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6526. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6527. if (intel_crtc->config->double_wide)
  6528. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6529. /* only g4x and later have fancy bpc/dither controls */
  6530. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6531. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6532. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6533. pipeconf |= PIPECONF_DITHER_EN |
  6534. PIPECONF_DITHER_TYPE_SP;
  6535. switch (intel_crtc->config->pipe_bpp) {
  6536. case 18:
  6537. pipeconf |= PIPECONF_6BPC;
  6538. break;
  6539. case 24:
  6540. pipeconf |= PIPECONF_8BPC;
  6541. break;
  6542. case 30:
  6543. pipeconf |= PIPECONF_10BPC;
  6544. break;
  6545. default:
  6546. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6547. BUG();
  6548. }
  6549. }
  6550. if (HAS_PIPE_CXSR(dev)) {
  6551. if (intel_crtc->lowfreq_avail) {
  6552. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6553. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6554. } else {
  6555. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6556. }
  6557. }
  6558. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6559. if (INTEL_INFO(dev)->gen < 4 ||
  6560. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6561. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6562. else
  6563. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6564. } else
  6565. pipeconf |= PIPECONF_PROGRESSIVE;
  6566. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6567. intel_crtc->config->limited_color_range)
  6568. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6569. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6570. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6571. }
  6572. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6573. struct intel_crtc_state *crtc_state)
  6574. {
  6575. struct drm_device *dev = crtc->base.dev;
  6576. struct drm_i915_private *dev_priv = dev->dev_private;
  6577. const intel_limit_t *limit;
  6578. int refclk = 48000;
  6579. memset(&crtc_state->dpll_hw_state, 0,
  6580. sizeof(crtc_state->dpll_hw_state));
  6581. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6582. if (intel_panel_use_ssc(dev_priv)) {
  6583. refclk = dev_priv->vbt.lvds_ssc_freq;
  6584. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6585. }
  6586. limit = &intel_limits_i8xx_lvds;
  6587. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6588. limit = &intel_limits_i8xx_dvo;
  6589. } else {
  6590. limit = &intel_limits_i8xx_dac;
  6591. }
  6592. if (!crtc_state->clock_set &&
  6593. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6594. refclk, NULL, &crtc_state->dpll)) {
  6595. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6596. return -EINVAL;
  6597. }
  6598. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6599. return 0;
  6600. }
  6601. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6602. struct intel_crtc_state *crtc_state)
  6603. {
  6604. struct drm_device *dev = crtc->base.dev;
  6605. struct drm_i915_private *dev_priv = dev->dev_private;
  6606. const intel_limit_t *limit;
  6607. int refclk = 96000;
  6608. memset(&crtc_state->dpll_hw_state, 0,
  6609. sizeof(crtc_state->dpll_hw_state));
  6610. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6611. if (intel_panel_use_ssc(dev_priv)) {
  6612. refclk = dev_priv->vbt.lvds_ssc_freq;
  6613. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6614. }
  6615. if (intel_is_dual_link_lvds(dev))
  6616. limit = &intel_limits_g4x_dual_channel_lvds;
  6617. else
  6618. limit = &intel_limits_g4x_single_channel_lvds;
  6619. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6620. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6621. limit = &intel_limits_g4x_hdmi;
  6622. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6623. limit = &intel_limits_g4x_sdvo;
  6624. } else {
  6625. /* The option is for other outputs */
  6626. limit = &intel_limits_i9xx_sdvo;
  6627. }
  6628. if (!crtc_state->clock_set &&
  6629. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6630. refclk, NULL, &crtc_state->dpll)) {
  6631. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6632. return -EINVAL;
  6633. }
  6634. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6635. return 0;
  6636. }
  6637. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6638. struct intel_crtc_state *crtc_state)
  6639. {
  6640. struct drm_device *dev = crtc->base.dev;
  6641. struct drm_i915_private *dev_priv = dev->dev_private;
  6642. const intel_limit_t *limit;
  6643. int refclk = 96000;
  6644. memset(&crtc_state->dpll_hw_state, 0,
  6645. sizeof(crtc_state->dpll_hw_state));
  6646. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6647. if (intel_panel_use_ssc(dev_priv)) {
  6648. refclk = dev_priv->vbt.lvds_ssc_freq;
  6649. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6650. }
  6651. limit = &intel_limits_pineview_lvds;
  6652. } else {
  6653. limit = &intel_limits_pineview_sdvo;
  6654. }
  6655. if (!crtc_state->clock_set &&
  6656. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6657. refclk, NULL, &crtc_state->dpll)) {
  6658. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6659. return -EINVAL;
  6660. }
  6661. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6662. return 0;
  6663. }
  6664. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6665. struct intel_crtc_state *crtc_state)
  6666. {
  6667. struct drm_device *dev = crtc->base.dev;
  6668. struct drm_i915_private *dev_priv = dev->dev_private;
  6669. const intel_limit_t *limit;
  6670. int refclk = 96000;
  6671. memset(&crtc_state->dpll_hw_state, 0,
  6672. sizeof(crtc_state->dpll_hw_state));
  6673. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6674. if (intel_panel_use_ssc(dev_priv)) {
  6675. refclk = dev_priv->vbt.lvds_ssc_freq;
  6676. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6677. }
  6678. limit = &intel_limits_i9xx_lvds;
  6679. } else {
  6680. limit = &intel_limits_i9xx_sdvo;
  6681. }
  6682. if (!crtc_state->clock_set &&
  6683. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6684. refclk, NULL, &crtc_state->dpll)) {
  6685. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6686. return -EINVAL;
  6687. }
  6688. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6689. return 0;
  6690. }
  6691. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6692. struct intel_crtc_state *crtc_state)
  6693. {
  6694. int refclk = 100000;
  6695. const intel_limit_t *limit = &intel_limits_chv;
  6696. memset(&crtc_state->dpll_hw_state, 0,
  6697. sizeof(crtc_state->dpll_hw_state));
  6698. if (!crtc_state->clock_set &&
  6699. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6700. refclk, NULL, &crtc_state->dpll)) {
  6701. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6702. return -EINVAL;
  6703. }
  6704. chv_compute_dpll(crtc, crtc_state);
  6705. return 0;
  6706. }
  6707. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6708. struct intel_crtc_state *crtc_state)
  6709. {
  6710. int refclk = 100000;
  6711. const intel_limit_t *limit = &intel_limits_vlv;
  6712. memset(&crtc_state->dpll_hw_state, 0,
  6713. sizeof(crtc_state->dpll_hw_state));
  6714. if (!crtc_state->clock_set &&
  6715. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6716. refclk, NULL, &crtc_state->dpll)) {
  6717. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6718. return -EINVAL;
  6719. }
  6720. vlv_compute_dpll(crtc, crtc_state);
  6721. return 0;
  6722. }
  6723. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6724. struct intel_crtc_state *pipe_config)
  6725. {
  6726. struct drm_device *dev = crtc->base.dev;
  6727. struct drm_i915_private *dev_priv = dev->dev_private;
  6728. uint32_t tmp;
  6729. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6730. return;
  6731. tmp = I915_READ(PFIT_CONTROL);
  6732. if (!(tmp & PFIT_ENABLE))
  6733. return;
  6734. /* Check whether the pfit is attached to our pipe. */
  6735. if (INTEL_INFO(dev)->gen < 4) {
  6736. if (crtc->pipe != PIPE_B)
  6737. return;
  6738. } else {
  6739. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6740. return;
  6741. }
  6742. pipe_config->gmch_pfit.control = tmp;
  6743. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6744. }
  6745. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6746. struct intel_crtc_state *pipe_config)
  6747. {
  6748. struct drm_device *dev = crtc->base.dev;
  6749. struct drm_i915_private *dev_priv = dev->dev_private;
  6750. int pipe = pipe_config->cpu_transcoder;
  6751. intel_clock_t clock;
  6752. u32 mdiv;
  6753. int refclk = 100000;
  6754. /* In case of DSI, DPLL will not be used */
  6755. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6756. return;
  6757. mutex_lock(&dev_priv->sb_lock);
  6758. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6759. mutex_unlock(&dev_priv->sb_lock);
  6760. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6761. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6762. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6763. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6764. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6765. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6766. }
  6767. static void
  6768. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6769. struct intel_initial_plane_config *plane_config)
  6770. {
  6771. struct drm_device *dev = crtc->base.dev;
  6772. struct drm_i915_private *dev_priv = dev->dev_private;
  6773. u32 val, base, offset;
  6774. int pipe = crtc->pipe, plane = crtc->plane;
  6775. int fourcc, pixel_format;
  6776. unsigned int aligned_height;
  6777. struct drm_framebuffer *fb;
  6778. struct intel_framebuffer *intel_fb;
  6779. val = I915_READ(DSPCNTR(plane));
  6780. if (!(val & DISPLAY_PLANE_ENABLE))
  6781. return;
  6782. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6783. if (!intel_fb) {
  6784. DRM_DEBUG_KMS("failed to alloc fb\n");
  6785. return;
  6786. }
  6787. fb = &intel_fb->base;
  6788. if (INTEL_INFO(dev)->gen >= 4) {
  6789. if (val & DISPPLANE_TILED) {
  6790. plane_config->tiling = I915_TILING_X;
  6791. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6792. }
  6793. }
  6794. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6795. fourcc = i9xx_format_to_fourcc(pixel_format);
  6796. fb->pixel_format = fourcc;
  6797. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6798. if (INTEL_INFO(dev)->gen >= 4) {
  6799. if (plane_config->tiling)
  6800. offset = I915_READ(DSPTILEOFF(plane));
  6801. else
  6802. offset = I915_READ(DSPLINOFF(plane));
  6803. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6804. } else {
  6805. base = I915_READ(DSPADDR(plane));
  6806. }
  6807. plane_config->base = base;
  6808. val = I915_READ(PIPESRC(pipe));
  6809. fb->width = ((val >> 16) & 0xfff) + 1;
  6810. fb->height = ((val >> 0) & 0xfff) + 1;
  6811. val = I915_READ(DSPSTRIDE(pipe));
  6812. fb->pitches[0] = val & 0xffffffc0;
  6813. aligned_height = intel_fb_align_height(dev, fb->height,
  6814. fb->pixel_format,
  6815. fb->modifier[0]);
  6816. plane_config->size = fb->pitches[0] * aligned_height;
  6817. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6818. pipe_name(pipe), plane, fb->width, fb->height,
  6819. fb->bits_per_pixel, base, fb->pitches[0],
  6820. plane_config->size);
  6821. plane_config->fb = intel_fb;
  6822. }
  6823. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6824. struct intel_crtc_state *pipe_config)
  6825. {
  6826. struct drm_device *dev = crtc->base.dev;
  6827. struct drm_i915_private *dev_priv = dev->dev_private;
  6828. int pipe = pipe_config->cpu_transcoder;
  6829. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6830. intel_clock_t clock;
  6831. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6832. int refclk = 100000;
  6833. /* In case of DSI, DPLL will not be used */
  6834. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6835. return;
  6836. mutex_lock(&dev_priv->sb_lock);
  6837. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6838. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6839. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6840. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6841. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6842. mutex_unlock(&dev_priv->sb_lock);
  6843. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6844. clock.m2 = (pll_dw0 & 0xff) << 22;
  6845. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6846. clock.m2 |= pll_dw2 & 0x3fffff;
  6847. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6848. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6849. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6850. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6851. }
  6852. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6853. struct intel_crtc_state *pipe_config)
  6854. {
  6855. struct drm_device *dev = crtc->base.dev;
  6856. struct drm_i915_private *dev_priv = dev->dev_private;
  6857. enum intel_display_power_domain power_domain;
  6858. uint32_t tmp;
  6859. bool ret;
  6860. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6861. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6862. return false;
  6863. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6864. pipe_config->shared_dpll = NULL;
  6865. ret = false;
  6866. tmp = I915_READ(PIPECONF(crtc->pipe));
  6867. if (!(tmp & PIPECONF_ENABLE))
  6868. goto out;
  6869. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6870. switch (tmp & PIPECONF_BPC_MASK) {
  6871. case PIPECONF_6BPC:
  6872. pipe_config->pipe_bpp = 18;
  6873. break;
  6874. case PIPECONF_8BPC:
  6875. pipe_config->pipe_bpp = 24;
  6876. break;
  6877. case PIPECONF_10BPC:
  6878. pipe_config->pipe_bpp = 30;
  6879. break;
  6880. default:
  6881. break;
  6882. }
  6883. }
  6884. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6885. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6886. pipe_config->limited_color_range = true;
  6887. if (INTEL_INFO(dev)->gen < 4)
  6888. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6889. intel_get_pipe_timings(crtc, pipe_config);
  6890. intel_get_pipe_src_size(crtc, pipe_config);
  6891. i9xx_get_pfit_config(crtc, pipe_config);
  6892. if (INTEL_INFO(dev)->gen >= 4) {
  6893. /* No way to read it out on pipes B and C */
  6894. if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
  6895. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6896. else
  6897. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6898. pipe_config->pixel_multiplier =
  6899. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6900. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6901. pipe_config->dpll_hw_state.dpll_md = tmp;
  6902. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6903. tmp = I915_READ(DPLL(crtc->pipe));
  6904. pipe_config->pixel_multiplier =
  6905. ((tmp & SDVO_MULTIPLIER_MASK)
  6906. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6907. } else {
  6908. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6909. * port and will be fixed up in the encoder->get_config
  6910. * function. */
  6911. pipe_config->pixel_multiplier = 1;
  6912. }
  6913. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6914. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  6915. /*
  6916. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6917. * on 830. Filter it out here so that we don't
  6918. * report errors due to that.
  6919. */
  6920. if (IS_I830(dev))
  6921. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6922. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6923. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6924. } else {
  6925. /* Mask out read-only status bits. */
  6926. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6927. DPLL_PORTC_READY_MASK |
  6928. DPLL_PORTB_READY_MASK);
  6929. }
  6930. if (IS_CHERRYVIEW(dev))
  6931. chv_crtc_clock_get(crtc, pipe_config);
  6932. else if (IS_VALLEYVIEW(dev))
  6933. vlv_crtc_clock_get(crtc, pipe_config);
  6934. else
  6935. i9xx_crtc_clock_get(crtc, pipe_config);
  6936. /*
  6937. * Normally the dotclock is filled in by the encoder .get_config()
  6938. * but in case the pipe is enabled w/o any ports we need a sane
  6939. * default.
  6940. */
  6941. pipe_config->base.adjusted_mode.crtc_clock =
  6942. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6943. ret = true;
  6944. out:
  6945. intel_display_power_put(dev_priv, power_domain);
  6946. return ret;
  6947. }
  6948. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6949. {
  6950. struct drm_i915_private *dev_priv = dev->dev_private;
  6951. struct intel_encoder *encoder;
  6952. u32 val, final;
  6953. bool has_lvds = false;
  6954. bool has_cpu_edp = false;
  6955. bool has_panel = false;
  6956. bool has_ck505 = false;
  6957. bool can_ssc = false;
  6958. /* We need to take the global config into account */
  6959. for_each_intel_encoder(dev, encoder) {
  6960. switch (encoder->type) {
  6961. case INTEL_OUTPUT_LVDS:
  6962. has_panel = true;
  6963. has_lvds = true;
  6964. break;
  6965. case INTEL_OUTPUT_EDP:
  6966. has_panel = true;
  6967. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6968. has_cpu_edp = true;
  6969. break;
  6970. default:
  6971. break;
  6972. }
  6973. }
  6974. if (HAS_PCH_IBX(dev)) {
  6975. has_ck505 = dev_priv->vbt.display_clock_mode;
  6976. can_ssc = has_ck505;
  6977. } else {
  6978. has_ck505 = false;
  6979. can_ssc = true;
  6980. }
  6981. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6982. has_panel, has_lvds, has_ck505);
  6983. /* Ironlake: try to setup display ref clock before DPLL
  6984. * enabling. This is only under driver's control after
  6985. * PCH B stepping, previous chipset stepping should be
  6986. * ignoring this setting.
  6987. */
  6988. val = I915_READ(PCH_DREF_CONTROL);
  6989. /* As we must carefully and slowly disable/enable each source in turn,
  6990. * compute the final state we want first and check if we need to
  6991. * make any changes at all.
  6992. */
  6993. final = val;
  6994. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6995. if (has_ck505)
  6996. final |= DREF_NONSPREAD_CK505_ENABLE;
  6997. else
  6998. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6999. final &= ~DREF_SSC_SOURCE_MASK;
  7000. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7001. final &= ~DREF_SSC1_ENABLE;
  7002. if (has_panel) {
  7003. final |= DREF_SSC_SOURCE_ENABLE;
  7004. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7005. final |= DREF_SSC1_ENABLE;
  7006. if (has_cpu_edp) {
  7007. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7008. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7009. else
  7010. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7011. } else
  7012. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7013. } else {
  7014. final |= DREF_SSC_SOURCE_DISABLE;
  7015. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7016. }
  7017. if (final == val)
  7018. return;
  7019. /* Always enable nonspread source */
  7020. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7021. if (has_ck505)
  7022. val |= DREF_NONSPREAD_CK505_ENABLE;
  7023. else
  7024. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7025. if (has_panel) {
  7026. val &= ~DREF_SSC_SOURCE_MASK;
  7027. val |= DREF_SSC_SOURCE_ENABLE;
  7028. /* SSC must be turned on before enabling the CPU output */
  7029. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7030. DRM_DEBUG_KMS("Using SSC on panel\n");
  7031. val |= DREF_SSC1_ENABLE;
  7032. } else
  7033. val &= ~DREF_SSC1_ENABLE;
  7034. /* Get SSC going before enabling the outputs */
  7035. I915_WRITE(PCH_DREF_CONTROL, val);
  7036. POSTING_READ(PCH_DREF_CONTROL);
  7037. udelay(200);
  7038. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7039. /* Enable CPU source on CPU attached eDP */
  7040. if (has_cpu_edp) {
  7041. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7042. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7043. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7044. } else
  7045. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7046. } else
  7047. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7048. I915_WRITE(PCH_DREF_CONTROL, val);
  7049. POSTING_READ(PCH_DREF_CONTROL);
  7050. udelay(200);
  7051. } else {
  7052. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  7053. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7054. /* Turn off CPU output */
  7055. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7056. I915_WRITE(PCH_DREF_CONTROL, val);
  7057. POSTING_READ(PCH_DREF_CONTROL);
  7058. udelay(200);
  7059. /* Turn off the SSC source */
  7060. val &= ~DREF_SSC_SOURCE_MASK;
  7061. val |= DREF_SSC_SOURCE_DISABLE;
  7062. /* Turn off SSC1 */
  7063. val &= ~DREF_SSC1_ENABLE;
  7064. I915_WRITE(PCH_DREF_CONTROL, val);
  7065. POSTING_READ(PCH_DREF_CONTROL);
  7066. udelay(200);
  7067. }
  7068. BUG_ON(val != final);
  7069. }
  7070. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7071. {
  7072. uint32_t tmp;
  7073. tmp = I915_READ(SOUTH_CHICKEN2);
  7074. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7075. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7076. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7077. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7078. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7079. tmp = I915_READ(SOUTH_CHICKEN2);
  7080. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7081. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7082. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7083. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7084. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7085. }
  7086. /* WaMPhyProgramming:hsw */
  7087. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7088. {
  7089. uint32_t tmp;
  7090. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7091. tmp &= ~(0xFF << 24);
  7092. tmp |= (0x12 << 24);
  7093. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7094. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7095. tmp |= (1 << 11);
  7096. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7097. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7098. tmp |= (1 << 11);
  7099. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7100. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7101. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7102. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7103. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7104. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7105. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7106. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7107. tmp &= ~(7 << 13);
  7108. tmp |= (5 << 13);
  7109. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7110. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7111. tmp &= ~(7 << 13);
  7112. tmp |= (5 << 13);
  7113. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7114. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7115. tmp &= ~0xFF;
  7116. tmp |= 0x1C;
  7117. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7118. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7119. tmp &= ~0xFF;
  7120. tmp |= 0x1C;
  7121. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7122. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7123. tmp &= ~(0xFF << 16);
  7124. tmp |= (0x1C << 16);
  7125. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7126. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7127. tmp &= ~(0xFF << 16);
  7128. tmp |= (0x1C << 16);
  7129. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7130. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7131. tmp |= (1 << 27);
  7132. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7133. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7134. tmp |= (1 << 27);
  7135. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7136. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7137. tmp &= ~(0xF << 28);
  7138. tmp |= (4 << 28);
  7139. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7140. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7141. tmp &= ~(0xF << 28);
  7142. tmp |= (4 << 28);
  7143. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7144. }
  7145. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7146. * Programming" based on the parameters passed:
  7147. * - Sequence to enable CLKOUT_DP
  7148. * - Sequence to enable CLKOUT_DP without spread
  7149. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7150. */
  7151. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7152. bool with_fdi)
  7153. {
  7154. struct drm_i915_private *dev_priv = dev->dev_private;
  7155. uint32_t reg, tmp;
  7156. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7157. with_spread = true;
  7158. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7159. with_fdi = false;
  7160. mutex_lock(&dev_priv->sb_lock);
  7161. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7162. tmp &= ~SBI_SSCCTL_DISABLE;
  7163. tmp |= SBI_SSCCTL_PATHALT;
  7164. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7165. udelay(24);
  7166. if (with_spread) {
  7167. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7168. tmp &= ~SBI_SSCCTL_PATHALT;
  7169. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7170. if (with_fdi) {
  7171. lpt_reset_fdi_mphy(dev_priv);
  7172. lpt_program_fdi_mphy(dev_priv);
  7173. }
  7174. }
  7175. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7176. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7177. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7178. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7179. mutex_unlock(&dev_priv->sb_lock);
  7180. }
  7181. /* Sequence to disable CLKOUT_DP */
  7182. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7183. {
  7184. struct drm_i915_private *dev_priv = dev->dev_private;
  7185. uint32_t reg, tmp;
  7186. mutex_lock(&dev_priv->sb_lock);
  7187. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7188. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7189. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7190. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7191. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7192. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7193. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7194. tmp |= SBI_SSCCTL_PATHALT;
  7195. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7196. udelay(32);
  7197. }
  7198. tmp |= SBI_SSCCTL_DISABLE;
  7199. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7200. }
  7201. mutex_unlock(&dev_priv->sb_lock);
  7202. }
  7203. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7204. static const uint16_t sscdivintphase[] = {
  7205. [BEND_IDX( 50)] = 0x3B23,
  7206. [BEND_IDX( 45)] = 0x3B23,
  7207. [BEND_IDX( 40)] = 0x3C23,
  7208. [BEND_IDX( 35)] = 0x3C23,
  7209. [BEND_IDX( 30)] = 0x3D23,
  7210. [BEND_IDX( 25)] = 0x3D23,
  7211. [BEND_IDX( 20)] = 0x3E23,
  7212. [BEND_IDX( 15)] = 0x3E23,
  7213. [BEND_IDX( 10)] = 0x3F23,
  7214. [BEND_IDX( 5)] = 0x3F23,
  7215. [BEND_IDX( 0)] = 0x0025,
  7216. [BEND_IDX( -5)] = 0x0025,
  7217. [BEND_IDX(-10)] = 0x0125,
  7218. [BEND_IDX(-15)] = 0x0125,
  7219. [BEND_IDX(-20)] = 0x0225,
  7220. [BEND_IDX(-25)] = 0x0225,
  7221. [BEND_IDX(-30)] = 0x0325,
  7222. [BEND_IDX(-35)] = 0x0325,
  7223. [BEND_IDX(-40)] = 0x0425,
  7224. [BEND_IDX(-45)] = 0x0425,
  7225. [BEND_IDX(-50)] = 0x0525,
  7226. };
  7227. /*
  7228. * Bend CLKOUT_DP
  7229. * steps -50 to 50 inclusive, in steps of 5
  7230. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7231. * change in clock period = -(steps / 10) * 5.787 ps
  7232. */
  7233. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7234. {
  7235. uint32_t tmp;
  7236. int idx = BEND_IDX(steps);
  7237. if (WARN_ON(steps % 5 != 0))
  7238. return;
  7239. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7240. return;
  7241. mutex_lock(&dev_priv->sb_lock);
  7242. if (steps % 10 != 0)
  7243. tmp = 0xAAAAAAAB;
  7244. else
  7245. tmp = 0x00000000;
  7246. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7247. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7248. tmp &= 0xffff0000;
  7249. tmp |= sscdivintphase[idx];
  7250. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7251. mutex_unlock(&dev_priv->sb_lock);
  7252. }
  7253. #undef BEND_IDX
  7254. static void lpt_init_pch_refclk(struct drm_device *dev)
  7255. {
  7256. struct intel_encoder *encoder;
  7257. bool has_vga = false;
  7258. for_each_intel_encoder(dev, encoder) {
  7259. switch (encoder->type) {
  7260. case INTEL_OUTPUT_ANALOG:
  7261. has_vga = true;
  7262. break;
  7263. default:
  7264. break;
  7265. }
  7266. }
  7267. if (has_vga) {
  7268. lpt_bend_clkout_dp(to_i915(dev), 0);
  7269. lpt_enable_clkout_dp(dev, true, true);
  7270. } else {
  7271. lpt_disable_clkout_dp(dev);
  7272. }
  7273. }
  7274. /*
  7275. * Initialize reference clocks when the driver loads
  7276. */
  7277. void intel_init_pch_refclk(struct drm_device *dev)
  7278. {
  7279. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7280. ironlake_init_pch_refclk(dev);
  7281. else if (HAS_PCH_LPT(dev))
  7282. lpt_init_pch_refclk(dev);
  7283. }
  7284. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7285. {
  7286. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7287. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7288. int pipe = intel_crtc->pipe;
  7289. uint32_t val;
  7290. val = 0;
  7291. switch (intel_crtc->config->pipe_bpp) {
  7292. case 18:
  7293. val |= PIPECONF_6BPC;
  7294. break;
  7295. case 24:
  7296. val |= PIPECONF_8BPC;
  7297. break;
  7298. case 30:
  7299. val |= PIPECONF_10BPC;
  7300. break;
  7301. case 36:
  7302. val |= PIPECONF_12BPC;
  7303. break;
  7304. default:
  7305. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7306. BUG();
  7307. }
  7308. if (intel_crtc->config->dither)
  7309. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7310. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7311. val |= PIPECONF_INTERLACED_ILK;
  7312. else
  7313. val |= PIPECONF_PROGRESSIVE;
  7314. if (intel_crtc->config->limited_color_range)
  7315. val |= PIPECONF_COLOR_RANGE_SELECT;
  7316. I915_WRITE(PIPECONF(pipe), val);
  7317. POSTING_READ(PIPECONF(pipe));
  7318. }
  7319. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7320. {
  7321. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7322. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7323. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7324. u32 val = 0;
  7325. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7326. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7327. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7328. val |= PIPECONF_INTERLACED_ILK;
  7329. else
  7330. val |= PIPECONF_PROGRESSIVE;
  7331. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7332. POSTING_READ(PIPECONF(cpu_transcoder));
  7333. }
  7334. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7335. {
  7336. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7337. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7338. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7339. u32 val = 0;
  7340. switch (intel_crtc->config->pipe_bpp) {
  7341. case 18:
  7342. val |= PIPEMISC_DITHER_6_BPC;
  7343. break;
  7344. case 24:
  7345. val |= PIPEMISC_DITHER_8_BPC;
  7346. break;
  7347. case 30:
  7348. val |= PIPEMISC_DITHER_10_BPC;
  7349. break;
  7350. case 36:
  7351. val |= PIPEMISC_DITHER_12_BPC;
  7352. break;
  7353. default:
  7354. /* Case prevented by pipe_config_set_bpp. */
  7355. BUG();
  7356. }
  7357. if (intel_crtc->config->dither)
  7358. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7359. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7360. }
  7361. }
  7362. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7363. {
  7364. /*
  7365. * Account for spread spectrum to avoid
  7366. * oversubscribing the link. Max center spread
  7367. * is 2.5%; use 5% for safety's sake.
  7368. */
  7369. u32 bps = target_clock * bpp * 21 / 20;
  7370. return DIV_ROUND_UP(bps, link_bw * 8);
  7371. }
  7372. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7373. {
  7374. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7375. }
  7376. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7377. struct intel_crtc_state *crtc_state,
  7378. intel_clock_t *reduced_clock)
  7379. {
  7380. struct drm_crtc *crtc = &intel_crtc->base;
  7381. struct drm_device *dev = crtc->dev;
  7382. struct drm_i915_private *dev_priv = dev->dev_private;
  7383. struct drm_atomic_state *state = crtc_state->base.state;
  7384. struct drm_connector *connector;
  7385. struct drm_connector_state *connector_state;
  7386. struct intel_encoder *encoder;
  7387. u32 dpll, fp, fp2;
  7388. int factor, i;
  7389. bool is_lvds = false, is_sdvo = false;
  7390. for_each_connector_in_state(state, connector, connector_state, i) {
  7391. if (connector_state->crtc != crtc_state->base.crtc)
  7392. continue;
  7393. encoder = to_intel_encoder(connector_state->best_encoder);
  7394. switch (encoder->type) {
  7395. case INTEL_OUTPUT_LVDS:
  7396. is_lvds = true;
  7397. break;
  7398. case INTEL_OUTPUT_SDVO:
  7399. case INTEL_OUTPUT_HDMI:
  7400. is_sdvo = true;
  7401. break;
  7402. default:
  7403. break;
  7404. }
  7405. }
  7406. /* Enable autotuning of the PLL clock (if permissible) */
  7407. factor = 21;
  7408. if (is_lvds) {
  7409. if ((intel_panel_use_ssc(dev_priv) &&
  7410. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7411. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7412. factor = 25;
  7413. } else if (crtc_state->sdvo_tv_clock)
  7414. factor = 20;
  7415. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7416. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7417. fp |= FP_CB_TUNE;
  7418. if (reduced_clock) {
  7419. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7420. if (reduced_clock->m < factor * reduced_clock->n)
  7421. fp2 |= FP_CB_TUNE;
  7422. } else {
  7423. fp2 = fp;
  7424. }
  7425. dpll = 0;
  7426. if (is_lvds)
  7427. dpll |= DPLLB_MODE_LVDS;
  7428. else
  7429. dpll |= DPLLB_MODE_DAC_SERIAL;
  7430. dpll |= (crtc_state->pixel_multiplier - 1)
  7431. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7432. if (is_sdvo)
  7433. dpll |= DPLL_SDVO_HIGH_SPEED;
  7434. if (crtc_state->has_dp_encoder)
  7435. dpll |= DPLL_SDVO_HIGH_SPEED;
  7436. /* compute bitmask from p1 value */
  7437. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7438. /* also FPA1 */
  7439. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7440. switch (crtc_state->dpll.p2) {
  7441. case 5:
  7442. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7443. break;
  7444. case 7:
  7445. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7446. break;
  7447. case 10:
  7448. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7449. break;
  7450. case 14:
  7451. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7452. break;
  7453. }
  7454. if (is_lvds && intel_panel_use_ssc(dev_priv))
  7455. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7456. else
  7457. dpll |= PLL_REF_INPUT_DREFCLK;
  7458. dpll |= DPLL_VCO_ENABLE;
  7459. crtc_state->dpll_hw_state.dpll = dpll;
  7460. crtc_state->dpll_hw_state.fp0 = fp;
  7461. crtc_state->dpll_hw_state.fp1 = fp2;
  7462. }
  7463. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7464. struct intel_crtc_state *crtc_state)
  7465. {
  7466. struct drm_device *dev = crtc->base.dev;
  7467. struct drm_i915_private *dev_priv = dev->dev_private;
  7468. intel_clock_t reduced_clock;
  7469. bool has_reduced_clock = false;
  7470. struct intel_shared_dpll *pll;
  7471. const intel_limit_t *limit;
  7472. int refclk = 120000;
  7473. memset(&crtc_state->dpll_hw_state, 0,
  7474. sizeof(crtc_state->dpll_hw_state));
  7475. crtc->lowfreq_avail = false;
  7476. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7477. if (!crtc_state->has_pch_encoder)
  7478. return 0;
  7479. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7480. if (intel_panel_use_ssc(dev_priv)) {
  7481. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7482. dev_priv->vbt.lvds_ssc_freq);
  7483. refclk = dev_priv->vbt.lvds_ssc_freq;
  7484. }
  7485. if (intel_is_dual_link_lvds(dev)) {
  7486. if (refclk == 100000)
  7487. limit = &intel_limits_ironlake_dual_lvds_100m;
  7488. else
  7489. limit = &intel_limits_ironlake_dual_lvds;
  7490. } else {
  7491. if (refclk == 100000)
  7492. limit = &intel_limits_ironlake_single_lvds_100m;
  7493. else
  7494. limit = &intel_limits_ironlake_single_lvds;
  7495. }
  7496. } else {
  7497. limit = &intel_limits_ironlake_dac;
  7498. }
  7499. if (!crtc_state->clock_set &&
  7500. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7501. refclk, NULL, &crtc_state->dpll)) {
  7502. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7503. return -EINVAL;
  7504. }
  7505. ironlake_compute_dpll(crtc, crtc_state,
  7506. has_reduced_clock ? &reduced_clock : NULL);
  7507. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  7508. if (pll == NULL) {
  7509. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7510. pipe_name(crtc->pipe));
  7511. return -EINVAL;
  7512. }
  7513. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7514. has_reduced_clock)
  7515. crtc->lowfreq_avail = true;
  7516. return 0;
  7517. }
  7518. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7519. struct intel_link_m_n *m_n)
  7520. {
  7521. struct drm_device *dev = crtc->base.dev;
  7522. struct drm_i915_private *dev_priv = dev->dev_private;
  7523. enum pipe pipe = crtc->pipe;
  7524. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7525. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7526. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7527. & ~TU_SIZE_MASK;
  7528. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7529. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7530. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7531. }
  7532. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7533. enum transcoder transcoder,
  7534. struct intel_link_m_n *m_n,
  7535. struct intel_link_m_n *m2_n2)
  7536. {
  7537. struct drm_device *dev = crtc->base.dev;
  7538. struct drm_i915_private *dev_priv = dev->dev_private;
  7539. enum pipe pipe = crtc->pipe;
  7540. if (INTEL_INFO(dev)->gen >= 5) {
  7541. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7542. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7543. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7544. & ~TU_SIZE_MASK;
  7545. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7546. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7547. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7548. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7549. * gen < 8) and if DRRS is supported (to make sure the
  7550. * registers are not unnecessarily read).
  7551. */
  7552. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7553. crtc->config->has_drrs) {
  7554. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7555. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7556. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7557. & ~TU_SIZE_MASK;
  7558. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7559. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7560. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7561. }
  7562. } else {
  7563. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7564. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7565. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7566. & ~TU_SIZE_MASK;
  7567. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7568. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7569. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7570. }
  7571. }
  7572. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7573. struct intel_crtc_state *pipe_config)
  7574. {
  7575. if (pipe_config->has_pch_encoder)
  7576. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7577. else
  7578. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7579. &pipe_config->dp_m_n,
  7580. &pipe_config->dp_m2_n2);
  7581. }
  7582. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7583. struct intel_crtc_state *pipe_config)
  7584. {
  7585. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7586. &pipe_config->fdi_m_n, NULL);
  7587. }
  7588. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7589. struct intel_crtc_state *pipe_config)
  7590. {
  7591. struct drm_device *dev = crtc->base.dev;
  7592. struct drm_i915_private *dev_priv = dev->dev_private;
  7593. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7594. uint32_t ps_ctrl = 0;
  7595. int id = -1;
  7596. int i;
  7597. /* find scaler attached to this pipe */
  7598. for (i = 0; i < crtc->num_scalers; i++) {
  7599. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7600. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7601. id = i;
  7602. pipe_config->pch_pfit.enabled = true;
  7603. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7604. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7605. break;
  7606. }
  7607. }
  7608. scaler_state->scaler_id = id;
  7609. if (id >= 0) {
  7610. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7611. } else {
  7612. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7613. }
  7614. }
  7615. static void
  7616. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7617. struct intel_initial_plane_config *plane_config)
  7618. {
  7619. struct drm_device *dev = crtc->base.dev;
  7620. struct drm_i915_private *dev_priv = dev->dev_private;
  7621. u32 val, base, offset, stride_mult, tiling;
  7622. int pipe = crtc->pipe;
  7623. int fourcc, pixel_format;
  7624. unsigned int aligned_height;
  7625. struct drm_framebuffer *fb;
  7626. struct intel_framebuffer *intel_fb;
  7627. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7628. if (!intel_fb) {
  7629. DRM_DEBUG_KMS("failed to alloc fb\n");
  7630. return;
  7631. }
  7632. fb = &intel_fb->base;
  7633. val = I915_READ(PLANE_CTL(pipe, 0));
  7634. if (!(val & PLANE_CTL_ENABLE))
  7635. goto error;
  7636. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7637. fourcc = skl_format_to_fourcc(pixel_format,
  7638. val & PLANE_CTL_ORDER_RGBX,
  7639. val & PLANE_CTL_ALPHA_MASK);
  7640. fb->pixel_format = fourcc;
  7641. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7642. tiling = val & PLANE_CTL_TILED_MASK;
  7643. switch (tiling) {
  7644. case PLANE_CTL_TILED_LINEAR:
  7645. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7646. break;
  7647. case PLANE_CTL_TILED_X:
  7648. plane_config->tiling = I915_TILING_X;
  7649. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7650. break;
  7651. case PLANE_CTL_TILED_Y:
  7652. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7653. break;
  7654. case PLANE_CTL_TILED_YF:
  7655. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7656. break;
  7657. default:
  7658. MISSING_CASE(tiling);
  7659. goto error;
  7660. }
  7661. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7662. plane_config->base = base;
  7663. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7664. val = I915_READ(PLANE_SIZE(pipe, 0));
  7665. fb->height = ((val >> 16) & 0xfff) + 1;
  7666. fb->width = ((val >> 0) & 0x1fff) + 1;
  7667. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7668. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  7669. fb->pixel_format);
  7670. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7671. aligned_height = intel_fb_align_height(dev, fb->height,
  7672. fb->pixel_format,
  7673. fb->modifier[0]);
  7674. plane_config->size = fb->pitches[0] * aligned_height;
  7675. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7676. pipe_name(pipe), fb->width, fb->height,
  7677. fb->bits_per_pixel, base, fb->pitches[0],
  7678. plane_config->size);
  7679. plane_config->fb = intel_fb;
  7680. return;
  7681. error:
  7682. kfree(fb);
  7683. }
  7684. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7685. struct intel_crtc_state *pipe_config)
  7686. {
  7687. struct drm_device *dev = crtc->base.dev;
  7688. struct drm_i915_private *dev_priv = dev->dev_private;
  7689. uint32_t tmp;
  7690. tmp = I915_READ(PF_CTL(crtc->pipe));
  7691. if (tmp & PF_ENABLE) {
  7692. pipe_config->pch_pfit.enabled = true;
  7693. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7694. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7695. /* We currently do not free assignements of panel fitters on
  7696. * ivb/hsw (since we don't use the higher upscaling modes which
  7697. * differentiates them) so just WARN about this case for now. */
  7698. if (IS_GEN7(dev)) {
  7699. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7700. PF_PIPE_SEL_IVB(crtc->pipe));
  7701. }
  7702. }
  7703. }
  7704. static void
  7705. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7706. struct intel_initial_plane_config *plane_config)
  7707. {
  7708. struct drm_device *dev = crtc->base.dev;
  7709. struct drm_i915_private *dev_priv = dev->dev_private;
  7710. u32 val, base, offset;
  7711. int pipe = crtc->pipe;
  7712. int fourcc, pixel_format;
  7713. unsigned int aligned_height;
  7714. struct drm_framebuffer *fb;
  7715. struct intel_framebuffer *intel_fb;
  7716. val = I915_READ(DSPCNTR(pipe));
  7717. if (!(val & DISPLAY_PLANE_ENABLE))
  7718. return;
  7719. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7720. if (!intel_fb) {
  7721. DRM_DEBUG_KMS("failed to alloc fb\n");
  7722. return;
  7723. }
  7724. fb = &intel_fb->base;
  7725. if (INTEL_INFO(dev)->gen >= 4) {
  7726. if (val & DISPPLANE_TILED) {
  7727. plane_config->tiling = I915_TILING_X;
  7728. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7729. }
  7730. }
  7731. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7732. fourcc = i9xx_format_to_fourcc(pixel_format);
  7733. fb->pixel_format = fourcc;
  7734. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7735. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7736. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7737. offset = I915_READ(DSPOFFSET(pipe));
  7738. } else {
  7739. if (plane_config->tiling)
  7740. offset = I915_READ(DSPTILEOFF(pipe));
  7741. else
  7742. offset = I915_READ(DSPLINOFF(pipe));
  7743. }
  7744. plane_config->base = base;
  7745. val = I915_READ(PIPESRC(pipe));
  7746. fb->width = ((val >> 16) & 0xfff) + 1;
  7747. fb->height = ((val >> 0) & 0xfff) + 1;
  7748. val = I915_READ(DSPSTRIDE(pipe));
  7749. fb->pitches[0] = val & 0xffffffc0;
  7750. aligned_height = intel_fb_align_height(dev, fb->height,
  7751. fb->pixel_format,
  7752. fb->modifier[0]);
  7753. plane_config->size = fb->pitches[0] * aligned_height;
  7754. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7755. pipe_name(pipe), fb->width, fb->height,
  7756. fb->bits_per_pixel, base, fb->pitches[0],
  7757. plane_config->size);
  7758. plane_config->fb = intel_fb;
  7759. }
  7760. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7761. struct intel_crtc_state *pipe_config)
  7762. {
  7763. struct drm_device *dev = crtc->base.dev;
  7764. struct drm_i915_private *dev_priv = dev->dev_private;
  7765. enum intel_display_power_domain power_domain;
  7766. uint32_t tmp;
  7767. bool ret;
  7768. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7769. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7770. return false;
  7771. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7772. pipe_config->shared_dpll = NULL;
  7773. ret = false;
  7774. tmp = I915_READ(PIPECONF(crtc->pipe));
  7775. if (!(tmp & PIPECONF_ENABLE))
  7776. goto out;
  7777. switch (tmp & PIPECONF_BPC_MASK) {
  7778. case PIPECONF_6BPC:
  7779. pipe_config->pipe_bpp = 18;
  7780. break;
  7781. case PIPECONF_8BPC:
  7782. pipe_config->pipe_bpp = 24;
  7783. break;
  7784. case PIPECONF_10BPC:
  7785. pipe_config->pipe_bpp = 30;
  7786. break;
  7787. case PIPECONF_12BPC:
  7788. pipe_config->pipe_bpp = 36;
  7789. break;
  7790. default:
  7791. break;
  7792. }
  7793. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7794. pipe_config->limited_color_range = true;
  7795. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7796. struct intel_shared_dpll *pll;
  7797. enum intel_dpll_id pll_id;
  7798. pipe_config->has_pch_encoder = true;
  7799. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7800. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7801. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7802. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7803. if (HAS_PCH_IBX(dev_priv)) {
  7804. pll_id = (enum intel_dpll_id) crtc->pipe;
  7805. } else {
  7806. tmp = I915_READ(PCH_DPLL_SEL);
  7807. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7808. pll_id = DPLL_ID_PCH_PLL_B;
  7809. else
  7810. pll_id= DPLL_ID_PCH_PLL_A;
  7811. }
  7812. pipe_config->shared_dpll =
  7813. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7814. pll = pipe_config->shared_dpll;
  7815. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7816. &pipe_config->dpll_hw_state));
  7817. tmp = pipe_config->dpll_hw_state.dpll;
  7818. pipe_config->pixel_multiplier =
  7819. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7820. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7821. ironlake_pch_clock_get(crtc, pipe_config);
  7822. } else {
  7823. pipe_config->pixel_multiplier = 1;
  7824. }
  7825. intel_get_pipe_timings(crtc, pipe_config);
  7826. intel_get_pipe_src_size(crtc, pipe_config);
  7827. ironlake_get_pfit_config(crtc, pipe_config);
  7828. ret = true;
  7829. out:
  7830. intel_display_power_put(dev_priv, power_domain);
  7831. return ret;
  7832. }
  7833. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7834. {
  7835. struct drm_device *dev = dev_priv->dev;
  7836. struct intel_crtc *crtc;
  7837. for_each_intel_crtc(dev, crtc)
  7838. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7839. pipe_name(crtc->pipe));
  7840. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7841. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7842. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7843. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7844. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7845. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7846. "CPU PWM1 enabled\n");
  7847. if (IS_HASWELL(dev))
  7848. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7849. "CPU PWM2 enabled\n");
  7850. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7851. "PCH PWM1 enabled\n");
  7852. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7853. "Utility pin enabled\n");
  7854. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7855. /*
  7856. * In theory we can still leave IRQs enabled, as long as only the HPD
  7857. * interrupts remain enabled. We used to check for that, but since it's
  7858. * gen-specific and since we only disable LCPLL after we fully disable
  7859. * the interrupts, the check below should be enough.
  7860. */
  7861. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7862. }
  7863. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7864. {
  7865. struct drm_device *dev = dev_priv->dev;
  7866. if (IS_HASWELL(dev))
  7867. return I915_READ(D_COMP_HSW);
  7868. else
  7869. return I915_READ(D_COMP_BDW);
  7870. }
  7871. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7872. {
  7873. struct drm_device *dev = dev_priv->dev;
  7874. if (IS_HASWELL(dev)) {
  7875. mutex_lock(&dev_priv->rps.hw_lock);
  7876. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7877. val))
  7878. DRM_ERROR("Failed to write to D_COMP\n");
  7879. mutex_unlock(&dev_priv->rps.hw_lock);
  7880. } else {
  7881. I915_WRITE(D_COMP_BDW, val);
  7882. POSTING_READ(D_COMP_BDW);
  7883. }
  7884. }
  7885. /*
  7886. * This function implements pieces of two sequences from BSpec:
  7887. * - Sequence for display software to disable LCPLL
  7888. * - Sequence for display software to allow package C8+
  7889. * The steps implemented here are just the steps that actually touch the LCPLL
  7890. * register. Callers should take care of disabling all the display engine
  7891. * functions, doing the mode unset, fixing interrupts, etc.
  7892. */
  7893. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7894. bool switch_to_fclk, bool allow_power_down)
  7895. {
  7896. uint32_t val;
  7897. assert_can_disable_lcpll(dev_priv);
  7898. val = I915_READ(LCPLL_CTL);
  7899. if (switch_to_fclk) {
  7900. val |= LCPLL_CD_SOURCE_FCLK;
  7901. I915_WRITE(LCPLL_CTL, val);
  7902. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7903. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7904. DRM_ERROR("Switching to FCLK failed\n");
  7905. val = I915_READ(LCPLL_CTL);
  7906. }
  7907. val |= LCPLL_PLL_DISABLE;
  7908. I915_WRITE(LCPLL_CTL, val);
  7909. POSTING_READ(LCPLL_CTL);
  7910. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7911. DRM_ERROR("LCPLL still locked\n");
  7912. val = hsw_read_dcomp(dev_priv);
  7913. val |= D_COMP_COMP_DISABLE;
  7914. hsw_write_dcomp(dev_priv, val);
  7915. ndelay(100);
  7916. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7917. 1))
  7918. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7919. if (allow_power_down) {
  7920. val = I915_READ(LCPLL_CTL);
  7921. val |= LCPLL_POWER_DOWN_ALLOW;
  7922. I915_WRITE(LCPLL_CTL, val);
  7923. POSTING_READ(LCPLL_CTL);
  7924. }
  7925. }
  7926. /*
  7927. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7928. * source.
  7929. */
  7930. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7931. {
  7932. uint32_t val;
  7933. val = I915_READ(LCPLL_CTL);
  7934. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7935. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7936. return;
  7937. /*
  7938. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7939. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7940. */
  7941. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7942. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7943. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7944. I915_WRITE(LCPLL_CTL, val);
  7945. POSTING_READ(LCPLL_CTL);
  7946. }
  7947. val = hsw_read_dcomp(dev_priv);
  7948. val |= D_COMP_COMP_FORCE;
  7949. val &= ~D_COMP_COMP_DISABLE;
  7950. hsw_write_dcomp(dev_priv, val);
  7951. val = I915_READ(LCPLL_CTL);
  7952. val &= ~LCPLL_PLL_DISABLE;
  7953. I915_WRITE(LCPLL_CTL, val);
  7954. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7955. DRM_ERROR("LCPLL not locked yet\n");
  7956. if (val & LCPLL_CD_SOURCE_FCLK) {
  7957. val = I915_READ(LCPLL_CTL);
  7958. val &= ~LCPLL_CD_SOURCE_FCLK;
  7959. I915_WRITE(LCPLL_CTL, val);
  7960. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7961. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7962. DRM_ERROR("Switching back to LCPLL failed\n");
  7963. }
  7964. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7965. intel_update_cdclk(dev_priv->dev);
  7966. }
  7967. /*
  7968. * Package states C8 and deeper are really deep PC states that can only be
  7969. * reached when all the devices on the system allow it, so even if the graphics
  7970. * device allows PC8+, it doesn't mean the system will actually get to these
  7971. * states. Our driver only allows PC8+ when going into runtime PM.
  7972. *
  7973. * The requirements for PC8+ are that all the outputs are disabled, the power
  7974. * well is disabled and most interrupts are disabled, and these are also
  7975. * requirements for runtime PM. When these conditions are met, we manually do
  7976. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7977. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7978. * hang the machine.
  7979. *
  7980. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7981. * the state of some registers, so when we come back from PC8+ we need to
  7982. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7983. * need to take care of the registers kept by RC6. Notice that this happens even
  7984. * if we don't put the device in PCI D3 state (which is what currently happens
  7985. * because of the runtime PM support).
  7986. *
  7987. * For more, read "Display Sequences for Package C8" on the hardware
  7988. * documentation.
  7989. */
  7990. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7991. {
  7992. struct drm_device *dev = dev_priv->dev;
  7993. uint32_t val;
  7994. DRM_DEBUG_KMS("Enabling package C8+\n");
  7995. if (HAS_PCH_LPT_LP(dev)) {
  7996. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7997. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7998. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7999. }
  8000. lpt_disable_clkout_dp(dev);
  8001. hsw_disable_lcpll(dev_priv, true, true);
  8002. }
  8003. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8004. {
  8005. struct drm_device *dev = dev_priv->dev;
  8006. uint32_t val;
  8007. DRM_DEBUG_KMS("Disabling package C8+\n");
  8008. hsw_restore_lcpll(dev_priv);
  8009. lpt_init_pch_refclk(dev);
  8010. if (HAS_PCH_LPT_LP(dev)) {
  8011. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8012. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8013. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8014. }
  8015. }
  8016. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8017. {
  8018. struct drm_device *dev = old_state->dev;
  8019. struct intel_atomic_state *old_intel_state =
  8020. to_intel_atomic_state(old_state);
  8021. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8022. broxton_set_cdclk(to_i915(dev), req_cdclk);
  8023. }
  8024. /* compute the max rate for new configuration */
  8025. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8026. {
  8027. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8028. struct drm_i915_private *dev_priv = state->dev->dev_private;
  8029. struct drm_crtc *crtc;
  8030. struct drm_crtc_state *cstate;
  8031. struct intel_crtc_state *crtc_state;
  8032. unsigned max_pixel_rate = 0, i;
  8033. enum pipe pipe;
  8034. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8035. sizeof(intel_state->min_pixclk));
  8036. for_each_crtc_in_state(state, crtc, cstate, i) {
  8037. int pixel_rate;
  8038. crtc_state = to_intel_crtc_state(cstate);
  8039. if (!crtc_state->base.enable) {
  8040. intel_state->min_pixclk[i] = 0;
  8041. continue;
  8042. }
  8043. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8044. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8045. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8046. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8047. intel_state->min_pixclk[i] = pixel_rate;
  8048. }
  8049. for_each_pipe(dev_priv, pipe)
  8050. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8051. return max_pixel_rate;
  8052. }
  8053. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8054. {
  8055. struct drm_i915_private *dev_priv = dev->dev_private;
  8056. uint32_t val, data;
  8057. int ret;
  8058. if (WARN((I915_READ(LCPLL_CTL) &
  8059. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8060. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8061. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8062. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8063. "trying to change cdclk frequency with cdclk not enabled\n"))
  8064. return;
  8065. mutex_lock(&dev_priv->rps.hw_lock);
  8066. ret = sandybridge_pcode_write(dev_priv,
  8067. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8068. mutex_unlock(&dev_priv->rps.hw_lock);
  8069. if (ret) {
  8070. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8071. return;
  8072. }
  8073. val = I915_READ(LCPLL_CTL);
  8074. val |= LCPLL_CD_SOURCE_FCLK;
  8075. I915_WRITE(LCPLL_CTL, val);
  8076. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8077. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8078. DRM_ERROR("Switching to FCLK failed\n");
  8079. val = I915_READ(LCPLL_CTL);
  8080. val &= ~LCPLL_CLK_FREQ_MASK;
  8081. switch (cdclk) {
  8082. case 450000:
  8083. val |= LCPLL_CLK_FREQ_450;
  8084. data = 0;
  8085. break;
  8086. case 540000:
  8087. val |= LCPLL_CLK_FREQ_54O_BDW;
  8088. data = 1;
  8089. break;
  8090. case 337500:
  8091. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8092. data = 2;
  8093. break;
  8094. case 675000:
  8095. val |= LCPLL_CLK_FREQ_675_BDW;
  8096. data = 3;
  8097. break;
  8098. default:
  8099. WARN(1, "invalid cdclk frequency\n");
  8100. return;
  8101. }
  8102. I915_WRITE(LCPLL_CTL, val);
  8103. val = I915_READ(LCPLL_CTL);
  8104. val &= ~LCPLL_CD_SOURCE_FCLK;
  8105. I915_WRITE(LCPLL_CTL, val);
  8106. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8107. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8108. DRM_ERROR("Switching back to LCPLL failed\n");
  8109. mutex_lock(&dev_priv->rps.hw_lock);
  8110. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8111. mutex_unlock(&dev_priv->rps.hw_lock);
  8112. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8113. intel_update_cdclk(dev);
  8114. WARN(cdclk != dev_priv->cdclk_freq,
  8115. "cdclk requested %d kHz but got %d kHz\n",
  8116. cdclk, dev_priv->cdclk_freq);
  8117. }
  8118. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8119. {
  8120. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8121. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8122. int max_pixclk = ilk_max_pixel_rate(state);
  8123. int cdclk;
  8124. /*
  8125. * FIXME should also account for plane ratio
  8126. * once 64bpp pixel formats are supported.
  8127. */
  8128. if (max_pixclk > 540000)
  8129. cdclk = 675000;
  8130. else if (max_pixclk > 450000)
  8131. cdclk = 540000;
  8132. else if (max_pixclk > 337500)
  8133. cdclk = 450000;
  8134. else
  8135. cdclk = 337500;
  8136. if (cdclk > dev_priv->max_cdclk_freq) {
  8137. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8138. cdclk, dev_priv->max_cdclk_freq);
  8139. return -EINVAL;
  8140. }
  8141. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8142. if (!intel_state->active_crtcs)
  8143. intel_state->dev_cdclk = 337500;
  8144. return 0;
  8145. }
  8146. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8147. {
  8148. struct drm_device *dev = old_state->dev;
  8149. struct intel_atomic_state *old_intel_state =
  8150. to_intel_atomic_state(old_state);
  8151. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8152. broadwell_set_cdclk(dev, req_cdclk);
  8153. }
  8154. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8155. struct intel_crtc_state *crtc_state)
  8156. {
  8157. struct intel_encoder *intel_encoder =
  8158. intel_ddi_get_crtc_new_encoder(crtc_state);
  8159. if (intel_encoder->type != INTEL_OUTPUT_DSI) {
  8160. if (!intel_ddi_pll_select(crtc, crtc_state))
  8161. return -EINVAL;
  8162. }
  8163. crtc->lowfreq_avail = false;
  8164. return 0;
  8165. }
  8166. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8167. enum port port,
  8168. struct intel_crtc_state *pipe_config)
  8169. {
  8170. enum intel_dpll_id id;
  8171. switch (port) {
  8172. case PORT_A:
  8173. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8174. id = DPLL_ID_SKL_DPLL0;
  8175. break;
  8176. case PORT_B:
  8177. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8178. id = DPLL_ID_SKL_DPLL1;
  8179. break;
  8180. case PORT_C:
  8181. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8182. id = DPLL_ID_SKL_DPLL2;
  8183. break;
  8184. default:
  8185. DRM_ERROR("Incorrect port type\n");
  8186. return;
  8187. }
  8188. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8189. }
  8190. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8191. enum port port,
  8192. struct intel_crtc_state *pipe_config)
  8193. {
  8194. enum intel_dpll_id id;
  8195. u32 temp;
  8196. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8197. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8198. switch (pipe_config->ddi_pll_sel) {
  8199. case SKL_DPLL0:
  8200. id = DPLL_ID_SKL_DPLL0;
  8201. break;
  8202. case SKL_DPLL1:
  8203. id = DPLL_ID_SKL_DPLL1;
  8204. break;
  8205. case SKL_DPLL2:
  8206. id = DPLL_ID_SKL_DPLL2;
  8207. break;
  8208. case SKL_DPLL3:
  8209. id = DPLL_ID_SKL_DPLL3;
  8210. break;
  8211. default:
  8212. MISSING_CASE(pipe_config->ddi_pll_sel);
  8213. return;
  8214. }
  8215. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8216. }
  8217. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8218. enum port port,
  8219. struct intel_crtc_state *pipe_config)
  8220. {
  8221. enum intel_dpll_id id;
  8222. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8223. switch (pipe_config->ddi_pll_sel) {
  8224. case PORT_CLK_SEL_WRPLL1:
  8225. id = DPLL_ID_WRPLL1;
  8226. break;
  8227. case PORT_CLK_SEL_WRPLL2:
  8228. id = DPLL_ID_WRPLL2;
  8229. break;
  8230. case PORT_CLK_SEL_SPLL:
  8231. id = DPLL_ID_SPLL;
  8232. break;
  8233. case PORT_CLK_SEL_LCPLL_810:
  8234. id = DPLL_ID_LCPLL_810;
  8235. break;
  8236. case PORT_CLK_SEL_LCPLL_1350:
  8237. id = DPLL_ID_LCPLL_1350;
  8238. break;
  8239. case PORT_CLK_SEL_LCPLL_2700:
  8240. id = DPLL_ID_LCPLL_2700;
  8241. break;
  8242. default:
  8243. MISSING_CASE(pipe_config->ddi_pll_sel);
  8244. /* fall through */
  8245. case PORT_CLK_SEL_NONE:
  8246. return;
  8247. }
  8248. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8249. }
  8250. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8251. struct intel_crtc_state *pipe_config,
  8252. unsigned long *power_domain_mask)
  8253. {
  8254. struct drm_device *dev = crtc->base.dev;
  8255. struct drm_i915_private *dev_priv = dev->dev_private;
  8256. enum intel_display_power_domain power_domain;
  8257. u32 tmp;
  8258. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8259. /*
  8260. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8261. * consistency and less surprising code; it's in always on power).
  8262. */
  8263. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8264. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8265. enum pipe trans_edp_pipe;
  8266. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8267. default:
  8268. WARN(1, "unknown pipe linked to edp transcoder\n");
  8269. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8270. case TRANS_DDI_EDP_INPUT_A_ON:
  8271. trans_edp_pipe = PIPE_A;
  8272. break;
  8273. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8274. trans_edp_pipe = PIPE_B;
  8275. break;
  8276. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8277. trans_edp_pipe = PIPE_C;
  8278. break;
  8279. }
  8280. if (trans_edp_pipe == crtc->pipe)
  8281. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8282. }
  8283. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8284. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8285. return false;
  8286. *power_domain_mask |= BIT(power_domain);
  8287. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8288. return tmp & PIPECONF_ENABLE;
  8289. }
  8290. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8291. struct intel_crtc_state *pipe_config,
  8292. unsigned long *power_domain_mask)
  8293. {
  8294. struct drm_device *dev = crtc->base.dev;
  8295. struct drm_i915_private *dev_priv = dev->dev_private;
  8296. enum intel_display_power_domain power_domain;
  8297. enum port port;
  8298. enum transcoder cpu_transcoder;
  8299. u32 tmp;
  8300. pipe_config->has_dsi_encoder = false;
  8301. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8302. if (port == PORT_A)
  8303. cpu_transcoder = TRANSCODER_DSI_A;
  8304. else
  8305. cpu_transcoder = TRANSCODER_DSI_C;
  8306. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8307. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8308. continue;
  8309. *power_domain_mask |= BIT(power_domain);
  8310. /*
  8311. * The PLL needs to be enabled with a valid divider
  8312. * configuration, otherwise accessing DSI registers will hang
  8313. * the machine. See BSpec North Display Engine
  8314. * registers/MIPI[BXT]. We can break out here early, since we
  8315. * need the same DSI PLL to be enabled for both DSI ports.
  8316. */
  8317. if (!intel_dsi_pll_is_enabled(dev_priv))
  8318. break;
  8319. /* XXX: this works for video mode only */
  8320. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8321. if (!(tmp & DPI_ENABLE))
  8322. continue;
  8323. tmp = I915_READ(MIPI_CTRL(port));
  8324. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8325. continue;
  8326. pipe_config->cpu_transcoder = cpu_transcoder;
  8327. pipe_config->has_dsi_encoder = true;
  8328. break;
  8329. }
  8330. return pipe_config->has_dsi_encoder;
  8331. }
  8332. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8333. struct intel_crtc_state *pipe_config)
  8334. {
  8335. struct drm_device *dev = crtc->base.dev;
  8336. struct drm_i915_private *dev_priv = dev->dev_private;
  8337. struct intel_shared_dpll *pll;
  8338. enum port port;
  8339. uint32_t tmp;
  8340. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8341. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8342. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8343. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8344. else if (IS_BROXTON(dev))
  8345. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8346. else
  8347. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8348. pll = pipe_config->shared_dpll;
  8349. if (pll) {
  8350. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8351. &pipe_config->dpll_hw_state));
  8352. }
  8353. /*
  8354. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8355. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8356. * the PCH transcoder is on.
  8357. */
  8358. if (INTEL_INFO(dev)->gen < 9 &&
  8359. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8360. pipe_config->has_pch_encoder = true;
  8361. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8362. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8363. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8364. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8365. }
  8366. }
  8367. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8368. struct intel_crtc_state *pipe_config)
  8369. {
  8370. struct drm_device *dev = crtc->base.dev;
  8371. struct drm_i915_private *dev_priv = dev->dev_private;
  8372. enum intel_display_power_domain power_domain;
  8373. unsigned long power_domain_mask;
  8374. bool active;
  8375. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8376. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8377. return false;
  8378. power_domain_mask = BIT(power_domain);
  8379. pipe_config->shared_dpll = NULL;
  8380. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8381. if (IS_BROXTON(dev_priv)) {
  8382. bxt_get_dsi_transcoder_state(crtc, pipe_config,
  8383. &power_domain_mask);
  8384. WARN_ON(active && pipe_config->has_dsi_encoder);
  8385. if (pipe_config->has_dsi_encoder)
  8386. active = true;
  8387. }
  8388. if (!active)
  8389. goto out;
  8390. if (!pipe_config->has_dsi_encoder) {
  8391. haswell_get_ddi_port_state(crtc, pipe_config);
  8392. intel_get_pipe_timings(crtc, pipe_config);
  8393. }
  8394. intel_get_pipe_src_size(crtc, pipe_config);
  8395. pipe_config->gamma_mode =
  8396. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8397. if (INTEL_INFO(dev)->gen >= 9) {
  8398. skl_init_scalers(dev, crtc, pipe_config);
  8399. }
  8400. if (INTEL_INFO(dev)->gen >= 9) {
  8401. pipe_config->scaler_state.scaler_id = -1;
  8402. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8403. }
  8404. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8405. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8406. power_domain_mask |= BIT(power_domain);
  8407. if (INTEL_INFO(dev)->gen >= 9)
  8408. skylake_get_pfit_config(crtc, pipe_config);
  8409. else
  8410. ironlake_get_pfit_config(crtc, pipe_config);
  8411. }
  8412. if (IS_HASWELL(dev))
  8413. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8414. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8415. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8416. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8417. pipe_config->pixel_multiplier =
  8418. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8419. } else {
  8420. pipe_config->pixel_multiplier = 1;
  8421. }
  8422. out:
  8423. for_each_power_domain(power_domain, power_domain_mask)
  8424. intel_display_power_put(dev_priv, power_domain);
  8425. return active;
  8426. }
  8427. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  8428. const struct intel_plane_state *plane_state)
  8429. {
  8430. struct drm_device *dev = crtc->dev;
  8431. struct drm_i915_private *dev_priv = dev->dev_private;
  8432. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8433. uint32_t cntl = 0, size = 0;
  8434. if (plane_state && plane_state->visible) {
  8435. unsigned int width = plane_state->base.crtc_w;
  8436. unsigned int height = plane_state->base.crtc_h;
  8437. unsigned int stride = roundup_pow_of_two(width) * 4;
  8438. switch (stride) {
  8439. default:
  8440. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8441. width, stride);
  8442. stride = 256;
  8443. /* fallthrough */
  8444. case 256:
  8445. case 512:
  8446. case 1024:
  8447. case 2048:
  8448. break;
  8449. }
  8450. cntl |= CURSOR_ENABLE |
  8451. CURSOR_GAMMA_ENABLE |
  8452. CURSOR_FORMAT_ARGB |
  8453. CURSOR_STRIDE(stride);
  8454. size = (height << 12) | width;
  8455. }
  8456. if (intel_crtc->cursor_cntl != 0 &&
  8457. (intel_crtc->cursor_base != base ||
  8458. intel_crtc->cursor_size != size ||
  8459. intel_crtc->cursor_cntl != cntl)) {
  8460. /* On these chipsets we can only modify the base/size/stride
  8461. * whilst the cursor is disabled.
  8462. */
  8463. I915_WRITE(CURCNTR(PIPE_A), 0);
  8464. POSTING_READ(CURCNTR(PIPE_A));
  8465. intel_crtc->cursor_cntl = 0;
  8466. }
  8467. if (intel_crtc->cursor_base != base) {
  8468. I915_WRITE(CURBASE(PIPE_A), base);
  8469. intel_crtc->cursor_base = base;
  8470. }
  8471. if (intel_crtc->cursor_size != size) {
  8472. I915_WRITE(CURSIZE, size);
  8473. intel_crtc->cursor_size = size;
  8474. }
  8475. if (intel_crtc->cursor_cntl != cntl) {
  8476. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8477. POSTING_READ(CURCNTR(PIPE_A));
  8478. intel_crtc->cursor_cntl = cntl;
  8479. }
  8480. }
  8481. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  8482. const struct intel_plane_state *plane_state)
  8483. {
  8484. struct drm_device *dev = crtc->dev;
  8485. struct drm_i915_private *dev_priv = dev->dev_private;
  8486. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8487. int pipe = intel_crtc->pipe;
  8488. uint32_t cntl = 0;
  8489. if (plane_state && plane_state->visible) {
  8490. cntl = MCURSOR_GAMMA_ENABLE;
  8491. switch (plane_state->base.crtc_w) {
  8492. case 64:
  8493. cntl |= CURSOR_MODE_64_ARGB_AX;
  8494. break;
  8495. case 128:
  8496. cntl |= CURSOR_MODE_128_ARGB_AX;
  8497. break;
  8498. case 256:
  8499. cntl |= CURSOR_MODE_256_ARGB_AX;
  8500. break;
  8501. default:
  8502. MISSING_CASE(plane_state->base.crtc_w);
  8503. return;
  8504. }
  8505. cntl |= pipe << 28; /* Connect to correct pipe */
  8506. if (HAS_DDI(dev))
  8507. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8508. if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
  8509. cntl |= CURSOR_ROTATE_180;
  8510. }
  8511. if (intel_crtc->cursor_cntl != cntl) {
  8512. I915_WRITE(CURCNTR(pipe), cntl);
  8513. POSTING_READ(CURCNTR(pipe));
  8514. intel_crtc->cursor_cntl = cntl;
  8515. }
  8516. /* and commit changes on next vblank */
  8517. I915_WRITE(CURBASE(pipe), base);
  8518. POSTING_READ(CURBASE(pipe));
  8519. intel_crtc->cursor_base = base;
  8520. }
  8521. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8522. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8523. const struct intel_plane_state *plane_state)
  8524. {
  8525. struct drm_device *dev = crtc->dev;
  8526. struct drm_i915_private *dev_priv = dev->dev_private;
  8527. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8528. int pipe = intel_crtc->pipe;
  8529. u32 base = intel_crtc->cursor_addr;
  8530. u32 pos = 0;
  8531. if (plane_state) {
  8532. int x = plane_state->base.crtc_x;
  8533. int y = plane_state->base.crtc_y;
  8534. if (x < 0) {
  8535. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8536. x = -x;
  8537. }
  8538. pos |= x << CURSOR_X_SHIFT;
  8539. if (y < 0) {
  8540. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8541. y = -y;
  8542. }
  8543. pos |= y << CURSOR_Y_SHIFT;
  8544. /* ILK+ do this automagically */
  8545. if (HAS_GMCH_DISPLAY(dev) &&
  8546. plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  8547. base += (plane_state->base.crtc_h *
  8548. plane_state->base.crtc_w - 1) * 4;
  8549. }
  8550. }
  8551. I915_WRITE(CURPOS(pipe), pos);
  8552. if (IS_845G(dev) || IS_I865G(dev))
  8553. i845_update_cursor(crtc, base, plane_state);
  8554. else
  8555. i9xx_update_cursor(crtc, base, plane_state);
  8556. }
  8557. static bool cursor_size_ok(struct drm_device *dev,
  8558. uint32_t width, uint32_t height)
  8559. {
  8560. if (width == 0 || height == 0)
  8561. return false;
  8562. /*
  8563. * 845g/865g are special in that they are only limited by
  8564. * the width of their cursors, the height is arbitrary up to
  8565. * the precision of the register. Everything else requires
  8566. * square cursors, limited to a few power-of-two sizes.
  8567. */
  8568. if (IS_845G(dev) || IS_I865G(dev)) {
  8569. if ((width & 63) != 0)
  8570. return false;
  8571. if (width > (IS_845G(dev) ? 64 : 512))
  8572. return false;
  8573. if (height > 1023)
  8574. return false;
  8575. } else {
  8576. switch (width | height) {
  8577. case 256:
  8578. case 128:
  8579. if (IS_GEN2(dev))
  8580. return false;
  8581. case 64:
  8582. break;
  8583. default:
  8584. return false;
  8585. }
  8586. }
  8587. return true;
  8588. }
  8589. /* VESA 640x480x72Hz mode to set on the pipe */
  8590. static struct drm_display_mode load_detect_mode = {
  8591. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8592. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8593. };
  8594. struct drm_framebuffer *
  8595. __intel_framebuffer_create(struct drm_device *dev,
  8596. struct drm_mode_fb_cmd2 *mode_cmd,
  8597. struct drm_i915_gem_object *obj)
  8598. {
  8599. struct intel_framebuffer *intel_fb;
  8600. int ret;
  8601. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8602. if (!intel_fb)
  8603. return ERR_PTR(-ENOMEM);
  8604. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8605. if (ret)
  8606. goto err;
  8607. return &intel_fb->base;
  8608. err:
  8609. kfree(intel_fb);
  8610. return ERR_PTR(ret);
  8611. }
  8612. static struct drm_framebuffer *
  8613. intel_framebuffer_create(struct drm_device *dev,
  8614. struct drm_mode_fb_cmd2 *mode_cmd,
  8615. struct drm_i915_gem_object *obj)
  8616. {
  8617. struct drm_framebuffer *fb;
  8618. int ret;
  8619. ret = i915_mutex_lock_interruptible(dev);
  8620. if (ret)
  8621. return ERR_PTR(ret);
  8622. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8623. mutex_unlock(&dev->struct_mutex);
  8624. return fb;
  8625. }
  8626. static u32
  8627. intel_framebuffer_pitch_for_width(int width, int bpp)
  8628. {
  8629. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8630. return ALIGN(pitch, 64);
  8631. }
  8632. static u32
  8633. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8634. {
  8635. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8636. return PAGE_ALIGN(pitch * mode->vdisplay);
  8637. }
  8638. static struct drm_framebuffer *
  8639. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8640. struct drm_display_mode *mode,
  8641. int depth, int bpp)
  8642. {
  8643. struct drm_framebuffer *fb;
  8644. struct drm_i915_gem_object *obj;
  8645. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8646. obj = i915_gem_object_create(dev,
  8647. intel_framebuffer_size_for_mode(mode, bpp));
  8648. if (IS_ERR(obj))
  8649. return ERR_CAST(obj);
  8650. mode_cmd.width = mode->hdisplay;
  8651. mode_cmd.height = mode->vdisplay;
  8652. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8653. bpp);
  8654. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8655. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8656. if (IS_ERR(fb))
  8657. drm_gem_object_unreference_unlocked(&obj->base);
  8658. return fb;
  8659. }
  8660. static struct drm_framebuffer *
  8661. mode_fits_in_fbdev(struct drm_device *dev,
  8662. struct drm_display_mode *mode)
  8663. {
  8664. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8665. struct drm_i915_private *dev_priv = dev->dev_private;
  8666. struct drm_i915_gem_object *obj;
  8667. struct drm_framebuffer *fb;
  8668. if (!dev_priv->fbdev)
  8669. return NULL;
  8670. if (!dev_priv->fbdev->fb)
  8671. return NULL;
  8672. obj = dev_priv->fbdev->fb->obj;
  8673. BUG_ON(!obj);
  8674. fb = &dev_priv->fbdev->fb->base;
  8675. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8676. fb->bits_per_pixel))
  8677. return NULL;
  8678. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8679. return NULL;
  8680. drm_framebuffer_reference(fb);
  8681. return fb;
  8682. #else
  8683. return NULL;
  8684. #endif
  8685. }
  8686. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8687. struct drm_crtc *crtc,
  8688. struct drm_display_mode *mode,
  8689. struct drm_framebuffer *fb,
  8690. int x, int y)
  8691. {
  8692. struct drm_plane_state *plane_state;
  8693. int hdisplay, vdisplay;
  8694. int ret;
  8695. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8696. if (IS_ERR(plane_state))
  8697. return PTR_ERR(plane_state);
  8698. if (mode)
  8699. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8700. else
  8701. hdisplay = vdisplay = 0;
  8702. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8703. if (ret)
  8704. return ret;
  8705. drm_atomic_set_fb_for_plane(plane_state, fb);
  8706. plane_state->crtc_x = 0;
  8707. plane_state->crtc_y = 0;
  8708. plane_state->crtc_w = hdisplay;
  8709. plane_state->crtc_h = vdisplay;
  8710. plane_state->src_x = x << 16;
  8711. plane_state->src_y = y << 16;
  8712. plane_state->src_w = hdisplay << 16;
  8713. plane_state->src_h = vdisplay << 16;
  8714. return 0;
  8715. }
  8716. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8717. struct drm_display_mode *mode,
  8718. struct intel_load_detect_pipe *old,
  8719. struct drm_modeset_acquire_ctx *ctx)
  8720. {
  8721. struct intel_crtc *intel_crtc;
  8722. struct intel_encoder *intel_encoder =
  8723. intel_attached_encoder(connector);
  8724. struct drm_crtc *possible_crtc;
  8725. struct drm_encoder *encoder = &intel_encoder->base;
  8726. struct drm_crtc *crtc = NULL;
  8727. struct drm_device *dev = encoder->dev;
  8728. struct drm_framebuffer *fb;
  8729. struct drm_mode_config *config = &dev->mode_config;
  8730. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8731. struct drm_connector_state *connector_state;
  8732. struct intel_crtc_state *crtc_state;
  8733. int ret, i = -1;
  8734. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8735. connector->base.id, connector->name,
  8736. encoder->base.id, encoder->name);
  8737. old->restore_state = NULL;
  8738. retry:
  8739. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8740. if (ret)
  8741. goto fail;
  8742. /*
  8743. * Algorithm gets a little messy:
  8744. *
  8745. * - if the connector already has an assigned crtc, use it (but make
  8746. * sure it's on first)
  8747. *
  8748. * - try to find the first unused crtc that can drive this connector,
  8749. * and use that if we find one
  8750. */
  8751. /* See if we already have a CRTC for this connector */
  8752. if (connector->state->crtc) {
  8753. crtc = connector->state->crtc;
  8754. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8755. if (ret)
  8756. goto fail;
  8757. /* Make sure the crtc and connector are running */
  8758. goto found;
  8759. }
  8760. /* Find an unused one (if possible) */
  8761. for_each_crtc(dev, possible_crtc) {
  8762. i++;
  8763. if (!(encoder->possible_crtcs & (1 << i)))
  8764. continue;
  8765. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8766. if (ret)
  8767. goto fail;
  8768. if (possible_crtc->state->enable) {
  8769. drm_modeset_unlock(&possible_crtc->mutex);
  8770. continue;
  8771. }
  8772. crtc = possible_crtc;
  8773. break;
  8774. }
  8775. /*
  8776. * If we didn't find an unused CRTC, don't use any.
  8777. */
  8778. if (!crtc) {
  8779. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8780. goto fail;
  8781. }
  8782. found:
  8783. intel_crtc = to_intel_crtc(crtc);
  8784. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8785. if (ret)
  8786. goto fail;
  8787. state = drm_atomic_state_alloc(dev);
  8788. restore_state = drm_atomic_state_alloc(dev);
  8789. if (!state || !restore_state) {
  8790. ret = -ENOMEM;
  8791. goto fail;
  8792. }
  8793. state->acquire_ctx = ctx;
  8794. restore_state->acquire_ctx = ctx;
  8795. connector_state = drm_atomic_get_connector_state(state, connector);
  8796. if (IS_ERR(connector_state)) {
  8797. ret = PTR_ERR(connector_state);
  8798. goto fail;
  8799. }
  8800. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8801. if (ret)
  8802. goto fail;
  8803. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8804. if (IS_ERR(crtc_state)) {
  8805. ret = PTR_ERR(crtc_state);
  8806. goto fail;
  8807. }
  8808. crtc_state->base.active = crtc_state->base.enable = true;
  8809. if (!mode)
  8810. mode = &load_detect_mode;
  8811. /* We need a framebuffer large enough to accommodate all accesses
  8812. * that the plane may generate whilst we perform load detection.
  8813. * We can not rely on the fbcon either being present (we get called
  8814. * during its initialisation to detect all boot displays, or it may
  8815. * not even exist) or that it is large enough to satisfy the
  8816. * requested mode.
  8817. */
  8818. fb = mode_fits_in_fbdev(dev, mode);
  8819. if (fb == NULL) {
  8820. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8821. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8822. } else
  8823. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8824. if (IS_ERR(fb)) {
  8825. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8826. goto fail;
  8827. }
  8828. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8829. if (ret)
  8830. goto fail;
  8831. drm_framebuffer_unreference(fb);
  8832. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8833. if (ret)
  8834. goto fail;
  8835. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8836. if (!ret)
  8837. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8838. if (!ret)
  8839. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8840. if (ret) {
  8841. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8842. goto fail;
  8843. }
  8844. ret = drm_atomic_commit(state);
  8845. if (ret) {
  8846. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8847. goto fail;
  8848. }
  8849. old->restore_state = restore_state;
  8850. /* let the connector get through one full cycle before testing */
  8851. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8852. return true;
  8853. fail:
  8854. drm_atomic_state_free(state);
  8855. drm_atomic_state_free(restore_state);
  8856. restore_state = state = NULL;
  8857. if (ret == -EDEADLK) {
  8858. drm_modeset_backoff(ctx);
  8859. goto retry;
  8860. }
  8861. return false;
  8862. }
  8863. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8864. struct intel_load_detect_pipe *old,
  8865. struct drm_modeset_acquire_ctx *ctx)
  8866. {
  8867. struct intel_encoder *intel_encoder =
  8868. intel_attached_encoder(connector);
  8869. struct drm_encoder *encoder = &intel_encoder->base;
  8870. struct drm_atomic_state *state = old->restore_state;
  8871. int ret;
  8872. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8873. connector->base.id, connector->name,
  8874. encoder->base.id, encoder->name);
  8875. if (!state)
  8876. return;
  8877. ret = drm_atomic_commit(state);
  8878. if (ret) {
  8879. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8880. drm_atomic_state_free(state);
  8881. }
  8882. }
  8883. static int i9xx_pll_refclk(struct drm_device *dev,
  8884. const struct intel_crtc_state *pipe_config)
  8885. {
  8886. struct drm_i915_private *dev_priv = dev->dev_private;
  8887. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8888. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8889. return dev_priv->vbt.lvds_ssc_freq;
  8890. else if (HAS_PCH_SPLIT(dev))
  8891. return 120000;
  8892. else if (!IS_GEN2(dev))
  8893. return 96000;
  8894. else
  8895. return 48000;
  8896. }
  8897. /* Returns the clock of the currently programmed mode of the given pipe. */
  8898. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8899. struct intel_crtc_state *pipe_config)
  8900. {
  8901. struct drm_device *dev = crtc->base.dev;
  8902. struct drm_i915_private *dev_priv = dev->dev_private;
  8903. int pipe = pipe_config->cpu_transcoder;
  8904. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8905. u32 fp;
  8906. intel_clock_t clock;
  8907. int port_clock;
  8908. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8909. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8910. fp = pipe_config->dpll_hw_state.fp0;
  8911. else
  8912. fp = pipe_config->dpll_hw_state.fp1;
  8913. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8914. if (IS_PINEVIEW(dev)) {
  8915. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8916. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8917. } else {
  8918. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8919. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8920. }
  8921. if (!IS_GEN2(dev)) {
  8922. if (IS_PINEVIEW(dev))
  8923. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8924. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8925. else
  8926. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8927. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8928. switch (dpll & DPLL_MODE_MASK) {
  8929. case DPLLB_MODE_DAC_SERIAL:
  8930. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8931. 5 : 10;
  8932. break;
  8933. case DPLLB_MODE_LVDS:
  8934. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8935. 7 : 14;
  8936. break;
  8937. default:
  8938. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8939. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8940. return;
  8941. }
  8942. if (IS_PINEVIEW(dev))
  8943. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8944. else
  8945. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8946. } else {
  8947. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8948. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8949. if (is_lvds) {
  8950. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8951. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8952. if (lvds & LVDS_CLKB_POWER_UP)
  8953. clock.p2 = 7;
  8954. else
  8955. clock.p2 = 14;
  8956. } else {
  8957. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8958. clock.p1 = 2;
  8959. else {
  8960. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8961. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8962. }
  8963. if (dpll & PLL_P2_DIVIDE_BY_4)
  8964. clock.p2 = 4;
  8965. else
  8966. clock.p2 = 2;
  8967. }
  8968. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8969. }
  8970. /*
  8971. * This value includes pixel_multiplier. We will use
  8972. * port_clock to compute adjusted_mode.crtc_clock in the
  8973. * encoder's get_config() function.
  8974. */
  8975. pipe_config->port_clock = port_clock;
  8976. }
  8977. int intel_dotclock_calculate(int link_freq,
  8978. const struct intel_link_m_n *m_n)
  8979. {
  8980. /*
  8981. * The calculation for the data clock is:
  8982. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8983. * But we want to avoid losing precison if possible, so:
  8984. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8985. *
  8986. * and the link clock is simpler:
  8987. * link_clock = (m * link_clock) / n
  8988. */
  8989. if (!m_n->link_n)
  8990. return 0;
  8991. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8992. }
  8993. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8994. struct intel_crtc_state *pipe_config)
  8995. {
  8996. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8997. /* read out port_clock from the DPLL */
  8998. i9xx_crtc_clock_get(crtc, pipe_config);
  8999. /*
  9000. * In case there is an active pipe without active ports,
  9001. * we may need some idea for the dotclock anyway.
  9002. * Calculate one based on the FDI configuration.
  9003. */
  9004. pipe_config->base.adjusted_mode.crtc_clock =
  9005. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9006. &pipe_config->fdi_m_n);
  9007. }
  9008. /** Returns the currently programmed mode of the given pipe. */
  9009. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9010. struct drm_crtc *crtc)
  9011. {
  9012. struct drm_i915_private *dev_priv = dev->dev_private;
  9013. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9014. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9015. struct drm_display_mode *mode;
  9016. struct intel_crtc_state *pipe_config;
  9017. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9018. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9019. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9020. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9021. enum pipe pipe = intel_crtc->pipe;
  9022. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9023. if (!mode)
  9024. return NULL;
  9025. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9026. if (!pipe_config) {
  9027. kfree(mode);
  9028. return NULL;
  9029. }
  9030. /*
  9031. * Construct a pipe_config sufficient for getting the clock info
  9032. * back out of crtc_clock_get.
  9033. *
  9034. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9035. * to use a real value here instead.
  9036. */
  9037. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9038. pipe_config->pixel_multiplier = 1;
  9039. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9040. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9041. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9042. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9043. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9044. mode->hdisplay = (htot & 0xffff) + 1;
  9045. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9046. mode->hsync_start = (hsync & 0xffff) + 1;
  9047. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9048. mode->vdisplay = (vtot & 0xffff) + 1;
  9049. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9050. mode->vsync_start = (vsync & 0xffff) + 1;
  9051. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9052. drm_mode_set_name(mode);
  9053. kfree(pipe_config);
  9054. return mode;
  9055. }
  9056. void intel_mark_busy(struct drm_device *dev)
  9057. {
  9058. struct drm_i915_private *dev_priv = dev->dev_private;
  9059. if (dev_priv->mm.busy)
  9060. return;
  9061. intel_runtime_pm_get(dev_priv);
  9062. i915_update_gfx_val(dev_priv);
  9063. if (INTEL_INFO(dev)->gen >= 6)
  9064. gen6_rps_busy(dev_priv);
  9065. dev_priv->mm.busy = true;
  9066. }
  9067. void intel_mark_idle(struct drm_device *dev)
  9068. {
  9069. struct drm_i915_private *dev_priv = dev->dev_private;
  9070. if (!dev_priv->mm.busy)
  9071. return;
  9072. dev_priv->mm.busy = false;
  9073. if (INTEL_INFO(dev)->gen >= 6)
  9074. gen6_rps_idle(dev->dev_private);
  9075. intel_runtime_pm_put(dev_priv);
  9076. }
  9077. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9078. {
  9079. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9080. struct drm_device *dev = crtc->dev;
  9081. struct intel_unpin_work *work;
  9082. spin_lock_irq(&dev->event_lock);
  9083. work = intel_crtc->unpin_work;
  9084. intel_crtc->unpin_work = NULL;
  9085. spin_unlock_irq(&dev->event_lock);
  9086. if (work) {
  9087. cancel_work_sync(&work->work);
  9088. kfree(work);
  9089. }
  9090. drm_crtc_cleanup(crtc);
  9091. kfree(intel_crtc);
  9092. }
  9093. static void intel_unpin_work_fn(struct work_struct *__work)
  9094. {
  9095. struct intel_unpin_work *work =
  9096. container_of(__work, struct intel_unpin_work, work);
  9097. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9098. struct drm_device *dev = crtc->base.dev;
  9099. struct drm_plane *primary = crtc->base.primary;
  9100. mutex_lock(&dev->struct_mutex);
  9101. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9102. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9103. if (work->flip_queued_req)
  9104. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9105. mutex_unlock(&dev->struct_mutex);
  9106. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  9107. intel_fbc_post_update(crtc);
  9108. drm_framebuffer_unreference(work->old_fb);
  9109. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9110. atomic_dec(&crtc->unpin_work_count);
  9111. kfree(work);
  9112. }
  9113. static void do_intel_finish_page_flip(struct drm_device *dev,
  9114. struct drm_crtc *crtc)
  9115. {
  9116. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9117. struct intel_unpin_work *work;
  9118. unsigned long flags;
  9119. /* Ignore early vblank irqs */
  9120. if (intel_crtc == NULL)
  9121. return;
  9122. /*
  9123. * This is called both by irq handlers and the reset code (to complete
  9124. * lost pageflips) so needs the full irqsave spinlocks.
  9125. */
  9126. spin_lock_irqsave(&dev->event_lock, flags);
  9127. work = intel_crtc->unpin_work;
  9128. /* Ensure we don't miss a work->pending update ... */
  9129. smp_rmb();
  9130. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9131. spin_unlock_irqrestore(&dev->event_lock, flags);
  9132. return;
  9133. }
  9134. page_flip_completed(intel_crtc);
  9135. spin_unlock_irqrestore(&dev->event_lock, flags);
  9136. }
  9137. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9138. {
  9139. struct drm_i915_private *dev_priv = dev->dev_private;
  9140. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9141. do_intel_finish_page_flip(dev, crtc);
  9142. }
  9143. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9144. {
  9145. struct drm_i915_private *dev_priv = dev->dev_private;
  9146. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9147. do_intel_finish_page_flip(dev, crtc);
  9148. }
  9149. /* Is 'a' after or equal to 'b'? */
  9150. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9151. {
  9152. return !((a - b) & 0x80000000);
  9153. }
  9154. static bool page_flip_finished(struct intel_crtc *crtc)
  9155. {
  9156. struct drm_device *dev = crtc->base.dev;
  9157. struct drm_i915_private *dev_priv = dev->dev_private;
  9158. unsigned reset_counter;
  9159. reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9160. if (crtc->reset_counter != reset_counter)
  9161. return true;
  9162. /*
  9163. * The relevant registers doen't exist on pre-ctg.
  9164. * As the flip done interrupt doesn't trigger for mmio
  9165. * flips on gmch platforms, a flip count check isn't
  9166. * really needed there. But since ctg has the registers,
  9167. * include it in the check anyway.
  9168. */
  9169. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9170. return true;
  9171. /*
  9172. * BDW signals flip done immediately if the plane
  9173. * is disabled, even if the plane enable is already
  9174. * armed to occur at the next vblank :(
  9175. */
  9176. /*
  9177. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9178. * used the same base address. In that case the mmio flip might
  9179. * have completed, but the CS hasn't even executed the flip yet.
  9180. *
  9181. * A flip count check isn't enough as the CS might have updated
  9182. * the base address just after start of vblank, but before we
  9183. * managed to process the interrupt. This means we'd complete the
  9184. * CS flip too soon.
  9185. *
  9186. * Combining both checks should get us a good enough result. It may
  9187. * still happen that the CS flip has been executed, but has not
  9188. * yet actually completed. But in case the base address is the same
  9189. * anyway, we don't really care.
  9190. */
  9191. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9192. crtc->unpin_work->gtt_offset &&
  9193. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9194. crtc->unpin_work->flip_count);
  9195. }
  9196. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9197. {
  9198. struct drm_i915_private *dev_priv = dev->dev_private;
  9199. struct intel_crtc *intel_crtc =
  9200. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9201. unsigned long flags;
  9202. /*
  9203. * This is called both by irq handlers and the reset code (to complete
  9204. * lost pageflips) so needs the full irqsave spinlocks.
  9205. *
  9206. * NB: An MMIO update of the plane base pointer will also
  9207. * generate a page-flip completion irq, i.e. every modeset
  9208. * is also accompanied by a spurious intel_prepare_page_flip().
  9209. */
  9210. spin_lock_irqsave(&dev->event_lock, flags);
  9211. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9212. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9213. spin_unlock_irqrestore(&dev->event_lock, flags);
  9214. }
  9215. static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
  9216. {
  9217. /* Ensure that the work item is consistent when activating it ... */
  9218. smp_wmb();
  9219. atomic_set(&work->pending, INTEL_FLIP_PENDING);
  9220. /* and that it is marked active as soon as the irq could fire. */
  9221. smp_wmb();
  9222. }
  9223. static int intel_gen2_queue_flip(struct drm_device *dev,
  9224. struct drm_crtc *crtc,
  9225. struct drm_framebuffer *fb,
  9226. struct drm_i915_gem_object *obj,
  9227. struct drm_i915_gem_request *req,
  9228. uint32_t flags)
  9229. {
  9230. struct intel_engine_cs *engine = req->engine;
  9231. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9232. u32 flip_mask;
  9233. int ret;
  9234. ret = intel_ring_begin(req, 6);
  9235. if (ret)
  9236. return ret;
  9237. /* Can't queue multiple flips, so wait for the previous
  9238. * one to finish before executing the next.
  9239. */
  9240. if (intel_crtc->plane)
  9241. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9242. else
  9243. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9244. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9245. intel_ring_emit(engine, MI_NOOP);
  9246. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9247. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9248. intel_ring_emit(engine, fb->pitches[0]);
  9249. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9250. intel_ring_emit(engine, 0); /* aux display base address, unused */
  9251. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9252. return 0;
  9253. }
  9254. static int intel_gen3_queue_flip(struct drm_device *dev,
  9255. struct drm_crtc *crtc,
  9256. struct drm_framebuffer *fb,
  9257. struct drm_i915_gem_object *obj,
  9258. struct drm_i915_gem_request *req,
  9259. uint32_t flags)
  9260. {
  9261. struct intel_engine_cs *engine = req->engine;
  9262. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9263. u32 flip_mask;
  9264. int ret;
  9265. ret = intel_ring_begin(req, 6);
  9266. if (ret)
  9267. return ret;
  9268. if (intel_crtc->plane)
  9269. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9270. else
  9271. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9272. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9273. intel_ring_emit(engine, MI_NOOP);
  9274. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
  9275. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9276. intel_ring_emit(engine, fb->pitches[0]);
  9277. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9278. intel_ring_emit(engine, MI_NOOP);
  9279. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9280. return 0;
  9281. }
  9282. static int intel_gen4_queue_flip(struct drm_device *dev,
  9283. struct drm_crtc *crtc,
  9284. struct drm_framebuffer *fb,
  9285. struct drm_i915_gem_object *obj,
  9286. struct drm_i915_gem_request *req,
  9287. uint32_t flags)
  9288. {
  9289. struct intel_engine_cs *engine = req->engine;
  9290. struct drm_i915_private *dev_priv = dev->dev_private;
  9291. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9292. uint32_t pf, pipesrc;
  9293. int ret;
  9294. ret = intel_ring_begin(req, 4);
  9295. if (ret)
  9296. return ret;
  9297. /* i965+ uses the linear or tiled offsets from the
  9298. * Display Registers (which do not change across a page-flip)
  9299. * so we need only reprogram the base address.
  9300. */
  9301. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9302. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9303. intel_ring_emit(engine, fb->pitches[0]);
  9304. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
  9305. obj->tiling_mode);
  9306. /* XXX Enabling the panel-fitter across page-flip is so far
  9307. * untested on non-native modes, so ignore it for now.
  9308. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9309. */
  9310. pf = 0;
  9311. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9312. intel_ring_emit(engine, pf | pipesrc);
  9313. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9314. return 0;
  9315. }
  9316. static int intel_gen6_queue_flip(struct drm_device *dev,
  9317. struct drm_crtc *crtc,
  9318. struct drm_framebuffer *fb,
  9319. struct drm_i915_gem_object *obj,
  9320. struct drm_i915_gem_request *req,
  9321. uint32_t flags)
  9322. {
  9323. struct intel_engine_cs *engine = req->engine;
  9324. struct drm_i915_private *dev_priv = dev->dev_private;
  9325. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9326. uint32_t pf, pipesrc;
  9327. int ret;
  9328. ret = intel_ring_begin(req, 4);
  9329. if (ret)
  9330. return ret;
  9331. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9332. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9333. intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
  9334. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9335. /* Contrary to the suggestions in the documentation,
  9336. * "Enable Panel Fitter" does not seem to be required when page
  9337. * flipping with a non-native mode, and worse causes a normal
  9338. * modeset to fail.
  9339. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9340. */
  9341. pf = 0;
  9342. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9343. intel_ring_emit(engine, pf | pipesrc);
  9344. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9345. return 0;
  9346. }
  9347. static int intel_gen7_queue_flip(struct drm_device *dev,
  9348. struct drm_crtc *crtc,
  9349. struct drm_framebuffer *fb,
  9350. struct drm_i915_gem_object *obj,
  9351. struct drm_i915_gem_request *req,
  9352. uint32_t flags)
  9353. {
  9354. struct intel_engine_cs *engine = req->engine;
  9355. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9356. uint32_t plane_bit = 0;
  9357. int len, ret;
  9358. switch (intel_crtc->plane) {
  9359. case PLANE_A:
  9360. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9361. break;
  9362. case PLANE_B:
  9363. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9364. break;
  9365. case PLANE_C:
  9366. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9367. break;
  9368. default:
  9369. WARN_ONCE(1, "unknown plane in flip command\n");
  9370. return -ENODEV;
  9371. }
  9372. len = 4;
  9373. if (engine->id == RCS) {
  9374. len += 6;
  9375. /*
  9376. * On Gen 8, SRM is now taking an extra dword to accommodate
  9377. * 48bits addresses, and we need a NOOP for the batch size to
  9378. * stay even.
  9379. */
  9380. if (IS_GEN8(dev))
  9381. len += 2;
  9382. }
  9383. /*
  9384. * BSpec MI_DISPLAY_FLIP for IVB:
  9385. * "The full packet must be contained within the same cache line."
  9386. *
  9387. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9388. * cacheline, if we ever start emitting more commands before
  9389. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9390. * then do the cacheline alignment, and finally emit the
  9391. * MI_DISPLAY_FLIP.
  9392. */
  9393. ret = intel_ring_cacheline_align(req);
  9394. if (ret)
  9395. return ret;
  9396. ret = intel_ring_begin(req, len);
  9397. if (ret)
  9398. return ret;
  9399. /* Unmask the flip-done completion message. Note that the bspec says that
  9400. * we should do this for both the BCS and RCS, and that we must not unmask
  9401. * more than one flip event at any time (or ensure that one flip message
  9402. * can be sent by waiting for flip-done prior to queueing new flips).
  9403. * Experimentation says that BCS works despite DERRMR masking all
  9404. * flip-done completion events and that unmasking all planes at once
  9405. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9406. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9407. */
  9408. if (engine->id == RCS) {
  9409. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
  9410. intel_ring_emit_reg(engine, DERRMR);
  9411. intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9412. DERRMR_PIPEB_PRI_FLIP_DONE |
  9413. DERRMR_PIPEC_PRI_FLIP_DONE));
  9414. if (IS_GEN8(dev))
  9415. intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
  9416. MI_SRM_LRM_GLOBAL_GTT);
  9417. else
  9418. intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
  9419. MI_SRM_LRM_GLOBAL_GTT);
  9420. intel_ring_emit_reg(engine, DERRMR);
  9421. intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
  9422. if (IS_GEN8(dev)) {
  9423. intel_ring_emit(engine, 0);
  9424. intel_ring_emit(engine, MI_NOOP);
  9425. }
  9426. }
  9427. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
  9428. intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
  9429. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9430. intel_ring_emit(engine, (MI_NOOP));
  9431. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9432. return 0;
  9433. }
  9434. static bool use_mmio_flip(struct intel_engine_cs *engine,
  9435. struct drm_i915_gem_object *obj)
  9436. {
  9437. /*
  9438. * This is not being used for older platforms, because
  9439. * non-availability of flip done interrupt forces us to use
  9440. * CS flips. Older platforms derive flip done using some clever
  9441. * tricks involving the flip_pending status bits and vblank irqs.
  9442. * So using MMIO flips there would disrupt this mechanism.
  9443. */
  9444. if (engine == NULL)
  9445. return true;
  9446. if (INTEL_INFO(engine->dev)->gen < 5)
  9447. return false;
  9448. if (i915.use_mmio_flip < 0)
  9449. return false;
  9450. else if (i915.use_mmio_flip > 0)
  9451. return true;
  9452. else if (i915.enable_execlists)
  9453. return true;
  9454. else if (obj->base.dma_buf &&
  9455. !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
  9456. false))
  9457. return true;
  9458. else
  9459. return engine != i915_gem_request_get_engine(obj->last_write_req);
  9460. }
  9461. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9462. unsigned int rotation,
  9463. struct intel_unpin_work *work)
  9464. {
  9465. struct drm_device *dev = intel_crtc->base.dev;
  9466. struct drm_i915_private *dev_priv = dev->dev_private;
  9467. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9468. const enum pipe pipe = intel_crtc->pipe;
  9469. u32 ctl, stride, tile_height;
  9470. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9471. ctl &= ~PLANE_CTL_TILED_MASK;
  9472. switch (fb->modifier[0]) {
  9473. case DRM_FORMAT_MOD_NONE:
  9474. break;
  9475. case I915_FORMAT_MOD_X_TILED:
  9476. ctl |= PLANE_CTL_TILED_X;
  9477. break;
  9478. case I915_FORMAT_MOD_Y_TILED:
  9479. ctl |= PLANE_CTL_TILED_Y;
  9480. break;
  9481. case I915_FORMAT_MOD_Yf_TILED:
  9482. ctl |= PLANE_CTL_TILED_YF;
  9483. break;
  9484. default:
  9485. MISSING_CASE(fb->modifier[0]);
  9486. }
  9487. /*
  9488. * The stride is either expressed as a multiple of 64 bytes chunks for
  9489. * linear buffers or in number of tiles for tiled buffers.
  9490. */
  9491. if (intel_rotation_90_or_270(rotation)) {
  9492. /* stride = Surface height in tiles */
  9493. tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
  9494. stride = DIV_ROUND_UP(fb->height, tile_height);
  9495. } else {
  9496. stride = fb->pitches[0] /
  9497. intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  9498. fb->pixel_format);
  9499. }
  9500. /*
  9501. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9502. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9503. */
  9504. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9505. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9506. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9507. POSTING_READ(PLANE_SURF(pipe, 0));
  9508. }
  9509. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9510. struct intel_unpin_work *work)
  9511. {
  9512. struct drm_device *dev = intel_crtc->base.dev;
  9513. struct drm_i915_private *dev_priv = dev->dev_private;
  9514. struct intel_framebuffer *intel_fb =
  9515. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9516. struct drm_i915_gem_object *obj = intel_fb->obj;
  9517. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  9518. u32 dspcntr;
  9519. dspcntr = I915_READ(reg);
  9520. if (obj->tiling_mode != I915_TILING_NONE)
  9521. dspcntr |= DISPPLANE_TILED;
  9522. else
  9523. dspcntr &= ~DISPPLANE_TILED;
  9524. I915_WRITE(reg, dspcntr);
  9525. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9526. POSTING_READ(DSPSURF(intel_crtc->plane));
  9527. }
  9528. /*
  9529. * XXX: This is the temporary way to update the plane registers until we get
  9530. * around to using the usual plane update functions for MMIO flips
  9531. */
  9532. static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
  9533. {
  9534. struct intel_crtc *crtc = mmio_flip->crtc;
  9535. struct intel_unpin_work *work;
  9536. spin_lock_irq(&crtc->base.dev->event_lock);
  9537. work = crtc->unpin_work;
  9538. spin_unlock_irq(&crtc->base.dev->event_lock);
  9539. if (work == NULL)
  9540. return;
  9541. intel_mark_page_flip_active(work);
  9542. intel_pipe_update_start(crtc);
  9543. if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
  9544. skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
  9545. else
  9546. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9547. ilk_do_mmio_flip(crtc, work);
  9548. intel_pipe_update_end(crtc);
  9549. }
  9550. static void intel_mmio_flip_work_func(struct work_struct *work)
  9551. {
  9552. struct intel_mmio_flip *mmio_flip =
  9553. container_of(work, struct intel_mmio_flip, work);
  9554. struct intel_framebuffer *intel_fb =
  9555. to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
  9556. struct drm_i915_gem_object *obj = intel_fb->obj;
  9557. if (mmio_flip->req) {
  9558. WARN_ON(__i915_wait_request(mmio_flip->req,
  9559. false, NULL,
  9560. &mmio_flip->i915->rps.mmioflips));
  9561. i915_gem_request_unreference(mmio_flip->req);
  9562. }
  9563. /* For framebuffer backed by dmabuf, wait for fence */
  9564. if (obj->base.dma_buf)
  9565. WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  9566. false, false,
  9567. MAX_SCHEDULE_TIMEOUT) < 0);
  9568. intel_do_mmio_flip(mmio_flip);
  9569. kfree(mmio_flip);
  9570. }
  9571. static int intel_queue_mmio_flip(struct drm_device *dev,
  9572. struct drm_crtc *crtc,
  9573. struct drm_i915_gem_object *obj)
  9574. {
  9575. struct intel_mmio_flip *mmio_flip;
  9576. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9577. if (mmio_flip == NULL)
  9578. return -ENOMEM;
  9579. mmio_flip->i915 = to_i915(dev);
  9580. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9581. mmio_flip->crtc = to_intel_crtc(crtc);
  9582. mmio_flip->rotation = crtc->primary->state->rotation;
  9583. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9584. schedule_work(&mmio_flip->work);
  9585. return 0;
  9586. }
  9587. static int intel_default_queue_flip(struct drm_device *dev,
  9588. struct drm_crtc *crtc,
  9589. struct drm_framebuffer *fb,
  9590. struct drm_i915_gem_object *obj,
  9591. struct drm_i915_gem_request *req,
  9592. uint32_t flags)
  9593. {
  9594. return -ENODEV;
  9595. }
  9596. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9597. struct drm_crtc *crtc)
  9598. {
  9599. struct drm_i915_private *dev_priv = dev->dev_private;
  9600. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9601. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9602. u32 addr;
  9603. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9604. return true;
  9605. if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
  9606. return false;
  9607. if (!work->enable_stall_check)
  9608. return false;
  9609. if (work->flip_ready_vblank == 0) {
  9610. if (work->flip_queued_req &&
  9611. !i915_gem_request_completed(work->flip_queued_req, true))
  9612. return false;
  9613. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9614. }
  9615. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9616. return false;
  9617. /* Potential stall - if we see that the flip has happened,
  9618. * assume a missed interrupt. */
  9619. if (INTEL_INFO(dev)->gen >= 4)
  9620. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9621. else
  9622. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9623. /* There is a potential issue here with a false positive after a flip
  9624. * to the same address. We could address this by checking for a
  9625. * non-incrementing frame counter.
  9626. */
  9627. return addr == work->gtt_offset;
  9628. }
  9629. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9630. {
  9631. struct drm_i915_private *dev_priv = dev->dev_private;
  9632. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9633. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9634. struct intel_unpin_work *work;
  9635. WARN_ON(!in_interrupt());
  9636. if (crtc == NULL)
  9637. return;
  9638. spin_lock(&dev->event_lock);
  9639. work = intel_crtc->unpin_work;
  9640. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9641. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9642. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9643. page_flip_completed(intel_crtc);
  9644. work = NULL;
  9645. }
  9646. if (work != NULL &&
  9647. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9648. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9649. spin_unlock(&dev->event_lock);
  9650. }
  9651. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9652. struct drm_framebuffer *fb,
  9653. struct drm_pending_vblank_event *event,
  9654. uint32_t page_flip_flags)
  9655. {
  9656. struct drm_device *dev = crtc->dev;
  9657. struct drm_i915_private *dev_priv = dev->dev_private;
  9658. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9659. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9660. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9661. struct drm_plane *primary = crtc->primary;
  9662. enum pipe pipe = intel_crtc->pipe;
  9663. struct intel_unpin_work *work;
  9664. struct intel_engine_cs *engine;
  9665. bool mmio_flip;
  9666. struct drm_i915_gem_request *request = NULL;
  9667. int ret;
  9668. /*
  9669. * drm_mode_page_flip_ioctl() should already catch this, but double
  9670. * check to be safe. In the future we may enable pageflipping from
  9671. * a disabled primary plane.
  9672. */
  9673. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9674. return -EBUSY;
  9675. /* Can't change pixel format via MI display flips. */
  9676. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9677. return -EINVAL;
  9678. /*
  9679. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9680. * Note that pitch changes could also affect these register.
  9681. */
  9682. if (INTEL_INFO(dev)->gen > 3 &&
  9683. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9684. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9685. return -EINVAL;
  9686. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9687. goto out_hang;
  9688. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9689. if (work == NULL)
  9690. return -ENOMEM;
  9691. work->event = event;
  9692. work->crtc = crtc;
  9693. work->old_fb = old_fb;
  9694. INIT_WORK(&work->work, intel_unpin_work_fn);
  9695. ret = drm_crtc_vblank_get(crtc);
  9696. if (ret)
  9697. goto free_work;
  9698. /* We borrow the event spin lock for protecting unpin_work */
  9699. spin_lock_irq(&dev->event_lock);
  9700. if (intel_crtc->unpin_work) {
  9701. /* Before declaring the flip queue wedged, check if
  9702. * the hardware completed the operation behind our backs.
  9703. */
  9704. if (__intel_pageflip_stall_check(dev, crtc)) {
  9705. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9706. page_flip_completed(intel_crtc);
  9707. } else {
  9708. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9709. spin_unlock_irq(&dev->event_lock);
  9710. drm_crtc_vblank_put(crtc);
  9711. kfree(work);
  9712. return -EBUSY;
  9713. }
  9714. }
  9715. intel_crtc->unpin_work = work;
  9716. spin_unlock_irq(&dev->event_lock);
  9717. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9718. flush_workqueue(dev_priv->wq);
  9719. /* Reference the objects for the scheduled work. */
  9720. drm_framebuffer_reference(work->old_fb);
  9721. drm_gem_object_reference(&obj->base);
  9722. crtc->primary->fb = fb;
  9723. update_state_fb(crtc->primary);
  9724. intel_fbc_pre_update(intel_crtc);
  9725. work->pending_flip_obj = obj;
  9726. ret = i915_mutex_lock_interruptible(dev);
  9727. if (ret)
  9728. goto cleanup;
  9729. intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9730. if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
  9731. ret = -EIO;
  9732. goto cleanup;
  9733. }
  9734. atomic_inc(&intel_crtc->unpin_work_count);
  9735. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9736. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9737. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  9738. engine = &dev_priv->engine[BCS];
  9739. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9740. /* vlv: DISPLAY_FLIP fails to change tiling */
  9741. engine = NULL;
  9742. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9743. engine = &dev_priv->engine[BCS];
  9744. } else if (INTEL_INFO(dev)->gen >= 7) {
  9745. engine = i915_gem_request_get_engine(obj->last_write_req);
  9746. if (engine == NULL || engine->id != RCS)
  9747. engine = &dev_priv->engine[BCS];
  9748. } else {
  9749. engine = &dev_priv->engine[RCS];
  9750. }
  9751. mmio_flip = use_mmio_flip(engine, obj);
  9752. /* When using CS flips, we want to emit semaphores between rings.
  9753. * However, when using mmio flips we will create a task to do the
  9754. * synchronisation, so all we want here is to pin the framebuffer
  9755. * into the display plane and skip any waits.
  9756. */
  9757. if (!mmio_flip) {
  9758. ret = i915_gem_object_sync(obj, engine, &request);
  9759. if (ret)
  9760. goto cleanup_pending;
  9761. }
  9762. ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  9763. if (ret)
  9764. goto cleanup_pending;
  9765. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9766. obj, 0);
  9767. work->gtt_offset += intel_crtc->dspaddr_offset;
  9768. if (mmio_flip) {
  9769. ret = intel_queue_mmio_flip(dev, crtc, obj);
  9770. if (ret)
  9771. goto cleanup_unpin;
  9772. i915_gem_request_assign(&work->flip_queued_req,
  9773. obj->last_write_req);
  9774. } else {
  9775. if (!request) {
  9776. request = i915_gem_request_alloc(engine, NULL);
  9777. if (IS_ERR(request)) {
  9778. ret = PTR_ERR(request);
  9779. goto cleanup_unpin;
  9780. }
  9781. }
  9782. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9783. page_flip_flags);
  9784. if (ret)
  9785. goto cleanup_unpin;
  9786. i915_gem_request_assign(&work->flip_queued_req, request);
  9787. }
  9788. if (request)
  9789. i915_add_request_no_flush(request);
  9790. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9791. work->enable_stall_check = true;
  9792. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9793. to_intel_plane(primary)->frontbuffer_bit);
  9794. mutex_unlock(&dev->struct_mutex);
  9795. intel_frontbuffer_flip_prepare(dev,
  9796. to_intel_plane(primary)->frontbuffer_bit);
  9797. trace_i915_flip_request(intel_crtc->plane, obj);
  9798. return 0;
  9799. cleanup_unpin:
  9800. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  9801. cleanup_pending:
  9802. if (!IS_ERR_OR_NULL(request))
  9803. i915_add_request_no_flush(request);
  9804. atomic_dec(&intel_crtc->unpin_work_count);
  9805. mutex_unlock(&dev->struct_mutex);
  9806. cleanup:
  9807. crtc->primary->fb = old_fb;
  9808. update_state_fb(crtc->primary);
  9809. drm_gem_object_unreference_unlocked(&obj->base);
  9810. drm_framebuffer_unreference(work->old_fb);
  9811. spin_lock_irq(&dev->event_lock);
  9812. intel_crtc->unpin_work = NULL;
  9813. spin_unlock_irq(&dev->event_lock);
  9814. drm_crtc_vblank_put(crtc);
  9815. free_work:
  9816. kfree(work);
  9817. if (ret == -EIO) {
  9818. struct drm_atomic_state *state;
  9819. struct drm_plane_state *plane_state;
  9820. out_hang:
  9821. state = drm_atomic_state_alloc(dev);
  9822. if (!state)
  9823. return -ENOMEM;
  9824. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9825. retry:
  9826. plane_state = drm_atomic_get_plane_state(state, primary);
  9827. ret = PTR_ERR_OR_ZERO(plane_state);
  9828. if (!ret) {
  9829. drm_atomic_set_fb_for_plane(plane_state, fb);
  9830. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9831. if (!ret)
  9832. ret = drm_atomic_commit(state);
  9833. }
  9834. if (ret == -EDEADLK) {
  9835. drm_modeset_backoff(state->acquire_ctx);
  9836. drm_atomic_state_clear(state);
  9837. goto retry;
  9838. }
  9839. if (ret)
  9840. drm_atomic_state_free(state);
  9841. if (ret == 0 && event) {
  9842. spin_lock_irq(&dev->event_lock);
  9843. drm_crtc_send_vblank_event(crtc, event);
  9844. spin_unlock_irq(&dev->event_lock);
  9845. }
  9846. }
  9847. return ret;
  9848. }
  9849. /**
  9850. * intel_wm_need_update - Check whether watermarks need updating
  9851. * @plane: drm plane
  9852. * @state: new plane state
  9853. *
  9854. * Check current plane state versus the new one to determine whether
  9855. * watermarks need to be recalculated.
  9856. *
  9857. * Returns true or false.
  9858. */
  9859. static bool intel_wm_need_update(struct drm_plane *plane,
  9860. struct drm_plane_state *state)
  9861. {
  9862. struct intel_plane_state *new = to_intel_plane_state(state);
  9863. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9864. /* Update watermarks on tiling or size changes. */
  9865. if (new->visible != cur->visible)
  9866. return true;
  9867. if (!cur->base.fb || !new->base.fb)
  9868. return false;
  9869. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  9870. cur->base.rotation != new->base.rotation ||
  9871. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9872. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9873. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9874. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9875. return true;
  9876. return false;
  9877. }
  9878. static bool needs_scaling(struct intel_plane_state *state)
  9879. {
  9880. int src_w = drm_rect_width(&state->src) >> 16;
  9881. int src_h = drm_rect_height(&state->src) >> 16;
  9882. int dst_w = drm_rect_width(&state->dst);
  9883. int dst_h = drm_rect_height(&state->dst);
  9884. return (src_w != dst_w || src_h != dst_h);
  9885. }
  9886. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9887. struct drm_plane_state *plane_state)
  9888. {
  9889. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9890. struct drm_crtc *crtc = crtc_state->crtc;
  9891. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9892. struct drm_plane *plane = plane_state->plane;
  9893. struct drm_device *dev = crtc->dev;
  9894. struct drm_i915_private *dev_priv = to_i915(dev);
  9895. struct intel_plane_state *old_plane_state =
  9896. to_intel_plane_state(plane->state);
  9897. int idx = intel_crtc->base.base.id, ret;
  9898. bool mode_changed = needs_modeset(crtc_state);
  9899. bool was_crtc_enabled = crtc->state->active;
  9900. bool is_crtc_enabled = crtc_state->active;
  9901. bool turn_off, turn_on, visible, was_visible;
  9902. struct drm_framebuffer *fb = plane_state->fb;
  9903. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9904. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9905. ret = skl_update_scaler_plane(
  9906. to_intel_crtc_state(crtc_state),
  9907. to_intel_plane_state(plane_state));
  9908. if (ret)
  9909. return ret;
  9910. }
  9911. was_visible = old_plane_state->visible;
  9912. visible = to_intel_plane_state(plane_state)->visible;
  9913. if (!was_crtc_enabled && WARN_ON(was_visible))
  9914. was_visible = false;
  9915. /*
  9916. * Visibility is calculated as if the crtc was on, but
  9917. * after scaler setup everything depends on it being off
  9918. * when the crtc isn't active.
  9919. */
  9920. if (!is_crtc_enabled)
  9921. to_intel_plane_state(plane_state)->visible = visible = false;
  9922. if (!was_visible && !visible)
  9923. return 0;
  9924. if (fb != old_plane_state->base.fb)
  9925. pipe_config->fb_changed = true;
  9926. turn_off = was_visible && (!visible || mode_changed);
  9927. turn_on = visible && (!was_visible || mode_changed);
  9928. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9929. plane->base.id, fb ? fb->base.id : -1);
  9930. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9931. plane->base.id, was_visible, visible,
  9932. turn_off, turn_on, mode_changed);
  9933. if (turn_on) {
  9934. pipe_config->update_wm_pre = true;
  9935. /* must disable cxsr around plane enable/disable */
  9936. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9937. pipe_config->disable_cxsr = true;
  9938. } else if (turn_off) {
  9939. pipe_config->update_wm_post = true;
  9940. /* must disable cxsr around plane enable/disable */
  9941. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9942. pipe_config->disable_cxsr = true;
  9943. } else if (intel_wm_need_update(plane, plane_state)) {
  9944. /* FIXME bollocks */
  9945. pipe_config->update_wm_pre = true;
  9946. pipe_config->update_wm_post = true;
  9947. }
  9948. /* Pre-gen9 platforms need two-step watermark updates */
  9949. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  9950. INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
  9951. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  9952. if (visible || was_visible)
  9953. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  9954. /*
  9955. * WaCxSRDisabledForSpriteScaling:ivb
  9956. *
  9957. * cstate->update_wm was already set above, so this flag will
  9958. * take effect when we commit and program watermarks.
  9959. */
  9960. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
  9961. needs_scaling(to_intel_plane_state(plane_state)) &&
  9962. !needs_scaling(old_plane_state))
  9963. pipe_config->disable_lp_wm = true;
  9964. return 0;
  9965. }
  9966. static bool encoders_cloneable(const struct intel_encoder *a,
  9967. const struct intel_encoder *b)
  9968. {
  9969. /* masks could be asymmetric, so check both ways */
  9970. return a == b || (a->cloneable & (1 << b->type) &&
  9971. b->cloneable & (1 << a->type));
  9972. }
  9973. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9974. struct intel_crtc *crtc,
  9975. struct intel_encoder *encoder)
  9976. {
  9977. struct intel_encoder *source_encoder;
  9978. struct drm_connector *connector;
  9979. struct drm_connector_state *connector_state;
  9980. int i;
  9981. for_each_connector_in_state(state, connector, connector_state, i) {
  9982. if (connector_state->crtc != &crtc->base)
  9983. continue;
  9984. source_encoder =
  9985. to_intel_encoder(connector_state->best_encoder);
  9986. if (!encoders_cloneable(encoder, source_encoder))
  9987. return false;
  9988. }
  9989. return true;
  9990. }
  9991. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9992. struct intel_crtc *crtc)
  9993. {
  9994. struct intel_encoder *encoder;
  9995. struct drm_connector *connector;
  9996. struct drm_connector_state *connector_state;
  9997. int i;
  9998. for_each_connector_in_state(state, connector, connector_state, i) {
  9999. if (connector_state->crtc != &crtc->base)
  10000. continue;
  10001. encoder = to_intel_encoder(connector_state->best_encoder);
  10002. if (!check_single_encoder_cloning(state, crtc, encoder))
  10003. return false;
  10004. }
  10005. return true;
  10006. }
  10007. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10008. struct drm_crtc_state *crtc_state)
  10009. {
  10010. struct drm_device *dev = crtc->dev;
  10011. struct drm_i915_private *dev_priv = dev->dev_private;
  10012. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10013. struct intel_crtc_state *pipe_config =
  10014. to_intel_crtc_state(crtc_state);
  10015. struct drm_atomic_state *state = crtc_state->state;
  10016. int ret;
  10017. bool mode_changed = needs_modeset(crtc_state);
  10018. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  10019. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10020. return -EINVAL;
  10021. }
  10022. if (mode_changed && !crtc_state->active)
  10023. pipe_config->update_wm_post = true;
  10024. if (mode_changed && crtc_state->enable &&
  10025. dev_priv->display.crtc_compute_clock &&
  10026. !WARN_ON(pipe_config->shared_dpll)) {
  10027. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10028. pipe_config);
  10029. if (ret)
  10030. return ret;
  10031. }
  10032. if (crtc_state->color_mgmt_changed) {
  10033. ret = intel_color_check(crtc, crtc_state);
  10034. if (ret)
  10035. return ret;
  10036. }
  10037. ret = 0;
  10038. if (dev_priv->display.compute_pipe_wm) {
  10039. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10040. if (ret) {
  10041. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10042. return ret;
  10043. }
  10044. }
  10045. if (dev_priv->display.compute_intermediate_wm &&
  10046. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10047. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10048. return 0;
  10049. /*
  10050. * Calculate 'intermediate' watermarks that satisfy both the
  10051. * old state and the new state. We can program these
  10052. * immediately.
  10053. */
  10054. ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
  10055. intel_crtc,
  10056. pipe_config);
  10057. if (ret) {
  10058. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10059. return ret;
  10060. }
  10061. }
  10062. if (INTEL_INFO(dev)->gen >= 9) {
  10063. if (mode_changed)
  10064. ret = skl_update_scaler_crtc(pipe_config);
  10065. if (!ret)
  10066. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10067. pipe_config);
  10068. }
  10069. return ret;
  10070. }
  10071. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10072. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10073. .atomic_begin = intel_begin_crtc_commit,
  10074. .atomic_flush = intel_finish_crtc_commit,
  10075. .atomic_check = intel_crtc_atomic_check,
  10076. };
  10077. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10078. {
  10079. struct intel_connector *connector;
  10080. for_each_intel_connector(dev, connector) {
  10081. if (connector->base.encoder) {
  10082. connector->base.state->best_encoder =
  10083. connector->base.encoder;
  10084. connector->base.state->crtc =
  10085. connector->base.encoder->crtc;
  10086. } else {
  10087. connector->base.state->best_encoder = NULL;
  10088. connector->base.state->crtc = NULL;
  10089. }
  10090. }
  10091. }
  10092. static void
  10093. connected_sink_compute_bpp(struct intel_connector *connector,
  10094. struct intel_crtc_state *pipe_config)
  10095. {
  10096. int bpp = pipe_config->pipe_bpp;
  10097. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10098. connector->base.base.id,
  10099. connector->base.name);
  10100. /* Don't use an invalid EDID bpc value */
  10101. if (connector->base.display_info.bpc &&
  10102. connector->base.display_info.bpc * 3 < bpp) {
  10103. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10104. bpp, connector->base.display_info.bpc*3);
  10105. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10106. }
  10107. /* Clamp bpp to default limit on screens without EDID 1.4 */
  10108. if (connector->base.display_info.bpc == 0) {
  10109. int type = connector->base.connector_type;
  10110. int clamp_bpp = 24;
  10111. /* Fall back to 18 bpp when DP sink capability is unknown. */
  10112. if (type == DRM_MODE_CONNECTOR_DisplayPort ||
  10113. type == DRM_MODE_CONNECTOR_eDP)
  10114. clamp_bpp = 18;
  10115. if (bpp > clamp_bpp) {
  10116. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
  10117. bpp, clamp_bpp);
  10118. pipe_config->pipe_bpp = clamp_bpp;
  10119. }
  10120. }
  10121. }
  10122. static int
  10123. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10124. struct intel_crtc_state *pipe_config)
  10125. {
  10126. struct drm_device *dev = crtc->base.dev;
  10127. struct drm_atomic_state *state;
  10128. struct drm_connector *connector;
  10129. struct drm_connector_state *connector_state;
  10130. int bpp, i;
  10131. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  10132. bpp = 10*3;
  10133. else if (INTEL_INFO(dev)->gen >= 5)
  10134. bpp = 12*3;
  10135. else
  10136. bpp = 8*3;
  10137. pipe_config->pipe_bpp = bpp;
  10138. state = pipe_config->base.state;
  10139. /* Clamp display bpp to EDID value */
  10140. for_each_connector_in_state(state, connector, connector_state, i) {
  10141. if (connector_state->crtc != &crtc->base)
  10142. continue;
  10143. connected_sink_compute_bpp(to_intel_connector(connector),
  10144. pipe_config);
  10145. }
  10146. return bpp;
  10147. }
  10148. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10149. {
  10150. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10151. "type: 0x%x flags: 0x%x\n",
  10152. mode->crtc_clock,
  10153. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10154. mode->crtc_hsync_end, mode->crtc_htotal,
  10155. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10156. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10157. }
  10158. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10159. struct intel_crtc_state *pipe_config,
  10160. const char *context)
  10161. {
  10162. struct drm_device *dev = crtc->base.dev;
  10163. struct drm_plane *plane;
  10164. struct intel_plane *intel_plane;
  10165. struct intel_plane_state *state;
  10166. struct drm_framebuffer *fb;
  10167. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  10168. context, pipe_config, pipe_name(crtc->pipe));
  10169. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  10170. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10171. pipe_config->pipe_bpp, pipe_config->dither);
  10172. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10173. pipe_config->has_pch_encoder,
  10174. pipe_config->fdi_lanes,
  10175. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10176. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10177. pipe_config->fdi_m_n.tu);
  10178. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10179. pipe_config->has_dp_encoder,
  10180. pipe_config->lane_count,
  10181. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10182. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10183. pipe_config->dp_m_n.tu);
  10184. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10185. pipe_config->has_dp_encoder,
  10186. pipe_config->lane_count,
  10187. pipe_config->dp_m2_n2.gmch_m,
  10188. pipe_config->dp_m2_n2.gmch_n,
  10189. pipe_config->dp_m2_n2.link_m,
  10190. pipe_config->dp_m2_n2.link_n,
  10191. pipe_config->dp_m2_n2.tu);
  10192. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10193. pipe_config->has_audio,
  10194. pipe_config->has_infoframe);
  10195. DRM_DEBUG_KMS("requested mode:\n");
  10196. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10197. DRM_DEBUG_KMS("adjusted mode:\n");
  10198. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10199. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10200. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10201. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10202. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10203. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10204. crtc->num_scalers,
  10205. pipe_config->scaler_state.scaler_users,
  10206. pipe_config->scaler_state.scaler_id);
  10207. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10208. pipe_config->gmch_pfit.control,
  10209. pipe_config->gmch_pfit.pgm_ratios,
  10210. pipe_config->gmch_pfit.lvds_border_bits);
  10211. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10212. pipe_config->pch_pfit.pos,
  10213. pipe_config->pch_pfit.size,
  10214. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10215. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10216. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10217. if (IS_BROXTON(dev)) {
  10218. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10219. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10220. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10221. pipe_config->ddi_pll_sel,
  10222. pipe_config->dpll_hw_state.ebb0,
  10223. pipe_config->dpll_hw_state.ebb4,
  10224. pipe_config->dpll_hw_state.pll0,
  10225. pipe_config->dpll_hw_state.pll1,
  10226. pipe_config->dpll_hw_state.pll2,
  10227. pipe_config->dpll_hw_state.pll3,
  10228. pipe_config->dpll_hw_state.pll6,
  10229. pipe_config->dpll_hw_state.pll8,
  10230. pipe_config->dpll_hw_state.pll9,
  10231. pipe_config->dpll_hw_state.pll10,
  10232. pipe_config->dpll_hw_state.pcsdw12);
  10233. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10234. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10235. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10236. pipe_config->ddi_pll_sel,
  10237. pipe_config->dpll_hw_state.ctrl1,
  10238. pipe_config->dpll_hw_state.cfgcr1,
  10239. pipe_config->dpll_hw_state.cfgcr2);
  10240. } else if (HAS_DDI(dev)) {
  10241. DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10242. pipe_config->ddi_pll_sel,
  10243. pipe_config->dpll_hw_state.wrpll,
  10244. pipe_config->dpll_hw_state.spll);
  10245. } else {
  10246. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10247. "fp0: 0x%x, fp1: 0x%x\n",
  10248. pipe_config->dpll_hw_state.dpll,
  10249. pipe_config->dpll_hw_state.dpll_md,
  10250. pipe_config->dpll_hw_state.fp0,
  10251. pipe_config->dpll_hw_state.fp1);
  10252. }
  10253. DRM_DEBUG_KMS("planes on this crtc\n");
  10254. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10255. intel_plane = to_intel_plane(plane);
  10256. if (intel_plane->pipe != crtc->pipe)
  10257. continue;
  10258. state = to_intel_plane_state(plane->state);
  10259. fb = state->base.fb;
  10260. if (!fb) {
  10261. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10262. "disabled, scaler_id = %d\n",
  10263. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10264. plane->base.id, intel_plane->pipe,
  10265. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10266. drm_plane_index(plane), state->scaler_id);
  10267. continue;
  10268. }
  10269. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10270. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10271. plane->base.id, intel_plane->pipe,
  10272. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10273. drm_plane_index(plane));
  10274. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10275. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10276. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10277. state->scaler_id,
  10278. state->src.x1 >> 16, state->src.y1 >> 16,
  10279. drm_rect_width(&state->src) >> 16,
  10280. drm_rect_height(&state->src) >> 16,
  10281. state->dst.x1, state->dst.y1,
  10282. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10283. }
  10284. }
  10285. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10286. {
  10287. struct drm_device *dev = state->dev;
  10288. struct drm_connector *connector;
  10289. unsigned int used_ports = 0;
  10290. /*
  10291. * Walk the connector list instead of the encoder
  10292. * list to detect the problem on ddi platforms
  10293. * where there's just one encoder per digital port.
  10294. */
  10295. drm_for_each_connector(connector, dev) {
  10296. struct drm_connector_state *connector_state;
  10297. struct intel_encoder *encoder;
  10298. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10299. if (!connector_state)
  10300. connector_state = connector->state;
  10301. if (!connector_state->best_encoder)
  10302. continue;
  10303. encoder = to_intel_encoder(connector_state->best_encoder);
  10304. WARN_ON(!connector_state->crtc);
  10305. switch (encoder->type) {
  10306. unsigned int port_mask;
  10307. case INTEL_OUTPUT_UNKNOWN:
  10308. if (WARN_ON(!HAS_DDI(dev)))
  10309. break;
  10310. case INTEL_OUTPUT_DISPLAYPORT:
  10311. case INTEL_OUTPUT_HDMI:
  10312. case INTEL_OUTPUT_EDP:
  10313. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10314. /* the same port mustn't appear more than once */
  10315. if (used_ports & port_mask)
  10316. return false;
  10317. used_ports |= port_mask;
  10318. default:
  10319. break;
  10320. }
  10321. }
  10322. return true;
  10323. }
  10324. static void
  10325. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10326. {
  10327. struct drm_crtc_state tmp_state;
  10328. struct intel_crtc_scaler_state scaler_state;
  10329. struct intel_dpll_hw_state dpll_hw_state;
  10330. struct intel_shared_dpll *shared_dpll;
  10331. uint32_t ddi_pll_sel;
  10332. bool force_thru;
  10333. /* FIXME: before the switch to atomic started, a new pipe_config was
  10334. * kzalloc'd. Code that depends on any field being zero should be
  10335. * fixed, so that the crtc_state can be safely duplicated. For now,
  10336. * only fields that are know to not cause problems are preserved. */
  10337. tmp_state = crtc_state->base;
  10338. scaler_state = crtc_state->scaler_state;
  10339. shared_dpll = crtc_state->shared_dpll;
  10340. dpll_hw_state = crtc_state->dpll_hw_state;
  10341. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10342. force_thru = crtc_state->pch_pfit.force_thru;
  10343. memset(crtc_state, 0, sizeof *crtc_state);
  10344. crtc_state->base = tmp_state;
  10345. crtc_state->scaler_state = scaler_state;
  10346. crtc_state->shared_dpll = shared_dpll;
  10347. crtc_state->dpll_hw_state = dpll_hw_state;
  10348. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10349. crtc_state->pch_pfit.force_thru = force_thru;
  10350. }
  10351. static int
  10352. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10353. struct intel_crtc_state *pipe_config)
  10354. {
  10355. struct drm_atomic_state *state = pipe_config->base.state;
  10356. struct intel_encoder *encoder;
  10357. struct drm_connector *connector;
  10358. struct drm_connector_state *connector_state;
  10359. int base_bpp, ret = -EINVAL;
  10360. int i;
  10361. bool retry = true;
  10362. clear_intel_crtc_state(pipe_config);
  10363. pipe_config->cpu_transcoder =
  10364. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10365. /*
  10366. * Sanitize sync polarity flags based on requested ones. If neither
  10367. * positive or negative polarity is requested, treat this as meaning
  10368. * negative polarity.
  10369. */
  10370. if (!(pipe_config->base.adjusted_mode.flags &
  10371. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10372. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10373. if (!(pipe_config->base.adjusted_mode.flags &
  10374. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10375. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10376. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10377. pipe_config);
  10378. if (base_bpp < 0)
  10379. goto fail;
  10380. /*
  10381. * Determine the real pipe dimensions. Note that stereo modes can
  10382. * increase the actual pipe size due to the frame doubling and
  10383. * insertion of additional space for blanks between the frame. This
  10384. * is stored in the crtc timings. We use the requested mode to do this
  10385. * computation to clearly distinguish it from the adjusted mode, which
  10386. * can be changed by the connectors in the below retry loop.
  10387. */
  10388. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10389. &pipe_config->pipe_src_w,
  10390. &pipe_config->pipe_src_h);
  10391. encoder_retry:
  10392. /* Ensure the port clock defaults are reset when retrying. */
  10393. pipe_config->port_clock = 0;
  10394. pipe_config->pixel_multiplier = 1;
  10395. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10396. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10397. CRTC_STEREO_DOUBLE);
  10398. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10399. * adjust it according to limitations or connector properties, and also
  10400. * a chance to reject the mode entirely.
  10401. */
  10402. for_each_connector_in_state(state, connector, connector_state, i) {
  10403. if (connector_state->crtc != crtc)
  10404. continue;
  10405. encoder = to_intel_encoder(connector_state->best_encoder);
  10406. if (!(encoder->compute_config(encoder, pipe_config))) {
  10407. DRM_DEBUG_KMS("Encoder config failure\n");
  10408. goto fail;
  10409. }
  10410. }
  10411. /* Set default port clock if not overwritten by the encoder. Needs to be
  10412. * done afterwards in case the encoder adjusts the mode. */
  10413. if (!pipe_config->port_clock)
  10414. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10415. * pipe_config->pixel_multiplier;
  10416. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10417. if (ret < 0) {
  10418. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10419. goto fail;
  10420. }
  10421. if (ret == RETRY) {
  10422. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10423. ret = -EINVAL;
  10424. goto fail;
  10425. }
  10426. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10427. retry = false;
  10428. goto encoder_retry;
  10429. }
  10430. /* Dithering seems to not pass-through bits correctly when it should, so
  10431. * only enable it on 6bpc panels. */
  10432. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10433. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10434. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10435. fail:
  10436. return ret;
  10437. }
  10438. static void
  10439. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10440. {
  10441. struct drm_crtc *crtc;
  10442. struct drm_crtc_state *crtc_state;
  10443. int i;
  10444. /* Double check state. */
  10445. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10446. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10447. /* Update hwmode for vblank functions */
  10448. if (crtc->state->active)
  10449. crtc->hwmode = crtc->state->adjusted_mode;
  10450. else
  10451. crtc->hwmode.crtc_clock = 0;
  10452. /*
  10453. * Update legacy state to satisfy fbc code. This can
  10454. * be removed when fbc uses the atomic state.
  10455. */
  10456. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10457. struct drm_plane_state *plane_state = crtc->primary->state;
  10458. crtc->primary->fb = plane_state->fb;
  10459. crtc->x = plane_state->src_x >> 16;
  10460. crtc->y = plane_state->src_y >> 16;
  10461. }
  10462. }
  10463. }
  10464. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10465. {
  10466. int diff;
  10467. if (clock1 == clock2)
  10468. return true;
  10469. if (!clock1 || !clock2)
  10470. return false;
  10471. diff = abs(clock1 - clock2);
  10472. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10473. return true;
  10474. return false;
  10475. }
  10476. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10477. list_for_each_entry((intel_crtc), \
  10478. &(dev)->mode_config.crtc_list, \
  10479. base.head) \
  10480. for_each_if (mask & (1 <<(intel_crtc)->pipe))
  10481. static bool
  10482. intel_compare_m_n(unsigned int m, unsigned int n,
  10483. unsigned int m2, unsigned int n2,
  10484. bool exact)
  10485. {
  10486. if (m == m2 && n == n2)
  10487. return true;
  10488. if (exact || !m || !n || !m2 || !n2)
  10489. return false;
  10490. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10491. if (n > n2) {
  10492. while (n > n2) {
  10493. m2 <<= 1;
  10494. n2 <<= 1;
  10495. }
  10496. } else if (n < n2) {
  10497. while (n < n2) {
  10498. m <<= 1;
  10499. n <<= 1;
  10500. }
  10501. }
  10502. if (n != n2)
  10503. return false;
  10504. return intel_fuzzy_clock_check(m, m2);
  10505. }
  10506. static bool
  10507. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10508. struct intel_link_m_n *m2_n2,
  10509. bool adjust)
  10510. {
  10511. if (m_n->tu == m2_n2->tu &&
  10512. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10513. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10514. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10515. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10516. if (adjust)
  10517. *m2_n2 = *m_n;
  10518. return true;
  10519. }
  10520. return false;
  10521. }
  10522. static bool
  10523. intel_pipe_config_compare(struct drm_device *dev,
  10524. struct intel_crtc_state *current_config,
  10525. struct intel_crtc_state *pipe_config,
  10526. bool adjust)
  10527. {
  10528. bool ret = true;
  10529. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10530. do { \
  10531. if (!adjust) \
  10532. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10533. else \
  10534. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10535. } while (0)
  10536. #define PIPE_CONF_CHECK_X(name) \
  10537. if (current_config->name != pipe_config->name) { \
  10538. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10539. "(expected 0x%08x, found 0x%08x)\n", \
  10540. current_config->name, \
  10541. pipe_config->name); \
  10542. ret = false; \
  10543. }
  10544. #define PIPE_CONF_CHECK_I(name) \
  10545. if (current_config->name != pipe_config->name) { \
  10546. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10547. "(expected %i, found %i)\n", \
  10548. current_config->name, \
  10549. pipe_config->name); \
  10550. ret = false; \
  10551. }
  10552. #define PIPE_CONF_CHECK_P(name) \
  10553. if (current_config->name != pipe_config->name) { \
  10554. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10555. "(expected %p, found %p)\n", \
  10556. current_config->name, \
  10557. pipe_config->name); \
  10558. ret = false; \
  10559. }
  10560. #define PIPE_CONF_CHECK_M_N(name) \
  10561. if (!intel_compare_link_m_n(&current_config->name, \
  10562. &pipe_config->name,\
  10563. adjust)) { \
  10564. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10565. "(expected tu %i gmch %i/%i link %i/%i, " \
  10566. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10567. current_config->name.tu, \
  10568. current_config->name.gmch_m, \
  10569. current_config->name.gmch_n, \
  10570. current_config->name.link_m, \
  10571. current_config->name.link_n, \
  10572. pipe_config->name.tu, \
  10573. pipe_config->name.gmch_m, \
  10574. pipe_config->name.gmch_n, \
  10575. pipe_config->name.link_m, \
  10576. pipe_config->name.link_n); \
  10577. ret = false; \
  10578. }
  10579. /* This is required for BDW+ where there is only one set of registers for
  10580. * switching between high and low RR.
  10581. * This macro can be used whenever a comparison has to be made between one
  10582. * hw state and multiple sw state variables.
  10583. */
  10584. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10585. if (!intel_compare_link_m_n(&current_config->name, \
  10586. &pipe_config->name, adjust) && \
  10587. !intel_compare_link_m_n(&current_config->alt_name, \
  10588. &pipe_config->name, adjust)) { \
  10589. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10590. "(expected tu %i gmch %i/%i link %i/%i, " \
  10591. "or tu %i gmch %i/%i link %i/%i, " \
  10592. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10593. current_config->name.tu, \
  10594. current_config->name.gmch_m, \
  10595. current_config->name.gmch_n, \
  10596. current_config->name.link_m, \
  10597. current_config->name.link_n, \
  10598. current_config->alt_name.tu, \
  10599. current_config->alt_name.gmch_m, \
  10600. current_config->alt_name.gmch_n, \
  10601. current_config->alt_name.link_m, \
  10602. current_config->alt_name.link_n, \
  10603. pipe_config->name.tu, \
  10604. pipe_config->name.gmch_m, \
  10605. pipe_config->name.gmch_n, \
  10606. pipe_config->name.link_m, \
  10607. pipe_config->name.link_n); \
  10608. ret = false; \
  10609. }
  10610. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10611. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10612. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10613. "(expected %i, found %i)\n", \
  10614. current_config->name & (mask), \
  10615. pipe_config->name & (mask)); \
  10616. ret = false; \
  10617. }
  10618. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10619. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10620. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10621. "(expected %i, found %i)\n", \
  10622. current_config->name, \
  10623. pipe_config->name); \
  10624. ret = false; \
  10625. }
  10626. #define PIPE_CONF_QUIRK(quirk) \
  10627. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10628. PIPE_CONF_CHECK_I(cpu_transcoder);
  10629. PIPE_CONF_CHECK_I(has_pch_encoder);
  10630. PIPE_CONF_CHECK_I(fdi_lanes);
  10631. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10632. PIPE_CONF_CHECK_I(has_dp_encoder);
  10633. PIPE_CONF_CHECK_I(lane_count);
  10634. if (INTEL_INFO(dev)->gen < 8) {
  10635. PIPE_CONF_CHECK_M_N(dp_m_n);
  10636. if (current_config->has_drrs)
  10637. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10638. } else
  10639. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10640. PIPE_CONF_CHECK_I(has_dsi_encoder);
  10641. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10642. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10643. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10644. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10645. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10646. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10647. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10648. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10649. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10650. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10651. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10652. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10653. PIPE_CONF_CHECK_I(pixel_multiplier);
  10654. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10655. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10656. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  10657. PIPE_CONF_CHECK_I(limited_color_range);
  10658. PIPE_CONF_CHECK_I(has_infoframe);
  10659. PIPE_CONF_CHECK_I(has_audio);
  10660. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10661. DRM_MODE_FLAG_INTERLACE);
  10662. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10663. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10664. DRM_MODE_FLAG_PHSYNC);
  10665. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10666. DRM_MODE_FLAG_NHSYNC);
  10667. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10668. DRM_MODE_FLAG_PVSYNC);
  10669. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10670. DRM_MODE_FLAG_NVSYNC);
  10671. }
  10672. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10673. /* pfit ratios are autocomputed by the hw on gen4+ */
  10674. if (INTEL_INFO(dev)->gen < 4)
  10675. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  10676. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10677. if (!adjust) {
  10678. PIPE_CONF_CHECK_I(pipe_src_w);
  10679. PIPE_CONF_CHECK_I(pipe_src_h);
  10680. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10681. if (current_config->pch_pfit.enabled) {
  10682. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10683. PIPE_CONF_CHECK_X(pch_pfit.size);
  10684. }
  10685. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10686. }
  10687. /* BDW+ don't expose a synchronous way to read the state */
  10688. if (IS_HASWELL(dev))
  10689. PIPE_CONF_CHECK_I(ips_enabled);
  10690. PIPE_CONF_CHECK_I(double_wide);
  10691. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10692. PIPE_CONF_CHECK_P(shared_dpll);
  10693. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10694. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10695. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10696. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10697. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10698. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10699. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10700. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10701. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10702. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  10703. PIPE_CONF_CHECK_X(dsi_pll.div);
  10704. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10705. PIPE_CONF_CHECK_I(pipe_bpp);
  10706. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10707. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10708. #undef PIPE_CONF_CHECK_X
  10709. #undef PIPE_CONF_CHECK_I
  10710. #undef PIPE_CONF_CHECK_P
  10711. #undef PIPE_CONF_CHECK_FLAGS
  10712. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10713. #undef PIPE_CONF_QUIRK
  10714. #undef INTEL_ERR_OR_DBG_KMS
  10715. return ret;
  10716. }
  10717. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  10718. const struct intel_crtc_state *pipe_config)
  10719. {
  10720. if (pipe_config->has_pch_encoder) {
  10721. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  10722. &pipe_config->fdi_m_n);
  10723. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  10724. /*
  10725. * FDI already provided one idea for the dotclock.
  10726. * Yell if the encoder disagrees.
  10727. */
  10728. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  10729. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10730. fdi_dotclock, dotclock);
  10731. }
  10732. }
  10733. static void verify_wm_state(struct drm_crtc *crtc,
  10734. struct drm_crtc_state *new_state)
  10735. {
  10736. struct drm_device *dev = crtc->dev;
  10737. struct drm_i915_private *dev_priv = dev->dev_private;
  10738. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10739. struct skl_ddb_entry *hw_entry, *sw_entry;
  10740. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10741. const enum pipe pipe = intel_crtc->pipe;
  10742. int plane;
  10743. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  10744. return;
  10745. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10746. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10747. /* planes */
  10748. for_each_plane(dev_priv, pipe, plane) {
  10749. hw_entry = &hw_ddb.plane[pipe][plane];
  10750. sw_entry = &sw_ddb->plane[pipe][plane];
  10751. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10752. continue;
  10753. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10754. "(expected (%u,%u), found (%u,%u))\n",
  10755. pipe_name(pipe), plane + 1,
  10756. sw_entry->start, sw_entry->end,
  10757. hw_entry->start, hw_entry->end);
  10758. }
  10759. /* cursor */
  10760. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10761. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10762. if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
  10763. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10764. "(expected (%u,%u), found (%u,%u))\n",
  10765. pipe_name(pipe),
  10766. sw_entry->start, sw_entry->end,
  10767. hw_entry->start, hw_entry->end);
  10768. }
  10769. }
  10770. static void
  10771. verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
  10772. {
  10773. struct drm_connector *connector;
  10774. drm_for_each_connector(connector, dev) {
  10775. struct drm_encoder *encoder = connector->encoder;
  10776. struct drm_connector_state *state = connector->state;
  10777. if (state->crtc != crtc)
  10778. continue;
  10779. intel_connector_verify_state(to_intel_connector(connector));
  10780. I915_STATE_WARN(state->best_encoder != encoder,
  10781. "connector's atomic encoder doesn't match legacy encoder\n");
  10782. }
  10783. }
  10784. static void
  10785. verify_encoder_state(struct drm_device *dev)
  10786. {
  10787. struct intel_encoder *encoder;
  10788. struct intel_connector *connector;
  10789. for_each_intel_encoder(dev, encoder) {
  10790. bool enabled = false;
  10791. enum pipe pipe;
  10792. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10793. encoder->base.base.id,
  10794. encoder->base.name);
  10795. for_each_intel_connector(dev, connector) {
  10796. if (connector->base.state->best_encoder != &encoder->base)
  10797. continue;
  10798. enabled = true;
  10799. I915_STATE_WARN(connector->base.state->crtc !=
  10800. encoder->base.crtc,
  10801. "connector's crtc doesn't match encoder crtc\n");
  10802. }
  10803. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10804. "encoder's enabled state mismatch "
  10805. "(expected %i, found %i)\n",
  10806. !!encoder->base.crtc, enabled);
  10807. if (!encoder->base.crtc) {
  10808. bool active;
  10809. active = encoder->get_hw_state(encoder, &pipe);
  10810. I915_STATE_WARN(active,
  10811. "encoder detached but still enabled on pipe %c.\n",
  10812. pipe_name(pipe));
  10813. }
  10814. }
  10815. }
  10816. static void
  10817. verify_crtc_state(struct drm_crtc *crtc,
  10818. struct drm_crtc_state *old_crtc_state,
  10819. struct drm_crtc_state *new_crtc_state)
  10820. {
  10821. struct drm_device *dev = crtc->dev;
  10822. struct drm_i915_private *dev_priv = dev->dev_private;
  10823. struct intel_encoder *encoder;
  10824. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10825. struct intel_crtc_state *pipe_config, *sw_config;
  10826. struct drm_atomic_state *old_state;
  10827. bool active;
  10828. old_state = old_crtc_state->state;
  10829. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10830. pipe_config = to_intel_crtc_state(old_crtc_state);
  10831. memset(pipe_config, 0, sizeof(*pipe_config));
  10832. pipe_config->base.crtc = crtc;
  10833. pipe_config->base.state = old_state;
  10834. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  10835. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10836. /* hw state is inconsistent with the pipe quirk */
  10837. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10838. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10839. active = new_crtc_state->active;
  10840. I915_STATE_WARN(new_crtc_state->active != active,
  10841. "crtc active state doesn't match with hw state "
  10842. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10843. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10844. "transitional active state does not match atomic hw state "
  10845. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10846. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10847. enum pipe pipe;
  10848. active = encoder->get_hw_state(encoder, &pipe);
  10849. I915_STATE_WARN(active != new_crtc_state->active,
  10850. "[ENCODER:%i] active %i with crtc active %i\n",
  10851. encoder->base.base.id, active, new_crtc_state->active);
  10852. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10853. "Encoder connected to wrong pipe %c\n",
  10854. pipe_name(pipe));
  10855. if (active)
  10856. encoder->get_config(encoder, pipe_config);
  10857. }
  10858. if (!new_crtc_state->active)
  10859. return;
  10860. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10861. sw_config = to_intel_crtc_state(crtc->state);
  10862. if (!intel_pipe_config_compare(dev, sw_config,
  10863. pipe_config, false)) {
  10864. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10865. intel_dump_pipe_config(intel_crtc, pipe_config,
  10866. "[hw state]");
  10867. intel_dump_pipe_config(intel_crtc, sw_config,
  10868. "[sw state]");
  10869. }
  10870. }
  10871. static void
  10872. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10873. struct intel_shared_dpll *pll,
  10874. struct drm_crtc *crtc,
  10875. struct drm_crtc_state *new_state)
  10876. {
  10877. struct intel_dpll_hw_state dpll_hw_state;
  10878. unsigned crtc_mask;
  10879. bool active;
  10880. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10881. DRM_DEBUG_KMS("%s\n", pll->name);
  10882. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10883. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10884. I915_STATE_WARN(!pll->on && pll->active_mask,
  10885. "pll in active use but not on in sw tracking\n");
  10886. I915_STATE_WARN(pll->on && !pll->active_mask,
  10887. "pll is on but not used by any active crtc\n");
  10888. I915_STATE_WARN(pll->on != active,
  10889. "pll on state mismatch (expected %i, found %i)\n",
  10890. pll->on, active);
  10891. }
  10892. if (!crtc) {
  10893. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  10894. "more active pll users than references: %x vs %x\n",
  10895. pll->active_mask, pll->config.crtc_mask);
  10896. return;
  10897. }
  10898. crtc_mask = 1 << drm_crtc_index(crtc);
  10899. if (new_state->active)
  10900. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10901. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10902. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10903. else
  10904. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10905. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10906. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10907. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  10908. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10909. crtc_mask, pll->config.crtc_mask);
  10910. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  10911. &dpll_hw_state,
  10912. sizeof(dpll_hw_state)),
  10913. "pll hw state mismatch\n");
  10914. }
  10915. static void
  10916. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10917. struct drm_crtc_state *old_crtc_state,
  10918. struct drm_crtc_state *new_crtc_state)
  10919. {
  10920. struct drm_i915_private *dev_priv = dev->dev_private;
  10921. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10922. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10923. if (new_state->shared_dpll)
  10924. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10925. if (old_state->shared_dpll &&
  10926. old_state->shared_dpll != new_state->shared_dpll) {
  10927. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  10928. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10929. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10930. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10931. pipe_name(drm_crtc_index(crtc)));
  10932. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  10933. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10934. pipe_name(drm_crtc_index(crtc)));
  10935. }
  10936. }
  10937. static void
  10938. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  10939. struct drm_crtc_state *old_state,
  10940. struct drm_crtc_state *new_state)
  10941. {
  10942. if (!needs_modeset(new_state) &&
  10943. !to_intel_crtc_state(new_state)->update_pipe)
  10944. return;
  10945. verify_wm_state(crtc, new_state);
  10946. verify_connector_state(crtc->dev, crtc);
  10947. verify_crtc_state(crtc, old_state, new_state);
  10948. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  10949. }
  10950. static void
  10951. verify_disabled_dpll_state(struct drm_device *dev)
  10952. {
  10953. struct drm_i915_private *dev_priv = dev->dev_private;
  10954. int i;
  10955. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10956. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  10957. }
  10958. static void
  10959. intel_modeset_verify_disabled(struct drm_device *dev)
  10960. {
  10961. verify_encoder_state(dev);
  10962. verify_connector_state(dev, NULL);
  10963. verify_disabled_dpll_state(dev);
  10964. }
  10965. static void update_scanline_offset(struct intel_crtc *crtc)
  10966. {
  10967. struct drm_device *dev = crtc->base.dev;
  10968. /*
  10969. * The scanline counter increments at the leading edge of hsync.
  10970. *
  10971. * On most platforms it starts counting from vtotal-1 on the
  10972. * first active line. That means the scanline counter value is
  10973. * always one less than what we would expect. Ie. just after
  10974. * start of vblank, which also occurs at start of hsync (on the
  10975. * last active line), the scanline counter will read vblank_start-1.
  10976. *
  10977. * On gen2 the scanline counter starts counting from 1 instead
  10978. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10979. * to keep the value positive), instead of adding one.
  10980. *
  10981. * On HSW+ the behaviour of the scanline counter depends on the output
  10982. * type. For DP ports it behaves like most other platforms, but on HDMI
  10983. * there's an extra 1 line difference. So we need to add two instead of
  10984. * one to the value.
  10985. */
  10986. if (IS_GEN2(dev)) {
  10987. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10988. int vtotal;
  10989. vtotal = adjusted_mode->crtc_vtotal;
  10990. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10991. vtotal /= 2;
  10992. crtc->scanline_offset = vtotal - 1;
  10993. } else if (HAS_DDI(dev) &&
  10994. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10995. crtc->scanline_offset = 2;
  10996. } else
  10997. crtc->scanline_offset = 1;
  10998. }
  10999. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11000. {
  11001. struct drm_device *dev = state->dev;
  11002. struct drm_i915_private *dev_priv = to_i915(dev);
  11003. struct intel_shared_dpll_config *shared_dpll = NULL;
  11004. struct drm_crtc *crtc;
  11005. struct drm_crtc_state *crtc_state;
  11006. int i;
  11007. if (!dev_priv->display.crtc_compute_clock)
  11008. return;
  11009. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11010. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11011. struct intel_shared_dpll *old_dpll =
  11012. to_intel_crtc_state(crtc->state)->shared_dpll;
  11013. if (!needs_modeset(crtc_state))
  11014. continue;
  11015. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11016. if (!old_dpll)
  11017. continue;
  11018. if (!shared_dpll)
  11019. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11020. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11021. }
  11022. }
  11023. /*
  11024. * This implements the workaround described in the "notes" section of the mode
  11025. * set sequence documentation. When going from no pipes or single pipe to
  11026. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11027. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11028. */
  11029. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11030. {
  11031. struct drm_crtc_state *crtc_state;
  11032. struct intel_crtc *intel_crtc;
  11033. struct drm_crtc *crtc;
  11034. struct intel_crtc_state *first_crtc_state = NULL;
  11035. struct intel_crtc_state *other_crtc_state = NULL;
  11036. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11037. int i;
  11038. /* look at all crtc's that are going to be enabled in during modeset */
  11039. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11040. intel_crtc = to_intel_crtc(crtc);
  11041. if (!crtc_state->active || !needs_modeset(crtc_state))
  11042. continue;
  11043. if (first_crtc_state) {
  11044. other_crtc_state = to_intel_crtc_state(crtc_state);
  11045. break;
  11046. } else {
  11047. first_crtc_state = to_intel_crtc_state(crtc_state);
  11048. first_pipe = intel_crtc->pipe;
  11049. }
  11050. }
  11051. /* No workaround needed? */
  11052. if (!first_crtc_state)
  11053. return 0;
  11054. /* w/a possibly needed, check how many crtc's are already enabled. */
  11055. for_each_intel_crtc(state->dev, intel_crtc) {
  11056. struct intel_crtc_state *pipe_config;
  11057. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11058. if (IS_ERR(pipe_config))
  11059. return PTR_ERR(pipe_config);
  11060. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11061. if (!pipe_config->base.active ||
  11062. needs_modeset(&pipe_config->base))
  11063. continue;
  11064. /* 2 or more enabled crtcs means no need for w/a */
  11065. if (enabled_pipe != INVALID_PIPE)
  11066. return 0;
  11067. enabled_pipe = intel_crtc->pipe;
  11068. }
  11069. if (enabled_pipe != INVALID_PIPE)
  11070. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11071. else if (other_crtc_state)
  11072. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11073. return 0;
  11074. }
  11075. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11076. {
  11077. struct drm_crtc *crtc;
  11078. struct drm_crtc_state *crtc_state;
  11079. int ret = 0;
  11080. /* add all active pipes to the state */
  11081. for_each_crtc(state->dev, crtc) {
  11082. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11083. if (IS_ERR(crtc_state))
  11084. return PTR_ERR(crtc_state);
  11085. if (!crtc_state->active || needs_modeset(crtc_state))
  11086. continue;
  11087. crtc_state->mode_changed = true;
  11088. ret = drm_atomic_add_affected_connectors(state, crtc);
  11089. if (ret)
  11090. break;
  11091. ret = drm_atomic_add_affected_planes(state, crtc);
  11092. if (ret)
  11093. break;
  11094. }
  11095. return ret;
  11096. }
  11097. static int intel_modeset_checks(struct drm_atomic_state *state)
  11098. {
  11099. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11100. struct drm_i915_private *dev_priv = state->dev->dev_private;
  11101. struct drm_crtc *crtc;
  11102. struct drm_crtc_state *crtc_state;
  11103. int ret = 0, i;
  11104. if (!check_digital_port_conflicts(state)) {
  11105. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11106. return -EINVAL;
  11107. }
  11108. intel_state->modeset = true;
  11109. intel_state->active_crtcs = dev_priv->active_crtcs;
  11110. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11111. if (crtc_state->active)
  11112. intel_state->active_crtcs |= 1 << i;
  11113. else
  11114. intel_state->active_crtcs &= ~(1 << i);
  11115. }
  11116. /*
  11117. * See if the config requires any additional preparation, e.g.
  11118. * to adjust global state with pipes off. We need to do this
  11119. * here so we can get the modeset_pipe updated config for the new
  11120. * mode set on this crtc. For other crtcs we need to use the
  11121. * adjusted_mode bits in the crtc directly.
  11122. */
  11123. if (dev_priv->display.modeset_calc_cdclk) {
  11124. ret = dev_priv->display.modeset_calc_cdclk(state);
  11125. if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
  11126. ret = intel_modeset_all_pipes(state);
  11127. if (ret < 0)
  11128. return ret;
  11129. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11130. intel_state->cdclk, intel_state->dev_cdclk);
  11131. } else
  11132. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11133. intel_modeset_clear_plls(state);
  11134. if (IS_HASWELL(dev_priv))
  11135. return haswell_mode_set_planes_workaround(state);
  11136. return 0;
  11137. }
  11138. /*
  11139. * Handle calculation of various watermark data at the end of the atomic check
  11140. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11141. * handlers to ensure that all derived state has been updated.
  11142. */
  11143. static void calc_watermark_data(struct drm_atomic_state *state)
  11144. {
  11145. struct drm_device *dev = state->dev;
  11146. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11147. struct drm_crtc *crtc;
  11148. struct drm_crtc_state *cstate;
  11149. struct drm_plane *plane;
  11150. struct drm_plane_state *pstate;
  11151. /*
  11152. * Calculate watermark configuration details now that derived
  11153. * plane/crtc state is all properly updated.
  11154. */
  11155. drm_for_each_crtc(crtc, dev) {
  11156. cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
  11157. crtc->state;
  11158. if (cstate->active)
  11159. intel_state->wm_config.num_pipes_active++;
  11160. }
  11161. drm_for_each_legacy_plane(plane, dev) {
  11162. pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
  11163. plane->state;
  11164. if (!to_intel_plane_state(pstate)->visible)
  11165. continue;
  11166. intel_state->wm_config.sprites_enabled = true;
  11167. if (pstate->crtc_w != pstate->src_w >> 16 ||
  11168. pstate->crtc_h != pstate->src_h >> 16)
  11169. intel_state->wm_config.sprites_scaled = true;
  11170. }
  11171. }
  11172. /**
  11173. * intel_atomic_check - validate state object
  11174. * @dev: drm device
  11175. * @state: state to validate
  11176. */
  11177. static int intel_atomic_check(struct drm_device *dev,
  11178. struct drm_atomic_state *state)
  11179. {
  11180. struct drm_i915_private *dev_priv = to_i915(dev);
  11181. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11182. struct drm_crtc *crtc;
  11183. struct drm_crtc_state *crtc_state;
  11184. int ret, i;
  11185. bool any_ms = false;
  11186. ret = drm_atomic_helper_check_modeset(dev, state);
  11187. if (ret)
  11188. return ret;
  11189. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11190. struct intel_crtc_state *pipe_config =
  11191. to_intel_crtc_state(crtc_state);
  11192. /* Catch I915_MODE_FLAG_INHERITED */
  11193. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11194. crtc_state->mode_changed = true;
  11195. if (!crtc_state->enable) {
  11196. if (needs_modeset(crtc_state))
  11197. any_ms = true;
  11198. continue;
  11199. }
  11200. if (!needs_modeset(crtc_state))
  11201. continue;
  11202. /* FIXME: For only active_changed we shouldn't need to do any
  11203. * state recomputation at all. */
  11204. ret = drm_atomic_add_affected_connectors(state, crtc);
  11205. if (ret)
  11206. return ret;
  11207. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11208. if (ret)
  11209. return ret;
  11210. if (i915.fastboot &&
  11211. intel_pipe_config_compare(dev,
  11212. to_intel_crtc_state(crtc->state),
  11213. pipe_config, true)) {
  11214. crtc_state->mode_changed = false;
  11215. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11216. }
  11217. if (needs_modeset(crtc_state)) {
  11218. any_ms = true;
  11219. ret = drm_atomic_add_affected_planes(state, crtc);
  11220. if (ret)
  11221. return ret;
  11222. }
  11223. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11224. needs_modeset(crtc_state) ?
  11225. "[modeset]" : "[fastset]");
  11226. }
  11227. if (any_ms) {
  11228. ret = intel_modeset_checks(state);
  11229. if (ret)
  11230. return ret;
  11231. } else
  11232. intel_state->cdclk = dev_priv->cdclk_freq;
  11233. ret = drm_atomic_helper_check_planes(dev, state);
  11234. if (ret)
  11235. return ret;
  11236. intel_fbc_choose_crtc(dev_priv, state);
  11237. calc_watermark_data(state);
  11238. return 0;
  11239. }
  11240. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11241. struct drm_atomic_state *state,
  11242. bool async)
  11243. {
  11244. struct drm_i915_private *dev_priv = dev->dev_private;
  11245. struct drm_plane_state *plane_state;
  11246. struct drm_crtc_state *crtc_state;
  11247. struct drm_plane *plane;
  11248. struct drm_crtc *crtc;
  11249. int i, ret;
  11250. if (async) {
  11251. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  11252. return -EINVAL;
  11253. }
  11254. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11255. if (state->legacy_cursor_update)
  11256. continue;
  11257. ret = intel_crtc_wait_for_pending_flips(crtc);
  11258. if (ret)
  11259. return ret;
  11260. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11261. flush_workqueue(dev_priv->wq);
  11262. }
  11263. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11264. if (ret)
  11265. return ret;
  11266. ret = drm_atomic_helper_prepare_planes(dev, state);
  11267. mutex_unlock(&dev->struct_mutex);
  11268. if (!ret && !async) {
  11269. for_each_plane_in_state(state, plane, plane_state, i) {
  11270. struct intel_plane_state *intel_plane_state =
  11271. to_intel_plane_state(plane_state);
  11272. if (!intel_plane_state->wait_req)
  11273. continue;
  11274. ret = __i915_wait_request(intel_plane_state->wait_req,
  11275. true, NULL, NULL);
  11276. if (ret) {
  11277. /* Any hang should be swallowed by the wait */
  11278. WARN_ON(ret == -EIO);
  11279. mutex_lock(&dev->struct_mutex);
  11280. drm_atomic_helper_cleanup_planes(dev, state);
  11281. mutex_unlock(&dev->struct_mutex);
  11282. break;
  11283. }
  11284. }
  11285. }
  11286. return ret;
  11287. }
  11288. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11289. struct drm_i915_private *dev_priv,
  11290. unsigned crtc_mask)
  11291. {
  11292. unsigned last_vblank_count[I915_MAX_PIPES];
  11293. enum pipe pipe;
  11294. int ret;
  11295. if (!crtc_mask)
  11296. return;
  11297. for_each_pipe(dev_priv, pipe) {
  11298. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11299. if (!((1 << pipe) & crtc_mask))
  11300. continue;
  11301. ret = drm_crtc_vblank_get(crtc);
  11302. if (WARN_ON(ret != 0)) {
  11303. crtc_mask &= ~(1 << pipe);
  11304. continue;
  11305. }
  11306. last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
  11307. }
  11308. for_each_pipe(dev_priv, pipe) {
  11309. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11310. long lret;
  11311. if (!((1 << pipe) & crtc_mask))
  11312. continue;
  11313. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11314. last_vblank_count[pipe] !=
  11315. drm_crtc_vblank_count(crtc),
  11316. msecs_to_jiffies(50));
  11317. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11318. drm_crtc_vblank_put(crtc);
  11319. }
  11320. }
  11321. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11322. {
  11323. /* fb updated, need to unpin old fb */
  11324. if (crtc_state->fb_changed)
  11325. return true;
  11326. /* wm changes, need vblank before final wm's */
  11327. if (crtc_state->update_wm_post)
  11328. return true;
  11329. /*
  11330. * cxsr is re-enabled after vblank.
  11331. * This is already handled by crtc_state->update_wm_post,
  11332. * but added for clarity.
  11333. */
  11334. if (crtc_state->disable_cxsr)
  11335. return true;
  11336. return false;
  11337. }
  11338. /**
  11339. * intel_atomic_commit - commit validated state object
  11340. * @dev: DRM device
  11341. * @state: the top-level driver state object
  11342. * @async: asynchronous commit
  11343. *
  11344. * This function commits a top-level state object that has been validated
  11345. * with drm_atomic_helper_check().
  11346. *
  11347. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11348. * we can only handle plane-related operations and do not yet support
  11349. * asynchronous commit.
  11350. *
  11351. * RETURNS
  11352. * Zero for success or -errno.
  11353. */
  11354. static int intel_atomic_commit(struct drm_device *dev,
  11355. struct drm_atomic_state *state,
  11356. bool async)
  11357. {
  11358. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11359. struct drm_i915_private *dev_priv = dev->dev_private;
  11360. struct drm_crtc_state *old_crtc_state;
  11361. struct drm_crtc *crtc;
  11362. struct intel_crtc_state *intel_cstate;
  11363. int ret = 0, i;
  11364. bool hw_check = intel_state->modeset;
  11365. unsigned long put_domains[I915_MAX_PIPES] = {};
  11366. unsigned crtc_vblank_mask = 0;
  11367. ret = intel_atomic_prepare_commit(dev, state, async);
  11368. if (ret) {
  11369. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11370. return ret;
  11371. }
  11372. drm_atomic_helper_swap_state(dev, state);
  11373. dev_priv->wm.config = intel_state->wm_config;
  11374. intel_shared_dpll_commit(state);
  11375. if (intel_state->modeset) {
  11376. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  11377. sizeof(intel_state->min_pixclk));
  11378. dev_priv->active_crtcs = intel_state->active_crtcs;
  11379. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  11380. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  11381. }
  11382. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11383. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11384. if (needs_modeset(crtc->state) ||
  11385. to_intel_crtc_state(crtc->state)->update_pipe) {
  11386. hw_check = true;
  11387. put_domains[to_intel_crtc(crtc)->pipe] =
  11388. modeset_get_crtc_power_domains(crtc,
  11389. to_intel_crtc_state(crtc->state));
  11390. }
  11391. if (!needs_modeset(crtc->state))
  11392. continue;
  11393. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11394. if (old_crtc_state->active) {
  11395. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  11396. dev_priv->display.crtc_disable(crtc);
  11397. intel_crtc->active = false;
  11398. intel_fbc_disable(intel_crtc);
  11399. intel_disable_shared_dpll(intel_crtc);
  11400. /*
  11401. * Underruns don't always raise
  11402. * interrupts, so check manually.
  11403. */
  11404. intel_check_cpu_fifo_underruns(dev_priv);
  11405. intel_check_pch_fifo_underruns(dev_priv);
  11406. if (!crtc->state->active)
  11407. intel_update_watermarks(crtc);
  11408. }
  11409. }
  11410. /* Only after disabling all output pipelines that will be changed can we
  11411. * update the the output configuration. */
  11412. intel_modeset_update_crtc_state(state);
  11413. if (intel_state->modeset) {
  11414. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11415. if (dev_priv->display.modeset_commit_cdclk &&
  11416. intel_state->dev_cdclk != dev_priv->cdclk_freq)
  11417. dev_priv->display.modeset_commit_cdclk(state);
  11418. intel_modeset_verify_disabled(dev);
  11419. }
  11420. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11421. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11422. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11423. bool modeset = needs_modeset(crtc->state);
  11424. struct intel_crtc_state *pipe_config =
  11425. to_intel_crtc_state(crtc->state);
  11426. bool update_pipe = !modeset && pipe_config->update_pipe;
  11427. if (modeset && crtc->state->active) {
  11428. update_scanline_offset(to_intel_crtc(crtc));
  11429. dev_priv->display.crtc_enable(crtc);
  11430. }
  11431. if (!modeset)
  11432. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11433. if (crtc->state->active &&
  11434. drm_atomic_get_existing_plane_state(state, crtc->primary))
  11435. intel_fbc_enable(intel_crtc);
  11436. if (crtc->state->active &&
  11437. (crtc->state->planes_changed || update_pipe))
  11438. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11439. if (pipe_config->base.active && needs_vblank_wait(pipe_config))
  11440. crtc_vblank_mask |= 1 << i;
  11441. }
  11442. /* FIXME: add subpixel order */
  11443. if (!state->legacy_cursor_update)
  11444. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  11445. /*
  11446. * Now that the vblank has passed, we can go ahead and program the
  11447. * optimal watermarks on platforms that need two-step watermark
  11448. * programming.
  11449. *
  11450. * TODO: Move this (and other cleanup) to an async worker eventually.
  11451. */
  11452. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11453. intel_cstate = to_intel_crtc_state(crtc->state);
  11454. if (dev_priv->display.optimize_watermarks)
  11455. dev_priv->display.optimize_watermarks(intel_cstate);
  11456. }
  11457. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11458. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  11459. if (put_domains[i])
  11460. modeset_put_power_domains(dev_priv, put_domains[i]);
  11461. intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
  11462. }
  11463. if (intel_state->modeset)
  11464. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  11465. mutex_lock(&dev->struct_mutex);
  11466. drm_atomic_helper_cleanup_planes(dev, state);
  11467. mutex_unlock(&dev->struct_mutex);
  11468. drm_atomic_state_free(state);
  11469. /* As one of the primary mmio accessors, KMS has a high likelihood
  11470. * of triggering bugs in unclaimed access. After we finish
  11471. * modesetting, see if an error has been flagged, and if so
  11472. * enable debugging for the next modeset - and hope we catch
  11473. * the culprit.
  11474. *
  11475. * XXX note that we assume display power is on at this point.
  11476. * This might hold true now but we need to add pm helper to check
  11477. * unclaimed only when the hardware is on, as atomic commits
  11478. * can happen also when the device is completely off.
  11479. */
  11480. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  11481. return 0;
  11482. }
  11483. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11484. {
  11485. struct drm_device *dev = crtc->dev;
  11486. struct drm_atomic_state *state;
  11487. struct drm_crtc_state *crtc_state;
  11488. int ret;
  11489. state = drm_atomic_state_alloc(dev);
  11490. if (!state) {
  11491. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11492. crtc->base.id);
  11493. return;
  11494. }
  11495. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11496. retry:
  11497. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11498. ret = PTR_ERR_OR_ZERO(crtc_state);
  11499. if (!ret) {
  11500. if (!crtc_state->active)
  11501. goto out;
  11502. crtc_state->mode_changed = true;
  11503. ret = drm_atomic_commit(state);
  11504. }
  11505. if (ret == -EDEADLK) {
  11506. drm_atomic_state_clear(state);
  11507. drm_modeset_backoff(state->acquire_ctx);
  11508. goto retry;
  11509. }
  11510. if (ret)
  11511. out:
  11512. drm_atomic_state_free(state);
  11513. }
  11514. #undef for_each_intel_crtc_masked
  11515. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11516. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  11517. .set_config = drm_atomic_helper_set_config,
  11518. .set_property = drm_atomic_helper_crtc_set_property,
  11519. .destroy = intel_crtc_destroy,
  11520. .page_flip = intel_crtc_page_flip,
  11521. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11522. .atomic_destroy_state = intel_crtc_destroy_state,
  11523. };
  11524. /**
  11525. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11526. * @plane: drm plane to prepare for
  11527. * @fb: framebuffer to prepare for presentation
  11528. *
  11529. * Prepares a framebuffer for usage on a display plane. Generally this
  11530. * involves pinning the underlying object and updating the frontbuffer tracking
  11531. * bits. Some older platforms need special physical address handling for
  11532. * cursor planes.
  11533. *
  11534. * Must be called with struct_mutex held.
  11535. *
  11536. * Returns 0 on success, negative error code on failure.
  11537. */
  11538. int
  11539. intel_prepare_plane_fb(struct drm_plane *plane,
  11540. const struct drm_plane_state *new_state)
  11541. {
  11542. struct drm_device *dev = plane->dev;
  11543. struct drm_framebuffer *fb = new_state->fb;
  11544. struct intel_plane *intel_plane = to_intel_plane(plane);
  11545. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11546. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11547. int ret = 0;
  11548. if (!obj && !old_obj)
  11549. return 0;
  11550. if (old_obj) {
  11551. struct drm_crtc_state *crtc_state =
  11552. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11553. /* Big Hammer, we also need to ensure that any pending
  11554. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11555. * current scanout is retired before unpinning the old
  11556. * framebuffer. Note that we rely on userspace rendering
  11557. * into the buffer attached to the pipe they are waiting
  11558. * on. If not, userspace generates a GPU hang with IPEHR
  11559. * point to the MI_WAIT_FOR_EVENT.
  11560. *
  11561. * This should only fail upon a hung GPU, in which case we
  11562. * can safely continue.
  11563. */
  11564. if (needs_modeset(crtc_state))
  11565. ret = i915_gem_object_wait_rendering(old_obj, true);
  11566. if (ret) {
  11567. /* GPU hangs should have been swallowed by the wait */
  11568. WARN_ON(ret == -EIO);
  11569. return ret;
  11570. }
  11571. }
  11572. /* For framebuffer backed by dmabuf, wait for fence */
  11573. if (obj && obj->base.dma_buf) {
  11574. long lret;
  11575. lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  11576. false, true,
  11577. MAX_SCHEDULE_TIMEOUT);
  11578. if (lret == -ERESTARTSYS)
  11579. return lret;
  11580. WARN(lret < 0, "waiting returns %li\n", lret);
  11581. }
  11582. if (!obj) {
  11583. ret = 0;
  11584. } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11585. INTEL_INFO(dev)->cursor_needs_physical) {
  11586. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11587. ret = i915_gem_object_attach_phys(obj, align);
  11588. if (ret)
  11589. DRM_DEBUG_KMS("failed to attach phys object\n");
  11590. } else {
  11591. ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  11592. }
  11593. if (ret == 0) {
  11594. if (obj) {
  11595. struct intel_plane_state *plane_state =
  11596. to_intel_plane_state(new_state);
  11597. i915_gem_request_assign(&plane_state->wait_req,
  11598. obj->last_write_req);
  11599. }
  11600. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11601. }
  11602. return ret;
  11603. }
  11604. /**
  11605. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11606. * @plane: drm plane to clean up for
  11607. * @fb: old framebuffer that was on plane
  11608. *
  11609. * Cleans up a framebuffer that has just been removed from a plane.
  11610. *
  11611. * Must be called with struct_mutex held.
  11612. */
  11613. void
  11614. intel_cleanup_plane_fb(struct drm_plane *plane,
  11615. const struct drm_plane_state *old_state)
  11616. {
  11617. struct drm_device *dev = plane->dev;
  11618. struct intel_plane *intel_plane = to_intel_plane(plane);
  11619. struct intel_plane_state *old_intel_state;
  11620. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11621. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11622. old_intel_state = to_intel_plane_state(old_state);
  11623. if (!obj && !old_obj)
  11624. return;
  11625. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11626. !INTEL_INFO(dev)->cursor_needs_physical))
  11627. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  11628. /* prepare_fb aborted? */
  11629. if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
  11630. (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
  11631. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11632. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11633. }
  11634. int
  11635. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11636. {
  11637. int max_scale;
  11638. struct drm_device *dev;
  11639. struct drm_i915_private *dev_priv;
  11640. int crtc_clock, cdclk;
  11641. if (!intel_crtc || !crtc_state->base.enable)
  11642. return DRM_PLANE_HELPER_NO_SCALING;
  11643. dev = intel_crtc->base.dev;
  11644. dev_priv = dev->dev_private;
  11645. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11646. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11647. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11648. return DRM_PLANE_HELPER_NO_SCALING;
  11649. /*
  11650. * skl max scale is lower of:
  11651. * close to 3 but not 3, -1 is for that purpose
  11652. * or
  11653. * cdclk/crtc_clock
  11654. */
  11655. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11656. return max_scale;
  11657. }
  11658. static int
  11659. intel_check_primary_plane(struct drm_plane *plane,
  11660. struct intel_crtc_state *crtc_state,
  11661. struct intel_plane_state *state)
  11662. {
  11663. struct drm_crtc *crtc = state->base.crtc;
  11664. struct drm_framebuffer *fb = state->base.fb;
  11665. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11666. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11667. bool can_position = false;
  11668. if (INTEL_INFO(plane->dev)->gen >= 9) {
  11669. /* use scaler when colorkey is not required */
  11670. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11671. min_scale = 1;
  11672. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11673. }
  11674. can_position = true;
  11675. }
  11676. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11677. &state->dst, &state->clip,
  11678. min_scale, max_scale,
  11679. can_position, true,
  11680. &state->visible);
  11681. }
  11682. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11683. struct drm_crtc_state *old_crtc_state)
  11684. {
  11685. struct drm_device *dev = crtc->dev;
  11686. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11687. struct intel_crtc_state *old_intel_state =
  11688. to_intel_crtc_state(old_crtc_state);
  11689. bool modeset = needs_modeset(crtc->state);
  11690. /* Perform vblank evasion around commit operation */
  11691. intel_pipe_update_start(intel_crtc);
  11692. if (modeset)
  11693. return;
  11694. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  11695. intel_color_set_csc(crtc->state);
  11696. intel_color_load_luts(crtc->state);
  11697. }
  11698. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11699. intel_update_pipe_config(intel_crtc, old_intel_state);
  11700. else if (INTEL_INFO(dev)->gen >= 9)
  11701. skl_detach_scalers(intel_crtc);
  11702. }
  11703. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11704. struct drm_crtc_state *old_crtc_state)
  11705. {
  11706. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11707. intel_pipe_update_end(intel_crtc);
  11708. }
  11709. /**
  11710. * intel_plane_destroy - destroy a plane
  11711. * @plane: plane to destroy
  11712. *
  11713. * Common destruction function for all types of planes (primary, cursor,
  11714. * sprite).
  11715. */
  11716. void intel_plane_destroy(struct drm_plane *plane)
  11717. {
  11718. struct intel_plane *intel_plane = to_intel_plane(plane);
  11719. drm_plane_cleanup(plane);
  11720. kfree(intel_plane);
  11721. }
  11722. const struct drm_plane_funcs intel_plane_funcs = {
  11723. .update_plane = drm_atomic_helper_update_plane,
  11724. .disable_plane = drm_atomic_helper_disable_plane,
  11725. .destroy = intel_plane_destroy,
  11726. .set_property = drm_atomic_helper_plane_set_property,
  11727. .atomic_get_property = intel_plane_atomic_get_property,
  11728. .atomic_set_property = intel_plane_atomic_set_property,
  11729. .atomic_duplicate_state = intel_plane_duplicate_state,
  11730. .atomic_destroy_state = intel_plane_destroy_state,
  11731. };
  11732. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11733. int pipe)
  11734. {
  11735. struct intel_plane *primary = NULL;
  11736. struct intel_plane_state *state = NULL;
  11737. const uint32_t *intel_primary_formats;
  11738. unsigned int num_formats;
  11739. int ret;
  11740. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11741. if (!primary)
  11742. goto fail;
  11743. state = intel_create_plane_state(&primary->base);
  11744. if (!state)
  11745. goto fail;
  11746. primary->base.state = &state->base;
  11747. primary->can_scale = false;
  11748. primary->max_downscale = 1;
  11749. if (INTEL_INFO(dev)->gen >= 9) {
  11750. primary->can_scale = true;
  11751. state->scaler_id = -1;
  11752. }
  11753. primary->pipe = pipe;
  11754. primary->plane = pipe;
  11755. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11756. primary->check_plane = intel_check_primary_plane;
  11757. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11758. primary->plane = !pipe;
  11759. if (INTEL_INFO(dev)->gen >= 9) {
  11760. intel_primary_formats = skl_primary_formats;
  11761. num_formats = ARRAY_SIZE(skl_primary_formats);
  11762. primary->update_plane = skylake_update_primary_plane;
  11763. primary->disable_plane = skylake_disable_primary_plane;
  11764. } else if (HAS_PCH_SPLIT(dev)) {
  11765. intel_primary_formats = i965_primary_formats;
  11766. num_formats = ARRAY_SIZE(i965_primary_formats);
  11767. primary->update_plane = ironlake_update_primary_plane;
  11768. primary->disable_plane = i9xx_disable_primary_plane;
  11769. } else if (INTEL_INFO(dev)->gen >= 4) {
  11770. intel_primary_formats = i965_primary_formats;
  11771. num_formats = ARRAY_SIZE(i965_primary_formats);
  11772. primary->update_plane = i9xx_update_primary_plane;
  11773. primary->disable_plane = i9xx_disable_primary_plane;
  11774. } else {
  11775. intel_primary_formats = i8xx_primary_formats;
  11776. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11777. primary->update_plane = i9xx_update_primary_plane;
  11778. primary->disable_plane = i9xx_disable_primary_plane;
  11779. }
  11780. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11781. &intel_plane_funcs,
  11782. intel_primary_formats, num_formats,
  11783. DRM_PLANE_TYPE_PRIMARY, NULL);
  11784. if (ret)
  11785. goto fail;
  11786. if (INTEL_INFO(dev)->gen >= 4)
  11787. intel_create_rotation_property(dev, primary);
  11788. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11789. return &primary->base;
  11790. fail:
  11791. kfree(state);
  11792. kfree(primary);
  11793. return NULL;
  11794. }
  11795. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11796. {
  11797. if (!dev->mode_config.rotation_property) {
  11798. unsigned long flags = BIT(DRM_ROTATE_0) |
  11799. BIT(DRM_ROTATE_180);
  11800. if (INTEL_INFO(dev)->gen >= 9)
  11801. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11802. dev->mode_config.rotation_property =
  11803. drm_mode_create_rotation_property(dev, flags);
  11804. }
  11805. if (dev->mode_config.rotation_property)
  11806. drm_object_attach_property(&plane->base.base,
  11807. dev->mode_config.rotation_property,
  11808. plane->base.state->rotation);
  11809. }
  11810. static int
  11811. intel_check_cursor_plane(struct drm_plane *plane,
  11812. struct intel_crtc_state *crtc_state,
  11813. struct intel_plane_state *state)
  11814. {
  11815. struct drm_crtc *crtc = crtc_state->base.crtc;
  11816. struct drm_framebuffer *fb = state->base.fb;
  11817. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11818. enum pipe pipe = to_intel_plane(plane)->pipe;
  11819. unsigned stride;
  11820. int ret;
  11821. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11822. &state->dst, &state->clip,
  11823. DRM_PLANE_HELPER_NO_SCALING,
  11824. DRM_PLANE_HELPER_NO_SCALING,
  11825. true, true, &state->visible);
  11826. if (ret)
  11827. return ret;
  11828. /* if we want to turn off the cursor ignore width and height */
  11829. if (!obj)
  11830. return 0;
  11831. /* Check for which cursor types we support */
  11832. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11833. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11834. state->base.crtc_w, state->base.crtc_h);
  11835. return -EINVAL;
  11836. }
  11837. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11838. if (obj->base.size < stride * state->base.crtc_h) {
  11839. DRM_DEBUG_KMS("buffer is too small\n");
  11840. return -ENOMEM;
  11841. }
  11842. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11843. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11844. return -EINVAL;
  11845. }
  11846. /*
  11847. * There's something wrong with the cursor on CHV pipe C.
  11848. * If it straddles the left edge of the screen then
  11849. * moving it away from the edge or disabling it often
  11850. * results in a pipe underrun, and often that can lead to
  11851. * dead pipe (constant underrun reported, and it scans
  11852. * out just a solid color). To recover from that, the
  11853. * display power well must be turned off and on again.
  11854. * Refuse the put the cursor into that compromised position.
  11855. */
  11856. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  11857. state->visible && state->base.crtc_x < 0) {
  11858. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  11859. return -EINVAL;
  11860. }
  11861. return 0;
  11862. }
  11863. static void
  11864. intel_disable_cursor_plane(struct drm_plane *plane,
  11865. struct drm_crtc *crtc)
  11866. {
  11867. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11868. intel_crtc->cursor_addr = 0;
  11869. intel_crtc_update_cursor(crtc, NULL);
  11870. }
  11871. static void
  11872. intel_update_cursor_plane(struct drm_plane *plane,
  11873. const struct intel_crtc_state *crtc_state,
  11874. const struct intel_plane_state *state)
  11875. {
  11876. struct drm_crtc *crtc = crtc_state->base.crtc;
  11877. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11878. struct drm_device *dev = plane->dev;
  11879. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11880. uint32_t addr;
  11881. if (!obj)
  11882. addr = 0;
  11883. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11884. addr = i915_gem_obj_ggtt_offset(obj);
  11885. else
  11886. addr = obj->phys_handle->busaddr;
  11887. intel_crtc->cursor_addr = addr;
  11888. intel_crtc_update_cursor(crtc, state);
  11889. }
  11890. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11891. int pipe)
  11892. {
  11893. struct intel_plane *cursor = NULL;
  11894. struct intel_plane_state *state = NULL;
  11895. int ret;
  11896. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11897. if (!cursor)
  11898. goto fail;
  11899. state = intel_create_plane_state(&cursor->base);
  11900. if (!state)
  11901. goto fail;
  11902. cursor->base.state = &state->base;
  11903. cursor->can_scale = false;
  11904. cursor->max_downscale = 1;
  11905. cursor->pipe = pipe;
  11906. cursor->plane = pipe;
  11907. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11908. cursor->check_plane = intel_check_cursor_plane;
  11909. cursor->update_plane = intel_update_cursor_plane;
  11910. cursor->disable_plane = intel_disable_cursor_plane;
  11911. ret = drm_universal_plane_init(dev, &cursor->base, 0,
  11912. &intel_plane_funcs,
  11913. intel_cursor_formats,
  11914. ARRAY_SIZE(intel_cursor_formats),
  11915. DRM_PLANE_TYPE_CURSOR, NULL);
  11916. if (ret)
  11917. goto fail;
  11918. if (INTEL_INFO(dev)->gen >= 4) {
  11919. if (!dev->mode_config.rotation_property)
  11920. dev->mode_config.rotation_property =
  11921. drm_mode_create_rotation_property(dev,
  11922. BIT(DRM_ROTATE_0) |
  11923. BIT(DRM_ROTATE_180));
  11924. if (dev->mode_config.rotation_property)
  11925. drm_object_attach_property(&cursor->base.base,
  11926. dev->mode_config.rotation_property,
  11927. state->base.rotation);
  11928. }
  11929. if (INTEL_INFO(dev)->gen >=9)
  11930. state->scaler_id = -1;
  11931. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11932. return &cursor->base;
  11933. fail:
  11934. kfree(state);
  11935. kfree(cursor);
  11936. return NULL;
  11937. }
  11938. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11939. struct intel_crtc_state *crtc_state)
  11940. {
  11941. int i;
  11942. struct intel_scaler *intel_scaler;
  11943. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11944. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11945. intel_scaler = &scaler_state->scalers[i];
  11946. intel_scaler->in_use = 0;
  11947. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11948. }
  11949. scaler_state->scaler_id = -1;
  11950. }
  11951. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11952. {
  11953. struct drm_i915_private *dev_priv = dev->dev_private;
  11954. struct intel_crtc *intel_crtc;
  11955. struct intel_crtc_state *crtc_state = NULL;
  11956. struct drm_plane *primary = NULL;
  11957. struct drm_plane *cursor = NULL;
  11958. int ret;
  11959. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11960. if (intel_crtc == NULL)
  11961. return;
  11962. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11963. if (!crtc_state)
  11964. goto fail;
  11965. intel_crtc->config = crtc_state;
  11966. intel_crtc->base.state = &crtc_state->base;
  11967. crtc_state->base.crtc = &intel_crtc->base;
  11968. /* initialize shared scalers */
  11969. if (INTEL_INFO(dev)->gen >= 9) {
  11970. if (pipe == PIPE_C)
  11971. intel_crtc->num_scalers = 1;
  11972. else
  11973. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11974. skl_init_scalers(dev, intel_crtc, crtc_state);
  11975. }
  11976. primary = intel_primary_plane_create(dev, pipe);
  11977. if (!primary)
  11978. goto fail;
  11979. cursor = intel_cursor_plane_create(dev, pipe);
  11980. if (!cursor)
  11981. goto fail;
  11982. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11983. cursor, &intel_crtc_funcs, NULL);
  11984. if (ret)
  11985. goto fail;
  11986. /*
  11987. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11988. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11989. */
  11990. intel_crtc->pipe = pipe;
  11991. intel_crtc->plane = pipe;
  11992. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11993. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11994. intel_crtc->plane = !pipe;
  11995. }
  11996. intel_crtc->cursor_base = ~0;
  11997. intel_crtc->cursor_cntl = ~0;
  11998. intel_crtc->cursor_size = ~0;
  11999. intel_crtc->wm.cxsr_allowed = true;
  12000. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12001. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12002. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  12003. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  12004. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12005. intel_color_init(&intel_crtc->base);
  12006. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12007. return;
  12008. fail:
  12009. if (primary)
  12010. drm_plane_cleanup(primary);
  12011. if (cursor)
  12012. drm_plane_cleanup(cursor);
  12013. kfree(crtc_state);
  12014. kfree(intel_crtc);
  12015. }
  12016. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12017. {
  12018. struct drm_encoder *encoder = connector->base.encoder;
  12019. struct drm_device *dev = connector->base.dev;
  12020. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12021. if (!encoder || WARN_ON(!encoder->crtc))
  12022. return INVALID_PIPE;
  12023. return to_intel_crtc(encoder->crtc)->pipe;
  12024. }
  12025. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12026. struct drm_file *file)
  12027. {
  12028. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12029. struct drm_crtc *drmmode_crtc;
  12030. struct intel_crtc *crtc;
  12031. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12032. if (!drmmode_crtc) {
  12033. DRM_ERROR("no such CRTC id\n");
  12034. return -ENOENT;
  12035. }
  12036. crtc = to_intel_crtc(drmmode_crtc);
  12037. pipe_from_crtc_id->pipe = crtc->pipe;
  12038. return 0;
  12039. }
  12040. static int intel_encoder_clones(struct intel_encoder *encoder)
  12041. {
  12042. struct drm_device *dev = encoder->base.dev;
  12043. struct intel_encoder *source_encoder;
  12044. int index_mask = 0;
  12045. int entry = 0;
  12046. for_each_intel_encoder(dev, source_encoder) {
  12047. if (encoders_cloneable(encoder, source_encoder))
  12048. index_mask |= (1 << entry);
  12049. entry++;
  12050. }
  12051. return index_mask;
  12052. }
  12053. static bool has_edp_a(struct drm_device *dev)
  12054. {
  12055. struct drm_i915_private *dev_priv = dev->dev_private;
  12056. if (!IS_MOBILE(dev))
  12057. return false;
  12058. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12059. return false;
  12060. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12061. return false;
  12062. return true;
  12063. }
  12064. static bool intel_crt_present(struct drm_device *dev)
  12065. {
  12066. struct drm_i915_private *dev_priv = dev->dev_private;
  12067. if (INTEL_INFO(dev)->gen >= 9)
  12068. return false;
  12069. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  12070. return false;
  12071. if (IS_CHERRYVIEW(dev))
  12072. return false;
  12073. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12074. return false;
  12075. /* DDI E can't be used if DDI A requires 4 lanes */
  12076. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12077. return false;
  12078. if (!dev_priv->vbt.int_crt_support)
  12079. return false;
  12080. return true;
  12081. }
  12082. static void intel_setup_outputs(struct drm_device *dev)
  12083. {
  12084. struct drm_i915_private *dev_priv = dev->dev_private;
  12085. struct intel_encoder *encoder;
  12086. bool dpd_is_edp = false;
  12087. intel_lvds_init(dev);
  12088. if (intel_crt_present(dev))
  12089. intel_crt_init(dev);
  12090. if (IS_BROXTON(dev)) {
  12091. /*
  12092. * FIXME: Broxton doesn't support port detection via the
  12093. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12094. * detect the ports.
  12095. */
  12096. intel_ddi_init(dev, PORT_A);
  12097. intel_ddi_init(dev, PORT_B);
  12098. intel_ddi_init(dev, PORT_C);
  12099. intel_dsi_init(dev);
  12100. } else if (HAS_DDI(dev)) {
  12101. int found;
  12102. /*
  12103. * Haswell uses DDI functions to detect digital outputs.
  12104. * On SKL pre-D0 the strap isn't connected, so we assume
  12105. * it's there.
  12106. */
  12107. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12108. /* WaIgnoreDDIAStrap: skl */
  12109. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12110. intel_ddi_init(dev, PORT_A);
  12111. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12112. * register */
  12113. found = I915_READ(SFUSE_STRAP);
  12114. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12115. intel_ddi_init(dev, PORT_B);
  12116. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12117. intel_ddi_init(dev, PORT_C);
  12118. if (found & SFUSE_STRAP_DDID_DETECTED)
  12119. intel_ddi_init(dev, PORT_D);
  12120. /*
  12121. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12122. */
  12123. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  12124. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12125. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12126. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12127. intel_ddi_init(dev, PORT_E);
  12128. } else if (HAS_PCH_SPLIT(dev)) {
  12129. int found;
  12130. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12131. if (has_edp_a(dev))
  12132. intel_dp_init(dev, DP_A, PORT_A);
  12133. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12134. /* PCH SDVOB multiplex with HDMIB */
  12135. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12136. if (!found)
  12137. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12138. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12139. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12140. }
  12141. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12142. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12143. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12144. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12145. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12146. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12147. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12148. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12149. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12150. /*
  12151. * The DP_DETECTED bit is the latched state of the DDC
  12152. * SDA pin at boot. However since eDP doesn't require DDC
  12153. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12154. * eDP ports may have been muxed to an alternate function.
  12155. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12156. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12157. * detect eDP ports.
  12158. */
  12159. if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
  12160. !intel_dp_is_edp(dev, PORT_B))
  12161. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  12162. if (I915_READ(VLV_DP_B) & DP_DETECTED ||
  12163. intel_dp_is_edp(dev, PORT_B))
  12164. intel_dp_init(dev, VLV_DP_B, PORT_B);
  12165. if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
  12166. !intel_dp_is_edp(dev, PORT_C))
  12167. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  12168. if (I915_READ(VLV_DP_C) & DP_DETECTED ||
  12169. intel_dp_is_edp(dev, PORT_C))
  12170. intel_dp_init(dev, VLV_DP_C, PORT_C);
  12171. if (IS_CHERRYVIEW(dev)) {
  12172. /* eDP not supported on port D, so don't check VBT */
  12173. if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
  12174. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  12175. if (I915_READ(CHV_DP_D) & DP_DETECTED)
  12176. intel_dp_init(dev, CHV_DP_D, PORT_D);
  12177. }
  12178. intel_dsi_init(dev);
  12179. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12180. bool found = false;
  12181. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12182. DRM_DEBUG_KMS("probing SDVOB\n");
  12183. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  12184. if (!found && IS_G4X(dev)) {
  12185. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12186. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12187. }
  12188. if (!found && IS_G4X(dev))
  12189. intel_dp_init(dev, DP_B, PORT_B);
  12190. }
  12191. /* Before G4X SDVOC doesn't have its own detect register */
  12192. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12193. DRM_DEBUG_KMS("probing SDVOC\n");
  12194. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  12195. }
  12196. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12197. if (IS_G4X(dev)) {
  12198. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12199. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12200. }
  12201. if (IS_G4X(dev))
  12202. intel_dp_init(dev, DP_C, PORT_C);
  12203. }
  12204. if (IS_G4X(dev) &&
  12205. (I915_READ(DP_D) & DP_DETECTED))
  12206. intel_dp_init(dev, DP_D, PORT_D);
  12207. } else if (IS_GEN2(dev))
  12208. intel_dvo_init(dev);
  12209. if (SUPPORTS_TV(dev))
  12210. intel_tv_init(dev);
  12211. intel_psr_init(dev);
  12212. for_each_intel_encoder(dev, encoder) {
  12213. encoder->base.possible_crtcs = encoder->crtc_mask;
  12214. encoder->base.possible_clones =
  12215. intel_encoder_clones(encoder);
  12216. }
  12217. intel_init_pch_refclk(dev);
  12218. drm_helper_move_panel_connectors_to_head(dev);
  12219. }
  12220. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12221. {
  12222. struct drm_device *dev = fb->dev;
  12223. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12224. drm_framebuffer_cleanup(fb);
  12225. mutex_lock(&dev->struct_mutex);
  12226. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12227. drm_gem_object_unreference(&intel_fb->obj->base);
  12228. mutex_unlock(&dev->struct_mutex);
  12229. kfree(intel_fb);
  12230. }
  12231. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12232. struct drm_file *file,
  12233. unsigned int *handle)
  12234. {
  12235. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12236. struct drm_i915_gem_object *obj = intel_fb->obj;
  12237. if (obj->userptr.mm) {
  12238. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  12239. return -EINVAL;
  12240. }
  12241. return drm_gem_handle_create(file, &obj->base, handle);
  12242. }
  12243. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12244. struct drm_file *file,
  12245. unsigned flags, unsigned color,
  12246. struct drm_clip_rect *clips,
  12247. unsigned num_clips)
  12248. {
  12249. struct drm_device *dev = fb->dev;
  12250. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12251. struct drm_i915_gem_object *obj = intel_fb->obj;
  12252. mutex_lock(&dev->struct_mutex);
  12253. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  12254. mutex_unlock(&dev->struct_mutex);
  12255. return 0;
  12256. }
  12257. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12258. .destroy = intel_user_framebuffer_destroy,
  12259. .create_handle = intel_user_framebuffer_create_handle,
  12260. .dirty = intel_user_framebuffer_dirty,
  12261. };
  12262. static
  12263. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12264. uint32_t pixel_format)
  12265. {
  12266. u32 gen = INTEL_INFO(dev)->gen;
  12267. if (gen >= 9) {
  12268. int cpp = drm_format_plane_cpp(pixel_format, 0);
  12269. /* "The stride in bytes must not exceed the of the size of 8K
  12270. * pixels and 32K bytes."
  12271. */
  12272. return min(8192 * cpp, 32768);
  12273. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12274. return 32*1024;
  12275. } else if (gen >= 4) {
  12276. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12277. return 16*1024;
  12278. else
  12279. return 32*1024;
  12280. } else if (gen >= 3) {
  12281. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12282. return 8*1024;
  12283. else
  12284. return 16*1024;
  12285. } else {
  12286. /* XXX DSPC is limited to 4k tiled */
  12287. return 8*1024;
  12288. }
  12289. }
  12290. static int intel_framebuffer_init(struct drm_device *dev,
  12291. struct intel_framebuffer *intel_fb,
  12292. struct drm_mode_fb_cmd2 *mode_cmd,
  12293. struct drm_i915_gem_object *obj)
  12294. {
  12295. struct drm_i915_private *dev_priv = to_i915(dev);
  12296. unsigned int aligned_height;
  12297. int ret;
  12298. u32 pitch_limit, stride_alignment;
  12299. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12300. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12301. /* Enforce that fb modifier and tiling mode match, but only for
  12302. * X-tiled. This is needed for FBC. */
  12303. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12304. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12305. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12306. return -EINVAL;
  12307. }
  12308. } else {
  12309. if (obj->tiling_mode == I915_TILING_X)
  12310. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12311. else if (obj->tiling_mode == I915_TILING_Y) {
  12312. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12313. return -EINVAL;
  12314. }
  12315. }
  12316. /* Passed in modifier sanity checking. */
  12317. switch (mode_cmd->modifier[0]) {
  12318. case I915_FORMAT_MOD_Y_TILED:
  12319. case I915_FORMAT_MOD_Yf_TILED:
  12320. if (INTEL_INFO(dev)->gen < 9) {
  12321. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12322. mode_cmd->modifier[0]);
  12323. return -EINVAL;
  12324. }
  12325. case DRM_FORMAT_MOD_NONE:
  12326. case I915_FORMAT_MOD_X_TILED:
  12327. break;
  12328. default:
  12329. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12330. mode_cmd->modifier[0]);
  12331. return -EINVAL;
  12332. }
  12333. stride_alignment = intel_fb_stride_alignment(dev_priv,
  12334. mode_cmd->modifier[0],
  12335. mode_cmd->pixel_format);
  12336. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12337. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12338. mode_cmd->pitches[0], stride_alignment);
  12339. return -EINVAL;
  12340. }
  12341. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12342. mode_cmd->pixel_format);
  12343. if (mode_cmd->pitches[0] > pitch_limit) {
  12344. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12345. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12346. "tiled" : "linear",
  12347. mode_cmd->pitches[0], pitch_limit);
  12348. return -EINVAL;
  12349. }
  12350. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12351. mode_cmd->pitches[0] != obj->stride) {
  12352. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12353. mode_cmd->pitches[0], obj->stride);
  12354. return -EINVAL;
  12355. }
  12356. /* Reject formats not supported by any plane early. */
  12357. switch (mode_cmd->pixel_format) {
  12358. case DRM_FORMAT_C8:
  12359. case DRM_FORMAT_RGB565:
  12360. case DRM_FORMAT_XRGB8888:
  12361. case DRM_FORMAT_ARGB8888:
  12362. break;
  12363. case DRM_FORMAT_XRGB1555:
  12364. if (INTEL_INFO(dev)->gen > 3) {
  12365. DRM_DEBUG("unsupported pixel format: %s\n",
  12366. drm_get_format_name(mode_cmd->pixel_format));
  12367. return -EINVAL;
  12368. }
  12369. break;
  12370. case DRM_FORMAT_ABGR8888:
  12371. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  12372. INTEL_INFO(dev)->gen < 9) {
  12373. DRM_DEBUG("unsupported pixel format: %s\n",
  12374. drm_get_format_name(mode_cmd->pixel_format));
  12375. return -EINVAL;
  12376. }
  12377. break;
  12378. case DRM_FORMAT_XBGR8888:
  12379. case DRM_FORMAT_XRGB2101010:
  12380. case DRM_FORMAT_XBGR2101010:
  12381. if (INTEL_INFO(dev)->gen < 4) {
  12382. DRM_DEBUG("unsupported pixel format: %s\n",
  12383. drm_get_format_name(mode_cmd->pixel_format));
  12384. return -EINVAL;
  12385. }
  12386. break;
  12387. case DRM_FORMAT_ABGR2101010:
  12388. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12389. DRM_DEBUG("unsupported pixel format: %s\n",
  12390. drm_get_format_name(mode_cmd->pixel_format));
  12391. return -EINVAL;
  12392. }
  12393. break;
  12394. case DRM_FORMAT_YUYV:
  12395. case DRM_FORMAT_UYVY:
  12396. case DRM_FORMAT_YVYU:
  12397. case DRM_FORMAT_VYUY:
  12398. if (INTEL_INFO(dev)->gen < 5) {
  12399. DRM_DEBUG("unsupported pixel format: %s\n",
  12400. drm_get_format_name(mode_cmd->pixel_format));
  12401. return -EINVAL;
  12402. }
  12403. break;
  12404. default:
  12405. DRM_DEBUG("unsupported pixel format: %s\n",
  12406. drm_get_format_name(mode_cmd->pixel_format));
  12407. return -EINVAL;
  12408. }
  12409. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12410. if (mode_cmd->offsets[0] != 0)
  12411. return -EINVAL;
  12412. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12413. mode_cmd->pixel_format,
  12414. mode_cmd->modifier[0]);
  12415. /* FIXME drm helper for size checks (especially planar formats)? */
  12416. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12417. return -EINVAL;
  12418. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12419. intel_fb->obj = obj;
  12420. intel_fill_fb_info(dev_priv, &intel_fb->base);
  12421. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12422. if (ret) {
  12423. DRM_ERROR("framebuffer init failed %d\n", ret);
  12424. return ret;
  12425. }
  12426. intel_fb->obj->framebuffer_references++;
  12427. return 0;
  12428. }
  12429. static struct drm_framebuffer *
  12430. intel_user_framebuffer_create(struct drm_device *dev,
  12431. struct drm_file *filp,
  12432. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12433. {
  12434. struct drm_framebuffer *fb;
  12435. struct drm_i915_gem_object *obj;
  12436. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12437. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12438. mode_cmd.handles[0]));
  12439. if (&obj->base == NULL)
  12440. return ERR_PTR(-ENOENT);
  12441. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  12442. if (IS_ERR(fb))
  12443. drm_gem_object_unreference_unlocked(&obj->base);
  12444. return fb;
  12445. }
  12446. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12447. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12448. {
  12449. }
  12450. #endif
  12451. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12452. .fb_create = intel_user_framebuffer_create,
  12453. .output_poll_changed = intel_fbdev_output_poll_changed,
  12454. .atomic_check = intel_atomic_check,
  12455. .atomic_commit = intel_atomic_commit,
  12456. .atomic_state_alloc = intel_atomic_state_alloc,
  12457. .atomic_state_clear = intel_atomic_state_clear,
  12458. };
  12459. /**
  12460. * intel_init_display_hooks - initialize the display modesetting hooks
  12461. * @dev_priv: device private
  12462. */
  12463. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12464. {
  12465. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12466. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12467. dev_priv->display.get_initial_plane_config =
  12468. skylake_get_initial_plane_config;
  12469. dev_priv->display.crtc_compute_clock =
  12470. haswell_crtc_compute_clock;
  12471. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12472. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12473. } else if (HAS_DDI(dev_priv)) {
  12474. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12475. dev_priv->display.get_initial_plane_config =
  12476. ironlake_get_initial_plane_config;
  12477. dev_priv->display.crtc_compute_clock =
  12478. haswell_crtc_compute_clock;
  12479. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12480. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12481. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12482. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12483. dev_priv->display.get_initial_plane_config =
  12484. ironlake_get_initial_plane_config;
  12485. dev_priv->display.crtc_compute_clock =
  12486. ironlake_crtc_compute_clock;
  12487. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12488. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12489. } else if (IS_CHERRYVIEW(dev_priv)) {
  12490. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12491. dev_priv->display.get_initial_plane_config =
  12492. i9xx_get_initial_plane_config;
  12493. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12494. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12495. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12496. } else if (IS_VALLEYVIEW(dev_priv)) {
  12497. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12498. dev_priv->display.get_initial_plane_config =
  12499. i9xx_get_initial_plane_config;
  12500. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12501. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12502. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12503. } else if (IS_G4X(dev_priv)) {
  12504. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12505. dev_priv->display.get_initial_plane_config =
  12506. i9xx_get_initial_plane_config;
  12507. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12508. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12509. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12510. } else if (IS_PINEVIEW(dev_priv)) {
  12511. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12512. dev_priv->display.get_initial_plane_config =
  12513. i9xx_get_initial_plane_config;
  12514. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12515. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12516. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12517. } else if (!IS_GEN2(dev_priv)) {
  12518. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12519. dev_priv->display.get_initial_plane_config =
  12520. i9xx_get_initial_plane_config;
  12521. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12522. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12523. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12524. } else {
  12525. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12526. dev_priv->display.get_initial_plane_config =
  12527. i9xx_get_initial_plane_config;
  12528. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12529. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12530. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12531. }
  12532. /* Returns the core display clock speed */
  12533. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12534. dev_priv->display.get_display_clock_speed =
  12535. skylake_get_display_clock_speed;
  12536. else if (IS_BROXTON(dev_priv))
  12537. dev_priv->display.get_display_clock_speed =
  12538. broxton_get_display_clock_speed;
  12539. else if (IS_BROADWELL(dev_priv))
  12540. dev_priv->display.get_display_clock_speed =
  12541. broadwell_get_display_clock_speed;
  12542. else if (IS_HASWELL(dev_priv))
  12543. dev_priv->display.get_display_clock_speed =
  12544. haswell_get_display_clock_speed;
  12545. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12546. dev_priv->display.get_display_clock_speed =
  12547. valleyview_get_display_clock_speed;
  12548. else if (IS_GEN5(dev_priv))
  12549. dev_priv->display.get_display_clock_speed =
  12550. ilk_get_display_clock_speed;
  12551. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  12552. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  12553. dev_priv->display.get_display_clock_speed =
  12554. i945_get_display_clock_speed;
  12555. else if (IS_GM45(dev_priv))
  12556. dev_priv->display.get_display_clock_speed =
  12557. gm45_get_display_clock_speed;
  12558. else if (IS_CRESTLINE(dev_priv))
  12559. dev_priv->display.get_display_clock_speed =
  12560. i965gm_get_display_clock_speed;
  12561. else if (IS_PINEVIEW(dev_priv))
  12562. dev_priv->display.get_display_clock_speed =
  12563. pnv_get_display_clock_speed;
  12564. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  12565. dev_priv->display.get_display_clock_speed =
  12566. g33_get_display_clock_speed;
  12567. else if (IS_I915G(dev_priv))
  12568. dev_priv->display.get_display_clock_speed =
  12569. i915_get_display_clock_speed;
  12570. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  12571. dev_priv->display.get_display_clock_speed =
  12572. i9xx_misc_get_display_clock_speed;
  12573. else if (IS_I915GM(dev_priv))
  12574. dev_priv->display.get_display_clock_speed =
  12575. i915gm_get_display_clock_speed;
  12576. else if (IS_I865G(dev_priv))
  12577. dev_priv->display.get_display_clock_speed =
  12578. i865_get_display_clock_speed;
  12579. else if (IS_I85X(dev_priv))
  12580. dev_priv->display.get_display_clock_speed =
  12581. i85x_get_display_clock_speed;
  12582. else { /* 830 */
  12583. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12584. dev_priv->display.get_display_clock_speed =
  12585. i830_get_display_clock_speed;
  12586. }
  12587. if (IS_GEN5(dev_priv)) {
  12588. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12589. } else if (IS_GEN6(dev_priv)) {
  12590. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12591. } else if (IS_IVYBRIDGE(dev_priv)) {
  12592. /* FIXME: detect B0+ stepping and use auto training */
  12593. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12594. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12595. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12596. if (IS_BROADWELL(dev_priv)) {
  12597. dev_priv->display.modeset_commit_cdclk =
  12598. broadwell_modeset_commit_cdclk;
  12599. dev_priv->display.modeset_calc_cdclk =
  12600. broadwell_modeset_calc_cdclk;
  12601. }
  12602. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12603. dev_priv->display.modeset_commit_cdclk =
  12604. valleyview_modeset_commit_cdclk;
  12605. dev_priv->display.modeset_calc_cdclk =
  12606. valleyview_modeset_calc_cdclk;
  12607. } else if (IS_BROXTON(dev_priv)) {
  12608. dev_priv->display.modeset_commit_cdclk =
  12609. broxton_modeset_commit_cdclk;
  12610. dev_priv->display.modeset_calc_cdclk =
  12611. broxton_modeset_calc_cdclk;
  12612. }
  12613. switch (INTEL_INFO(dev_priv)->gen) {
  12614. case 2:
  12615. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12616. break;
  12617. case 3:
  12618. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12619. break;
  12620. case 4:
  12621. case 5:
  12622. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12623. break;
  12624. case 6:
  12625. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12626. break;
  12627. case 7:
  12628. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12629. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12630. break;
  12631. case 9:
  12632. /* Drop through - unsupported since execlist only. */
  12633. default:
  12634. /* Default just returns -ENODEV to indicate unsupported */
  12635. dev_priv->display.queue_flip = intel_default_queue_flip;
  12636. }
  12637. }
  12638. /*
  12639. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12640. * resume, or other times. This quirk makes sure that's the case for
  12641. * affected systems.
  12642. */
  12643. static void quirk_pipea_force(struct drm_device *dev)
  12644. {
  12645. struct drm_i915_private *dev_priv = dev->dev_private;
  12646. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12647. DRM_INFO("applying pipe a force quirk\n");
  12648. }
  12649. static void quirk_pipeb_force(struct drm_device *dev)
  12650. {
  12651. struct drm_i915_private *dev_priv = dev->dev_private;
  12652. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12653. DRM_INFO("applying pipe b force quirk\n");
  12654. }
  12655. /*
  12656. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12657. */
  12658. static void quirk_ssc_force_disable(struct drm_device *dev)
  12659. {
  12660. struct drm_i915_private *dev_priv = dev->dev_private;
  12661. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12662. DRM_INFO("applying lvds SSC disable quirk\n");
  12663. }
  12664. /*
  12665. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12666. * brightness value
  12667. */
  12668. static void quirk_invert_brightness(struct drm_device *dev)
  12669. {
  12670. struct drm_i915_private *dev_priv = dev->dev_private;
  12671. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12672. DRM_INFO("applying inverted panel brightness quirk\n");
  12673. }
  12674. /* Some VBT's incorrectly indicate no backlight is present */
  12675. static void quirk_backlight_present(struct drm_device *dev)
  12676. {
  12677. struct drm_i915_private *dev_priv = dev->dev_private;
  12678. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12679. DRM_INFO("applying backlight present quirk\n");
  12680. }
  12681. struct intel_quirk {
  12682. int device;
  12683. int subsystem_vendor;
  12684. int subsystem_device;
  12685. void (*hook)(struct drm_device *dev);
  12686. };
  12687. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12688. struct intel_dmi_quirk {
  12689. void (*hook)(struct drm_device *dev);
  12690. const struct dmi_system_id (*dmi_id_list)[];
  12691. };
  12692. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12693. {
  12694. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12695. return 1;
  12696. }
  12697. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12698. {
  12699. .dmi_id_list = &(const struct dmi_system_id[]) {
  12700. {
  12701. .callback = intel_dmi_reverse_brightness,
  12702. .ident = "NCR Corporation",
  12703. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12704. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12705. },
  12706. },
  12707. { } /* terminating entry */
  12708. },
  12709. .hook = quirk_invert_brightness,
  12710. },
  12711. };
  12712. static struct intel_quirk intel_quirks[] = {
  12713. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12714. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12715. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12716. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12717. /* 830 needs to leave pipe A & dpll A up */
  12718. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12719. /* 830 needs to leave pipe B & dpll B up */
  12720. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12721. /* Lenovo U160 cannot use SSC on LVDS */
  12722. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12723. /* Sony Vaio Y cannot use SSC on LVDS */
  12724. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12725. /* Acer Aspire 5734Z must invert backlight brightness */
  12726. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12727. /* Acer/eMachines G725 */
  12728. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12729. /* Acer/eMachines e725 */
  12730. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12731. /* Acer/Packard Bell NCL20 */
  12732. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12733. /* Acer Aspire 4736Z */
  12734. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12735. /* Acer Aspire 5336 */
  12736. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12737. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12738. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12739. /* Acer C720 Chromebook (Core i3 4005U) */
  12740. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12741. /* Apple Macbook 2,1 (Core 2 T7400) */
  12742. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12743. /* Apple Macbook 4,1 */
  12744. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12745. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12746. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12747. /* HP Chromebook 14 (Celeron 2955U) */
  12748. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12749. /* Dell Chromebook 11 */
  12750. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12751. /* Dell Chromebook 11 (2015 version) */
  12752. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12753. };
  12754. static void intel_init_quirks(struct drm_device *dev)
  12755. {
  12756. struct pci_dev *d = dev->pdev;
  12757. int i;
  12758. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12759. struct intel_quirk *q = &intel_quirks[i];
  12760. if (d->device == q->device &&
  12761. (d->subsystem_vendor == q->subsystem_vendor ||
  12762. q->subsystem_vendor == PCI_ANY_ID) &&
  12763. (d->subsystem_device == q->subsystem_device ||
  12764. q->subsystem_device == PCI_ANY_ID))
  12765. q->hook(dev);
  12766. }
  12767. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12768. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12769. intel_dmi_quirks[i].hook(dev);
  12770. }
  12771. }
  12772. /* Disable the VGA plane that we never use */
  12773. static void i915_disable_vga(struct drm_device *dev)
  12774. {
  12775. struct drm_i915_private *dev_priv = dev->dev_private;
  12776. u8 sr1;
  12777. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  12778. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12779. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12780. outb(SR01, VGA_SR_INDEX);
  12781. sr1 = inb(VGA_SR_DATA);
  12782. outb(sr1 | 1<<5, VGA_SR_DATA);
  12783. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12784. udelay(300);
  12785. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12786. POSTING_READ(vga_reg);
  12787. }
  12788. void intel_modeset_init_hw(struct drm_device *dev)
  12789. {
  12790. struct drm_i915_private *dev_priv = dev->dev_private;
  12791. intel_update_cdclk(dev);
  12792. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  12793. intel_init_clock_gating(dev);
  12794. intel_enable_gt_powersave(dev);
  12795. }
  12796. /*
  12797. * Calculate what we think the watermarks should be for the state we've read
  12798. * out of the hardware and then immediately program those watermarks so that
  12799. * we ensure the hardware settings match our internal state.
  12800. *
  12801. * We can calculate what we think WM's should be by creating a duplicate of the
  12802. * current state (which was constructed during hardware readout) and running it
  12803. * through the atomic check code to calculate new watermark values in the
  12804. * state object.
  12805. */
  12806. static void sanitize_watermarks(struct drm_device *dev)
  12807. {
  12808. struct drm_i915_private *dev_priv = to_i915(dev);
  12809. struct drm_atomic_state *state;
  12810. struct drm_crtc *crtc;
  12811. struct drm_crtc_state *cstate;
  12812. struct drm_modeset_acquire_ctx ctx;
  12813. int ret;
  12814. int i;
  12815. /* Only supported on platforms that use atomic watermark design */
  12816. if (!dev_priv->display.optimize_watermarks)
  12817. return;
  12818. /*
  12819. * We need to hold connection_mutex before calling duplicate_state so
  12820. * that the connector loop is protected.
  12821. */
  12822. drm_modeset_acquire_init(&ctx, 0);
  12823. retry:
  12824. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12825. if (ret == -EDEADLK) {
  12826. drm_modeset_backoff(&ctx);
  12827. goto retry;
  12828. } else if (WARN_ON(ret)) {
  12829. goto fail;
  12830. }
  12831. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12832. if (WARN_ON(IS_ERR(state)))
  12833. goto fail;
  12834. /*
  12835. * Hardware readout is the only time we don't want to calculate
  12836. * intermediate watermarks (since we don't trust the current
  12837. * watermarks).
  12838. */
  12839. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  12840. ret = intel_atomic_check(dev, state);
  12841. if (ret) {
  12842. /*
  12843. * If we fail here, it means that the hardware appears to be
  12844. * programmed in a way that shouldn't be possible, given our
  12845. * understanding of watermark requirements. This might mean a
  12846. * mistake in the hardware readout code or a mistake in the
  12847. * watermark calculations for a given platform. Raise a WARN
  12848. * so that this is noticeable.
  12849. *
  12850. * If this actually happens, we'll have to just leave the
  12851. * BIOS-programmed watermarks untouched and hope for the best.
  12852. */
  12853. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12854. goto fail;
  12855. }
  12856. /* Write calculated watermark values back */
  12857. to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
  12858. for_each_crtc_in_state(state, crtc, cstate, i) {
  12859. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12860. cs->wm.need_postvbl_update = true;
  12861. dev_priv->display.optimize_watermarks(cs);
  12862. }
  12863. drm_atomic_state_free(state);
  12864. fail:
  12865. drm_modeset_drop_locks(&ctx);
  12866. drm_modeset_acquire_fini(&ctx);
  12867. }
  12868. void intel_modeset_init(struct drm_device *dev)
  12869. {
  12870. struct drm_i915_private *dev_priv = to_i915(dev);
  12871. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12872. int sprite, ret;
  12873. enum pipe pipe;
  12874. struct intel_crtc *crtc;
  12875. drm_mode_config_init(dev);
  12876. dev->mode_config.min_width = 0;
  12877. dev->mode_config.min_height = 0;
  12878. dev->mode_config.preferred_depth = 24;
  12879. dev->mode_config.prefer_shadow = 1;
  12880. dev->mode_config.allow_fb_modifiers = true;
  12881. dev->mode_config.funcs = &intel_mode_funcs;
  12882. intel_init_quirks(dev);
  12883. intel_init_pm(dev);
  12884. if (INTEL_INFO(dev)->num_pipes == 0)
  12885. return;
  12886. /*
  12887. * There may be no VBT; and if the BIOS enabled SSC we can
  12888. * just keep using it to avoid unnecessary flicker. Whereas if the
  12889. * BIOS isn't using it, don't assume it will work even if the VBT
  12890. * indicates as much.
  12891. */
  12892. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12893. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12894. DREF_SSC1_ENABLE);
  12895. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12896. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12897. bios_lvds_use_ssc ? "en" : "dis",
  12898. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12899. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12900. }
  12901. }
  12902. if (IS_GEN2(dev)) {
  12903. dev->mode_config.max_width = 2048;
  12904. dev->mode_config.max_height = 2048;
  12905. } else if (IS_GEN3(dev)) {
  12906. dev->mode_config.max_width = 4096;
  12907. dev->mode_config.max_height = 4096;
  12908. } else {
  12909. dev->mode_config.max_width = 8192;
  12910. dev->mode_config.max_height = 8192;
  12911. }
  12912. if (IS_845G(dev) || IS_I865G(dev)) {
  12913. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12914. dev->mode_config.cursor_height = 1023;
  12915. } else if (IS_GEN2(dev)) {
  12916. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12917. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12918. } else {
  12919. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12920. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12921. }
  12922. dev->mode_config.fb_base = ggtt->mappable_base;
  12923. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12924. INTEL_INFO(dev)->num_pipes,
  12925. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12926. for_each_pipe(dev_priv, pipe) {
  12927. intel_crtc_init(dev, pipe);
  12928. for_each_sprite(dev_priv, pipe, sprite) {
  12929. ret = intel_plane_init(dev, pipe, sprite);
  12930. if (ret)
  12931. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12932. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12933. }
  12934. }
  12935. intel_update_czclk(dev_priv);
  12936. intel_update_cdclk(dev);
  12937. intel_shared_dpll_init(dev);
  12938. /* Just disable it once at startup */
  12939. i915_disable_vga(dev);
  12940. intel_setup_outputs(dev);
  12941. drm_modeset_lock_all(dev);
  12942. intel_modeset_setup_hw_state(dev);
  12943. drm_modeset_unlock_all(dev);
  12944. for_each_intel_crtc(dev, crtc) {
  12945. struct intel_initial_plane_config plane_config = {};
  12946. if (!crtc->active)
  12947. continue;
  12948. /*
  12949. * Note that reserving the BIOS fb up front prevents us
  12950. * from stuffing other stolen allocations like the ring
  12951. * on top. This prevents some ugliness at boot time, and
  12952. * can even allow for smooth boot transitions if the BIOS
  12953. * fb is large enough for the active pipe configuration.
  12954. */
  12955. dev_priv->display.get_initial_plane_config(crtc,
  12956. &plane_config);
  12957. /*
  12958. * If the fb is shared between multiple heads, we'll
  12959. * just get the first one.
  12960. */
  12961. intel_find_initial_plane_obj(crtc, &plane_config);
  12962. }
  12963. /*
  12964. * Make sure hardware watermarks really match the state we read out.
  12965. * Note that we need to do this after reconstructing the BIOS fb's
  12966. * since the watermark calculation done here will use pstate->fb.
  12967. */
  12968. sanitize_watermarks(dev);
  12969. }
  12970. static void intel_enable_pipe_a(struct drm_device *dev)
  12971. {
  12972. struct intel_connector *connector;
  12973. struct drm_connector *crt = NULL;
  12974. struct intel_load_detect_pipe load_detect_temp;
  12975. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12976. /* We can't just switch on the pipe A, we need to set things up with a
  12977. * proper mode and output configuration. As a gross hack, enable pipe A
  12978. * by enabling the load detect pipe once. */
  12979. for_each_intel_connector(dev, connector) {
  12980. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12981. crt = &connector->base;
  12982. break;
  12983. }
  12984. }
  12985. if (!crt)
  12986. return;
  12987. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12988. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12989. }
  12990. static bool
  12991. intel_check_plane_mapping(struct intel_crtc *crtc)
  12992. {
  12993. struct drm_device *dev = crtc->base.dev;
  12994. struct drm_i915_private *dev_priv = dev->dev_private;
  12995. u32 val;
  12996. if (INTEL_INFO(dev)->num_pipes == 1)
  12997. return true;
  12998. val = I915_READ(DSPCNTR(!crtc->plane));
  12999. if ((val & DISPLAY_PLANE_ENABLE) &&
  13000. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13001. return false;
  13002. return true;
  13003. }
  13004. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13005. {
  13006. struct drm_device *dev = crtc->base.dev;
  13007. struct intel_encoder *encoder;
  13008. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13009. return true;
  13010. return false;
  13011. }
  13012. static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
  13013. {
  13014. struct drm_device *dev = encoder->base.dev;
  13015. struct intel_connector *connector;
  13016. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13017. return true;
  13018. return false;
  13019. }
  13020. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13021. {
  13022. struct drm_device *dev = crtc->base.dev;
  13023. struct drm_i915_private *dev_priv = dev->dev_private;
  13024. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13025. /* Clear any frame start delays used for debugging left by the BIOS */
  13026. if (!transcoder_is_dsi(cpu_transcoder)) {
  13027. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13028. I915_WRITE(reg,
  13029. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13030. }
  13031. /* restore vblank interrupts to correct state */
  13032. drm_crtc_vblank_reset(&crtc->base);
  13033. if (crtc->active) {
  13034. struct intel_plane *plane;
  13035. drm_crtc_vblank_on(&crtc->base);
  13036. /* Disable everything but the primary plane */
  13037. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13038. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13039. continue;
  13040. plane->disable_plane(&plane->base, &crtc->base);
  13041. }
  13042. }
  13043. /* We need to sanitize the plane -> pipe mapping first because this will
  13044. * disable the crtc (and hence change the state) if it is wrong. Note
  13045. * that gen4+ has a fixed plane -> pipe mapping. */
  13046. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  13047. bool plane;
  13048. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  13049. crtc->base.base.id);
  13050. /* Pipe has the wrong plane attached and the plane is active.
  13051. * Temporarily change the plane mapping and disable everything
  13052. * ... */
  13053. plane = crtc->plane;
  13054. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  13055. crtc->plane = !plane;
  13056. intel_crtc_disable_noatomic(&crtc->base);
  13057. crtc->plane = plane;
  13058. }
  13059. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13060. crtc->pipe == PIPE_A && !crtc->active) {
  13061. /* BIOS forgot to enable pipe A, this mostly happens after
  13062. * resume. Force-enable the pipe to fix this, the update_dpms
  13063. * call below we restore the pipe to the right state, but leave
  13064. * the required bits on. */
  13065. intel_enable_pipe_a(dev);
  13066. }
  13067. /* Adjust the state of the output pipe according to whether we
  13068. * have active connectors/encoders. */
  13069. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13070. intel_crtc_disable_noatomic(&crtc->base);
  13071. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  13072. /*
  13073. * We start out with underrun reporting disabled to avoid races.
  13074. * For correct bookkeeping mark this on active crtcs.
  13075. *
  13076. * Also on gmch platforms we dont have any hardware bits to
  13077. * disable the underrun reporting. Which means we need to start
  13078. * out with underrun reporting disabled also on inactive pipes,
  13079. * since otherwise we'll complain about the garbage we read when
  13080. * e.g. coming up after runtime pm.
  13081. *
  13082. * No protection against concurrent access is required - at
  13083. * worst a fifo underrun happens which also sets this to false.
  13084. */
  13085. crtc->cpu_fifo_underrun_disabled = true;
  13086. crtc->pch_fifo_underrun_disabled = true;
  13087. }
  13088. }
  13089. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13090. {
  13091. struct intel_connector *connector;
  13092. struct drm_device *dev = encoder->base.dev;
  13093. /* We need to check both for a crtc link (meaning that the
  13094. * encoder is active and trying to read from a pipe) and the
  13095. * pipe itself being active. */
  13096. bool has_active_crtc = encoder->base.crtc &&
  13097. to_intel_crtc(encoder->base.crtc)->active;
  13098. if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
  13099. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13100. encoder->base.base.id,
  13101. encoder->base.name);
  13102. /* Connector is active, but has no active pipe. This is
  13103. * fallout from our resume register restoring. Disable
  13104. * the encoder manually again. */
  13105. if (encoder->base.crtc) {
  13106. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13107. encoder->base.base.id,
  13108. encoder->base.name);
  13109. encoder->disable(encoder);
  13110. if (encoder->post_disable)
  13111. encoder->post_disable(encoder);
  13112. }
  13113. encoder->base.crtc = NULL;
  13114. /* Inconsistent output/port/pipe state happens presumably due to
  13115. * a bug in one of the get_hw_state functions. Or someplace else
  13116. * in our code, like the register restore mess on resume. Clamp
  13117. * things to off as a safer default. */
  13118. for_each_intel_connector(dev, connector) {
  13119. if (connector->encoder != encoder)
  13120. continue;
  13121. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13122. connector->base.encoder = NULL;
  13123. }
  13124. }
  13125. /* Enabled encoders without active connectors will be fixed in
  13126. * the crtc fixup. */
  13127. }
  13128. void i915_redisable_vga_power_on(struct drm_device *dev)
  13129. {
  13130. struct drm_i915_private *dev_priv = dev->dev_private;
  13131. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13132. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13133. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13134. i915_disable_vga(dev);
  13135. }
  13136. }
  13137. void i915_redisable_vga(struct drm_device *dev)
  13138. {
  13139. struct drm_i915_private *dev_priv = dev->dev_private;
  13140. /* This function can be called both from intel_modeset_setup_hw_state or
  13141. * at a very early point in our resume sequence, where the power well
  13142. * structures are not yet restored. Since this function is at a very
  13143. * paranoid "someone might have enabled VGA while we were not looking"
  13144. * level, just check if the power well is enabled instead of trying to
  13145. * follow the "don't touch the power well if we don't need it" policy
  13146. * the rest of the driver uses. */
  13147. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  13148. return;
  13149. i915_redisable_vga_power_on(dev);
  13150. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  13151. }
  13152. static bool primary_get_hw_state(struct intel_plane *plane)
  13153. {
  13154. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  13155. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  13156. }
  13157. /* FIXME read out full plane state for all planes */
  13158. static void readout_plane_state(struct intel_crtc *crtc)
  13159. {
  13160. struct drm_plane *primary = crtc->base.primary;
  13161. struct intel_plane_state *plane_state =
  13162. to_intel_plane_state(primary->state);
  13163. plane_state->visible = crtc->active &&
  13164. primary_get_hw_state(to_intel_plane(primary));
  13165. if (plane_state->visible)
  13166. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  13167. }
  13168. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13169. {
  13170. struct drm_i915_private *dev_priv = dev->dev_private;
  13171. enum pipe pipe;
  13172. struct intel_crtc *crtc;
  13173. struct intel_encoder *encoder;
  13174. struct intel_connector *connector;
  13175. int i;
  13176. dev_priv->active_crtcs = 0;
  13177. for_each_intel_crtc(dev, crtc) {
  13178. struct intel_crtc_state *crtc_state = crtc->config;
  13179. int pixclk = 0;
  13180. __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
  13181. memset(crtc_state, 0, sizeof(*crtc_state));
  13182. crtc_state->base.crtc = &crtc->base;
  13183. crtc_state->base.active = crtc_state->base.enable =
  13184. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13185. crtc->base.enabled = crtc_state->base.enable;
  13186. crtc->active = crtc_state->base.active;
  13187. if (crtc_state->base.active) {
  13188. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13189. if (IS_BROADWELL(dev_priv)) {
  13190. pixclk = ilk_pipe_pixel_rate(crtc_state);
  13191. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  13192. if (crtc_state->ips_enabled)
  13193. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  13194. } else if (IS_VALLEYVIEW(dev_priv) ||
  13195. IS_CHERRYVIEW(dev_priv) ||
  13196. IS_BROXTON(dev_priv))
  13197. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  13198. else
  13199. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  13200. }
  13201. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  13202. readout_plane_state(crtc);
  13203. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  13204. crtc->base.base.id,
  13205. crtc->active ? "enabled" : "disabled");
  13206. }
  13207. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13208. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13209. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  13210. &pll->config.hw_state);
  13211. pll->config.crtc_mask = 0;
  13212. for_each_intel_crtc(dev, crtc) {
  13213. if (crtc->active && crtc->config->shared_dpll == pll)
  13214. pll->config.crtc_mask |= 1 << crtc->pipe;
  13215. }
  13216. pll->active_mask = pll->config.crtc_mask;
  13217. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13218. pll->name, pll->config.crtc_mask, pll->on);
  13219. }
  13220. for_each_intel_encoder(dev, encoder) {
  13221. pipe = 0;
  13222. if (encoder->get_hw_state(encoder, &pipe)) {
  13223. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13224. encoder->base.crtc = &crtc->base;
  13225. encoder->get_config(encoder, crtc->config);
  13226. } else {
  13227. encoder->base.crtc = NULL;
  13228. }
  13229. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13230. encoder->base.base.id,
  13231. encoder->base.name,
  13232. encoder->base.crtc ? "enabled" : "disabled",
  13233. pipe_name(pipe));
  13234. }
  13235. for_each_intel_connector(dev, connector) {
  13236. if (connector->get_hw_state(connector)) {
  13237. connector->base.dpms = DRM_MODE_DPMS_ON;
  13238. encoder = connector->encoder;
  13239. connector->base.encoder = &encoder->base;
  13240. if (encoder->base.crtc &&
  13241. encoder->base.crtc->state->active) {
  13242. /*
  13243. * This has to be done during hardware readout
  13244. * because anything calling .crtc_disable may
  13245. * rely on the connector_mask being accurate.
  13246. */
  13247. encoder->base.crtc->state->connector_mask |=
  13248. 1 << drm_connector_index(&connector->base);
  13249. encoder->base.crtc->state->encoder_mask |=
  13250. 1 << drm_encoder_index(&encoder->base);
  13251. }
  13252. } else {
  13253. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13254. connector->base.encoder = NULL;
  13255. }
  13256. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13257. connector->base.base.id,
  13258. connector->base.name,
  13259. connector->base.encoder ? "enabled" : "disabled");
  13260. }
  13261. for_each_intel_crtc(dev, crtc) {
  13262. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  13263. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13264. if (crtc->base.state->active) {
  13265. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  13266. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  13267. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13268. /*
  13269. * The initial mode needs to be set in order to keep
  13270. * the atomic core happy. It wants a valid mode if the
  13271. * crtc's enabled, so we do the above call.
  13272. *
  13273. * At this point some state updated by the connectors
  13274. * in their ->detect() callback has not run yet, so
  13275. * no recalculation can be done yet.
  13276. *
  13277. * Even if we could do a recalculation and modeset
  13278. * right now it would cause a double modeset if
  13279. * fbdev or userspace chooses a different initial mode.
  13280. *
  13281. * If that happens, someone indicated they wanted a
  13282. * mode change, which means it's safe to do a full
  13283. * recalculation.
  13284. */
  13285. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  13286. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  13287. update_scanline_offset(crtc);
  13288. }
  13289. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  13290. }
  13291. }
  13292. /* Scan out the current hw modeset state,
  13293. * and sanitizes it to the current state
  13294. */
  13295. static void
  13296. intel_modeset_setup_hw_state(struct drm_device *dev)
  13297. {
  13298. struct drm_i915_private *dev_priv = dev->dev_private;
  13299. enum pipe pipe;
  13300. struct intel_crtc *crtc;
  13301. struct intel_encoder *encoder;
  13302. int i;
  13303. intel_modeset_readout_hw_state(dev);
  13304. /* HW state is read out, now we need to sanitize this mess. */
  13305. for_each_intel_encoder(dev, encoder) {
  13306. intel_sanitize_encoder(encoder);
  13307. }
  13308. for_each_pipe(dev_priv, pipe) {
  13309. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13310. intel_sanitize_crtc(crtc);
  13311. intel_dump_pipe_config(crtc, crtc->config,
  13312. "[setup_hw_state]");
  13313. }
  13314. intel_modeset_update_connector_atomic_state(dev);
  13315. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13316. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13317. if (!pll->on || pll->active_mask)
  13318. continue;
  13319. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13320. pll->funcs.disable(dev_priv, pll);
  13321. pll->on = false;
  13322. }
  13323. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  13324. vlv_wm_get_hw_state(dev);
  13325. else if (IS_GEN9(dev))
  13326. skl_wm_get_hw_state(dev);
  13327. else if (HAS_PCH_SPLIT(dev))
  13328. ilk_wm_get_hw_state(dev);
  13329. for_each_intel_crtc(dev, crtc) {
  13330. unsigned long put_domains;
  13331. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13332. if (WARN_ON(put_domains))
  13333. modeset_put_power_domains(dev_priv, put_domains);
  13334. }
  13335. intel_display_set_init_power(dev_priv, false);
  13336. intel_fbc_init_pipe_state(dev_priv);
  13337. }
  13338. void intel_display_resume(struct drm_device *dev)
  13339. {
  13340. struct drm_i915_private *dev_priv = to_i915(dev);
  13341. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13342. struct drm_modeset_acquire_ctx ctx;
  13343. int ret;
  13344. bool setup = false;
  13345. dev_priv->modeset_restore_state = NULL;
  13346. /*
  13347. * This is a cludge because with real atomic modeset mode_config.mutex
  13348. * won't be taken. Unfortunately some probed state like
  13349. * audio_codec_enable is still protected by mode_config.mutex, so lock
  13350. * it here for now.
  13351. */
  13352. mutex_lock(&dev->mode_config.mutex);
  13353. drm_modeset_acquire_init(&ctx, 0);
  13354. retry:
  13355. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13356. if (ret == 0 && !setup) {
  13357. setup = true;
  13358. intel_modeset_setup_hw_state(dev);
  13359. i915_redisable_vga(dev);
  13360. }
  13361. if (ret == 0 && state) {
  13362. struct drm_crtc_state *crtc_state;
  13363. struct drm_crtc *crtc;
  13364. int i;
  13365. state->acquire_ctx = &ctx;
  13366. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  13367. /*
  13368. * Force recalculation even if we restore
  13369. * current state. With fast modeset this may not result
  13370. * in a modeset when the state is compatible.
  13371. */
  13372. crtc_state->mode_changed = true;
  13373. }
  13374. ret = drm_atomic_commit(state);
  13375. }
  13376. if (ret == -EDEADLK) {
  13377. drm_modeset_backoff(&ctx);
  13378. goto retry;
  13379. }
  13380. drm_modeset_drop_locks(&ctx);
  13381. drm_modeset_acquire_fini(&ctx);
  13382. mutex_unlock(&dev->mode_config.mutex);
  13383. if (ret) {
  13384. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13385. drm_atomic_state_free(state);
  13386. }
  13387. }
  13388. void intel_modeset_gem_init(struct drm_device *dev)
  13389. {
  13390. struct drm_crtc *c;
  13391. struct drm_i915_gem_object *obj;
  13392. int ret;
  13393. intel_init_gt_powersave(dev);
  13394. intel_modeset_init_hw(dev);
  13395. intel_setup_overlay(dev);
  13396. /*
  13397. * Make sure any fbs we allocated at startup are properly
  13398. * pinned & fenced. When we do the allocation it's too early
  13399. * for this.
  13400. */
  13401. for_each_crtc(dev, c) {
  13402. obj = intel_fb_obj(c->primary->fb);
  13403. if (obj == NULL)
  13404. continue;
  13405. mutex_lock(&dev->struct_mutex);
  13406. ret = intel_pin_and_fence_fb_obj(c->primary->fb,
  13407. c->primary->state->rotation);
  13408. mutex_unlock(&dev->struct_mutex);
  13409. if (ret) {
  13410. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13411. to_intel_crtc(c)->pipe);
  13412. drm_framebuffer_unreference(c->primary->fb);
  13413. c->primary->fb = NULL;
  13414. c->primary->crtc = c->primary->state->crtc = NULL;
  13415. update_state_fb(c->primary);
  13416. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13417. }
  13418. }
  13419. intel_backlight_register(dev);
  13420. }
  13421. void intel_connector_unregister(struct intel_connector *intel_connector)
  13422. {
  13423. struct drm_connector *connector = &intel_connector->base;
  13424. intel_panel_destroy_backlight(connector);
  13425. drm_connector_unregister(connector);
  13426. }
  13427. void intel_modeset_cleanup(struct drm_device *dev)
  13428. {
  13429. struct drm_i915_private *dev_priv = dev->dev_private;
  13430. struct intel_connector *connector;
  13431. intel_disable_gt_powersave(dev);
  13432. intel_backlight_unregister(dev);
  13433. /*
  13434. * Interrupts and polling as the first thing to avoid creating havoc.
  13435. * Too much stuff here (turning of connectors, ...) would
  13436. * experience fancy races otherwise.
  13437. */
  13438. intel_irq_uninstall(dev_priv);
  13439. /*
  13440. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13441. * poll handlers. Hence disable polling after hpd handling is shut down.
  13442. */
  13443. drm_kms_helper_poll_fini(dev);
  13444. intel_unregister_dsm_handler();
  13445. intel_fbc_global_disable(dev_priv);
  13446. /* flush any delayed tasks or pending work */
  13447. flush_scheduled_work();
  13448. /* destroy the backlight and sysfs files before encoders/connectors */
  13449. for_each_intel_connector(dev, connector)
  13450. connector->unregister(connector);
  13451. drm_mode_config_cleanup(dev);
  13452. intel_cleanup_overlay(dev);
  13453. intel_cleanup_gt_powersave(dev);
  13454. intel_teardown_gmbus(dev);
  13455. }
  13456. /*
  13457. * Return which encoder is currently attached for connector.
  13458. */
  13459. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13460. {
  13461. return &intel_attached_encoder(connector)->base;
  13462. }
  13463. void intel_connector_attach_encoder(struct intel_connector *connector,
  13464. struct intel_encoder *encoder)
  13465. {
  13466. connector->encoder = encoder;
  13467. drm_mode_connector_attach_encoder(&connector->base,
  13468. &encoder->base);
  13469. }
  13470. /*
  13471. * set vga decode state - true == enable VGA decode
  13472. */
  13473. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13474. {
  13475. struct drm_i915_private *dev_priv = dev->dev_private;
  13476. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13477. u16 gmch_ctrl;
  13478. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13479. DRM_ERROR("failed to read control word\n");
  13480. return -EIO;
  13481. }
  13482. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13483. return 0;
  13484. if (state)
  13485. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13486. else
  13487. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13488. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13489. DRM_ERROR("failed to write control word\n");
  13490. return -EIO;
  13491. }
  13492. return 0;
  13493. }
  13494. struct intel_display_error_state {
  13495. u32 power_well_driver;
  13496. int num_transcoders;
  13497. struct intel_cursor_error_state {
  13498. u32 control;
  13499. u32 position;
  13500. u32 base;
  13501. u32 size;
  13502. } cursor[I915_MAX_PIPES];
  13503. struct intel_pipe_error_state {
  13504. bool power_domain_on;
  13505. u32 source;
  13506. u32 stat;
  13507. } pipe[I915_MAX_PIPES];
  13508. struct intel_plane_error_state {
  13509. u32 control;
  13510. u32 stride;
  13511. u32 size;
  13512. u32 pos;
  13513. u32 addr;
  13514. u32 surface;
  13515. u32 tile_offset;
  13516. } plane[I915_MAX_PIPES];
  13517. struct intel_transcoder_error_state {
  13518. bool power_domain_on;
  13519. enum transcoder cpu_transcoder;
  13520. u32 conf;
  13521. u32 htotal;
  13522. u32 hblank;
  13523. u32 hsync;
  13524. u32 vtotal;
  13525. u32 vblank;
  13526. u32 vsync;
  13527. } transcoder[4];
  13528. };
  13529. struct intel_display_error_state *
  13530. intel_display_capture_error_state(struct drm_device *dev)
  13531. {
  13532. struct drm_i915_private *dev_priv = dev->dev_private;
  13533. struct intel_display_error_state *error;
  13534. int transcoders[] = {
  13535. TRANSCODER_A,
  13536. TRANSCODER_B,
  13537. TRANSCODER_C,
  13538. TRANSCODER_EDP,
  13539. };
  13540. int i;
  13541. if (INTEL_INFO(dev)->num_pipes == 0)
  13542. return NULL;
  13543. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13544. if (error == NULL)
  13545. return NULL;
  13546. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13547. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13548. for_each_pipe(dev_priv, i) {
  13549. error->pipe[i].power_domain_on =
  13550. __intel_display_power_is_enabled(dev_priv,
  13551. POWER_DOMAIN_PIPE(i));
  13552. if (!error->pipe[i].power_domain_on)
  13553. continue;
  13554. error->cursor[i].control = I915_READ(CURCNTR(i));
  13555. error->cursor[i].position = I915_READ(CURPOS(i));
  13556. error->cursor[i].base = I915_READ(CURBASE(i));
  13557. error->plane[i].control = I915_READ(DSPCNTR(i));
  13558. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13559. if (INTEL_INFO(dev)->gen <= 3) {
  13560. error->plane[i].size = I915_READ(DSPSIZE(i));
  13561. error->plane[i].pos = I915_READ(DSPPOS(i));
  13562. }
  13563. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13564. error->plane[i].addr = I915_READ(DSPADDR(i));
  13565. if (INTEL_INFO(dev)->gen >= 4) {
  13566. error->plane[i].surface = I915_READ(DSPSURF(i));
  13567. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13568. }
  13569. error->pipe[i].source = I915_READ(PIPESRC(i));
  13570. if (HAS_GMCH_DISPLAY(dev))
  13571. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13572. }
  13573. /* Note: this does not include DSI transcoders. */
  13574. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13575. if (HAS_DDI(dev_priv))
  13576. error->num_transcoders++; /* Account for eDP. */
  13577. for (i = 0; i < error->num_transcoders; i++) {
  13578. enum transcoder cpu_transcoder = transcoders[i];
  13579. error->transcoder[i].power_domain_on =
  13580. __intel_display_power_is_enabled(dev_priv,
  13581. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13582. if (!error->transcoder[i].power_domain_on)
  13583. continue;
  13584. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13585. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13586. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13587. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13588. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13589. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13590. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13591. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13592. }
  13593. return error;
  13594. }
  13595. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13596. void
  13597. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13598. struct drm_device *dev,
  13599. struct intel_display_error_state *error)
  13600. {
  13601. struct drm_i915_private *dev_priv = dev->dev_private;
  13602. int i;
  13603. if (!error)
  13604. return;
  13605. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13606. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13607. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13608. error->power_well_driver);
  13609. for_each_pipe(dev_priv, i) {
  13610. err_printf(m, "Pipe [%d]:\n", i);
  13611. err_printf(m, " Power: %s\n",
  13612. onoff(error->pipe[i].power_domain_on));
  13613. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13614. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13615. err_printf(m, "Plane [%d]:\n", i);
  13616. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13617. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13618. if (INTEL_INFO(dev)->gen <= 3) {
  13619. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13620. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13621. }
  13622. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13623. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13624. if (INTEL_INFO(dev)->gen >= 4) {
  13625. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13626. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13627. }
  13628. err_printf(m, "Cursor [%d]:\n", i);
  13629. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13630. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13631. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13632. }
  13633. for (i = 0; i < error->num_transcoders; i++) {
  13634. err_printf(m, "CPU transcoder: %s\n",
  13635. transcoder_name(error->transcoder[i].cpu_transcoder));
  13636. err_printf(m, " Power: %s\n",
  13637. onoff(error->transcoder[i].power_domain_on));
  13638. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13639. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13640. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13641. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13642. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13643. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13644. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13645. }
  13646. }