intel_display.c 461 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "i915_gem_dmabuf.h"
  40. #include "intel_dsi.h"
  41. #include "i915_trace.h"
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_dp_helper.h>
  45. #include <drm/drm_crtc_helper.h>
  46. #include <drm/drm_plane_helper.h>
  47. #include <drm/drm_rect.h>
  48. #include <linux/dma_remapping.h>
  49. #include <linux/reservation.h>
  50. static bool is_mmio_work(struct intel_flip_work *work)
  51. {
  52. return work->mmio_work.func;
  53. }
  54. /* Primary plane formats for gen <= 3 */
  55. static const uint32_t i8xx_primary_formats[] = {
  56. DRM_FORMAT_C8,
  57. DRM_FORMAT_RGB565,
  58. DRM_FORMAT_XRGB1555,
  59. DRM_FORMAT_XRGB8888,
  60. };
  61. /* Primary plane formats for gen >= 4 */
  62. static const uint32_t i965_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_XRGB2101010,
  68. DRM_FORMAT_XBGR2101010,
  69. };
  70. static const uint32_t skl_primary_formats[] = {
  71. DRM_FORMAT_C8,
  72. DRM_FORMAT_RGB565,
  73. DRM_FORMAT_XRGB8888,
  74. DRM_FORMAT_XBGR8888,
  75. DRM_FORMAT_ARGB8888,
  76. DRM_FORMAT_ABGR8888,
  77. DRM_FORMAT_XRGB2101010,
  78. DRM_FORMAT_XBGR2101010,
  79. DRM_FORMAT_YUYV,
  80. DRM_FORMAT_YVYU,
  81. DRM_FORMAT_UYVY,
  82. DRM_FORMAT_VYUY,
  83. };
  84. /* Cursor formats */
  85. static const uint32_t intel_cursor_formats[] = {
  86. DRM_FORMAT_ARGB8888,
  87. };
  88. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  89. struct intel_crtc_state *pipe_config);
  90. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  91. struct intel_crtc_state *pipe_config);
  92. static int intel_framebuffer_init(struct drm_device *dev,
  93. struct intel_framebuffer *ifb,
  94. struct drm_mode_fb_cmd2 *mode_cmd,
  95. struct drm_i915_gem_object *obj);
  96. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  98. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  99. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  100. struct intel_link_m_n *m_n,
  101. struct intel_link_m_n *m2_n2);
  102. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  104. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  105. static void vlv_prepare_pll(struct intel_crtc *crtc,
  106. const struct intel_crtc_state *pipe_config);
  107. static void chv_prepare_pll(struct intel_crtc *crtc,
  108. const struct intel_crtc_state *pipe_config);
  109. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  111. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  112. struct intel_crtc_state *crtc_state);
  113. static void skylake_pfit_enable(struct intel_crtc *crtc);
  114. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  115. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  116. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  117. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  118. static int ilk_max_pixel_rate(struct drm_atomic_state *state);
  119. static int bxt_calc_cdclk(int max_pixclk);
  120. struct intel_limit {
  121. struct {
  122. int min, max;
  123. } dot, vco, n, m, m1, m2, p, p1;
  124. struct {
  125. int dot_limit;
  126. int p2_slow, p2_fast;
  127. } p2;
  128. };
  129. /* returns HPLL frequency in kHz */
  130. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  131. {
  132. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  133. /* Obtain SKU information */
  134. mutex_lock(&dev_priv->sb_lock);
  135. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  136. CCK_FUSE_HPLL_FREQ_MASK;
  137. mutex_unlock(&dev_priv->sb_lock);
  138. return vco_freq[hpll_freq] * 1000;
  139. }
  140. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  141. const char *name, u32 reg, int ref_freq)
  142. {
  143. u32 val;
  144. int divider;
  145. mutex_lock(&dev_priv->sb_lock);
  146. val = vlv_cck_read(dev_priv, reg);
  147. mutex_unlock(&dev_priv->sb_lock);
  148. divider = val & CCK_FREQUENCY_VALUES;
  149. WARN((val & CCK_FREQUENCY_STATUS) !=
  150. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  151. "%s change in progress\n", name);
  152. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  153. }
  154. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  155. const char *name, u32 reg)
  156. {
  157. if (dev_priv->hpll_freq == 0)
  158. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  159. return vlv_get_cck_clock(dev_priv, name, reg,
  160. dev_priv->hpll_freq);
  161. }
  162. static int
  163. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  164. {
  165. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  166. }
  167. static int
  168. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  169. {
  170. /* RAWCLK_FREQ_VLV register updated from power well code */
  171. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  172. CCK_DISPLAY_REF_CLOCK_CONTROL);
  173. }
  174. static int
  175. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  176. {
  177. uint32_t clkcfg;
  178. /* hrawclock is 1/4 the FSB frequency */
  179. clkcfg = I915_READ(CLKCFG);
  180. switch (clkcfg & CLKCFG_FSB_MASK) {
  181. case CLKCFG_FSB_400:
  182. return 100000;
  183. case CLKCFG_FSB_533:
  184. return 133333;
  185. case CLKCFG_FSB_667:
  186. return 166667;
  187. case CLKCFG_FSB_800:
  188. return 200000;
  189. case CLKCFG_FSB_1067:
  190. return 266667;
  191. case CLKCFG_FSB_1333:
  192. return 333333;
  193. /* these two are just a guess; one of them might be right */
  194. case CLKCFG_FSB_1600:
  195. case CLKCFG_FSB_1600_ALT:
  196. return 400000;
  197. default:
  198. return 133333;
  199. }
  200. }
  201. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  202. {
  203. if (HAS_PCH_SPLIT(dev_priv))
  204. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  205. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  206. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  207. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  208. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  209. else
  210. return; /* no rawclk on other platforms, or no need to know it */
  211. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  212. }
  213. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  214. {
  215. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  216. return;
  217. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  218. CCK_CZ_CLOCK_CONTROL);
  219. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  220. }
  221. static inline u32 /* units of 100MHz */
  222. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  223. const struct intel_crtc_state *pipe_config)
  224. {
  225. if (HAS_DDI(dev_priv))
  226. return pipe_config->port_clock; /* SPLL */
  227. else if (IS_GEN5(dev_priv))
  228. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  229. else
  230. return 270000;
  231. }
  232. static const struct intel_limit intel_limits_i8xx_dac = {
  233. .dot = { .min = 25000, .max = 350000 },
  234. .vco = { .min = 908000, .max = 1512000 },
  235. .n = { .min = 2, .max = 16 },
  236. .m = { .min = 96, .max = 140 },
  237. .m1 = { .min = 18, .max = 26 },
  238. .m2 = { .min = 6, .max = 16 },
  239. .p = { .min = 4, .max = 128 },
  240. .p1 = { .min = 2, .max = 33 },
  241. .p2 = { .dot_limit = 165000,
  242. .p2_slow = 4, .p2_fast = 2 },
  243. };
  244. static const struct intel_limit intel_limits_i8xx_dvo = {
  245. .dot = { .min = 25000, .max = 350000 },
  246. .vco = { .min = 908000, .max = 1512000 },
  247. .n = { .min = 2, .max = 16 },
  248. .m = { .min = 96, .max = 140 },
  249. .m1 = { .min = 18, .max = 26 },
  250. .m2 = { .min = 6, .max = 16 },
  251. .p = { .min = 4, .max = 128 },
  252. .p1 = { .min = 2, .max = 33 },
  253. .p2 = { .dot_limit = 165000,
  254. .p2_slow = 4, .p2_fast = 4 },
  255. };
  256. static const struct intel_limit intel_limits_i8xx_lvds = {
  257. .dot = { .min = 25000, .max = 350000 },
  258. .vco = { .min = 908000, .max = 1512000 },
  259. .n = { .min = 2, .max = 16 },
  260. .m = { .min = 96, .max = 140 },
  261. .m1 = { .min = 18, .max = 26 },
  262. .m2 = { .min = 6, .max = 16 },
  263. .p = { .min = 4, .max = 128 },
  264. .p1 = { .min = 1, .max = 6 },
  265. .p2 = { .dot_limit = 165000,
  266. .p2_slow = 14, .p2_fast = 7 },
  267. };
  268. static const struct intel_limit intel_limits_i9xx_sdvo = {
  269. .dot = { .min = 20000, .max = 400000 },
  270. .vco = { .min = 1400000, .max = 2800000 },
  271. .n = { .min = 1, .max = 6 },
  272. .m = { .min = 70, .max = 120 },
  273. .m1 = { .min = 8, .max = 18 },
  274. .m2 = { .min = 3, .max = 7 },
  275. .p = { .min = 5, .max = 80 },
  276. .p1 = { .min = 1, .max = 8 },
  277. .p2 = { .dot_limit = 200000,
  278. .p2_slow = 10, .p2_fast = 5 },
  279. };
  280. static const struct intel_limit intel_limits_i9xx_lvds = {
  281. .dot = { .min = 20000, .max = 400000 },
  282. .vco = { .min = 1400000, .max = 2800000 },
  283. .n = { .min = 1, .max = 6 },
  284. .m = { .min = 70, .max = 120 },
  285. .m1 = { .min = 8, .max = 18 },
  286. .m2 = { .min = 3, .max = 7 },
  287. .p = { .min = 7, .max = 98 },
  288. .p1 = { .min = 1, .max = 8 },
  289. .p2 = { .dot_limit = 112000,
  290. .p2_slow = 14, .p2_fast = 7 },
  291. };
  292. static const struct intel_limit intel_limits_g4x_sdvo = {
  293. .dot = { .min = 25000, .max = 270000 },
  294. .vco = { .min = 1750000, .max = 3500000},
  295. .n = { .min = 1, .max = 4 },
  296. .m = { .min = 104, .max = 138 },
  297. .m1 = { .min = 17, .max = 23 },
  298. .m2 = { .min = 5, .max = 11 },
  299. .p = { .min = 10, .max = 30 },
  300. .p1 = { .min = 1, .max = 3},
  301. .p2 = { .dot_limit = 270000,
  302. .p2_slow = 10,
  303. .p2_fast = 10
  304. },
  305. };
  306. static const struct intel_limit intel_limits_g4x_hdmi = {
  307. .dot = { .min = 22000, .max = 400000 },
  308. .vco = { .min = 1750000, .max = 3500000},
  309. .n = { .min = 1, .max = 4 },
  310. .m = { .min = 104, .max = 138 },
  311. .m1 = { .min = 16, .max = 23 },
  312. .m2 = { .min = 5, .max = 11 },
  313. .p = { .min = 5, .max = 80 },
  314. .p1 = { .min = 1, .max = 8},
  315. .p2 = { .dot_limit = 165000,
  316. .p2_slow = 10, .p2_fast = 5 },
  317. };
  318. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  319. .dot = { .min = 20000, .max = 115000 },
  320. .vco = { .min = 1750000, .max = 3500000 },
  321. .n = { .min = 1, .max = 3 },
  322. .m = { .min = 104, .max = 138 },
  323. .m1 = { .min = 17, .max = 23 },
  324. .m2 = { .min = 5, .max = 11 },
  325. .p = { .min = 28, .max = 112 },
  326. .p1 = { .min = 2, .max = 8 },
  327. .p2 = { .dot_limit = 0,
  328. .p2_slow = 14, .p2_fast = 14
  329. },
  330. };
  331. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  332. .dot = { .min = 80000, .max = 224000 },
  333. .vco = { .min = 1750000, .max = 3500000 },
  334. .n = { .min = 1, .max = 3 },
  335. .m = { .min = 104, .max = 138 },
  336. .m1 = { .min = 17, .max = 23 },
  337. .m2 = { .min = 5, .max = 11 },
  338. .p = { .min = 14, .max = 42 },
  339. .p1 = { .min = 2, .max = 6 },
  340. .p2 = { .dot_limit = 0,
  341. .p2_slow = 7, .p2_fast = 7
  342. },
  343. };
  344. static const struct intel_limit intel_limits_pineview_sdvo = {
  345. .dot = { .min = 20000, .max = 400000},
  346. .vco = { .min = 1700000, .max = 3500000 },
  347. /* Pineview's Ncounter is a ring counter */
  348. .n = { .min = 3, .max = 6 },
  349. .m = { .min = 2, .max = 256 },
  350. /* Pineview only has one combined m divider, which we treat as m2. */
  351. .m1 = { .min = 0, .max = 0 },
  352. .m2 = { .min = 0, .max = 254 },
  353. .p = { .min = 5, .max = 80 },
  354. .p1 = { .min = 1, .max = 8 },
  355. .p2 = { .dot_limit = 200000,
  356. .p2_slow = 10, .p2_fast = 5 },
  357. };
  358. static const struct intel_limit intel_limits_pineview_lvds = {
  359. .dot = { .min = 20000, .max = 400000 },
  360. .vco = { .min = 1700000, .max = 3500000 },
  361. .n = { .min = 3, .max = 6 },
  362. .m = { .min = 2, .max = 256 },
  363. .m1 = { .min = 0, .max = 0 },
  364. .m2 = { .min = 0, .max = 254 },
  365. .p = { .min = 7, .max = 112 },
  366. .p1 = { .min = 1, .max = 8 },
  367. .p2 = { .dot_limit = 112000,
  368. .p2_slow = 14, .p2_fast = 14 },
  369. };
  370. /* Ironlake / Sandybridge
  371. *
  372. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  373. * the range value for them is (actual_value - 2).
  374. */
  375. static const struct intel_limit intel_limits_ironlake_dac = {
  376. .dot = { .min = 25000, .max = 350000 },
  377. .vco = { .min = 1760000, .max = 3510000 },
  378. .n = { .min = 1, .max = 5 },
  379. .m = { .min = 79, .max = 127 },
  380. .m1 = { .min = 12, .max = 22 },
  381. .m2 = { .min = 5, .max = 9 },
  382. .p = { .min = 5, .max = 80 },
  383. .p1 = { .min = 1, .max = 8 },
  384. .p2 = { .dot_limit = 225000,
  385. .p2_slow = 10, .p2_fast = 5 },
  386. };
  387. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  388. .dot = { .min = 25000, .max = 350000 },
  389. .vco = { .min = 1760000, .max = 3510000 },
  390. .n = { .min = 1, .max = 3 },
  391. .m = { .min = 79, .max = 118 },
  392. .m1 = { .min = 12, .max = 22 },
  393. .m2 = { .min = 5, .max = 9 },
  394. .p = { .min = 28, .max = 112 },
  395. .p1 = { .min = 2, .max = 8 },
  396. .p2 = { .dot_limit = 225000,
  397. .p2_slow = 14, .p2_fast = 14 },
  398. };
  399. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  400. .dot = { .min = 25000, .max = 350000 },
  401. .vco = { .min = 1760000, .max = 3510000 },
  402. .n = { .min = 1, .max = 3 },
  403. .m = { .min = 79, .max = 127 },
  404. .m1 = { .min = 12, .max = 22 },
  405. .m2 = { .min = 5, .max = 9 },
  406. .p = { .min = 14, .max = 56 },
  407. .p1 = { .min = 2, .max = 8 },
  408. .p2 = { .dot_limit = 225000,
  409. .p2_slow = 7, .p2_fast = 7 },
  410. };
  411. /* LVDS 100mhz refclk limits. */
  412. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  413. .dot = { .min = 25000, .max = 350000 },
  414. .vco = { .min = 1760000, .max = 3510000 },
  415. .n = { .min = 1, .max = 2 },
  416. .m = { .min = 79, .max = 126 },
  417. .m1 = { .min = 12, .max = 22 },
  418. .m2 = { .min = 5, .max = 9 },
  419. .p = { .min = 28, .max = 112 },
  420. .p1 = { .min = 2, .max = 8 },
  421. .p2 = { .dot_limit = 225000,
  422. .p2_slow = 14, .p2_fast = 14 },
  423. };
  424. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  425. .dot = { .min = 25000, .max = 350000 },
  426. .vco = { .min = 1760000, .max = 3510000 },
  427. .n = { .min = 1, .max = 3 },
  428. .m = { .min = 79, .max = 126 },
  429. .m1 = { .min = 12, .max = 22 },
  430. .m2 = { .min = 5, .max = 9 },
  431. .p = { .min = 14, .max = 42 },
  432. .p1 = { .min = 2, .max = 6 },
  433. .p2 = { .dot_limit = 225000,
  434. .p2_slow = 7, .p2_fast = 7 },
  435. };
  436. static const struct intel_limit intel_limits_vlv = {
  437. /*
  438. * These are the data rate limits (measured in fast clocks)
  439. * since those are the strictest limits we have. The fast
  440. * clock and actual rate limits are more relaxed, so checking
  441. * them would make no difference.
  442. */
  443. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  444. .vco = { .min = 4000000, .max = 6000000 },
  445. .n = { .min = 1, .max = 7 },
  446. .m1 = { .min = 2, .max = 3 },
  447. .m2 = { .min = 11, .max = 156 },
  448. .p1 = { .min = 2, .max = 3 },
  449. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  450. };
  451. static const struct intel_limit intel_limits_chv = {
  452. /*
  453. * These are the data rate limits (measured in fast clocks)
  454. * since those are the strictest limits we have. The fast
  455. * clock and actual rate limits are more relaxed, so checking
  456. * them would make no difference.
  457. */
  458. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  459. .vco = { .min = 4800000, .max = 6480000 },
  460. .n = { .min = 1, .max = 1 },
  461. .m1 = { .min = 2, .max = 2 },
  462. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  463. .p1 = { .min = 2, .max = 4 },
  464. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  465. };
  466. static const struct intel_limit intel_limits_bxt = {
  467. /* FIXME: find real dot limits */
  468. .dot = { .min = 0, .max = INT_MAX },
  469. .vco = { .min = 4800000, .max = 6700000 },
  470. .n = { .min = 1, .max = 1 },
  471. .m1 = { .min = 2, .max = 2 },
  472. /* FIXME: find real m2 limits */
  473. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  474. .p1 = { .min = 2, .max = 4 },
  475. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  476. };
  477. static bool
  478. needs_modeset(struct drm_crtc_state *state)
  479. {
  480. return drm_atomic_crtc_needs_modeset(state);
  481. }
  482. /*
  483. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  484. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  485. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  486. * The helpers' return value is the rate of the clock that is fed to the
  487. * display engine's pipe which can be the above fast dot clock rate or a
  488. * divided-down version of it.
  489. */
  490. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  491. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  492. {
  493. clock->m = clock->m2 + 2;
  494. clock->p = clock->p1 * clock->p2;
  495. if (WARN_ON(clock->n == 0 || clock->p == 0))
  496. return 0;
  497. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  498. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  499. return clock->dot;
  500. }
  501. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  502. {
  503. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  504. }
  505. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  506. {
  507. clock->m = i9xx_dpll_compute_m(clock);
  508. clock->p = clock->p1 * clock->p2;
  509. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  510. return 0;
  511. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  512. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  513. return clock->dot;
  514. }
  515. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  516. {
  517. clock->m = clock->m1 * clock->m2;
  518. clock->p = clock->p1 * clock->p2;
  519. if (WARN_ON(clock->n == 0 || clock->p == 0))
  520. return 0;
  521. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  522. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  523. return clock->dot / 5;
  524. }
  525. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  526. {
  527. clock->m = clock->m1 * clock->m2;
  528. clock->p = clock->p1 * clock->p2;
  529. if (WARN_ON(clock->n == 0 || clock->p == 0))
  530. return 0;
  531. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  532. clock->n << 22);
  533. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  534. return clock->dot / 5;
  535. }
  536. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  537. /**
  538. * Returns whether the given set of divisors are valid for a given refclk with
  539. * the given connectors.
  540. */
  541. static bool intel_PLL_is_valid(struct drm_device *dev,
  542. const struct intel_limit *limit,
  543. const struct dpll *clock)
  544. {
  545. if (clock->n < limit->n.min || limit->n.max < clock->n)
  546. INTELPllInvalid("n out of range\n");
  547. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  548. INTELPllInvalid("p1 out of range\n");
  549. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  550. INTELPllInvalid("m2 out of range\n");
  551. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  552. INTELPllInvalid("m1 out of range\n");
  553. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
  554. !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
  555. if (clock->m1 <= clock->m2)
  556. INTELPllInvalid("m1 <= m2\n");
  557. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
  558. if (clock->p < limit->p.min || limit->p.max < clock->p)
  559. INTELPllInvalid("p out of range\n");
  560. if (clock->m < limit->m.min || limit->m.max < clock->m)
  561. INTELPllInvalid("m out of range\n");
  562. }
  563. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  564. INTELPllInvalid("vco out of range\n");
  565. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  566. * connector, etc., rather than just a single range.
  567. */
  568. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  569. INTELPllInvalid("dot out of range\n");
  570. return true;
  571. }
  572. static int
  573. i9xx_select_p2_div(const struct intel_limit *limit,
  574. const struct intel_crtc_state *crtc_state,
  575. int target)
  576. {
  577. struct drm_device *dev = crtc_state->base.crtc->dev;
  578. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  579. /*
  580. * For LVDS just rely on its current settings for dual-channel.
  581. * We haven't figured out how to reliably set up different
  582. * single/dual channel state, if we even can.
  583. */
  584. if (intel_is_dual_link_lvds(dev))
  585. return limit->p2.p2_fast;
  586. else
  587. return limit->p2.p2_slow;
  588. } else {
  589. if (target < limit->p2.dot_limit)
  590. return limit->p2.p2_slow;
  591. else
  592. return limit->p2.p2_fast;
  593. }
  594. }
  595. /*
  596. * Returns a set of divisors for the desired target clock with the given
  597. * refclk, or FALSE. The returned values represent the clock equation:
  598. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  599. *
  600. * Target and reference clocks are specified in kHz.
  601. *
  602. * If match_clock is provided, then best_clock P divider must match the P
  603. * divider from @match_clock used for LVDS downclocking.
  604. */
  605. static bool
  606. i9xx_find_best_dpll(const struct intel_limit *limit,
  607. struct intel_crtc_state *crtc_state,
  608. int target, int refclk, struct dpll *match_clock,
  609. struct dpll *best_clock)
  610. {
  611. struct drm_device *dev = crtc_state->base.crtc->dev;
  612. struct dpll clock;
  613. int err = target;
  614. memset(best_clock, 0, sizeof(*best_clock));
  615. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  616. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  617. clock.m1++) {
  618. for (clock.m2 = limit->m2.min;
  619. clock.m2 <= limit->m2.max; clock.m2++) {
  620. if (clock.m2 >= clock.m1)
  621. break;
  622. for (clock.n = limit->n.min;
  623. clock.n <= limit->n.max; clock.n++) {
  624. for (clock.p1 = limit->p1.min;
  625. clock.p1 <= limit->p1.max; clock.p1++) {
  626. int this_err;
  627. i9xx_calc_dpll_params(refclk, &clock);
  628. if (!intel_PLL_is_valid(dev, limit,
  629. &clock))
  630. continue;
  631. if (match_clock &&
  632. clock.p != match_clock->p)
  633. continue;
  634. this_err = abs(clock.dot - target);
  635. if (this_err < err) {
  636. *best_clock = clock;
  637. err = this_err;
  638. }
  639. }
  640. }
  641. }
  642. }
  643. return (err != target);
  644. }
  645. /*
  646. * Returns a set of divisors for the desired target clock with the given
  647. * refclk, or FALSE. The returned values represent the clock equation:
  648. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  649. *
  650. * Target and reference clocks are specified in kHz.
  651. *
  652. * If match_clock is provided, then best_clock P divider must match the P
  653. * divider from @match_clock used for LVDS downclocking.
  654. */
  655. static bool
  656. pnv_find_best_dpll(const struct intel_limit *limit,
  657. struct intel_crtc_state *crtc_state,
  658. int target, int refclk, struct dpll *match_clock,
  659. struct dpll *best_clock)
  660. {
  661. struct drm_device *dev = crtc_state->base.crtc->dev;
  662. struct dpll clock;
  663. int err = target;
  664. memset(best_clock, 0, sizeof(*best_clock));
  665. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  666. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  667. clock.m1++) {
  668. for (clock.m2 = limit->m2.min;
  669. clock.m2 <= limit->m2.max; clock.m2++) {
  670. for (clock.n = limit->n.min;
  671. clock.n <= limit->n.max; clock.n++) {
  672. for (clock.p1 = limit->p1.min;
  673. clock.p1 <= limit->p1.max; clock.p1++) {
  674. int this_err;
  675. pnv_calc_dpll_params(refclk, &clock);
  676. if (!intel_PLL_is_valid(dev, limit,
  677. &clock))
  678. continue;
  679. if (match_clock &&
  680. clock.p != match_clock->p)
  681. continue;
  682. this_err = abs(clock.dot - target);
  683. if (this_err < err) {
  684. *best_clock = clock;
  685. err = this_err;
  686. }
  687. }
  688. }
  689. }
  690. }
  691. return (err != target);
  692. }
  693. /*
  694. * Returns a set of divisors for the desired target clock with the given
  695. * refclk, or FALSE. The returned values represent the clock equation:
  696. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  697. *
  698. * Target and reference clocks are specified in kHz.
  699. *
  700. * If match_clock is provided, then best_clock P divider must match the P
  701. * divider from @match_clock used for LVDS downclocking.
  702. */
  703. static bool
  704. g4x_find_best_dpll(const struct intel_limit *limit,
  705. struct intel_crtc_state *crtc_state,
  706. int target, int refclk, struct dpll *match_clock,
  707. struct dpll *best_clock)
  708. {
  709. struct drm_device *dev = crtc_state->base.crtc->dev;
  710. struct dpll clock;
  711. int max_n;
  712. bool found = false;
  713. /* approximately equals target * 0.00585 */
  714. int err_most = (target >> 8) + (target >> 9);
  715. memset(best_clock, 0, sizeof(*best_clock));
  716. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  717. max_n = limit->n.max;
  718. /* based on hardware requirement, prefer smaller n to precision */
  719. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  720. /* based on hardware requirement, prefere larger m1,m2 */
  721. for (clock.m1 = limit->m1.max;
  722. clock.m1 >= limit->m1.min; clock.m1--) {
  723. for (clock.m2 = limit->m2.max;
  724. clock.m2 >= limit->m2.min; clock.m2--) {
  725. for (clock.p1 = limit->p1.max;
  726. clock.p1 >= limit->p1.min; clock.p1--) {
  727. int this_err;
  728. i9xx_calc_dpll_params(refclk, &clock);
  729. if (!intel_PLL_is_valid(dev, limit,
  730. &clock))
  731. continue;
  732. this_err = abs(clock.dot - target);
  733. if (this_err < err_most) {
  734. *best_clock = clock;
  735. err_most = this_err;
  736. max_n = clock.n;
  737. found = true;
  738. }
  739. }
  740. }
  741. }
  742. }
  743. return found;
  744. }
  745. /*
  746. * Check if the calculated PLL configuration is more optimal compared to the
  747. * best configuration and error found so far. Return the calculated error.
  748. */
  749. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  750. const struct dpll *calculated_clock,
  751. const struct dpll *best_clock,
  752. unsigned int best_error_ppm,
  753. unsigned int *error_ppm)
  754. {
  755. /*
  756. * For CHV ignore the error and consider only the P value.
  757. * Prefer a bigger P value based on HW requirements.
  758. */
  759. if (IS_CHERRYVIEW(dev)) {
  760. *error_ppm = 0;
  761. return calculated_clock->p > best_clock->p;
  762. }
  763. if (WARN_ON_ONCE(!target_freq))
  764. return false;
  765. *error_ppm = div_u64(1000000ULL *
  766. abs(target_freq - calculated_clock->dot),
  767. target_freq);
  768. /*
  769. * Prefer a better P value over a better (smaller) error if the error
  770. * is small. Ensure this preference for future configurations too by
  771. * setting the error to 0.
  772. */
  773. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  774. *error_ppm = 0;
  775. return true;
  776. }
  777. return *error_ppm + 10 < best_error_ppm;
  778. }
  779. /*
  780. * Returns a set of divisors for the desired target clock with the given
  781. * refclk, or FALSE. The returned values represent the clock equation:
  782. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  783. */
  784. static bool
  785. vlv_find_best_dpll(const struct intel_limit *limit,
  786. struct intel_crtc_state *crtc_state,
  787. int target, int refclk, struct dpll *match_clock,
  788. struct dpll *best_clock)
  789. {
  790. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  791. struct drm_device *dev = crtc->base.dev;
  792. struct dpll clock;
  793. unsigned int bestppm = 1000000;
  794. /* min update 19.2 MHz */
  795. int max_n = min(limit->n.max, refclk / 19200);
  796. bool found = false;
  797. target *= 5; /* fast clock */
  798. memset(best_clock, 0, sizeof(*best_clock));
  799. /* based on hardware requirement, prefer smaller n to precision */
  800. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  801. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  802. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  803. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  804. clock.p = clock.p1 * clock.p2;
  805. /* based on hardware requirement, prefer bigger m1,m2 values */
  806. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  807. unsigned int ppm;
  808. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  809. refclk * clock.m1);
  810. vlv_calc_dpll_params(refclk, &clock);
  811. if (!intel_PLL_is_valid(dev, limit,
  812. &clock))
  813. continue;
  814. if (!vlv_PLL_is_optimal(dev, target,
  815. &clock,
  816. best_clock,
  817. bestppm, &ppm))
  818. continue;
  819. *best_clock = clock;
  820. bestppm = ppm;
  821. found = true;
  822. }
  823. }
  824. }
  825. }
  826. return found;
  827. }
  828. /*
  829. * Returns a set of divisors for the desired target clock with the given
  830. * refclk, or FALSE. The returned values represent the clock equation:
  831. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  832. */
  833. static bool
  834. chv_find_best_dpll(const struct intel_limit *limit,
  835. struct intel_crtc_state *crtc_state,
  836. int target, int refclk, struct dpll *match_clock,
  837. struct dpll *best_clock)
  838. {
  839. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  840. struct drm_device *dev = crtc->base.dev;
  841. unsigned int best_error_ppm;
  842. struct dpll clock;
  843. uint64_t m2;
  844. int found = false;
  845. memset(best_clock, 0, sizeof(*best_clock));
  846. best_error_ppm = 1000000;
  847. /*
  848. * Based on hardware doc, the n always set to 1, and m1 always
  849. * set to 2. If requires to support 200Mhz refclk, we need to
  850. * revisit this because n may not 1 anymore.
  851. */
  852. clock.n = 1, clock.m1 = 2;
  853. target *= 5; /* fast clock */
  854. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  855. for (clock.p2 = limit->p2.p2_fast;
  856. clock.p2 >= limit->p2.p2_slow;
  857. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  858. unsigned int error_ppm;
  859. clock.p = clock.p1 * clock.p2;
  860. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  861. clock.n) << 22, refclk * clock.m1);
  862. if (m2 > INT_MAX/clock.m1)
  863. continue;
  864. clock.m2 = m2;
  865. chv_calc_dpll_params(refclk, &clock);
  866. if (!intel_PLL_is_valid(dev, limit, &clock))
  867. continue;
  868. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  869. best_error_ppm, &error_ppm))
  870. continue;
  871. *best_clock = clock;
  872. best_error_ppm = error_ppm;
  873. found = true;
  874. }
  875. }
  876. return found;
  877. }
  878. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  879. struct dpll *best_clock)
  880. {
  881. int refclk = 100000;
  882. const struct intel_limit *limit = &intel_limits_bxt;
  883. return chv_find_best_dpll(limit, crtc_state,
  884. target_clock, refclk, NULL, best_clock);
  885. }
  886. bool intel_crtc_active(struct drm_crtc *crtc)
  887. {
  888. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  889. /* Be paranoid as we can arrive here with only partial
  890. * state retrieved from the hardware during setup.
  891. *
  892. * We can ditch the adjusted_mode.crtc_clock check as soon
  893. * as Haswell has gained clock readout/fastboot support.
  894. *
  895. * We can ditch the crtc->primary->fb check as soon as we can
  896. * properly reconstruct framebuffers.
  897. *
  898. * FIXME: The intel_crtc->active here should be switched to
  899. * crtc->state->active once we have proper CRTC states wired up
  900. * for atomic.
  901. */
  902. return intel_crtc->active && crtc->primary->state->fb &&
  903. intel_crtc->config->base.adjusted_mode.crtc_clock;
  904. }
  905. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  906. enum pipe pipe)
  907. {
  908. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  909. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  910. return intel_crtc->config->cpu_transcoder;
  911. }
  912. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  913. {
  914. struct drm_i915_private *dev_priv = to_i915(dev);
  915. i915_reg_t reg = PIPEDSL(pipe);
  916. u32 line1, line2;
  917. u32 line_mask;
  918. if (IS_GEN2(dev))
  919. line_mask = DSL_LINEMASK_GEN2;
  920. else
  921. line_mask = DSL_LINEMASK_GEN3;
  922. line1 = I915_READ(reg) & line_mask;
  923. msleep(5);
  924. line2 = I915_READ(reg) & line_mask;
  925. return line1 == line2;
  926. }
  927. /*
  928. * intel_wait_for_pipe_off - wait for pipe to turn off
  929. * @crtc: crtc whose pipe to wait for
  930. *
  931. * After disabling a pipe, we can't wait for vblank in the usual way,
  932. * spinning on the vblank interrupt status bit, since we won't actually
  933. * see an interrupt when the pipe is disabled.
  934. *
  935. * On Gen4 and above:
  936. * wait for the pipe register state bit to turn off
  937. *
  938. * Otherwise:
  939. * wait for the display line value to settle (it usually
  940. * ends up stopping at the start of the next frame).
  941. *
  942. */
  943. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  944. {
  945. struct drm_device *dev = crtc->base.dev;
  946. struct drm_i915_private *dev_priv = to_i915(dev);
  947. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  948. enum pipe pipe = crtc->pipe;
  949. if (INTEL_INFO(dev)->gen >= 4) {
  950. i915_reg_t reg = PIPECONF(cpu_transcoder);
  951. /* Wait for the Pipe State to go off */
  952. if (intel_wait_for_register(dev_priv,
  953. reg, I965_PIPECONF_ACTIVE, 0,
  954. 100))
  955. WARN(1, "pipe_off wait timed out\n");
  956. } else {
  957. /* Wait for the display line to settle */
  958. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  959. WARN(1, "pipe_off wait timed out\n");
  960. }
  961. }
  962. /* Only for pre-ILK configs */
  963. void assert_pll(struct drm_i915_private *dev_priv,
  964. enum pipe pipe, bool state)
  965. {
  966. u32 val;
  967. bool cur_state;
  968. val = I915_READ(DPLL(pipe));
  969. cur_state = !!(val & DPLL_VCO_ENABLE);
  970. I915_STATE_WARN(cur_state != state,
  971. "PLL state assertion failure (expected %s, current %s)\n",
  972. onoff(state), onoff(cur_state));
  973. }
  974. /* XXX: the dsi pll is shared between MIPI DSI ports */
  975. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  976. {
  977. u32 val;
  978. bool cur_state;
  979. mutex_lock(&dev_priv->sb_lock);
  980. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  981. mutex_unlock(&dev_priv->sb_lock);
  982. cur_state = val & DSI_PLL_VCO_EN;
  983. I915_STATE_WARN(cur_state != state,
  984. "DSI PLL state assertion failure (expected %s, current %s)\n",
  985. onoff(state), onoff(cur_state));
  986. }
  987. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  988. enum pipe pipe, bool state)
  989. {
  990. bool cur_state;
  991. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  992. pipe);
  993. if (HAS_DDI(dev_priv)) {
  994. /* DDI does not have a specific FDI_TX register */
  995. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  996. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  997. } else {
  998. u32 val = I915_READ(FDI_TX_CTL(pipe));
  999. cur_state = !!(val & FDI_TX_ENABLE);
  1000. }
  1001. I915_STATE_WARN(cur_state != state,
  1002. "FDI TX state assertion failure (expected %s, current %s)\n",
  1003. onoff(state), onoff(cur_state));
  1004. }
  1005. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1006. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1007. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1008. enum pipe pipe, bool state)
  1009. {
  1010. u32 val;
  1011. bool cur_state;
  1012. val = I915_READ(FDI_RX_CTL(pipe));
  1013. cur_state = !!(val & FDI_RX_ENABLE);
  1014. I915_STATE_WARN(cur_state != state,
  1015. "FDI RX state assertion failure (expected %s, current %s)\n",
  1016. onoff(state), onoff(cur_state));
  1017. }
  1018. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1019. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1020. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1021. enum pipe pipe)
  1022. {
  1023. u32 val;
  1024. /* ILK FDI PLL is always enabled */
  1025. if (IS_GEN5(dev_priv))
  1026. return;
  1027. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1028. if (HAS_DDI(dev_priv))
  1029. return;
  1030. val = I915_READ(FDI_TX_CTL(pipe));
  1031. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1032. }
  1033. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1034. enum pipe pipe, bool state)
  1035. {
  1036. u32 val;
  1037. bool cur_state;
  1038. val = I915_READ(FDI_RX_CTL(pipe));
  1039. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1040. I915_STATE_WARN(cur_state != state,
  1041. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1042. onoff(state), onoff(cur_state));
  1043. }
  1044. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1045. enum pipe pipe)
  1046. {
  1047. struct drm_device *dev = &dev_priv->drm;
  1048. i915_reg_t pp_reg;
  1049. u32 val;
  1050. enum pipe panel_pipe = PIPE_A;
  1051. bool locked = true;
  1052. if (WARN_ON(HAS_DDI(dev)))
  1053. return;
  1054. if (HAS_PCH_SPLIT(dev)) {
  1055. u32 port_sel;
  1056. pp_reg = PCH_PP_CONTROL;
  1057. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1058. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1059. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1060. panel_pipe = PIPE_B;
  1061. /* XXX: else fix for eDP */
  1062. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1063. /* presumably write lock depends on pipe, not port select */
  1064. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1065. panel_pipe = pipe;
  1066. } else {
  1067. pp_reg = PP_CONTROL;
  1068. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1069. panel_pipe = PIPE_B;
  1070. }
  1071. val = I915_READ(pp_reg);
  1072. if (!(val & PANEL_POWER_ON) ||
  1073. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1074. locked = false;
  1075. I915_STATE_WARN(panel_pipe == pipe && locked,
  1076. "panel assertion failure, pipe %c regs locked\n",
  1077. pipe_name(pipe));
  1078. }
  1079. static void assert_cursor(struct drm_i915_private *dev_priv,
  1080. enum pipe pipe, bool state)
  1081. {
  1082. struct drm_device *dev = &dev_priv->drm;
  1083. bool cur_state;
  1084. if (IS_845G(dev) || IS_I865G(dev))
  1085. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1086. else
  1087. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1088. I915_STATE_WARN(cur_state != state,
  1089. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1090. pipe_name(pipe), onoff(state), onoff(cur_state));
  1091. }
  1092. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1093. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1094. void assert_pipe(struct drm_i915_private *dev_priv,
  1095. enum pipe pipe, bool state)
  1096. {
  1097. bool cur_state;
  1098. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1099. pipe);
  1100. enum intel_display_power_domain power_domain;
  1101. /* if we need the pipe quirk it must be always on */
  1102. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1103. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1104. state = true;
  1105. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1106. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1107. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1108. cur_state = !!(val & PIPECONF_ENABLE);
  1109. intel_display_power_put(dev_priv, power_domain);
  1110. } else {
  1111. cur_state = false;
  1112. }
  1113. I915_STATE_WARN(cur_state != state,
  1114. "pipe %c assertion failure (expected %s, current %s)\n",
  1115. pipe_name(pipe), onoff(state), onoff(cur_state));
  1116. }
  1117. static void assert_plane(struct drm_i915_private *dev_priv,
  1118. enum plane plane, bool state)
  1119. {
  1120. u32 val;
  1121. bool cur_state;
  1122. val = I915_READ(DSPCNTR(plane));
  1123. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1124. I915_STATE_WARN(cur_state != state,
  1125. "plane %c assertion failure (expected %s, current %s)\n",
  1126. plane_name(plane), onoff(state), onoff(cur_state));
  1127. }
  1128. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1129. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1130. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1131. enum pipe pipe)
  1132. {
  1133. struct drm_device *dev = &dev_priv->drm;
  1134. int i;
  1135. /* Primary planes are fixed to pipes on gen4+ */
  1136. if (INTEL_INFO(dev)->gen >= 4) {
  1137. u32 val = I915_READ(DSPCNTR(pipe));
  1138. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1139. "plane %c assertion failure, should be disabled but not\n",
  1140. plane_name(pipe));
  1141. return;
  1142. }
  1143. /* Need to check both planes against the pipe */
  1144. for_each_pipe(dev_priv, i) {
  1145. u32 val = I915_READ(DSPCNTR(i));
  1146. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1147. DISPPLANE_SEL_PIPE_SHIFT;
  1148. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1149. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1150. plane_name(i), pipe_name(pipe));
  1151. }
  1152. }
  1153. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1154. enum pipe pipe)
  1155. {
  1156. struct drm_device *dev = &dev_priv->drm;
  1157. int sprite;
  1158. if (INTEL_INFO(dev)->gen >= 9) {
  1159. for_each_sprite(dev_priv, pipe, sprite) {
  1160. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1161. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1162. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1163. sprite, pipe_name(pipe));
  1164. }
  1165. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1166. for_each_sprite(dev_priv, pipe, sprite) {
  1167. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1168. I915_STATE_WARN(val & SP_ENABLE,
  1169. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1170. sprite_name(pipe, sprite), pipe_name(pipe));
  1171. }
  1172. } else if (INTEL_INFO(dev)->gen >= 7) {
  1173. u32 val = I915_READ(SPRCTL(pipe));
  1174. I915_STATE_WARN(val & SPRITE_ENABLE,
  1175. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1176. plane_name(pipe), pipe_name(pipe));
  1177. } else if (INTEL_INFO(dev)->gen >= 5) {
  1178. u32 val = I915_READ(DVSCNTR(pipe));
  1179. I915_STATE_WARN(val & DVS_ENABLE,
  1180. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1181. plane_name(pipe), pipe_name(pipe));
  1182. }
  1183. }
  1184. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1185. {
  1186. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1187. drm_crtc_vblank_put(crtc);
  1188. }
  1189. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1190. enum pipe pipe)
  1191. {
  1192. u32 val;
  1193. bool enabled;
  1194. val = I915_READ(PCH_TRANSCONF(pipe));
  1195. enabled = !!(val & TRANS_ENABLE);
  1196. I915_STATE_WARN(enabled,
  1197. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1198. pipe_name(pipe));
  1199. }
  1200. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1201. enum pipe pipe, u32 port_sel, u32 val)
  1202. {
  1203. if ((val & DP_PORT_EN) == 0)
  1204. return false;
  1205. if (HAS_PCH_CPT(dev_priv)) {
  1206. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1207. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1208. return false;
  1209. } else if (IS_CHERRYVIEW(dev_priv)) {
  1210. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1211. return false;
  1212. } else {
  1213. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1214. return false;
  1215. }
  1216. return true;
  1217. }
  1218. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1219. enum pipe pipe, u32 val)
  1220. {
  1221. if ((val & SDVO_ENABLE) == 0)
  1222. return false;
  1223. if (HAS_PCH_CPT(dev_priv)) {
  1224. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1225. return false;
  1226. } else if (IS_CHERRYVIEW(dev_priv)) {
  1227. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1228. return false;
  1229. } else {
  1230. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1231. return false;
  1232. }
  1233. return true;
  1234. }
  1235. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1236. enum pipe pipe, u32 val)
  1237. {
  1238. if ((val & LVDS_PORT_EN) == 0)
  1239. return false;
  1240. if (HAS_PCH_CPT(dev_priv)) {
  1241. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1242. return false;
  1243. } else {
  1244. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1245. return false;
  1246. }
  1247. return true;
  1248. }
  1249. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1250. enum pipe pipe, u32 val)
  1251. {
  1252. if ((val & ADPA_DAC_ENABLE) == 0)
  1253. return false;
  1254. if (HAS_PCH_CPT(dev_priv)) {
  1255. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1256. return false;
  1257. } else {
  1258. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1259. return false;
  1260. }
  1261. return true;
  1262. }
  1263. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1264. enum pipe pipe, i915_reg_t reg,
  1265. u32 port_sel)
  1266. {
  1267. u32 val = I915_READ(reg);
  1268. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1269. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1270. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1271. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1272. && (val & DP_PIPEB_SELECT),
  1273. "IBX PCH dp port still using transcoder B\n");
  1274. }
  1275. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1276. enum pipe pipe, i915_reg_t reg)
  1277. {
  1278. u32 val = I915_READ(reg);
  1279. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1280. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1281. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1282. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1283. && (val & SDVO_PIPE_B_SELECT),
  1284. "IBX PCH hdmi port still using transcoder B\n");
  1285. }
  1286. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1287. enum pipe pipe)
  1288. {
  1289. u32 val;
  1290. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1291. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1292. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1293. val = I915_READ(PCH_ADPA);
  1294. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1295. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1296. pipe_name(pipe));
  1297. val = I915_READ(PCH_LVDS);
  1298. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1299. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1300. pipe_name(pipe));
  1301. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1302. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1303. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1304. }
  1305. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1306. const struct intel_crtc_state *pipe_config)
  1307. {
  1308. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1309. enum pipe pipe = crtc->pipe;
  1310. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1311. POSTING_READ(DPLL(pipe));
  1312. udelay(150);
  1313. if (intel_wait_for_register(dev_priv,
  1314. DPLL(pipe),
  1315. DPLL_LOCK_VLV,
  1316. DPLL_LOCK_VLV,
  1317. 1))
  1318. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1319. }
  1320. static void vlv_enable_pll(struct intel_crtc *crtc,
  1321. const struct intel_crtc_state *pipe_config)
  1322. {
  1323. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1324. enum pipe pipe = crtc->pipe;
  1325. assert_pipe_disabled(dev_priv, pipe);
  1326. /* PLL is protected by panel, make sure we can write it */
  1327. assert_panel_unlocked(dev_priv, pipe);
  1328. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1329. _vlv_enable_pll(crtc, pipe_config);
  1330. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1331. POSTING_READ(DPLL_MD(pipe));
  1332. }
  1333. static void _chv_enable_pll(struct intel_crtc *crtc,
  1334. const struct intel_crtc_state *pipe_config)
  1335. {
  1336. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1337. enum pipe pipe = crtc->pipe;
  1338. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1339. u32 tmp;
  1340. mutex_lock(&dev_priv->sb_lock);
  1341. /* Enable back the 10bit clock to display controller */
  1342. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1343. tmp |= DPIO_DCLKP_EN;
  1344. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1345. mutex_unlock(&dev_priv->sb_lock);
  1346. /*
  1347. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1348. */
  1349. udelay(1);
  1350. /* Enable PLL */
  1351. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1352. /* Check PLL is locked */
  1353. if (intel_wait_for_register(dev_priv,
  1354. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1355. 1))
  1356. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1357. }
  1358. static void chv_enable_pll(struct intel_crtc *crtc,
  1359. const struct intel_crtc_state *pipe_config)
  1360. {
  1361. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1362. enum pipe pipe = crtc->pipe;
  1363. assert_pipe_disabled(dev_priv, pipe);
  1364. /* PLL is protected by panel, make sure we can write it */
  1365. assert_panel_unlocked(dev_priv, pipe);
  1366. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1367. _chv_enable_pll(crtc, pipe_config);
  1368. if (pipe != PIPE_A) {
  1369. /*
  1370. * WaPixelRepeatModeFixForC0:chv
  1371. *
  1372. * DPLLCMD is AWOL. Use chicken bits to propagate
  1373. * the value from DPLLBMD to either pipe B or C.
  1374. */
  1375. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1376. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1377. I915_WRITE(CBR4_VLV, 0);
  1378. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1379. /*
  1380. * DPLLB VGA mode also seems to cause problems.
  1381. * We should always have it disabled.
  1382. */
  1383. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1384. } else {
  1385. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1386. POSTING_READ(DPLL_MD(pipe));
  1387. }
  1388. }
  1389. static int intel_num_dvo_pipes(struct drm_device *dev)
  1390. {
  1391. struct intel_crtc *crtc;
  1392. int count = 0;
  1393. for_each_intel_crtc(dev, crtc) {
  1394. count += crtc->base.state->active &&
  1395. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1396. }
  1397. return count;
  1398. }
  1399. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1400. {
  1401. struct drm_device *dev = crtc->base.dev;
  1402. struct drm_i915_private *dev_priv = to_i915(dev);
  1403. i915_reg_t reg = DPLL(crtc->pipe);
  1404. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1405. assert_pipe_disabled(dev_priv, crtc->pipe);
  1406. /* PLL is protected by panel, make sure we can write it */
  1407. if (IS_MOBILE(dev) && !IS_I830(dev))
  1408. assert_panel_unlocked(dev_priv, crtc->pipe);
  1409. /* Enable DVO 2x clock on both PLLs if necessary */
  1410. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1411. /*
  1412. * It appears to be important that we don't enable this
  1413. * for the current pipe before otherwise configuring the
  1414. * PLL. No idea how this should be handled if multiple
  1415. * DVO outputs are enabled simultaneosly.
  1416. */
  1417. dpll |= DPLL_DVO_2X_MODE;
  1418. I915_WRITE(DPLL(!crtc->pipe),
  1419. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1420. }
  1421. /*
  1422. * Apparently we need to have VGA mode enabled prior to changing
  1423. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1424. * dividers, even though the register value does change.
  1425. */
  1426. I915_WRITE(reg, 0);
  1427. I915_WRITE(reg, dpll);
  1428. /* Wait for the clocks to stabilize. */
  1429. POSTING_READ(reg);
  1430. udelay(150);
  1431. if (INTEL_INFO(dev)->gen >= 4) {
  1432. I915_WRITE(DPLL_MD(crtc->pipe),
  1433. crtc->config->dpll_hw_state.dpll_md);
  1434. } else {
  1435. /* The pixel multiplier can only be updated once the
  1436. * DPLL is enabled and the clocks are stable.
  1437. *
  1438. * So write it again.
  1439. */
  1440. I915_WRITE(reg, dpll);
  1441. }
  1442. /* We do this three times for luck */
  1443. I915_WRITE(reg, dpll);
  1444. POSTING_READ(reg);
  1445. udelay(150); /* wait for warmup */
  1446. I915_WRITE(reg, dpll);
  1447. POSTING_READ(reg);
  1448. udelay(150); /* wait for warmup */
  1449. I915_WRITE(reg, dpll);
  1450. POSTING_READ(reg);
  1451. udelay(150); /* wait for warmup */
  1452. }
  1453. /**
  1454. * i9xx_disable_pll - disable a PLL
  1455. * @dev_priv: i915 private structure
  1456. * @pipe: pipe PLL to disable
  1457. *
  1458. * Disable the PLL for @pipe, making sure the pipe is off first.
  1459. *
  1460. * Note! This is for pre-ILK only.
  1461. */
  1462. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1463. {
  1464. struct drm_device *dev = crtc->base.dev;
  1465. struct drm_i915_private *dev_priv = to_i915(dev);
  1466. enum pipe pipe = crtc->pipe;
  1467. /* Disable DVO 2x clock on both PLLs if necessary */
  1468. if (IS_I830(dev) &&
  1469. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1470. !intel_num_dvo_pipes(dev)) {
  1471. I915_WRITE(DPLL(PIPE_B),
  1472. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1473. I915_WRITE(DPLL(PIPE_A),
  1474. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1475. }
  1476. /* Don't disable pipe or pipe PLLs if needed */
  1477. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1478. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1479. return;
  1480. /* Make sure the pipe isn't still relying on us */
  1481. assert_pipe_disabled(dev_priv, pipe);
  1482. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1483. POSTING_READ(DPLL(pipe));
  1484. }
  1485. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1486. {
  1487. u32 val;
  1488. /* Make sure the pipe isn't still relying on us */
  1489. assert_pipe_disabled(dev_priv, pipe);
  1490. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1491. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1492. if (pipe != PIPE_A)
  1493. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1494. I915_WRITE(DPLL(pipe), val);
  1495. POSTING_READ(DPLL(pipe));
  1496. }
  1497. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1498. {
  1499. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1500. u32 val;
  1501. /* Make sure the pipe isn't still relying on us */
  1502. assert_pipe_disabled(dev_priv, pipe);
  1503. val = DPLL_SSC_REF_CLK_CHV |
  1504. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1505. if (pipe != PIPE_A)
  1506. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1507. I915_WRITE(DPLL(pipe), val);
  1508. POSTING_READ(DPLL(pipe));
  1509. mutex_lock(&dev_priv->sb_lock);
  1510. /* Disable 10bit clock to display controller */
  1511. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1512. val &= ~DPIO_DCLKP_EN;
  1513. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1514. mutex_unlock(&dev_priv->sb_lock);
  1515. }
  1516. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1517. struct intel_digital_port *dport,
  1518. unsigned int expected_mask)
  1519. {
  1520. u32 port_mask;
  1521. i915_reg_t dpll_reg;
  1522. switch (dport->port) {
  1523. case PORT_B:
  1524. port_mask = DPLL_PORTB_READY_MASK;
  1525. dpll_reg = DPLL(0);
  1526. break;
  1527. case PORT_C:
  1528. port_mask = DPLL_PORTC_READY_MASK;
  1529. dpll_reg = DPLL(0);
  1530. expected_mask <<= 4;
  1531. break;
  1532. case PORT_D:
  1533. port_mask = DPLL_PORTD_READY_MASK;
  1534. dpll_reg = DPIO_PHY_STATUS;
  1535. break;
  1536. default:
  1537. BUG();
  1538. }
  1539. if (intel_wait_for_register(dev_priv,
  1540. dpll_reg, port_mask, expected_mask,
  1541. 1000))
  1542. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1543. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1544. }
  1545. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1546. enum pipe pipe)
  1547. {
  1548. struct drm_device *dev = &dev_priv->drm;
  1549. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1550. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1551. i915_reg_t reg;
  1552. uint32_t val, pipeconf_val;
  1553. /* Make sure PCH DPLL is enabled */
  1554. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1555. /* FDI must be feeding us bits for PCH ports */
  1556. assert_fdi_tx_enabled(dev_priv, pipe);
  1557. assert_fdi_rx_enabled(dev_priv, pipe);
  1558. if (HAS_PCH_CPT(dev)) {
  1559. /* Workaround: Set the timing override bit before enabling the
  1560. * pch transcoder. */
  1561. reg = TRANS_CHICKEN2(pipe);
  1562. val = I915_READ(reg);
  1563. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1564. I915_WRITE(reg, val);
  1565. }
  1566. reg = PCH_TRANSCONF(pipe);
  1567. val = I915_READ(reg);
  1568. pipeconf_val = I915_READ(PIPECONF(pipe));
  1569. if (HAS_PCH_IBX(dev_priv)) {
  1570. /*
  1571. * Make the BPC in transcoder be consistent with
  1572. * that in pipeconf reg. For HDMI we must use 8bpc
  1573. * here for both 8bpc and 12bpc.
  1574. */
  1575. val &= ~PIPECONF_BPC_MASK;
  1576. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1577. val |= PIPECONF_8BPC;
  1578. else
  1579. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1580. }
  1581. val &= ~TRANS_INTERLACE_MASK;
  1582. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1583. if (HAS_PCH_IBX(dev_priv) &&
  1584. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1585. val |= TRANS_LEGACY_INTERLACED_ILK;
  1586. else
  1587. val |= TRANS_INTERLACED;
  1588. else
  1589. val |= TRANS_PROGRESSIVE;
  1590. I915_WRITE(reg, val | TRANS_ENABLE);
  1591. if (intel_wait_for_register(dev_priv,
  1592. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1593. 100))
  1594. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1595. }
  1596. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1597. enum transcoder cpu_transcoder)
  1598. {
  1599. u32 val, pipeconf_val;
  1600. /* FDI must be feeding us bits for PCH ports */
  1601. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1602. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1603. /* Workaround: set timing override bit. */
  1604. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1605. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1606. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1607. val = TRANS_ENABLE;
  1608. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1609. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1610. PIPECONF_INTERLACED_ILK)
  1611. val |= TRANS_INTERLACED;
  1612. else
  1613. val |= TRANS_PROGRESSIVE;
  1614. I915_WRITE(LPT_TRANSCONF, val);
  1615. if (intel_wait_for_register(dev_priv,
  1616. LPT_TRANSCONF,
  1617. TRANS_STATE_ENABLE,
  1618. TRANS_STATE_ENABLE,
  1619. 100))
  1620. DRM_ERROR("Failed to enable PCH transcoder\n");
  1621. }
  1622. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1623. enum pipe pipe)
  1624. {
  1625. struct drm_device *dev = &dev_priv->drm;
  1626. i915_reg_t reg;
  1627. uint32_t val;
  1628. /* FDI relies on the transcoder */
  1629. assert_fdi_tx_disabled(dev_priv, pipe);
  1630. assert_fdi_rx_disabled(dev_priv, pipe);
  1631. /* Ports must be off as well */
  1632. assert_pch_ports_disabled(dev_priv, pipe);
  1633. reg = PCH_TRANSCONF(pipe);
  1634. val = I915_READ(reg);
  1635. val &= ~TRANS_ENABLE;
  1636. I915_WRITE(reg, val);
  1637. /* wait for PCH transcoder off, transcoder state */
  1638. if (intel_wait_for_register(dev_priv,
  1639. reg, TRANS_STATE_ENABLE, 0,
  1640. 50))
  1641. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1642. if (HAS_PCH_CPT(dev)) {
  1643. /* Workaround: Clear the timing override chicken bit again. */
  1644. reg = TRANS_CHICKEN2(pipe);
  1645. val = I915_READ(reg);
  1646. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1647. I915_WRITE(reg, val);
  1648. }
  1649. }
  1650. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1651. {
  1652. u32 val;
  1653. val = I915_READ(LPT_TRANSCONF);
  1654. val &= ~TRANS_ENABLE;
  1655. I915_WRITE(LPT_TRANSCONF, val);
  1656. /* wait for PCH transcoder off, transcoder state */
  1657. if (intel_wait_for_register(dev_priv,
  1658. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1659. 50))
  1660. DRM_ERROR("Failed to disable PCH transcoder\n");
  1661. /* Workaround: clear timing override bit. */
  1662. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1663. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1664. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1665. }
  1666. /**
  1667. * intel_enable_pipe - enable a pipe, asserting requirements
  1668. * @crtc: crtc responsible for the pipe
  1669. *
  1670. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1671. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1672. */
  1673. static void intel_enable_pipe(struct intel_crtc *crtc)
  1674. {
  1675. struct drm_device *dev = crtc->base.dev;
  1676. struct drm_i915_private *dev_priv = to_i915(dev);
  1677. enum pipe pipe = crtc->pipe;
  1678. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1679. enum pipe pch_transcoder;
  1680. i915_reg_t reg;
  1681. u32 val;
  1682. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1683. assert_planes_disabled(dev_priv, pipe);
  1684. assert_cursor_disabled(dev_priv, pipe);
  1685. assert_sprites_disabled(dev_priv, pipe);
  1686. if (HAS_PCH_LPT(dev_priv))
  1687. pch_transcoder = TRANSCODER_A;
  1688. else
  1689. pch_transcoder = pipe;
  1690. /*
  1691. * A pipe without a PLL won't actually be able to drive bits from
  1692. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1693. * need the check.
  1694. */
  1695. if (HAS_GMCH_DISPLAY(dev_priv))
  1696. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1697. assert_dsi_pll_enabled(dev_priv);
  1698. else
  1699. assert_pll_enabled(dev_priv, pipe);
  1700. else {
  1701. if (crtc->config->has_pch_encoder) {
  1702. /* if driving the PCH, we need FDI enabled */
  1703. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1704. assert_fdi_tx_pll_enabled(dev_priv,
  1705. (enum pipe) cpu_transcoder);
  1706. }
  1707. /* FIXME: assert CPU port conditions for SNB+ */
  1708. }
  1709. reg = PIPECONF(cpu_transcoder);
  1710. val = I915_READ(reg);
  1711. if (val & PIPECONF_ENABLE) {
  1712. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1713. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1714. return;
  1715. }
  1716. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1717. POSTING_READ(reg);
  1718. /*
  1719. * Until the pipe starts DSL will read as 0, which would cause
  1720. * an apparent vblank timestamp jump, which messes up also the
  1721. * frame count when it's derived from the timestamps. So let's
  1722. * wait for the pipe to start properly before we call
  1723. * drm_crtc_vblank_on()
  1724. */
  1725. if (dev->max_vblank_count == 0 &&
  1726. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1727. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1728. }
  1729. /**
  1730. * intel_disable_pipe - disable a pipe, asserting requirements
  1731. * @crtc: crtc whose pipes is to be disabled
  1732. *
  1733. * Disable the pipe of @crtc, making sure that various hardware
  1734. * specific requirements are met, if applicable, e.g. plane
  1735. * disabled, panel fitter off, etc.
  1736. *
  1737. * Will wait until the pipe has shut down before returning.
  1738. */
  1739. static void intel_disable_pipe(struct intel_crtc *crtc)
  1740. {
  1741. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1742. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1743. enum pipe pipe = crtc->pipe;
  1744. i915_reg_t reg;
  1745. u32 val;
  1746. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1747. /*
  1748. * Make sure planes won't keep trying to pump pixels to us,
  1749. * or we might hang the display.
  1750. */
  1751. assert_planes_disabled(dev_priv, pipe);
  1752. assert_cursor_disabled(dev_priv, pipe);
  1753. assert_sprites_disabled(dev_priv, pipe);
  1754. reg = PIPECONF(cpu_transcoder);
  1755. val = I915_READ(reg);
  1756. if ((val & PIPECONF_ENABLE) == 0)
  1757. return;
  1758. /*
  1759. * Double wide has implications for planes
  1760. * so best keep it disabled when not needed.
  1761. */
  1762. if (crtc->config->double_wide)
  1763. val &= ~PIPECONF_DOUBLE_WIDE;
  1764. /* Don't disable pipe or pipe PLLs if needed */
  1765. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1766. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1767. val &= ~PIPECONF_ENABLE;
  1768. I915_WRITE(reg, val);
  1769. if ((val & PIPECONF_ENABLE) == 0)
  1770. intel_wait_for_pipe_off(crtc);
  1771. }
  1772. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1773. {
  1774. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1775. }
  1776. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1777. uint64_t fb_modifier, unsigned int cpp)
  1778. {
  1779. switch (fb_modifier) {
  1780. case DRM_FORMAT_MOD_NONE:
  1781. return cpp;
  1782. case I915_FORMAT_MOD_X_TILED:
  1783. if (IS_GEN2(dev_priv))
  1784. return 128;
  1785. else
  1786. return 512;
  1787. case I915_FORMAT_MOD_Y_TILED:
  1788. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1789. return 128;
  1790. else
  1791. return 512;
  1792. case I915_FORMAT_MOD_Yf_TILED:
  1793. switch (cpp) {
  1794. case 1:
  1795. return 64;
  1796. case 2:
  1797. case 4:
  1798. return 128;
  1799. case 8:
  1800. case 16:
  1801. return 256;
  1802. default:
  1803. MISSING_CASE(cpp);
  1804. return cpp;
  1805. }
  1806. break;
  1807. default:
  1808. MISSING_CASE(fb_modifier);
  1809. return cpp;
  1810. }
  1811. }
  1812. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1813. uint64_t fb_modifier, unsigned int cpp)
  1814. {
  1815. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1816. return 1;
  1817. else
  1818. return intel_tile_size(dev_priv) /
  1819. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1820. }
  1821. /* Return the tile dimensions in pixel units */
  1822. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1823. unsigned int *tile_width,
  1824. unsigned int *tile_height,
  1825. uint64_t fb_modifier,
  1826. unsigned int cpp)
  1827. {
  1828. unsigned int tile_width_bytes =
  1829. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1830. *tile_width = tile_width_bytes / cpp;
  1831. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1832. }
  1833. unsigned int
  1834. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1835. uint32_t pixel_format, uint64_t fb_modifier)
  1836. {
  1837. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1838. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1839. return ALIGN(height, tile_height);
  1840. }
  1841. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1842. {
  1843. unsigned int size = 0;
  1844. int i;
  1845. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1846. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1847. return size;
  1848. }
  1849. static void
  1850. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1851. const struct drm_framebuffer *fb,
  1852. unsigned int rotation)
  1853. {
  1854. if (intel_rotation_90_or_270(rotation)) {
  1855. *view = i915_ggtt_view_rotated;
  1856. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1857. } else {
  1858. *view = i915_ggtt_view_normal;
  1859. }
  1860. }
  1861. static void
  1862. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  1863. struct drm_framebuffer *fb)
  1864. {
  1865. struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
  1866. unsigned int tile_size, tile_width, tile_height, cpp;
  1867. tile_size = intel_tile_size(dev_priv);
  1868. cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  1869. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1870. fb->modifier[0], cpp);
  1871. info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
  1872. info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
  1873. if (info->pixel_format == DRM_FORMAT_NV12) {
  1874. cpp = drm_format_plane_cpp(fb->pixel_format, 1);
  1875. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1876. fb->modifier[1], cpp);
  1877. info->uv_offset = fb->offsets[1];
  1878. info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
  1879. info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
  1880. }
  1881. }
  1882. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1883. {
  1884. if (INTEL_INFO(dev_priv)->gen >= 9)
  1885. return 256 * 1024;
  1886. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1887. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1888. return 128 * 1024;
  1889. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1890. return 4 * 1024;
  1891. else
  1892. return 0;
  1893. }
  1894. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1895. uint64_t fb_modifier)
  1896. {
  1897. switch (fb_modifier) {
  1898. case DRM_FORMAT_MOD_NONE:
  1899. return intel_linear_alignment(dev_priv);
  1900. case I915_FORMAT_MOD_X_TILED:
  1901. if (INTEL_INFO(dev_priv)->gen >= 9)
  1902. return 256 * 1024;
  1903. return 0;
  1904. case I915_FORMAT_MOD_Y_TILED:
  1905. case I915_FORMAT_MOD_Yf_TILED:
  1906. return 1 * 1024 * 1024;
  1907. default:
  1908. MISSING_CASE(fb_modifier);
  1909. return 0;
  1910. }
  1911. }
  1912. int
  1913. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1914. unsigned int rotation)
  1915. {
  1916. struct drm_device *dev = fb->dev;
  1917. struct drm_i915_private *dev_priv = to_i915(dev);
  1918. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1919. struct i915_ggtt_view view;
  1920. u32 alignment;
  1921. int ret;
  1922. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1923. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  1924. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1925. /* Note that the w/a also requires 64 PTE of padding following the
  1926. * bo. We currently fill all unused PTE with the shadow page and so
  1927. * we should always have valid PTE following the scanout preventing
  1928. * the VT-d warning.
  1929. */
  1930. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1931. alignment = 256 * 1024;
  1932. /*
  1933. * Global gtt pte registers are special registers which actually forward
  1934. * writes to a chunk of system memory. Which means that there is no risk
  1935. * that the register values disappear as soon as we call
  1936. * intel_runtime_pm_put(), so it is correct to wrap only the
  1937. * pin/unpin/fence and not more.
  1938. */
  1939. intel_runtime_pm_get(dev_priv);
  1940. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  1941. &view);
  1942. if (ret)
  1943. goto err_pm;
  1944. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1945. * fence, whereas 965+ only requires a fence if using
  1946. * framebuffer compression. For simplicity, we always install
  1947. * a fence as the cost is not that onerous.
  1948. */
  1949. if (view.type == I915_GGTT_VIEW_NORMAL) {
  1950. ret = i915_gem_object_get_fence(obj);
  1951. if (ret == -EDEADLK) {
  1952. /*
  1953. * -EDEADLK means there are no free fences
  1954. * no pending flips.
  1955. *
  1956. * This is propagated to atomic, but it uses
  1957. * -EDEADLK to force a locking recovery, so
  1958. * change the returned error to -EBUSY.
  1959. */
  1960. ret = -EBUSY;
  1961. goto err_unpin;
  1962. } else if (ret)
  1963. goto err_unpin;
  1964. i915_gem_object_pin_fence(obj);
  1965. }
  1966. intel_runtime_pm_put(dev_priv);
  1967. return 0;
  1968. err_unpin:
  1969. i915_gem_object_unpin_from_display_plane(obj, &view);
  1970. err_pm:
  1971. intel_runtime_pm_put(dev_priv);
  1972. return ret;
  1973. }
  1974. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1975. {
  1976. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1977. struct i915_ggtt_view view;
  1978. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1979. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1980. if (view.type == I915_GGTT_VIEW_NORMAL)
  1981. i915_gem_object_unpin_fence(obj);
  1982. i915_gem_object_unpin_from_display_plane(obj, &view);
  1983. }
  1984. /*
  1985. * Adjust the tile offset by moving the difference into
  1986. * the x/y offsets.
  1987. *
  1988. * Input tile dimensions and pitch must already be
  1989. * rotated to match x and y, and in pixel units.
  1990. */
  1991. static u32 intel_adjust_tile_offset(int *x, int *y,
  1992. unsigned int tile_width,
  1993. unsigned int tile_height,
  1994. unsigned int tile_size,
  1995. unsigned int pitch_tiles,
  1996. u32 old_offset,
  1997. u32 new_offset)
  1998. {
  1999. unsigned int tiles;
  2000. WARN_ON(old_offset & (tile_size - 1));
  2001. WARN_ON(new_offset & (tile_size - 1));
  2002. WARN_ON(new_offset > old_offset);
  2003. tiles = (old_offset - new_offset) / tile_size;
  2004. *y += tiles / pitch_tiles * tile_height;
  2005. *x += tiles % pitch_tiles * tile_width;
  2006. return new_offset;
  2007. }
  2008. /*
  2009. * Computes the linear offset to the base tile and adjusts
  2010. * x, y. bytes per pixel is assumed to be a power-of-two.
  2011. *
  2012. * In the 90/270 rotated case, x and y are assumed
  2013. * to be already rotated to match the rotated GTT view, and
  2014. * pitch is the tile_height aligned framebuffer height.
  2015. */
  2016. u32 intel_compute_tile_offset(int *x, int *y,
  2017. const struct drm_framebuffer *fb, int plane,
  2018. unsigned int pitch,
  2019. unsigned int rotation)
  2020. {
  2021. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2022. uint64_t fb_modifier = fb->modifier[plane];
  2023. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2024. u32 offset, offset_aligned, alignment;
  2025. alignment = intel_surf_alignment(dev_priv, fb_modifier);
  2026. if (alignment)
  2027. alignment--;
  2028. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2029. unsigned int tile_size, tile_width, tile_height;
  2030. unsigned int tile_rows, tiles, pitch_tiles;
  2031. tile_size = intel_tile_size(dev_priv);
  2032. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2033. fb_modifier, cpp);
  2034. if (intel_rotation_90_or_270(rotation)) {
  2035. pitch_tiles = pitch / tile_height;
  2036. swap(tile_width, tile_height);
  2037. } else {
  2038. pitch_tiles = pitch / (tile_width * cpp);
  2039. }
  2040. tile_rows = *y / tile_height;
  2041. *y %= tile_height;
  2042. tiles = *x / tile_width;
  2043. *x %= tile_width;
  2044. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2045. offset_aligned = offset & ~alignment;
  2046. intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2047. tile_size, pitch_tiles,
  2048. offset, offset_aligned);
  2049. } else {
  2050. offset = *y * pitch + *x * cpp;
  2051. offset_aligned = offset & ~alignment;
  2052. *y = (offset & alignment) / pitch;
  2053. *x = ((offset & alignment) - *y * pitch) / cpp;
  2054. }
  2055. return offset_aligned;
  2056. }
  2057. static int i9xx_format_to_fourcc(int format)
  2058. {
  2059. switch (format) {
  2060. case DISPPLANE_8BPP:
  2061. return DRM_FORMAT_C8;
  2062. case DISPPLANE_BGRX555:
  2063. return DRM_FORMAT_XRGB1555;
  2064. case DISPPLANE_BGRX565:
  2065. return DRM_FORMAT_RGB565;
  2066. default:
  2067. case DISPPLANE_BGRX888:
  2068. return DRM_FORMAT_XRGB8888;
  2069. case DISPPLANE_RGBX888:
  2070. return DRM_FORMAT_XBGR8888;
  2071. case DISPPLANE_BGRX101010:
  2072. return DRM_FORMAT_XRGB2101010;
  2073. case DISPPLANE_RGBX101010:
  2074. return DRM_FORMAT_XBGR2101010;
  2075. }
  2076. }
  2077. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2078. {
  2079. switch (format) {
  2080. case PLANE_CTL_FORMAT_RGB_565:
  2081. return DRM_FORMAT_RGB565;
  2082. default:
  2083. case PLANE_CTL_FORMAT_XRGB_8888:
  2084. if (rgb_order) {
  2085. if (alpha)
  2086. return DRM_FORMAT_ABGR8888;
  2087. else
  2088. return DRM_FORMAT_XBGR8888;
  2089. } else {
  2090. if (alpha)
  2091. return DRM_FORMAT_ARGB8888;
  2092. else
  2093. return DRM_FORMAT_XRGB8888;
  2094. }
  2095. case PLANE_CTL_FORMAT_XRGB_2101010:
  2096. if (rgb_order)
  2097. return DRM_FORMAT_XBGR2101010;
  2098. else
  2099. return DRM_FORMAT_XRGB2101010;
  2100. }
  2101. }
  2102. static bool
  2103. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2104. struct intel_initial_plane_config *plane_config)
  2105. {
  2106. struct drm_device *dev = crtc->base.dev;
  2107. struct drm_i915_private *dev_priv = to_i915(dev);
  2108. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2109. struct drm_i915_gem_object *obj = NULL;
  2110. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2111. struct drm_framebuffer *fb = &plane_config->fb->base;
  2112. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2113. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2114. PAGE_SIZE);
  2115. size_aligned -= base_aligned;
  2116. if (plane_config->size == 0)
  2117. return false;
  2118. /* If the FB is too big, just don't use it since fbdev is not very
  2119. * important and we should probably use that space with FBC or other
  2120. * features. */
  2121. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2122. return false;
  2123. mutex_lock(&dev->struct_mutex);
  2124. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2125. base_aligned,
  2126. base_aligned,
  2127. size_aligned);
  2128. if (!obj) {
  2129. mutex_unlock(&dev->struct_mutex);
  2130. return false;
  2131. }
  2132. if (plane_config->tiling == I915_TILING_X)
  2133. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2134. mode_cmd.pixel_format = fb->pixel_format;
  2135. mode_cmd.width = fb->width;
  2136. mode_cmd.height = fb->height;
  2137. mode_cmd.pitches[0] = fb->pitches[0];
  2138. mode_cmd.modifier[0] = fb->modifier[0];
  2139. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2140. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2141. &mode_cmd, obj)) {
  2142. DRM_DEBUG_KMS("intel fb init failed\n");
  2143. goto out_unref_obj;
  2144. }
  2145. mutex_unlock(&dev->struct_mutex);
  2146. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2147. return true;
  2148. out_unref_obj:
  2149. i915_gem_object_put(obj);
  2150. mutex_unlock(&dev->struct_mutex);
  2151. return false;
  2152. }
  2153. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2154. static void
  2155. update_state_fb(struct drm_plane *plane)
  2156. {
  2157. if (plane->fb == plane->state->fb)
  2158. return;
  2159. if (plane->state->fb)
  2160. drm_framebuffer_unreference(plane->state->fb);
  2161. plane->state->fb = plane->fb;
  2162. if (plane->state->fb)
  2163. drm_framebuffer_reference(plane->state->fb);
  2164. }
  2165. static void
  2166. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2167. struct intel_initial_plane_config *plane_config)
  2168. {
  2169. struct drm_device *dev = intel_crtc->base.dev;
  2170. struct drm_i915_private *dev_priv = to_i915(dev);
  2171. struct drm_crtc *c;
  2172. struct intel_crtc *i;
  2173. struct drm_i915_gem_object *obj;
  2174. struct drm_plane *primary = intel_crtc->base.primary;
  2175. struct drm_plane_state *plane_state = primary->state;
  2176. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2177. struct intel_plane *intel_plane = to_intel_plane(primary);
  2178. struct intel_plane_state *intel_state =
  2179. to_intel_plane_state(plane_state);
  2180. struct drm_framebuffer *fb;
  2181. if (!plane_config->fb)
  2182. return;
  2183. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2184. fb = &plane_config->fb->base;
  2185. goto valid_fb;
  2186. }
  2187. kfree(plane_config->fb);
  2188. /*
  2189. * Failed to alloc the obj, check to see if we should share
  2190. * an fb with another CRTC instead
  2191. */
  2192. for_each_crtc(dev, c) {
  2193. i = to_intel_crtc(c);
  2194. if (c == &intel_crtc->base)
  2195. continue;
  2196. if (!i->active)
  2197. continue;
  2198. fb = c->primary->fb;
  2199. if (!fb)
  2200. continue;
  2201. obj = intel_fb_obj(fb);
  2202. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2203. drm_framebuffer_reference(fb);
  2204. goto valid_fb;
  2205. }
  2206. }
  2207. /*
  2208. * We've failed to reconstruct the BIOS FB. Current display state
  2209. * indicates that the primary plane is visible, but has a NULL FB,
  2210. * which will lead to problems later if we don't fix it up. The
  2211. * simplest solution is to just disable the primary plane now and
  2212. * pretend the BIOS never had it enabled.
  2213. */
  2214. to_intel_plane_state(plane_state)->visible = false;
  2215. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2216. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2217. intel_plane->disable_plane(primary, &intel_crtc->base);
  2218. return;
  2219. valid_fb:
  2220. plane_state->src_x = 0;
  2221. plane_state->src_y = 0;
  2222. plane_state->src_w = fb->width << 16;
  2223. plane_state->src_h = fb->height << 16;
  2224. plane_state->crtc_x = 0;
  2225. plane_state->crtc_y = 0;
  2226. plane_state->crtc_w = fb->width;
  2227. plane_state->crtc_h = fb->height;
  2228. intel_state->src.x1 = plane_state->src_x;
  2229. intel_state->src.y1 = plane_state->src_y;
  2230. intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
  2231. intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
  2232. intel_state->dst.x1 = plane_state->crtc_x;
  2233. intel_state->dst.y1 = plane_state->crtc_y;
  2234. intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2235. intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2236. obj = intel_fb_obj(fb);
  2237. if (i915_gem_object_is_tiled(obj))
  2238. dev_priv->preserve_bios_swizzle = true;
  2239. drm_framebuffer_reference(fb);
  2240. primary->fb = primary->state->fb = fb;
  2241. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2242. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2243. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2244. &obj->frontbuffer_bits);
  2245. }
  2246. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2247. const struct intel_crtc_state *crtc_state,
  2248. const struct intel_plane_state *plane_state)
  2249. {
  2250. struct drm_device *dev = primary->dev;
  2251. struct drm_i915_private *dev_priv = to_i915(dev);
  2252. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2253. struct drm_framebuffer *fb = plane_state->base.fb;
  2254. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2255. int plane = intel_crtc->plane;
  2256. u32 linear_offset;
  2257. u32 dspcntr;
  2258. i915_reg_t reg = DSPCNTR(plane);
  2259. unsigned int rotation = plane_state->base.rotation;
  2260. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2261. int x = plane_state->src.x1 >> 16;
  2262. int y = plane_state->src.y1 >> 16;
  2263. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2264. dspcntr |= DISPLAY_PLANE_ENABLE;
  2265. if (INTEL_INFO(dev)->gen < 4) {
  2266. if (intel_crtc->pipe == PIPE_B)
  2267. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2268. /* pipesrc and dspsize control the size that is scaled from,
  2269. * which should always be the user's requested size.
  2270. */
  2271. I915_WRITE(DSPSIZE(plane),
  2272. ((crtc_state->pipe_src_h - 1) << 16) |
  2273. (crtc_state->pipe_src_w - 1));
  2274. I915_WRITE(DSPPOS(plane), 0);
  2275. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2276. I915_WRITE(PRIMSIZE(plane),
  2277. ((crtc_state->pipe_src_h - 1) << 16) |
  2278. (crtc_state->pipe_src_w - 1));
  2279. I915_WRITE(PRIMPOS(plane), 0);
  2280. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2281. }
  2282. switch (fb->pixel_format) {
  2283. case DRM_FORMAT_C8:
  2284. dspcntr |= DISPPLANE_8BPP;
  2285. break;
  2286. case DRM_FORMAT_XRGB1555:
  2287. dspcntr |= DISPPLANE_BGRX555;
  2288. break;
  2289. case DRM_FORMAT_RGB565:
  2290. dspcntr |= DISPPLANE_BGRX565;
  2291. break;
  2292. case DRM_FORMAT_XRGB8888:
  2293. dspcntr |= DISPPLANE_BGRX888;
  2294. break;
  2295. case DRM_FORMAT_XBGR8888:
  2296. dspcntr |= DISPPLANE_RGBX888;
  2297. break;
  2298. case DRM_FORMAT_XRGB2101010:
  2299. dspcntr |= DISPPLANE_BGRX101010;
  2300. break;
  2301. case DRM_FORMAT_XBGR2101010:
  2302. dspcntr |= DISPPLANE_RGBX101010;
  2303. break;
  2304. default:
  2305. BUG();
  2306. }
  2307. if (INTEL_INFO(dev)->gen >= 4 && i915_gem_object_is_tiled(obj))
  2308. dspcntr |= DISPPLANE_TILED;
  2309. if (IS_G4X(dev))
  2310. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2311. linear_offset = y * fb->pitches[0] + x * cpp;
  2312. if (INTEL_INFO(dev)->gen >= 4) {
  2313. intel_crtc->dspaddr_offset =
  2314. intel_compute_tile_offset(&x, &y, fb, 0,
  2315. fb->pitches[0], rotation);
  2316. linear_offset -= intel_crtc->dspaddr_offset;
  2317. } else {
  2318. intel_crtc->dspaddr_offset = linear_offset;
  2319. }
  2320. if (rotation == BIT(DRM_ROTATE_180)) {
  2321. dspcntr |= DISPPLANE_ROTATE_180;
  2322. x += (crtc_state->pipe_src_w - 1);
  2323. y += (crtc_state->pipe_src_h - 1);
  2324. /* Finding the last pixel of the last line of the display
  2325. data and adding to linear_offset*/
  2326. linear_offset +=
  2327. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2328. (crtc_state->pipe_src_w - 1) * cpp;
  2329. }
  2330. intel_crtc->adjusted_x = x;
  2331. intel_crtc->adjusted_y = y;
  2332. I915_WRITE(reg, dspcntr);
  2333. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2334. if (INTEL_INFO(dev)->gen >= 4) {
  2335. I915_WRITE(DSPSURF(plane),
  2336. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2337. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2338. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2339. } else
  2340. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2341. POSTING_READ(reg);
  2342. }
  2343. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2344. struct drm_crtc *crtc)
  2345. {
  2346. struct drm_device *dev = crtc->dev;
  2347. struct drm_i915_private *dev_priv = to_i915(dev);
  2348. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2349. int plane = intel_crtc->plane;
  2350. I915_WRITE(DSPCNTR(plane), 0);
  2351. if (INTEL_INFO(dev_priv)->gen >= 4)
  2352. I915_WRITE(DSPSURF(plane), 0);
  2353. else
  2354. I915_WRITE(DSPADDR(plane), 0);
  2355. POSTING_READ(DSPCNTR(plane));
  2356. }
  2357. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2358. const struct intel_crtc_state *crtc_state,
  2359. const struct intel_plane_state *plane_state)
  2360. {
  2361. struct drm_device *dev = primary->dev;
  2362. struct drm_i915_private *dev_priv = to_i915(dev);
  2363. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2364. struct drm_framebuffer *fb = plane_state->base.fb;
  2365. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2366. int plane = intel_crtc->plane;
  2367. u32 linear_offset;
  2368. u32 dspcntr;
  2369. i915_reg_t reg = DSPCNTR(plane);
  2370. unsigned int rotation = plane_state->base.rotation;
  2371. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2372. int x = plane_state->src.x1 >> 16;
  2373. int y = plane_state->src.y1 >> 16;
  2374. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2375. dspcntr |= DISPLAY_PLANE_ENABLE;
  2376. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2377. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2378. switch (fb->pixel_format) {
  2379. case DRM_FORMAT_C8:
  2380. dspcntr |= DISPPLANE_8BPP;
  2381. break;
  2382. case DRM_FORMAT_RGB565:
  2383. dspcntr |= DISPPLANE_BGRX565;
  2384. break;
  2385. case DRM_FORMAT_XRGB8888:
  2386. dspcntr |= DISPPLANE_BGRX888;
  2387. break;
  2388. case DRM_FORMAT_XBGR8888:
  2389. dspcntr |= DISPPLANE_RGBX888;
  2390. break;
  2391. case DRM_FORMAT_XRGB2101010:
  2392. dspcntr |= DISPPLANE_BGRX101010;
  2393. break;
  2394. case DRM_FORMAT_XBGR2101010:
  2395. dspcntr |= DISPPLANE_RGBX101010;
  2396. break;
  2397. default:
  2398. BUG();
  2399. }
  2400. if (i915_gem_object_is_tiled(obj))
  2401. dspcntr |= DISPPLANE_TILED;
  2402. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2403. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2404. linear_offset = y * fb->pitches[0] + x * cpp;
  2405. intel_crtc->dspaddr_offset =
  2406. intel_compute_tile_offset(&x, &y, fb, 0,
  2407. fb->pitches[0], rotation);
  2408. linear_offset -= intel_crtc->dspaddr_offset;
  2409. if (rotation == BIT(DRM_ROTATE_180)) {
  2410. dspcntr |= DISPPLANE_ROTATE_180;
  2411. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2412. x += (crtc_state->pipe_src_w - 1);
  2413. y += (crtc_state->pipe_src_h - 1);
  2414. /* Finding the last pixel of the last line of the display
  2415. data and adding to linear_offset*/
  2416. linear_offset +=
  2417. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2418. (crtc_state->pipe_src_w - 1) * cpp;
  2419. }
  2420. }
  2421. intel_crtc->adjusted_x = x;
  2422. intel_crtc->adjusted_y = y;
  2423. I915_WRITE(reg, dspcntr);
  2424. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2425. I915_WRITE(DSPSURF(plane),
  2426. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2427. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2428. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2429. } else {
  2430. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2431. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2432. }
  2433. POSTING_READ(reg);
  2434. }
  2435. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2436. uint64_t fb_modifier, uint32_t pixel_format)
  2437. {
  2438. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2439. return 64;
  2440. } else {
  2441. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2442. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2443. }
  2444. }
  2445. u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
  2446. struct drm_i915_gem_object *obj,
  2447. unsigned int plane)
  2448. {
  2449. struct i915_ggtt_view view;
  2450. struct i915_vma *vma;
  2451. u64 offset;
  2452. intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
  2453. intel_plane->base.state->rotation);
  2454. vma = i915_gem_obj_to_ggtt_view(obj, &view);
  2455. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2456. view.type))
  2457. return -1;
  2458. offset = vma->node.start;
  2459. if (plane == 1) {
  2460. offset += vma->ggtt_view.params.rotated.uv_start_page *
  2461. PAGE_SIZE;
  2462. }
  2463. WARN_ON(upper_32_bits(offset));
  2464. return lower_32_bits(offset);
  2465. }
  2466. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2467. {
  2468. struct drm_device *dev = intel_crtc->base.dev;
  2469. struct drm_i915_private *dev_priv = to_i915(dev);
  2470. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2471. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2472. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2473. }
  2474. /*
  2475. * This function detaches (aka. unbinds) unused scalers in hardware
  2476. */
  2477. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2478. {
  2479. struct intel_crtc_scaler_state *scaler_state;
  2480. int i;
  2481. scaler_state = &intel_crtc->config->scaler_state;
  2482. /* loop through and disable scalers that aren't in use */
  2483. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2484. if (!scaler_state->scalers[i].in_use)
  2485. skl_detach_scaler(intel_crtc, i);
  2486. }
  2487. }
  2488. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2489. {
  2490. switch (pixel_format) {
  2491. case DRM_FORMAT_C8:
  2492. return PLANE_CTL_FORMAT_INDEXED;
  2493. case DRM_FORMAT_RGB565:
  2494. return PLANE_CTL_FORMAT_RGB_565;
  2495. case DRM_FORMAT_XBGR8888:
  2496. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2497. case DRM_FORMAT_XRGB8888:
  2498. return PLANE_CTL_FORMAT_XRGB_8888;
  2499. /*
  2500. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2501. * to be already pre-multiplied. We need to add a knob (or a different
  2502. * DRM_FORMAT) for user-space to configure that.
  2503. */
  2504. case DRM_FORMAT_ABGR8888:
  2505. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2506. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2507. case DRM_FORMAT_ARGB8888:
  2508. return PLANE_CTL_FORMAT_XRGB_8888 |
  2509. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2510. case DRM_FORMAT_XRGB2101010:
  2511. return PLANE_CTL_FORMAT_XRGB_2101010;
  2512. case DRM_FORMAT_XBGR2101010:
  2513. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2514. case DRM_FORMAT_YUYV:
  2515. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2516. case DRM_FORMAT_YVYU:
  2517. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2518. case DRM_FORMAT_UYVY:
  2519. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2520. case DRM_FORMAT_VYUY:
  2521. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2522. default:
  2523. MISSING_CASE(pixel_format);
  2524. }
  2525. return 0;
  2526. }
  2527. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2528. {
  2529. switch (fb_modifier) {
  2530. case DRM_FORMAT_MOD_NONE:
  2531. break;
  2532. case I915_FORMAT_MOD_X_TILED:
  2533. return PLANE_CTL_TILED_X;
  2534. case I915_FORMAT_MOD_Y_TILED:
  2535. return PLANE_CTL_TILED_Y;
  2536. case I915_FORMAT_MOD_Yf_TILED:
  2537. return PLANE_CTL_TILED_YF;
  2538. default:
  2539. MISSING_CASE(fb_modifier);
  2540. }
  2541. return 0;
  2542. }
  2543. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2544. {
  2545. switch (rotation) {
  2546. case BIT(DRM_ROTATE_0):
  2547. break;
  2548. /*
  2549. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2550. * while i915 HW rotation is clockwise, thats why this swapping.
  2551. */
  2552. case BIT(DRM_ROTATE_90):
  2553. return PLANE_CTL_ROTATE_270;
  2554. case BIT(DRM_ROTATE_180):
  2555. return PLANE_CTL_ROTATE_180;
  2556. case BIT(DRM_ROTATE_270):
  2557. return PLANE_CTL_ROTATE_90;
  2558. default:
  2559. MISSING_CASE(rotation);
  2560. }
  2561. return 0;
  2562. }
  2563. static void skylake_update_primary_plane(struct drm_plane *plane,
  2564. const struct intel_crtc_state *crtc_state,
  2565. const struct intel_plane_state *plane_state)
  2566. {
  2567. struct drm_device *dev = plane->dev;
  2568. struct drm_i915_private *dev_priv = to_i915(dev);
  2569. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2570. struct drm_framebuffer *fb = plane_state->base.fb;
  2571. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2572. int pipe = intel_crtc->pipe;
  2573. u32 plane_ctl, stride_div, stride;
  2574. u32 tile_height, plane_offset, plane_size;
  2575. unsigned int rotation = plane_state->base.rotation;
  2576. int x_offset, y_offset;
  2577. u32 surf_addr;
  2578. int scaler_id = plane_state->scaler_id;
  2579. int src_x = plane_state->src.x1 >> 16;
  2580. int src_y = plane_state->src.y1 >> 16;
  2581. int src_w = drm_rect_width(&plane_state->src) >> 16;
  2582. int src_h = drm_rect_height(&plane_state->src) >> 16;
  2583. int dst_x = plane_state->dst.x1;
  2584. int dst_y = plane_state->dst.y1;
  2585. int dst_w = drm_rect_width(&plane_state->dst);
  2586. int dst_h = drm_rect_height(&plane_state->dst);
  2587. plane_ctl = PLANE_CTL_ENABLE |
  2588. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2589. PLANE_CTL_PIPE_CSC_ENABLE;
  2590. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2591. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2592. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2593. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2594. stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2595. fb->pixel_format);
  2596. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2597. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2598. if (intel_rotation_90_or_270(rotation)) {
  2599. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2600. /* stride = Surface height in tiles */
  2601. tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2602. stride = DIV_ROUND_UP(fb->height, tile_height);
  2603. x_offset = stride * tile_height - src_y - src_h;
  2604. y_offset = src_x;
  2605. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2606. } else {
  2607. stride = fb->pitches[0] / stride_div;
  2608. x_offset = src_x;
  2609. y_offset = src_y;
  2610. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2611. }
  2612. plane_offset = y_offset << 16 | x_offset;
  2613. intel_crtc->adjusted_x = x_offset;
  2614. intel_crtc->adjusted_y = y_offset;
  2615. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2616. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2617. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2618. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2619. if (scaler_id >= 0) {
  2620. uint32_t ps_ctrl = 0;
  2621. WARN_ON(!dst_w || !dst_h);
  2622. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2623. crtc_state->scaler_state.scalers[scaler_id].mode;
  2624. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2625. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2626. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2627. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2628. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2629. } else {
  2630. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2631. }
  2632. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2633. POSTING_READ(PLANE_SURF(pipe, 0));
  2634. }
  2635. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2636. struct drm_crtc *crtc)
  2637. {
  2638. struct drm_device *dev = crtc->dev;
  2639. struct drm_i915_private *dev_priv = to_i915(dev);
  2640. int pipe = to_intel_crtc(crtc)->pipe;
  2641. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2642. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2643. POSTING_READ(PLANE_SURF(pipe, 0));
  2644. }
  2645. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2646. static int
  2647. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2648. int x, int y, enum mode_set_atomic state)
  2649. {
  2650. /* Support for kgdboc is disabled, this needs a major rework. */
  2651. DRM_ERROR("legacy panic handler not supported any more.\n");
  2652. return -ENODEV;
  2653. }
  2654. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2655. {
  2656. struct intel_crtc *crtc;
  2657. for_each_intel_crtc(&dev_priv->drm, crtc)
  2658. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2659. }
  2660. static void intel_update_primary_planes(struct drm_device *dev)
  2661. {
  2662. struct drm_crtc *crtc;
  2663. for_each_crtc(dev, crtc) {
  2664. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2665. struct intel_plane_state *plane_state;
  2666. drm_modeset_lock_crtc(crtc, &plane->base);
  2667. plane_state = to_intel_plane_state(plane->base.state);
  2668. if (plane_state->visible)
  2669. plane->update_plane(&plane->base,
  2670. to_intel_crtc_state(crtc->state),
  2671. plane_state);
  2672. drm_modeset_unlock_crtc(crtc);
  2673. }
  2674. }
  2675. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  2676. {
  2677. /* no reset support for gen2 */
  2678. if (IS_GEN2(dev_priv))
  2679. return;
  2680. /* reset doesn't touch the display */
  2681. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  2682. return;
  2683. drm_modeset_lock_all(&dev_priv->drm);
  2684. /*
  2685. * Disabling the crtcs gracefully seems nicer. Also the
  2686. * g33 docs say we should at least disable all the planes.
  2687. */
  2688. intel_display_suspend(&dev_priv->drm);
  2689. }
  2690. void intel_finish_reset(struct drm_i915_private *dev_priv)
  2691. {
  2692. /*
  2693. * Flips in the rings will be nuked by the reset,
  2694. * so complete all pending flips so that user space
  2695. * will get its events and not get stuck.
  2696. */
  2697. intel_complete_page_flips(dev_priv);
  2698. /* no reset support for gen2 */
  2699. if (IS_GEN2(dev_priv))
  2700. return;
  2701. /* reset doesn't touch the display */
  2702. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
  2703. /*
  2704. * Flips in the rings have been nuked by the reset,
  2705. * so update the base address of all primary
  2706. * planes to the the last fb to make sure we're
  2707. * showing the correct fb after a reset.
  2708. *
  2709. * FIXME: Atomic will make this obsolete since we won't schedule
  2710. * CS-based flips (which might get lost in gpu resets) any more.
  2711. */
  2712. intel_update_primary_planes(&dev_priv->drm);
  2713. return;
  2714. }
  2715. /*
  2716. * The display has been reset as well,
  2717. * so need a full re-initialization.
  2718. */
  2719. intel_runtime_pm_disable_interrupts(dev_priv);
  2720. intel_runtime_pm_enable_interrupts(dev_priv);
  2721. intel_modeset_init_hw(&dev_priv->drm);
  2722. spin_lock_irq(&dev_priv->irq_lock);
  2723. if (dev_priv->display.hpd_irq_setup)
  2724. dev_priv->display.hpd_irq_setup(dev_priv);
  2725. spin_unlock_irq(&dev_priv->irq_lock);
  2726. intel_display_resume(&dev_priv->drm);
  2727. intel_hpd_init(dev_priv);
  2728. drm_modeset_unlock_all(&dev_priv->drm);
  2729. }
  2730. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2731. {
  2732. struct drm_device *dev = crtc->dev;
  2733. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2734. unsigned reset_counter;
  2735. bool pending;
  2736. reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
  2737. if (intel_crtc->reset_counter != reset_counter)
  2738. return false;
  2739. spin_lock_irq(&dev->event_lock);
  2740. pending = to_intel_crtc(crtc)->flip_work != NULL;
  2741. spin_unlock_irq(&dev->event_lock);
  2742. return pending;
  2743. }
  2744. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2745. struct intel_crtc_state *old_crtc_state)
  2746. {
  2747. struct drm_device *dev = crtc->base.dev;
  2748. struct drm_i915_private *dev_priv = to_i915(dev);
  2749. struct intel_crtc_state *pipe_config =
  2750. to_intel_crtc_state(crtc->base.state);
  2751. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2752. crtc->base.mode = crtc->base.state->mode;
  2753. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2754. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2755. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2756. /*
  2757. * Update pipe size and adjust fitter if needed: the reason for this is
  2758. * that in compute_mode_changes we check the native mode (not the pfit
  2759. * mode) to see if we can flip rather than do a full mode set. In the
  2760. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2761. * pfit state, we'll end up with a big fb scanned out into the wrong
  2762. * sized surface.
  2763. */
  2764. I915_WRITE(PIPESRC(crtc->pipe),
  2765. ((pipe_config->pipe_src_w - 1) << 16) |
  2766. (pipe_config->pipe_src_h - 1));
  2767. /* on skylake this is done by detaching scalers */
  2768. if (INTEL_INFO(dev)->gen >= 9) {
  2769. skl_detach_scalers(crtc);
  2770. if (pipe_config->pch_pfit.enabled)
  2771. skylake_pfit_enable(crtc);
  2772. } else if (HAS_PCH_SPLIT(dev)) {
  2773. if (pipe_config->pch_pfit.enabled)
  2774. ironlake_pfit_enable(crtc);
  2775. else if (old_crtc_state->pch_pfit.enabled)
  2776. ironlake_pfit_disable(crtc, true);
  2777. }
  2778. }
  2779. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2780. {
  2781. struct drm_device *dev = crtc->dev;
  2782. struct drm_i915_private *dev_priv = to_i915(dev);
  2783. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2784. int pipe = intel_crtc->pipe;
  2785. i915_reg_t reg;
  2786. u32 temp;
  2787. /* enable normal train */
  2788. reg = FDI_TX_CTL(pipe);
  2789. temp = I915_READ(reg);
  2790. if (IS_IVYBRIDGE(dev)) {
  2791. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2792. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2793. } else {
  2794. temp &= ~FDI_LINK_TRAIN_NONE;
  2795. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2796. }
  2797. I915_WRITE(reg, temp);
  2798. reg = FDI_RX_CTL(pipe);
  2799. temp = I915_READ(reg);
  2800. if (HAS_PCH_CPT(dev)) {
  2801. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2802. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2803. } else {
  2804. temp &= ~FDI_LINK_TRAIN_NONE;
  2805. temp |= FDI_LINK_TRAIN_NONE;
  2806. }
  2807. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2808. /* wait one idle pattern time */
  2809. POSTING_READ(reg);
  2810. udelay(1000);
  2811. /* IVB wants error correction enabled */
  2812. if (IS_IVYBRIDGE(dev))
  2813. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2814. FDI_FE_ERRC_ENABLE);
  2815. }
  2816. /* The FDI link training functions for ILK/Ibexpeak. */
  2817. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2818. {
  2819. struct drm_device *dev = crtc->dev;
  2820. struct drm_i915_private *dev_priv = to_i915(dev);
  2821. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2822. int pipe = intel_crtc->pipe;
  2823. i915_reg_t reg;
  2824. u32 temp, tries;
  2825. /* FDI needs bits from pipe first */
  2826. assert_pipe_enabled(dev_priv, pipe);
  2827. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2828. for train result */
  2829. reg = FDI_RX_IMR(pipe);
  2830. temp = I915_READ(reg);
  2831. temp &= ~FDI_RX_SYMBOL_LOCK;
  2832. temp &= ~FDI_RX_BIT_LOCK;
  2833. I915_WRITE(reg, temp);
  2834. I915_READ(reg);
  2835. udelay(150);
  2836. /* enable CPU FDI TX and PCH FDI RX */
  2837. reg = FDI_TX_CTL(pipe);
  2838. temp = I915_READ(reg);
  2839. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2840. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2841. temp &= ~FDI_LINK_TRAIN_NONE;
  2842. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2843. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2844. reg = FDI_RX_CTL(pipe);
  2845. temp = I915_READ(reg);
  2846. temp &= ~FDI_LINK_TRAIN_NONE;
  2847. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2848. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2849. POSTING_READ(reg);
  2850. udelay(150);
  2851. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2852. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2853. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2854. FDI_RX_PHASE_SYNC_POINTER_EN);
  2855. reg = FDI_RX_IIR(pipe);
  2856. for (tries = 0; tries < 5; tries++) {
  2857. temp = I915_READ(reg);
  2858. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2859. if ((temp & FDI_RX_BIT_LOCK)) {
  2860. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2861. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2862. break;
  2863. }
  2864. }
  2865. if (tries == 5)
  2866. DRM_ERROR("FDI train 1 fail!\n");
  2867. /* Train 2 */
  2868. reg = FDI_TX_CTL(pipe);
  2869. temp = I915_READ(reg);
  2870. temp &= ~FDI_LINK_TRAIN_NONE;
  2871. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2872. I915_WRITE(reg, temp);
  2873. reg = FDI_RX_CTL(pipe);
  2874. temp = I915_READ(reg);
  2875. temp &= ~FDI_LINK_TRAIN_NONE;
  2876. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2877. I915_WRITE(reg, temp);
  2878. POSTING_READ(reg);
  2879. udelay(150);
  2880. reg = FDI_RX_IIR(pipe);
  2881. for (tries = 0; tries < 5; tries++) {
  2882. temp = I915_READ(reg);
  2883. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2884. if (temp & FDI_RX_SYMBOL_LOCK) {
  2885. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2886. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2887. break;
  2888. }
  2889. }
  2890. if (tries == 5)
  2891. DRM_ERROR("FDI train 2 fail!\n");
  2892. DRM_DEBUG_KMS("FDI train done\n");
  2893. }
  2894. static const int snb_b_fdi_train_param[] = {
  2895. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2896. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2897. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2898. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2899. };
  2900. /* The FDI link training functions for SNB/Cougarpoint. */
  2901. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2902. {
  2903. struct drm_device *dev = crtc->dev;
  2904. struct drm_i915_private *dev_priv = to_i915(dev);
  2905. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2906. int pipe = intel_crtc->pipe;
  2907. i915_reg_t reg;
  2908. u32 temp, i, retry;
  2909. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2910. for train result */
  2911. reg = FDI_RX_IMR(pipe);
  2912. temp = I915_READ(reg);
  2913. temp &= ~FDI_RX_SYMBOL_LOCK;
  2914. temp &= ~FDI_RX_BIT_LOCK;
  2915. I915_WRITE(reg, temp);
  2916. POSTING_READ(reg);
  2917. udelay(150);
  2918. /* enable CPU FDI TX and PCH FDI RX */
  2919. reg = FDI_TX_CTL(pipe);
  2920. temp = I915_READ(reg);
  2921. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2922. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2923. temp &= ~FDI_LINK_TRAIN_NONE;
  2924. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2925. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2926. /* SNB-B */
  2927. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2928. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2929. I915_WRITE(FDI_RX_MISC(pipe),
  2930. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2931. reg = FDI_RX_CTL(pipe);
  2932. temp = I915_READ(reg);
  2933. if (HAS_PCH_CPT(dev)) {
  2934. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2935. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2936. } else {
  2937. temp &= ~FDI_LINK_TRAIN_NONE;
  2938. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2939. }
  2940. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2941. POSTING_READ(reg);
  2942. udelay(150);
  2943. for (i = 0; i < 4; i++) {
  2944. reg = FDI_TX_CTL(pipe);
  2945. temp = I915_READ(reg);
  2946. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2947. temp |= snb_b_fdi_train_param[i];
  2948. I915_WRITE(reg, temp);
  2949. POSTING_READ(reg);
  2950. udelay(500);
  2951. for (retry = 0; retry < 5; retry++) {
  2952. reg = FDI_RX_IIR(pipe);
  2953. temp = I915_READ(reg);
  2954. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2955. if (temp & FDI_RX_BIT_LOCK) {
  2956. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2957. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2958. break;
  2959. }
  2960. udelay(50);
  2961. }
  2962. if (retry < 5)
  2963. break;
  2964. }
  2965. if (i == 4)
  2966. DRM_ERROR("FDI train 1 fail!\n");
  2967. /* Train 2 */
  2968. reg = FDI_TX_CTL(pipe);
  2969. temp = I915_READ(reg);
  2970. temp &= ~FDI_LINK_TRAIN_NONE;
  2971. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2972. if (IS_GEN6(dev)) {
  2973. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2974. /* SNB-B */
  2975. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2976. }
  2977. I915_WRITE(reg, temp);
  2978. reg = FDI_RX_CTL(pipe);
  2979. temp = I915_READ(reg);
  2980. if (HAS_PCH_CPT(dev)) {
  2981. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2982. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2983. } else {
  2984. temp &= ~FDI_LINK_TRAIN_NONE;
  2985. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2986. }
  2987. I915_WRITE(reg, temp);
  2988. POSTING_READ(reg);
  2989. udelay(150);
  2990. for (i = 0; i < 4; i++) {
  2991. reg = FDI_TX_CTL(pipe);
  2992. temp = I915_READ(reg);
  2993. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2994. temp |= snb_b_fdi_train_param[i];
  2995. I915_WRITE(reg, temp);
  2996. POSTING_READ(reg);
  2997. udelay(500);
  2998. for (retry = 0; retry < 5; retry++) {
  2999. reg = FDI_RX_IIR(pipe);
  3000. temp = I915_READ(reg);
  3001. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3002. if (temp & FDI_RX_SYMBOL_LOCK) {
  3003. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3004. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3005. break;
  3006. }
  3007. udelay(50);
  3008. }
  3009. if (retry < 5)
  3010. break;
  3011. }
  3012. if (i == 4)
  3013. DRM_ERROR("FDI train 2 fail!\n");
  3014. DRM_DEBUG_KMS("FDI train done.\n");
  3015. }
  3016. /* Manual link training for Ivy Bridge A0 parts */
  3017. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3018. {
  3019. struct drm_device *dev = crtc->dev;
  3020. struct drm_i915_private *dev_priv = to_i915(dev);
  3021. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3022. int pipe = intel_crtc->pipe;
  3023. i915_reg_t reg;
  3024. u32 temp, i, j;
  3025. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3026. for train result */
  3027. reg = FDI_RX_IMR(pipe);
  3028. temp = I915_READ(reg);
  3029. temp &= ~FDI_RX_SYMBOL_LOCK;
  3030. temp &= ~FDI_RX_BIT_LOCK;
  3031. I915_WRITE(reg, temp);
  3032. POSTING_READ(reg);
  3033. udelay(150);
  3034. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3035. I915_READ(FDI_RX_IIR(pipe)));
  3036. /* Try each vswing and preemphasis setting twice before moving on */
  3037. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3038. /* disable first in case we need to retry */
  3039. reg = FDI_TX_CTL(pipe);
  3040. temp = I915_READ(reg);
  3041. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3042. temp &= ~FDI_TX_ENABLE;
  3043. I915_WRITE(reg, temp);
  3044. reg = FDI_RX_CTL(pipe);
  3045. temp = I915_READ(reg);
  3046. temp &= ~FDI_LINK_TRAIN_AUTO;
  3047. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3048. temp &= ~FDI_RX_ENABLE;
  3049. I915_WRITE(reg, temp);
  3050. /* enable CPU FDI TX and PCH FDI RX */
  3051. reg = FDI_TX_CTL(pipe);
  3052. temp = I915_READ(reg);
  3053. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3054. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3055. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3056. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3057. temp |= snb_b_fdi_train_param[j/2];
  3058. temp |= FDI_COMPOSITE_SYNC;
  3059. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3060. I915_WRITE(FDI_RX_MISC(pipe),
  3061. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3062. reg = FDI_RX_CTL(pipe);
  3063. temp = I915_READ(reg);
  3064. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3065. temp |= FDI_COMPOSITE_SYNC;
  3066. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3067. POSTING_READ(reg);
  3068. udelay(1); /* should be 0.5us */
  3069. for (i = 0; i < 4; i++) {
  3070. reg = FDI_RX_IIR(pipe);
  3071. temp = I915_READ(reg);
  3072. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3073. if (temp & FDI_RX_BIT_LOCK ||
  3074. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3075. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3076. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3077. i);
  3078. break;
  3079. }
  3080. udelay(1); /* should be 0.5us */
  3081. }
  3082. if (i == 4) {
  3083. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3084. continue;
  3085. }
  3086. /* Train 2 */
  3087. reg = FDI_TX_CTL(pipe);
  3088. temp = I915_READ(reg);
  3089. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3090. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3091. I915_WRITE(reg, temp);
  3092. reg = FDI_RX_CTL(pipe);
  3093. temp = I915_READ(reg);
  3094. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3095. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3096. I915_WRITE(reg, temp);
  3097. POSTING_READ(reg);
  3098. udelay(2); /* should be 1.5us */
  3099. for (i = 0; i < 4; i++) {
  3100. reg = FDI_RX_IIR(pipe);
  3101. temp = I915_READ(reg);
  3102. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3103. if (temp & FDI_RX_SYMBOL_LOCK ||
  3104. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3105. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3106. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3107. i);
  3108. goto train_done;
  3109. }
  3110. udelay(2); /* should be 1.5us */
  3111. }
  3112. if (i == 4)
  3113. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3114. }
  3115. train_done:
  3116. DRM_DEBUG_KMS("FDI train done.\n");
  3117. }
  3118. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3119. {
  3120. struct drm_device *dev = intel_crtc->base.dev;
  3121. struct drm_i915_private *dev_priv = to_i915(dev);
  3122. int pipe = intel_crtc->pipe;
  3123. i915_reg_t reg;
  3124. u32 temp;
  3125. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3126. reg = FDI_RX_CTL(pipe);
  3127. temp = I915_READ(reg);
  3128. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3129. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3130. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3131. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3132. POSTING_READ(reg);
  3133. udelay(200);
  3134. /* Switch from Rawclk to PCDclk */
  3135. temp = I915_READ(reg);
  3136. I915_WRITE(reg, temp | FDI_PCDCLK);
  3137. POSTING_READ(reg);
  3138. udelay(200);
  3139. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3140. reg = FDI_TX_CTL(pipe);
  3141. temp = I915_READ(reg);
  3142. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3143. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3144. POSTING_READ(reg);
  3145. udelay(100);
  3146. }
  3147. }
  3148. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3149. {
  3150. struct drm_device *dev = intel_crtc->base.dev;
  3151. struct drm_i915_private *dev_priv = to_i915(dev);
  3152. int pipe = intel_crtc->pipe;
  3153. i915_reg_t reg;
  3154. u32 temp;
  3155. /* Switch from PCDclk to Rawclk */
  3156. reg = FDI_RX_CTL(pipe);
  3157. temp = I915_READ(reg);
  3158. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3159. /* Disable CPU FDI TX PLL */
  3160. reg = FDI_TX_CTL(pipe);
  3161. temp = I915_READ(reg);
  3162. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3163. POSTING_READ(reg);
  3164. udelay(100);
  3165. reg = FDI_RX_CTL(pipe);
  3166. temp = I915_READ(reg);
  3167. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3168. /* Wait for the clocks to turn off. */
  3169. POSTING_READ(reg);
  3170. udelay(100);
  3171. }
  3172. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3173. {
  3174. struct drm_device *dev = crtc->dev;
  3175. struct drm_i915_private *dev_priv = to_i915(dev);
  3176. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3177. int pipe = intel_crtc->pipe;
  3178. i915_reg_t reg;
  3179. u32 temp;
  3180. /* disable CPU FDI tx and PCH FDI rx */
  3181. reg = FDI_TX_CTL(pipe);
  3182. temp = I915_READ(reg);
  3183. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3184. POSTING_READ(reg);
  3185. reg = FDI_RX_CTL(pipe);
  3186. temp = I915_READ(reg);
  3187. temp &= ~(0x7 << 16);
  3188. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3189. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3190. POSTING_READ(reg);
  3191. udelay(100);
  3192. /* Ironlake workaround, disable clock pointer after downing FDI */
  3193. if (HAS_PCH_IBX(dev))
  3194. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3195. /* still set train pattern 1 */
  3196. reg = FDI_TX_CTL(pipe);
  3197. temp = I915_READ(reg);
  3198. temp &= ~FDI_LINK_TRAIN_NONE;
  3199. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3200. I915_WRITE(reg, temp);
  3201. reg = FDI_RX_CTL(pipe);
  3202. temp = I915_READ(reg);
  3203. if (HAS_PCH_CPT(dev)) {
  3204. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3205. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3206. } else {
  3207. temp &= ~FDI_LINK_TRAIN_NONE;
  3208. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3209. }
  3210. /* BPC in FDI rx is consistent with that in PIPECONF */
  3211. temp &= ~(0x07 << 16);
  3212. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3213. I915_WRITE(reg, temp);
  3214. POSTING_READ(reg);
  3215. udelay(100);
  3216. }
  3217. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3218. {
  3219. struct intel_crtc *crtc;
  3220. /* Note that we don't need to be called with mode_config.lock here
  3221. * as our list of CRTC objects is static for the lifetime of the
  3222. * device and so cannot disappear as we iterate. Similarly, we can
  3223. * happily treat the predicates as racy, atomic checks as userspace
  3224. * cannot claim and pin a new fb without at least acquring the
  3225. * struct_mutex and so serialising with us.
  3226. */
  3227. for_each_intel_crtc(dev, crtc) {
  3228. if (atomic_read(&crtc->unpin_work_count) == 0)
  3229. continue;
  3230. if (crtc->flip_work)
  3231. intel_wait_for_vblank(dev, crtc->pipe);
  3232. return true;
  3233. }
  3234. return false;
  3235. }
  3236. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3237. {
  3238. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3239. struct intel_flip_work *work = intel_crtc->flip_work;
  3240. intel_crtc->flip_work = NULL;
  3241. if (work->event)
  3242. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3243. drm_crtc_vblank_put(&intel_crtc->base);
  3244. wake_up_all(&dev_priv->pending_flip_queue);
  3245. queue_work(dev_priv->wq, &work->unpin_work);
  3246. trace_i915_flip_complete(intel_crtc->plane,
  3247. work->pending_flip_obj);
  3248. }
  3249. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3250. {
  3251. struct drm_device *dev = crtc->dev;
  3252. struct drm_i915_private *dev_priv = to_i915(dev);
  3253. long ret;
  3254. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3255. ret = wait_event_interruptible_timeout(
  3256. dev_priv->pending_flip_queue,
  3257. !intel_crtc_has_pending_flip(crtc),
  3258. 60*HZ);
  3259. if (ret < 0)
  3260. return ret;
  3261. if (ret == 0) {
  3262. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3263. struct intel_flip_work *work;
  3264. spin_lock_irq(&dev->event_lock);
  3265. work = intel_crtc->flip_work;
  3266. if (work && !is_mmio_work(work)) {
  3267. WARN_ONCE(1, "Removing stuck page flip\n");
  3268. page_flip_completed(intel_crtc);
  3269. }
  3270. spin_unlock_irq(&dev->event_lock);
  3271. }
  3272. return 0;
  3273. }
  3274. static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3275. {
  3276. u32 temp;
  3277. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3278. mutex_lock(&dev_priv->sb_lock);
  3279. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3280. temp |= SBI_SSCCTL_DISABLE;
  3281. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3282. mutex_unlock(&dev_priv->sb_lock);
  3283. }
  3284. /* Program iCLKIP clock to the desired frequency */
  3285. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3286. {
  3287. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3288. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3289. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3290. u32 temp;
  3291. lpt_disable_iclkip(dev_priv);
  3292. /* The iCLK virtual clock root frequency is in MHz,
  3293. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3294. * divisors, it is necessary to divide one by another, so we
  3295. * convert the virtual clock precision to KHz here for higher
  3296. * precision.
  3297. */
  3298. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3299. u32 iclk_virtual_root_freq = 172800 * 1000;
  3300. u32 iclk_pi_range = 64;
  3301. u32 desired_divisor;
  3302. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3303. clock << auxdiv);
  3304. divsel = (desired_divisor / iclk_pi_range) - 2;
  3305. phaseinc = desired_divisor % iclk_pi_range;
  3306. /*
  3307. * Near 20MHz is a corner case which is
  3308. * out of range for the 7-bit divisor
  3309. */
  3310. if (divsel <= 0x7f)
  3311. break;
  3312. }
  3313. /* This should not happen with any sane values */
  3314. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3315. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3316. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3317. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3318. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3319. clock,
  3320. auxdiv,
  3321. divsel,
  3322. phasedir,
  3323. phaseinc);
  3324. mutex_lock(&dev_priv->sb_lock);
  3325. /* Program SSCDIVINTPHASE6 */
  3326. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3327. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3328. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3329. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3330. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3331. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3332. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3333. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3334. /* Program SSCAUXDIV */
  3335. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3336. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3337. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3338. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3339. /* Enable modulator and associated divider */
  3340. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3341. temp &= ~SBI_SSCCTL_DISABLE;
  3342. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3343. mutex_unlock(&dev_priv->sb_lock);
  3344. /* Wait for initialization time */
  3345. udelay(24);
  3346. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3347. }
  3348. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3349. {
  3350. u32 divsel, phaseinc, auxdiv;
  3351. u32 iclk_virtual_root_freq = 172800 * 1000;
  3352. u32 iclk_pi_range = 64;
  3353. u32 desired_divisor;
  3354. u32 temp;
  3355. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3356. return 0;
  3357. mutex_lock(&dev_priv->sb_lock);
  3358. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3359. if (temp & SBI_SSCCTL_DISABLE) {
  3360. mutex_unlock(&dev_priv->sb_lock);
  3361. return 0;
  3362. }
  3363. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3364. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3365. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3366. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3367. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3368. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3369. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3370. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3371. mutex_unlock(&dev_priv->sb_lock);
  3372. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3373. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3374. desired_divisor << auxdiv);
  3375. }
  3376. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3377. enum pipe pch_transcoder)
  3378. {
  3379. struct drm_device *dev = crtc->base.dev;
  3380. struct drm_i915_private *dev_priv = to_i915(dev);
  3381. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3382. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3383. I915_READ(HTOTAL(cpu_transcoder)));
  3384. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3385. I915_READ(HBLANK(cpu_transcoder)));
  3386. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3387. I915_READ(HSYNC(cpu_transcoder)));
  3388. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3389. I915_READ(VTOTAL(cpu_transcoder)));
  3390. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3391. I915_READ(VBLANK(cpu_transcoder)));
  3392. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3393. I915_READ(VSYNC(cpu_transcoder)));
  3394. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3395. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3396. }
  3397. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3398. {
  3399. struct drm_i915_private *dev_priv = to_i915(dev);
  3400. uint32_t temp;
  3401. temp = I915_READ(SOUTH_CHICKEN1);
  3402. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3403. return;
  3404. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3405. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3406. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3407. if (enable)
  3408. temp |= FDI_BC_BIFURCATION_SELECT;
  3409. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3410. I915_WRITE(SOUTH_CHICKEN1, temp);
  3411. POSTING_READ(SOUTH_CHICKEN1);
  3412. }
  3413. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3414. {
  3415. struct drm_device *dev = intel_crtc->base.dev;
  3416. switch (intel_crtc->pipe) {
  3417. case PIPE_A:
  3418. break;
  3419. case PIPE_B:
  3420. if (intel_crtc->config->fdi_lanes > 2)
  3421. cpt_set_fdi_bc_bifurcation(dev, false);
  3422. else
  3423. cpt_set_fdi_bc_bifurcation(dev, true);
  3424. break;
  3425. case PIPE_C:
  3426. cpt_set_fdi_bc_bifurcation(dev, true);
  3427. break;
  3428. default:
  3429. BUG();
  3430. }
  3431. }
  3432. /* Return which DP Port should be selected for Transcoder DP control */
  3433. static enum port
  3434. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3435. {
  3436. struct drm_device *dev = crtc->dev;
  3437. struct intel_encoder *encoder;
  3438. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3439. if (encoder->type == INTEL_OUTPUT_DP ||
  3440. encoder->type == INTEL_OUTPUT_EDP)
  3441. return enc_to_dig_port(&encoder->base)->port;
  3442. }
  3443. return -1;
  3444. }
  3445. /*
  3446. * Enable PCH resources required for PCH ports:
  3447. * - PCH PLLs
  3448. * - FDI training & RX/TX
  3449. * - update transcoder timings
  3450. * - DP transcoding bits
  3451. * - transcoder
  3452. */
  3453. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3454. {
  3455. struct drm_device *dev = crtc->dev;
  3456. struct drm_i915_private *dev_priv = to_i915(dev);
  3457. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3458. int pipe = intel_crtc->pipe;
  3459. u32 temp;
  3460. assert_pch_transcoder_disabled(dev_priv, pipe);
  3461. if (IS_IVYBRIDGE(dev))
  3462. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3463. /* Write the TU size bits before fdi link training, so that error
  3464. * detection works. */
  3465. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3466. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3467. /* For PCH output, training FDI link */
  3468. dev_priv->display.fdi_link_train(crtc);
  3469. /* We need to program the right clock selection before writing the pixel
  3470. * mutliplier into the DPLL. */
  3471. if (HAS_PCH_CPT(dev)) {
  3472. u32 sel;
  3473. temp = I915_READ(PCH_DPLL_SEL);
  3474. temp |= TRANS_DPLL_ENABLE(pipe);
  3475. sel = TRANS_DPLLB_SEL(pipe);
  3476. if (intel_crtc->config->shared_dpll ==
  3477. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3478. temp |= sel;
  3479. else
  3480. temp &= ~sel;
  3481. I915_WRITE(PCH_DPLL_SEL, temp);
  3482. }
  3483. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3484. * transcoder, and we actually should do this to not upset any PCH
  3485. * transcoder that already use the clock when we share it.
  3486. *
  3487. * Note that enable_shared_dpll tries to do the right thing, but
  3488. * get_shared_dpll unconditionally resets the pll - we need that to have
  3489. * the right LVDS enable sequence. */
  3490. intel_enable_shared_dpll(intel_crtc);
  3491. /* set transcoder timing, panel must allow it */
  3492. assert_panel_unlocked(dev_priv, pipe);
  3493. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3494. intel_fdi_normal_train(crtc);
  3495. /* For PCH DP, enable TRANS_DP_CTL */
  3496. if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3497. const struct drm_display_mode *adjusted_mode =
  3498. &intel_crtc->config->base.adjusted_mode;
  3499. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3500. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3501. temp = I915_READ(reg);
  3502. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3503. TRANS_DP_SYNC_MASK |
  3504. TRANS_DP_BPC_MASK);
  3505. temp |= TRANS_DP_OUTPUT_ENABLE;
  3506. temp |= bpc << 9; /* same format but at 11:9 */
  3507. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3508. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3509. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3510. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3511. switch (intel_trans_dp_port_sel(crtc)) {
  3512. case PORT_B:
  3513. temp |= TRANS_DP_PORT_SEL_B;
  3514. break;
  3515. case PORT_C:
  3516. temp |= TRANS_DP_PORT_SEL_C;
  3517. break;
  3518. case PORT_D:
  3519. temp |= TRANS_DP_PORT_SEL_D;
  3520. break;
  3521. default:
  3522. BUG();
  3523. }
  3524. I915_WRITE(reg, temp);
  3525. }
  3526. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3527. }
  3528. static void lpt_pch_enable(struct drm_crtc *crtc)
  3529. {
  3530. struct drm_device *dev = crtc->dev;
  3531. struct drm_i915_private *dev_priv = to_i915(dev);
  3532. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3533. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3534. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3535. lpt_program_iclkip(crtc);
  3536. /* Set transcoder timing. */
  3537. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3538. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3539. }
  3540. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3541. {
  3542. struct drm_i915_private *dev_priv = to_i915(dev);
  3543. i915_reg_t dslreg = PIPEDSL(pipe);
  3544. u32 temp;
  3545. temp = I915_READ(dslreg);
  3546. udelay(500);
  3547. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3548. if (wait_for(I915_READ(dslreg) != temp, 5))
  3549. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3550. }
  3551. }
  3552. static int
  3553. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3554. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3555. int src_w, int src_h, int dst_w, int dst_h)
  3556. {
  3557. struct intel_crtc_scaler_state *scaler_state =
  3558. &crtc_state->scaler_state;
  3559. struct intel_crtc *intel_crtc =
  3560. to_intel_crtc(crtc_state->base.crtc);
  3561. int need_scaling;
  3562. need_scaling = intel_rotation_90_or_270(rotation) ?
  3563. (src_h != dst_w || src_w != dst_h):
  3564. (src_w != dst_w || src_h != dst_h);
  3565. /*
  3566. * if plane is being disabled or scaler is no more required or force detach
  3567. * - free scaler binded to this plane/crtc
  3568. * - in order to do this, update crtc->scaler_usage
  3569. *
  3570. * Here scaler state in crtc_state is set free so that
  3571. * scaler can be assigned to other user. Actual register
  3572. * update to free the scaler is done in plane/panel-fit programming.
  3573. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3574. */
  3575. if (force_detach || !need_scaling) {
  3576. if (*scaler_id >= 0) {
  3577. scaler_state->scaler_users &= ~(1 << scaler_user);
  3578. scaler_state->scalers[*scaler_id].in_use = 0;
  3579. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3580. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3581. intel_crtc->pipe, scaler_user, *scaler_id,
  3582. scaler_state->scaler_users);
  3583. *scaler_id = -1;
  3584. }
  3585. return 0;
  3586. }
  3587. /* range checks */
  3588. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3589. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3590. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3591. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3592. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3593. "size is out of scaler range\n",
  3594. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3595. return -EINVAL;
  3596. }
  3597. /* mark this plane as a scaler user in crtc_state */
  3598. scaler_state->scaler_users |= (1 << scaler_user);
  3599. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3600. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3601. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3602. scaler_state->scaler_users);
  3603. return 0;
  3604. }
  3605. /**
  3606. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3607. *
  3608. * @state: crtc's scaler state
  3609. *
  3610. * Return
  3611. * 0 - scaler_usage updated successfully
  3612. * error - requested scaling cannot be supported or other error condition
  3613. */
  3614. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3615. {
  3616. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3617. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3618. DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
  3619. intel_crtc->base.base.id, intel_crtc->base.name,
  3620. intel_crtc->pipe, SKL_CRTC_INDEX);
  3621. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3622. &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
  3623. state->pipe_src_w, state->pipe_src_h,
  3624. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3625. }
  3626. /**
  3627. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3628. *
  3629. * @state: crtc's scaler state
  3630. * @plane_state: atomic plane state to update
  3631. *
  3632. * Return
  3633. * 0 - scaler_usage updated successfully
  3634. * error - requested scaling cannot be supported or other error condition
  3635. */
  3636. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3637. struct intel_plane_state *plane_state)
  3638. {
  3639. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3640. struct intel_plane *intel_plane =
  3641. to_intel_plane(plane_state->base.plane);
  3642. struct drm_framebuffer *fb = plane_state->base.fb;
  3643. int ret;
  3644. bool force_detach = !fb || !plane_state->visible;
  3645. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
  3646. intel_plane->base.base.id, intel_plane->base.name,
  3647. intel_crtc->pipe, drm_plane_index(&intel_plane->base));
  3648. ret = skl_update_scaler(crtc_state, force_detach,
  3649. drm_plane_index(&intel_plane->base),
  3650. &plane_state->scaler_id,
  3651. plane_state->base.rotation,
  3652. drm_rect_width(&plane_state->src) >> 16,
  3653. drm_rect_height(&plane_state->src) >> 16,
  3654. drm_rect_width(&plane_state->dst),
  3655. drm_rect_height(&plane_state->dst));
  3656. if (ret || plane_state->scaler_id < 0)
  3657. return ret;
  3658. /* check colorkey */
  3659. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3660. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  3661. intel_plane->base.base.id,
  3662. intel_plane->base.name);
  3663. return -EINVAL;
  3664. }
  3665. /* Check src format */
  3666. switch (fb->pixel_format) {
  3667. case DRM_FORMAT_RGB565:
  3668. case DRM_FORMAT_XBGR8888:
  3669. case DRM_FORMAT_XRGB8888:
  3670. case DRM_FORMAT_ABGR8888:
  3671. case DRM_FORMAT_ARGB8888:
  3672. case DRM_FORMAT_XRGB2101010:
  3673. case DRM_FORMAT_XBGR2101010:
  3674. case DRM_FORMAT_YUYV:
  3675. case DRM_FORMAT_YVYU:
  3676. case DRM_FORMAT_UYVY:
  3677. case DRM_FORMAT_VYUY:
  3678. break;
  3679. default:
  3680. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  3681. intel_plane->base.base.id, intel_plane->base.name,
  3682. fb->base.id, fb->pixel_format);
  3683. return -EINVAL;
  3684. }
  3685. return 0;
  3686. }
  3687. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3688. {
  3689. int i;
  3690. for (i = 0; i < crtc->num_scalers; i++)
  3691. skl_detach_scaler(crtc, i);
  3692. }
  3693. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3694. {
  3695. struct drm_device *dev = crtc->base.dev;
  3696. struct drm_i915_private *dev_priv = to_i915(dev);
  3697. int pipe = crtc->pipe;
  3698. struct intel_crtc_scaler_state *scaler_state =
  3699. &crtc->config->scaler_state;
  3700. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3701. if (crtc->config->pch_pfit.enabled) {
  3702. int id;
  3703. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3704. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3705. return;
  3706. }
  3707. id = scaler_state->scaler_id;
  3708. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3709. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3710. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3711. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3712. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3713. }
  3714. }
  3715. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3716. {
  3717. struct drm_device *dev = crtc->base.dev;
  3718. struct drm_i915_private *dev_priv = to_i915(dev);
  3719. int pipe = crtc->pipe;
  3720. if (crtc->config->pch_pfit.enabled) {
  3721. /* Force use of hard-coded filter coefficients
  3722. * as some pre-programmed values are broken,
  3723. * e.g. x201.
  3724. */
  3725. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3726. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3727. PF_PIPE_SEL_IVB(pipe));
  3728. else
  3729. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3730. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3731. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3732. }
  3733. }
  3734. void hsw_enable_ips(struct intel_crtc *crtc)
  3735. {
  3736. struct drm_device *dev = crtc->base.dev;
  3737. struct drm_i915_private *dev_priv = to_i915(dev);
  3738. if (!crtc->config->ips_enabled)
  3739. return;
  3740. /*
  3741. * We can only enable IPS after we enable a plane and wait for a vblank
  3742. * This function is called from post_plane_update, which is run after
  3743. * a vblank wait.
  3744. */
  3745. assert_plane_enabled(dev_priv, crtc->plane);
  3746. if (IS_BROADWELL(dev)) {
  3747. mutex_lock(&dev_priv->rps.hw_lock);
  3748. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3749. mutex_unlock(&dev_priv->rps.hw_lock);
  3750. /* Quoting Art Runyan: "its not safe to expect any particular
  3751. * value in IPS_CTL bit 31 after enabling IPS through the
  3752. * mailbox." Moreover, the mailbox may return a bogus state,
  3753. * so we need to just enable it and continue on.
  3754. */
  3755. } else {
  3756. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3757. /* The bit only becomes 1 in the next vblank, so this wait here
  3758. * is essentially intel_wait_for_vblank. If we don't have this
  3759. * and don't wait for vblanks until the end of crtc_enable, then
  3760. * the HW state readout code will complain that the expected
  3761. * IPS_CTL value is not the one we read. */
  3762. if (intel_wait_for_register(dev_priv,
  3763. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  3764. 50))
  3765. DRM_ERROR("Timed out waiting for IPS enable\n");
  3766. }
  3767. }
  3768. void hsw_disable_ips(struct intel_crtc *crtc)
  3769. {
  3770. struct drm_device *dev = crtc->base.dev;
  3771. struct drm_i915_private *dev_priv = to_i915(dev);
  3772. if (!crtc->config->ips_enabled)
  3773. return;
  3774. assert_plane_enabled(dev_priv, crtc->plane);
  3775. if (IS_BROADWELL(dev)) {
  3776. mutex_lock(&dev_priv->rps.hw_lock);
  3777. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3778. mutex_unlock(&dev_priv->rps.hw_lock);
  3779. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3780. if (intel_wait_for_register(dev_priv,
  3781. IPS_CTL, IPS_ENABLE, 0,
  3782. 42))
  3783. DRM_ERROR("Timed out waiting for IPS disable\n");
  3784. } else {
  3785. I915_WRITE(IPS_CTL, 0);
  3786. POSTING_READ(IPS_CTL);
  3787. }
  3788. /* We need to wait for a vblank before we can disable the plane. */
  3789. intel_wait_for_vblank(dev, crtc->pipe);
  3790. }
  3791. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3792. {
  3793. if (intel_crtc->overlay) {
  3794. struct drm_device *dev = intel_crtc->base.dev;
  3795. struct drm_i915_private *dev_priv = to_i915(dev);
  3796. mutex_lock(&dev->struct_mutex);
  3797. dev_priv->mm.interruptible = false;
  3798. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3799. dev_priv->mm.interruptible = true;
  3800. mutex_unlock(&dev->struct_mutex);
  3801. }
  3802. /* Let userspace switch the overlay on again. In most cases userspace
  3803. * has to recompute where to put it anyway.
  3804. */
  3805. }
  3806. /**
  3807. * intel_post_enable_primary - Perform operations after enabling primary plane
  3808. * @crtc: the CRTC whose primary plane was just enabled
  3809. *
  3810. * Performs potentially sleeping operations that must be done after the primary
  3811. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3812. * called due to an explicit primary plane update, or due to an implicit
  3813. * re-enable that is caused when a sprite plane is updated to no longer
  3814. * completely hide the primary plane.
  3815. */
  3816. static void
  3817. intel_post_enable_primary(struct drm_crtc *crtc)
  3818. {
  3819. struct drm_device *dev = crtc->dev;
  3820. struct drm_i915_private *dev_priv = to_i915(dev);
  3821. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3822. int pipe = intel_crtc->pipe;
  3823. /*
  3824. * FIXME IPS should be fine as long as one plane is
  3825. * enabled, but in practice it seems to have problems
  3826. * when going from primary only to sprite only and vice
  3827. * versa.
  3828. */
  3829. hsw_enable_ips(intel_crtc);
  3830. /*
  3831. * Gen2 reports pipe underruns whenever all planes are disabled.
  3832. * So don't enable underrun reporting before at least some planes
  3833. * are enabled.
  3834. * FIXME: Need to fix the logic to work when we turn off all planes
  3835. * but leave the pipe running.
  3836. */
  3837. if (IS_GEN2(dev))
  3838. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3839. /* Underruns don't always raise interrupts, so check manually. */
  3840. intel_check_cpu_fifo_underruns(dev_priv);
  3841. intel_check_pch_fifo_underruns(dev_priv);
  3842. }
  3843. /* FIXME move all this to pre_plane_update() with proper state tracking */
  3844. static void
  3845. intel_pre_disable_primary(struct drm_crtc *crtc)
  3846. {
  3847. struct drm_device *dev = crtc->dev;
  3848. struct drm_i915_private *dev_priv = to_i915(dev);
  3849. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3850. int pipe = intel_crtc->pipe;
  3851. /*
  3852. * Gen2 reports pipe underruns whenever all planes are disabled.
  3853. * So diasble underrun reporting before all the planes get disabled.
  3854. * FIXME: Need to fix the logic to work when we turn off all planes
  3855. * but leave the pipe running.
  3856. */
  3857. if (IS_GEN2(dev))
  3858. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3859. /*
  3860. * FIXME IPS should be fine as long as one plane is
  3861. * enabled, but in practice it seems to have problems
  3862. * when going from primary only to sprite only and vice
  3863. * versa.
  3864. */
  3865. hsw_disable_ips(intel_crtc);
  3866. }
  3867. /* FIXME get rid of this and use pre_plane_update */
  3868. static void
  3869. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  3870. {
  3871. struct drm_device *dev = crtc->dev;
  3872. struct drm_i915_private *dev_priv = to_i915(dev);
  3873. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3874. int pipe = intel_crtc->pipe;
  3875. intel_pre_disable_primary(crtc);
  3876. /*
  3877. * Vblank time updates from the shadow to live plane control register
  3878. * are blocked if the memory self-refresh mode is active at that
  3879. * moment. So to make sure the plane gets truly disabled, disable
  3880. * first the self-refresh mode. The self-refresh enable bit in turn
  3881. * will be checked/applied by the HW only at the next frame start
  3882. * event which is after the vblank start event, so we need to have a
  3883. * wait-for-vblank between disabling the plane and the pipe.
  3884. */
  3885. if (HAS_GMCH_DISPLAY(dev)) {
  3886. intel_set_memory_cxsr(dev_priv, false);
  3887. dev_priv->wm.vlv.cxsr = false;
  3888. intel_wait_for_vblank(dev, pipe);
  3889. }
  3890. }
  3891. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  3892. {
  3893. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3894. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3895. struct intel_crtc_state *pipe_config =
  3896. to_intel_crtc_state(crtc->base.state);
  3897. struct drm_plane *primary = crtc->base.primary;
  3898. struct drm_plane_state *old_pri_state =
  3899. drm_atomic_get_existing_plane_state(old_state, primary);
  3900. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  3901. crtc->wm.cxsr_allowed = true;
  3902. if (pipe_config->update_wm_post && pipe_config->base.active)
  3903. intel_update_watermarks(&crtc->base);
  3904. if (old_pri_state) {
  3905. struct intel_plane_state *primary_state =
  3906. to_intel_plane_state(primary->state);
  3907. struct intel_plane_state *old_primary_state =
  3908. to_intel_plane_state(old_pri_state);
  3909. intel_fbc_post_update(crtc);
  3910. if (primary_state->visible &&
  3911. (needs_modeset(&pipe_config->base) ||
  3912. !old_primary_state->visible))
  3913. intel_post_enable_primary(&crtc->base);
  3914. }
  3915. }
  3916. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  3917. {
  3918. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3919. struct drm_device *dev = crtc->base.dev;
  3920. struct drm_i915_private *dev_priv = to_i915(dev);
  3921. struct intel_crtc_state *pipe_config =
  3922. to_intel_crtc_state(crtc->base.state);
  3923. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3924. struct drm_plane *primary = crtc->base.primary;
  3925. struct drm_plane_state *old_pri_state =
  3926. drm_atomic_get_existing_plane_state(old_state, primary);
  3927. bool modeset = needs_modeset(&pipe_config->base);
  3928. if (old_pri_state) {
  3929. struct intel_plane_state *primary_state =
  3930. to_intel_plane_state(primary->state);
  3931. struct intel_plane_state *old_primary_state =
  3932. to_intel_plane_state(old_pri_state);
  3933. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  3934. if (old_primary_state->visible &&
  3935. (modeset || !primary_state->visible))
  3936. intel_pre_disable_primary(&crtc->base);
  3937. }
  3938. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
  3939. crtc->wm.cxsr_allowed = false;
  3940. /*
  3941. * Vblank time updates from the shadow to live plane control register
  3942. * are blocked if the memory self-refresh mode is active at that
  3943. * moment. So to make sure the plane gets truly disabled, disable
  3944. * first the self-refresh mode. The self-refresh enable bit in turn
  3945. * will be checked/applied by the HW only at the next frame start
  3946. * event which is after the vblank start event, so we need to have a
  3947. * wait-for-vblank between disabling the plane and the pipe.
  3948. */
  3949. if (old_crtc_state->base.active) {
  3950. intel_set_memory_cxsr(dev_priv, false);
  3951. dev_priv->wm.vlv.cxsr = false;
  3952. intel_wait_for_vblank(dev, crtc->pipe);
  3953. }
  3954. }
  3955. /*
  3956. * IVB workaround: must disable low power watermarks for at least
  3957. * one frame before enabling scaling. LP watermarks can be re-enabled
  3958. * when scaling is disabled.
  3959. *
  3960. * WaCxSRDisabledForSpriteScaling:ivb
  3961. */
  3962. if (pipe_config->disable_lp_wm) {
  3963. ilk_disable_lp_wm(dev);
  3964. intel_wait_for_vblank(dev, crtc->pipe);
  3965. }
  3966. /*
  3967. * If we're doing a modeset, we're done. No need to do any pre-vblank
  3968. * watermark programming here.
  3969. */
  3970. if (needs_modeset(&pipe_config->base))
  3971. return;
  3972. /*
  3973. * For platforms that support atomic watermarks, program the
  3974. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  3975. * will be the intermediate values that are safe for both pre- and
  3976. * post- vblank; when vblank happens, the 'active' values will be set
  3977. * to the final 'target' values and we'll do this again to get the
  3978. * optimal watermarks. For gen9+ platforms, the values we program here
  3979. * will be the final target values which will get automatically latched
  3980. * at vblank time; no further programming will be necessary.
  3981. *
  3982. * If a platform hasn't been transitioned to atomic watermarks yet,
  3983. * we'll continue to update watermarks the old way, if flags tell
  3984. * us to.
  3985. */
  3986. if (dev_priv->display.initial_watermarks != NULL)
  3987. dev_priv->display.initial_watermarks(pipe_config);
  3988. else if (pipe_config->update_wm_pre)
  3989. intel_update_watermarks(&crtc->base);
  3990. }
  3991. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  3992. {
  3993. struct drm_device *dev = crtc->dev;
  3994. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3995. struct drm_plane *p;
  3996. int pipe = intel_crtc->pipe;
  3997. intel_crtc_dpms_overlay_disable(intel_crtc);
  3998. drm_for_each_plane_mask(p, dev, plane_mask)
  3999. to_intel_plane(p)->disable_plane(p, crtc);
  4000. /*
  4001. * FIXME: Once we grow proper nuclear flip support out of this we need
  4002. * to compute the mask of flip planes precisely. For the time being
  4003. * consider this a flip to a NULL plane.
  4004. */
  4005. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4006. }
  4007. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4008. {
  4009. struct drm_device *dev = crtc->dev;
  4010. struct drm_i915_private *dev_priv = to_i915(dev);
  4011. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4012. struct intel_encoder *encoder;
  4013. int pipe = intel_crtc->pipe;
  4014. struct intel_crtc_state *pipe_config =
  4015. to_intel_crtc_state(crtc->state);
  4016. if (WARN_ON(intel_crtc->active))
  4017. return;
  4018. /*
  4019. * Sometimes spurious CPU pipe underruns happen during FDI
  4020. * training, at least with VGA+HDMI cloning. Suppress them.
  4021. *
  4022. * On ILK we get an occasional spurious CPU pipe underruns
  4023. * between eDP port A enable and vdd enable. Also PCH port
  4024. * enable seems to result in the occasional CPU pipe underrun.
  4025. *
  4026. * Spurious PCH underruns also occur during PCH enabling.
  4027. */
  4028. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4029. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4030. if (intel_crtc->config->has_pch_encoder)
  4031. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4032. if (intel_crtc->config->has_pch_encoder)
  4033. intel_prepare_shared_dpll(intel_crtc);
  4034. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4035. intel_dp_set_m_n(intel_crtc, M1_N1);
  4036. intel_set_pipe_timings(intel_crtc);
  4037. intel_set_pipe_src_size(intel_crtc);
  4038. if (intel_crtc->config->has_pch_encoder) {
  4039. intel_cpu_transcoder_set_m_n(intel_crtc,
  4040. &intel_crtc->config->fdi_m_n, NULL);
  4041. }
  4042. ironlake_set_pipeconf(crtc);
  4043. intel_crtc->active = true;
  4044. for_each_encoder_on_crtc(dev, crtc, encoder)
  4045. if (encoder->pre_enable)
  4046. encoder->pre_enable(encoder);
  4047. if (intel_crtc->config->has_pch_encoder) {
  4048. /* Note: FDI PLL enabling _must_ be done before we enable the
  4049. * cpu pipes, hence this is separate from all the other fdi/pch
  4050. * enabling. */
  4051. ironlake_fdi_pll_enable(intel_crtc);
  4052. } else {
  4053. assert_fdi_tx_disabled(dev_priv, pipe);
  4054. assert_fdi_rx_disabled(dev_priv, pipe);
  4055. }
  4056. ironlake_pfit_enable(intel_crtc);
  4057. /*
  4058. * On ILK+ LUT must be loaded before the pipe is running but with
  4059. * clocks enabled
  4060. */
  4061. intel_color_load_luts(&pipe_config->base);
  4062. if (dev_priv->display.initial_watermarks != NULL)
  4063. dev_priv->display.initial_watermarks(intel_crtc->config);
  4064. intel_enable_pipe(intel_crtc);
  4065. if (intel_crtc->config->has_pch_encoder)
  4066. ironlake_pch_enable(crtc);
  4067. assert_vblank_disabled(crtc);
  4068. drm_crtc_vblank_on(crtc);
  4069. for_each_encoder_on_crtc(dev, crtc, encoder)
  4070. encoder->enable(encoder);
  4071. if (HAS_PCH_CPT(dev))
  4072. cpt_verify_modeset(dev, intel_crtc->pipe);
  4073. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4074. if (intel_crtc->config->has_pch_encoder)
  4075. intel_wait_for_vblank(dev, pipe);
  4076. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4077. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4078. }
  4079. /* IPS only exists on ULT machines and is tied to pipe A. */
  4080. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4081. {
  4082. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4083. }
  4084. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4085. {
  4086. struct drm_device *dev = crtc->dev;
  4087. struct drm_i915_private *dev_priv = to_i915(dev);
  4088. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4089. struct intel_encoder *encoder;
  4090. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4091. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4092. struct intel_crtc_state *pipe_config =
  4093. to_intel_crtc_state(crtc->state);
  4094. if (WARN_ON(intel_crtc->active))
  4095. return;
  4096. if (intel_crtc->config->has_pch_encoder)
  4097. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4098. false);
  4099. for_each_encoder_on_crtc(dev, crtc, encoder)
  4100. if (encoder->pre_pll_enable)
  4101. encoder->pre_pll_enable(encoder);
  4102. if (intel_crtc->config->shared_dpll)
  4103. intel_enable_shared_dpll(intel_crtc);
  4104. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4105. intel_dp_set_m_n(intel_crtc, M1_N1);
  4106. if (!transcoder_is_dsi(cpu_transcoder))
  4107. intel_set_pipe_timings(intel_crtc);
  4108. intel_set_pipe_src_size(intel_crtc);
  4109. if (cpu_transcoder != TRANSCODER_EDP &&
  4110. !transcoder_is_dsi(cpu_transcoder)) {
  4111. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4112. intel_crtc->config->pixel_multiplier - 1);
  4113. }
  4114. if (intel_crtc->config->has_pch_encoder) {
  4115. intel_cpu_transcoder_set_m_n(intel_crtc,
  4116. &intel_crtc->config->fdi_m_n, NULL);
  4117. }
  4118. if (!transcoder_is_dsi(cpu_transcoder))
  4119. haswell_set_pipeconf(crtc);
  4120. haswell_set_pipemisc(crtc);
  4121. intel_color_set_csc(&pipe_config->base);
  4122. intel_crtc->active = true;
  4123. if (intel_crtc->config->has_pch_encoder)
  4124. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4125. else
  4126. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4127. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4128. if (encoder->pre_enable)
  4129. encoder->pre_enable(encoder);
  4130. }
  4131. if (intel_crtc->config->has_pch_encoder)
  4132. dev_priv->display.fdi_link_train(crtc);
  4133. if (!transcoder_is_dsi(cpu_transcoder))
  4134. intel_ddi_enable_pipe_clock(intel_crtc);
  4135. if (INTEL_INFO(dev)->gen >= 9)
  4136. skylake_pfit_enable(intel_crtc);
  4137. else
  4138. ironlake_pfit_enable(intel_crtc);
  4139. /*
  4140. * On ILK+ LUT must be loaded before the pipe is running but with
  4141. * clocks enabled
  4142. */
  4143. intel_color_load_luts(&pipe_config->base);
  4144. intel_ddi_set_pipe_settings(crtc);
  4145. if (!transcoder_is_dsi(cpu_transcoder))
  4146. intel_ddi_enable_transcoder_func(crtc);
  4147. if (dev_priv->display.initial_watermarks != NULL)
  4148. dev_priv->display.initial_watermarks(pipe_config);
  4149. else
  4150. intel_update_watermarks(crtc);
  4151. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4152. if (!transcoder_is_dsi(cpu_transcoder))
  4153. intel_enable_pipe(intel_crtc);
  4154. if (intel_crtc->config->has_pch_encoder)
  4155. lpt_pch_enable(crtc);
  4156. if (intel_crtc->config->dp_encoder_is_mst)
  4157. intel_ddi_set_vc_payload_alloc(crtc, true);
  4158. assert_vblank_disabled(crtc);
  4159. drm_crtc_vblank_on(crtc);
  4160. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4161. encoder->enable(encoder);
  4162. intel_opregion_notify_encoder(encoder, true);
  4163. }
  4164. if (intel_crtc->config->has_pch_encoder) {
  4165. intel_wait_for_vblank(dev, pipe);
  4166. intel_wait_for_vblank(dev, pipe);
  4167. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4168. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4169. true);
  4170. }
  4171. /* If we change the relative order between pipe/planes enabling, we need
  4172. * to change the workaround. */
  4173. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4174. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4175. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4176. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4177. }
  4178. }
  4179. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4180. {
  4181. struct drm_device *dev = crtc->base.dev;
  4182. struct drm_i915_private *dev_priv = to_i915(dev);
  4183. int pipe = crtc->pipe;
  4184. /* To avoid upsetting the power well on haswell only disable the pfit if
  4185. * it's in use. The hw state code will make sure we get this right. */
  4186. if (force || crtc->config->pch_pfit.enabled) {
  4187. I915_WRITE(PF_CTL(pipe), 0);
  4188. I915_WRITE(PF_WIN_POS(pipe), 0);
  4189. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4190. }
  4191. }
  4192. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4193. {
  4194. struct drm_device *dev = crtc->dev;
  4195. struct drm_i915_private *dev_priv = to_i915(dev);
  4196. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4197. struct intel_encoder *encoder;
  4198. int pipe = intel_crtc->pipe;
  4199. /*
  4200. * Sometimes spurious CPU pipe underruns happen when the
  4201. * pipe is already disabled, but FDI RX/TX is still enabled.
  4202. * Happens at least with VGA+HDMI cloning. Suppress them.
  4203. */
  4204. if (intel_crtc->config->has_pch_encoder) {
  4205. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4206. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4207. }
  4208. for_each_encoder_on_crtc(dev, crtc, encoder)
  4209. encoder->disable(encoder);
  4210. drm_crtc_vblank_off(crtc);
  4211. assert_vblank_disabled(crtc);
  4212. intel_disable_pipe(intel_crtc);
  4213. ironlake_pfit_disable(intel_crtc, false);
  4214. if (intel_crtc->config->has_pch_encoder)
  4215. ironlake_fdi_disable(crtc);
  4216. for_each_encoder_on_crtc(dev, crtc, encoder)
  4217. if (encoder->post_disable)
  4218. encoder->post_disable(encoder);
  4219. if (intel_crtc->config->has_pch_encoder) {
  4220. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4221. if (HAS_PCH_CPT(dev)) {
  4222. i915_reg_t reg;
  4223. u32 temp;
  4224. /* disable TRANS_DP_CTL */
  4225. reg = TRANS_DP_CTL(pipe);
  4226. temp = I915_READ(reg);
  4227. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4228. TRANS_DP_PORT_SEL_MASK);
  4229. temp |= TRANS_DP_PORT_SEL_NONE;
  4230. I915_WRITE(reg, temp);
  4231. /* disable DPLL_SEL */
  4232. temp = I915_READ(PCH_DPLL_SEL);
  4233. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4234. I915_WRITE(PCH_DPLL_SEL, temp);
  4235. }
  4236. ironlake_fdi_pll_disable(intel_crtc);
  4237. }
  4238. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4239. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4240. }
  4241. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4242. {
  4243. struct drm_device *dev = crtc->dev;
  4244. struct drm_i915_private *dev_priv = to_i915(dev);
  4245. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4246. struct intel_encoder *encoder;
  4247. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4248. if (intel_crtc->config->has_pch_encoder)
  4249. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4250. false);
  4251. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4252. intel_opregion_notify_encoder(encoder, false);
  4253. encoder->disable(encoder);
  4254. }
  4255. drm_crtc_vblank_off(crtc);
  4256. assert_vblank_disabled(crtc);
  4257. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4258. if (!transcoder_is_dsi(cpu_transcoder))
  4259. intel_disable_pipe(intel_crtc);
  4260. if (intel_crtc->config->dp_encoder_is_mst)
  4261. intel_ddi_set_vc_payload_alloc(crtc, false);
  4262. if (!transcoder_is_dsi(cpu_transcoder))
  4263. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4264. if (INTEL_INFO(dev)->gen >= 9)
  4265. skylake_scaler_disable(intel_crtc);
  4266. else
  4267. ironlake_pfit_disable(intel_crtc, false);
  4268. if (!transcoder_is_dsi(cpu_transcoder))
  4269. intel_ddi_disable_pipe_clock(intel_crtc);
  4270. for_each_encoder_on_crtc(dev, crtc, encoder)
  4271. if (encoder->post_disable)
  4272. encoder->post_disable(encoder);
  4273. if (intel_crtc->config->has_pch_encoder) {
  4274. lpt_disable_pch_transcoder(dev_priv);
  4275. lpt_disable_iclkip(dev_priv);
  4276. intel_ddi_fdi_disable(crtc);
  4277. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4278. true);
  4279. }
  4280. }
  4281. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4282. {
  4283. struct drm_device *dev = crtc->base.dev;
  4284. struct drm_i915_private *dev_priv = to_i915(dev);
  4285. struct intel_crtc_state *pipe_config = crtc->config;
  4286. if (!pipe_config->gmch_pfit.control)
  4287. return;
  4288. /*
  4289. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4290. * according to register description and PRM.
  4291. */
  4292. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4293. assert_pipe_disabled(dev_priv, crtc->pipe);
  4294. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4295. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4296. /* Border color in case we don't scale up to the full screen. Black by
  4297. * default, change to something else for debugging. */
  4298. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4299. }
  4300. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4301. {
  4302. switch (port) {
  4303. case PORT_A:
  4304. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4305. case PORT_B:
  4306. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4307. case PORT_C:
  4308. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4309. case PORT_D:
  4310. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4311. case PORT_E:
  4312. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4313. default:
  4314. MISSING_CASE(port);
  4315. return POWER_DOMAIN_PORT_OTHER;
  4316. }
  4317. }
  4318. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4319. {
  4320. switch (port) {
  4321. case PORT_A:
  4322. return POWER_DOMAIN_AUX_A;
  4323. case PORT_B:
  4324. return POWER_DOMAIN_AUX_B;
  4325. case PORT_C:
  4326. return POWER_DOMAIN_AUX_C;
  4327. case PORT_D:
  4328. return POWER_DOMAIN_AUX_D;
  4329. case PORT_E:
  4330. /* FIXME: Check VBT for actual wiring of PORT E */
  4331. return POWER_DOMAIN_AUX_D;
  4332. default:
  4333. MISSING_CASE(port);
  4334. return POWER_DOMAIN_AUX_A;
  4335. }
  4336. }
  4337. enum intel_display_power_domain
  4338. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4339. {
  4340. struct drm_device *dev = intel_encoder->base.dev;
  4341. struct intel_digital_port *intel_dig_port;
  4342. switch (intel_encoder->type) {
  4343. case INTEL_OUTPUT_UNKNOWN:
  4344. /* Only DDI platforms should ever use this output type */
  4345. WARN_ON_ONCE(!HAS_DDI(dev));
  4346. case INTEL_OUTPUT_DP:
  4347. case INTEL_OUTPUT_HDMI:
  4348. case INTEL_OUTPUT_EDP:
  4349. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4350. return port_to_power_domain(intel_dig_port->port);
  4351. case INTEL_OUTPUT_DP_MST:
  4352. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4353. return port_to_power_domain(intel_dig_port->port);
  4354. case INTEL_OUTPUT_ANALOG:
  4355. return POWER_DOMAIN_PORT_CRT;
  4356. case INTEL_OUTPUT_DSI:
  4357. return POWER_DOMAIN_PORT_DSI;
  4358. default:
  4359. return POWER_DOMAIN_PORT_OTHER;
  4360. }
  4361. }
  4362. enum intel_display_power_domain
  4363. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4364. {
  4365. struct drm_device *dev = intel_encoder->base.dev;
  4366. struct intel_digital_port *intel_dig_port;
  4367. switch (intel_encoder->type) {
  4368. case INTEL_OUTPUT_UNKNOWN:
  4369. case INTEL_OUTPUT_HDMI:
  4370. /*
  4371. * Only DDI platforms should ever use these output types.
  4372. * We can get here after the HDMI detect code has already set
  4373. * the type of the shared encoder. Since we can't be sure
  4374. * what's the status of the given connectors, play safe and
  4375. * run the DP detection too.
  4376. */
  4377. WARN_ON_ONCE(!HAS_DDI(dev));
  4378. case INTEL_OUTPUT_DP:
  4379. case INTEL_OUTPUT_EDP:
  4380. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4381. return port_to_aux_power_domain(intel_dig_port->port);
  4382. case INTEL_OUTPUT_DP_MST:
  4383. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4384. return port_to_aux_power_domain(intel_dig_port->port);
  4385. default:
  4386. MISSING_CASE(intel_encoder->type);
  4387. return POWER_DOMAIN_AUX_A;
  4388. }
  4389. }
  4390. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4391. struct intel_crtc_state *crtc_state)
  4392. {
  4393. struct drm_device *dev = crtc->dev;
  4394. struct drm_encoder *encoder;
  4395. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4396. enum pipe pipe = intel_crtc->pipe;
  4397. unsigned long mask;
  4398. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4399. if (!crtc_state->base.active)
  4400. return 0;
  4401. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4402. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4403. if (crtc_state->pch_pfit.enabled ||
  4404. crtc_state->pch_pfit.force_thru)
  4405. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4406. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4407. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4408. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4409. }
  4410. if (crtc_state->shared_dpll)
  4411. mask |= BIT(POWER_DOMAIN_PLLS);
  4412. return mask;
  4413. }
  4414. static unsigned long
  4415. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4416. struct intel_crtc_state *crtc_state)
  4417. {
  4418. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4419. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4420. enum intel_display_power_domain domain;
  4421. unsigned long domains, new_domains, old_domains;
  4422. old_domains = intel_crtc->enabled_power_domains;
  4423. intel_crtc->enabled_power_domains = new_domains =
  4424. get_crtc_power_domains(crtc, crtc_state);
  4425. domains = new_domains & ~old_domains;
  4426. for_each_power_domain(domain, domains)
  4427. intel_display_power_get(dev_priv, domain);
  4428. return old_domains & ~new_domains;
  4429. }
  4430. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4431. unsigned long domains)
  4432. {
  4433. enum intel_display_power_domain domain;
  4434. for_each_power_domain(domain, domains)
  4435. intel_display_power_put(dev_priv, domain);
  4436. }
  4437. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4438. {
  4439. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4440. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4441. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4442. return max_cdclk_freq;
  4443. else if (IS_CHERRYVIEW(dev_priv))
  4444. return max_cdclk_freq*95/100;
  4445. else if (INTEL_INFO(dev_priv)->gen < 4)
  4446. return 2*max_cdclk_freq*90/100;
  4447. else
  4448. return max_cdclk_freq*90/100;
  4449. }
  4450. static int skl_calc_cdclk(int max_pixclk, int vco);
  4451. static void intel_update_max_cdclk(struct drm_device *dev)
  4452. {
  4453. struct drm_i915_private *dev_priv = to_i915(dev);
  4454. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4455. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4456. int max_cdclk, vco;
  4457. vco = dev_priv->skl_preferred_vco_freq;
  4458. WARN_ON(vco != 8100000 && vco != 8640000);
  4459. /*
  4460. * Use the lower (vco 8640) cdclk values as a
  4461. * first guess. skl_calc_cdclk() will correct it
  4462. * if the preferred vco is 8100 instead.
  4463. */
  4464. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4465. max_cdclk = 617143;
  4466. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4467. max_cdclk = 540000;
  4468. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4469. max_cdclk = 432000;
  4470. else
  4471. max_cdclk = 308571;
  4472. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4473. } else if (IS_BROXTON(dev)) {
  4474. dev_priv->max_cdclk_freq = 624000;
  4475. } else if (IS_BROADWELL(dev)) {
  4476. /*
  4477. * FIXME with extra cooling we can allow
  4478. * 540 MHz for ULX and 675 Mhz for ULT.
  4479. * How can we know if extra cooling is
  4480. * available? PCI ID, VTB, something else?
  4481. */
  4482. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4483. dev_priv->max_cdclk_freq = 450000;
  4484. else if (IS_BDW_ULX(dev))
  4485. dev_priv->max_cdclk_freq = 450000;
  4486. else if (IS_BDW_ULT(dev))
  4487. dev_priv->max_cdclk_freq = 540000;
  4488. else
  4489. dev_priv->max_cdclk_freq = 675000;
  4490. } else if (IS_CHERRYVIEW(dev)) {
  4491. dev_priv->max_cdclk_freq = 320000;
  4492. } else if (IS_VALLEYVIEW(dev)) {
  4493. dev_priv->max_cdclk_freq = 400000;
  4494. } else {
  4495. /* otherwise assume cdclk is fixed */
  4496. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4497. }
  4498. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4499. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4500. dev_priv->max_cdclk_freq);
  4501. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4502. dev_priv->max_dotclk_freq);
  4503. }
  4504. static void intel_update_cdclk(struct drm_device *dev)
  4505. {
  4506. struct drm_i915_private *dev_priv = to_i915(dev);
  4507. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4508. if (INTEL_GEN(dev_priv) >= 9)
  4509. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  4510. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  4511. dev_priv->cdclk_pll.ref);
  4512. else
  4513. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4514. dev_priv->cdclk_freq);
  4515. /*
  4516. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4517. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4518. * of cdclk that generates 4MHz reference clock freq which is used to
  4519. * generate GMBus clock. This will vary with the cdclk freq.
  4520. */
  4521. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4522. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4523. }
  4524. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4525. static int skl_cdclk_decimal(int cdclk)
  4526. {
  4527. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  4528. }
  4529. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  4530. {
  4531. int ratio;
  4532. if (cdclk == dev_priv->cdclk_pll.ref)
  4533. return 0;
  4534. switch (cdclk) {
  4535. default:
  4536. MISSING_CASE(cdclk);
  4537. case 144000:
  4538. case 288000:
  4539. case 384000:
  4540. case 576000:
  4541. ratio = 60;
  4542. break;
  4543. case 624000:
  4544. ratio = 65;
  4545. break;
  4546. }
  4547. return dev_priv->cdclk_pll.ref * ratio;
  4548. }
  4549. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  4550. {
  4551. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  4552. /* Timeout 200us */
  4553. if (intel_wait_for_register(dev_priv,
  4554. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  4555. 1))
  4556. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  4557. dev_priv->cdclk_pll.vco = 0;
  4558. }
  4559. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  4560. {
  4561. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  4562. u32 val;
  4563. val = I915_READ(BXT_DE_PLL_CTL);
  4564. val &= ~BXT_DE_PLL_RATIO_MASK;
  4565. val |= BXT_DE_PLL_RATIO(ratio);
  4566. I915_WRITE(BXT_DE_PLL_CTL, val);
  4567. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4568. /* Timeout 200us */
  4569. if (intel_wait_for_register(dev_priv,
  4570. BXT_DE_PLL_ENABLE,
  4571. BXT_DE_PLL_LOCK,
  4572. BXT_DE_PLL_LOCK,
  4573. 1))
  4574. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4575. dev_priv->cdclk_pll.vco = vco;
  4576. }
  4577. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  4578. {
  4579. u32 val, divider;
  4580. int vco, ret;
  4581. vco = bxt_de_pll_vco(dev_priv, cdclk);
  4582. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  4583. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  4584. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  4585. case 8:
  4586. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4587. break;
  4588. case 4:
  4589. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4590. break;
  4591. case 3:
  4592. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4593. break;
  4594. case 2:
  4595. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4596. break;
  4597. default:
  4598. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  4599. WARN_ON(vco != 0);
  4600. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4601. break;
  4602. }
  4603. /* Inform power controller of upcoming frequency change */
  4604. mutex_lock(&dev_priv->rps.hw_lock);
  4605. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4606. 0x80000000);
  4607. mutex_unlock(&dev_priv->rps.hw_lock);
  4608. if (ret) {
  4609. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4610. ret, cdclk);
  4611. return;
  4612. }
  4613. if (dev_priv->cdclk_pll.vco != 0 &&
  4614. dev_priv->cdclk_pll.vco != vco)
  4615. bxt_de_pll_disable(dev_priv);
  4616. if (dev_priv->cdclk_pll.vco != vco)
  4617. bxt_de_pll_enable(dev_priv, vco);
  4618. val = divider | skl_cdclk_decimal(cdclk);
  4619. /*
  4620. * FIXME if only the cd2x divider needs changing, it could be done
  4621. * without shutting off the pipe (if only one pipe is active).
  4622. */
  4623. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  4624. /*
  4625. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4626. * enable otherwise.
  4627. */
  4628. if (cdclk >= 500000)
  4629. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4630. I915_WRITE(CDCLK_CTL, val);
  4631. mutex_lock(&dev_priv->rps.hw_lock);
  4632. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4633. DIV_ROUND_UP(cdclk, 25000));
  4634. mutex_unlock(&dev_priv->rps.hw_lock);
  4635. if (ret) {
  4636. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4637. ret, cdclk);
  4638. return;
  4639. }
  4640. intel_update_cdclk(&dev_priv->drm);
  4641. }
  4642. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4643. {
  4644. u32 cdctl, expected;
  4645. intel_update_cdclk(&dev_priv->drm);
  4646. if (dev_priv->cdclk_pll.vco == 0 ||
  4647. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  4648. goto sanitize;
  4649. /* DPLL okay; verify the cdclock
  4650. *
  4651. * Some BIOS versions leave an incorrect decimal frequency value and
  4652. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  4653. * so sanitize this register.
  4654. */
  4655. cdctl = I915_READ(CDCLK_CTL);
  4656. /*
  4657. * Let's ignore the pipe field, since BIOS could have configured the
  4658. * dividers both synching to an active pipe, or asynchronously
  4659. * (PIPE_NONE).
  4660. */
  4661. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  4662. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  4663. skl_cdclk_decimal(dev_priv->cdclk_freq);
  4664. /*
  4665. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4666. * enable otherwise.
  4667. */
  4668. if (dev_priv->cdclk_freq >= 500000)
  4669. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4670. if (cdctl == expected)
  4671. /* All well; nothing to sanitize */
  4672. return;
  4673. sanitize:
  4674. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  4675. /* force cdclk programming */
  4676. dev_priv->cdclk_freq = 0;
  4677. /* force full PLL disable + enable */
  4678. dev_priv->cdclk_pll.vco = -1;
  4679. }
  4680. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  4681. {
  4682. bxt_sanitize_cdclk(dev_priv);
  4683. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  4684. return;
  4685. /*
  4686. * FIXME:
  4687. * - The initial CDCLK needs to be read from VBT.
  4688. * Need to make this change after VBT has changes for BXT.
  4689. */
  4690. bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
  4691. }
  4692. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  4693. {
  4694. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  4695. }
  4696. static int skl_calc_cdclk(int max_pixclk, int vco)
  4697. {
  4698. if (vco == 8640000) {
  4699. if (max_pixclk > 540000)
  4700. return 617143;
  4701. else if (max_pixclk > 432000)
  4702. return 540000;
  4703. else if (max_pixclk > 308571)
  4704. return 432000;
  4705. else
  4706. return 308571;
  4707. } else {
  4708. if (max_pixclk > 540000)
  4709. return 675000;
  4710. else if (max_pixclk > 450000)
  4711. return 540000;
  4712. else if (max_pixclk > 337500)
  4713. return 450000;
  4714. else
  4715. return 337500;
  4716. }
  4717. }
  4718. static void
  4719. skl_dpll0_update(struct drm_i915_private *dev_priv)
  4720. {
  4721. u32 val;
  4722. dev_priv->cdclk_pll.ref = 24000;
  4723. dev_priv->cdclk_pll.vco = 0;
  4724. val = I915_READ(LCPLL1_CTL);
  4725. if ((val & LCPLL_PLL_ENABLE) == 0)
  4726. return;
  4727. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  4728. return;
  4729. val = I915_READ(DPLL_CTRL1);
  4730. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  4731. DPLL_CTRL1_SSC(SKL_DPLL0) |
  4732. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  4733. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  4734. return;
  4735. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  4736. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  4737. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  4738. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  4739. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  4740. dev_priv->cdclk_pll.vco = 8100000;
  4741. break;
  4742. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  4743. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  4744. dev_priv->cdclk_pll.vco = 8640000;
  4745. break;
  4746. default:
  4747. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4748. break;
  4749. }
  4750. }
  4751. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  4752. {
  4753. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  4754. dev_priv->skl_preferred_vco_freq = vco;
  4755. if (changed)
  4756. intel_update_max_cdclk(&dev_priv->drm);
  4757. }
  4758. static void
  4759. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  4760. {
  4761. int min_cdclk = skl_calc_cdclk(0, vco);
  4762. u32 val;
  4763. WARN_ON(vco != 8100000 && vco != 8640000);
  4764. /* select the minimum CDCLK before enabling DPLL 0 */
  4765. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  4766. I915_WRITE(CDCLK_CTL, val);
  4767. POSTING_READ(CDCLK_CTL);
  4768. /*
  4769. * We always enable DPLL0 with the lowest link rate possible, but still
  4770. * taking into account the VCO required to operate the eDP panel at the
  4771. * desired frequency. The usual DP link rates operate with a VCO of
  4772. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4773. * The modeset code is responsible for the selection of the exact link
  4774. * rate later on, with the constraint of choosing a frequency that
  4775. * works with vco.
  4776. */
  4777. val = I915_READ(DPLL_CTRL1);
  4778. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4779. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4780. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4781. if (vco == 8640000)
  4782. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4783. SKL_DPLL0);
  4784. else
  4785. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4786. SKL_DPLL0);
  4787. I915_WRITE(DPLL_CTRL1, val);
  4788. POSTING_READ(DPLL_CTRL1);
  4789. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4790. if (intel_wait_for_register(dev_priv,
  4791. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  4792. 5))
  4793. DRM_ERROR("DPLL0 not locked\n");
  4794. dev_priv->cdclk_pll.vco = vco;
  4795. /* We'll want to keep using the current vco from now on. */
  4796. skl_set_preferred_cdclk_vco(dev_priv, vco);
  4797. }
  4798. static void
  4799. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  4800. {
  4801. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4802. if (intel_wait_for_register(dev_priv,
  4803. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  4804. 1))
  4805. DRM_ERROR("Couldn't disable DPLL0\n");
  4806. dev_priv->cdclk_pll.vco = 0;
  4807. }
  4808. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4809. {
  4810. int ret;
  4811. u32 val;
  4812. /* inform PCU we want to change CDCLK */
  4813. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4814. mutex_lock(&dev_priv->rps.hw_lock);
  4815. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4816. mutex_unlock(&dev_priv->rps.hw_lock);
  4817. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4818. }
  4819. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4820. {
  4821. return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
  4822. }
  4823. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  4824. {
  4825. struct drm_device *dev = &dev_priv->drm;
  4826. u32 freq_select, pcu_ack;
  4827. WARN_ON((cdclk == 24000) != (vco == 0));
  4828. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  4829. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4830. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4831. return;
  4832. }
  4833. /* set CDCLK_CTL */
  4834. switch (cdclk) {
  4835. case 450000:
  4836. case 432000:
  4837. freq_select = CDCLK_FREQ_450_432;
  4838. pcu_ack = 1;
  4839. break;
  4840. case 540000:
  4841. freq_select = CDCLK_FREQ_540;
  4842. pcu_ack = 2;
  4843. break;
  4844. case 308571:
  4845. case 337500:
  4846. default:
  4847. freq_select = CDCLK_FREQ_337_308;
  4848. pcu_ack = 0;
  4849. break;
  4850. case 617143:
  4851. case 675000:
  4852. freq_select = CDCLK_FREQ_675_617;
  4853. pcu_ack = 3;
  4854. break;
  4855. }
  4856. if (dev_priv->cdclk_pll.vco != 0 &&
  4857. dev_priv->cdclk_pll.vco != vco)
  4858. skl_dpll0_disable(dev_priv);
  4859. if (dev_priv->cdclk_pll.vco != vco)
  4860. skl_dpll0_enable(dev_priv, vco);
  4861. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  4862. POSTING_READ(CDCLK_CTL);
  4863. /* inform PCU of the change */
  4864. mutex_lock(&dev_priv->rps.hw_lock);
  4865. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4866. mutex_unlock(&dev_priv->rps.hw_lock);
  4867. intel_update_cdclk(dev);
  4868. }
  4869. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  4870. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4871. {
  4872. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  4873. }
  4874. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4875. {
  4876. int cdclk, vco;
  4877. skl_sanitize_cdclk(dev_priv);
  4878. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  4879. /*
  4880. * Use the current vco as our initial
  4881. * guess as to what the preferred vco is.
  4882. */
  4883. if (dev_priv->skl_preferred_vco_freq == 0)
  4884. skl_set_preferred_cdclk_vco(dev_priv,
  4885. dev_priv->cdclk_pll.vco);
  4886. return;
  4887. }
  4888. vco = dev_priv->skl_preferred_vco_freq;
  4889. if (vco == 0)
  4890. vco = 8100000;
  4891. cdclk = skl_calc_cdclk(0, vco);
  4892. skl_set_cdclk(dev_priv, cdclk, vco);
  4893. }
  4894. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4895. {
  4896. uint32_t cdctl, expected;
  4897. /*
  4898. * check if the pre-os intialized the display
  4899. * There is SWF18 scratchpad register defined which is set by the
  4900. * pre-os which can be used by the OS drivers to check the status
  4901. */
  4902. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4903. goto sanitize;
  4904. intel_update_cdclk(&dev_priv->drm);
  4905. /* Is PLL enabled and locked ? */
  4906. if (dev_priv->cdclk_pll.vco == 0 ||
  4907. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  4908. goto sanitize;
  4909. /* DPLL okay; verify the cdclock
  4910. *
  4911. * Noticed in some instances that the freq selection is correct but
  4912. * decimal part is programmed wrong from BIOS where pre-os does not
  4913. * enable display. Verify the same as well.
  4914. */
  4915. cdctl = I915_READ(CDCLK_CTL);
  4916. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  4917. skl_cdclk_decimal(dev_priv->cdclk_freq);
  4918. if (cdctl == expected)
  4919. /* All well; nothing to sanitize */
  4920. return;
  4921. sanitize:
  4922. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  4923. /* force cdclk programming */
  4924. dev_priv->cdclk_freq = 0;
  4925. /* force full PLL disable + enable */
  4926. dev_priv->cdclk_pll.vco = -1;
  4927. }
  4928. /* Adjust CDclk dividers to allow high res or save power if possible */
  4929. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4930. {
  4931. struct drm_i915_private *dev_priv = to_i915(dev);
  4932. u32 val, cmd;
  4933. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4934. != dev_priv->cdclk_freq);
  4935. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4936. cmd = 2;
  4937. else if (cdclk == 266667)
  4938. cmd = 1;
  4939. else
  4940. cmd = 0;
  4941. mutex_lock(&dev_priv->rps.hw_lock);
  4942. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4943. val &= ~DSPFREQGUAR_MASK;
  4944. val |= (cmd << DSPFREQGUAR_SHIFT);
  4945. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4946. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4947. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4948. 50)) {
  4949. DRM_ERROR("timed out waiting for CDclk change\n");
  4950. }
  4951. mutex_unlock(&dev_priv->rps.hw_lock);
  4952. mutex_lock(&dev_priv->sb_lock);
  4953. if (cdclk == 400000) {
  4954. u32 divider;
  4955. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4956. /* adjust cdclk divider */
  4957. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4958. val &= ~CCK_FREQUENCY_VALUES;
  4959. val |= divider;
  4960. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4961. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4962. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4963. 50))
  4964. DRM_ERROR("timed out waiting for CDclk change\n");
  4965. }
  4966. /* adjust self-refresh exit latency value */
  4967. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4968. val &= ~0x7f;
  4969. /*
  4970. * For high bandwidth configs, we set a higher latency in the bunit
  4971. * so that the core display fetch happens in time to avoid underruns.
  4972. */
  4973. if (cdclk == 400000)
  4974. val |= 4500 / 250; /* 4.5 usec */
  4975. else
  4976. val |= 3000 / 250; /* 3.0 usec */
  4977. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4978. mutex_unlock(&dev_priv->sb_lock);
  4979. intel_update_cdclk(dev);
  4980. }
  4981. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4982. {
  4983. struct drm_i915_private *dev_priv = to_i915(dev);
  4984. u32 val, cmd;
  4985. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4986. != dev_priv->cdclk_freq);
  4987. switch (cdclk) {
  4988. case 333333:
  4989. case 320000:
  4990. case 266667:
  4991. case 200000:
  4992. break;
  4993. default:
  4994. MISSING_CASE(cdclk);
  4995. return;
  4996. }
  4997. /*
  4998. * Specs are full of misinformation, but testing on actual
  4999. * hardware has shown that we just need to write the desired
  5000. * CCK divider into the Punit register.
  5001. */
  5002. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5003. mutex_lock(&dev_priv->rps.hw_lock);
  5004. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5005. val &= ~DSPFREQGUAR_MASK_CHV;
  5006. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5007. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5008. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5009. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5010. 50)) {
  5011. DRM_ERROR("timed out waiting for CDclk change\n");
  5012. }
  5013. mutex_unlock(&dev_priv->rps.hw_lock);
  5014. intel_update_cdclk(dev);
  5015. }
  5016. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5017. int max_pixclk)
  5018. {
  5019. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5020. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5021. /*
  5022. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5023. * 200MHz
  5024. * 267MHz
  5025. * 320/333MHz (depends on HPLL freq)
  5026. * 400MHz (VLV only)
  5027. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5028. * of the lower bin and adjust if needed.
  5029. *
  5030. * We seem to get an unstable or solid color picture at 200MHz.
  5031. * Not sure what's wrong. For now use 200MHz only when all pipes
  5032. * are off.
  5033. */
  5034. if (!IS_CHERRYVIEW(dev_priv) &&
  5035. max_pixclk > freq_320*limit/100)
  5036. return 400000;
  5037. else if (max_pixclk > 266667*limit/100)
  5038. return freq_320;
  5039. else if (max_pixclk > 0)
  5040. return 266667;
  5041. else
  5042. return 200000;
  5043. }
  5044. static int bxt_calc_cdclk(int max_pixclk)
  5045. {
  5046. if (max_pixclk > 576000)
  5047. return 624000;
  5048. else if (max_pixclk > 384000)
  5049. return 576000;
  5050. else if (max_pixclk > 288000)
  5051. return 384000;
  5052. else if (max_pixclk > 144000)
  5053. return 288000;
  5054. else
  5055. return 144000;
  5056. }
  5057. /* Compute the max pixel clock for new configuration. */
  5058. static int intel_mode_max_pixclk(struct drm_device *dev,
  5059. struct drm_atomic_state *state)
  5060. {
  5061. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5062. struct drm_i915_private *dev_priv = to_i915(dev);
  5063. struct drm_crtc *crtc;
  5064. struct drm_crtc_state *crtc_state;
  5065. unsigned max_pixclk = 0, i;
  5066. enum pipe pipe;
  5067. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5068. sizeof(intel_state->min_pixclk));
  5069. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5070. int pixclk = 0;
  5071. if (crtc_state->enable)
  5072. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5073. intel_state->min_pixclk[i] = pixclk;
  5074. }
  5075. for_each_pipe(dev_priv, pipe)
  5076. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5077. return max_pixclk;
  5078. }
  5079. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5080. {
  5081. struct drm_device *dev = state->dev;
  5082. struct drm_i915_private *dev_priv = to_i915(dev);
  5083. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5084. struct intel_atomic_state *intel_state =
  5085. to_intel_atomic_state(state);
  5086. intel_state->cdclk = intel_state->dev_cdclk =
  5087. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5088. if (!intel_state->active_crtcs)
  5089. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5090. return 0;
  5091. }
  5092. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  5093. {
  5094. int max_pixclk = ilk_max_pixel_rate(state);
  5095. struct intel_atomic_state *intel_state =
  5096. to_intel_atomic_state(state);
  5097. intel_state->cdclk = intel_state->dev_cdclk =
  5098. bxt_calc_cdclk(max_pixclk);
  5099. if (!intel_state->active_crtcs)
  5100. intel_state->dev_cdclk = bxt_calc_cdclk(0);
  5101. return 0;
  5102. }
  5103. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5104. {
  5105. unsigned int credits, default_credits;
  5106. if (IS_CHERRYVIEW(dev_priv))
  5107. default_credits = PFI_CREDIT(12);
  5108. else
  5109. default_credits = PFI_CREDIT(8);
  5110. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5111. /* CHV suggested value is 31 or 63 */
  5112. if (IS_CHERRYVIEW(dev_priv))
  5113. credits = PFI_CREDIT_63;
  5114. else
  5115. credits = PFI_CREDIT(15);
  5116. } else {
  5117. credits = default_credits;
  5118. }
  5119. /*
  5120. * WA - write default credits before re-programming
  5121. * FIXME: should we also set the resend bit here?
  5122. */
  5123. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5124. default_credits);
  5125. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5126. credits | PFI_CREDIT_RESEND);
  5127. /*
  5128. * FIXME is this guaranteed to clear
  5129. * immediately or should we poll for it?
  5130. */
  5131. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5132. }
  5133. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5134. {
  5135. struct drm_device *dev = old_state->dev;
  5136. struct drm_i915_private *dev_priv = to_i915(dev);
  5137. struct intel_atomic_state *old_intel_state =
  5138. to_intel_atomic_state(old_state);
  5139. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5140. /*
  5141. * FIXME: We can end up here with all power domains off, yet
  5142. * with a CDCLK frequency other than the minimum. To account
  5143. * for this take the PIPE-A power domain, which covers the HW
  5144. * blocks needed for the following programming. This can be
  5145. * removed once it's guaranteed that we get here either with
  5146. * the minimum CDCLK set, or the required power domains
  5147. * enabled.
  5148. */
  5149. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5150. if (IS_CHERRYVIEW(dev))
  5151. cherryview_set_cdclk(dev, req_cdclk);
  5152. else
  5153. valleyview_set_cdclk(dev, req_cdclk);
  5154. vlv_program_pfi_credits(dev_priv);
  5155. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5156. }
  5157. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5158. {
  5159. struct drm_device *dev = crtc->dev;
  5160. struct drm_i915_private *dev_priv = to_i915(dev);
  5161. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5162. struct intel_encoder *encoder;
  5163. struct intel_crtc_state *pipe_config =
  5164. to_intel_crtc_state(crtc->state);
  5165. int pipe = intel_crtc->pipe;
  5166. if (WARN_ON(intel_crtc->active))
  5167. return;
  5168. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5169. intel_dp_set_m_n(intel_crtc, M1_N1);
  5170. intel_set_pipe_timings(intel_crtc);
  5171. intel_set_pipe_src_size(intel_crtc);
  5172. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5173. struct drm_i915_private *dev_priv = to_i915(dev);
  5174. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5175. I915_WRITE(CHV_CANVAS(pipe), 0);
  5176. }
  5177. i9xx_set_pipeconf(intel_crtc);
  5178. intel_crtc->active = true;
  5179. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5180. for_each_encoder_on_crtc(dev, crtc, encoder)
  5181. if (encoder->pre_pll_enable)
  5182. encoder->pre_pll_enable(encoder);
  5183. if (IS_CHERRYVIEW(dev)) {
  5184. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5185. chv_enable_pll(intel_crtc, intel_crtc->config);
  5186. } else {
  5187. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5188. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5189. }
  5190. for_each_encoder_on_crtc(dev, crtc, encoder)
  5191. if (encoder->pre_enable)
  5192. encoder->pre_enable(encoder);
  5193. i9xx_pfit_enable(intel_crtc);
  5194. intel_color_load_luts(&pipe_config->base);
  5195. intel_update_watermarks(crtc);
  5196. intel_enable_pipe(intel_crtc);
  5197. assert_vblank_disabled(crtc);
  5198. drm_crtc_vblank_on(crtc);
  5199. for_each_encoder_on_crtc(dev, crtc, encoder)
  5200. encoder->enable(encoder);
  5201. }
  5202. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5203. {
  5204. struct drm_device *dev = crtc->base.dev;
  5205. struct drm_i915_private *dev_priv = to_i915(dev);
  5206. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5207. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5208. }
  5209. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5210. {
  5211. struct drm_device *dev = crtc->dev;
  5212. struct drm_i915_private *dev_priv = to_i915(dev);
  5213. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5214. struct intel_encoder *encoder;
  5215. struct intel_crtc_state *pipe_config =
  5216. to_intel_crtc_state(crtc->state);
  5217. enum pipe pipe = intel_crtc->pipe;
  5218. if (WARN_ON(intel_crtc->active))
  5219. return;
  5220. i9xx_set_pll_dividers(intel_crtc);
  5221. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5222. intel_dp_set_m_n(intel_crtc, M1_N1);
  5223. intel_set_pipe_timings(intel_crtc);
  5224. intel_set_pipe_src_size(intel_crtc);
  5225. i9xx_set_pipeconf(intel_crtc);
  5226. intel_crtc->active = true;
  5227. if (!IS_GEN2(dev))
  5228. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5229. for_each_encoder_on_crtc(dev, crtc, encoder)
  5230. if (encoder->pre_enable)
  5231. encoder->pre_enable(encoder);
  5232. i9xx_enable_pll(intel_crtc);
  5233. i9xx_pfit_enable(intel_crtc);
  5234. intel_color_load_luts(&pipe_config->base);
  5235. intel_update_watermarks(crtc);
  5236. intel_enable_pipe(intel_crtc);
  5237. assert_vblank_disabled(crtc);
  5238. drm_crtc_vblank_on(crtc);
  5239. for_each_encoder_on_crtc(dev, crtc, encoder)
  5240. encoder->enable(encoder);
  5241. }
  5242. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5243. {
  5244. struct drm_device *dev = crtc->base.dev;
  5245. struct drm_i915_private *dev_priv = to_i915(dev);
  5246. if (!crtc->config->gmch_pfit.control)
  5247. return;
  5248. assert_pipe_disabled(dev_priv, crtc->pipe);
  5249. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5250. I915_READ(PFIT_CONTROL));
  5251. I915_WRITE(PFIT_CONTROL, 0);
  5252. }
  5253. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5254. {
  5255. struct drm_device *dev = crtc->dev;
  5256. struct drm_i915_private *dev_priv = to_i915(dev);
  5257. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5258. struct intel_encoder *encoder;
  5259. int pipe = intel_crtc->pipe;
  5260. /*
  5261. * On gen2 planes are double buffered but the pipe isn't, so we must
  5262. * wait for planes to fully turn off before disabling the pipe.
  5263. */
  5264. if (IS_GEN2(dev))
  5265. intel_wait_for_vblank(dev, pipe);
  5266. for_each_encoder_on_crtc(dev, crtc, encoder)
  5267. encoder->disable(encoder);
  5268. drm_crtc_vblank_off(crtc);
  5269. assert_vblank_disabled(crtc);
  5270. intel_disable_pipe(intel_crtc);
  5271. i9xx_pfit_disable(intel_crtc);
  5272. for_each_encoder_on_crtc(dev, crtc, encoder)
  5273. if (encoder->post_disable)
  5274. encoder->post_disable(encoder);
  5275. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5276. if (IS_CHERRYVIEW(dev))
  5277. chv_disable_pll(dev_priv, pipe);
  5278. else if (IS_VALLEYVIEW(dev))
  5279. vlv_disable_pll(dev_priv, pipe);
  5280. else
  5281. i9xx_disable_pll(intel_crtc);
  5282. }
  5283. for_each_encoder_on_crtc(dev, crtc, encoder)
  5284. if (encoder->post_pll_disable)
  5285. encoder->post_pll_disable(encoder);
  5286. if (!IS_GEN2(dev))
  5287. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5288. }
  5289. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5290. {
  5291. struct intel_encoder *encoder;
  5292. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5293. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5294. enum intel_display_power_domain domain;
  5295. unsigned long domains;
  5296. if (!intel_crtc->active)
  5297. return;
  5298. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5299. WARN_ON(intel_crtc->flip_work);
  5300. intel_pre_disable_primary_noatomic(crtc);
  5301. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5302. to_intel_plane_state(crtc->primary->state)->visible = false;
  5303. }
  5304. dev_priv->display.crtc_disable(crtc);
  5305. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5306. crtc->base.id, crtc->name);
  5307. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5308. crtc->state->active = false;
  5309. intel_crtc->active = false;
  5310. crtc->enabled = false;
  5311. crtc->state->connector_mask = 0;
  5312. crtc->state->encoder_mask = 0;
  5313. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5314. encoder->base.crtc = NULL;
  5315. intel_fbc_disable(intel_crtc);
  5316. intel_update_watermarks(crtc);
  5317. intel_disable_shared_dpll(intel_crtc);
  5318. domains = intel_crtc->enabled_power_domains;
  5319. for_each_power_domain(domain, domains)
  5320. intel_display_power_put(dev_priv, domain);
  5321. intel_crtc->enabled_power_domains = 0;
  5322. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5323. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5324. }
  5325. /*
  5326. * turn all crtc's off, but do not adjust state
  5327. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5328. */
  5329. int intel_display_suspend(struct drm_device *dev)
  5330. {
  5331. struct drm_i915_private *dev_priv = to_i915(dev);
  5332. struct drm_atomic_state *state;
  5333. int ret;
  5334. state = drm_atomic_helper_suspend(dev);
  5335. ret = PTR_ERR_OR_ZERO(state);
  5336. if (ret)
  5337. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5338. else
  5339. dev_priv->modeset_restore_state = state;
  5340. return ret;
  5341. }
  5342. void intel_encoder_destroy(struct drm_encoder *encoder)
  5343. {
  5344. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5345. drm_encoder_cleanup(encoder);
  5346. kfree(intel_encoder);
  5347. }
  5348. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5349. * internal consistency). */
  5350. static void intel_connector_verify_state(struct intel_connector *connector)
  5351. {
  5352. struct drm_crtc *crtc = connector->base.state->crtc;
  5353. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5354. connector->base.base.id,
  5355. connector->base.name);
  5356. if (connector->get_hw_state(connector)) {
  5357. struct intel_encoder *encoder = connector->encoder;
  5358. struct drm_connector_state *conn_state = connector->base.state;
  5359. I915_STATE_WARN(!crtc,
  5360. "connector enabled without attached crtc\n");
  5361. if (!crtc)
  5362. return;
  5363. I915_STATE_WARN(!crtc->state->active,
  5364. "connector is active, but attached crtc isn't\n");
  5365. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5366. return;
  5367. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5368. "atomic encoder doesn't match attached encoder\n");
  5369. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5370. "attached encoder crtc differs from connector crtc\n");
  5371. } else {
  5372. I915_STATE_WARN(crtc && crtc->state->active,
  5373. "attached crtc is active, but connector isn't\n");
  5374. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5375. "best encoder set without crtc!\n");
  5376. }
  5377. }
  5378. int intel_connector_init(struct intel_connector *connector)
  5379. {
  5380. drm_atomic_helper_connector_reset(&connector->base);
  5381. if (!connector->base.state)
  5382. return -ENOMEM;
  5383. return 0;
  5384. }
  5385. struct intel_connector *intel_connector_alloc(void)
  5386. {
  5387. struct intel_connector *connector;
  5388. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5389. if (!connector)
  5390. return NULL;
  5391. if (intel_connector_init(connector) < 0) {
  5392. kfree(connector);
  5393. return NULL;
  5394. }
  5395. return connector;
  5396. }
  5397. /* Simple connector->get_hw_state implementation for encoders that support only
  5398. * one connector and no cloning and hence the encoder state determines the state
  5399. * of the connector. */
  5400. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5401. {
  5402. enum pipe pipe = 0;
  5403. struct intel_encoder *encoder = connector->encoder;
  5404. return encoder->get_hw_state(encoder, &pipe);
  5405. }
  5406. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5407. {
  5408. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5409. return crtc_state->fdi_lanes;
  5410. return 0;
  5411. }
  5412. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5413. struct intel_crtc_state *pipe_config)
  5414. {
  5415. struct drm_atomic_state *state = pipe_config->base.state;
  5416. struct intel_crtc *other_crtc;
  5417. struct intel_crtc_state *other_crtc_state;
  5418. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5419. pipe_name(pipe), pipe_config->fdi_lanes);
  5420. if (pipe_config->fdi_lanes > 4) {
  5421. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5422. pipe_name(pipe), pipe_config->fdi_lanes);
  5423. return -EINVAL;
  5424. }
  5425. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5426. if (pipe_config->fdi_lanes > 2) {
  5427. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5428. pipe_config->fdi_lanes);
  5429. return -EINVAL;
  5430. } else {
  5431. return 0;
  5432. }
  5433. }
  5434. if (INTEL_INFO(dev)->num_pipes == 2)
  5435. return 0;
  5436. /* Ivybridge 3 pipe is really complicated */
  5437. switch (pipe) {
  5438. case PIPE_A:
  5439. return 0;
  5440. case PIPE_B:
  5441. if (pipe_config->fdi_lanes <= 2)
  5442. return 0;
  5443. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5444. other_crtc_state =
  5445. intel_atomic_get_crtc_state(state, other_crtc);
  5446. if (IS_ERR(other_crtc_state))
  5447. return PTR_ERR(other_crtc_state);
  5448. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5449. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5450. pipe_name(pipe), pipe_config->fdi_lanes);
  5451. return -EINVAL;
  5452. }
  5453. return 0;
  5454. case PIPE_C:
  5455. if (pipe_config->fdi_lanes > 2) {
  5456. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5457. pipe_name(pipe), pipe_config->fdi_lanes);
  5458. return -EINVAL;
  5459. }
  5460. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5461. other_crtc_state =
  5462. intel_atomic_get_crtc_state(state, other_crtc);
  5463. if (IS_ERR(other_crtc_state))
  5464. return PTR_ERR(other_crtc_state);
  5465. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5466. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5467. return -EINVAL;
  5468. }
  5469. return 0;
  5470. default:
  5471. BUG();
  5472. }
  5473. }
  5474. #define RETRY 1
  5475. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5476. struct intel_crtc_state *pipe_config)
  5477. {
  5478. struct drm_device *dev = intel_crtc->base.dev;
  5479. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5480. int lane, link_bw, fdi_dotclock, ret;
  5481. bool needs_recompute = false;
  5482. retry:
  5483. /* FDI is a binary signal running at ~2.7GHz, encoding
  5484. * each output octet as 10 bits. The actual frequency
  5485. * is stored as a divider into a 100MHz clock, and the
  5486. * mode pixel clock is stored in units of 1KHz.
  5487. * Hence the bw of each lane in terms of the mode signal
  5488. * is:
  5489. */
  5490. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5491. fdi_dotclock = adjusted_mode->crtc_clock;
  5492. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5493. pipe_config->pipe_bpp);
  5494. pipe_config->fdi_lanes = lane;
  5495. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5496. link_bw, &pipe_config->fdi_m_n);
  5497. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5498. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5499. pipe_config->pipe_bpp -= 2*3;
  5500. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5501. pipe_config->pipe_bpp);
  5502. needs_recompute = true;
  5503. pipe_config->bw_constrained = true;
  5504. goto retry;
  5505. }
  5506. if (needs_recompute)
  5507. return RETRY;
  5508. return ret;
  5509. }
  5510. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5511. struct intel_crtc_state *pipe_config)
  5512. {
  5513. if (pipe_config->pipe_bpp > 24)
  5514. return false;
  5515. /* HSW can handle pixel rate up to cdclk? */
  5516. if (IS_HASWELL(dev_priv))
  5517. return true;
  5518. /*
  5519. * We compare against max which means we must take
  5520. * the increased cdclk requirement into account when
  5521. * calculating the new cdclk.
  5522. *
  5523. * Should measure whether using a lower cdclk w/o IPS
  5524. */
  5525. return ilk_pipe_pixel_rate(pipe_config) <=
  5526. dev_priv->max_cdclk_freq * 95 / 100;
  5527. }
  5528. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5529. struct intel_crtc_state *pipe_config)
  5530. {
  5531. struct drm_device *dev = crtc->base.dev;
  5532. struct drm_i915_private *dev_priv = to_i915(dev);
  5533. pipe_config->ips_enabled = i915.enable_ips &&
  5534. hsw_crtc_supports_ips(crtc) &&
  5535. pipe_config_supports_ips(dev_priv, pipe_config);
  5536. }
  5537. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5538. {
  5539. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5540. /* GDG double wide on either pipe, otherwise pipe A only */
  5541. return INTEL_INFO(dev_priv)->gen < 4 &&
  5542. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5543. }
  5544. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5545. struct intel_crtc_state *pipe_config)
  5546. {
  5547. struct drm_device *dev = crtc->base.dev;
  5548. struct drm_i915_private *dev_priv = to_i915(dev);
  5549. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5550. int clock_limit = dev_priv->max_dotclk_freq;
  5551. if (INTEL_INFO(dev)->gen < 4) {
  5552. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5553. /*
  5554. * Enable double wide mode when the dot clock
  5555. * is > 90% of the (display) core speed.
  5556. */
  5557. if (intel_crtc_supports_double_wide(crtc) &&
  5558. adjusted_mode->crtc_clock > clock_limit) {
  5559. clock_limit = dev_priv->max_dotclk_freq;
  5560. pipe_config->double_wide = true;
  5561. }
  5562. }
  5563. if (adjusted_mode->crtc_clock > clock_limit) {
  5564. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5565. adjusted_mode->crtc_clock, clock_limit,
  5566. yesno(pipe_config->double_wide));
  5567. return -EINVAL;
  5568. }
  5569. /*
  5570. * Pipe horizontal size must be even in:
  5571. * - DVO ganged mode
  5572. * - LVDS dual channel mode
  5573. * - Double wide pipe
  5574. */
  5575. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5576. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5577. pipe_config->pipe_src_w &= ~1;
  5578. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5579. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5580. */
  5581. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5582. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5583. return -EINVAL;
  5584. if (HAS_IPS(dev))
  5585. hsw_compute_ips_config(crtc, pipe_config);
  5586. if (pipe_config->has_pch_encoder)
  5587. return ironlake_fdi_compute_config(crtc, pipe_config);
  5588. return 0;
  5589. }
  5590. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5591. {
  5592. struct drm_i915_private *dev_priv = to_i915(dev);
  5593. uint32_t cdctl;
  5594. skl_dpll0_update(dev_priv);
  5595. if (dev_priv->cdclk_pll.vco == 0)
  5596. return dev_priv->cdclk_pll.ref;
  5597. cdctl = I915_READ(CDCLK_CTL);
  5598. if (dev_priv->cdclk_pll.vco == 8640000) {
  5599. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5600. case CDCLK_FREQ_450_432:
  5601. return 432000;
  5602. case CDCLK_FREQ_337_308:
  5603. return 308571;
  5604. case CDCLK_FREQ_540:
  5605. return 540000;
  5606. case CDCLK_FREQ_675_617:
  5607. return 617143;
  5608. default:
  5609. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5610. }
  5611. } else {
  5612. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5613. case CDCLK_FREQ_450_432:
  5614. return 450000;
  5615. case CDCLK_FREQ_337_308:
  5616. return 337500;
  5617. case CDCLK_FREQ_540:
  5618. return 540000;
  5619. case CDCLK_FREQ_675_617:
  5620. return 675000;
  5621. default:
  5622. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5623. }
  5624. }
  5625. return dev_priv->cdclk_pll.ref;
  5626. }
  5627. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  5628. {
  5629. u32 val;
  5630. dev_priv->cdclk_pll.ref = 19200;
  5631. dev_priv->cdclk_pll.vco = 0;
  5632. val = I915_READ(BXT_DE_PLL_ENABLE);
  5633. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  5634. return;
  5635. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  5636. return;
  5637. val = I915_READ(BXT_DE_PLL_CTL);
  5638. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  5639. dev_priv->cdclk_pll.ref;
  5640. }
  5641. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5642. {
  5643. struct drm_i915_private *dev_priv = to_i915(dev);
  5644. u32 divider;
  5645. int div, vco;
  5646. bxt_de_pll_update(dev_priv);
  5647. vco = dev_priv->cdclk_pll.vco;
  5648. if (vco == 0)
  5649. return dev_priv->cdclk_pll.ref;
  5650. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  5651. switch (divider) {
  5652. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5653. div = 2;
  5654. break;
  5655. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5656. div = 3;
  5657. break;
  5658. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5659. div = 4;
  5660. break;
  5661. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5662. div = 8;
  5663. break;
  5664. default:
  5665. MISSING_CASE(divider);
  5666. return dev_priv->cdclk_pll.ref;
  5667. }
  5668. return DIV_ROUND_CLOSEST(vco, div);
  5669. }
  5670. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5671. {
  5672. struct drm_i915_private *dev_priv = to_i915(dev);
  5673. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5674. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5675. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5676. return 800000;
  5677. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5678. return 450000;
  5679. else if (freq == LCPLL_CLK_FREQ_450)
  5680. return 450000;
  5681. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5682. return 540000;
  5683. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5684. return 337500;
  5685. else
  5686. return 675000;
  5687. }
  5688. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5689. {
  5690. struct drm_i915_private *dev_priv = to_i915(dev);
  5691. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5692. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5693. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5694. return 800000;
  5695. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5696. return 450000;
  5697. else if (freq == LCPLL_CLK_FREQ_450)
  5698. return 450000;
  5699. else if (IS_HSW_ULT(dev))
  5700. return 337500;
  5701. else
  5702. return 540000;
  5703. }
  5704. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5705. {
  5706. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5707. CCK_DISPLAY_CLOCK_CONTROL);
  5708. }
  5709. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5710. {
  5711. return 450000;
  5712. }
  5713. static int i945_get_display_clock_speed(struct drm_device *dev)
  5714. {
  5715. return 400000;
  5716. }
  5717. static int i915_get_display_clock_speed(struct drm_device *dev)
  5718. {
  5719. return 333333;
  5720. }
  5721. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5722. {
  5723. return 200000;
  5724. }
  5725. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5726. {
  5727. u16 gcfgc = 0;
  5728. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5729. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5730. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5731. return 266667;
  5732. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5733. return 333333;
  5734. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5735. return 444444;
  5736. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5737. return 200000;
  5738. default:
  5739. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5740. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5741. return 133333;
  5742. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5743. return 166667;
  5744. }
  5745. }
  5746. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5747. {
  5748. u16 gcfgc = 0;
  5749. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5750. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5751. return 133333;
  5752. else {
  5753. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5754. case GC_DISPLAY_CLOCK_333_MHZ:
  5755. return 333333;
  5756. default:
  5757. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5758. return 190000;
  5759. }
  5760. }
  5761. }
  5762. static int i865_get_display_clock_speed(struct drm_device *dev)
  5763. {
  5764. return 266667;
  5765. }
  5766. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5767. {
  5768. u16 hpllcc = 0;
  5769. /*
  5770. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5771. * encoding is different :(
  5772. * FIXME is this the right way to detect 852GM/852GMV?
  5773. */
  5774. if (dev->pdev->revision == 0x1)
  5775. return 133333;
  5776. pci_bus_read_config_word(dev->pdev->bus,
  5777. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5778. /* Assume that the hardware is in the high speed state. This
  5779. * should be the default.
  5780. */
  5781. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5782. case GC_CLOCK_133_200:
  5783. case GC_CLOCK_133_200_2:
  5784. case GC_CLOCK_100_200:
  5785. return 200000;
  5786. case GC_CLOCK_166_250:
  5787. return 250000;
  5788. case GC_CLOCK_100_133:
  5789. return 133333;
  5790. case GC_CLOCK_133_266:
  5791. case GC_CLOCK_133_266_2:
  5792. case GC_CLOCK_166_266:
  5793. return 266667;
  5794. }
  5795. /* Shouldn't happen */
  5796. return 0;
  5797. }
  5798. static int i830_get_display_clock_speed(struct drm_device *dev)
  5799. {
  5800. return 133333;
  5801. }
  5802. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5803. {
  5804. struct drm_i915_private *dev_priv = to_i915(dev);
  5805. static const unsigned int blb_vco[8] = {
  5806. [0] = 3200000,
  5807. [1] = 4000000,
  5808. [2] = 5333333,
  5809. [3] = 4800000,
  5810. [4] = 6400000,
  5811. };
  5812. static const unsigned int pnv_vco[8] = {
  5813. [0] = 3200000,
  5814. [1] = 4000000,
  5815. [2] = 5333333,
  5816. [3] = 4800000,
  5817. [4] = 2666667,
  5818. };
  5819. static const unsigned int cl_vco[8] = {
  5820. [0] = 3200000,
  5821. [1] = 4000000,
  5822. [2] = 5333333,
  5823. [3] = 6400000,
  5824. [4] = 3333333,
  5825. [5] = 3566667,
  5826. [6] = 4266667,
  5827. };
  5828. static const unsigned int elk_vco[8] = {
  5829. [0] = 3200000,
  5830. [1] = 4000000,
  5831. [2] = 5333333,
  5832. [3] = 4800000,
  5833. };
  5834. static const unsigned int ctg_vco[8] = {
  5835. [0] = 3200000,
  5836. [1] = 4000000,
  5837. [2] = 5333333,
  5838. [3] = 6400000,
  5839. [4] = 2666667,
  5840. [5] = 4266667,
  5841. };
  5842. const unsigned int *vco_table;
  5843. unsigned int vco;
  5844. uint8_t tmp = 0;
  5845. /* FIXME other chipsets? */
  5846. if (IS_GM45(dev))
  5847. vco_table = ctg_vco;
  5848. else if (IS_G4X(dev))
  5849. vco_table = elk_vco;
  5850. else if (IS_CRESTLINE(dev))
  5851. vco_table = cl_vco;
  5852. else if (IS_PINEVIEW(dev))
  5853. vco_table = pnv_vco;
  5854. else if (IS_G33(dev))
  5855. vco_table = blb_vco;
  5856. else
  5857. return 0;
  5858. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5859. vco = vco_table[tmp & 0x7];
  5860. if (vco == 0)
  5861. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5862. else
  5863. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5864. return vco;
  5865. }
  5866. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5867. {
  5868. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5869. uint16_t tmp = 0;
  5870. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5871. cdclk_sel = (tmp >> 12) & 0x1;
  5872. switch (vco) {
  5873. case 2666667:
  5874. case 4000000:
  5875. case 5333333:
  5876. return cdclk_sel ? 333333 : 222222;
  5877. case 3200000:
  5878. return cdclk_sel ? 320000 : 228571;
  5879. default:
  5880. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5881. return 222222;
  5882. }
  5883. }
  5884. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5885. {
  5886. static const uint8_t div_3200[] = { 16, 10, 8 };
  5887. static const uint8_t div_4000[] = { 20, 12, 10 };
  5888. static const uint8_t div_5333[] = { 24, 16, 14 };
  5889. const uint8_t *div_table;
  5890. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5891. uint16_t tmp = 0;
  5892. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5893. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5894. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5895. goto fail;
  5896. switch (vco) {
  5897. case 3200000:
  5898. div_table = div_3200;
  5899. break;
  5900. case 4000000:
  5901. div_table = div_4000;
  5902. break;
  5903. case 5333333:
  5904. div_table = div_5333;
  5905. break;
  5906. default:
  5907. goto fail;
  5908. }
  5909. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5910. fail:
  5911. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5912. return 200000;
  5913. }
  5914. static int g33_get_display_clock_speed(struct drm_device *dev)
  5915. {
  5916. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5917. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5918. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5919. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5920. const uint8_t *div_table;
  5921. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5922. uint16_t tmp = 0;
  5923. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5924. cdclk_sel = (tmp >> 4) & 0x7;
  5925. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5926. goto fail;
  5927. switch (vco) {
  5928. case 3200000:
  5929. div_table = div_3200;
  5930. break;
  5931. case 4000000:
  5932. div_table = div_4000;
  5933. break;
  5934. case 4800000:
  5935. div_table = div_4800;
  5936. break;
  5937. case 5333333:
  5938. div_table = div_5333;
  5939. break;
  5940. default:
  5941. goto fail;
  5942. }
  5943. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5944. fail:
  5945. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5946. return 190476;
  5947. }
  5948. static void
  5949. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5950. {
  5951. while (*num > DATA_LINK_M_N_MASK ||
  5952. *den > DATA_LINK_M_N_MASK) {
  5953. *num >>= 1;
  5954. *den >>= 1;
  5955. }
  5956. }
  5957. static void compute_m_n(unsigned int m, unsigned int n,
  5958. uint32_t *ret_m, uint32_t *ret_n)
  5959. {
  5960. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5961. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5962. intel_reduce_m_n_ratio(ret_m, ret_n);
  5963. }
  5964. void
  5965. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5966. int pixel_clock, int link_clock,
  5967. struct intel_link_m_n *m_n)
  5968. {
  5969. m_n->tu = 64;
  5970. compute_m_n(bits_per_pixel * pixel_clock,
  5971. link_clock * nlanes * 8,
  5972. &m_n->gmch_m, &m_n->gmch_n);
  5973. compute_m_n(pixel_clock, link_clock,
  5974. &m_n->link_m, &m_n->link_n);
  5975. }
  5976. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5977. {
  5978. if (i915.panel_use_ssc >= 0)
  5979. return i915.panel_use_ssc != 0;
  5980. return dev_priv->vbt.lvds_use_ssc
  5981. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5982. }
  5983. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5984. {
  5985. return (1 << dpll->n) << 16 | dpll->m2;
  5986. }
  5987. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5988. {
  5989. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5990. }
  5991. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5992. struct intel_crtc_state *crtc_state,
  5993. struct dpll *reduced_clock)
  5994. {
  5995. struct drm_device *dev = crtc->base.dev;
  5996. u32 fp, fp2 = 0;
  5997. if (IS_PINEVIEW(dev)) {
  5998. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5999. if (reduced_clock)
  6000. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6001. } else {
  6002. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6003. if (reduced_clock)
  6004. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6005. }
  6006. crtc_state->dpll_hw_state.fp0 = fp;
  6007. crtc->lowfreq_avail = false;
  6008. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6009. reduced_clock) {
  6010. crtc_state->dpll_hw_state.fp1 = fp2;
  6011. crtc->lowfreq_avail = true;
  6012. } else {
  6013. crtc_state->dpll_hw_state.fp1 = fp;
  6014. }
  6015. }
  6016. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6017. pipe)
  6018. {
  6019. u32 reg_val;
  6020. /*
  6021. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6022. * and set it to a reasonable value instead.
  6023. */
  6024. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6025. reg_val &= 0xffffff00;
  6026. reg_val |= 0x00000030;
  6027. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6028. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6029. reg_val &= 0x8cffffff;
  6030. reg_val = 0x8c000000;
  6031. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6032. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6033. reg_val &= 0xffffff00;
  6034. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6035. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6036. reg_val &= 0x00ffffff;
  6037. reg_val |= 0xb0000000;
  6038. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6039. }
  6040. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6041. struct intel_link_m_n *m_n)
  6042. {
  6043. struct drm_device *dev = crtc->base.dev;
  6044. struct drm_i915_private *dev_priv = to_i915(dev);
  6045. int pipe = crtc->pipe;
  6046. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6047. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6048. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6049. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6050. }
  6051. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6052. struct intel_link_m_n *m_n,
  6053. struct intel_link_m_n *m2_n2)
  6054. {
  6055. struct drm_device *dev = crtc->base.dev;
  6056. struct drm_i915_private *dev_priv = to_i915(dev);
  6057. int pipe = crtc->pipe;
  6058. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6059. if (INTEL_INFO(dev)->gen >= 5) {
  6060. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6061. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6062. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6063. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6064. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6065. * for gen < 8) and if DRRS is supported (to make sure the
  6066. * registers are not unnecessarily accessed).
  6067. */
  6068. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6069. crtc->config->has_drrs) {
  6070. I915_WRITE(PIPE_DATA_M2(transcoder),
  6071. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6072. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6073. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6074. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6075. }
  6076. } else {
  6077. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6078. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6079. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6080. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6081. }
  6082. }
  6083. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6084. {
  6085. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6086. if (m_n == M1_N1) {
  6087. dp_m_n = &crtc->config->dp_m_n;
  6088. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6089. } else if (m_n == M2_N2) {
  6090. /*
  6091. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6092. * needs to be programmed into M1_N1.
  6093. */
  6094. dp_m_n = &crtc->config->dp_m2_n2;
  6095. } else {
  6096. DRM_ERROR("Unsupported divider value\n");
  6097. return;
  6098. }
  6099. if (crtc->config->has_pch_encoder)
  6100. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6101. else
  6102. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6103. }
  6104. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6105. struct intel_crtc_state *pipe_config)
  6106. {
  6107. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6108. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6109. if (crtc->pipe != PIPE_A)
  6110. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6111. /* DPLL not used with DSI, but still need the rest set up */
  6112. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6113. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6114. DPLL_EXT_BUFFER_ENABLE_VLV;
  6115. pipe_config->dpll_hw_state.dpll_md =
  6116. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6117. }
  6118. static void chv_compute_dpll(struct intel_crtc *crtc,
  6119. struct intel_crtc_state *pipe_config)
  6120. {
  6121. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6122. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6123. if (crtc->pipe != PIPE_A)
  6124. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6125. /* DPLL not used with DSI, but still need the rest set up */
  6126. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6127. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6128. pipe_config->dpll_hw_state.dpll_md =
  6129. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6130. }
  6131. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6132. const struct intel_crtc_state *pipe_config)
  6133. {
  6134. struct drm_device *dev = crtc->base.dev;
  6135. struct drm_i915_private *dev_priv = to_i915(dev);
  6136. enum pipe pipe = crtc->pipe;
  6137. u32 mdiv;
  6138. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6139. u32 coreclk, reg_val;
  6140. /* Enable Refclk */
  6141. I915_WRITE(DPLL(pipe),
  6142. pipe_config->dpll_hw_state.dpll &
  6143. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6144. /* No need to actually set up the DPLL with DSI */
  6145. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6146. return;
  6147. mutex_lock(&dev_priv->sb_lock);
  6148. bestn = pipe_config->dpll.n;
  6149. bestm1 = pipe_config->dpll.m1;
  6150. bestm2 = pipe_config->dpll.m2;
  6151. bestp1 = pipe_config->dpll.p1;
  6152. bestp2 = pipe_config->dpll.p2;
  6153. /* See eDP HDMI DPIO driver vbios notes doc */
  6154. /* PLL B needs special handling */
  6155. if (pipe == PIPE_B)
  6156. vlv_pllb_recal_opamp(dev_priv, pipe);
  6157. /* Set up Tx target for periodic Rcomp update */
  6158. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6159. /* Disable target IRef on PLL */
  6160. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6161. reg_val &= 0x00ffffff;
  6162. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6163. /* Disable fast lock */
  6164. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6165. /* Set idtafcrecal before PLL is enabled */
  6166. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6167. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6168. mdiv |= ((bestn << DPIO_N_SHIFT));
  6169. mdiv |= (1 << DPIO_K_SHIFT);
  6170. /*
  6171. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6172. * but we don't support that).
  6173. * Note: don't use the DAC post divider as it seems unstable.
  6174. */
  6175. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6176. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6177. mdiv |= DPIO_ENABLE_CALIBRATION;
  6178. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6179. /* Set HBR and RBR LPF coefficients */
  6180. if (pipe_config->port_clock == 162000 ||
  6181. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  6182. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  6183. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6184. 0x009f0003);
  6185. else
  6186. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6187. 0x00d0000f);
  6188. if (intel_crtc_has_dp_encoder(pipe_config)) {
  6189. /* Use SSC source */
  6190. if (pipe == PIPE_A)
  6191. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6192. 0x0df40000);
  6193. else
  6194. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6195. 0x0df70000);
  6196. } else { /* HDMI or VGA */
  6197. /* Use bend source */
  6198. if (pipe == PIPE_A)
  6199. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6200. 0x0df70000);
  6201. else
  6202. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6203. 0x0df40000);
  6204. }
  6205. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6206. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6207. if (intel_crtc_has_dp_encoder(crtc->config))
  6208. coreclk |= 0x01000000;
  6209. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6210. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6211. mutex_unlock(&dev_priv->sb_lock);
  6212. }
  6213. static void chv_prepare_pll(struct intel_crtc *crtc,
  6214. const struct intel_crtc_state *pipe_config)
  6215. {
  6216. struct drm_device *dev = crtc->base.dev;
  6217. struct drm_i915_private *dev_priv = to_i915(dev);
  6218. enum pipe pipe = crtc->pipe;
  6219. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6220. u32 loopfilter, tribuf_calcntr;
  6221. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6222. u32 dpio_val;
  6223. int vco;
  6224. /* Enable Refclk and SSC */
  6225. I915_WRITE(DPLL(pipe),
  6226. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6227. /* No need to actually set up the DPLL with DSI */
  6228. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6229. return;
  6230. bestn = pipe_config->dpll.n;
  6231. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6232. bestm1 = pipe_config->dpll.m1;
  6233. bestm2 = pipe_config->dpll.m2 >> 22;
  6234. bestp1 = pipe_config->dpll.p1;
  6235. bestp2 = pipe_config->dpll.p2;
  6236. vco = pipe_config->dpll.vco;
  6237. dpio_val = 0;
  6238. loopfilter = 0;
  6239. mutex_lock(&dev_priv->sb_lock);
  6240. /* p1 and p2 divider */
  6241. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6242. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6243. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6244. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6245. 1 << DPIO_CHV_K_DIV_SHIFT);
  6246. /* Feedback post-divider - m2 */
  6247. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6248. /* Feedback refclk divider - n and m1 */
  6249. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6250. DPIO_CHV_M1_DIV_BY_2 |
  6251. 1 << DPIO_CHV_N_DIV_SHIFT);
  6252. /* M2 fraction division */
  6253. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6254. /* M2 fraction division enable */
  6255. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6256. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6257. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6258. if (bestm2_frac)
  6259. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6260. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6261. /* Program digital lock detect threshold */
  6262. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6263. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6264. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6265. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6266. if (!bestm2_frac)
  6267. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6268. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6269. /* Loop filter */
  6270. if (vco == 5400000) {
  6271. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6272. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6273. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6274. tribuf_calcntr = 0x9;
  6275. } else if (vco <= 6200000) {
  6276. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6277. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6278. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6279. tribuf_calcntr = 0x9;
  6280. } else if (vco <= 6480000) {
  6281. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6282. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6283. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6284. tribuf_calcntr = 0x8;
  6285. } else {
  6286. /* Not supported. Apply the same limits as in the max case */
  6287. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6288. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6289. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6290. tribuf_calcntr = 0;
  6291. }
  6292. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6293. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6294. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6295. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6296. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6297. /* AFC Recal */
  6298. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6299. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6300. DPIO_AFC_RECAL);
  6301. mutex_unlock(&dev_priv->sb_lock);
  6302. }
  6303. /**
  6304. * vlv_force_pll_on - forcibly enable just the PLL
  6305. * @dev_priv: i915 private structure
  6306. * @pipe: pipe PLL to enable
  6307. * @dpll: PLL configuration
  6308. *
  6309. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6310. * in cases where we need the PLL enabled even when @pipe is not going to
  6311. * be enabled.
  6312. */
  6313. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6314. const struct dpll *dpll)
  6315. {
  6316. struct intel_crtc *crtc =
  6317. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6318. struct intel_crtc_state *pipe_config;
  6319. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6320. if (!pipe_config)
  6321. return -ENOMEM;
  6322. pipe_config->base.crtc = &crtc->base;
  6323. pipe_config->pixel_multiplier = 1;
  6324. pipe_config->dpll = *dpll;
  6325. if (IS_CHERRYVIEW(dev)) {
  6326. chv_compute_dpll(crtc, pipe_config);
  6327. chv_prepare_pll(crtc, pipe_config);
  6328. chv_enable_pll(crtc, pipe_config);
  6329. } else {
  6330. vlv_compute_dpll(crtc, pipe_config);
  6331. vlv_prepare_pll(crtc, pipe_config);
  6332. vlv_enable_pll(crtc, pipe_config);
  6333. }
  6334. kfree(pipe_config);
  6335. return 0;
  6336. }
  6337. /**
  6338. * vlv_force_pll_off - forcibly disable just the PLL
  6339. * @dev_priv: i915 private structure
  6340. * @pipe: pipe PLL to disable
  6341. *
  6342. * Disable the PLL for @pipe. To be used in cases where we need
  6343. * the PLL enabled even when @pipe is not going to be enabled.
  6344. */
  6345. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6346. {
  6347. if (IS_CHERRYVIEW(dev))
  6348. chv_disable_pll(to_i915(dev), pipe);
  6349. else
  6350. vlv_disable_pll(to_i915(dev), pipe);
  6351. }
  6352. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6353. struct intel_crtc_state *crtc_state,
  6354. struct dpll *reduced_clock)
  6355. {
  6356. struct drm_device *dev = crtc->base.dev;
  6357. struct drm_i915_private *dev_priv = to_i915(dev);
  6358. u32 dpll;
  6359. struct dpll *clock = &crtc_state->dpll;
  6360. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6361. dpll = DPLL_VGA_MODE_DIS;
  6362. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6363. dpll |= DPLLB_MODE_LVDS;
  6364. else
  6365. dpll |= DPLLB_MODE_DAC_SERIAL;
  6366. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6367. dpll |= (crtc_state->pixel_multiplier - 1)
  6368. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6369. }
  6370. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6371. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6372. dpll |= DPLL_SDVO_HIGH_SPEED;
  6373. if (intel_crtc_has_dp_encoder(crtc_state))
  6374. dpll |= DPLL_SDVO_HIGH_SPEED;
  6375. /* compute bitmask from p1 value */
  6376. if (IS_PINEVIEW(dev))
  6377. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6378. else {
  6379. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6380. if (IS_G4X(dev) && reduced_clock)
  6381. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6382. }
  6383. switch (clock->p2) {
  6384. case 5:
  6385. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6386. break;
  6387. case 7:
  6388. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6389. break;
  6390. case 10:
  6391. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6392. break;
  6393. case 14:
  6394. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6395. break;
  6396. }
  6397. if (INTEL_INFO(dev)->gen >= 4)
  6398. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6399. if (crtc_state->sdvo_tv_clock)
  6400. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6401. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6402. intel_panel_use_ssc(dev_priv))
  6403. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6404. else
  6405. dpll |= PLL_REF_INPUT_DREFCLK;
  6406. dpll |= DPLL_VCO_ENABLE;
  6407. crtc_state->dpll_hw_state.dpll = dpll;
  6408. if (INTEL_INFO(dev)->gen >= 4) {
  6409. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6410. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6411. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6412. }
  6413. }
  6414. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6415. struct intel_crtc_state *crtc_state,
  6416. struct dpll *reduced_clock)
  6417. {
  6418. struct drm_device *dev = crtc->base.dev;
  6419. struct drm_i915_private *dev_priv = to_i915(dev);
  6420. u32 dpll;
  6421. struct dpll *clock = &crtc_state->dpll;
  6422. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6423. dpll = DPLL_VGA_MODE_DIS;
  6424. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6425. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6426. } else {
  6427. if (clock->p1 == 2)
  6428. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6429. else
  6430. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6431. if (clock->p2 == 4)
  6432. dpll |= PLL_P2_DIVIDE_BY_4;
  6433. }
  6434. if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6435. dpll |= DPLL_DVO_2X_MODE;
  6436. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6437. intel_panel_use_ssc(dev_priv))
  6438. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6439. else
  6440. dpll |= PLL_REF_INPUT_DREFCLK;
  6441. dpll |= DPLL_VCO_ENABLE;
  6442. crtc_state->dpll_hw_state.dpll = dpll;
  6443. }
  6444. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6445. {
  6446. struct drm_device *dev = intel_crtc->base.dev;
  6447. struct drm_i915_private *dev_priv = to_i915(dev);
  6448. enum pipe pipe = intel_crtc->pipe;
  6449. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6450. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6451. uint32_t crtc_vtotal, crtc_vblank_end;
  6452. int vsyncshift = 0;
  6453. /* We need to be careful not to changed the adjusted mode, for otherwise
  6454. * the hw state checker will get angry at the mismatch. */
  6455. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6456. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6457. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6458. /* the chip adds 2 halflines automatically */
  6459. crtc_vtotal -= 1;
  6460. crtc_vblank_end -= 1;
  6461. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6462. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6463. else
  6464. vsyncshift = adjusted_mode->crtc_hsync_start -
  6465. adjusted_mode->crtc_htotal / 2;
  6466. if (vsyncshift < 0)
  6467. vsyncshift += adjusted_mode->crtc_htotal;
  6468. }
  6469. if (INTEL_INFO(dev)->gen > 3)
  6470. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6471. I915_WRITE(HTOTAL(cpu_transcoder),
  6472. (adjusted_mode->crtc_hdisplay - 1) |
  6473. ((adjusted_mode->crtc_htotal - 1) << 16));
  6474. I915_WRITE(HBLANK(cpu_transcoder),
  6475. (adjusted_mode->crtc_hblank_start - 1) |
  6476. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6477. I915_WRITE(HSYNC(cpu_transcoder),
  6478. (adjusted_mode->crtc_hsync_start - 1) |
  6479. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6480. I915_WRITE(VTOTAL(cpu_transcoder),
  6481. (adjusted_mode->crtc_vdisplay - 1) |
  6482. ((crtc_vtotal - 1) << 16));
  6483. I915_WRITE(VBLANK(cpu_transcoder),
  6484. (adjusted_mode->crtc_vblank_start - 1) |
  6485. ((crtc_vblank_end - 1) << 16));
  6486. I915_WRITE(VSYNC(cpu_transcoder),
  6487. (adjusted_mode->crtc_vsync_start - 1) |
  6488. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6489. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6490. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6491. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6492. * bits. */
  6493. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6494. (pipe == PIPE_B || pipe == PIPE_C))
  6495. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6496. }
  6497. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6498. {
  6499. struct drm_device *dev = intel_crtc->base.dev;
  6500. struct drm_i915_private *dev_priv = to_i915(dev);
  6501. enum pipe pipe = intel_crtc->pipe;
  6502. /* pipesrc controls the size that is scaled from, which should
  6503. * always be the user's requested size.
  6504. */
  6505. I915_WRITE(PIPESRC(pipe),
  6506. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6507. (intel_crtc->config->pipe_src_h - 1));
  6508. }
  6509. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6510. struct intel_crtc_state *pipe_config)
  6511. {
  6512. struct drm_device *dev = crtc->base.dev;
  6513. struct drm_i915_private *dev_priv = to_i915(dev);
  6514. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6515. uint32_t tmp;
  6516. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6517. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6518. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6519. tmp = I915_READ(HBLANK(cpu_transcoder));
  6520. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6521. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6522. tmp = I915_READ(HSYNC(cpu_transcoder));
  6523. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6524. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6525. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6526. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6527. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6528. tmp = I915_READ(VBLANK(cpu_transcoder));
  6529. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6530. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6531. tmp = I915_READ(VSYNC(cpu_transcoder));
  6532. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6533. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6534. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6535. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6536. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6537. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6538. }
  6539. }
  6540. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6541. struct intel_crtc_state *pipe_config)
  6542. {
  6543. struct drm_device *dev = crtc->base.dev;
  6544. struct drm_i915_private *dev_priv = to_i915(dev);
  6545. u32 tmp;
  6546. tmp = I915_READ(PIPESRC(crtc->pipe));
  6547. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6548. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6549. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6550. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6551. }
  6552. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6553. struct intel_crtc_state *pipe_config)
  6554. {
  6555. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6556. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6557. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6558. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6559. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6560. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6561. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6562. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6563. mode->flags = pipe_config->base.adjusted_mode.flags;
  6564. mode->type = DRM_MODE_TYPE_DRIVER;
  6565. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6566. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6567. mode->hsync = drm_mode_hsync(mode);
  6568. mode->vrefresh = drm_mode_vrefresh(mode);
  6569. drm_mode_set_name(mode);
  6570. }
  6571. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6572. {
  6573. struct drm_device *dev = intel_crtc->base.dev;
  6574. struct drm_i915_private *dev_priv = to_i915(dev);
  6575. uint32_t pipeconf;
  6576. pipeconf = 0;
  6577. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6578. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6579. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6580. if (intel_crtc->config->double_wide)
  6581. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6582. /* only g4x and later have fancy bpc/dither controls */
  6583. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6584. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6585. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6586. pipeconf |= PIPECONF_DITHER_EN |
  6587. PIPECONF_DITHER_TYPE_SP;
  6588. switch (intel_crtc->config->pipe_bpp) {
  6589. case 18:
  6590. pipeconf |= PIPECONF_6BPC;
  6591. break;
  6592. case 24:
  6593. pipeconf |= PIPECONF_8BPC;
  6594. break;
  6595. case 30:
  6596. pipeconf |= PIPECONF_10BPC;
  6597. break;
  6598. default:
  6599. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6600. BUG();
  6601. }
  6602. }
  6603. if (HAS_PIPE_CXSR(dev)) {
  6604. if (intel_crtc->lowfreq_avail) {
  6605. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6606. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6607. } else {
  6608. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6609. }
  6610. }
  6611. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6612. if (INTEL_INFO(dev)->gen < 4 ||
  6613. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6614. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6615. else
  6616. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6617. } else
  6618. pipeconf |= PIPECONF_PROGRESSIVE;
  6619. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6620. intel_crtc->config->limited_color_range)
  6621. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6622. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6623. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6624. }
  6625. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6626. struct intel_crtc_state *crtc_state)
  6627. {
  6628. struct drm_device *dev = crtc->base.dev;
  6629. struct drm_i915_private *dev_priv = to_i915(dev);
  6630. const struct intel_limit *limit;
  6631. int refclk = 48000;
  6632. memset(&crtc_state->dpll_hw_state, 0,
  6633. sizeof(crtc_state->dpll_hw_state));
  6634. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6635. if (intel_panel_use_ssc(dev_priv)) {
  6636. refclk = dev_priv->vbt.lvds_ssc_freq;
  6637. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6638. }
  6639. limit = &intel_limits_i8xx_lvds;
  6640. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6641. limit = &intel_limits_i8xx_dvo;
  6642. } else {
  6643. limit = &intel_limits_i8xx_dac;
  6644. }
  6645. if (!crtc_state->clock_set &&
  6646. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6647. refclk, NULL, &crtc_state->dpll)) {
  6648. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6649. return -EINVAL;
  6650. }
  6651. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6652. return 0;
  6653. }
  6654. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6655. struct intel_crtc_state *crtc_state)
  6656. {
  6657. struct drm_device *dev = crtc->base.dev;
  6658. struct drm_i915_private *dev_priv = to_i915(dev);
  6659. const struct intel_limit *limit;
  6660. int refclk = 96000;
  6661. memset(&crtc_state->dpll_hw_state, 0,
  6662. sizeof(crtc_state->dpll_hw_state));
  6663. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6664. if (intel_panel_use_ssc(dev_priv)) {
  6665. refclk = dev_priv->vbt.lvds_ssc_freq;
  6666. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6667. }
  6668. if (intel_is_dual_link_lvds(dev))
  6669. limit = &intel_limits_g4x_dual_channel_lvds;
  6670. else
  6671. limit = &intel_limits_g4x_single_channel_lvds;
  6672. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6673. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6674. limit = &intel_limits_g4x_hdmi;
  6675. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6676. limit = &intel_limits_g4x_sdvo;
  6677. } else {
  6678. /* The option is for other outputs */
  6679. limit = &intel_limits_i9xx_sdvo;
  6680. }
  6681. if (!crtc_state->clock_set &&
  6682. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6683. refclk, NULL, &crtc_state->dpll)) {
  6684. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6685. return -EINVAL;
  6686. }
  6687. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6688. return 0;
  6689. }
  6690. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6691. struct intel_crtc_state *crtc_state)
  6692. {
  6693. struct drm_device *dev = crtc->base.dev;
  6694. struct drm_i915_private *dev_priv = to_i915(dev);
  6695. const struct intel_limit *limit;
  6696. int refclk = 96000;
  6697. memset(&crtc_state->dpll_hw_state, 0,
  6698. sizeof(crtc_state->dpll_hw_state));
  6699. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6700. if (intel_panel_use_ssc(dev_priv)) {
  6701. refclk = dev_priv->vbt.lvds_ssc_freq;
  6702. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6703. }
  6704. limit = &intel_limits_pineview_lvds;
  6705. } else {
  6706. limit = &intel_limits_pineview_sdvo;
  6707. }
  6708. if (!crtc_state->clock_set &&
  6709. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6710. refclk, NULL, &crtc_state->dpll)) {
  6711. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6712. return -EINVAL;
  6713. }
  6714. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6715. return 0;
  6716. }
  6717. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6718. struct intel_crtc_state *crtc_state)
  6719. {
  6720. struct drm_device *dev = crtc->base.dev;
  6721. struct drm_i915_private *dev_priv = to_i915(dev);
  6722. const struct intel_limit *limit;
  6723. int refclk = 96000;
  6724. memset(&crtc_state->dpll_hw_state, 0,
  6725. sizeof(crtc_state->dpll_hw_state));
  6726. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6727. if (intel_panel_use_ssc(dev_priv)) {
  6728. refclk = dev_priv->vbt.lvds_ssc_freq;
  6729. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6730. }
  6731. limit = &intel_limits_i9xx_lvds;
  6732. } else {
  6733. limit = &intel_limits_i9xx_sdvo;
  6734. }
  6735. if (!crtc_state->clock_set &&
  6736. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6737. refclk, NULL, &crtc_state->dpll)) {
  6738. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6739. return -EINVAL;
  6740. }
  6741. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6742. return 0;
  6743. }
  6744. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6745. struct intel_crtc_state *crtc_state)
  6746. {
  6747. int refclk = 100000;
  6748. const struct intel_limit *limit = &intel_limits_chv;
  6749. memset(&crtc_state->dpll_hw_state, 0,
  6750. sizeof(crtc_state->dpll_hw_state));
  6751. if (!crtc_state->clock_set &&
  6752. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6753. refclk, NULL, &crtc_state->dpll)) {
  6754. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6755. return -EINVAL;
  6756. }
  6757. chv_compute_dpll(crtc, crtc_state);
  6758. return 0;
  6759. }
  6760. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6761. struct intel_crtc_state *crtc_state)
  6762. {
  6763. int refclk = 100000;
  6764. const struct intel_limit *limit = &intel_limits_vlv;
  6765. memset(&crtc_state->dpll_hw_state, 0,
  6766. sizeof(crtc_state->dpll_hw_state));
  6767. if (!crtc_state->clock_set &&
  6768. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6769. refclk, NULL, &crtc_state->dpll)) {
  6770. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6771. return -EINVAL;
  6772. }
  6773. vlv_compute_dpll(crtc, crtc_state);
  6774. return 0;
  6775. }
  6776. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6777. struct intel_crtc_state *pipe_config)
  6778. {
  6779. struct drm_device *dev = crtc->base.dev;
  6780. struct drm_i915_private *dev_priv = to_i915(dev);
  6781. uint32_t tmp;
  6782. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6783. return;
  6784. tmp = I915_READ(PFIT_CONTROL);
  6785. if (!(tmp & PFIT_ENABLE))
  6786. return;
  6787. /* Check whether the pfit is attached to our pipe. */
  6788. if (INTEL_INFO(dev)->gen < 4) {
  6789. if (crtc->pipe != PIPE_B)
  6790. return;
  6791. } else {
  6792. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6793. return;
  6794. }
  6795. pipe_config->gmch_pfit.control = tmp;
  6796. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6797. }
  6798. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6799. struct intel_crtc_state *pipe_config)
  6800. {
  6801. struct drm_device *dev = crtc->base.dev;
  6802. struct drm_i915_private *dev_priv = to_i915(dev);
  6803. int pipe = pipe_config->cpu_transcoder;
  6804. struct dpll clock;
  6805. u32 mdiv;
  6806. int refclk = 100000;
  6807. /* In case of DSI, DPLL will not be used */
  6808. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6809. return;
  6810. mutex_lock(&dev_priv->sb_lock);
  6811. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6812. mutex_unlock(&dev_priv->sb_lock);
  6813. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6814. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6815. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6816. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6817. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6818. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6819. }
  6820. static void
  6821. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6822. struct intel_initial_plane_config *plane_config)
  6823. {
  6824. struct drm_device *dev = crtc->base.dev;
  6825. struct drm_i915_private *dev_priv = to_i915(dev);
  6826. u32 val, base, offset;
  6827. int pipe = crtc->pipe, plane = crtc->plane;
  6828. int fourcc, pixel_format;
  6829. unsigned int aligned_height;
  6830. struct drm_framebuffer *fb;
  6831. struct intel_framebuffer *intel_fb;
  6832. val = I915_READ(DSPCNTR(plane));
  6833. if (!(val & DISPLAY_PLANE_ENABLE))
  6834. return;
  6835. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6836. if (!intel_fb) {
  6837. DRM_DEBUG_KMS("failed to alloc fb\n");
  6838. return;
  6839. }
  6840. fb = &intel_fb->base;
  6841. if (INTEL_INFO(dev)->gen >= 4) {
  6842. if (val & DISPPLANE_TILED) {
  6843. plane_config->tiling = I915_TILING_X;
  6844. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6845. }
  6846. }
  6847. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6848. fourcc = i9xx_format_to_fourcc(pixel_format);
  6849. fb->pixel_format = fourcc;
  6850. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6851. if (INTEL_INFO(dev)->gen >= 4) {
  6852. if (plane_config->tiling)
  6853. offset = I915_READ(DSPTILEOFF(plane));
  6854. else
  6855. offset = I915_READ(DSPLINOFF(plane));
  6856. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6857. } else {
  6858. base = I915_READ(DSPADDR(plane));
  6859. }
  6860. plane_config->base = base;
  6861. val = I915_READ(PIPESRC(pipe));
  6862. fb->width = ((val >> 16) & 0xfff) + 1;
  6863. fb->height = ((val >> 0) & 0xfff) + 1;
  6864. val = I915_READ(DSPSTRIDE(pipe));
  6865. fb->pitches[0] = val & 0xffffffc0;
  6866. aligned_height = intel_fb_align_height(dev, fb->height,
  6867. fb->pixel_format,
  6868. fb->modifier[0]);
  6869. plane_config->size = fb->pitches[0] * aligned_height;
  6870. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6871. pipe_name(pipe), plane, fb->width, fb->height,
  6872. fb->bits_per_pixel, base, fb->pitches[0],
  6873. plane_config->size);
  6874. plane_config->fb = intel_fb;
  6875. }
  6876. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6877. struct intel_crtc_state *pipe_config)
  6878. {
  6879. struct drm_device *dev = crtc->base.dev;
  6880. struct drm_i915_private *dev_priv = to_i915(dev);
  6881. int pipe = pipe_config->cpu_transcoder;
  6882. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6883. struct dpll clock;
  6884. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6885. int refclk = 100000;
  6886. /* In case of DSI, DPLL will not be used */
  6887. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6888. return;
  6889. mutex_lock(&dev_priv->sb_lock);
  6890. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6891. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6892. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6893. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6894. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6895. mutex_unlock(&dev_priv->sb_lock);
  6896. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6897. clock.m2 = (pll_dw0 & 0xff) << 22;
  6898. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6899. clock.m2 |= pll_dw2 & 0x3fffff;
  6900. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6901. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6902. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6903. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6904. }
  6905. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6906. struct intel_crtc_state *pipe_config)
  6907. {
  6908. struct drm_device *dev = crtc->base.dev;
  6909. struct drm_i915_private *dev_priv = to_i915(dev);
  6910. enum intel_display_power_domain power_domain;
  6911. uint32_t tmp;
  6912. bool ret;
  6913. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6914. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6915. return false;
  6916. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6917. pipe_config->shared_dpll = NULL;
  6918. ret = false;
  6919. tmp = I915_READ(PIPECONF(crtc->pipe));
  6920. if (!(tmp & PIPECONF_ENABLE))
  6921. goto out;
  6922. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6923. switch (tmp & PIPECONF_BPC_MASK) {
  6924. case PIPECONF_6BPC:
  6925. pipe_config->pipe_bpp = 18;
  6926. break;
  6927. case PIPECONF_8BPC:
  6928. pipe_config->pipe_bpp = 24;
  6929. break;
  6930. case PIPECONF_10BPC:
  6931. pipe_config->pipe_bpp = 30;
  6932. break;
  6933. default:
  6934. break;
  6935. }
  6936. }
  6937. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6938. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6939. pipe_config->limited_color_range = true;
  6940. if (INTEL_INFO(dev)->gen < 4)
  6941. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6942. intel_get_pipe_timings(crtc, pipe_config);
  6943. intel_get_pipe_src_size(crtc, pipe_config);
  6944. i9xx_get_pfit_config(crtc, pipe_config);
  6945. if (INTEL_INFO(dev)->gen >= 4) {
  6946. /* No way to read it out on pipes B and C */
  6947. if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
  6948. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6949. else
  6950. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6951. pipe_config->pixel_multiplier =
  6952. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6953. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6954. pipe_config->dpll_hw_state.dpll_md = tmp;
  6955. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6956. tmp = I915_READ(DPLL(crtc->pipe));
  6957. pipe_config->pixel_multiplier =
  6958. ((tmp & SDVO_MULTIPLIER_MASK)
  6959. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6960. } else {
  6961. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6962. * port and will be fixed up in the encoder->get_config
  6963. * function. */
  6964. pipe_config->pixel_multiplier = 1;
  6965. }
  6966. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6967. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  6968. /*
  6969. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6970. * on 830. Filter it out here so that we don't
  6971. * report errors due to that.
  6972. */
  6973. if (IS_I830(dev))
  6974. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6975. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6976. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6977. } else {
  6978. /* Mask out read-only status bits. */
  6979. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6980. DPLL_PORTC_READY_MASK |
  6981. DPLL_PORTB_READY_MASK);
  6982. }
  6983. if (IS_CHERRYVIEW(dev))
  6984. chv_crtc_clock_get(crtc, pipe_config);
  6985. else if (IS_VALLEYVIEW(dev))
  6986. vlv_crtc_clock_get(crtc, pipe_config);
  6987. else
  6988. i9xx_crtc_clock_get(crtc, pipe_config);
  6989. /*
  6990. * Normally the dotclock is filled in by the encoder .get_config()
  6991. * but in case the pipe is enabled w/o any ports we need a sane
  6992. * default.
  6993. */
  6994. pipe_config->base.adjusted_mode.crtc_clock =
  6995. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6996. ret = true;
  6997. out:
  6998. intel_display_power_put(dev_priv, power_domain);
  6999. return ret;
  7000. }
  7001. static void ironlake_init_pch_refclk(struct drm_device *dev)
  7002. {
  7003. struct drm_i915_private *dev_priv = to_i915(dev);
  7004. struct intel_encoder *encoder;
  7005. int i;
  7006. u32 val, final;
  7007. bool has_lvds = false;
  7008. bool has_cpu_edp = false;
  7009. bool has_panel = false;
  7010. bool has_ck505 = false;
  7011. bool can_ssc = false;
  7012. bool using_ssc_source = false;
  7013. /* We need to take the global config into account */
  7014. for_each_intel_encoder(dev, encoder) {
  7015. switch (encoder->type) {
  7016. case INTEL_OUTPUT_LVDS:
  7017. has_panel = true;
  7018. has_lvds = true;
  7019. break;
  7020. case INTEL_OUTPUT_EDP:
  7021. has_panel = true;
  7022. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7023. has_cpu_edp = true;
  7024. break;
  7025. default:
  7026. break;
  7027. }
  7028. }
  7029. if (HAS_PCH_IBX(dev)) {
  7030. has_ck505 = dev_priv->vbt.display_clock_mode;
  7031. can_ssc = has_ck505;
  7032. } else {
  7033. has_ck505 = false;
  7034. can_ssc = true;
  7035. }
  7036. /* Check if any DPLLs are using the SSC source */
  7037. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7038. u32 temp = I915_READ(PCH_DPLL(i));
  7039. if (!(temp & DPLL_VCO_ENABLE))
  7040. continue;
  7041. if ((temp & PLL_REF_INPUT_MASK) ==
  7042. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7043. using_ssc_source = true;
  7044. break;
  7045. }
  7046. }
  7047. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7048. has_panel, has_lvds, has_ck505, using_ssc_source);
  7049. /* Ironlake: try to setup display ref clock before DPLL
  7050. * enabling. This is only under driver's control after
  7051. * PCH B stepping, previous chipset stepping should be
  7052. * ignoring this setting.
  7053. */
  7054. val = I915_READ(PCH_DREF_CONTROL);
  7055. /* As we must carefully and slowly disable/enable each source in turn,
  7056. * compute the final state we want first and check if we need to
  7057. * make any changes at all.
  7058. */
  7059. final = val;
  7060. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7061. if (has_ck505)
  7062. final |= DREF_NONSPREAD_CK505_ENABLE;
  7063. else
  7064. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7065. final &= ~DREF_SSC_SOURCE_MASK;
  7066. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7067. final &= ~DREF_SSC1_ENABLE;
  7068. if (has_panel) {
  7069. final |= DREF_SSC_SOURCE_ENABLE;
  7070. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7071. final |= DREF_SSC1_ENABLE;
  7072. if (has_cpu_edp) {
  7073. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7074. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7075. else
  7076. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7077. } else
  7078. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7079. } else if (using_ssc_source) {
  7080. final |= DREF_SSC_SOURCE_ENABLE;
  7081. final |= DREF_SSC1_ENABLE;
  7082. }
  7083. if (final == val)
  7084. return;
  7085. /* Always enable nonspread source */
  7086. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7087. if (has_ck505)
  7088. val |= DREF_NONSPREAD_CK505_ENABLE;
  7089. else
  7090. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7091. if (has_panel) {
  7092. val &= ~DREF_SSC_SOURCE_MASK;
  7093. val |= DREF_SSC_SOURCE_ENABLE;
  7094. /* SSC must be turned on before enabling the CPU output */
  7095. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7096. DRM_DEBUG_KMS("Using SSC on panel\n");
  7097. val |= DREF_SSC1_ENABLE;
  7098. } else
  7099. val &= ~DREF_SSC1_ENABLE;
  7100. /* Get SSC going before enabling the outputs */
  7101. I915_WRITE(PCH_DREF_CONTROL, val);
  7102. POSTING_READ(PCH_DREF_CONTROL);
  7103. udelay(200);
  7104. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7105. /* Enable CPU source on CPU attached eDP */
  7106. if (has_cpu_edp) {
  7107. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7108. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7109. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7110. } else
  7111. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7112. } else
  7113. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7114. I915_WRITE(PCH_DREF_CONTROL, val);
  7115. POSTING_READ(PCH_DREF_CONTROL);
  7116. udelay(200);
  7117. } else {
  7118. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7119. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7120. /* Turn off CPU output */
  7121. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7122. I915_WRITE(PCH_DREF_CONTROL, val);
  7123. POSTING_READ(PCH_DREF_CONTROL);
  7124. udelay(200);
  7125. if (!using_ssc_source) {
  7126. DRM_DEBUG_KMS("Disabling SSC source\n");
  7127. /* Turn off the SSC source */
  7128. val &= ~DREF_SSC_SOURCE_MASK;
  7129. val |= DREF_SSC_SOURCE_DISABLE;
  7130. /* Turn off SSC1 */
  7131. val &= ~DREF_SSC1_ENABLE;
  7132. I915_WRITE(PCH_DREF_CONTROL, val);
  7133. POSTING_READ(PCH_DREF_CONTROL);
  7134. udelay(200);
  7135. }
  7136. }
  7137. BUG_ON(val != final);
  7138. }
  7139. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7140. {
  7141. uint32_t tmp;
  7142. tmp = I915_READ(SOUTH_CHICKEN2);
  7143. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7144. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7145. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  7146. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7147. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7148. tmp = I915_READ(SOUTH_CHICKEN2);
  7149. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7150. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7151. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  7152. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7153. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7154. }
  7155. /* WaMPhyProgramming:hsw */
  7156. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7157. {
  7158. uint32_t tmp;
  7159. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7160. tmp &= ~(0xFF << 24);
  7161. tmp |= (0x12 << 24);
  7162. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7163. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7164. tmp |= (1 << 11);
  7165. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7166. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7167. tmp |= (1 << 11);
  7168. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7169. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7170. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7171. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7172. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7173. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7174. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7175. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7176. tmp &= ~(7 << 13);
  7177. tmp |= (5 << 13);
  7178. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7179. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7180. tmp &= ~(7 << 13);
  7181. tmp |= (5 << 13);
  7182. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7183. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7184. tmp &= ~0xFF;
  7185. tmp |= 0x1C;
  7186. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7187. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7188. tmp &= ~0xFF;
  7189. tmp |= 0x1C;
  7190. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7191. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7192. tmp &= ~(0xFF << 16);
  7193. tmp |= (0x1C << 16);
  7194. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7195. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7196. tmp &= ~(0xFF << 16);
  7197. tmp |= (0x1C << 16);
  7198. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7199. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7200. tmp |= (1 << 27);
  7201. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7202. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7203. tmp |= (1 << 27);
  7204. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7205. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7206. tmp &= ~(0xF << 28);
  7207. tmp |= (4 << 28);
  7208. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7209. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7210. tmp &= ~(0xF << 28);
  7211. tmp |= (4 << 28);
  7212. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7213. }
  7214. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7215. * Programming" based on the parameters passed:
  7216. * - Sequence to enable CLKOUT_DP
  7217. * - Sequence to enable CLKOUT_DP without spread
  7218. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7219. */
  7220. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7221. bool with_fdi)
  7222. {
  7223. struct drm_i915_private *dev_priv = to_i915(dev);
  7224. uint32_t reg, tmp;
  7225. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7226. with_spread = true;
  7227. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7228. with_fdi = false;
  7229. mutex_lock(&dev_priv->sb_lock);
  7230. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7231. tmp &= ~SBI_SSCCTL_DISABLE;
  7232. tmp |= SBI_SSCCTL_PATHALT;
  7233. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7234. udelay(24);
  7235. if (with_spread) {
  7236. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7237. tmp &= ~SBI_SSCCTL_PATHALT;
  7238. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7239. if (with_fdi) {
  7240. lpt_reset_fdi_mphy(dev_priv);
  7241. lpt_program_fdi_mphy(dev_priv);
  7242. }
  7243. }
  7244. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7245. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7246. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7247. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7248. mutex_unlock(&dev_priv->sb_lock);
  7249. }
  7250. /* Sequence to disable CLKOUT_DP */
  7251. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7252. {
  7253. struct drm_i915_private *dev_priv = to_i915(dev);
  7254. uint32_t reg, tmp;
  7255. mutex_lock(&dev_priv->sb_lock);
  7256. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7257. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7258. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7259. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7260. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7261. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7262. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7263. tmp |= SBI_SSCCTL_PATHALT;
  7264. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7265. udelay(32);
  7266. }
  7267. tmp |= SBI_SSCCTL_DISABLE;
  7268. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7269. }
  7270. mutex_unlock(&dev_priv->sb_lock);
  7271. }
  7272. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7273. static const uint16_t sscdivintphase[] = {
  7274. [BEND_IDX( 50)] = 0x3B23,
  7275. [BEND_IDX( 45)] = 0x3B23,
  7276. [BEND_IDX( 40)] = 0x3C23,
  7277. [BEND_IDX( 35)] = 0x3C23,
  7278. [BEND_IDX( 30)] = 0x3D23,
  7279. [BEND_IDX( 25)] = 0x3D23,
  7280. [BEND_IDX( 20)] = 0x3E23,
  7281. [BEND_IDX( 15)] = 0x3E23,
  7282. [BEND_IDX( 10)] = 0x3F23,
  7283. [BEND_IDX( 5)] = 0x3F23,
  7284. [BEND_IDX( 0)] = 0x0025,
  7285. [BEND_IDX( -5)] = 0x0025,
  7286. [BEND_IDX(-10)] = 0x0125,
  7287. [BEND_IDX(-15)] = 0x0125,
  7288. [BEND_IDX(-20)] = 0x0225,
  7289. [BEND_IDX(-25)] = 0x0225,
  7290. [BEND_IDX(-30)] = 0x0325,
  7291. [BEND_IDX(-35)] = 0x0325,
  7292. [BEND_IDX(-40)] = 0x0425,
  7293. [BEND_IDX(-45)] = 0x0425,
  7294. [BEND_IDX(-50)] = 0x0525,
  7295. };
  7296. /*
  7297. * Bend CLKOUT_DP
  7298. * steps -50 to 50 inclusive, in steps of 5
  7299. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7300. * change in clock period = -(steps / 10) * 5.787 ps
  7301. */
  7302. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7303. {
  7304. uint32_t tmp;
  7305. int idx = BEND_IDX(steps);
  7306. if (WARN_ON(steps % 5 != 0))
  7307. return;
  7308. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7309. return;
  7310. mutex_lock(&dev_priv->sb_lock);
  7311. if (steps % 10 != 0)
  7312. tmp = 0xAAAAAAAB;
  7313. else
  7314. tmp = 0x00000000;
  7315. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7316. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7317. tmp &= 0xffff0000;
  7318. tmp |= sscdivintphase[idx];
  7319. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7320. mutex_unlock(&dev_priv->sb_lock);
  7321. }
  7322. #undef BEND_IDX
  7323. static void lpt_init_pch_refclk(struct drm_device *dev)
  7324. {
  7325. struct intel_encoder *encoder;
  7326. bool has_vga = false;
  7327. for_each_intel_encoder(dev, encoder) {
  7328. switch (encoder->type) {
  7329. case INTEL_OUTPUT_ANALOG:
  7330. has_vga = true;
  7331. break;
  7332. default:
  7333. break;
  7334. }
  7335. }
  7336. if (has_vga) {
  7337. lpt_bend_clkout_dp(to_i915(dev), 0);
  7338. lpt_enable_clkout_dp(dev, true, true);
  7339. } else {
  7340. lpt_disable_clkout_dp(dev);
  7341. }
  7342. }
  7343. /*
  7344. * Initialize reference clocks when the driver loads
  7345. */
  7346. void intel_init_pch_refclk(struct drm_device *dev)
  7347. {
  7348. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7349. ironlake_init_pch_refclk(dev);
  7350. else if (HAS_PCH_LPT(dev))
  7351. lpt_init_pch_refclk(dev);
  7352. }
  7353. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7354. {
  7355. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7356. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7357. int pipe = intel_crtc->pipe;
  7358. uint32_t val;
  7359. val = 0;
  7360. switch (intel_crtc->config->pipe_bpp) {
  7361. case 18:
  7362. val |= PIPECONF_6BPC;
  7363. break;
  7364. case 24:
  7365. val |= PIPECONF_8BPC;
  7366. break;
  7367. case 30:
  7368. val |= PIPECONF_10BPC;
  7369. break;
  7370. case 36:
  7371. val |= PIPECONF_12BPC;
  7372. break;
  7373. default:
  7374. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7375. BUG();
  7376. }
  7377. if (intel_crtc->config->dither)
  7378. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7379. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7380. val |= PIPECONF_INTERLACED_ILK;
  7381. else
  7382. val |= PIPECONF_PROGRESSIVE;
  7383. if (intel_crtc->config->limited_color_range)
  7384. val |= PIPECONF_COLOR_RANGE_SELECT;
  7385. I915_WRITE(PIPECONF(pipe), val);
  7386. POSTING_READ(PIPECONF(pipe));
  7387. }
  7388. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7389. {
  7390. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7391. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7392. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7393. u32 val = 0;
  7394. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7395. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7396. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7397. val |= PIPECONF_INTERLACED_ILK;
  7398. else
  7399. val |= PIPECONF_PROGRESSIVE;
  7400. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7401. POSTING_READ(PIPECONF(cpu_transcoder));
  7402. }
  7403. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7404. {
  7405. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7406. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7407. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7408. u32 val = 0;
  7409. switch (intel_crtc->config->pipe_bpp) {
  7410. case 18:
  7411. val |= PIPEMISC_DITHER_6_BPC;
  7412. break;
  7413. case 24:
  7414. val |= PIPEMISC_DITHER_8_BPC;
  7415. break;
  7416. case 30:
  7417. val |= PIPEMISC_DITHER_10_BPC;
  7418. break;
  7419. case 36:
  7420. val |= PIPEMISC_DITHER_12_BPC;
  7421. break;
  7422. default:
  7423. /* Case prevented by pipe_config_set_bpp. */
  7424. BUG();
  7425. }
  7426. if (intel_crtc->config->dither)
  7427. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7428. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7429. }
  7430. }
  7431. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7432. {
  7433. /*
  7434. * Account for spread spectrum to avoid
  7435. * oversubscribing the link. Max center spread
  7436. * is 2.5%; use 5% for safety's sake.
  7437. */
  7438. u32 bps = target_clock * bpp * 21 / 20;
  7439. return DIV_ROUND_UP(bps, link_bw * 8);
  7440. }
  7441. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7442. {
  7443. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7444. }
  7445. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7446. struct intel_crtc_state *crtc_state,
  7447. struct dpll *reduced_clock)
  7448. {
  7449. struct drm_crtc *crtc = &intel_crtc->base;
  7450. struct drm_device *dev = crtc->dev;
  7451. struct drm_i915_private *dev_priv = to_i915(dev);
  7452. u32 dpll, fp, fp2;
  7453. int factor;
  7454. /* Enable autotuning of the PLL clock (if permissible) */
  7455. factor = 21;
  7456. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7457. if ((intel_panel_use_ssc(dev_priv) &&
  7458. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7459. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7460. factor = 25;
  7461. } else if (crtc_state->sdvo_tv_clock)
  7462. factor = 20;
  7463. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7464. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7465. fp |= FP_CB_TUNE;
  7466. if (reduced_clock) {
  7467. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7468. if (reduced_clock->m < factor * reduced_clock->n)
  7469. fp2 |= FP_CB_TUNE;
  7470. } else {
  7471. fp2 = fp;
  7472. }
  7473. dpll = 0;
  7474. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7475. dpll |= DPLLB_MODE_LVDS;
  7476. else
  7477. dpll |= DPLLB_MODE_DAC_SERIAL;
  7478. dpll |= (crtc_state->pixel_multiplier - 1)
  7479. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7480. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7481. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7482. dpll |= DPLL_SDVO_HIGH_SPEED;
  7483. if (intel_crtc_has_dp_encoder(crtc_state))
  7484. dpll |= DPLL_SDVO_HIGH_SPEED;
  7485. /* compute bitmask from p1 value */
  7486. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7487. /* also FPA1 */
  7488. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7489. switch (crtc_state->dpll.p2) {
  7490. case 5:
  7491. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7492. break;
  7493. case 7:
  7494. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7495. break;
  7496. case 10:
  7497. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7498. break;
  7499. case 14:
  7500. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7501. break;
  7502. }
  7503. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7504. intel_panel_use_ssc(dev_priv))
  7505. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7506. else
  7507. dpll |= PLL_REF_INPUT_DREFCLK;
  7508. dpll |= DPLL_VCO_ENABLE;
  7509. crtc_state->dpll_hw_state.dpll = dpll;
  7510. crtc_state->dpll_hw_state.fp0 = fp;
  7511. crtc_state->dpll_hw_state.fp1 = fp2;
  7512. }
  7513. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7514. struct intel_crtc_state *crtc_state)
  7515. {
  7516. struct drm_device *dev = crtc->base.dev;
  7517. struct drm_i915_private *dev_priv = to_i915(dev);
  7518. struct dpll reduced_clock;
  7519. bool has_reduced_clock = false;
  7520. struct intel_shared_dpll *pll;
  7521. const struct intel_limit *limit;
  7522. int refclk = 120000;
  7523. memset(&crtc_state->dpll_hw_state, 0,
  7524. sizeof(crtc_state->dpll_hw_state));
  7525. crtc->lowfreq_avail = false;
  7526. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7527. if (!crtc_state->has_pch_encoder)
  7528. return 0;
  7529. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7530. if (intel_panel_use_ssc(dev_priv)) {
  7531. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7532. dev_priv->vbt.lvds_ssc_freq);
  7533. refclk = dev_priv->vbt.lvds_ssc_freq;
  7534. }
  7535. if (intel_is_dual_link_lvds(dev)) {
  7536. if (refclk == 100000)
  7537. limit = &intel_limits_ironlake_dual_lvds_100m;
  7538. else
  7539. limit = &intel_limits_ironlake_dual_lvds;
  7540. } else {
  7541. if (refclk == 100000)
  7542. limit = &intel_limits_ironlake_single_lvds_100m;
  7543. else
  7544. limit = &intel_limits_ironlake_single_lvds;
  7545. }
  7546. } else {
  7547. limit = &intel_limits_ironlake_dac;
  7548. }
  7549. if (!crtc_state->clock_set &&
  7550. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7551. refclk, NULL, &crtc_state->dpll)) {
  7552. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7553. return -EINVAL;
  7554. }
  7555. ironlake_compute_dpll(crtc, crtc_state,
  7556. has_reduced_clock ? &reduced_clock : NULL);
  7557. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  7558. if (pll == NULL) {
  7559. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7560. pipe_name(crtc->pipe));
  7561. return -EINVAL;
  7562. }
  7563. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7564. has_reduced_clock)
  7565. crtc->lowfreq_avail = true;
  7566. return 0;
  7567. }
  7568. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7569. struct intel_link_m_n *m_n)
  7570. {
  7571. struct drm_device *dev = crtc->base.dev;
  7572. struct drm_i915_private *dev_priv = to_i915(dev);
  7573. enum pipe pipe = crtc->pipe;
  7574. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7575. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7576. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7577. & ~TU_SIZE_MASK;
  7578. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7579. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7580. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7581. }
  7582. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7583. enum transcoder transcoder,
  7584. struct intel_link_m_n *m_n,
  7585. struct intel_link_m_n *m2_n2)
  7586. {
  7587. struct drm_device *dev = crtc->base.dev;
  7588. struct drm_i915_private *dev_priv = to_i915(dev);
  7589. enum pipe pipe = crtc->pipe;
  7590. if (INTEL_INFO(dev)->gen >= 5) {
  7591. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7592. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7593. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7594. & ~TU_SIZE_MASK;
  7595. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7596. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7597. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7598. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7599. * gen < 8) and if DRRS is supported (to make sure the
  7600. * registers are not unnecessarily read).
  7601. */
  7602. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7603. crtc->config->has_drrs) {
  7604. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7605. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7606. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7607. & ~TU_SIZE_MASK;
  7608. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7609. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7610. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7611. }
  7612. } else {
  7613. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7614. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7615. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7616. & ~TU_SIZE_MASK;
  7617. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7618. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7619. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7620. }
  7621. }
  7622. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7623. struct intel_crtc_state *pipe_config)
  7624. {
  7625. if (pipe_config->has_pch_encoder)
  7626. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7627. else
  7628. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7629. &pipe_config->dp_m_n,
  7630. &pipe_config->dp_m2_n2);
  7631. }
  7632. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7633. struct intel_crtc_state *pipe_config)
  7634. {
  7635. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7636. &pipe_config->fdi_m_n, NULL);
  7637. }
  7638. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7639. struct intel_crtc_state *pipe_config)
  7640. {
  7641. struct drm_device *dev = crtc->base.dev;
  7642. struct drm_i915_private *dev_priv = to_i915(dev);
  7643. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7644. uint32_t ps_ctrl = 0;
  7645. int id = -1;
  7646. int i;
  7647. /* find scaler attached to this pipe */
  7648. for (i = 0; i < crtc->num_scalers; i++) {
  7649. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7650. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7651. id = i;
  7652. pipe_config->pch_pfit.enabled = true;
  7653. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7654. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7655. break;
  7656. }
  7657. }
  7658. scaler_state->scaler_id = id;
  7659. if (id >= 0) {
  7660. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7661. } else {
  7662. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7663. }
  7664. }
  7665. static void
  7666. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7667. struct intel_initial_plane_config *plane_config)
  7668. {
  7669. struct drm_device *dev = crtc->base.dev;
  7670. struct drm_i915_private *dev_priv = to_i915(dev);
  7671. u32 val, base, offset, stride_mult, tiling;
  7672. int pipe = crtc->pipe;
  7673. int fourcc, pixel_format;
  7674. unsigned int aligned_height;
  7675. struct drm_framebuffer *fb;
  7676. struct intel_framebuffer *intel_fb;
  7677. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7678. if (!intel_fb) {
  7679. DRM_DEBUG_KMS("failed to alloc fb\n");
  7680. return;
  7681. }
  7682. fb = &intel_fb->base;
  7683. val = I915_READ(PLANE_CTL(pipe, 0));
  7684. if (!(val & PLANE_CTL_ENABLE))
  7685. goto error;
  7686. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7687. fourcc = skl_format_to_fourcc(pixel_format,
  7688. val & PLANE_CTL_ORDER_RGBX,
  7689. val & PLANE_CTL_ALPHA_MASK);
  7690. fb->pixel_format = fourcc;
  7691. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7692. tiling = val & PLANE_CTL_TILED_MASK;
  7693. switch (tiling) {
  7694. case PLANE_CTL_TILED_LINEAR:
  7695. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7696. break;
  7697. case PLANE_CTL_TILED_X:
  7698. plane_config->tiling = I915_TILING_X;
  7699. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7700. break;
  7701. case PLANE_CTL_TILED_Y:
  7702. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7703. break;
  7704. case PLANE_CTL_TILED_YF:
  7705. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7706. break;
  7707. default:
  7708. MISSING_CASE(tiling);
  7709. goto error;
  7710. }
  7711. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7712. plane_config->base = base;
  7713. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7714. val = I915_READ(PLANE_SIZE(pipe, 0));
  7715. fb->height = ((val >> 16) & 0xfff) + 1;
  7716. fb->width = ((val >> 0) & 0x1fff) + 1;
  7717. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7718. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  7719. fb->pixel_format);
  7720. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7721. aligned_height = intel_fb_align_height(dev, fb->height,
  7722. fb->pixel_format,
  7723. fb->modifier[0]);
  7724. plane_config->size = fb->pitches[0] * aligned_height;
  7725. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7726. pipe_name(pipe), fb->width, fb->height,
  7727. fb->bits_per_pixel, base, fb->pitches[0],
  7728. plane_config->size);
  7729. plane_config->fb = intel_fb;
  7730. return;
  7731. error:
  7732. kfree(fb);
  7733. }
  7734. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7735. struct intel_crtc_state *pipe_config)
  7736. {
  7737. struct drm_device *dev = crtc->base.dev;
  7738. struct drm_i915_private *dev_priv = to_i915(dev);
  7739. uint32_t tmp;
  7740. tmp = I915_READ(PF_CTL(crtc->pipe));
  7741. if (tmp & PF_ENABLE) {
  7742. pipe_config->pch_pfit.enabled = true;
  7743. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7744. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7745. /* We currently do not free assignements of panel fitters on
  7746. * ivb/hsw (since we don't use the higher upscaling modes which
  7747. * differentiates them) so just WARN about this case for now. */
  7748. if (IS_GEN7(dev)) {
  7749. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7750. PF_PIPE_SEL_IVB(crtc->pipe));
  7751. }
  7752. }
  7753. }
  7754. static void
  7755. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7756. struct intel_initial_plane_config *plane_config)
  7757. {
  7758. struct drm_device *dev = crtc->base.dev;
  7759. struct drm_i915_private *dev_priv = to_i915(dev);
  7760. u32 val, base, offset;
  7761. int pipe = crtc->pipe;
  7762. int fourcc, pixel_format;
  7763. unsigned int aligned_height;
  7764. struct drm_framebuffer *fb;
  7765. struct intel_framebuffer *intel_fb;
  7766. val = I915_READ(DSPCNTR(pipe));
  7767. if (!(val & DISPLAY_PLANE_ENABLE))
  7768. return;
  7769. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7770. if (!intel_fb) {
  7771. DRM_DEBUG_KMS("failed to alloc fb\n");
  7772. return;
  7773. }
  7774. fb = &intel_fb->base;
  7775. if (INTEL_INFO(dev)->gen >= 4) {
  7776. if (val & DISPPLANE_TILED) {
  7777. plane_config->tiling = I915_TILING_X;
  7778. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7779. }
  7780. }
  7781. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7782. fourcc = i9xx_format_to_fourcc(pixel_format);
  7783. fb->pixel_format = fourcc;
  7784. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7785. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7786. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7787. offset = I915_READ(DSPOFFSET(pipe));
  7788. } else {
  7789. if (plane_config->tiling)
  7790. offset = I915_READ(DSPTILEOFF(pipe));
  7791. else
  7792. offset = I915_READ(DSPLINOFF(pipe));
  7793. }
  7794. plane_config->base = base;
  7795. val = I915_READ(PIPESRC(pipe));
  7796. fb->width = ((val >> 16) & 0xfff) + 1;
  7797. fb->height = ((val >> 0) & 0xfff) + 1;
  7798. val = I915_READ(DSPSTRIDE(pipe));
  7799. fb->pitches[0] = val & 0xffffffc0;
  7800. aligned_height = intel_fb_align_height(dev, fb->height,
  7801. fb->pixel_format,
  7802. fb->modifier[0]);
  7803. plane_config->size = fb->pitches[0] * aligned_height;
  7804. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7805. pipe_name(pipe), fb->width, fb->height,
  7806. fb->bits_per_pixel, base, fb->pitches[0],
  7807. plane_config->size);
  7808. plane_config->fb = intel_fb;
  7809. }
  7810. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7811. struct intel_crtc_state *pipe_config)
  7812. {
  7813. struct drm_device *dev = crtc->base.dev;
  7814. struct drm_i915_private *dev_priv = to_i915(dev);
  7815. enum intel_display_power_domain power_domain;
  7816. uint32_t tmp;
  7817. bool ret;
  7818. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7819. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7820. return false;
  7821. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7822. pipe_config->shared_dpll = NULL;
  7823. ret = false;
  7824. tmp = I915_READ(PIPECONF(crtc->pipe));
  7825. if (!(tmp & PIPECONF_ENABLE))
  7826. goto out;
  7827. switch (tmp & PIPECONF_BPC_MASK) {
  7828. case PIPECONF_6BPC:
  7829. pipe_config->pipe_bpp = 18;
  7830. break;
  7831. case PIPECONF_8BPC:
  7832. pipe_config->pipe_bpp = 24;
  7833. break;
  7834. case PIPECONF_10BPC:
  7835. pipe_config->pipe_bpp = 30;
  7836. break;
  7837. case PIPECONF_12BPC:
  7838. pipe_config->pipe_bpp = 36;
  7839. break;
  7840. default:
  7841. break;
  7842. }
  7843. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7844. pipe_config->limited_color_range = true;
  7845. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7846. struct intel_shared_dpll *pll;
  7847. enum intel_dpll_id pll_id;
  7848. pipe_config->has_pch_encoder = true;
  7849. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7850. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7851. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7852. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7853. if (HAS_PCH_IBX(dev_priv)) {
  7854. /*
  7855. * The pipe->pch transcoder and pch transcoder->pll
  7856. * mapping is fixed.
  7857. */
  7858. pll_id = (enum intel_dpll_id) crtc->pipe;
  7859. } else {
  7860. tmp = I915_READ(PCH_DPLL_SEL);
  7861. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7862. pll_id = DPLL_ID_PCH_PLL_B;
  7863. else
  7864. pll_id= DPLL_ID_PCH_PLL_A;
  7865. }
  7866. pipe_config->shared_dpll =
  7867. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7868. pll = pipe_config->shared_dpll;
  7869. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7870. &pipe_config->dpll_hw_state));
  7871. tmp = pipe_config->dpll_hw_state.dpll;
  7872. pipe_config->pixel_multiplier =
  7873. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7874. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7875. ironlake_pch_clock_get(crtc, pipe_config);
  7876. } else {
  7877. pipe_config->pixel_multiplier = 1;
  7878. }
  7879. intel_get_pipe_timings(crtc, pipe_config);
  7880. intel_get_pipe_src_size(crtc, pipe_config);
  7881. ironlake_get_pfit_config(crtc, pipe_config);
  7882. ret = true;
  7883. out:
  7884. intel_display_power_put(dev_priv, power_domain);
  7885. return ret;
  7886. }
  7887. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7888. {
  7889. struct drm_device *dev = &dev_priv->drm;
  7890. struct intel_crtc *crtc;
  7891. for_each_intel_crtc(dev, crtc)
  7892. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7893. pipe_name(crtc->pipe));
  7894. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7895. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7896. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7897. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7898. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7899. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7900. "CPU PWM1 enabled\n");
  7901. if (IS_HASWELL(dev))
  7902. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7903. "CPU PWM2 enabled\n");
  7904. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7905. "PCH PWM1 enabled\n");
  7906. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7907. "Utility pin enabled\n");
  7908. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7909. /*
  7910. * In theory we can still leave IRQs enabled, as long as only the HPD
  7911. * interrupts remain enabled. We used to check for that, but since it's
  7912. * gen-specific and since we only disable LCPLL after we fully disable
  7913. * the interrupts, the check below should be enough.
  7914. */
  7915. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7916. }
  7917. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7918. {
  7919. struct drm_device *dev = &dev_priv->drm;
  7920. if (IS_HASWELL(dev))
  7921. return I915_READ(D_COMP_HSW);
  7922. else
  7923. return I915_READ(D_COMP_BDW);
  7924. }
  7925. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7926. {
  7927. struct drm_device *dev = &dev_priv->drm;
  7928. if (IS_HASWELL(dev)) {
  7929. mutex_lock(&dev_priv->rps.hw_lock);
  7930. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7931. val))
  7932. DRM_ERROR("Failed to write to D_COMP\n");
  7933. mutex_unlock(&dev_priv->rps.hw_lock);
  7934. } else {
  7935. I915_WRITE(D_COMP_BDW, val);
  7936. POSTING_READ(D_COMP_BDW);
  7937. }
  7938. }
  7939. /*
  7940. * This function implements pieces of two sequences from BSpec:
  7941. * - Sequence for display software to disable LCPLL
  7942. * - Sequence for display software to allow package C8+
  7943. * The steps implemented here are just the steps that actually touch the LCPLL
  7944. * register. Callers should take care of disabling all the display engine
  7945. * functions, doing the mode unset, fixing interrupts, etc.
  7946. */
  7947. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7948. bool switch_to_fclk, bool allow_power_down)
  7949. {
  7950. uint32_t val;
  7951. assert_can_disable_lcpll(dev_priv);
  7952. val = I915_READ(LCPLL_CTL);
  7953. if (switch_to_fclk) {
  7954. val |= LCPLL_CD_SOURCE_FCLK;
  7955. I915_WRITE(LCPLL_CTL, val);
  7956. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7957. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7958. DRM_ERROR("Switching to FCLK failed\n");
  7959. val = I915_READ(LCPLL_CTL);
  7960. }
  7961. val |= LCPLL_PLL_DISABLE;
  7962. I915_WRITE(LCPLL_CTL, val);
  7963. POSTING_READ(LCPLL_CTL);
  7964. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7965. DRM_ERROR("LCPLL still locked\n");
  7966. val = hsw_read_dcomp(dev_priv);
  7967. val |= D_COMP_COMP_DISABLE;
  7968. hsw_write_dcomp(dev_priv, val);
  7969. ndelay(100);
  7970. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7971. 1))
  7972. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7973. if (allow_power_down) {
  7974. val = I915_READ(LCPLL_CTL);
  7975. val |= LCPLL_POWER_DOWN_ALLOW;
  7976. I915_WRITE(LCPLL_CTL, val);
  7977. POSTING_READ(LCPLL_CTL);
  7978. }
  7979. }
  7980. /*
  7981. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7982. * source.
  7983. */
  7984. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7985. {
  7986. uint32_t val;
  7987. val = I915_READ(LCPLL_CTL);
  7988. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7989. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7990. return;
  7991. /*
  7992. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7993. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7994. */
  7995. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7996. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7997. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7998. I915_WRITE(LCPLL_CTL, val);
  7999. POSTING_READ(LCPLL_CTL);
  8000. }
  8001. val = hsw_read_dcomp(dev_priv);
  8002. val |= D_COMP_COMP_FORCE;
  8003. val &= ~D_COMP_COMP_DISABLE;
  8004. hsw_write_dcomp(dev_priv, val);
  8005. val = I915_READ(LCPLL_CTL);
  8006. val &= ~LCPLL_PLL_DISABLE;
  8007. I915_WRITE(LCPLL_CTL, val);
  8008. if (intel_wait_for_register(dev_priv,
  8009. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  8010. 5))
  8011. DRM_ERROR("LCPLL not locked yet\n");
  8012. if (val & LCPLL_CD_SOURCE_FCLK) {
  8013. val = I915_READ(LCPLL_CTL);
  8014. val &= ~LCPLL_CD_SOURCE_FCLK;
  8015. I915_WRITE(LCPLL_CTL, val);
  8016. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8017. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8018. DRM_ERROR("Switching back to LCPLL failed\n");
  8019. }
  8020. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8021. intel_update_cdclk(&dev_priv->drm);
  8022. }
  8023. /*
  8024. * Package states C8 and deeper are really deep PC states that can only be
  8025. * reached when all the devices on the system allow it, so even if the graphics
  8026. * device allows PC8+, it doesn't mean the system will actually get to these
  8027. * states. Our driver only allows PC8+ when going into runtime PM.
  8028. *
  8029. * The requirements for PC8+ are that all the outputs are disabled, the power
  8030. * well is disabled and most interrupts are disabled, and these are also
  8031. * requirements for runtime PM. When these conditions are met, we manually do
  8032. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8033. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8034. * hang the machine.
  8035. *
  8036. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8037. * the state of some registers, so when we come back from PC8+ we need to
  8038. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8039. * need to take care of the registers kept by RC6. Notice that this happens even
  8040. * if we don't put the device in PCI D3 state (which is what currently happens
  8041. * because of the runtime PM support).
  8042. *
  8043. * For more, read "Display Sequences for Package C8" on the hardware
  8044. * documentation.
  8045. */
  8046. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8047. {
  8048. struct drm_device *dev = &dev_priv->drm;
  8049. uint32_t val;
  8050. DRM_DEBUG_KMS("Enabling package C8+\n");
  8051. if (HAS_PCH_LPT_LP(dev)) {
  8052. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8053. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8054. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8055. }
  8056. lpt_disable_clkout_dp(dev);
  8057. hsw_disable_lcpll(dev_priv, true, true);
  8058. }
  8059. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8060. {
  8061. struct drm_device *dev = &dev_priv->drm;
  8062. uint32_t val;
  8063. DRM_DEBUG_KMS("Disabling package C8+\n");
  8064. hsw_restore_lcpll(dev_priv);
  8065. lpt_init_pch_refclk(dev);
  8066. if (HAS_PCH_LPT_LP(dev)) {
  8067. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8068. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8069. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8070. }
  8071. }
  8072. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8073. {
  8074. struct drm_device *dev = old_state->dev;
  8075. struct intel_atomic_state *old_intel_state =
  8076. to_intel_atomic_state(old_state);
  8077. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8078. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8079. }
  8080. /* compute the max rate for new configuration */
  8081. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8082. {
  8083. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8084. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8085. struct drm_crtc *crtc;
  8086. struct drm_crtc_state *cstate;
  8087. struct intel_crtc_state *crtc_state;
  8088. unsigned max_pixel_rate = 0, i;
  8089. enum pipe pipe;
  8090. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8091. sizeof(intel_state->min_pixclk));
  8092. for_each_crtc_in_state(state, crtc, cstate, i) {
  8093. int pixel_rate;
  8094. crtc_state = to_intel_crtc_state(cstate);
  8095. if (!crtc_state->base.enable) {
  8096. intel_state->min_pixclk[i] = 0;
  8097. continue;
  8098. }
  8099. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8100. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8101. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8102. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8103. intel_state->min_pixclk[i] = pixel_rate;
  8104. }
  8105. for_each_pipe(dev_priv, pipe)
  8106. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8107. return max_pixel_rate;
  8108. }
  8109. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8110. {
  8111. struct drm_i915_private *dev_priv = to_i915(dev);
  8112. uint32_t val, data;
  8113. int ret;
  8114. if (WARN((I915_READ(LCPLL_CTL) &
  8115. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8116. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8117. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8118. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8119. "trying to change cdclk frequency with cdclk not enabled\n"))
  8120. return;
  8121. mutex_lock(&dev_priv->rps.hw_lock);
  8122. ret = sandybridge_pcode_write(dev_priv,
  8123. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8124. mutex_unlock(&dev_priv->rps.hw_lock);
  8125. if (ret) {
  8126. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8127. return;
  8128. }
  8129. val = I915_READ(LCPLL_CTL);
  8130. val |= LCPLL_CD_SOURCE_FCLK;
  8131. I915_WRITE(LCPLL_CTL, val);
  8132. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8133. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8134. DRM_ERROR("Switching to FCLK failed\n");
  8135. val = I915_READ(LCPLL_CTL);
  8136. val &= ~LCPLL_CLK_FREQ_MASK;
  8137. switch (cdclk) {
  8138. case 450000:
  8139. val |= LCPLL_CLK_FREQ_450;
  8140. data = 0;
  8141. break;
  8142. case 540000:
  8143. val |= LCPLL_CLK_FREQ_54O_BDW;
  8144. data = 1;
  8145. break;
  8146. case 337500:
  8147. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8148. data = 2;
  8149. break;
  8150. case 675000:
  8151. val |= LCPLL_CLK_FREQ_675_BDW;
  8152. data = 3;
  8153. break;
  8154. default:
  8155. WARN(1, "invalid cdclk frequency\n");
  8156. return;
  8157. }
  8158. I915_WRITE(LCPLL_CTL, val);
  8159. val = I915_READ(LCPLL_CTL);
  8160. val &= ~LCPLL_CD_SOURCE_FCLK;
  8161. I915_WRITE(LCPLL_CTL, val);
  8162. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8163. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8164. DRM_ERROR("Switching back to LCPLL failed\n");
  8165. mutex_lock(&dev_priv->rps.hw_lock);
  8166. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8167. mutex_unlock(&dev_priv->rps.hw_lock);
  8168. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8169. intel_update_cdclk(dev);
  8170. WARN(cdclk != dev_priv->cdclk_freq,
  8171. "cdclk requested %d kHz but got %d kHz\n",
  8172. cdclk, dev_priv->cdclk_freq);
  8173. }
  8174. static int broadwell_calc_cdclk(int max_pixclk)
  8175. {
  8176. if (max_pixclk > 540000)
  8177. return 675000;
  8178. else if (max_pixclk > 450000)
  8179. return 540000;
  8180. else if (max_pixclk > 337500)
  8181. return 450000;
  8182. else
  8183. return 337500;
  8184. }
  8185. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8186. {
  8187. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8188. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8189. int max_pixclk = ilk_max_pixel_rate(state);
  8190. int cdclk;
  8191. /*
  8192. * FIXME should also account for plane ratio
  8193. * once 64bpp pixel formats are supported.
  8194. */
  8195. cdclk = broadwell_calc_cdclk(max_pixclk);
  8196. if (cdclk > dev_priv->max_cdclk_freq) {
  8197. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8198. cdclk, dev_priv->max_cdclk_freq);
  8199. return -EINVAL;
  8200. }
  8201. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8202. if (!intel_state->active_crtcs)
  8203. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8204. return 0;
  8205. }
  8206. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8207. {
  8208. struct drm_device *dev = old_state->dev;
  8209. struct intel_atomic_state *old_intel_state =
  8210. to_intel_atomic_state(old_state);
  8211. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8212. broadwell_set_cdclk(dev, req_cdclk);
  8213. }
  8214. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8215. {
  8216. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8217. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8218. const int max_pixclk = ilk_max_pixel_rate(state);
  8219. int vco = intel_state->cdclk_pll_vco;
  8220. int cdclk;
  8221. /*
  8222. * FIXME should also account for plane ratio
  8223. * once 64bpp pixel formats are supported.
  8224. */
  8225. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8226. /*
  8227. * FIXME move the cdclk caclulation to
  8228. * compute_config() so we can fail gracegully.
  8229. */
  8230. if (cdclk > dev_priv->max_cdclk_freq) {
  8231. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8232. cdclk, dev_priv->max_cdclk_freq);
  8233. cdclk = dev_priv->max_cdclk_freq;
  8234. }
  8235. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8236. if (!intel_state->active_crtcs)
  8237. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8238. return 0;
  8239. }
  8240. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8241. {
  8242. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8243. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8244. unsigned int req_cdclk = intel_state->dev_cdclk;
  8245. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8246. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8247. }
  8248. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8249. struct intel_crtc_state *crtc_state)
  8250. {
  8251. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  8252. if (!intel_ddi_pll_select(crtc, crtc_state))
  8253. return -EINVAL;
  8254. }
  8255. crtc->lowfreq_avail = false;
  8256. return 0;
  8257. }
  8258. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8259. enum port port,
  8260. struct intel_crtc_state *pipe_config)
  8261. {
  8262. enum intel_dpll_id id;
  8263. switch (port) {
  8264. case PORT_A:
  8265. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8266. id = DPLL_ID_SKL_DPLL0;
  8267. break;
  8268. case PORT_B:
  8269. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8270. id = DPLL_ID_SKL_DPLL1;
  8271. break;
  8272. case PORT_C:
  8273. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8274. id = DPLL_ID_SKL_DPLL2;
  8275. break;
  8276. default:
  8277. DRM_ERROR("Incorrect port type\n");
  8278. return;
  8279. }
  8280. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8281. }
  8282. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8283. enum port port,
  8284. struct intel_crtc_state *pipe_config)
  8285. {
  8286. enum intel_dpll_id id;
  8287. u32 temp;
  8288. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8289. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8290. switch (pipe_config->ddi_pll_sel) {
  8291. case SKL_DPLL0:
  8292. id = DPLL_ID_SKL_DPLL0;
  8293. break;
  8294. case SKL_DPLL1:
  8295. id = DPLL_ID_SKL_DPLL1;
  8296. break;
  8297. case SKL_DPLL2:
  8298. id = DPLL_ID_SKL_DPLL2;
  8299. break;
  8300. case SKL_DPLL3:
  8301. id = DPLL_ID_SKL_DPLL3;
  8302. break;
  8303. default:
  8304. MISSING_CASE(pipe_config->ddi_pll_sel);
  8305. return;
  8306. }
  8307. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8308. }
  8309. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8310. enum port port,
  8311. struct intel_crtc_state *pipe_config)
  8312. {
  8313. enum intel_dpll_id id;
  8314. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8315. switch (pipe_config->ddi_pll_sel) {
  8316. case PORT_CLK_SEL_WRPLL1:
  8317. id = DPLL_ID_WRPLL1;
  8318. break;
  8319. case PORT_CLK_SEL_WRPLL2:
  8320. id = DPLL_ID_WRPLL2;
  8321. break;
  8322. case PORT_CLK_SEL_SPLL:
  8323. id = DPLL_ID_SPLL;
  8324. break;
  8325. case PORT_CLK_SEL_LCPLL_810:
  8326. id = DPLL_ID_LCPLL_810;
  8327. break;
  8328. case PORT_CLK_SEL_LCPLL_1350:
  8329. id = DPLL_ID_LCPLL_1350;
  8330. break;
  8331. case PORT_CLK_SEL_LCPLL_2700:
  8332. id = DPLL_ID_LCPLL_2700;
  8333. break;
  8334. default:
  8335. MISSING_CASE(pipe_config->ddi_pll_sel);
  8336. /* fall through */
  8337. case PORT_CLK_SEL_NONE:
  8338. return;
  8339. }
  8340. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8341. }
  8342. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8343. struct intel_crtc_state *pipe_config,
  8344. unsigned long *power_domain_mask)
  8345. {
  8346. struct drm_device *dev = crtc->base.dev;
  8347. struct drm_i915_private *dev_priv = to_i915(dev);
  8348. enum intel_display_power_domain power_domain;
  8349. u32 tmp;
  8350. /*
  8351. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8352. * transcoder handled below.
  8353. */
  8354. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8355. /*
  8356. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8357. * consistency and less surprising code; it's in always on power).
  8358. */
  8359. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8360. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8361. enum pipe trans_edp_pipe;
  8362. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8363. default:
  8364. WARN(1, "unknown pipe linked to edp transcoder\n");
  8365. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8366. case TRANS_DDI_EDP_INPUT_A_ON:
  8367. trans_edp_pipe = PIPE_A;
  8368. break;
  8369. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8370. trans_edp_pipe = PIPE_B;
  8371. break;
  8372. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8373. trans_edp_pipe = PIPE_C;
  8374. break;
  8375. }
  8376. if (trans_edp_pipe == crtc->pipe)
  8377. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8378. }
  8379. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8380. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8381. return false;
  8382. *power_domain_mask |= BIT(power_domain);
  8383. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8384. return tmp & PIPECONF_ENABLE;
  8385. }
  8386. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8387. struct intel_crtc_state *pipe_config,
  8388. unsigned long *power_domain_mask)
  8389. {
  8390. struct drm_device *dev = crtc->base.dev;
  8391. struct drm_i915_private *dev_priv = to_i915(dev);
  8392. enum intel_display_power_domain power_domain;
  8393. enum port port;
  8394. enum transcoder cpu_transcoder;
  8395. u32 tmp;
  8396. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8397. if (port == PORT_A)
  8398. cpu_transcoder = TRANSCODER_DSI_A;
  8399. else
  8400. cpu_transcoder = TRANSCODER_DSI_C;
  8401. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8402. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8403. continue;
  8404. *power_domain_mask |= BIT(power_domain);
  8405. /*
  8406. * The PLL needs to be enabled with a valid divider
  8407. * configuration, otherwise accessing DSI registers will hang
  8408. * the machine. See BSpec North Display Engine
  8409. * registers/MIPI[BXT]. We can break out here early, since we
  8410. * need the same DSI PLL to be enabled for both DSI ports.
  8411. */
  8412. if (!intel_dsi_pll_is_enabled(dev_priv))
  8413. break;
  8414. /* XXX: this works for video mode only */
  8415. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8416. if (!(tmp & DPI_ENABLE))
  8417. continue;
  8418. tmp = I915_READ(MIPI_CTRL(port));
  8419. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8420. continue;
  8421. pipe_config->cpu_transcoder = cpu_transcoder;
  8422. break;
  8423. }
  8424. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  8425. }
  8426. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8427. struct intel_crtc_state *pipe_config)
  8428. {
  8429. struct drm_device *dev = crtc->base.dev;
  8430. struct drm_i915_private *dev_priv = to_i915(dev);
  8431. struct intel_shared_dpll *pll;
  8432. enum port port;
  8433. uint32_t tmp;
  8434. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8435. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8436. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8437. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8438. else if (IS_BROXTON(dev))
  8439. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8440. else
  8441. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8442. pll = pipe_config->shared_dpll;
  8443. if (pll) {
  8444. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8445. &pipe_config->dpll_hw_state));
  8446. }
  8447. /*
  8448. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8449. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8450. * the PCH transcoder is on.
  8451. */
  8452. if (INTEL_INFO(dev)->gen < 9 &&
  8453. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8454. pipe_config->has_pch_encoder = true;
  8455. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8456. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8457. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8458. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8459. }
  8460. }
  8461. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8462. struct intel_crtc_state *pipe_config)
  8463. {
  8464. struct drm_device *dev = crtc->base.dev;
  8465. struct drm_i915_private *dev_priv = to_i915(dev);
  8466. enum intel_display_power_domain power_domain;
  8467. unsigned long power_domain_mask;
  8468. bool active;
  8469. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8470. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8471. return false;
  8472. power_domain_mask = BIT(power_domain);
  8473. pipe_config->shared_dpll = NULL;
  8474. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8475. if (IS_BROXTON(dev_priv) &&
  8476. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  8477. WARN_ON(active);
  8478. active = true;
  8479. }
  8480. if (!active)
  8481. goto out;
  8482. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8483. haswell_get_ddi_port_state(crtc, pipe_config);
  8484. intel_get_pipe_timings(crtc, pipe_config);
  8485. }
  8486. intel_get_pipe_src_size(crtc, pipe_config);
  8487. pipe_config->gamma_mode =
  8488. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8489. if (INTEL_INFO(dev)->gen >= 9) {
  8490. skl_init_scalers(dev, crtc, pipe_config);
  8491. }
  8492. if (INTEL_INFO(dev)->gen >= 9) {
  8493. pipe_config->scaler_state.scaler_id = -1;
  8494. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8495. }
  8496. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8497. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8498. power_domain_mask |= BIT(power_domain);
  8499. if (INTEL_INFO(dev)->gen >= 9)
  8500. skylake_get_pfit_config(crtc, pipe_config);
  8501. else
  8502. ironlake_get_pfit_config(crtc, pipe_config);
  8503. }
  8504. if (IS_HASWELL(dev))
  8505. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8506. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8507. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8508. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8509. pipe_config->pixel_multiplier =
  8510. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8511. } else {
  8512. pipe_config->pixel_multiplier = 1;
  8513. }
  8514. out:
  8515. for_each_power_domain(power_domain, power_domain_mask)
  8516. intel_display_power_put(dev_priv, power_domain);
  8517. return active;
  8518. }
  8519. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  8520. const struct intel_plane_state *plane_state)
  8521. {
  8522. struct drm_device *dev = crtc->dev;
  8523. struct drm_i915_private *dev_priv = to_i915(dev);
  8524. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8525. uint32_t cntl = 0, size = 0;
  8526. if (plane_state && plane_state->visible) {
  8527. unsigned int width = plane_state->base.crtc_w;
  8528. unsigned int height = plane_state->base.crtc_h;
  8529. unsigned int stride = roundup_pow_of_two(width) * 4;
  8530. switch (stride) {
  8531. default:
  8532. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8533. width, stride);
  8534. stride = 256;
  8535. /* fallthrough */
  8536. case 256:
  8537. case 512:
  8538. case 1024:
  8539. case 2048:
  8540. break;
  8541. }
  8542. cntl |= CURSOR_ENABLE |
  8543. CURSOR_GAMMA_ENABLE |
  8544. CURSOR_FORMAT_ARGB |
  8545. CURSOR_STRIDE(stride);
  8546. size = (height << 12) | width;
  8547. }
  8548. if (intel_crtc->cursor_cntl != 0 &&
  8549. (intel_crtc->cursor_base != base ||
  8550. intel_crtc->cursor_size != size ||
  8551. intel_crtc->cursor_cntl != cntl)) {
  8552. /* On these chipsets we can only modify the base/size/stride
  8553. * whilst the cursor is disabled.
  8554. */
  8555. I915_WRITE(CURCNTR(PIPE_A), 0);
  8556. POSTING_READ(CURCNTR(PIPE_A));
  8557. intel_crtc->cursor_cntl = 0;
  8558. }
  8559. if (intel_crtc->cursor_base != base) {
  8560. I915_WRITE(CURBASE(PIPE_A), base);
  8561. intel_crtc->cursor_base = base;
  8562. }
  8563. if (intel_crtc->cursor_size != size) {
  8564. I915_WRITE(CURSIZE, size);
  8565. intel_crtc->cursor_size = size;
  8566. }
  8567. if (intel_crtc->cursor_cntl != cntl) {
  8568. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8569. POSTING_READ(CURCNTR(PIPE_A));
  8570. intel_crtc->cursor_cntl = cntl;
  8571. }
  8572. }
  8573. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  8574. const struct intel_plane_state *plane_state)
  8575. {
  8576. struct drm_device *dev = crtc->dev;
  8577. struct drm_i915_private *dev_priv = to_i915(dev);
  8578. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8579. int pipe = intel_crtc->pipe;
  8580. uint32_t cntl = 0;
  8581. if (plane_state && plane_state->visible) {
  8582. cntl = MCURSOR_GAMMA_ENABLE;
  8583. switch (plane_state->base.crtc_w) {
  8584. case 64:
  8585. cntl |= CURSOR_MODE_64_ARGB_AX;
  8586. break;
  8587. case 128:
  8588. cntl |= CURSOR_MODE_128_ARGB_AX;
  8589. break;
  8590. case 256:
  8591. cntl |= CURSOR_MODE_256_ARGB_AX;
  8592. break;
  8593. default:
  8594. MISSING_CASE(plane_state->base.crtc_w);
  8595. return;
  8596. }
  8597. cntl |= pipe << 28; /* Connect to correct pipe */
  8598. if (HAS_DDI(dev))
  8599. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8600. if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
  8601. cntl |= CURSOR_ROTATE_180;
  8602. }
  8603. if (intel_crtc->cursor_cntl != cntl) {
  8604. I915_WRITE(CURCNTR(pipe), cntl);
  8605. POSTING_READ(CURCNTR(pipe));
  8606. intel_crtc->cursor_cntl = cntl;
  8607. }
  8608. /* and commit changes on next vblank */
  8609. I915_WRITE(CURBASE(pipe), base);
  8610. POSTING_READ(CURBASE(pipe));
  8611. intel_crtc->cursor_base = base;
  8612. }
  8613. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8614. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8615. const struct intel_plane_state *plane_state)
  8616. {
  8617. struct drm_device *dev = crtc->dev;
  8618. struct drm_i915_private *dev_priv = to_i915(dev);
  8619. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8620. int pipe = intel_crtc->pipe;
  8621. u32 base = intel_crtc->cursor_addr;
  8622. u32 pos = 0;
  8623. if (plane_state) {
  8624. int x = plane_state->base.crtc_x;
  8625. int y = plane_state->base.crtc_y;
  8626. if (x < 0) {
  8627. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8628. x = -x;
  8629. }
  8630. pos |= x << CURSOR_X_SHIFT;
  8631. if (y < 0) {
  8632. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8633. y = -y;
  8634. }
  8635. pos |= y << CURSOR_Y_SHIFT;
  8636. /* ILK+ do this automagically */
  8637. if (HAS_GMCH_DISPLAY(dev) &&
  8638. plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  8639. base += (plane_state->base.crtc_h *
  8640. plane_state->base.crtc_w - 1) * 4;
  8641. }
  8642. }
  8643. I915_WRITE(CURPOS(pipe), pos);
  8644. if (IS_845G(dev) || IS_I865G(dev))
  8645. i845_update_cursor(crtc, base, plane_state);
  8646. else
  8647. i9xx_update_cursor(crtc, base, plane_state);
  8648. }
  8649. static bool cursor_size_ok(struct drm_device *dev,
  8650. uint32_t width, uint32_t height)
  8651. {
  8652. if (width == 0 || height == 0)
  8653. return false;
  8654. /*
  8655. * 845g/865g are special in that they are only limited by
  8656. * the width of their cursors, the height is arbitrary up to
  8657. * the precision of the register. Everything else requires
  8658. * square cursors, limited to a few power-of-two sizes.
  8659. */
  8660. if (IS_845G(dev) || IS_I865G(dev)) {
  8661. if ((width & 63) != 0)
  8662. return false;
  8663. if (width > (IS_845G(dev) ? 64 : 512))
  8664. return false;
  8665. if (height > 1023)
  8666. return false;
  8667. } else {
  8668. switch (width | height) {
  8669. case 256:
  8670. case 128:
  8671. if (IS_GEN2(dev))
  8672. return false;
  8673. case 64:
  8674. break;
  8675. default:
  8676. return false;
  8677. }
  8678. }
  8679. return true;
  8680. }
  8681. /* VESA 640x480x72Hz mode to set on the pipe */
  8682. static struct drm_display_mode load_detect_mode = {
  8683. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8684. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8685. };
  8686. struct drm_framebuffer *
  8687. __intel_framebuffer_create(struct drm_device *dev,
  8688. struct drm_mode_fb_cmd2 *mode_cmd,
  8689. struct drm_i915_gem_object *obj)
  8690. {
  8691. struct intel_framebuffer *intel_fb;
  8692. int ret;
  8693. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8694. if (!intel_fb)
  8695. return ERR_PTR(-ENOMEM);
  8696. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8697. if (ret)
  8698. goto err;
  8699. return &intel_fb->base;
  8700. err:
  8701. kfree(intel_fb);
  8702. return ERR_PTR(ret);
  8703. }
  8704. static struct drm_framebuffer *
  8705. intel_framebuffer_create(struct drm_device *dev,
  8706. struct drm_mode_fb_cmd2 *mode_cmd,
  8707. struct drm_i915_gem_object *obj)
  8708. {
  8709. struct drm_framebuffer *fb;
  8710. int ret;
  8711. ret = i915_mutex_lock_interruptible(dev);
  8712. if (ret)
  8713. return ERR_PTR(ret);
  8714. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8715. mutex_unlock(&dev->struct_mutex);
  8716. return fb;
  8717. }
  8718. static u32
  8719. intel_framebuffer_pitch_for_width(int width, int bpp)
  8720. {
  8721. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8722. return ALIGN(pitch, 64);
  8723. }
  8724. static u32
  8725. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8726. {
  8727. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8728. return PAGE_ALIGN(pitch * mode->vdisplay);
  8729. }
  8730. static struct drm_framebuffer *
  8731. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8732. struct drm_display_mode *mode,
  8733. int depth, int bpp)
  8734. {
  8735. struct drm_framebuffer *fb;
  8736. struct drm_i915_gem_object *obj;
  8737. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8738. obj = i915_gem_object_create(dev,
  8739. intel_framebuffer_size_for_mode(mode, bpp));
  8740. if (IS_ERR(obj))
  8741. return ERR_CAST(obj);
  8742. mode_cmd.width = mode->hdisplay;
  8743. mode_cmd.height = mode->vdisplay;
  8744. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8745. bpp);
  8746. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8747. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8748. if (IS_ERR(fb))
  8749. i915_gem_object_put_unlocked(obj);
  8750. return fb;
  8751. }
  8752. static struct drm_framebuffer *
  8753. mode_fits_in_fbdev(struct drm_device *dev,
  8754. struct drm_display_mode *mode)
  8755. {
  8756. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8757. struct drm_i915_private *dev_priv = to_i915(dev);
  8758. struct drm_i915_gem_object *obj;
  8759. struct drm_framebuffer *fb;
  8760. if (!dev_priv->fbdev)
  8761. return NULL;
  8762. if (!dev_priv->fbdev->fb)
  8763. return NULL;
  8764. obj = dev_priv->fbdev->fb->obj;
  8765. BUG_ON(!obj);
  8766. fb = &dev_priv->fbdev->fb->base;
  8767. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8768. fb->bits_per_pixel))
  8769. return NULL;
  8770. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8771. return NULL;
  8772. drm_framebuffer_reference(fb);
  8773. return fb;
  8774. #else
  8775. return NULL;
  8776. #endif
  8777. }
  8778. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8779. struct drm_crtc *crtc,
  8780. struct drm_display_mode *mode,
  8781. struct drm_framebuffer *fb,
  8782. int x, int y)
  8783. {
  8784. struct drm_plane_state *plane_state;
  8785. int hdisplay, vdisplay;
  8786. int ret;
  8787. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8788. if (IS_ERR(plane_state))
  8789. return PTR_ERR(plane_state);
  8790. if (mode)
  8791. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8792. else
  8793. hdisplay = vdisplay = 0;
  8794. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8795. if (ret)
  8796. return ret;
  8797. drm_atomic_set_fb_for_plane(plane_state, fb);
  8798. plane_state->crtc_x = 0;
  8799. plane_state->crtc_y = 0;
  8800. plane_state->crtc_w = hdisplay;
  8801. plane_state->crtc_h = vdisplay;
  8802. plane_state->src_x = x << 16;
  8803. plane_state->src_y = y << 16;
  8804. plane_state->src_w = hdisplay << 16;
  8805. plane_state->src_h = vdisplay << 16;
  8806. return 0;
  8807. }
  8808. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8809. struct drm_display_mode *mode,
  8810. struct intel_load_detect_pipe *old,
  8811. struct drm_modeset_acquire_ctx *ctx)
  8812. {
  8813. struct intel_crtc *intel_crtc;
  8814. struct intel_encoder *intel_encoder =
  8815. intel_attached_encoder(connector);
  8816. struct drm_crtc *possible_crtc;
  8817. struct drm_encoder *encoder = &intel_encoder->base;
  8818. struct drm_crtc *crtc = NULL;
  8819. struct drm_device *dev = encoder->dev;
  8820. struct drm_framebuffer *fb;
  8821. struct drm_mode_config *config = &dev->mode_config;
  8822. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8823. struct drm_connector_state *connector_state;
  8824. struct intel_crtc_state *crtc_state;
  8825. int ret, i = -1;
  8826. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8827. connector->base.id, connector->name,
  8828. encoder->base.id, encoder->name);
  8829. old->restore_state = NULL;
  8830. retry:
  8831. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8832. if (ret)
  8833. goto fail;
  8834. /*
  8835. * Algorithm gets a little messy:
  8836. *
  8837. * - if the connector already has an assigned crtc, use it (but make
  8838. * sure it's on first)
  8839. *
  8840. * - try to find the first unused crtc that can drive this connector,
  8841. * and use that if we find one
  8842. */
  8843. /* See if we already have a CRTC for this connector */
  8844. if (connector->state->crtc) {
  8845. crtc = connector->state->crtc;
  8846. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8847. if (ret)
  8848. goto fail;
  8849. /* Make sure the crtc and connector are running */
  8850. goto found;
  8851. }
  8852. /* Find an unused one (if possible) */
  8853. for_each_crtc(dev, possible_crtc) {
  8854. i++;
  8855. if (!(encoder->possible_crtcs & (1 << i)))
  8856. continue;
  8857. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8858. if (ret)
  8859. goto fail;
  8860. if (possible_crtc->state->enable) {
  8861. drm_modeset_unlock(&possible_crtc->mutex);
  8862. continue;
  8863. }
  8864. crtc = possible_crtc;
  8865. break;
  8866. }
  8867. /*
  8868. * If we didn't find an unused CRTC, don't use any.
  8869. */
  8870. if (!crtc) {
  8871. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8872. goto fail;
  8873. }
  8874. found:
  8875. intel_crtc = to_intel_crtc(crtc);
  8876. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8877. if (ret)
  8878. goto fail;
  8879. state = drm_atomic_state_alloc(dev);
  8880. restore_state = drm_atomic_state_alloc(dev);
  8881. if (!state || !restore_state) {
  8882. ret = -ENOMEM;
  8883. goto fail;
  8884. }
  8885. state->acquire_ctx = ctx;
  8886. restore_state->acquire_ctx = ctx;
  8887. connector_state = drm_atomic_get_connector_state(state, connector);
  8888. if (IS_ERR(connector_state)) {
  8889. ret = PTR_ERR(connector_state);
  8890. goto fail;
  8891. }
  8892. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8893. if (ret)
  8894. goto fail;
  8895. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8896. if (IS_ERR(crtc_state)) {
  8897. ret = PTR_ERR(crtc_state);
  8898. goto fail;
  8899. }
  8900. crtc_state->base.active = crtc_state->base.enable = true;
  8901. if (!mode)
  8902. mode = &load_detect_mode;
  8903. /* We need a framebuffer large enough to accommodate all accesses
  8904. * that the plane may generate whilst we perform load detection.
  8905. * We can not rely on the fbcon either being present (we get called
  8906. * during its initialisation to detect all boot displays, or it may
  8907. * not even exist) or that it is large enough to satisfy the
  8908. * requested mode.
  8909. */
  8910. fb = mode_fits_in_fbdev(dev, mode);
  8911. if (fb == NULL) {
  8912. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8913. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8914. } else
  8915. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8916. if (IS_ERR(fb)) {
  8917. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8918. goto fail;
  8919. }
  8920. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8921. if (ret)
  8922. goto fail;
  8923. drm_framebuffer_unreference(fb);
  8924. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8925. if (ret)
  8926. goto fail;
  8927. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8928. if (!ret)
  8929. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8930. if (!ret)
  8931. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8932. if (ret) {
  8933. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8934. goto fail;
  8935. }
  8936. ret = drm_atomic_commit(state);
  8937. if (ret) {
  8938. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8939. goto fail;
  8940. }
  8941. old->restore_state = restore_state;
  8942. /* let the connector get through one full cycle before testing */
  8943. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8944. return true;
  8945. fail:
  8946. drm_atomic_state_free(state);
  8947. drm_atomic_state_free(restore_state);
  8948. restore_state = state = NULL;
  8949. if (ret == -EDEADLK) {
  8950. drm_modeset_backoff(ctx);
  8951. goto retry;
  8952. }
  8953. return false;
  8954. }
  8955. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8956. struct intel_load_detect_pipe *old,
  8957. struct drm_modeset_acquire_ctx *ctx)
  8958. {
  8959. struct intel_encoder *intel_encoder =
  8960. intel_attached_encoder(connector);
  8961. struct drm_encoder *encoder = &intel_encoder->base;
  8962. struct drm_atomic_state *state = old->restore_state;
  8963. int ret;
  8964. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8965. connector->base.id, connector->name,
  8966. encoder->base.id, encoder->name);
  8967. if (!state)
  8968. return;
  8969. ret = drm_atomic_commit(state);
  8970. if (ret) {
  8971. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8972. drm_atomic_state_free(state);
  8973. }
  8974. }
  8975. static int i9xx_pll_refclk(struct drm_device *dev,
  8976. const struct intel_crtc_state *pipe_config)
  8977. {
  8978. struct drm_i915_private *dev_priv = to_i915(dev);
  8979. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8980. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8981. return dev_priv->vbt.lvds_ssc_freq;
  8982. else if (HAS_PCH_SPLIT(dev))
  8983. return 120000;
  8984. else if (!IS_GEN2(dev))
  8985. return 96000;
  8986. else
  8987. return 48000;
  8988. }
  8989. /* Returns the clock of the currently programmed mode of the given pipe. */
  8990. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8991. struct intel_crtc_state *pipe_config)
  8992. {
  8993. struct drm_device *dev = crtc->base.dev;
  8994. struct drm_i915_private *dev_priv = to_i915(dev);
  8995. int pipe = pipe_config->cpu_transcoder;
  8996. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8997. u32 fp;
  8998. struct dpll clock;
  8999. int port_clock;
  9000. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9001. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9002. fp = pipe_config->dpll_hw_state.fp0;
  9003. else
  9004. fp = pipe_config->dpll_hw_state.fp1;
  9005. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9006. if (IS_PINEVIEW(dev)) {
  9007. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9008. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9009. } else {
  9010. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9011. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9012. }
  9013. if (!IS_GEN2(dev)) {
  9014. if (IS_PINEVIEW(dev))
  9015. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9016. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9017. else
  9018. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9019. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9020. switch (dpll & DPLL_MODE_MASK) {
  9021. case DPLLB_MODE_DAC_SERIAL:
  9022. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9023. 5 : 10;
  9024. break;
  9025. case DPLLB_MODE_LVDS:
  9026. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9027. 7 : 14;
  9028. break;
  9029. default:
  9030. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9031. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9032. return;
  9033. }
  9034. if (IS_PINEVIEW(dev))
  9035. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9036. else
  9037. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9038. } else {
  9039. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  9040. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9041. if (is_lvds) {
  9042. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9043. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9044. if (lvds & LVDS_CLKB_POWER_UP)
  9045. clock.p2 = 7;
  9046. else
  9047. clock.p2 = 14;
  9048. } else {
  9049. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9050. clock.p1 = 2;
  9051. else {
  9052. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9053. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9054. }
  9055. if (dpll & PLL_P2_DIVIDE_BY_4)
  9056. clock.p2 = 4;
  9057. else
  9058. clock.p2 = 2;
  9059. }
  9060. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9061. }
  9062. /*
  9063. * This value includes pixel_multiplier. We will use
  9064. * port_clock to compute adjusted_mode.crtc_clock in the
  9065. * encoder's get_config() function.
  9066. */
  9067. pipe_config->port_clock = port_clock;
  9068. }
  9069. int intel_dotclock_calculate(int link_freq,
  9070. const struct intel_link_m_n *m_n)
  9071. {
  9072. /*
  9073. * The calculation for the data clock is:
  9074. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9075. * But we want to avoid losing precison if possible, so:
  9076. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9077. *
  9078. * and the link clock is simpler:
  9079. * link_clock = (m * link_clock) / n
  9080. */
  9081. if (!m_n->link_n)
  9082. return 0;
  9083. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9084. }
  9085. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9086. struct intel_crtc_state *pipe_config)
  9087. {
  9088. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9089. /* read out port_clock from the DPLL */
  9090. i9xx_crtc_clock_get(crtc, pipe_config);
  9091. /*
  9092. * In case there is an active pipe without active ports,
  9093. * we may need some idea for the dotclock anyway.
  9094. * Calculate one based on the FDI configuration.
  9095. */
  9096. pipe_config->base.adjusted_mode.crtc_clock =
  9097. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9098. &pipe_config->fdi_m_n);
  9099. }
  9100. /** Returns the currently programmed mode of the given pipe. */
  9101. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9102. struct drm_crtc *crtc)
  9103. {
  9104. struct drm_i915_private *dev_priv = to_i915(dev);
  9105. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9106. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9107. struct drm_display_mode *mode;
  9108. struct intel_crtc_state *pipe_config;
  9109. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9110. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9111. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9112. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9113. enum pipe pipe = intel_crtc->pipe;
  9114. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9115. if (!mode)
  9116. return NULL;
  9117. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9118. if (!pipe_config) {
  9119. kfree(mode);
  9120. return NULL;
  9121. }
  9122. /*
  9123. * Construct a pipe_config sufficient for getting the clock info
  9124. * back out of crtc_clock_get.
  9125. *
  9126. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9127. * to use a real value here instead.
  9128. */
  9129. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9130. pipe_config->pixel_multiplier = 1;
  9131. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9132. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9133. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9134. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9135. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9136. mode->hdisplay = (htot & 0xffff) + 1;
  9137. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9138. mode->hsync_start = (hsync & 0xffff) + 1;
  9139. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9140. mode->vdisplay = (vtot & 0xffff) + 1;
  9141. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9142. mode->vsync_start = (vsync & 0xffff) + 1;
  9143. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9144. drm_mode_set_name(mode);
  9145. kfree(pipe_config);
  9146. return mode;
  9147. }
  9148. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9149. {
  9150. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9151. struct drm_device *dev = crtc->dev;
  9152. struct intel_flip_work *work;
  9153. spin_lock_irq(&dev->event_lock);
  9154. work = intel_crtc->flip_work;
  9155. intel_crtc->flip_work = NULL;
  9156. spin_unlock_irq(&dev->event_lock);
  9157. if (work) {
  9158. cancel_work_sync(&work->mmio_work);
  9159. cancel_work_sync(&work->unpin_work);
  9160. kfree(work);
  9161. }
  9162. drm_crtc_cleanup(crtc);
  9163. kfree(intel_crtc);
  9164. }
  9165. static void intel_unpin_work_fn(struct work_struct *__work)
  9166. {
  9167. struct intel_flip_work *work =
  9168. container_of(__work, struct intel_flip_work, unpin_work);
  9169. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9170. struct drm_device *dev = crtc->base.dev;
  9171. struct drm_plane *primary = crtc->base.primary;
  9172. if (is_mmio_work(work))
  9173. flush_work(&work->mmio_work);
  9174. mutex_lock(&dev->struct_mutex);
  9175. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9176. i915_gem_object_put(work->pending_flip_obj);
  9177. mutex_unlock(&dev->struct_mutex);
  9178. i915_gem_request_put(work->flip_queued_req);
  9179. intel_frontbuffer_flip_complete(to_i915(dev),
  9180. to_intel_plane(primary)->frontbuffer_bit);
  9181. intel_fbc_post_update(crtc);
  9182. drm_framebuffer_unreference(work->old_fb);
  9183. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9184. atomic_dec(&crtc->unpin_work_count);
  9185. kfree(work);
  9186. }
  9187. /* Is 'a' after or equal to 'b'? */
  9188. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9189. {
  9190. return !((a - b) & 0x80000000);
  9191. }
  9192. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9193. struct intel_flip_work *work)
  9194. {
  9195. struct drm_device *dev = crtc->base.dev;
  9196. struct drm_i915_private *dev_priv = to_i915(dev);
  9197. unsigned reset_counter;
  9198. reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9199. if (crtc->reset_counter != reset_counter)
  9200. return true;
  9201. /*
  9202. * The relevant registers doen't exist on pre-ctg.
  9203. * As the flip done interrupt doesn't trigger for mmio
  9204. * flips on gmch platforms, a flip count check isn't
  9205. * really needed there. But since ctg has the registers,
  9206. * include it in the check anyway.
  9207. */
  9208. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9209. return true;
  9210. /*
  9211. * BDW signals flip done immediately if the plane
  9212. * is disabled, even if the plane enable is already
  9213. * armed to occur at the next vblank :(
  9214. */
  9215. /*
  9216. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9217. * used the same base address. In that case the mmio flip might
  9218. * have completed, but the CS hasn't even executed the flip yet.
  9219. *
  9220. * A flip count check isn't enough as the CS might have updated
  9221. * the base address just after start of vblank, but before we
  9222. * managed to process the interrupt. This means we'd complete the
  9223. * CS flip too soon.
  9224. *
  9225. * Combining both checks should get us a good enough result. It may
  9226. * still happen that the CS flip has been executed, but has not
  9227. * yet actually completed. But in case the base address is the same
  9228. * anyway, we don't really care.
  9229. */
  9230. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9231. crtc->flip_work->gtt_offset &&
  9232. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9233. crtc->flip_work->flip_count);
  9234. }
  9235. static bool
  9236. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9237. struct intel_flip_work *work)
  9238. {
  9239. /*
  9240. * MMIO work completes when vblank is different from
  9241. * flip_queued_vblank.
  9242. *
  9243. * Reset counter value doesn't matter, this is handled by
  9244. * i915_wait_request finishing early, so no need to handle
  9245. * reset here.
  9246. */
  9247. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9248. }
  9249. static bool pageflip_finished(struct intel_crtc *crtc,
  9250. struct intel_flip_work *work)
  9251. {
  9252. if (!atomic_read(&work->pending))
  9253. return false;
  9254. smp_rmb();
  9255. if (is_mmio_work(work))
  9256. return __pageflip_finished_mmio(crtc, work);
  9257. else
  9258. return __pageflip_finished_cs(crtc, work);
  9259. }
  9260. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9261. {
  9262. struct drm_device *dev = &dev_priv->drm;
  9263. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9264. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9265. struct intel_flip_work *work;
  9266. unsigned long flags;
  9267. /* Ignore early vblank irqs */
  9268. if (!crtc)
  9269. return;
  9270. /*
  9271. * This is called both by irq handlers and the reset code (to complete
  9272. * lost pageflips) so needs the full irqsave spinlocks.
  9273. */
  9274. spin_lock_irqsave(&dev->event_lock, flags);
  9275. work = intel_crtc->flip_work;
  9276. if (work != NULL &&
  9277. !is_mmio_work(work) &&
  9278. pageflip_finished(intel_crtc, work))
  9279. page_flip_completed(intel_crtc);
  9280. spin_unlock_irqrestore(&dev->event_lock, flags);
  9281. }
  9282. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9283. {
  9284. struct drm_device *dev = &dev_priv->drm;
  9285. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9286. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9287. struct intel_flip_work *work;
  9288. unsigned long flags;
  9289. /* Ignore early vblank irqs */
  9290. if (!crtc)
  9291. return;
  9292. /*
  9293. * This is called both by irq handlers and the reset code (to complete
  9294. * lost pageflips) so needs the full irqsave spinlocks.
  9295. */
  9296. spin_lock_irqsave(&dev->event_lock, flags);
  9297. work = intel_crtc->flip_work;
  9298. if (work != NULL &&
  9299. is_mmio_work(work) &&
  9300. pageflip_finished(intel_crtc, work))
  9301. page_flip_completed(intel_crtc);
  9302. spin_unlock_irqrestore(&dev->event_lock, flags);
  9303. }
  9304. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9305. struct intel_flip_work *work)
  9306. {
  9307. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9308. /* Ensure that the work item is consistent when activating it ... */
  9309. smp_mb__before_atomic();
  9310. atomic_set(&work->pending, 1);
  9311. }
  9312. static int intel_gen2_queue_flip(struct drm_device *dev,
  9313. struct drm_crtc *crtc,
  9314. struct drm_framebuffer *fb,
  9315. struct drm_i915_gem_object *obj,
  9316. struct drm_i915_gem_request *req,
  9317. uint32_t flags)
  9318. {
  9319. struct intel_ring *ring = req->ring;
  9320. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9321. u32 flip_mask;
  9322. int ret;
  9323. ret = intel_ring_begin(req, 6);
  9324. if (ret)
  9325. return ret;
  9326. /* Can't queue multiple flips, so wait for the previous
  9327. * one to finish before executing the next.
  9328. */
  9329. if (intel_crtc->plane)
  9330. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9331. else
  9332. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9333. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9334. intel_ring_emit(ring, MI_NOOP);
  9335. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9336. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9337. intel_ring_emit(ring, fb->pitches[0]);
  9338. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9339. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9340. return 0;
  9341. }
  9342. static int intel_gen3_queue_flip(struct drm_device *dev,
  9343. struct drm_crtc *crtc,
  9344. struct drm_framebuffer *fb,
  9345. struct drm_i915_gem_object *obj,
  9346. struct drm_i915_gem_request *req,
  9347. uint32_t flags)
  9348. {
  9349. struct intel_ring *ring = req->ring;
  9350. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9351. u32 flip_mask;
  9352. int ret;
  9353. ret = intel_ring_begin(req, 6);
  9354. if (ret)
  9355. return ret;
  9356. if (intel_crtc->plane)
  9357. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9358. else
  9359. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9360. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9361. intel_ring_emit(ring, MI_NOOP);
  9362. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9363. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9364. intel_ring_emit(ring, fb->pitches[0]);
  9365. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9366. intel_ring_emit(ring, MI_NOOP);
  9367. return 0;
  9368. }
  9369. static int intel_gen4_queue_flip(struct drm_device *dev,
  9370. struct drm_crtc *crtc,
  9371. struct drm_framebuffer *fb,
  9372. struct drm_i915_gem_object *obj,
  9373. struct drm_i915_gem_request *req,
  9374. uint32_t flags)
  9375. {
  9376. struct intel_ring *ring = req->ring;
  9377. struct drm_i915_private *dev_priv = to_i915(dev);
  9378. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9379. uint32_t pf, pipesrc;
  9380. int ret;
  9381. ret = intel_ring_begin(req, 4);
  9382. if (ret)
  9383. return ret;
  9384. /* i965+ uses the linear or tiled offsets from the
  9385. * Display Registers (which do not change across a page-flip)
  9386. * so we need only reprogram the base address.
  9387. */
  9388. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9389. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9390. intel_ring_emit(ring, fb->pitches[0]);
  9391. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
  9392. i915_gem_object_get_tiling(obj));
  9393. /* XXX Enabling the panel-fitter across page-flip is so far
  9394. * untested on non-native modes, so ignore it for now.
  9395. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9396. */
  9397. pf = 0;
  9398. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9399. intel_ring_emit(ring, pf | pipesrc);
  9400. return 0;
  9401. }
  9402. static int intel_gen6_queue_flip(struct drm_device *dev,
  9403. struct drm_crtc *crtc,
  9404. struct drm_framebuffer *fb,
  9405. struct drm_i915_gem_object *obj,
  9406. struct drm_i915_gem_request *req,
  9407. uint32_t flags)
  9408. {
  9409. struct intel_ring *ring = req->ring;
  9410. struct drm_i915_private *dev_priv = to_i915(dev);
  9411. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9412. uint32_t pf, pipesrc;
  9413. int ret;
  9414. ret = intel_ring_begin(req, 4);
  9415. if (ret)
  9416. return ret;
  9417. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9418. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9419. intel_ring_emit(ring, fb->pitches[0] | i915_gem_object_get_tiling(obj));
  9420. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9421. /* Contrary to the suggestions in the documentation,
  9422. * "Enable Panel Fitter" does not seem to be required when page
  9423. * flipping with a non-native mode, and worse causes a normal
  9424. * modeset to fail.
  9425. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9426. */
  9427. pf = 0;
  9428. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9429. intel_ring_emit(ring, pf | pipesrc);
  9430. return 0;
  9431. }
  9432. static int intel_gen7_queue_flip(struct drm_device *dev,
  9433. struct drm_crtc *crtc,
  9434. struct drm_framebuffer *fb,
  9435. struct drm_i915_gem_object *obj,
  9436. struct drm_i915_gem_request *req,
  9437. uint32_t flags)
  9438. {
  9439. struct intel_ring *ring = req->ring;
  9440. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9441. uint32_t plane_bit = 0;
  9442. int len, ret;
  9443. switch (intel_crtc->plane) {
  9444. case PLANE_A:
  9445. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9446. break;
  9447. case PLANE_B:
  9448. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9449. break;
  9450. case PLANE_C:
  9451. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9452. break;
  9453. default:
  9454. WARN_ONCE(1, "unknown plane in flip command\n");
  9455. return -ENODEV;
  9456. }
  9457. len = 4;
  9458. if (req->engine->id == RCS) {
  9459. len += 6;
  9460. /*
  9461. * On Gen 8, SRM is now taking an extra dword to accommodate
  9462. * 48bits addresses, and we need a NOOP for the batch size to
  9463. * stay even.
  9464. */
  9465. if (IS_GEN8(dev))
  9466. len += 2;
  9467. }
  9468. /*
  9469. * BSpec MI_DISPLAY_FLIP for IVB:
  9470. * "The full packet must be contained within the same cache line."
  9471. *
  9472. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9473. * cacheline, if we ever start emitting more commands before
  9474. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9475. * then do the cacheline alignment, and finally emit the
  9476. * MI_DISPLAY_FLIP.
  9477. */
  9478. ret = intel_ring_cacheline_align(req);
  9479. if (ret)
  9480. return ret;
  9481. ret = intel_ring_begin(req, len);
  9482. if (ret)
  9483. return ret;
  9484. /* Unmask the flip-done completion message. Note that the bspec says that
  9485. * we should do this for both the BCS and RCS, and that we must not unmask
  9486. * more than one flip event at any time (or ensure that one flip message
  9487. * can be sent by waiting for flip-done prior to queueing new flips).
  9488. * Experimentation says that BCS works despite DERRMR masking all
  9489. * flip-done completion events and that unmasking all planes at once
  9490. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9491. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9492. */
  9493. if (req->engine->id == RCS) {
  9494. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9495. intel_ring_emit_reg(ring, DERRMR);
  9496. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9497. DERRMR_PIPEB_PRI_FLIP_DONE |
  9498. DERRMR_PIPEC_PRI_FLIP_DONE));
  9499. if (IS_GEN8(dev))
  9500. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  9501. MI_SRM_LRM_GLOBAL_GTT);
  9502. else
  9503. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  9504. MI_SRM_LRM_GLOBAL_GTT);
  9505. intel_ring_emit_reg(ring, DERRMR);
  9506. intel_ring_emit(ring, req->engine->scratch.gtt_offset + 256);
  9507. if (IS_GEN8(dev)) {
  9508. intel_ring_emit(ring, 0);
  9509. intel_ring_emit(ring, MI_NOOP);
  9510. }
  9511. }
  9512. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9513. intel_ring_emit(ring, fb->pitches[0] | i915_gem_object_get_tiling(obj));
  9514. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9515. intel_ring_emit(ring, (MI_NOOP));
  9516. return 0;
  9517. }
  9518. static bool use_mmio_flip(struct intel_engine_cs *engine,
  9519. struct drm_i915_gem_object *obj)
  9520. {
  9521. struct reservation_object *resv;
  9522. /*
  9523. * This is not being used for older platforms, because
  9524. * non-availability of flip done interrupt forces us to use
  9525. * CS flips. Older platforms derive flip done using some clever
  9526. * tricks involving the flip_pending status bits and vblank irqs.
  9527. * So using MMIO flips there would disrupt this mechanism.
  9528. */
  9529. if (engine == NULL)
  9530. return true;
  9531. if (INTEL_GEN(engine->i915) < 5)
  9532. return false;
  9533. if (i915.use_mmio_flip < 0)
  9534. return false;
  9535. else if (i915.use_mmio_flip > 0)
  9536. return true;
  9537. else if (i915.enable_execlists)
  9538. return true;
  9539. resv = i915_gem_object_get_dmabuf_resv(obj);
  9540. if (resv && !reservation_object_test_signaled_rcu(resv, false))
  9541. return true;
  9542. return engine != i915_gem_active_get_engine(&obj->last_write,
  9543. &obj->base.dev->struct_mutex);
  9544. }
  9545. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9546. unsigned int rotation,
  9547. struct intel_flip_work *work)
  9548. {
  9549. struct drm_device *dev = intel_crtc->base.dev;
  9550. struct drm_i915_private *dev_priv = to_i915(dev);
  9551. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9552. const enum pipe pipe = intel_crtc->pipe;
  9553. u32 ctl, stride, tile_height;
  9554. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9555. ctl &= ~PLANE_CTL_TILED_MASK;
  9556. switch (fb->modifier[0]) {
  9557. case DRM_FORMAT_MOD_NONE:
  9558. break;
  9559. case I915_FORMAT_MOD_X_TILED:
  9560. ctl |= PLANE_CTL_TILED_X;
  9561. break;
  9562. case I915_FORMAT_MOD_Y_TILED:
  9563. ctl |= PLANE_CTL_TILED_Y;
  9564. break;
  9565. case I915_FORMAT_MOD_Yf_TILED:
  9566. ctl |= PLANE_CTL_TILED_YF;
  9567. break;
  9568. default:
  9569. MISSING_CASE(fb->modifier[0]);
  9570. }
  9571. /*
  9572. * The stride is either expressed as a multiple of 64 bytes chunks for
  9573. * linear buffers or in number of tiles for tiled buffers.
  9574. */
  9575. if (intel_rotation_90_or_270(rotation)) {
  9576. /* stride = Surface height in tiles */
  9577. tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
  9578. stride = DIV_ROUND_UP(fb->height, tile_height);
  9579. } else {
  9580. stride = fb->pitches[0] /
  9581. intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  9582. fb->pixel_format);
  9583. }
  9584. /*
  9585. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9586. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9587. */
  9588. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9589. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9590. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9591. POSTING_READ(PLANE_SURF(pipe, 0));
  9592. }
  9593. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9594. struct intel_flip_work *work)
  9595. {
  9596. struct drm_device *dev = intel_crtc->base.dev;
  9597. struct drm_i915_private *dev_priv = to_i915(dev);
  9598. struct intel_framebuffer *intel_fb =
  9599. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9600. struct drm_i915_gem_object *obj = intel_fb->obj;
  9601. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  9602. u32 dspcntr;
  9603. dspcntr = I915_READ(reg);
  9604. if (i915_gem_object_is_tiled(obj))
  9605. dspcntr |= DISPPLANE_TILED;
  9606. else
  9607. dspcntr &= ~DISPPLANE_TILED;
  9608. I915_WRITE(reg, dspcntr);
  9609. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9610. POSTING_READ(DSPSURF(intel_crtc->plane));
  9611. }
  9612. static void intel_mmio_flip_work_func(struct work_struct *w)
  9613. {
  9614. struct intel_flip_work *work =
  9615. container_of(w, struct intel_flip_work, mmio_work);
  9616. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9617. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9618. struct intel_framebuffer *intel_fb =
  9619. to_intel_framebuffer(crtc->base.primary->fb);
  9620. struct drm_i915_gem_object *obj = intel_fb->obj;
  9621. struct reservation_object *resv;
  9622. if (work->flip_queued_req)
  9623. WARN_ON(i915_wait_request(work->flip_queued_req,
  9624. false, NULL,
  9625. NO_WAITBOOST));
  9626. /* For framebuffer backed by dmabuf, wait for fence */
  9627. resv = i915_gem_object_get_dmabuf_resv(obj);
  9628. if (resv)
  9629. WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
  9630. MAX_SCHEDULE_TIMEOUT) < 0);
  9631. intel_pipe_update_start(crtc);
  9632. if (INTEL_GEN(dev_priv) >= 9)
  9633. skl_do_mmio_flip(crtc, work->rotation, work);
  9634. else
  9635. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9636. ilk_do_mmio_flip(crtc, work);
  9637. intel_pipe_update_end(crtc, work);
  9638. }
  9639. static int intel_default_queue_flip(struct drm_device *dev,
  9640. struct drm_crtc *crtc,
  9641. struct drm_framebuffer *fb,
  9642. struct drm_i915_gem_object *obj,
  9643. struct drm_i915_gem_request *req,
  9644. uint32_t flags)
  9645. {
  9646. return -ENODEV;
  9647. }
  9648. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  9649. struct intel_crtc *intel_crtc,
  9650. struct intel_flip_work *work)
  9651. {
  9652. u32 addr, vblank;
  9653. if (!atomic_read(&work->pending))
  9654. return false;
  9655. smp_rmb();
  9656. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  9657. if (work->flip_ready_vblank == 0) {
  9658. if (work->flip_queued_req &&
  9659. !i915_gem_request_completed(work->flip_queued_req))
  9660. return false;
  9661. work->flip_ready_vblank = vblank;
  9662. }
  9663. if (vblank - work->flip_ready_vblank < 3)
  9664. return false;
  9665. /* Potential stall - if we see that the flip has happened,
  9666. * assume a missed interrupt. */
  9667. if (INTEL_GEN(dev_priv) >= 4)
  9668. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9669. else
  9670. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9671. /* There is a potential issue here with a false positive after a flip
  9672. * to the same address. We could address this by checking for a
  9673. * non-incrementing frame counter.
  9674. */
  9675. return addr == work->gtt_offset;
  9676. }
  9677. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  9678. {
  9679. struct drm_device *dev = &dev_priv->drm;
  9680. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9681. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9682. struct intel_flip_work *work;
  9683. WARN_ON(!in_interrupt());
  9684. if (crtc == NULL)
  9685. return;
  9686. spin_lock(&dev->event_lock);
  9687. work = intel_crtc->flip_work;
  9688. if (work != NULL && !is_mmio_work(work) &&
  9689. __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
  9690. WARN_ONCE(1,
  9691. "Kicking stuck page flip: queued at %d, now %d\n",
  9692. work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
  9693. page_flip_completed(intel_crtc);
  9694. work = NULL;
  9695. }
  9696. if (work != NULL && !is_mmio_work(work) &&
  9697. intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
  9698. intel_queue_rps_boost_for_request(work->flip_queued_req);
  9699. spin_unlock(&dev->event_lock);
  9700. }
  9701. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9702. struct drm_framebuffer *fb,
  9703. struct drm_pending_vblank_event *event,
  9704. uint32_t page_flip_flags)
  9705. {
  9706. struct drm_device *dev = crtc->dev;
  9707. struct drm_i915_private *dev_priv = to_i915(dev);
  9708. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9709. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9710. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9711. struct drm_plane *primary = crtc->primary;
  9712. enum pipe pipe = intel_crtc->pipe;
  9713. struct intel_flip_work *work;
  9714. struct intel_engine_cs *engine;
  9715. bool mmio_flip;
  9716. struct drm_i915_gem_request *request;
  9717. int ret;
  9718. /*
  9719. * drm_mode_page_flip_ioctl() should already catch this, but double
  9720. * check to be safe. In the future we may enable pageflipping from
  9721. * a disabled primary plane.
  9722. */
  9723. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9724. return -EBUSY;
  9725. /* Can't change pixel format via MI display flips. */
  9726. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9727. return -EINVAL;
  9728. /*
  9729. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9730. * Note that pitch changes could also affect these register.
  9731. */
  9732. if (INTEL_INFO(dev)->gen > 3 &&
  9733. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9734. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9735. return -EINVAL;
  9736. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9737. goto out_hang;
  9738. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9739. if (work == NULL)
  9740. return -ENOMEM;
  9741. work->event = event;
  9742. work->crtc = crtc;
  9743. work->old_fb = old_fb;
  9744. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  9745. ret = drm_crtc_vblank_get(crtc);
  9746. if (ret)
  9747. goto free_work;
  9748. /* We borrow the event spin lock for protecting flip_work */
  9749. spin_lock_irq(&dev->event_lock);
  9750. if (intel_crtc->flip_work) {
  9751. /* Before declaring the flip queue wedged, check if
  9752. * the hardware completed the operation behind our backs.
  9753. */
  9754. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  9755. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9756. page_flip_completed(intel_crtc);
  9757. } else {
  9758. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9759. spin_unlock_irq(&dev->event_lock);
  9760. drm_crtc_vblank_put(crtc);
  9761. kfree(work);
  9762. return -EBUSY;
  9763. }
  9764. }
  9765. intel_crtc->flip_work = work;
  9766. spin_unlock_irq(&dev->event_lock);
  9767. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9768. flush_workqueue(dev_priv->wq);
  9769. /* Reference the objects for the scheduled work. */
  9770. drm_framebuffer_reference(work->old_fb);
  9771. crtc->primary->fb = fb;
  9772. update_state_fb(crtc->primary);
  9773. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  9774. to_intel_plane_state(primary->state));
  9775. work->pending_flip_obj = i915_gem_object_get(obj);
  9776. ret = i915_mutex_lock_interruptible(dev);
  9777. if (ret)
  9778. goto cleanup;
  9779. intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9780. if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
  9781. ret = -EIO;
  9782. goto cleanup;
  9783. }
  9784. atomic_inc(&intel_crtc->unpin_work_count);
  9785. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9786. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9787. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  9788. engine = &dev_priv->engine[BCS];
  9789. if (i915_gem_object_get_tiling(obj) !=
  9790. i915_gem_object_get_tiling(intel_fb_obj(work->old_fb)))
  9791. /* vlv: DISPLAY_FLIP fails to change tiling */
  9792. engine = NULL;
  9793. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9794. engine = &dev_priv->engine[BCS];
  9795. } else if (INTEL_INFO(dev)->gen >= 7) {
  9796. engine = i915_gem_active_get_engine(&obj->last_write,
  9797. &obj->base.dev->struct_mutex);
  9798. if (engine == NULL || engine->id != RCS)
  9799. engine = &dev_priv->engine[BCS];
  9800. } else {
  9801. engine = &dev_priv->engine[RCS];
  9802. }
  9803. mmio_flip = use_mmio_flip(engine, obj);
  9804. ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  9805. if (ret)
  9806. goto cleanup_pending;
  9807. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9808. obj, 0);
  9809. work->gtt_offset += intel_crtc->dspaddr_offset;
  9810. work->rotation = crtc->primary->state->rotation;
  9811. if (mmio_flip) {
  9812. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  9813. work->flip_queued_req = i915_gem_active_get(&obj->last_write,
  9814. &obj->base.dev->struct_mutex);
  9815. schedule_work(&work->mmio_work);
  9816. } else {
  9817. request = i915_gem_request_alloc(engine, engine->last_context);
  9818. if (IS_ERR(request)) {
  9819. ret = PTR_ERR(request);
  9820. goto cleanup_unpin;
  9821. }
  9822. ret = i915_gem_object_sync(obj, request);
  9823. if (ret)
  9824. goto cleanup_request;
  9825. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9826. page_flip_flags);
  9827. if (ret)
  9828. goto cleanup_request;
  9829. intel_mark_page_flip_active(intel_crtc, work);
  9830. work->flip_queued_req = i915_gem_request_get(request);
  9831. i915_add_request_no_flush(request);
  9832. }
  9833. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  9834. to_intel_plane(primary)->frontbuffer_bit);
  9835. mutex_unlock(&dev->struct_mutex);
  9836. intel_frontbuffer_flip_prepare(to_i915(dev),
  9837. to_intel_plane(primary)->frontbuffer_bit);
  9838. trace_i915_flip_request(intel_crtc->plane, obj);
  9839. return 0;
  9840. cleanup_request:
  9841. i915_add_request_no_flush(request);
  9842. cleanup_unpin:
  9843. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  9844. cleanup_pending:
  9845. atomic_dec(&intel_crtc->unpin_work_count);
  9846. mutex_unlock(&dev->struct_mutex);
  9847. cleanup:
  9848. crtc->primary->fb = old_fb;
  9849. update_state_fb(crtc->primary);
  9850. i915_gem_object_put_unlocked(obj);
  9851. drm_framebuffer_unreference(work->old_fb);
  9852. spin_lock_irq(&dev->event_lock);
  9853. intel_crtc->flip_work = NULL;
  9854. spin_unlock_irq(&dev->event_lock);
  9855. drm_crtc_vblank_put(crtc);
  9856. free_work:
  9857. kfree(work);
  9858. if (ret == -EIO) {
  9859. struct drm_atomic_state *state;
  9860. struct drm_plane_state *plane_state;
  9861. out_hang:
  9862. state = drm_atomic_state_alloc(dev);
  9863. if (!state)
  9864. return -ENOMEM;
  9865. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9866. retry:
  9867. plane_state = drm_atomic_get_plane_state(state, primary);
  9868. ret = PTR_ERR_OR_ZERO(plane_state);
  9869. if (!ret) {
  9870. drm_atomic_set_fb_for_plane(plane_state, fb);
  9871. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9872. if (!ret)
  9873. ret = drm_atomic_commit(state);
  9874. }
  9875. if (ret == -EDEADLK) {
  9876. drm_modeset_backoff(state->acquire_ctx);
  9877. drm_atomic_state_clear(state);
  9878. goto retry;
  9879. }
  9880. if (ret)
  9881. drm_atomic_state_free(state);
  9882. if (ret == 0 && event) {
  9883. spin_lock_irq(&dev->event_lock);
  9884. drm_crtc_send_vblank_event(crtc, event);
  9885. spin_unlock_irq(&dev->event_lock);
  9886. }
  9887. }
  9888. return ret;
  9889. }
  9890. /**
  9891. * intel_wm_need_update - Check whether watermarks need updating
  9892. * @plane: drm plane
  9893. * @state: new plane state
  9894. *
  9895. * Check current plane state versus the new one to determine whether
  9896. * watermarks need to be recalculated.
  9897. *
  9898. * Returns true or false.
  9899. */
  9900. static bool intel_wm_need_update(struct drm_plane *plane,
  9901. struct drm_plane_state *state)
  9902. {
  9903. struct intel_plane_state *new = to_intel_plane_state(state);
  9904. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9905. /* Update watermarks on tiling or size changes. */
  9906. if (new->visible != cur->visible)
  9907. return true;
  9908. if (!cur->base.fb || !new->base.fb)
  9909. return false;
  9910. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  9911. cur->base.rotation != new->base.rotation ||
  9912. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9913. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9914. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9915. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9916. return true;
  9917. return false;
  9918. }
  9919. static bool needs_scaling(struct intel_plane_state *state)
  9920. {
  9921. int src_w = drm_rect_width(&state->src) >> 16;
  9922. int src_h = drm_rect_height(&state->src) >> 16;
  9923. int dst_w = drm_rect_width(&state->dst);
  9924. int dst_h = drm_rect_height(&state->dst);
  9925. return (src_w != dst_w || src_h != dst_h);
  9926. }
  9927. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9928. struct drm_plane_state *plane_state)
  9929. {
  9930. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9931. struct drm_crtc *crtc = crtc_state->crtc;
  9932. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9933. struct drm_plane *plane = plane_state->plane;
  9934. struct drm_device *dev = crtc->dev;
  9935. struct drm_i915_private *dev_priv = to_i915(dev);
  9936. struct intel_plane_state *old_plane_state =
  9937. to_intel_plane_state(plane->state);
  9938. bool mode_changed = needs_modeset(crtc_state);
  9939. bool was_crtc_enabled = crtc->state->active;
  9940. bool is_crtc_enabled = crtc_state->active;
  9941. bool turn_off, turn_on, visible, was_visible;
  9942. struct drm_framebuffer *fb = plane_state->fb;
  9943. int ret;
  9944. if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  9945. ret = skl_update_scaler_plane(
  9946. to_intel_crtc_state(crtc_state),
  9947. to_intel_plane_state(plane_state));
  9948. if (ret)
  9949. return ret;
  9950. }
  9951. was_visible = old_plane_state->visible;
  9952. visible = to_intel_plane_state(plane_state)->visible;
  9953. if (!was_crtc_enabled && WARN_ON(was_visible))
  9954. was_visible = false;
  9955. /*
  9956. * Visibility is calculated as if the crtc was on, but
  9957. * after scaler setup everything depends on it being off
  9958. * when the crtc isn't active.
  9959. *
  9960. * FIXME this is wrong for watermarks. Watermarks should also
  9961. * be computed as if the pipe would be active. Perhaps move
  9962. * per-plane wm computation to the .check_plane() hook, and
  9963. * only combine the results from all planes in the current place?
  9964. */
  9965. if (!is_crtc_enabled)
  9966. to_intel_plane_state(plane_state)->visible = visible = false;
  9967. if (!was_visible && !visible)
  9968. return 0;
  9969. if (fb != old_plane_state->base.fb)
  9970. pipe_config->fb_changed = true;
  9971. turn_off = was_visible && (!visible || mode_changed);
  9972. turn_on = visible && (!was_visible || mode_changed);
  9973. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  9974. intel_crtc->base.base.id,
  9975. intel_crtc->base.name,
  9976. plane->base.id, plane->name,
  9977. fb ? fb->base.id : -1);
  9978. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  9979. plane->base.id, plane->name,
  9980. was_visible, visible,
  9981. turn_off, turn_on, mode_changed);
  9982. if (turn_on) {
  9983. pipe_config->update_wm_pre = true;
  9984. /* must disable cxsr around plane enable/disable */
  9985. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9986. pipe_config->disable_cxsr = true;
  9987. } else if (turn_off) {
  9988. pipe_config->update_wm_post = true;
  9989. /* must disable cxsr around plane enable/disable */
  9990. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9991. pipe_config->disable_cxsr = true;
  9992. } else if (intel_wm_need_update(plane, plane_state)) {
  9993. /* FIXME bollocks */
  9994. pipe_config->update_wm_pre = true;
  9995. pipe_config->update_wm_post = true;
  9996. }
  9997. /* Pre-gen9 platforms need two-step watermark updates */
  9998. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  9999. INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
  10000. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10001. if (visible || was_visible)
  10002. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10003. /*
  10004. * WaCxSRDisabledForSpriteScaling:ivb
  10005. *
  10006. * cstate->update_wm was already set above, so this flag will
  10007. * take effect when we commit and program watermarks.
  10008. */
  10009. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
  10010. needs_scaling(to_intel_plane_state(plane_state)) &&
  10011. !needs_scaling(old_plane_state))
  10012. pipe_config->disable_lp_wm = true;
  10013. return 0;
  10014. }
  10015. static bool encoders_cloneable(const struct intel_encoder *a,
  10016. const struct intel_encoder *b)
  10017. {
  10018. /* masks could be asymmetric, so check both ways */
  10019. return a == b || (a->cloneable & (1 << b->type) &&
  10020. b->cloneable & (1 << a->type));
  10021. }
  10022. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10023. struct intel_crtc *crtc,
  10024. struct intel_encoder *encoder)
  10025. {
  10026. struct intel_encoder *source_encoder;
  10027. struct drm_connector *connector;
  10028. struct drm_connector_state *connector_state;
  10029. int i;
  10030. for_each_connector_in_state(state, connector, connector_state, i) {
  10031. if (connector_state->crtc != &crtc->base)
  10032. continue;
  10033. source_encoder =
  10034. to_intel_encoder(connector_state->best_encoder);
  10035. if (!encoders_cloneable(encoder, source_encoder))
  10036. return false;
  10037. }
  10038. return true;
  10039. }
  10040. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10041. struct drm_crtc_state *crtc_state)
  10042. {
  10043. struct drm_device *dev = crtc->dev;
  10044. struct drm_i915_private *dev_priv = to_i915(dev);
  10045. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10046. struct intel_crtc_state *pipe_config =
  10047. to_intel_crtc_state(crtc_state);
  10048. struct drm_atomic_state *state = crtc_state->state;
  10049. int ret;
  10050. bool mode_changed = needs_modeset(crtc_state);
  10051. if (mode_changed && !crtc_state->active)
  10052. pipe_config->update_wm_post = true;
  10053. if (mode_changed && crtc_state->enable &&
  10054. dev_priv->display.crtc_compute_clock &&
  10055. !WARN_ON(pipe_config->shared_dpll)) {
  10056. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10057. pipe_config);
  10058. if (ret)
  10059. return ret;
  10060. }
  10061. if (crtc_state->color_mgmt_changed) {
  10062. ret = intel_color_check(crtc, crtc_state);
  10063. if (ret)
  10064. return ret;
  10065. /*
  10066. * Changing color management on Intel hardware is
  10067. * handled as part of planes update.
  10068. */
  10069. crtc_state->planes_changed = true;
  10070. }
  10071. ret = 0;
  10072. if (dev_priv->display.compute_pipe_wm) {
  10073. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10074. if (ret) {
  10075. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10076. return ret;
  10077. }
  10078. }
  10079. if (dev_priv->display.compute_intermediate_wm &&
  10080. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10081. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10082. return 0;
  10083. /*
  10084. * Calculate 'intermediate' watermarks that satisfy both the
  10085. * old state and the new state. We can program these
  10086. * immediately.
  10087. */
  10088. ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
  10089. intel_crtc,
  10090. pipe_config);
  10091. if (ret) {
  10092. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10093. return ret;
  10094. }
  10095. } else if (dev_priv->display.compute_intermediate_wm) {
  10096. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10097. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10098. }
  10099. if (INTEL_INFO(dev)->gen >= 9) {
  10100. if (mode_changed)
  10101. ret = skl_update_scaler_crtc(pipe_config);
  10102. if (!ret)
  10103. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10104. pipe_config);
  10105. }
  10106. return ret;
  10107. }
  10108. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10109. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10110. .atomic_begin = intel_begin_crtc_commit,
  10111. .atomic_flush = intel_finish_crtc_commit,
  10112. .atomic_check = intel_crtc_atomic_check,
  10113. };
  10114. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10115. {
  10116. struct intel_connector *connector;
  10117. for_each_intel_connector(dev, connector) {
  10118. if (connector->base.state->crtc)
  10119. drm_connector_unreference(&connector->base);
  10120. if (connector->base.encoder) {
  10121. connector->base.state->best_encoder =
  10122. connector->base.encoder;
  10123. connector->base.state->crtc =
  10124. connector->base.encoder->crtc;
  10125. drm_connector_reference(&connector->base);
  10126. } else {
  10127. connector->base.state->best_encoder = NULL;
  10128. connector->base.state->crtc = NULL;
  10129. }
  10130. }
  10131. }
  10132. static void
  10133. connected_sink_compute_bpp(struct intel_connector *connector,
  10134. struct intel_crtc_state *pipe_config)
  10135. {
  10136. int bpp = pipe_config->pipe_bpp;
  10137. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10138. connector->base.base.id,
  10139. connector->base.name);
  10140. /* Don't use an invalid EDID bpc value */
  10141. if (connector->base.display_info.bpc &&
  10142. connector->base.display_info.bpc * 3 < bpp) {
  10143. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10144. bpp, connector->base.display_info.bpc*3);
  10145. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10146. }
  10147. /* Clamp bpp to default limit on screens without EDID 1.4 */
  10148. if (connector->base.display_info.bpc == 0) {
  10149. int type = connector->base.connector_type;
  10150. int clamp_bpp = 24;
  10151. /* Fall back to 18 bpp when DP sink capability is unknown. */
  10152. if (type == DRM_MODE_CONNECTOR_DisplayPort ||
  10153. type == DRM_MODE_CONNECTOR_eDP)
  10154. clamp_bpp = 18;
  10155. if (bpp > clamp_bpp) {
  10156. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
  10157. bpp, clamp_bpp);
  10158. pipe_config->pipe_bpp = clamp_bpp;
  10159. }
  10160. }
  10161. }
  10162. static int
  10163. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10164. struct intel_crtc_state *pipe_config)
  10165. {
  10166. struct drm_device *dev = crtc->base.dev;
  10167. struct drm_atomic_state *state;
  10168. struct drm_connector *connector;
  10169. struct drm_connector_state *connector_state;
  10170. int bpp, i;
  10171. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  10172. bpp = 10*3;
  10173. else if (INTEL_INFO(dev)->gen >= 5)
  10174. bpp = 12*3;
  10175. else
  10176. bpp = 8*3;
  10177. pipe_config->pipe_bpp = bpp;
  10178. state = pipe_config->base.state;
  10179. /* Clamp display bpp to EDID value */
  10180. for_each_connector_in_state(state, connector, connector_state, i) {
  10181. if (connector_state->crtc != &crtc->base)
  10182. continue;
  10183. connected_sink_compute_bpp(to_intel_connector(connector),
  10184. pipe_config);
  10185. }
  10186. return bpp;
  10187. }
  10188. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10189. {
  10190. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10191. "type: 0x%x flags: 0x%x\n",
  10192. mode->crtc_clock,
  10193. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10194. mode->crtc_hsync_end, mode->crtc_htotal,
  10195. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10196. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10197. }
  10198. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10199. struct intel_crtc_state *pipe_config,
  10200. const char *context)
  10201. {
  10202. struct drm_device *dev = crtc->base.dev;
  10203. struct drm_plane *plane;
  10204. struct intel_plane *intel_plane;
  10205. struct intel_plane_state *state;
  10206. struct drm_framebuffer *fb;
  10207. DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
  10208. crtc->base.base.id, crtc->base.name,
  10209. context, pipe_config, pipe_name(crtc->pipe));
  10210. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  10211. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10212. pipe_config->pipe_bpp, pipe_config->dither);
  10213. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10214. pipe_config->has_pch_encoder,
  10215. pipe_config->fdi_lanes,
  10216. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10217. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10218. pipe_config->fdi_m_n.tu);
  10219. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10220. intel_crtc_has_dp_encoder(pipe_config),
  10221. pipe_config->lane_count,
  10222. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10223. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10224. pipe_config->dp_m_n.tu);
  10225. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10226. intel_crtc_has_dp_encoder(pipe_config),
  10227. pipe_config->lane_count,
  10228. pipe_config->dp_m2_n2.gmch_m,
  10229. pipe_config->dp_m2_n2.gmch_n,
  10230. pipe_config->dp_m2_n2.link_m,
  10231. pipe_config->dp_m2_n2.link_n,
  10232. pipe_config->dp_m2_n2.tu);
  10233. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10234. pipe_config->has_audio,
  10235. pipe_config->has_infoframe);
  10236. DRM_DEBUG_KMS("requested mode:\n");
  10237. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10238. DRM_DEBUG_KMS("adjusted mode:\n");
  10239. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10240. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10241. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10242. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10243. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10244. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10245. crtc->num_scalers,
  10246. pipe_config->scaler_state.scaler_users,
  10247. pipe_config->scaler_state.scaler_id);
  10248. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10249. pipe_config->gmch_pfit.control,
  10250. pipe_config->gmch_pfit.pgm_ratios,
  10251. pipe_config->gmch_pfit.lvds_border_bits);
  10252. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10253. pipe_config->pch_pfit.pos,
  10254. pipe_config->pch_pfit.size,
  10255. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10256. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10257. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10258. if (IS_BROXTON(dev)) {
  10259. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10260. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10261. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10262. pipe_config->ddi_pll_sel,
  10263. pipe_config->dpll_hw_state.ebb0,
  10264. pipe_config->dpll_hw_state.ebb4,
  10265. pipe_config->dpll_hw_state.pll0,
  10266. pipe_config->dpll_hw_state.pll1,
  10267. pipe_config->dpll_hw_state.pll2,
  10268. pipe_config->dpll_hw_state.pll3,
  10269. pipe_config->dpll_hw_state.pll6,
  10270. pipe_config->dpll_hw_state.pll8,
  10271. pipe_config->dpll_hw_state.pll9,
  10272. pipe_config->dpll_hw_state.pll10,
  10273. pipe_config->dpll_hw_state.pcsdw12);
  10274. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10275. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10276. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10277. pipe_config->ddi_pll_sel,
  10278. pipe_config->dpll_hw_state.ctrl1,
  10279. pipe_config->dpll_hw_state.cfgcr1,
  10280. pipe_config->dpll_hw_state.cfgcr2);
  10281. } else if (HAS_DDI(dev)) {
  10282. DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10283. pipe_config->ddi_pll_sel,
  10284. pipe_config->dpll_hw_state.wrpll,
  10285. pipe_config->dpll_hw_state.spll);
  10286. } else {
  10287. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10288. "fp0: 0x%x, fp1: 0x%x\n",
  10289. pipe_config->dpll_hw_state.dpll,
  10290. pipe_config->dpll_hw_state.dpll_md,
  10291. pipe_config->dpll_hw_state.fp0,
  10292. pipe_config->dpll_hw_state.fp1);
  10293. }
  10294. DRM_DEBUG_KMS("planes on this crtc\n");
  10295. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10296. intel_plane = to_intel_plane(plane);
  10297. if (intel_plane->pipe != crtc->pipe)
  10298. continue;
  10299. state = to_intel_plane_state(plane->state);
  10300. fb = state->base.fb;
  10301. if (!fb) {
  10302. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10303. plane->base.id, plane->name, state->scaler_id);
  10304. continue;
  10305. }
  10306. DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
  10307. plane->base.id, plane->name);
  10308. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
  10309. fb->base.id, fb->width, fb->height,
  10310. drm_get_format_name(fb->pixel_format));
  10311. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10312. state->scaler_id,
  10313. state->src.x1 >> 16, state->src.y1 >> 16,
  10314. drm_rect_width(&state->src) >> 16,
  10315. drm_rect_height(&state->src) >> 16,
  10316. state->dst.x1, state->dst.y1,
  10317. drm_rect_width(&state->dst),
  10318. drm_rect_height(&state->dst));
  10319. }
  10320. }
  10321. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10322. {
  10323. struct drm_device *dev = state->dev;
  10324. struct drm_connector *connector;
  10325. unsigned int used_ports = 0;
  10326. unsigned int used_mst_ports = 0;
  10327. /*
  10328. * Walk the connector list instead of the encoder
  10329. * list to detect the problem on ddi platforms
  10330. * where there's just one encoder per digital port.
  10331. */
  10332. drm_for_each_connector(connector, dev) {
  10333. struct drm_connector_state *connector_state;
  10334. struct intel_encoder *encoder;
  10335. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10336. if (!connector_state)
  10337. connector_state = connector->state;
  10338. if (!connector_state->best_encoder)
  10339. continue;
  10340. encoder = to_intel_encoder(connector_state->best_encoder);
  10341. WARN_ON(!connector_state->crtc);
  10342. switch (encoder->type) {
  10343. unsigned int port_mask;
  10344. case INTEL_OUTPUT_UNKNOWN:
  10345. if (WARN_ON(!HAS_DDI(dev)))
  10346. break;
  10347. case INTEL_OUTPUT_DP:
  10348. case INTEL_OUTPUT_HDMI:
  10349. case INTEL_OUTPUT_EDP:
  10350. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10351. /* the same port mustn't appear more than once */
  10352. if (used_ports & port_mask)
  10353. return false;
  10354. used_ports |= port_mask;
  10355. break;
  10356. case INTEL_OUTPUT_DP_MST:
  10357. used_mst_ports |=
  10358. 1 << enc_to_mst(&encoder->base)->primary->port;
  10359. break;
  10360. default:
  10361. break;
  10362. }
  10363. }
  10364. /* can't mix MST and SST/HDMI on the same port */
  10365. if (used_ports & used_mst_ports)
  10366. return false;
  10367. return true;
  10368. }
  10369. static void
  10370. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10371. {
  10372. struct drm_crtc_state tmp_state;
  10373. struct intel_crtc_scaler_state scaler_state;
  10374. struct intel_dpll_hw_state dpll_hw_state;
  10375. struct intel_shared_dpll *shared_dpll;
  10376. uint32_t ddi_pll_sel;
  10377. bool force_thru;
  10378. /* FIXME: before the switch to atomic started, a new pipe_config was
  10379. * kzalloc'd. Code that depends on any field being zero should be
  10380. * fixed, so that the crtc_state can be safely duplicated. For now,
  10381. * only fields that are know to not cause problems are preserved. */
  10382. tmp_state = crtc_state->base;
  10383. scaler_state = crtc_state->scaler_state;
  10384. shared_dpll = crtc_state->shared_dpll;
  10385. dpll_hw_state = crtc_state->dpll_hw_state;
  10386. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10387. force_thru = crtc_state->pch_pfit.force_thru;
  10388. memset(crtc_state, 0, sizeof *crtc_state);
  10389. crtc_state->base = tmp_state;
  10390. crtc_state->scaler_state = scaler_state;
  10391. crtc_state->shared_dpll = shared_dpll;
  10392. crtc_state->dpll_hw_state = dpll_hw_state;
  10393. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10394. crtc_state->pch_pfit.force_thru = force_thru;
  10395. }
  10396. static int
  10397. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10398. struct intel_crtc_state *pipe_config)
  10399. {
  10400. struct drm_atomic_state *state = pipe_config->base.state;
  10401. struct intel_encoder *encoder;
  10402. struct drm_connector *connector;
  10403. struct drm_connector_state *connector_state;
  10404. int base_bpp, ret = -EINVAL;
  10405. int i;
  10406. bool retry = true;
  10407. clear_intel_crtc_state(pipe_config);
  10408. pipe_config->cpu_transcoder =
  10409. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10410. /*
  10411. * Sanitize sync polarity flags based on requested ones. If neither
  10412. * positive or negative polarity is requested, treat this as meaning
  10413. * negative polarity.
  10414. */
  10415. if (!(pipe_config->base.adjusted_mode.flags &
  10416. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10417. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10418. if (!(pipe_config->base.adjusted_mode.flags &
  10419. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10420. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10421. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10422. pipe_config);
  10423. if (base_bpp < 0)
  10424. goto fail;
  10425. /*
  10426. * Determine the real pipe dimensions. Note that stereo modes can
  10427. * increase the actual pipe size due to the frame doubling and
  10428. * insertion of additional space for blanks between the frame. This
  10429. * is stored in the crtc timings. We use the requested mode to do this
  10430. * computation to clearly distinguish it from the adjusted mode, which
  10431. * can be changed by the connectors in the below retry loop.
  10432. */
  10433. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10434. &pipe_config->pipe_src_w,
  10435. &pipe_config->pipe_src_h);
  10436. for_each_connector_in_state(state, connector, connector_state, i) {
  10437. if (connector_state->crtc != crtc)
  10438. continue;
  10439. encoder = to_intel_encoder(connector_state->best_encoder);
  10440. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  10441. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10442. goto fail;
  10443. }
  10444. /*
  10445. * Determine output_types before calling the .compute_config()
  10446. * hooks so that the hooks can use this information safely.
  10447. */
  10448. pipe_config->output_types |= 1 << encoder->type;
  10449. }
  10450. encoder_retry:
  10451. /* Ensure the port clock defaults are reset when retrying. */
  10452. pipe_config->port_clock = 0;
  10453. pipe_config->pixel_multiplier = 1;
  10454. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10455. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10456. CRTC_STEREO_DOUBLE);
  10457. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10458. * adjust it according to limitations or connector properties, and also
  10459. * a chance to reject the mode entirely.
  10460. */
  10461. for_each_connector_in_state(state, connector, connector_state, i) {
  10462. if (connector_state->crtc != crtc)
  10463. continue;
  10464. encoder = to_intel_encoder(connector_state->best_encoder);
  10465. if (!(encoder->compute_config(encoder, pipe_config))) {
  10466. DRM_DEBUG_KMS("Encoder config failure\n");
  10467. goto fail;
  10468. }
  10469. }
  10470. /* Set default port clock if not overwritten by the encoder. Needs to be
  10471. * done afterwards in case the encoder adjusts the mode. */
  10472. if (!pipe_config->port_clock)
  10473. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10474. * pipe_config->pixel_multiplier;
  10475. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10476. if (ret < 0) {
  10477. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10478. goto fail;
  10479. }
  10480. if (ret == RETRY) {
  10481. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10482. ret = -EINVAL;
  10483. goto fail;
  10484. }
  10485. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10486. retry = false;
  10487. goto encoder_retry;
  10488. }
  10489. /* Dithering seems to not pass-through bits correctly when it should, so
  10490. * only enable it on 6bpc panels. */
  10491. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10492. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10493. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10494. fail:
  10495. return ret;
  10496. }
  10497. static void
  10498. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10499. {
  10500. struct drm_crtc *crtc;
  10501. struct drm_crtc_state *crtc_state;
  10502. int i;
  10503. /* Double check state. */
  10504. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10505. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10506. /* Update hwmode for vblank functions */
  10507. if (crtc->state->active)
  10508. crtc->hwmode = crtc->state->adjusted_mode;
  10509. else
  10510. crtc->hwmode.crtc_clock = 0;
  10511. /*
  10512. * Update legacy state to satisfy fbc code. This can
  10513. * be removed when fbc uses the atomic state.
  10514. */
  10515. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10516. struct drm_plane_state *plane_state = crtc->primary->state;
  10517. crtc->primary->fb = plane_state->fb;
  10518. crtc->x = plane_state->src_x >> 16;
  10519. crtc->y = plane_state->src_y >> 16;
  10520. }
  10521. }
  10522. }
  10523. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10524. {
  10525. int diff;
  10526. if (clock1 == clock2)
  10527. return true;
  10528. if (!clock1 || !clock2)
  10529. return false;
  10530. diff = abs(clock1 - clock2);
  10531. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10532. return true;
  10533. return false;
  10534. }
  10535. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10536. list_for_each_entry((intel_crtc), \
  10537. &(dev)->mode_config.crtc_list, \
  10538. base.head) \
  10539. for_each_if (mask & (1 <<(intel_crtc)->pipe))
  10540. static bool
  10541. intel_compare_m_n(unsigned int m, unsigned int n,
  10542. unsigned int m2, unsigned int n2,
  10543. bool exact)
  10544. {
  10545. if (m == m2 && n == n2)
  10546. return true;
  10547. if (exact || !m || !n || !m2 || !n2)
  10548. return false;
  10549. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10550. if (n > n2) {
  10551. while (n > n2) {
  10552. m2 <<= 1;
  10553. n2 <<= 1;
  10554. }
  10555. } else if (n < n2) {
  10556. while (n < n2) {
  10557. m <<= 1;
  10558. n <<= 1;
  10559. }
  10560. }
  10561. if (n != n2)
  10562. return false;
  10563. return intel_fuzzy_clock_check(m, m2);
  10564. }
  10565. static bool
  10566. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10567. struct intel_link_m_n *m2_n2,
  10568. bool adjust)
  10569. {
  10570. if (m_n->tu == m2_n2->tu &&
  10571. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10572. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10573. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10574. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10575. if (adjust)
  10576. *m2_n2 = *m_n;
  10577. return true;
  10578. }
  10579. return false;
  10580. }
  10581. static bool
  10582. intel_pipe_config_compare(struct drm_device *dev,
  10583. struct intel_crtc_state *current_config,
  10584. struct intel_crtc_state *pipe_config,
  10585. bool adjust)
  10586. {
  10587. bool ret = true;
  10588. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10589. do { \
  10590. if (!adjust) \
  10591. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10592. else \
  10593. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10594. } while (0)
  10595. #define PIPE_CONF_CHECK_X(name) \
  10596. if (current_config->name != pipe_config->name) { \
  10597. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10598. "(expected 0x%08x, found 0x%08x)\n", \
  10599. current_config->name, \
  10600. pipe_config->name); \
  10601. ret = false; \
  10602. }
  10603. #define PIPE_CONF_CHECK_I(name) \
  10604. if (current_config->name != pipe_config->name) { \
  10605. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10606. "(expected %i, found %i)\n", \
  10607. current_config->name, \
  10608. pipe_config->name); \
  10609. ret = false; \
  10610. }
  10611. #define PIPE_CONF_CHECK_P(name) \
  10612. if (current_config->name != pipe_config->name) { \
  10613. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10614. "(expected %p, found %p)\n", \
  10615. current_config->name, \
  10616. pipe_config->name); \
  10617. ret = false; \
  10618. }
  10619. #define PIPE_CONF_CHECK_M_N(name) \
  10620. if (!intel_compare_link_m_n(&current_config->name, \
  10621. &pipe_config->name,\
  10622. adjust)) { \
  10623. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10624. "(expected tu %i gmch %i/%i link %i/%i, " \
  10625. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10626. current_config->name.tu, \
  10627. current_config->name.gmch_m, \
  10628. current_config->name.gmch_n, \
  10629. current_config->name.link_m, \
  10630. current_config->name.link_n, \
  10631. pipe_config->name.tu, \
  10632. pipe_config->name.gmch_m, \
  10633. pipe_config->name.gmch_n, \
  10634. pipe_config->name.link_m, \
  10635. pipe_config->name.link_n); \
  10636. ret = false; \
  10637. }
  10638. /* This is required for BDW+ where there is only one set of registers for
  10639. * switching between high and low RR.
  10640. * This macro can be used whenever a comparison has to be made between one
  10641. * hw state and multiple sw state variables.
  10642. */
  10643. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10644. if (!intel_compare_link_m_n(&current_config->name, \
  10645. &pipe_config->name, adjust) && \
  10646. !intel_compare_link_m_n(&current_config->alt_name, \
  10647. &pipe_config->name, adjust)) { \
  10648. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10649. "(expected tu %i gmch %i/%i link %i/%i, " \
  10650. "or tu %i gmch %i/%i link %i/%i, " \
  10651. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10652. current_config->name.tu, \
  10653. current_config->name.gmch_m, \
  10654. current_config->name.gmch_n, \
  10655. current_config->name.link_m, \
  10656. current_config->name.link_n, \
  10657. current_config->alt_name.tu, \
  10658. current_config->alt_name.gmch_m, \
  10659. current_config->alt_name.gmch_n, \
  10660. current_config->alt_name.link_m, \
  10661. current_config->alt_name.link_n, \
  10662. pipe_config->name.tu, \
  10663. pipe_config->name.gmch_m, \
  10664. pipe_config->name.gmch_n, \
  10665. pipe_config->name.link_m, \
  10666. pipe_config->name.link_n); \
  10667. ret = false; \
  10668. }
  10669. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10670. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10671. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10672. "(expected %i, found %i)\n", \
  10673. current_config->name & (mask), \
  10674. pipe_config->name & (mask)); \
  10675. ret = false; \
  10676. }
  10677. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10678. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10679. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10680. "(expected %i, found %i)\n", \
  10681. current_config->name, \
  10682. pipe_config->name); \
  10683. ret = false; \
  10684. }
  10685. #define PIPE_CONF_QUIRK(quirk) \
  10686. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10687. PIPE_CONF_CHECK_I(cpu_transcoder);
  10688. PIPE_CONF_CHECK_I(has_pch_encoder);
  10689. PIPE_CONF_CHECK_I(fdi_lanes);
  10690. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10691. PIPE_CONF_CHECK_I(lane_count);
  10692. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  10693. if (INTEL_INFO(dev)->gen < 8) {
  10694. PIPE_CONF_CHECK_M_N(dp_m_n);
  10695. if (current_config->has_drrs)
  10696. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10697. } else
  10698. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10699. PIPE_CONF_CHECK_X(output_types);
  10700. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10701. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10702. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10703. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10704. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10705. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10706. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10707. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10708. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10709. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10710. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10711. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10712. PIPE_CONF_CHECK_I(pixel_multiplier);
  10713. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10714. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10715. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  10716. PIPE_CONF_CHECK_I(limited_color_range);
  10717. PIPE_CONF_CHECK_I(has_infoframe);
  10718. PIPE_CONF_CHECK_I(has_audio);
  10719. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10720. DRM_MODE_FLAG_INTERLACE);
  10721. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10722. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10723. DRM_MODE_FLAG_PHSYNC);
  10724. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10725. DRM_MODE_FLAG_NHSYNC);
  10726. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10727. DRM_MODE_FLAG_PVSYNC);
  10728. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10729. DRM_MODE_FLAG_NVSYNC);
  10730. }
  10731. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10732. /* pfit ratios are autocomputed by the hw on gen4+ */
  10733. if (INTEL_INFO(dev)->gen < 4)
  10734. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  10735. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10736. if (!adjust) {
  10737. PIPE_CONF_CHECK_I(pipe_src_w);
  10738. PIPE_CONF_CHECK_I(pipe_src_h);
  10739. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10740. if (current_config->pch_pfit.enabled) {
  10741. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10742. PIPE_CONF_CHECK_X(pch_pfit.size);
  10743. }
  10744. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10745. }
  10746. /* BDW+ don't expose a synchronous way to read the state */
  10747. if (IS_HASWELL(dev))
  10748. PIPE_CONF_CHECK_I(ips_enabled);
  10749. PIPE_CONF_CHECK_I(double_wide);
  10750. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10751. PIPE_CONF_CHECK_P(shared_dpll);
  10752. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10753. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10754. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10755. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10756. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10757. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10758. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10759. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10760. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10761. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  10762. PIPE_CONF_CHECK_X(dsi_pll.div);
  10763. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10764. PIPE_CONF_CHECK_I(pipe_bpp);
  10765. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10766. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10767. #undef PIPE_CONF_CHECK_X
  10768. #undef PIPE_CONF_CHECK_I
  10769. #undef PIPE_CONF_CHECK_P
  10770. #undef PIPE_CONF_CHECK_FLAGS
  10771. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10772. #undef PIPE_CONF_QUIRK
  10773. #undef INTEL_ERR_OR_DBG_KMS
  10774. return ret;
  10775. }
  10776. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  10777. const struct intel_crtc_state *pipe_config)
  10778. {
  10779. if (pipe_config->has_pch_encoder) {
  10780. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  10781. &pipe_config->fdi_m_n);
  10782. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  10783. /*
  10784. * FDI already provided one idea for the dotclock.
  10785. * Yell if the encoder disagrees.
  10786. */
  10787. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  10788. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10789. fdi_dotclock, dotclock);
  10790. }
  10791. }
  10792. static void verify_wm_state(struct drm_crtc *crtc,
  10793. struct drm_crtc_state *new_state)
  10794. {
  10795. struct drm_device *dev = crtc->dev;
  10796. struct drm_i915_private *dev_priv = to_i915(dev);
  10797. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10798. struct skl_ddb_entry *hw_entry, *sw_entry;
  10799. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10800. const enum pipe pipe = intel_crtc->pipe;
  10801. int plane;
  10802. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  10803. return;
  10804. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10805. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10806. /* planes */
  10807. for_each_plane(dev_priv, pipe, plane) {
  10808. hw_entry = &hw_ddb.plane[pipe][plane];
  10809. sw_entry = &sw_ddb->plane[pipe][plane];
  10810. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10811. continue;
  10812. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10813. "(expected (%u,%u), found (%u,%u))\n",
  10814. pipe_name(pipe), plane + 1,
  10815. sw_entry->start, sw_entry->end,
  10816. hw_entry->start, hw_entry->end);
  10817. }
  10818. /* cursor */
  10819. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10820. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10821. if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
  10822. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10823. "(expected (%u,%u), found (%u,%u))\n",
  10824. pipe_name(pipe),
  10825. sw_entry->start, sw_entry->end,
  10826. hw_entry->start, hw_entry->end);
  10827. }
  10828. }
  10829. static void
  10830. verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
  10831. {
  10832. struct drm_connector *connector;
  10833. drm_for_each_connector(connector, dev) {
  10834. struct drm_encoder *encoder = connector->encoder;
  10835. struct drm_connector_state *state = connector->state;
  10836. if (state->crtc != crtc)
  10837. continue;
  10838. intel_connector_verify_state(to_intel_connector(connector));
  10839. I915_STATE_WARN(state->best_encoder != encoder,
  10840. "connector's atomic encoder doesn't match legacy encoder\n");
  10841. }
  10842. }
  10843. static void
  10844. verify_encoder_state(struct drm_device *dev)
  10845. {
  10846. struct intel_encoder *encoder;
  10847. struct intel_connector *connector;
  10848. for_each_intel_encoder(dev, encoder) {
  10849. bool enabled = false;
  10850. enum pipe pipe;
  10851. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10852. encoder->base.base.id,
  10853. encoder->base.name);
  10854. for_each_intel_connector(dev, connector) {
  10855. if (connector->base.state->best_encoder != &encoder->base)
  10856. continue;
  10857. enabled = true;
  10858. I915_STATE_WARN(connector->base.state->crtc !=
  10859. encoder->base.crtc,
  10860. "connector's crtc doesn't match encoder crtc\n");
  10861. }
  10862. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10863. "encoder's enabled state mismatch "
  10864. "(expected %i, found %i)\n",
  10865. !!encoder->base.crtc, enabled);
  10866. if (!encoder->base.crtc) {
  10867. bool active;
  10868. active = encoder->get_hw_state(encoder, &pipe);
  10869. I915_STATE_WARN(active,
  10870. "encoder detached but still enabled on pipe %c.\n",
  10871. pipe_name(pipe));
  10872. }
  10873. }
  10874. }
  10875. static void
  10876. verify_crtc_state(struct drm_crtc *crtc,
  10877. struct drm_crtc_state *old_crtc_state,
  10878. struct drm_crtc_state *new_crtc_state)
  10879. {
  10880. struct drm_device *dev = crtc->dev;
  10881. struct drm_i915_private *dev_priv = to_i915(dev);
  10882. struct intel_encoder *encoder;
  10883. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10884. struct intel_crtc_state *pipe_config, *sw_config;
  10885. struct drm_atomic_state *old_state;
  10886. bool active;
  10887. old_state = old_crtc_state->state;
  10888. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10889. pipe_config = to_intel_crtc_state(old_crtc_state);
  10890. memset(pipe_config, 0, sizeof(*pipe_config));
  10891. pipe_config->base.crtc = crtc;
  10892. pipe_config->base.state = old_state;
  10893. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  10894. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10895. /* hw state is inconsistent with the pipe quirk */
  10896. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10897. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10898. active = new_crtc_state->active;
  10899. I915_STATE_WARN(new_crtc_state->active != active,
  10900. "crtc active state doesn't match with hw state "
  10901. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10902. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10903. "transitional active state does not match atomic hw state "
  10904. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10905. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10906. enum pipe pipe;
  10907. active = encoder->get_hw_state(encoder, &pipe);
  10908. I915_STATE_WARN(active != new_crtc_state->active,
  10909. "[ENCODER:%i] active %i with crtc active %i\n",
  10910. encoder->base.base.id, active, new_crtc_state->active);
  10911. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10912. "Encoder connected to wrong pipe %c\n",
  10913. pipe_name(pipe));
  10914. if (active) {
  10915. pipe_config->output_types |= 1 << encoder->type;
  10916. encoder->get_config(encoder, pipe_config);
  10917. }
  10918. }
  10919. if (!new_crtc_state->active)
  10920. return;
  10921. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10922. sw_config = to_intel_crtc_state(crtc->state);
  10923. if (!intel_pipe_config_compare(dev, sw_config,
  10924. pipe_config, false)) {
  10925. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10926. intel_dump_pipe_config(intel_crtc, pipe_config,
  10927. "[hw state]");
  10928. intel_dump_pipe_config(intel_crtc, sw_config,
  10929. "[sw state]");
  10930. }
  10931. }
  10932. static void
  10933. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10934. struct intel_shared_dpll *pll,
  10935. struct drm_crtc *crtc,
  10936. struct drm_crtc_state *new_state)
  10937. {
  10938. struct intel_dpll_hw_state dpll_hw_state;
  10939. unsigned crtc_mask;
  10940. bool active;
  10941. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10942. DRM_DEBUG_KMS("%s\n", pll->name);
  10943. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10944. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10945. I915_STATE_WARN(!pll->on && pll->active_mask,
  10946. "pll in active use but not on in sw tracking\n");
  10947. I915_STATE_WARN(pll->on && !pll->active_mask,
  10948. "pll is on but not used by any active crtc\n");
  10949. I915_STATE_WARN(pll->on != active,
  10950. "pll on state mismatch (expected %i, found %i)\n",
  10951. pll->on, active);
  10952. }
  10953. if (!crtc) {
  10954. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  10955. "more active pll users than references: %x vs %x\n",
  10956. pll->active_mask, pll->config.crtc_mask);
  10957. return;
  10958. }
  10959. crtc_mask = 1 << drm_crtc_index(crtc);
  10960. if (new_state->active)
  10961. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10962. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10963. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10964. else
  10965. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10966. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10967. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10968. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  10969. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10970. crtc_mask, pll->config.crtc_mask);
  10971. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  10972. &dpll_hw_state,
  10973. sizeof(dpll_hw_state)),
  10974. "pll hw state mismatch\n");
  10975. }
  10976. static void
  10977. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10978. struct drm_crtc_state *old_crtc_state,
  10979. struct drm_crtc_state *new_crtc_state)
  10980. {
  10981. struct drm_i915_private *dev_priv = to_i915(dev);
  10982. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10983. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10984. if (new_state->shared_dpll)
  10985. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10986. if (old_state->shared_dpll &&
  10987. old_state->shared_dpll != new_state->shared_dpll) {
  10988. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  10989. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10990. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10991. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10992. pipe_name(drm_crtc_index(crtc)));
  10993. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  10994. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10995. pipe_name(drm_crtc_index(crtc)));
  10996. }
  10997. }
  10998. static void
  10999. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11000. struct drm_crtc_state *old_state,
  11001. struct drm_crtc_state *new_state)
  11002. {
  11003. if (!needs_modeset(new_state) &&
  11004. !to_intel_crtc_state(new_state)->update_pipe)
  11005. return;
  11006. verify_wm_state(crtc, new_state);
  11007. verify_connector_state(crtc->dev, crtc);
  11008. verify_crtc_state(crtc, old_state, new_state);
  11009. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11010. }
  11011. static void
  11012. verify_disabled_dpll_state(struct drm_device *dev)
  11013. {
  11014. struct drm_i915_private *dev_priv = to_i915(dev);
  11015. int i;
  11016. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11017. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11018. }
  11019. static void
  11020. intel_modeset_verify_disabled(struct drm_device *dev)
  11021. {
  11022. verify_encoder_state(dev);
  11023. verify_connector_state(dev, NULL);
  11024. verify_disabled_dpll_state(dev);
  11025. }
  11026. static void update_scanline_offset(struct intel_crtc *crtc)
  11027. {
  11028. struct drm_device *dev = crtc->base.dev;
  11029. /*
  11030. * The scanline counter increments at the leading edge of hsync.
  11031. *
  11032. * On most platforms it starts counting from vtotal-1 on the
  11033. * first active line. That means the scanline counter value is
  11034. * always one less than what we would expect. Ie. just after
  11035. * start of vblank, which also occurs at start of hsync (on the
  11036. * last active line), the scanline counter will read vblank_start-1.
  11037. *
  11038. * On gen2 the scanline counter starts counting from 1 instead
  11039. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11040. * to keep the value positive), instead of adding one.
  11041. *
  11042. * On HSW+ the behaviour of the scanline counter depends on the output
  11043. * type. For DP ports it behaves like most other platforms, but on HDMI
  11044. * there's an extra 1 line difference. So we need to add two instead of
  11045. * one to the value.
  11046. */
  11047. if (IS_GEN2(dev)) {
  11048. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11049. int vtotal;
  11050. vtotal = adjusted_mode->crtc_vtotal;
  11051. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11052. vtotal /= 2;
  11053. crtc->scanline_offset = vtotal - 1;
  11054. } else if (HAS_DDI(dev) &&
  11055. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  11056. crtc->scanline_offset = 2;
  11057. } else
  11058. crtc->scanline_offset = 1;
  11059. }
  11060. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11061. {
  11062. struct drm_device *dev = state->dev;
  11063. struct drm_i915_private *dev_priv = to_i915(dev);
  11064. struct intel_shared_dpll_config *shared_dpll = NULL;
  11065. struct drm_crtc *crtc;
  11066. struct drm_crtc_state *crtc_state;
  11067. int i;
  11068. if (!dev_priv->display.crtc_compute_clock)
  11069. return;
  11070. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11071. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11072. struct intel_shared_dpll *old_dpll =
  11073. to_intel_crtc_state(crtc->state)->shared_dpll;
  11074. if (!needs_modeset(crtc_state))
  11075. continue;
  11076. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11077. if (!old_dpll)
  11078. continue;
  11079. if (!shared_dpll)
  11080. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11081. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11082. }
  11083. }
  11084. /*
  11085. * This implements the workaround described in the "notes" section of the mode
  11086. * set sequence documentation. When going from no pipes or single pipe to
  11087. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11088. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11089. */
  11090. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11091. {
  11092. struct drm_crtc_state *crtc_state;
  11093. struct intel_crtc *intel_crtc;
  11094. struct drm_crtc *crtc;
  11095. struct intel_crtc_state *first_crtc_state = NULL;
  11096. struct intel_crtc_state *other_crtc_state = NULL;
  11097. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11098. int i;
  11099. /* look at all crtc's that are going to be enabled in during modeset */
  11100. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11101. intel_crtc = to_intel_crtc(crtc);
  11102. if (!crtc_state->active || !needs_modeset(crtc_state))
  11103. continue;
  11104. if (first_crtc_state) {
  11105. other_crtc_state = to_intel_crtc_state(crtc_state);
  11106. break;
  11107. } else {
  11108. first_crtc_state = to_intel_crtc_state(crtc_state);
  11109. first_pipe = intel_crtc->pipe;
  11110. }
  11111. }
  11112. /* No workaround needed? */
  11113. if (!first_crtc_state)
  11114. return 0;
  11115. /* w/a possibly needed, check how many crtc's are already enabled. */
  11116. for_each_intel_crtc(state->dev, intel_crtc) {
  11117. struct intel_crtc_state *pipe_config;
  11118. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11119. if (IS_ERR(pipe_config))
  11120. return PTR_ERR(pipe_config);
  11121. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11122. if (!pipe_config->base.active ||
  11123. needs_modeset(&pipe_config->base))
  11124. continue;
  11125. /* 2 or more enabled crtcs means no need for w/a */
  11126. if (enabled_pipe != INVALID_PIPE)
  11127. return 0;
  11128. enabled_pipe = intel_crtc->pipe;
  11129. }
  11130. if (enabled_pipe != INVALID_PIPE)
  11131. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11132. else if (other_crtc_state)
  11133. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11134. return 0;
  11135. }
  11136. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11137. {
  11138. struct drm_crtc *crtc;
  11139. struct drm_crtc_state *crtc_state;
  11140. int ret = 0;
  11141. /* add all active pipes to the state */
  11142. for_each_crtc(state->dev, crtc) {
  11143. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11144. if (IS_ERR(crtc_state))
  11145. return PTR_ERR(crtc_state);
  11146. if (!crtc_state->active || needs_modeset(crtc_state))
  11147. continue;
  11148. crtc_state->mode_changed = true;
  11149. ret = drm_atomic_add_affected_connectors(state, crtc);
  11150. if (ret)
  11151. break;
  11152. ret = drm_atomic_add_affected_planes(state, crtc);
  11153. if (ret)
  11154. break;
  11155. }
  11156. return ret;
  11157. }
  11158. static int intel_modeset_checks(struct drm_atomic_state *state)
  11159. {
  11160. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11161. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11162. struct drm_crtc *crtc;
  11163. struct drm_crtc_state *crtc_state;
  11164. int ret = 0, i;
  11165. if (!check_digital_port_conflicts(state)) {
  11166. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11167. return -EINVAL;
  11168. }
  11169. intel_state->modeset = true;
  11170. intel_state->active_crtcs = dev_priv->active_crtcs;
  11171. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11172. if (crtc_state->active)
  11173. intel_state->active_crtcs |= 1 << i;
  11174. else
  11175. intel_state->active_crtcs &= ~(1 << i);
  11176. if (crtc_state->active != crtc->state->active)
  11177. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11178. }
  11179. /*
  11180. * See if the config requires any additional preparation, e.g.
  11181. * to adjust global state with pipes off. We need to do this
  11182. * here so we can get the modeset_pipe updated config for the new
  11183. * mode set on this crtc. For other crtcs we need to use the
  11184. * adjusted_mode bits in the crtc directly.
  11185. */
  11186. if (dev_priv->display.modeset_calc_cdclk) {
  11187. if (!intel_state->cdclk_pll_vco)
  11188. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11189. if (!intel_state->cdclk_pll_vco)
  11190. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11191. ret = dev_priv->display.modeset_calc_cdclk(state);
  11192. if (ret < 0)
  11193. return ret;
  11194. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11195. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
  11196. ret = intel_modeset_all_pipes(state);
  11197. if (ret < 0)
  11198. return ret;
  11199. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11200. intel_state->cdclk, intel_state->dev_cdclk);
  11201. } else
  11202. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11203. intel_modeset_clear_plls(state);
  11204. if (IS_HASWELL(dev_priv))
  11205. return haswell_mode_set_planes_workaround(state);
  11206. return 0;
  11207. }
  11208. /*
  11209. * Handle calculation of various watermark data at the end of the atomic check
  11210. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11211. * handlers to ensure that all derived state has been updated.
  11212. */
  11213. static int calc_watermark_data(struct drm_atomic_state *state)
  11214. {
  11215. struct drm_device *dev = state->dev;
  11216. struct drm_i915_private *dev_priv = to_i915(dev);
  11217. /* Is there platform-specific watermark information to calculate? */
  11218. if (dev_priv->display.compute_global_watermarks)
  11219. return dev_priv->display.compute_global_watermarks(state);
  11220. return 0;
  11221. }
  11222. /**
  11223. * intel_atomic_check - validate state object
  11224. * @dev: drm device
  11225. * @state: state to validate
  11226. */
  11227. static int intel_atomic_check(struct drm_device *dev,
  11228. struct drm_atomic_state *state)
  11229. {
  11230. struct drm_i915_private *dev_priv = to_i915(dev);
  11231. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11232. struct drm_crtc *crtc;
  11233. struct drm_crtc_state *crtc_state;
  11234. int ret, i;
  11235. bool any_ms = false;
  11236. ret = drm_atomic_helper_check_modeset(dev, state);
  11237. if (ret)
  11238. return ret;
  11239. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11240. struct intel_crtc_state *pipe_config =
  11241. to_intel_crtc_state(crtc_state);
  11242. /* Catch I915_MODE_FLAG_INHERITED */
  11243. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11244. crtc_state->mode_changed = true;
  11245. if (!needs_modeset(crtc_state))
  11246. continue;
  11247. if (!crtc_state->enable) {
  11248. any_ms = true;
  11249. continue;
  11250. }
  11251. /* FIXME: For only active_changed we shouldn't need to do any
  11252. * state recomputation at all. */
  11253. ret = drm_atomic_add_affected_connectors(state, crtc);
  11254. if (ret)
  11255. return ret;
  11256. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11257. if (ret) {
  11258. intel_dump_pipe_config(to_intel_crtc(crtc),
  11259. pipe_config, "[failed]");
  11260. return ret;
  11261. }
  11262. if (i915.fastboot &&
  11263. intel_pipe_config_compare(dev,
  11264. to_intel_crtc_state(crtc->state),
  11265. pipe_config, true)) {
  11266. crtc_state->mode_changed = false;
  11267. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11268. }
  11269. if (needs_modeset(crtc_state))
  11270. any_ms = true;
  11271. ret = drm_atomic_add_affected_planes(state, crtc);
  11272. if (ret)
  11273. return ret;
  11274. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11275. needs_modeset(crtc_state) ?
  11276. "[modeset]" : "[fastset]");
  11277. }
  11278. if (any_ms) {
  11279. ret = intel_modeset_checks(state);
  11280. if (ret)
  11281. return ret;
  11282. } else
  11283. intel_state->cdclk = dev_priv->cdclk_freq;
  11284. ret = drm_atomic_helper_check_planes(dev, state);
  11285. if (ret)
  11286. return ret;
  11287. intel_fbc_choose_crtc(dev_priv, state);
  11288. return calc_watermark_data(state);
  11289. }
  11290. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11291. struct drm_atomic_state *state,
  11292. bool nonblock)
  11293. {
  11294. struct drm_i915_private *dev_priv = to_i915(dev);
  11295. struct drm_plane_state *plane_state;
  11296. struct drm_crtc_state *crtc_state;
  11297. struct drm_plane *plane;
  11298. struct drm_crtc *crtc;
  11299. int i, ret;
  11300. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11301. if (state->legacy_cursor_update)
  11302. continue;
  11303. ret = intel_crtc_wait_for_pending_flips(crtc);
  11304. if (ret)
  11305. return ret;
  11306. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11307. flush_workqueue(dev_priv->wq);
  11308. }
  11309. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11310. if (ret)
  11311. return ret;
  11312. ret = drm_atomic_helper_prepare_planes(dev, state);
  11313. mutex_unlock(&dev->struct_mutex);
  11314. if (!ret && !nonblock) {
  11315. for_each_plane_in_state(state, plane, plane_state, i) {
  11316. struct intel_plane_state *intel_plane_state =
  11317. to_intel_plane_state(plane_state);
  11318. if (!intel_plane_state->wait_req)
  11319. continue;
  11320. ret = i915_wait_request(intel_plane_state->wait_req,
  11321. true, NULL, NULL);
  11322. if (ret) {
  11323. /* Any hang should be swallowed by the wait */
  11324. WARN_ON(ret == -EIO);
  11325. mutex_lock(&dev->struct_mutex);
  11326. drm_atomic_helper_cleanup_planes(dev, state);
  11327. mutex_unlock(&dev->struct_mutex);
  11328. break;
  11329. }
  11330. }
  11331. }
  11332. return ret;
  11333. }
  11334. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11335. {
  11336. struct drm_device *dev = crtc->base.dev;
  11337. if (!dev->max_vblank_count)
  11338. return drm_accurate_vblank_count(&crtc->base);
  11339. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11340. }
  11341. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11342. struct drm_i915_private *dev_priv,
  11343. unsigned crtc_mask)
  11344. {
  11345. unsigned last_vblank_count[I915_MAX_PIPES];
  11346. enum pipe pipe;
  11347. int ret;
  11348. if (!crtc_mask)
  11349. return;
  11350. for_each_pipe(dev_priv, pipe) {
  11351. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11352. if (!((1 << pipe) & crtc_mask))
  11353. continue;
  11354. ret = drm_crtc_vblank_get(crtc);
  11355. if (WARN_ON(ret != 0)) {
  11356. crtc_mask &= ~(1 << pipe);
  11357. continue;
  11358. }
  11359. last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
  11360. }
  11361. for_each_pipe(dev_priv, pipe) {
  11362. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11363. long lret;
  11364. if (!((1 << pipe) & crtc_mask))
  11365. continue;
  11366. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11367. last_vblank_count[pipe] !=
  11368. drm_crtc_vblank_count(crtc),
  11369. msecs_to_jiffies(50));
  11370. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11371. drm_crtc_vblank_put(crtc);
  11372. }
  11373. }
  11374. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11375. {
  11376. /* fb updated, need to unpin old fb */
  11377. if (crtc_state->fb_changed)
  11378. return true;
  11379. /* wm changes, need vblank before final wm's */
  11380. if (crtc_state->update_wm_post)
  11381. return true;
  11382. /*
  11383. * cxsr is re-enabled after vblank.
  11384. * This is already handled by crtc_state->update_wm_post,
  11385. * but added for clarity.
  11386. */
  11387. if (crtc_state->disable_cxsr)
  11388. return true;
  11389. return false;
  11390. }
  11391. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  11392. {
  11393. struct drm_device *dev = state->dev;
  11394. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11395. struct drm_i915_private *dev_priv = to_i915(dev);
  11396. struct drm_crtc_state *old_crtc_state;
  11397. struct drm_crtc *crtc;
  11398. struct intel_crtc_state *intel_cstate;
  11399. struct drm_plane *plane;
  11400. struct drm_plane_state *plane_state;
  11401. bool hw_check = intel_state->modeset;
  11402. unsigned long put_domains[I915_MAX_PIPES] = {};
  11403. unsigned crtc_vblank_mask = 0;
  11404. int i, ret;
  11405. for_each_plane_in_state(state, plane, plane_state, i) {
  11406. struct intel_plane_state *intel_plane_state =
  11407. to_intel_plane_state(plane_state);
  11408. if (!intel_plane_state->wait_req)
  11409. continue;
  11410. ret = i915_wait_request(intel_plane_state->wait_req,
  11411. true, NULL, NULL);
  11412. /* EIO should be eaten, and we can't get interrupted in the
  11413. * worker, and blocking commits have waited already. */
  11414. WARN_ON(ret);
  11415. }
  11416. drm_atomic_helper_wait_for_dependencies(state);
  11417. if (intel_state->modeset) {
  11418. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  11419. sizeof(intel_state->min_pixclk));
  11420. dev_priv->active_crtcs = intel_state->active_crtcs;
  11421. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  11422. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  11423. }
  11424. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11425. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11426. if (needs_modeset(crtc->state) ||
  11427. to_intel_crtc_state(crtc->state)->update_pipe) {
  11428. hw_check = true;
  11429. put_domains[to_intel_crtc(crtc)->pipe] =
  11430. modeset_get_crtc_power_domains(crtc,
  11431. to_intel_crtc_state(crtc->state));
  11432. }
  11433. if (!needs_modeset(crtc->state))
  11434. continue;
  11435. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11436. if (old_crtc_state->active) {
  11437. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  11438. dev_priv->display.crtc_disable(crtc);
  11439. intel_crtc->active = false;
  11440. intel_fbc_disable(intel_crtc);
  11441. intel_disable_shared_dpll(intel_crtc);
  11442. /*
  11443. * Underruns don't always raise
  11444. * interrupts, so check manually.
  11445. */
  11446. intel_check_cpu_fifo_underruns(dev_priv);
  11447. intel_check_pch_fifo_underruns(dev_priv);
  11448. if (!crtc->state->active)
  11449. intel_update_watermarks(crtc);
  11450. }
  11451. }
  11452. /* Only after disabling all output pipelines that will be changed can we
  11453. * update the the output configuration. */
  11454. intel_modeset_update_crtc_state(state);
  11455. if (intel_state->modeset) {
  11456. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11457. if (dev_priv->display.modeset_commit_cdclk &&
  11458. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11459. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  11460. dev_priv->display.modeset_commit_cdclk(state);
  11461. intel_modeset_verify_disabled(dev);
  11462. }
  11463. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11464. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11465. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11466. bool modeset = needs_modeset(crtc->state);
  11467. struct intel_crtc_state *pipe_config =
  11468. to_intel_crtc_state(crtc->state);
  11469. if (modeset && crtc->state->active) {
  11470. update_scanline_offset(to_intel_crtc(crtc));
  11471. dev_priv->display.crtc_enable(crtc);
  11472. }
  11473. /* Complete events for now disable pipes here. */
  11474. if (modeset && !crtc->state->active && crtc->state->event) {
  11475. spin_lock_irq(&dev->event_lock);
  11476. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  11477. spin_unlock_irq(&dev->event_lock);
  11478. crtc->state->event = NULL;
  11479. }
  11480. if (!modeset)
  11481. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11482. if (crtc->state->active &&
  11483. drm_atomic_get_existing_plane_state(state, crtc->primary))
  11484. intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
  11485. if (crtc->state->active)
  11486. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11487. if (pipe_config->base.active && needs_vblank_wait(pipe_config))
  11488. crtc_vblank_mask |= 1 << i;
  11489. }
  11490. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  11491. * already, but still need the state for the delayed optimization. To
  11492. * fix this:
  11493. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  11494. * - schedule that vblank worker _before_ calling hw_done
  11495. * - at the start of commit_tail, cancel it _synchrously
  11496. * - switch over to the vblank wait helper in the core after that since
  11497. * we don't need out special handling any more.
  11498. */
  11499. if (!state->legacy_cursor_update)
  11500. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  11501. /*
  11502. * Now that the vblank has passed, we can go ahead and program the
  11503. * optimal watermarks on platforms that need two-step watermark
  11504. * programming.
  11505. *
  11506. * TODO: Move this (and other cleanup) to an async worker eventually.
  11507. */
  11508. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11509. intel_cstate = to_intel_crtc_state(crtc->state);
  11510. if (dev_priv->display.optimize_watermarks)
  11511. dev_priv->display.optimize_watermarks(intel_cstate);
  11512. }
  11513. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11514. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  11515. if (put_domains[i])
  11516. modeset_put_power_domains(dev_priv, put_domains[i]);
  11517. intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
  11518. }
  11519. drm_atomic_helper_commit_hw_done(state);
  11520. if (intel_state->modeset)
  11521. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  11522. mutex_lock(&dev->struct_mutex);
  11523. drm_atomic_helper_cleanup_planes(dev, state);
  11524. mutex_unlock(&dev->struct_mutex);
  11525. drm_atomic_helper_commit_cleanup_done(state);
  11526. drm_atomic_state_free(state);
  11527. /* As one of the primary mmio accessors, KMS has a high likelihood
  11528. * of triggering bugs in unclaimed access. After we finish
  11529. * modesetting, see if an error has been flagged, and if so
  11530. * enable debugging for the next modeset - and hope we catch
  11531. * the culprit.
  11532. *
  11533. * XXX note that we assume display power is on at this point.
  11534. * This might hold true now but we need to add pm helper to check
  11535. * unclaimed only when the hardware is on, as atomic commits
  11536. * can happen also when the device is completely off.
  11537. */
  11538. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  11539. }
  11540. static void intel_atomic_commit_work(struct work_struct *work)
  11541. {
  11542. struct drm_atomic_state *state = container_of(work,
  11543. struct drm_atomic_state,
  11544. commit_work);
  11545. intel_atomic_commit_tail(state);
  11546. }
  11547. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  11548. {
  11549. struct drm_plane_state *old_plane_state;
  11550. struct drm_plane *plane;
  11551. int i;
  11552. for_each_plane_in_state(state, plane, old_plane_state, i)
  11553. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  11554. intel_fb_obj(plane->state->fb),
  11555. to_intel_plane(plane)->frontbuffer_bit);
  11556. }
  11557. /**
  11558. * intel_atomic_commit - commit validated state object
  11559. * @dev: DRM device
  11560. * @state: the top-level driver state object
  11561. * @nonblock: nonblocking commit
  11562. *
  11563. * This function commits a top-level state object that has been validated
  11564. * with drm_atomic_helper_check().
  11565. *
  11566. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11567. * nonblocking commits are only safe for pure plane updates. Everything else
  11568. * should work though.
  11569. *
  11570. * RETURNS
  11571. * Zero for success or -errno.
  11572. */
  11573. static int intel_atomic_commit(struct drm_device *dev,
  11574. struct drm_atomic_state *state,
  11575. bool nonblock)
  11576. {
  11577. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11578. struct drm_i915_private *dev_priv = to_i915(dev);
  11579. int ret = 0;
  11580. if (intel_state->modeset && nonblock) {
  11581. DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
  11582. return -EINVAL;
  11583. }
  11584. ret = drm_atomic_helper_setup_commit(state, nonblock);
  11585. if (ret)
  11586. return ret;
  11587. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  11588. ret = intel_atomic_prepare_commit(dev, state, nonblock);
  11589. if (ret) {
  11590. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11591. return ret;
  11592. }
  11593. drm_atomic_helper_swap_state(state, true);
  11594. dev_priv->wm.distrust_bios_wm = false;
  11595. dev_priv->wm.skl_results = intel_state->wm_results;
  11596. intel_shared_dpll_commit(state);
  11597. intel_atomic_track_fbs(state);
  11598. if (nonblock)
  11599. queue_work(system_unbound_wq, &state->commit_work);
  11600. else
  11601. intel_atomic_commit_tail(state);
  11602. return 0;
  11603. }
  11604. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11605. {
  11606. struct drm_device *dev = crtc->dev;
  11607. struct drm_atomic_state *state;
  11608. struct drm_crtc_state *crtc_state;
  11609. int ret;
  11610. state = drm_atomic_state_alloc(dev);
  11611. if (!state) {
  11612. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  11613. crtc->base.id, crtc->name);
  11614. return;
  11615. }
  11616. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11617. retry:
  11618. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11619. ret = PTR_ERR_OR_ZERO(crtc_state);
  11620. if (!ret) {
  11621. if (!crtc_state->active)
  11622. goto out;
  11623. crtc_state->mode_changed = true;
  11624. ret = drm_atomic_commit(state);
  11625. }
  11626. if (ret == -EDEADLK) {
  11627. drm_atomic_state_clear(state);
  11628. drm_modeset_backoff(state->acquire_ctx);
  11629. goto retry;
  11630. }
  11631. if (ret)
  11632. out:
  11633. drm_atomic_state_free(state);
  11634. }
  11635. #undef for_each_intel_crtc_masked
  11636. /*
  11637. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  11638. * drm_atomic_helper_legacy_gamma_set() directly.
  11639. */
  11640. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  11641. u16 *red, u16 *green, u16 *blue,
  11642. uint32_t size)
  11643. {
  11644. struct drm_device *dev = crtc->dev;
  11645. struct drm_mode_config *config = &dev->mode_config;
  11646. struct drm_crtc_state *state;
  11647. int ret;
  11648. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  11649. if (ret)
  11650. return ret;
  11651. /*
  11652. * Make sure we update the legacy properties so this works when
  11653. * atomic is not enabled.
  11654. */
  11655. state = crtc->state;
  11656. drm_object_property_set_value(&crtc->base,
  11657. config->degamma_lut_property,
  11658. (state->degamma_lut) ?
  11659. state->degamma_lut->base.id : 0);
  11660. drm_object_property_set_value(&crtc->base,
  11661. config->ctm_property,
  11662. (state->ctm) ?
  11663. state->ctm->base.id : 0);
  11664. drm_object_property_set_value(&crtc->base,
  11665. config->gamma_lut_property,
  11666. (state->gamma_lut) ?
  11667. state->gamma_lut->base.id : 0);
  11668. return 0;
  11669. }
  11670. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11671. .gamma_set = intel_atomic_legacy_gamma_set,
  11672. .set_config = drm_atomic_helper_set_config,
  11673. .set_property = drm_atomic_helper_crtc_set_property,
  11674. .destroy = intel_crtc_destroy,
  11675. .page_flip = intel_crtc_page_flip,
  11676. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11677. .atomic_destroy_state = intel_crtc_destroy_state,
  11678. };
  11679. /**
  11680. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11681. * @plane: drm plane to prepare for
  11682. * @fb: framebuffer to prepare for presentation
  11683. *
  11684. * Prepares a framebuffer for usage on a display plane. Generally this
  11685. * involves pinning the underlying object and updating the frontbuffer tracking
  11686. * bits. Some older platforms need special physical address handling for
  11687. * cursor planes.
  11688. *
  11689. * Must be called with struct_mutex held.
  11690. *
  11691. * Returns 0 on success, negative error code on failure.
  11692. */
  11693. int
  11694. intel_prepare_plane_fb(struct drm_plane *plane,
  11695. const struct drm_plane_state *new_state)
  11696. {
  11697. struct drm_device *dev = plane->dev;
  11698. struct drm_framebuffer *fb = new_state->fb;
  11699. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11700. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11701. struct reservation_object *resv;
  11702. int ret = 0;
  11703. if (!obj && !old_obj)
  11704. return 0;
  11705. if (old_obj) {
  11706. struct drm_crtc_state *crtc_state =
  11707. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11708. /* Big Hammer, we also need to ensure that any pending
  11709. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11710. * current scanout is retired before unpinning the old
  11711. * framebuffer. Note that we rely on userspace rendering
  11712. * into the buffer attached to the pipe they are waiting
  11713. * on. If not, userspace generates a GPU hang with IPEHR
  11714. * point to the MI_WAIT_FOR_EVENT.
  11715. *
  11716. * This should only fail upon a hung GPU, in which case we
  11717. * can safely continue.
  11718. */
  11719. if (needs_modeset(crtc_state))
  11720. ret = i915_gem_object_wait_rendering(old_obj, true);
  11721. if (ret) {
  11722. /* GPU hangs should have been swallowed by the wait */
  11723. WARN_ON(ret == -EIO);
  11724. return ret;
  11725. }
  11726. }
  11727. if (!obj)
  11728. return 0;
  11729. /* For framebuffer backed by dmabuf, wait for fence */
  11730. resv = i915_gem_object_get_dmabuf_resv(obj);
  11731. if (resv) {
  11732. long lret;
  11733. lret = reservation_object_wait_timeout_rcu(resv, false, true,
  11734. MAX_SCHEDULE_TIMEOUT);
  11735. if (lret == -ERESTARTSYS)
  11736. return lret;
  11737. WARN(lret < 0, "waiting returns %li\n", lret);
  11738. }
  11739. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11740. INTEL_INFO(dev)->cursor_needs_physical) {
  11741. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11742. ret = i915_gem_object_attach_phys(obj, align);
  11743. if (ret)
  11744. DRM_DEBUG_KMS("failed to attach phys object\n");
  11745. } else {
  11746. ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  11747. }
  11748. if (ret == 0) {
  11749. to_intel_plane_state(new_state)->wait_req =
  11750. i915_gem_active_get(&obj->last_write,
  11751. &obj->base.dev->struct_mutex);
  11752. }
  11753. return ret;
  11754. }
  11755. /**
  11756. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11757. * @plane: drm plane to clean up for
  11758. * @fb: old framebuffer that was on plane
  11759. *
  11760. * Cleans up a framebuffer that has just been removed from a plane.
  11761. *
  11762. * Must be called with struct_mutex held.
  11763. */
  11764. void
  11765. intel_cleanup_plane_fb(struct drm_plane *plane,
  11766. const struct drm_plane_state *old_state)
  11767. {
  11768. struct drm_device *dev = plane->dev;
  11769. struct intel_plane_state *old_intel_state;
  11770. struct intel_plane_state *intel_state = to_intel_plane_state(plane->state);
  11771. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11772. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11773. old_intel_state = to_intel_plane_state(old_state);
  11774. if (!obj && !old_obj)
  11775. return;
  11776. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11777. !INTEL_INFO(dev)->cursor_needs_physical))
  11778. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  11779. i915_gem_request_assign(&intel_state->wait_req, NULL);
  11780. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11781. }
  11782. int
  11783. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11784. {
  11785. int max_scale;
  11786. int crtc_clock, cdclk;
  11787. if (!intel_crtc || !crtc_state->base.enable)
  11788. return DRM_PLANE_HELPER_NO_SCALING;
  11789. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11790. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11791. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11792. return DRM_PLANE_HELPER_NO_SCALING;
  11793. /*
  11794. * skl max scale is lower of:
  11795. * close to 3 but not 3, -1 is for that purpose
  11796. * or
  11797. * cdclk/crtc_clock
  11798. */
  11799. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11800. return max_scale;
  11801. }
  11802. static int
  11803. intel_check_primary_plane(struct drm_plane *plane,
  11804. struct intel_crtc_state *crtc_state,
  11805. struct intel_plane_state *state)
  11806. {
  11807. struct drm_crtc *crtc = state->base.crtc;
  11808. struct drm_framebuffer *fb = state->base.fb;
  11809. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11810. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11811. bool can_position = false;
  11812. if (INTEL_INFO(plane->dev)->gen >= 9) {
  11813. /* use scaler when colorkey is not required */
  11814. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11815. min_scale = 1;
  11816. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11817. }
  11818. can_position = true;
  11819. }
  11820. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11821. &state->dst, &state->clip,
  11822. state->base.rotation,
  11823. min_scale, max_scale,
  11824. can_position, true,
  11825. &state->visible);
  11826. }
  11827. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11828. struct drm_crtc_state *old_crtc_state)
  11829. {
  11830. struct drm_device *dev = crtc->dev;
  11831. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11832. struct intel_crtc_state *old_intel_state =
  11833. to_intel_crtc_state(old_crtc_state);
  11834. bool modeset = needs_modeset(crtc->state);
  11835. /* Perform vblank evasion around commit operation */
  11836. intel_pipe_update_start(intel_crtc);
  11837. if (modeset)
  11838. return;
  11839. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  11840. intel_color_set_csc(crtc->state);
  11841. intel_color_load_luts(crtc->state);
  11842. }
  11843. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11844. intel_update_pipe_config(intel_crtc, old_intel_state);
  11845. else if (INTEL_INFO(dev)->gen >= 9)
  11846. skl_detach_scalers(intel_crtc);
  11847. }
  11848. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11849. struct drm_crtc_state *old_crtc_state)
  11850. {
  11851. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11852. intel_pipe_update_end(intel_crtc, NULL);
  11853. }
  11854. /**
  11855. * intel_plane_destroy - destroy a plane
  11856. * @plane: plane to destroy
  11857. *
  11858. * Common destruction function for all types of planes (primary, cursor,
  11859. * sprite).
  11860. */
  11861. void intel_plane_destroy(struct drm_plane *plane)
  11862. {
  11863. if (!plane)
  11864. return;
  11865. drm_plane_cleanup(plane);
  11866. kfree(to_intel_plane(plane));
  11867. }
  11868. const struct drm_plane_funcs intel_plane_funcs = {
  11869. .update_plane = drm_atomic_helper_update_plane,
  11870. .disable_plane = drm_atomic_helper_disable_plane,
  11871. .destroy = intel_plane_destroy,
  11872. .set_property = drm_atomic_helper_plane_set_property,
  11873. .atomic_get_property = intel_plane_atomic_get_property,
  11874. .atomic_set_property = intel_plane_atomic_set_property,
  11875. .atomic_duplicate_state = intel_plane_duplicate_state,
  11876. .atomic_destroy_state = intel_plane_destroy_state,
  11877. };
  11878. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11879. int pipe)
  11880. {
  11881. struct intel_plane *primary = NULL;
  11882. struct intel_plane_state *state = NULL;
  11883. const uint32_t *intel_primary_formats;
  11884. unsigned int num_formats;
  11885. int ret;
  11886. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11887. if (!primary)
  11888. goto fail;
  11889. state = intel_create_plane_state(&primary->base);
  11890. if (!state)
  11891. goto fail;
  11892. primary->base.state = &state->base;
  11893. primary->can_scale = false;
  11894. primary->max_downscale = 1;
  11895. if (INTEL_INFO(dev)->gen >= 9) {
  11896. primary->can_scale = true;
  11897. state->scaler_id = -1;
  11898. }
  11899. primary->pipe = pipe;
  11900. primary->plane = pipe;
  11901. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11902. primary->check_plane = intel_check_primary_plane;
  11903. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11904. primary->plane = !pipe;
  11905. if (INTEL_INFO(dev)->gen >= 9) {
  11906. intel_primary_formats = skl_primary_formats;
  11907. num_formats = ARRAY_SIZE(skl_primary_formats);
  11908. primary->update_plane = skylake_update_primary_plane;
  11909. primary->disable_plane = skylake_disable_primary_plane;
  11910. } else if (HAS_PCH_SPLIT(dev)) {
  11911. intel_primary_formats = i965_primary_formats;
  11912. num_formats = ARRAY_SIZE(i965_primary_formats);
  11913. primary->update_plane = ironlake_update_primary_plane;
  11914. primary->disable_plane = i9xx_disable_primary_plane;
  11915. } else if (INTEL_INFO(dev)->gen >= 4) {
  11916. intel_primary_formats = i965_primary_formats;
  11917. num_formats = ARRAY_SIZE(i965_primary_formats);
  11918. primary->update_plane = i9xx_update_primary_plane;
  11919. primary->disable_plane = i9xx_disable_primary_plane;
  11920. } else {
  11921. intel_primary_formats = i8xx_primary_formats;
  11922. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11923. primary->update_plane = i9xx_update_primary_plane;
  11924. primary->disable_plane = i9xx_disable_primary_plane;
  11925. }
  11926. if (INTEL_INFO(dev)->gen >= 9)
  11927. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11928. &intel_plane_funcs,
  11929. intel_primary_formats, num_formats,
  11930. DRM_PLANE_TYPE_PRIMARY,
  11931. "plane 1%c", pipe_name(pipe));
  11932. else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  11933. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11934. &intel_plane_funcs,
  11935. intel_primary_formats, num_formats,
  11936. DRM_PLANE_TYPE_PRIMARY,
  11937. "primary %c", pipe_name(pipe));
  11938. else
  11939. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11940. &intel_plane_funcs,
  11941. intel_primary_formats, num_formats,
  11942. DRM_PLANE_TYPE_PRIMARY,
  11943. "plane %c", plane_name(primary->plane));
  11944. if (ret)
  11945. goto fail;
  11946. if (INTEL_INFO(dev)->gen >= 4)
  11947. intel_create_rotation_property(dev, primary);
  11948. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11949. return &primary->base;
  11950. fail:
  11951. kfree(state);
  11952. kfree(primary);
  11953. return NULL;
  11954. }
  11955. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11956. {
  11957. if (!dev->mode_config.rotation_property) {
  11958. unsigned long flags = BIT(DRM_ROTATE_0) |
  11959. BIT(DRM_ROTATE_180);
  11960. if (INTEL_INFO(dev)->gen >= 9)
  11961. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11962. dev->mode_config.rotation_property =
  11963. drm_mode_create_rotation_property(dev, flags);
  11964. }
  11965. if (dev->mode_config.rotation_property)
  11966. drm_object_attach_property(&plane->base.base,
  11967. dev->mode_config.rotation_property,
  11968. plane->base.state->rotation);
  11969. }
  11970. static int
  11971. intel_check_cursor_plane(struct drm_plane *plane,
  11972. struct intel_crtc_state *crtc_state,
  11973. struct intel_plane_state *state)
  11974. {
  11975. struct drm_crtc *crtc = crtc_state->base.crtc;
  11976. struct drm_framebuffer *fb = state->base.fb;
  11977. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11978. enum pipe pipe = to_intel_plane(plane)->pipe;
  11979. unsigned stride;
  11980. int ret;
  11981. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11982. &state->dst, &state->clip,
  11983. state->base.rotation,
  11984. DRM_PLANE_HELPER_NO_SCALING,
  11985. DRM_PLANE_HELPER_NO_SCALING,
  11986. true, true, &state->visible);
  11987. if (ret)
  11988. return ret;
  11989. /* if we want to turn off the cursor ignore width and height */
  11990. if (!obj)
  11991. return 0;
  11992. /* Check for which cursor types we support */
  11993. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11994. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11995. state->base.crtc_w, state->base.crtc_h);
  11996. return -EINVAL;
  11997. }
  11998. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11999. if (obj->base.size < stride * state->base.crtc_h) {
  12000. DRM_DEBUG_KMS("buffer is too small\n");
  12001. return -ENOMEM;
  12002. }
  12003. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  12004. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  12005. return -EINVAL;
  12006. }
  12007. /*
  12008. * There's something wrong with the cursor on CHV pipe C.
  12009. * If it straddles the left edge of the screen then
  12010. * moving it away from the edge or disabling it often
  12011. * results in a pipe underrun, and often that can lead to
  12012. * dead pipe (constant underrun reported, and it scans
  12013. * out just a solid color). To recover from that, the
  12014. * display power well must be turned off and on again.
  12015. * Refuse the put the cursor into that compromised position.
  12016. */
  12017. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  12018. state->visible && state->base.crtc_x < 0) {
  12019. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12020. return -EINVAL;
  12021. }
  12022. return 0;
  12023. }
  12024. static void
  12025. intel_disable_cursor_plane(struct drm_plane *plane,
  12026. struct drm_crtc *crtc)
  12027. {
  12028. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12029. intel_crtc->cursor_addr = 0;
  12030. intel_crtc_update_cursor(crtc, NULL);
  12031. }
  12032. static void
  12033. intel_update_cursor_plane(struct drm_plane *plane,
  12034. const struct intel_crtc_state *crtc_state,
  12035. const struct intel_plane_state *state)
  12036. {
  12037. struct drm_crtc *crtc = crtc_state->base.crtc;
  12038. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12039. struct drm_device *dev = plane->dev;
  12040. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12041. uint32_t addr;
  12042. if (!obj)
  12043. addr = 0;
  12044. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  12045. addr = i915_gem_obj_ggtt_offset(obj);
  12046. else
  12047. addr = obj->phys_handle->busaddr;
  12048. intel_crtc->cursor_addr = addr;
  12049. intel_crtc_update_cursor(crtc, state);
  12050. }
  12051. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  12052. int pipe)
  12053. {
  12054. struct intel_plane *cursor = NULL;
  12055. struct intel_plane_state *state = NULL;
  12056. int ret;
  12057. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12058. if (!cursor)
  12059. goto fail;
  12060. state = intel_create_plane_state(&cursor->base);
  12061. if (!state)
  12062. goto fail;
  12063. cursor->base.state = &state->base;
  12064. cursor->can_scale = false;
  12065. cursor->max_downscale = 1;
  12066. cursor->pipe = pipe;
  12067. cursor->plane = pipe;
  12068. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12069. cursor->check_plane = intel_check_cursor_plane;
  12070. cursor->update_plane = intel_update_cursor_plane;
  12071. cursor->disable_plane = intel_disable_cursor_plane;
  12072. ret = drm_universal_plane_init(dev, &cursor->base, 0,
  12073. &intel_plane_funcs,
  12074. intel_cursor_formats,
  12075. ARRAY_SIZE(intel_cursor_formats),
  12076. DRM_PLANE_TYPE_CURSOR,
  12077. "cursor %c", pipe_name(pipe));
  12078. if (ret)
  12079. goto fail;
  12080. if (INTEL_INFO(dev)->gen >= 4) {
  12081. if (!dev->mode_config.rotation_property)
  12082. dev->mode_config.rotation_property =
  12083. drm_mode_create_rotation_property(dev,
  12084. BIT(DRM_ROTATE_0) |
  12085. BIT(DRM_ROTATE_180));
  12086. if (dev->mode_config.rotation_property)
  12087. drm_object_attach_property(&cursor->base.base,
  12088. dev->mode_config.rotation_property,
  12089. state->base.rotation);
  12090. }
  12091. if (INTEL_INFO(dev)->gen >=9)
  12092. state->scaler_id = -1;
  12093. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12094. return &cursor->base;
  12095. fail:
  12096. kfree(state);
  12097. kfree(cursor);
  12098. return NULL;
  12099. }
  12100. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  12101. struct intel_crtc_state *crtc_state)
  12102. {
  12103. int i;
  12104. struct intel_scaler *intel_scaler;
  12105. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  12106. for (i = 0; i < intel_crtc->num_scalers; i++) {
  12107. intel_scaler = &scaler_state->scalers[i];
  12108. intel_scaler->in_use = 0;
  12109. intel_scaler->mode = PS_SCALER_MODE_DYN;
  12110. }
  12111. scaler_state->scaler_id = -1;
  12112. }
  12113. static void intel_crtc_init(struct drm_device *dev, int pipe)
  12114. {
  12115. struct drm_i915_private *dev_priv = to_i915(dev);
  12116. struct intel_crtc *intel_crtc;
  12117. struct intel_crtc_state *crtc_state = NULL;
  12118. struct drm_plane *primary = NULL;
  12119. struct drm_plane *cursor = NULL;
  12120. int ret;
  12121. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12122. if (intel_crtc == NULL)
  12123. return;
  12124. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12125. if (!crtc_state)
  12126. goto fail;
  12127. intel_crtc->config = crtc_state;
  12128. intel_crtc->base.state = &crtc_state->base;
  12129. crtc_state->base.crtc = &intel_crtc->base;
  12130. /* initialize shared scalers */
  12131. if (INTEL_INFO(dev)->gen >= 9) {
  12132. if (pipe == PIPE_C)
  12133. intel_crtc->num_scalers = 1;
  12134. else
  12135. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  12136. skl_init_scalers(dev, intel_crtc, crtc_state);
  12137. }
  12138. primary = intel_primary_plane_create(dev, pipe);
  12139. if (!primary)
  12140. goto fail;
  12141. cursor = intel_cursor_plane_create(dev, pipe);
  12142. if (!cursor)
  12143. goto fail;
  12144. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  12145. cursor, &intel_crtc_funcs,
  12146. "pipe %c", pipe_name(pipe));
  12147. if (ret)
  12148. goto fail;
  12149. /*
  12150. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  12151. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  12152. */
  12153. intel_crtc->pipe = pipe;
  12154. intel_crtc->plane = pipe;
  12155. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  12156. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  12157. intel_crtc->plane = !pipe;
  12158. }
  12159. intel_crtc->cursor_base = ~0;
  12160. intel_crtc->cursor_cntl = ~0;
  12161. intel_crtc->cursor_size = ~0;
  12162. intel_crtc->wm.cxsr_allowed = true;
  12163. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12164. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12165. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  12166. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  12167. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12168. intel_color_init(&intel_crtc->base);
  12169. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12170. return;
  12171. fail:
  12172. intel_plane_destroy(primary);
  12173. intel_plane_destroy(cursor);
  12174. kfree(crtc_state);
  12175. kfree(intel_crtc);
  12176. }
  12177. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12178. {
  12179. struct drm_encoder *encoder = connector->base.encoder;
  12180. struct drm_device *dev = connector->base.dev;
  12181. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12182. if (!encoder || WARN_ON(!encoder->crtc))
  12183. return INVALID_PIPE;
  12184. return to_intel_crtc(encoder->crtc)->pipe;
  12185. }
  12186. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12187. struct drm_file *file)
  12188. {
  12189. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12190. struct drm_crtc *drmmode_crtc;
  12191. struct intel_crtc *crtc;
  12192. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12193. if (!drmmode_crtc)
  12194. return -ENOENT;
  12195. crtc = to_intel_crtc(drmmode_crtc);
  12196. pipe_from_crtc_id->pipe = crtc->pipe;
  12197. return 0;
  12198. }
  12199. static int intel_encoder_clones(struct intel_encoder *encoder)
  12200. {
  12201. struct drm_device *dev = encoder->base.dev;
  12202. struct intel_encoder *source_encoder;
  12203. int index_mask = 0;
  12204. int entry = 0;
  12205. for_each_intel_encoder(dev, source_encoder) {
  12206. if (encoders_cloneable(encoder, source_encoder))
  12207. index_mask |= (1 << entry);
  12208. entry++;
  12209. }
  12210. return index_mask;
  12211. }
  12212. static bool has_edp_a(struct drm_device *dev)
  12213. {
  12214. struct drm_i915_private *dev_priv = to_i915(dev);
  12215. if (!IS_MOBILE(dev))
  12216. return false;
  12217. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12218. return false;
  12219. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12220. return false;
  12221. return true;
  12222. }
  12223. static bool intel_crt_present(struct drm_device *dev)
  12224. {
  12225. struct drm_i915_private *dev_priv = to_i915(dev);
  12226. if (INTEL_INFO(dev)->gen >= 9)
  12227. return false;
  12228. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  12229. return false;
  12230. if (IS_CHERRYVIEW(dev))
  12231. return false;
  12232. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12233. return false;
  12234. /* DDI E can't be used if DDI A requires 4 lanes */
  12235. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12236. return false;
  12237. if (!dev_priv->vbt.int_crt_support)
  12238. return false;
  12239. return true;
  12240. }
  12241. static void intel_setup_outputs(struct drm_device *dev)
  12242. {
  12243. struct drm_i915_private *dev_priv = to_i915(dev);
  12244. struct intel_encoder *encoder;
  12245. bool dpd_is_edp = false;
  12246. /*
  12247. * intel_edp_init_connector() depends on this completing first, to
  12248. * prevent the registeration of both eDP and LVDS and the incorrect
  12249. * sharing of the PPS.
  12250. */
  12251. intel_lvds_init(dev);
  12252. if (intel_crt_present(dev))
  12253. intel_crt_init(dev);
  12254. if (IS_BROXTON(dev)) {
  12255. /*
  12256. * FIXME: Broxton doesn't support port detection via the
  12257. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12258. * detect the ports.
  12259. */
  12260. intel_ddi_init(dev, PORT_A);
  12261. intel_ddi_init(dev, PORT_B);
  12262. intel_ddi_init(dev, PORT_C);
  12263. intel_dsi_init(dev);
  12264. } else if (HAS_DDI(dev)) {
  12265. int found;
  12266. /*
  12267. * Haswell uses DDI functions to detect digital outputs.
  12268. * On SKL pre-D0 the strap isn't connected, so we assume
  12269. * it's there.
  12270. */
  12271. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12272. /* WaIgnoreDDIAStrap: skl */
  12273. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12274. intel_ddi_init(dev, PORT_A);
  12275. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12276. * register */
  12277. found = I915_READ(SFUSE_STRAP);
  12278. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12279. intel_ddi_init(dev, PORT_B);
  12280. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12281. intel_ddi_init(dev, PORT_C);
  12282. if (found & SFUSE_STRAP_DDID_DETECTED)
  12283. intel_ddi_init(dev, PORT_D);
  12284. /*
  12285. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12286. */
  12287. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  12288. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12289. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12290. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12291. intel_ddi_init(dev, PORT_E);
  12292. } else if (HAS_PCH_SPLIT(dev)) {
  12293. int found;
  12294. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12295. if (has_edp_a(dev))
  12296. intel_dp_init(dev, DP_A, PORT_A);
  12297. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12298. /* PCH SDVOB multiplex with HDMIB */
  12299. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12300. if (!found)
  12301. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12302. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12303. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12304. }
  12305. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12306. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12307. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12308. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12309. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12310. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12311. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12312. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12313. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12314. bool has_edp, has_port;
  12315. /*
  12316. * The DP_DETECTED bit is the latched state of the DDC
  12317. * SDA pin at boot. However since eDP doesn't require DDC
  12318. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12319. * eDP ports may have been muxed to an alternate function.
  12320. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12321. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12322. * detect eDP ports.
  12323. *
  12324. * Sadly the straps seem to be missing sometimes even for HDMI
  12325. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  12326. * and VBT for the presence of the port. Additionally we can't
  12327. * trust the port type the VBT declares as we've seen at least
  12328. * HDMI ports that the VBT claim are DP or eDP.
  12329. */
  12330. has_edp = intel_dp_is_edp(dev, PORT_B);
  12331. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  12332. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  12333. has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
  12334. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  12335. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  12336. has_edp = intel_dp_is_edp(dev, PORT_C);
  12337. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  12338. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  12339. has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
  12340. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  12341. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  12342. if (IS_CHERRYVIEW(dev)) {
  12343. /*
  12344. * eDP not supported on port D,
  12345. * so no need to worry about it
  12346. */
  12347. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  12348. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  12349. intel_dp_init(dev, CHV_DP_D, PORT_D);
  12350. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  12351. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  12352. }
  12353. intel_dsi_init(dev);
  12354. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12355. bool found = false;
  12356. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12357. DRM_DEBUG_KMS("probing SDVOB\n");
  12358. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  12359. if (!found && IS_G4X(dev)) {
  12360. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12361. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12362. }
  12363. if (!found && IS_G4X(dev))
  12364. intel_dp_init(dev, DP_B, PORT_B);
  12365. }
  12366. /* Before G4X SDVOC doesn't have its own detect register */
  12367. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12368. DRM_DEBUG_KMS("probing SDVOC\n");
  12369. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  12370. }
  12371. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12372. if (IS_G4X(dev)) {
  12373. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12374. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12375. }
  12376. if (IS_G4X(dev))
  12377. intel_dp_init(dev, DP_C, PORT_C);
  12378. }
  12379. if (IS_G4X(dev) &&
  12380. (I915_READ(DP_D) & DP_DETECTED))
  12381. intel_dp_init(dev, DP_D, PORT_D);
  12382. } else if (IS_GEN2(dev))
  12383. intel_dvo_init(dev);
  12384. if (SUPPORTS_TV(dev))
  12385. intel_tv_init(dev);
  12386. intel_psr_init(dev);
  12387. for_each_intel_encoder(dev, encoder) {
  12388. encoder->base.possible_crtcs = encoder->crtc_mask;
  12389. encoder->base.possible_clones =
  12390. intel_encoder_clones(encoder);
  12391. }
  12392. intel_init_pch_refclk(dev);
  12393. drm_helper_move_panel_connectors_to_head(dev);
  12394. }
  12395. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12396. {
  12397. struct drm_device *dev = fb->dev;
  12398. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12399. drm_framebuffer_cleanup(fb);
  12400. mutex_lock(&dev->struct_mutex);
  12401. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12402. i915_gem_object_put(intel_fb->obj);
  12403. mutex_unlock(&dev->struct_mutex);
  12404. kfree(intel_fb);
  12405. }
  12406. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12407. struct drm_file *file,
  12408. unsigned int *handle)
  12409. {
  12410. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12411. struct drm_i915_gem_object *obj = intel_fb->obj;
  12412. if (obj->userptr.mm) {
  12413. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  12414. return -EINVAL;
  12415. }
  12416. return drm_gem_handle_create(file, &obj->base, handle);
  12417. }
  12418. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12419. struct drm_file *file,
  12420. unsigned flags, unsigned color,
  12421. struct drm_clip_rect *clips,
  12422. unsigned num_clips)
  12423. {
  12424. struct drm_device *dev = fb->dev;
  12425. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12426. struct drm_i915_gem_object *obj = intel_fb->obj;
  12427. mutex_lock(&dev->struct_mutex);
  12428. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  12429. mutex_unlock(&dev->struct_mutex);
  12430. return 0;
  12431. }
  12432. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12433. .destroy = intel_user_framebuffer_destroy,
  12434. .create_handle = intel_user_framebuffer_create_handle,
  12435. .dirty = intel_user_framebuffer_dirty,
  12436. };
  12437. static
  12438. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12439. uint32_t pixel_format)
  12440. {
  12441. u32 gen = INTEL_INFO(dev)->gen;
  12442. if (gen >= 9) {
  12443. int cpp = drm_format_plane_cpp(pixel_format, 0);
  12444. /* "The stride in bytes must not exceed the of the size of 8K
  12445. * pixels and 32K bytes."
  12446. */
  12447. return min(8192 * cpp, 32768);
  12448. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12449. return 32*1024;
  12450. } else if (gen >= 4) {
  12451. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12452. return 16*1024;
  12453. else
  12454. return 32*1024;
  12455. } else if (gen >= 3) {
  12456. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12457. return 8*1024;
  12458. else
  12459. return 16*1024;
  12460. } else {
  12461. /* XXX DSPC is limited to 4k tiled */
  12462. return 8*1024;
  12463. }
  12464. }
  12465. static int intel_framebuffer_init(struct drm_device *dev,
  12466. struct intel_framebuffer *intel_fb,
  12467. struct drm_mode_fb_cmd2 *mode_cmd,
  12468. struct drm_i915_gem_object *obj)
  12469. {
  12470. struct drm_i915_private *dev_priv = to_i915(dev);
  12471. unsigned int aligned_height;
  12472. int ret;
  12473. u32 pitch_limit, stride_alignment;
  12474. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12475. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12476. /* Enforce that fb modifier and tiling mode match, but only for
  12477. * X-tiled. This is needed for FBC. */
  12478. if (!!(i915_gem_object_get_tiling(obj) == I915_TILING_X) !=
  12479. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12480. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12481. return -EINVAL;
  12482. }
  12483. } else {
  12484. if (i915_gem_object_get_tiling(obj) == I915_TILING_X)
  12485. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12486. else if (i915_gem_object_get_tiling(obj) == I915_TILING_Y) {
  12487. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12488. return -EINVAL;
  12489. }
  12490. }
  12491. /* Passed in modifier sanity checking. */
  12492. switch (mode_cmd->modifier[0]) {
  12493. case I915_FORMAT_MOD_Y_TILED:
  12494. case I915_FORMAT_MOD_Yf_TILED:
  12495. if (INTEL_INFO(dev)->gen < 9) {
  12496. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12497. mode_cmd->modifier[0]);
  12498. return -EINVAL;
  12499. }
  12500. case DRM_FORMAT_MOD_NONE:
  12501. case I915_FORMAT_MOD_X_TILED:
  12502. break;
  12503. default:
  12504. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12505. mode_cmd->modifier[0]);
  12506. return -EINVAL;
  12507. }
  12508. stride_alignment = intel_fb_stride_alignment(dev_priv,
  12509. mode_cmd->modifier[0],
  12510. mode_cmd->pixel_format);
  12511. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12512. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12513. mode_cmd->pitches[0], stride_alignment);
  12514. return -EINVAL;
  12515. }
  12516. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12517. mode_cmd->pixel_format);
  12518. if (mode_cmd->pitches[0] > pitch_limit) {
  12519. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12520. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12521. "tiled" : "linear",
  12522. mode_cmd->pitches[0], pitch_limit);
  12523. return -EINVAL;
  12524. }
  12525. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12526. mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
  12527. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12528. mode_cmd->pitches[0],
  12529. i915_gem_object_get_stride(obj));
  12530. return -EINVAL;
  12531. }
  12532. /* Reject formats not supported by any plane early. */
  12533. switch (mode_cmd->pixel_format) {
  12534. case DRM_FORMAT_C8:
  12535. case DRM_FORMAT_RGB565:
  12536. case DRM_FORMAT_XRGB8888:
  12537. case DRM_FORMAT_ARGB8888:
  12538. break;
  12539. case DRM_FORMAT_XRGB1555:
  12540. if (INTEL_INFO(dev)->gen > 3) {
  12541. DRM_DEBUG("unsupported pixel format: %s\n",
  12542. drm_get_format_name(mode_cmd->pixel_format));
  12543. return -EINVAL;
  12544. }
  12545. break;
  12546. case DRM_FORMAT_ABGR8888:
  12547. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  12548. INTEL_INFO(dev)->gen < 9) {
  12549. DRM_DEBUG("unsupported pixel format: %s\n",
  12550. drm_get_format_name(mode_cmd->pixel_format));
  12551. return -EINVAL;
  12552. }
  12553. break;
  12554. case DRM_FORMAT_XBGR8888:
  12555. case DRM_FORMAT_XRGB2101010:
  12556. case DRM_FORMAT_XBGR2101010:
  12557. if (INTEL_INFO(dev)->gen < 4) {
  12558. DRM_DEBUG("unsupported pixel format: %s\n",
  12559. drm_get_format_name(mode_cmd->pixel_format));
  12560. return -EINVAL;
  12561. }
  12562. break;
  12563. case DRM_FORMAT_ABGR2101010:
  12564. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12565. DRM_DEBUG("unsupported pixel format: %s\n",
  12566. drm_get_format_name(mode_cmd->pixel_format));
  12567. return -EINVAL;
  12568. }
  12569. break;
  12570. case DRM_FORMAT_YUYV:
  12571. case DRM_FORMAT_UYVY:
  12572. case DRM_FORMAT_YVYU:
  12573. case DRM_FORMAT_VYUY:
  12574. if (INTEL_INFO(dev)->gen < 5) {
  12575. DRM_DEBUG("unsupported pixel format: %s\n",
  12576. drm_get_format_name(mode_cmd->pixel_format));
  12577. return -EINVAL;
  12578. }
  12579. break;
  12580. default:
  12581. DRM_DEBUG("unsupported pixel format: %s\n",
  12582. drm_get_format_name(mode_cmd->pixel_format));
  12583. return -EINVAL;
  12584. }
  12585. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12586. if (mode_cmd->offsets[0] != 0)
  12587. return -EINVAL;
  12588. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12589. mode_cmd->pixel_format,
  12590. mode_cmd->modifier[0]);
  12591. /* FIXME drm helper for size checks (especially planar formats)? */
  12592. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12593. return -EINVAL;
  12594. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12595. intel_fb->obj = obj;
  12596. intel_fill_fb_info(dev_priv, &intel_fb->base);
  12597. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12598. if (ret) {
  12599. DRM_ERROR("framebuffer init failed %d\n", ret);
  12600. return ret;
  12601. }
  12602. intel_fb->obj->framebuffer_references++;
  12603. return 0;
  12604. }
  12605. static struct drm_framebuffer *
  12606. intel_user_framebuffer_create(struct drm_device *dev,
  12607. struct drm_file *filp,
  12608. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12609. {
  12610. struct drm_framebuffer *fb;
  12611. struct drm_i915_gem_object *obj;
  12612. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12613. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  12614. if (!obj)
  12615. return ERR_PTR(-ENOENT);
  12616. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  12617. if (IS_ERR(fb))
  12618. i915_gem_object_put_unlocked(obj);
  12619. return fb;
  12620. }
  12621. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12622. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12623. {
  12624. }
  12625. #endif
  12626. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12627. .fb_create = intel_user_framebuffer_create,
  12628. .output_poll_changed = intel_fbdev_output_poll_changed,
  12629. .atomic_check = intel_atomic_check,
  12630. .atomic_commit = intel_atomic_commit,
  12631. .atomic_state_alloc = intel_atomic_state_alloc,
  12632. .atomic_state_clear = intel_atomic_state_clear,
  12633. };
  12634. /**
  12635. * intel_init_display_hooks - initialize the display modesetting hooks
  12636. * @dev_priv: device private
  12637. */
  12638. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12639. {
  12640. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12641. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12642. dev_priv->display.get_initial_plane_config =
  12643. skylake_get_initial_plane_config;
  12644. dev_priv->display.crtc_compute_clock =
  12645. haswell_crtc_compute_clock;
  12646. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12647. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12648. } else if (HAS_DDI(dev_priv)) {
  12649. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12650. dev_priv->display.get_initial_plane_config =
  12651. ironlake_get_initial_plane_config;
  12652. dev_priv->display.crtc_compute_clock =
  12653. haswell_crtc_compute_clock;
  12654. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12655. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12656. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12657. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12658. dev_priv->display.get_initial_plane_config =
  12659. ironlake_get_initial_plane_config;
  12660. dev_priv->display.crtc_compute_clock =
  12661. ironlake_crtc_compute_clock;
  12662. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12663. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12664. } else if (IS_CHERRYVIEW(dev_priv)) {
  12665. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12666. dev_priv->display.get_initial_plane_config =
  12667. i9xx_get_initial_plane_config;
  12668. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12669. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12670. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12671. } else if (IS_VALLEYVIEW(dev_priv)) {
  12672. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12673. dev_priv->display.get_initial_plane_config =
  12674. i9xx_get_initial_plane_config;
  12675. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12676. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12677. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12678. } else if (IS_G4X(dev_priv)) {
  12679. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12680. dev_priv->display.get_initial_plane_config =
  12681. i9xx_get_initial_plane_config;
  12682. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12683. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12684. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12685. } else if (IS_PINEVIEW(dev_priv)) {
  12686. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12687. dev_priv->display.get_initial_plane_config =
  12688. i9xx_get_initial_plane_config;
  12689. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12690. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12691. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12692. } else if (!IS_GEN2(dev_priv)) {
  12693. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12694. dev_priv->display.get_initial_plane_config =
  12695. i9xx_get_initial_plane_config;
  12696. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12697. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12698. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12699. } else {
  12700. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12701. dev_priv->display.get_initial_plane_config =
  12702. i9xx_get_initial_plane_config;
  12703. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12704. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12705. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12706. }
  12707. /* Returns the core display clock speed */
  12708. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12709. dev_priv->display.get_display_clock_speed =
  12710. skylake_get_display_clock_speed;
  12711. else if (IS_BROXTON(dev_priv))
  12712. dev_priv->display.get_display_clock_speed =
  12713. broxton_get_display_clock_speed;
  12714. else if (IS_BROADWELL(dev_priv))
  12715. dev_priv->display.get_display_clock_speed =
  12716. broadwell_get_display_clock_speed;
  12717. else if (IS_HASWELL(dev_priv))
  12718. dev_priv->display.get_display_clock_speed =
  12719. haswell_get_display_clock_speed;
  12720. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12721. dev_priv->display.get_display_clock_speed =
  12722. valleyview_get_display_clock_speed;
  12723. else if (IS_GEN5(dev_priv))
  12724. dev_priv->display.get_display_clock_speed =
  12725. ilk_get_display_clock_speed;
  12726. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  12727. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  12728. dev_priv->display.get_display_clock_speed =
  12729. i945_get_display_clock_speed;
  12730. else if (IS_GM45(dev_priv))
  12731. dev_priv->display.get_display_clock_speed =
  12732. gm45_get_display_clock_speed;
  12733. else if (IS_CRESTLINE(dev_priv))
  12734. dev_priv->display.get_display_clock_speed =
  12735. i965gm_get_display_clock_speed;
  12736. else if (IS_PINEVIEW(dev_priv))
  12737. dev_priv->display.get_display_clock_speed =
  12738. pnv_get_display_clock_speed;
  12739. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  12740. dev_priv->display.get_display_clock_speed =
  12741. g33_get_display_clock_speed;
  12742. else if (IS_I915G(dev_priv))
  12743. dev_priv->display.get_display_clock_speed =
  12744. i915_get_display_clock_speed;
  12745. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  12746. dev_priv->display.get_display_clock_speed =
  12747. i9xx_misc_get_display_clock_speed;
  12748. else if (IS_I915GM(dev_priv))
  12749. dev_priv->display.get_display_clock_speed =
  12750. i915gm_get_display_clock_speed;
  12751. else if (IS_I865G(dev_priv))
  12752. dev_priv->display.get_display_clock_speed =
  12753. i865_get_display_clock_speed;
  12754. else if (IS_I85X(dev_priv))
  12755. dev_priv->display.get_display_clock_speed =
  12756. i85x_get_display_clock_speed;
  12757. else { /* 830 */
  12758. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12759. dev_priv->display.get_display_clock_speed =
  12760. i830_get_display_clock_speed;
  12761. }
  12762. if (IS_GEN5(dev_priv)) {
  12763. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12764. } else if (IS_GEN6(dev_priv)) {
  12765. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12766. } else if (IS_IVYBRIDGE(dev_priv)) {
  12767. /* FIXME: detect B0+ stepping and use auto training */
  12768. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12769. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12770. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12771. }
  12772. if (IS_BROADWELL(dev_priv)) {
  12773. dev_priv->display.modeset_commit_cdclk =
  12774. broadwell_modeset_commit_cdclk;
  12775. dev_priv->display.modeset_calc_cdclk =
  12776. broadwell_modeset_calc_cdclk;
  12777. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12778. dev_priv->display.modeset_commit_cdclk =
  12779. valleyview_modeset_commit_cdclk;
  12780. dev_priv->display.modeset_calc_cdclk =
  12781. valleyview_modeset_calc_cdclk;
  12782. } else if (IS_BROXTON(dev_priv)) {
  12783. dev_priv->display.modeset_commit_cdclk =
  12784. bxt_modeset_commit_cdclk;
  12785. dev_priv->display.modeset_calc_cdclk =
  12786. bxt_modeset_calc_cdclk;
  12787. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  12788. dev_priv->display.modeset_commit_cdclk =
  12789. skl_modeset_commit_cdclk;
  12790. dev_priv->display.modeset_calc_cdclk =
  12791. skl_modeset_calc_cdclk;
  12792. }
  12793. switch (INTEL_INFO(dev_priv)->gen) {
  12794. case 2:
  12795. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12796. break;
  12797. case 3:
  12798. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12799. break;
  12800. case 4:
  12801. case 5:
  12802. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12803. break;
  12804. case 6:
  12805. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12806. break;
  12807. case 7:
  12808. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12809. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12810. break;
  12811. case 9:
  12812. /* Drop through - unsupported since execlist only. */
  12813. default:
  12814. /* Default just returns -ENODEV to indicate unsupported */
  12815. dev_priv->display.queue_flip = intel_default_queue_flip;
  12816. }
  12817. }
  12818. /*
  12819. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12820. * resume, or other times. This quirk makes sure that's the case for
  12821. * affected systems.
  12822. */
  12823. static void quirk_pipea_force(struct drm_device *dev)
  12824. {
  12825. struct drm_i915_private *dev_priv = to_i915(dev);
  12826. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12827. DRM_INFO("applying pipe a force quirk\n");
  12828. }
  12829. static void quirk_pipeb_force(struct drm_device *dev)
  12830. {
  12831. struct drm_i915_private *dev_priv = to_i915(dev);
  12832. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12833. DRM_INFO("applying pipe b force quirk\n");
  12834. }
  12835. /*
  12836. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12837. */
  12838. static void quirk_ssc_force_disable(struct drm_device *dev)
  12839. {
  12840. struct drm_i915_private *dev_priv = to_i915(dev);
  12841. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12842. DRM_INFO("applying lvds SSC disable quirk\n");
  12843. }
  12844. /*
  12845. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12846. * brightness value
  12847. */
  12848. static void quirk_invert_brightness(struct drm_device *dev)
  12849. {
  12850. struct drm_i915_private *dev_priv = to_i915(dev);
  12851. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12852. DRM_INFO("applying inverted panel brightness quirk\n");
  12853. }
  12854. /* Some VBT's incorrectly indicate no backlight is present */
  12855. static void quirk_backlight_present(struct drm_device *dev)
  12856. {
  12857. struct drm_i915_private *dev_priv = to_i915(dev);
  12858. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12859. DRM_INFO("applying backlight present quirk\n");
  12860. }
  12861. struct intel_quirk {
  12862. int device;
  12863. int subsystem_vendor;
  12864. int subsystem_device;
  12865. void (*hook)(struct drm_device *dev);
  12866. };
  12867. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12868. struct intel_dmi_quirk {
  12869. void (*hook)(struct drm_device *dev);
  12870. const struct dmi_system_id (*dmi_id_list)[];
  12871. };
  12872. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12873. {
  12874. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12875. return 1;
  12876. }
  12877. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12878. {
  12879. .dmi_id_list = &(const struct dmi_system_id[]) {
  12880. {
  12881. .callback = intel_dmi_reverse_brightness,
  12882. .ident = "NCR Corporation",
  12883. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12884. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12885. },
  12886. },
  12887. { } /* terminating entry */
  12888. },
  12889. .hook = quirk_invert_brightness,
  12890. },
  12891. };
  12892. static struct intel_quirk intel_quirks[] = {
  12893. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12894. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12895. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12896. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12897. /* 830 needs to leave pipe A & dpll A up */
  12898. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12899. /* 830 needs to leave pipe B & dpll B up */
  12900. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12901. /* Lenovo U160 cannot use SSC on LVDS */
  12902. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12903. /* Sony Vaio Y cannot use SSC on LVDS */
  12904. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12905. /* Acer Aspire 5734Z must invert backlight brightness */
  12906. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12907. /* Acer/eMachines G725 */
  12908. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12909. /* Acer/eMachines e725 */
  12910. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12911. /* Acer/Packard Bell NCL20 */
  12912. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12913. /* Acer Aspire 4736Z */
  12914. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12915. /* Acer Aspire 5336 */
  12916. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12917. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12918. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12919. /* Acer C720 Chromebook (Core i3 4005U) */
  12920. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12921. /* Apple Macbook 2,1 (Core 2 T7400) */
  12922. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12923. /* Apple Macbook 4,1 */
  12924. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12925. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12926. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12927. /* HP Chromebook 14 (Celeron 2955U) */
  12928. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12929. /* Dell Chromebook 11 */
  12930. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12931. /* Dell Chromebook 11 (2015 version) */
  12932. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12933. };
  12934. static void intel_init_quirks(struct drm_device *dev)
  12935. {
  12936. struct pci_dev *d = dev->pdev;
  12937. int i;
  12938. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12939. struct intel_quirk *q = &intel_quirks[i];
  12940. if (d->device == q->device &&
  12941. (d->subsystem_vendor == q->subsystem_vendor ||
  12942. q->subsystem_vendor == PCI_ANY_ID) &&
  12943. (d->subsystem_device == q->subsystem_device ||
  12944. q->subsystem_device == PCI_ANY_ID))
  12945. q->hook(dev);
  12946. }
  12947. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12948. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12949. intel_dmi_quirks[i].hook(dev);
  12950. }
  12951. }
  12952. /* Disable the VGA plane that we never use */
  12953. static void i915_disable_vga(struct drm_device *dev)
  12954. {
  12955. struct drm_i915_private *dev_priv = to_i915(dev);
  12956. u8 sr1;
  12957. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  12958. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12959. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12960. outb(SR01, VGA_SR_INDEX);
  12961. sr1 = inb(VGA_SR_DATA);
  12962. outb(sr1 | 1<<5, VGA_SR_DATA);
  12963. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12964. udelay(300);
  12965. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12966. POSTING_READ(vga_reg);
  12967. }
  12968. void intel_modeset_init_hw(struct drm_device *dev)
  12969. {
  12970. struct drm_i915_private *dev_priv = to_i915(dev);
  12971. intel_update_cdclk(dev);
  12972. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  12973. intel_init_clock_gating(dev);
  12974. }
  12975. /*
  12976. * Calculate what we think the watermarks should be for the state we've read
  12977. * out of the hardware and then immediately program those watermarks so that
  12978. * we ensure the hardware settings match our internal state.
  12979. *
  12980. * We can calculate what we think WM's should be by creating a duplicate of the
  12981. * current state (which was constructed during hardware readout) and running it
  12982. * through the atomic check code to calculate new watermark values in the
  12983. * state object.
  12984. */
  12985. static void sanitize_watermarks(struct drm_device *dev)
  12986. {
  12987. struct drm_i915_private *dev_priv = to_i915(dev);
  12988. struct drm_atomic_state *state;
  12989. struct drm_crtc *crtc;
  12990. struct drm_crtc_state *cstate;
  12991. struct drm_modeset_acquire_ctx ctx;
  12992. int ret;
  12993. int i;
  12994. /* Only supported on platforms that use atomic watermark design */
  12995. if (!dev_priv->display.optimize_watermarks)
  12996. return;
  12997. /*
  12998. * We need to hold connection_mutex before calling duplicate_state so
  12999. * that the connector loop is protected.
  13000. */
  13001. drm_modeset_acquire_init(&ctx, 0);
  13002. retry:
  13003. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13004. if (ret == -EDEADLK) {
  13005. drm_modeset_backoff(&ctx);
  13006. goto retry;
  13007. } else if (WARN_ON(ret)) {
  13008. goto fail;
  13009. }
  13010. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13011. if (WARN_ON(IS_ERR(state)))
  13012. goto fail;
  13013. /*
  13014. * Hardware readout is the only time we don't want to calculate
  13015. * intermediate watermarks (since we don't trust the current
  13016. * watermarks).
  13017. */
  13018. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13019. ret = intel_atomic_check(dev, state);
  13020. if (ret) {
  13021. /*
  13022. * If we fail here, it means that the hardware appears to be
  13023. * programmed in a way that shouldn't be possible, given our
  13024. * understanding of watermark requirements. This might mean a
  13025. * mistake in the hardware readout code or a mistake in the
  13026. * watermark calculations for a given platform. Raise a WARN
  13027. * so that this is noticeable.
  13028. *
  13029. * If this actually happens, we'll have to just leave the
  13030. * BIOS-programmed watermarks untouched and hope for the best.
  13031. */
  13032. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13033. goto fail;
  13034. }
  13035. /* Write calculated watermark values back */
  13036. for_each_crtc_in_state(state, crtc, cstate, i) {
  13037. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13038. cs->wm.need_postvbl_update = true;
  13039. dev_priv->display.optimize_watermarks(cs);
  13040. }
  13041. drm_atomic_state_free(state);
  13042. fail:
  13043. drm_modeset_drop_locks(&ctx);
  13044. drm_modeset_acquire_fini(&ctx);
  13045. }
  13046. void intel_modeset_init(struct drm_device *dev)
  13047. {
  13048. struct drm_i915_private *dev_priv = to_i915(dev);
  13049. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13050. int sprite, ret;
  13051. enum pipe pipe;
  13052. struct intel_crtc *crtc;
  13053. drm_mode_config_init(dev);
  13054. dev->mode_config.min_width = 0;
  13055. dev->mode_config.min_height = 0;
  13056. dev->mode_config.preferred_depth = 24;
  13057. dev->mode_config.prefer_shadow = 1;
  13058. dev->mode_config.allow_fb_modifiers = true;
  13059. dev->mode_config.funcs = &intel_mode_funcs;
  13060. intel_init_quirks(dev);
  13061. intel_init_pm(dev);
  13062. if (INTEL_INFO(dev)->num_pipes == 0)
  13063. return;
  13064. /*
  13065. * There may be no VBT; and if the BIOS enabled SSC we can
  13066. * just keep using it to avoid unnecessary flicker. Whereas if the
  13067. * BIOS isn't using it, don't assume it will work even if the VBT
  13068. * indicates as much.
  13069. */
  13070. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  13071. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13072. DREF_SSC1_ENABLE);
  13073. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13074. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13075. bios_lvds_use_ssc ? "en" : "dis",
  13076. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13077. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13078. }
  13079. }
  13080. if (IS_GEN2(dev)) {
  13081. dev->mode_config.max_width = 2048;
  13082. dev->mode_config.max_height = 2048;
  13083. } else if (IS_GEN3(dev)) {
  13084. dev->mode_config.max_width = 4096;
  13085. dev->mode_config.max_height = 4096;
  13086. } else {
  13087. dev->mode_config.max_width = 8192;
  13088. dev->mode_config.max_height = 8192;
  13089. }
  13090. if (IS_845G(dev) || IS_I865G(dev)) {
  13091. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  13092. dev->mode_config.cursor_height = 1023;
  13093. } else if (IS_GEN2(dev)) {
  13094. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13095. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13096. } else {
  13097. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13098. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13099. }
  13100. dev->mode_config.fb_base = ggtt->mappable_base;
  13101. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13102. INTEL_INFO(dev)->num_pipes,
  13103. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  13104. for_each_pipe(dev_priv, pipe) {
  13105. intel_crtc_init(dev, pipe);
  13106. for_each_sprite(dev_priv, pipe, sprite) {
  13107. ret = intel_plane_init(dev, pipe, sprite);
  13108. if (ret)
  13109. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  13110. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  13111. }
  13112. }
  13113. intel_update_czclk(dev_priv);
  13114. intel_update_cdclk(dev);
  13115. intel_shared_dpll_init(dev);
  13116. if (dev_priv->max_cdclk_freq == 0)
  13117. intel_update_max_cdclk(dev);
  13118. /* Just disable it once at startup */
  13119. i915_disable_vga(dev);
  13120. intel_setup_outputs(dev);
  13121. drm_modeset_lock_all(dev);
  13122. intel_modeset_setup_hw_state(dev);
  13123. drm_modeset_unlock_all(dev);
  13124. for_each_intel_crtc(dev, crtc) {
  13125. struct intel_initial_plane_config plane_config = {};
  13126. if (!crtc->active)
  13127. continue;
  13128. /*
  13129. * Note that reserving the BIOS fb up front prevents us
  13130. * from stuffing other stolen allocations like the ring
  13131. * on top. This prevents some ugliness at boot time, and
  13132. * can even allow for smooth boot transitions if the BIOS
  13133. * fb is large enough for the active pipe configuration.
  13134. */
  13135. dev_priv->display.get_initial_plane_config(crtc,
  13136. &plane_config);
  13137. /*
  13138. * If the fb is shared between multiple heads, we'll
  13139. * just get the first one.
  13140. */
  13141. intel_find_initial_plane_obj(crtc, &plane_config);
  13142. }
  13143. /*
  13144. * Make sure hardware watermarks really match the state we read out.
  13145. * Note that we need to do this after reconstructing the BIOS fb's
  13146. * since the watermark calculation done here will use pstate->fb.
  13147. */
  13148. sanitize_watermarks(dev);
  13149. }
  13150. static void intel_enable_pipe_a(struct drm_device *dev)
  13151. {
  13152. struct intel_connector *connector;
  13153. struct drm_connector *crt = NULL;
  13154. struct intel_load_detect_pipe load_detect_temp;
  13155. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13156. /* We can't just switch on the pipe A, we need to set things up with a
  13157. * proper mode and output configuration. As a gross hack, enable pipe A
  13158. * by enabling the load detect pipe once. */
  13159. for_each_intel_connector(dev, connector) {
  13160. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  13161. crt = &connector->base;
  13162. break;
  13163. }
  13164. }
  13165. if (!crt)
  13166. return;
  13167. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  13168. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  13169. }
  13170. static bool
  13171. intel_check_plane_mapping(struct intel_crtc *crtc)
  13172. {
  13173. struct drm_device *dev = crtc->base.dev;
  13174. struct drm_i915_private *dev_priv = to_i915(dev);
  13175. u32 val;
  13176. if (INTEL_INFO(dev)->num_pipes == 1)
  13177. return true;
  13178. val = I915_READ(DSPCNTR(!crtc->plane));
  13179. if ((val & DISPLAY_PLANE_ENABLE) &&
  13180. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13181. return false;
  13182. return true;
  13183. }
  13184. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13185. {
  13186. struct drm_device *dev = crtc->base.dev;
  13187. struct intel_encoder *encoder;
  13188. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13189. return true;
  13190. return false;
  13191. }
  13192. static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
  13193. {
  13194. struct drm_device *dev = encoder->base.dev;
  13195. struct intel_connector *connector;
  13196. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13197. return true;
  13198. return false;
  13199. }
  13200. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13201. {
  13202. struct drm_device *dev = crtc->base.dev;
  13203. struct drm_i915_private *dev_priv = to_i915(dev);
  13204. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13205. /* Clear any frame start delays used for debugging left by the BIOS */
  13206. if (!transcoder_is_dsi(cpu_transcoder)) {
  13207. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13208. I915_WRITE(reg,
  13209. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13210. }
  13211. /* restore vblank interrupts to correct state */
  13212. drm_crtc_vblank_reset(&crtc->base);
  13213. if (crtc->active) {
  13214. struct intel_plane *plane;
  13215. drm_crtc_vblank_on(&crtc->base);
  13216. /* Disable everything but the primary plane */
  13217. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13218. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13219. continue;
  13220. plane->disable_plane(&plane->base, &crtc->base);
  13221. }
  13222. }
  13223. /* We need to sanitize the plane -> pipe mapping first because this will
  13224. * disable the crtc (and hence change the state) if it is wrong. Note
  13225. * that gen4+ has a fixed plane -> pipe mapping. */
  13226. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  13227. bool plane;
  13228. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  13229. crtc->base.base.id, crtc->base.name);
  13230. /* Pipe has the wrong plane attached and the plane is active.
  13231. * Temporarily change the plane mapping and disable everything
  13232. * ... */
  13233. plane = crtc->plane;
  13234. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  13235. crtc->plane = !plane;
  13236. intel_crtc_disable_noatomic(&crtc->base);
  13237. crtc->plane = plane;
  13238. }
  13239. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13240. crtc->pipe == PIPE_A && !crtc->active) {
  13241. /* BIOS forgot to enable pipe A, this mostly happens after
  13242. * resume. Force-enable the pipe to fix this, the update_dpms
  13243. * call below we restore the pipe to the right state, but leave
  13244. * the required bits on. */
  13245. intel_enable_pipe_a(dev);
  13246. }
  13247. /* Adjust the state of the output pipe according to whether we
  13248. * have active connectors/encoders. */
  13249. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13250. intel_crtc_disable_noatomic(&crtc->base);
  13251. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  13252. /*
  13253. * We start out with underrun reporting disabled to avoid races.
  13254. * For correct bookkeeping mark this on active crtcs.
  13255. *
  13256. * Also on gmch platforms we dont have any hardware bits to
  13257. * disable the underrun reporting. Which means we need to start
  13258. * out with underrun reporting disabled also on inactive pipes,
  13259. * since otherwise we'll complain about the garbage we read when
  13260. * e.g. coming up after runtime pm.
  13261. *
  13262. * No protection against concurrent access is required - at
  13263. * worst a fifo underrun happens which also sets this to false.
  13264. */
  13265. crtc->cpu_fifo_underrun_disabled = true;
  13266. crtc->pch_fifo_underrun_disabled = true;
  13267. }
  13268. }
  13269. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13270. {
  13271. struct intel_connector *connector;
  13272. struct drm_device *dev = encoder->base.dev;
  13273. /* We need to check both for a crtc link (meaning that the
  13274. * encoder is active and trying to read from a pipe) and the
  13275. * pipe itself being active. */
  13276. bool has_active_crtc = encoder->base.crtc &&
  13277. to_intel_crtc(encoder->base.crtc)->active;
  13278. if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
  13279. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13280. encoder->base.base.id,
  13281. encoder->base.name);
  13282. /* Connector is active, but has no active pipe. This is
  13283. * fallout from our resume register restoring. Disable
  13284. * the encoder manually again. */
  13285. if (encoder->base.crtc) {
  13286. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13287. encoder->base.base.id,
  13288. encoder->base.name);
  13289. encoder->disable(encoder);
  13290. if (encoder->post_disable)
  13291. encoder->post_disable(encoder);
  13292. }
  13293. encoder->base.crtc = NULL;
  13294. /* Inconsistent output/port/pipe state happens presumably due to
  13295. * a bug in one of the get_hw_state functions. Or someplace else
  13296. * in our code, like the register restore mess on resume. Clamp
  13297. * things to off as a safer default. */
  13298. for_each_intel_connector(dev, connector) {
  13299. if (connector->encoder != encoder)
  13300. continue;
  13301. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13302. connector->base.encoder = NULL;
  13303. }
  13304. }
  13305. /* Enabled encoders without active connectors will be fixed in
  13306. * the crtc fixup. */
  13307. }
  13308. void i915_redisable_vga_power_on(struct drm_device *dev)
  13309. {
  13310. struct drm_i915_private *dev_priv = to_i915(dev);
  13311. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13312. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13313. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13314. i915_disable_vga(dev);
  13315. }
  13316. }
  13317. void i915_redisable_vga(struct drm_device *dev)
  13318. {
  13319. struct drm_i915_private *dev_priv = to_i915(dev);
  13320. /* This function can be called both from intel_modeset_setup_hw_state or
  13321. * at a very early point in our resume sequence, where the power well
  13322. * structures are not yet restored. Since this function is at a very
  13323. * paranoid "someone might have enabled VGA while we were not looking"
  13324. * level, just check if the power well is enabled instead of trying to
  13325. * follow the "don't touch the power well if we don't need it" policy
  13326. * the rest of the driver uses. */
  13327. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  13328. return;
  13329. i915_redisable_vga_power_on(dev);
  13330. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  13331. }
  13332. static bool primary_get_hw_state(struct intel_plane *plane)
  13333. {
  13334. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  13335. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  13336. }
  13337. /* FIXME read out full plane state for all planes */
  13338. static void readout_plane_state(struct intel_crtc *crtc)
  13339. {
  13340. struct drm_plane *primary = crtc->base.primary;
  13341. struct intel_plane_state *plane_state =
  13342. to_intel_plane_state(primary->state);
  13343. plane_state->visible = crtc->active &&
  13344. primary_get_hw_state(to_intel_plane(primary));
  13345. if (plane_state->visible)
  13346. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  13347. }
  13348. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13349. {
  13350. struct drm_i915_private *dev_priv = to_i915(dev);
  13351. enum pipe pipe;
  13352. struct intel_crtc *crtc;
  13353. struct intel_encoder *encoder;
  13354. struct intel_connector *connector;
  13355. int i;
  13356. dev_priv->active_crtcs = 0;
  13357. for_each_intel_crtc(dev, crtc) {
  13358. struct intel_crtc_state *crtc_state = crtc->config;
  13359. int pixclk = 0;
  13360. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  13361. memset(crtc_state, 0, sizeof(*crtc_state));
  13362. crtc_state->base.crtc = &crtc->base;
  13363. crtc_state->base.active = crtc_state->base.enable =
  13364. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13365. crtc->base.enabled = crtc_state->base.enable;
  13366. crtc->active = crtc_state->base.active;
  13367. if (crtc_state->base.active) {
  13368. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13369. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  13370. pixclk = ilk_pipe_pixel_rate(crtc_state);
  13371. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13372. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  13373. else
  13374. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  13375. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  13376. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  13377. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  13378. }
  13379. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  13380. readout_plane_state(crtc);
  13381. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  13382. crtc->base.base.id, crtc->base.name,
  13383. crtc->active ? "enabled" : "disabled");
  13384. }
  13385. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13386. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13387. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  13388. &pll->config.hw_state);
  13389. pll->config.crtc_mask = 0;
  13390. for_each_intel_crtc(dev, crtc) {
  13391. if (crtc->active && crtc->config->shared_dpll == pll)
  13392. pll->config.crtc_mask |= 1 << crtc->pipe;
  13393. }
  13394. pll->active_mask = pll->config.crtc_mask;
  13395. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13396. pll->name, pll->config.crtc_mask, pll->on);
  13397. }
  13398. for_each_intel_encoder(dev, encoder) {
  13399. pipe = 0;
  13400. if (encoder->get_hw_state(encoder, &pipe)) {
  13401. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13402. encoder->base.crtc = &crtc->base;
  13403. crtc->config->output_types |= 1 << encoder->type;
  13404. encoder->get_config(encoder, crtc->config);
  13405. } else {
  13406. encoder->base.crtc = NULL;
  13407. }
  13408. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13409. encoder->base.base.id,
  13410. encoder->base.name,
  13411. encoder->base.crtc ? "enabled" : "disabled",
  13412. pipe_name(pipe));
  13413. }
  13414. for_each_intel_connector(dev, connector) {
  13415. if (connector->get_hw_state(connector)) {
  13416. connector->base.dpms = DRM_MODE_DPMS_ON;
  13417. encoder = connector->encoder;
  13418. connector->base.encoder = &encoder->base;
  13419. if (encoder->base.crtc &&
  13420. encoder->base.crtc->state->active) {
  13421. /*
  13422. * This has to be done during hardware readout
  13423. * because anything calling .crtc_disable may
  13424. * rely on the connector_mask being accurate.
  13425. */
  13426. encoder->base.crtc->state->connector_mask |=
  13427. 1 << drm_connector_index(&connector->base);
  13428. encoder->base.crtc->state->encoder_mask |=
  13429. 1 << drm_encoder_index(&encoder->base);
  13430. }
  13431. } else {
  13432. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13433. connector->base.encoder = NULL;
  13434. }
  13435. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13436. connector->base.base.id,
  13437. connector->base.name,
  13438. connector->base.encoder ? "enabled" : "disabled");
  13439. }
  13440. for_each_intel_crtc(dev, crtc) {
  13441. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  13442. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13443. if (crtc->base.state->active) {
  13444. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  13445. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  13446. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13447. /*
  13448. * The initial mode needs to be set in order to keep
  13449. * the atomic core happy. It wants a valid mode if the
  13450. * crtc's enabled, so we do the above call.
  13451. *
  13452. * At this point some state updated by the connectors
  13453. * in their ->detect() callback has not run yet, so
  13454. * no recalculation can be done yet.
  13455. *
  13456. * Even if we could do a recalculation and modeset
  13457. * right now it would cause a double modeset if
  13458. * fbdev or userspace chooses a different initial mode.
  13459. *
  13460. * If that happens, someone indicated they wanted a
  13461. * mode change, which means it's safe to do a full
  13462. * recalculation.
  13463. */
  13464. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  13465. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  13466. update_scanline_offset(crtc);
  13467. }
  13468. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  13469. }
  13470. }
  13471. /* Scan out the current hw modeset state,
  13472. * and sanitizes it to the current state
  13473. */
  13474. static void
  13475. intel_modeset_setup_hw_state(struct drm_device *dev)
  13476. {
  13477. struct drm_i915_private *dev_priv = to_i915(dev);
  13478. enum pipe pipe;
  13479. struct intel_crtc *crtc;
  13480. struct intel_encoder *encoder;
  13481. int i;
  13482. intel_modeset_readout_hw_state(dev);
  13483. /* HW state is read out, now we need to sanitize this mess. */
  13484. for_each_intel_encoder(dev, encoder) {
  13485. intel_sanitize_encoder(encoder);
  13486. }
  13487. for_each_pipe(dev_priv, pipe) {
  13488. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13489. intel_sanitize_crtc(crtc);
  13490. intel_dump_pipe_config(crtc, crtc->config,
  13491. "[setup_hw_state]");
  13492. }
  13493. intel_modeset_update_connector_atomic_state(dev);
  13494. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13495. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13496. if (!pll->on || pll->active_mask)
  13497. continue;
  13498. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13499. pll->funcs.disable(dev_priv, pll);
  13500. pll->on = false;
  13501. }
  13502. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  13503. vlv_wm_get_hw_state(dev);
  13504. else if (IS_GEN9(dev))
  13505. skl_wm_get_hw_state(dev);
  13506. else if (HAS_PCH_SPLIT(dev))
  13507. ilk_wm_get_hw_state(dev);
  13508. for_each_intel_crtc(dev, crtc) {
  13509. unsigned long put_domains;
  13510. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13511. if (WARN_ON(put_domains))
  13512. modeset_put_power_domains(dev_priv, put_domains);
  13513. }
  13514. intel_display_set_init_power(dev_priv, false);
  13515. intel_fbc_init_pipe_state(dev_priv);
  13516. }
  13517. void intel_display_resume(struct drm_device *dev)
  13518. {
  13519. struct drm_i915_private *dev_priv = to_i915(dev);
  13520. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13521. struct drm_modeset_acquire_ctx ctx;
  13522. int ret;
  13523. bool setup = false;
  13524. dev_priv->modeset_restore_state = NULL;
  13525. /*
  13526. * This is a cludge because with real atomic modeset mode_config.mutex
  13527. * won't be taken. Unfortunately some probed state like
  13528. * audio_codec_enable is still protected by mode_config.mutex, so lock
  13529. * it here for now.
  13530. */
  13531. mutex_lock(&dev->mode_config.mutex);
  13532. drm_modeset_acquire_init(&ctx, 0);
  13533. retry:
  13534. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13535. if (ret == 0 && !setup) {
  13536. setup = true;
  13537. intel_modeset_setup_hw_state(dev);
  13538. i915_redisable_vga(dev);
  13539. }
  13540. if (ret == 0 && state) {
  13541. struct drm_crtc_state *crtc_state;
  13542. struct drm_crtc *crtc;
  13543. int i;
  13544. state->acquire_ctx = &ctx;
  13545. /* ignore any reset values/BIOS leftovers in the WM registers */
  13546. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13547. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  13548. /*
  13549. * Force recalculation even if we restore
  13550. * current state. With fast modeset this may not result
  13551. * in a modeset when the state is compatible.
  13552. */
  13553. crtc_state->mode_changed = true;
  13554. }
  13555. ret = drm_atomic_commit(state);
  13556. }
  13557. if (ret == -EDEADLK) {
  13558. drm_modeset_backoff(&ctx);
  13559. goto retry;
  13560. }
  13561. drm_modeset_drop_locks(&ctx);
  13562. drm_modeset_acquire_fini(&ctx);
  13563. mutex_unlock(&dev->mode_config.mutex);
  13564. if (ret) {
  13565. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13566. drm_atomic_state_free(state);
  13567. }
  13568. }
  13569. void intel_modeset_gem_init(struct drm_device *dev)
  13570. {
  13571. struct drm_i915_private *dev_priv = to_i915(dev);
  13572. struct drm_crtc *c;
  13573. struct drm_i915_gem_object *obj;
  13574. int ret;
  13575. intel_init_gt_powersave(dev_priv);
  13576. intel_modeset_init_hw(dev);
  13577. intel_setup_overlay(dev_priv);
  13578. /*
  13579. * Make sure any fbs we allocated at startup are properly
  13580. * pinned & fenced. When we do the allocation it's too early
  13581. * for this.
  13582. */
  13583. for_each_crtc(dev, c) {
  13584. obj = intel_fb_obj(c->primary->fb);
  13585. if (obj == NULL)
  13586. continue;
  13587. mutex_lock(&dev->struct_mutex);
  13588. ret = intel_pin_and_fence_fb_obj(c->primary->fb,
  13589. c->primary->state->rotation);
  13590. mutex_unlock(&dev->struct_mutex);
  13591. if (ret) {
  13592. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13593. to_intel_crtc(c)->pipe);
  13594. drm_framebuffer_unreference(c->primary->fb);
  13595. c->primary->fb = NULL;
  13596. c->primary->crtc = c->primary->state->crtc = NULL;
  13597. update_state_fb(c->primary);
  13598. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13599. }
  13600. }
  13601. }
  13602. int intel_connector_register(struct drm_connector *connector)
  13603. {
  13604. struct intel_connector *intel_connector = to_intel_connector(connector);
  13605. int ret;
  13606. ret = intel_backlight_device_register(intel_connector);
  13607. if (ret)
  13608. goto err;
  13609. return 0;
  13610. err:
  13611. return ret;
  13612. }
  13613. void intel_connector_unregister(struct drm_connector *connector)
  13614. {
  13615. struct intel_connector *intel_connector = to_intel_connector(connector);
  13616. intel_backlight_device_unregister(intel_connector);
  13617. intel_panel_destroy_backlight(connector);
  13618. }
  13619. void intel_modeset_cleanup(struct drm_device *dev)
  13620. {
  13621. struct drm_i915_private *dev_priv = to_i915(dev);
  13622. intel_disable_gt_powersave(dev_priv);
  13623. /*
  13624. * Interrupts and polling as the first thing to avoid creating havoc.
  13625. * Too much stuff here (turning of connectors, ...) would
  13626. * experience fancy races otherwise.
  13627. */
  13628. intel_irq_uninstall(dev_priv);
  13629. /*
  13630. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13631. * poll handlers. Hence disable polling after hpd handling is shut down.
  13632. */
  13633. drm_kms_helper_poll_fini(dev);
  13634. intel_unregister_dsm_handler();
  13635. intel_fbc_global_disable(dev_priv);
  13636. /* flush any delayed tasks or pending work */
  13637. flush_scheduled_work();
  13638. drm_mode_config_cleanup(dev);
  13639. intel_cleanup_overlay(dev_priv);
  13640. intel_cleanup_gt_powersave(dev_priv);
  13641. intel_teardown_gmbus(dev);
  13642. }
  13643. void intel_connector_attach_encoder(struct intel_connector *connector,
  13644. struct intel_encoder *encoder)
  13645. {
  13646. connector->encoder = encoder;
  13647. drm_mode_connector_attach_encoder(&connector->base,
  13648. &encoder->base);
  13649. }
  13650. /*
  13651. * set vga decode state - true == enable VGA decode
  13652. */
  13653. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13654. {
  13655. struct drm_i915_private *dev_priv = to_i915(dev);
  13656. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13657. u16 gmch_ctrl;
  13658. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13659. DRM_ERROR("failed to read control word\n");
  13660. return -EIO;
  13661. }
  13662. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13663. return 0;
  13664. if (state)
  13665. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13666. else
  13667. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13668. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13669. DRM_ERROR("failed to write control word\n");
  13670. return -EIO;
  13671. }
  13672. return 0;
  13673. }
  13674. struct intel_display_error_state {
  13675. u32 power_well_driver;
  13676. int num_transcoders;
  13677. struct intel_cursor_error_state {
  13678. u32 control;
  13679. u32 position;
  13680. u32 base;
  13681. u32 size;
  13682. } cursor[I915_MAX_PIPES];
  13683. struct intel_pipe_error_state {
  13684. bool power_domain_on;
  13685. u32 source;
  13686. u32 stat;
  13687. } pipe[I915_MAX_PIPES];
  13688. struct intel_plane_error_state {
  13689. u32 control;
  13690. u32 stride;
  13691. u32 size;
  13692. u32 pos;
  13693. u32 addr;
  13694. u32 surface;
  13695. u32 tile_offset;
  13696. } plane[I915_MAX_PIPES];
  13697. struct intel_transcoder_error_state {
  13698. bool power_domain_on;
  13699. enum transcoder cpu_transcoder;
  13700. u32 conf;
  13701. u32 htotal;
  13702. u32 hblank;
  13703. u32 hsync;
  13704. u32 vtotal;
  13705. u32 vblank;
  13706. u32 vsync;
  13707. } transcoder[4];
  13708. };
  13709. struct intel_display_error_state *
  13710. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13711. {
  13712. struct intel_display_error_state *error;
  13713. int transcoders[] = {
  13714. TRANSCODER_A,
  13715. TRANSCODER_B,
  13716. TRANSCODER_C,
  13717. TRANSCODER_EDP,
  13718. };
  13719. int i;
  13720. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13721. return NULL;
  13722. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13723. if (error == NULL)
  13724. return NULL;
  13725. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13726. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13727. for_each_pipe(dev_priv, i) {
  13728. error->pipe[i].power_domain_on =
  13729. __intel_display_power_is_enabled(dev_priv,
  13730. POWER_DOMAIN_PIPE(i));
  13731. if (!error->pipe[i].power_domain_on)
  13732. continue;
  13733. error->cursor[i].control = I915_READ(CURCNTR(i));
  13734. error->cursor[i].position = I915_READ(CURPOS(i));
  13735. error->cursor[i].base = I915_READ(CURBASE(i));
  13736. error->plane[i].control = I915_READ(DSPCNTR(i));
  13737. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13738. if (INTEL_GEN(dev_priv) <= 3) {
  13739. error->plane[i].size = I915_READ(DSPSIZE(i));
  13740. error->plane[i].pos = I915_READ(DSPPOS(i));
  13741. }
  13742. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13743. error->plane[i].addr = I915_READ(DSPADDR(i));
  13744. if (INTEL_GEN(dev_priv) >= 4) {
  13745. error->plane[i].surface = I915_READ(DSPSURF(i));
  13746. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13747. }
  13748. error->pipe[i].source = I915_READ(PIPESRC(i));
  13749. if (HAS_GMCH_DISPLAY(dev_priv))
  13750. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13751. }
  13752. /* Note: this does not include DSI transcoders. */
  13753. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13754. if (HAS_DDI(dev_priv))
  13755. error->num_transcoders++; /* Account for eDP. */
  13756. for (i = 0; i < error->num_transcoders; i++) {
  13757. enum transcoder cpu_transcoder = transcoders[i];
  13758. error->transcoder[i].power_domain_on =
  13759. __intel_display_power_is_enabled(dev_priv,
  13760. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13761. if (!error->transcoder[i].power_domain_on)
  13762. continue;
  13763. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13764. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13765. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13766. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13767. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13768. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13769. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13770. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13771. }
  13772. return error;
  13773. }
  13774. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13775. void
  13776. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13777. struct drm_device *dev,
  13778. struct intel_display_error_state *error)
  13779. {
  13780. struct drm_i915_private *dev_priv = to_i915(dev);
  13781. int i;
  13782. if (!error)
  13783. return;
  13784. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13785. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13786. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13787. error->power_well_driver);
  13788. for_each_pipe(dev_priv, i) {
  13789. err_printf(m, "Pipe [%d]:\n", i);
  13790. err_printf(m, " Power: %s\n",
  13791. onoff(error->pipe[i].power_domain_on));
  13792. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13793. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13794. err_printf(m, "Plane [%d]:\n", i);
  13795. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13796. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13797. if (INTEL_INFO(dev)->gen <= 3) {
  13798. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13799. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13800. }
  13801. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13802. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13803. if (INTEL_INFO(dev)->gen >= 4) {
  13804. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13805. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13806. }
  13807. err_printf(m, "Cursor [%d]:\n", i);
  13808. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13809. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13810. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13811. }
  13812. for (i = 0; i < error->num_transcoders; i++) {
  13813. err_printf(m, "CPU transcoder: %s\n",
  13814. transcoder_name(error->transcoder[i].cpu_transcoder));
  13815. err_printf(m, " Power: %s\n",
  13816. onoff(error->transcoder[i].power_domain_on));
  13817. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13818. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13819. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13820. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13821. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13822. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13823. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13824. }
  13825. }