intel_display.c 456 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335
  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. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  167. CCK_DISPLAY_REF_CLOCK_CONTROL);
  168. }
  169. static int
  170. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  171. {
  172. uint32_t clkcfg;
  173. /* hrawclock is 1/4 the FSB frequency */
  174. clkcfg = I915_READ(CLKCFG);
  175. switch (clkcfg & CLKCFG_FSB_MASK) {
  176. case CLKCFG_FSB_400:
  177. return 100000;
  178. case CLKCFG_FSB_533:
  179. return 133333;
  180. case CLKCFG_FSB_667:
  181. return 166667;
  182. case CLKCFG_FSB_800:
  183. return 200000;
  184. case CLKCFG_FSB_1067:
  185. return 266667;
  186. case CLKCFG_FSB_1333:
  187. return 333333;
  188. /* these two are just a guess; one of them might be right */
  189. case CLKCFG_FSB_1600:
  190. case CLKCFG_FSB_1600_ALT:
  191. return 400000;
  192. default:
  193. return 133333;
  194. }
  195. }
  196. static void intel_update_rawclk(struct drm_i915_private *dev_priv)
  197. {
  198. if (HAS_PCH_SPLIT(dev_priv))
  199. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  200. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  201. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  202. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  203. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  204. else
  205. return; /* no rawclk on other platforms, or no need to know it */
  206. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  207. }
  208. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  209. {
  210. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  211. return;
  212. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  213. CCK_CZ_CLOCK_CONTROL);
  214. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  215. }
  216. static inline u32 /* units of 100MHz */
  217. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  218. const struct intel_crtc_state *pipe_config)
  219. {
  220. if (HAS_DDI(dev_priv))
  221. return pipe_config->port_clock; /* SPLL */
  222. else if (IS_GEN5(dev_priv))
  223. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  224. else
  225. return 270000;
  226. }
  227. static const intel_limit_t intel_limits_i8xx_dac = {
  228. .dot = { .min = 25000, .max = 350000 },
  229. .vco = { .min = 908000, .max = 1512000 },
  230. .n = { .min = 2, .max = 16 },
  231. .m = { .min = 96, .max = 140 },
  232. .m1 = { .min = 18, .max = 26 },
  233. .m2 = { .min = 6, .max = 16 },
  234. .p = { .min = 4, .max = 128 },
  235. .p1 = { .min = 2, .max = 33 },
  236. .p2 = { .dot_limit = 165000,
  237. .p2_slow = 4, .p2_fast = 2 },
  238. };
  239. static const intel_limit_t intel_limits_i8xx_dvo = {
  240. .dot = { .min = 25000, .max = 350000 },
  241. .vco = { .min = 908000, .max = 1512000 },
  242. .n = { .min = 2, .max = 16 },
  243. .m = { .min = 96, .max = 140 },
  244. .m1 = { .min = 18, .max = 26 },
  245. .m2 = { .min = 6, .max = 16 },
  246. .p = { .min = 4, .max = 128 },
  247. .p1 = { .min = 2, .max = 33 },
  248. .p2 = { .dot_limit = 165000,
  249. .p2_slow = 4, .p2_fast = 4 },
  250. };
  251. static const intel_limit_t intel_limits_i8xx_lvds = {
  252. .dot = { .min = 25000, .max = 350000 },
  253. .vco = { .min = 908000, .max = 1512000 },
  254. .n = { .min = 2, .max = 16 },
  255. .m = { .min = 96, .max = 140 },
  256. .m1 = { .min = 18, .max = 26 },
  257. .m2 = { .min = 6, .max = 16 },
  258. .p = { .min = 4, .max = 128 },
  259. .p1 = { .min = 1, .max = 6 },
  260. .p2 = { .dot_limit = 165000,
  261. .p2_slow = 14, .p2_fast = 7 },
  262. };
  263. static const intel_limit_t intel_limits_i9xx_sdvo = {
  264. .dot = { .min = 20000, .max = 400000 },
  265. .vco = { .min = 1400000, .max = 2800000 },
  266. .n = { .min = 1, .max = 6 },
  267. .m = { .min = 70, .max = 120 },
  268. .m1 = { .min = 8, .max = 18 },
  269. .m2 = { .min = 3, .max = 7 },
  270. .p = { .min = 5, .max = 80 },
  271. .p1 = { .min = 1, .max = 8 },
  272. .p2 = { .dot_limit = 200000,
  273. .p2_slow = 10, .p2_fast = 5 },
  274. };
  275. static const intel_limit_t intel_limits_i9xx_lvds = {
  276. .dot = { .min = 20000, .max = 400000 },
  277. .vco = { .min = 1400000, .max = 2800000 },
  278. .n = { .min = 1, .max = 6 },
  279. .m = { .min = 70, .max = 120 },
  280. .m1 = { .min = 8, .max = 18 },
  281. .m2 = { .min = 3, .max = 7 },
  282. .p = { .min = 7, .max = 98 },
  283. .p1 = { .min = 1, .max = 8 },
  284. .p2 = { .dot_limit = 112000,
  285. .p2_slow = 14, .p2_fast = 7 },
  286. };
  287. static const intel_limit_t intel_limits_g4x_sdvo = {
  288. .dot = { .min = 25000, .max = 270000 },
  289. .vco = { .min = 1750000, .max = 3500000},
  290. .n = { .min = 1, .max = 4 },
  291. .m = { .min = 104, .max = 138 },
  292. .m1 = { .min = 17, .max = 23 },
  293. .m2 = { .min = 5, .max = 11 },
  294. .p = { .min = 10, .max = 30 },
  295. .p1 = { .min = 1, .max = 3},
  296. .p2 = { .dot_limit = 270000,
  297. .p2_slow = 10,
  298. .p2_fast = 10
  299. },
  300. };
  301. static const intel_limit_t intel_limits_g4x_hdmi = {
  302. .dot = { .min = 22000, .max = 400000 },
  303. .vco = { .min = 1750000, .max = 3500000},
  304. .n = { .min = 1, .max = 4 },
  305. .m = { .min = 104, .max = 138 },
  306. .m1 = { .min = 16, .max = 23 },
  307. .m2 = { .min = 5, .max = 11 },
  308. .p = { .min = 5, .max = 80 },
  309. .p1 = { .min = 1, .max = 8},
  310. .p2 = { .dot_limit = 165000,
  311. .p2_slow = 10, .p2_fast = 5 },
  312. };
  313. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  314. .dot = { .min = 20000, .max = 115000 },
  315. .vco = { .min = 1750000, .max = 3500000 },
  316. .n = { .min = 1, .max = 3 },
  317. .m = { .min = 104, .max = 138 },
  318. .m1 = { .min = 17, .max = 23 },
  319. .m2 = { .min = 5, .max = 11 },
  320. .p = { .min = 28, .max = 112 },
  321. .p1 = { .min = 2, .max = 8 },
  322. .p2 = { .dot_limit = 0,
  323. .p2_slow = 14, .p2_fast = 14
  324. },
  325. };
  326. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  327. .dot = { .min = 80000, .max = 224000 },
  328. .vco = { .min = 1750000, .max = 3500000 },
  329. .n = { .min = 1, .max = 3 },
  330. .m = { .min = 104, .max = 138 },
  331. .m1 = { .min = 17, .max = 23 },
  332. .m2 = { .min = 5, .max = 11 },
  333. .p = { .min = 14, .max = 42 },
  334. .p1 = { .min = 2, .max = 6 },
  335. .p2 = { .dot_limit = 0,
  336. .p2_slow = 7, .p2_fast = 7
  337. },
  338. };
  339. static const intel_limit_t intel_limits_pineview_sdvo = {
  340. .dot = { .min = 20000, .max = 400000},
  341. .vco = { .min = 1700000, .max = 3500000 },
  342. /* Pineview's Ncounter is a ring counter */
  343. .n = { .min = 3, .max = 6 },
  344. .m = { .min = 2, .max = 256 },
  345. /* Pineview only has one combined m divider, which we treat as m2. */
  346. .m1 = { .min = 0, .max = 0 },
  347. .m2 = { .min = 0, .max = 254 },
  348. .p = { .min = 5, .max = 80 },
  349. .p1 = { .min = 1, .max = 8 },
  350. .p2 = { .dot_limit = 200000,
  351. .p2_slow = 10, .p2_fast = 5 },
  352. };
  353. static const intel_limit_t intel_limits_pineview_lvds = {
  354. .dot = { .min = 20000, .max = 400000 },
  355. .vco = { .min = 1700000, .max = 3500000 },
  356. .n = { .min = 3, .max = 6 },
  357. .m = { .min = 2, .max = 256 },
  358. .m1 = { .min = 0, .max = 0 },
  359. .m2 = { .min = 0, .max = 254 },
  360. .p = { .min = 7, .max = 112 },
  361. .p1 = { .min = 1, .max = 8 },
  362. .p2 = { .dot_limit = 112000,
  363. .p2_slow = 14, .p2_fast = 14 },
  364. };
  365. /* Ironlake / Sandybridge
  366. *
  367. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  368. * the range value for them is (actual_value - 2).
  369. */
  370. static const intel_limit_t intel_limits_ironlake_dac = {
  371. .dot = { .min = 25000, .max = 350000 },
  372. .vco = { .min = 1760000, .max = 3510000 },
  373. .n = { .min = 1, .max = 5 },
  374. .m = { .min = 79, .max = 127 },
  375. .m1 = { .min = 12, .max = 22 },
  376. .m2 = { .min = 5, .max = 9 },
  377. .p = { .min = 5, .max = 80 },
  378. .p1 = { .min = 1, .max = 8 },
  379. .p2 = { .dot_limit = 225000,
  380. .p2_slow = 10, .p2_fast = 5 },
  381. };
  382. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  383. .dot = { .min = 25000, .max = 350000 },
  384. .vco = { .min = 1760000, .max = 3510000 },
  385. .n = { .min = 1, .max = 3 },
  386. .m = { .min = 79, .max = 118 },
  387. .m1 = { .min = 12, .max = 22 },
  388. .m2 = { .min = 5, .max = 9 },
  389. .p = { .min = 28, .max = 112 },
  390. .p1 = { .min = 2, .max = 8 },
  391. .p2 = { .dot_limit = 225000,
  392. .p2_slow = 14, .p2_fast = 14 },
  393. };
  394. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  395. .dot = { .min = 25000, .max = 350000 },
  396. .vco = { .min = 1760000, .max = 3510000 },
  397. .n = { .min = 1, .max = 3 },
  398. .m = { .min = 79, .max = 127 },
  399. .m1 = { .min = 12, .max = 22 },
  400. .m2 = { .min = 5, .max = 9 },
  401. .p = { .min = 14, .max = 56 },
  402. .p1 = { .min = 2, .max = 8 },
  403. .p2 = { .dot_limit = 225000,
  404. .p2_slow = 7, .p2_fast = 7 },
  405. };
  406. /* LVDS 100mhz refclk limits. */
  407. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  408. .dot = { .min = 25000, .max = 350000 },
  409. .vco = { .min = 1760000, .max = 3510000 },
  410. .n = { .min = 1, .max = 2 },
  411. .m = { .min = 79, .max = 126 },
  412. .m1 = { .min = 12, .max = 22 },
  413. .m2 = { .min = 5, .max = 9 },
  414. .p = { .min = 28, .max = 112 },
  415. .p1 = { .min = 2, .max = 8 },
  416. .p2 = { .dot_limit = 225000,
  417. .p2_slow = 14, .p2_fast = 14 },
  418. };
  419. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  420. .dot = { .min = 25000, .max = 350000 },
  421. .vco = { .min = 1760000, .max = 3510000 },
  422. .n = { .min = 1, .max = 3 },
  423. .m = { .min = 79, .max = 126 },
  424. .m1 = { .min = 12, .max = 22 },
  425. .m2 = { .min = 5, .max = 9 },
  426. .p = { .min = 14, .max = 42 },
  427. .p1 = { .min = 2, .max = 6 },
  428. .p2 = { .dot_limit = 225000,
  429. .p2_slow = 7, .p2_fast = 7 },
  430. };
  431. static const intel_limit_t intel_limits_vlv = {
  432. /*
  433. * These are the data rate limits (measured in fast clocks)
  434. * since those are the strictest limits we have. The fast
  435. * clock and actual rate limits are more relaxed, so checking
  436. * them would make no difference.
  437. */
  438. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  439. .vco = { .min = 4000000, .max = 6000000 },
  440. .n = { .min = 1, .max = 7 },
  441. .m1 = { .min = 2, .max = 3 },
  442. .m2 = { .min = 11, .max = 156 },
  443. .p1 = { .min = 2, .max = 3 },
  444. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  445. };
  446. static const intel_limit_t intel_limits_chv = {
  447. /*
  448. * These are the data rate limits (measured in fast clocks)
  449. * since those are the strictest limits we have. The fast
  450. * clock and actual rate limits are more relaxed, so checking
  451. * them would make no difference.
  452. */
  453. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  454. .vco = { .min = 4800000, .max = 6480000 },
  455. .n = { .min = 1, .max = 1 },
  456. .m1 = { .min = 2, .max = 2 },
  457. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  458. .p1 = { .min = 2, .max = 4 },
  459. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  460. };
  461. static const intel_limit_t intel_limits_bxt = {
  462. /* FIXME: find real dot limits */
  463. .dot = { .min = 0, .max = INT_MAX },
  464. .vco = { .min = 4800000, .max = 6700000 },
  465. .n = { .min = 1, .max = 1 },
  466. .m1 = { .min = 2, .max = 2 },
  467. /* FIXME: find real m2 limits */
  468. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  469. .p1 = { .min = 2, .max = 4 },
  470. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  471. };
  472. static bool
  473. needs_modeset(struct drm_crtc_state *state)
  474. {
  475. return drm_atomic_crtc_needs_modeset(state);
  476. }
  477. /**
  478. * Returns whether any output on the specified pipe is of the specified type
  479. */
  480. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  481. {
  482. struct drm_device *dev = crtc->base.dev;
  483. struct intel_encoder *encoder;
  484. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  485. if (encoder->type == type)
  486. return true;
  487. return false;
  488. }
  489. /**
  490. * Returns whether any output on the specified pipe will have the specified
  491. * type after a staged modeset is complete, i.e., the same as
  492. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  493. * encoder->crtc.
  494. */
  495. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  496. int type)
  497. {
  498. struct drm_atomic_state *state = crtc_state->base.state;
  499. struct drm_connector *connector;
  500. struct drm_connector_state *connector_state;
  501. struct intel_encoder *encoder;
  502. int i, num_connectors = 0;
  503. for_each_connector_in_state(state, connector, connector_state, i) {
  504. if (connector_state->crtc != crtc_state->base.crtc)
  505. continue;
  506. num_connectors++;
  507. encoder = to_intel_encoder(connector_state->best_encoder);
  508. if (encoder->type == type)
  509. return true;
  510. }
  511. WARN_ON(num_connectors == 0);
  512. return false;
  513. }
  514. /*
  515. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  516. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  517. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  518. * The helpers' return value is the rate of the clock that is fed to the
  519. * display engine's pipe which can be the above fast dot clock rate or a
  520. * divided-down version of it.
  521. */
  522. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  523. static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
  524. {
  525. clock->m = clock->m2 + 2;
  526. clock->p = clock->p1 * clock->p2;
  527. if (WARN_ON(clock->n == 0 || clock->p == 0))
  528. return 0;
  529. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  530. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  531. return clock->dot;
  532. }
  533. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  534. {
  535. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  536. }
  537. static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
  538. {
  539. clock->m = i9xx_dpll_compute_m(clock);
  540. clock->p = clock->p1 * clock->p2;
  541. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  542. return 0;
  543. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  544. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  545. return clock->dot;
  546. }
  547. static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
  548. {
  549. clock->m = clock->m1 * clock->m2;
  550. clock->p = clock->p1 * clock->p2;
  551. if (WARN_ON(clock->n == 0 || clock->p == 0))
  552. return 0;
  553. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  554. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  555. return clock->dot / 5;
  556. }
  557. int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
  558. {
  559. clock->m = clock->m1 * clock->m2;
  560. clock->p = clock->p1 * clock->p2;
  561. if (WARN_ON(clock->n == 0 || clock->p == 0))
  562. return 0;
  563. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  564. clock->n << 22);
  565. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  566. return clock->dot / 5;
  567. }
  568. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  569. /**
  570. * Returns whether the given set of divisors are valid for a given refclk with
  571. * the given connectors.
  572. */
  573. static bool intel_PLL_is_valid(struct drm_device *dev,
  574. const intel_limit_t *limit,
  575. const intel_clock_t *clock)
  576. {
  577. if (clock->n < limit->n.min || limit->n.max < clock->n)
  578. INTELPllInvalid("n out of range\n");
  579. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  580. INTELPllInvalid("p1 out of range\n");
  581. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  582. INTELPllInvalid("m2 out of range\n");
  583. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  584. INTELPllInvalid("m1 out of range\n");
  585. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
  586. !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
  587. if (clock->m1 <= clock->m2)
  588. INTELPllInvalid("m1 <= m2\n");
  589. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
  590. if (clock->p < limit->p.min || limit->p.max < clock->p)
  591. INTELPllInvalid("p out of range\n");
  592. if (clock->m < limit->m.min || limit->m.max < clock->m)
  593. INTELPllInvalid("m out of range\n");
  594. }
  595. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  596. INTELPllInvalid("vco out of range\n");
  597. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  598. * connector, etc., rather than just a single range.
  599. */
  600. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  601. INTELPllInvalid("dot out of range\n");
  602. return true;
  603. }
  604. static int
  605. i9xx_select_p2_div(const intel_limit_t *limit,
  606. const struct intel_crtc_state *crtc_state,
  607. int target)
  608. {
  609. struct drm_device *dev = crtc_state->base.crtc->dev;
  610. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  611. /*
  612. * For LVDS just rely on its current settings for dual-channel.
  613. * We haven't figured out how to reliably set up different
  614. * single/dual channel state, if we even can.
  615. */
  616. if (intel_is_dual_link_lvds(dev))
  617. return limit->p2.p2_fast;
  618. else
  619. return limit->p2.p2_slow;
  620. } else {
  621. if (target < limit->p2.dot_limit)
  622. return limit->p2.p2_slow;
  623. else
  624. return limit->p2.p2_fast;
  625. }
  626. }
  627. /*
  628. * Returns a set of divisors for the desired target clock with the given
  629. * refclk, or FALSE. The returned values represent the clock equation:
  630. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  631. *
  632. * Target and reference clocks are specified in kHz.
  633. *
  634. * If match_clock is provided, then best_clock P divider must match the P
  635. * divider from @match_clock used for LVDS downclocking.
  636. */
  637. static bool
  638. i9xx_find_best_dpll(const intel_limit_t *limit,
  639. struct intel_crtc_state *crtc_state,
  640. int target, int refclk, intel_clock_t *match_clock,
  641. intel_clock_t *best_clock)
  642. {
  643. struct drm_device *dev = crtc_state->base.crtc->dev;
  644. intel_clock_t clock;
  645. int err = target;
  646. memset(best_clock, 0, sizeof(*best_clock));
  647. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  648. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  649. clock.m1++) {
  650. for (clock.m2 = limit->m2.min;
  651. clock.m2 <= limit->m2.max; clock.m2++) {
  652. if (clock.m2 >= clock.m1)
  653. break;
  654. for (clock.n = limit->n.min;
  655. clock.n <= limit->n.max; clock.n++) {
  656. for (clock.p1 = limit->p1.min;
  657. clock.p1 <= limit->p1.max; clock.p1++) {
  658. int this_err;
  659. i9xx_calc_dpll_params(refclk, &clock);
  660. if (!intel_PLL_is_valid(dev, limit,
  661. &clock))
  662. continue;
  663. if (match_clock &&
  664. clock.p != match_clock->p)
  665. continue;
  666. this_err = abs(clock.dot - target);
  667. if (this_err < err) {
  668. *best_clock = clock;
  669. err = this_err;
  670. }
  671. }
  672. }
  673. }
  674. }
  675. return (err != target);
  676. }
  677. /*
  678. * Returns a set of divisors for the desired target clock with the given
  679. * refclk, or FALSE. The returned values represent the clock equation:
  680. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  681. *
  682. * Target and reference clocks are specified in kHz.
  683. *
  684. * If match_clock is provided, then best_clock P divider must match the P
  685. * divider from @match_clock used for LVDS downclocking.
  686. */
  687. static bool
  688. pnv_find_best_dpll(const intel_limit_t *limit,
  689. struct intel_crtc_state *crtc_state,
  690. int target, int refclk, intel_clock_t *match_clock,
  691. intel_clock_t *best_clock)
  692. {
  693. struct drm_device *dev = crtc_state->base.crtc->dev;
  694. intel_clock_t clock;
  695. int err = target;
  696. memset(best_clock, 0, sizeof(*best_clock));
  697. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  698. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  699. clock.m1++) {
  700. for (clock.m2 = limit->m2.min;
  701. clock.m2 <= limit->m2.max; clock.m2++) {
  702. for (clock.n = limit->n.min;
  703. clock.n <= limit->n.max; clock.n++) {
  704. for (clock.p1 = limit->p1.min;
  705. clock.p1 <= limit->p1.max; clock.p1++) {
  706. int this_err;
  707. pnv_calc_dpll_params(refclk, &clock);
  708. if (!intel_PLL_is_valid(dev, limit,
  709. &clock))
  710. continue;
  711. if (match_clock &&
  712. clock.p != match_clock->p)
  713. continue;
  714. this_err = abs(clock.dot - target);
  715. if (this_err < err) {
  716. *best_clock = clock;
  717. err = this_err;
  718. }
  719. }
  720. }
  721. }
  722. }
  723. return (err != target);
  724. }
  725. /*
  726. * Returns a set of divisors for the desired target clock with the given
  727. * refclk, or FALSE. The returned values represent the clock equation:
  728. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  729. *
  730. * Target and reference clocks are specified in kHz.
  731. *
  732. * If match_clock is provided, then best_clock P divider must match the P
  733. * divider from @match_clock used for LVDS downclocking.
  734. */
  735. static bool
  736. g4x_find_best_dpll(const intel_limit_t *limit,
  737. struct intel_crtc_state *crtc_state,
  738. int target, int refclk, intel_clock_t *match_clock,
  739. intel_clock_t *best_clock)
  740. {
  741. struct drm_device *dev = crtc_state->base.crtc->dev;
  742. intel_clock_t clock;
  743. int max_n;
  744. bool found = false;
  745. /* approximately equals target * 0.00585 */
  746. int err_most = (target >> 8) + (target >> 9);
  747. memset(best_clock, 0, sizeof(*best_clock));
  748. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  749. max_n = limit->n.max;
  750. /* based on hardware requirement, prefer smaller n to precision */
  751. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  752. /* based on hardware requirement, prefere larger m1,m2 */
  753. for (clock.m1 = limit->m1.max;
  754. clock.m1 >= limit->m1.min; clock.m1--) {
  755. for (clock.m2 = limit->m2.max;
  756. clock.m2 >= limit->m2.min; clock.m2--) {
  757. for (clock.p1 = limit->p1.max;
  758. clock.p1 >= limit->p1.min; clock.p1--) {
  759. int this_err;
  760. i9xx_calc_dpll_params(refclk, &clock);
  761. if (!intel_PLL_is_valid(dev, limit,
  762. &clock))
  763. continue;
  764. this_err = abs(clock.dot - target);
  765. if (this_err < err_most) {
  766. *best_clock = clock;
  767. err_most = this_err;
  768. max_n = clock.n;
  769. found = true;
  770. }
  771. }
  772. }
  773. }
  774. }
  775. return found;
  776. }
  777. /*
  778. * Check if the calculated PLL configuration is more optimal compared to the
  779. * best configuration and error found so far. Return the calculated error.
  780. */
  781. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  782. const intel_clock_t *calculated_clock,
  783. const intel_clock_t *best_clock,
  784. unsigned int best_error_ppm,
  785. unsigned int *error_ppm)
  786. {
  787. /*
  788. * For CHV ignore the error and consider only the P value.
  789. * Prefer a bigger P value based on HW requirements.
  790. */
  791. if (IS_CHERRYVIEW(dev)) {
  792. *error_ppm = 0;
  793. return calculated_clock->p > best_clock->p;
  794. }
  795. if (WARN_ON_ONCE(!target_freq))
  796. return false;
  797. *error_ppm = div_u64(1000000ULL *
  798. abs(target_freq - calculated_clock->dot),
  799. target_freq);
  800. /*
  801. * Prefer a better P value over a better (smaller) error if the error
  802. * is small. Ensure this preference for future configurations too by
  803. * setting the error to 0.
  804. */
  805. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  806. *error_ppm = 0;
  807. return true;
  808. }
  809. return *error_ppm + 10 < best_error_ppm;
  810. }
  811. /*
  812. * Returns a set of divisors for the desired target clock with the given
  813. * refclk, or FALSE. The returned values represent the clock equation:
  814. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  815. */
  816. static bool
  817. vlv_find_best_dpll(const intel_limit_t *limit,
  818. struct intel_crtc_state *crtc_state,
  819. int target, int refclk, intel_clock_t *match_clock,
  820. intel_clock_t *best_clock)
  821. {
  822. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  823. struct drm_device *dev = crtc->base.dev;
  824. intel_clock_t clock;
  825. unsigned int bestppm = 1000000;
  826. /* min update 19.2 MHz */
  827. int max_n = min(limit->n.max, refclk / 19200);
  828. bool found = false;
  829. target *= 5; /* fast clock */
  830. memset(best_clock, 0, sizeof(*best_clock));
  831. /* based on hardware requirement, prefer smaller n to precision */
  832. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  833. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  834. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  835. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  836. clock.p = clock.p1 * clock.p2;
  837. /* based on hardware requirement, prefer bigger m1,m2 values */
  838. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  839. unsigned int ppm;
  840. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  841. refclk * clock.m1);
  842. vlv_calc_dpll_params(refclk, &clock);
  843. if (!intel_PLL_is_valid(dev, limit,
  844. &clock))
  845. continue;
  846. if (!vlv_PLL_is_optimal(dev, target,
  847. &clock,
  848. best_clock,
  849. bestppm, &ppm))
  850. continue;
  851. *best_clock = clock;
  852. bestppm = ppm;
  853. found = true;
  854. }
  855. }
  856. }
  857. }
  858. return found;
  859. }
  860. /*
  861. * Returns a set of divisors for the desired target clock with the given
  862. * refclk, or FALSE. The returned values represent the clock equation:
  863. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  864. */
  865. static bool
  866. chv_find_best_dpll(const intel_limit_t *limit,
  867. struct intel_crtc_state *crtc_state,
  868. int target, int refclk, intel_clock_t *match_clock,
  869. intel_clock_t *best_clock)
  870. {
  871. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  872. struct drm_device *dev = crtc->base.dev;
  873. unsigned int best_error_ppm;
  874. intel_clock_t clock;
  875. uint64_t m2;
  876. int found = false;
  877. memset(best_clock, 0, sizeof(*best_clock));
  878. best_error_ppm = 1000000;
  879. /*
  880. * Based on hardware doc, the n always set to 1, and m1 always
  881. * set to 2. If requires to support 200Mhz refclk, we need to
  882. * revisit this because n may not 1 anymore.
  883. */
  884. clock.n = 1, clock.m1 = 2;
  885. target *= 5; /* fast clock */
  886. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  887. for (clock.p2 = limit->p2.p2_fast;
  888. clock.p2 >= limit->p2.p2_slow;
  889. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  890. unsigned int error_ppm;
  891. clock.p = clock.p1 * clock.p2;
  892. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  893. clock.n) << 22, refclk * clock.m1);
  894. if (m2 > INT_MAX/clock.m1)
  895. continue;
  896. clock.m2 = m2;
  897. chv_calc_dpll_params(refclk, &clock);
  898. if (!intel_PLL_is_valid(dev, limit, &clock))
  899. continue;
  900. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  901. best_error_ppm, &error_ppm))
  902. continue;
  903. *best_clock = clock;
  904. best_error_ppm = error_ppm;
  905. found = true;
  906. }
  907. }
  908. return found;
  909. }
  910. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  911. intel_clock_t *best_clock)
  912. {
  913. int refclk = 100000;
  914. const intel_limit_t *limit = &intel_limits_bxt;
  915. return chv_find_best_dpll(limit, crtc_state,
  916. target_clock, refclk, NULL, best_clock);
  917. }
  918. bool intel_crtc_active(struct drm_crtc *crtc)
  919. {
  920. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  921. /* Be paranoid as we can arrive here with only partial
  922. * state retrieved from the hardware during setup.
  923. *
  924. * We can ditch the adjusted_mode.crtc_clock check as soon
  925. * as Haswell has gained clock readout/fastboot support.
  926. *
  927. * We can ditch the crtc->primary->fb check as soon as we can
  928. * properly reconstruct framebuffers.
  929. *
  930. * FIXME: The intel_crtc->active here should be switched to
  931. * crtc->state->active once we have proper CRTC states wired up
  932. * for atomic.
  933. */
  934. return intel_crtc->active && crtc->primary->state->fb &&
  935. intel_crtc->config->base.adjusted_mode.crtc_clock;
  936. }
  937. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  938. enum pipe pipe)
  939. {
  940. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  941. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  942. return intel_crtc->config->cpu_transcoder;
  943. }
  944. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  945. {
  946. struct drm_i915_private *dev_priv = dev->dev_private;
  947. i915_reg_t reg = PIPEDSL(pipe);
  948. u32 line1, line2;
  949. u32 line_mask;
  950. if (IS_GEN2(dev))
  951. line_mask = DSL_LINEMASK_GEN2;
  952. else
  953. line_mask = DSL_LINEMASK_GEN3;
  954. line1 = I915_READ(reg) & line_mask;
  955. msleep(5);
  956. line2 = I915_READ(reg) & line_mask;
  957. return line1 == line2;
  958. }
  959. /*
  960. * intel_wait_for_pipe_off - wait for pipe to turn off
  961. * @crtc: crtc whose pipe to wait for
  962. *
  963. * After disabling a pipe, we can't wait for vblank in the usual way,
  964. * spinning on the vblank interrupt status bit, since we won't actually
  965. * see an interrupt when the pipe is disabled.
  966. *
  967. * On Gen4 and above:
  968. * wait for the pipe register state bit to turn off
  969. *
  970. * Otherwise:
  971. * wait for the display line value to settle (it usually
  972. * ends up stopping at the start of the next frame).
  973. *
  974. */
  975. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  976. {
  977. struct drm_device *dev = crtc->base.dev;
  978. struct drm_i915_private *dev_priv = dev->dev_private;
  979. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  980. enum pipe pipe = crtc->pipe;
  981. if (INTEL_INFO(dev)->gen >= 4) {
  982. i915_reg_t reg = PIPECONF(cpu_transcoder);
  983. /* Wait for the Pipe State to go off */
  984. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  985. 100))
  986. WARN(1, "pipe_off wait timed out\n");
  987. } else {
  988. /* Wait for the display line to settle */
  989. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  990. WARN(1, "pipe_off wait timed out\n");
  991. }
  992. }
  993. /* Only for pre-ILK configs */
  994. void assert_pll(struct drm_i915_private *dev_priv,
  995. enum pipe pipe, bool state)
  996. {
  997. u32 val;
  998. bool cur_state;
  999. val = I915_READ(DPLL(pipe));
  1000. cur_state = !!(val & DPLL_VCO_ENABLE);
  1001. I915_STATE_WARN(cur_state != state,
  1002. "PLL state assertion failure (expected %s, current %s)\n",
  1003. onoff(state), onoff(cur_state));
  1004. }
  1005. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1006. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1007. {
  1008. u32 val;
  1009. bool cur_state;
  1010. mutex_lock(&dev_priv->sb_lock);
  1011. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1012. mutex_unlock(&dev_priv->sb_lock);
  1013. cur_state = val & DSI_PLL_VCO_EN;
  1014. I915_STATE_WARN(cur_state != state,
  1015. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1016. onoff(state), onoff(cur_state));
  1017. }
  1018. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1019. enum pipe pipe, bool state)
  1020. {
  1021. bool cur_state;
  1022. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1023. pipe);
  1024. if (HAS_DDI(dev_priv)) {
  1025. /* DDI does not have a specific FDI_TX register */
  1026. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1027. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1028. } else {
  1029. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1030. cur_state = !!(val & FDI_TX_ENABLE);
  1031. }
  1032. I915_STATE_WARN(cur_state != state,
  1033. "FDI TX state assertion failure (expected %s, current %s)\n",
  1034. onoff(state), onoff(cur_state));
  1035. }
  1036. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1037. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1038. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1039. enum pipe pipe, bool state)
  1040. {
  1041. u32 val;
  1042. bool cur_state;
  1043. val = I915_READ(FDI_RX_CTL(pipe));
  1044. cur_state = !!(val & FDI_RX_ENABLE);
  1045. I915_STATE_WARN(cur_state != state,
  1046. "FDI RX state assertion failure (expected %s, current %s)\n",
  1047. onoff(state), onoff(cur_state));
  1048. }
  1049. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1050. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1051. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1052. enum pipe pipe)
  1053. {
  1054. u32 val;
  1055. /* ILK FDI PLL is always enabled */
  1056. if (INTEL_INFO(dev_priv)->gen == 5)
  1057. return;
  1058. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1059. if (HAS_DDI(dev_priv))
  1060. return;
  1061. val = I915_READ(FDI_TX_CTL(pipe));
  1062. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1063. }
  1064. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1065. enum pipe pipe, bool state)
  1066. {
  1067. u32 val;
  1068. bool cur_state;
  1069. val = I915_READ(FDI_RX_CTL(pipe));
  1070. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1071. I915_STATE_WARN(cur_state != state,
  1072. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1073. onoff(state), onoff(cur_state));
  1074. }
  1075. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1076. enum pipe pipe)
  1077. {
  1078. struct drm_device *dev = dev_priv->dev;
  1079. i915_reg_t pp_reg;
  1080. u32 val;
  1081. enum pipe panel_pipe = PIPE_A;
  1082. bool locked = true;
  1083. if (WARN_ON(HAS_DDI(dev)))
  1084. return;
  1085. if (HAS_PCH_SPLIT(dev)) {
  1086. u32 port_sel;
  1087. pp_reg = PCH_PP_CONTROL;
  1088. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1089. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1090. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1091. panel_pipe = PIPE_B;
  1092. /* XXX: else fix for eDP */
  1093. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1094. /* presumably write lock depends on pipe, not port select */
  1095. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1096. panel_pipe = pipe;
  1097. } else {
  1098. pp_reg = PP_CONTROL;
  1099. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1100. panel_pipe = PIPE_B;
  1101. }
  1102. val = I915_READ(pp_reg);
  1103. if (!(val & PANEL_POWER_ON) ||
  1104. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1105. locked = false;
  1106. I915_STATE_WARN(panel_pipe == pipe && locked,
  1107. "panel assertion failure, pipe %c regs locked\n",
  1108. pipe_name(pipe));
  1109. }
  1110. static void assert_cursor(struct drm_i915_private *dev_priv,
  1111. enum pipe pipe, bool state)
  1112. {
  1113. struct drm_device *dev = dev_priv->dev;
  1114. bool cur_state;
  1115. if (IS_845G(dev) || IS_I865G(dev))
  1116. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1117. else
  1118. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1119. I915_STATE_WARN(cur_state != state,
  1120. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1121. pipe_name(pipe), onoff(state), onoff(cur_state));
  1122. }
  1123. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1124. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1125. void assert_pipe(struct drm_i915_private *dev_priv,
  1126. enum pipe pipe, bool state)
  1127. {
  1128. bool cur_state;
  1129. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1130. pipe);
  1131. enum intel_display_power_domain power_domain;
  1132. /* if we need the pipe quirk it must be always on */
  1133. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1134. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1135. state = true;
  1136. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1137. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1138. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1139. cur_state = !!(val & PIPECONF_ENABLE);
  1140. intel_display_power_put(dev_priv, power_domain);
  1141. } else {
  1142. cur_state = false;
  1143. }
  1144. I915_STATE_WARN(cur_state != state,
  1145. "pipe %c assertion failure (expected %s, current %s)\n",
  1146. pipe_name(pipe), onoff(state), onoff(cur_state));
  1147. }
  1148. static void assert_plane(struct drm_i915_private *dev_priv,
  1149. enum plane plane, bool state)
  1150. {
  1151. u32 val;
  1152. bool cur_state;
  1153. val = I915_READ(DSPCNTR(plane));
  1154. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1155. I915_STATE_WARN(cur_state != state,
  1156. "plane %c assertion failure (expected %s, current %s)\n",
  1157. plane_name(plane), onoff(state), onoff(cur_state));
  1158. }
  1159. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1160. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1161. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1162. enum pipe pipe)
  1163. {
  1164. struct drm_device *dev = dev_priv->dev;
  1165. int i;
  1166. /* Primary planes are fixed to pipes on gen4+ */
  1167. if (INTEL_INFO(dev)->gen >= 4) {
  1168. u32 val = I915_READ(DSPCNTR(pipe));
  1169. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1170. "plane %c assertion failure, should be disabled but not\n",
  1171. plane_name(pipe));
  1172. return;
  1173. }
  1174. /* Need to check both planes against the pipe */
  1175. for_each_pipe(dev_priv, i) {
  1176. u32 val = I915_READ(DSPCNTR(i));
  1177. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1178. DISPPLANE_SEL_PIPE_SHIFT;
  1179. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1180. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1181. plane_name(i), pipe_name(pipe));
  1182. }
  1183. }
  1184. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1185. enum pipe pipe)
  1186. {
  1187. struct drm_device *dev = dev_priv->dev;
  1188. int sprite;
  1189. if (INTEL_INFO(dev)->gen >= 9) {
  1190. for_each_sprite(dev_priv, pipe, sprite) {
  1191. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1192. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1193. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1194. sprite, pipe_name(pipe));
  1195. }
  1196. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1197. for_each_sprite(dev_priv, pipe, sprite) {
  1198. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1199. I915_STATE_WARN(val & SP_ENABLE,
  1200. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1201. sprite_name(pipe, sprite), pipe_name(pipe));
  1202. }
  1203. } else if (INTEL_INFO(dev)->gen >= 7) {
  1204. u32 val = I915_READ(SPRCTL(pipe));
  1205. I915_STATE_WARN(val & SPRITE_ENABLE,
  1206. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1207. plane_name(pipe), pipe_name(pipe));
  1208. } else if (INTEL_INFO(dev)->gen >= 5) {
  1209. u32 val = I915_READ(DVSCNTR(pipe));
  1210. I915_STATE_WARN(val & DVS_ENABLE,
  1211. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1212. plane_name(pipe), pipe_name(pipe));
  1213. }
  1214. }
  1215. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1216. {
  1217. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1218. drm_crtc_vblank_put(crtc);
  1219. }
  1220. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1221. enum pipe pipe)
  1222. {
  1223. u32 val;
  1224. bool enabled;
  1225. val = I915_READ(PCH_TRANSCONF(pipe));
  1226. enabled = !!(val & TRANS_ENABLE);
  1227. I915_STATE_WARN(enabled,
  1228. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1229. pipe_name(pipe));
  1230. }
  1231. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1232. enum pipe pipe, u32 port_sel, u32 val)
  1233. {
  1234. if ((val & DP_PORT_EN) == 0)
  1235. return false;
  1236. if (HAS_PCH_CPT(dev_priv)) {
  1237. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1238. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1239. return false;
  1240. } else if (IS_CHERRYVIEW(dev_priv)) {
  1241. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1242. return false;
  1243. } else {
  1244. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1245. return false;
  1246. }
  1247. return true;
  1248. }
  1249. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1250. enum pipe pipe, u32 val)
  1251. {
  1252. if ((val & SDVO_ENABLE) == 0)
  1253. return false;
  1254. if (HAS_PCH_CPT(dev_priv)) {
  1255. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1256. return false;
  1257. } else if (IS_CHERRYVIEW(dev_priv)) {
  1258. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1259. return false;
  1260. } else {
  1261. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1262. return false;
  1263. }
  1264. return true;
  1265. }
  1266. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1267. enum pipe pipe, u32 val)
  1268. {
  1269. if ((val & LVDS_PORT_EN) == 0)
  1270. return false;
  1271. if (HAS_PCH_CPT(dev_priv)) {
  1272. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1273. return false;
  1274. } else {
  1275. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1276. return false;
  1277. }
  1278. return true;
  1279. }
  1280. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1281. enum pipe pipe, u32 val)
  1282. {
  1283. if ((val & ADPA_DAC_ENABLE) == 0)
  1284. return false;
  1285. if (HAS_PCH_CPT(dev_priv)) {
  1286. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1287. return false;
  1288. } else {
  1289. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1290. return false;
  1291. }
  1292. return true;
  1293. }
  1294. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1295. enum pipe pipe, i915_reg_t reg,
  1296. u32 port_sel)
  1297. {
  1298. u32 val = I915_READ(reg);
  1299. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1300. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1301. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1302. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1303. && (val & DP_PIPEB_SELECT),
  1304. "IBX PCH dp port still using transcoder B\n");
  1305. }
  1306. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1307. enum pipe pipe, i915_reg_t reg)
  1308. {
  1309. u32 val = I915_READ(reg);
  1310. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1311. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1312. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1313. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1314. && (val & SDVO_PIPE_B_SELECT),
  1315. "IBX PCH hdmi port still using transcoder B\n");
  1316. }
  1317. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1318. enum pipe pipe)
  1319. {
  1320. u32 val;
  1321. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1322. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1323. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1324. val = I915_READ(PCH_ADPA);
  1325. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1326. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1327. pipe_name(pipe));
  1328. val = I915_READ(PCH_LVDS);
  1329. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1330. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1331. pipe_name(pipe));
  1332. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1333. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1334. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1335. }
  1336. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1337. const struct intel_crtc_state *pipe_config)
  1338. {
  1339. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1340. enum pipe pipe = crtc->pipe;
  1341. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1342. POSTING_READ(DPLL(pipe));
  1343. udelay(150);
  1344. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1345. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1346. }
  1347. static void vlv_enable_pll(struct intel_crtc *crtc,
  1348. const struct intel_crtc_state *pipe_config)
  1349. {
  1350. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1351. enum pipe pipe = crtc->pipe;
  1352. assert_pipe_disabled(dev_priv, pipe);
  1353. /* PLL is protected by panel, make sure we can write it */
  1354. assert_panel_unlocked(dev_priv, pipe);
  1355. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1356. _vlv_enable_pll(crtc, pipe_config);
  1357. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1358. POSTING_READ(DPLL_MD(pipe));
  1359. }
  1360. static void _chv_enable_pll(struct intel_crtc *crtc,
  1361. const struct intel_crtc_state *pipe_config)
  1362. {
  1363. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1364. enum pipe pipe = crtc->pipe;
  1365. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1366. u32 tmp;
  1367. mutex_lock(&dev_priv->sb_lock);
  1368. /* Enable back the 10bit clock to display controller */
  1369. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1370. tmp |= DPIO_DCLKP_EN;
  1371. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1372. mutex_unlock(&dev_priv->sb_lock);
  1373. /*
  1374. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1375. */
  1376. udelay(1);
  1377. /* Enable PLL */
  1378. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1379. /* Check PLL is locked */
  1380. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1381. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1382. }
  1383. static void chv_enable_pll(struct intel_crtc *crtc,
  1384. const struct intel_crtc_state *pipe_config)
  1385. {
  1386. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1387. enum pipe pipe = crtc->pipe;
  1388. assert_pipe_disabled(dev_priv, pipe);
  1389. /* PLL is protected by panel, make sure we can write it */
  1390. assert_panel_unlocked(dev_priv, pipe);
  1391. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1392. _chv_enable_pll(crtc, pipe_config);
  1393. if (pipe != PIPE_A) {
  1394. /*
  1395. * WaPixelRepeatModeFixForC0:chv
  1396. *
  1397. * DPLLCMD is AWOL. Use chicken bits to propagate
  1398. * the value from DPLLBMD to either pipe B or C.
  1399. */
  1400. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1401. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1402. I915_WRITE(CBR4_VLV, 0);
  1403. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1404. /*
  1405. * DPLLB VGA mode also seems to cause problems.
  1406. * We should always have it disabled.
  1407. */
  1408. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1409. } else {
  1410. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1411. POSTING_READ(DPLL_MD(pipe));
  1412. }
  1413. }
  1414. static int intel_num_dvo_pipes(struct drm_device *dev)
  1415. {
  1416. struct intel_crtc *crtc;
  1417. int count = 0;
  1418. for_each_intel_crtc(dev, crtc)
  1419. count += crtc->base.state->active &&
  1420. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1421. return count;
  1422. }
  1423. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1424. {
  1425. struct drm_device *dev = crtc->base.dev;
  1426. struct drm_i915_private *dev_priv = dev->dev_private;
  1427. i915_reg_t reg = DPLL(crtc->pipe);
  1428. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1429. assert_pipe_disabled(dev_priv, crtc->pipe);
  1430. /* PLL is protected by panel, make sure we can write it */
  1431. if (IS_MOBILE(dev) && !IS_I830(dev))
  1432. assert_panel_unlocked(dev_priv, crtc->pipe);
  1433. /* Enable DVO 2x clock on both PLLs if necessary */
  1434. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1435. /*
  1436. * It appears to be important that we don't enable this
  1437. * for the current pipe before otherwise configuring the
  1438. * PLL. No idea how this should be handled if multiple
  1439. * DVO outputs are enabled simultaneosly.
  1440. */
  1441. dpll |= DPLL_DVO_2X_MODE;
  1442. I915_WRITE(DPLL(!crtc->pipe),
  1443. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1444. }
  1445. /*
  1446. * Apparently we need to have VGA mode enabled prior to changing
  1447. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1448. * dividers, even though the register value does change.
  1449. */
  1450. I915_WRITE(reg, 0);
  1451. I915_WRITE(reg, dpll);
  1452. /* Wait for the clocks to stabilize. */
  1453. POSTING_READ(reg);
  1454. udelay(150);
  1455. if (INTEL_INFO(dev)->gen >= 4) {
  1456. I915_WRITE(DPLL_MD(crtc->pipe),
  1457. crtc->config->dpll_hw_state.dpll_md);
  1458. } else {
  1459. /* The pixel multiplier can only be updated once the
  1460. * DPLL is enabled and the clocks are stable.
  1461. *
  1462. * So write it again.
  1463. */
  1464. I915_WRITE(reg, dpll);
  1465. }
  1466. /* We do this three times for luck */
  1467. I915_WRITE(reg, dpll);
  1468. POSTING_READ(reg);
  1469. udelay(150); /* wait for warmup */
  1470. I915_WRITE(reg, dpll);
  1471. POSTING_READ(reg);
  1472. udelay(150); /* wait for warmup */
  1473. I915_WRITE(reg, dpll);
  1474. POSTING_READ(reg);
  1475. udelay(150); /* wait for warmup */
  1476. }
  1477. /**
  1478. * i9xx_disable_pll - disable a PLL
  1479. * @dev_priv: i915 private structure
  1480. * @pipe: pipe PLL to disable
  1481. *
  1482. * Disable the PLL for @pipe, making sure the pipe is off first.
  1483. *
  1484. * Note! This is for pre-ILK only.
  1485. */
  1486. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1487. {
  1488. struct drm_device *dev = crtc->base.dev;
  1489. struct drm_i915_private *dev_priv = dev->dev_private;
  1490. enum pipe pipe = crtc->pipe;
  1491. /* Disable DVO 2x clock on both PLLs if necessary */
  1492. if (IS_I830(dev) &&
  1493. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1494. !intel_num_dvo_pipes(dev)) {
  1495. I915_WRITE(DPLL(PIPE_B),
  1496. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1497. I915_WRITE(DPLL(PIPE_A),
  1498. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1499. }
  1500. /* Don't disable pipe or pipe PLLs if needed */
  1501. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1502. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1503. return;
  1504. /* Make sure the pipe isn't still relying on us */
  1505. assert_pipe_disabled(dev_priv, pipe);
  1506. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1507. POSTING_READ(DPLL(pipe));
  1508. }
  1509. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1510. {
  1511. u32 val;
  1512. /* Make sure the pipe isn't still relying on us */
  1513. assert_pipe_disabled(dev_priv, pipe);
  1514. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1515. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1516. if (pipe != PIPE_A)
  1517. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1518. I915_WRITE(DPLL(pipe), val);
  1519. POSTING_READ(DPLL(pipe));
  1520. }
  1521. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1522. {
  1523. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1524. u32 val;
  1525. /* Make sure the pipe isn't still relying on us */
  1526. assert_pipe_disabled(dev_priv, pipe);
  1527. val = DPLL_SSC_REF_CLK_CHV |
  1528. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1529. if (pipe != PIPE_A)
  1530. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1531. I915_WRITE(DPLL(pipe), val);
  1532. POSTING_READ(DPLL(pipe));
  1533. mutex_lock(&dev_priv->sb_lock);
  1534. /* Disable 10bit clock to display controller */
  1535. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1536. val &= ~DPIO_DCLKP_EN;
  1537. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1538. mutex_unlock(&dev_priv->sb_lock);
  1539. }
  1540. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1541. struct intel_digital_port *dport,
  1542. unsigned int expected_mask)
  1543. {
  1544. u32 port_mask;
  1545. i915_reg_t dpll_reg;
  1546. switch (dport->port) {
  1547. case PORT_B:
  1548. port_mask = DPLL_PORTB_READY_MASK;
  1549. dpll_reg = DPLL(0);
  1550. break;
  1551. case PORT_C:
  1552. port_mask = DPLL_PORTC_READY_MASK;
  1553. dpll_reg = DPLL(0);
  1554. expected_mask <<= 4;
  1555. break;
  1556. case PORT_D:
  1557. port_mask = DPLL_PORTD_READY_MASK;
  1558. dpll_reg = DPIO_PHY_STATUS;
  1559. break;
  1560. default:
  1561. BUG();
  1562. }
  1563. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1564. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1565. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1566. }
  1567. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1568. enum pipe pipe)
  1569. {
  1570. struct drm_device *dev = dev_priv->dev;
  1571. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1572. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1573. i915_reg_t reg;
  1574. uint32_t val, pipeconf_val;
  1575. /* Make sure PCH DPLL is enabled */
  1576. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1577. /* FDI must be feeding us bits for PCH ports */
  1578. assert_fdi_tx_enabled(dev_priv, pipe);
  1579. assert_fdi_rx_enabled(dev_priv, pipe);
  1580. if (HAS_PCH_CPT(dev)) {
  1581. /* Workaround: Set the timing override bit before enabling the
  1582. * pch transcoder. */
  1583. reg = TRANS_CHICKEN2(pipe);
  1584. val = I915_READ(reg);
  1585. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1586. I915_WRITE(reg, val);
  1587. }
  1588. reg = PCH_TRANSCONF(pipe);
  1589. val = I915_READ(reg);
  1590. pipeconf_val = I915_READ(PIPECONF(pipe));
  1591. if (HAS_PCH_IBX(dev_priv)) {
  1592. /*
  1593. * Make the BPC in transcoder be consistent with
  1594. * that in pipeconf reg. For HDMI we must use 8bpc
  1595. * here for both 8bpc and 12bpc.
  1596. */
  1597. val &= ~PIPECONF_BPC_MASK;
  1598. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1599. val |= PIPECONF_8BPC;
  1600. else
  1601. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1602. }
  1603. val &= ~TRANS_INTERLACE_MASK;
  1604. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1605. if (HAS_PCH_IBX(dev_priv) &&
  1606. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1607. val |= TRANS_LEGACY_INTERLACED_ILK;
  1608. else
  1609. val |= TRANS_INTERLACED;
  1610. else
  1611. val |= TRANS_PROGRESSIVE;
  1612. I915_WRITE(reg, val | TRANS_ENABLE);
  1613. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1614. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1615. }
  1616. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1617. enum transcoder cpu_transcoder)
  1618. {
  1619. u32 val, pipeconf_val;
  1620. /* FDI must be feeding us bits for PCH ports */
  1621. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1622. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1623. /* Workaround: set timing override bit. */
  1624. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1625. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1626. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1627. val = TRANS_ENABLE;
  1628. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1629. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1630. PIPECONF_INTERLACED_ILK)
  1631. val |= TRANS_INTERLACED;
  1632. else
  1633. val |= TRANS_PROGRESSIVE;
  1634. I915_WRITE(LPT_TRANSCONF, val);
  1635. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1636. DRM_ERROR("Failed to enable PCH transcoder\n");
  1637. }
  1638. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1639. enum pipe pipe)
  1640. {
  1641. struct drm_device *dev = dev_priv->dev;
  1642. i915_reg_t reg;
  1643. uint32_t val;
  1644. /* FDI relies on the transcoder */
  1645. assert_fdi_tx_disabled(dev_priv, pipe);
  1646. assert_fdi_rx_disabled(dev_priv, pipe);
  1647. /* Ports must be off as well */
  1648. assert_pch_ports_disabled(dev_priv, pipe);
  1649. reg = PCH_TRANSCONF(pipe);
  1650. val = I915_READ(reg);
  1651. val &= ~TRANS_ENABLE;
  1652. I915_WRITE(reg, val);
  1653. /* wait for PCH transcoder off, transcoder state */
  1654. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1655. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1656. if (HAS_PCH_CPT(dev)) {
  1657. /* Workaround: Clear the timing override chicken bit again. */
  1658. reg = TRANS_CHICKEN2(pipe);
  1659. val = I915_READ(reg);
  1660. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1661. I915_WRITE(reg, val);
  1662. }
  1663. }
  1664. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1665. {
  1666. u32 val;
  1667. val = I915_READ(LPT_TRANSCONF);
  1668. val &= ~TRANS_ENABLE;
  1669. I915_WRITE(LPT_TRANSCONF, val);
  1670. /* wait for PCH transcoder off, transcoder state */
  1671. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1672. DRM_ERROR("Failed to disable PCH transcoder\n");
  1673. /* Workaround: clear timing override bit. */
  1674. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1675. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1676. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1677. }
  1678. /**
  1679. * intel_enable_pipe - enable a pipe, asserting requirements
  1680. * @crtc: crtc responsible for the pipe
  1681. *
  1682. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1683. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1684. */
  1685. static void intel_enable_pipe(struct intel_crtc *crtc)
  1686. {
  1687. struct drm_device *dev = crtc->base.dev;
  1688. struct drm_i915_private *dev_priv = dev->dev_private;
  1689. enum pipe pipe = crtc->pipe;
  1690. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1691. enum pipe pch_transcoder;
  1692. i915_reg_t reg;
  1693. u32 val;
  1694. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1695. assert_planes_disabled(dev_priv, pipe);
  1696. assert_cursor_disabled(dev_priv, pipe);
  1697. assert_sprites_disabled(dev_priv, pipe);
  1698. if (HAS_PCH_LPT(dev_priv))
  1699. pch_transcoder = TRANSCODER_A;
  1700. else
  1701. pch_transcoder = pipe;
  1702. /*
  1703. * A pipe without a PLL won't actually be able to drive bits from
  1704. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1705. * need the check.
  1706. */
  1707. if (HAS_GMCH_DISPLAY(dev_priv))
  1708. if (crtc->config->has_dsi_encoder)
  1709. assert_dsi_pll_enabled(dev_priv);
  1710. else
  1711. assert_pll_enabled(dev_priv, pipe);
  1712. else {
  1713. if (crtc->config->has_pch_encoder) {
  1714. /* if driving the PCH, we need FDI enabled */
  1715. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1716. assert_fdi_tx_pll_enabled(dev_priv,
  1717. (enum pipe) cpu_transcoder);
  1718. }
  1719. /* FIXME: assert CPU port conditions for SNB+ */
  1720. }
  1721. reg = PIPECONF(cpu_transcoder);
  1722. val = I915_READ(reg);
  1723. if (val & PIPECONF_ENABLE) {
  1724. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1725. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1726. return;
  1727. }
  1728. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1729. POSTING_READ(reg);
  1730. /*
  1731. * Until the pipe starts DSL will read as 0, which would cause
  1732. * an apparent vblank timestamp jump, which messes up also the
  1733. * frame count when it's derived from the timestamps. So let's
  1734. * wait for the pipe to start properly before we call
  1735. * drm_crtc_vblank_on()
  1736. */
  1737. if (dev->max_vblank_count == 0 &&
  1738. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1739. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1740. }
  1741. /**
  1742. * intel_disable_pipe - disable a pipe, asserting requirements
  1743. * @crtc: crtc whose pipes is to be disabled
  1744. *
  1745. * Disable the pipe of @crtc, making sure that various hardware
  1746. * specific requirements are met, if applicable, e.g. plane
  1747. * disabled, panel fitter off, etc.
  1748. *
  1749. * Will wait until the pipe has shut down before returning.
  1750. */
  1751. static void intel_disable_pipe(struct intel_crtc *crtc)
  1752. {
  1753. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1754. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1755. enum pipe pipe = crtc->pipe;
  1756. i915_reg_t reg;
  1757. u32 val;
  1758. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1759. /*
  1760. * Make sure planes won't keep trying to pump pixels to us,
  1761. * or we might hang the display.
  1762. */
  1763. assert_planes_disabled(dev_priv, pipe);
  1764. assert_cursor_disabled(dev_priv, pipe);
  1765. assert_sprites_disabled(dev_priv, pipe);
  1766. reg = PIPECONF(cpu_transcoder);
  1767. val = I915_READ(reg);
  1768. if ((val & PIPECONF_ENABLE) == 0)
  1769. return;
  1770. /*
  1771. * Double wide has implications for planes
  1772. * so best keep it disabled when not needed.
  1773. */
  1774. if (crtc->config->double_wide)
  1775. val &= ~PIPECONF_DOUBLE_WIDE;
  1776. /* Don't disable pipe or pipe PLLs if needed */
  1777. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1778. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1779. val &= ~PIPECONF_ENABLE;
  1780. I915_WRITE(reg, val);
  1781. if ((val & PIPECONF_ENABLE) == 0)
  1782. intel_wait_for_pipe_off(crtc);
  1783. }
  1784. static bool need_vtd_wa(struct drm_device *dev)
  1785. {
  1786. #ifdef CONFIG_INTEL_IOMMU
  1787. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1788. return true;
  1789. #endif
  1790. return false;
  1791. }
  1792. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1793. {
  1794. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1795. }
  1796. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1797. uint64_t fb_modifier, unsigned int cpp)
  1798. {
  1799. switch (fb_modifier) {
  1800. case DRM_FORMAT_MOD_NONE:
  1801. return cpp;
  1802. case I915_FORMAT_MOD_X_TILED:
  1803. if (IS_GEN2(dev_priv))
  1804. return 128;
  1805. else
  1806. return 512;
  1807. case I915_FORMAT_MOD_Y_TILED:
  1808. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1809. return 128;
  1810. else
  1811. return 512;
  1812. case I915_FORMAT_MOD_Yf_TILED:
  1813. switch (cpp) {
  1814. case 1:
  1815. return 64;
  1816. case 2:
  1817. case 4:
  1818. return 128;
  1819. case 8:
  1820. case 16:
  1821. return 256;
  1822. default:
  1823. MISSING_CASE(cpp);
  1824. return cpp;
  1825. }
  1826. break;
  1827. default:
  1828. MISSING_CASE(fb_modifier);
  1829. return cpp;
  1830. }
  1831. }
  1832. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1833. uint64_t fb_modifier, unsigned int cpp)
  1834. {
  1835. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1836. return 1;
  1837. else
  1838. return intel_tile_size(dev_priv) /
  1839. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1840. }
  1841. /* Return the tile dimensions in pixel units */
  1842. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1843. unsigned int *tile_width,
  1844. unsigned int *tile_height,
  1845. uint64_t fb_modifier,
  1846. unsigned int cpp)
  1847. {
  1848. unsigned int tile_width_bytes =
  1849. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1850. *tile_width = tile_width_bytes / cpp;
  1851. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1852. }
  1853. unsigned int
  1854. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1855. uint32_t pixel_format, uint64_t fb_modifier)
  1856. {
  1857. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1858. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1859. return ALIGN(height, tile_height);
  1860. }
  1861. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1862. {
  1863. unsigned int size = 0;
  1864. int i;
  1865. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1866. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1867. return size;
  1868. }
  1869. static void
  1870. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1871. const struct drm_framebuffer *fb,
  1872. unsigned int rotation)
  1873. {
  1874. if (intel_rotation_90_or_270(rotation)) {
  1875. *view = i915_ggtt_view_rotated;
  1876. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1877. } else {
  1878. *view = i915_ggtt_view_normal;
  1879. }
  1880. }
  1881. static void
  1882. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  1883. struct drm_framebuffer *fb)
  1884. {
  1885. struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
  1886. unsigned int tile_size, tile_width, tile_height, cpp;
  1887. tile_size = intel_tile_size(dev_priv);
  1888. cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  1889. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1890. fb->modifier[0], cpp);
  1891. info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
  1892. info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
  1893. if (info->pixel_format == DRM_FORMAT_NV12) {
  1894. cpp = drm_format_plane_cpp(fb->pixel_format, 1);
  1895. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1896. fb->modifier[1], cpp);
  1897. info->uv_offset = fb->offsets[1];
  1898. info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
  1899. info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
  1900. }
  1901. }
  1902. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1903. {
  1904. if (INTEL_INFO(dev_priv)->gen >= 9)
  1905. return 256 * 1024;
  1906. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1907. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1908. return 128 * 1024;
  1909. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1910. return 4 * 1024;
  1911. else
  1912. return 0;
  1913. }
  1914. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1915. uint64_t fb_modifier)
  1916. {
  1917. switch (fb_modifier) {
  1918. case DRM_FORMAT_MOD_NONE:
  1919. return intel_linear_alignment(dev_priv);
  1920. case I915_FORMAT_MOD_X_TILED:
  1921. if (INTEL_INFO(dev_priv)->gen >= 9)
  1922. return 256 * 1024;
  1923. return 0;
  1924. case I915_FORMAT_MOD_Y_TILED:
  1925. case I915_FORMAT_MOD_Yf_TILED:
  1926. return 1 * 1024 * 1024;
  1927. default:
  1928. MISSING_CASE(fb_modifier);
  1929. return 0;
  1930. }
  1931. }
  1932. int
  1933. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1934. unsigned int rotation)
  1935. {
  1936. struct drm_device *dev = fb->dev;
  1937. struct drm_i915_private *dev_priv = dev->dev_private;
  1938. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1939. struct i915_ggtt_view view;
  1940. u32 alignment;
  1941. int ret;
  1942. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1943. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  1944. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1945. /* Note that the w/a also requires 64 PTE of padding following the
  1946. * bo. We currently fill all unused PTE with the shadow page and so
  1947. * we should always have valid PTE following the scanout preventing
  1948. * the VT-d warning.
  1949. */
  1950. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1951. alignment = 256 * 1024;
  1952. /*
  1953. * Global gtt pte registers are special registers which actually forward
  1954. * writes to a chunk of system memory. Which means that there is no risk
  1955. * that the register values disappear as soon as we call
  1956. * intel_runtime_pm_put(), so it is correct to wrap only the
  1957. * pin/unpin/fence and not more.
  1958. */
  1959. intel_runtime_pm_get(dev_priv);
  1960. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  1961. &view);
  1962. if (ret)
  1963. goto err_pm;
  1964. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1965. * fence, whereas 965+ only requires a fence if using
  1966. * framebuffer compression. For simplicity, we always install
  1967. * a fence as the cost is not that onerous.
  1968. */
  1969. if (view.type == I915_GGTT_VIEW_NORMAL) {
  1970. ret = i915_gem_object_get_fence(obj);
  1971. if (ret == -EDEADLK) {
  1972. /*
  1973. * -EDEADLK means there are no free fences
  1974. * no pending flips.
  1975. *
  1976. * This is propagated to atomic, but it uses
  1977. * -EDEADLK to force a locking recovery, so
  1978. * change the returned error to -EBUSY.
  1979. */
  1980. ret = -EBUSY;
  1981. goto err_unpin;
  1982. } else if (ret)
  1983. goto err_unpin;
  1984. i915_gem_object_pin_fence(obj);
  1985. }
  1986. intel_runtime_pm_put(dev_priv);
  1987. return 0;
  1988. err_unpin:
  1989. i915_gem_object_unpin_from_display_plane(obj, &view);
  1990. err_pm:
  1991. intel_runtime_pm_put(dev_priv);
  1992. return ret;
  1993. }
  1994. static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1995. {
  1996. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1997. struct i915_ggtt_view view;
  1998. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1999. intel_fill_fb_ggtt_view(&view, fb, rotation);
  2000. if (view.type == I915_GGTT_VIEW_NORMAL)
  2001. i915_gem_object_unpin_fence(obj);
  2002. i915_gem_object_unpin_from_display_plane(obj, &view);
  2003. }
  2004. /*
  2005. * Adjust the tile offset by moving the difference into
  2006. * the x/y offsets.
  2007. *
  2008. * Input tile dimensions and pitch must already be
  2009. * rotated to match x and y, and in pixel units.
  2010. */
  2011. static u32 intel_adjust_tile_offset(int *x, int *y,
  2012. unsigned int tile_width,
  2013. unsigned int tile_height,
  2014. unsigned int tile_size,
  2015. unsigned int pitch_tiles,
  2016. u32 old_offset,
  2017. u32 new_offset)
  2018. {
  2019. unsigned int tiles;
  2020. WARN_ON(old_offset & (tile_size - 1));
  2021. WARN_ON(new_offset & (tile_size - 1));
  2022. WARN_ON(new_offset > old_offset);
  2023. tiles = (old_offset - new_offset) / tile_size;
  2024. *y += tiles / pitch_tiles * tile_height;
  2025. *x += tiles % pitch_tiles * tile_width;
  2026. return new_offset;
  2027. }
  2028. /*
  2029. * Computes the linear offset to the base tile and adjusts
  2030. * x, y. bytes per pixel is assumed to be a power-of-two.
  2031. *
  2032. * In the 90/270 rotated case, x and y are assumed
  2033. * to be already rotated to match the rotated GTT view, and
  2034. * pitch is the tile_height aligned framebuffer height.
  2035. */
  2036. u32 intel_compute_tile_offset(int *x, int *y,
  2037. const struct drm_framebuffer *fb, int plane,
  2038. unsigned int pitch,
  2039. unsigned int rotation)
  2040. {
  2041. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2042. uint64_t fb_modifier = fb->modifier[plane];
  2043. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2044. u32 offset, offset_aligned, alignment;
  2045. alignment = intel_surf_alignment(dev_priv, fb_modifier);
  2046. if (alignment)
  2047. alignment--;
  2048. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2049. unsigned int tile_size, tile_width, tile_height;
  2050. unsigned int tile_rows, tiles, pitch_tiles;
  2051. tile_size = intel_tile_size(dev_priv);
  2052. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2053. fb_modifier, cpp);
  2054. if (intel_rotation_90_or_270(rotation)) {
  2055. pitch_tiles = pitch / tile_height;
  2056. swap(tile_width, tile_height);
  2057. } else {
  2058. pitch_tiles = pitch / (tile_width * cpp);
  2059. }
  2060. tile_rows = *y / tile_height;
  2061. *y %= tile_height;
  2062. tiles = *x / tile_width;
  2063. *x %= tile_width;
  2064. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2065. offset_aligned = offset & ~alignment;
  2066. intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2067. tile_size, pitch_tiles,
  2068. offset, offset_aligned);
  2069. } else {
  2070. offset = *y * pitch + *x * cpp;
  2071. offset_aligned = offset & ~alignment;
  2072. *y = (offset & alignment) / pitch;
  2073. *x = ((offset & alignment) - *y * pitch) / cpp;
  2074. }
  2075. return offset_aligned;
  2076. }
  2077. static int i9xx_format_to_fourcc(int format)
  2078. {
  2079. switch (format) {
  2080. case DISPPLANE_8BPP:
  2081. return DRM_FORMAT_C8;
  2082. case DISPPLANE_BGRX555:
  2083. return DRM_FORMAT_XRGB1555;
  2084. case DISPPLANE_BGRX565:
  2085. return DRM_FORMAT_RGB565;
  2086. default:
  2087. case DISPPLANE_BGRX888:
  2088. return DRM_FORMAT_XRGB8888;
  2089. case DISPPLANE_RGBX888:
  2090. return DRM_FORMAT_XBGR8888;
  2091. case DISPPLANE_BGRX101010:
  2092. return DRM_FORMAT_XRGB2101010;
  2093. case DISPPLANE_RGBX101010:
  2094. return DRM_FORMAT_XBGR2101010;
  2095. }
  2096. }
  2097. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2098. {
  2099. switch (format) {
  2100. case PLANE_CTL_FORMAT_RGB_565:
  2101. return DRM_FORMAT_RGB565;
  2102. default:
  2103. case PLANE_CTL_FORMAT_XRGB_8888:
  2104. if (rgb_order) {
  2105. if (alpha)
  2106. return DRM_FORMAT_ABGR8888;
  2107. else
  2108. return DRM_FORMAT_XBGR8888;
  2109. } else {
  2110. if (alpha)
  2111. return DRM_FORMAT_ARGB8888;
  2112. else
  2113. return DRM_FORMAT_XRGB8888;
  2114. }
  2115. case PLANE_CTL_FORMAT_XRGB_2101010:
  2116. if (rgb_order)
  2117. return DRM_FORMAT_XBGR2101010;
  2118. else
  2119. return DRM_FORMAT_XRGB2101010;
  2120. }
  2121. }
  2122. static bool
  2123. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2124. struct intel_initial_plane_config *plane_config)
  2125. {
  2126. struct drm_device *dev = crtc->base.dev;
  2127. struct drm_i915_private *dev_priv = to_i915(dev);
  2128. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2129. struct drm_i915_gem_object *obj = NULL;
  2130. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2131. struct drm_framebuffer *fb = &plane_config->fb->base;
  2132. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2133. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2134. PAGE_SIZE);
  2135. size_aligned -= base_aligned;
  2136. if (plane_config->size == 0)
  2137. return false;
  2138. /* If the FB is too big, just don't use it since fbdev is not very
  2139. * important and we should probably use that space with FBC or other
  2140. * features. */
  2141. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2142. return false;
  2143. mutex_lock(&dev->struct_mutex);
  2144. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2145. base_aligned,
  2146. base_aligned,
  2147. size_aligned);
  2148. if (!obj) {
  2149. mutex_unlock(&dev->struct_mutex);
  2150. return false;
  2151. }
  2152. obj->tiling_mode = plane_config->tiling;
  2153. if (obj->tiling_mode == I915_TILING_X)
  2154. obj->stride = fb->pitches[0];
  2155. mode_cmd.pixel_format = fb->pixel_format;
  2156. mode_cmd.width = fb->width;
  2157. mode_cmd.height = fb->height;
  2158. mode_cmd.pitches[0] = fb->pitches[0];
  2159. mode_cmd.modifier[0] = fb->modifier[0];
  2160. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2161. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2162. &mode_cmd, obj)) {
  2163. DRM_DEBUG_KMS("intel fb init failed\n");
  2164. goto out_unref_obj;
  2165. }
  2166. mutex_unlock(&dev->struct_mutex);
  2167. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2168. return true;
  2169. out_unref_obj:
  2170. drm_gem_object_unreference(&obj->base);
  2171. mutex_unlock(&dev->struct_mutex);
  2172. return false;
  2173. }
  2174. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2175. static void
  2176. update_state_fb(struct drm_plane *plane)
  2177. {
  2178. if (plane->fb == plane->state->fb)
  2179. return;
  2180. if (plane->state->fb)
  2181. drm_framebuffer_unreference(plane->state->fb);
  2182. plane->state->fb = plane->fb;
  2183. if (plane->state->fb)
  2184. drm_framebuffer_reference(plane->state->fb);
  2185. }
  2186. static void
  2187. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2188. struct intel_initial_plane_config *plane_config)
  2189. {
  2190. struct drm_device *dev = intel_crtc->base.dev;
  2191. struct drm_i915_private *dev_priv = dev->dev_private;
  2192. struct drm_crtc *c;
  2193. struct intel_crtc *i;
  2194. struct drm_i915_gem_object *obj;
  2195. struct drm_plane *primary = intel_crtc->base.primary;
  2196. struct drm_plane_state *plane_state = primary->state;
  2197. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2198. struct intel_plane *intel_plane = to_intel_plane(primary);
  2199. struct intel_plane_state *intel_state =
  2200. to_intel_plane_state(plane_state);
  2201. struct drm_framebuffer *fb;
  2202. if (!plane_config->fb)
  2203. return;
  2204. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2205. fb = &plane_config->fb->base;
  2206. goto valid_fb;
  2207. }
  2208. kfree(plane_config->fb);
  2209. /*
  2210. * Failed to alloc the obj, check to see if we should share
  2211. * an fb with another CRTC instead
  2212. */
  2213. for_each_crtc(dev, c) {
  2214. i = to_intel_crtc(c);
  2215. if (c == &intel_crtc->base)
  2216. continue;
  2217. if (!i->active)
  2218. continue;
  2219. fb = c->primary->fb;
  2220. if (!fb)
  2221. continue;
  2222. obj = intel_fb_obj(fb);
  2223. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2224. drm_framebuffer_reference(fb);
  2225. goto valid_fb;
  2226. }
  2227. }
  2228. /*
  2229. * We've failed to reconstruct the BIOS FB. Current display state
  2230. * indicates that the primary plane is visible, but has a NULL FB,
  2231. * which will lead to problems later if we don't fix it up. The
  2232. * simplest solution is to just disable the primary plane now and
  2233. * pretend the BIOS never had it enabled.
  2234. */
  2235. to_intel_plane_state(plane_state)->visible = false;
  2236. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2237. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2238. intel_plane->disable_plane(primary, &intel_crtc->base);
  2239. return;
  2240. valid_fb:
  2241. plane_state->src_x = 0;
  2242. plane_state->src_y = 0;
  2243. plane_state->src_w = fb->width << 16;
  2244. plane_state->src_h = fb->height << 16;
  2245. plane_state->crtc_x = 0;
  2246. plane_state->crtc_y = 0;
  2247. plane_state->crtc_w = fb->width;
  2248. plane_state->crtc_h = fb->height;
  2249. intel_state->src.x1 = plane_state->src_x;
  2250. intel_state->src.y1 = plane_state->src_y;
  2251. intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
  2252. intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
  2253. intel_state->dst.x1 = plane_state->crtc_x;
  2254. intel_state->dst.y1 = plane_state->crtc_y;
  2255. intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2256. intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2257. obj = intel_fb_obj(fb);
  2258. if (obj->tiling_mode != I915_TILING_NONE)
  2259. dev_priv->preserve_bios_swizzle = true;
  2260. drm_framebuffer_reference(fb);
  2261. primary->fb = primary->state->fb = fb;
  2262. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2263. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2264. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2265. }
  2266. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2267. const struct intel_crtc_state *crtc_state,
  2268. const struct intel_plane_state *plane_state)
  2269. {
  2270. struct drm_device *dev = primary->dev;
  2271. struct drm_i915_private *dev_priv = dev->dev_private;
  2272. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2273. struct drm_framebuffer *fb = plane_state->base.fb;
  2274. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2275. int plane = intel_crtc->plane;
  2276. u32 linear_offset;
  2277. u32 dspcntr;
  2278. i915_reg_t reg = DSPCNTR(plane);
  2279. unsigned int rotation = plane_state->base.rotation;
  2280. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2281. int x = plane_state->src.x1 >> 16;
  2282. int y = plane_state->src.y1 >> 16;
  2283. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2284. dspcntr |= DISPLAY_PLANE_ENABLE;
  2285. if (INTEL_INFO(dev)->gen < 4) {
  2286. if (intel_crtc->pipe == PIPE_B)
  2287. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2288. /* pipesrc and dspsize control the size that is scaled from,
  2289. * which should always be the user's requested size.
  2290. */
  2291. I915_WRITE(DSPSIZE(plane),
  2292. ((crtc_state->pipe_src_h - 1) << 16) |
  2293. (crtc_state->pipe_src_w - 1));
  2294. I915_WRITE(DSPPOS(plane), 0);
  2295. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2296. I915_WRITE(PRIMSIZE(plane),
  2297. ((crtc_state->pipe_src_h - 1) << 16) |
  2298. (crtc_state->pipe_src_w - 1));
  2299. I915_WRITE(PRIMPOS(plane), 0);
  2300. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2301. }
  2302. switch (fb->pixel_format) {
  2303. case DRM_FORMAT_C8:
  2304. dspcntr |= DISPPLANE_8BPP;
  2305. break;
  2306. case DRM_FORMAT_XRGB1555:
  2307. dspcntr |= DISPPLANE_BGRX555;
  2308. break;
  2309. case DRM_FORMAT_RGB565:
  2310. dspcntr |= DISPPLANE_BGRX565;
  2311. break;
  2312. case DRM_FORMAT_XRGB8888:
  2313. dspcntr |= DISPPLANE_BGRX888;
  2314. break;
  2315. case DRM_FORMAT_XBGR8888:
  2316. dspcntr |= DISPPLANE_RGBX888;
  2317. break;
  2318. case DRM_FORMAT_XRGB2101010:
  2319. dspcntr |= DISPPLANE_BGRX101010;
  2320. break;
  2321. case DRM_FORMAT_XBGR2101010:
  2322. dspcntr |= DISPPLANE_RGBX101010;
  2323. break;
  2324. default:
  2325. BUG();
  2326. }
  2327. if (INTEL_INFO(dev)->gen >= 4 &&
  2328. obj->tiling_mode != I915_TILING_NONE)
  2329. dspcntr |= DISPPLANE_TILED;
  2330. if (IS_G4X(dev))
  2331. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2332. linear_offset = y * fb->pitches[0] + x * cpp;
  2333. if (INTEL_INFO(dev)->gen >= 4) {
  2334. intel_crtc->dspaddr_offset =
  2335. intel_compute_tile_offset(&x, &y, fb, 0,
  2336. fb->pitches[0], rotation);
  2337. linear_offset -= intel_crtc->dspaddr_offset;
  2338. } else {
  2339. intel_crtc->dspaddr_offset = linear_offset;
  2340. }
  2341. if (rotation == BIT(DRM_ROTATE_180)) {
  2342. dspcntr |= DISPPLANE_ROTATE_180;
  2343. x += (crtc_state->pipe_src_w - 1);
  2344. y += (crtc_state->pipe_src_h - 1);
  2345. /* Finding the last pixel of the last line of the display
  2346. data and adding to linear_offset*/
  2347. linear_offset +=
  2348. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2349. (crtc_state->pipe_src_w - 1) * cpp;
  2350. }
  2351. intel_crtc->adjusted_x = x;
  2352. intel_crtc->adjusted_y = y;
  2353. I915_WRITE(reg, dspcntr);
  2354. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2355. if (INTEL_INFO(dev)->gen >= 4) {
  2356. I915_WRITE(DSPSURF(plane),
  2357. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2358. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2359. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2360. } else
  2361. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2362. POSTING_READ(reg);
  2363. }
  2364. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2365. struct drm_crtc *crtc)
  2366. {
  2367. struct drm_device *dev = crtc->dev;
  2368. struct drm_i915_private *dev_priv = dev->dev_private;
  2369. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2370. int plane = intel_crtc->plane;
  2371. I915_WRITE(DSPCNTR(plane), 0);
  2372. if (INTEL_INFO(dev_priv)->gen >= 4)
  2373. I915_WRITE(DSPSURF(plane), 0);
  2374. else
  2375. I915_WRITE(DSPADDR(plane), 0);
  2376. POSTING_READ(DSPCNTR(plane));
  2377. }
  2378. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2379. const struct intel_crtc_state *crtc_state,
  2380. const struct intel_plane_state *plane_state)
  2381. {
  2382. struct drm_device *dev = primary->dev;
  2383. struct drm_i915_private *dev_priv = dev->dev_private;
  2384. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2385. struct drm_framebuffer *fb = plane_state->base.fb;
  2386. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2387. int plane = intel_crtc->plane;
  2388. u32 linear_offset;
  2389. u32 dspcntr;
  2390. i915_reg_t reg = DSPCNTR(plane);
  2391. unsigned int rotation = plane_state->base.rotation;
  2392. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2393. int x = plane_state->src.x1 >> 16;
  2394. int y = plane_state->src.y1 >> 16;
  2395. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2396. dspcntr |= DISPLAY_PLANE_ENABLE;
  2397. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2398. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2399. switch (fb->pixel_format) {
  2400. case DRM_FORMAT_C8:
  2401. dspcntr |= DISPPLANE_8BPP;
  2402. break;
  2403. case DRM_FORMAT_RGB565:
  2404. dspcntr |= DISPPLANE_BGRX565;
  2405. break;
  2406. case DRM_FORMAT_XRGB8888:
  2407. dspcntr |= DISPPLANE_BGRX888;
  2408. break;
  2409. case DRM_FORMAT_XBGR8888:
  2410. dspcntr |= DISPPLANE_RGBX888;
  2411. break;
  2412. case DRM_FORMAT_XRGB2101010:
  2413. dspcntr |= DISPPLANE_BGRX101010;
  2414. break;
  2415. case DRM_FORMAT_XBGR2101010:
  2416. dspcntr |= DISPPLANE_RGBX101010;
  2417. break;
  2418. default:
  2419. BUG();
  2420. }
  2421. if (obj->tiling_mode != I915_TILING_NONE)
  2422. dspcntr |= DISPPLANE_TILED;
  2423. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2424. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2425. linear_offset = y * fb->pitches[0] + x * cpp;
  2426. intel_crtc->dspaddr_offset =
  2427. intel_compute_tile_offset(&x, &y, fb, 0,
  2428. fb->pitches[0], rotation);
  2429. linear_offset -= intel_crtc->dspaddr_offset;
  2430. if (rotation == BIT(DRM_ROTATE_180)) {
  2431. dspcntr |= DISPPLANE_ROTATE_180;
  2432. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2433. x += (crtc_state->pipe_src_w - 1);
  2434. y += (crtc_state->pipe_src_h - 1);
  2435. /* Finding the last pixel of the last line of the display
  2436. data and adding to linear_offset*/
  2437. linear_offset +=
  2438. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2439. (crtc_state->pipe_src_w - 1) * cpp;
  2440. }
  2441. }
  2442. intel_crtc->adjusted_x = x;
  2443. intel_crtc->adjusted_y = y;
  2444. I915_WRITE(reg, dspcntr);
  2445. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2446. I915_WRITE(DSPSURF(plane),
  2447. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2448. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2449. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2450. } else {
  2451. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2452. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2453. }
  2454. POSTING_READ(reg);
  2455. }
  2456. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2457. uint64_t fb_modifier, uint32_t pixel_format)
  2458. {
  2459. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2460. return 64;
  2461. } else {
  2462. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2463. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2464. }
  2465. }
  2466. u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
  2467. struct drm_i915_gem_object *obj,
  2468. unsigned int plane)
  2469. {
  2470. struct i915_ggtt_view view;
  2471. struct i915_vma *vma;
  2472. u64 offset;
  2473. intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
  2474. intel_plane->base.state->rotation);
  2475. vma = i915_gem_obj_to_ggtt_view(obj, &view);
  2476. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2477. view.type))
  2478. return -1;
  2479. offset = vma->node.start;
  2480. if (plane == 1) {
  2481. offset += vma->ggtt_view.params.rotated.uv_start_page *
  2482. PAGE_SIZE;
  2483. }
  2484. WARN_ON(upper_32_bits(offset));
  2485. return lower_32_bits(offset);
  2486. }
  2487. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2488. {
  2489. struct drm_device *dev = intel_crtc->base.dev;
  2490. struct drm_i915_private *dev_priv = dev->dev_private;
  2491. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2492. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2493. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2494. }
  2495. /*
  2496. * This function detaches (aka. unbinds) unused scalers in hardware
  2497. */
  2498. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2499. {
  2500. struct intel_crtc_scaler_state *scaler_state;
  2501. int i;
  2502. scaler_state = &intel_crtc->config->scaler_state;
  2503. /* loop through and disable scalers that aren't in use */
  2504. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2505. if (!scaler_state->scalers[i].in_use)
  2506. skl_detach_scaler(intel_crtc, i);
  2507. }
  2508. }
  2509. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2510. {
  2511. switch (pixel_format) {
  2512. case DRM_FORMAT_C8:
  2513. return PLANE_CTL_FORMAT_INDEXED;
  2514. case DRM_FORMAT_RGB565:
  2515. return PLANE_CTL_FORMAT_RGB_565;
  2516. case DRM_FORMAT_XBGR8888:
  2517. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2518. case DRM_FORMAT_XRGB8888:
  2519. return PLANE_CTL_FORMAT_XRGB_8888;
  2520. /*
  2521. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2522. * to be already pre-multiplied. We need to add a knob (or a different
  2523. * DRM_FORMAT) for user-space to configure that.
  2524. */
  2525. case DRM_FORMAT_ABGR8888:
  2526. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2527. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2528. case DRM_FORMAT_ARGB8888:
  2529. return PLANE_CTL_FORMAT_XRGB_8888 |
  2530. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2531. case DRM_FORMAT_XRGB2101010:
  2532. return PLANE_CTL_FORMAT_XRGB_2101010;
  2533. case DRM_FORMAT_XBGR2101010:
  2534. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2535. case DRM_FORMAT_YUYV:
  2536. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2537. case DRM_FORMAT_YVYU:
  2538. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2539. case DRM_FORMAT_UYVY:
  2540. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2541. case DRM_FORMAT_VYUY:
  2542. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2543. default:
  2544. MISSING_CASE(pixel_format);
  2545. }
  2546. return 0;
  2547. }
  2548. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2549. {
  2550. switch (fb_modifier) {
  2551. case DRM_FORMAT_MOD_NONE:
  2552. break;
  2553. case I915_FORMAT_MOD_X_TILED:
  2554. return PLANE_CTL_TILED_X;
  2555. case I915_FORMAT_MOD_Y_TILED:
  2556. return PLANE_CTL_TILED_Y;
  2557. case I915_FORMAT_MOD_Yf_TILED:
  2558. return PLANE_CTL_TILED_YF;
  2559. default:
  2560. MISSING_CASE(fb_modifier);
  2561. }
  2562. return 0;
  2563. }
  2564. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2565. {
  2566. switch (rotation) {
  2567. case BIT(DRM_ROTATE_0):
  2568. break;
  2569. /*
  2570. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2571. * while i915 HW rotation is clockwise, thats why this swapping.
  2572. */
  2573. case BIT(DRM_ROTATE_90):
  2574. return PLANE_CTL_ROTATE_270;
  2575. case BIT(DRM_ROTATE_180):
  2576. return PLANE_CTL_ROTATE_180;
  2577. case BIT(DRM_ROTATE_270):
  2578. return PLANE_CTL_ROTATE_90;
  2579. default:
  2580. MISSING_CASE(rotation);
  2581. }
  2582. return 0;
  2583. }
  2584. static void skylake_update_primary_plane(struct drm_plane *plane,
  2585. const struct intel_crtc_state *crtc_state,
  2586. const struct intel_plane_state *plane_state)
  2587. {
  2588. struct drm_device *dev = plane->dev;
  2589. struct drm_i915_private *dev_priv = dev->dev_private;
  2590. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2591. struct drm_framebuffer *fb = plane_state->base.fb;
  2592. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2593. int pipe = intel_crtc->pipe;
  2594. u32 plane_ctl, stride_div, stride;
  2595. u32 tile_height, plane_offset, plane_size;
  2596. unsigned int rotation = plane_state->base.rotation;
  2597. int x_offset, y_offset;
  2598. u32 surf_addr;
  2599. int scaler_id = plane_state->scaler_id;
  2600. int src_x = plane_state->src.x1 >> 16;
  2601. int src_y = plane_state->src.y1 >> 16;
  2602. int src_w = drm_rect_width(&plane_state->src) >> 16;
  2603. int src_h = drm_rect_height(&plane_state->src) >> 16;
  2604. int dst_x = plane_state->dst.x1;
  2605. int dst_y = plane_state->dst.y1;
  2606. int dst_w = drm_rect_width(&plane_state->dst);
  2607. int dst_h = drm_rect_height(&plane_state->dst);
  2608. plane_ctl = PLANE_CTL_ENABLE |
  2609. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2610. PLANE_CTL_PIPE_CSC_ENABLE;
  2611. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2612. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2613. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2614. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2615. stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2616. fb->pixel_format);
  2617. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2618. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2619. if (intel_rotation_90_or_270(rotation)) {
  2620. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2621. /* stride = Surface height in tiles */
  2622. tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2623. stride = DIV_ROUND_UP(fb->height, tile_height);
  2624. x_offset = stride * tile_height - src_y - src_h;
  2625. y_offset = src_x;
  2626. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2627. } else {
  2628. stride = fb->pitches[0] / stride_div;
  2629. x_offset = src_x;
  2630. y_offset = src_y;
  2631. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2632. }
  2633. plane_offset = y_offset << 16 | x_offset;
  2634. intel_crtc->adjusted_x = x_offset;
  2635. intel_crtc->adjusted_y = y_offset;
  2636. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2637. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2638. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2639. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2640. if (scaler_id >= 0) {
  2641. uint32_t ps_ctrl = 0;
  2642. WARN_ON(!dst_w || !dst_h);
  2643. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2644. crtc_state->scaler_state.scalers[scaler_id].mode;
  2645. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2646. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2647. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2648. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2649. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2650. } else {
  2651. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2652. }
  2653. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2654. POSTING_READ(PLANE_SURF(pipe, 0));
  2655. }
  2656. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2657. struct drm_crtc *crtc)
  2658. {
  2659. struct drm_device *dev = crtc->dev;
  2660. struct drm_i915_private *dev_priv = dev->dev_private;
  2661. int pipe = to_intel_crtc(crtc)->pipe;
  2662. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2663. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2664. POSTING_READ(PLANE_SURF(pipe, 0));
  2665. }
  2666. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2667. static int
  2668. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2669. int x, int y, enum mode_set_atomic state)
  2670. {
  2671. /* Support for kgdboc is disabled, this needs a major rework. */
  2672. DRM_ERROR("legacy panic handler not supported any more.\n");
  2673. return -ENODEV;
  2674. }
  2675. static void intel_complete_page_flips(struct drm_device *dev)
  2676. {
  2677. struct drm_crtc *crtc;
  2678. for_each_crtc(dev, crtc) {
  2679. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2680. enum plane plane = intel_crtc->plane;
  2681. intel_prepare_page_flip(dev, plane);
  2682. intel_finish_page_flip_plane(dev, plane);
  2683. }
  2684. }
  2685. static void intel_update_primary_planes(struct drm_device *dev)
  2686. {
  2687. struct drm_crtc *crtc;
  2688. for_each_crtc(dev, crtc) {
  2689. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2690. struct intel_plane_state *plane_state;
  2691. drm_modeset_lock_crtc(crtc, &plane->base);
  2692. plane_state = to_intel_plane_state(plane->base.state);
  2693. if (plane_state->visible)
  2694. plane->update_plane(&plane->base,
  2695. to_intel_crtc_state(crtc->state),
  2696. plane_state);
  2697. drm_modeset_unlock_crtc(crtc);
  2698. }
  2699. }
  2700. void intel_prepare_reset(struct drm_device *dev)
  2701. {
  2702. /* no reset support for gen2 */
  2703. if (IS_GEN2(dev))
  2704. return;
  2705. /* reset doesn't touch the display */
  2706. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2707. return;
  2708. drm_modeset_lock_all(dev);
  2709. /*
  2710. * Disabling the crtcs gracefully seems nicer. Also the
  2711. * g33 docs say we should at least disable all the planes.
  2712. */
  2713. intel_display_suspend(dev);
  2714. }
  2715. void intel_finish_reset(struct drm_device *dev)
  2716. {
  2717. struct drm_i915_private *dev_priv = to_i915(dev);
  2718. /*
  2719. * Flips in the rings will be nuked by the reset,
  2720. * so complete all pending flips so that user space
  2721. * will get its events and not get stuck.
  2722. */
  2723. intel_complete_page_flips(dev);
  2724. /* no reset support for gen2 */
  2725. if (IS_GEN2(dev))
  2726. return;
  2727. /* reset doesn't touch the display */
  2728. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2729. /*
  2730. * Flips in the rings have been nuked by the reset,
  2731. * so update the base address of all primary
  2732. * planes to the the last fb to make sure we're
  2733. * showing the correct fb after a reset.
  2734. *
  2735. * FIXME: Atomic will make this obsolete since we won't schedule
  2736. * CS-based flips (which might get lost in gpu resets) any more.
  2737. */
  2738. intel_update_primary_planes(dev);
  2739. return;
  2740. }
  2741. /*
  2742. * The display has been reset as well,
  2743. * so need a full re-initialization.
  2744. */
  2745. intel_runtime_pm_disable_interrupts(dev_priv);
  2746. intel_runtime_pm_enable_interrupts(dev_priv);
  2747. intel_modeset_init_hw(dev);
  2748. spin_lock_irq(&dev_priv->irq_lock);
  2749. if (dev_priv->display.hpd_irq_setup)
  2750. dev_priv->display.hpd_irq_setup(dev);
  2751. spin_unlock_irq(&dev_priv->irq_lock);
  2752. intel_display_resume(dev);
  2753. intel_hpd_init(dev_priv);
  2754. drm_modeset_unlock_all(dev);
  2755. }
  2756. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2757. {
  2758. struct drm_device *dev = crtc->dev;
  2759. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2760. unsigned reset_counter;
  2761. bool pending;
  2762. reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
  2763. if (intel_crtc->reset_counter != reset_counter)
  2764. return false;
  2765. spin_lock_irq(&dev->event_lock);
  2766. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2767. spin_unlock_irq(&dev->event_lock);
  2768. return pending;
  2769. }
  2770. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2771. struct intel_crtc_state *old_crtc_state)
  2772. {
  2773. struct drm_device *dev = crtc->base.dev;
  2774. struct drm_i915_private *dev_priv = dev->dev_private;
  2775. struct intel_crtc_state *pipe_config =
  2776. to_intel_crtc_state(crtc->base.state);
  2777. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2778. crtc->base.mode = crtc->base.state->mode;
  2779. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2780. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2781. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2782. /*
  2783. * Update pipe size and adjust fitter if needed: the reason for this is
  2784. * that in compute_mode_changes we check the native mode (not the pfit
  2785. * mode) to see if we can flip rather than do a full mode set. In the
  2786. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2787. * pfit state, we'll end up with a big fb scanned out into the wrong
  2788. * sized surface.
  2789. */
  2790. I915_WRITE(PIPESRC(crtc->pipe),
  2791. ((pipe_config->pipe_src_w - 1) << 16) |
  2792. (pipe_config->pipe_src_h - 1));
  2793. /* on skylake this is done by detaching scalers */
  2794. if (INTEL_INFO(dev)->gen >= 9) {
  2795. skl_detach_scalers(crtc);
  2796. if (pipe_config->pch_pfit.enabled)
  2797. skylake_pfit_enable(crtc);
  2798. } else if (HAS_PCH_SPLIT(dev)) {
  2799. if (pipe_config->pch_pfit.enabled)
  2800. ironlake_pfit_enable(crtc);
  2801. else if (old_crtc_state->pch_pfit.enabled)
  2802. ironlake_pfit_disable(crtc, true);
  2803. }
  2804. }
  2805. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2806. {
  2807. struct drm_device *dev = crtc->dev;
  2808. struct drm_i915_private *dev_priv = dev->dev_private;
  2809. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2810. int pipe = intel_crtc->pipe;
  2811. i915_reg_t reg;
  2812. u32 temp;
  2813. /* enable normal train */
  2814. reg = FDI_TX_CTL(pipe);
  2815. temp = I915_READ(reg);
  2816. if (IS_IVYBRIDGE(dev)) {
  2817. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2818. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2819. } else {
  2820. temp &= ~FDI_LINK_TRAIN_NONE;
  2821. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2822. }
  2823. I915_WRITE(reg, temp);
  2824. reg = FDI_RX_CTL(pipe);
  2825. temp = I915_READ(reg);
  2826. if (HAS_PCH_CPT(dev)) {
  2827. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2828. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2829. } else {
  2830. temp &= ~FDI_LINK_TRAIN_NONE;
  2831. temp |= FDI_LINK_TRAIN_NONE;
  2832. }
  2833. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2834. /* wait one idle pattern time */
  2835. POSTING_READ(reg);
  2836. udelay(1000);
  2837. /* IVB wants error correction enabled */
  2838. if (IS_IVYBRIDGE(dev))
  2839. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2840. FDI_FE_ERRC_ENABLE);
  2841. }
  2842. /* The FDI link training functions for ILK/Ibexpeak. */
  2843. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2844. {
  2845. struct drm_device *dev = crtc->dev;
  2846. struct drm_i915_private *dev_priv = dev->dev_private;
  2847. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2848. int pipe = intel_crtc->pipe;
  2849. i915_reg_t reg;
  2850. u32 temp, tries;
  2851. /* FDI needs bits from pipe first */
  2852. assert_pipe_enabled(dev_priv, pipe);
  2853. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2854. for train result */
  2855. reg = FDI_RX_IMR(pipe);
  2856. temp = I915_READ(reg);
  2857. temp &= ~FDI_RX_SYMBOL_LOCK;
  2858. temp &= ~FDI_RX_BIT_LOCK;
  2859. I915_WRITE(reg, temp);
  2860. I915_READ(reg);
  2861. udelay(150);
  2862. /* enable CPU FDI TX and PCH FDI RX */
  2863. reg = FDI_TX_CTL(pipe);
  2864. temp = I915_READ(reg);
  2865. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2866. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2867. temp &= ~FDI_LINK_TRAIN_NONE;
  2868. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2869. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2870. reg = FDI_RX_CTL(pipe);
  2871. temp = I915_READ(reg);
  2872. temp &= ~FDI_LINK_TRAIN_NONE;
  2873. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2874. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2875. POSTING_READ(reg);
  2876. udelay(150);
  2877. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2878. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2879. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2880. FDI_RX_PHASE_SYNC_POINTER_EN);
  2881. reg = FDI_RX_IIR(pipe);
  2882. for (tries = 0; tries < 5; tries++) {
  2883. temp = I915_READ(reg);
  2884. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2885. if ((temp & FDI_RX_BIT_LOCK)) {
  2886. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2887. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2888. break;
  2889. }
  2890. }
  2891. if (tries == 5)
  2892. DRM_ERROR("FDI train 1 fail!\n");
  2893. /* Train 2 */
  2894. reg = FDI_TX_CTL(pipe);
  2895. temp = I915_READ(reg);
  2896. temp &= ~FDI_LINK_TRAIN_NONE;
  2897. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2898. I915_WRITE(reg, temp);
  2899. reg = FDI_RX_CTL(pipe);
  2900. temp = I915_READ(reg);
  2901. temp &= ~FDI_LINK_TRAIN_NONE;
  2902. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2903. I915_WRITE(reg, temp);
  2904. POSTING_READ(reg);
  2905. udelay(150);
  2906. reg = FDI_RX_IIR(pipe);
  2907. for (tries = 0; tries < 5; tries++) {
  2908. temp = I915_READ(reg);
  2909. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2910. if (temp & FDI_RX_SYMBOL_LOCK) {
  2911. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2912. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2913. break;
  2914. }
  2915. }
  2916. if (tries == 5)
  2917. DRM_ERROR("FDI train 2 fail!\n");
  2918. DRM_DEBUG_KMS("FDI train done\n");
  2919. }
  2920. static const int snb_b_fdi_train_param[] = {
  2921. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2922. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2923. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2924. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2925. };
  2926. /* The FDI link training functions for SNB/Cougarpoint. */
  2927. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2928. {
  2929. struct drm_device *dev = crtc->dev;
  2930. struct drm_i915_private *dev_priv = dev->dev_private;
  2931. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2932. int pipe = intel_crtc->pipe;
  2933. i915_reg_t reg;
  2934. u32 temp, i, retry;
  2935. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2936. for train result */
  2937. reg = FDI_RX_IMR(pipe);
  2938. temp = I915_READ(reg);
  2939. temp &= ~FDI_RX_SYMBOL_LOCK;
  2940. temp &= ~FDI_RX_BIT_LOCK;
  2941. I915_WRITE(reg, temp);
  2942. POSTING_READ(reg);
  2943. udelay(150);
  2944. /* enable CPU FDI TX and PCH FDI RX */
  2945. reg = FDI_TX_CTL(pipe);
  2946. temp = I915_READ(reg);
  2947. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2948. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2949. temp &= ~FDI_LINK_TRAIN_NONE;
  2950. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2951. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2952. /* SNB-B */
  2953. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2954. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2955. I915_WRITE(FDI_RX_MISC(pipe),
  2956. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2957. reg = FDI_RX_CTL(pipe);
  2958. temp = I915_READ(reg);
  2959. if (HAS_PCH_CPT(dev)) {
  2960. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2961. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2962. } else {
  2963. temp &= ~FDI_LINK_TRAIN_NONE;
  2964. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2965. }
  2966. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2967. POSTING_READ(reg);
  2968. udelay(150);
  2969. for (i = 0; i < 4; i++) {
  2970. reg = FDI_TX_CTL(pipe);
  2971. temp = I915_READ(reg);
  2972. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2973. temp |= snb_b_fdi_train_param[i];
  2974. I915_WRITE(reg, temp);
  2975. POSTING_READ(reg);
  2976. udelay(500);
  2977. for (retry = 0; retry < 5; retry++) {
  2978. reg = FDI_RX_IIR(pipe);
  2979. temp = I915_READ(reg);
  2980. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2981. if (temp & FDI_RX_BIT_LOCK) {
  2982. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2983. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2984. break;
  2985. }
  2986. udelay(50);
  2987. }
  2988. if (retry < 5)
  2989. break;
  2990. }
  2991. if (i == 4)
  2992. DRM_ERROR("FDI train 1 fail!\n");
  2993. /* Train 2 */
  2994. reg = FDI_TX_CTL(pipe);
  2995. temp = I915_READ(reg);
  2996. temp &= ~FDI_LINK_TRAIN_NONE;
  2997. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2998. if (IS_GEN6(dev)) {
  2999. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3000. /* SNB-B */
  3001. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3002. }
  3003. I915_WRITE(reg, temp);
  3004. reg = FDI_RX_CTL(pipe);
  3005. temp = I915_READ(reg);
  3006. if (HAS_PCH_CPT(dev)) {
  3007. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3008. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3009. } else {
  3010. temp &= ~FDI_LINK_TRAIN_NONE;
  3011. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3012. }
  3013. I915_WRITE(reg, temp);
  3014. POSTING_READ(reg);
  3015. udelay(150);
  3016. for (i = 0; i < 4; i++) {
  3017. reg = FDI_TX_CTL(pipe);
  3018. temp = I915_READ(reg);
  3019. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3020. temp |= snb_b_fdi_train_param[i];
  3021. I915_WRITE(reg, temp);
  3022. POSTING_READ(reg);
  3023. udelay(500);
  3024. for (retry = 0; retry < 5; retry++) {
  3025. reg = FDI_RX_IIR(pipe);
  3026. temp = I915_READ(reg);
  3027. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3028. if (temp & FDI_RX_SYMBOL_LOCK) {
  3029. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3030. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3031. break;
  3032. }
  3033. udelay(50);
  3034. }
  3035. if (retry < 5)
  3036. break;
  3037. }
  3038. if (i == 4)
  3039. DRM_ERROR("FDI train 2 fail!\n");
  3040. DRM_DEBUG_KMS("FDI train done.\n");
  3041. }
  3042. /* Manual link training for Ivy Bridge A0 parts */
  3043. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3044. {
  3045. struct drm_device *dev = crtc->dev;
  3046. struct drm_i915_private *dev_priv = dev->dev_private;
  3047. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3048. int pipe = intel_crtc->pipe;
  3049. i915_reg_t reg;
  3050. u32 temp, i, j;
  3051. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3052. for train result */
  3053. reg = FDI_RX_IMR(pipe);
  3054. temp = I915_READ(reg);
  3055. temp &= ~FDI_RX_SYMBOL_LOCK;
  3056. temp &= ~FDI_RX_BIT_LOCK;
  3057. I915_WRITE(reg, temp);
  3058. POSTING_READ(reg);
  3059. udelay(150);
  3060. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3061. I915_READ(FDI_RX_IIR(pipe)));
  3062. /* Try each vswing and preemphasis setting twice before moving on */
  3063. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3064. /* disable first in case we need to retry */
  3065. reg = FDI_TX_CTL(pipe);
  3066. temp = I915_READ(reg);
  3067. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3068. temp &= ~FDI_TX_ENABLE;
  3069. I915_WRITE(reg, temp);
  3070. reg = FDI_RX_CTL(pipe);
  3071. temp = I915_READ(reg);
  3072. temp &= ~FDI_LINK_TRAIN_AUTO;
  3073. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3074. temp &= ~FDI_RX_ENABLE;
  3075. I915_WRITE(reg, temp);
  3076. /* enable CPU FDI TX and PCH FDI RX */
  3077. reg = FDI_TX_CTL(pipe);
  3078. temp = I915_READ(reg);
  3079. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3080. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3081. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3082. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3083. temp |= snb_b_fdi_train_param[j/2];
  3084. temp |= FDI_COMPOSITE_SYNC;
  3085. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3086. I915_WRITE(FDI_RX_MISC(pipe),
  3087. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3088. reg = FDI_RX_CTL(pipe);
  3089. temp = I915_READ(reg);
  3090. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3091. temp |= FDI_COMPOSITE_SYNC;
  3092. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3093. POSTING_READ(reg);
  3094. udelay(1); /* should be 0.5us */
  3095. for (i = 0; i < 4; i++) {
  3096. reg = FDI_RX_IIR(pipe);
  3097. temp = I915_READ(reg);
  3098. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3099. if (temp & FDI_RX_BIT_LOCK ||
  3100. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3101. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3102. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3103. i);
  3104. break;
  3105. }
  3106. udelay(1); /* should be 0.5us */
  3107. }
  3108. if (i == 4) {
  3109. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3110. continue;
  3111. }
  3112. /* Train 2 */
  3113. reg = FDI_TX_CTL(pipe);
  3114. temp = I915_READ(reg);
  3115. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3116. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3117. I915_WRITE(reg, temp);
  3118. reg = FDI_RX_CTL(pipe);
  3119. temp = I915_READ(reg);
  3120. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3121. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3122. I915_WRITE(reg, temp);
  3123. POSTING_READ(reg);
  3124. udelay(2); /* should be 1.5us */
  3125. for (i = 0; i < 4; i++) {
  3126. reg = FDI_RX_IIR(pipe);
  3127. temp = I915_READ(reg);
  3128. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3129. if (temp & FDI_RX_SYMBOL_LOCK ||
  3130. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3131. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3132. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3133. i);
  3134. goto train_done;
  3135. }
  3136. udelay(2); /* should be 1.5us */
  3137. }
  3138. if (i == 4)
  3139. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3140. }
  3141. train_done:
  3142. DRM_DEBUG_KMS("FDI train done.\n");
  3143. }
  3144. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3145. {
  3146. struct drm_device *dev = intel_crtc->base.dev;
  3147. struct drm_i915_private *dev_priv = dev->dev_private;
  3148. int pipe = intel_crtc->pipe;
  3149. i915_reg_t reg;
  3150. u32 temp;
  3151. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3152. reg = FDI_RX_CTL(pipe);
  3153. temp = I915_READ(reg);
  3154. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3155. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3156. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3157. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3158. POSTING_READ(reg);
  3159. udelay(200);
  3160. /* Switch from Rawclk to PCDclk */
  3161. temp = I915_READ(reg);
  3162. I915_WRITE(reg, temp | FDI_PCDCLK);
  3163. POSTING_READ(reg);
  3164. udelay(200);
  3165. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3166. reg = FDI_TX_CTL(pipe);
  3167. temp = I915_READ(reg);
  3168. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3169. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3170. POSTING_READ(reg);
  3171. udelay(100);
  3172. }
  3173. }
  3174. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3175. {
  3176. struct drm_device *dev = intel_crtc->base.dev;
  3177. struct drm_i915_private *dev_priv = dev->dev_private;
  3178. int pipe = intel_crtc->pipe;
  3179. i915_reg_t reg;
  3180. u32 temp;
  3181. /* Switch from PCDclk to Rawclk */
  3182. reg = FDI_RX_CTL(pipe);
  3183. temp = I915_READ(reg);
  3184. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3185. /* Disable CPU FDI TX PLL */
  3186. reg = FDI_TX_CTL(pipe);
  3187. temp = I915_READ(reg);
  3188. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3189. POSTING_READ(reg);
  3190. udelay(100);
  3191. reg = FDI_RX_CTL(pipe);
  3192. temp = I915_READ(reg);
  3193. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3194. /* Wait for the clocks to turn off. */
  3195. POSTING_READ(reg);
  3196. udelay(100);
  3197. }
  3198. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3199. {
  3200. struct drm_device *dev = crtc->dev;
  3201. struct drm_i915_private *dev_priv = dev->dev_private;
  3202. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3203. int pipe = intel_crtc->pipe;
  3204. i915_reg_t reg;
  3205. u32 temp;
  3206. /* disable CPU FDI tx and PCH FDI rx */
  3207. reg = FDI_TX_CTL(pipe);
  3208. temp = I915_READ(reg);
  3209. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3210. POSTING_READ(reg);
  3211. reg = FDI_RX_CTL(pipe);
  3212. temp = I915_READ(reg);
  3213. temp &= ~(0x7 << 16);
  3214. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3215. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3216. POSTING_READ(reg);
  3217. udelay(100);
  3218. /* Ironlake workaround, disable clock pointer after downing FDI */
  3219. if (HAS_PCH_IBX(dev))
  3220. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3221. /* still set train pattern 1 */
  3222. reg = FDI_TX_CTL(pipe);
  3223. temp = I915_READ(reg);
  3224. temp &= ~FDI_LINK_TRAIN_NONE;
  3225. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3226. I915_WRITE(reg, temp);
  3227. reg = FDI_RX_CTL(pipe);
  3228. temp = I915_READ(reg);
  3229. if (HAS_PCH_CPT(dev)) {
  3230. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3231. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3232. } else {
  3233. temp &= ~FDI_LINK_TRAIN_NONE;
  3234. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3235. }
  3236. /* BPC in FDI rx is consistent with that in PIPECONF */
  3237. temp &= ~(0x07 << 16);
  3238. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3239. I915_WRITE(reg, temp);
  3240. POSTING_READ(reg);
  3241. udelay(100);
  3242. }
  3243. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3244. {
  3245. struct intel_crtc *crtc;
  3246. /* Note that we don't need to be called with mode_config.lock here
  3247. * as our list of CRTC objects is static for the lifetime of the
  3248. * device and so cannot disappear as we iterate. Similarly, we can
  3249. * happily treat the predicates as racy, atomic checks as userspace
  3250. * cannot claim and pin a new fb without at least acquring the
  3251. * struct_mutex and so serialising with us.
  3252. */
  3253. for_each_intel_crtc(dev, crtc) {
  3254. if (atomic_read(&crtc->unpin_work_count) == 0)
  3255. continue;
  3256. if (crtc->unpin_work)
  3257. intel_wait_for_vblank(dev, crtc->pipe);
  3258. return true;
  3259. }
  3260. return false;
  3261. }
  3262. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3263. {
  3264. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3265. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3266. /* ensure that the unpin work is consistent wrt ->pending. */
  3267. smp_rmb();
  3268. intel_crtc->unpin_work = NULL;
  3269. if (work->event)
  3270. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3271. drm_crtc_vblank_put(&intel_crtc->base);
  3272. wake_up_all(&dev_priv->pending_flip_queue);
  3273. queue_work(dev_priv->wq, &work->work);
  3274. trace_i915_flip_complete(intel_crtc->plane,
  3275. work->pending_flip_obj);
  3276. }
  3277. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3278. {
  3279. struct drm_device *dev = crtc->dev;
  3280. struct drm_i915_private *dev_priv = dev->dev_private;
  3281. long ret;
  3282. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3283. ret = wait_event_interruptible_timeout(
  3284. dev_priv->pending_flip_queue,
  3285. !intel_crtc_has_pending_flip(crtc),
  3286. 60*HZ);
  3287. if (ret < 0)
  3288. return ret;
  3289. if (ret == 0) {
  3290. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3291. spin_lock_irq(&dev->event_lock);
  3292. if (intel_crtc->unpin_work) {
  3293. WARN_ONCE(1, "Removing stuck page flip\n");
  3294. page_flip_completed(intel_crtc);
  3295. }
  3296. spin_unlock_irq(&dev->event_lock);
  3297. }
  3298. return 0;
  3299. }
  3300. static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3301. {
  3302. u32 temp;
  3303. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3304. mutex_lock(&dev_priv->sb_lock);
  3305. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3306. temp |= SBI_SSCCTL_DISABLE;
  3307. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3308. mutex_unlock(&dev_priv->sb_lock);
  3309. }
  3310. /* Program iCLKIP clock to the desired frequency */
  3311. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3312. {
  3313. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3314. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3315. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3316. u32 temp;
  3317. lpt_disable_iclkip(dev_priv);
  3318. /* The iCLK virtual clock root frequency is in MHz,
  3319. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3320. * divisors, it is necessary to divide one by another, so we
  3321. * convert the virtual clock precision to KHz here for higher
  3322. * precision.
  3323. */
  3324. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3325. u32 iclk_virtual_root_freq = 172800 * 1000;
  3326. u32 iclk_pi_range = 64;
  3327. u32 desired_divisor;
  3328. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3329. clock << auxdiv);
  3330. divsel = (desired_divisor / iclk_pi_range) - 2;
  3331. phaseinc = desired_divisor % iclk_pi_range;
  3332. /*
  3333. * Near 20MHz is a corner case which is
  3334. * out of range for the 7-bit divisor
  3335. */
  3336. if (divsel <= 0x7f)
  3337. break;
  3338. }
  3339. /* This should not happen with any sane values */
  3340. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3341. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3342. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3343. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3344. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3345. clock,
  3346. auxdiv,
  3347. divsel,
  3348. phasedir,
  3349. phaseinc);
  3350. mutex_lock(&dev_priv->sb_lock);
  3351. /* Program SSCDIVINTPHASE6 */
  3352. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3353. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3354. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3355. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3356. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3357. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3358. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3359. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3360. /* Program SSCAUXDIV */
  3361. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3362. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3363. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3364. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3365. /* Enable modulator and associated divider */
  3366. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3367. temp &= ~SBI_SSCCTL_DISABLE;
  3368. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3369. mutex_unlock(&dev_priv->sb_lock);
  3370. /* Wait for initialization time */
  3371. udelay(24);
  3372. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3373. }
  3374. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3375. {
  3376. u32 divsel, phaseinc, auxdiv;
  3377. u32 iclk_virtual_root_freq = 172800 * 1000;
  3378. u32 iclk_pi_range = 64;
  3379. u32 desired_divisor;
  3380. u32 temp;
  3381. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3382. return 0;
  3383. mutex_lock(&dev_priv->sb_lock);
  3384. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3385. if (temp & SBI_SSCCTL_DISABLE) {
  3386. mutex_unlock(&dev_priv->sb_lock);
  3387. return 0;
  3388. }
  3389. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3390. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3391. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3392. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3393. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3394. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3395. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3396. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3397. mutex_unlock(&dev_priv->sb_lock);
  3398. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3399. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3400. desired_divisor << auxdiv);
  3401. }
  3402. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3403. enum pipe pch_transcoder)
  3404. {
  3405. struct drm_device *dev = crtc->base.dev;
  3406. struct drm_i915_private *dev_priv = dev->dev_private;
  3407. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3408. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3409. I915_READ(HTOTAL(cpu_transcoder)));
  3410. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3411. I915_READ(HBLANK(cpu_transcoder)));
  3412. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3413. I915_READ(HSYNC(cpu_transcoder)));
  3414. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3415. I915_READ(VTOTAL(cpu_transcoder)));
  3416. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3417. I915_READ(VBLANK(cpu_transcoder)));
  3418. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3419. I915_READ(VSYNC(cpu_transcoder)));
  3420. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3421. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3422. }
  3423. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3424. {
  3425. struct drm_i915_private *dev_priv = dev->dev_private;
  3426. uint32_t temp;
  3427. temp = I915_READ(SOUTH_CHICKEN1);
  3428. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3429. return;
  3430. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3431. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3432. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3433. if (enable)
  3434. temp |= FDI_BC_BIFURCATION_SELECT;
  3435. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3436. I915_WRITE(SOUTH_CHICKEN1, temp);
  3437. POSTING_READ(SOUTH_CHICKEN1);
  3438. }
  3439. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3440. {
  3441. struct drm_device *dev = intel_crtc->base.dev;
  3442. switch (intel_crtc->pipe) {
  3443. case PIPE_A:
  3444. break;
  3445. case PIPE_B:
  3446. if (intel_crtc->config->fdi_lanes > 2)
  3447. cpt_set_fdi_bc_bifurcation(dev, false);
  3448. else
  3449. cpt_set_fdi_bc_bifurcation(dev, true);
  3450. break;
  3451. case PIPE_C:
  3452. cpt_set_fdi_bc_bifurcation(dev, true);
  3453. break;
  3454. default:
  3455. BUG();
  3456. }
  3457. }
  3458. /* Return which DP Port should be selected for Transcoder DP control */
  3459. static enum port
  3460. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3461. {
  3462. struct drm_device *dev = crtc->dev;
  3463. struct intel_encoder *encoder;
  3464. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3465. if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
  3466. encoder->type == INTEL_OUTPUT_EDP)
  3467. return enc_to_dig_port(&encoder->base)->port;
  3468. }
  3469. return -1;
  3470. }
  3471. /*
  3472. * Enable PCH resources required for PCH ports:
  3473. * - PCH PLLs
  3474. * - FDI training & RX/TX
  3475. * - update transcoder timings
  3476. * - DP transcoding bits
  3477. * - transcoder
  3478. */
  3479. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3480. {
  3481. struct drm_device *dev = crtc->dev;
  3482. struct drm_i915_private *dev_priv = dev->dev_private;
  3483. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3484. int pipe = intel_crtc->pipe;
  3485. u32 temp;
  3486. assert_pch_transcoder_disabled(dev_priv, pipe);
  3487. if (IS_IVYBRIDGE(dev))
  3488. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3489. /* Write the TU size bits before fdi link training, so that error
  3490. * detection works. */
  3491. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3492. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3493. /* For PCH output, training FDI link */
  3494. dev_priv->display.fdi_link_train(crtc);
  3495. /* We need to program the right clock selection before writing the pixel
  3496. * mutliplier into the DPLL. */
  3497. if (HAS_PCH_CPT(dev)) {
  3498. u32 sel;
  3499. temp = I915_READ(PCH_DPLL_SEL);
  3500. temp |= TRANS_DPLL_ENABLE(pipe);
  3501. sel = TRANS_DPLLB_SEL(pipe);
  3502. if (intel_crtc->config->shared_dpll ==
  3503. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3504. temp |= sel;
  3505. else
  3506. temp &= ~sel;
  3507. I915_WRITE(PCH_DPLL_SEL, temp);
  3508. }
  3509. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3510. * transcoder, and we actually should do this to not upset any PCH
  3511. * transcoder that already use the clock when we share it.
  3512. *
  3513. * Note that enable_shared_dpll tries to do the right thing, but
  3514. * get_shared_dpll unconditionally resets the pll - we need that to have
  3515. * the right LVDS enable sequence. */
  3516. intel_enable_shared_dpll(intel_crtc);
  3517. /* set transcoder timing, panel must allow it */
  3518. assert_panel_unlocked(dev_priv, pipe);
  3519. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3520. intel_fdi_normal_train(crtc);
  3521. /* For PCH DP, enable TRANS_DP_CTL */
  3522. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3523. const struct drm_display_mode *adjusted_mode =
  3524. &intel_crtc->config->base.adjusted_mode;
  3525. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3526. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3527. temp = I915_READ(reg);
  3528. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3529. TRANS_DP_SYNC_MASK |
  3530. TRANS_DP_BPC_MASK);
  3531. temp |= TRANS_DP_OUTPUT_ENABLE;
  3532. temp |= bpc << 9; /* same format but at 11:9 */
  3533. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3534. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3535. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3536. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3537. switch (intel_trans_dp_port_sel(crtc)) {
  3538. case PORT_B:
  3539. temp |= TRANS_DP_PORT_SEL_B;
  3540. break;
  3541. case PORT_C:
  3542. temp |= TRANS_DP_PORT_SEL_C;
  3543. break;
  3544. case PORT_D:
  3545. temp |= TRANS_DP_PORT_SEL_D;
  3546. break;
  3547. default:
  3548. BUG();
  3549. }
  3550. I915_WRITE(reg, temp);
  3551. }
  3552. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3553. }
  3554. static void lpt_pch_enable(struct drm_crtc *crtc)
  3555. {
  3556. struct drm_device *dev = crtc->dev;
  3557. struct drm_i915_private *dev_priv = dev->dev_private;
  3558. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3559. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3560. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3561. lpt_program_iclkip(crtc);
  3562. /* Set transcoder timing. */
  3563. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3564. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3565. }
  3566. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3567. {
  3568. struct drm_i915_private *dev_priv = dev->dev_private;
  3569. i915_reg_t dslreg = PIPEDSL(pipe);
  3570. u32 temp;
  3571. temp = I915_READ(dslreg);
  3572. udelay(500);
  3573. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3574. if (wait_for(I915_READ(dslreg) != temp, 5))
  3575. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3576. }
  3577. }
  3578. static int
  3579. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3580. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3581. int src_w, int src_h, int dst_w, int dst_h)
  3582. {
  3583. struct intel_crtc_scaler_state *scaler_state =
  3584. &crtc_state->scaler_state;
  3585. struct intel_crtc *intel_crtc =
  3586. to_intel_crtc(crtc_state->base.crtc);
  3587. int need_scaling;
  3588. need_scaling = intel_rotation_90_or_270(rotation) ?
  3589. (src_h != dst_w || src_w != dst_h):
  3590. (src_w != dst_w || src_h != dst_h);
  3591. /*
  3592. * if plane is being disabled or scaler is no more required or force detach
  3593. * - free scaler binded to this plane/crtc
  3594. * - in order to do this, update crtc->scaler_usage
  3595. *
  3596. * Here scaler state in crtc_state is set free so that
  3597. * scaler can be assigned to other user. Actual register
  3598. * update to free the scaler is done in plane/panel-fit programming.
  3599. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3600. */
  3601. if (force_detach || !need_scaling) {
  3602. if (*scaler_id >= 0) {
  3603. scaler_state->scaler_users &= ~(1 << scaler_user);
  3604. scaler_state->scalers[*scaler_id].in_use = 0;
  3605. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3606. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3607. intel_crtc->pipe, scaler_user, *scaler_id,
  3608. scaler_state->scaler_users);
  3609. *scaler_id = -1;
  3610. }
  3611. return 0;
  3612. }
  3613. /* range checks */
  3614. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3615. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3616. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3617. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3618. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3619. "size is out of scaler range\n",
  3620. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3621. return -EINVAL;
  3622. }
  3623. /* mark this plane as a scaler user in crtc_state */
  3624. scaler_state->scaler_users |= (1 << scaler_user);
  3625. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3626. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3627. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3628. scaler_state->scaler_users);
  3629. return 0;
  3630. }
  3631. /**
  3632. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3633. *
  3634. * @state: crtc's scaler state
  3635. *
  3636. * Return
  3637. * 0 - scaler_usage updated successfully
  3638. * error - requested scaling cannot be supported or other error condition
  3639. */
  3640. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3641. {
  3642. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3643. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3644. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3645. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3646. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3647. &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
  3648. state->pipe_src_w, state->pipe_src_h,
  3649. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3650. }
  3651. /**
  3652. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3653. *
  3654. * @state: crtc's scaler state
  3655. * @plane_state: atomic plane state to update
  3656. *
  3657. * Return
  3658. * 0 - scaler_usage updated successfully
  3659. * error - requested scaling cannot be supported or other error condition
  3660. */
  3661. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3662. struct intel_plane_state *plane_state)
  3663. {
  3664. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3665. struct intel_plane *intel_plane =
  3666. to_intel_plane(plane_state->base.plane);
  3667. struct drm_framebuffer *fb = plane_state->base.fb;
  3668. int ret;
  3669. bool force_detach = !fb || !plane_state->visible;
  3670. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3671. intel_plane->base.base.id, intel_crtc->pipe,
  3672. drm_plane_index(&intel_plane->base));
  3673. ret = skl_update_scaler(crtc_state, force_detach,
  3674. drm_plane_index(&intel_plane->base),
  3675. &plane_state->scaler_id,
  3676. plane_state->base.rotation,
  3677. drm_rect_width(&plane_state->src) >> 16,
  3678. drm_rect_height(&plane_state->src) >> 16,
  3679. drm_rect_width(&plane_state->dst),
  3680. drm_rect_height(&plane_state->dst));
  3681. if (ret || plane_state->scaler_id < 0)
  3682. return ret;
  3683. /* check colorkey */
  3684. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3685. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3686. intel_plane->base.base.id);
  3687. return -EINVAL;
  3688. }
  3689. /* Check src format */
  3690. switch (fb->pixel_format) {
  3691. case DRM_FORMAT_RGB565:
  3692. case DRM_FORMAT_XBGR8888:
  3693. case DRM_FORMAT_XRGB8888:
  3694. case DRM_FORMAT_ABGR8888:
  3695. case DRM_FORMAT_ARGB8888:
  3696. case DRM_FORMAT_XRGB2101010:
  3697. case DRM_FORMAT_XBGR2101010:
  3698. case DRM_FORMAT_YUYV:
  3699. case DRM_FORMAT_YVYU:
  3700. case DRM_FORMAT_UYVY:
  3701. case DRM_FORMAT_VYUY:
  3702. break;
  3703. default:
  3704. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3705. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3706. return -EINVAL;
  3707. }
  3708. return 0;
  3709. }
  3710. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3711. {
  3712. int i;
  3713. for (i = 0; i < crtc->num_scalers; i++)
  3714. skl_detach_scaler(crtc, i);
  3715. }
  3716. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3717. {
  3718. struct drm_device *dev = crtc->base.dev;
  3719. struct drm_i915_private *dev_priv = dev->dev_private;
  3720. int pipe = crtc->pipe;
  3721. struct intel_crtc_scaler_state *scaler_state =
  3722. &crtc->config->scaler_state;
  3723. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3724. if (crtc->config->pch_pfit.enabled) {
  3725. int id;
  3726. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3727. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3728. return;
  3729. }
  3730. id = scaler_state->scaler_id;
  3731. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3732. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3733. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3734. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3735. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3736. }
  3737. }
  3738. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3739. {
  3740. struct drm_device *dev = crtc->base.dev;
  3741. struct drm_i915_private *dev_priv = dev->dev_private;
  3742. int pipe = crtc->pipe;
  3743. if (crtc->config->pch_pfit.enabled) {
  3744. /* Force use of hard-coded filter coefficients
  3745. * as some pre-programmed values are broken,
  3746. * e.g. x201.
  3747. */
  3748. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3749. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3750. PF_PIPE_SEL_IVB(pipe));
  3751. else
  3752. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3753. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3754. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3755. }
  3756. }
  3757. void hsw_enable_ips(struct intel_crtc *crtc)
  3758. {
  3759. struct drm_device *dev = crtc->base.dev;
  3760. struct drm_i915_private *dev_priv = dev->dev_private;
  3761. if (!crtc->config->ips_enabled)
  3762. return;
  3763. /*
  3764. * We can only enable IPS after we enable a plane and wait for a vblank
  3765. * This function is called from post_plane_update, which is run after
  3766. * a vblank wait.
  3767. */
  3768. assert_plane_enabled(dev_priv, crtc->plane);
  3769. if (IS_BROADWELL(dev)) {
  3770. mutex_lock(&dev_priv->rps.hw_lock);
  3771. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3772. mutex_unlock(&dev_priv->rps.hw_lock);
  3773. /* Quoting Art Runyan: "its not safe to expect any particular
  3774. * value in IPS_CTL bit 31 after enabling IPS through the
  3775. * mailbox." Moreover, the mailbox may return a bogus state,
  3776. * so we need to just enable it and continue on.
  3777. */
  3778. } else {
  3779. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3780. /* The bit only becomes 1 in the next vblank, so this wait here
  3781. * is essentially intel_wait_for_vblank. If we don't have this
  3782. * and don't wait for vblanks until the end of crtc_enable, then
  3783. * the HW state readout code will complain that the expected
  3784. * IPS_CTL value is not the one we read. */
  3785. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3786. DRM_ERROR("Timed out waiting for IPS enable\n");
  3787. }
  3788. }
  3789. void hsw_disable_ips(struct intel_crtc *crtc)
  3790. {
  3791. struct drm_device *dev = crtc->base.dev;
  3792. struct drm_i915_private *dev_priv = dev->dev_private;
  3793. if (!crtc->config->ips_enabled)
  3794. return;
  3795. assert_plane_enabled(dev_priv, crtc->plane);
  3796. if (IS_BROADWELL(dev)) {
  3797. mutex_lock(&dev_priv->rps.hw_lock);
  3798. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3799. mutex_unlock(&dev_priv->rps.hw_lock);
  3800. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3801. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3802. DRM_ERROR("Timed out waiting for IPS disable\n");
  3803. } else {
  3804. I915_WRITE(IPS_CTL, 0);
  3805. POSTING_READ(IPS_CTL);
  3806. }
  3807. /* We need to wait for a vblank before we can disable the plane. */
  3808. intel_wait_for_vblank(dev, crtc->pipe);
  3809. }
  3810. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3811. {
  3812. if (intel_crtc->overlay) {
  3813. struct drm_device *dev = intel_crtc->base.dev;
  3814. struct drm_i915_private *dev_priv = dev->dev_private;
  3815. mutex_lock(&dev->struct_mutex);
  3816. dev_priv->mm.interruptible = false;
  3817. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3818. dev_priv->mm.interruptible = true;
  3819. mutex_unlock(&dev->struct_mutex);
  3820. }
  3821. /* Let userspace switch the overlay on again. In most cases userspace
  3822. * has to recompute where to put it anyway.
  3823. */
  3824. }
  3825. /**
  3826. * intel_post_enable_primary - Perform operations after enabling primary plane
  3827. * @crtc: the CRTC whose primary plane was just enabled
  3828. *
  3829. * Performs potentially sleeping operations that must be done after the primary
  3830. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3831. * called due to an explicit primary plane update, or due to an implicit
  3832. * re-enable that is caused when a sprite plane is updated to no longer
  3833. * completely hide the primary plane.
  3834. */
  3835. static void
  3836. intel_post_enable_primary(struct drm_crtc *crtc)
  3837. {
  3838. struct drm_device *dev = crtc->dev;
  3839. struct drm_i915_private *dev_priv = dev->dev_private;
  3840. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3841. int pipe = intel_crtc->pipe;
  3842. /*
  3843. * FIXME IPS should be fine as long as one plane is
  3844. * enabled, but in practice it seems to have problems
  3845. * when going from primary only to sprite only and vice
  3846. * versa.
  3847. */
  3848. hsw_enable_ips(intel_crtc);
  3849. /*
  3850. * Gen2 reports pipe underruns whenever all planes are disabled.
  3851. * So don't enable underrun reporting before at least some planes
  3852. * are enabled.
  3853. * FIXME: Need to fix the logic to work when we turn off all planes
  3854. * but leave the pipe running.
  3855. */
  3856. if (IS_GEN2(dev))
  3857. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3858. /* Underruns don't always raise interrupts, so check manually. */
  3859. intel_check_cpu_fifo_underruns(dev_priv);
  3860. intel_check_pch_fifo_underruns(dev_priv);
  3861. }
  3862. /* FIXME move all this to pre_plane_update() with proper state tracking */
  3863. static void
  3864. intel_pre_disable_primary(struct drm_crtc *crtc)
  3865. {
  3866. struct drm_device *dev = crtc->dev;
  3867. struct drm_i915_private *dev_priv = dev->dev_private;
  3868. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3869. int pipe = intel_crtc->pipe;
  3870. /*
  3871. * Gen2 reports pipe underruns whenever all planes are disabled.
  3872. * So diasble underrun reporting before all the planes get disabled.
  3873. * FIXME: Need to fix the logic to work when we turn off all planes
  3874. * but leave the pipe running.
  3875. */
  3876. if (IS_GEN2(dev))
  3877. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3878. /*
  3879. * FIXME IPS should be fine as long as one plane is
  3880. * enabled, but in practice it seems to have problems
  3881. * when going from primary only to sprite only and vice
  3882. * versa.
  3883. */
  3884. hsw_disable_ips(intel_crtc);
  3885. }
  3886. /* FIXME get rid of this and use pre_plane_update */
  3887. static void
  3888. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  3889. {
  3890. struct drm_device *dev = crtc->dev;
  3891. struct drm_i915_private *dev_priv = dev->dev_private;
  3892. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3893. int pipe = intel_crtc->pipe;
  3894. intel_pre_disable_primary(crtc);
  3895. /*
  3896. * Vblank time updates from the shadow to live plane control register
  3897. * are blocked if the memory self-refresh mode is active at that
  3898. * moment. So to make sure the plane gets truly disabled, disable
  3899. * first the self-refresh mode. The self-refresh enable bit in turn
  3900. * will be checked/applied by the HW only at the next frame start
  3901. * event which is after the vblank start event, so we need to have a
  3902. * wait-for-vblank between disabling the plane and the pipe.
  3903. */
  3904. if (HAS_GMCH_DISPLAY(dev)) {
  3905. intel_set_memory_cxsr(dev_priv, false);
  3906. dev_priv->wm.vlv.cxsr = false;
  3907. intel_wait_for_vblank(dev, pipe);
  3908. }
  3909. }
  3910. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  3911. {
  3912. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3913. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3914. struct intel_crtc_state *pipe_config =
  3915. to_intel_crtc_state(crtc->base.state);
  3916. struct drm_device *dev = crtc->base.dev;
  3917. struct drm_plane *primary = crtc->base.primary;
  3918. struct drm_plane_state *old_pri_state =
  3919. drm_atomic_get_existing_plane_state(old_state, primary);
  3920. intel_frontbuffer_flip(dev, pipe_config->fb_bits);
  3921. crtc->wm.cxsr_allowed = true;
  3922. if (pipe_config->update_wm_post && pipe_config->base.active)
  3923. intel_update_watermarks(&crtc->base);
  3924. if (old_pri_state) {
  3925. struct intel_plane_state *primary_state =
  3926. to_intel_plane_state(primary->state);
  3927. struct intel_plane_state *old_primary_state =
  3928. to_intel_plane_state(old_pri_state);
  3929. intel_fbc_post_update(crtc);
  3930. if (primary_state->visible &&
  3931. (needs_modeset(&pipe_config->base) ||
  3932. !old_primary_state->visible))
  3933. intel_post_enable_primary(&crtc->base);
  3934. }
  3935. }
  3936. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  3937. {
  3938. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3939. struct drm_device *dev = crtc->base.dev;
  3940. struct drm_i915_private *dev_priv = dev->dev_private;
  3941. struct intel_crtc_state *pipe_config =
  3942. to_intel_crtc_state(crtc->base.state);
  3943. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3944. struct drm_plane *primary = crtc->base.primary;
  3945. struct drm_plane_state *old_pri_state =
  3946. drm_atomic_get_existing_plane_state(old_state, primary);
  3947. bool modeset = needs_modeset(&pipe_config->base);
  3948. if (old_pri_state) {
  3949. struct intel_plane_state *primary_state =
  3950. to_intel_plane_state(primary->state);
  3951. struct intel_plane_state *old_primary_state =
  3952. to_intel_plane_state(old_pri_state);
  3953. intel_fbc_pre_update(crtc);
  3954. if (old_primary_state->visible &&
  3955. (modeset || !primary_state->visible))
  3956. intel_pre_disable_primary(&crtc->base);
  3957. }
  3958. if (pipe_config->disable_cxsr) {
  3959. crtc->wm.cxsr_allowed = false;
  3960. /*
  3961. * Vblank time updates from the shadow to live plane control register
  3962. * are blocked if the memory self-refresh mode is active at that
  3963. * moment. So to make sure the plane gets truly disabled, disable
  3964. * first the self-refresh mode. The self-refresh enable bit in turn
  3965. * will be checked/applied by the HW only at the next frame start
  3966. * event which is after the vblank start event, so we need to have a
  3967. * wait-for-vblank between disabling the plane and the pipe.
  3968. */
  3969. if (old_crtc_state->base.active) {
  3970. intel_set_memory_cxsr(dev_priv, false);
  3971. dev_priv->wm.vlv.cxsr = false;
  3972. intel_wait_for_vblank(dev, crtc->pipe);
  3973. }
  3974. }
  3975. /*
  3976. * IVB workaround: must disable low power watermarks for at least
  3977. * one frame before enabling scaling. LP watermarks can be re-enabled
  3978. * when scaling is disabled.
  3979. *
  3980. * WaCxSRDisabledForSpriteScaling:ivb
  3981. */
  3982. if (pipe_config->disable_lp_wm) {
  3983. ilk_disable_lp_wm(dev);
  3984. intel_wait_for_vblank(dev, crtc->pipe);
  3985. }
  3986. /*
  3987. * If we're doing a modeset, we're done. No need to do any pre-vblank
  3988. * watermark programming here.
  3989. */
  3990. if (needs_modeset(&pipe_config->base))
  3991. return;
  3992. /*
  3993. * For platforms that support atomic watermarks, program the
  3994. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  3995. * will be the intermediate values that are safe for both pre- and
  3996. * post- vblank; when vblank happens, the 'active' values will be set
  3997. * to the final 'target' values and we'll do this again to get the
  3998. * optimal watermarks. For gen9+ platforms, the values we program here
  3999. * will be the final target values which will get automatically latched
  4000. * at vblank time; no further programming will be necessary.
  4001. *
  4002. * If a platform hasn't been transitioned to atomic watermarks yet,
  4003. * we'll continue to update watermarks the old way, if flags tell
  4004. * us to.
  4005. */
  4006. if (dev_priv->display.initial_watermarks != NULL)
  4007. dev_priv->display.initial_watermarks(pipe_config);
  4008. else if (pipe_config->update_wm_pre)
  4009. intel_update_watermarks(&crtc->base);
  4010. }
  4011. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4012. {
  4013. struct drm_device *dev = crtc->dev;
  4014. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4015. struct drm_plane *p;
  4016. int pipe = intel_crtc->pipe;
  4017. intel_crtc_dpms_overlay_disable(intel_crtc);
  4018. drm_for_each_plane_mask(p, dev, plane_mask)
  4019. to_intel_plane(p)->disable_plane(p, crtc);
  4020. /*
  4021. * FIXME: Once we grow proper nuclear flip support out of this we need
  4022. * to compute the mask of flip planes precisely. For the time being
  4023. * consider this a flip to a NULL plane.
  4024. */
  4025. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4026. }
  4027. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4028. {
  4029. struct drm_device *dev = crtc->dev;
  4030. struct drm_i915_private *dev_priv = dev->dev_private;
  4031. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4032. struct intel_encoder *encoder;
  4033. int pipe = intel_crtc->pipe;
  4034. struct intel_crtc_state *pipe_config =
  4035. to_intel_crtc_state(crtc->state);
  4036. if (WARN_ON(intel_crtc->active))
  4037. return;
  4038. /*
  4039. * Sometimes spurious CPU pipe underruns happen during FDI
  4040. * training, at least with VGA+HDMI cloning. Suppress them.
  4041. *
  4042. * On ILK we get an occasional spurious CPU pipe underruns
  4043. * between eDP port A enable and vdd enable. Also PCH port
  4044. * enable seems to result in the occasional CPU pipe underrun.
  4045. *
  4046. * Spurious PCH underruns also occur during PCH enabling.
  4047. */
  4048. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4049. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4050. if (intel_crtc->config->has_pch_encoder)
  4051. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4052. if (intel_crtc->config->has_pch_encoder)
  4053. intel_prepare_shared_dpll(intel_crtc);
  4054. if (intel_crtc->config->has_dp_encoder)
  4055. intel_dp_set_m_n(intel_crtc, M1_N1);
  4056. intel_set_pipe_timings(intel_crtc);
  4057. intel_set_pipe_src_size(intel_crtc);
  4058. if (intel_crtc->config->has_pch_encoder) {
  4059. intel_cpu_transcoder_set_m_n(intel_crtc,
  4060. &intel_crtc->config->fdi_m_n, NULL);
  4061. }
  4062. ironlake_set_pipeconf(crtc);
  4063. intel_crtc->active = true;
  4064. for_each_encoder_on_crtc(dev, crtc, encoder)
  4065. if (encoder->pre_enable)
  4066. encoder->pre_enable(encoder);
  4067. if (intel_crtc->config->has_pch_encoder) {
  4068. /* Note: FDI PLL enabling _must_ be done before we enable the
  4069. * cpu pipes, hence this is separate from all the other fdi/pch
  4070. * enabling. */
  4071. ironlake_fdi_pll_enable(intel_crtc);
  4072. } else {
  4073. assert_fdi_tx_disabled(dev_priv, pipe);
  4074. assert_fdi_rx_disabled(dev_priv, pipe);
  4075. }
  4076. ironlake_pfit_enable(intel_crtc);
  4077. /*
  4078. * On ILK+ LUT must be loaded before the pipe is running but with
  4079. * clocks enabled
  4080. */
  4081. intel_color_load_luts(&pipe_config->base);
  4082. if (dev_priv->display.initial_watermarks != NULL)
  4083. dev_priv->display.initial_watermarks(intel_crtc->config);
  4084. intel_enable_pipe(intel_crtc);
  4085. if (intel_crtc->config->has_pch_encoder)
  4086. ironlake_pch_enable(crtc);
  4087. assert_vblank_disabled(crtc);
  4088. drm_crtc_vblank_on(crtc);
  4089. for_each_encoder_on_crtc(dev, crtc, encoder)
  4090. encoder->enable(encoder);
  4091. if (HAS_PCH_CPT(dev))
  4092. cpt_verify_modeset(dev, intel_crtc->pipe);
  4093. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4094. if (intel_crtc->config->has_pch_encoder)
  4095. intel_wait_for_vblank(dev, pipe);
  4096. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4097. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4098. }
  4099. /* IPS only exists on ULT machines and is tied to pipe A. */
  4100. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4101. {
  4102. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4103. }
  4104. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4105. {
  4106. struct drm_device *dev = crtc->dev;
  4107. struct drm_i915_private *dev_priv = dev->dev_private;
  4108. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4109. struct intel_encoder *encoder;
  4110. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4111. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4112. struct intel_crtc_state *pipe_config =
  4113. to_intel_crtc_state(crtc->state);
  4114. if (WARN_ON(intel_crtc->active))
  4115. return;
  4116. if (intel_crtc->config->has_pch_encoder)
  4117. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4118. false);
  4119. if (intel_crtc->config->shared_dpll)
  4120. intel_enable_shared_dpll(intel_crtc);
  4121. if (intel_crtc->config->has_dp_encoder)
  4122. intel_dp_set_m_n(intel_crtc, M1_N1);
  4123. if (!intel_crtc->config->has_dsi_encoder)
  4124. intel_set_pipe_timings(intel_crtc);
  4125. intel_set_pipe_src_size(intel_crtc);
  4126. if (cpu_transcoder != TRANSCODER_EDP &&
  4127. !transcoder_is_dsi(cpu_transcoder)) {
  4128. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4129. intel_crtc->config->pixel_multiplier - 1);
  4130. }
  4131. if (intel_crtc->config->has_pch_encoder) {
  4132. intel_cpu_transcoder_set_m_n(intel_crtc,
  4133. &intel_crtc->config->fdi_m_n, NULL);
  4134. }
  4135. if (!intel_crtc->config->has_dsi_encoder)
  4136. haswell_set_pipeconf(crtc);
  4137. haswell_set_pipemisc(crtc);
  4138. intel_color_set_csc(&pipe_config->base);
  4139. intel_crtc->active = true;
  4140. if (intel_crtc->config->has_pch_encoder)
  4141. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4142. else
  4143. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4144. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4145. if (encoder->pre_enable)
  4146. encoder->pre_enable(encoder);
  4147. }
  4148. if (intel_crtc->config->has_pch_encoder)
  4149. dev_priv->display.fdi_link_train(crtc);
  4150. if (!intel_crtc->config->has_dsi_encoder)
  4151. intel_ddi_enable_pipe_clock(intel_crtc);
  4152. if (INTEL_INFO(dev)->gen >= 9)
  4153. skylake_pfit_enable(intel_crtc);
  4154. else
  4155. ironlake_pfit_enable(intel_crtc);
  4156. /*
  4157. * On ILK+ LUT must be loaded before the pipe is running but with
  4158. * clocks enabled
  4159. */
  4160. intel_color_load_luts(&pipe_config->base);
  4161. intel_ddi_set_pipe_settings(crtc);
  4162. if (!intel_crtc->config->has_dsi_encoder)
  4163. intel_ddi_enable_transcoder_func(crtc);
  4164. if (dev_priv->display.initial_watermarks != NULL)
  4165. dev_priv->display.initial_watermarks(pipe_config);
  4166. else
  4167. intel_update_watermarks(crtc);
  4168. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4169. if (!intel_crtc->config->has_dsi_encoder)
  4170. intel_enable_pipe(intel_crtc);
  4171. if (intel_crtc->config->has_pch_encoder)
  4172. lpt_pch_enable(crtc);
  4173. if (intel_crtc->config->dp_encoder_is_mst)
  4174. intel_ddi_set_vc_payload_alloc(crtc, true);
  4175. assert_vblank_disabled(crtc);
  4176. drm_crtc_vblank_on(crtc);
  4177. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4178. encoder->enable(encoder);
  4179. intel_opregion_notify_encoder(encoder, true);
  4180. }
  4181. if (intel_crtc->config->has_pch_encoder) {
  4182. intel_wait_for_vblank(dev, pipe);
  4183. intel_wait_for_vblank(dev, pipe);
  4184. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4185. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4186. true);
  4187. }
  4188. /* If we change the relative order between pipe/planes enabling, we need
  4189. * to change the workaround. */
  4190. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4191. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4192. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4193. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4194. }
  4195. }
  4196. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4197. {
  4198. struct drm_device *dev = crtc->base.dev;
  4199. struct drm_i915_private *dev_priv = dev->dev_private;
  4200. int pipe = crtc->pipe;
  4201. /* To avoid upsetting the power well on haswell only disable the pfit if
  4202. * it's in use. The hw state code will make sure we get this right. */
  4203. if (force || crtc->config->pch_pfit.enabled) {
  4204. I915_WRITE(PF_CTL(pipe), 0);
  4205. I915_WRITE(PF_WIN_POS(pipe), 0);
  4206. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4207. }
  4208. }
  4209. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4210. {
  4211. struct drm_device *dev = crtc->dev;
  4212. struct drm_i915_private *dev_priv = dev->dev_private;
  4213. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4214. struct intel_encoder *encoder;
  4215. int pipe = intel_crtc->pipe;
  4216. /*
  4217. * Sometimes spurious CPU pipe underruns happen when the
  4218. * pipe is already disabled, but FDI RX/TX is still enabled.
  4219. * Happens at least with VGA+HDMI cloning. Suppress them.
  4220. */
  4221. if (intel_crtc->config->has_pch_encoder) {
  4222. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4223. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4224. }
  4225. for_each_encoder_on_crtc(dev, crtc, encoder)
  4226. encoder->disable(encoder);
  4227. drm_crtc_vblank_off(crtc);
  4228. assert_vblank_disabled(crtc);
  4229. intel_disable_pipe(intel_crtc);
  4230. ironlake_pfit_disable(intel_crtc, false);
  4231. if (intel_crtc->config->has_pch_encoder)
  4232. ironlake_fdi_disable(crtc);
  4233. for_each_encoder_on_crtc(dev, crtc, encoder)
  4234. if (encoder->post_disable)
  4235. encoder->post_disable(encoder);
  4236. if (intel_crtc->config->has_pch_encoder) {
  4237. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4238. if (HAS_PCH_CPT(dev)) {
  4239. i915_reg_t reg;
  4240. u32 temp;
  4241. /* disable TRANS_DP_CTL */
  4242. reg = TRANS_DP_CTL(pipe);
  4243. temp = I915_READ(reg);
  4244. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4245. TRANS_DP_PORT_SEL_MASK);
  4246. temp |= TRANS_DP_PORT_SEL_NONE;
  4247. I915_WRITE(reg, temp);
  4248. /* disable DPLL_SEL */
  4249. temp = I915_READ(PCH_DPLL_SEL);
  4250. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4251. I915_WRITE(PCH_DPLL_SEL, temp);
  4252. }
  4253. ironlake_fdi_pll_disable(intel_crtc);
  4254. }
  4255. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4256. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4257. }
  4258. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4259. {
  4260. struct drm_device *dev = crtc->dev;
  4261. struct drm_i915_private *dev_priv = dev->dev_private;
  4262. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4263. struct intel_encoder *encoder;
  4264. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4265. if (intel_crtc->config->has_pch_encoder)
  4266. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4267. false);
  4268. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4269. intel_opregion_notify_encoder(encoder, false);
  4270. encoder->disable(encoder);
  4271. }
  4272. drm_crtc_vblank_off(crtc);
  4273. assert_vblank_disabled(crtc);
  4274. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4275. if (!intel_crtc->config->has_dsi_encoder)
  4276. intel_disable_pipe(intel_crtc);
  4277. if (intel_crtc->config->dp_encoder_is_mst)
  4278. intel_ddi_set_vc_payload_alloc(crtc, false);
  4279. if (!intel_crtc->config->has_dsi_encoder)
  4280. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4281. if (INTEL_INFO(dev)->gen >= 9)
  4282. skylake_scaler_disable(intel_crtc);
  4283. else
  4284. ironlake_pfit_disable(intel_crtc, false);
  4285. if (!intel_crtc->config->has_dsi_encoder)
  4286. intel_ddi_disable_pipe_clock(intel_crtc);
  4287. for_each_encoder_on_crtc(dev, crtc, encoder)
  4288. if (encoder->post_disable)
  4289. encoder->post_disable(encoder);
  4290. if (intel_crtc->config->has_pch_encoder) {
  4291. lpt_disable_pch_transcoder(dev_priv);
  4292. lpt_disable_iclkip(dev_priv);
  4293. intel_ddi_fdi_disable(crtc);
  4294. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4295. true);
  4296. }
  4297. }
  4298. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4299. {
  4300. struct drm_device *dev = crtc->base.dev;
  4301. struct drm_i915_private *dev_priv = dev->dev_private;
  4302. struct intel_crtc_state *pipe_config = crtc->config;
  4303. if (!pipe_config->gmch_pfit.control)
  4304. return;
  4305. /*
  4306. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4307. * according to register description and PRM.
  4308. */
  4309. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4310. assert_pipe_disabled(dev_priv, crtc->pipe);
  4311. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4312. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4313. /* Border color in case we don't scale up to the full screen. Black by
  4314. * default, change to something else for debugging. */
  4315. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4316. }
  4317. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4318. {
  4319. switch (port) {
  4320. case PORT_A:
  4321. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4322. case PORT_B:
  4323. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4324. case PORT_C:
  4325. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4326. case PORT_D:
  4327. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4328. case PORT_E:
  4329. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4330. default:
  4331. MISSING_CASE(port);
  4332. return POWER_DOMAIN_PORT_OTHER;
  4333. }
  4334. }
  4335. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4336. {
  4337. switch (port) {
  4338. case PORT_A:
  4339. return POWER_DOMAIN_AUX_A;
  4340. case PORT_B:
  4341. return POWER_DOMAIN_AUX_B;
  4342. case PORT_C:
  4343. return POWER_DOMAIN_AUX_C;
  4344. case PORT_D:
  4345. return POWER_DOMAIN_AUX_D;
  4346. case PORT_E:
  4347. /* FIXME: Check VBT for actual wiring of PORT E */
  4348. return POWER_DOMAIN_AUX_D;
  4349. default:
  4350. MISSING_CASE(port);
  4351. return POWER_DOMAIN_AUX_A;
  4352. }
  4353. }
  4354. enum intel_display_power_domain
  4355. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4356. {
  4357. struct drm_device *dev = intel_encoder->base.dev;
  4358. struct intel_digital_port *intel_dig_port;
  4359. switch (intel_encoder->type) {
  4360. case INTEL_OUTPUT_UNKNOWN:
  4361. /* Only DDI platforms should ever use this output type */
  4362. WARN_ON_ONCE(!HAS_DDI(dev));
  4363. case INTEL_OUTPUT_DISPLAYPORT:
  4364. case INTEL_OUTPUT_HDMI:
  4365. case INTEL_OUTPUT_EDP:
  4366. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4367. return port_to_power_domain(intel_dig_port->port);
  4368. case INTEL_OUTPUT_DP_MST:
  4369. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4370. return port_to_power_domain(intel_dig_port->port);
  4371. case INTEL_OUTPUT_ANALOG:
  4372. return POWER_DOMAIN_PORT_CRT;
  4373. case INTEL_OUTPUT_DSI:
  4374. return POWER_DOMAIN_PORT_DSI;
  4375. default:
  4376. return POWER_DOMAIN_PORT_OTHER;
  4377. }
  4378. }
  4379. enum intel_display_power_domain
  4380. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4381. {
  4382. struct drm_device *dev = intel_encoder->base.dev;
  4383. struct intel_digital_port *intel_dig_port;
  4384. switch (intel_encoder->type) {
  4385. case INTEL_OUTPUT_UNKNOWN:
  4386. case INTEL_OUTPUT_HDMI:
  4387. /*
  4388. * Only DDI platforms should ever use these output types.
  4389. * We can get here after the HDMI detect code has already set
  4390. * the type of the shared encoder. Since we can't be sure
  4391. * what's the status of the given connectors, play safe and
  4392. * run the DP detection too.
  4393. */
  4394. WARN_ON_ONCE(!HAS_DDI(dev));
  4395. case INTEL_OUTPUT_DISPLAYPORT:
  4396. case INTEL_OUTPUT_EDP:
  4397. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4398. return port_to_aux_power_domain(intel_dig_port->port);
  4399. case INTEL_OUTPUT_DP_MST:
  4400. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4401. return port_to_aux_power_domain(intel_dig_port->port);
  4402. default:
  4403. MISSING_CASE(intel_encoder->type);
  4404. return POWER_DOMAIN_AUX_A;
  4405. }
  4406. }
  4407. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4408. struct intel_crtc_state *crtc_state)
  4409. {
  4410. struct drm_device *dev = crtc->dev;
  4411. struct drm_encoder *encoder;
  4412. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4413. enum pipe pipe = intel_crtc->pipe;
  4414. unsigned long mask;
  4415. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4416. if (!crtc_state->base.active)
  4417. return 0;
  4418. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4419. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4420. if (crtc_state->pch_pfit.enabled ||
  4421. crtc_state->pch_pfit.force_thru)
  4422. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4423. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4424. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4425. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4426. }
  4427. if (crtc_state->shared_dpll)
  4428. mask |= BIT(POWER_DOMAIN_PLLS);
  4429. return mask;
  4430. }
  4431. static unsigned long
  4432. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4433. struct intel_crtc_state *crtc_state)
  4434. {
  4435. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4436. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4437. enum intel_display_power_domain domain;
  4438. unsigned long domains, new_domains, old_domains;
  4439. old_domains = intel_crtc->enabled_power_domains;
  4440. intel_crtc->enabled_power_domains = new_domains =
  4441. get_crtc_power_domains(crtc, crtc_state);
  4442. domains = new_domains & ~old_domains;
  4443. for_each_power_domain(domain, domains)
  4444. intel_display_power_get(dev_priv, domain);
  4445. return old_domains & ~new_domains;
  4446. }
  4447. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4448. unsigned long domains)
  4449. {
  4450. enum intel_display_power_domain domain;
  4451. for_each_power_domain(domain, domains)
  4452. intel_display_power_put(dev_priv, domain);
  4453. }
  4454. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4455. {
  4456. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4457. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4458. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4459. return max_cdclk_freq;
  4460. else if (IS_CHERRYVIEW(dev_priv))
  4461. return max_cdclk_freq*95/100;
  4462. else if (INTEL_INFO(dev_priv)->gen < 4)
  4463. return 2*max_cdclk_freq*90/100;
  4464. else
  4465. return max_cdclk_freq*90/100;
  4466. }
  4467. static void intel_update_max_cdclk(struct drm_device *dev)
  4468. {
  4469. struct drm_i915_private *dev_priv = dev->dev_private;
  4470. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4471. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4472. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4473. dev_priv->max_cdclk_freq = 675000;
  4474. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4475. dev_priv->max_cdclk_freq = 540000;
  4476. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4477. dev_priv->max_cdclk_freq = 450000;
  4478. else
  4479. dev_priv->max_cdclk_freq = 337500;
  4480. } else if (IS_BROXTON(dev)) {
  4481. dev_priv->max_cdclk_freq = 624000;
  4482. } else if (IS_BROADWELL(dev)) {
  4483. /*
  4484. * FIXME with extra cooling we can allow
  4485. * 540 MHz for ULX and 675 Mhz for ULT.
  4486. * How can we know if extra cooling is
  4487. * available? PCI ID, VTB, something else?
  4488. */
  4489. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4490. dev_priv->max_cdclk_freq = 450000;
  4491. else if (IS_BDW_ULX(dev))
  4492. dev_priv->max_cdclk_freq = 450000;
  4493. else if (IS_BDW_ULT(dev))
  4494. dev_priv->max_cdclk_freq = 540000;
  4495. else
  4496. dev_priv->max_cdclk_freq = 675000;
  4497. } else if (IS_CHERRYVIEW(dev)) {
  4498. dev_priv->max_cdclk_freq = 320000;
  4499. } else if (IS_VALLEYVIEW(dev)) {
  4500. dev_priv->max_cdclk_freq = 400000;
  4501. } else {
  4502. /* otherwise assume cdclk is fixed */
  4503. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4504. }
  4505. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4506. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4507. dev_priv->max_cdclk_freq);
  4508. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4509. dev_priv->max_dotclk_freq);
  4510. }
  4511. static void intel_update_cdclk(struct drm_device *dev)
  4512. {
  4513. struct drm_i915_private *dev_priv = dev->dev_private;
  4514. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4515. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4516. dev_priv->cdclk_freq);
  4517. /*
  4518. * Program the gmbus_freq based on the cdclk frequency.
  4519. * BSpec erroneously claims we should aim for 4MHz, but
  4520. * in fact 1MHz is the correct frequency.
  4521. */
  4522. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  4523. /*
  4524. * Program the gmbus_freq based on the cdclk frequency.
  4525. * BSpec erroneously claims we should aim for 4MHz, but
  4526. * in fact 1MHz is the correct frequency.
  4527. */
  4528. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4529. }
  4530. if (dev_priv->max_cdclk_freq == 0)
  4531. intel_update_max_cdclk(dev);
  4532. }
  4533. static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int frequency)
  4534. {
  4535. uint32_t divider;
  4536. uint32_t ratio;
  4537. uint32_t current_freq;
  4538. int ret;
  4539. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4540. switch (frequency) {
  4541. case 144000:
  4542. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4543. ratio = BXT_DE_PLL_RATIO(60);
  4544. break;
  4545. case 288000:
  4546. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4547. ratio = BXT_DE_PLL_RATIO(60);
  4548. break;
  4549. case 384000:
  4550. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4551. ratio = BXT_DE_PLL_RATIO(60);
  4552. break;
  4553. case 576000:
  4554. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4555. ratio = BXT_DE_PLL_RATIO(60);
  4556. break;
  4557. case 624000:
  4558. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4559. ratio = BXT_DE_PLL_RATIO(65);
  4560. break;
  4561. case 19200:
  4562. /*
  4563. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4564. * to suppress GCC warning.
  4565. */
  4566. ratio = 0;
  4567. divider = 0;
  4568. break;
  4569. default:
  4570. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4571. return;
  4572. }
  4573. mutex_lock(&dev_priv->rps.hw_lock);
  4574. /* Inform power controller of upcoming frequency change */
  4575. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4576. 0x80000000);
  4577. mutex_unlock(&dev_priv->rps.hw_lock);
  4578. if (ret) {
  4579. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4580. ret, frequency);
  4581. return;
  4582. }
  4583. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4584. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4585. current_freq = current_freq * 500 + 1000;
  4586. /*
  4587. * DE PLL has to be disabled when
  4588. * - setting to 19.2MHz (bypass, PLL isn't used)
  4589. * - before setting to 624MHz (PLL needs toggling)
  4590. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4591. */
  4592. if (frequency == 19200 || frequency == 624000 ||
  4593. current_freq == 624000) {
  4594. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4595. /* Timeout 200us */
  4596. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4597. 1))
  4598. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4599. }
  4600. if (frequency != 19200) {
  4601. uint32_t val;
  4602. val = I915_READ(BXT_DE_PLL_CTL);
  4603. val &= ~BXT_DE_PLL_RATIO_MASK;
  4604. val |= ratio;
  4605. I915_WRITE(BXT_DE_PLL_CTL, val);
  4606. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4607. /* Timeout 200us */
  4608. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4609. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4610. val = I915_READ(CDCLK_CTL);
  4611. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4612. val |= divider;
  4613. /*
  4614. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4615. * enable otherwise.
  4616. */
  4617. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4618. if (frequency >= 500000)
  4619. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4620. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4621. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4622. val |= (frequency - 1000) / 500;
  4623. I915_WRITE(CDCLK_CTL, val);
  4624. }
  4625. mutex_lock(&dev_priv->rps.hw_lock);
  4626. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4627. DIV_ROUND_UP(frequency, 25000));
  4628. mutex_unlock(&dev_priv->rps.hw_lock);
  4629. if (ret) {
  4630. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4631. ret, frequency);
  4632. return;
  4633. }
  4634. intel_update_cdclk(dev_priv->dev);
  4635. }
  4636. static bool broxton_cdclk_is_enabled(struct drm_i915_private *dev_priv)
  4637. {
  4638. if (!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE))
  4639. return false;
  4640. /* TODO: Check for a valid CDCLK rate */
  4641. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_REQUEST)) {
  4642. DRM_DEBUG_DRIVER("CDCLK enabled, but DBUF power not requested\n");
  4643. return false;
  4644. }
  4645. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE)) {
  4646. DRM_DEBUG_DRIVER("CDCLK enabled, but DBUF power hasn't settled\n");
  4647. return false;
  4648. }
  4649. return true;
  4650. }
  4651. bool broxton_cdclk_verify_state(struct drm_i915_private *dev_priv)
  4652. {
  4653. return broxton_cdclk_is_enabled(dev_priv);
  4654. }
  4655. void broxton_init_cdclk(struct drm_i915_private *dev_priv)
  4656. {
  4657. /* check if cd clock is enabled */
  4658. if (broxton_cdclk_is_enabled(dev_priv)) {
  4659. DRM_DEBUG_KMS("CDCLK already enabled, won't reprogram it\n");
  4660. return;
  4661. }
  4662. DRM_DEBUG_KMS("CDCLK not enabled, enabling it\n");
  4663. /*
  4664. * FIXME:
  4665. * - The initial CDCLK needs to be read from VBT.
  4666. * Need to make this change after VBT has changes for BXT.
  4667. * - check if setting the max (or any) cdclk freq is really necessary
  4668. * here, it belongs to modeset time
  4669. */
  4670. broxton_set_cdclk(dev_priv, 624000);
  4671. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4672. POSTING_READ(DBUF_CTL);
  4673. udelay(10);
  4674. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4675. DRM_ERROR("DBuf power enable timeout!\n");
  4676. }
  4677. void broxton_uninit_cdclk(struct drm_i915_private *dev_priv)
  4678. {
  4679. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4680. POSTING_READ(DBUF_CTL);
  4681. udelay(10);
  4682. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4683. DRM_ERROR("DBuf power disable timeout!\n");
  4684. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4685. broxton_set_cdclk(dev_priv, 19200);
  4686. }
  4687. static const struct skl_cdclk_entry {
  4688. unsigned int freq;
  4689. unsigned int vco;
  4690. } skl_cdclk_frequencies[] = {
  4691. { .freq = 308570, .vco = 8640 },
  4692. { .freq = 337500, .vco = 8100 },
  4693. { .freq = 432000, .vco = 8640 },
  4694. { .freq = 450000, .vco = 8100 },
  4695. { .freq = 540000, .vco = 8100 },
  4696. { .freq = 617140, .vco = 8640 },
  4697. { .freq = 675000, .vco = 8100 },
  4698. };
  4699. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4700. {
  4701. return (freq - 1000) / 500;
  4702. }
  4703. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4704. {
  4705. unsigned int i;
  4706. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4707. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4708. if (e->freq == freq)
  4709. return e->vco;
  4710. }
  4711. return 8100;
  4712. }
  4713. static void
  4714. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4715. {
  4716. unsigned int min_freq;
  4717. u32 val;
  4718. /* select the minimum CDCLK before enabling DPLL 0 */
  4719. val = I915_READ(CDCLK_CTL);
  4720. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4721. val |= CDCLK_FREQ_337_308;
  4722. if (required_vco == 8640)
  4723. min_freq = 308570;
  4724. else
  4725. min_freq = 337500;
  4726. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4727. I915_WRITE(CDCLK_CTL, val);
  4728. POSTING_READ(CDCLK_CTL);
  4729. /*
  4730. * We always enable DPLL0 with the lowest link rate possible, but still
  4731. * taking into account the VCO required to operate the eDP panel at the
  4732. * desired frequency. The usual DP link rates operate with a VCO of
  4733. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4734. * The modeset code is responsible for the selection of the exact link
  4735. * rate later on, with the constraint of choosing a frequency that
  4736. * works with required_vco.
  4737. */
  4738. val = I915_READ(DPLL_CTRL1);
  4739. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4740. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4741. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4742. if (required_vco == 8640)
  4743. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4744. SKL_DPLL0);
  4745. else
  4746. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4747. SKL_DPLL0);
  4748. I915_WRITE(DPLL_CTRL1, val);
  4749. POSTING_READ(DPLL_CTRL1);
  4750. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4751. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4752. DRM_ERROR("DPLL0 not locked\n");
  4753. }
  4754. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4755. {
  4756. int ret;
  4757. u32 val;
  4758. /* inform PCU we want to change CDCLK */
  4759. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4760. mutex_lock(&dev_priv->rps.hw_lock);
  4761. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4762. mutex_unlock(&dev_priv->rps.hw_lock);
  4763. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4764. }
  4765. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4766. {
  4767. unsigned int i;
  4768. for (i = 0; i < 15; i++) {
  4769. if (skl_cdclk_pcu_ready(dev_priv))
  4770. return true;
  4771. udelay(10);
  4772. }
  4773. return false;
  4774. }
  4775. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4776. {
  4777. struct drm_device *dev = dev_priv->dev;
  4778. u32 freq_select, pcu_ack;
  4779. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4780. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4781. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4782. return;
  4783. }
  4784. /* set CDCLK_CTL */
  4785. switch(freq) {
  4786. case 450000:
  4787. case 432000:
  4788. freq_select = CDCLK_FREQ_450_432;
  4789. pcu_ack = 1;
  4790. break;
  4791. case 540000:
  4792. freq_select = CDCLK_FREQ_540;
  4793. pcu_ack = 2;
  4794. break;
  4795. case 308570:
  4796. case 337500:
  4797. default:
  4798. freq_select = CDCLK_FREQ_337_308;
  4799. pcu_ack = 0;
  4800. break;
  4801. case 617140:
  4802. case 675000:
  4803. freq_select = CDCLK_FREQ_675_617;
  4804. pcu_ack = 3;
  4805. break;
  4806. }
  4807. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4808. POSTING_READ(CDCLK_CTL);
  4809. /* inform PCU of the change */
  4810. mutex_lock(&dev_priv->rps.hw_lock);
  4811. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4812. mutex_unlock(&dev_priv->rps.hw_lock);
  4813. intel_update_cdclk(dev);
  4814. }
  4815. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4816. {
  4817. /* disable DBUF power */
  4818. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4819. POSTING_READ(DBUF_CTL);
  4820. udelay(10);
  4821. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4822. DRM_ERROR("DBuf power disable timeout\n");
  4823. /* disable DPLL0 */
  4824. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4825. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4826. DRM_ERROR("Couldn't disable DPLL0\n");
  4827. }
  4828. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4829. {
  4830. unsigned int required_vco;
  4831. /* DPLL0 not enabled (happens on early BIOS versions) */
  4832. if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
  4833. /* enable DPLL0 */
  4834. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4835. skl_dpll0_enable(dev_priv, required_vco);
  4836. }
  4837. /* set CDCLK to the frequency the BIOS chose */
  4838. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4839. /* enable DBUF power */
  4840. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4841. POSTING_READ(DBUF_CTL);
  4842. udelay(10);
  4843. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4844. DRM_ERROR("DBuf power enable timeout\n");
  4845. }
  4846. int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4847. {
  4848. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  4849. uint32_t cdctl = I915_READ(CDCLK_CTL);
  4850. int freq = dev_priv->skl_boot_cdclk;
  4851. /*
  4852. * check if the pre-os intialized the display
  4853. * There is SWF18 scratchpad register defined which is set by the
  4854. * pre-os which can be used by the OS drivers to check the status
  4855. */
  4856. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4857. goto sanitize;
  4858. /* Is PLL enabled and locked ? */
  4859. if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
  4860. goto sanitize;
  4861. /* DPLL okay; verify the cdclock
  4862. *
  4863. * Noticed in some instances that the freq selection is correct but
  4864. * decimal part is programmed wrong from BIOS where pre-os does not
  4865. * enable display. Verify the same as well.
  4866. */
  4867. if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
  4868. /* All well; nothing to sanitize */
  4869. return false;
  4870. sanitize:
  4871. /*
  4872. * As of now initialize with max cdclk till
  4873. * we get dynamic cdclk support
  4874. * */
  4875. dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
  4876. skl_init_cdclk(dev_priv);
  4877. /* we did have to sanitize */
  4878. return true;
  4879. }
  4880. /* Adjust CDclk dividers to allow high res or save power if possible */
  4881. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4882. {
  4883. struct drm_i915_private *dev_priv = dev->dev_private;
  4884. u32 val, cmd;
  4885. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4886. != dev_priv->cdclk_freq);
  4887. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4888. cmd = 2;
  4889. else if (cdclk == 266667)
  4890. cmd = 1;
  4891. else
  4892. cmd = 0;
  4893. mutex_lock(&dev_priv->rps.hw_lock);
  4894. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4895. val &= ~DSPFREQGUAR_MASK;
  4896. val |= (cmd << DSPFREQGUAR_SHIFT);
  4897. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4898. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4899. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4900. 50)) {
  4901. DRM_ERROR("timed out waiting for CDclk change\n");
  4902. }
  4903. mutex_unlock(&dev_priv->rps.hw_lock);
  4904. mutex_lock(&dev_priv->sb_lock);
  4905. if (cdclk == 400000) {
  4906. u32 divider;
  4907. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4908. /* adjust cdclk divider */
  4909. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4910. val &= ~CCK_FREQUENCY_VALUES;
  4911. val |= divider;
  4912. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4913. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4914. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4915. 50))
  4916. DRM_ERROR("timed out waiting for CDclk change\n");
  4917. }
  4918. /* adjust self-refresh exit latency value */
  4919. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4920. val &= ~0x7f;
  4921. /*
  4922. * For high bandwidth configs, we set a higher latency in the bunit
  4923. * so that the core display fetch happens in time to avoid underruns.
  4924. */
  4925. if (cdclk == 400000)
  4926. val |= 4500 / 250; /* 4.5 usec */
  4927. else
  4928. val |= 3000 / 250; /* 3.0 usec */
  4929. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4930. mutex_unlock(&dev_priv->sb_lock);
  4931. intel_update_cdclk(dev);
  4932. }
  4933. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4934. {
  4935. struct drm_i915_private *dev_priv = dev->dev_private;
  4936. u32 val, cmd;
  4937. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4938. != dev_priv->cdclk_freq);
  4939. switch (cdclk) {
  4940. case 333333:
  4941. case 320000:
  4942. case 266667:
  4943. case 200000:
  4944. break;
  4945. default:
  4946. MISSING_CASE(cdclk);
  4947. return;
  4948. }
  4949. /*
  4950. * Specs are full of misinformation, but testing on actual
  4951. * hardware has shown that we just need to write the desired
  4952. * CCK divider into the Punit register.
  4953. */
  4954. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4955. mutex_lock(&dev_priv->rps.hw_lock);
  4956. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4957. val &= ~DSPFREQGUAR_MASK_CHV;
  4958. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4959. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4960. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4961. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4962. 50)) {
  4963. DRM_ERROR("timed out waiting for CDclk change\n");
  4964. }
  4965. mutex_unlock(&dev_priv->rps.hw_lock);
  4966. intel_update_cdclk(dev);
  4967. }
  4968. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4969. int max_pixclk)
  4970. {
  4971. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4972. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4973. /*
  4974. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4975. * 200MHz
  4976. * 267MHz
  4977. * 320/333MHz (depends on HPLL freq)
  4978. * 400MHz (VLV only)
  4979. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4980. * of the lower bin and adjust if needed.
  4981. *
  4982. * We seem to get an unstable or solid color picture at 200MHz.
  4983. * Not sure what's wrong. For now use 200MHz only when all pipes
  4984. * are off.
  4985. */
  4986. if (!IS_CHERRYVIEW(dev_priv) &&
  4987. max_pixclk > freq_320*limit/100)
  4988. return 400000;
  4989. else if (max_pixclk > 266667*limit/100)
  4990. return freq_320;
  4991. else if (max_pixclk > 0)
  4992. return 266667;
  4993. else
  4994. return 200000;
  4995. }
  4996. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4997. int max_pixclk)
  4998. {
  4999. /*
  5000. * FIXME:
  5001. * - remove the guardband, it's not needed on BXT
  5002. * - set 19.2MHz bypass frequency if there are no active pipes
  5003. */
  5004. if (max_pixclk > 576000*9/10)
  5005. return 624000;
  5006. else if (max_pixclk > 384000*9/10)
  5007. return 576000;
  5008. else if (max_pixclk > 288000*9/10)
  5009. return 384000;
  5010. else if (max_pixclk > 144000*9/10)
  5011. return 288000;
  5012. else
  5013. return 144000;
  5014. }
  5015. /* Compute the max pixel clock for new configuration. */
  5016. static int intel_mode_max_pixclk(struct drm_device *dev,
  5017. struct drm_atomic_state *state)
  5018. {
  5019. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5020. struct drm_i915_private *dev_priv = dev->dev_private;
  5021. struct drm_crtc *crtc;
  5022. struct drm_crtc_state *crtc_state;
  5023. unsigned max_pixclk = 0, i;
  5024. enum pipe pipe;
  5025. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5026. sizeof(intel_state->min_pixclk));
  5027. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5028. int pixclk = 0;
  5029. if (crtc_state->enable)
  5030. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5031. intel_state->min_pixclk[i] = pixclk;
  5032. }
  5033. for_each_pipe(dev_priv, pipe)
  5034. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5035. return max_pixclk;
  5036. }
  5037. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5038. {
  5039. struct drm_device *dev = state->dev;
  5040. struct drm_i915_private *dev_priv = dev->dev_private;
  5041. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5042. struct intel_atomic_state *intel_state =
  5043. to_intel_atomic_state(state);
  5044. if (max_pixclk < 0)
  5045. return max_pixclk;
  5046. intel_state->cdclk = intel_state->dev_cdclk =
  5047. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5048. if (!intel_state->active_crtcs)
  5049. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5050. return 0;
  5051. }
  5052. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5053. {
  5054. struct drm_device *dev = state->dev;
  5055. struct drm_i915_private *dev_priv = dev->dev_private;
  5056. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5057. struct intel_atomic_state *intel_state =
  5058. to_intel_atomic_state(state);
  5059. if (max_pixclk < 0)
  5060. return max_pixclk;
  5061. intel_state->cdclk = intel_state->dev_cdclk =
  5062. broxton_calc_cdclk(dev_priv, max_pixclk);
  5063. if (!intel_state->active_crtcs)
  5064. intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
  5065. return 0;
  5066. }
  5067. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5068. {
  5069. unsigned int credits, default_credits;
  5070. if (IS_CHERRYVIEW(dev_priv))
  5071. default_credits = PFI_CREDIT(12);
  5072. else
  5073. default_credits = PFI_CREDIT(8);
  5074. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5075. /* CHV suggested value is 31 or 63 */
  5076. if (IS_CHERRYVIEW(dev_priv))
  5077. credits = PFI_CREDIT_63;
  5078. else
  5079. credits = PFI_CREDIT(15);
  5080. } else {
  5081. credits = default_credits;
  5082. }
  5083. /*
  5084. * WA - write default credits before re-programming
  5085. * FIXME: should we also set the resend bit here?
  5086. */
  5087. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5088. default_credits);
  5089. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5090. credits | PFI_CREDIT_RESEND);
  5091. /*
  5092. * FIXME is this guaranteed to clear
  5093. * immediately or should we poll for it?
  5094. */
  5095. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5096. }
  5097. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5098. {
  5099. struct drm_device *dev = old_state->dev;
  5100. struct drm_i915_private *dev_priv = dev->dev_private;
  5101. struct intel_atomic_state *old_intel_state =
  5102. to_intel_atomic_state(old_state);
  5103. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5104. /*
  5105. * FIXME: We can end up here with all power domains off, yet
  5106. * with a CDCLK frequency other than the minimum. To account
  5107. * for this take the PIPE-A power domain, which covers the HW
  5108. * blocks needed for the following programming. This can be
  5109. * removed once it's guaranteed that we get here either with
  5110. * the minimum CDCLK set, or the required power domains
  5111. * enabled.
  5112. */
  5113. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5114. if (IS_CHERRYVIEW(dev))
  5115. cherryview_set_cdclk(dev, req_cdclk);
  5116. else
  5117. valleyview_set_cdclk(dev, req_cdclk);
  5118. vlv_program_pfi_credits(dev_priv);
  5119. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5120. }
  5121. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5122. {
  5123. struct drm_device *dev = crtc->dev;
  5124. struct drm_i915_private *dev_priv = to_i915(dev);
  5125. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5126. struct intel_encoder *encoder;
  5127. struct intel_crtc_state *pipe_config =
  5128. to_intel_crtc_state(crtc->state);
  5129. int pipe = intel_crtc->pipe;
  5130. if (WARN_ON(intel_crtc->active))
  5131. return;
  5132. if (intel_crtc->config->has_dp_encoder)
  5133. intel_dp_set_m_n(intel_crtc, M1_N1);
  5134. intel_set_pipe_timings(intel_crtc);
  5135. intel_set_pipe_src_size(intel_crtc);
  5136. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5137. struct drm_i915_private *dev_priv = dev->dev_private;
  5138. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5139. I915_WRITE(CHV_CANVAS(pipe), 0);
  5140. }
  5141. i9xx_set_pipeconf(intel_crtc);
  5142. intel_crtc->active = true;
  5143. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5144. for_each_encoder_on_crtc(dev, crtc, encoder)
  5145. if (encoder->pre_pll_enable)
  5146. encoder->pre_pll_enable(encoder);
  5147. if (IS_CHERRYVIEW(dev)) {
  5148. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5149. chv_enable_pll(intel_crtc, intel_crtc->config);
  5150. } else {
  5151. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5152. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5153. }
  5154. for_each_encoder_on_crtc(dev, crtc, encoder)
  5155. if (encoder->pre_enable)
  5156. encoder->pre_enable(encoder);
  5157. i9xx_pfit_enable(intel_crtc);
  5158. intel_color_load_luts(&pipe_config->base);
  5159. intel_update_watermarks(crtc);
  5160. intel_enable_pipe(intel_crtc);
  5161. assert_vblank_disabled(crtc);
  5162. drm_crtc_vblank_on(crtc);
  5163. for_each_encoder_on_crtc(dev, crtc, encoder)
  5164. encoder->enable(encoder);
  5165. }
  5166. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5167. {
  5168. struct drm_device *dev = crtc->base.dev;
  5169. struct drm_i915_private *dev_priv = dev->dev_private;
  5170. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5171. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5172. }
  5173. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5174. {
  5175. struct drm_device *dev = crtc->dev;
  5176. struct drm_i915_private *dev_priv = to_i915(dev);
  5177. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5178. struct intel_encoder *encoder;
  5179. struct intel_crtc_state *pipe_config =
  5180. to_intel_crtc_state(crtc->state);
  5181. enum pipe pipe = intel_crtc->pipe;
  5182. if (WARN_ON(intel_crtc->active))
  5183. return;
  5184. i9xx_set_pll_dividers(intel_crtc);
  5185. if (intel_crtc->config->has_dp_encoder)
  5186. intel_dp_set_m_n(intel_crtc, M1_N1);
  5187. intel_set_pipe_timings(intel_crtc);
  5188. intel_set_pipe_src_size(intel_crtc);
  5189. i9xx_set_pipeconf(intel_crtc);
  5190. intel_crtc->active = true;
  5191. if (!IS_GEN2(dev))
  5192. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5193. for_each_encoder_on_crtc(dev, crtc, encoder)
  5194. if (encoder->pre_enable)
  5195. encoder->pre_enable(encoder);
  5196. i9xx_enable_pll(intel_crtc);
  5197. i9xx_pfit_enable(intel_crtc);
  5198. intel_color_load_luts(&pipe_config->base);
  5199. intel_update_watermarks(crtc);
  5200. intel_enable_pipe(intel_crtc);
  5201. assert_vblank_disabled(crtc);
  5202. drm_crtc_vblank_on(crtc);
  5203. for_each_encoder_on_crtc(dev, crtc, encoder)
  5204. encoder->enable(encoder);
  5205. }
  5206. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5207. {
  5208. struct drm_device *dev = crtc->base.dev;
  5209. struct drm_i915_private *dev_priv = dev->dev_private;
  5210. if (!crtc->config->gmch_pfit.control)
  5211. return;
  5212. assert_pipe_disabled(dev_priv, crtc->pipe);
  5213. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5214. I915_READ(PFIT_CONTROL));
  5215. I915_WRITE(PFIT_CONTROL, 0);
  5216. }
  5217. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5218. {
  5219. struct drm_device *dev = crtc->dev;
  5220. struct drm_i915_private *dev_priv = dev->dev_private;
  5221. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5222. struct intel_encoder *encoder;
  5223. int pipe = intel_crtc->pipe;
  5224. /*
  5225. * On gen2 planes are double buffered but the pipe isn't, so we must
  5226. * wait for planes to fully turn off before disabling the pipe.
  5227. */
  5228. if (IS_GEN2(dev))
  5229. intel_wait_for_vblank(dev, pipe);
  5230. for_each_encoder_on_crtc(dev, crtc, encoder)
  5231. encoder->disable(encoder);
  5232. drm_crtc_vblank_off(crtc);
  5233. assert_vblank_disabled(crtc);
  5234. intel_disable_pipe(intel_crtc);
  5235. i9xx_pfit_disable(intel_crtc);
  5236. for_each_encoder_on_crtc(dev, crtc, encoder)
  5237. if (encoder->post_disable)
  5238. encoder->post_disable(encoder);
  5239. if (!intel_crtc->config->has_dsi_encoder) {
  5240. if (IS_CHERRYVIEW(dev))
  5241. chv_disable_pll(dev_priv, pipe);
  5242. else if (IS_VALLEYVIEW(dev))
  5243. vlv_disable_pll(dev_priv, pipe);
  5244. else
  5245. i9xx_disable_pll(intel_crtc);
  5246. }
  5247. for_each_encoder_on_crtc(dev, crtc, encoder)
  5248. if (encoder->post_pll_disable)
  5249. encoder->post_pll_disable(encoder);
  5250. if (!IS_GEN2(dev))
  5251. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5252. }
  5253. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5254. {
  5255. struct intel_encoder *encoder;
  5256. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5257. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5258. enum intel_display_power_domain domain;
  5259. unsigned long domains;
  5260. if (!intel_crtc->active)
  5261. return;
  5262. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5263. WARN_ON(intel_crtc->unpin_work);
  5264. intel_pre_disable_primary_noatomic(crtc);
  5265. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5266. to_intel_plane_state(crtc->primary->state)->visible = false;
  5267. }
  5268. dev_priv->display.crtc_disable(crtc);
  5269. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
  5270. crtc->base.id);
  5271. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5272. crtc->state->active = false;
  5273. intel_crtc->active = false;
  5274. crtc->enabled = false;
  5275. crtc->state->connector_mask = 0;
  5276. crtc->state->encoder_mask = 0;
  5277. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5278. encoder->base.crtc = NULL;
  5279. intel_fbc_disable(intel_crtc);
  5280. intel_update_watermarks(crtc);
  5281. intel_disable_shared_dpll(intel_crtc);
  5282. domains = intel_crtc->enabled_power_domains;
  5283. for_each_power_domain(domain, domains)
  5284. intel_display_power_put(dev_priv, domain);
  5285. intel_crtc->enabled_power_domains = 0;
  5286. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5287. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5288. }
  5289. /*
  5290. * turn all crtc's off, but do not adjust state
  5291. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5292. */
  5293. int intel_display_suspend(struct drm_device *dev)
  5294. {
  5295. struct drm_i915_private *dev_priv = to_i915(dev);
  5296. struct drm_atomic_state *state;
  5297. int ret;
  5298. state = drm_atomic_helper_suspend(dev);
  5299. ret = PTR_ERR_OR_ZERO(state);
  5300. if (ret)
  5301. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5302. else
  5303. dev_priv->modeset_restore_state = state;
  5304. return ret;
  5305. }
  5306. void intel_encoder_destroy(struct drm_encoder *encoder)
  5307. {
  5308. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5309. drm_encoder_cleanup(encoder);
  5310. kfree(intel_encoder);
  5311. }
  5312. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5313. * internal consistency). */
  5314. static void intel_connector_verify_state(struct intel_connector *connector)
  5315. {
  5316. struct drm_crtc *crtc = connector->base.state->crtc;
  5317. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5318. connector->base.base.id,
  5319. connector->base.name);
  5320. if (connector->get_hw_state(connector)) {
  5321. struct intel_encoder *encoder = connector->encoder;
  5322. struct drm_connector_state *conn_state = connector->base.state;
  5323. I915_STATE_WARN(!crtc,
  5324. "connector enabled without attached crtc\n");
  5325. if (!crtc)
  5326. return;
  5327. I915_STATE_WARN(!crtc->state->active,
  5328. "connector is active, but attached crtc isn't\n");
  5329. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5330. return;
  5331. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5332. "atomic encoder doesn't match attached encoder\n");
  5333. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5334. "attached encoder crtc differs from connector crtc\n");
  5335. } else {
  5336. I915_STATE_WARN(crtc && crtc->state->active,
  5337. "attached crtc is active, but connector isn't\n");
  5338. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5339. "best encoder set without crtc!\n");
  5340. }
  5341. }
  5342. int intel_connector_init(struct intel_connector *connector)
  5343. {
  5344. drm_atomic_helper_connector_reset(&connector->base);
  5345. if (!connector->base.state)
  5346. return -ENOMEM;
  5347. return 0;
  5348. }
  5349. struct intel_connector *intel_connector_alloc(void)
  5350. {
  5351. struct intel_connector *connector;
  5352. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5353. if (!connector)
  5354. return NULL;
  5355. if (intel_connector_init(connector) < 0) {
  5356. kfree(connector);
  5357. return NULL;
  5358. }
  5359. return connector;
  5360. }
  5361. /* Simple connector->get_hw_state implementation for encoders that support only
  5362. * one connector and no cloning and hence the encoder state determines the state
  5363. * of the connector. */
  5364. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5365. {
  5366. enum pipe pipe = 0;
  5367. struct intel_encoder *encoder = connector->encoder;
  5368. return encoder->get_hw_state(encoder, &pipe);
  5369. }
  5370. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5371. {
  5372. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5373. return crtc_state->fdi_lanes;
  5374. return 0;
  5375. }
  5376. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5377. struct intel_crtc_state *pipe_config)
  5378. {
  5379. struct drm_atomic_state *state = pipe_config->base.state;
  5380. struct intel_crtc *other_crtc;
  5381. struct intel_crtc_state *other_crtc_state;
  5382. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5383. pipe_name(pipe), pipe_config->fdi_lanes);
  5384. if (pipe_config->fdi_lanes > 4) {
  5385. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5386. pipe_name(pipe), pipe_config->fdi_lanes);
  5387. return -EINVAL;
  5388. }
  5389. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5390. if (pipe_config->fdi_lanes > 2) {
  5391. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5392. pipe_config->fdi_lanes);
  5393. return -EINVAL;
  5394. } else {
  5395. return 0;
  5396. }
  5397. }
  5398. if (INTEL_INFO(dev)->num_pipes == 2)
  5399. return 0;
  5400. /* Ivybridge 3 pipe is really complicated */
  5401. switch (pipe) {
  5402. case PIPE_A:
  5403. return 0;
  5404. case PIPE_B:
  5405. if (pipe_config->fdi_lanes <= 2)
  5406. return 0;
  5407. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5408. other_crtc_state =
  5409. intel_atomic_get_crtc_state(state, other_crtc);
  5410. if (IS_ERR(other_crtc_state))
  5411. return PTR_ERR(other_crtc_state);
  5412. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5413. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5414. pipe_name(pipe), pipe_config->fdi_lanes);
  5415. return -EINVAL;
  5416. }
  5417. return 0;
  5418. case PIPE_C:
  5419. if (pipe_config->fdi_lanes > 2) {
  5420. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5421. pipe_name(pipe), pipe_config->fdi_lanes);
  5422. return -EINVAL;
  5423. }
  5424. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5425. other_crtc_state =
  5426. intel_atomic_get_crtc_state(state, other_crtc);
  5427. if (IS_ERR(other_crtc_state))
  5428. return PTR_ERR(other_crtc_state);
  5429. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5430. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5431. return -EINVAL;
  5432. }
  5433. return 0;
  5434. default:
  5435. BUG();
  5436. }
  5437. }
  5438. #define RETRY 1
  5439. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5440. struct intel_crtc_state *pipe_config)
  5441. {
  5442. struct drm_device *dev = intel_crtc->base.dev;
  5443. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5444. int lane, link_bw, fdi_dotclock, ret;
  5445. bool needs_recompute = false;
  5446. retry:
  5447. /* FDI is a binary signal running at ~2.7GHz, encoding
  5448. * each output octet as 10 bits. The actual frequency
  5449. * is stored as a divider into a 100MHz clock, and the
  5450. * mode pixel clock is stored in units of 1KHz.
  5451. * Hence the bw of each lane in terms of the mode signal
  5452. * is:
  5453. */
  5454. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5455. fdi_dotclock = adjusted_mode->crtc_clock;
  5456. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5457. pipe_config->pipe_bpp);
  5458. pipe_config->fdi_lanes = lane;
  5459. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5460. link_bw, &pipe_config->fdi_m_n);
  5461. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5462. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5463. pipe_config->pipe_bpp -= 2*3;
  5464. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5465. pipe_config->pipe_bpp);
  5466. needs_recompute = true;
  5467. pipe_config->bw_constrained = true;
  5468. goto retry;
  5469. }
  5470. if (needs_recompute)
  5471. return RETRY;
  5472. return ret;
  5473. }
  5474. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5475. struct intel_crtc_state *pipe_config)
  5476. {
  5477. if (pipe_config->pipe_bpp > 24)
  5478. return false;
  5479. /* HSW can handle pixel rate up to cdclk? */
  5480. if (IS_HASWELL(dev_priv))
  5481. return true;
  5482. /*
  5483. * We compare against max which means we must take
  5484. * the increased cdclk requirement into account when
  5485. * calculating the new cdclk.
  5486. *
  5487. * Should measure whether using a lower cdclk w/o IPS
  5488. */
  5489. return ilk_pipe_pixel_rate(pipe_config) <=
  5490. dev_priv->max_cdclk_freq * 95 / 100;
  5491. }
  5492. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5493. struct intel_crtc_state *pipe_config)
  5494. {
  5495. struct drm_device *dev = crtc->base.dev;
  5496. struct drm_i915_private *dev_priv = dev->dev_private;
  5497. pipe_config->ips_enabled = i915.enable_ips &&
  5498. hsw_crtc_supports_ips(crtc) &&
  5499. pipe_config_supports_ips(dev_priv, pipe_config);
  5500. }
  5501. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5502. {
  5503. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5504. /* GDG double wide on either pipe, otherwise pipe A only */
  5505. return INTEL_INFO(dev_priv)->gen < 4 &&
  5506. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5507. }
  5508. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5509. struct intel_crtc_state *pipe_config)
  5510. {
  5511. struct drm_device *dev = crtc->base.dev;
  5512. struct drm_i915_private *dev_priv = dev->dev_private;
  5513. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5514. /* FIXME should check pixel clock limits on all platforms */
  5515. if (INTEL_INFO(dev)->gen < 4) {
  5516. int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5517. /*
  5518. * Enable double wide mode when the dot clock
  5519. * is > 90% of the (display) core speed.
  5520. */
  5521. if (intel_crtc_supports_double_wide(crtc) &&
  5522. adjusted_mode->crtc_clock > clock_limit) {
  5523. clock_limit *= 2;
  5524. pipe_config->double_wide = true;
  5525. }
  5526. if (adjusted_mode->crtc_clock > clock_limit) {
  5527. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5528. adjusted_mode->crtc_clock, clock_limit,
  5529. yesno(pipe_config->double_wide));
  5530. return -EINVAL;
  5531. }
  5532. }
  5533. /*
  5534. * Pipe horizontal size must be even in:
  5535. * - DVO ganged mode
  5536. * - LVDS dual channel mode
  5537. * - Double wide pipe
  5538. */
  5539. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5540. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5541. pipe_config->pipe_src_w &= ~1;
  5542. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5543. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5544. */
  5545. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5546. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5547. return -EINVAL;
  5548. if (HAS_IPS(dev))
  5549. hsw_compute_ips_config(crtc, pipe_config);
  5550. if (pipe_config->has_pch_encoder)
  5551. return ironlake_fdi_compute_config(crtc, pipe_config);
  5552. return 0;
  5553. }
  5554. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5555. {
  5556. struct drm_i915_private *dev_priv = to_i915(dev);
  5557. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5558. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5559. uint32_t linkrate;
  5560. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5561. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5562. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5563. return 540000;
  5564. linkrate = (I915_READ(DPLL_CTRL1) &
  5565. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5566. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5567. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5568. /* vco 8640 */
  5569. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5570. case CDCLK_FREQ_450_432:
  5571. return 432000;
  5572. case CDCLK_FREQ_337_308:
  5573. return 308570;
  5574. case CDCLK_FREQ_675_617:
  5575. return 617140;
  5576. default:
  5577. WARN(1, "Unknown cd freq selection\n");
  5578. }
  5579. } else {
  5580. /* vco 8100 */
  5581. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5582. case CDCLK_FREQ_450_432:
  5583. return 450000;
  5584. case CDCLK_FREQ_337_308:
  5585. return 337500;
  5586. case CDCLK_FREQ_675_617:
  5587. return 675000;
  5588. default:
  5589. WARN(1, "Unknown cd freq selection\n");
  5590. }
  5591. }
  5592. /* error case, do as if DPLL0 isn't enabled */
  5593. return 24000;
  5594. }
  5595. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5596. {
  5597. struct drm_i915_private *dev_priv = to_i915(dev);
  5598. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5599. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5600. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5601. int cdclk;
  5602. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5603. return 19200;
  5604. cdclk = 19200 * pll_ratio / 2;
  5605. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5606. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5607. return cdclk; /* 576MHz or 624MHz */
  5608. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5609. return cdclk * 2 / 3; /* 384MHz */
  5610. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5611. return cdclk / 2; /* 288MHz */
  5612. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5613. return cdclk / 4; /* 144MHz */
  5614. }
  5615. /* error case, do as if DE PLL isn't enabled */
  5616. return 19200;
  5617. }
  5618. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5619. {
  5620. struct drm_i915_private *dev_priv = dev->dev_private;
  5621. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5622. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5623. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5624. return 800000;
  5625. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5626. return 450000;
  5627. else if (freq == LCPLL_CLK_FREQ_450)
  5628. return 450000;
  5629. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5630. return 540000;
  5631. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5632. return 337500;
  5633. else
  5634. return 675000;
  5635. }
  5636. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5637. {
  5638. struct drm_i915_private *dev_priv = dev->dev_private;
  5639. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5640. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5641. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5642. return 800000;
  5643. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5644. return 450000;
  5645. else if (freq == LCPLL_CLK_FREQ_450)
  5646. return 450000;
  5647. else if (IS_HSW_ULT(dev))
  5648. return 337500;
  5649. else
  5650. return 540000;
  5651. }
  5652. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5653. {
  5654. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5655. CCK_DISPLAY_CLOCK_CONTROL);
  5656. }
  5657. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5658. {
  5659. return 450000;
  5660. }
  5661. static int i945_get_display_clock_speed(struct drm_device *dev)
  5662. {
  5663. return 400000;
  5664. }
  5665. static int i915_get_display_clock_speed(struct drm_device *dev)
  5666. {
  5667. return 333333;
  5668. }
  5669. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5670. {
  5671. return 200000;
  5672. }
  5673. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5674. {
  5675. u16 gcfgc = 0;
  5676. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5677. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5678. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5679. return 266667;
  5680. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5681. return 333333;
  5682. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5683. return 444444;
  5684. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5685. return 200000;
  5686. default:
  5687. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5688. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5689. return 133333;
  5690. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5691. return 166667;
  5692. }
  5693. }
  5694. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5695. {
  5696. u16 gcfgc = 0;
  5697. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5698. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5699. return 133333;
  5700. else {
  5701. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5702. case GC_DISPLAY_CLOCK_333_MHZ:
  5703. return 333333;
  5704. default:
  5705. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5706. return 190000;
  5707. }
  5708. }
  5709. }
  5710. static int i865_get_display_clock_speed(struct drm_device *dev)
  5711. {
  5712. return 266667;
  5713. }
  5714. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5715. {
  5716. u16 hpllcc = 0;
  5717. /*
  5718. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5719. * encoding is different :(
  5720. * FIXME is this the right way to detect 852GM/852GMV?
  5721. */
  5722. if (dev->pdev->revision == 0x1)
  5723. return 133333;
  5724. pci_bus_read_config_word(dev->pdev->bus,
  5725. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5726. /* Assume that the hardware is in the high speed state. This
  5727. * should be the default.
  5728. */
  5729. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5730. case GC_CLOCK_133_200:
  5731. case GC_CLOCK_133_200_2:
  5732. case GC_CLOCK_100_200:
  5733. return 200000;
  5734. case GC_CLOCK_166_250:
  5735. return 250000;
  5736. case GC_CLOCK_100_133:
  5737. return 133333;
  5738. case GC_CLOCK_133_266:
  5739. case GC_CLOCK_133_266_2:
  5740. case GC_CLOCK_166_266:
  5741. return 266667;
  5742. }
  5743. /* Shouldn't happen */
  5744. return 0;
  5745. }
  5746. static int i830_get_display_clock_speed(struct drm_device *dev)
  5747. {
  5748. return 133333;
  5749. }
  5750. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5751. {
  5752. struct drm_i915_private *dev_priv = dev->dev_private;
  5753. static const unsigned int blb_vco[8] = {
  5754. [0] = 3200000,
  5755. [1] = 4000000,
  5756. [2] = 5333333,
  5757. [3] = 4800000,
  5758. [4] = 6400000,
  5759. };
  5760. static const unsigned int pnv_vco[8] = {
  5761. [0] = 3200000,
  5762. [1] = 4000000,
  5763. [2] = 5333333,
  5764. [3] = 4800000,
  5765. [4] = 2666667,
  5766. };
  5767. static const unsigned int cl_vco[8] = {
  5768. [0] = 3200000,
  5769. [1] = 4000000,
  5770. [2] = 5333333,
  5771. [3] = 6400000,
  5772. [4] = 3333333,
  5773. [5] = 3566667,
  5774. [6] = 4266667,
  5775. };
  5776. static const unsigned int elk_vco[8] = {
  5777. [0] = 3200000,
  5778. [1] = 4000000,
  5779. [2] = 5333333,
  5780. [3] = 4800000,
  5781. };
  5782. static const unsigned int ctg_vco[8] = {
  5783. [0] = 3200000,
  5784. [1] = 4000000,
  5785. [2] = 5333333,
  5786. [3] = 6400000,
  5787. [4] = 2666667,
  5788. [5] = 4266667,
  5789. };
  5790. const unsigned int *vco_table;
  5791. unsigned int vco;
  5792. uint8_t tmp = 0;
  5793. /* FIXME other chipsets? */
  5794. if (IS_GM45(dev))
  5795. vco_table = ctg_vco;
  5796. else if (IS_G4X(dev))
  5797. vco_table = elk_vco;
  5798. else if (IS_CRESTLINE(dev))
  5799. vco_table = cl_vco;
  5800. else if (IS_PINEVIEW(dev))
  5801. vco_table = pnv_vco;
  5802. else if (IS_G33(dev))
  5803. vco_table = blb_vco;
  5804. else
  5805. return 0;
  5806. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5807. vco = vco_table[tmp & 0x7];
  5808. if (vco == 0)
  5809. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5810. else
  5811. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5812. return vco;
  5813. }
  5814. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5815. {
  5816. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5817. uint16_t tmp = 0;
  5818. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5819. cdclk_sel = (tmp >> 12) & 0x1;
  5820. switch (vco) {
  5821. case 2666667:
  5822. case 4000000:
  5823. case 5333333:
  5824. return cdclk_sel ? 333333 : 222222;
  5825. case 3200000:
  5826. return cdclk_sel ? 320000 : 228571;
  5827. default:
  5828. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5829. return 222222;
  5830. }
  5831. }
  5832. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5833. {
  5834. static const uint8_t div_3200[] = { 16, 10, 8 };
  5835. static const uint8_t div_4000[] = { 20, 12, 10 };
  5836. static const uint8_t div_5333[] = { 24, 16, 14 };
  5837. const uint8_t *div_table;
  5838. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5839. uint16_t tmp = 0;
  5840. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5841. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5842. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5843. goto fail;
  5844. switch (vco) {
  5845. case 3200000:
  5846. div_table = div_3200;
  5847. break;
  5848. case 4000000:
  5849. div_table = div_4000;
  5850. break;
  5851. case 5333333:
  5852. div_table = div_5333;
  5853. break;
  5854. default:
  5855. goto fail;
  5856. }
  5857. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5858. fail:
  5859. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5860. return 200000;
  5861. }
  5862. static int g33_get_display_clock_speed(struct drm_device *dev)
  5863. {
  5864. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5865. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5866. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5867. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5868. const uint8_t *div_table;
  5869. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5870. uint16_t tmp = 0;
  5871. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5872. cdclk_sel = (tmp >> 4) & 0x7;
  5873. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5874. goto fail;
  5875. switch (vco) {
  5876. case 3200000:
  5877. div_table = div_3200;
  5878. break;
  5879. case 4000000:
  5880. div_table = div_4000;
  5881. break;
  5882. case 4800000:
  5883. div_table = div_4800;
  5884. break;
  5885. case 5333333:
  5886. div_table = div_5333;
  5887. break;
  5888. default:
  5889. goto fail;
  5890. }
  5891. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5892. fail:
  5893. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5894. return 190476;
  5895. }
  5896. static void
  5897. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5898. {
  5899. while (*num > DATA_LINK_M_N_MASK ||
  5900. *den > DATA_LINK_M_N_MASK) {
  5901. *num >>= 1;
  5902. *den >>= 1;
  5903. }
  5904. }
  5905. static void compute_m_n(unsigned int m, unsigned int n,
  5906. uint32_t *ret_m, uint32_t *ret_n)
  5907. {
  5908. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5909. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5910. intel_reduce_m_n_ratio(ret_m, ret_n);
  5911. }
  5912. void
  5913. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5914. int pixel_clock, int link_clock,
  5915. struct intel_link_m_n *m_n)
  5916. {
  5917. m_n->tu = 64;
  5918. compute_m_n(bits_per_pixel * pixel_clock,
  5919. link_clock * nlanes * 8,
  5920. &m_n->gmch_m, &m_n->gmch_n);
  5921. compute_m_n(pixel_clock, link_clock,
  5922. &m_n->link_m, &m_n->link_n);
  5923. }
  5924. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5925. {
  5926. if (i915.panel_use_ssc >= 0)
  5927. return i915.panel_use_ssc != 0;
  5928. return dev_priv->vbt.lvds_use_ssc
  5929. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5930. }
  5931. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5932. {
  5933. return (1 << dpll->n) << 16 | dpll->m2;
  5934. }
  5935. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5936. {
  5937. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5938. }
  5939. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5940. struct intel_crtc_state *crtc_state,
  5941. intel_clock_t *reduced_clock)
  5942. {
  5943. struct drm_device *dev = crtc->base.dev;
  5944. u32 fp, fp2 = 0;
  5945. if (IS_PINEVIEW(dev)) {
  5946. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5947. if (reduced_clock)
  5948. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5949. } else {
  5950. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5951. if (reduced_clock)
  5952. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5953. }
  5954. crtc_state->dpll_hw_state.fp0 = fp;
  5955. crtc->lowfreq_avail = false;
  5956. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5957. reduced_clock) {
  5958. crtc_state->dpll_hw_state.fp1 = fp2;
  5959. crtc->lowfreq_avail = true;
  5960. } else {
  5961. crtc_state->dpll_hw_state.fp1 = fp;
  5962. }
  5963. }
  5964. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5965. pipe)
  5966. {
  5967. u32 reg_val;
  5968. /*
  5969. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5970. * and set it to a reasonable value instead.
  5971. */
  5972. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5973. reg_val &= 0xffffff00;
  5974. reg_val |= 0x00000030;
  5975. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5976. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5977. reg_val &= 0x8cffffff;
  5978. reg_val = 0x8c000000;
  5979. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5980. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5981. reg_val &= 0xffffff00;
  5982. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5983. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5984. reg_val &= 0x00ffffff;
  5985. reg_val |= 0xb0000000;
  5986. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5987. }
  5988. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5989. struct intel_link_m_n *m_n)
  5990. {
  5991. struct drm_device *dev = crtc->base.dev;
  5992. struct drm_i915_private *dev_priv = dev->dev_private;
  5993. int pipe = crtc->pipe;
  5994. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5995. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5996. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5997. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5998. }
  5999. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6000. struct intel_link_m_n *m_n,
  6001. struct intel_link_m_n *m2_n2)
  6002. {
  6003. struct drm_device *dev = crtc->base.dev;
  6004. struct drm_i915_private *dev_priv = dev->dev_private;
  6005. int pipe = crtc->pipe;
  6006. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6007. if (INTEL_INFO(dev)->gen >= 5) {
  6008. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6009. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6010. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6011. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6012. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6013. * for gen < 8) and if DRRS is supported (to make sure the
  6014. * registers are not unnecessarily accessed).
  6015. */
  6016. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6017. crtc->config->has_drrs) {
  6018. I915_WRITE(PIPE_DATA_M2(transcoder),
  6019. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6020. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6021. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6022. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6023. }
  6024. } else {
  6025. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6026. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6027. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6028. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6029. }
  6030. }
  6031. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6032. {
  6033. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6034. if (m_n == M1_N1) {
  6035. dp_m_n = &crtc->config->dp_m_n;
  6036. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6037. } else if (m_n == M2_N2) {
  6038. /*
  6039. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6040. * needs to be programmed into M1_N1.
  6041. */
  6042. dp_m_n = &crtc->config->dp_m2_n2;
  6043. } else {
  6044. DRM_ERROR("Unsupported divider value\n");
  6045. return;
  6046. }
  6047. if (crtc->config->has_pch_encoder)
  6048. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6049. else
  6050. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6051. }
  6052. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6053. struct intel_crtc_state *pipe_config)
  6054. {
  6055. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6056. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6057. if (crtc->pipe != PIPE_A)
  6058. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6059. /* DPLL not used with DSI, but still need the rest set up */
  6060. if (!pipe_config->has_dsi_encoder)
  6061. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6062. DPLL_EXT_BUFFER_ENABLE_VLV;
  6063. pipe_config->dpll_hw_state.dpll_md =
  6064. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6065. }
  6066. static void chv_compute_dpll(struct intel_crtc *crtc,
  6067. struct intel_crtc_state *pipe_config)
  6068. {
  6069. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6070. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6071. if (crtc->pipe != PIPE_A)
  6072. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6073. /* DPLL not used with DSI, but still need the rest set up */
  6074. if (!pipe_config->has_dsi_encoder)
  6075. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6076. pipe_config->dpll_hw_state.dpll_md =
  6077. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6078. }
  6079. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6080. const struct intel_crtc_state *pipe_config)
  6081. {
  6082. struct drm_device *dev = crtc->base.dev;
  6083. struct drm_i915_private *dev_priv = dev->dev_private;
  6084. enum pipe pipe = crtc->pipe;
  6085. u32 mdiv;
  6086. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6087. u32 coreclk, reg_val;
  6088. /* Enable Refclk */
  6089. I915_WRITE(DPLL(pipe),
  6090. pipe_config->dpll_hw_state.dpll &
  6091. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6092. /* No need to actually set up the DPLL with DSI */
  6093. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6094. return;
  6095. mutex_lock(&dev_priv->sb_lock);
  6096. bestn = pipe_config->dpll.n;
  6097. bestm1 = pipe_config->dpll.m1;
  6098. bestm2 = pipe_config->dpll.m2;
  6099. bestp1 = pipe_config->dpll.p1;
  6100. bestp2 = pipe_config->dpll.p2;
  6101. /* See eDP HDMI DPIO driver vbios notes doc */
  6102. /* PLL B needs special handling */
  6103. if (pipe == PIPE_B)
  6104. vlv_pllb_recal_opamp(dev_priv, pipe);
  6105. /* Set up Tx target for periodic Rcomp update */
  6106. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6107. /* Disable target IRef on PLL */
  6108. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6109. reg_val &= 0x00ffffff;
  6110. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6111. /* Disable fast lock */
  6112. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6113. /* Set idtafcrecal before PLL is enabled */
  6114. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6115. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6116. mdiv |= ((bestn << DPIO_N_SHIFT));
  6117. mdiv |= (1 << DPIO_K_SHIFT);
  6118. /*
  6119. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6120. * but we don't support that).
  6121. * Note: don't use the DAC post divider as it seems unstable.
  6122. */
  6123. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6124. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6125. mdiv |= DPIO_ENABLE_CALIBRATION;
  6126. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6127. /* Set HBR and RBR LPF coefficients */
  6128. if (pipe_config->port_clock == 162000 ||
  6129. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6130. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6131. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6132. 0x009f0003);
  6133. else
  6134. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6135. 0x00d0000f);
  6136. if (pipe_config->has_dp_encoder) {
  6137. /* Use SSC source */
  6138. if (pipe == PIPE_A)
  6139. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6140. 0x0df40000);
  6141. else
  6142. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6143. 0x0df70000);
  6144. } else { /* HDMI or VGA */
  6145. /* Use bend source */
  6146. if (pipe == PIPE_A)
  6147. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6148. 0x0df70000);
  6149. else
  6150. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6151. 0x0df40000);
  6152. }
  6153. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6154. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6155. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6156. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6157. coreclk |= 0x01000000;
  6158. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6159. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6160. mutex_unlock(&dev_priv->sb_lock);
  6161. }
  6162. static void chv_prepare_pll(struct intel_crtc *crtc,
  6163. const struct intel_crtc_state *pipe_config)
  6164. {
  6165. struct drm_device *dev = crtc->base.dev;
  6166. struct drm_i915_private *dev_priv = dev->dev_private;
  6167. enum pipe pipe = crtc->pipe;
  6168. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6169. u32 loopfilter, tribuf_calcntr;
  6170. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6171. u32 dpio_val;
  6172. int vco;
  6173. /* Enable Refclk and SSC */
  6174. I915_WRITE(DPLL(pipe),
  6175. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6176. /* No need to actually set up the DPLL with DSI */
  6177. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6178. return;
  6179. bestn = pipe_config->dpll.n;
  6180. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6181. bestm1 = pipe_config->dpll.m1;
  6182. bestm2 = pipe_config->dpll.m2 >> 22;
  6183. bestp1 = pipe_config->dpll.p1;
  6184. bestp2 = pipe_config->dpll.p2;
  6185. vco = pipe_config->dpll.vco;
  6186. dpio_val = 0;
  6187. loopfilter = 0;
  6188. mutex_lock(&dev_priv->sb_lock);
  6189. /* p1 and p2 divider */
  6190. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6191. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6192. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6193. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6194. 1 << DPIO_CHV_K_DIV_SHIFT);
  6195. /* Feedback post-divider - m2 */
  6196. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6197. /* Feedback refclk divider - n and m1 */
  6198. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6199. DPIO_CHV_M1_DIV_BY_2 |
  6200. 1 << DPIO_CHV_N_DIV_SHIFT);
  6201. /* M2 fraction division */
  6202. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6203. /* M2 fraction division enable */
  6204. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6205. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6206. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6207. if (bestm2_frac)
  6208. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6209. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6210. /* Program digital lock detect threshold */
  6211. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6212. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6213. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6214. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6215. if (!bestm2_frac)
  6216. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6217. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6218. /* Loop filter */
  6219. if (vco == 5400000) {
  6220. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6221. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6222. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6223. tribuf_calcntr = 0x9;
  6224. } else if (vco <= 6200000) {
  6225. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6226. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6227. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6228. tribuf_calcntr = 0x9;
  6229. } else if (vco <= 6480000) {
  6230. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6231. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6232. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6233. tribuf_calcntr = 0x8;
  6234. } else {
  6235. /* Not supported. Apply the same limits as in the max case */
  6236. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6237. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6238. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6239. tribuf_calcntr = 0;
  6240. }
  6241. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6242. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6243. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6244. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6245. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6246. /* AFC Recal */
  6247. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6248. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6249. DPIO_AFC_RECAL);
  6250. mutex_unlock(&dev_priv->sb_lock);
  6251. }
  6252. /**
  6253. * vlv_force_pll_on - forcibly enable just the PLL
  6254. * @dev_priv: i915 private structure
  6255. * @pipe: pipe PLL to enable
  6256. * @dpll: PLL configuration
  6257. *
  6258. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6259. * in cases where we need the PLL enabled even when @pipe is not going to
  6260. * be enabled.
  6261. */
  6262. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6263. const struct dpll *dpll)
  6264. {
  6265. struct intel_crtc *crtc =
  6266. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6267. struct intel_crtc_state *pipe_config;
  6268. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6269. if (!pipe_config)
  6270. return -ENOMEM;
  6271. pipe_config->base.crtc = &crtc->base;
  6272. pipe_config->pixel_multiplier = 1;
  6273. pipe_config->dpll = *dpll;
  6274. if (IS_CHERRYVIEW(dev)) {
  6275. chv_compute_dpll(crtc, pipe_config);
  6276. chv_prepare_pll(crtc, pipe_config);
  6277. chv_enable_pll(crtc, pipe_config);
  6278. } else {
  6279. vlv_compute_dpll(crtc, pipe_config);
  6280. vlv_prepare_pll(crtc, pipe_config);
  6281. vlv_enable_pll(crtc, pipe_config);
  6282. }
  6283. kfree(pipe_config);
  6284. return 0;
  6285. }
  6286. /**
  6287. * vlv_force_pll_off - forcibly disable just the PLL
  6288. * @dev_priv: i915 private structure
  6289. * @pipe: pipe PLL to disable
  6290. *
  6291. * Disable the PLL for @pipe. To be used in cases where we need
  6292. * the PLL enabled even when @pipe is not going to be enabled.
  6293. */
  6294. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6295. {
  6296. if (IS_CHERRYVIEW(dev))
  6297. chv_disable_pll(to_i915(dev), pipe);
  6298. else
  6299. vlv_disable_pll(to_i915(dev), pipe);
  6300. }
  6301. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6302. struct intel_crtc_state *crtc_state,
  6303. intel_clock_t *reduced_clock)
  6304. {
  6305. struct drm_device *dev = crtc->base.dev;
  6306. struct drm_i915_private *dev_priv = dev->dev_private;
  6307. u32 dpll;
  6308. bool is_sdvo;
  6309. struct dpll *clock = &crtc_state->dpll;
  6310. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6311. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6312. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6313. dpll = DPLL_VGA_MODE_DIS;
  6314. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6315. dpll |= DPLLB_MODE_LVDS;
  6316. else
  6317. dpll |= DPLLB_MODE_DAC_SERIAL;
  6318. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6319. dpll |= (crtc_state->pixel_multiplier - 1)
  6320. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6321. }
  6322. if (is_sdvo)
  6323. dpll |= DPLL_SDVO_HIGH_SPEED;
  6324. if (crtc_state->has_dp_encoder)
  6325. dpll |= DPLL_SDVO_HIGH_SPEED;
  6326. /* compute bitmask from p1 value */
  6327. if (IS_PINEVIEW(dev))
  6328. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6329. else {
  6330. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6331. if (IS_G4X(dev) && reduced_clock)
  6332. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6333. }
  6334. switch (clock->p2) {
  6335. case 5:
  6336. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6337. break;
  6338. case 7:
  6339. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6340. break;
  6341. case 10:
  6342. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6343. break;
  6344. case 14:
  6345. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6346. break;
  6347. }
  6348. if (INTEL_INFO(dev)->gen >= 4)
  6349. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6350. if (crtc_state->sdvo_tv_clock)
  6351. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6352. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6353. intel_panel_use_ssc(dev_priv))
  6354. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6355. else
  6356. dpll |= PLL_REF_INPUT_DREFCLK;
  6357. dpll |= DPLL_VCO_ENABLE;
  6358. crtc_state->dpll_hw_state.dpll = dpll;
  6359. if (INTEL_INFO(dev)->gen >= 4) {
  6360. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6361. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6362. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6363. }
  6364. }
  6365. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6366. struct intel_crtc_state *crtc_state,
  6367. intel_clock_t *reduced_clock)
  6368. {
  6369. struct drm_device *dev = crtc->base.dev;
  6370. struct drm_i915_private *dev_priv = dev->dev_private;
  6371. u32 dpll;
  6372. struct dpll *clock = &crtc_state->dpll;
  6373. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6374. dpll = DPLL_VGA_MODE_DIS;
  6375. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6376. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6377. } else {
  6378. if (clock->p1 == 2)
  6379. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6380. else
  6381. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6382. if (clock->p2 == 4)
  6383. dpll |= PLL_P2_DIVIDE_BY_4;
  6384. }
  6385. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6386. dpll |= DPLL_DVO_2X_MODE;
  6387. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6388. intel_panel_use_ssc(dev_priv))
  6389. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6390. else
  6391. dpll |= PLL_REF_INPUT_DREFCLK;
  6392. dpll |= DPLL_VCO_ENABLE;
  6393. crtc_state->dpll_hw_state.dpll = dpll;
  6394. }
  6395. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6396. {
  6397. struct drm_device *dev = intel_crtc->base.dev;
  6398. struct drm_i915_private *dev_priv = dev->dev_private;
  6399. enum pipe pipe = intel_crtc->pipe;
  6400. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6401. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6402. uint32_t crtc_vtotal, crtc_vblank_end;
  6403. int vsyncshift = 0;
  6404. /* We need to be careful not to changed the adjusted mode, for otherwise
  6405. * the hw state checker will get angry at the mismatch. */
  6406. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6407. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6408. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6409. /* the chip adds 2 halflines automatically */
  6410. crtc_vtotal -= 1;
  6411. crtc_vblank_end -= 1;
  6412. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6413. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6414. else
  6415. vsyncshift = adjusted_mode->crtc_hsync_start -
  6416. adjusted_mode->crtc_htotal / 2;
  6417. if (vsyncshift < 0)
  6418. vsyncshift += adjusted_mode->crtc_htotal;
  6419. }
  6420. if (INTEL_INFO(dev)->gen > 3)
  6421. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6422. I915_WRITE(HTOTAL(cpu_transcoder),
  6423. (adjusted_mode->crtc_hdisplay - 1) |
  6424. ((adjusted_mode->crtc_htotal - 1) << 16));
  6425. I915_WRITE(HBLANK(cpu_transcoder),
  6426. (adjusted_mode->crtc_hblank_start - 1) |
  6427. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6428. I915_WRITE(HSYNC(cpu_transcoder),
  6429. (adjusted_mode->crtc_hsync_start - 1) |
  6430. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6431. I915_WRITE(VTOTAL(cpu_transcoder),
  6432. (adjusted_mode->crtc_vdisplay - 1) |
  6433. ((crtc_vtotal - 1) << 16));
  6434. I915_WRITE(VBLANK(cpu_transcoder),
  6435. (adjusted_mode->crtc_vblank_start - 1) |
  6436. ((crtc_vblank_end - 1) << 16));
  6437. I915_WRITE(VSYNC(cpu_transcoder),
  6438. (adjusted_mode->crtc_vsync_start - 1) |
  6439. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6440. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6441. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6442. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6443. * bits. */
  6444. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6445. (pipe == PIPE_B || pipe == PIPE_C))
  6446. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6447. }
  6448. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6449. {
  6450. struct drm_device *dev = intel_crtc->base.dev;
  6451. struct drm_i915_private *dev_priv = dev->dev_private;
  6452. enum pipe pipe = intel_crtc->pipe;
  6453. /* pipesrc controls the size that is scaled from, which should
  6454. * always be the user's requested size.
  6455. */
  6456. I915_WRITE(PIPESRC(pipe),
  6457. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6458. (intel_crtc->config->pipe_src_h - 1));
  6459. }
  6460. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6461. struct intel_crtc_state *pipe_config)
  6462. {
  6463. struct drm_device *dev = crtc->base.dev;
  6464. struct drm_i915_private *dev_priv = dev->dev_private;
  6465. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6466. uint32_t tmp;
  6467. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6468. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6469. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6470. tmp = I915_READ(HBLANK(cpu_transcoder));
  6471. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6472. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6473. tmp = I915_READ(HSYNC(cpu_transcoder));
  6474. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6475. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6476. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6477. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6478. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6479. tmp = I915_READ(VBLANK(cpu_transcoder));
  6480. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6481. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6482. tmp = I915_READ(VSYNC(cpu_transcoder));
  6483. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6484. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6485. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6486. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6487. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6488. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6489. }
  6490. }
  6491. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6492. struct intel_crtc_state *pipe_config)
  6493. {
  6494. struct drm_device *dev = crtc->base.dev;
  6495. struct drm_i915_private *dev_priv = dev->dev_private;
  6496. u32 tmp;
  6497. tmp = I915_READ(PIPESRC(crtc->pipe));
  6498. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6499. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6500. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6501. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6502. }
  6503. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6504. struct intel_crtc_state *pipe_config)
  6505. {
  6506. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6507. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6508. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6509. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6510. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6511. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6512. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6513. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6514. mode->flags = pipe_config->base.adjusted_mode.flags;
  6515. mode->type = DRM_MODE_TYPE_DRIVER;
  6516. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6517. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6518. mode->hsync = drm_mode_hsync(mode);
  6519. mode->vrefresh = drm_mode_vrefresh(mode);
  6520. drm_mode_set_name(mode);
  6521. }
  6522. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6523. {
  6524. struct drm_device *dev = intel_crtc->base.dev;
  6525. struct drm_i915_private *dev_priv = dev->dev_private;
  6526. uint32_t pipeconf;
  6527. pipeconf = 0;
  6528. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6529. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6530. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6531. if (intel_crtc->config->double_wide)
  6532. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6533. /* only g4x and later have fancy bpc/dither controls */
  6534. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6535. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6536. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6537. pipeconf |= PIPECONF_DITHER_EN |
  6538. PIPECONF_DITHER_TYPE_SP;
  6539. switch (intel_crtc->config->pipe_bpp) {
  6540. case 18:
  6541. pipeconf |= PIPECONF_6BPC;
  6542. break;
  6543. case 24:
  6544. pipeconf |= PIPECONF_8BPC;
  6545. break;
  6546. case 30:
  6547. pipeconf |= PIPECONF_10BPC;
  6548. break;
  6549. default:
  6550. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6551. BUG();
  6552. }
  6553. }
  6554. if (HAS_PIPE_CXSR(dev)) {
  6555. if (intel_crtc->lowfreq_avail) {
  6556. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6557. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6558. } else {
  6559. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6560. }
  6561. }
  6562. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6563. if (INTEL_INFO(dev)->gen < 4 ||
  6564. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6565. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6566. else
  6567. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6568. } else
  6569. pipeconf |= PIPECONF_PROGRESSIVE;
  6570. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6571. intel_crtc->config->limited_color_range)
  6572. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6573. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6574. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6575. }
  6576. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6577. struct intel_crtc_state *crtc_state)
  6578. {
  6579. struct drm_device *dev = crtc->base.dev;
  6580. struct drm_i915_private *dev_priv = dev->dev_private;
  6581. const intel_limit_t *limit;
  6582. int refclk = 48000;
  6583. memset(&crtc_state->dpll_hw_state, 0,
  6584. sizeof(crtc_state->dpll_hw_state));
  6585. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6586. if (intel_panel_use_ssc(dev_priv)) {
  6587. refclk = dev_priv->vbt.lvds_ssc_freq;
  6588. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6589. }
  6590. limit = &intel_limits_i8xx_lvds;
  6591. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6592. limit = &intel_limits_i8xx_dvo;
  6593. } else {
  6594. limit = &intel_limits_i8xx_dac;
  6595. }
  6596. if (!crtc_state->clock_set &&
  6597. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6598. refclk, NULL, &crtc_state->dpll)) {
  6599. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6600. return -EINVAL;
  6601. }
  6602. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6603. return 0;
  6604. }
  6605. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6606. struct intel_crtc_state *crtc_state)
  6607. {
  6608. struct drm_device *dev = crtc->base.dev;
  6609. struct drm_i915_private *dev_priv = dev->dev_private;
  6610. const intel_limit_t *limit;
  6611. int refclk = 96000;
  6612. memset(&crtc_state->dpll_hw_state, 0,
  6613. sizeof(crtc_state->dpll_hw_state));
  6614. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6615. if (intel_panel_use_ssc(dev_priv)) {
  6616. refclk = dev_priv->vbt.lvds_ssc_freq;
  6617. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6618. }
  6619. if (intel_is_dual_link_lvds(dev))
  6620. limit = &intel_limits_g4x_dual_channel_lvds;
  6621. else
  6622. limit = &intel_limits_g4x_single_channel_lvds;
  6623. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6624. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6625. limit = &intel_limits_g4x_hdmi;
  6626. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6627. limit = &intel_limits_g4x_sdvo;
  6628. } else {
  6629. /* The option is for other outputs */
  6630. limit = &intel_limits_i9xx_sdvo;
  6631. }
  6632. if (!crtc_state->clock_set &&
  6633. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6634. refclk, NULL, &crtc_state->dpll)) {
  6635. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6636. return -EINVAL;
  6637. }
  6638. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6639. return 0;
  6640. }
  6641. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6642. struct intel_crtc_state *crtc_state)
  6643. {
  6644. struct drm_device *dev = crtc->base.dev;
  6645. struct drm_i915_private *dev_priv = dev->dev_private;
  6646. const intel_limit_t *limit;
  6647. int refclk = 96000;
  6648. memset(&crtc_state->dpll_hw_state, 0,
  6649. sizeof(crtc_state->dpll_hw_state));
  6650. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6651. if (intel_panel_use_ssc(dev_priv)) {
  6652. refclk = dev_priv->vbt.lvds_ssc_freq;
  6653. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6654. }
  6655. limit = &intel_limits_pineview_lvds;
  6656. } else {
  6657. limit = &intel_limits_pineview_sdvo;
  6658. }
  6659. if (!crtc_state->clock_set &&
  6660. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6661. refclk, NULL, &crtc_state->dpll)) {
  6662. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6663. return -EINVAL;
  6664. }
  6665. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6666. return 0;
  6667. }
  6668. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6669. struct intel_crtc_state *crtc_state)
  6670. {
  6671. struct drm_device *dev = crtc->base.dev;
  6672. struct drm_i915_private *dev_priv = dev->dev_private;
  6673. const intel_limit_t *limit;
  6674. int refclk = 96000;
  6675. memset(&crtc_state->dpll_hw_state, 0,
  6676. sizeof(crtc_state->dpll_hw_state));
  6677. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6678. if (intel_panel_use_ssc(dev_priv)) {
  6679. refclk = dev_priv->vbt.lvds_ssc_freq;
  6680. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6681. }
  6682. limit = &intel_limits_i9xx_lvds;
  6683. } else {
  6684. limit = &intel_limits_i9xx_sdvo;
  6685. }
  6686. if (!crtc_state->clock_set &&
  6687. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6688. refclk, NULL, &crtc_state->dpll)) {
  6689. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6690. return -EINVAL;
  6691. }
  6692. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6693. return 0;
  6694. }
  6695. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6696. struct intel_crtc_state *crtc_state)
  6697. {
  6698. int refclk = 100000;
  6699. const intel_limit_t *limit = &intel_limits_chv;
  6700. memset(&crtc_state->dpll_hw_state, 0,
  6701. sizeof(crtc_state->dpll_hw_state));
  6702. if (!crtc_state->clock_set &&
  6703. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6704. refclk, NULL, &crtc_state->dpll)) {
  6705. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6706. return -EINVAL;
  6707. }
  6708. chv_compute_dpll(crtc, crtc_state);
  6709. return 0;
  6710. }
  6711. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6712. struct intel_crtc_state *crtc_state)
  6713. {
  6714. int refclk = 100000;
  6715. const intel_limit_t *limit = &intel_limits_vlv;
  6716. memset(&crtc_state->dpll_hw_state, 0,
  6717. sizeof(crtc_state->dpll_hw_state));
  6718. if (!crtc_state->clock_set &&
  6719. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6720. refclk, NULL, &crtc_state->dpll)) {
  6721. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6722. return -EINVAL;
  6723. }
  6724. vlv_compute_dpll(crtc, crtc_state);
  6725. return 0;
  6726. }
  6727. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6728. struct intel_crtc_state *pipe_config)
  6729. {
  6730. struct drm_device *dev = crtc->base.dev;
  6731. struct drm_i915_private *dev_priv = dev->dev_private;
  6732. uint32_t tmp;
  6733. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6734. return;
  6735. tmp = I915_READ(PFIT_CONTROL);
  6736. if (!(tmp & PFIT_ENABLE))
  6737. return;
  6738. /* Check whether the pfit is attached to our pipe. */
  6739. if (INTEL_INFO(dev)->gen < 4) {
  6740. if (crtc->pipe != PIPE_B)
  6741. return;
  6742. } else {
  6743. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6744. return;
  6745. }
  6746. pipe_config->gmch_pfit.control = tmp;
  6747. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6748. if (INTEL_INFO(dev)->gen < 5)
  6749. pipe_config->gmch_pfit.lvds_border_bits =
  6750. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6751. }
  6752. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6753. struct intel_crtc_state *pipe_config)
  6754. {
  6755. struct drm_device *dev = crtc->base.dev;
  6756. struct drm_i915_private *dev_priv = dev->dev_private;
  6757. int pipe = pipe_config->cpu_transcoder;
  6758. intel_clock_t clock;
  6759. u32 mdiv;
  6760. int refclk = 100000;
  6761. /* In case of DSI, DPLL will not be used */
  6762. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6763. return;
  6764. mutex_lock(&dev_priv->sb_lock);
  6765. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6766. mutex_unlock(&dev_priv->sb_lock);
  6767. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6768. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6769. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6770. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6771. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6772. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6773. }
  6774. static void
  6775. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6776. struct intel_initial_plane_config *plane_config)
  6777. {
  6778. struct drm_device *dev = crtc->base.dev;
  6779. struct drm_i915_private *dev_priv = dev->dev_private;
  6780. u32 val, base, offset;
  6781. int pipe = crtc->pipe, plane = crtc->plane;
  6782. int fourcc, pixel_format;
  6783. unsigned int aligned_height;
  6784. struct drm_framebuffer *fb;
  6785. struct intel_framebuffer *intel_fb;
  6786. val = I915_READ(DSPCNTR(plane));
  6787. if (!(val & DISPLAY_PLANE_ENABLE))
  6788. return;
  6789. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6790. if (!intel_fb) {
  6791. DRM_DEBUG_KMS("failed to alloc fb\n");
  6792. return;
  6793. }
  6794. fb = &intel_fb->base;
  6795. if (INTEL_INFO(dev)->gen >= 4) {
  6796. if (val & DISPPLANE_TILED) {
  6797. plane_config->tiling = I915_TILING_X;
  6798. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6799. }
  6800. }
  6801. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6802. fourcc = i9xx_format_to_fourcc(pixel_format);
  6803. fb->pixel_format = fourcc;
  6804. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6805. if (INTEL_INFO(dev)->gen >= 4) {
  6806. if (plane_config->tiling)
  6807. offset = I915_READ(DSPTILEOFF(plane));
  6808. else
  6809. offset = I915_READ(DSPLINOFF(plane));
  6810. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6811. } else {
  6812. base = I915_READ(DSPADDR(plane));
  6813. }
  6814. plane_config->base = base;
  6815. val = I915_READ(PIPESRC(pipe));
  6816. fb->width = ((val >> 16) & 0xfff) + 1;
  6817. fb->height = ((val >> 0) & 0xfff) + 1;
  6818. val = I915_READ(DSPSTRIDE(pipe));
  6819. fb->pitches[0] = val & 0xffffffc0;
  6820. aligned_height = intel_fb_align_height(dev, fb->height,
  6821. fb->pixel_format,
  6822. fb->modifier[0]);
  6823. plane_config->size = fb->pitches[0] * aligned_height;
  6824. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6825. pipe_name(pipe), plane, fb->width, fb->height,
  6826. fb->bits_per_pixel, base, fb->pitches[0],
  6827. plane_config->size);
  6828. plane_config->fb = intel_fb;
  6829. }
  6830. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6831. struct intel_crtc_state *pipe_config)
  6832. {
  6833. struct drm_device *dev = crtc->base.dev;
  6834. struct drm_i915_private *dev_priv = dev->dev_private;
  6835. int pipe = pipe_config->cpu_transcoder;
  6836. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6837. intel_clock_t clock;
  6838. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6839. int refclk = 100000;
  6840. /* In case of DSI, DPLL will not be used */
  6841. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6842. return;
  6843. mutex_lock(&dev_priv->sb_lock);
  6844. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6845. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6846. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6847. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6848. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6849. mutex_unlock(&dev_priv->sb_lock);
  6850. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6851. clock.m2 = (pll_dw0 & 0xff) << 22;
  6852. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6853. clock.m2 |= pll_dw2 & 0x3fffff;
  6854. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6855. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6856. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6857. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6858. }
  6859. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6860. struct intel_crtc_state *pipe_config)
  6861. {
  6862. struct drm_device *dev = crtc->base.dev;
  6863. struct drm_i915_private *dev_priv = dev->dev_private;
  6864. enum intel_display_power_domain power_domain;
  6865. uint32_t tmp;
  6866. bool ret;
  6867. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6868. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6869. return false;
  6870. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6871. pipe_config->shared_dpll = NULL;
  6872. ret = false;
  6873. tmp = I915_READ(PIPECONF(crtc->pipe));
  6874. if (!(tmp & PIPECONF_ENABLE))
  6875. goto out;
  6876. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6877. switch (tmp & PIPECONF_BPC_MASK) {
  6878. case PIPECONF_6BPC:
  6879. pipe_config->pipe_bpp = 18;
  6880. break;
  6881. case PIPECONF_8BPC:
  6882. pipe_config->pipe_bpp = 24;
  6883. break;
  6884. case PIPECONF_10BPC:
  6885. pipe_config->pipe_bpp = 30;
  6886. break;
  6887. default:
  6888. break;
  6889. }
  6890. }
  6891. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6892. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6893. pipe_config->limited_color_range = true;
  6894. if (INTEL_INFO(dev)->gen < 4)
  6895. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6896. intel_get_pipe_timings(crtc, pipe_config);
  6897. intel_get_pipe_src_size(crtc, pipe_config);
  6898. i9xx_get_pfit_config(crtc, pipe_config);
  6899. if (INTEL_INFO(dev)->gen >= 4) {
  6900. /* No way to read it out on pipes B and C */
  6901. if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
  6902. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6903. else
  6904. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6905. pipe_config->pixel_multiplier =
  6906. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6907. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6908. pipe_config->dpll_hw_state.dpll_md = tmp;
  6909. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6910. tmp = I915_READ(DPLL(crtc->pipe));
  6911. pipe_config->pixel_multiplier =
  6912. ((tmp & SDVO_MULTIPLIER_MASK)
  6913. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6914. } else {
  6915. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6916. * port and will be fixed up in the encoder->get_config
  6917. * function. */
  6918. pipe_config->pixel_multiplier = 1;
  6919. }
  6920. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6921. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  6922. /*
  6923. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6924. * on 830. Filter it out here so that we don't
  6925. * report errors due to that.
  6926. */
  6927. if (IS_I830(dev))
  6928. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6929. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6930. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6931. } else {
  6932. /* Mask out read-only status bits. */
  6933. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6934. DPLL_PORTC_READY_MASK |
  6935. DPLL_PORTB_READY_MASK);
  6936. }
  6937. if (IS_CHERRYVIEW(dev))
  6938. chv_crtc_clock_get(crtc, pipe_config);
  6939. else if (IS_VALLEYVIEW(dev))
  6940. vlv_crtc_clock_get(crtc, pipe_config);
  6941. else
  6942. i9xx_crtc_clock_get(crtc, pipe_config);
  6943. /*
  6944. * Normally the dotclock is filled in by the encoder .get_config()
  6945. * but in case the pipe is enabled w/o any ports we need a sane
  6946. * default.
  6947. */
  6948. pipe_config->base.adjusted_mode.crtc_clock =
  6949. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6950. ret = true;
  6951. out:
  6952. intel_display_power_put(dev_priv, power_domain);
  6953. return ret;
  6954. }
  6955. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6956. {
  6957. struct drm_i915_private *dev_priv = dev->dev_private;
  6958. struct intel_encoder *encoder;
  6959. u32 val, final;
  6960. bool has_lvds = false;
  6961. bool has_cpu_edp = false;
  6962. bool has_panel = false;
  6963. bool has_ck505 = false;
  6964. bool can_ssc = false;
  6965. /* We need to take the global config into account */
  6966. for_each_intel_encoder(dev, encoder) {
  6967. switch (encoder->type) {
  6968. case INTEL_OUTPUT_LVDS:
  6969. has_panel = true;
  6970. has_lvds = true;
  6971. break;
  6972. case INTEL_OUTPUT_EDP:
  6973. has_panel = true;
  6974. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6975. has_cpu_edp = true;
  6976. break;
  6977. default:
  6978. break;
  6979. }
  6980. }
  6981. if (HAS_PCH_IBX(dev)) {
  6982. has_ck505 = dev_priv->vbt.display_clock_mode;
  6983. can_ssc = has_ck505;
  6984. } else {
  6985. has_ck505 = false;
  6986. can_ssc = true;
  6987. }
  6988. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6989. has_panel, has_lvds, has_ck505);
  6990. /* Ironlake: try to setup display ref clock before DPLL
  6991. * enabling. This is only under driver's control after
  6992. * PCH B stepping, previous chipset stepping should be
  6993. * ignoring this setting.
  6994. */
  6995. val = I915_READ(PCH_DREF_CONTROL);
  6996. /* As we must carefully and slowly disable/enable each source in turn,
  6997. * compute the final state we want first and check if we need to
  6998. * make any changes at all.
  6999. */
  7000. final = val;
  7001. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7002. if (has_ck505)
  7003. final |= DREF_NONSPREAD_CK505_ENABLE;
  7004. else
  7005. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7006. final &= ~DREF_SSC_SOURCE_MASK;
  7007. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7008. final &= ~DREF_SSC1_ENABLE;
  7009. if (has_panel) {
  7010. final |= DREF_SSC_SOURCE_ENABLE;
  7011. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7012. final |= DREF_SSC1_ENABLE;
  7013. if (has_cpu_edp) {
  7014. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7015. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7016. else
  7017. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7018. } else
  7019. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7020. } else {
  7021. final |= DREF_SSC_SOURCE_DISABLE;
  7022. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7023. }
  7024. if (final == val)
  7025. return;
  7026. /* Always enable nonspread source */
  7027. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7028. if (has_ck505)
  7029. val |= DREF_NONSPREAD_CK505_ENABLE;
  7030. else
  7031. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7032. if (has_panel) {
  7033. val &= ~DREF_SSC_SOURCE_MASK;
  7034. val |= DREF_SSC_SOURCE_ENABLE;
  7035. /* SSC must be turned on before enabling the CPU output */
  7036. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7037. DRM_DEBUG_KMS("Using SSC on panel\n");
  7038. val |= DREF_SSC1_ENABLE;
  7039. } else
  7040. val &= ~DREF_SSC1_ENABLE;
  7041. /* Get SSC going before enabling the outputs */
  7042. I915_WRITE(PCH_DREF_CONTROL, val);
  7043. POSTING_READ(PCH_DREF_CONTROL);
  7044. udelay(200);
  7045. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7046. /* Enable CPU source on CPU attached eDP */
  7047. if (has_cpu_edp) {
  7048. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7049. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7050. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7051. } else
  7052. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7053. } else
  7054. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7055. I915_WRITE(PCH_DREF_CONTROL, val);
  7056. POSTING_READ(PCH_DREF_CONTROL);
  7057. udelay(200);
  7058. } else {
  7059. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  7060. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7061. /* Turn off CPU output */
  7062. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7063. I915_WRITE(PCH_DREF_CONTROL, val);
  7064. POSTING_READ(PCH_DREF_CONTROL);
  7065. udelay(200);
  7066. /* Turn off the SSC source */
  7067. val &= ~DREF_SSC_SOURCE_MASK;
  7068. val |= DREF_SSC_SOURCE_DISABLE;
  7069. /* Turn off SSC1 */
  7070. val &= ~DREF_SSC1_ENABLE;
  7071. I915_WRITE(PCH_DREF_CONTROL, val);
  7072. POSTING_READ(PCH_DREF_CONTROL);
  7073. udelay(200);
  7074. }
  7075. BUG_ON(val != final);
  7076. }
  7077. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7078. {
  7079. uint32_t tmp;
  7080. tmp = I915_READ(SOUTH_CHICKEN2);
  7081. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7082. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7083. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7084. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7085. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7086. tmp = I915_READ(SOUTH_CHICKEN2);
  7087. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7088. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7089. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7090. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7091. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7092. }
  7093. /* WaMPhyProgramming:hsw */
  7094. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7095. {
  7096. uint32_t tmp;
  7097. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7098. tmp &= ~(0xFF << 24);
  7099. tmp |= (0x12 << 24);
  7100. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7101. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7102. tmp |= (1 << 11);
  7103. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7104. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7105. tmp |= (1 << 11);
  7106. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7107. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7108. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7109. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7110. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7111. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7112. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7113. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7114. tmp &= ~(7 << 13);
  7115. tmp |= (5 << 13);
  7116. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7117. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7118. tmp &= ~(7 << 13);
  7119. tmp |= (5 << 13);
  7120. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7121. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7122. tmp &= ~0xFF;
  7123. tmp |= 0x1C;
  7124. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7125. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7126. tmp &= ~0xFF;
  7127. tmp |= 0x1C;
  7128. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7129. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7130. tmp &= ~(0xFF << 16);
  7131. tmp |= (0x1C << 16);
  7132. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7133. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7134. tmp &= ~(0xFF << 16);
  7135. tmp |= (0x1C << 16);
  7136. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7137. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7138. tmp |= (1 << 27);
  7139. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7140. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7141. tmp |= (1 << 27);
  7142. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7143. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7144. tmp &= ~(0xF << 28);
  7145. tmp |= (4 << 28);
  7146. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7147. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7148. tmp &= ~(0xF << 28);
  7149. tmp |= (4 << 28);
  7150. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7151. }
  7152. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7153. * Programming" based on the parameters passed:
  7154. * - Sequence to enable CLKOUT_DP
  7155. * - Sequence to enable CLKOUT_DP without spread
  7156. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7157. */
  7158. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7159. bool with_fdi)
  7160. {
  7161. struct drm_i915_private *dev_priv = dev->dev_private;
  7162. uint32_t reg, tmp;
  7163. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7164. with_spread = true;
  7165. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7166. with_fdi = false;
  7167. mutex_lock(&dev_priv->sb_lock);
  7168. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7169. tmp &= ~SBI_SSCCTL_DISABLE;
  7170. tmp |= SBI_SSCCTL_PATHALT;
  7171. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7172. udelay(24);
  7173. if (with_spread) {
  7174. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7175. tmp &= ~SBI_SSCCTL_PATHALT;
  7176. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7177. if (with_fdi) {
  7178. lpt_reset_fdi_mphy(dev_priv);
  7179. lpt_program_fdi_mphy(dev_priv);
  7180. }
  7181. }
  7182. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7183. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7184. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7185. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7186. mutex_unlock(&dev_priv->sb_lock);
  7187. }
  7188. /* Sequence to disable CLKOUT_DP */
  7189. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7190. {
  7191. struct drm_i915_private *dev_priv = dev->dev_private;
  7192. uint32_t reg, tmp;
  7193. mutex_lock(&dev_priv->sb_lock);
  7194. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7195. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7196. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7197. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7198. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7199. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7200. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7201. tmp |= SBI_SSCCTL_PATHALT;
  7202. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7203. udelay(32);
  7204. }
  7205. tmp |= SBI_SSCCTL_DISABLE;
  7206. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7207. }
  7208. mutex_unlock(&dev_priv->sb_lock);
  7209. }
  7210. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7211. static const uint16_t sscdivintphase[] = {
  7212. [BEND_IDX( 50)] = 0x3B23,
  7213. [BEND_IDX( 45)] = 0x3B23,
  7214. [BEND_IDX( 40)] = 0x3C23,
  7215. [BEND_IDX( 35)] = 0x3C23,
  7216. [BEND_IDX( 30)] = 0x3D23,
  7217. [BEND_IDX( 25)] = 0x3D23,
  7218. [BEND_IDX( 20)] = 0x3E23,
  7219. [BEND_IDX( 15)] = 0x3E23,
  7220. [BEND_IDX( 10)] = 0x3F23,
  7221. [BEND_IDX( 5)] = 0x3F23,
  7222. [BEND_IDX( 0)] = 0x0025,
  7223. [BEND_IDX( -5)] = 0x0025,
  7224. [BEND_IDX(-10)] = 0x0125,
  7225. [BEND_IDX(-15)] = 0x0125,
  7226. [BEND_IDX(-20)] = 0x0225,
  7227. [BEND_IDX(-25)] = 0x0225,
  7228. [BEND_IDX(-30)] = 0x0325,
  7229. [BEND_IDX(-35)] = 0x0325,
  7230. [BEND_IDX(-40)] = 0x0425,
  7231. [BEND_IDX(-45)] = 0x0425,
  7232. [BEND_IDX(-50)] = 0x0525,
  7233. };
  7234. /*
  7235. * Bend CLKOUT_DP
  7236. * steps -50 to 50 inclusive, in steps of 5
  7237. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7238. * change in clock period = -(steps / 10) * 5.787 ps
  7239. */
  7240. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7241. {
  7242. uint32_t tmp;
  7243. int idx = BEND_IDX(steps);
  7244. if (WARN_ON(steps % 5 != 0))
  7245. return;
  7246. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7247. return;
  7248. mutex_lock(&dev_priv->sb_lock);
  7249. if (steps % 10 != 0)
  7250. tmp = 0xAAAAAAAB;
  7251. else
  7252. tmp = 0x00000000;
  7253. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7254. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7255. tmp &= 0xffff0000;
  7256. tmp |= sscdivintphase[idx];
  7257. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7258. mutex_unlock(&dev_priv->sb_lock);
  7259. }
  7260. #undef BEND_IDX
  7261. static void lpt_init_pch_refclk(struct drm_device *dev)
  7262. {
  7263. struct intel_encoder *encoder;
  7264. bool has_vga = false;
  7265. for_each_intel_encoder(dev, encoder) {
  7266. switch (encoder->type) {
  7267. case INTEL_OUTPUT_ANALOG:
  7268. has_vga = true;
  7269. break;
  7270. default:
  7271. break;
  7272. }
  7273. }
  7274. if (has_vga) {
  7275. lpt_bend_clkout_dp(to_i915(dev), 0);
  7276. lpt_enable_clkout_dp(dev, true, true);
  7277. } else {
  7278. lpt_disable_clkout_dp(dev);
  7279. }
  7280. }
  7281. /*
  7282. * Initialize reference clocks when the driver loads
  7283. */
  7284. void intel_init_pch_refclk(struct drm_device *dev)
  7285. {
  7286. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7287. ironlake_init_pch_refclk(dev);
  7288. else if (HAS_PCH_LPT(dev))
  7289. lpt_init_pch_refclk(dev);
  7290. }
  7291. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7292. {
  7293. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7294. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7295. int pipe = intel_crtc->pipe;
  7296. uint32_t val;
  7297. val = 0;
  7298. switch (intel_crtc->config->pipe_bpp) {
  7299. case 18:
  7300. val |= PIPECONF_6BPC;
  7301. break;
  7302. case 24:
  7303. val |= PIPECONF_8BPC;
  7304. break;
  7305. case 30:
  7306. val |= PIPECONF_10BPC;
  7307. break;
  7308. case 36:
  7309. val |= PIPECONF_12BPC;
  7310. break;
  7311. default:
  7312. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7313. BUG();
  7314. }
  7315. if (intel_crtc->config->dither)
  7316. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7317. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7318. val |= PIPECONF_INTERLACED_ILK;
  7319. else
  7320. val |= PIPECONF_PROGRESSIVE;
  7321. if (intel_crtc->config->limited_color_range)
  7322. val |= PIPECONF_COLOR_RANGE_SELECT;
  7323. I915_WRITE(PIPECONF(pipe), val);
  7324. POSTING_READ(PIPECONF(pipe));
  7325. }
  7326. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7327. {
  7328. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7329. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7330. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7331. u32 val = 0;
  7332. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7333. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7334. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7335. val |= PIPECONF_INTERLACED_ILK;
  7336. else
  7337. val |= PIPECONF_PROGRESSIVE;
  7338. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7339. POSTING_READ(PIPECONF(cpu_transcoder));
  7340. }
  7341. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7342. {
  7343. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7344. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7345. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7346. u32 val = 0;
  7347. switch (intel_crtc->config->pipe_bpp) {
  7348. case 18:
  7349. val |= PIPEMISC_DITHER_6_BPC;
  7350. break;
  7351. case 24:
  7352. val |= PIPEMISC_DITHER_8_BPC;
  7353. break;
  7354. case 30:
  7355. val |= PIPEMISC_DITHER_10_BPC;
  7356. break;
  7357. case 36:
  7358. val |= PIPEMISC_DITHER_12_BPC;
  7359. break;
  7360. default:
  7361. /* Case prevented by pipe_config_set_bpp. */
  7362. BUG();
  7363. }
  7364. if (intel_crtc->config->dither)
  7365. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7366. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7367. }
  7368. }
  7369. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7370. {
  7371. /*
  7372. * Account for spread spectrum to avoid
  7373. * oversubscribing the link. Max center spread
  7374. * is 2.5%; use 5% for safety's sake.
  7375. */
  7376. u32 bps = target_clock * bpp * 21 / 20;
  7377. return DIV_ROUND_UP(bps, link_bw * 8);
  7378. }
  7379. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7380. {
  7381. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7382. }
  7383. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7384. struct intel_crtc_state *crtc_state,
  7385. intel_clock_t *reduced_clock)
  7386. {
  7387. struct drm_crtc *crtc = &intel_crtc->base;
  7388. struct drm_device *dev = crtc->dev;
  7389. struct drm_i915_private *dev_priv = dev->dev_private;
  7390. struct drm_atomic_state *state = crtc_state->base.state;
  7391. struct drm_connector *connector;
  7392. struct drm_connector_state *connector_state;
  7393. struct intel_encoder *encoder;
  7394. u32 dpll, fp, fp2;
  7395. int factor, i;
  7396. bool is_lvds = false, is_sdvo = false;
  7397. for_each_connector_in_state(state, connector, connector_state, i) {
  7398. if (connector_state->crtc != crtc_state->base.crtc)
  7399. continue;
  7400. encoder = to_intel_encoder(connector_state->best_encoder);
  7401. switch (encoder->type) {
  7402. case INTEL_OUTPUT_LVDS:
  7403. is_lvds = true;
  7404. break;
  7405. case INTEL_OUTPUT_SDVO:
  7406. case INTEL_OUTPUT_HDMI:
  7407. is_sdvo = true;
  7408. break;
  7409. default:
  7410. break;
  7411. }
  7412. }
  7413. /* Enable autotuning of the PLL clock (if permissible) */
  7414. factor = 21;
  7415. if (is_lvds) {
  7416. if ((intel_panel_use_ssc(dev_priv) &&
  7417. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7418. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7419. factor = 25;
  7420. } else if (crtc_state->sdvo_tv_clock)
  7421. factor = 20;
  7422. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7423. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7424. fp |= FP_CB_TUNE;
  7425. if (reduced_clock) {
  7426. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7427. if (reduced_clock->m < factor * reduced_clock->n)
  7428. fp2 |= FP_CB_TUNE;
  7429. } else {
  7430. fp2 = fp;
  7431. }
  7432. dpll = 0;
  7433. if (is_lvds)
  7434. dpll |= DPLLB_MODE_LVDS;
  7435. else
  7436. dpll |= DPLLB_MODE_DAC_SERIAL;
  7437. dpll |= (crtc_state->pixel_multiplier - 1)
  7438. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7439. if (is_sdvo)
  7440. dpll |= DPLL_SDVO_HIGH_SPEED;
  7441. if (crtc_state->has_dp_encoder)
  7442. dpll |= DPLL_SDVO_HIGH_SPEED;
  7443. /* compute bitmask from p1 value */
  7444. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7445. /* also FPA1 */
  7446. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7447. switch (crtc_state->dpll.p2) {
  7448. case 5:
  7449. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7450. break;
  7451. case 7:
  7452. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7453. break;
  7454. case 10:
  7455. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7456. break;
  7457. case 14:
  7458. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7459. break;
  7460. }
  7461. if (is_lvds && intel_panel_use_ssc(dev_priv))
  7462. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7463. else
  7464. dpll |= PLL_REF_INPUT_DREFCLK;
  7465. dpll |= DPLL_VCO_ENABLE;
  7466. crtc_state->dpll_hw_state.dpll = dpll;
  7467. crtc_state->dpll_hw_state.fp0 = fp;
  7468. crtc_state->dpll_hw_state.fp1 = fp2;
  7469. }
  7470. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7471. struct intel_crtc_state *crtc_state)
  7472. {
  7473. struct drm_device *dev = crtc->base.dev;
  7474. struct drm_i915_private *dev_priv = dev->dev_private;
  7475. intel_clock_t reduced_clock;
  7476. bool has_reduced_clock = false;
  7477. struct intel_shared_dpll *pll;
  7478. const intel_limit_t *limit;
  7479. int refclk = 120000;
  7480. memset(&crtc_state->dpll_hw_state, 0,
  7481. sizeof(crtc_state->dpll_hw_state));
  7482. crtc->lowfreq_avail = false;
  7483. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7484. if (!crtc_state->has_pch_encoder)
  7485. return 0;
  7486. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7487. if (intel_panel_use_ssc(dev_priv)) {
  7488. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7489. dev_priv->vbt.lvds_ssc_freq);
  7490. refclk = dev_priv->vbt.lvds_ssc_freq;
  7491. }
  7492. if (intel_is_dual_link_lvds(dev)) {
  7493. if (refclk == 100000)
  7494. limit = &intel_limits_ironlake_dual_lvds_100m;
  7495. else
  7496. limit = &intel_limits_ironlake_dual_lvds;
  7497. } else {
  7498. if (refclk == 100000)
  7499. limit = &intel_limits_ironlake_single_lvds_100m;
  7500. else
  7501. limit = &intel_limits_ironlake_single_lvds;
  7502. }
  7503. } else {
  7504. limit = &intel_limits_ironlake_dac;
  7505. }
  7506. if (!crtc_state->clock_set &&
  7507. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7508. refclk, NULL, &crtc_state->dpll)) {
  7509. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7510. return -EINVAL;
  7511. }
  7512. ironlake_compute_dpll(crtc, crtc_state,
  7513. has_reduced_clock ? &reduced_clock : NULL);
  7514. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  7515. if (pll == NULL) {
  7516. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7517. pipe_name(crtc->pipe));
  7518. return -EINVAL;
  7519. }
  7520. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7521. has_reduced_clock)
  7522. crtc->lowfreq_avail = true;
  7523. return 0;
  7524. }
  7525. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7526. struct intel_link_m_n *m_n)
  7527. {
  7528. struct drm_device *dev = crtc->base.dev;
  7529. struct drm_i915_private *dev_priv = dev->dev_private;
  7530. enum pipe pipe = crtc->pipe;
  7531. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7532. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7533. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7534. & ~TU_SIZE_MASK;
  7535. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7536. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7537. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7538. }
  7539. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7540. enum transcoder transcoder,
  7541. struct intel_link_m_n *m_n,
  7542. struct intel_link_m_n *m2_n2)
  7543. {
  7544. struct drm_device *dev = crtc->base.dev;
  7545. struct drm_i915_private *dev_priv = dev->dev_private;
  7546. enum pipe pipe = crtc->pipe;
  7547. if (INTEL_INFO(dev)->gen >= 5) {
  7548. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7549. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7550. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7551. & ~TU_SIZE_MASK;
  7552. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7553. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7554. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7555. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7556. * gen < 8) and if DRRS is supported (to make sure the
  7557. * registers are not unnecessarily read).
  7558. */
  7559. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7560. crtc->config->has_drrs) {
  7561. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7562. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7563. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7564. & ~TU_SIZE_MASK;
  7565. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7566. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7567. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7568. }
  7569. } else {
  7570. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7571. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7572. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7573. & ~TU_SIZE_MASK;
  7574. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7575. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7576. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7577. }
  7578. }
  7579. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7580. struct intel_crtc_state *pipe_config)
  7581. {
  7582. if (pipe_config->has_pch_encoder)
  7583. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7584. else
  7585. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7586. &pipe_config->dp_m_n,
  7587. &pipe_config->dp_m2_n2);
  7588. }
  7589. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7590. struct intel_crtc_state *pipe_config)
  7591. {
  7592. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7593. &pipe_config->fdi_m_n, NULL);
  7594. }
  7595. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7596. struct intel_crtc_state *pipe_config)
  7597. {
  7598. struct drm_device *dev = crtc->base.dev;
  7599. struct drm_i915_private *dev_priv = dev->dev_private;
  7600. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7601. uint32_t ps_ctrl = 0;
  7602. int id = -1;
  7603. int i;
  7604. /* find scaler attached to this pipe */
  7605. for (i = 0; i < crtc->num_scalers; i++) {
  7606. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7607. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7608. id = i;
  7609. pipe_config->pch_pfit.enabled = true;
  7610. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7611. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7612. break;
  7613. }
  7614. }
  7615. scaler_state->scaler_id = id;
  7616. if (id >= 0) {
  7617. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7618. } else {
  7619. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7620. }
  7621. }
  7622. static void
  7623. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7624. struct intel_initial_plane_config *plane_config)
  7625. {
  7626. struct drm_device *dev = crtc->base.dev;
  7627. struct drm_i915_private *dev_priv = dev->dev_private;
  7628. u32 val, base, offset, stride_mult, tiling;
  7629. int pipe = crtc->pipe;
  7630. int fourcc, pixel_format;
  7631. unsigned int aligned_height;
  7632. struct drm_framebuffer *fb;
  7633. struct intel_framebuffer *intel_fb;
  7634. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7635. if (!intel_fb) {
  7636. DRM_DEBUG_KMS("failed to alloc fb\n");
  7637. return;
  7638. }
  7639. fb = &intel_fb->base;
  7640. val = I915_READ(PLANE_CTL(pipe, 0));
  7641. if (!(val & PLANE_CTL_ENABLE))
  7642. goto error;
  7643. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7644. fourcc = skl_format_to_fourcc(pixel_format,
  7645. val & PLANE_CTL_ORDER_RGBX,
  7646. val & PLANE_CTL_ALPHA_MASK);
  7647. fb->pixel_format = fourcc;
  7648. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7649. tiling = val & PLANE_CTL_TILED_MASK;
  7650. switch (tiling) {
  7651. case PLANE_CTL_TILED_LINEAR:
  7652. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7653. break;
  7654. case PLANE_CTL_TILED_X:
  7655. plane_config->tiling = I915_TILING_X;
  7656. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7657. break;
  7658. case PLANE_CTL_TILED_Y:
  7659. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7660. break;
  7661. case PLANE_CTL_TILED_YF:
  7662. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7663. break;
  7664. default:
  7665. MISSING_CASE(tiling);
  7666. goto error;
  7667. }
  7668. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7669. plane_config->base = base;
  7670. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7671. val = I915_READ(PLANE_SIZE(pipe, 0));
  7672. fb->height = ((val >> 16) & 0xfff) + 1;
  7673. fb->width = ((val >> 0) & 0x1fff) + 1;
  7674. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7675. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  7676. fb->pixel_format);
  7677. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7678. aligned_height = intel_fb_align_height(dev, fb->height,
  7679. fb->pixel_format,
  7680. fb->modifier[0]);
  7681. plane_config->size = fb->pitches[0] * aligned_height;
  7682. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7683. pipe_name(pipe), fb->width, fb->height,
  7684. fb->bits_per_pixel, base, fb->pitches[0],
  7685. plane_config->size);
  7686. plane_config->fb = intel_fb;
  7687. return;
  7688. error:
  7689. kfree(fb);
  7690. }
  7691. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7692. struct intel_crtc_state *pipe_config)
  7693. {
  7694. struct drm_device *dev = crtc->base.dev;
  7695. struct drm_i915_private *dev_priv = dev->dev_private;
  7696. uint32_t tmp;
  7697. tmp = I915_READ(PF_CTL(crtc->pipe));
  7698. if (tmp & PF_ENABLE) {
  7699. pipe_config->pch_pfit.enabled = true;
  7700. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7701. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7702. /* We currently do not free assignements of panel fitters on
  7703. * ivb/hsw (since we don't use the higher upscaling modes which
  7704. * differentiates them) so just WARN about this case for now. */
  7705. if (IS_GEN7(dev)) {
  7706. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7707. PF_PIPE_SEL_IVB(crtc->pipe));
  7708. }
  7709. }
  7710. }
  7711. static void
  7712. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7713. struct intel_initial_plane_config *plane_config)
  7714. {
  7715. struct drm_device *dev = crtc->base.dev;
  7716. struct drm_i915_private *dev_priv = dev->dev_private;
  7717. u32 val, base, offset;
  7718. int pipe = crtc->pipe;
  7719. int fourcc, pixel_format;
  7720. unsigned int aligned_height;
  7721. struct drm_framebuffer *fb;
  7722. struct intel_framebuffer *intel_fb;
  7723. val = I915_READ(DSPCNTR(pipe));
  7724. if (!(val & DISPLAY_PLANE_ENABLE))
  7725. return;
  7726. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7727. if (!intel_fb) {
  7728. DRM_DEBUG_KMS("failed to alloc fb\n");
  7729. return;
  7730. }
  7731. fb = &intel_fb->base;
  7732. if (INTEL_INFO(dev)->gen >= 4) {
  7733. if (val & DISPPLANE_TILED) {
  7734. plane_config->tiling = I915_TILING_X;
  7735. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7736. }
  7737. }
  7738. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7739. fourcc = i9xx_format_to_fourcc(pixel_format);
  7740. fb->pixel_format = fourcc;
  7741. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7742. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7743. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7744. offset = I915_READ(DSPOFFSET(pipe));
  7745. } else {
  7746. if (plane_config->tiling)
  7747. offset = I915_READ(DSPTILEOFF(pipe));
  7748. else
  7749. offset = I915_READ(DSPLINOFF(pipe));
  7750. }
  7751. plane_config->base = base;
  7752. val = I915_READ(PIPESRC(pipe));
  7753. fb->width = ((val >> 16) & 0xfff) + 1;
  7754. fb->height = ((val >> 0) & 0xfff) + 1;
  7755. val = I915_READ(DSPSTRIDE(pipe));
  7756. fb->pitches[0] = val & 0xffffffc0;
  7757. aligned_height = intel_fb_align_height(dev, fb->height,
  7758. fb->pixel_format,
  7759. fb->modifier[0]);
  7760. plane_config->size = fb->pitches[0] * aligned_height;
  7761. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7762. pipe_name(pipe), fb->width, fb->height,
  7763. fb->bits_per_pixel, base, fb->pitches[0],
  7764. plane_config->size);
  7765. plane_config->fb = intel_fb;
  7766. }
  7767. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7768. struct intel_crtc_state *pipe_config)
  7769. {
  7770. struct drm_device *dev = crtc->base.dev;
  7771. struct drm_i915_private *dev_priv = dev->dev_private;
  7772. enum intel_display_power_domain power_domain;
  7773. uint32_t tmp;
  7774. bool ret;
  7775. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7776. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7777. return false;
  7778. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7779. pipe_config->shared_dpll = NULL;
  7780. ret = false;
  7781. tmp = I915_READ(PIPECONF(crtc->pipe));
  7782. if (!(tmp & PIPECONF_ENABLE))
  7783. goto out;
  7784. switch (tmp & PIPECONF_BPC_MASK) {
  7785. case PIPECONF_6BPC:
  7786. pipe_config->pipe_bpp = 18;
  7787. break;
  7788. case PIPECONF_8BPC:
  7789. pipe_config->pipe_bpp = 24;
  7790. break;
  7791. case PIPECONF_10BPC:
  7792. pipe_config->pipe_bpp = 30;
  7793. break;
  7794. case PIPECONF_12BPC:
  7795. pipe_config->pipe_bpp = 36;
  7796. break;
  7797. default:
  7798. break;
  7799. }
  7800. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7801. pipe_config->limited_color_range = true;
  7802. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7803. struct intel_shared_dpll *pll;
  7804. enum intel_dpll_id pll_id;
  7805. pipe_config->has_pch_encoder = true;
  7806. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7807. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7808. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7809. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7810. if (HAS_PCH_IBX(dev_priv)) {
  7811. pll_id = (enum intel_dpll_id) crtc->pipe;
  7812. } else {
  7813. tmp = I915_READ(PCH_DPLL_SEL);
  7814. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7815. pll_id = DPLL_ID_PCH_PLL_B;
  7816. else
  7817. pll_id= DPLL_ID_PCH_PLL_A;
  7818. }
  7819. pipe_config->shared_dpll =
  7820. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7821. pll = pipe_config->shared_dpll;
  7822. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7823. &pipe_config->dpll_hw_state));
  7824. tmp = pipe_config->dpll_hw_state.dpll;
  7825. pipe_config->pixel_multiplier =
  7826. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7827. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7828. ironlake_pch_clock_get(crtc, pipe_config);
  7829. } else {
  7830. pipe_config->pixel_multiplier = 1;
  7831. }
  7832. intel_get_pipe_timings(crtc, pipe_config);
  7833. intel_get_pipe_src_size(crtc, pipe_config);
  7834. ironlake_get_pfit_config(crtc, pipe_config);
  7835. ret = true;
  7836. out:
  7837. intel_display_power_put(dev_priv, power_domain);
  7838. return ret;
  7839. }
  7840. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7841. {
  7842. struct drm_device *dev = dev_priv->dev;
  7843. struct intel_crtc *crtc;
  7844. for_each_intel_crtc(dev, crtc)
  7845. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7846. pipe_name(crtc->pipe));
  7847. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7848. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7849. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7850. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7851. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7852. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7853. "CPU PWM1 enabled\n");
  7854. if (IS_HASWELL(dev))
  7855. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7856. "CPU PWM2 enabled\n");
  7857. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7858. "PCH PWM1 enabled\n");
  7859. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7860. "Utility pin enabled\n");
  7861. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7862. /*
  7863. * In theory we can still leave IRQs enabled, as long as only the HPD
  7864. * interrupts remain enabled. We used to check for that, but since it's
  7865. * gen-specific and since we only disable LCPLL after we fully disable
  7866. * the interrupts, the check below should be enough.
  7867. */
  7868. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7869. }
  7870. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7871. {
  7872. struct drm_device *dev = dev_priv->dev;
  7873. if (IS_HASWELL(dev))
  7874. return I915_READ(D_COMP_HSW);
  7875. else
  7876. return I915_READ(D_COMP_BDW);
  7877. }
  7878. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7879. {
  7880. struct drm_device *dev = dev_priv->dev;
  7881. if (IS_HASWELL(dev)) {
  7882. mutex_lock(&dev_priv->rps.hw_lock);
  7883. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7884. val))
  7885. DRM_ERROR("Failed to write to D_COMP\n");
  7886. mutex_unlock(&dev_priv->rps.hw_lock);
  7887. } else {
  7888. I915_WRITE(D_COMP_BDW, val);
  7889. POSTING_READ(D_COMP_BDW);
  7890. }
  7891. }
  7892. /*
  7893. * This function implements pieces of two sequences from BSpec:
  7894. * - Sequence for display software to disable LCPLL
  7895. * - Sequence for display software to allow package C8+
  7896. * The steps implemented here are just the steps that actually touch the LCPLL
  7897. * register. Callers should take care of disabling all the display engine
  7898. * functions, doing the mode unset, fixing interrupts, etc.
  7899. */
  7900. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7901. bool switch_to_fclk, bool allow_power_down)
  7902. {
  7903. uint32_t val;
  7904. assert_can_disable_lcpll(dev_priv);
  7905. val = I915_READ(LCPLL_CTL);
  7906. if (switch_to_fclk) {
  7907. val |= LCPLL_CD_SOURCE_FCLK;
  7908. I915_WRITE(LCPLL_CTL, val);
  7909. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7910. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7911. DRM_ERROR("Switching to FCLK failed\n");
  7912. val = I915_READ(LCPLL_CTL);
  7913. }
  7914. val |= LCPLL_PLL_DISABLE;
  7915. I915_WRITE(LCPLL_CTL, val);
  7916. POSTING_READ(LCPLL_CTL);
  7917. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7918. DRM_ERROR("LCPLL still locked\n");
  7919. val = hsw_read_dcomp(dev_priv);
  7920. val |= D_COMP_COMP_DISABLE;
  7921. hsw_write_dcomp(dev_priv, val);
  7922. ndelay(100);
  7923. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7924. 1))
  7925. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7926. if (allow_power_down) {
  7927. val = I915_READ(LCPLL_CTL);
  7928. val |= LCPLL_POWER_DOWN_ALLOW;
  7929. I915_WRITE(LCPLL_CTL, val);
  7930. POSTING_READ(LCPLL_CTL);
  7931. }
  7932. }
  7933. /*
  7934. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7935. * source.
  7936. */
  7937. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7938. {
  7939. uint32_t val;
  7940. val = I915_READ(LCPLL_CTL);
  7941. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7942. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7943. return;
  7944. /*
  7945. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7946. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7947. */
  7948. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7949. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7950. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7951. I915_WRITE(LCPLL_CTL, val);
  7952. POSTING_READ(LCPLL_CTL);
  7953. }
  7954. val = hsw_read_dcomp(dev_priv);
  7955. val |= D_COMP_COMP_FORCE;
  7956. val &= ~D_COMP_COMP_DISABLE;
  7957. hsw_write_dcomp(dev_priv, val);
  7958. val = I915_READ(LCPLL_CTL);
  7959. val &= ~LCPLL_PLL_DISABLE;
  7960. I915_WRITE(LCPLL_CTL, val);
  7961. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7962. DRM_ERROR("LCPLL not locked yet\n");
  7963. if (val & LCPLL_CD_SOURCE_FCLK) {
  7964. val = I915_READ(LCPLL_CTL);
  7965. val &= ~LCPLL_CD_SOURCE_FCLK;
  7966. I915_WRITE(LCPLL_CTL, val);
  7967. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7968. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7969. DRM_ERROR("Switching back to LCPLL failed\n");
  7970. }
  7971. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7972. intel_update_cdclk(dev_priv->dev);
  7973. }
  7974. /*
  7975. * Package states C8 and deeper are really deep PC states that can only be
  7976. * reached when all the devices on the system allow it, so even if the graphics
  7977. * device allows PC8+, it doesn't mean the system will actually get to these
  7978. * states. Our driver only allows PC8+ when going into runtime PM.
  7979. *
  7980. * The requirements for PC8+ are that all the outputs are disabled, the power
  7981. * well is disabled and most interrupts are disabled, and these are also
  7982. * requirements for runtime PM. When these conditions are met, we manually do
  7983. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7984. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7985. * hang the machine.
  7986. *
  7987. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7988. * the state of some registers, so when we come back from PC8+ we need to
  7989. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7990. * need to take care of the registers kept by RC6. Notice that this happens even
  7991. * if we don't put the device in PCI D3 state (which is what currently happens
  7992. * because of the runtime PM support).
  7993. *
  7994. * For more, read "Display Sequences for Package C8" on the hardware
  7995. * documentation.
  7996. */
  7997. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7998. {
  7999. struct drm_device *dev = dev_priv->dev;
  8000. uint32_t val;
  8001. DRM_DEBUG_KMS("Enabling package C8+\n");
  8002. if (HAS_PCH_LPT_LP(dev)) {
  8003. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8004. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8005. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8006. }
  8007. lpt_disable_clkout_dp(dev);
  8008. hsw_disable_lcpll(dev_priv, true, true);
  8009. }
  8010. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8011. {
  8012. struct drm_device *dev = dev_priv->dev;
  8013. uint32_t val;
  8014. DRM_DEBUG_KMS("Disabling package C8+\n");
  8015. hsw_restore_lcpll(dev_priv);
  8016. lpt_init_pch_refclk(dev);
  8017. if (HAS_PCH_LPT_LP(dev)) {
  8018. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8019. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8020. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8021. }
  8022. }
  8023. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8024. {
  8025. struct drm_device *dev = old_state->dev;
  8026. struct intel_atomic_state *old_intel_state =
  8027. to_intel_atomic_state(old_state);
  8028. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8029. broxton_set_cdclk(to_i915(dev), req_cdclk);
  8030. }
  8031. /* compute the max rate for new configuration */
  8032. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8033. {
  8034. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8035. struct drm_i915_private *dev_priv = state->dev->dev_private;
  8036. struct drm_crtc *crtc;
  8037. struct drm_crtc_state *cstate;
  8038. struct intel_crtc_state *crtc_state;
  8039. unsigned max_pixel_rate = 0, i;
  8040. enum pipe pipe;
  8041. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8042. sizeof(intel_state->min_pixclk));
  8043. for_each_crtc_in_state(state, crtc, cstate, i) {
  8044. int pixel_rate;
  8045. crtc_state = to_intel_crtc_state(cstate);
  8046. if (!crtc_state->base.enable) {
  8047. intel_state->min_pixclk[i] = 0;
  8048. continue;
  8049. }
  8050. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8051. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8052. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8053. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8054. intel_state->min_pixclk[i] = pixel_rate;
  8055. }
  8056. for_each_pipe(dev_priv, pipe)
  8057. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8058. return max_pixel_rate;
  8059. }
  8060. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8061. {
  8062. struct drm_i915_private *dev_priv = dev->dev_private;
  8063. uint32_t val, data;
  8064. int ret;
  8065. if (WARN((I915_READ(LCPLL_CTL) &
  8066. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8067. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8068. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8069. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8070. "trying to change cdclk frequency with cdclk not enabled\n"))
  8071. return;
  8072. mutex_lock(&dev_priv->rps.hw_lock);
  8073. ret = sandybridge_pcode_write(dev_priv,
  8074. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8075. mutex_unlock(&dev_priv->rps.hw_lock);
  8076. if (ret) {
  8077. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8078. return;
  8079. }
  8080. val = I915_READ(LCPLL_CTL);
  8081. val |= LCPLL_CD_SOURCE_FCLK;
  8082. I915_WRITE(LCPLL_CTL, val);
  8083. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8084. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8085. DRM_ERROR("Switching to FCLK failed\n");
  8086. val = I915_READ(LCPLL_CTL);
  8087. val &= ~LCPLL_CLK_FREQ_MASK;
  8088. switch (cdclk) {
  8089. case 450000:
  8090. val |= LCPLL_CLK_FREQ_450;
  8091. data = 0;
  8092. break;
  8093. case 540000:
  8094. val |= LCPLL_CLK_FREQ_54O_BDW;
  8095. data = 1;
  8096. break;
  8097. case 337500:
  8098. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8099. data = 2;
  8100. break;
  8101. case 675000:
  8102. val |= LCPLL_CLK_FREQ_675_BDW;
  8103. data = 3;
  8104. break;
  8105. default:
  8106. WARN(1, "invalid cdclk frequency\n");
  8107. return;
  8108. }
  8109. I915_WRITE(LCPLL_CTL, val);
  8110. val = I915_READ(LCPLL_CTL);
  8111. val &= ~LCPLL_CD_SOURCE_FCLK;
  8112. I915_WRITE(LCPLL_CTL, val);
  8113. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8114. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8115. DRM_ERROR("Switching back to LCPLL failed\n");
  8116. mutex_lock(&dev_priv->rps.hw_lock);
  8117. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8118. mutex_unlock(&dev_priv->rps.hw_lock);
  8119. intel_update_cdclk(dev);
  8120. WARN(cdclk != dev_priv->cdclk_freq,
  8121. "cdclk requested %d kHz but got %d kHz\n",
  8122. cdclk, dev_priv->cdclk_freq);
  8123. }
  8124. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8125. {
  8126. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8127. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8128. int max_pixclk = ilk_max_pixel_rate(state);
  8129. int cdclk;
  8130. /*
  8131. * FIXME should also account for plane ratio
  8132. * once 64bpp pixel formats are supported.
  8133. */
  8134. if (max_pixclk > 540000)
  8135. cdclk = 675000;
  8136. else if (max_pixclk > 450000)
  8137. cdclk = 540000;
  8138. else if (max_pixclk > 337500)
  8139. cdclk = 450000;
  8140. else
  8141. cdclk = 337500;
  8142. if (cdclk > dev_priv->max_cdclk_freq) {
  8143. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8144. cdclk, dev_priv->max_cdclk_freq);
  8145. return -EINVAL;
  8146. }
  8147. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8148. if (!intel_state->active_crtcs)
  8149. intel_state->dev_cdclk = 337500;
  8150. return 0;
  8151. }
  8152. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8153. {
  8154. struct drm_device *dev = old_state->dev;
  8155. struct intel_atomic_state *old_intel_state =
  8156. to_intel_atomic_state(old_state);
  8157. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8158. broadwell_set_cdclk(dev, req_cdclk);
  8159. }
  8160. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8161. struct intel_crtc_state *crtc_state)
  8162. {
  8163. struct intel_encoder *intel_encoder =
  8164. intel_ddi_get_crtc_new_encoder(crtc_state);
  8165. if (intel_encoder->type != INTEL_OUTPUT_DSI) {
  8166. if (!intel_ddi_pll_select(crtc, crtc_state))
  8167. return -EINVAL;
  8168. }
  8169. crtc->lowfreq_avail = false;
  8170. return 0;
  8171. }
  8172. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8173. enum port port,
  8174. struct intel_crtc_state *pipe_config)
  8175. {
  8176. enum intel_dpll_id id;
  8177. switch (port) {
  8178. case PORT_A:
  8179. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8180. id = DPLL_ID_SKL_DPLL0;
  8181. break;
  8182. case PORT_B:
  8183. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8184. id = DPLL_ID_SKL_DPLL1;
  8185. break;
  8186. case PORT_C:
  8187. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8188. id = DPLL_ID_SKL_DPLL2;
  8189. break;
  8190. default:
  8191. DRM_ERROR("Incorrect port type\n");
  8192. return;
  8193. }
  8194. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8195. }
  8196. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8197. enum port port,
  8198. struct intel_crtc_state *pipe_config)
  8199. {
  8200. enum intel_dpll_id id;
  8201. u32 temp;
  8202. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8203. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8204. switch (pipe_config->ddi_pll_sel) {
  8205. case SKL_DPLL0:
  8206. id = DPLL_ID_SKL_DPLL0;
  8207. break;
  8208. case SKL_DPLL1:
  8209. id = DPLL_ID_SKL_DPLL1;
  8210. break;
  8211. case SKL_DPLL2:
  8212. id = DPLL_ID_SKL_DPLL2;
  8213. break;
  8214. case SKL_DPLL3:
  8215. id = DPLL_ID_SKL_DPLL3;
  8216. break;
  8217. default:
  8218. MISSING_CASE(pipe_config->ddi_pll_sel);
  8219. return;
  8220. }
  8221. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8222. }
  8223. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8224. enum port port,
  8225. struct intel_crtc_state *pipe_config)
  8226. {
  8227. enum intel_dpll_id id;
  8228. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8229. switch (pipe_config->ddi_pll_sel) {
  8230. case PORT_CLK_SEL_WRPLL1:
  8231. id = DPLL_ID_WRPLL1;
  8232. break;
  8233. case PORT_CLK_SEL_WRPLL2:
  8234. id = DPLL_ID_WRPLL2;
  8235. break;
  8236. case PORT_CLK_SEL_SPLL:
  8237. id = DPLL_ID_SPLL;
  8238. break;
  8239. case PORT_CLK_SEL_LCPLL_810:
  8240. id = DPLL_ID_LCPLL_810;
  8241. break;
  8242. case PORT_CLK_SEL_LCPLL_1350:
  8243. id = DPLL_ID_LCPLL_1350;
  8244. break;
  8245. case PORT_CLK_SEL_LCPLL_2700:
  8246. id = DPLL_ID_LCPLL_2700;
  8247. break;
  8248. default:
  8249. MISSING_CASE(pipe_config->ddi_pll_sel);
  8250. /* fall through */
  8251. case PORT_CLK_SEL_NONE:
  8252. return;
  8253. }
  8254. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8255. }
  8256. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8257. struct intel_crtc_state *pipe_config,
  8258. unsigned long *power_domain_mask)
  8259. {
  8260. struct drm_device *dev = crtc->base.dev;
  8261. struct drm_i915_private *dev_priv = dev->dev_private;
  8262. enum intel_display_power_domain power_domain;
  8263. u32 tmp;
  8264. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8265. /*
  8266. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8267. * consistency and less surprising code; it's in always on power).
  8268. */
  8269. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8270. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8271. enum pipe trans_edp_pipe;
  8272. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8273. default:
  8274. WARN(1, "unknown pipe linked to edp transcoder\n");
  8275. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8276. case TRANS_DDI_EDP_INPUT_A_ON:
  8277. trans_edp_pipe = PIPE_A;
  8278. break;
  8279. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8280. trans_edp_pipe = PIPE_B;
  8281. break;
  8282. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8283. trans_edp_pipe = PIPE_C;
  8284. break;
  8285. }
  8286. if (trans_edp_pipe == crtc->pipe)
  8287. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8288. }
  8289. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8290. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8291. return false;
  8292. *power_domain_mask |= BIT(power_domain);
  8293. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8294. return tmp & PIPECONF_ENABLE;
  8295. }
  8296. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8297. struct intel_crtc_state *pipe_config,
  8298. unsigned long *power_domain_mask)
  8299. {
  8300. struct drm_device *dev = crtc->base.dev;
  8301. struct drm_i915_private *dev_priv = dev->dev_private;
  8302. enum intel_display_power_domain power_domain;
  8303. enum port port;
  8304. enum transcoder cpu_transcoder;
  8305. u32 tmp;
  8306. pipe_config->has_dsi_encoder = false;
  8307. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8308. if (port == PORT_A)
  8309. cpu_transcoder = TRANSCODER_DSI_A;
  8310. else
  8311. cpu_transcoder = TRANSCODER_DSI_C;
  8312. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8313. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8314. continue;
  8315. *power_domain_mask |= BIT(power_domain);
  8316. /*
  8317. * The PLL needs to be enabled with a valid divider
  8318. * configuration, otherwise accessing DSI registers will hang
  8319. * the machine. See BSpec North Display Engine
  8320. * registers/MIPI[BXT]. We can break out here early, since we
  8321. * need the same DSI PLL to be enabled for both DSI ports.
  8322. */
  8323. if (!intel_dsi_pll_is_enabled(dev_priv))
  8324. break;
  8325. /* XXX: this works for video mode only */
  8326. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8327. if (!(tmp & DPI_ENABLE))
  8328. continue;
  8329. tmp = I915_READ(MIPI_CTRL(port));
  8330. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8331. continue;
  8332. pipe_config->cpu_transcoder = cpu_transcoder;
  8333. pipe_config->has_dsi_encoder = true;
  8334. break;
  8335. }
  8336. return pipe_config->has_dsi_encoder;
  8337. }
  8338. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8339. struct intel_crtc_state *pipe_config)
  8340. {
  8341. struct drm_device *dev = crtc->base.dev;
  8342. struct drm_i915_private *dev_priv = dev->dev_private;
  8343. struct intel_shared_dpll *pll;
  8344. enum port port;
  8345. uint32_t tmp;
  8346. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8347. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8348. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8349. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8350. else if (IS_BROXTON(dev))
  8351. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8352. else
  8353. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8354. pll = pipe_config->shared_dpll;
  8355. if (pll) {
  8356. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8357. &pipe_config->dpll_hw_state));
  8358. }
  8359. /*
  8360. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8361. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8362. * the PCH transcoder is on.
  8363. */
  8364. if (INTEL_INFO(dev)->gen < 9 &&
  8365. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8366. pipe_config->has_pch_encoder = true;
  8367. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8368. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8369. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8370. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8371. }
  8372. }
  8373. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8374. struct intel_crtc_state *pipe_config)
  8375. {
  8376. struct drm_device *dev = crtc->base.dev;
  8377. struct drm_i915_private *dev_priv = dev->dev_private;
  8378. enum intel_display_power_domain power_domain;
  8379. unsigned long power_domain_mask;
  8380. bool active;
  8381. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8382. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8383. return false;
  8384. power_domain_mask = BIT(power_domain);
  8385. pipe_config->shared_dpll = NULL;
  8386. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8387. if (IS_BROXTON(dev_priv)) {
  8388. bxt_get_dsi_transcoder_state(crtc, pipe_config,
  8389. &power_domain_mask);
  8390. WARN_ON(active && pipe_config->has_dsi_encoder);
  8391. if (pipe_config->has_dsi_encoder)
  8392. active = true;
  8393. }
  8394. if (!active)
  8395. goto out;
  8396. if (!pipe_config->has_dsi_encoder) {
  8397. haswell_get_ddi_port_state(crtc, pipe_config);
  8398. intel_get_pipe_timings(crtc, pipe_config);
  8399. }
  8400. intel_get_pipe_src_size(crtc, pipe_config);
  8401. pipe_config->gamma_mode =
  8402. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8403. if (INTEL_INFO(dev)->gen >= 9) {
  8404. skl_init_scalers(dev, crtc, pipe_config);
  8405. }
  8406. if (INTEL_INFO(dev)->gen >= 9) {
  8407. pipe_config->scaler_state.scaler_id = -1;
  8408. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8409. }
  8410. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8411. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8412. power_domain_mask |= BIT(power_domain);
  8413. if (INTEL_INFO(dev)->gen >= 9)
  8414. skylake_get_pfit_config(crtc, pipe_config);
  8415. else
  8416. ironlake_get_pfit_config(crtc, pipe_config);
  8417. }
  8418. if (IS_HASWELL(dev))
  8419. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8420. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8421. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8422. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8423. pipe_config->pixel_multiplier =
  8424. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8425. } else {
  8426. pipe_config->pixel_multiplier = 1;
  8427. }
  8428. out:
  8429. for_each_power_domain(power_domain, power_domain_mask)
  8430. intel_display_power_put(dev_priv, power_domain);
  8431. return active;
  8432. }
  8433. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  8434. const struct intel_plane_state *plane_state)
  8435. {
  8436. struct drm_device *dev = crtc->dev;
  8437. struct drm_i915_private *dev_priv = dev->dev_private;
  8438. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8439. uint32_t cntl = 0, size = 0;
  8440. if (plane_state && plane_state->visible) {
  8441. unsigned int width = plane_state->base.crtc_w;
  8442. unsigned int height = plane_state->base.crtc_h;
  8443. unsigned int stride = roundup_pow_of_two(width) * 4;
  8444. switch (stride) {
  8445. default:
  8446. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8447. width, stride);
  8448. stride = 256;
  8449. /* fallthrough */
  8450. case 256:
  8451. case 512:
  8452. case 1024:
  8453. case 2048:
  8454. break;
  8455. }
  8456. cntl |= CURSOR_ENABLE |
  8457. CURSOR_GAMMA_ENABLE |
  8458. CURSOR_FORMAT_ARGB |
  8459. CURSOR_STRIDE(stride);
  8460. size = (height << 12) | width;
  8461. }
  8462. if (intel_crtc->cursor_cntl != 0 &&
  8463. (intel_crtc->cursor_base != base ||
  8464. intel_crtc->cursor_size != size ||
  8465. intel_crtc->cursor_cntl != cntl)) {
  8466. /* On these chipsets we can only modify the base/size/stride
  8467. * whilst the cursor is disabled.
  8468. */
  8469. I915_WRITE(CURCNTR(PIPE_A), 0);
  8470. POSTING_READ(CURCNTR(PIPE_A));
  8471. intel_crtc->cursor_cntl = 0;
  8472. }
  8473. if (intel_crtc->cursor_base != base) {
  8474. I915_WRITE(CURBASE(PIPE_A), base);
  8475. intel_crtc->cursor_base = base;
  8476. }
  8477. if (intel_crtc->cursor_size != size) {
  8478. I915_WRITE(CURSIZE, size);
  8479. intel_crtc->cursor_size = size;
  8480. }
  8481. if (intel_crtc->cursor_cntl != cntl) {
  8482. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8483. POSTING_READ(CURCNTR(PIPE_A));
  8484. intel_crtc->cursor_cntl = cntl;
  8485. }
  8486. }
  8487. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  8488. const struct intel_plane_state *plane_state)
  8489. {
  8490. struct drm_device *dev = crtc->dev;
  8491. struct drm_i915_private *dev_priv = dev->dev_private;
  8492. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8493. int pipe = intel_crtc->pipe;
  8494. uint32_t cntl = 0;
  8495. if (plane_state && plane_state->visible) {
  8496. cntl = MCURSOR_GAMMA_ENABLE;
  8497. switch (plane_state->base.crtc_w) {
  8498. case 64:
  8499. cntl |= CURSOR_MODE_64_ARGB_AX;
  8500. break;
  8501. case 128:
  8502. cntl |= CURSOR_MODE_128_ARGB_AX;
  8503. break;
  8504. case 256:
  8505. cntl |= CURSOR_MODE_256_ARGB_AX;
  8506. break;
  8507. default:
  8508. MISSING_CASE(plane_state->base.crtc_w);
  8509. return;
  8510. }
  8511. cntl |= pipe << 28; /* Connect to correct pipe */
  8512. if (HAS_DDI(dev))
  8513. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8514. if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
  8515. cntl |= CURSOR_ROTATE_180;
  8516. }
  8517. if (intel_crtc->cursor_cntl != cntl) {
  8518. I915_WRITE(CURCNTR(pipe), cntl);
  8519. POSTING_READ(CURCNTR(pipe));
  8520. intel_crtc->cursor_cntl = cntl;
  8521. }
  8522. /* and commit changes on next vblank */
  8523. I915_WRITE(CURBASE(pipe), base);
  8524. POSTING_READ(CURBASE(pipe));
  8525. intel_crtc->cursor_base = base;
  8526. }
  8527. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8528. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8529. const struct intel_plane_state *plane_state)
  8530. {
  8531. struct drm_device *dev = crtc->dev;
  8532. struct drm_i915_private *dev_priv = dev->dev_private;
  8533. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8534. int pipe = intel_crtc->pipe;
  8535. u32 base = intel_crtc->cursor_addr;
  8536. u32 pos = 0;
  8537. if (plane_state) {
  8538. int x = plane_state->base.crtc_x;
  8539. int y = plane_state->base.crtc_y;
  8540. if (x < 0) {
  8541. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8542. x = -x;
  8543. }
  8544. pos |= x << CURSOR_X_SHIFT;
  8545. if (y < 0) {
  8546. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8547. y = -y;
  8548. }
  8549. pos |= y << CURSOR_Y_SHIFT;
  8550. /* ILK+ do this automagically */
  8551. if (HAS_GMCH_DISPLAY(dev) &&
  8552. plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  8553. base += (plane_state->base.crtc_h *
  8554. plane_state->base.crtc_w - 1) * 4;
  8555. }
  8556. }
  8557. I915_WRITE(CURPOS(pipe), pos);
  8558. if (IS_845G(dev) || IS_I865G(dev))
  8559. i845_update_cursor(crtc, base, plane_state);
  8560. else
  8561. i9xx_update_cursor(crtc, base, plane_state);
  8562. }
  8563. static bool cursor_size_ok(struct drm_device *dev,
  8564. uint32_t width, uint32_t height)
  8565. {
  8566. if (width == 0 || height == 0)
  8567. return false;
  8568. /*
  8569. * 845g/865g are special in that they are only limited by
  8570. * the width of their cursors, the height is arbitrary up to
  8571. * the precision of the register. Everything else requires
  8572. * square cursors, limited to a few power-of-two sizes.
  8573. */
  8574. if (IS_845G(dev) || IS_I865G(dev)) {
  8575. if ((width & 63) != 0)
  8576. return false;
  8577. if (width > (IS_845G(dev) ? 64 : 512))
  8578. return false;
  8579. if (height > 1023)
  8580. return false;
  8581. } else {
  8582. switch (width | height) {
  8583. case 256:
  8584. case 128:
  8585. if (IS_GEN2(dev))
  8586. return false;
  8587. case 64:
  8588. break;
  8589. default:
  8590. return false;
  8591. }
  8592. }
  8593. return true;
  8594. }
  8595. /* VESA 640x480x72Hz mode to set on the pipe */
  8596. static struct drm_display_mode load_detect_mode = {
  8597. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8598. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8599. };
  8600. struct drm_framebuffer *
  8601. __intel_framebuffer_create(struct drm_device *dev,
  8602. struct drm_mode_fb_cmd2 *mode_cmd,
  8603. struct drm_i915_gem_object *obj)
  8604. {
  8605. struct intel_framebuffer *intel_fb;
  8606. int ret;
  8607. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8608. if (!intel_fb)
  8609. return ERR_PTR(-ENOMEM);
  8610. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8611. if (ret)
  8612. goto err;
  8613. return &intel_fb->base;
  8614. err:
  8615. kfree(intel_fb);
  8616. return ERR_PTR(ret);
  8617. }
  8618. static struct drm_framebuffer *
  8619. intel_framebuffer_create(struct drm_device *dev,
  8620. struct drm_mode_fb_cmd2 *mode_cmd,
  8621. struct drm_i915_gem_object *obj)
  8622. {
  8623. struct drm_framebuffer *fb;
  8624. int ret;
  8625. ret = i915_mutex_lock_interruptible(dev);
  8626. if (ret)
  8627. return ERR_PTR(ret);
  8628. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8629. mutex_unlock(&dev->struct_mutex);
  8630. return fb;
  8631. }
  8632. static u32
  8633. intel_framebuffer_pitch_for_width(int width, int bpp)
  8634. {
  8635. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8636. return ALIGN(pitch, 64);
  8637. }
  8638. static u32
  8639. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8640. {
  8641. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8642. return PAGE_ALIGN(pitch * mode->vdisplay);
  8643. }
  8644. static struct drm_framebuffer *
  8645. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8646. struct drm_display_mode *mode,
  8647. int depth, int bpp)
  8648. {
  8649. struct drm_framebuffer *fb;
  8650. struct drm_i915_gem_object *obj;
  8651. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8652. obj = i915_gem_object_create(dev,
  8653. intel_framebuffer_size_for_mode(mode, bpp));
  8654. if (obj == NULL)
  8655. return ERR_PTR(-ENOMEM);
  8656. mode_cmd.width = mode->hdisplay;
  8657. mode_cmd.height = mode->vdisplay;
  8658. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8659. bpp);
  8660. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8661. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8662. if (IS_ERR(fb))
  8663. drm_gem_object_unreference_unlocked(&obj->base);
  8664. return fb;
  8665. }
  8666. static struct drm_framebuffer *
  8667. mode_fits_in_fbdev(struct drm_device *dev,
  8668. struct drm_display_mode *mode)
  8669. {
  8670. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8671. struct drm_i915_private *dev_priv = dev->dev_private;
  8672. struct drm_i915_gem_object *obj;
  8673. struct drm_framebuffer *fb;
  8674. if (!dev_priv->fbdev)
  8675. return NULL;
  8676. if (!dev_priv->fbdev->fb)
  8677. return NULL;
  8678. obj = dev_priv->fbdev->fb->obj;
  8679. BUG_ON(!obj);
  8680. fb = &dev_priv->fbdev->fb->base;
  8681. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8682. fb->bits_per_pixel))
  8683. return NULL;
  8684. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8685. return NULL;
  8686. drm_framebuffer_reference(fb);
  8687. return fb;
  8688. #else
  8689. return NULL;
  8690. #endif
  8691. }
  8692. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8693. struct drm_crtc *crtc,
  8694. struct drm_display_mode *mode,
  8695. struct drm_framebuffer *fb,
  8696. int x, int y)
  8697. {
  8698. struct drm_plane_state *plane_state;
  8699. int hdisplay, vdisplay;
  8700. int ret;
  8701. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8702. if (IS_ERR(plane_state))
  8703. return PTR_ERR(plane_state);
  8704. if (mode)
  8705. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8706. else
  8707. hdisplay = vdisplay = 0;
  8708. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8709. if (ret)
  8710. return ret;
  8711. drm_atomic_set_fb_for_plane(plane_state, fb);
  8712. plane_state->crtc_x = 0;
  8713. plane_state->crtc_y = 0;
  8714. plane_state->crtc_w = hdisplay;
  8715. plane_state->crtc_h = vdisplay;
  8716. plane_state->src_x = x << 16;
  8717. plane_state->src_y = y << 16;
  8718. plane_state->src_w = hdisplay << 16;
  8719. plane_state->src_h = vdisplay << 16;
  8720. return 0;
  8721. }
  8722. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8723. struct drm_display_mode *mode,
  8724. struct intel_load_detect_pipe *old,
  8725. struct drm_modeset_acquire_ctx *ctx)
  8726. {
  8727. struct intel_crtc *intel_crtc;
  8728. struct intel_encoder *intel_encoder =
  8729. intel_attached_encoder(connector);
  8730. struct drm_crtc *possible_crtc;
  8731. struct drm_encoder *encoder = &intel_encoder->base;
  8732. struct drm_crtc *crtc = NULL;
  8733. struct drm_device *dev = encoder->dev;
  8734. struct drm_framebuffer *fb;
  8735. struct drm_mode_config *config = &dev->mode_config;
  8736. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8737. struct drm_connector_state *connector_state;
  8738. struct intel_crtc_state *crtc_state;
  8739. int ret, i = -1;
  8740. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8741. connector->base.id, connector->name,
  8742. encoder->base.id, encoder->name);
  8743. old->restore_state = NULL;
  8744. retry:
  8745. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8746. if (ret)
  8747. goto fail;
  8748. /*
  8749. * Algorithm gets a little messy:
  8750. *
  8751. * - if the connector already has an assigned crtc, use it (but make
  8752. * sure it's on first)
  8753. *
  8754. * - try to find the first unused crtc that can drive this connector,
  8755. * and use that if we find one
  8756. */
  8757. /* See if we already have a CRTC for this connector */
  8758. if (connector->state->crtc) {
  8759. crtc = connector->state->crtc;
  8760. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8761. if (ret)
  8762. goto fail;
  8763. /* Make sure the crtc and connector are running */
  8764. goto found;
  8765. }
  8766. /* Find an unused one (if possible) */
  8767. for_each_crtc(dev, possible_crtc) {
  8768. i++;
  8769. if (!(encoder->possible_crtcs & (1 << i)))
  8770. continue;
  8771. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8772. if (ret)
  8773. goto fail;
  8774. if (possible_crtc->state->enable) {
  8775. drm_modeset_unlock(&possible_crtc->mutex);
  8776. continue;
  8777. }
  8778. crtc = possible_crtc;
  8779. break;
  8780. }
  8781. /*
  8782. * If we didn't find an unused CRTC, don't use any.
  8783. */
  8784. if (!crtc) {
  8785. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8786. goto fail;
  8787. }
  8788. found:
  8789. intel_crtc = to_intel_crtc(crtc);
  8790. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8791. if (ret)
  8792. goto fail;
  8793. state = drm_atomic_state_alloc(dev);
  8794. restore_state = drm_atomic_state_alloc(dev);
  8795. if (!state || !restore_state) {
  8796. ret = -ENOMEM;
  8797. goto fail;
  8798. }
  8799. state->acquire_ctx = ctx;
  8800. restore_state->acquire_ctx = ctx;
  8801. connector_state = drm_atomic_get_connector_state(state, connector);
  8802. if (IS_ERR(connector_state)) {
  8803. ret = PTR_ERR(connector_state);
  8804. goto fail;
  8805. }
  8806. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8807. if (ret)
  8808. goto fail;
  8809. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8810. if (IS_ERR(crtc_state)) {
  8811. ret = PTR_ERR(crtc_state);
  8812. goto fail;
  8813. }
  8814. crtc_state->base.active = crtc_state->base.enable = true;
  8815. if (!mode)
  8816. mode = &load_detect_mode;
  8817. /* We need a framebuffer large enough to accommodate all accesses
  8818. * that the plane may generate whilst we perform load detection.
  8819. * We can not rely on the fbcon either being present (we get called
  8820. * during its initialisation to detect all boot displays, or it may
  8821. * not even exist) or that it is large enough to satisfy the
  8822. * requested mode.
  8823. */
  8824. fb = mode_fits_in_fbdev(dev, mode);
  8825. if (fb == NULL) {
  8826. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8827. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8828. } else
  8829. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8830. if (IS_ERR(fb)) {
  8831. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8832. goto fail;
  8833. }
  8834. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8835. if (ret)
  8836. goto fail;
  8837. drm_framebuffer_unreference(fb);
  8838. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8839. if (ret)
  8840. goto fail;
  8841. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8842. if (!ret)
  8843. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8844. if (!ret)
  8845. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8846. if (ret) {
  8847. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8848. goto fail;
  8849. }
  8850. ret = drm_atomic_commit(state);
  8851. if (ret) {
  8852. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8853. goto fail;
  8854. }
  8855. old->restore_state = restore_state;
  8856. /* let the connector get through one full cycle before testing */
  8857. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8858. return true;
  8859. fail:
  8860. drm_atomic_state_free(state);
  8861. drm_atomic_state_free(restore_state);
  8862. restore_state = state = NULL;
  8863. if (ret == -EDEADLK) {
  8864. drm_modeset_backoff(ctx);
  8865. goto retry;
  8866. }
  8867. return false;
  8868. }
  8869. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8870. struct intel_load_detect_pipe *old,
  8871. struct drm_modeset_acquire_ctx *ctx)
  8872. {
  8873. struct intel_encoder *intel_encoder =
  8874. intel_attached_encoder(connector);
  8875. struct drm_encoder *encoder = &intel_encoder->base;
  8876. struct drm_atomic_state *state = old->restore_state;
  8877. int ret;
  8878. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8879. connector->base.id, connector->name,
  8880. encoder->base.id, encoder->name);
  8881. if (!state)
  8882. return;
  8883. ret = drm_atomic_commit(state);
  8884. if (ret) {
  8885. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8886. drm_atomic_state_free(state);
  8887. }
  8888. }
  8889. static int i9xx_pll_refclk(struct drm_device *dev,
  8890. const struct intel_crtc_state *pipe_config)
  8891. {
  8892. struct drm_i915_private *dev_priv = dev->dev_private;
  8893. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8894. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8895. return dev_priv->vbt.lvds_ssc_freq;
  8896. else if (HAS_PCH_SPLIT(dev))
  8897. return 120000;
  8898. else if (!IS_GEN2(dev))
  8899. return 96000;
  8900. else
  8901. return 48000;
  8902. }
  8903. /* Returns the clock of the currently programmed mode of the given pipe. */
  8904. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8905. struct intel_crtc_state *pipe_config)
  8906. {
  8907. struct drm_device *dev = crtc->base.dev;
  8908. struct drm_i915_private *dev_priv = dev->dev_private;
  8909. int pipe = pipe_config->cpu_transcoder;
  8910. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8911. u32 fp;
  8912. intel_clock_t clock;
  8913. int port_clock;
  8914. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8915. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8916. fp = pipe_config->dpll_hw_state.fp0;
  8917. else
  8918. fp = pipe_config->dpll_hw_state.fp1;
  8919. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8920. if (IS_PINEVIEW(dev)) {
  8921. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8922. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8923. } else {
  8924. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8925. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8926. }
  8927. if (!IS_GEN2(dev)) {
  8928. if (IS_PINEVIEW(dev))
  8929. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8930. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8931. else
  8932. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8933. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8934. switch (dpll & DPLL_MODE_MASK) {
  8935. case DPLLB_MODE_DAC_SERIAL:
  8936. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8937. 5 : 10;
  8938. break;
  8939. case DPLLB_MODE_LVDS:
  8940. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8941. 7 : 14;
  8942. break;
  8943. default:
  8944. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8945. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8946. return;
  8947. }
  8948. if (IS_PINEVIEW(dev))
  8949. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8950. else
  8951. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8952. } else {
  8953. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8954. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8955. if (is_lvds) {
  8956. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8957. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8958. if (lvds & LVDS_CLKB_POWER_UP)
  8959. clock.p2 = 7;
  8960. else
  8961. clock.p2 = 14;
  8962. } else {
  8963. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8964. clock.p1 = 2;
  8965. else {
  8966. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8967. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8968. }
  8969. if (dpll & PLL_P2_DIVIDE_BY_4)
  8970. clock.p2 = 4;
  8971. else
  8972. clock.p2 = 2;
  8973. }
  8974. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8975. }
  8976. /*
  8977. * This value includes pixel_multiplier. We will use
  8978. * port_clock to compute adjusted_mode.crtc_clock in the
  8979. * encoder's get_config() function.
  8980. */
  8981. pipe_config->port_clock = port_clock;
  8982. }
  8983. int intel_dotclock_calculate(int link_freq,
  8984. const struct intel_link_m_n *m_n)
  8985. {
  8986. /*
  8987. * The calculation for the data clock is:
  8988. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8989. * But we want to avoid losing precison if possible, so:
  8990. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8991. *
  8992. * and the link clock is simpler:
  8993. * link_clock = (m * link_clock) / n
  8994. */
  8995. if (!m_n->link_n)
  8996. return 0;
  8997. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8998. }
  8999. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9000. struct intel_crtc_state *pipe_config)
  9001. {
  9002. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9003. /* read out port_clock from the DPLL */
  9004. i9xx_crtc_clock_get(crtc, pipe_config);
  9005. /*
  9006. * In case there is an active pipe without active ports,
  9007. * we may need some idea for the dotclock anyway.
  9008. * Calculate one based on the FDI configuration.
  9009. */
  9010. pipe_config->base.adjusted_mode.crtc_clock =
  9011. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9012. &pipe_config->fdi_m_n);
  9013. }
  9014. /** Returns the currently programmed mode of the given pipe. */
  9015. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9016. struct drm_crtc *crtc)
  9017. {
  9018. struct drm_i915_private *dev_priv = dev->dev_private;
  9019. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9020. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9021. struct drm_display_mode *mode;
  9022. struct intel_crtc_state *pipe_config;
  9023. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9024. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9025. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9026. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9027. enum pipe pipe = intel_crtc->pipe;
  9028. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9029. if (!mode)
  9030. return NULL;
  9031. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9032. if (!pipe_config) {
  9033. kfree(mode);
  9034. return NULL;
  9035. }
  9036. /*
  9037. * Construct a pipe_config sufficient for getting the clock info
  9038. * back out of crtc_clock_get.
  9039. *
  9040. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9041. * to use a real value here instead.
  9042. */
  9043. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9044. pipe_config->pixel_multiplier = 1;
  9045. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9046. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9047. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9048. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9049. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9050. mode->hdisplay = (htot & 0xffff) + 1;
  9051. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9052. mode->hsync_start = (hsync & 0xffff) + 1;
  9053. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9054. mode->vdisplay = (vtot & 0xffff) + 1;
  9055. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9056. mode->vsync_start = (vsync & 0xffff) + 1;
  9057. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9058. drm_mode_set_name(mode);
  9059. kfree(pipe_config);
  9060. return mode;
  9061. }
  9062. void intel_mark_busy(struct drm_device *dev)
  9063. {
  9064. struct drm_i915_private *dev_priv = dev->dev_private;
  9065. if (dev_priv->mm.busy)
  9066. return;
  9067. intel_runtime_pm_get(dev_priv);
  9068. i915_update_gfx_val(dev_priv);
  9069. if (INTEL_INFO(dev)->gen >= 6)
  9070. gen6_rps_busy(dev_priv);
  9071. dev_priv->mm.busy = true;
  9072. }
  9073. void intel_mark_idle(struct drm_device *dev)
  9074. {
  9075. struct drm_i915_private *dev_priv = dev->dev_private;
  9076. if (!dev_priv->mm.busy)
  9077. return;
  9078. dev_priv->mm.busy = false;
  9079. if (INTEL_INFO(dev)->gen >= 6)
  9080. gen6_rps_idle(dev->dev_private);
  9081. intel_runtime_pm_put(dev_priv);
  9082. }
  9083. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9084. {
  9085. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9086. struct drm_device *dev = crtc->dev;
  9087. struct intel_unpin_work *work;
  9088. spin_lock_irq(&dev->event_lock);
  9089. work = intel_crtc->unpin_work;
  9090. intel_crtc->unpin_work = NULL;
  9091. spin_unlock_irq(&dev->event_lock);
  9092. if (work) {
  9093. cancel_work_sync(&work->work);
  9094. kfree(work);
  9095. }
  9096. drm_crtc_cleanup(crtc);
  9097. kfree(intel_crtc);
  9098. }
  9099. static void intel_unpin_work_fn(struct work_struct *__work)
  9100. {
  9101. struct intel_unpin_work *work =
  9102. container_of(__work, struct intel_unpin_work, work);
  9103. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9104. struct drm_device *dev = crtc->base.dev;
  9105. struct drm_plane *primary = crtc->base.primary;
  9106. mutex_lock(&dev->struct_mutex);
  9107. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9108. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9109. if (work->flip_queued_req)
  9110. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9111. mutex_unlock(&dev->struct_mutex);
  9112. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  9113. intel_fbc_post_update(crtc);
  9114. drm_framebuffer_unreference(work->old_fb);
  9115. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9116. atomic_dec(&crtc->unpin_work_count);
  9117. kfree(work);
  9118. }
  9119. static void do_intel_finish_page_flip(struct drm_device *dev,
  9120. struct drm_crtc *crtc)
  9121. {
  9122. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9123. struct intel_unpin_work *work;
  9124. unsigned long flags;
  9125. /* Ignore early vblank irqs */
  9126. if (intel_crtc == NULL)
  9127. return;
  9128. /*
  9129. * This is called both by irq handlers and the reset code (to complete
  9130. * lost pageflips) so needs the full irqsave spinlocks.
  9131. */
  9132. spin_lock_irqsave(&dev->event_lock, flags);
  9133. work = intel_crtc->unpin_work;
  9134. /* Ensure we don't miss a work->pending update ... */
  9135. smp_rmb();
  9136. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9137. spin_unlock_irqrestore(&dev->event_lock, flags);
  9138. return;
  9139. }
  9140. page_flip_completed(intel_crtc);
  9141. spin_unlock_irqrestore(&dev->event_lock, flags);
  9142. }
  9143. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9144. {
  9145. struct drm_i915_private *dev_priv = dev->dev_private;
  9146. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9147. do_intel_finish_page_flip(dev, crtc);
  9148. }
  9149. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9150. {
  9151. struct drm_i915_private *dev_priv = dev->dev_private;
  9152. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9153. do_intel_finish_page_flip(dev, crtc);
  9154. }
  9155. /* Is 'a' after or equal to 'b'? */
  9156. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9157. {
  9158. return !((a - b) & 0x80000000);
  9159. }
  9160. static bool page_flip_finished(struct intel_crtc *crtc)
  9161. {
  9162. struct drm_device *dev = crtc->base.dev;
  9163. struct drm_i915_private *dev_priv = dev->dev_private;
  9164. unsigned reset_counter;
  9165. reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9166. if (crtc->reset_counter != reset_counter)
  9167. return true;
  9168. /*
  9169. * The relevant registers doen't exist on pre-ctg.
  9170. * As the flip done interrupt doesn't trigger for mmio
  9171. * flips on gmch platforms, a flip count check isn't
  9172. * really needed there. But since ctg has the registers,
  9173. * include it in the check anyway.
  9174. */
  9175. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9176. return true;
  9177. /*
  9178. * BDW signals flip done immediately if the plane
  9179. * is disabled, even if the plane enable is already
  9180. * armed to occur at the next vblank :(
  9181. */
  9182. /*
  9183. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9184. * used the same base address. In that case the mmio flip might
  9185. * have completed, but the CS hasn't even executed the flip yet.
  9186. *
  9187. * A flip count check isn't enough as the CS might have updated
  9188. * the base address just after start of vblank, but before we
  9189. * managed to process the interrupt. This means we'd complete the
  9190. * CS flip too soon.
  9191. *
  9192. * Combining both checks should get us a good enough result. It may
  9193. * still happen that the CS flip has been executed, but has not
  9194. * yet actually completed. But in case the base address is the same
  9195. * anyway, we don't really care.
  9196. */
  9197. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9198. crtc->unpin_work->gtt_offset &&
  9199. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9200. crtc->unpin_work->flip_count);
  9201. }
  9202. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9203. {
  9204. struct drm_i915_private *dev_priv = dev->dev_private;
  9205. struct intel_crtc *intel_crtc =
  9206. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9207. unsigned long flags;
  9208. /*
  9209. * This is called both by irq handlers and the reset code (to complete
  9210. * lost pageflips) so needs the full irqsave spinlocks.
  9211. *
  9212. * NB: An MMIO update of the plane base pointer will also
  9213. * generate a page-flip completion irq, i.e. every modeset
  9214. * is also accompanied by a spurious intel_prepare_page_flip().
  9215. */
  9216. spin_lock_irqsave(&dev->event_lock, flags);
  9217. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9218. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9219. spin_unlock_irqrestore(&dev->event_lock, flags);
  9220. }
  9221. static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
  9222. {
  9223. /* Ensure that the work item is consistent when activating it ... */
  9224. smp_wmb();
  9225. atomic_set(&work->pending, INTEL_FLIP_PENDING);
  9226. /* and that it is marked active as soon as the irq could fire. */
  9227. smp_wmb();
  9228. }
  9229. static int intel_gen2_queue_flip(struct drm_device *dev,
  9230. struct drm_crtc *crtc,
  9231. struct drm_framebuffer *fb,
  9232. struct drm_i915_gem_object *obj,
  9233. struct drm_i915_gem_request *req,
  9234. uint32_t flags)
  9235. {
  9236. struct intel_engine_cs *engine = req->engine;
  9237. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9238. u32 flip_mask;
  9239. int ret;
  9240. ret = intel_ring_begin(req, 6);
  9241. if (ret)
  9242. return ret;
  9243. /* Can't queue multiple flips, so wait for the previous
  9244. * one to finish before executing the next.
  9245. */
  9246. if (intel_crtc->plane)
  9247. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9248. else
  9249. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9250. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9251. intel_ring_emit(engine, MI_NOOP);
  9252. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9253. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9254. intel_ring_emit(engine, fb->pitches[0]);
  9255. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9256. intel_ring_emit(engine, 0); /* aux display base address, unused */
  9257. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9258. return 0;
  9259. }
  9260. static int intel_gen3_queue_flip(struct drm_device *dev,
  9261. struct drm_crtc *crtc,
  9262. struct drm_framebuffer *fb,
  9263. struct drm_i915_gem_object *obj,
  9264. struct drm_i915_gem_request *req,
  9265. uint32_t flags)
  9266. {
  9267. struct intel_engine_cs *engine = req->engine;
  9268. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9269. u32 flip_mask;
  9270. int ret;
  9271. ret = intel_ring_begin(req, 6);
  9272. if (ret)
  9273. return ret;
  9274. if (intel_crtc->plane)
  9275. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9276. else
  9277. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9278. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9279. intel_ring_emit(engine, MI_NOOP);
  9280. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
  9281. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9282. intel_ring_emit(engine, fb->pitches[0]);
  9283. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9284. intel_ring_emit(engine, MI_NOOP);
  9285. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9286. return 0;
  9287. }
  9288. static int intel_gen4_queue_flip(struct drm_device *dev,
  9289. struct drm_crtc *crtc,
  9290. struct drm_framebuffer *fb,
  9291. struct drm_i915_gem_object *obj,
  9292. struct drm_i915_gem_request *req,
  9293. uint32_t flags)
  9294. {
  9295. struct intel_engine_cs *engine = req->engine;
  9296. struct drm_i915_private *dev_priv = dev->dev_private;
  9297. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9298. uint32_t pf, pipesrc;
  9299. int ret;
  9300. ret = intel_ring_begin(req, 4);
  9301. if (ret)
  9302. return ret;
  9303. /* i965+ uses the linear or tiled offsets from the
  9304. * Display Registers (which do not change across a page-flip)
  9305. * so we need only reprogram the base address.
  9306. */
  9307. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9308. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9309. intel_ring_emit(engine, fb->pitches[0]);
  9310. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
  9311. obj->tiling_mode);
  9312. /* XXX Enabling the panel-fitter across page-flip is so far
  9313. * untested on non-native modes, so ignore it for now.
  9314. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9315. */
  9316. pf = 0;
  9317. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9318. intel_ring_emit(engine, pf | pipesrc);
  9319. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9320. return 0;
  9321. }
  9322. static int intel_gen6_queue_flip(struct drm_device *dev,
  9323. struct drm_crtc *crtc,
  9324. struct drm_framebuffer *fb,
  9325. struct drm_i915_gem_object *obj,
  9326. struct drm_i915_gem_request *req,
  9327. uint32_t flags)
  9328. {
  9329. struct intel_engine_cs *engine = req->engine;
  9330. struct drm_i915_private *dev_priv = dev->dev_private;
  9331. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9332. uint32_t pf, pipesrc;
  9333. int ret;
  9334. ret = intel_ring_begin(req, 4);
  9335. if (ret)
  9336. return ret;
  9337. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9338. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9339. intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
  9340. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9341. /* Contrary to the suggestions in the documentation,
  9342. * "Enable Panel Fitter" does not seem to be required when page
  9343. * flipping with a non-native mode, and worse causes a normal
  9344. * modeset to fail.
  9345. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9346. */
  9347. pf = 0;
  9348. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9349. intel_ring_emit(engine, pf | pipesrc);
  9350. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9351. return 0;
  9352. }
  9353. static int intel_gen7_queue_flip(struct drm_device *dev,
  9354. struct drm_crtc *crtc,
  9355. struct drm_framebuffer *fb,
  9356. struct drm_i915_gem_object *obj,
  9357. struct drm_i915_gem_request *req,
  9358. uint32_t flags)
  9359. {
  9360. struct intel_engine_cs *engine = req->engine;
  9361. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9362. uint32_t plane_bit = 0;
  9363. int len, ret;
  9364. switch (intel_crtc->plane) {
  9365. case PLANE_A:
  9366. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9367. break;
  9368. case PLANE_B:
  9369. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9370. break;
  9371. case PLANE_C:
  9372. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9373. break;
  9374. default:
  9375. WARN_ONCE(1, "unknown plane in flip command\n");
  9376. return -ENODEV;
  9377. }
  9378. len = 4;
  9379. if (engine->id == RCS) {
  9380. len += 6;
  9381. /*
  9382. * On Gen 8, SRM is now taking an extra dword to accommodate
  9383. * 48bits addresses, and we need a NOOP for the batch size to
  9384. * stay even.
  9385. */
  9386. if (IS_GEN8(dev))
  9387. len += 2;
  9388. }
  9389. /*
  9390. * BSpec MI_DISPLAY_FLIP for IVB:
  9391. * "The full packet must be contained within the same cache line."
  9392. *
  9393. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9394. * cacheline, if we ever start emitting more commands before
  9395. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9396. * then do the cacheline alignment, and finally emit the
  9397. * MI_DISPLAY_FLIP.
  9398. */
  9399. ret = intel_ring_cacheline_align(req);
  9400. if (ret)
  9401. return ret;
  9402. ret = intel_ring_begin(req, len);
  9403. if (ret)
  9404. return ret;
  9405. /* Unmask the flip-done completion message. Note that the bspec says that
  9406. * we should do this for both the BCS and RCS, and that we must not unmask
  9407. * more than one flip event at any time (or ensure that one flip message
  9408. * can be sent by waiting for flip-done prior to queueing new flips).
  9409. * Experimentation says that BCS works despite DERRMR masking all
  9410. * flip-done completion events and that unmasking all planes at once
  9411. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9412. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9413. */
  9414. if (engine->id == RCS) {
  9415. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
  9416. intel_ring_emit_reg(engine, DERRMR);
  9417. intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9418. DERRMR_PIPEB_PRI_FLIP_DONE |
  9419. DERRMR_PIPEC_PRI_FLIP_DONE));
  9420. if (IS_GEN8(dev))
  9421. intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
  9422. MI_SRM_LRM_GLOBAL_GTT);
  9423. else
  9424. intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
  9425. MI_SRM_LRM_GLOBAL_GTT);
  9426. intel_ring_emit_reg(engine, DERRMR);
  9427. intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
  9428. if (IS_GEN8(dev)) {
  9429. intel_ring_emit(engine, 0);
  9430. intel_ring_emit(engine, MI_NOOP);
  9431. }
  9432. }
  9433. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
  9434. intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
  9435. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9436. intel_ring_emit(engine, (MI_NOOP));
  9437. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9438. return 0;
  9439. }
  9440. static bool use_mmio_flip(struct intel_engine_cs *engine,
  9441. struct drm_i915_gem_object *obj)
  9442. {
  9443. /*
  9444. * This is not being used for older platforms, because
  9445. * non-availability of flip done interrupt forces us to use
  9446. * CS flips. Older platforms derive flip done using some clever
  9447. * tricks involving the flip_pending status bits and vblank irqs.
  9448. * So using MMIO flips there would disrupt this mechanism.
  9449. */
  9450. if (engine == NULL)
  9451. return true;
  9452. if (INTEL_INFO(engine->dev)->gen < 5)
  9453. return false;
  9454. if (i915.use_mmio_flip < 0)
  9455. return false;
  9456. else if (i915.use_mmio_flip > 0)
  9457. return true;
  9458. else if (i915.enable_execlists)
  9459. return true;
  9460. else if (obj->base.dma_buf &&
  9461. !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
  9462. false))
  9463. return true;
  9464. else
  9465. return engine != i915_gem_request_get_engine(obj->last_write_req);
  9466. }
  9467. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9468. unsigned int rotation,
  9469. struct intel_unpin_work *work)
  9470. {
  9471. struct drm_device *dev = intel_crtc->base.dev;
  9472. struct drm_i915_private *dev_priv = dev->dev_private;
  9473. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9474. const enum pipe pipe = intel_crtc->pipe;
  9475. u32 ctl, stride, tile_height;
  9476. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9477. ctl &= ~PLANE_CTL_TILED_MASK;
  9478. switch (fb->modifier[0]) {
  9479. case DRM_FORMAT_MOD_NONE:
  9480. break;
  9481. case I915_FORMAT_MOD_X_TILED:
  9482. ctl |= PLANE_CTL_TILED_X;
  9483. break;
  9484. case I915_FORMAT_MOD_Y_TILED:
  9485. ctl |= PLANE_CTL_TILED_Y;
  9486. break;
  9487. case I915_FORMAT_MOD_Yf_TILED:
  9488. ctl |= PLANE_CTL_TILED_YF;
  9489. break;
  9490. default:
  9491. MISSING_CASE(fb->modifier[0]);
  9492. }
  9493. /*
  9494. * The stride is either expressed as a multiple of 64 bytes chunks for
  9495. * linear buffers or in number of tiles for tiled buffers.
  9496. */
  9497. if (intel_rotation_90_or_270(rotation)) {
  9498. /* stride = Surface height in tiles */
  9499. tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
  9500. stride = DIV_ROUND_UP(fb->height, tile_height);
  9501. } else {
  9502. stride = fb->pitches[0] /
  9503. intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  9504. fb->pixel_format);
  9505. }
  9506. /*
  9507. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9508. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9509. */
  9510. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9511. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9512. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9513. POSTING_READ(PLANE_SURF(pipe, 0));
  9514. }
  9515. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9516. struct intel_unpin_work *work)
  9517. {
  9518. struct drm_device *dev = intel_crtc->base.dev;
  9519. struct drm_i915_private *dev_priv = dev->dev_private;
  9520. struct intel_framebuffer *intel_fb =
  9521. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9522. struct drm_i915_gem_object *obj = intel_fb->obj;
  9523. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  9524. u32 dspcntr;
  9525. dspcntr = I915_READ(reg);
  9526. if (obj->tiling_mode != I915_TILING_NONE)
  9527. dspcntr |= DISPPLANE_TILED;
  9528. else
  9529. dspcntr &= ~DISPPLANE_TILED;
  9530. I915_WRITE(reg, dspcntr);
  9531. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9532. POSTING_READ(DSPSURF(intel_crtc->plane));
  9533. }
  9534. /*
  9535. * XXX: This is the temporary way to update the plane registers until we get
  9536. * around to using the usual plane update functions for MMIO flips
  9537. */
  9538. static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
  9539. {
  9540. struct intel_crtc *crtc = mmio_flip->crtc;
  9541. struct intel_unpin_work *work;
  9542. spin_lock_irq(&crtc->base.dev->event_lock);
  9543. work = crtc->unpin_work;
  9544. spin_unlock_irq(&crtc->base.dev->event_lock);
  9545. if (work == NULL)
  9546. return;
  9547. intel_mark_page_flip_active(work);
  9548. intel_pipe_update_start(crtc);
  9549. if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
  9550. skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
  9551. else
  9552. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9553. ilk_do_mmio_flip(crtc, work);
  9554. intel_pipe_update_end(crtc);
  9555. }
  9556. static void intel_mmio_flip_work_func(struct work_struct *work)
  9557. {
  9558. struct intel_mmio_flip *mmio_flip =
  9559. container_of(work, struct intel_mmio_flip, work);
  9560. struct intel_framebuffer *intel_fb =
  9561. to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
  9562. struct drm_i915_gem_object *obj = intel_fb->obj;
  9563. if (mmio_flip->req) {
  9564. WARN_ON(__i915_wait_request(mmio_flip->req,
  9565. false, NULL,
  9566. &mmio_flip->i915->rps.mmioflips));
  9567. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9568. }
  9569. /* For framebuffer backed by dmabuf, wait for fence */
  9570. if (obj->base.dma_buf)
  9571. WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  9572. false, false,
  9573. MAX_SCHEDULE_TIMEOUT) < 0);
  9574. intel_do_mmio_flip(mmio_flip);
  9575. kfree(mmio_flip);
  9576. }
  9577. static int intel_queue_mmio_flip(struct drm_device *dev,
  9578. struct drm_crtc *crtc,
  9579. struct drm_i915_gem_object *obj)
  9580. {
  9581. struct intel_mmio_flip *mmio_flip;
  9582. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9583. if (mmio_flip == NULL)
  9584. return -ENOMEM;
  9585. mmio_flip->i915 = to_i915(dev);
  9586. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9587. mmio_flip->crtc = to_intel_crtc(crtc);
  9588. mmio_flip->rotation = crtc->primary->state->rotation;
  9589. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9590. schedule_work(&mmio_flip->work);
  9591. return 0;
  9592. }
  9593. static int intel_default_queue_flip(struct drm_device *dev,
  9594. struct drm_crtc *crtc,
  9595. struct drm_framebuffer *fb,
  9596. struct drm_i915_gem_object *obj,
  9597. struct drm_i915_gem_request *req,
  9598. uint32_t flags)
  9599. {
  9600. return -ENODEV;
  9601. }
  9602. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9603. struct drm_crtc *crtc)
  9604. {
  9605. struct drm_i915_private *dev_priv = dev->dev_private;
  9606. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9607. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9608. u32 addr;
  9609. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9610. return true;
  9611. if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
  9612. return false;
  9613. if (!work->enable_stall_check)
  9614. return false;
  9615. if (work->flip_ready_vblank == 0) {
  9616. if (work->flip_queued_req &&
  9617. !i915_gem_request_completed(work->flip_queued_req, true))
  9618. return false;
  9619. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9620. }
  9621. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9622. return false;
  9623. /* Potential stall - if we see that the flip has happened,
  9624. * assume a missed interrupt. */
  9625. if (INTEL_INFO(dev)->gen >= 4)
  9626. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9627. else
  9628. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9629. /* There is a potential issue here with a false positive after a flip
  9630. * to the same address. We could address this by checking for a
  9631. * non-incrementing frame counter.
  9632. */
  9633. return addr == work->gtt_offset;
  9634. }
  9635. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9636. {
  9637. struct drm_i915_private *dev_priv = dev->dev_private;
  9638. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9639. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9640. struct intel_unpin_work *work;
  9641. WARN_ON(!in_interrupt());
  9642. if (crtc == NULL)
  9643. return;
  9644. spin_lock(&dev->event_lock);
  9645. work = intel_crtc->unpin_work;
  9646. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9647. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9648. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9649. page_flip_completed(intel_crtc);
  9650. work = NULL;
  9651. }
  9652. if (work != NULL &&
  9653. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9654. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9655. spin_unlock(&dev->event_lock);
  9656. }
  9657. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9658. struct drm_framebuffer *fb,
  9659. struct drm_pending_vblank_event *event,
  9660. uint32_t page_flip_flags)
  9661. {
  9662. struct drm_device *dev = crtc->dev;
  9663. struct drm_i915_private *dev_priv = dev->dev_private;
  9664. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9665. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9666. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9667. struct drm_plane *primary = crtc->primary;
  9668. enum pipe pipe = intel_crtc->pipe;
  9669. struct intel_unpin_work *work;
  9670. struct intel_engine_cs *engine;
  9671. bool mmio_flip;
  9672. struct drm_i915_gem_request *request = NULL;
  9673. int ret;
  9674. /*
  9675. * drm_mode_page_flip_ioctl() should already catch this, but double
  9676. * check to be safe. In the future we may enable pageflipping from
  9677. * a disabled primary plane.
  9678. */
  9679. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9680. return -EBUSY;
  9681. /* Can't change pixel format via MI display flips. */
  9682. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9683. return -EINVAL;
  9684. /*
  9685. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9686. * Note that pitch changes could also affect these register.
  9687. */
  9688. if (INTEL_INFO(dev)->gen > 3 &&
  9689. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9690. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9691. return -EINVAL;
  9692. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9693. goto out_hang;
  9694. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9695. if (work == NULL)
  9696. return -ENOMEM;
  9697. work->event = event;
  9698. work->crtc = crtc;
  9699. work->old_fb = old_fb;
  9700. INIT_WORK(&work->work, intel_unpin_work_fn);
  9701. ret = drm_crtc_vblank_get(crtc);
  9702. if (ret)
  9703. goto free_work;
  9704. /* We borrow the event spin lock for protecting unpin_work */
  9705. spin_lock_irq(&dev->event_lock);
  9706. if (intel_crtc->unpin_work) {
  9707. /* Before declaring the flip queue wedged, check if
  9708. * the hardware completed the operation behind our backs.
  9709. */
  9710. if (__intel_pageflip_stall_check(dev, crtc)) {
  9711. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9712. page_flip_completed(intel_crtc);
  9713. } else {
  9714. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9715. spin_unlock_irq(&dev->event_lock);
  9716. drm_crtc_vblank_put(crtc);
  9717. kfree(work);
  9718. return -EBUSY;
  9719. }
  9720. }
  9721. intel_crtc->unpin_work = work;
  9722. spin_unlock_irq(&dev->event_lock);
  9723. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9724. flush_workqueue(dev_priv->wq);
  9725. /* Reference the objects for the scheduled work. */
  9726. drm_framebuffer_reference(work->old_fb);
  9727. drm_gem_object_reference(&obj->base);
  9728. crtc->primary->fb = fb;
  9729. update_state_fb(crtc->primary);
  9730. intel_fbc_pre_update(intel_crtc);
  9731. work->pending_flip_obj = obj;
  9732. ret = i915_mutex_lock_interruptible(dev);
  9733. if (ret)
  9734. goto cleanup;
  9735. intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9736. if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
  9737. ret = -EIO;
  9738. goto cleanup;
  9739. }
  9740. atomic_inc(&intel_crtc->unpin_work_count);
  9741. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9742. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9743. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  9744. engine = &dev_priv->engine[BCS];
  9745. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9746. /* vlv: DISPLAY_FLIP fails to change tiling */
  9747. engine = NULL;
  9748. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9749. engine = &dev_priv->engine[BCS];
  9750. } else if (INTEL_INFO(dev)->gen >= 7) {
  9751. engine = i915_gem_request_get_engine(obj->last_write_req);
  9752. if (engine == NULL || engine->id != RCS)
  9753. engine = &dev_priv->engine[BCS];
  9754. } else {
  9755. engine = &dev_priv->engine[RCS];
  9756. }
  9757. mmio_flip = use_mmio_flip(engine, obj);
  9758. /* When using CS flips, we want to emit semaphores between rings.
  9759. * However, when using mmio flips we will create a task to do the
  9760. * synchronisation, so all we want here is to pin the framebuffer
  9761. * into the display plane and skip any waits.
  9762. */
  9763. if (!mmio_flip) {
  9764. ret = i915_gem_object_sync(obj, engine, &request);
  9765. if (ret)
  9766. goto cleanup_pending;
  9767. }
  9768. ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  9769. if (ret)
  9770. goto cleanup_pending;
  9771. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9772. obj, 0);
  9773. work->gtt_offset += intel_crtc->dspaddr_offset;
  9774. if (mmio_flip) {
  9775. ret = intel_queue_mmio_flip(dev, crtc, obj);
  9776. if (ret)
  9777. goto cleanup_unpin;
  9778. i915_gem_request_assign(&work->flip_queued_req,
  9779. obj->last_write_req);
  9780. } else {
  9781. if (!request) {
  9782. request = i915_gem_request_alloc(engine, NULL);
  9783. if (IS_ERR(request)) {
  9784. ret = PTR_ERR(request);
  9785. goto cleanup_unpin;
  9786. }
  9787. }
  9788. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9789. page_flip_flags);
  9790. if (ret)
  9791. goto cleanup_unpin;
  9792. i915_gem_request_assign(&work->flip_queued_req, request);
  9793. }
  9794. if (request)
  9795. i915_add_request_no_flush(request);
  9796. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9797. work->enable_stall_check = true;
  9798. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9799. to_intel_plane(primary)->frontbuffer_bit);
  9800. mutex_unlock(&dev->struct_mutex);
  9801. intel_frontbuffer_flip_prepare(dev,
  9802. to_intel_plane(primary)->frontbuffer_bit);
  9803. trace_i915_flip_request(intel_crtc->plane, obj);
  9804. return 0;
  9805. cleanup_unpin:
  9806. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  9807. cleanup_pending:
  9808. if (!IS_ERR_OR_NULL(request))
  9809. i915_add_request_no_flush(request);
  9810. atomic_dec(&intel_crtc->unpin_work_count);
  9811. mutex_unlock(&dev->struct_mutex);
  9812. cleanup:
  9813. crtc->primary->fb = old_fb;
  9814. update_state_fb(crtc->primary);
  9815. drm_gem_object_unreference_unlocked(&obj->base);
  9816. drm_framebuffer_unreference(work->old_fb);
  9817. spin_lock_irq(&dev->event_lock);
  9818. intel_crtc->unpin_work = NULL;
  9819. spin_unlock_irq(&dev->event_lock);
  9820. drm_crtc_vblank_put(crtc);
  9821. free_work:
  9822. kfree(work);
  9823. if (ret == -EIO) {
  9824. struct drm_atomic_state *state;
  9825. struct drm_plane_state *plane_state;
  9826. out_hang:
  9827. state = drm_atomic_state_alloc(dev);
  9828. if (!state)
  9829. return -ENOMEM;
  9830. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9831. retry:
  9832. plane_state = drm_atomic_get_plane_state(state, primary);
  9833. ret = PTR_ERR_OR_ZERO(plane_state);
  9834. if (!ret) {
  9835. drm_atomic_set_fb_for_plane(plane_state, fb);
  9836. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9837. if (!ret)
  9838. ret = drm_atomic_commit(state);
  9839. }
  9840. if (ret == -EDEADLK) {
  9841. drm_modeset_backoff(state->acquire_ctx);
  9842. drm_atomic_state_clear(state);
  9843. goto retry;
  9844. }
  9845. if (ret)
  9846. drm_atomic_state_free(state);
  9847. if (ret == 0 && event) {
  9848. spin_lock_irq(&dev->event_lock);
  9849. drm_crtc_send_vblank_event(crtc, event);
  9850. spin_unlock_irq(&dev->event_lock);
  9851. }
  9852. }
  9853. return ret;
  9854. }
  9855. /**
  9856. * intel_wm_need_update - Check whether watermarks need updating
  9857. * @plane: drm plane
  9858. * @state: new plane state
  9859. *
  9860. * Check current plane state versus the new one to determine whether
  9861. * watermarks need to be recalculated.
  9862. *
  9863. * Returns true or false.
  9864. */
  9865. static bool intel_wm_need_update(struct drm_plane *plane,
  9866. struct drm_plane_state *state)
  9867. {
  9868. struct intel_plane_state *new = to_intel_plane_state(state);
  9869. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9870. /* Update watermarks on tiling or size changes. */
  9871. if (new->visible != cur->visible)
  9872. return true;
  9873. if (!cur->base.fb || !new->base.fb)
  9874. return false;
  9875. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  9876. cur->base.rotation != new->base.rotation ||
  9877. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9878. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9879. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9880. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9881. return true;
  9882. return false;
  9883. }
  9884. static bool needs_scaling(struct intel_plane_state *state)
  9885. {
  9886. int src_w = drm_rect_width(&state->src) >> 16;
  9887. int src_h = drm_rect_height(&state->src) >> 16;
  9888. int dst_w = drm_rect_width(&state->dst);
  9889. int dst_h = drm_rect_height(&state->dst);
  9890. return (src_w != dst_w || src_h != dst_h);
  9891. }
  9892. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9893. struct drm_plane_state *plane_state)
  9894. {
  9895. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9896. struct drm_crtc *crtc = crtc_state->crtc;
  9897. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9898. struct drm_plane *plane = plane_state->plane;
  9899. struct drm_device *dev = crtc->dev;
  9900. struct drm_i915_private *dev_priv = to_i915(dev);
  9901. struct intel_plane_state *old_plane_state =
  9902. to_intel_plane_state(plane->state);
  9903. int idx = intel_crtc->base.base.id, ret;
  9904. bool mode_changed = needs_modeset(crtc_state);
  9905. bool was_crtc_enabled = crtc->state->active;
  9906. bool is_crtc_enabled = crtc_state->active;
  9907. bool turn_off, turn_on, visible, was_visible;
  9908. struct drm_framebuffer *fb = plane_state->fb;
  9909. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9910. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9911. ret = skl_update_scaler_plane(
  9912. to_intel_crtc_state(crtc_state),
  9913. to_intel_plane_state(plane_state));
  9914. if (ret)
  9915. return ret;
  9916. }
  9917. was_visible = old_plane_state->visible;
  9918. visible = to_intel_plane_state(plane_state)->visible;
  9919. if (!was_crtc_enabled && WARN_ON(was_visible))
  9920. was_visible = false;
  9921. /*
  9922. * Visibility is calculated as if the crtc was on, but
  9923. * after scaler setup everything depends on it being off
  9924. * when the crtc isn't active.
  9925. */
  9926. if (!is_crtc_enabled)
  9927. to_intel_plane_state(plane_state)->visible = visible = false;
  9928. if (!was_visible && !visible)
  9929. return 0;
  9930. if (fb != old_plane_state->base.fb)
  9931. pipe_config->fb_changed = true;
  9932. turn_off = was_visible && (!visible || mode_changed);
  9933. turn_on = visible && (!was_visible || mode_changed);
  9934. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9935. plane->base.id, fb ? fb->base.id : -1);
  9936. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9937. plane->base.id, was_visible, visible,
  9938. turn_off, turn_on, mode_changed);
  9939. if (turn_on) {
  9940. pipe_config->update_wm_pre = true;
  9941. /* must disable cxsr around plane enable/disable */
  9942. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9943. pipe_config->disable_cxsr = true;
  9944. } else if (turn_off) {
  9945. pipe_config->update_wm_post = true;
  9946. /* must disable cxsr around plane enable/disable */
  9947. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9948. pipe_config->disable_cxsr = true;
  9949. } else if (intel_wm_need_update(plane, plane_state)) {
  9950. /* FIXME bollocks */
  9951. pipe_config->update_wm_pre = true;
  9952. pipe_config->update_wm_post = true;
  9953. }
  9954. /* Pre-gen9 platforms need two-step watermark updates */
  9955. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  9956. INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
  9957. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  9958. if (visible || was_visible)
  9959. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  9960. /*
  9961. * WaCxSRDisabledForSpriteScaling:ivb
  9962. *
  9963. * cstate->update_wm was already set above, so this flag will
  9964. * take effect when we commit and program watermarks.
  9965. */
  9966. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
  9967. needs_scaling(to_intel_plane_state(plane_state)) &&
  9968. !needs_scaling(old_plane_state))
  9969. pipe_config->disable_lp_wm = true;
  9970. return 0;
  9971. }
  9972. static bool encoders_cloneable(const struct intel_encoder *a,
  9973. const struct intel_encoder *b)
  9974. {
  9975. /* masks could be asymmetric, so check both ways */
  9976. return a == b || (a->cloneable & (1 << b->type) &&
  9977. b->cloneable & (1 << a->type));
  9978. }
  9979. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9980. struct intel_crtc *crtc,
  9981. struct intel_encoder *encoder)
  9982. {
  9983. struct intel_encoder *source_encoder;
  9984. struct drm_connector *connector;
  9985. struct drm_connector_state *connector_state;
  9986. int i;
  9987. for_each_connector_in_state(state, connector, connector_state, i) {
  9988. if (connector_state->crtc != &crtc->base)
  9989. continue;
  9990. source_encoder =
  9991. to_intel_encoder(connector_state->best_encoder);
  9992. if (!encoders_cloneable(encoder, source_encoder))
  9993. return false;
  9994. }
  9995. return true;
  9996. }
  9997. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9998. struct intel_crtc *crtc)
  9999. {
  10000. struct intel_encoder *encoder;
  10001. struct drm_connector *connector;
  10002. struct drm_connector_state *connector_state;
  10003. int i;
  10004. for_each_connector_in_state(state, connector, connector_state, i) {
  10005. if (connector_state->crtc != &crtc->base)
  10006. continue;
  10007. encoder = to_intel_encoder(connector_state->best_encoder);
  10008. if (!check_single_encoder_cloning(state, crtc, encoder))
  10009. return false;
  10010. }
  10011. return true;
  10012. }
  10013. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10014. struct drm_crtc_state *crtc_state)
  10015. {
  10016. struct drm_device *dev = crtc->dev;
  10017. struct drm_i915_private *dev_priv = dev->dev_private;
  10018. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10019. struct intel_crtc_state *pipe_config =
  10020. to_intel_crtc_state(crtc_state);
  10021. struct drm_atomic_state *state = crtc_state->state;
  10022. int ret;
  10023. bool mode_changed = needs_modeset(crtc_state);
  10024. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  10025. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10026. return -EINVAL;
  10027. }
  10028. if (mode_changed && !crtc_state->active)
  10029. pipe_config->update_wm_post = true;
  10030. if (mode_changed && crtc_state->enable &&
  10031. dev_priv->display.crtc_compute_clock &&
  10032. !WARN_ON(pipe_config->shared_dpll)) {
  10033. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10034. pipe_config);
  10035. if (ret)
  10036. return ret;
  10037. }
  10038. if (crtc_state->color_mgmt_changed) {
  10039. ret = intel_color_check(crtc, crtc_state);
  10040. if (ret)
  10041. return ret;
  10042. }
  10043. ret = 0;
  10044. if (dev_priv->display.compute_pipe_wm) {
  10045. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10046. if (ret) {
  10047. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10048. return ret;
  10049. }
  10050. }
  10051. if (dev_priv->display.compute_intermediate_wm &&
  10052. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10053. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10054. return 0;
  10055. /*
  10056. * Calculate 'intermediate' watermarks that satisfy both the
  10057. * old state and the new state. We can program these
  10058. * immediately.
  10059. */
  10060. ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
  10061. intel_crtc,
  10062. pipe_config);
  10063. if (ret) {
  10064. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10065. return ret;
  10066. }
  10067. }
  10068. if (INTEL_INFO(dev)->gen >= 9) {
  10069. if (mode_changed)
  10070. ret = skl_update_scaler_crtc(pipe_config);
  10071. if (!ret)
  10072. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10073. pipe_config);
  10074. }
  10075. return ret;
  10076. }
  10077. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10078. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10079. .atomic_begin = intel_begin_crtc_commit,
  10080. .atomic_flush = intel_finish_crtc_commit,
  10081. .atomic_check = intel_crtc_atomic_check,
  10082. };
  10083. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10084. {
  10085. struct intel_connector *connector;
  10086. for_each_intel_connector(dev, connector) {
  10087. if (connector->base.encoder) {
  10088. connector->base.state->best_encoder =
  10089. connector->base.encoder;
  10090. connector->base.state->crtc =
  10091. connector->base.encoder->crtc;
  10092. } else {
  10093. connector->base.state->best_encoder = NULL;
  10094. connector->base.state->crtc = NULL;
  10095. }
  10096. }
  10097. }
  10098. static void
  10099. connected_sink_compute_bpp(struct intel_connector *connector,
  10100. struct intel_crtc_state *pipe_config)
  10101. {
  10102. int bpp = pipe_config->pipe_bpp;
  10103. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10104. connector->base.base.id,
  10105. connector->base.name);
  10106. /* Don't use an invalid EDID bpc value */
  10107. if (connector->base.display_info.bpc &&
  10108. connector->base.display_info.bpc * 3 < bpp) {
  10109. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10110. bpp, connector->base.display_info.bpc*3);
  10111. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10112. }
  10113. /* Clamp bpp to default limit on screens without EDID 1.4 */
  10114. if (connector->base.display_info.bpc == 0) {
  10115. int type = connector->base.connector_type;
  10116. int clamp_bpp = 24;
  10117. /* Fall back to 18 bpp when DP sink capability is unknown. */
  10118. if (type == DRM_MODE_CONNECTOR_DisplayPort ||
  10119. type == DRM_MODE_CONNECTOR_eDP)
  10120. clamp_bpp = 18;
  10121. if (bpp > clamp_bpp) {
  10122. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
  10123. bpp, clamp_bpp);
  10124. pipe_config->pipe_bpp = clamp_bpp;
  10125. }
  10126. }
  10127. }
  10128. static int
  10129. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10130. struct intel_crtc_state *pipe_config)
  10131. {
  10132. struct drm_device *dev = crtc->base.dev;
  10133. struct drm_atomic_state *state;
  10134. struct drm_connector *connector;
  10135. struct drm_connector_state *connector_state;
  10136. int bpp, i;
  10137. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  10138. bpp = 10*3;
  10139. else if (INTEL_INFO(dev)->gen >= 5)
  10140. bpp = 12*3;
  10141. else
  10142. bpp = 8*3;
  10143. pipe_config->pipe_bpp = bpp;
  10144. state = pipe_config->base.state;
  10145. /* Clamp display bpp to EDID value */
  10146. for_each_connector_in_state(state, connector, connector_state, i) {
  10147. if (connector_state->crtc != &crtc->base)
  10148. continue;
  10149. connected_sink_compute_bpp(to_intel_connector(connector),
  10150. pipe_config);
  10151. }
  10152. return bpp;
  10153. }
  10154. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10155. {
  10156. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10157. "type: 0x%x flags: 0x%x\n",
  10158. mode->crtc_clock,
  10159. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10160. mode->crtc_hsync_end, mode->crtc_htotal,
  10161. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10162. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10163. }
  10164. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10165. struct intel_crtc_state *pipe_config,
  10166. const char *context)
  10167. {
  10168. struct drm_device *dev = crtc->base.dev;
  10169. struct drm_plane *plane;
  10170. struct intel_plane *intel_plane;
  10171. struct intel_plane_state *state;
  10172. struct drm_framebuffer *fb;
  10173. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  10174. context, pipe_config, pipe_name(crtc->pipe));
  10175. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  10176. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10177. pipe_config->pipe_bpp, pipe_config->dither);
  10178. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10179. pipe_config->has_pch_encoder,
  10180. pipe_config->fdi_lanes,
  10181. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10182. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10183. pipe_config->fdi_m_n.tu);
  10184. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10185. pipe_config->has_dp_encoder,
  10186. pipe_config->lane_count,
  10187. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10188. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10189. pipe_config->dp_m_n.tu);
  10190. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10191. pipe_config->has_dp_encoder,
  10192. pipe_config->lane_count,
  10193. pipe_config->dp_m2_n2.gmch_m,
  10194. pipe_config->dp_m2_n2.gmch_n,
  10195. pipe_config->dp_m2_n2.link_m,
  10196. pipe_config->dp_m2_n2.link_n,
  10197. pipe_config->dp_m2_n2.tu);
  10198. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10199. pipe_config->has_audio,
  10200. pipe_config->has_infoframe);
  10201. DRM_DEBUG_KMS("requested mode:\n");
  10202. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10203. DRM_DEBUG_KMS("adjusted mode:\n");
  10204. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10205. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10206. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10207. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10208. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10209. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10210. crtc->num_scalers,
  10211. pipe_config->scaler_state.scaler_users,
  10212. pipe_config->scaler_state.scaler_id);
  10213. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10214. pipe_config->gmch_pfit.control,
  10215. pipe_config->gmch_pfit.pgm_ratios,
  10216. pipe_config->gmch_pfit.lvds_border_bits);
  10217. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10218. pipe_config->pch_pfit.pos,
  10219. pipe_config->pch_pfit.size,
  10220. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10221. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10222. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10223. if (IS_BROXTON(dev)) {
  10224. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10225. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10226. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10227. pipe_config->ddi_pll_sel,
  10228. pipe_config->dpll_hw_state.ebb0,
  10229. pipe_config->dpll_hw_state.ebb4,
  10230. pipe_config->dpll_hw_state.pll0,
  10231. pipe_config->dpll_hw_state.pll1,
  10232. pipe_config->dpll_hw_state.pll2,
  10233. pipe_config->dpll_hw_state.pll3,
  10234. pipe_config->dpll_hw_state.pll6,
  10235. pipe_config->dpll_hw_state.pll8,
  10236. pipe_config->dpll_hw_state.pll9,
  10237. pipe_config->dpll_hw_state.pll10,
  10238. pipe_config->dpll_hw_state.pcsdw12);
  10239. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10240. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10241. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10242. pipe_config->ddi_pll_sel,
  10243. pipe_config->dpll_hw_state.ctrl1,
  10244. pipe_config->dpll_hw_state.cfgcr1,
  10245. pipe_config->dpll_hw_state.cfgcr2);
  10246. } else if (HAS_DDI(dev)) {
  10247. DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10248. pipe_config->ddi_pll_sel,
  10249. pipe_config->dpll_hw_state.wrpll,
  10250. pipe_config->dpll_hw_state.spll);
  10251. } else {
  10252. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10253. "fp0: 0x%x, fp1: 0x%x\n",
  10254. pipe_config->dpll_hw_state.dpll,
  10255. pipe_config->dpll_hw_state.dpll_md,
  10256. pipe_config->dpll_hw_state.fp0,
  10257. pipe_config->dpll_hw_state.fp1);
  10258. }
  10259. DRM_DEBUG_KMS("planes on this crtc\n");
  10260. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10261. intel_plane = to_intel_plane(plane);
  10262. if (intel_plane->pipe != crtc->pipe)
  10263. continue;
  10264. state = to_intel_plane_state(plane->state);
  10265. fb = state->base.fb;
  10266. if (!fb) {
  10267. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10268. "disabled, scaler_id = %d\n",
  10269. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10270. plane->base.id, intel_plane->pipe,
  10271. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10272. drm_plane_index(plane), state->scaler_id);
  10273. continue;
  10274. }
  10275. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10276. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10277. plane->base.id, intel_plane->pipe,
  10278. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10279. drm_plane_index(plane));
  10280. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10281. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10282. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10283. state->scaler_id,
  10284. state->src.x1 >> 16, state->src.y1 >> 16,
  10285. drm_rect_width(&state->src) >> 16,
  10286. drm_rect_height(&state->src) >> 16,
  10287. state->dst.x1, state->dst.y1,
  10288. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10289. }
  10290. }
  10291. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10292. {
  10293. struct drm_device *dev = state->dev;
  10294. struct drm_connector *connector;
  10295. unsigned int used_ports = 0;
  10296. /*
  10297. * Walk the connector list instead of the encoder
  10298. * list to detect the problem on ddi platforms
  10299. * where there's just one encoder per digital port.
  10300. */
  10301. drm_for_each_connector(connector, dev) {
  10302. struct drm_connector_state *connector_state;
  10303. struct intel_encoder *encoder;
  10304. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10305. if (!connector_state)
  10306. connector_state = connector->state;
  10307. if (!connector_state->best_encoder)
  10308. continue;
  10309. encoder = to_intel_encoder(connector_state->best_encoder);
  10310. WARN_ON(!connector_state->crtc);
  10311. switch (encoder->type) {
  10312. unsigned int port_mask;
  10313. case INTEL_OUTPUT_UNKNOWN:
  10314. if (WARN_ON(!HAS_DDI(dev)))
  10315. break;
  10316. case INTEL_OUTPUT_DISPLAYPORT:
  10317. case INTEL_OUTPUT_HDMI:
  10318. case INTEL_OUTPUT_EDP:
  10319. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10320. /* the same port mustn't appear more than once */
  10321. if (used_ports & port_mask)
  10322. return false;
  10323. used_ports |= port_mask;
  10324. default:
  10325. break;
  10326. }
  10327. }
  10328. return true;
  10329. }
  10330. static void
  10331. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10332. {
  10333. struct drm_crtc_state tmp_state;
  10334. struct intel_crtc_scaler_state scaler_state;
  10335. struct intel_dpll_hw_state dpll_hw_state;
  10336. struct intel_shared_dpll *shared_dpll;
  10337. uint32_t ddi_pll_sel;
  10338. bool force_thru;
  10339. /* FIXME: before the switch to atomic started, a new pipe_config was
  10340. * kzalloc'd. Code that depends on any field being zero should be
  10341. * fixed, so that the crtc_state can be safely duplicated. For now,
  10342. * only fields that are know to not cause problems are preserved. */
  10343. tmp_state = crtc_state->base;
  10344. scaler_state = crtc_state->scaler_state;
  10345. shared_dpll = crtc_state->shared_dpll;
  10346. dpll_hw_state = crtc_state->dpll_hw_state;
  10347. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10348. force_thru = crtc_state->pch_pfit.force_thru;
  10349. memset(crtc_state, 0, sizeof *crtc_state);
  10350. crtc_state->base = tmp_state;
  10351. crtc_state->scaler_state = scaler_state;
  10352. crtc_state->shared_dpll = shared_dpll;
  10353. crtc_state->dpll_hw_state = dpll_hw_state;
  10354. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10355. crtc_state->pch_pfit.force_thru = force_thru;
  10356. }
  10357. static int
  10358. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10359. struct intel_crtc_state *pipe_config)
  10360. {
  10361. struct drm_atomic_state *state = pipe_config->base.state;
  10362. struct intel_encoder *encoder;
  10363. struct drm_connector *connector;
  10364. struct drm_connector_state *connector_state;
  10365. int base_bpp, ret = -EINVAL;
  10366. int i;
  10367. bool retry = true;
  10368. clear_intel_crtc_state(pipe_config);
  10369. pipe_config->cpu_transcoder =
  10370. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10371. /*
  10372. * Sanitize sync polarity flags based on requested ones. If neither
  10373. * positive or negative polarity is requested, treat this as meaning
  10374. * negative polarity.
  10375. */
  10376. if (!(pipe_config->base.adjusted_mode.flags &
  10377. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10378. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10379. if (!(pipe_config->base.adjusted_mode.flags &
  10380. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10381. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10382. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10383. pipe_config);
  10384. if (base_bpp < 0)
  10385. goto fail;
  10386. /*
  10387. * Determine the real pipe dimensions. Note that stereo modes can
  10388. * increase the actual pipe size due to the frame doubling and
  10389. * insertion of additional space for blanks between the frame. This
  10390. * is stored in the crtc timings. We use the requested mode to do this
  10391. * computation to clearly distinguish it from the adjusted mode, which
  10392. * can be changed by the connectors in the below retry loop.
  10393. */
  10394. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10395. &pipe_config->pipe_src_w,
  10396. &pipe_config->pipe_src_h);
  10397. encoder_retry:
  10398. /* Ensure the port clock defaults are reset when retrying. */
  10399. pipe_config->port_clock = 0;
  10400. pipe_config->pixel_multiplier = 1;
  10401. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10402. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10403. CRTC_STEREO_DOUBLE);
  10404. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10405. * adjust it according to limitations or connector properties, and also
  10406. * a chance to reject the mode entirely.
  10407. */
  10408. for_each_connector_in_state(state, connector, connector_state, i) {
  10409. if (connector_state->crtc != crtc)
  10410. continue;
  10411. encoder = to_intel_encoder(connector_state->best_encoder);
  10412. if (!(encoder->compute_config(encoder, pipe_config))) {
  10413. DRM_DEBUG_KMS("Encoder config failure\n");
  10414. goto fail;
  10415. }
  10416. }
  10417. /* Set default port clock if not overwritten by the encoder. Needs to be
  10418. * done afterwards in case the encoder adjusts the mode. */
  10419. if (!pipe_config->port_clock)
  10420. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10421. * pipe_config->pixel_multiplier;
  10422. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10423. if (ret < 0) {
  10424. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10425. goto fail;
  10426. }
  10427. if (ret == RETRY) {
  10428. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10429. ret = -EINVAL;
  10430. goto fail;
  10431. }
  10432. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10433. retry = false;
  10434. goto encoder_retry;
  10435. }
  10436. /* Dithering seems to not pass-through bits correctly when it should, so
  10437. * only enable it on 6bpc panels. */
  10438. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10439. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10440. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10441. fail:
  10442. return ret;
  10443. }
  10444. static void
  10445. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10446. {
  10447. struct drm_crtc *crtc;
  10448. struct drm_crtc_state *crtc_state;
  10449. int i;
  10450. /* Double check state. */
  10451. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10452. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10453. /* Update hwmode for vblank functions */
  10454. if (crtc->state->active)
  10455. crtc->hwmode = crtc->state->adjusted_mode;
  10456. else
  10457. crtc->hwmode.crtc_clock = 0;
  10458. /*
  10459. * Update legacy state to satisfy fbc code. This can
  10460. * be removed when fbc uses the atomic state.
  10461. */
  10462. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10463. struct drm_plane_state *plane_state = crtc->primary->state;
  10464. crtc->primary->fb = plane_state->fb;
  10465. crtc->x = plane_state->src_x >> 16;
  10466. crtc->y = plane_state->src_y >> 16;
  10467. }
  10468. }
  10469. }
  10470. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10471. {
  10472. int diff;
  10473. if (clock1 == clock2)
  10474. return true;
  10475. if (!clock1 || !clock2)
  10476. return false;
  10477. diff = abs(clock1 - clock2);
  10478. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10479. return true;
  10480. return false;
  10481. }
  10482. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10483. list_for_each_entry((intel_crtc), \
  10484. &(dev)->mode_config.crtc_list, \
  10485. base.head) \
  10486. for_each_if (mask & (1 <<(intel_crtc)->pipe))
  10487. static bool
  10488. intel_compare_m_n(unsigned int m, unsigned int n,
  10489. unsigned int m2, unsigned int n2,
  10490. bool exact)
  10491. {
  10492. if (m == m2 && n == n2)
  10493. return true;
  10494. if (exact || !m || !n || !m2 || !n2)
  10495. return false;
  10496. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10497. if (n > n2) {
  10498. while (n > n2) {
  10499. m2 <<= 1;
  10500. n2 <<= 1;
  10501. }
  10502. } else if (n < n2) {
  10503. while (n < n2) {
  10504. m <<= 1;
  10505. n <<= 1;
  10506. }
  10507. }
  10508. if (n != n2)
  10509. return false;
  10510. return intel_fuzzy_clock_check(m, m2);
  10511. }
  10512. static bool
  10513. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10514. struct intel_link_m_n *m2_n2,
  10515. bool adjust)
  10516. {
  10517. if (m_n->tu == m2_n2->tu &&
  10518. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10519. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10520. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10521. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10522. if (adjust)
  10523. *m2_n2 = *m_n;
  10524. return true;
  10525. }
  10526. return false;
  10527. }
  10528. static bool
  10529. intel_pipe_config_compare(struct drm_device *dev,
  10530. struct intel_crtc_state *current_config,
  10531. struct intel_crtc_state *pipe_config,
  10532. bool adjust)
  10533. {
  10534. bool ret = true;
  10535. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10536. do { \
  10537. if (!adjust) \
  10538. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10539. else \
  10540. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10541. } while (0)
  10542. #define PIPE_CONF_CHECK_X(name) \
  10543. if (current_config->name != pipe_config->name) { \
  10544. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10545. "(expected 0x%08x, found 0x%08x)\n", \
  10546. current_config->name, \
  10547. pipe_config->name); \
  10548. ret = false; \
  10549. }
  10550. #define PIPE_CONF_CHECK_I(name) \
  10551. if (current_config->name != pipe_config->name) { \
  10552. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10553. "(expected %i, found %i)\n", \
  10554. current_config->name, \
  10555. pipe_config->name); \
  10556. ret = false; \
  10557. }
  10558. #define PIPE_CONF_CHECK_P(name) \
  10559. if (current_config->name != pipe_config->name) { \
  10560. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10561. "(expected %p, found %p)\n", \
  10562. current_config->name, \
  10563. pipe_config->name); \
  10564. ret = false; \
  10565. }
  10566. #define PIPE_CONF_CHECK_M_N(name) \
  10567. if (!intel_compare_link_m_n(&current_config->name, \
  10568. &pipe_config->name,\
  10569. adjust)) { \
  10570. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10571. "(expected tu %i gmch %i/%i link %i/%i, " \
  10572. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10573. current_config->name.tu, \
  10574. current_config->name.gmch_m, \
  10575. current_config->name.gmch_n, \
  10576. current_config->name.link_m, \
  10577. current_config->name.link_n, \
  10578. pipe_config->name.tu, \
  10579. pipe_config->name.gmch_m, \
  10580. pipe_config->name.gmch_n, \
  10581. pipe_config->name.link_m, \
  10582. pipe_config->name.link_n); \
  10583. ret = false; \
  10584. }
  10585. /* This is required for BDW+ where there is only one set of registers for
  10586. * switching between high and low RR.
  10587. * This macro can be used whenever a comparison has to be made between one
  10588. * hw state and multiple sw state variables.
  10589. */
  10590. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10591. if (!intel_compare_link_m_n(&current_config->name, \
  10592. &pipe_config->name, adjust) && \
  10593. !intel_compare_link_m_n(&current_config->alt_name, \
  10594. &pipe_config->name, adjust)) { \
  10595. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10596. "(expected tu %i gmch %i/%i link %i/%i, " \
  10597. "or tu %i gmch %i/%i link %i/%i, " \
  10598. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10599. current_config->name.tu, \
  10600. current_config->name.gmch_m, \
  10601. current_config->name.gmch_n, \
  10602. current_config->name.link_m, \
  10603. current_config->name.link_n, \
  10604. current_config->alt_name.tu, \
  10605. current_config->alt_name.gmch_m, \
  10606. current_config->alt_name.gmch_n, \
  10607. current_config->alt_name.link_m, \
  10608. current_config->alt_name.link_n, \
  10609. pipe_config->name.tu, \
  10610. pipe_config->name.gmch_m, \
  10611. pipe_config->name.gmch_n, \
  10612. pipe_config->name.link_m, \
  10613. pipe_config->name.link_n); \
  10614. ret = false; \
  10615. }
  10616. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10617. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10618. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10619. "(expected %i, found %i)\n", \
  10620. current_config->name & (mask), \
  10621. pipe_config->name & (mask)); \
  10622. ret = false; \
  10623. }
  10624. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10625. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10626. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10627. "(expected %i, found %i)\n", \
  10628. current_config->name, \
  10629. pipe_config->name); \
  10630. ret = false; \
  10631. }
  10632. #define PIPE_CONF_QUIRK(quirk) \
  10633. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10634. PIPE_CONF_CHECK_I(cpu_transcoder);
  10635. PIPE_CONF_CHECK_I(has_pch_encoder);
  10636. PIPE_CONF_CHECK_I(fdi_lanes);
  10637. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10638. PIPE_CONF_CHECK_I(has_dp_encoder);
  10639. PIPE_CONF_CHECK_I(lane_count);
  10640. if (INTEL_INFO(dev)->gen < 8) {
  10641. PIPE_CONF_CHECK_M_N(dp_m_n);
  10642. if (current_config->has_drrs)
  10643. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10644. } else
  10645. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10646. PIPE_CONF_CHECK_I(has_dsi_encoder);
  10647. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10648. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10649. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10650. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10651. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10652. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10653. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10654. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10655. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10656. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10657. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10658. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10659. PIPE_CONF_CHECK_I(pixel_multiplier);
  10660. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10661. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10662. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  10663. PIPE_CONF_CHECK_I(limited_color_range);
  10664. PIPE_CONF_CHECK_I(has_infoframe);
  10665. PIPE_CONF_CHECK_I(has_audio);
  10666. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10667. DRM_MODE_FLAG_INTERLACE);
  10668. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10669. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10670. DRM_MODE_FLAG_PHSYNC);
  10671. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10672. DRM_MODE_FLAG_NHSYNC);
  10673. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10674. DRM_MODE_FLAG_PVSYNC);
  10675. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10676. DRM_MODE_FLAG_NVSYNC);
  10677. }
  10678. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10679. /* pfit ratios are autocomputed by the hw on gen4+ */
  10680. if (INTEL_INFO(dev)->gen < 4)
  10681. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  10682. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10683. if (!adjust) {
  10684. PIPE_CONF_CHECK_I(pipe_src_w);
  10685. PIPE_CONF_CHECK_I(pipe_src_h);
  10686. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10687. if (current_config->pch_pfit.enabled) {
  10688. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10689. PIPE_CONF_CHECK_X(pch_pfit.size);
  10690. }
  10691. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10692. }
  10693. /* BDW+ don't expose a synchronous way to read the state */
  10694. if (IS_HASWELL(dev))
  10695. PIPE_CONF_CHECK_I(ips_enabled);
  10696. PIPE_CONF_CHECK_I(double_wide);
  10697. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10698. PIPE_CONF_CHECK_P(shared_dpll);
  10699. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10700. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10701. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10702. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10703. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10704. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10705. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10706. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10707. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10708. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  10709. PIPE_CONF_CHECK_X(dsi_pll.div);
  10710. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10711. PIPE_CONF_CHECK_I(pipe_bpp);
  10712. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10713. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10714. #undef PIPE_CONF_CHECK_X
  10715. #undef PIPE_CONF_CHECK_I
  10716. #undef PIPE_CONF_CHECK_P
  10717. #undef PIPE_CONF_CHECK_FLAGS
  10718. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10719. #undef PIPE_CONF_QUIRK
  10720. #undef INTEL_ERR_OR_DBG_KMS
  10721. return ret;
  10722. }
  10723. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  10724. const struct intel_crtc_state *pipe_config)
  10725. {
  10726. if (pipe_config->has_pch_encoder) {
  10727. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  10728. &pipe_config->fdi_m_n);
  10729. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  10730. /*
  10731. * FDI already provided one idea for the dotclock.
  10732. * Yell if the encoder disagrees.
  10733. */
  10734. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  10735. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10736. fdi_dotclock, dotclock);
  10737. }
  10738. }
  10739. static void verify_wm_state(struct drm_crtc *crtc,
  10740. struct drm_crtc_state *new_state)
  10741. {
  10742. struct drm_device *dev = crtc->dev;
  10743. struct drm_i915_private *dev_priv = dev->dev_private;
  10744. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10745. struct skl_ddb_entry *hw_entry, *sw_entry;
  10746. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10747. const enum pipe pipe = intel_crtc->pipe;
  10748. int plane;
  10749. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  10750. return;
  10751. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10752. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10753. /* planes */
  10754. for_each_plane(dev_priv, pipe, plane) {
  10755. hw_entry = &hw_ddb.plane[pipe][plane];
  10756. sw_entry = &sw_ddb->plane[pipe][plane];
  10757. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10758. continue;
  10759. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10760. "(expected (%u,%u), found (%u,%u))\n",
  10761. pipe_name(pipe), plane + 1,
  10762. sw_entry->start, sw_entry->end,
  10763. hw_entry->start, hw_entry->end);
  10764. }
  10765. /* cursor */
  10766. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10767. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10768. if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
  10769. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10770. "(expected (%u,%u), found (%u,%u))\n",
  10771. pipe_name(pipe),
  10772. sw_entry->start, sw_entry->end,
  10773. hw_entry->start, hw_entry->end);
  10774. }
  10775. }
  10776. static void
  10777. verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
  10778. {
  10779. struct drm_connector *connector;
  10780. drm_for_each_connector(connector, dev) {
  10781. struct drm_encoder *encoder = connector->encoder;
  10782. struct drm_connector_state *state = connector->state;
  10783. if (state->crtc != crtc)
  10784. continue;
  10785. intel_connector_verify_state(to_intel_connector(connector));
  10786. I915_STATE_WARN(state->best_encoder != encoder,
  10787. "connector's atomic encoder doesn't match legacy encoder\n");
  10788. }
  10789. }
  10790. static void
  10791. verify_encoder_state(struct drm_device *dev)
  10792. {
  10793. struct intel_encoder *encoder;
  10794. struct intel_connector *connector;
  10795. for_each_intel_encoder(dev, encoder) {
  10796. bool enabled = false;
  10797. enum pipe pipe;
  10798. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10799. encoder->base.base.id,
  10800. encoder->base.name);
  10801. for_each_intel_connector(dev, connector) {
  10802. if (connector->base.state->best_encoder != &encoder->base)
  10803. continue;
  10804. enabled = true;
  10805. I915_STATE_WARN(connector->base.state->crtc !=
  10806. encoder->base.crtc,
  10807. "connector's crtc doesn't match encoder crtc\n");
  10808. }
  10809. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10810. "encoder's enabled state mismatch "
  10811. "(expected %i, found %i)\n",
  10812. !!encoder->base.crtc, enabled);
  10813. if (!encoder->base.crtc) {
  10814. bool active;
  10815. active = encoder->get_hw_state(encoder, &pipe);
  10816. I915_STATE_WARN(active,
  10817. "encoder detached but still enabled on pipe %c.\n",
  10818. pipe_name(pipe));
  10819. }
  10820. }
  10821. }
  10822. static void
  10823. verify_crtc_state(struct drm_crtc *crtc,
  10824. struct drm_crtc_state *old_crtc_state,
  10825. struct drm_crtc_state *new_crtc_state)
  10826. {
  10827. struct drm_device *dev = crtc->dev;
  10828. struct drm_i915_private *dev_priv = dev->dev_private;
  10829. struct intel_encoder *encoder;
  10830. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10831. struct intel_crtc_state *pipe_config, *sw_config;
  10832. struct drm_atomic_state *old_state;
  10833. bool active;
  10834. old_state = old_crtc_state->state;
  10835. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10836. pipe_config = to_intel_crtc_state(old_crtc_state);
  10837. memset(pipe_config, 0, sizeof(*pipe_config));
  10838. pipe_config->base.crtc = crtc;
  10839. pipe_config->base.state = old_state;
  10840. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  10841. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10842. /* hw state is inconsistent with the pipe quirk */
  10843. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10844. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10845. active = new_crtc_state->active;
  10846. I915_STATE_WARN(new_crtc_state->active != active,
  10847. "crtc active state doesn't match with hw state "
  10848. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10849. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10850. "transitional active state does not match atomic hw state "
  10851. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10852. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10853. enum pipe pipe;
  10854. active = encoder->get_hw_state(encoder, &pipe);
  10855. I915_STATE_WARN(active != new_crtc_state->active,
  10856. "[ENCODER:%i] active %i with crtc active %i\n",
  10857. encoder->base.base.id, active, new_crtc_state->active);
  10858. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10859. "Encoder connected to wrong pipe %c\n",
  10860. pipe_name(pipe));
  10861. if (active)
  10862. encoder->get_config(encoder, pipe_config);
  10863. }
  10864. if (!new_crtc_state->active)
  10865. return;
  10866. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10867. sw_config = to_intel_crtc_state(crtc->state);
  10868. if (!intel_pipe_config_compare(dev, sw_config,
  10869. pipe_config, false)) {
  10870. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10871. intel_dump_pipe_config(intel_crtc, pipe_config,
  10872. "[hw state]");
  10873. intel_dump_pipe_config(intel_crtc, sw_config,
  10874. "[sw state]");
  10875. }
  10876. }
  10877. static void
  10878. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10879. struct intel_shared_dpll *pll,
  10880. struct drm_crtc *crtc,
  10881. struct drm_crtc_state *new_state)
  10882. {
  10883. struct intel_dpll_hw_state dpll_hw_state;
  10884. unsigned crtc_mask;
  10885. bool active;
  10886. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10887. DRM_DEBUG_KMS("%s\n", pll->name);
  10888. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10889. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10890. I915_STATE_WARN(!pll->on && pll->active_mask,
  10891. "pll in active use but not on in sw tracking\n");
  10892. I915_STATE_WARN(pll->on && !pll->active_mask,
  10893. "pll is on but not used by any active crtc\n");
  10894. I915_STATE_WARN(pll->on != active,
  10895. "pll on state mismatch (expected %i, found %i)\n",
  10896. pll->on, active);
  10897. }
  10898. if (!crtc) {
  10899. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  10900. "more active pll users than references: %x vs %x\n",
  10901. pll->active_mask, pll->config.crtc_mask);
  10902. return;
  10903. }
  10904. crtc_mask = 1 << drm_crtc_index(crtc);
  10905. if (new_state->active)
  10906. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10907. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10908. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10909. else
  10910. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10911. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10912. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10913. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  10914. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10915. crtc_mask, pll->config.crtc_mask);
  10916. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  10917. &dpll_hw_state,
  10918. sizeof(dpll_hw_state)),
  10919. "pll hw state mismatch\n");
  10920. }
  10921. static void
  10922. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10923. struct drm_crtc_state *old_crtc_state,
  10924. struct drm_crtc_state *new_crtc_state)
  10925. {
  10926. struct drm_i915_private *dev_priv = dev->dev_private;
  10927. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10928. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10929. if (new_state->shared_dpll)
  10930. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10931. if (old_state->shared_dpll &&
  10932. old_state->shared_dpll != new_state->shared_dpll) {
  10933. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  10934. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10935. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10936. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10937. pipe_name(drm_crtc_index(crtc)));
  10938. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  10939. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10940. pipe_name(drm_crtc_index(crtc)));
  10941. }
  10942. }
  10943. static void
  10944. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  10945. struct drm_crtc_state *old_state,
  10946. struct drm_crtc_state *new_state)
  10947. {
  10948. if (!needs_modeset(new_state) &&
  10949. !to_intel_crtc_state(new_state)->update_pipe)
  10950. return;
  10951. verify_wm_state(crtc, new_state);
  10952. verify_connector_state(crtc->dev, crtc);
  10953. verify_crtc_state(crtc, old_state, new_state);
  10954. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  10955. }
  10956. static void
  10957. verify_disabled_dpll_state(struct drm_device *dev)
  10958. {
  10959. struct drm_i915_private *dev_priv = dev->dev_private;
  10960. int i;
  10961. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10962. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  10963. }
  10964. static void
  10965. intel_modeset_verify_disabled(struct drm_device *dev)
  10966. {
  10967. verify_encoder_state(dev);
  10968. verify_connector_state(dev, NULL);
  10969. verify_disabled_dpll_state(dev);
  10970. }
  10971. static void update_scanline_offset(struct intel_crtc *crtc)
  10972. {
  10973. struct drm_device *dev = crtc->base.dev;
  10974. /*
  10975. * The scanline counter increments at the leading edge of hsync.
  10976. *
  10977. * On most platforms it starts counting from vtotal-1 on the
  10978. * first active line. That means the scanline counter value is
  10979. * always one less than what we would expect. Ie. just after
  10980. * start of vblank, which also occurs at start of hsync (on the
  10981. * last active line), the scanline counter will read vblank_start-1.
  10982. *
  10983. * On gen2 the scanline counter starts counting from 1 instead
  10984. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10985. * to keep the value positive), instead of adding one.
  10986. *
  10987. * On HSW+ the behaviour of the scanline counter depends on the output
  10988. * type. For DP ports it behaves like most other platforms, but on HDMI
  10989. * there's an extra 1 line difference. So we need to add two instead of
  10990. * one to the value.
  10991. */
  10992. if (IS_GEN2(dev)) {
  10993. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10994. int vtotal;
  10995. vtotal = adjusted_mode->crtc_vtotal;
  10996. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10997. vtotal /= 2;
  10998. crtc->scanline_offset = vtotal - 1;
  10999. } else if (HAS_DDI(dev) &&
  11000. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  11001. crtc->scanline_offset = 2;
  11002. } else
  11003. crtc->scanline_offset = 1;
  11004. }
  11005. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11006. {
  11007. struct drm_device *dev = state->dev;
  11008. struct drm_i915_private *dev_priv = to_i915(dev);
  11009. struct intel_shared_dpll_config *shared_dpll = NULL;
  11010. struct drm_crtc *crtc;
  11011. struct drm_crtc_state *crtc_state;
  11012. int i;
  11013. if (!dev_priv->display.crtc_compute_clock)
  11014. return;
  11015. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11016. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11017. struct intel_shared_dpll *old_dpll =
  11018. to_intel_crtc_state(crtc->state)->shared_dpll;
  11019. if (!needs_modeset(crtc_state))
  11020. continue;
  11021. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11022. if (!old_dpll)
  11023. continue;
  11024. if (!shared_dpll)
  11025. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11026. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11027. }
  11028. }
  11029. /*
  11030. * This implements the workaround described in the "notes" section of the mode
  11031. * set sequence documentation. When going from no pipes or single pipe to
  11032. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11033. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11034. */
  11035. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11036. {
  11037. struct drm_crtc_state *crtc_state;
  11038. struct intel_crtc *intel_crtc;
  11039. struct drm_crtc *crtc;
  11040. struct intel_crtc_state *first_crtc_state = NULL;
  11041. struct intel_crtc_state *other_crtc_state = NULL;
  11042. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11043. int i;
  11044. /* look at all crtc's that are going to be enabled in during modeset */
  11045. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11046. intel_crtc = to_intel_crtc(crtc);
  11047. if (!crtc_state->active || !needs_modeset(crtc_state))
  11048. continue;
  11049. if (first_crtc_state) {
  11050. other_crtc_state = to_intel_crtc_state(crtc_state);
  11051. break;
  11052. } else {
  11053. first_crtc_state = to_intel_crtc_state(crtc_state);
  11054. first_pipe = intel_crtc->pipe;
  11055. }
  11056. }
  11057. /* No workaround needed? */
  11058. if (!first_crtc_state)
  11059. return 0;
  11060. /* w/a possibly needed, check how many crtc's are already enabled. */
  11061. for_each_intel_crtc(state->dev, intel_crtc) {
  11062. struct intel_crtc_state *pipe_config;
  11063. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11064. if (IS_ERR(pipe_config))
  11065. return PTR_ERR(pipe_config);
  11066. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11067. if (!pipe_config->base.active ||
  11068. needs_modeset(&pipe_config->base))
  11069. continue;
  11070. /* 2 or more enabled crtcs means no need for w/a */
  11071. if (enabled_pipe != INVALID_PIPE)
  11072. return 0;
  11073. enabled_pipe = intel_crtc->pipe;
  11074. }
  11075. if (enabled_pipe != INVALID_PIPE)
  11076. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11077. else if (other_crtc_state)
  11078. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11079. return 0;
  11080. }
  11081. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11082. {
  11083. struct drm_crtc *crtc;
  11084. struct drm_crtc_state *crtc_state;
  11085. int ret = 0;
  11086. /* add all active pipes to the state */
  11087. for_each_crtc(state->dev, crtc) {
  11088. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11089. if (IS_ERR(crtc_state))
  11090. return PTR_ERR(crtc_state);
  11091. if (!crtc_state->active || needs_modeset(crtc_state))
  11092. continue;
  11093. crtc_state->mode_changed = true;
  11094. ret = drm_atomic_add_affected_connectors(state, crtc);
  11095. if (ret)
  11096. break;
  11097. ret = drm_atomic_add_affected_planes(state, crtc);
  11098. if (ret)
  11099. break;
  11100. }
  11101. return ret;
  11102. }
  11103. static int intel_modeset_checks(struct drm_atomic_state *state)
  11104. {
  11105. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11106. struct drm_i915_private *dev_priv = state->dev->dev_private;
  11107. struct drm_crtc *crtc;
  11108. struct drm_crtc_state *crtc_state;
  11109. int ret = 0, i;
  11110. if (!check_digital_port_conflicts(state)) {
  11111. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11112. return -EINVAL;
  11113. }
  11114. intel_state->modeset = true;
  11115. intel_state->active_crtcs = dev_priv->active_crtcs;
  11116. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11117. if (crtc_state->active)
  11118. intel_state->active_crtcs |= 1 << i;
  11119. else
  11120. intel_state->active_crtcs &= ~(1 << i);
  11121. }
  11122. /*
  11123. * See if the config requires any additional preparation, e.g.
  11124. * to adjust global state with pipes off. We need to do this
  11125. * here so we can get the modeset_pipe updated config for the new
  11126. * mode set on this crtc. For other crtcs we need to use the
  11127. * adjusted_mode bits in the crtc directly.
  11128. */
  11129. if (dev_priv->display.modeset_calc_cdclk) {
  11130. ret = dev_priv->display.modeset_calc_cdclk(state);
  11131. if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
  11132. ret = intel_modeset_all_pipes(state);
  11133. if (ret < 0)
  11134. return ret;
  11135. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11136. intel_state->cdclk, intel_state->dev_cdclk);
  11137. } else
  11138. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11139. intel_modeset_clear_plls(state);
  11140. if (IS_HASWELL(dev_priv))
  11141. return haswell_mode_set_planes_workaround(state);
  11142. return 0;
  11143. }
  11144. /*
  11145. * Handle calculation of various watermark data at the end of the atomic check
  11146. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11147. * handlers to ensure that all derived state has been updated.
  11148. */
  11149. static void calc_watermark_data(struct drm_atomic_state *state)
  11150. {
  11151. struct drm_device *dev = state->dev;
  11152. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11153. struct drm_crtc *crtc;
  11154. struct drm_crtc_state *cstate;
  11155. struct drm_plane *plane;
  11156. struct drm_plane_state *pstate;
  11157. /*
  11158. * Calculate watermark configuration details now that derived
  11159. * plane/crtc state is all properly updated.
  11160. */
  11161. drm_for_each_crtc(crtc, dev) {
  11162. cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
  11163. crtc->state;
  11164. if (cstate->active)
  11165. intel_state->wm_config.num_pipes_active++;
  11166. }
  11167. drm_for_each_legacy_plane(plane, dev) {
  11168. pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
  11169. plane->state;
  11170. if (!to_intel_plane_state(pstate)->visible)
  11171. continue;
  11172. intel_state->wm_config.sprites_enabled = true;
  11173. if (pstate->crtc_w != pstate->src_w >> 16 ||
  11174. pstate->crtc_h != pstate->src_h >> 16)
  11175. intel_state->wm_config.sprites_scaled = true;
  11176. }
  11177. }
  11178. /**
  11179. * intel_atomic_check - validate state object
  11180. * @dev: drm device
  11181. * @state: state to validate
  11182. */
  11183. static int intel_atomic_check(struct drm_device *dev,
  11184. struct drm_atomic_state *state)
  11185. {
  11186. struct drm_i915_private *dev_priv = to_i915(dev);
  11187. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11188. struct drm_crtc *crtc;
  11189. struct drm_crtc_state *crtc_state;
  11190. int ret, i;
  11191. bool any_ms = false;
  11192. ret = drm_atomic_helper_check_modeset(dev, state);
  11193. if (ret)
  11194. return ret;
  11195. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11196. struct intel_crtc_state *pipe_config =
  11197. to_intel_crtc_state(crtc_state);
  11198. /* Catch I915_MODE_FLAG_INHERITED */
  11199. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11200. crtc_state->mode_changed = true;
  11201. if (!crtc_state->enable) {
  11202. if (needs_modeset(crtc_state))
  11203. any_ms = true;
  11204. continue;
  11205. }
  11206. if (!needs_modeset(crtc_state))
  11207. continue;
  11208. /* FIXME: For only active_changed we shouldn't need to do any
  11209. * state recomputation at all. */
  11210. ret = drm_atomic_add_affected_connectors(state, crtc);
  11211. if (ret)
  11212. return ret;
  11213. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11214. if (ret)
  11215. return ret;
  11216. if (i915.fastboot &&
  11217. intel_pipe_config_compare(dev,
  11218. to_intel_crtc_state(crtc->state),
  11219. pipe_config, true)) {
  11220. crtc_state->mode_changed = false;
  11221. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11222. }
  11223. if (needs_modeset(crtc_state)) {
  11224. any_ms = true;
  11225. ret = drm_atomic_add_affected_planes(state, crtc);
  11226. if (ret)
  11227. return ret;
  11228. }
  11229. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11230. needs_modeset(crtc_state) ?
  11231. "[modeset]" : "[fastset]");
  11232. }
  11233. if (any_ms) {
  11234. ret = intel_modeset_checks(state);
  11235. if (ret)
  11236. return ret;
  11237. } else
  11238. intel_state->cdclk = dev_priv->cdclk_freq;
  11239. ret = drm_atomic_helper_check_planes(dev, state);
  11240. if (ret)
  11241. return ret;
  11242. intel_fbc_choose_crtc(dev_priv, state);
  11243. calc_watermark_data(state);
  11244. return 0;
  11245. }
  11246. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11247. struct drm_atomic_state *state,
  11248. bool async)
  11249. {
  11250. struct drm_i915_private *dev_priv = dev->dev_private;
  11251. struct drm_plane_state *plane_state;
  11252. struct drm_crtc_state *crtc_state;
  11253. struct drm_plane *plane;
  11254. struct drm_crtc *crtc;
  11255. int i, ret;
  11256. if (async) {
  11257. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  11258. return -EINVAL;
  11259. }
  11260. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11261. if (state->legacy_cursor_update)
  11262. continue;
  11263. ret = intel_crtc_wait_for_pending_flips(crtc);
  11264. if (ret)
  11265. return ret;
  11266. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11267. flush_workqueue(dev_priv->wq);
  11268. }
  11269. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11270. if (ret)
  11271. return ret;
  11272. ret = drm_atomic_helper_prepare_planes(dev, state);
  11273. mutex_unlock(&dev->struct_mutex);
  11274. if (!ret && !async) {
  11275. for_each_plane_in_state(state, plane, plane_state, i) {
  11276. struct intel_plane_state *intel_plane_state =
  11277. to_intel_plane_state(plane_state);
  11278. if (!intel_plane_state->wait_req)
  11279. continue;
  11280. ret = __i915_wait_request(intel_plane_state->wait_req,
  11281. true, NULL, NULL);
  11282. if (ret) {
  11283. /* Any hang should be swallowed by the wait */
  11284. WARN_ON(ret == -EIO);
  11285. mutex_lock(&dev->struct_mutex);
  11286. drm_atomic_helper_cleanup_planes(dev, state);
  11287. mutex_unlock(&dev->struct_mutex);
  11288. break;
  11289. }
  11290. }
  11291. }
  11292. return ret;
  11293. }
  11294. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11295. struct drm_i915_private *dev_priv,
  11296. unsigned crtc_mask)
  11297. {
  11298. unsigned last_vblank_count[I915_MAX_PIPES];
  11299. enum pipe pipe;
  11300. int ret;
  11301. if (!crtc_mask)
  11302. return;
  11303. for_each_pipe(dev_priv, pipe) {
  11304. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11305. if (!((1 << pipe) & crtc_mask))
  11306. continue;
  11307. ret = drm_crtc_vblank_get(crtc);
  11308. if (WARN_ON(ret != 0)) {
  11309. crtc_mask &= ~(1 << pipe);
  11310. continue;
  11311. }
  11312. last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
  11313. }
  11314. for_each_pipe(dev_priv, pipe) {
  11315. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11316. long lret;
  11317. if (!((1 << pipe) & crtc_mask))
  11318. continue;
  11319. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11320. last_vblank_count[pipe] !=
  11321. drm_crtc_vblank_count(crtc),
  11322. msecs_to_jiffies(50));
  11323. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11324. drm_crtc_vblank_put(crtc);
  11325. }
  11326. }
  11327. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11328. {
  11329. /* fb updated, need to unpin old fb */
  11330. if (crtc_state->fb_changed)
  11331. return true;
  11332. /* wm changes, need vblank before final wm's */
  11333. if (crtc_state->update_wm_post)
  11334. return true;
  11335. /*
  11336. * cxsr is re-enabled after vblank.
  11337. * This is already handled by crtc_state->update_wm_post,
  11338. * but added for clarity.
  11339. */
  11340. if (crtc_state->disable_cxsr)
  11341. return true;
  11342. return false;
  11343. }
  11344. /**
  11345. * intel_atomic_commit - commit validated state object
  11346. * @dev: DRM device
  11347. * @state: the top-level driver state object
  11348. * @async: asynchronous commit
  11349. *
  11350. * This function commits a top-level state object that has been validated
  11351. * with drm_atomic_helper_check().
  11352. *
  11353. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11354. * we can only handle plane-related operations and do not yet support
  11355. * asynchronous commit.
  11356. *
  11357. * RETURNS
  11358. * Zero for success or -errno.
  11359. */
  11360. static int intel_atomic_commit(struct drm_device *dev,
  11361. struct drm_atomic_state *state,
  11362. bool async)
  11363. {
  11364. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11365. struct drm_i915_private *dev_priv = dev->dev_private;
  11366. struct drm_crtc_state *old_crtc_state;
  11367. struct drm_crtc *crtc;
  11368. struct intel_crtc_state *intel_cstate;
  11369. int ret = 0, i;
  11370. bool hw_check = intel_state->modeset;
  11371. unsigned long put_domains[I915_MAX_PIPES] = {};
  11372. unsigned crtc_vblank_mask = 0;
  11373. ret = intel_atomic_prepare_commit(dev, state, async);
  11374. if (ret) {
  11375. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11376. return ret;
  11377. }
  11378. drm_atomic_helper_swap_state(dev, state);
  11379. dev_priv->wm.config = intel_state->wm_config;
  11380. intel_shared_dpll_commit(state);
  11381. if (intel_state->modeset) {
  11382. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  11383. sizeof(intel_state->min_pixclk));
  11384. dev_priv->active_crtcs = intel_state->active_crtcs;
  11385. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  11386. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  11387. }
  11388. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11389. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11390. if (needs_modeset(crtc->state) ||
  11391. to_intel_crtc_state(crtc->state)->update_pipe) {
  11392. hw_check = true;
  11393. put_domains[to_intel_crtc(crtc)->pipe] =
  11394. modeset_get_crtc_power_domains(crtc,
  11395. to_intel_crtc_state(crtc->state));
  11396. }
  11397. if (!needs_modeset(crtc->state))
  11398. continue;
  11399. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11400. if (old_crtc_state->active) {
  11401. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  11402. dev_priv->display.crtc_disable(crtc);
  11403. intel_crtc->active = false;
  11404. intel_fbc_disable(intel_crtc);
  11405. intel_disable_shared_dpll(intel_crtc);
  11406. /*
  11407. * Underruns don't always raise
  11408. * interrupts, so check manually.
  11409. */
  11410. intel_check_cpu_fifo_underruns(dev_priv);
  11411. intel_check_pch_fifo_underruns(dev_priv);
  11412. if (!crtc->state->active)
  11413. intel_update_watermarks(crtc);
  11414. }
  11415. }
  11416. /* Only after disabling all output pipelines that will be changed can we
  11417. * update the the output configuration. */
  11418. intel_modeset_update_crtc_state(state);
  11419. if (intel_state->modeset) {
  11420. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11421. if (dev_priv->display.modeset_commit_cdclk &&
  11422. intel_state->dev_cdclk != dev_priv->cdclk_freq)
  11423. dev_priv->display.modeset_commit_cdclk(state);
  11424. intel_modeset_verify_disabled(dev);
  11425. }
  11426. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11427. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11428. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11429. bool modeset = needs_modeset(crtc->state);
  11430. struct intel_crtc_state *pipe_config =
  11431. to_intel_crtc_state(crtc->state);
  11432. bool update_pipe = !modeset && pipe_config->update_pipe;
  11433. if (modeset && crtc->state->active) {
  11434. update_scanline_offset(to_intel_crtc(crtc));
  11435. dev_priv->display.crtc_enable(crtc);
  11436. }
  11437. if (!modeset)
  11438. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11439. if (crtc->state->active &&
  11440. drm_atomic_get_existing_plane_state(state, crtc->primary))
  11441. intel_fbc_enable(intel_crtc);
  11442. if (crtc->state->active &&
  11443. (crtc->state->planes_changed || update_pipe))
  11444. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11445. if (pipe_config->base.active && needs_vblank_wait(pipe_config))
  11446. crtc_vblank_mask |= 1 << i;
  11447. }
  11448. /* FIXME: add subpixel order */
  11449. if (!state->legacy_cursor_update)
  11450. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  11451. /*
  11452. * Now that the vblank has passed, we can go ahead and program the
  11453. * optimal watermarks on platforms that need two-step watermark
  11454. * programming.
  11455. *
  11456. * TODO: Move this (and other cleanup) to an async worker eventually.
  11457. */
  11458. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11459. intel_cstate = to_intel_crtc_state(crtc->state);
  11460. if (dev_priv->display.optimize_watermarks)
  11461. dev_priv->display.optimize_watermarks(intel_cstate);
  11462. }
  11463. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11464. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  11465. if (put_domains[i])
  11466. modeset_put_power_domains(dev_priv, put_domains[i]);
  11467. intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
  11468. }
  11469. if (intel_state->modeset)
  11470. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  11471. mutex_lock(&dev->struct_mutex);
  11472. drm_atomic_helper_cleanup_planes(dev, state);
  11473. mutex_unlock(&dev->struct_mutex);
  11474. drm_atomic_state_free(state);
  11475. /* As one of the primary mmio accessors, KMS has a high likelihood
  11476. * of triggering bugs in unclaimed access. After we finish
  11477. * modesetting, see if an error has been flagged, and if so
  11478. * enable debugging for the next modeset - and hope we catch
  11479. * the culprit.
  11480. *
  11481. * XXX note that we assume display power is on at this point.
  11482. * This might hold true now but we need to add pm helper to check
  11483. * unclaimed only when the hardware is on, as atomic commits
  11484. * can happen also when the device is completely off.
  11485. */
  11486. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  11487. return 0;
  11488. }
  11489. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11490. {
  11491. struct drm_device *dev = crtc->dev;
  11492. struct drm_atomic_state *state;
  11493. struct drm_crtc_state *crtc_state;
  11494. int ret;
  11495. state = drm_atomic_state_alloc(dev);
  11496. if (!state) {
  11497. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11498. crtc->base.id);
  11499. return;
  11500. }
  11501. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11502. retry:
  11503. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11504. ret = PTR_ERR_OR_ZERO(crtc_state);
  11505. if (!ret) {
  11506. if (!crtc_state->active)
  11507. goto out;
  11508. crtc_state->mode_changed = true;
  11509. ret = drm_atomic_commit(state);
  11510. }
  11511. if (ret == -EDEADLK) {
  11512. drm_atomic_state_clear(state);
  11513. drm_modeset_backoff(state->acquire_ctx);
  11514. goto retry;
  11515. }
  11516. if (ret)
  11517. out:
  11518. drm_atomic_state_free(state);
  11519. }
  11520. #undef for_each_intel_crtc_masked
  11521. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11522. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  11523. .set_config = drm_atomic_helper_set_config,
  11524. .set_property = drm_atomic_helper_crtc_set_property,
  11525. .destroy = intel_crtc_destroy,
  11526. .page_flip = intel_crtc_page_flip,
  11527. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11528. .atomic_destroy_state = intel_crtc_destroy_state,
  11529. };
  11530. /**
  11531. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11532. * @plane: drm plane to prepare for
  11533. * @fb: framebuffer to prepare for presentation
  11534. *
  11535. * Prepares a framebuffer for usage on a display plane. Generally this
  11536. * involves pinning the underlying object and updating the frontbuffer tracking
  11537. * bits. Some older platforms need special physical address handling for
  11538. * cursor planes.
  11539. *
  11540. * Must be called with struct_mutex held.
  11541. *
  11542. * Returns 0 on success, negative error code on failure.
  11543. */
  11544. int
  11545. intel_prepare_plane_fb(struct drm_plane *plane,
  11546. const struct drm_plane_state *new_state)
  11547. {
  11548. struct drm_device *dev = plane->dev;
  11549. struct drm_framebuffer *fb = new_state->fb;
  11550. struct intel_plane *intel_plane = to_intel_plane(plane);
  11551. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11552. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11553. int ret = 0;
  11554. if (!obj && !old_obj)
  11555. return 0;
  11556. if (old_obj) {
  11557. struct drm_crtc_state *crtc_state =
  11558. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11559. /* Big Hammer, we also need to ensure that any pending
  11560. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11561. * current scanout is retired before unpinning the old
  11562. * framebuffer. Note that we rely on userspace rendering
  11563. * into the buffer attached to the pipe they are waiting
  11564. * on. If not, userspace generates a GPU hang with IPEHR
  11565. * point to the MI_WAIT_FOR_EVENT.
  11566. *
  11567. * This should only fail upon a hung GPU, in which case we
  11568. * can safely continue.
  11569. */
  11570. if (needs_modeset(crtc_state))
  11571. ret = i915_gem_object_wait_rendering(old_obj, true);
  11572. if (ret) {
  11573. /* GPU hangs should have been swallowed by the wait */
  11574. WARN_ON(ret == -EIO);
  11575. return ret;
  11576. }
  11577. }
  11578. /* For framebuffer backed by dmabuf, wait for fence */
  11579. if (obj && obj->base.dma_buf) {
  11580. long lret;
  11581. lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  11582. false, true,
  11583. MAX_SCHEDULE_TIMEOUT);
  11584. if (lret == -ERESTARTSYS)
  11585. return lret;
  11586. WARN(lret < 0, "waiting returns %li\n", lret);
  11587. }
  11588. if (!obj) {
  11589. ret = 0;
  11590. } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11591. INTEL_INFO(dev)->cursor_needs_physical) {
  11592. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11593. ret = i915_gem_object_attach_phys(obj, align);
  11594. if (ret)
  11595. DRM_DEBUG_KMS("failed to attach phys object\n");
  11596. } else {
  11597. ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  11598. }
  11599. if (ret == 0) {
  11600. if (obj) {
  11601. struct intel_plane_state *plane_state =
  11602. to_intel_plane_state(new_state);
  11603. i915_gem_request_assign(&plane_state->wait_req,
  11604. obj->last_write_req);
  11605. }
  11606. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11607. }
  11608. return ret;
  11609. }
  11610. /**
  11611. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11612. * @plane: drm plane to clean up for
  11613. * @fb: old framebuffer that was on plane
  11614. *
  11615. * Cleans up a framebuffer that has just been removed from a plane.
  11616. *
  11617. * Must be called with struct_mutex held.
  11618. */
  11619. void
  11620. intel_cleanup_plane_fb(struct drm_plane *plane,
  11621. const struct drm_plane_state *old_state)
  11622. {
  11623. struct drm_device *dev = plane->dev;
  11624. struct intel_plane *intel_plane = to_intel_plane(plane);
  11625. struct intel_plane_state *old_intel_state;
  11626. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11627. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11628. old_intel_state = to_intel_plane_state(old_state);
  11629. if (!obj && !old_obj)
  11630. return;
  11631. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11632. !INTEL_INFO(dev)->cursor_needs_physical))
  11633. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  11634. /* prepare_fb aborted? */
  11635. if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
  11636. (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
  11637. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11638. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11639. }
  11640. int
  11641. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11642. {
  11643. int max_scale;
  11644. struct drm_device *dev;
  11645. struct drm_i915_private *dev_priv;
  11646. int crtc_clock, cdclk;
  11647. if (!intel_crtc || !crtc_state->base.enable)
  11648. return DRM_PLANE_HELPER_NO_SCALING;
  11649. dev = intel_crtc->base.dev;
  11650. dev_priv = dev->dev_private;
  11651. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11652. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11653. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11654. return DRM_PLANE_HELPER_NO_SCALING;
  11655. /*
  11656. * skl max scale is lower of:
  11657. * close to 3 but not 3, -1 is for that purpose
  11658. * or
  11659. * cdclk/crtc_clock
  11660. */
  11661. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11662. return max_scale;
  11663. }
  11664. static int
  11665. intel_check_primary_plane(struct drm_plane *plane,
  11666. struct intel_crtc_state *crtc_state,
  11667. struct intel_plane_state *state)
  11668. {
  11669. struct drm_crtc *crtc = state->base.crtc;
  11670. struct drm_framebuffer *fb = state->base.fb;
  11671. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11672. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11673. bool can_position = false;
  11674. if (INTEL_INFO(plane->dev)->gen >= 9) {
  11675. /* use scaler when colorkey is not required */
  11676. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11677. min_scale = 1;
  11678. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11679. }
  11680. can_position = true;
  11681. }
  11682. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11683. &state->dst, &state->clip,
  11684. min_scale, max_scale,
  11685. can_position, true,
  11686. &state->visible);
  11687. }
  11688. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11689. struct drm_crtc_state *old_crtc_state)
  11690. {
  11691. struct drm_device *dev = crtc->dev;
  11692. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11693. struct intel_crtc_state *old_intel_state =
  11694. to_intel_crtc_state(old_crtc_state);
  11695. bool modeset = needs_modeset(crtc->state);
  11696. /* Perform vblank evasion around commit operation */
  11697. intel_pipe_update_start(intel_crtc);
  11698. if (modeset)
  11699. return;
  11700. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  11701. intel_color_set_csc(crtc->state);
  11702. intel_color_load_luts(crtc->state);
  11703. }
  11704. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11705. intel_update_pipe_config(intel_crtc, old_intel_state);
  11706. else if (INTEL_INFO(dev)->gen >= 9)
  11707. skl_detach_scalers(intel_crtc);
  11708. }
  11709. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11710. struct drm_crtc_state *old_crtc_state)
  11711. {
  11712. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11713. intel_pipe_update_end(intel_crtc);
  11714. }
  11715. /**
  11716. * intel_plane_destroy - destroy a plane
  11717. * @plane: plane to destroy
  11718. *
  11719. * Common destruction function for all types of planes (primary, cursor,
  11720. * sprite).
  11721. */
  11722. void intel_plane_destroy(struct drm_plane *plane)
  11723. {
  11724. struct intel_plane *intel_plane = to_intel_plane(plane);
  11725. drm_plane_cleanup(plane);
  11726. kfree(intel_plane);
  11727. }
  11728. const struct drm_plane_funcs intel_plane_funcs = {
  11729. .update_plane = drm_atomic_helper_update_plane,
  11730. .disable_plane = drm_atomic_helper_disable_plane,
  11731. .destroy = intel_plane_destroy,
  11732. .set_property = drm_atomic_helper_plane_set_property,
  11733. .atomic_get_property = intel_plane_atomic_get_property,
  11734. .atomic_set_property = intel_plane_atomic_set_property,
  11735. .atomic_duplicate_state = intel_plane_duplicate_state,
  11736. .atomic_destroy_state = intel_plane_destroy_state,
  11737. };
  11738. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11739. int pipe)
  11740. {
  11741. struct intel_plane *primary = NULL;
  11742. struct intel_plane_state *state = NULL;
  11743. const uint32_t *intel_primary_formats;
  11744. unsigned int num_formats;
  11745. int ret;
  11746. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11747. if (!primary)
  11748. goto fail;
  11749. state = intel_create_plane_state(&primary->base);
  11750. if (!state)
  11751. goto fail;
  11752. primary->base.state = &state->base;
  11753. primary->can_scale = false;
  11754. primary->max_downscale = 1;
  11755. if (INTEL_INFO(dev)->gen >= 9) {
  11756. primary->can_scale = true;
  11757. state->scaler_id = -1;
  11758. }
  11759. primary->pipe = pipe;
  11760. primary->plane = pipe;
  11761. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11762. primary->check_plane = intel_check_primary_plane;
  11763. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11764. primary->plane = !pipe;
  11765. if (INTEL_INFO(dev)->gen >= 9) {
  11766. intel_primary_formats = skl_primary_formats;
  11767. num_formats = ARRAY_SIZE(skl_primary_formats);
  11768. primary->update_plane = skylake_update_primary_plane;
  11769. primary->disable_plane = skylake_disable_primary_plane;
  11770. } else if (HAS_PCH_SPLIT(dev)) {
  11771. intel_primary_formats = i965_primary_formats;
  11772. num_formats = ARRAY_SIZE(i965_primary_formats);
  11773. primary->update_plane = ironlake_update_primary_plane;
  11774. primary->disable_plane = i9xx_disable_primary_plane;
  11775. } else if (INTEL_INFO(dev)->gen >= 4) {
  11776. intel_primary_formats = i965_primary_formats;
  11777. num_formats = ARRAY_SIZE(i965_primary_formats);
  11778. primary->update_plane = i9xx_update_primary_plane;
  11779. primary->disable_plane = i9xx_disable_primary_plane;
  11780. } else {
  11781. intel_primary_formats = i8xx_primary_formats;
  11782. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11783. primary->update_plane = i9xx_update_primary_plane;
  11784. primary->disable_plane = i9xx_disable_primary_plane;
  11785. }
  11786. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11787. &intel_plane_funcs,
  11788. intel_primary_formats, num_formats,
  11789. DRM_PLANE_TYPE_PRIMARY, NULL);
  11790. if (ret)
  11791. goto fail;
  11792. if (INTEL_INFO(dev)->gen >= 4)
  11793. intel_create_rotation_property(dev, primary);
  11794. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11795. return &primary->base;
  11796. fail:
  11797. kfree(state);
  11798. kfree(primary);
  11799. return NULL;
  11800. }
  11801. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11802. {
  11803. if (!dev->mode_config.rotation_property) {
  11804. unsigned long flags = BIT(DRM_ROTATE_0) |
  11805. BIT(DRM_ROTATE_180);
  11806. if (INTEL_INFO(dev)->gen >= 9)
  11807. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11808. dev->mode_config.rotation_property =
  11809. drm_mode_create_rotation_property(dev, flags);
  11810. }
  11811. if (dev->mode_config.rotation_property)
  11812. drm_object_attach_property(&plane->base.base,
  11813. dev->mode_config.rotation_property,
  11814. plane->base.state->rotation);
  11815. }
  11816. static int
  11817. intel_check_cursor_plane(struct drm_plane *plane,
  11818. struct intel_crtc_state *crtc_state,
  11819. struct intel_plane_state *state)
  11820. {
  11821. struct drm_crtc *crtc = crtc_state->base.crtc;
  11822. struct drm_framebuffer *fb = state->base.fb;
  11823. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11824. enum pipe pipe = to_intel_plane(plane)->pipe;
  11825. unsigned stride;
  11826. int ret;
  11827. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11828. &state->dst, &state->clip,
  11829. DRM_PLANE_HELPER_NO_SCALING,
  11830. DRM_PLANE_HELPER_NO_SCALING,
  11831. true, true, &state->visible);
  11832. if (ret)
  11833. return ret;
  11834. /* if we want to turn off the cursor ignore width and height */
  11835. if (!obj)
  11836. return 0;
  11837. /* Check for which cursor types we support */
  11838. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11839. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11840. state->base.crtc_w, state->base.crtc_h);
  11841. return -EINVAL;
  11842. }
  11843. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11844. if (obj->base.size < stride * state->base.crtc_h) {
  11845. DRM_DEBUG_KMS("buffer is too small\n");
  11846. return -ENOMEM;
  11847. }
  11848. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11849. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11850. return -EINVAL;
  11851. }
  11852. /*
  11853. * There's something wrong with the cursor on CHV pipe C.
  11854. * If it straddles the left edge of the screen then
  11855. * moving it away from the edge or disabling it often
  11856. * results in a pipe underrun, and often that can lead to
  11857. * dead pipe (constant underrun reported, and it scans
  11858. * out just a solid color). To recover from that, the
  11859. * display power well must be turned off and on again.
  11860. * Refuse the put the cursor into that compromised position.
  11861. */
  11862. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  11863. state->visible && state->base.crtc_x < 0) {
  11864. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  11865. return -EINVAL;
  11866. }
  11867. return 0;
  11868. }
  11869. static void
  11870. intel_disable_cursor_plane(struct drm_plane *plane,
  11871. struct drm_crtc *crtc)
  11872. {
  11873. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11874. intel_crtc->cursor_addr = 0;
  11875. intel_crtc_update_cursor(crtc, NULL);
  11876. }
  11877. static void
  11878. intel_update_cursor_plane(struct drm_plane *plane,
  11879. const struct intel_crtc_state *crtc_state,
  11880. const struct intel_plane_state *state)
  11881. {
  11882. struct drm_crtc *crtc = crtc_state->base.crtc;
  11883. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11884. struct drm_device *dev = plane->dev;
  11885. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11886. uint32_t addr;
  11887. if (!obj)
  11888. addr = 0;
  11889. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11890. addr = i915_gem_obj_ggtt_offset(obj);
  11891. else
  11892. addr = obj->phys_handle->busaddr;
  11893. intel_crtc->cursor_addr = addr;
  11894. intel_crtc_update_cursor(crtc, state);
  11895. }
  11896. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11897. int pipe)
  11898. {
  11899. struct intel_plane *cursor = NULL;
  11900. struct intel_plane_state *state = NULL;
  11901. int ret;
  11902. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11903. if (!cursor)
  11904. goto fail;
  11905. state = intel_create_plane_state(&cursor->base);
  11906. if (!state)
  11907. goto fail;
  11908. cursor->base.state = &state->base;
  11909. cursor->can_scale = false;
  11910. cursor->max_downscale = 1;
  11911. cursor->pipe = pipe;
  11912. cursor->plane = pipe;
  11913. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11914. cursor->check_plane = intel_check_cursor_plane;
  11915. cursor->update_plane = intel_update_cursor_plane;
  11916. cursor->disable_plane = intel_disable_cursor_plane;
  11917. ret = drm_universal_plane_init(dev, &cursor->base, 0,
  11918. &intel_plane_funcs,
  11919. intel_cursor_formats,
  11920. ARRAY_SIZE(intel_cursor_formats),
  11921. DRM_PLANE_TYPE_CURSOR, NULL);
  11922. if (ret)
  11923. goto fail;
  11924. if (INTEL_INFO(dev)->gen >= 4) {
  11925. if (!dev->mode_config.rotation_property)
  11926. dev->mode_config.rotation_property =
  11927. drm_mode_create_rotation_property(dev,
  11928. BIT(DRM_ROTATE_0) |
  11929. BIT(DRM_ROTATE_180));
  11930. if (dev->mode_config.rotation_property)
  11931. drm_object_attach_property(&cursor->base.base,
  11932. dev->mode_config.rotation_property,
  11933. state->base.rotation);
  11934. }
  11935. if (INTEL_INFO(dev)->gen >=9)
  11936. state->scaler_id = -1;
  11937. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11938. return &cursor->base;
  11939. fail:
  11940. kfree(state);
  11941. kfree(cursor);
  11942. return NULL;
  11943. }
  11944. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11945. struct intel_crtc_state *crtc_state)
  11946. {
  11947. int i;
  11948. struct intel_scaler *intel_scaler;
  11949. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11950. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11951. intel_scaler = &scaler_state->scalers[i];
  11952. intel_scaler->in_use = 0;
  11953. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11954. }
  11955. scaler_state->scaler_id = -1;
  11956. }
  11957. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11958. {
  11959. struct drm_i915_private *dev_priv = dev->dev_private;
  11960. struct intel_crtc *intel_crtc;
  11961. struct intel_crtc_state *crtc_state = NULL;
  11962. struct drm_plane *primary = NULL;
  11963. struct drm_plane *cursor = NULL;
  11964. int ret;
  11965. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11966. if (intel_crtc == NULL)
  11967. return;
  11968. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11969. if (!crtc_state)
  11970. goto fail;
  11971. intel_crtc->config = crtc_state;
  11972. intel_crtc->base.state = &crtc_state->base;
  11973. crtc_state->base.crtc = &intel_crtc->base;
  11974. /* initialize shared scalers */
  11975. if (INTEL_INFO(dev)->gen >= 9) {
  11976. if (pipe == PIPE_C)
  11977. intel_crtc->num_scalers = 1;
  11978. else
  11979. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11980. skl_init_scalers(dev, intel_crtc, crtc_state);
  11981. }
  11982. primary = intel_primary_plane_create(dev, pipe);
  11983. if (!primary)
  11984. goto fail;
  11985. cursor = intel_cursor_plane_create(dev, pipe);
  11986. if (!cursor)
  11987. goto fail;
  11988. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11989. cursor, &intel_crtc_funcs, NULL);
  11990. if (ret)
  11991. goto fail;
  11992. /*
  11993. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11994. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11995. */
  11996. intel_crtc->pipe = pipe;
  11997. intel_crtc->plane = pipe;
  11998. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11999. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  12000. intel_crtc->plane = !pipe;
  12001. }
  12002. intel_crtc->cursor_base = ~0;
  12003. intel_crtc->cursor_cntl = ~0;
  12004. intel_crtc->cursor_size = ~0;
  12005. intel_crtc->wm.cxsr_allowed = true;
  12006. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12007. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12008. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  12009. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  12010. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12011. intel_color_init(&intel_crtc->base);
  12012. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12013. return;
  12014. fail:
  12015. if (primary)
  12016. drm_plane_cleanup(primary);
  12017. if (cursor)
  12018. drm_plane_cleanup(cursor);
  12019. kfree(crtc_state);
  12020. kfree(intel_crtc);
  12021. }
  12022. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12023. {
  12024. struct drm_encoder *encoder = connector->base.encoder;
  12025. struct drm_device *dev = connector->base.dev;
  12026. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12027. if (!encoder || WARN_ON(!encoder->crtc))
  12028. return INVALID_PIPE;
  12029. return to_intel_crtc(encoder->crtc)->pipe;
  12030. }
  12031. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12032. struct drm_file *file)
  12033. {
  12034. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12035. struct drm_crtc *drmmode_crtc;
  12036. struct intel_crtc *crtc;
  12037. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12038. if (!drmmode_crtc) {
  12039. DRM_ERROR("no such CRTC id\n");
  12040. return -ENOENT;
  12041. }
  12042. crtc = to_intel_crtc(drmmode_crtc);
  12043. pipe_from_crtc_id->pipe = crtc->pipe;
  12044. return 0;
  12045. }
  12046. static int intel_encoder_clones(struct intel_encoder *encoder)
  12047. {
  12048. struct drm_device *dev = encoder->base.dev;
  12049. struct intel_encoder *source_encoder;
  12050. int index_mask = 0;
  12051. int entry = 0;
  12052. for_each_intel_encoder(dev, source_encoder) {
  12053. if (encoders_cloneable(encoder, source_encoder))
  12054. index_mask |= (1 << entry);
  12055. entry++;
  12056. }
  12057. return index_mask;
  12058. }
  12059. static bool has_edp_a(struct drm_device *dev)
  12060. {
  12061. struct drm_i915_private *dev_priv = dev->dev_private;
  12062. if (!IS_MOBILE(dev))
  12063. return false;
  12064. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12065. return false;
  12066. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12067. return false;
  12068. return true;
  12069. }
  12070. static bool intel_crt_present(struct drm_device *dev)
  12071. {
  12072. struct drm_i915_private *dev_priv = dev->dev_private;
  12073. if (INTEL_INFO(dev)->gen >= 9)
  12074. return false;
  12075. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  12076. return false;
  12077. if (IS_CHERRYVIEW(dev))
  12078. return false;
  12079. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12080. return false;
  12081. /* DDI E can't be used if DDI A requires 4 lanes */
  12082. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12083. return false;
  12084. if (!dev_priv->vbt.int_crt_support)
  12085. return false;
  12086. return true;
  12087. }
  12088. static void intel_setup_outputs(struct drm_device *dev)
  12089. {
  12090. struct drm_i915_private *dev_priv = dev->dev_private;
  12091. struct intel_encoder *encoder;
  12092. bool dpd_is_edp = false;
  12093. intel_lvds_init(dev);
  12094. if (intel_crt_present(dev))
  12095. intel_crt_init(dev);
  12096. if (IS_BROXTON(dev)) {
  12097. /*
  12098. * FIXME: Broxton doesn't support port detection via the
  12099. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12100. * detect the ports.
  12101. */
  12102. intel_ddi_init(dev, PORT_A);
  12103. intel_ddi_init(dev, PORT_B);
  12104. intel_ddi_init(dev, PORT_C);
  12105. intel_dsi_init(dev);
  12106. } else if (HAS_DDI(dev)) {
  12107. int found;
  12108. /*
  12109. * Haswell uses DDI functions to detect digital outputs.
  12110. * On SKL pre-D0 the strap isn't connected, so we assume
  12111. * it's there.
  12112. */
  12113. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12114. /* WaIgnoreDDIAStrap: skl */
  12115. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12116. intel_ddi_init(dev, PORT_A);
  12117. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12118. * register */
  12119. found = I915_READ(SFUSE_STRAP);
  12120. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12121. intel_ddi_init(dev, PORT_B);
  12122. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12123. intel_ddi_init(dev, PORT_C);
  12124. if (found & SFUSE_STRAP_DDID_DETECTED)
  12125. intel_ddi_init(dev, PORT_D);
  12126. /*
  12127. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12128. */
  12129. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  12130. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12131. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12132. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12133. intel_ddi_init(dev, PORT_E);
  12134. } else if (HAS_PCH_SPLIT(dev)) {
  12135. int found;
  12136. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12137. if (has_edp_a(dev))
  12138. intel_dp_init(dev, DP_A, PORT_A);
  12139. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12140. /* PCH SDVOB multiplex with HDMIB */
  12141. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12142. if (!found)
  12143. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12144. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12145. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12146. }
  12147. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12148. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12149. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12150. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12151. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12152. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12153. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12154. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12155. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12156. /*
  12157. * The DP_DETECTED bit is the latched state of the DDC
  12158. * SDA pin at boot. However since eDP doesn't require DDC
  12159. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12160. * eDP ports may have been muxed to an alternate function.
  12161. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12162. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12163. * detect eDP ports.
  12164. */
  12165. if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
  12166. !intel_dp_is_edp(dev, PORT_B))
  12167. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  12168. if (I915_READ(VLV_DP_B) & DP_DETECTED ||
  12169. intel_dp_is_edp(dev, PORT_B))
  12170. intel_dp_init(dev, VLV_DP_B, PORT_B);
  12171. if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
  12172. !intel_dp_is_edp(dev, PORT_C))
  12173. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  12174. if (I915_READ(VLV_DP_C) & DP_DETECTED ||
  12175. intel_dp_is_edp(dev, PORT_C))
  12176. intel_dp_init(dev, VLV_DP_C, PORT_C);
  12177. if (IS_CHERRYVIEW(dev)) {
  12178. /* eDP not supported on port D, so don't check VBT */
  12179. if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
  12180. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  12181. if (I915_READ(CHV_DP_D) & DP_DETECTED)
  12182. intel_dp_init(dev, CHV_DP_D, PORT_D);
  12183. }
  12184. intel_dsi_init(dev);
  12185. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12186. bool found = false;
  12187. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12188. DRM_DEBUG_KMS("probing SDVOB\n");
  12189. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  12190. if (!found && IS_G4X(dev)) {
  12191. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12192. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12193. }
  12194. if (!found && IS_G4X(dev))
  12195. intel_dp_init(dev, DP_B, PORT_B);
  12196. }
  12197. /* Before G4X SDVOC doesn't have its own detect register */
  12198. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12199. DRM_DEBUG_KMS("probing SDVOC\n");
  12200. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  12201. }
  12202. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12203. if (IS_G4X(dev)) {
  12204. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12205. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12206. }
  12207. if (IS_G4X(dev))
  12208. intel_dp_init(dev, DP_C, PORT_C);
  12209. }
  12210. if (IS_G4X(dev) &&
  12211. (I915_READ(DP_D) & DP_DETECTED))
  12212. intel_dp_init(dev, DP_D, PORT_D);
  12213. } else if (IS_GEN2(dev))
  12214. intel_dvo_init(dev);
  12215. if (SUPPORTS_TV(dev))
  12216. intel_tv_init(dev);
  12217. intel_psr_init(dev);
  12218. for_each_intel_encoder(dev, encoder) {
  12219. encoder->base.possible_crtcs = encoder->crtc_mask;
  12220. encoder->base.possible_clones =
  12221. intel_encoder_clones(encoder);
  12222. }
  12223. intel_init_pch_refclk(dev);
  12224. drm_helper_move_panel_connectors_to_head(dev);
  12225. }
  12226. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12227. {
  12228. struct drm_device *dev = fb->dev;
  12229. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12230. drm_framebuffer_cleanup(fb);
  12231. mutex_lock(&dev->struct_mutex);
  12232. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12233. drm_gem_object_unreference(&intel_fb->obj->base);
  12234. mutex_unlock(&dev->struct_mutex);
  12235. kfree(intel_fb);
  12236. }
  12237. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12238. struct drm_file *file,
  12239. unsigned int *handle)
  12240. {
  12241. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12242. struct drm_i915_gem_object *obj = intel_fb->obj;
  12243. if (obj->userptr.mm) {
  12244. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  12245. return -EINVAL;
  12246. }
  12247. return drm_gem_handle_create(file, &obj->base, handle);
  12248. }
  12249. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12250. struct drm_file *file,
  12251. unsigned flags, unsigned color,
  12252. struct drm_clip_rect *clips,
  12253. unsigned num_clips)
  12254. {
  12255. struct drm_device *dev = fb->dev;
  12256. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12257. struct drm_i915_gem_object *obj = intel_fb->obj;
  12258. mutex_lock(&dev->struct_mutex);
  12259. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  12260. mutex_unlock(&dev->struct_mutex);
  12261. return 0;
  12262. }
  12263. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12264. .destroy = intel_user_framebuffer_destroy,
  12265. .create_handle = intel_user_framebuffer_create_handle,
  12266. .dirty = intel_user_framebuffer_dirty,
  12267. };
  12268. static
  12269. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12270. uint32_t pixel_format)
  12271. {
  12272. u32 gen = INTEL_INFO(dev)->gen;
  12273. if (gen >= 9) {
  12274. int cpp = drm_format_plane_cpp(pixel_format, 0);
  12275. /* "The stride in bytes must not exceed the of the size of 8K
  12276. * pixels and 32K bytes."
  12277. */
  12278. return min(8192 * cpp, 32768);
  12279. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12280. return 32*1024;
  12281. } else if (gen >= 4) {
  12282. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12283. return 16*1024;
  12284. else
  12285. return 32*1024;
  12286. } else if (gen >= 3) {
  12287. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12288. return 8*1024;
  12289. else
  12290. return 16*1024;
  12291. } else {
  12292. /* XXX DSPC is limited to 4k tiled */
  12293. return 8*1024;
  12294. }
  12295. }
  12296. static int intel_framebuffer_init(struct drm_device *dev,
  12297. struct intel_framebuffer *intel_fb,
  12298. struct drm_mode_fb_cmd2 *mode_cmd,
  12299. struct drm_i915_gem_object *obj)
  12300. {
  12301. struct drm_i915_private *dev_priv = to_i915(dev);
  12302. unsigned int aligned_height;
  12303. int ret;
  12304. u32 pitch_limit, stride_alignment;
  12305. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12306. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12307. /* Enforce that fb modifier and tiling mode match, but only for
  12308. * X-tiled. This is needed for FBC. */
  12309. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12310. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12311. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12312. return -EINVAL;
  12313. }
  12314. } else {
  12315. if (obj->tiling_mode == I915_TILING_X)
  12316. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12317. else if (obj->tiling_mode == I915_TILING_Y) {
  12318. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12319. return -EINVAL;
  12320. }
  12321. }
  12322. /* Passed in modifier sanity checking. */
  12323. switch (mode_cmd->modifier[0]) {
  12324. case I915_FORMAT_MOD_Y_TILED:
  12325. case I915_FORMAT_MOD_Yf_TILED:
  12326. if (INTEL_INFO(dev)->gen < 9) {
  12327. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12328. mode_cmd->modifier[0]);
  12329. return -EINVAL;
  12330. }
  12331. case DRM_FORMAT_MOD_NONE:
  12332. case I915_FORMAT_MOD_X_TILED:
  12333. break;
  12334. default:
  12335. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12336. mode_cmd->modifier[0]);
  12337. return -EINVAL;
  12338. }
  12339. stride_alignment = intel_fb_stride_alignment(dev_priv,
  12340. mode_cmd->modifier[0],
  12341. mode_cmd->pixel_format);
  12342. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12343. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12344. mode_cmd->pitches[0], stride_alignment);
  12345. return -EINVAL;
  12346. }
  12347. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12348. mode_cmd->pixel_format);
  12349. if (mode_cmd->pitches[0] > pitch_limit) {
  12350. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12351. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12352. "tiled" : "linear",
  12353. mode_cmd->pitches[0], pitch_limit);
  12354. return -EINVAL;
  12355. }
  12356. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12357. mode_cmd->pitches[0] != obj->stride) {
  12358. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12359. mode_cmd->pitches[0], obj->stride);
  12360. return -EINVAL;
  12361. }
  12362. /* Reject formats not supported by any plane early. */
  12363. switch (mode_cmd->pixel_format) {
  12364. case DRM_FORMAT_C8:
  12365. case DRM_FORMAT_RGB565:
  12366. case DRM_FORMAT_XRGB8888:
  12367. case DRM_FORMAT_ARGB8888:
  12368. break;
  12369. case DRM_FORMAT_XRGB1555:
  12370. if (INTEL_INFO(dev)->gen > 3) {
  12371. DRM_DEBUG("unsupported pixel format: %s\n",
  12372. drm_get_format_name(mode_cmd->pixel_format));
  12373. return -EINVAL;
  12374. }
  12375. break;
  12376. case DRM_FORMAT_ABGR8888:
  12377. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  12378. INTEL_INFO(dev)->gen < 9) {
  12379. DRM_DEBUG("unsupported pixel format: %s\n",
  12380. drm_get_format_name(mode_cmd->pixel_format));
  12381. return -EINVAL;
  12382. }
  12383. break;
  12384. case DRM_FORMAT_XBGR8888:
  12385. case DRM_FORMAT_XRGB2101010:
  12386. case DRM_FORMAT_XBGR2101010:
  12387. if (INTEL_INFO(dev)->gen < 4) {
  12388. DRM_DEBUG("unsupported pixel format: %s\n",
  12389. drm_get_format_name(mode_cmd->pixel_format));
  12390. return -EINVAL;
  12391. }
  12392. break;
  12393. case DRM_FORMAT_ABGR2101010:
  12394. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12395. DRM_DEBUG("unsupported pixel format: %s\n",
  12396. drm_get_format_name(mode_cmd->pixel_format));
  12397. return -EINVAL;
  12398. }
  12399. break;
  12400. case DRM_FORMAT_YUYV:
  12401. case DRM_FORMAT_UYVY:
  12402. case DRM_FORMAT_YVYU:
  12403. case DRM_FORMAT_VYUY:
  12404. if (INTEL_INFO(dev)->gen < 5) {
  12405. DRM_DEBUG("unsupported pixel format: %s\n",
  12406. drm_get_format_name(mode_cmd->pixel_format));
  12407. return -EINVAL;
  12408. }
  12409. break;
  12410. default:
  12411. DRM_DEBUG("unsupported pixel format: %s\n",
  12412. drm_get_format_name(mode_cmd->pixel_format));
  12413. return -EINVAL;
  12414. }
  12415. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12416. if (mode_cmd->offsets[0] != 0)
  12417. return -EINVAL;
  12418. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12419. mode_cmd->pixel_format,
  12420. mode_cmd->modifier[0]);
  12421. /* FIXME drm helper for size checks (especially planar formats)? */
  12422. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12423. return -EINVAL;
  12424. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12425. intel_fb->obj = obj;
  12426. intel_fill_fb_info(dev_priv, &intel_fb->base);
  12427. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12428. if (ret) {
  12429. DRM_ERROR("framebuffer init failed %d\n", ret);
  12430. return ret;
  12431. }
  12432. intel_fb->obj->framebuffer_references++;
  12433. return 0;
  12434. }
  12435. static struct drm_framebuffer *
  12436. intel_user_framebuffer_create(struct drm_device *dev,
  12437. struct drm_file *filp,
  12438. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12439. {
  12440. struct drm_framebuffer *fb;
  12441. struct drm_i915_gem_object *obj;
  12442. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12443. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12444. mode_cmd.handles[0]));
  12445. if (&obj->base == NULL)
  12446. return ERR_PTR(-ENOENT);
  12447. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  12448. if (IS_ERR(fb))
  12449. drm_gem_object_unreference_unlocked(&obj->base);
  12450. return fb;
  12451. }
  12452. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12453. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12454. {
  12455. }
  12456. #endif
  12457. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12458. .fb_create = intel_user_framebuffer_create,
  12459. .output_poll_changed = intel_fbdev_output_poll_changed,
  12460. .atomic_check = intel_atomic_check,
  12461. .atomic_commit = intel_atomic_commit,
  12462. .atomic_state_alloc = intel_atomic_state_alloc,
  12463. .atomic_state_clear = intel_atomic_state_clear,
  12464. };
  12465. /**
  12466. * intel_init_display_hooks - initialize the display modesetting hooks
  12467. * @dev_priv: device private
  12468. */
  12469. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12470. {
  12471. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12472. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12473. dev_priv->display.get_initial_plane_config =
  12474. skylake_get_initial_plane_config;
  12475. dev_priv->display.crtc_compute_clock =
  12476. haswell_crtc_compute_clock;
  12477. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12478. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12479. } else if (HAS_DDI(dev_priv)) {
  12480. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12481. dev_priv->display.get_initial_plane_config =
  12482. ironlake_get_initial_plane_config;
  12483. dev_priv->display.crtc_compute_clock =
  12484. haswell_crtc_compute_clock;
  12485. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12486. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12487. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12488. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12489. dev_priv->display.get_initial_plane_config =
  12490. ironlake_get_initial_plane_config;
  12491. dev_priv->display.crtc_compute_clock =
  12492. ironlake_crtc_compute_clock;
  12493. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12494. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12495. } else if (IS_CHERRYVIEW(dev_priv)) {
  12496. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12497. dev_priv->display.get_initial_plane_config =
  12498. i9xx_get_initial_plane_config;
  12499. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12500. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12501. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12502. } else if (IS_VALLEYVIEW(dev_priv)) {
  12503. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12504. dev_priv->display.get_initial_plane_config =
  12505. i9xx_get_initial_plane_config;
  12506. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12507. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12508. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12509. } else if (IS_G4X(dev_priv)) {
  12510. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12511. dev_priv->display.get_initial_plane_config =
  12512. i9xx_get_initial_plane_config;
  12513. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12514. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12515. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12516. } else if (IS_PINEVIEW(dev_priv)) {
  12517. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12518. dev_priv->display.get_initial_plane_config =
  12519. i9xx_get_initial_plane_config;
  12520. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12521. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12522. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12523. } else if (!IS_GEN2(dev_priv)) {
  12524. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12525. dev_priv->display.get_initial_plane_config =
  12526. i9xx_get_initial_plane_config;
  12527. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12528. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12529. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12530. } else {
  12531. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12532. dev_priv->display.get_initial_plane_config =
  12533. i9xx_get_initial_plane_config;
  12534. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12535. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12536. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12537. }
  12538. /* Returns the core display clock speed */
  12539. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12540. dev_priv->display.get_display_clock_speed =
  12541. skylake_get_display_clock_speed;
  12542. else if (IS_BROXTON(dev_priv))
  12543. dev_priv->display.get_display_clock_speed =
  12544. broxton_get_display_clock_speed;
  12545. else if (IS_BROADWELL(dev_priv))
  12546. dev_priv->display.get_display_clock_speed =
  12547. broadwell_get_display_clock_speed;
  12548. else if (IS_HASWELL(dev_priv))
  12549. dev_priv->display.get_display_clock_speed =
  12550. haswell_get_display_clock_speed;
  12551. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12552. dev_priv->display.get_display_clock_speed =
  12553. valleyview_get_display_clock_speed;
  12554. else if (IS_GEN5(dev_priv))
  12555. dev_priv->display.get_display_clock_speed =
  12556. ilk_get_display_clock_speed;
  12557. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  12558. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  12559. dev_priv->display.get_display_clock_speed =
  12560. i945_get_display_clock_speed;
  12561. else if (IS_GM45(dev_priv))
  12562. dev_priv->display.get_display_clock_speed =
  12563. gm45_get_display_clock_speed;
  12564. else if (IS_CRESTLINE(dev_priv))
  12565. dev_priv->display.get_display_clock_speed =
  12566. i965gm_get_display_clock_speed;
  12567. else if (IS_PINEVIEW(dev_priv))
  12568. dev_priv->display.get_display_clock_speed =
  12569. pnv_get_display_clock_speed;
  12570. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  12571. dev_priv->display.get_display_clock_speed =
  12572. g33_get_display_clock_speed;
  12573. else if (IS_I915G(dev_priv))
  12574. dev_priv->display.get_display_clock_speed =
  12575. i915_get_display_clock_speed;
  12576. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  12577. dev_priv->display.get_display_clock_speed =
  12578. i9xx_misc_get_display_clock_speed;
  12579. else if (IS_I915GM(dev_priv))
  12580. dev_priv->display.get_display_clock_speed =
  12581. i915gm_get_display_clock_speed;
  12582. else if (IS_I865G(dev_priv))
  12583. dev_priv->display.get_display_clock_speed =
  12584. i865_get_display_clock_speed;
  12585. else if (IS_I85X(dev_priv))
  12586. dev_priv->display.get_display_clock_speed =
  12587. i85x_get_display_clock_speed;
  12588. else { /* 830 */
  12589. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12590. dev_priv->display.get_display_clock_speed =
  12591. i830_get_display_clock_speed;
  12592. }
  12593. if (IS_GEN5(dev_priv)) {
  12594. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12595. } else if (IS_GEN6(dev_priv)) {
  12596. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12597. } else if (IS_IVYBRIDGE(dev_priv)) {
  12598. /* FIXME: detect B0+ stepping and use auto training */
  12599. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12600. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12601. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12602. if (IS_BROADWELL(dev_priv)) {
  12603. dev_priv->display.modeset_commit_cdclk =
  12604. broadwell_modeset_commit_cdclk;
  12605. dev_priv->display.modeset_calc_cdclk =
  12606. broadwell_modeset_calc_cdclk;
  12607. }
  12608. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12609. dev_priv->display.modeset_commit_cdclk =
  12610. valleyview_modeset_commit_cdclk;
  12611. dev_priv->display.modeset_calc_cdclk =
  12612. valleyview_modeset_calc_cdclk;
  12613. } else if (IS_BROXTON(dev_priv)) {
  12614. dev_priv->display.modeset_commit_cdclk =
  12615. broxton_modeset_commit_cdclk;
  12616. dev_priv->display.modeset_calc_cdclk =
  12617. broxton_modeset_calc_cdclk;
  12618. }
  12619. switch (INTEL_INFO(dev_priv)->gen) {
  12620. case 2:
  12621. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12622. break;
  12623. case 3:
  12624. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12625. break;
  12626. case 4:
  12627. case 5:
  12628. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12629. break;
  12630. case 6:
  12631. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12632. break;
  12633. case 7:
  12634. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12635. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12636. break;
  12637. case 9:
  12638. /* Drop through - unsupported since execlist only. */
  12639. default:
  12640. /* Default just returns -ENODEV to indicate unsupported */
  12641. dev_priv->display.queue_flip = intel_default_queue_flip;
  12642. }
  12643. }
  12644. /*
  12645. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12646. * resume, or other times. This quirk makes sure that's the case for
  12647. * affected systems.
  12648. */
  12649. static void quirk_pipea_force(struct drm_device *dev)
  12650. {
  12651. struct drm_i915_private *dev_priv = dev->dev_private;
  12652. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12653. DRM_INFO("applying pipe a force quirk\n");
  12654. }
  12655. static void quirk_pipeb_force(struct drm_device *dev)
  12656. {
  12657. struct drm_i915_private *dev_priv = dev->dev_private;
  12658. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12659. DRM_INFO("applying pipe b force quirk\n");
  12660. }
  12661. /*
  12662. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12663. */
  12664. static void quirk_ssc_force_disable(struct drm_device *dev)
  12665. {
  12666. struct drm_i915_private *dev_priv = dev->dev_private;
  12667. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12668. DRM_INFO("applying lvds SSC disable quirk\n");
  12669. }
  12670. /*
  12671. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12672. * brightness value
  12673. */
  12674. static void quirk_invert_brightness(struct drm_device *dev)
  12675. {
  12676. struct drm_i915_private *dev_priv = dev->dev_private;
  12677. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12678. DRM_INFO("applying inverted panel brightness quirk\n");
  12679. }
  12680. /* Some VBT's incorrectly indicate no backlight is present */
  12681. static void quirk_backlight_present(struct drm_device *dev)
  12682. {
  12683. struct drm_i915_private *dev_priv = dev->dev_private;
  12684. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12685. DRM_INFO("applying backlight present quirk\n");
  12686. }
  12687. struct intel_quirk {
  12688. int device;
  12689. int subsystem_vendor;
  12690. int subsystem_device;
  12691. void (*hook)(struct drm_device *dev);
  12692. };
  12693. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12694. struct intel_dmi_quirk {
  12695. void (*hook)(struct drm_device *dev);
  12696. const struct dmi_system_id (*dmi_id_list)[];
  12697. };
  12698. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12699. {
  12700. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12701. return 1;
  12702. }
  12703. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12704. {
  12705. .dmi_id_list = &(const struct dmi_system_id[]) {
  12706. {
  12707. .callback = intel_dmi_reverse_brightness,
  12708. .ident = "NCR Corporation",
  12709. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12710. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12711. },
  12712. },
  12713. { } /* terminating entry */
  12714. },
  12715. .hook = quirk_invert_brightness,
  12716. },
  12717. };
  12718. static struct intel_quirk intel_quirks[] = {
  12719. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12720. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12721. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12722. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12723. /* 830 needs to leave pipe A & dpll A up */
  12724. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12725. /* 830 needs to leave pipe B & dpll B up */
  12726. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12727. /* Lenovo U160 cannot use SSC on LVDS */
  12728. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12729. /* Sony Vaio Y cannot use SSC on LVDS */
  12730. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12731. /* Acer Aspire 5734Z must invert backlight brightness */
  12732. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12733. /* Acer/eMachines G725 */
  12734. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12735. /* Acer/eMachines e725 */
  12736. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12737. /* Acer/Packard Bell NCL20 */
  12738. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12739. /* Acer Aspire 4736Z */
  12740. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12741. /* Acer Aspire 5336 */
  12742. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12743. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12744. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12745. /* Acer C720 Chromebook (Core i3 4005U) */
  12746. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12747. /* Apple Macbook 2,1 (Core 2 T7400) */
  12748. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12749. /* Apple Macbook 4,1 */
  12750. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12751. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12752. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12753. /* HP Chromebook 14 (Celeron 2955U) */
  12754. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12755. /* Dell Chromebook 11 */
  12756. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12757. /* Dell Chromebook 11 (2015 version) */
  12758. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12759. };
  12760. static void intel_init_quirks(struct drm_device *dev)
  12761. {
  12762. struct pci_dev *d = dev->pdev;
  12763. int i;
  12764. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12765. struct intel_quirk *q = &intel_quirks[i];
  12766. if (d->device == q->device &&
  12767. (d->subsystem_vendor == q->subsystem_vendor ||
  12768. q->subsystem_vendor == PCI_ANY_ID) &&
  12769. (d->subsystem_device == q->subsystem_device ||
  12770. q->subsystem_device == PCI_ANY_ID))
  12771. q->hook(dev);
  12772. }
  12773. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12774. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12775. intel_dmi_quirks[i].hook(dev);
  12776. }
  12777. }
  12778. /* Disable the VGA plane that we never use */
  12779. static void i915_disable_vga(struct drm_device *dev)
  12780. {
  12781. struct drm_i915_private *dev_priv = dev->dev_private;
  12782. u8 sr1;
  12783. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  12784. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12785. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12786. outb(SR01, VGA_SR_INDEX);
  12787. sr1 = inb(VGA_SR_DATA);
  12788. outb(sr1 | 1<<5, VGA_SR_DATA);
  12789. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12790. udelay(300);
  12791. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12792. POSTING_READ(vga_reg);
  12793. }
  12794. void intel_modeset_init_hw(struct drm_device *dev)
  12795. {
  12796. struct drm_i915_private *dev_priv = dev->dev_private;
  12797. intel_update_cdclk(dev);
  12798. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  12799. intel_init_clock_gating(dev);
  12800. intel_enable_gt_powersave(dev);
  12801. }
  12802. /*
  12803. * Calculate what we think the watermarks should be for the state we've read
  12804. * out of the hardware and then immediately program those watermarks so that
  12805. * we ensure the hardware settings match our internal state.
  12806. *
  12807. * We can calculate what we think WM's should be by creating a duplicate of the
  12808. * current state (which was constructed during hardware readout) and running it
  12809. * through the atomic check code to calculate new watermark values in the
  12810. * state object.
  12811. */
  12812. static void sanitize_watermarks(struct drm_device *dev)
  12813. {
  12814. struct drm_i915_private *dev_priv = to_i915(dev);
  12815. struct drm_atomic_state *state;
  12816. struct drm_crtc *crtc;
  12817. struct drm_crtc_state *cstate;
  12818. struct drm_modeset_acquire_ctx ctx;
  12819. int ret;
  12820. int i;
  12821. /* Only supported on platforms that use atomic watermark design */
  12822. if (!dev_priv->display.optimize_watermarks)
  12823. return;
  12824. /*
  12825. * We need to hold connection_mutex before calling duplicate_state so
  12826. * that the connector loop is protected.
  12827. */
  12828. drm_modeset_acquire_init(&ctx, 0);
  12829. retry:
  12830. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12831. if (ret == -EDEADLK) {
  12832. drm_modeset_backoff(&ctx);
  12833. goto retry;
  12834. } else if (WARN_ON(ret)) {
  12835. goto fail;
  12836. }
  12837. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12838. if (WARN_ON(IS_ERR(state)))
  12839. goto fail;
  12840. /*
  12841. * Hardware readout is the only time we don't want to calculate
  12842. * intermediate watermarks (since we don't trust the current
  12843. * watermarks).
  12844. */
  12845. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  12846. ret = intel_atomic_check(dev, state);
  12847. if (ret) {
  12848. /*
  12849. * If we fail here, it means that the hardware appears to be
  12850. * programmed in a way that shouldn't be possible, given our
  12851. * understanding of watermark requirements. This might mean a
  12852. * mistake in the hardware readout code or a mistake in the
  12853. * watermark calculations for a given platform. Raise a WARN
  12854. * so that this is noticeable.
  12855. *
  12856. * If this actually happens, we'll have to just leave the
  12857. * BIOS-programmed watermarks untouched and hope for the best.
  12858. */
  12859. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12860. goto fail;
  12861. }
  12862. /* Write calculated watermark values back */
  12863. to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
  12864. for_each_crtc_in_state(state, crtc, cstate, i) {
  12865. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12866. cs->wm.need_postvbl_update = true;
  12867. dev_priv->display.optimize_watermarks(cs);
  12868. }
  12869. drm_atomic_state_free(state);
  12870. fail:
  12871. drm_modeset_drop_locks(&ctx);
  12872. drm_modeset_acquire_fini(&ctx);
  12873. }
  12874. void intel_modeset_init(struct drm_device *dev)
  12875. {
  12876. struct drm_i915_private *dev_priv = to_i915(dev);
  12877. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12878. int sprite, ret;
  12879. enum pipe pipe;
  12880. struct intel_crtc *crtc;
  12881. drm_mode_config_init(dev);
  12882. dev->mode_config.min_width = 0;
  12883. dev->mode_config.min_height = 0;
  12884. dev->mode_config.preferred_depth = 24;
  12885. dev->mode_config.prefer_shadow = 1;
  12886. dev->mode_config.allow_fb_modifiers = true;
  12887. dev->mode_config.funcs = &intel_mode_funcs;
  12888. intel_init_quirks(dev);
  12889. intel_init_pm(dev);
  12890. if (INTEL_INFO(dev)->num_pipes == 0)
  12891. return;
  12892. /*
  12893. * There may be no VBT; and if the BIOS enabled SSC we can
  12894. * just keep using it to avoid unnecessary flicker. Whereas if the
  12895. * BIOS isn't using it, don't assume it will work even if the VBT
  12896. * indicates as much.
  12897. */
  12898. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12899. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12900. DREF_SSC1_ENABLE);
  12901. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12902. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12903. bios_lvds_use_ssc ? "en" : "dis",
  12904. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12905. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12906. }
  12907. }
  12908. if (IS_GEN2(dev)) {
  12909. dev->mode_config.max_width = 2048;
  12910. dev->mode_config.max_height = 2048;
  12911. } else if (IS_GEN3(dev)) {
  12912. dev->mode_config.max_width = 4096;
  12913. dev->mode_config.max_height = 4096;
  12914. } else {
  12915. dev->mode_config.max_width = 8192;
  12916. dev->mode_config.max_height = 8192;
  12917. }
  12918. if (IS_845G(dev) || IS_I865G(dev)) {
  12919. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12920. dev->mode_config.cursor_height = 1023;
  12921. } else if (IS_GEN2(dev)) {
  12922. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12923. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12924. } else {
  12925. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12926. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12927. }
  12928. dev->mode_config.fb_base = ggtt->mappable_base;
  12929. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12930. INTEL_INFO(dev)->num_pipes,
  12931. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12932. for_each_pipe(dev_priv, pipe) {
  12933. intel_crtc_init(dev, pipe);
  12934. for_each_sprite(dev_priv, pipe, sprite) {
  12935. ret = intel_plane_init(dev, pipe, sprite);
  12936. if (ret)
  12937. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12938. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12939. }
  12940. }
  12941. intel_update_czclk(dev_priv);
  12942. intel_update_rawclk(dev_priv);
  12943. intel_update_cdclk(dev);
  12944. intel_shared_dpll_init(dev);
  12945. /* Just disable it once at startup */
  12946. i915_disable_vga(dev);
  12947. intel_setup_outputs(dev);
  12948. drm_modeset_lock_all(dev);
  12949. intel_modeset_setup_hw_state(dev);
  12950. drm_modeset_unlock_all(dev);
  12951. for_each_intel_crtc(dev, crtc) {
  12952. struct intel_initial_plane_config plane_config = {};
  12953. if (!crtc->active)
  12954. continue;
  12955. /*
  12956. * Note that reserving the BIOS fb up front prevents us
  12957. * from stuffing other stolen allocations like the ring
  12958. * on top. This prevents some ugliness at boot time, and
  12959. * can even allow for smooth boot transitions if the BIOS
  12960. * fb is large enough for the active pipe configuration.
  12961. */
  12962. dev_priv->display.get_initial_plane_config(crtc,
  12963. &plane_config);
  12964. /*
  12965. * If the fb is shared between multiple heads, we'll
  12966. * just get the first one.
  12967. */
  12968. intel_find_initial_plane_obj(crtc, &plane_config);
  12969. }
  12970. /*
  12971. * Make sure hardware watermarks really match the state we read out.
  12972. * Note that we need to do this after reconstructing the BIOS fb's
  12973. * since the watermark calculation done here will use pstate->fb.
  12974. */
  12975. sanitize_watermarks(dev);
  12976. }
  12977. static void intel_enable_pipe_a(struct drm_device *dev)
  12978. {
  12979. struct intel_connector *connector;
  12980. struct drm_connector *crt = NULL;
  12981. struct intel_load_detect_pipe load_detect_temp;
  12982. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12983. /* We can't just switch on the pipe A, we need to set things up with a
  12984. * proper mode and output configuration. As a gross hack, enable pipe A
  12985. * by enabling the load detect pipe once. */
  12986. for_each_intel_connector(dev, connector) {
  12987. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12988. crt = &connector->base;
  12989. break;
  12990. }
  12991. }
  12992. if (!crt)
  12993. return;
  12994. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12995. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12996. }
  12997. static bool
  12998. intel_check_plane_mapping(struct intel_crtc *crtc)
  12999. {
  13000. struct drm_device *dev = crtc->base.dev;
  13001. struct drm_i915_private *dev_priv = dev->dev_private;
  13002. u32 val;
  13003. if (INTEL_INFO(dev)->num_pipes == 1)
  13004. return true;
  13005. val = I915_READ(DSPCNTR(!crtc->plane));
  13006. if ((val & DISPLAY_PLANE_ENABLE) &&
  13007. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13008. return false;
  13009. return true;
  13010. }
  13011. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13012. {
  13013. struct drm_device *dev = crtc->base.dev;
  13014. struct intel_encoder *encoder;
  13015. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13016. return true;
  13017. return false;
  13018. }
  13019. static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
  13020. {
  13021. struct drm_device *dev = encoder->base.dev;
  13022. struct intel_connector *connector;
  13023. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13024. return true;
  13025. return false;
  13026. }
  13027. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13028. {
  13029. struct drm_device *dev = crtc->base.dev;
  13030. struct drm_i915_private *dev_priv = dev->dev_private;
  13031. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13032. /* Clear any frame start delays used for debugging left by the BIOS */
  13033. if (!transcoder_is_dsi(cpu_transcoder)) {
  13034. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13035. I915_WRITE(reg,
  13036. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13037. }
  13038. /* restore vblank interrupts to correct state */
  13039. drm_crtc_vblank_reset(&crtc->base);
  13040. if (crtc->active) {
  13041. struct intel_plane *plane;
  13042. drm_crtc_vblank_on(&crtc->base);
  13043. /* Disable everything but the primary plane */
  13044. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13045. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13046. continue;
  13047. plane->disable_plane(&plane->base, &crtc->base);
  13048. }
  13049. }
  13050. /* We need to sanitize the plane -> pipe mapping first because this will
  13051. * disable the crtc (and hence change the state) if it is wrong. Note
  13052. * that gen4+ has a fixed plane -> pipe mapping. */
  13053. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  13054. bool plane;
  13055. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  13056. crtc->base.base.id);
  13057. /* Pipe has the wrong plane attached and the plane is active.
  13058. * Temporarily change the plane mapping and disable everything
  13059. * ... */
  13060. plane = crtc->plane;
  13061. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  13062. crtc->plane = !plane;
  13063. intel_crtc_disable_noatomic(&crtc->base);
  13064. crtc->plane = plane;
  13065. }
  13066. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13067. crtc->pipe == PIPE_A && !crtc->active) {
  13068. /* BIOS forgot to enable pipe A, this mostly happens after
  13069. * resume. Force-enable the pipe to fix this, the update_dpms
  13070. * call below we restore the pipe to the right state, but leave
  13071. * the required bits on. */
  13072. intel_enable_pipe_a(dev);
  13073. }
  13074. /* Adjust the state of the output pipe according to whether we
  13075. * have active connectors/encoders. */
  13076. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13077. intel_crtc_disable_noatomic(&crtc->base);
  13078. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  13079. /*
  13080. * We start out with underrun reporting disabled to avoid races.
  13081. * For correct bookkeeping mark this on active crtcs.
  13082. *
  13083. * Also on gmch platforms we dont have any hardware bits to
  13084. * disable the underrun reporting. Which means we need to start
  13085. * out with underrun reporting disabled also on inactive pipes,
  13086. * since otherwise we'll complain about the garbage we read when
  13087. * e.g. coming up after runtime pm.
  13088. *
  13089. * No protection against concurrent access is required - at
  13090. * worst a fifo underrun happens which also sets this to false.
  13091. */
  13092. crtc->cpu_fifo_underrun_disabled = true;
  13093. crtc->pch_fifo_underrun_disabled = true;
  13094. }
  13095. }
  13096. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13097. {
  13098. struct intel_connector *connector;
  13099. struct drm_device *dev = encoder->base.dev;
  13100. /* We need to check both for a crtc link (meaning that the
  13101. * encoder is active and trying to read from a pipe) and the
  13102. * pipe itself being active. */
  13103. bool has_active_crtc = encoder->base.crtc &&
  13104. to_intel_crtc(encoder->base.crtc)->active;
  13105. if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
  13106. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13107. encoder->base.base.id,
  13108. encoder->base.name);
  13109. /* Connector is active, but has no active pipe. This is
  13110. * fallout from our resume register restoring. Disable
  13111. * the encoder manually again. */
  13112. if (encoder->base.crtc) {
  13113. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13114. encoder->base.base.id,
  13115. encoder->base.name);
  13116. encoder->disable(encoder);
  13117. if (encoder->post_disable)
  13118. encoder->post_disable(encoder);
  13119. }
  13120. encoder->base.crtc = NULL;
  13121. /* Inconsistent output/port/pipe state happens presumably due to
  13122. * a bug in one of the get_hw_state functions. Or someplace else
  13123. * in our code, like the register restore mess on resume. Clamp
  13124. * things to off as a safer default. */
  13125. for_each_intel_connector(dev, connector) {
  13126. if (connector->encoder != encoder)
  13127. continue;
  13128. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13129. connector->base.encoder = NULL;
  13130. }
  13131. }
  13132. /* Enabled encoders without active connectors will be fixed in
  13133. * the crtc fixup. */
  13134. }
  13135. void i915_redisable_vga_power_on(struct drm_device *dev)
  13136. {
  13137. struct drm_i915_private *dev_priv = dev->dev_private;
  13138. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13139. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13140. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13141. i915_disable_vga(dev);
  13142. }
  13143. }
  13144. void i915_redisable_vga(struct drm_device *dev)
  13145. {
  13146. struct drm_i915_private *dev_priv = dev->dev_private;
  13147. /* This function can be called both from intel_modeset_setup_hw_state or
  13148. * at a very early point in our resume sequence, where the power well
  13149. * structures are not yet restored. Since this function is at a very
  13150. * paranoid "someone might have enabled VGA while we were not looking"
  13151. * level, just check if the power well is enabled instead of trying to
  13152. * follow the "don't touch the power well if we don't need it" policy
  13153. * the rest of the driver uses. */
  13154. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  13155. return;
  13156. i915_redisable_vga_power_on(dev);
  13157. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  13158. }
  13159. static bool primary_get_hw_state(struct intel_plane *plane)
  13160. {
  13161. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  13162. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  13163. }
  13164. /* FIXME read out full plane state for all planes */
  13165. static void readout_plane_state(struct intel_crtc *crtc)
  13166. {
  13167. struct drm_plane *primary = crtc->base.primary;
  13168. struct intel_plane_state *plane_state =
  13169. to_intel_plane_state(primary->state);
  13170. plane_state->visible = crtc->active &&
  13171. primary_get_hw_state(to_intel_plane(primary));
  13172. if (plane_state->visible)
  13173. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  13174. }
  13175. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13176. {
  13177. struct drm_i915_private *dev_priv = dev->dev_private;
  13178. enum pipe pipe;
  13179. struct intel_crtc *crtc;
  13180. struct intel_encoder *encoder;
  13181. struct intel_connector *connector;
  13182. int i;
  13183. dev_priv->active_crtcs = 0;
  13184. for_each_intel_crtc(dev, crtc) {
  13185. struct intel_crtc_state *crtc_state = crtc->config;
  13186. int pixclk = 0;
  13187. __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
  13188. memset(crtc_state, 0, sizeof(*crtc_state));
  13189. crtc_state->base.crtc = &crtc->base;
  13190. crtc_state->base.active = crtc_state->base.enable =
  13191. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13192. crtc->base.enabled = crtc_state->base.enable;
  13193. crtc->active = crtc_state->base.active;
  13194. if (crtc_state->base.active) {
  13195. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13196. if (IS_BROADWELL(dev_priv)) {
  13197. pixclk = ilk_pipe_pixel_rate(crtc_state);
  13198. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  13199. if (crtc_state->ips_enabled)
  13200. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  13201. } else if (IS_VALLEYVIEW(dev_priv) ||
  13202. IS_CHERRYVIEW(dev_priv) ||
  13203. IS_BROXTON(dev_priv))
  13204. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  13205. else
  13206. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  13207. }
  13208. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  13209. readout_plane_state(crtc);
  13210. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  13211. crtc->base.base.id,
  13212. crtc->active ? "enabled" : "disabled");
  13213. }
  13214. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13215. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13216. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  13217. &pll->config.hw_state);
  13218. pll->config.crtc_mask = 0;
  13219. for_each_intel_crtc(dev, crtc) {
  13220. if (crtc->active && crtc->config->shared_dpll == pll)
  13221. pll->config.crtc_mask |= 1 << crtc->pipe;
  13222. }
  13223. pll->active_mask = pll->config.crtc_mask;
  13224. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13225. pll->name, pll->config.crtc_mask, pll->on);
  13226. }
  13227. for_each_intel_encoder(dev, encoder) {
  13228. pipe = 0;
  13229. if (encoder->get_hw_state(encoder, &pipe)) {
  13230. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13231. encoder->base.crtc = &crtc->base;
  13232. encoder->get_config(encoder, crtc->config);
  13233. } else {
  13234. encoder->base.crtc = NULL;
  13235. }
  13236. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13237. encoder->base.base.id,
  13238. encoder->base.name,
  13239. encoder->base.crtc ? "enabled" : "disabled",
  13240. pipe_name(pipe));
  13241. }
  13242. for_each_intel_connector(dev, connector) {
  13243. if (connector->get_hw_state(connector)) {
  13244. connector->base.dpms = DRM_MODE_DPMS_ON;
  13245. encoder = connector->encoder;
  13246. connector->base.encoder = &encoder->base;
  13247. if (encoder->base.crtc &&
  13248. encoder->base.crtc->state->active) {
  13249. /*
  13250. * This has to be done during hardware readout
  13251. * because anything calling .crtc_disable may
  13252. * rely on the connector_mask being accurate.
  13253. */
  13254. encoder->base.crtc->state->connector_mask |=
  13255. 1 << drm_connector_index(&connector->base);
  13256. encoder->base.crtc->state->encoder_mask |=
  13257. 1 << drm_encoder_index(&encoder->base);
  13258. }
  13259. } else {
  13260. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13261. connector->base.encoder = NULL;
  13262. }
  13263. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13264. connector->base.base.id,
  13265. connector->base.name,
  13266. connector->base.encoder ? "enabled" : "disabled");
  13267. }
  13268. for_each_intel_crtc(dev, crtc) {
  13269. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  13270. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13271. if (crtc->base.state->active) {
  13272. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  13273. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  13274. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13275. /*
  13276. * The initial mode needs to be set in order to keep
  13277. * the atomic core happy. It wants a valid mode if the
  13278. * crtc's enabled, so we do the above call.
  13279. *
  13280. * At this point some state updated by the connectors
  13281. * in their ->detect() callback has not run yet, so
  13282. * no recalculation can be done yet.
  13283. *
  13284. * Even if we could do a recalculation and modeset
  13285. * right now it would cause a double modeset if
  13286. * fbdev or userspace chooses a different initial mode.
  13287. *
  13288. * If that happens, someone indicated they wanted a
  13289. * mode change, which means it's safe to do a full
  13290. * recalculation.
  13291. */
  13292. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  13293. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  13294. update_scanline_offset(crtc);
  13295. }
  13296. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  13297. }
  13298. }
  13299. /* Scan out the current hw modeset state,
  13300. * and sanitizes it to the current state
  13301. */
  13302. static void
  13303. intel_modeset_setup_hw_state(struct drm_device *dev)
  13304. {
  13305. struct drm_i915_private *dev_priv = dev->dev_private;
  13306. enum pipe pipe;
  13307. struct intel_crtc *crtc;
  13308. struct intel_encoder *encoder;
  13309. int i;
  13310. intel_modeset_readout_hw_state(dev);
  13311. /* HW state is read out, now we need to sanitize this mess. */
  13312. for_each_intel_encoder(dev, encoder) {
  13313. intel_sanitize_encoder(encoder);
  13314. }
  13315. for_each_pipe(dev_priv, pipe) {
  13316. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13317. intel_sanitize_crtc(crtc);
  13318. intel_dump_pipe_config(crtc, crtc->config,
  13319. "[setup_hw_state]");
  13320. }
  13321. intel_modeset_update_connector_atomic_state(dev);
  13322. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13323. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13324. if (!pll->on || pll->active_mask)
  13325. continue;
  13326. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13327. pll->funcs.disable(dev_priv, pll);
  13328. pll->on = false;
  13329. }
  13330. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  13331. vlv_wm_get_hw_state(dev);
  13332. else if (IS_GEN9(dev))
  13333. skl_wm_get_hw_state(dev);
  13334. else if (HAS_PCH_SPLIT(dev))
  13335. ilk_wm_get_hw_state(dev);
  13336. for_each_intel_crtc(dev, crtc) {
  13337. unsigned long put_domains;
  13338. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13339. if (WARN_ON(put_domains))
  13340. modeset_put_power_domains(dev_priv, put_domains);
  13341. }
  13342. intel_display_set_init_power(dev_priv, false);
  13343. intel_fbc_init_pipe_state(dev_priv);
  13344. }
  13345. void intel_display_resume(struct drm_device *dev)
  13346. {
  13347. struct drm_i915_private *dev_priv = to_i915(dev);
  13348. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13349. struct drm_modeset_acquire_ctx ctx;
  13350. int ret;
  13351. bool setup = false;
  13352. dev_priv->modeset_restore_state = NULL;
  13353. /*
  13354. * This is a cludge because with real atomic modeset mode_config.mutex
  13355. * won't be taken. Unfortunately some probed state like
  13356. * audio_codec_enable is still protected by mode_config.mutex, so lock
  13357. * it here for now.
  13358. */
  13359. mutex_lock(&dev->mode_config.mutex);
  13360. drm_modeset_acquire_init(&ctx, 0);
  13361. retry:
  13362. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13363. if (ret == 0 && !setup) {
  13364. setup = true;
  13365. intel_modeset_setup_hw_state(dev);
  13366. i915_redisable_vga(dev);
  13367. }
  13368. if (ret == 0 && state) {
  13369. struct drm_crtc_state *crtc_state;
  13370. struct drm_crtc *crtc;
  13371. int i;
  13372. state->acquire_ctx = &ctx;
  13373. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  13374. /*
  13375. * Force recalculation even if we restore
  13376. * current state. With fast modeset this may not result
  13377. * in a modeset when the state is compatible.
  13378. */
  13379. crtc_state->mode_changed = true;
  13380. }
  13381. ret = drm_atomic_commit(state);
  13382. }
  13383. if (ret == -EDEADLK) {
  13384. drm_modeset_backoff(&ctx);
  13385. goto retry;
  13386. }
  13387. drm_modeset_drop_locks(&ctx);
  13388. drm_modeset_acquire_fini(&ctx);
  13389. mutex_unlock(&dev->mode_config.mutex);
  13390. if (ret) {
  13391. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13392. drm_atomic_state_free(state);
  13393. }
  13394. }
  13395. void intel_modeset_gem_init(struct drm_device *dev)
  13396. {
  13397. struct drm_crtc *c;
  13398. struct drm_i915_gem_object *obj;
  13399. int ret;
  13400. intel_init_gt_powersave(dev);
  13401. intel_modeset_init_hw(dev);
  13402. intel_setup_overlay(dev);
  13403. /*
  13404. * Make sure any fbs we allocated at startup are properly
  13405. * pinned & fenced. When we do the allocation it's too early
  13406. * for this.
  13407. */
  13408. for_each_crtc(dev, c) {
  13409. obj = intel_fb_obj(c->primary->fb);
  13410. if (obj == NULL)
  13411. continue;
  13412. mutex_lock(&dev->struct_mutex);
  13413. ret = intel_pin_and_fence_fb_obj(c->primary->fb,
  13414. c->primary->state->rotation);
  13415. mutex_unlock(&dev->struct_mutex);
  13416. if (ret) {
  13417. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13418. to_intel_crtc(c)->pipe);
  13419. drm_framebuffer_unreference(c->primary->fb);
  13420. c->primary->fb = NULL;
  13421. c->primary->crtc = c->primary->state->crtc = NULL;
  13422. update_state_fb(c->primary);
  13423. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13424. }
  13425. }
  13426. intel_backlight_register(dev);
  13427. }
  13428. void intel_connector_unregister(struct intel_connector *intel_connector)
  13429. {
  13430. struct drm_connector *connector = &intel_connector->base;
  13431. intel_panel_destroy_backlight(connector);
  13432. drm_connector_unregister(connector);
  13433. }
  13434. void intel_modeset_cleanup(struct drm_device *dev)
  13435. {
  13436. struct drm_i915_private *dev_priv = dev->dev_private;
  13437. struct intel_connector *connector;
  13438. intel_disable_gt_powersave(dev);
  13439. intel_backlight_unregister(dev);
  13440. /*
  13441. * Interrupts and polling as the first thing to avoid creating havoc.
  13442. * Too much stuff here (turning of connectors, ...) would
  13443. * experience fancy races otherwise.
  13444. */
  13445. intel_irq_uninstall(dev_priv);
  13446. /*
  13447. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13448. * poll handlers. Hence disable polling after hpd handling is shut down.
  13449. */
  13450. drm_kms_helper_poll_fini(dev);
  13451. intel_unregister_dsm_handler();
  13452. intel_fbc_global_disable(dev_priv);
  13453. /* flush any delayed tasks or pending work */
  13454. flush_scheduled_work();
  13455. /* destroy the backlight and sysfs files before encoders/connectors */
  13456. for_each_intel_connector(dev, connector)
  13457. connector->unregister(connector);
  13458. drm_mode_config_cleanup(dev);
  13459. intel_cleanup_overlay(dev);
  13460. intel_cleanup_gt_powersave(dev);
  13461. intel_teardown_gmbus(dev);
  13462. }
  13463. /*
  13464. * Return which encoder is currently attached for connector.
  13465. */
  13466. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13467. {
  13468. return &intel_attached_encoder(connector)->base;
  13469. }
  13470. void intel_connector_attach_encoder(struct intel_connector *connector,
  13471. struct intel_encoder *encoder)
  13472. {
  13473. connector->encoder = encoder;
  13474. drm_mode_connector_attach_encoder(&connector->base,
  13475. &encoder->base);
  13476. }
  13477. /*
  13478. * set vga decode state - true == enable VGA decode
  13479. */
  13480. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13481. {
  13482. struct drm_i915_private *dev_priv = dev->dev_private;
  13483. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13484. u16 gmch_ctrl;
  13485. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13486. DRM_ERROR("failed to read control word\n");
  13487. return -EIO;
  13488. }
  13489. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13490. return 0;
  13491. if (state)
  13492. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13493. else
  13494. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13495. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13496. DRM_ERROR("failed to write control word\n");
  13497. return -EIO;
  13498. }
  13499. return 0;
  13500. }
  13501. struct intel_display_error_state {
  13502. u32 power_well_driver;
  13503. int num_transcoders;
  13504. struct intel_cursor_error_state {
  13505. u32 control;
  13506. u32 position;
  13507. u32 base;
  13508. u32 size;
  13509. } cursor[I915_MAX_PIPES];
  13510. struct intel_pipe_error_state {
  13511. bool power_domain_on;
  13512. u32 source;
  13513. u32 stat;
  13514. } pipe[I915_MAX_PIPES];
  13515. struct intel_plane_error_state {
  13516. u32 control;
  13517. u32 stride;
  13518. u32 size;
  13519. u32 pos;
  13520. u32 addr;
  13521. u32 surface;
  13522. u32 tile_offset;
  13523. } plane[I915_MAX_PIPES];
  13524. struct intel_transcoder_error_state {
  13525. bool power_domain_on;
  13526. enum transcoder cpu_transcoder;
  13527. u32 conf;
  13528. u32 htotal;
  13529. u32 hblank;
  13530. u32 hsync;
  13531. u32 vtotal;
  13532. u32 vblank;
  13533. u32 vsync;
  13534. } transcoder[4];
  13535. };
  13536. struct intel_display_error_state *
  13537. intel_display_capture_error_state(struct drm_device *dev)
  13538. {
  13539. struct drm_i915_private *dev_priv = dev->dev_private;
  13540. struct intel_display_error_state *error;
  13541. int transcoders[] = {
  13542. TRANSCODER_A,
  13543. TRANSCODER_B,
  13544. TRANSCODER_C,
  13545. TRANSCODER_EDP,
  13546. };
  13547. int i;
  13548. if (INTEL_INFO(dev)->num_pipes == 0)
  13549. return NULL;
  13550. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13551. if (error == NULL)
  13552. return NULL;
  13553. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13554. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13555. for_each_pipe(dev_priv, i) {
  13556. error->pipe[i].power_domain_on =
  13557. __intel_display_power_is_enabled(dev_priv,
  13558. POWER_DOMAIN_PIPE(i));
  13559. if (!error->pipe[i].power_domain_on)
  13560. continue;
  13561. error->cursor[i].control = I915_READ(CURCNTR(i));
  13562. error->cursor[i].position = I915_READ(CURPOS(i));
  13563. error->cursor[i].base = I915_READ(CURBASE(i));
  13564. error->plane[i].control = I915_READ(DSPCNTR(i));
  13565. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13566. if (INTEL_INFO(dev)->gen <= 3) {
  13567. error->plane[i].size = I915_READ(DSPSIZE(i));
  13568. error->plane[i].pos = I915_READ(DSPPOS(i));
  13569. }
  13570. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13571. error->plane[i].addr = I915_READ(DSPADDR(i));
  13572. if (INTEL_INFO(dev)->gen >= 4) {
  13573. error->plane[i].surface = I915_READ(DSPSURF(i));
  13574. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13575. }
  13576. error->pipe[i].source = I915_READ(PIPESRC(i));
  13577. if (HAS_GMCH_DISPLAY(dev))
  13578. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13579. }
  13580. /* Note: this does not include DSI transcoders. */
  13581. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13582. if (HAS_DDI(dev_priv))
  13583. error->num_transcoders++; /* Account for eDP. */
  13584. for (i = 0; i < error->num_transcoders; i++) {
  13585. enum transcoder cpu_transcoder = transcoders[i];
  13586. error->transcoder[i].power_domain_on =
  13587. __intel_display_power_is_enabled(dev_priv,
  13588. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13589. if (!error->transcoder[i].power_domain_on)
  13590. continue;
  13591. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13592. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13593. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13594. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13595. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13596. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13597. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13598. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13599. }
  13600. return error;
  13601. }
  13602. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13603. void
  13604. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13605. struct drm_device *dev,
  13606. struct intel_display_error_state *error)
  13607. {
  13608. struct drm_i915_private *dev_priv = dev->dev_private;
  13609. int i;
  13610. if (!error)
  13611. return;
  13612. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13613. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13614. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13615. error->power_well_driver);
  13616. for_each_pipe(dev_priv, i) {
  13617. err_printf(m, "Pipe [%d]:\n", i);
  13618. err_printf(m, " Power: %s\n",
  13619. onoff(error->pipe[i].power_domain_on));
  13620. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13621. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13622. err_printf(m, "Plane [%d]:\n", i);
  13623. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13624. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13625. if (INTEL_INFO(dev)->gen <= 3) {
  13626. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13627. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13628. }
  13629. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13630. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13631. if (INTEL_INFO(dev)->gen >= 4) {
  13632. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13633. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13634. }
  13635. err_printf(m, "Cursor [%d]:\n", i);
  13636. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13637. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13638. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13639. }
  13640. for (i = 0; i < error->num_transcoders; i++) {
  13641. err_printf(m, "CPU transcoder: %s\n",
  13642. transcoder_name(error->transcoder[i].cpu_transcoder));
  13643. err_printf(m, " Power: %s\n",
  13644. onoff(error->transcoder[i].power_domain_on));
  13645. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13646. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13647. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13648. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13649. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13650. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13651. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13652. }
  13653. }