intel_display.c 462 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_gem_dmabuf.h"
  39. #include "intel_dsi.h"
  40. #include "i915_trace.h"
  41. #include <drm/drm_atomic.h>
  42. #include <drm/drm_atomic_helper.h>
  43. #include <drm/drm_dp_helper.h>
  44. #include <drm/drm_crtc_helper.h>
  45. #include <drm/drm_plane_helper.h>
  46. #include <drm/drm_rect.h>
  47. #include <linux/dma_remapping.h>
  48. #include <linux/reservation.h>
  49. static bool is_mmio_work(struct intel_flip_work *work)
  50. {
  51. return work->mmio_work.func;
  52. }
  53. /* Primary plane formats for gen <= 3 */
  54. static const uint32_t i8xx_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB1555,
  58. DRM_FORMAT_XRGB8888,
  59. };
  60. /* Primary plane formats for gen >= 4 */
  61. static const uint32_t i965_primary_formats[] = {
  62. DRM_FORMAT_C8,
  63. DRM_FORMAT_RGB565,
  64. DRM_FORMAT_XRGB8888,
  65. DRM_FORMAT_XBGR8888,
  66. DRM_FORMAT_XRGB2101010,
  67. DRM_FORMAT_XBGR2101010,
  68. };
  69. static const uint32_t skl_primary_formats[] = {
  70. DRM_FORMAT_C8,
  71. DRM_FORMAT_RGB565,
  72. DRM_FORMAT_XRGB8888,
  73. DRM_FORMAT_XBGR8888,
  74. DRM_FORMAT_ARGB8888,
  75. DRM_FORMAT_ABGR8888,
  76. DRM_FORMAT_XRGB2101010,
  77. DRM_FORMAT_XBGR2101010,
  78. DRM_FORMAT_YUYV,
  79. DRM_FORMAT_YVYU,
  80. DRM_FORMAT_UYVY,
  81. DRM_FORMAT_VYUY,
  82. };
  83. /* Cursor formats */
  84. static const uint32_t intel_cursor_formats[] = {
  85. DRM_FORMAT_ARGB8888,
  86. };
  87. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  88. struct intel_crtc_state *pipe_config);
  89. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  90. struct intel_crtc_state *pipe_config);
  91. static int intel_framebuffer_init(struct drm_device *dev,
  92. struct intel_framebuffer *ifb,
  93. struct drm_mode_fb_cmd2 *mode_cmd,
  94. struct drm_i915_gem_object *obj);
  95. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  96. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  98. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  99. struct intel_link_m_n *m_n,
  100. struct intel_link_m_n *m2_n2);
  101. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  102. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  104. static void vlv_prepare_pll(struct intel_crtc *crtc,
  105. const struct intel_crtc_state *pipe_config);
  106. static void chv_prepare_pll(struct intel_crtc *crtc,
  107. const struct intel_crtc_state *pipe_config);
  108. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  109. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  111. struct intel_crtc_state *crtc_state);
  112. static void skylake_pfit_enable(struct intel_crtc *crtc);
  113. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  114. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  115. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  116. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  117. static int ilk_max_pixel_rate(struct drm_atomic_state *state);
  118. static int bxt_calc_cdclk(int max_pixclk);
  119. struct intel_limit {
  120. struct {
  121. int min, max;
  122. } dot, vco, n, m, m1, m2, p, p1;
  123. struct {
  124. int dot_limit;
  125. int p2_slow, p2_fast;
  126. } p2;
  127. };
  128. /* returns HPLL frequency in kHz */
  129. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  130. {
  131. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  132. /* Obtain SKU information */
  133. mutex_lock(&dev_priv->sb_lock);
  134. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  135. CCK_FUSE_HPLL_FREQ_MASK;
  136. mutex_unlock(&dev_priv->sb_lock);
  137. return vco_freq[hpll_freq] * 1000;
  138. }
  139. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  140. const char *name, u32 reg, int ref_freq)
  141. {
  142. u32 val;
  143. int divider;
  144. mutex_lock(&dev_priv->sb_lock);
  145. val = vlv_cck_read(dev_priv, reg);
  146. mutex_unlock(&dev_priv->sb_lock);
  147. divider = val & CCK_FREQUENCY_VALUES;
  148. WARN((val & CCK_FREQUENCY_STATUS) !=
  149. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  150. "%s change in progress\n", name);
  151. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  152. }
  153. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  154. const char *name, u32 reg)
  155. {
  156. if (dev_priv->hpll_freq == 0)
  157. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  158. return vlv_get_cck_clock(dev_priv, name, reg,
  159. dev_priv->hpll_freq);
  160. }
  161. static int
  162. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  163. {
  164. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  165. }
  166. static int
  167. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  168. {
  169. /* RAWCLK_FREQ_VLV register updated from power well code */
  170. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  171. CCK_DISPLAY_REF_CLOCK_CONTROL);
  172. }
  173. static int
  174. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  175. {
  176. uint32_t clkcfg;
  177. /* hrawclock is 1/4 the FSB frequency */
  178. clkcfg = I915_READ(CLKCFG);
  179. switch (clkcfg & CLKCFG_FSB_MASK) {
  180. case CLKCFG_FSB_400:
  181. return 100000;
  182. case CLKCFG_FSB_533:
  183. return 133333;
  184. case CLKCFG_FSB_667:
  185. return 166667;
  186. case CLKCFG_FSB_800:
  187. return 200000;
  188. case CLKCFG_FSB_1067:
  189. return 266667;
  190. case CLKCFG_FSB_1333:
  191. return 333333;
  192. /* these two are just a guess; one of them might be right */
  193. case CLKCFG_FSB_1600:
  194. case CLKCFG_FSB_1600_ALT:
  195. return 400000;
  196. default:
  197. return 133333;
  198. }
  199. }
  200. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  201. {
  202. if (HAS_PCH_SPLIT(dev_priv))
  203. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  204. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  205. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  206. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  207. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  208. else
  209. return; /* no rawclk on other platforms, or no need to know it */
  210. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  211. }
  212. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  213. {
  214. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  215. return;
  216. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  217. CCK_CZ_CLOCK_CONTROL);
  218. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  219. }
  220. static inline u32 /* units of 100MHz */
  221. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  222. const struct intel_crtc_state *pipe_config)
  223. {
  224. if (HAS_DDI(dev_priv))
  225. return pipe_config->port_clock; /* SPLL */
  226. else if (IS_GEN5(dev_priv))
  227. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  228. else
  229. return 270000;
  230. }
  231. static const struct intel_limit intel_limits_i8xx_dac = {
  232. .dot = { .min = 25000, .max = 350000 },
  233. .vco = { .min = 908000, .max = 1512000 },
  234. .n = { .min = 2, .max = 16 },
  235. .m = { .min = 96, .max = 140 },
  236. .m1 = { .min = 18, .max = 26 },
  237. .m2 = { .min = 6, .max = 16 },
  238. .p = { .min = 4, .max = 128 },
  239. .p1 = { .min = 2, .max = 33 },
  240. .p2 = { .dot_limit = 165000,
  241. .p2_slow = 4, .p2_fast = 2 },
  242. };
  243. static const struct intel_limit intel_limits_i8xx_dvo = {
  244. .dot = { .min = 25000, .max = 350000 },
  245. .vco = { .min = 908000, .max = 1512000 },
  246. .n = { .min = 2, .max = 16 },
  247. .m = { .min = 96, .max = 140 },
  248. .m1 = { .min = 18, .max = 26 },
  249. .m2 = { .min = 6, .max = 16 },
  250. .p = { .min = 4, .max = 128 },
  251. .p1 = { .min = 2, .max = 33 },
  252. .p2 = { .dot_limit = 165000,
  253. .p2_slow = 4, .p2_fast = 4 },
  254. };
  255. static const struct intel_limit intel_limits_i8xx_lvds = {
  256. .dot = { .min = 25000, .max = 350000 },
  257. .vco = { .min = 908000, .max = 1512000 },
  258. .n = { .min = 2, .max = 16 },
  259. .m = { .min = 96, .max = 140 },
  260. .m1 = { .min = 18, .max = 26 },
  261. .m2 = { .min = 6, .max = 16 },
  262. .p = { .min = 4, .max = 128 },
  263. .p1 = { .min = 1, .max = 6 },
  264. .p2 = { .dot_limit = 165000,
  265. .p2_slow = 14, .p2_fast = 7 },
  266. };
  267. static const struct intel_limit intel_limits_i9xx_sdvo = {
  268. .dot = { .min = 20000, .max = 400000 },
  269. .vco = { .min = 1400000, .max = 2800000 },
  270. .n = { .min = 1, .max = 6 },
  271. .m = { .min = 70, .max = 120 },
  272. .m1 = { .min = 8, .max = 18 },
  273. .m2 = { .min = 3, .max = 7 },
  274. .p = { .min = 5, .max = 80 },
  275. .p1 = { .min = 1, .max = 8 },
  276. .p2 = { .dot_limit = 200000,
  277. .p2_slow = 10, .p2_fast = 5 },
  278. };
  279. static const struct intel_limit intel_limits_i9xx_lvds = {
  280. .dot = { .min = 20000, .max = 400000 },
  281. .vco = { .min = 1400000, .max = 2800000 },
  282. .n = { .min = 1, .max = 6 },
  283. .m = { .min = 70, .max = 120 },
  284. .m1 = { .min = 8, .max = 18 },
  285. .m2 = { .min = 3, .max = 7 },
  286. .p = { .min = 7, .max = 98 },
  287. .p1 = { .min = 1, .max = 8 },
  288. .p2 = { .dot_limit = 112000,
  289. .p2_slow = 14, .p2_fast = 7 },
  290. };
  291. static const struct intel_limit intel_limits_g4x_sdvo = {
  292. .dot = { .min = 25000, .max = 270000 },
  293. .vco = { .min = 1750000, .max = 3500000},
  294. .n = { .min = 1, .max = 4 },
  295. .m = { .min = 104, .max = 138 },
  296. .m1 = { .min = 17, .max = 23 },
  297. .m2 = { .min = 5, .max = 11 },
  298. .p = { .min = 10, .max = 30 },
  299. .p1 = { .min = 1, .max = 3},
  300. .p2 = { .dot_limit = 270000,
  301. .p2_slow = 10,
  302. .p2_fast = 10
  303. },
  304. };
  305. static const struct intel_limit intel_limits_g4x_hdmi = {
  306. .dot = { .min = 22000, .max = 400000 },
  307. .vco = { .min = 1750000, .max = 3500000},
  308. .n = { .min = 1, .max = 4 },
  309. .m = { .min = 104, .max = 138 },
  310. .m1 = { .min = 16, .max = 23 },
  311. .m2 = { .min = 5, .max = 11 },
  312. .p = { .min = 5, .max = 80 },
  313. .p1 = { .min = 1, .max = 8},
  314. .p2 = { .dot_limit = 165000,
  315. .p2_slow = 10, .p2_fast = 5 },
  316. };
  317. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  318. .dot = { .min = 20000, .max = 115000 },
  319. .vco = { .min = 1750000, .max = 3500000 },
  320. .n = { .min = 1, .max = 3 },
  321. .m = { .min = 104, .max = 138 },
  322. .m1 = { .min = 17, .max = 23 },
  323. .m2 = { .min = 5, .max = 11 },
  324. .p = { .min = 28, .max = 112 },
  325. .p1 = { .min = 2, .max = 8 },
  326. .p2 = { .dot_limit = 0,
  327. .p2_slow = 14, .p2_fast = 14
  328. },
  329. };
  330. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  331. .dot = { .min = 80000, .max = 224000 },
  332. .vco = { .min = 1750000, .max = 3500000 },
  333. .n = { .min = 1, .max = 3 },
  334. .m = { .min = 104, .max = 138 },
  335. .m1 = { .min = 17, .max = 23 },
  336. .m2 = { .min = 5, .max = 11 },
  337. .p = { .min = 14, .max = 42 },
  338. .p1 = { .min = 2, .max = 6 },
  339. .p2 = { .dot_limit = 0,
  340. .p2_slow = 7, .p2_fast = 7
  341. },
  342. };
  343. static const struct intel_limit intel_limits_pineview_sdvo = {
  344. .dot = { .min = 20000, .max = 400000},
  345. .vco = { .min = 1700000, .max = 3500000 },
  346. /* Pineview's Ncounter is a ring counter */
  347. .n = { .min = 3, .max = 6 },
  348. .m = { .min = 2, .max = 256 },
  349. /* Pineview only has one combined m divider, which we treat as m2. */
  350. .m1 = { .min = 0, .max = 0 },
  351. .m2 = { .min = 0, .max = 254 },
  352. .p = { .min = 5, .max = 80 },
  353. .p1 = { .min = 1, .max = 8 },
  354. .p2 = { .dot_limit = 200000,
  355. .p2_slow = 10, .p2_fast = 5 },
  356. };
  357. static const struct intel_limit intel_limits_pineview_lvds = {
  358. .dot = { .min = 20000, .max = 400000 },
  359. .vco = { .min = 1700000, .max = 3500000 },
  360. .n = { .min = 3, .max = 6 },
  361. .m = { .min = 2, .max = 256 },
  362. .m1 = { .min = 0, .max = 0 },
  363. .m2 = { .min = 0, .max = 254 },
  364. .p = { .min = 7, .max = 112 },
  365. .p1 = { .min = 1, .max = 8 },
  366. .p2 = { .dot_limit = 112000,
  367. .p2_slow = 14, .p2_fast = 14 },
  368. };
  369. /* Ironlake / Sandybridge
  370. *
  371. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  372. * the range value for them is (actual_value - 2).
  373. */
  374. static const struct intel_limit intel_limits_ironlake_dac = {
  375. .dot = { .min = 25000, .max = 350000 },
  376. .vco = { .min = 1760000, .max = 3510000 },
  377. .n = { .min = 1, .max = 5 },
  378. .m = { .min = 79, .max = 127 },
  379. .m1 = { .min = 12, .max = 22 },
  380. .m2 = { .min = 5, .max = 9 },
  381. .p = { .min = 5, .max = 80 },
  382. .p1 = { .min = 1, .max = 8 },
  383. .p2 = { .dot_limit = 225000,
  384. .p2_slow = 10, .p2_fast = 5 },
  385. };
  386. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  387. .dot = { .min = 25000, .max = 350000 },
  388. .vco = { .min = 1760000, .max = 3510000 },
  389. .n = { .min = 1, .max = 3 },
  390. .m = { .min = 79, .max = 118 },
  391. .m1 = { .min = 12, .max = 22 },
  392. .m2 = { .min = 5, .max = 9 },
  393. .p = { .min = 28, .max = 112 },
  394. .p1 = { .min = 2, .max = 8 },
  395. .p2 = { .dot_limit = 225000,
  396. .p2_slow = 14, .p2_fast = 14 },
  397. };
  398. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  399. .dot = { .min = 25000, .max = 350000 },
  400. .vco = { .min = 1760000, .max = 3510000 },
  401. .n = { .min = 1, .max = 3 },
  402. .m = { .min = 79, .max = 127 },
  403. .m1 = { .min = 12, .max = 22 },
  404. .m2 = { .min = 5, .max = 9 },
  405. .p = { .min = 14, .max = 56 },
  406. .p1 = { .min = 2, .max = 8 },
  407. .p2 = { .dot_limit = 225000,
  408. .p2_slow = 7, .p2_fast = 7 },
  409. };
  410. /* LVDS 100mhz refclk limits. */
  411. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  412. .dot = { .min = 25000, .max = 350000 },
  413. .vco = { .min = 1760000, .max = 3510000 },
  414. .n = { .min = 1, .max = 2 },
  415. .m = { .min = 79, .max = 126 },
  416. .m1 = { .min = 12, .max = 22 },
  417. .m2 = { .min = 5, .max = 9 },
  418. .p = { .min = 28, .max = 112 },
  419. .p1 = { .min = 2, .max = 8 },
  420. .p2 = { .dot_limit = 225000,
  421. .p2_slow = 14, .p2_fast = 14 },
  422. };
  423. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  424. .dot = { .min = 25000, .max = 350000 },
  425. .vco = { .min = 1760000, .max = 3510000 },
  426. .n = { .min = 1, .max = 3 },
  427. .m = { .min = 79, .max = 126 },
  428. .m1 = { .min = 12, .max = 22 },
  429. .m2 = { .min = 5, .max = 9 },
  430. .p = { .min = 14, .max = 42 },
  431. .p1 = { .min = 2, .max = 6 },
  432. .p2 = { .dot_limit = 225000,
  433. .p2_slow = 7, .p2_fast = 7 },
  434. };
  435. static const struct intel_limit intel_limits_vlv = {
  436. /*
  437. * These are the data rate limits (measured in fast clocks)
  438. * since those are the strictest limits we have. The fast
  439. * clock and actual rate limits are more relaxed, so checking
  440. * them would make no difference.
  441. */
  442. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  443. .vco = { .min = 4000000, .max = 6000000 },
  444. .n = { .min = 1, .max = 7 },
  445. .m1 = { .min = 2, .max = 3 },
  446. .m2 = { .min = 11, .max = 156 },
  447. .p1 = { .min = 2, .max = 3 },
  448. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  449. };
  450. static const struct intel_limit intel_limits_chv = {
  451. /*
  452. * These are the data rate limits (measured in fast clocks)
  453. * since those are the strictest limits we have. The fast
  454. * clock and actual rate limits are more relaxed, so checking
  455. * them would make no difference.
  456. */
  457. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  458. .vco = { .min = 4800000, .max = 6480000 },
  459. .n = { .min = 1, .max = 1 },
  460. .m1 = { .min = 2, .max = 2 },
  461. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  462. .p1 = { .min = 2, .max = 4 },
  463. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  464. };
  465. static const struct intel_limit intel_limits_bxt = {
  466. /* FIXME: find real dot limits */
  467. .dot = { .min = 0, .max = INT_MAX },
  468. .vco = { .min = 4800000, .max = 6700000 },
  469. .n = { .min = 1, .max = 1 },
  470. .m1 = { .min = 2, .max = 2 },
  471. /* FIXME: find real m2 limits */
  472. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  473. .p1 = { .min = 2, .max = 4 },
  474. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  475. };
  476. static bool
  477. needs_modeset(struct drm_crtc_state *state)
  478. {
  479. return drm_atomic_crtc_needs_modeset(state);
  480. }
  481. /*
  482. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  483. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  484. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  485. * The helpers' return value is the rate of the clock that is fed to the
  486. * display engine's pipe which can be the above fast dot clock rate or a
  487. * divided-down version of it.
  488. */
  489. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  490. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  491. {
  492. clock->m = clock->m2 + 2;
  493. clock->p = clock->p1 * clock->p2;
  494. if (WARN_ON(clock->n == 0 || clock->p == 0))
  495. return 0;
  496. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  497. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  498. return clock->dot;
  499. }
  500. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  501. {
  502. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  503. }
  504. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  505. {
  506. clock->m = i9xx_dpll_compute_m(clock);
  507. clock->p = clock->p1 * clock->p2;
  508. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  509. return 0;
  510. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  511. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  512. return clock->dot;
  513. }
  514. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  515. {
  516. clock->m = clock->m1 * clock->m2;
  517. clock->p = clock->p1 * clock->p2;
  518. if (WARN_ON(clock->n == 0 || clock->p == 0))
  519. return 0;
  520. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  521. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  522. return clock->dot / 5;
  523. }
  524. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  525. {
  526. clock->m = clock->m1 * clock->m2;
  527. clock->p = clock->p1 * clock->p2;
  528. if (WARN_ON(clock->n == 0 || clock->p == 0))
  529. return 0;
  530. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  531. clock->n << 22);
  532. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  533. return clock->dot / 5;
  534. }
  535. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  536. /**
  537. * Returns whether the given set of divisors are valid for a given refclk with
  538. * the given connectors.
  539. */
  540. static bool intel_PLL_is_valid(struct drm_device *dev,
  541. const struct intel_limit *limit,
  542. const struct dpll *clock)
  543. {
  544. if (clock->n < limit->n.min || limit->n.max < clock->n)
  545. INTELPllInvalid("n out of range\n");
  546. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  547. INTELPllInvalid("p1 out of range\n");
  548. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  549. INTELPllInvalid("m2 out of range\n");
  550. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  551. INTELPllInvalid("m1 out of range\n");
  552. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
  553. !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
  554. if (clock->m1 <= clock->m2)
  555. INTELPllInvalid("m1 <= m2\n");
  556. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
  557. if (clock->p < limit->p.min || limit->p.max < clock->p)
  558. INTELPllInvalid("p out of range\n");
  559. if (clock->m < limit->m.min || limit->m.max < clock->m)
  560. INTELPllInvalid("m out of range\n");
  561. }
  562. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  563. INTELPllInvalid("vco out of range\n");
  564. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  565. * connector, etc., rather than just a single range.
  566. */
  567. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  568. INTELPllInvalid("dot out of range\n");
  569. return true;
  570. }
  571. static int
  572. i9xx_select_p2_div(const struct intel_limit *limit,
  573. const struct intel_crtc_state *crtc_state,
  574. int target)
  575. {
  576. struct drm_device *dev = crtc_state->base.crtc->dev;
  577. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  578. /*
  579. * For LVDS just rely on its current settings for dual-channel.
  580. * We haven't figured out how to reliably set up different
  581. * single/dual channel state, if we even can.
  582. */
  583. if (intel_is_dual_link_lvds(dev))
  584. return limit->p2.p2_fast;
  585. else
  586. return limit->p2.p2_slow;
  587. } else {
  588. if (target < limit->p2.dot_limit)
  589. return limit->p2.p2_slow;
  590. else
  591. return limit->p2.p2_fast;
  592. }
  593. }
  594. /*
  595. * Returns a set of divisors for the desired target clock with the given
  596. * refclk, or FALSE. The returned values represent the clock equation:
  597. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  598. *
  599. * Target and reference clocks are specified in kHz.
  600. *
  601. * If match_clock is provided, then best_clock P divider must match the P
  602. * divider from @match_clock used for LVDS downclocking.
  603. */
  604. static bool
  605. i9xx_find_best_dpll(const struct intel_limit *limit,
  606. struct intel_crtc_state *crtc_state,
  607. int target, int refclk, struct dpll *match_clock,
  608. struct dpll *best_clock)
  609. {
  610. struct drm_device *dev = crtc_state->base.crtc->dev;
  611. struct dpll clock;
  612. int err = target;
  613. memset(best_clock, 0, sizeof(*best_clock));
  614. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  615. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  616. clock.m1++) {
  617. for (clock.m2 = limit->m2.min;
  618. clock.m2 <= limit->m2.max; clock.m2++) {
  619. if (clock.m2 >= clock.m1)
  620. break;
  621. for (clock.n = limit->n.min;
  622. clock.n <= limit->n.max; clock.n++) {
  623. for (clock.p1 = limit->p1.min;
  624. clock.p1 <= limit->p1.max; clock.p1++) {
  625. int this_err;
  626. i9xx_calc_dpll_params(refclk, &clock);
  627. if (!intel_PLL_is_valid(dev, limit,
  628. &clock))
  629. continue;
  630. if (match_clock &&
  631. clock.p != match_clock->p)
  632. continue;
  633. this_err = abs(clock.dot - target);
  634. if (this_err < err) {
  635. *best_clock = clock;
  636. err = this_err;
  637. }
  638. }
  639. }
  640. }
  641. }
  642. return (err != target);
  643. }
  644. /*
  645. * Returns a set of divisors for the desired target clock with the given
  646. * refclk, or FALSE. The returned values represent the clock equation:
  647. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  648. *
  649. * Target and reference clocks are specified in kHz.
  650. *
  651. * If match_clock is provided, then best_clock P divider must match the P
  652. * divider from @match_clock used for LVDS downclocking.
  653. */
  654. static bool
  655. pnv_find_best_dpll(const struct intel_limit *limit,
  656. struct intel_crtc_state *crtc_state,
  657. int target, int refclk, struct dpll *match_clock,
  658. struct dpll *best_clock)
  659. {
  660. struct drm_device *dev = crtc_state->base.crtc->dev;
  661. struct dpll clock;
  662. int err = target;
  663. memset(best_clock, 0, sizeof(*best_clock));
  664. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  665. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  666. clock.m1++) {
  667. for (clock.m2 = limit->m2.min;
  668. clock.m2 <= limit->m2.max; clock.m2++) {
  669. for (clock.n = limit->n.min;
  670. clock.n <= limit->n.max; clock.n++) {
  671. for (clock.p1 = limit->p1.min;
  672. clock.p1 <= limit->p1.max; clock.p1++) {
  673. int this_err;
  674. pnv_calc_dpll_params(refclk, &clock);
  675. if (!intel_PLL_is_valid(dev, limit,
  676. &clock))
  677. continue;
  678. if (match_clock &&
  679. clock.p != match_clock->p)
  680. continue;
  681. this_err = abs(clock.dot - target);
  682. if (this_err < err) {
  683. *best_clock = clock;
  684. err = this_err;
  685. }
  686. }
  687. }
  688. }
  689. }
  690. return (err != target);
  691. }
  692. /*
  693. * Returns a set of divisors for the desired target clock with the given
  694. * refclk, or FALSE. The returned values represent the clock equation:
  695. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  696. *
  697. * Target and reference clocks are specified in kHz.
  698. *
  699. * If match_clock is provided, then best_clock P divider must match the P
  700. * divider from @match_clock used for LVDS downclocking.
  701. */
  702. static bool
  703. g4x_find_best_dpll(const struct intel_limit *limit,
  704. struct intel_crtc_state *crtc_state,
  705. int target, int refclk, struct dpll *match_clock,
  706. struct dpll *best_clock)
  707. {
  708. struct drm_device *dev = crtc_state->base.crtc->dev;
  709. struct dpll clock;
  710. int max_n;
  711. bool found = false;
  712. /* approximately equals target * 0.00585 */
  713. int err_most = (target >> 8) + (target >> 9);
  714. memset(best_clock, 0, sizeof(*best_clock));
  715. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  716. max_n = limit->n.max;
  717. /* based on hardware requirement, prefer smaller n to precision */
  718. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  719. /* based on hardware requirement, prefere larger m1,m2 */
  720. for (clock.m1 = limit->m1.max;
  721. clock.m1 >= limit->m1.min; clock.m1--) {
  722. for (clock.m2 = limit->m2.max;
  723. clock.m2 >= limit->m2.min; clock.m2--) {
  724. for (clock.p1 = limit->p1.max;
  725. clock.p1 >= limit->p1.min; clock.p1--) {
  726. int this_err;
  727. i9xx_calc_dpll_params(refclk, &clock);
  728. if (!intel_PLL_is_valid(dev, limit,
  729. &clock))
  730. continue;
  731. this_err = abs(clock.dot - target);
  732. if (this_err < err_most) {
  733. *best_clock = clock;
  734. err_most = this_err;
  735. max_n = clock.n;
  736. found = true;
  737. }
  738. }
  739. }
  740. }
  741. }
  742. return found;
  743. }
  744. /*
  745. * Check if the calculated PLL configuration is more optimal compared to the
  746. * best configuration and error found so far. Return the calculated error.
  747. */
  748. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  749. const struct dpll *calculated_clock,
  750. const struct dpll *best_clock,
  751. unsigned int best_error_ppm,
  752. unsigned int *error_ppm)
  753. {
  754. /*
  755. * For CHV ignore the error and consider only the P value.
  756. * Prefer a bigger P value based on HW requirements.
  757. */
  758. if (IS_CHERRYVIEW(dev)) {
  759. *error_ppm = 0;
  760. return calculated_clock->p > best_clock->p;
  761. }
  762. if (WARN_ON_ONCE(!target_freq))
  763. return false;
  764. *error_ppm = div_u64(1000000ULL *
  765. abs(target_freq - calculated_clock->dot),
  766. target_freq);
  767. /*
  768. * Prefer a better P value over a better (smaller) error if the error
  769. * is small. Ensure this preference for future configurations too by
  770. * setting the error to 0.
  771. */
  772. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  773. *error_ppm = 0;
  774. return true;
  775. }
  776. return *error_ppm + 10 < best_error_ppm;
  777. }
  778. /*
  779. * Returns a set of divisors for the desired target clock with the given
  780. * refclk, or FALSE. The returned values represent the clock equation:
  781. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  782. */
  783. static bool
  784. vlv_find_best_dpll(const struct intel_limit *limit,
  785. struct intel_crtc_state *crtc_state,
  786. int target, int refclk, struct dpll *match_clock,
  787. struct dpll *best_clock)
  788. {
  789. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  790. struct drm_device *dev = crtc->base.dev;
  791. struct dpll clock;
  792. unsigned int bestppm = 1000000;
  793. /* min update 19.2 MHz */
  794. int max_n = min(limit->n.max, refclk / 19200);
  795. bool found = false;
  796. target *= 5; /* fast clock */
  797. memset(best_clock, 0, sizeof(*best_clock));
  798. /* based on hardware requirement, prefer smaller n to precision */
  799. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  800. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  801. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  802. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  803. clock.p = clock.p1 * clock.p2;
  804. /* based on hardware requirement, prefer bigger m1,m2 values */
  805. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  806. unsigned int ppm;
  807. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  808. refclk * clock.m1);
  809. vlv_calc_dpll_params(refclk, &clock);
  810. if (!intel_PLL_is_valid(dev, limit,
  811. &clock))
  812. continue;
  813. if (!vlv_PLL_is_optimal(dev, target,
  814. &clock,
  815. best_clock,
  816. bestppm, &ppm))
  817. continue;
  818. *best_clock = clock;
  819. bestppm = ppm;
  820. found = true;
  821. }
  822. }
  823. }
  824. }
  825. return found;
  826. }
  827. /*
  828. * Returns a set of divisors for the desired target clock with the given
  829. * refclk, or FALSE. The returned values represent the clock equation:
  830. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  831. */
  832. static bool
  833. chv_find_best_dpll(const struct intel_limit *limit,
  834. struct intel_crtc_state *crtc_state,
  835. int target, int refclk, struct dpll *match_clock,
  836. struct dpll *best_clock)
  837. {
  838. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  839. struct drm_device *dev = crtc->base.dev;
  840. unsigned int best_error_ppm;
  841. struct dpll clock;
  842. uint64_t m2;
  843. int found = false;
  844. memset(best_clock, 0, sizeof(*best_clock));
  845. best_error_ppm = 1000000;
  846. /*
  847. * Based on hardware doc, the n always set to 1, and m1 always
  848. * set to 2. If requires to support 200Mhz refclk, we need to
  849. * revisit this because n may not 1 anymore.
  850. */
  851. clock.n = 1, clock.m1 = 2;
  852. target *= 5; /* fast clock */
  853. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  854. for (clock.p2 = limit->p2.p2_fast;
  855. clock.p2 >= limit->p2.p2_slow;
  856. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  857. unsigned int error_ppm;
  858. clock.p = clock.p1 * clock.p2;
  859. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  860. clock.n) << 22, refclk * clock.m1);
  861. if (m2 > INT_MAX/clock.m1)
  862. continue;
  863. clock.m2 = m2;
  864. chv_calc_dpll_params(refclk, &clock);
  865. if (!intel_PLL_is_valid(dev, limit, &clock))
  866. continue;
  867. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  868. best_error_ppm, &error_ppm))
  869. continue;
  870. *best_clock = clock;
  871. best_error_ppm = error_ppm;
  872. found = true;
  873. }
  874. }
  875. return found;
  876. }
  877. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  878. struct dpll *best_clock)
  879. {
  880. int refclk = 100000;
  881. const struct intel_limit *limit = &intel_limits_bxt;
  882. return chv_find_best_dpll(limit, crtc_state,
  883. target_clock, refclk, NULL, best_clock);
  884. }
  885. bool intel_crtc_active(struct drm_crtc *crtc)
  886. {
  887. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  888. /* Be paranoid as we can arrive here with only partial
  889. * state retrieved from the hardware during setup.
  890. *
  891. * We can ditch the adjusted_mode.crtc_clock check as soon
  892. * as Haswell has gained clock readout/fastboot support.
  893. *
  894. * We can ditch the crtc->primary->fb check as soon as we can
  895. * properly reconstruct framebuffers.
  896. *
  897. * FIXME: The intel_crtc->active here should be switched to
  898. * crtc->state->active once we have proper CRTC states wired up
  899. * for atomic.
  900. */
  901. return intel_crtc->active && crtc->primary->state->fb &&
  902. intel_crtc->config->base.adjusted_mode.crtc_clock;
  903. }
  904. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  905. enum pipe pipe)
  906. {
  907. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  908. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  909. return intel_crtc->config->cpu_transcoder;
  910. }
  911. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  912. {
  913. struct drm_i915_private *dev_priv = to_i915(dev);
  914. i915_reg_t reg = PIPEDSL(pipe);
  915. u32 line1, line2;
  916. u32 line_mask;
  917. if (IS_GEN2(dev))
  918. line_mask = DSL_LINEMASK_GEN2;
  919. else
  920. line_mask = DSL_LINEMASK_GEN3;
  921. line1 = I915_READ(reg) & line_mask;
  922. msleep(5);
  923. line2 = I915_READ(reg) & line_mask;
  924. return line1 == line2;
  925. }
  926. /*
  927. * intel_wait_for_pipe_off - wait for pipe to turn off
  928. * @crtc: crtc whose pipe to wait for
  929. *
  930. * After disabling a pipe, we can't wait for vblank in the usual way,
  931. * spinning on the vblank interrupt status bit, since we won't actually
  932. * see an interrupt when the pipe is disabled.
  933. *
  934. * On Gen4 and above:
  935. * wait for the pipe register state bit to turn off
  936. *
  937. * Otherwise:
  938. * wait for the display line value to settle (it usually
  939. * ends up stopping at the start of the next frame).
  940. *
  941. */
  942. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  943. {
  944. struct drm_device *dev = crtc->base.dev;
  945. struct drm_i915_private *dev_priv = to_i915(dev);
  946. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  947. enum pipe pipe = crtc->pipe;
  948. if (INTEL_INFO(dev)->gen >= 4) {
  949. i915_reg_t reg = PIPECONF(cpu_transcoder);
  950. /* Wait for the Pipe State to go off */
  951. if (intel_wait_for_register(dev_priv,
  952. reg, I965_PIPECONF_ACTIVE, 0,
  953. 100))
  954. WARN(1, "pipe_off wait timed out\n");
  955. } else {
  956. /* Wait for the display line to settle */
  957. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  958. WARN(1, "pipe_off wait timed out\n");
  959. }
  960. }
  961. /* Only for pre-ILK configs */
  962. void assert_pll(struct drm_i915_private *dev_priv,
  963. enum pipe pipe, bool state)
  964. {
  965. u32 val;
  966. bool cur_state;
  967. val = I915_READ(DPLL(pipe));
  968. cur_state = !!(val & DPLL_VCO_ENABLE);
  969. I915_STATE_WARN(cur_state != state,
  970. "PLL state assertion failure (expected %s, current %s)\n",
  971. onoff(state), onoff(cur_state));
  972. }
  973. /* XXX: the dsi pll is shared between MIPI DSI ports */
  974. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  975. {
  976. u32 val;
  977. bool cur_state;
  978. mutex_lock(&dev_priv->sb_lock);
  979. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  980. mutex_unlock(&dev_priv->sb_lock);
  981. cur_state = val & DSI_PLL_VCO_EN;
  982. I915_STATE_WARN(cur_state != state,
  983. "DSI PLL state assertion failure (expected %s, current %s)\n",
  984. onoff(state), onoff(cur_state));
  985. }
  986. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  987. enum pipe pipe, bool state)
  988. {
  989. bool cur_state;
  990. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  991. pipe);
  992. if (HAS_DDI(dev_priv)) {
  993. /* DDI does not have a specific FDI_TX register */
  994. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  995. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  996. } else {
  997. u32 val = I915_READ(FDI_TX_CTL(pipe));
  998. cur_state = !!(val & FDI_TX_ENABLE);
  999. }
  1000. I915_STATE_WARN(cur_state != state,
  1001. "FDI TX state assertion failure (expected %s, current %s)\n",
  1002. onoff(state), onoff(cur_state));
  1003. }
  1004. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1005. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1006. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1007. enum pipe pipe, bool state)
  1008. {
  1009. u32 val;
  1010. bool cur_state;
  1011. val = I915_READ(FDI_RX_CTL(pipe));
  1012. cur_state = !!(val & FDI_RX_ENABLE);
  1013. I915_STATE_WARN(cur_state != state,
  1014. "FDI RX state assertion failure (expected %s, current %s)\n",
  1015. onoff(state), onoff(cur_state));
  1016. }
  1017. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1018. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1019. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1020. enum pipe pipe)
  1021. {
  1022. u32 val;
  1023. /* ILK FDI PLL is always enabled */
  1024. if (IS_GEN5(dev_priv))
  1025. return;
  1026. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1027. if (HAS_DDI(dev_priv))
  1028. return;
  1029. val = I915_READ(FDI_TX_CTL(pipe));
  1030. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1031. }
  1032. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1033. enum pipe pipe, bool state)
  1034. {
  1035. u32 val;
  1036. bool cur_state;
  1037. val = I915_READ(FDI_RX_CTL(pipe));
  1038. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1039. I915_STATE_WARN(cur_state != state,
  1040. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1041. onoff(state), onoff(cur_state));
  1042. }
  1043. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1044. enum pipe pipe)
  1045. {
  1046. struct drm_device *dev = &dev_priv->drm;
  1047. i915_reg_t pp_reg;
  1048. u32 val;
  1049. enum pipe panel_pipe = PIPE_A;
  1050. bool locked = true;
  1051. if (WARN_ON(HAS_DDI(dev)))
  1052. return;
  1053. if (HAS_PCH_SPLIT(dev)) {
  1054. u32 port_sel;
  1055. pp_reg = PCH_PP_CONTROL;
  1056. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1057. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1058. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1059. panel_pipe = PIPE_B;
  1060. /* XXX: else fix for eDP */
  1061. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1062. /* presumably write lock depends on pipe, not port select */
  1063. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1064. panel_pipe = pipe;
  1065. } else {
  1066. pp_reg = PP_CONTROL;
  1067. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1068. panel_pipe = PIPE_B;
  1069. }
  1070. val = I915_READ(pp_reg);
  1071. if (!(val & PANEL_POWER_ON) ||
  1072. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1073. locked = false;
  1074. I915_STATE_WARN(panel_pipe == pipe && locked,
  1075. "panel assertion failure, pipe %c regs locked\n",
  1076. pipe_name(pipe));
  1077. }
  1078. static void assert_cursor(struct drm_i915_private *dev_priv,
  1079. enum pipe pipe, bool state)
  1080. {
  1081. struct drm_device *dev = &dev_priv->drm;
  1082. bool cur_state;
  1083. if (IS_845G(dev) || IS_I865G(dev))
  1084. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1085. else
  1086. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1087. I915_STATE_WARN(cur_state != state,
  1088. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1089. pipe_name(pipe), onoff(state), onoff(cur_state));
  1090. }
  1091. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1092. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1093. void assert_pipe(struct drm_i915_private *dev_priv,
  1094. enum pipe pipe, bool state)
  1095. {
  1096. bool cur_state;
  1097. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1098. pipe);
  1099. enum intel_display_power_domain power_domain;
  1100. /* if we need the pipe quirk it must be always on */
  1101. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1102. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1103. state = true;
  1104. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1105. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1106. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1107. cur_state = !!(val & PIPECONF_ENABLE);
  1108. intel_display_power_put(dev_priv, power_domain);
  1109. } else {
  1110. cur_state = false;
  1111. }
  1112. I915_STATE_WARN(cur_state != state,
  1113. "pipe %c assertion failure (expected %s, current %s)\n",
  1114. pipe_name(pipe), onoff(state), onoff(cur_state));
  1115. }
  1116. static void assert_plane(struct drm_i915_private *dev_priv,
  1117. enum plane plane, bool state)
  1118. {
  1119. u32 val;
  1120. bool cur_state;
  1121. val = I915_READ(DSPCNTR(plane));
  1122. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1123. I915_STATE_WARN(cur_state != state,
  1124. "plane %c assertion failure (expected %s, current %s)\n",
  1125. plane_name(plane), onoff(state), onoff(cur_state));
  1126. }
  1127. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1128. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1129. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1130. enum pipe pipe)
  1131. {
  1132. struct drm_device *dev = &dev_priv->drm;
  1133. int i;
  1134. /* Primary planes are fixed to pipes on gen4+ */
  1135. if (INTEL_INFO(dev)->gen >= 4) {
  1136. u32 val = I915_READ(DSPCNTR(pipe));
  1137. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1138. "plane %c assertion failure, should be disabled but not\n",
  1139. plane_name(pipe));
  1140. return;
  1141. }
  1142. /* Need to check both planes against the pipe */
  1143. for_each_pipe(dev_priv, i) {
  1144. u32 val = I915_READ(DSPCNTR(i));
  1145. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1146. DISPPLANE_SEL_PIPE_SHIFT;
  1147. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1148. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1149. plane_name(i), pipe_name(pipe));
  1150. }
  1151. }
  1152. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1153. enum pipe pipe)
  1154. {
  1155. struct drm_device *dev = &dev_priv->drm;
  1156. int sprite;
  1157. if (INTEL_INFO(dev)->gen >= 9) {
  1158. for_each_sprite(dev_priv, pipe, sprite) {
  1159. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1160. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1161. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1162. sprite, pipe_name(pipe));
  1163. }
  1164. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1165. for_each_sprite(dev_priv, pipe, sprite) {
  1166. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1167. I915_STATE_WARN(val & SP_ENABLE,
  1168. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1169. sprite_name(pipe, sprite), pipe_name(pipe));
  1170. }
  1171. } else if (INTEL_INFO(dev)->gen >= 7) {
  1172. u32 val = I915_READ(SPRCTL(pipe));
  1173. I915_STATE_WARN(val & SPRITE_ENABLE,
  1174. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1175. plane_name(pipe), pipe_name(pipe));
  1176. } else if (INTEL_INFO(dev)->gen >= 5) {
  1177. u32 val = I915_READ(DVSCNTR(pipe));
  1178. I915_STATE_WARN(val & DVS_ENABLE,
  1179. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1180. plane_name(pipe), pipe_name(pipe));
  1181. }
  1182. }
  1183. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1184. {
  1185. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1186. drm_crtc_vblank_put(crtc);
  1187. }
  1188. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1189. enum pipe pipe)
  1190. {
  1191. u32 val;
  1192. bool enabled;
  1193. val = I915_READ(PCH_TRANSCONF(pipe));
  1194. enabled = !!(val & TRANS_ENABLE);
  1195. I915_STATE_WARN(enabled,
  1196. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1197. pipe_name(pipe));
  1198. }
  1199. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1200. enum pipe pipe, u32 port_sel, u32 val)
  1201. {
  1202. if ((val & DP_PORT_EN) == 0)
  1203. return false;
  1204. if (HAS_PCH_CPT(dev_priv)) {
  1205. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1206. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1207. return false;
  1208. } else if (IS_CHERRYVIEW(dev_priv)) {
  1209. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1210. return false;
  1211. } else {
  1212. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1213. return false;
  1214. }
  1215. return true;
  1216. }
  1217. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1218. enum pipe pipe, u32 val)
  1219. {
  1220. if ((val & SDVO_ENABLE) == 0)
  1221. return false;
  1222. if (HAS_PCH_CPT(dev_priv)) {
  1223. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1224. return false;
  1225. } else if (IS_CHERRYVIEW(dev_priv)) {
  1226. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1227. return false;
  1228. } else {
  1229. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1230. return false;
  1231. }
  1232. return true;
  1233. }
  1234. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1235. enum pipe pipe, u32 val)
  1236. {
  1237. if ((val & LVDS_PORT_EN) == 0)
  1238. return false;
  1239. if (HAS_PCH_CPT(dev_priv)) {
  1240. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1241. return false;
  1242. } else {
  1243. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1244. return false;
  1245. }
  1246. return true;
  1247. }
  1248. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1249. enum pipe pipe, u32 val)
  1250. {
  1251. if ((val & ADPA_DAC_ENABLE) == 0)
  1252. return false;
  1253. if (HAS_PCH_CPT(dev_priv)) {
  1254. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1255. return false;
  1256. } else {
  1257. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1258. return false;
  1259. }
  1260. return true;
  1261. }
  1262. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1263. enum pipe pipe, i915_reg_t reg,
  1264. u32 port_sel)
  1265. {
  1266. u32 val = I915_READ(reg);
  1267. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1268. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1269. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1270. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1271. && (val & DP_PIPEB_SELECT),
  1272. "IBX PCH dp port still using transcoder B\n");
  1273. }
  1274. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1275. enum pipe pipe, i915_reg_t reg)
  1276. {
  1277. u32 val = I915_READ(reg);
  1278. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1279. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1280. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1281. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1282. && (val & SDVO_PIPE_B_SELECT),
  1283. "IBX PCH hdmi port still using transcoder B\n");
  1284. }
  1285. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1286. enum pipe pipe)
  1287. {
  1288. u32 val;
  1289. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1290. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1291. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1292. val = I915_READ(PCH_ADPA);
  1293. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1294. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1295. pipe_name(pipe));
  1296. val = I915_READ(PCH_LVDS);
  1297. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1298. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1299. pipe_name(pipe));
  1300. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1301. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1302. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1303. }
  1304. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1305. const struct intel_crtc_state *pipe_config)
  1306. {
  1307. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1308. enum pipe pipe = crtc->pipe;
  1309. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1310. POSTING_READ(DPLL(pipe));
  1311. udelay(150);
  1312. if (intel_wait_for_register(dev_priv,
  1313. DPLL(pipe),
  1314. DPLL_LOCK_VLV,
  1315. DPLL_LOCK_VLV,
  1316. 1))
  1317. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1318. }
  1319. static void vlv_enable_pll(struct intel_crtc *crtc,
  1320. const struct intel_crtc_state *pipe_config)
  1321. {
  1322. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1323. enum pipe pipe = crtc->pipe;
  1324. assert_pipe_disabled(dev_priv, pipe);
  1325. /* PLL is protected by panel, make sure we can write it */
  1326. assert_panel_unlocked(dev_priv, pipe);
  1327. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1328. _vlv_enable_pll(crtc, pipe_config);
  1329. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1330. POSTING_READ(DPLL_MD(pipe));
  1331. }
  1332. static void _chv_enable_pll(struct intel_crtc *crtc,
  1333. const struct intel_crtc_state *pipe_config)
  1334. {
  1335. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1336. enum pipe pipe = crtc->pipe;
  1337. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1338. u32 tmp;
  1339. mutex_lock(&dev_priv->sb_lock);
  1340. /* Enable back the 10bit clock to display controller */
  1341. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1342. tmp |= DPIO_DCLKP_EN;
  1343. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1344. mutex_unlock(&dev_priv->sb_lock);
  1345. /*
  1346. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1347. */
  1348. udelay(1);
  1349. /* Enable PLL */
  1350. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1351. /* Check PLL is locked */
  1352. if (intel_wait_for_register(dev_priv,
  1353. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1354. 1))
  1355. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1356. }
  1357. static void chv_enable_pll(struct intel_crtc *crtc,
  1358. const struct intel_crtc_state *pipe_config)
  1359. {
  1360. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1361. enum pipe pipe = crtc->pipe;
  1362. assert_pipe_disabled(dev_priv, pipe);
  1363. /* PLL is protected by panel, make sure we can write it */
  1364. assert_panel_unlocked(dev_priv, pipe);
  1365. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1366. _chv_enable_pll(crtc, pipe_config);
  1367. if (pipe != PIPE_A) {
  1368. /*
  1369. * WaPixelRepeatModeFixForC0:chv
  1370. *
  1371. * DPLLCMD is AWOL. Use chicken bits to propagate
  1372. * the value from DPLLBMD to either pipe B or C.
  1373. */
  1374. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1375. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1376. I915_WRITE(CBR4_VLV, 0);
  1377. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1378. /*
  1379. * DPLLB VGA mode also seems to cause problems.
  1380. * We should always have it disabled.
  1381. */
  1382. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1383. } else {
  1384. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1385. POSTING_READ(DPLL_MD(pipe));
  1386. }
  1387. }
  1388. static int intel_num_dvo_pipes(struct drm_device *dev)
  1389. {
  1390. struct intel_crtc *crtc;
  1391. int count = 0;
  1392. for_each_intel_crtc(dev, crtc) {
  1393. count += crtc->base.state->active &&
  1394. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1395. }
  1396. return count;
  1397. }
  1398. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1399. {
  1400. struct drm_device *dev = crtc->base.dev;
  1401. struct drm_i915_private *dev_priv = to_i915(dev);
  1402. i915_reg_t reg = DPLL(crtc->pipe);
  1403. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1404. assert_pipe_disabled(dev_priv, crtc->pipe);
  1405. /* PLL is protected by panel, make sure we can write it */
  1406. if (IS_MOBILE(dev) && !IS_I830(dev))
  1407. assert_panel_unlocked(dev_priv, crtc->pipe);
  1408. /* Enable DVO 2x clock on both PLLs if necessary */
  1409. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1410. /*
  1411. * It appears to be important that we don't enable this
  1412. * for the current pipe before otherwise configuring the
  1413. * PLL. No idea how this should be handled if multiple
  1414. * DVO outputs are enabled simultaneosly.
  1415. */
  1416. dpll |= DPLL_DVO_2X_MODE;
  1417. I915_WRITE(DPLL(!crtc->pipe),
  1418. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1419. }
  1420. /*
  1421. * Apparently we need to have VGA mode enabled prior to changing
  1422. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1423. * dividers, even though the register value does change.
  1424. */
  1425. I915_WRITE(reg, 0);
  1426. I915_WRITE(reg, dpll);
  1427. /* Wait for the clocks to stabilize. */
  1428. POSTING_READ(reg);
  1429. udelay(150);
  1430. if (INTEL_INFO(dev)->gen >= 4) {
  1431. I915_WRITE(DPLL_MD(crtc->pipe),
  1432. crtc->config->dpll_hw_state.dpll_md);
  1433. } else {
  1434. /* The pixel multiplier can only be updated once the
  1435. * DPLL is enabled and the clocks are stable.
  1436. *
  1437. * So write it again.
  1438. */
  1439. I915_WRITE(reg, dpll);
  1440. }
  1441. /* We do this three times for luck */
  1442. I915_WRITE(reg, dpll);
  1443. POSTING_READ(reg);
  1444. udelay(150); /* wait for warmup */
  1445. I915_WRITE(reg, dpll);
  1446. POSTING_READ(reg);
  1447. udelay(150); /* wait for warmup */
  1448. I915_WRITE(reg, dpll);
  1449. POSTING_READ(reg);
  1450. udelay(150); /* wait for warmup */
  1451. }
  1452. /**
  1453. * i9xx_disable_pll - disable a PLL
  1454. * @dev_priv: i915 private structure
  1455. * @pipe: pipe PLL to disable
  1456. *
  1457. * Disable the PLL for @pipe, making sure the pipe is off first.
  1458. *
  1459. * Note! This is for pre-ILK only.
  1460. */
  1461. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1462. {
  1463. struct drm_device *dev = crtc->base.dev;
  1464. struct drm_i915_private *dev_priv = to_i915(dev);
  1465. enum pipe pipe = crtc->pipe;
  1466. /* Disable DVO 2x clock on both PLLs if necessary */
  1467. if (IS_I830(dev) &&
  1468. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1469. !intel_num_dvo_pipes(dev)) {
  1470. I915_WRITE(DPLL(PIPE_B),
  1471. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1472. I915_WRITE(DPLL(PIPE_A),
  1473. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1474. }
  1475. /* Don't disable pipe or pipe PLLs if needed */
  1476. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1477. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1478. return;
  1479. /* Make sure the pipe isn't still relying on us */
  1480. assert_pipe_disabled(dev_priv, pipe);
  1481. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1482. POSTING_READ(DPLL(pipe));
  1483. }
  1484. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1485. {
  1486. u32 val;
  1487. /* Make sure the pipe isn't still relying on us */
  1488. assert_pipe_disabled(dev_priv, pipe);
  1489. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1490. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1491. if (pipe != PIPE_A)
  1492. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1493. I915_WRITE(DPLL(pipe), val);
  1494. POSTING_READ(DPLL(pipe));
  1495. }
  1496. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1497. {
  1498. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1499. u32 val;
  1500. /* Make sure the pipe isn't still relying on us */
  1501. assert_pipe_disabled(dev_priv, pipe);
  1502. val = DPLL_SSC_REF_CLK_CHV |
  1503. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1504. if (pipe != PIPE_A)
  1505. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1506. I915_WRITE(DPLL(pipe), val);
  1507. POSTING_READ(DPLL(pipe));
  1508. mutex_lock(&dev_priv->sb_lock);
  1509. /* Disable 10bit clock to display controller */
  1510. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1511. val &= ~DPIO_DCLKP_EN;
  1512. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1513. mutex_unlock(&dev_priv->sb_lock);
  1514. }
  1515. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1516. struct intel_digital_port *dport,
  1517. unsigned int expected_mask)
  1518. {
  1519. u32 port_mask;
  1520. i915_reg_t dpll_reg;
  1521. switch (dport->port) {
  1522. case PORT_B:
  1523. port_mask = DPLL_PORTB_READY_MASK;
  1524. dpll_reg = DPLL(0);
  1525. break;
  1526. case PORT_C:
  1527. port_mask = DPLL_PORTC_READY_MASK;
  1528. dpll_reg = DPLL(0);
  1529. expected_mask <<= 4;
  1530. break;
  1531. case PORT_D:
  1532. port_mask = DPLL_PORTD_READY_MASK;
  1533. dpll_reg = DPIO_PHY_STATUS;
  1534. break;
  1535. default:
  1536. BUG();
  1537. }
  1538. if (intel_wait_for_register(dev_priv,
  1539. dpll_reg, port_mask, expected_mask,
  1540. 1000))
  1541. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1542. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1543. }
  1544. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1545. enum pipe pipe)
  1546. {
  1547. struct drm_device *dev = &dev_priv->drm;
  1548. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1549. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1550. i915_reg_t reg;
  1551. uint32_t val, pipeconf_val;
  1552. /* Make sure PCH DPLL is enabled */
  1553. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1554. /* FDI must be feeding us bits for PCH ports */
  1555. assert_fdi_tx_enabled(dev_priv, pipe);
  1556. assert_fdi_rx_enabled(dev_priv, pipe);
  1557. if (HAS_PCH_CPT(dev)) {
  1558. /* Workaround: Set the timing override bit before enabling the
  1559. * pch transcoder. */
  1560. reg = TRANS_CHICKEN2(pipe);
  1561. val = I915_READ(reg);
  1562. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1563. I915_WRITE(reg, val);
  1564. }
  1565. reg = PCH_TRANSCONF(pipe);
  1566. val = I915_READ(reg);
  1567. pipeconf_val = I915_READ(PIPECONF(pipe));
  1568. if (HAS_PCH_IBX(dev_priv)) {
  1569. /*
  1570. * Make the BPC in transcoder be consistent with
  1571. * that in pipeconf reg. For HDMI we must use 8bpc
  1572. * here for both 8bpc and 12bpc.
  1573. */
  1574. val &= ~PIPECONF_BPC_MASK;
  1575. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1576. val |= PIPECONF_8BPC;
  1577. else
  1578. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1579. }
  1580. val &= ~TRANS_INTERLACE_MASK;
  1581. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1582. if (HAS_PCH_IBX(dev_priv) &&
  1583. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1584. val |= TRANS_LEGACY_INTERLACED_ILK;
  1585. else
  1586. val |= TRANS_INTERLACED;
  1587. else
  1588. val |= TRANS_PROGRESSIVE;
  1589. I915_WRITE(reg, val | TRANS_ENABLE);
  1590. if (intel_wait_for_register(dev_priv,
  1591. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1592. 100))
  1593. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1594. }
  1595. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1596. enum transcoder cpu_transcoder)
  1597. {
  1598. u32 val, pipeconf_val;
  1599. /* FDI must be feeding us bits for PCH ports */
  1600. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1601. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1602. /* Workaround: set timing override bit. */
  1603. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1604. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1605. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1606. val = TRANS_ENABLE;
  1607. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1608. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1609. PIPECONF_INTERLACED_ILK)
  1610. val |= TRANS_INTERLACED;
  1611. else
  1612. val |= TRANS_PROGRESSIVE;
  1613. I915_WRITE(LPT_TRANSCONF, val);
  1614. if (intel_wait_for_register(dev_priv,
  1615. LPT_TRANSCONF,
  1616. TRANS_STATE_ENABLE,
  1617. TRANS_STATE_ENABLE,
  1618. 100))
  1619. DRM_ERROR("Failed to enable PCH transcoder\n");
  1620. }
  1621. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1622. enum pipe pipe)
  1623. {
  1624. struct drm_device *dev = &dev_priv->drm;
  1625. i915_reg_t reg;
  1626. uint32_t val;
  1627. /* FDI relies on the transcoder */
  1628. assert_fdi_tx_disabled(dev_priv, pipe);
  1629. assert_fdi_rx_disabled(dev_priv, pipe);
  1630. /* Ports must be off as well */
  1631. assert_pch_ports_disabled(dev_priv, pipe);
  1632. reg = PCH_TRANSCONF(pipe);
  1633. val = I915_READ(reg);
  1634. val &= ~TRANS_ENABLE;
  1635. I915_WRITE(reg, val);
  1636. /* wait for PCH transcoder off, transcoder state */
  1637. if (intel_wait_for_register(dev_priv,
  1638. reg, TRANS_STATE_ENABLE, 0,
  1639. 50))
  1640. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1641. if (HAS_PCH_CPT(dev)) {
  1642. /* Workaround: Clear the timing override chicken bit again. */
  1643. reg = TRANS_CHICKEN2(pipe);
  1644. val = I915_READ(reg);
  1645. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1646. I915_WRITE(reg, val);
  1647. }
  1648. }
  1649. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1650. {
  1651. u32 val;
  1652. val = I915_READ(LPT_TRANSCONF);
  1653. val &= ~TRANS_ENABLE;
  1654. I915_WRITE(LPT_TRANSCONF, val);
  1655. /* wait for PCH transcoder off, transcoder state */
  1656. if (intel_wait_for_register(dev_priv,
  1657. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1658. 50))
  1659. DRM_ERROR("Failed to disable PCH transcoder\n");
  1660. /* Workaround: clear timing override bit. */
  1661. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1662. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1663. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1664. }
  1665. /**
  1666. * intel_enable_pipe - enable a pipe, asserting requirements
  1667. * @crtc: crtc responsible for the pipe
  1668. *
  1669. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1670. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1671. */
  1672. static void intel_enable_pipe(struct intel_crtc *crtc)
  1673. {
  1674. struct drm_device *dev = crtc->base.dev;
  1675. struct drm_i915_private *dev_priv = to_i915(dev);
  1676. enum pipe pipe = crtc->pipe;
  1677. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1678. enum pipe pch_transcoder;
  1679. i915_reg_t reg;
  1680. u32 val;
  1681. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1682. assert_planes_disabled(dev_priv, pipe);
  1683. assert_cursor_disabled(dev_priv, pipe);
  1684. assert_sprites_disabled(dev_priv, pipe);
  1685. if (HAS_PCH_LPT(dev_priv))
  1686. pch_transcoder = TRANSCODER_A;
  1687. else
  1688. pch_transcoder = pipe;
  1689. /*
  1690. * A pipe without a PLL won't actually be able to drive bits from
  1691. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1692. * need the check.
  1693. */
  1694. if (HAS_GMCH_DISPLAY(dev_priv))
  1695. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1696. assert_dsi_pll_enabled(dev_priv);
  1697. else
  1698. assert_pll_enabled(dev_priv, pipe);
  1699. else {
  1700. if (crtc->config->has_pch_encoder) {
  1701. /* if driving the PCH, we need FDI enabled */
  1702. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1703. assert_fdi_tx_pll_enabled(dev_priv,
  1704. (enum pipe) cpu_transcoder);
  1705. }
  1706. /* FIXME: assert CPU port conditions for SNB+ */
  1707. }
  1708. reg = PIPECONF(cpu_transcoder);
  1709. val = I915_READ(reg);
  1710. if (val & PIPECONF_ENABLE) {
  1711. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1712. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1713. return;
  1714. }
  1715. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1716. POSTING_READ(reg);
  1717. /*
  1718. * Until the pipe starts DSL will read as 0, which would cause
  1719. * an apparent vblank timestamp jump, which messes up also the
  1720. * frame count when it's derived from the timestamps. So let's
  1721. * wait for the pipe to start properly before we call
  1722. * drm_crtc_vblank_on()
  1723. */
  1724. if (dev->max_vblank_count == 0 &&
  1725. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1726. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1727. }
  1728. /**
  1729. * intel_disable_pipe - disable a pipe, asserting requirements
  1730. * @crtc: crtc whose pipes is to be disabled
  1731. *
  1732. * Disable the pipe of @crtc, making sure that various hardware
  1733. * specific requirements are met, if applicable, e.g. plane
  1734. * disabled, panel fitter off, etc.
  1735. *
  1736. * Will wait until the pipe has shut down before returning.
  1737. */
  1738. static void intel_disable_pipe(struct intel_crtc *crtc)
  1739. {
  1740. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1741. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1742. enum pipe pipe = crtc->pipe;
  1743. i915_reg_t reg;
  1744. u32 val;
  1745. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1746. /*
  1747. * Make sure planes won't keep trying to pump pixels to us,
  1748. * or we might hang the display.
  1749. */
  1750. assert_planes_disabled(dev_priv, pipe);
  1751. assert_cursor_disabled(dev_priv, pipe);
  1752. assert_sprites_disabled(dev_priv, pipe);
  1753. reg = PIPECONF(cpu_transcoder);
  1754. val = I915_READ(reg);
  1755. if ((val & PIPECONF_ENABLE) == 0)
  1756. return;
  1757. /*
  1758. * Double wide has implications for planes
  1759. * so best keep it disabled when not needed.
  1760. */
  1761. if (crtc->config->double_wide)
  1762. val &= ~PIPECONF_DOUBLE_WIDE;
  1763. /* Don't disable pipe or pipe PLLs if needed */
  1764. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1765. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1766. val &= ~PIPECONF_ENABLE;
  1767. I915_WRITE(reg, val);
  1768. if ((val & PIPECONF_ENABLE) == 0)
  1769. intel_wait_for_pipe_off(crtc);
  1770. }
  1771. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1772. {
  1773. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1774. }
  1775. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1776. uint64_t fb_modifier, unsigned int cpp)
  1777. {
  1778. switch (fb_modifier) {
  1779. case DRM_FORMAT_MOD_NONE:
  1780. return cpp;
  1781. case I915_FORMAT_MOD_X_TILED:
  1782. if (IS_GEN2(dev_priv))
  1783. return 128;
  1784. else
  1785. return 512;
  1786. case I915_FORMAT_MOD_Y_TILED:
  1787. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1788. return 128;
  1789. else
  1790. return 512;
  1791. case I915_FORMAT_MOD_Yf_TILED:
  1792. switch (cpp) {
  1793. case 1:
  1794. return 64;
  1795. case 2:
  1796. case 4:
  1797. return 128;
  1798. case 8:
  1799. case 16:
  1800. return 256;
  1801. default:
  1802. MISSING_CASE(cpp);
  1803. return cpp;
  1804. }
  1805. break;
  1806. default:
  1807. MISSING_CASE(fb_modifier);
  1808. return cpp;
  1809. }
  1810. }
  1811. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1812. uint64_t fb_modifier, unsigned int cpp)
  1813. {
  1814. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1815. return 1;
  1816. else
  1817. return intel_tile_size(dev_priv) /
  1818. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1819. }
  1820. /* Return the tile dimensions in pixel units */
  1821. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1822. unsigned int *tile_width,
  1823. unsigned int *tile_height,
  1824. uint64_t fb_modifier,
  1825. unsigned int cpp)
  1826. {
  1827. unsigned int tile_width_bytes =
  1828. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1829. *tile_width = tile_width_bytes / cpp;
  1830. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1831. }
  1832. unsigned int
  1833. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1834. uint32_t pixel_format, uint64_t fb_modifier)
  1835. {
  1836. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1837. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1838. return ALIGN(height, tile_height);
  1839. }
  1840. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1841. {
  1842. unsigned int size = 0;
  1843. int i;
  1844. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1845. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1846. return size;
  1847. }
  1848. static void
  1849. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1850. const struct drm_framebuffer *fb,
  1851. unsigned int rotation)
  1852. {
  1853. if (intel_rotation_90_or_270(rotation)) {
  1854. *view = i915_ggtt_view_rotated;
  1855. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1856. } else {
  1857. *view = i915_ggtt_view_normal;
  1858. }
  1859. }
  1860. static void
  1861. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  1862. struct drm_framebuffer *fb)
  1863. {
  1864. struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
  1865. unsigned int tile_size, tile_width, tile_height, cpp;
  1866. tile_size = intel_tile_size(dev_priv);
  1867. cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  1868. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1869. fb->modifier[0], cpp);
  1870. info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
  1871. info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
  1872. if (info->pixel_format == DRM_FORMAT_NV12) {
  1873. cpp = drm_format_plane_cpp(fb->pixel_format, 1);
  1874. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1875. fb->modifier[1], cpp);
  1876. info->uv_offset = fb->offsets[1];
  1877. info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
  1878. info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
  1879. }
  1880. }
  1881. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1882. {
  1883. if (INTEL_INFO(dev_priv)->gen >= 9)
  1884. return 256 * 1024;
  1885. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1886. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1887. return 128 * 1024;
  1888. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1889. return 4 * 1024;
  1890. else
  1891. return 0;
  1892. }
  1893. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1894. uint64_t fb_modifier)
  1895. {
  1896. switch (fb_modifier) {
  1897. case DRM_FORMAT_MOD_NONE:
  1898. return intel_linear_alignment(dev_priv);
  1899. case I915_FORMAT_MOD_X_TILED:
  1900. if (INTEL_INFO(dev_priv)->gen >= 9)
  1901. return 256 * 1024;
  1902. return 0;
  1903. case I915_FORMAT_MOD_Y_TILED:
  1904. case I915_FORMAT_MOD_Yf_TILED:
  1905. return 1 * 1024 * 1024;
  1906. default:
  1907. MISSING_CASE(fb_modifier);
  1908. return 0;
  1909. }
  1910. }
  1911. int
  1912. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1913. unsigned int rotation)
  1914. {
  1915. struct drm_device *dev = fb->dev;
  1916. struct drm_i915_private *dev_priv = to_i915(dev);
  1917. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1918. struct i915_ggtt_view view;
  1919. u32 alignment;
  1920. int ret;
  1921. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1922. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  1923. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1924. /* Note that the w/a also requires 64 PTE of padding following the
  1925. * bo. We currently fill all unused PTE with the shadow page and so
  1926. * we should always have valid PTE following the scanout preventing
  1927. * the VT-d warning.
  1928. */
  1929. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1930. alignment = 256 * 1024;
  1931. /*
  1932. * Global gtt pte registers are special registers which actually forward
  1933. * writes to a chunk of system memory. Which means that there is no risk
  1934. * that the register values disappear as soon as we call
  1935. * intel_runtime_pm_put(), so it is correct to wrap only the
  1936. * pin/unpin/fence and not more.
  1937. */
  1938. intel_runtime_pm_get(dev_priv);
  1939. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  1940. &view);
  1941. if (ret)
  1942. goto err_pm;
  1943. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1944. * fence, whereas 965+ only requires a fence if using
  1945. * framebuffer compression. For simplicity, we always install
  1946. * a fence as the cost is not that onerous.
  1947. */
  1948. if (view.type == I915_GGTT_VIEW_NORMAL) {
  1949. ret = i915_gem_object_get_fence(obj);
  1950. if (ret == -EDEADLK) {
  1951. /*
  1952. * -EDEADLK means there are no free fences
  1953. * no pending flips.
  1954. *
  1955. * This is propagated to atomic, but it uses
  1956. * -EDEADLK to force a locking recovery, so
  1957. * change the returned error to -EBUSY.
  1958. */
  1959. ret = -EBUSY;
  1960. goto err_unpin;
  1961. } else if (ret)
  1962. goto err_unpin;
  1963. i915_gem_object_pin_fence(obj);
  1964. }
  1965. intel_runtime_pm_put(dev_priv);
  1966. return 0;
  1967. err_unpin:
  1968. i915_gem_object_unpin_from_display_plane(obj, &view);
  1969. err_pm:
  1970. intel_runtime_pm_put(dev_priv);
  1971. return ret;
  1972. }
  1973. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1974. {
  1975. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1976. struct i915_ggtt_view view;
  1977. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1978. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1979. if (view.type == I915_GGTT_VIEW_NORMAL)
  1980. i915_gem_object_unpin_fence(obj);
  1981. i915_gem_object_unpin_from_display_plane(obj, &view);
  1982. }
  1983. /*
  1984. * Adjust the tile offset by moving the difference into
  1985. * the x/y offsets.
  1986. *
  1987. * Input tile dimensions and pitch must already be
  1988. * rotated to match x and y, and in pixel units.
  1989. */
  1990. static u32 intel_adjust_tile_offset(int *x, int *y,
  1991. unsigned int tile_width,
  1992. unsigned int tile_height,
  1993. unsigned int tile_size,
  1994. unsigned int pitch_tiles,
  1995. u32 old_offset,
  1996. u32 new_offset)
  1997. {
  1998. unsigned int tiles;
  1999. WARN_ON(old_offset & (tile_size - 1));
  2000. WARN_ON(new_offset & (tile_size - 1));
  2001. WARN_ON(new_offset > old_offset);
  2002. tiles = (old_offset - new_offset) / tile_size;
  2003. *y += tiles / pitch_tiles * tile_height;
  2004. *x += tiles % pitch_tiles * tile_width;
  2005. return new_offset;
  2006. }
  2007. /*
  2008. * Computes the linear offset to the base tile and adjusts
  2009. * x, y. bytes per pixel is assumed to be a power-of-two.
  2010. *
  2011. * In the 90/270 rotated case, x and y are assumed
  2012. * to be already rotated to match the rotated GTT view, and
  2013. * pitch is the tile_height aligned framebuffer height.
  2014. */
  2015. u32 intel_compute_tile_offset(int *x, int *y,
  2016. const struct drm_framebuffer *fb, int plane,
  2017. unsigned int pitch,
  2018. unsigned int rotation)
  2019. {
  2020. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2021. uint64_t fb_modifier = fb->modifier[plane];
  2022. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2023. u32 offset, offset_aligned, alignment;
  2024. alignment = intel_surf_alignment(dev_priv, fb_modifier);
  2025. if (alignment)
  2026. alignment--;
  2027. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2028. unsigned int tile_size, tile_width, tile_height;
  2029. unsigned int tile_rows, tiles, pitch_tiles;
  2030. tile_size = intel_tile_size(dev_priv);
  2031. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2032. fb_modifier, cpp);
  2033. if (intel_rotation_90_or_270(rotation)) {
  2034. pitch_tiles = pitch / tile_height;
  2035. swap(tile_width, tile_height);
  2036. } else {
  2037. pitch_tiles = pitch / (tile_width * cpp);
  2038. }
  2039. tile_rows = *y / tile_height;
  2040. *y %= tile_height;
  2041. tiles = *x / tile_width;
  2042. *x %= tile_width;
  2043. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2044. offset_aligned = offset & ~alignment;
  2045. intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2046. tile_size, pitch_tiles,
  2047. offset, offset_aligned);
  2048. } else {
  2049. offset = *y * pitch + *x * cpp;
  2050. offset_aligned = offset & ~alignment;
  2051. *y = (offset & alignment) / pitch;
  2052. *x = ((offset & alignment) - *y * pitch) / cpp;
  2053. }
  2054. return offset_aligned;
  2055. }
  2056. static int i9xx_format_to_fourcc(int format)
  2057. {
  2058. switch (format) {
  2059. case DISPPLANE_8BPP:
  2060. return DRM_FORMAT_C8;
  2061. case DISPPLANE_BGRX555:
  2062. return DRM_FORMAT_XRGB1555;
  2063. case DISPPLANE_BGRX565:
  2064. return DRM_FORMAT_RGB565;
  2065. default:
  2066. case DISPPLANE_BGRX888:
  2067. return DRM_FORMAT_XRGB8888;
  2068. case DISPPLANE_RGBX888:
  2069. return DRM_FORMAT_XBGR8888;
  2070. case DISPPLANE_BGRX101010:
  2071. return DRM_FORMAT_XRGB2101010;
  2072. case DISPPLANE_RGBX101010:
  2073. return DRM_FORMAT_XBGR2101010;
  2074. }
  2075. }
  2076. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2077. {
  2078. switch (format) {
  2079. case PLANE_CTL_FORMAT_RGB_565:
  2080. return DRM_FORMAT_RGB565;
  2081. default:
  2082. case PLANE_CTL_FORMAT_XRGB_8888:
  2083. if (rgb_order) {
  2084. if (alpha)
  2085. return DRM_FORMAT_ABGR8888;
  2086. else
  2087. return DRM_FORMAT_XBGR8888;
  2088. } else {
  2089. if (alpha)
  2090. return DRM_FORMAT_ARGB8888;
  2091. else
  2092. return DRM_FORMAT_XRGB8888;
  2093. }
  2094. case PLANE_CTL_FORMAT_XRGB_2101010:
  2095. if (rgb_order)
  2096. return DRM_FORMAT_XBGR2101010;
  2097. else
  2098. return DRM_FORMAT_XRGB2101010;
  2099. }
  2100. }
  2101. static bool
  2102. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2103. struct intel_initial_plane_config *plane_config)
  2104. {
  2105. struct drm_device *dev = crtc->base.dev;
  2106. struct drm_i915_private *dev_priv = to_i915(dev);
  2107. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2108. struct drm_i915_gem_object *obj = NULL;
  2109. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2110. struct drm_framebuffer *fb = &plane_config->fb->base;
  2111. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2112. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2113. PAGE_SIZE);
  2114. size_aligned -= base_aligned;
  2115. if (plane_config->size == 0)
  2116. return false;
  2117. /* If the FB is too big, just don't use it since fbdev is not very
  2118. * important and we should probably use that space with FBC or other
  2119. * features. */
  2120. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2121. return false;
  2122. mutex_lock(&dev->struct_mutex);
  2123. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2124. base_aligned,
  2125. base_aligned,
  2126. size_aligned);
  2127. if (!obj) {
  2128. mutex_unlock(&dev->struct_mutex);
  2129. return false;
  2130. }
  2131. obj->tiling_mode = plane_config->tiling;
  2132. if (obj->tiling_mode == I915_TILING_X)
  2133. obj->stride = fb->pitches[0];
  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. drm_gem_object_unreference(&obj->base);
  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 (obj->tiling_mode != I915_TILING_NONE)
  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. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2244. }
  2245. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2246. const struct intel_crtc_state *crtc_state,
  2247. const struct intel_plane_state *plane_state)
  2248. {
  2249. struct drm_device *dev = primary->dev;
  2250. struct drm_i915_private *dev_priv = to_i915(dev);
  2251. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2252. struct drm_framebuffer *fb = plane_state->base.fb;
  2253. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2254. int plane = intel_crtc->plane;
  2255. u32 linear_offset;
  2256. u32 dspcntr;
  2257. i915_reg_t reg = DSPCNTR(plane);
  2258. unsigned int rotation = plane_state->base.rotation;
  2259. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2260. int x = plane_state->src.x1 >> 16;
  2261. int y = plane_state->src.y1 >> 16;
  2262. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2263. dspcntr |= DISPLAY_PLANE_ENABLE;
  2264. if (INTEL_INFO(dev)->gen < 4) {
  2265. if (intel_crtc->pipe == PIPE_B)
  2266. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2267. /* pipesrc and dspsize control the size that is scaled from,
  2268. * which should always be the user's requested size.
  2269. */
  2270. I915_WRITE(DSPSIZE(plane),
  2271. ((crtc_state->pipe_src_h - 1) << 16) |
  2272. (crtc_state->pipe_src_w - 1));
  2273. I915_WRITE(DSPPOS(plane), 0);
  2274. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2275. I915_WRITE(PRIMSIZE(plane),
  2276. ((crtc_state->pipe_src_h - 1) << 16) |
  2277. (crtc_state->pipe_src_w - 1));
  2278. I915_WRITE(PRIMPOS(plane), 0);
  2279. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2280. }
  2281. switch (fb->pixel_format) {
  2282. case DRM_FORMAT_C8:
  2283. dspcntr |= DISPPLANE_8BPP;
  2284. break;
  2285. case DRM_FORMAT_XRGB1555:
  2286. dspcntr |= DISPPLANE_BGRX555;
  2287. break;
  2288. case DRM_FORMAT_RGB565:
  2289. dspcntr |= DISPPLANE_BGRX565;
  2290. break;
  2291. case DRM_FORMAT_XRGB8888:
  2292. dspcntr |= DISPPLANE_BGRX888;
  2293. break;
  2294. case DRM_FORMAT_XBGR8888:
  2295. dspcntr |= DISPPLANE_RGBX888;
  2296. break;
  2297. case DRM_FORMAT_XRGB2101010:
  2298. dspcntr |= DISPPLANE_BGRX101010;
  2299. break;
  2300. case DRM_FORMAT_XBGR2101010:
  2301. dspcntr |= DISPPLANE_RGBX101010;
  2302. break;
  2303. default:
  2304. BUG();
  2305. }
  2306. if (INTEL_INFO(dev)->gen >= 4 &&
  2307. obj->tiling_mode != I915_TILING_NONE)
  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 (obj->tiling_mode != I915_TILING_NONE)
  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. to_intel_plane_state(plane->base.state);
  2667. if (plane_state->visible)
  2668. plane->update_plane(&plane->base,
  2669. to_intel_crtc_state(crtc->state),
  2670. plane_state);
  2671. }
  2672. }
  2673. static int
  2674. __intel_display_resume(struct drm_device *dev,
  2675. struct drm_atomic_state *state)
  2676. {
  2677. struct drm_crtc_state *crtc_state;
  2678. struct drm_crtc *crtc;
  2679. int i, ret;
  2680. intel_modeset_setup_hw_state(dev);
  2681. i915_redisable_vga(dev);
  2682. if (!state)
  2683. return 0;
  2684. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  2685. /*
  2686. * Force recalculation even if we restore
  2687. * current state. With fast modeset this may not result
  2688. * in a modeset when the state is compatible.
  2689. */
  2690. crtc_state->mode_changed = true;
  2691. }
  2692. /* ignore any reset values/BIOS leftovers in the WM registers */
  2693. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  2694. ret = drm_atomic_commit(state);
  2695. WARN_ON(ret == -EDEADLK);
  2696. return ret;
  2697. }
  2698. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  2699. {
  2700. struct drm_device *dev = &dev_priv->drm;
  2701. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  2702. struct drm_atomic_state *state;
  2703. int ret;
  2704. /* no reset support for gen2 */
  2705. if (IS_GEN2(dev_priv))
  2706. return;
  2707. /*
  2708. * Need mode_config.mutex so that we don't
  2709. * trample ongoing ->detect() and whatnot.
  2710. */
  2711. mutex_lock(&dev->mode_config.mutex);
  2712. drm_modeset_acquire_init(ctx, 0);
  2713. while (1) {
  2714. ret = drm_modeset_lock_all_ctx(dev, ctx);
  2715. if (ret != -EDEADLK)
  2716. break;
  2717. drm_modeset_backoff(ctx);
  2718. }
  2719. /* reset doesn't touch the display, but flips might get nuked anyway, */
  2720. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  2721. return;
  2722. /*
  2723. * Disabling the crtcs gracefully seems nicer. Also the
  2724. * g33 docs say we should at least disable all the planes.
  2725. */
  2726. state = drm_atomic_helper_duplicate_state(dev, ctx);
  2727. if (IS_ERR(state)) {
  2728. ret = PTR_ERR(state);
  2729. state = NULL;
  2730. DRM_ERROR("Duplicating state failed with %i\n", ret);
  2731. goto err;
  2732. }
  2733. ret = drm_atomic_helper_disable_all(dev, ctx);
  2734. if (ret) {
  2735. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  2736. goto err;
  2737. }
  2738. dev_priv->modeset_restore_state = state;
  2739. state->acquire_ctx = ctx;
  2740. return;
  2741. err:
  2742. drm_atomic_state_free(state);
  2743. }
  2744. void intel_finish_reset(struct drm_i915_private *dev_priv)
  2745. {
  2746. struct drm_device *dev = &dev_priv->drm;
  2747. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  2748. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  2749. int ret;
  2750. /*
  2751. * Flips in the rings will be nuked by the reset,
  2752. * so complete all pending flips so that user space
  2753. * will get its events and not get stuck.
  2754. */
  2755. intel_complete_page_flips(dev_priv);
  2756. /* no reset support for gen2 */
  2757. if (IS_GEN2(dev_priv))
  2758. return;
  2759. dev_priv->modeset_restore_state = NULL;
  2760. /* reset doesn't touch the display */
  2761. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
  2762. /*
  2763. * Flips in the rings have been nuked by the reset,
  2764. * so update the base address of all primary
  2765. * planes to the the last fb to make sure we're
  2766. * showing the correct fb after a reset.
  2767. *
  2768. * FIXME: Atomic will make this obsolete since we won't schedule
  2769. * CS-based flips (which might get lost in gpu resets) any more.
  2770. */
  2771. intel_update_primary_planes(dev);
  2772. } else {
  2773. /*
  2774. * The display has been reset as well,
  2775. * so need a full re-initialization.
  2776. */
  2777. intel_runtime_pm_disable_interrupts(dev_priv);
  2778. intel_runtime_pm_enable_interrupts(dev_priv);
  2779. intel_modeset_init_hw(dev);
  2780. spin_lock_irq(&dev_priv->irq_lock);
  2781. if (dev_priv->display.hpd_irq_setup)
  2782. dev_priv->display.hpd_irq_setup(dev_priv);
  2783. spin_unlock_irq(&dev_priv->irq_lock);
  2784. ret = __intel_display_resume(dev, state);
  2785. if (ret)
  2786. DRM_ERROR("Restoring old state failed with %i\n", ret);
  2787. intel_hpd_init(dev_priv);
  2788. }
  2789. drm_modeset_drop_locks(ctx);
  2790. drm_modeset_acquire_fini(ctx);
  2791. mutex_unlock(&dev->mode_config.mutex);
  2792. }
  2793. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2794. {
  2795. struct drm_device *dev = crtc->dev;
  2796. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2797. unsigned reset_counter;
  2798. bool pending;
  2799. reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
  2800. if (intel_crtc->reset_counter != reset_counter)
  2801. return false;
  2802. spin_lock_irq(&dev->event_lock);
  2803. pending = to_intel_crtc(crtc)->flip_work != NULL;
  2804. spin_unlock_irq(&dev->event_lock);
  2805. return pending;
  2806. }
  2807. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2808. struct intel_crtc_state *old_crtc_state)
  2809. {
  2810. struct drm_device *dev = crtc->base.dev;
  2811. struct drm_i915_private *dev_priv = to_i915(dev);
  2812. struct intel_crtc_state *pipe_config =
  2813. to_intel_crtc_state(crtc->base.state);
  2814. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2815. crtc->base.mode = crtc->base.state->mode;
  2816. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2817. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2818. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2819. /*
  2820. * Update pipe size and adjust fitter if needed: the reason for this is
  2821. * that in compute_mode_changes we check the native mode (not the pfit
  2822. * mode) to see if we can flip rather than do a full mode set. In the
  2823. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2824. * pfit state, we'll end up with a big fb scanned out into the wrong
  2825. * sized surface.
  2826. */
  2827. I915_WRITE(PIPESRC(crtc->pipe),
  2828. ((pipe_config->pipe_src_w - 1) << 16) |
  2829. (pipe_config->pipe_src_h - 1));
  2830. /* on skylake this is done by detaching scalers */
  2831. if (INTEL_INFO(dev)->gen >= 9) {
  2832. skl_detach_scalers(crtc);
  2833. if (pipe_config->pch_pfit.enabled)
  2834. skylake_pfit_enable(crtc);
  2835. } else if (HAS_PCH_SPLIT(dev)) {
  2836. if (pipe_config->pch_pfit.enabled)
  2837. ironlake_pfit_enable(crtc);
  2838. else if (old_crtc_state->pch_pfit.enabled)
  2839. ironlake_pfit_disable(crtc, true);
  2840. }
  2841. }
  2842. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2843. {
  2844. struct drm_device *dev = crtc->dev;
  2845. struct drm_i915_private *dev_priv = to_i915(dev);
  2846. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2847. int pipe = intel_crtc->pipe;
  2848. i915_reg_t reg;
  2849. u32 temp;
  2850. /* enable normal train */
  2851. reg = FDI_TX_CTL(pipe);
  2852. temp = I915_READ(reg);
  2853. if (IS_IVYBRIDGE(dev)) {
  2854. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2855. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2856. } else {
  2857. temp &= ~FDI_LINK_TRAIN_NONE;
  2858. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2859. }
  2860. I915_WRITE(reg, temp);
  2861. reg = FDI_RX_CTL(pipe);
  2862. temp = I915_READ(reg);
  2863. if (HAS_PCH_CPT(dev)) {
  2864. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2865. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2866. } else {
  2867. temp &= ~FDI_LINK_TRAIN_NONE;
  2868. temp |= FDI_LINK_TRAIN_NONE;
  2869. }
  2870. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2871. /* wait one idle pattern time */
  2872. POSTING_READ(reg);
  2873. udelay(1000);
  2874. /* IVB wants error correction enabled */
  2875. if (IS_IVYBRIDGE(dev))
  2876. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2877. FDI_FE_ERRC_ENABLE);
  2878. }
  2879. /* The FDI link training functions for ILK/Ibexpeak. */
  2880. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2881. {
  2882. struct drm_device *dev = crtc->dev;
  2883. struct drm_i915_private *dev_priv = to_i915(dev);
  2884. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2885. int pipe = intel_crtc->pipe;
  2886. i915_reg_t reg;
  2887. u32 temp, tries;
  2888. /* FDI needs bits from pipe first */
  2889. assert_pipe_enabled(dev_priv, pipe);
  2890. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2891. for train result */
  2892. reg = FDI_RX_IMR(pipe);
  2893. temp = I915_READ(reg);
  2894. temp &= ~FDI_RX_SYMBOL_LOCK;
  2895. temp &= ~FDI_RX_BIT_LOCK;
  2896. I915_WRITE(reg, temp);
  2897. I915_READ(reg);
  2898. udelay(150);
  2899. /* enable CPU FDI TX and PCH FDI RX */
  2900. reg = FDI_TX_CTL(pipe);
  2901. temp = I915_READ(reg);
  2902. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2903. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2904. temp &= ~FDI_LINK_TRAIN_NONE;
  2905. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2906. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2907. reg = FDI_RX_CTL(pipe);
  2908. temp = I915_READ(reg);
  2909. temp &= ~FDI_LINK_TRAIN_NONE;
  2910. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2911. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2912. POSTING_READ(reg);
  2913. udelay(150);
  2914. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2915. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2916. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2917. FDI_RX_PHASE_SYNC_POINTER_EN);
  2918. reg = FDI_RX_IIR(pipe);
  2919. for (tries = 0; tries < 5; tries++) {
  2920. temp = I915_READ(reg);
  2921. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2922. if ((temp & FDI_RX_BIT_LOCK)) {
  2923. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2924. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2925. break;
  2926. }
  2927. }
  2928. if (tries == 5)
  2929. DRM_ERROR("FDI train 1 fail!\n");
  2930. /* Train 2 */
  2931. reg = FDI_TX_CTL(pipe);
  2932. temp = I915_READ(reg);
  2933. temp &= ~FDI_LINK_TRAIN_NONE;
  2934. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2935. I915_WRITE(reg, temp);
  2936. reg = FDI_RX_CTL(pipe);
  2937. temp = I915_READ(reg);
  2938. temp &= ~FDI_LINK_TRAIN_NONE;
  2939. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2940. I915_WRITE(reg, temp);
  2941. POSTING_READ(reg);
  2942. udelay(150);
  2943. reg = FDI_RX_IIR(pipe);
  2944. for (tries = 0; tries < 5; tries++) {
  2945. temp = I915_READ(reg);
  2946. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2947. if (temp & FDI_RX_SYMBOL_LOCK) {
  2948. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2949. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2950. break;
  2951. }
  2952. }
  2953. if (tries == 5)
  2954. DRM_ERROR("FDI train 2 fail!\n");
  2955. DRM_DEBUG_KMS("FDI train done\n");
  2956. }
  2957. static const int snb_b_fdi_train_param[] = {
  2958. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2959. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2960. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2961. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2962. };
  2963. /* The FDI link training functions for SNB/Cougarpoint. */
  2964. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2965. {
  2966. struct drm_device *dev = crtc->dev;
  2967. struct drm_i915_private *dev_priv = to_i915(dev);
  2968. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2969. int pipe = intel_crtc->pipe;
  2970. i915_reg_t reg;
  2971. u32 temp, i, retry;
  2972. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2973. for train result */
  2974. reg = FDI_RX_IMR(pipe);
  2975. temp = I915_READ(reg);
  2976. temp &= ~FDI_RX_SYMBOL_LOCK;
  2977. temp &= ~FDI_RX_BIT_LOCK;
  2978. I915_WRITE(reg, temp);
  2979. POSTING_READ(reg);
  2980. udelay(150);
  2981. /* enable CPU FDI TX and PCH FDI RX */
  2982. reg = FDI_TX_CTL(pipe);
  2983. temp = I915_READ(reg);
  2984. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2985. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2986. temp &= ~FDI_LINK_TRAIN_NONE;
  2987. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2988. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2989. /* SNB-B */
  2990. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2991. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2992. I915_WRITE(FDI_RX_MISC(pipe),
  2993. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2994. reg = FDI_RX_CTL(pipe);
  2995. temp = I915_READ(reg);
  2996. if (HAS_PCH_CPT(dev)) {
  2997. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2998. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2999. } else {
  3000. temp &= ~FDI_LINK_TRAIN_NONE;
  3001. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3002. }
  3003. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3004. POSTING_READ(reg);
  3005. udelay(150);
  3006. for (i = 0; i < 4; i++) {
  3007. reg = FDI_TX_CTL(pipe);
  3008. temp = I915_READ(reg);
  3009. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3010. temp |= snb_b_fdi_train_param[i];
  3011. I915_WRITE(reg, temp);
  3012. POSTING_READ(reg);
  3013. udelay(500);
  3014. for (retry = 0; retry < 5; retry++) {
  3015. reg = FDI_RX_IIR(pipe);
  3016. temp = I915_READ(reg);
  3017. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3018. if (temp & FDI_RX_BIT_LOCK) {
  3019. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3020. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3021. break;
  3022. }
  3023. udelay(50);
  3024. }
  3025. if (retry < 5)
  3026. break;
  3027. }
  3028. if (i == 4)
  3029. DRM_ERROR("FDI train 1 fail!\n");
  3030. /* Train 2 */
  3031. reg = FDI_TX_CTL(pipe);
  3032. temp = I915_READ(reg);
  3033. temp &= ~FDI_LINK_TRAIN_NONE;
  3034. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3035. if (IS_GEN6(dev)) {
  3036. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3037. /* SNB-B */
  3038. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3039. }
  3040. I915_WRITE(reg, temp);
  3041. reg = FDI_RX_CTL(pipe);
  3042. temp = I915_READ(reg);
  3043. if (HAS_PCH_CPT(dev)) {
  3044. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3045. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3046. } else {
  3047. temp &= ~FDI_LINK_TRAIN_NONE;
  3048. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3049. }
  3050. I915_WRITE(reg, temp);
  3051. POSTING_READ(reg);
  3052. udelay(150);
  3053. for (i = 0; i < 4; i++) {
  3054. reg = FDI_TX_CTL(pipe);
  3055. temp = I915_READ(reg);
  3056. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3057. temp |= snb_b_fdi_train_param[i];
  3058. I915_WRITE(reg, temp);
  3059. POSTING_READ(reg);
  3060. udelay(500);
  3061. for (retry = 0; retry < 5; retry++) {
  3062. reg = FDI_RX_IIR(pipe);
  3063. temp = I915_READ(reg);
  3064. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3065. if (temp & FDI_RX_SYMBOL_LOCK) {
  3066. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3067. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3068. break;
  3069. }
  3070. udelay(50);
  3071. }
  3072. if (retry < 5)
  3073. break;
  3074. }
  3075. if (i == 4)
  3076. DRM_ERROR("FDI train 2 fail!\n");
  3077. DRM_DEBUG_KMS("FDI train done.\n");
  3078. }
  3079. /* Manual link training for Ivy Bridge A0 parts */
  3080. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3081. {
  3082. struct drm_device *dev = crtc->dev;
  3083. struct drm_i915_private *dev_priv = to_i915(dev);
  3084. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3085. int pipe = intel_crtc->pipe;
  3086. i915_reg_t reg;
  3087. u32 temp, i, j;
  3088. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3089. for train result */
  3090. reg = FDI_RX_IMR(pipe);
  3091. temp = I915_READ(reg);
  3092. temp &= ~FDI_RX_SYMBOL_LOCK;
  3093. temp &= ~FDI_RX_BIT_LOCK;
  3094. I915_WRITE(reg, temp);
  3095. POSTING_READ(reg);
  3096. udelay(150);
  3097. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3098. I915_READ(FDI_RX_IIR(pipe)));
  3099. /* Try each vswing and preemphasis setting twice before moving on */
  3100. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3101. /* disable first in case we need to retry */
  3102. reg = FDI_TX_CTL(pipe);
  3103. temp = I915_READ(reg);
  3104. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3105. temp &= ~FDI_TX_ENABLE;
  3106. I915_WRITE(reg, temp);
  3107. reg = FDI_RX_CTL(pipe);
  3108. temp = I915_READ(reg);
  3109. temp &= ~FDI_LINK_TRAIN_AUTO;
  3110. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3111. temp &= ~FDI_RX_ENABLE;
  3112. I915_WRITE(reg, temp);
  3113. /* enable CPU FDI TX and PCH FDI RX */
  3114. reg = FDI_TX_CTL(pipe);
  3115. temp = I915_READ(reg);
  3116. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3117. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3118. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3119. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3120. temp |= snb_b_fdi_train_param[j/2];
  3121. temp |= FDI_COMPOSITE_SYNC;
  3122. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3123. I915_WRITE(FDI_RX_MISC(pipe),
  3124. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3125. reg = FDI_RX_CTL(pipe);
  3126. temp = I915_READ(reg);
  3127. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3128. temp |= FDI_COMPOSITE_SYNC;
  3129. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3130. POSTING_READ(reg);
  3131. udelay(1); /* should be 0.5us */
  3132. for (i = 0; i < 4; i++) {
  3133. reg = FDI_RX_IIR(pipe);
  3134. temp = I915_READ(reg);
  3135. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3136. if (temp & FDI_RX_BIT_LOCK ||
  3137. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3138. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3139. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3140. i);
  3141. break;
  3142. }
  3143. udelay(1); /* should be 0.5us */
  3144. }
  3145. if (i == 4) {
  3146. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3147. continue;
  3148. }
  3149. /* Train 2 */
  3150. reg = FDI_TX_CTL(pipe);
  3151. temp = I915_READ(reg);
  3152. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3153. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3154. I915_WRITE(reg, temp);
  3155. reg = FDI_RX_CTL(pipe);
  3156. temp = I915_READ(reg);
  3157. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3158. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3159. I915_WRITE(reg, temp);
  3160. POSTING_READ(reg);
  3161. udelay(2); /* should be 1.5us */
  3162. for (i = 0; i < 4; i++) {
  3163. reg = FDI_RX_IIR(pipe);
  3164. temp = I915_READ(reg);
  3165. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3166. if (temp & FDI_RX_SYMBOL_LOCK ||
  3167. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3168. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3169. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3170. i);
  3171. goto train_done;
  3172. }
  3173. udelay(2); /* should be 1.5us */
  3174. }
  3175. if (i == 4)
  3176. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3177. }
  3178. train_done:
  3179. DRM_DEBUG_KMS("FDI train done.\n");
  3180. }
  3181. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3182. {
  3183. struct drm_device *dev = intel_crtc->base.dev;
  3184. struct drm_i915_private *dev_priv = to_i915(dev);
  3185. int pipe = intel_crtc->pipe;
  3186. i915_reg_t reg;
  3187. u32 temp;
  3188. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3189. reg = FDI_RX_CTL(pipe);
  3190. temp = I915_READ(reg);
  3191. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3192. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3193. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3194. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3195. POSTING_READ(reg);
  3196. udelay(200);
  3197. /* Switch from Rawclk to PCDclk */
  3198. temp = I915_READ(reg);
  3199. I915_WRITE(reg, temp | FDI_PCDCLK);
  3200. POSTING_READ(reg);
  3201. udelay(200);
  3202. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3203. reg = FDI_TX_CTL(pipe);
  3204. temp = I915_READ(reg);
  3205. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3206. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3207. POSTING_READ(reg);
  3208. udelay(100);
  3209. }
  3210. }
  3211. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3212. {
  3213. struct drm_device *dev = intel_crtc->base.dev;
  3214. struct drm_i915_private *dev_priv = to_i915(dev);
  3215. int pipe = intel_crtc->pipe;
  3216. i915_reg_t reg;
  3217. u32 temp;
  3218. /* Switch from PCDclk to Rawclk */
  3219. reg = FDI_RX_CTL(pipe);
  3220. temp = I915_READ(reg);
  3221. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3222. /* Disable CPU FDI TX PLL */
  3223. reg = FDI_TX_CTL(pipe);
  3224. temp = I915_READ(reg);
  3225. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3226. POSTING_READ(reg);
  3227. udelay(100);
  3228. reg = FDI_RX_CTL(pipe);
  3229. temp = I915_READ(reg);
  3230. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3231. /* Wait for the clocks to turn off. */
  3232. POSTING_READ(reg);
  3233. udelay(100);
  3234. }
  3235. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3236. {
  3237. struct drm_device *dev = crtc->dev;
  3238. struct drm_i915_private *dev_priv = to_i915(dev);
  3239. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3240. int pipe = intel_crtc->pipe;
  3241. i915_reg_t reg;
  3242. u32 temp;
  3243. /* disable CPU FDI tx and PCH FDI rx */
  3244. reg = FDI_TX_CTL(pipe);
  3245. temp = I915_READ(reg);
  3246. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3247. POSTING_READ(reg);
  3248. reg = FDI_RX_CTL(pipe);
  3249. temp = I915_READ(reg);
  3250. temp &= ~(0x7 << 16);
  3251. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3252. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3253. POSTING_READ(reg);
  3254. udelay(100);
  3255. /* Ironlake workaround, disable clock pointer after downing FDI */
  3256. if (HAS_PCH_IBX(dev))
  3257. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3258. /* still set train pattern 1 */
  3259. reg = FDI_TX_CTL(pipe);
  3260. temp = I915_READ(reg);
  3261. temp &= ~FDI_LINK_TRAIN_NONE;
  3262. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3263. I915_WRITE(reg, temp);
  3264. reg = FDI_RX_CTL(pipe);
  3265. temp = I915_READ(reg);
  3266. if (HAS_PCH_CPT(dev)) {
  3267. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3268. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3269. } else {
  3270. temp &= ~FDI_LINK_TRAIN_NONE;
  3271. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3272. }
  3273. /* BPC in FDI rx is consistent with that in PIPECONF */
  3274. temp &= ~(0x07 << 16);
  3275. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3276. I915_WRITE(reg, temp);
  3277. POSTING_READ(reg);
  3278. udelay(100);
  3279. }
  3280. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3281. {
  3282. struct intel_crtc *crtc;
  3283. /* Note that we don't need to be called with mode_config.lock here
  3284. * as our list of CRTC objects is static for the lifetime of the
  3285. * device and so cannot disappear as we iterate. Similarly, we can
  3286. * happily treat the predicates as racy, atomic checks as userspace
  3287. * cannot claim and pin a new fb without at least acquring the
  3288. * struct_mutex and so serialising with us.
  3289. */
  3290. for_each_intel_crtc(dev, crtc) {
  3291. if (atomic_read(&crtc->unpin_work_count) == 0)
  3292. continue;
  3293. if (crtc->flip_work)
  3294. intel_wait_for_vblank(dev, crtc->pipe);
  3295. return true;
  3296. }
  3297. return false;
  3298. }
  3299. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3300. {
  3301. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3302. struct intel_flip_work *work = intel_crtc->flip_work;
  3303. intel_crtc->flip_work = NULL;
  3304. if (work->event)
  3305. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3306. drm_crtc_vblank_put(&intel_crtc->base);
  3307. wake_up_all(&dev_priv->pending_flip_queue);
  3308. queue_work(dev_priv->wq, &work->unpin_work);
  3309. trace_i915_flip_complete(intel_crtc->plane,
  3310. work->pending_flip_obj);
  3311. }
  3312. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3313. {
  3314. struct drm_device *dev = crtc->dev;
  3315. struct drm_i915_private *dev_priv = to_i915(dev);
  3316. long ret;
  3317. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3318. ret = wait_event_interruptible_timeout(
  3319. dev_priv->pending_flip_queue,
  3320. !intel_crtc_has_pending_flip(crtc),
  3321. 60*HZ);
  3322. if (ret < 0)
  3323. return ret;
  3324. if (ret == 0) {
  3325. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3326. struct intel_flip_work *work;
  3327. spin_lock_irq(&dev->event_lock);
  3328. work = intel_crtc->flip_work;
  3329. if (work && !is_mmio_work(work)) {
  3330. WARN_ONCE(1, "Removing stuck page flip\n");
  3331. page_flip_completed(intel_crtc);
  3332. }
  3333. spin_unlock_irq(&dev->event_lock);
  3334. }
  3335. return 0;
  3336. }
  3337. static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3338. {
  3339. u32 temp;
  3340. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3341. mutex_lock(&dev_priv->sb_lock);
  3342. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3343. temp |= SBI_SSCCTL_DISABLE;
  3344. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3345. mutex_unlock(&dev_priv->sb_lock);
  3346. }
  3347. /* Program iCLKIP clock to the desired frequency */
  3348. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3349. {
  3350. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3351. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3352. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3353. u32 temp;
  3354. lpt_disable_iclkip(dev_priv);
  3355. /* The iCLK virtual clock root frequency is in MHz,
  3356. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3357. * divisors, it is necessary to divide one by another, so we
  3358. * convert the virtual clock precision to KHz here for higher
  3359. * precision.
  3360. */
  3361. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3362. u32 iclk_virtual_root_freq = 172800 * 1000;
  3363. u32 iclk_pi_range = 64;
  3364. u32 desired_divisor;
  3365. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3366. clock << auxdiv);
  3367. divsel = (desired_divisor / iclk_pi_range) - 2;
  3368. phaseinc = desired_divisor % iclk_pi_range;
  3369. /*
  3370. * Near 20MHz is a corner case which is
  3371. * out of range for the 7-bit divisor
  3372. */
  3373. if (divsel <= 0x7f)
  3374. break;
  3375. }
  3376. /* This should not happen with any sane values */
  3377. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3378. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3379. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3380. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3381. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3382. clock,
  3383. auxdiv,
  3384. divsel,
  3385. phasedir,
  3386. phaseinc);
  3387. mutex_lock(&dev_priv->sb_lock);
  3388. /* Program SSCDIVINTPHASE6 */
  3389. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3390. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3391. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3392. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3393. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3394. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3395. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3396. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3397. /* Program SSCAUXDIV */
  3398. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3399. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3400. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3401. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3402. /* Enable modulator and associated divider */
  3403. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3404. temp &= ~SBI_SSCCTL_DISABLE;
  3405. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3406. mutex_unlock(&dev_priv->sb_lock);
  3407. /* Wait for initialization time */
  3408. udelay(24);
  3409. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3410. }
  3411. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3412. {
  3413. u32 divsel, phaseinc, auxdiv;
  3414. u32 iclk_virtual_root_freq = 172800 * 1000;
  3415. u32 iclk_pi_range = 64;
  3416. u32 desired_divisor;
  3417. u32 temp;
  3418. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3419. return 0;
  3420. mutex_lock(&dev_priv->sb_lock);
  3421. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3422. if (temp & SBI_SSCCTL_DISABLE) {
  3423. mutex_unlock(&dev_priv->sb_lock);
  3424. return 0;
  3425. }
  3426. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3427. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3428. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3429. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3430. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3431. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3432. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3433. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3434. mutex_unlock(&dev_priv->sb_lock);
  3435. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3436. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3437. desired_divisor << auxdiv);
  3438. }
  3439. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3440. enum pipe pch_transcoder)
  3441. {
  3442. struct drm_device *dev = crtc->base.dev;
  3443. struct drm_i915_private *dev_priv = to_i915(dev);
  3444. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3445. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3446. I915_READ(HTOTAL(cpu_transcoder)));
  3447. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3448. I915_READ(HBLANK(cpu_transcoder)));
  3449. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3450. I915_READ(HSYNC(cpu_transcoder)));
  3451. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3452. I915_READ(VTOTAL(cpu_transcoder)));
  3453. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3454. I915_READ(VBLANK(cpu_transcoder)));
  3455. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3456. I915_READ(VSYNC(cpu_transcoder)));
  3457. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3458. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3459. }
  3460. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3461. {
  3462. struct drm_i915_private *dev_priv = to_i915(dev);
  3463. uint32_t temp;
  3464. temp = I915_READ(SOUTH_CHICKEN1);
  3465. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3466. return;
  3467. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3468. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3469. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3470. if (enable)
  3471. temp |= FDI_BC_BIFURCATION_SELECT;
  3472. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3473. I915_WRITE(SOUTH_CHICKEN1, temp);
  3474. POSTING_READ(SOUTH_CHICKEN1);
  3475. }
  3476. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3477. {
  3478. struct drm_device *dev = intel_crtc->base.dev;
  3479. switch (intel_crtc->pipe) {
  3480. case PIPE_A:
  3481. break;
  3482. case PIPE_B:
  3483. if (intel_crtc->config->fdi_lanes > 2)
  3484. cpt_set_fdi_bc_bifurcation(dev, false);
  3485. else
  3486. cpt_set_fdi_bc_bifurcation(dev, true);
  3487. break;
  3488. case PIPE_C:
  3489. cpt_set_fdi_bc_bifurcation(dev, true);
  3490. break;
  3491. default:
  3492. BUG();
  3493. }
  3494. }
  3495. /* Return which DP Port should be selected for Transcoder DP control */
  3496. static enum port
  3497. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3498. {
  3499. struct drm_device *dev = crtc->dev;
  3500. struct intel_encoder *encoder;
  3501. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3502. if (encoder->type == INTEL_OUTPUT_DP ||
  3503. encoder->type == INTEL_OUTPUT_EDP)
  3504. return enc_to_dig_port(&encoder->base)->port;
  3505. }
  3506. return -1;
  3507. }
  3508. /*
  3509. * Enable PCH resources required for PCH ports:
  3510. * - PCH PLLs
  3511. * - FDI training & RX/TX
  3512. * - update transcoder timings
  3513. * - DP transcoding bits
  3514. * - transcoder
  3515. */
  3516. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3517. {
  3518. struct drm_device *dev = crtc->dev;
  3519. struct drm_i915_private *dev_priv = to_i915(dev);
  3520. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3521. int pipe = intel_crtc->pipe;
  3522. u32 temp;
  3523. assert_pch_transcoder_disabled(dev_priv, pipe);
  3524. if (IS_IVYBRIDGE(dev))
  3525. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3526. /* Write the TU size bits before fdi link training, so that error
  3527. * detection works. */
  3528. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3529. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3530. /* For PCH output, training FDI link */
  3531. dev_priv->display.fdi_link_train(crtc);
  3532. /* We need to program the right clock selection before writing the pixel
  3533. * mutliplier into the DPLL. */
  3534. if (HAS_PCH_CPT(dev)) {
  3535. u32 sel;
  3536. temp = I915_READ(PCH_DPLL_SEL);
  3537. temp |= TRANS_DPLL_ENABLE(pipe);
  3538. sel = TRANS_DPLLB_SEL(pipe);
  3539. if (intel_crtc->config->shared_dpll ==
  3540. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3541. temp |= sel;
  3542. else
  3543. temp &= ~sel;
  3544. I915_WRITE(PCH_DPLL_SEL, temp);
  3545. }
  3546. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3547. * transcoder, and we actually should do this to not upset any PCH
  3548. * transcoder that already use the clock when we share it.
  3549. *
  3550. * Note that enable_shared_dpll tries to do the right thing, but
  3551. * get_shared_dpll unconditionally resets the pll - we need that to have
  3552. * the right LVDS enable sequence. */
  3553. intel_enable_shared_dpll(intel_crtc);
  3554. /* set transcoder timing, panel must allow it */
  3555. assert_panel_unlocked(dev_priv, pipe);
  3556. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3557. intel_fdi_normal_train(crtc);
  3558. /* For PCH DP, enable TRANS_DP_CTL */
  3559. if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3560. const struct drm_display_mode *adjusted_mode =
  3561. &intel_crtc->config->base.adjusted_mode;
  3562. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3563. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3564. temp = I915_READ(reg);
  3565. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3566. TRANS_DP_SYNC_MASK |
  3567. TRANS_DP_BPC_MASK);
  3568. temp |= TRANS_DP_OUTPUT_ENABLE;
  3569. temp |= bpc << 9; /* same format but at 11:9 */
  3570. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3571. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3572. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3573. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3574. switch (intel_trans_dp_port_sel(crtc)) {
  3575. case PORT_B:
  3576. temp |= TRANS_DP_PORT_SEL_B;
  3577. break;
  3578. case PORT_C:
  3579. temp |= TRANS_DP_PORT_SEL_C;
  3580. break;
  3581. case PORT_D:
  3582. temp |= TRANS_DP_PORT_SEL_D;
  3583. break;
  3584. default:
  3585. BUG();
  3586. }
  3587. I915_WRITE(reg, temp);
  3588. }
  3589. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3590. }
  3591. static void lpt_pch_enable(struct drm_crtc *crtc)
  3592. {
  3593. struct drm_device *dev = crtc->dev;
  3594. struct drm_i915_private *dev_priv = to_i915(dev);
  3595. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3596. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3597. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3598. lpt_program_iclkip(crtc);
  3599. /* Set transcoder timing. */
  3600. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3601. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3602. }
  3603. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3604. {
  3605. struct drm_i915_private *dev_priv = to_i915(dev);
  3606. i915_reg_t dslreg = PIPEDSL(pipe);
  3607. u32 temp;
  3608. temp = I915_READ(dslreg);
  3609. udelay(500);
  3610. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3611. if (wait_for(I915_READ(dslreg) != temp, 5))
  3612. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3613. }
  3614. }
  3615. static int
  3616. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3617. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3618. int src_w, int src_h, int dst_w, int dst_h)
  3619. {
  3620. struct intel_crtc_scaler_state *scaler_state =
  3621. &crtc_state->scaler_state;
  3622. struct intel_crtc *intel_crtc =
  3623. to_intel_crtc(crtc_state->base.crtc);
  3624. int need_scaling;
  3625. need_scaling = intel_rotation_90_or_270(rotation) ?
  3626. (src_h != dst_w || src_w != dst_h):
  3627. (src_w != dst_w || src_h != dst_h);
  3628. /*
  3629. * if plane is being disabled or scaler is no more required or force detach
  3630. * - free scaler binded to this plane/crtc
  3631. * - in order to do this, update crtc->scaler_usage
  3632. *
  3633. * Here scaler state in crtc_state is set free so that
  3634. * scaler can be assigned to other user. Actual register
  3635. * update to free the scaler is done in plane/panel-fit programming.
  3636. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3637. */
  3638. if (force_detach || !need_scaling) {
  3639. if (*scaler_id >= 0) {
  3640. scaler_state->scaler_users &= ~(1 << scaler_user);
  3641. scaler_state->scalers[*scaler_id].in_use = 0;
  3642. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3643. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3644. intel_crtc->pipe, scaler_user, *scaler_id,
  3645. scaler_state->scaler_users);
  3646. *scaler_id = -1;
  3647. }
  3648. return 0;
  3649. }
  3650. /* range checks */
  3651. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3652. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3653. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3654. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3655. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3656. "size is out of scaler range\n",
  3657. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3658. return -EINVAL;
  3659. }
  3660. /* mark this plane as a scaler user in crtc_state */
  3661. scaler_state->scaler_users |= (1 << scaler_user);
  3662. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3663. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3664. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3665. scaler_state->scaler_users);
  3666. return 0;
  3667. }
  3668. /**
  3669. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3670. *
  3671. * @state: crtc's scaler state
  3672. *
  3673. * Return
  3674. * 0 - scaler_usage updated successfully
  3675. * error - requested scaling cannot be supported or other error condition
  3676. */
  3677. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3678. {
  3679. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3680. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3681. DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
  3682. intel_crtc->base.base.id, intel_crtc->base.name,
  3683. intel_crtc->pipe, SKL_CRTC_INDEX);
  3684. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3685. &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
  3686. state->pipe_src_w, state->pipe_src_h,
  3687. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3688. }
  3689. /**
  3690. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3691. *
  3692. * @state: crtc's scaler state
  3693. * @plane_state: atomic plane state to update
  3694. *
  3695. * Return
  3696. * 0 - scaler_usage updated successfully
  3697. * error - requested scaling cannot be supported or other error condition
  3698. */
  3699. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3700. struct intel_plane_state *plane_state)
  3701. {
  3702. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3703. struct intel_plane *intel_plane =
  3704. to_intel_plane(plane_state->base.plane);
  3705. struct drm_framebuffer *fb = plane_state->base.fb;
  3706. int ret;
  3707. bool force_detach = !fb || !plane_state->visible;
  3708. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
  3709. intel_plane->base.base.id, intel_plane->base.name,
  3710. intel_crtc->pipe, drm_plane_index(&intel_plane->base));
  3711. ret = skl_update_scaler(crtc_state, force_detach,
  3712. drm_plane_index(&intel_plane->base),
  3713. &plane_state->scaler_id,
  3714. plane_state->base.rotation,
  3715. drm_rect_width(&plane_state->src) >> 16,
  3716. drm_rect_height(&plane_state->src) >> 16,
  3717. drm_rect_width(&plane_state->dst),
  3718. drm_rect_height(&plane_state->dst));
  3719. if (ret || plane_state->scaler_id < 0)
  3720. return ret;
  3721. /* check colorkey */
  3722. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3723. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  3724. intel_plane->base.base.id,
  3725. intel_plane->base.name);
  3726. return -EINVAL;
  3727. }
  3728. /* Check src format */
  3729. switch (fb->pixel_format) {
  3730. case DRM_FORMAT_RGB565:
  3731. case DRM_FORMAT_XBGR8888:
  3732. case DRM_FORMAT_XRGB8888:
  3733. case DRM_FORMAT_ABGR8888:
  3734. case DRM_FORMAT_ARGB8888:
  3735. case DRM_FORMAT_XRGB2101010:
  3736. case DRM_FORMAT_XBGR2101010:
  3737. case DRM_FORMAT_YUYV:
  3738. case DRM_FORMAT_YVYU:
  3739. case DRM_FORMAT_UYVY:
  3740. case DRM_FORMAT_VYUY:
  3741. break;
  3742. default:
  3743. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  3744. intel_plane->base.base.id, intel_plane->base.name,
  3745. fb->base.id, fb->pixel_format);
  3746. return -EINVAL;
  3747. }
  3748. return 0;
  3749. }
  3750. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3751. {
  3752. int i;
  3753. for (i = 0; i < crtc->num_scalers; i++)
  3754. skl_detach_scaler(crtc, i);
  3755. }
  3756. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3757. {
  3758. struct drm_device *dev = crtc->base.dev;
  3759. struct drm_i915_private *dev_priv = to_i915(dev);
  3760. int pipe = crtc->pipe;
  3761. struct intel_crtc_scaler_state *scaler_state =
  3762. &crtc->config->scaler_state;
  3763. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3764. if (crtc->config->pch_pfit.enabled) {
  3765. int id;
  3766. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3767. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3768. return;
  3769. }
  3770. id = scaler_state->scaler_id;
  3771. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3772. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3773. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3774. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3775. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3776. }
  3777. }
  3778. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3779. {
  3780. struct drm_device *dev = crtc->base.dev;
  3781. struct drm_i915_private *dev_priv = to_i915(dev);
  3782. int pipe = crtc->pipe;
  3783. if (crtc->config->pch_pfit.enabled) {
  3784. /* Force use of hard-coded filter coefficients
  3785. * as some pre-programmed values are broken,
  3786. * e.g. x201.
  3787. */
  3788. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3789. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3790. PF_PIPE_SEL_IVB(pipe));
  3791. else
  3792. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3793. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3794. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3795. }
  3796. }
  3797. void hsw_enable_ips(struct intel_crtc *crtc)
  3798. {
  3799. struct drm_device *dev = crtc->base.dev;
  3800. struct drm_i915_private *dev_priv = to_i915(dev);
  3801. if (!crtc->config->ips_enabled)
  3802. return;
  3803. /*
  3804. * We can only enable IPS after we enable a plane and wait for a vblank
  3805. * This function is called from post_plane_update, which is run after
  3806. * a vblank wait.
  3807. */
  3808. assert_plane_enabled(dev_priv, crtc->plane);
  3809. if (IS_BROADWELL(dev)) {
  3810. mutex_lock(&dev_priv->rps.hw_lock);
  3811. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3812. mutex_unlock(&dev_priv->rps.hw_lock);
  3813. /* Quoting Art Runyan: "its not safe to expect any particular
  3814. * value in IPS_CTL bit 31 after enabling IPS through the
  3815. * mailbox." Moreover, the mailbox may return a bogus state,
  3816. * so we need to just enable it and continue on.
  3817. */
  3818. } else {
  3819. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3820. /* The bit only becomes 1 in the next vblank, so this wait here
  3821. * is essentially intel_wait_for_vblank. If we don't have this
  3822. * and don't wait for vblanks until the end of crtc_enable, then
  3823. * the HW state readout code will complain that the expected
  3824. * IPS_CTL value is not the one we read. */
  3825. if (intel_wait_for_register(dev_priv,
  3826. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  3827. 50))
  3828. DRM_ERROR("Timed out waiting for IPS enable\n");
  3829. }
  3830. }
  3831. void hsw_disable_ips(struct intel_crtc *crtc)
  3832. {
  3833. struct drm_device *dev = crtc->base.dev;
  3834. struct drm_i915_private *dev_priv = to_i915(dev);
  3835. if (!crtc->config->ips_enabled)
  3836. return;
  3837. assert_plane_enabled(dev_priv, crtc->plane);
  3838. if (IS_BROADWELL(dev)) {
  3839. mutex_lock(&dev_priv->rps.hw_lock);
  3840. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3841. mutex_unlock(&dev_priv->rps.hw_lock);
  3842. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3843. if (intel_wait_for_register(dev_priv,
  3844. IPS_CTL, IPS_ENABLE, 0,
  3845. 42))
  3846. DRM_ERROR("Timed out waiting for IPS disable\n");
  3847. } else {
  3848. I915_WRITE(IPS_CTL, 0);
  3849. POSTING_READ(IPS_CTL);
  3850. }
  3851. /* We need to wait for a vblank before we can disable the plane. */
  3852. intel_wait_for_vblank(dev, crtc->pipe);
  3853. }
  3854. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3855. {
  3856. if (intel_crtc->overlay) {
  3857. struct drm_device *dev = intel_crtc->base.dev;
  3858. struct drm_i915_private *dev_priv = to_i915(dev);
  3859. mutex_lock(&dev->struct_mutex);
  3860. dev_priv->mm.interruptible = false;
  3861. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3862. dev_priv->mm.interruptible = true;
  3863. mutex_unlock(&dev->struct_mutex);
  3864. }
  3865. /* Let userspace switch the overlay on again. In most cases userspace
  3866. * has to recompute where to put it anyway.
  3867. */
  3868. }
  3869. /**
  3870. * intel_post_enable_primary - Perform operations after enabling primary plane
  3871. * @crtc: the CRTC whose primary plane was just enabled
  3872. *
  3873. * Performs potentially sleeping operations that must be done after the primary
  3874. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3875. * called due to an explicit primary plane update, or due to an implicit
  3876. * re-enable that is caused when a sprite plane is updated to no longer
  3877. * completely hide the primary plane.
  3878. */
  3879. static void
  3880. intel_post_enable_primary(struct drm_crtc *crtc)
  3881. {
  3882. struct drm_device *dev = crtc->dev;
  3883. struct drm_i915_private *dev_priv = to_i915(dev);
  3884. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3885. int pipe = intel_crtc->pipe;
  3886. /*
  3887. * FIXME IPS should be fine as long as one plane is
  3888. * enabled, but in practice it seems to have problems
  3889. * when going from primary only to sprite only and vice
  3890. * versa.
  3891. */
  3892. hsw_enable_ips(intel_crtc);
  3893. /*
  3894. * Gen2 reports pipe underruns whenever all planes are disabled.
  3895. * So don't enable underrun reporting before at least some planes
  3896. * are enabled.
  3897. * FIXME: Need to fix the logic to work when we turn off all planes
  3898. * but leave the pipe running.
  3899. */
  3900. if (IS_GEN2(dev))
  3901. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3902. /* Underruns don't always raise interrupts, so check manually. */
  3903. intel_check_cpu_fifo_underruns(dev_priv);
  3904. intel_check_pch_fifo_underruns(dev_priv);
  3905. }
  3906. /* FIXME move all this to pre_plane_update() with proper state tracking */
  3907. static void
  3908. intel_pre_disable_primary(struct drm_crtc *crtc)
  3909. {
  3910. struct drm_device *dev = crtc->dev;
  3911. struct drm_i915_private *dev_priv = to_i915(dev);
  3912. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3913. int pipe = intel_crtc->pipe;
  3914. /*
  3915. * Gen2 reports pipe underruns whenever all planes are disabled.
  3916. * So diasble underrun reporting before all the planes get disabled.
  3917. * FIXME: Need to fix the logic to work when we turn off all planes
  3918. * but leave the pipe running.
  3919. */
  3920. if (IS_GEN2(dev))
  3921. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3922. /*
  3923. * FIXME IPS should be fine as long as one plane is
  3924. * enabled, but in practice it seems to have problems
  3925. * when going from primary only to sprite only and vice
  3926. * versa.
  3927. */
  3928. hsw_disable_ips(intel_crtc);
  3929. }
  3930. /* FIXME get rid of this and use pre_plane_update */
  3931. static void
  3932. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  3933. {
  3934. struct drm_device *dev = crtc->dev;
  3935. struct drm_i915_private *dev_priv = to_i915(dev);
  3936. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3937. int pipe = intel_crtc->pipe;
  3938. intel_pre_disable_primary(crtc);
  3939. /*
  3940. * Vblank time updates from the shadow to live plane control register
  3941. * are blocked if the memory self-refresh mode is active at that
  3942. * moment. So to make sure the plane gets truly disabled, disable
  3943. * first the self-refresh mode. The self-refresh enable bit in turn
  3944. * will be checked/applied by the HW only at the next frame start
  3945. * event which is after the vblank start event, so we need to have a
  3946. * wait-for-vblank between disabling the plane and the pipe.
  3947. */
  3948. if (HAS_GMCH_DISPLAY(dev)) {
  3949. intel_set_memory_cxsr(dev_priv, false);
  3950. dev_priv->wm.vlv.cxsr = false;
  3951. intel_wait_for_vblank(dev, pipe);
  3952. }
  3953. }
  3954. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  3955. {
  3956. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3957. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3958. struct intel_crtc_state *pipe_config =
  3959. to_intel_crtc_state(crtc->base.state);
  3960. struct drm_device *dev = crtc->base.dev;
  3961. struct drm_plane *primary = crtc->base.primary;
  3962. struct drm_plane_state *old_pri_state =
  3963. drm_atomic_get_existing_plane_state(old_state, primary);
  3964. intel_frontbuffer_flip(dev, pipe_config->fb_bits);
  3965. crtc->wm.cxsr_allowed = true;
  3966. if (pipe_config->update_wm_post && pipe_config->base.active)
  3967. intel_update_watermarks(&crtc->base);
  3968. if (old_pri_state) {
  3969. struct intel_plane_state *primary_state =
  3970. to_intel_plane_state(primary->state);
  3971. struct intel_plane_state *old_primary_state =
  3972. to_intel_plane_state(old_pri_state);
  3973. intel_fbc_post_update(crtc);
  3974. if (primary_state->visible &&
  3975. (needs_modeset(&pipe_config->base) ||
  3976. !old_primary_state->visible))
  3977. intel_post_enable_primary(&crtc->base);
  3978. }
  3979. }
  3980. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  3981. {
  3982. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3983. struct drm_device *dev = crtc->base.dev;
  3984. struct drm_i915_private *dev_priv = to_i915(dev);
  3985. struct intel_crtc_state *pipe_config =
  3986. to_intel_crtc_state(crtc->base.state);
  3987. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3988. struct drm_plane *primary = crtc->base.primary;
  3989. struct drm_plane_state *old_pri_state =
  3990. drm_atomic_get_existing_plane_state(old_state, primary);
  3991. bool modeset = needs_modeset(&pipe_config->base);
  3992. if (old_pri_state) {
  3993. struct intel_plane_state *primary_state =
  3994. to_intel_plane_state(primary->state);
  3995. struct intel_plane_state *old_primary_state =
  3996. to_intel_plane_state(old_pri_state);
  3997. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  3998. if (old_primary_state->visible &&
  3999. (modeset || !primary_state->visible))
  4000. intel_pre_disable_primary(&crtc->base);
  4001. }
  4002. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
  4003. crtc->wm.cxsr_allowed = false;
  4004. /*
  4005. * Vblank time updates from the shadow to live plane control register
  4006. * are blocked if the memory self-refresh mode is active at that
  4007. * moment. So to make sure the plane gets truly disabled, disable
  4008. * first the self-refresh mode. The self-refresh enable bit in turn
  4009. * will be checked/applied by the HW only at the next frame start
  4010. * event which is after the vblank start event, so we need to have a
  4011. * wait-for-vblank between disabling the plane and the pipe.
  4012. */
  4013. if (old_crtc_state->base.active) {
  4014. intel_set_memory_cxsr(dev_priv, false);
  4015. dev_priv->wm.vlv.cxsr = false;
  4016. intel_wait_for_vblank(dev, crtc->pipe);
  4017. }
  4018. }
  4019. /*
  4020. * IVB workaround: must disable low power watermarks for at least
  4021. * one frame before enabling scaling. LP watermarks can be re-enabled
  4022. * when scaling is disabled.
  4023. *
  4024. * WaCxSRDisabledForSpriteScaling:ivb
  4025. */
  4026. if (pipe_config->disable_lp_wm) {
  4027. ilk_disable_lp_wm(dev);
  4028. intel_wait_for_vblank(dev, crtc->pipe);
  4029. }
  4030. /*
  4031. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4032. * watermark programming here.
  4033. */
  4034. if (needs_modeset(&pipe_config->base))
  4035. return;
  4036. /*
  4037. * For platforms that support atomic watermarks, program the
  4038. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4039. * will be the intermediate values that are safe for both pre- and
  4040. * post- vblank; when vblank happens, the 'active' values will be set
  4041. * to the final 'target' values and we'll do this again to get the
  4042. * optimal watermarks. For gen9+ platforms, the values we program here
  4043. * will be the final target values which will get automatically latched
  4044. * at vblank time; no further programming will be necessary.
  4045. *
  4046. * If a platform hasn't been transitioned to atomic watermarks yet,
  4047. * we'll continue to update watermarks the old way, if flags tell
  4048. * us to.
  4049. */
  4050. if (dev_priv->display.initial_watermarks != NULL)
  4051. dev_priv->display.initial_watermarks(pipe_config);
  4052. else if (pipe_config->update_wm_pre)
  4053. intel_update_watermarks(&crtc->base);
  4054. }
  4055. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4056. {
  4057. struct drm_device *dev = crtc->dev;
  4058. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4059. struct drm_plane *p;
  4060. int pipe = intel_crtc->pipe;
  4061. intel_crtc_dpms_overlay_disable(intel_crtc);
  4062. drm_for_each_plane_mask(p, dev, plane_mask)
  4063. to_intel_plane(p)->disable_plane(p, crtc);
  4064. /*
  4065. * FIXME: Once we grow proper nuclear flip support out of this we need
  4066. * to compute the mask of flip planes precisely. For the time being
  4067. * consider this a flip to a NULL plane.
  4068. */
  4069. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4070. }
  4071. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4072. {
  4073. struct drm_device *dev = crtc->dev;
  4074. struct drm_i915_private *dev_priv = to_i915(dev);
  4075. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4076. struct intel_encoder *encoder;
  4077. int pipe = intel_crtc->pipe;
  4078. struct intel_crtc_state *pipe_config =
  4079. to_intel_crtc_state(crtc->state);
  4080. if (WARN_ON(intel_crtc->active))
  4081. return;
  4082. /*
  4083. * Sometimes spurious CPU pipe underruns happen during FDI
  4084. * training, at least with VGA+HDMI cloning. Suppress them.
  4085. *
  4086. * On ILK we get an occasional spurious CPU pipe underruns
  4087. * between eDP port A enable and vdd enable. Also PCH port
  4088. * enable seems to result in the occasional CPU pipe underrun.
  4089. *
  4090. * Spurious PCH underruns also occur during PCH enabling.
  4091. */
  4092. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4093. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4094. if (intel_crtc->config->has_pch_encoder)
  4095. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4096. if (intel_crtc->config->has_pch_encoder)
  4097. intel_prepare_shared_dpll(intel_crtc);
  4098. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4099. intel_dp_set_m_n(intel_crtc, M1_N1);
  4100. intel_set_pipe_timings(intel_crtc);
  4101. intel_set_pipe_src_size(intel_crtc);
  4102. if (intel_crtc->config->has_pch_encoder) {
  4103. intel_cpu_transcoder_set_m_n(intel_crtc,
  4104. &intel_crtc->config->fdi_m_n, NULL);
  4105. }
  4106. ironlake_set_pipeconf(crtc);
  4107. intel_crtc->active = true;
  4108. for_each_encoder_on_crtc(dev, crtc, encoder)
  4109. if (encoder->pre_enable)
  4110. encoder->pre_enable(encoder);
  4111. if (intel_crtc->config->has_pch_encoder) {
  4112. /* Note: FDI PLL enabling _must_ be done before we enable the
  4113. * cpu pipes, hence this is separate from all the other fdi/pch
  4114. * enabling. */
  4115. ironlake_fdi_pll_enable(intel_crtc);
  4116. } else {
  4117. assert_fdi_tx_disabled(dev_priv, pipe);
  4118. assert_fdi_rx_disabled(dev_priv, pipe);
  4119. }
  4120. ironlake_pfit_enable(intel_crtc);
  4121. /*
  4122. * On ILK+ LUT must be loaded before the pipe is running but with
  4123. * clocks enabled
  4124. */
  4125. intel_color_load_luts(&pipe_config->base);
  4126. if (dev_priv->display.initial_watermarks != NULL)
  4127. dev_priv->display.initial_watermarks(intel_crtc->config);
  4128. intel_enable_pipe(intel_crtc);
  4129. if (intel_crtc->config->has_pch_encoder)
  4130. ironlake_pch_enable(crtc);
  4131. assert_vblank_disabled(crtc);
  4132. drm_crtc_vblank_on(crtc);
  4133. for_each_encoder_on_crtc(dev, crtc, encoder)
  4134. encoder->enable(encoder);
  4135. if (HAS_PCH_CPT(dev))
  4136. cpt_verify_modeset(dev, intel_crtc->pipe);
  4137. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4138. if (intel_crtc->config->has_pch_encoder)
  4139. intel_wait_for_vblank(dev, pipe);
  4140. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4141. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4142. }
  4143. /* IPS only exists on ULT machines and is tied to pipe A. */
  4144. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4145. {
  4146. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4147. }
  4148. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4149. {
  4150. struct drm_device *dev = crtc->dev;
  4151. struct drm_i915_private *dev_priv = to_i915(dev);
  4152. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4153. struct intel_encoder *encoder;
  4154. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4155. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4156. struct intel_crtc_state *pipe_config =
  4157. to_intel_crtc_state(crtc->state);
  4158. if (WARN_ON(intel_crtc->active))
  4159. return;
  4160. if (intel_crtc->config->has_pch_encoder)
  4161. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4162. false);
  4163. for_each_encoder_on_crtc(dev, crtc, encoder)
  4164. if (encoder->pre_pll_enable)
  4165. encoder->pre_pll_enable(encoder);
  4166. if (intel_crtc->config->shared_dpll)
  4167. intel_enable_shared_dpll(intel_crtc);
  4168. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4169. intel_dp_set_m_n(intel_crtc, M1_N1);
  4170. if (!transcoder_is_dsi(cpu_transcoder))
  4171. intel_set_pipe_timings(intel_crtc);
  4172. intel_set_pipe_src_size(intel_crtc);
  4173. if (cpu_transcoder != TRANSCODER_EDP &&
  4174. !transcoder_is_dsi(cpu_transcoder)) {
  4175. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4176. intel_crtc->config->pixel_multiplier - 1);
  4177. }
  4178. if (intel_crtc->config->has_pch_encoder) {
  4179. intel_cpu_transcoder_set_m_n(intel_crtc,
  4180. &intel_crtc->config->fdi_m_n, NULL);
  4181. }
  4182. if (!transcoder_is_dsi(cpu_transcoder))
  4183. haswell_set_pipeconf(crtc);
  4184. haswell_set_pipemisc(crtc);
  4185. intel_color_set_csc(&pipe_config->base);
  4186. intel_crtc->active = true;
  4187. if (intel_crtc->config->has_pch_encoder)
  4188. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4189. else
  4190. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4191. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4192. if (encoder->pre_enable)
  4193. encoder->pre_enable(encoder);
  4194. }
  4195. if (intel_crtc->config->has_pch_encoder)
  4196. dev_priv->display.fdi_link_train(crtc);
  4197. if (!transcoder_is_dsi(cpu_transcoder))
  4198. intel_ddi_enable_pipe_clock(intel_crtc);
  4199. if (INTEL_INFO(dev)->gen >= 9)
  4200. skylake_pfit_enable(intel_crtc);
  4201. else
  4202. ironlake_pfit_enable(intel_crtc);
  4203. /*
  4204. * On ILK+ LUT must be loaded before the pipe is running but with
  4205. * clocks enabled
  4206. */
  4207. intel_color_load_luts(&pipe_config->base);
  4208. intel_ddi_set_pipe_settings(crtc);
  4209. if (!transcoder_is_dsi(cpu_transcoder))
  4210. intel_ddi_enable_transcoder_func(crtc);
  4211. if (dev_priv->display.initial_watermarks != NULL)
  4212. dev_priv->display.initial_watermarks(pipe_config);
  4213. else
  4214. intel_update_watermarks(crtc);
  4215. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4216. if (!transcoder_is_dsi(cpu_transcoder))
  4217. intel_enable_pipe(intel_crtc);
  4218. if (intel_crtc->config->has_pch_encoder)
  4219. lpt_pch_enable(crtc);
  4220. if (intel_crtc->config->dp_encoder_is_mst)
  4221. intel_ddi_set_vc_payload_alloc(crtc, true);
  4222. assert_vblank_disabled(crtc);
  4223. drm_crtc_vblank_on(crtc);
  4224. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4225. encoder->enable(encoder);
  4226. intel_opregion_notify_encoder(encoder, true);
  4227. }
  4228. if (intel_crtc->config->has_pch_encoder) {
  4229. intel_wait_for_vblank(dev, pipe);
  4230. intel_wait_for_vblank(dev, pipe);
  4231. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4232. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4233. true);
  4234. }
  4235. /* If we change the relative order between pipe/planes enabling, we need
  4236. * to change the workaround. */
  4237. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4238. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4239. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4240. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4241. }
  4242. }
  4243. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4244. {
  4245. struct drm_device *dev = crtc->base.dev;
  4246. struct drm_i915_private *dev_priv = to_i915(dev);
  4247. int pipe = crtc->pipe;
  4248. /* To avoid upsetting the power well on haswell only disable the pfit if
  4249. * it's in use. The hw state code will make sure we get this right. */
  4250. if (force || crtc->config->pch_pfit.enabled) {
  4251. I915_WRITE(PF_CTL(pipe), 0);
  4252. I915_WRITE(PF_WIN_POS(pipe), 0);
  4253. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4254. }
  4255. }
  4256. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4257. {
  4258. struct drm_device *dev = crtc->dev;
  4259. struct drm_i915_private *dev_priv = to_i915(dev);
  4260. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4261. struct intel_encoder *encoder;
  4262. int pipe = intel_crtc->pipe;
  4263. /*
  4264. * Sometimes spurious CPU pipe underruns happen when the
  4265. * pipe is already disabled, but FDI RX/TX is still enabled.
  4266. * Happens at least with VGA+HDMI cloning. Suppress them.
  4267. */
  4268. if (intel_crtc->config->has_pch_encoder) {
  4269. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4270. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4271. }
  4272. for_each_encoder_on_crtc(dev, crtc, encoder)
  4273. encoder->disable(encoder);
  4274. drm_crtc_vblank_off(crtc);
  4275. assert_vblank_disabled(crtc);
  4276. intel_disable_pipe(intel_crtc);
  4277. ironlake_pfit_disable(intel_crtc, false);
  4278. if (intel_crtc->config->has_pch_encoder)
  4279. ironlake_fdi_disable(crtc);
  4280. for_each_encoder_on_crtc(dev, crtc, encoder)
  4281. if (encoder->post_disable)
  4282. encoder->post_disable(encoder);
  4283. if (intel_crtc->config->has_pch_encoder) {
  4284. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4285. if (HAS_PCH_CPT(dev)) {
  4286. i915_reg_t reg;
  4287. u32 temp;
  4288. /* disable TRANS_DP_CTL */
  4289. reg = TRANS_DP_CTL(pipe);
  4290. temp = I915_READ(reg);
  4291. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4292. TRANS_DP_PORT_SEL_MASK);
  4293. temp |= TRANS_DP_PORT_SEL_NONE;
  4294. I915_WRITE(reg, temp);
  4295. /* disable DPLL_SEL */
  4296. temp = I915_READ(PCH_DPLL_SEL);
  4297. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4298. I915_WRITE(PCH_DPLL_SEL, temp);
  4299. }
  4300. ironlake_fdi_pll_disable(intel_crtc);
  4301. }
  4302. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4303. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4304. }
  4305. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4306. {
  4307. struct drm_device *dev = crtc->dev;
  4308. struct drm_i915_private *dev_priv = to_i915(dev);
  4309. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4310. struct intel_encoder *encoder;
  4311. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4312. if (intel_crtc->config->has_pch_encoder)
  4313. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4314. false);
  4315. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4316. intel_opregion_notify_encoder(encoder, false);
  4317. encoder->disable(encoder);
  4318. }
  4319. drm_crtc_vblank_off(crtc);
  4320. assert_vblank_disabled(crtc);
  4321. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4322. if (!transcoder_is_dsi(cpu_transcoder))
  4323. intel_disable_pipe(intel_crtc);
  4324. if (intel_crtc->config->dp_encoder_is_mst)
  4325. intel_ddi_set_vc_payload_alloc(crtc, false);
  4326. if (!transcoder_is_dsi(cpu_transcoder))
  4327. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4328. if (INTEL_INFO(dev)->gen >= 9)
  4329. skylake_scaler_disable(intel_crtc);
  4330. else
  4331. ironlake_pfit_disable(intel_crtc, false);
  4332. if (!transcoder_is_dsi(cpu_transcoder))
  4333. intel_ddi_disable_pipe_clock(intel_crtc);
  4334. for_each_encoder_on_crtc(dev, crtc, encoder)
  4335. if (encoder->post_disable)
  4336. encoder->post_disable(encoder);
  4337. if (intel_crtc->config->has_pch_encoder) {
  4338. lpt_disable_pch_transcoder(dev_priv);
  4339. lpt_disable_iclkip(dev_priv);
  4340. intel_ddi_fdi_disable(crtc);
  4341. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4342. true);
  4343. }
  4344. }
  4345. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4346. {
  4347. struct drm_device *dev = crtc->base.dev;
  4348. struct drm_i915_private *dev_priv = to_i915(dev);
  4349. struct intel_crtc_state *pipe_config = crtc->config;
  4350. if (!pipe_config->gmch_pfit.control)
  4351. return;
  4352. /*
  4353. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4354. * according to register description and PRM.
  4355. */
  4356. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4357. assert_pipe_disabled(dev_priv, crtc->pipe);
  4358. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4359. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4360. /* Border color in case we don't scale up to the full screen. Black by
  4361. * default, change to something else for debugging. */
  4362. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4363. }
  4364. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4365. {
  4366. switch (port) {
  4367. case PORT_A:
  4368. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4369. case PORT_B:
  4370. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4371. case PORT_C:
  4372. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4373. case PORT_D:
  4374. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4375. case PORT_E:
  4376. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4377. default:
  4378. MISSING_CASE(port);
  4379. return POWER_DOMAIN_PORT_OTHER;
  4380. }
  4381. }
  4382. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4383. {
  4384. switch (port) {
  4385. case PORT_A:
  4386. return POWER_DOMAIN_AUX_A;
  4387. case PORT_B:
  4388. return POWER_DOMAIN_AUX_B;
  4389. case PORT_C:
  4390. return POWER_DOMAIN_AUX_C;
  4391. case PORT_D:
  4392. return POWER_DOMAIN_AUX_D;
  4393. case PORT_E:
  4394. /* FIXME: Check VBT for actual wiring of PORT E */
  4395. return POWER_DOMAIN_AUX_D;
  4396. default:
  4397. MISSING_CASE(port);
  4398. return POWER_DOMAIN_AUX_A;
  4399. }
  4400. }
  4401. enum intel_display_power_domain
  4402. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4403. {
  4404. struct drm_device *dev = intel_encoder->base.dev;
  4405. struct intel_digital_port *intel_dig_port;
  4406. switch (intel_encoder->type) {
  4407. case INTEL_OUTPUT_UNKNOWN:
  4408. /* Only DDI platforms should ever use this output type */
  4409. WARN_ON_ONCE(!HAS_DDI(dev));
  4410. case INTEL_OUTPUT_DP:
  4411. case INTEL_OUTPUT_HDMI:
  4412. case INTEL_OUTPUT_EDP:
  4413. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4414. return port_to_power_domain(intel_dig_port->port);
  4415. case INTEL_OUTPUT_DP_MST:
  4416. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4417. return port_to_power_domain(intel_dig_port->port);
  4418. case INTEL_OUTPUT_ANALOG:
  4419. return POWER_DOMAIN_PORT_CRT;
  4420. case INTEL_OUTPUT_DSI:
  4421. return POWER_DOMAIN_PORT_DSI;
  4422. default:
  4423. return POWER_DOMAIN_PORT_OTHER;
  4424. }
  4425. }
  4426. enum intel_display_power_domain
  4427. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4428. {
  4429. struct drm_device *dev = intel_encoder->base.dev;
  4430. struct intel_digital_port *intel_dig_port;
  4431. switch (intel_encoder->type) {
  4432. case INTEL_OUTPUT_UNKNOWN:
  4433. case INTEL_OUTPUT_HDMI:
  4434. /*
  4435. * Only DDI platforms should ever use these output types.
  4436. * We can get here after the HDMI detect code has already set
  4437. * the type of the shared encoder. Since we can't be sure
  4438. * what's the status of the given connectors, play safe and
  4439. * run the DP detection too.
  4440. */
  4441. WARN_ON_ONCE(!HAS_DDI(dev));
  4442. case INTEL_OUTPUT_DP:
  4443. case INTEL_OUTPUT_EDP:
  4444. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4445. return port_to_aux_power_domain(intel_dig_port->port);
  4446. case INTEL_OUTPUT_DP_MST:
  4447. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4448. return port_to_aux_power_domain(intel_dig_port->port);
  4449. default:
  4450. MISSING_CASE(intel_encoder->type);
  4451. return POWER_DOMAIN_AUX_A;
  4452. }
  4453. }
  4454. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4455. struct intel_crtc_state *crtc_state)
  4456. {
  4457. struct drm_device *dev = crtc->dev;
  4458. struct drm_encoder *encoder;
  4459. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4460. enum pipe pipe = intel_crtc->pipe;
  4461. unsigned long mask;
  4462. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4463. if (!crtc_state->base.active)
  4464. return 0;
  4465. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4466. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4467. if (crtc_state->pch_pfit.enabled ||
  4468. crtc_state->pch_pfit.force_thru)
  4469. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4470. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4471. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4472. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4473. }
  4474. if (crtc_state->shared_dpll)
  4475. mask |= BIT(POWER_DOMAIN_PLLS);
  4476. return mask;
  4477. }
  4478. static unsigned long
  4479. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4480. struct intel_crtc_state *crtc_state)
  4481. {
  4482. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4483. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4484. enum intel_display_power_domain domain;
  4485. unsigned long domains, new_domains, old_domains;
  4486. old_domains = intel_crtc->enabled_power_domains;
  4487. intel_crtc->enabled_power_domains = new_domains =
  4488. get_crtc_power_domains(crtc, crtc_state);
  4489. domains = new_domains & ~old_domains;
  4490. for_each_power_domain(domain, domains)
  4491. intel_display_power_get(dev_priv, domain);
  4492. return old_domains & ~new_domains;
  4493. }
  4494. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4495. unsigned long domains)
  4496. {
  4497. enum intel_display_power_domain domain;
  4498. for_each_power_domain(domain, domains)
  4499. intel_display_power_put(dev_priv, domain);
  4500. }
  4501. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4502. {
  4503. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4504. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4505. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4506. return max_cdclk_freq;
  4507. else if (IS_CHERRYVIEW(dev_priv))
  4508. return max_cdclk_freq*95/100;
  4509. else if (INTEL_INFO(dev_priv)->gen < 4)
  4510. return 2*max_cdclk_freq*90/100;
  4511. else
  4512. return max_cdclk_freq*90/100;
  4513. }
  4514. static int skl_calc_cdclk(int max_pixclk, int vco);
  4515. static void intel_update_max_cdclk(struct drm_device *dev)
  4516. {
  4517. struct drm_i915_private *dev_priv = to_i915(dev);
  4518. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4519. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4520. int max_cdclk, vco;
  4521. vco = dev_priv->skl_preferred_vco_freq;
  4522. WARN_ON(vco != 8100000 && vco != 8640000);
  4523. /*
  4524. * Use the lower (vco 8640) cdclk values as a
  4525. * first guess. skl_calc_cdclk() will correct it
  4526. * if the preferred vco is 8100 instead.
  4527. */
  4528. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4529. max_cdclk = 617143;
  4530. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4531. max_cdclk = 540000;
  4532. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4533. max_cdclk = 432000;
  4534. else
  4535. max_cdclk = 308571;
  4536. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4537. } else if (IS_BROXTON(dev)) {
  4538. dev_priv->max_cdclk_freq = 624000;
  4539. } else if (IS_BROADWELL(dev)) {
  4540. /*
  4541. * FIXME with extra cooling we can allow
  4542. * 540 MHz for ULX and 675 Mhz for ULT.
  4543. * How can we know if extra cooling is
  4544. * available? PCI ID, VTB, something else?
  4545. */
  4546. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4547. dev_priv->max_cdclk_freq = 450000;
  4548. else if (IS_BDW_ULX(dev))
  4549. dev_priv->max_cdclk_freq = 450000;
  4550. else if (IS_BDW_ULT(dev))
  4551. dev_priv->max_cdclk_freq = 540000;
  4552. else
  4553. dev_priv->max_cdclk_freq = 675000;
  4554. } else if (IS_CHERRYVIEW(dev)) {
  4555. dev_priv->max_cdclk_freq = 320000;
  4556. } else if (IS_VALLEYVIEW(dev)) {
  4557. dev_priv->max_cdclk_freq = 400000;
  4558. } else {
  4559. /* otherwise assume cdclk is fixed */
  4560. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4561. }
  4562. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4563. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4564. dev_priv->max_cdclk_freq);
  4565. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4566. dev_priv->max_dotclk_freq);
  4567. }
  4568. static void intel_update_cdclk(struct drm_device *dev)
  4569. {
  4570. struct drm_i915_private *dev_priv = to_i915(dev);
  4571. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4572. if (INTEL_GEN(dev_priv) >= 9)
  4573. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  4574. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  4575. dev_priv->cdclk_pll.ref);
  4576. else
  4577. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4578. dev_priv->cdclk_freq);
  4579. /*
  4580. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4581. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4582. * of cdclk that generates 4MHz reference clock freq which is used to
  4583. * generate GMBus clock. This will vary with the cdclk freq.
  4584. */
  4585. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4586. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4587. }
  4588. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4589. static int skl_cdclk_decimal(int cdclk)
  4590. {
  4591. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  4592. }
  4593. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  4594. {
  4595. int ratio;
  4596. if (cdclk == dev_priv->cdclk_pll.ref)
  4597. return 0;
  4598. switch (cdclk) {
  4599. default:
  4600. MISSING_CASE(cdclk);
  4601. case 144000:
  4602. case 288000:
  4603. case 384000:
  4604. case 576000:
  4605. ratio = 60;
  4606. break;
  4607. case 624000:
  4608. ratio = 65;
  4609. break;
  4610. }
  4611. return dev_priv->cdclk_pll.ref * ratio;
  4612. }
  4613. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  4614. {
  4615. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  4616. /* Timeout 200us */
  4617. if (intel_wait_for_register(dev_priv,
  4618. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  4619. 1))
  4620. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  4621. dev_priv->cdclk_pll.vco = 0;
  4622. }
  4623. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  4624. {
  4625. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  4626. u32 val;
  4627. val = I915_READ(BXT_DE_PLL_CTL);
  4628. val &= ~BXT_DE_PLL_RATIO_MASK;
  4629. val |= BXT_DE_PLL_RATIO(ratio);
  4630. I915_WRITE(BXT_DE_PLL_CTL, val);
  4631. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4632. /* Timeout 200us */
  4633. if (intel_wait_for_register(dev_priv,
  4634. BXT_DE_PLL_ENABLE,
  4635. BXT_DE_PLL_LOCK,
  4636. BXT_DE_PLL_LOCK,
  4637. 1))
  4638. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4639. dev_priv->cdclk_pll.vco = vco;
  4640. }
  4641. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  4642. {
  4643. u32 val, divider;
  4644. int vco, ret;
  4645. vco = bxt_de_pll_vco(dev_priv, cdclk);
  4646. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  4647. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  4648. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  4649. case 8:
  4650. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4651. break;
  4652. case 4:
  4653. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4654. break;
  4655. case 3:
  4656. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4657. break;
  4658. case 2:
  4659. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4660. break;
  4661. default:
  4662. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  4663. WARN_ON(vco != 0);
  4664. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4665. break;
  4666. }
  4667. /* Inform power controller of upcoming frequency change */
  4668. mutex_lock(&dev_priv->rps.hw_lock);
  4669. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4670. 0x80000000);
  4671. mutex_unlock(&dev_priv->rps.hw_lock);
  4672. if (ret) {
  4673. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4674. ret, cdclk);
  4675. return;
  4676. }
  4677. if (dev_priv->cdclk_pll.vco != 0 &&
  4678. dev_priv->cdclk_pll.vco != vco)
  4679. bxt_de_pll_disable(dev_priv);
  4680. if (dev_priv->cdclk_pll.vco != vco)
  4681. bxt_de_pll_enable(dev_priv, vco);
  4682. val = divider | skl_cdclk_decimal(cdclk);
  4683. /*
  4684. * FIXME if only the cd2x divider needs changing, it could be done
  4685. * without shutting off the pipe (if only one pipe is active).
  4686. */
  4687. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  4688. /*
  4689. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4690. * enable otherwise.
  4691. */
  4692. if (cdclk >= 500000)
  4693. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4694. I915_WRITE(CDCLK_CTL, val);
  4695. mutex_lock(&dev_priv->rps.hw_lock);
  4696. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4697. DIV_ROUND_UP(cdclk, 25000));
  4698. mutex_unlock(&dev_priv->rps.hw_lock);
  4699. if (ret) {
  4700. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4701. ret, cdclk);
  4702. return;
  4703. }
  4704. intel_update_cdclk(&dev_priv->drm);
  4705. }
  4706. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4707. {
  4708. u32 cdctl, expected;
  4709. intel_update_cdclk(&dev_priv->drm);
  4710. if (dev_priv->cdclk_pll.vco == 0 ||
  4711. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  4712. goto sanitize;
  4713. /* DPLL okay; verify the cdclock
  4714. *
  4715. * Some BIOS versions leave an incorrect decimal frequency value and
  4716. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  4717. * so sanitize this register.
  4718. */
  4719. cdctl = I915_READ(CDCLK_CTL);
  4720. /*
  4721. * Let's ignore the pipe field, since BIOS could have configured the
  4722. * dividers both synching to an active pipe, or asynchronously
  4723. * (PIPE_NONE).
  4724. */
  4725. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  4726. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  4727. skl_cdclk_decimal(dev_priv->cdclk_freq);
  4728. /*
  4729. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4730. * enable otherwise.
  4731. */
  4732. if (dev_priv->cdclk_freq >= 500000)
  4733. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4734. if (cdctl == expected)
  4735. /* All well; nothing to sanitize */
  4736. return;
  4737. sanitize:
  4738. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  4739. /* force cdclk programming */
  4740. dev_priv->cdclk_freq = 0;
  4741. /* force full PLL disable + enable */
  4742. dev_priv->cdclk_pll.vco = -1;
  4743. }
  4744. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  4745. {
  4746. bxt_sanitize_cdclk(dev_priv);
  4747. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  4748. return;
  4749. /*
  4750. * FIXME:
  4751. * - The initial CDCLK needs to be read from VBT.
  4752. * Need to make this change after VBT has changes for BXT.
  4753. */
  4754. bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
  4755. }
  4756. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  4757. {
  4758. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  4759. }
  4760. static int skl_calc_cdclk(int max_pixclk, int vco)
  4761. {
  4762. if (vco == 8640000) {
  4763. if (max_pixclk > 540000)
  4764. return 617143;
  4765. else if (max_pixclk > 432000)
  4766. return 540000;
  4767. else if (max_pixclk > 308571)
  4768. return 432000;
  4769. else
  4770. return 308571;
  4771. } else {
  4772. if (max_pixclk > 540000)
  4773. return 675000;
  4774. else if (max_pixclk > 450000)
  4775. return 540000;
  4776. else if (max_pixclk > 337500)
  4777. return 450000;
  4778. else
  4779. return 337500;
  4780. }
  4781. }
  4782. static void
  4783. skl_dpll0_update(struct drm_i915_private *dev_priv)
  4784. {
  4785. u32 val;
  4786. dev_priv->cdclk_pll.ref = 24000;
  4787. dev_priv->cdclk_pll.vco = 0;
  4788. val = I915_READ(LCPLL1_CTL);
  4789. if ((val & LCPLL_PLL_ENABLE) == 0)
  4790. return;
  4791. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  4792. return;
  4793. val = I915_READ(DPLL_CTRL1);
  4794. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  4795. DPLL_CTRL1_SSC(SKL_DPLL0) |
  4796. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  4797. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  4798. return;
  4799. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  4800. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  4801. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  4802. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  4803. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  4804. dev_priv->cdclk_pll.vco = 8100000;
  4805. break;
  4806. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  4807. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  4808. dev_priv->cdclk_pll.vco = 8640000;
  4809. break;
  4810. default:
  4811. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4812. break;
  4813. }
  4814. }
  4815. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  4816. {
  4817. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  4818. dev_priv->skl_preferred_vco_freq = vco;
  4819. if (changed)
  4820. intel_update_max_cdclk(&dev_priv->drm);
  4821. }
  4822. static void
  4823. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  4824. {
  4825. int min_cdclk = skl_calc_cdclk(0, vco);
  4826. u32 val;
  4827. WARN_ON(vco != 8100000 && vco != 8640000);
  4828. /* select the minimum CDCLK before enabling DPLL 0 */
  4829. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  4830. I915_WRITE(CDCLK_CTL, val);
  4831. POSTING_READ(CDCLK_CTL);
  4832. /*
  4833. * We always enable DPLL0 with the lowest link rate possible, but still
  4834. * taking into account the VCO required to operate the eDP panel at the
  4835. * desired frequency. The usual DP link rates operate with a VCO of
  4836. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4837. * The modeset code is responsible for the selection of the exact link
  4838. * rate later on, with the constraint of choosing a frequency that
  4839. * works with vco.
  4840. */
  4841. val = I915_READ(DPLL_CTRL1);
  4842. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4843. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4844. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4845. if (vco == 8640000)
  4846. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4847. SKL_DPLL0);
  4848. else
  4849. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4850. SKL_DPLL0);
  4851. I915_WRITE(DPLL_CTRL1, val);
  4852. POSTING_READ(DPLL_CTRL1);
  4853. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4854. if (intel_wait_for_register(dev_priv,
  4855. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  4856. 5))
  4857. DRM_ERROR("DPLL0 not locked\n");
  4858. dev_priv->cdclk_pll.vco = vco;
  4859. /* We'll want to keep using the current vco from now on. */
  4860. skl_set_preferred_cdclk_vco(dev_priv, vco);
  4861. }
  4862. static void
  4863. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  4864. {
  4865. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4866. if (intel_wait_for_register(dev_priv,
  4867. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  4868. 1))
  4869. DRM_ERROR("Couldn't disable DPLL0\n");
  4870. dev_priv->cdclk_pll.vco = 0;
  4871. }
  4872. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4873. {
  4874. int ret;
  4875. u32 val;
  4876. /* inform PCU we want to change CDCLK */
  4877. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4878. mutex_lock(&dev_priv->rps.hw_lock);
  4879. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4880. mutex_unlock(&dev_priv->rps.hw_lock);
  4881. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4882. }
  4883. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4884. {
  4885. return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
  4886. }
  4887. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  4888. {
  4889. struct drm_device *dev = &dev_priv->drm;
  4890. u32 freq_select, pcu_ack;
  4891. WARN_ON((cdclk == 24000) != (vco == 0));
  4892. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  4893. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4894. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4895. return;
  4896. }
  4897. /* set CDCLK_CTL */
  4898. switch (cdclk) {
  4899. case 450000:
  4900. case 432000:
  4901. freq_select = CDCLK_FREQ_450_432;
  4902. pcu_ack = 1;
  4903. break;
  4904. case 540000:
  4905. freq_select = CDCLK_FREQ_540;
  4906. pcu_ack = 2;
  4907. break;
  4908. case 308571:
  4909. case 337500:
  4910. default:
  4911. freq_select = CDCLK_FREQ_337_308;
  4912. pcu_ack = 0;
  4913. break;
  4914. case 617143:
  4915. case 675000:
  4916. freq_select = CDCLK_FREQ_675_617;
  4917. pcu_ack = 3;
  4918. break;
  4919. }
  4920. if (dev_priv->cdclk_pll.vco != 0 &&
  4921. dev_priv->cdclk_pll.vco != vco)
  4922. skl_dpll0_disable(dev_priv);
  4923. if (dev_priv->cdclk_pll.vco != vco)
  4924. skl_dpll0_enable(dev_priv, vco);
  4925. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  4926. POSTING_READ(CDCLK_CTL);
  4927. /* inform PCU of the change */
  4928. mutex_lock(&dev_priv->rps.hw_lock);
  4929. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4930. mutex_unlock(&dev_priv->rps.hw_lock);
  4931. intel_update_cdclk(dev);
  4932. }
  4933. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  4934. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4935. {
  4936. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  4937. }
  4938. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4939. {
  4940. int cdclk, vco;
  4941. skl_sanitize_cdclk(dev_priv);
  4942. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  4943. /*
  4944. * Use the current vco as our initial
  4945. * guess as to what the preferred vco is.
  4946. */
  4947. if (dev_priv->skl_preferred_vco_freq == 0)
  4948. skl_set_preferred_cdclk_vco(dev_priv,
  4949. dev_priv->cdclk_pll.vco);
  4950. return;
  4951. }
  4952. vco = dev_priv->skl_preferred_vco_freq;
  4953. if (vco == 0)
  4954. vco = 8100000;
  4955. cdclk = skl_calc_cdclk(0, vco);
  4956. skl_set_cdclk(dev_priv, cdclk, vco);
  4957. }
  4958. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4959. {
  4960. uint32_t cdctl, expected;
  4961. /*
  4962. * check if the pre-os intialized the display
  4963. * There is SWF18 scratchpad register defined which is set by the
  4964. * pre-os which can be used by the OS drivers to check the status
  4965. */
  4966. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4967. goto sanitize;
  4968. intel_update_cdclk(&dev_priv->drm);
  4969. /* Is PLL enabled and locked ? */
  4970. if (dev_priv->cdclk_pll.vco == 0 ||
  4971. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  4972. goto sanitize;
  4973. /* DPLL okay; verify the cdclock
  4974. *
  4975. * Noticed in some instances that the freq selection is correct but
  4976. * decimal part is programmed wrong from BIOS where pre-os does not
  4977. * enable display. Verify the same as well.
  4978. */
  4979. cdctl = I915_READ(CDCLK_CTL);
  4980. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  4981. skl_cdclk_decimal(dev_priv->cdclk_freq);
  4982. if (cdctl == expected)
  4983. /* All well; nothing to sanitize */
  4984. return;
  4985. sanitize:
  4986. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  4987. /* force cdclk programming */
  4988. dev_priv->cdclk_freq = 0;
  4989. /* force full PLL disable + enable */
  4990. dev_priv->cdclk_pll.vco = -1;
  4991. }
  4992. /* Adjust CDclk dividers to allow high res or save power if possible */
  4993. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4994. {
  4995. struct drm_i915_private *dev_priv = to_i915(dev);
  4996. u32 val, cmd;
  4997. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4998. != dev_priv->cdclk_freq);
  4999. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  5000. cmd = 2;
  5001. else if (cdclk == 266667)
  5002. cmd = 1;
  5003. else
  5004. cmd = 0;
  5005. mutex_lock(&dev_priv->rps.hw_lock);
  5006. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5007. val &= ~DSPFREQGUAR_MASK;
  5008. val |= (cmd << DSPFREQGUAR_SHIFT);
  5009. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5010. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5011. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  5012. 50)) {
  5013. DRM_ERROR("timed out waiting for CDclk change\n");
  5014. }
  5015. mutex_unlock(&dev_priv->rps.hw_lock);
  5016. mutex_lock(&dev_priv->sb_lock);
  5017. if (cdclk == 400000) {
  5018. u32 divider;
  5019. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5020. /* adjust cdclk divider */
  5021. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5022. val &= ~CCK_FREQUENCY_VALUES;
  5023. val |= divider;
  5024. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  5025. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  5026. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  5027. 50))
  5028. DRM_ERROR("timed out waiting for CDclk change\n");
  5029. }
  5030. /* adjust self-refresh exit latency value */
  5031. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  5032. val &= ~0x7f;
  5033. /*
  5034. * For high bandwidth configs, we set a higher latency in the bunit
  5035. * so that the core display fetch happens in time to avoid underruns.
  5036. */
  5037. if (cdclk == 400000)
  5038. val |= 4500 / 250; /* 4.5 usec */
  5039. else
  5040. val |= 3000 / 250; /* 3.0 usec */
  5041. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  5042. mutex_unlock(&dev_priv->sb_lock);
  5043. intel_update_cdclk(dev);
  5044. }
  5045. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  5046. {
  5047. struct drm_i915_private *dev_priv = to_i915(dev);
  5048. u32 val, cmd;
  5049. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  5050. != dev_priv->cdclk_freq);
  5051. switch (cdclk) {
  5052. case 333333:
  5053. case 320000:
  5054. case 266667:
  5055. case 200000:
  5056. break;
  5057. default:
  5058. MISSING_CASE(cdclk);
  5059. return;
  5060. }
  5061. /*
  5062. * Specs are full of misinformation, but testing on actual
  5063. * hardware has shown that we just need to write the desired
  5064. * CCK divider into the Punit register.
  5065. */
  5066. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5067. mutex_lock(&dev_priv->rps.hw_lock);
  5068. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5069. val &= ~DSPFREQGUAR_MASK_CHV;
  5070. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5071. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5072. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5073. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5074. 50)) {
  5075. DRM_ERROR("timed out waiting for CDclk change\n");
  5076. }
  5077. mutex_unlock(&dev_priv->rps.hw_lock);
  5078. intel_update_cdclk(dev);
  5079. }
  5080. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5081. int max_pixclk)
  5082. {
  5083. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5084. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5085. /*
  5086. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5087. * 200MHz
  5088. * 267MHz
  5089. * 320/333MHz (depends on HPLL freq)
  5090. * 400MHz (VLV only)
  5091. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5092. * of the lower bin and adjust if needed.
  5093. *
  5094. * We seem to get an unstable or solid color picture at 200MHz.
  5095. * Not sure what's wrong. For now use 200MHz only when all pipes
  5096. * are off.
  5097. */
  5098. if (!IS_CHERRYVIEW(dev_priv) &&
  5099. max_pixclk > freq_320*limit/100)
  5100. return 400000;
  5101. else if (max_pixclk > 266667*limit/100)
  5102. return freq_320;
  5103. else if (max_pixclk > 0)
  5104. return 266667;
  5105. else
  5106. return 200000;
  5107. }
  5108. static int bxt_calc_cdclk(int max_pixclk)
  5109. {
  5110. if (max_pixclk > 576000)
  5111. return 624000;
  5112. else if (max_pixclk > 384000)
  5113. return 576000;
  5114. else if (max_pixclk > 288000)
  5115. return 384000;
  5116. else if (max_pixclk > 144000)
  5117. return 288000;
  5118. else
  5119. return 144000;
  5120. }
  5121. /* Compute the max pixel clock for new configuration. */
  5122. static int intel_mode_max_pixclk(struct drm_device *dev,
  5123. struct drm_atomic_state *state)
  5124. {
  5125. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5126. struct drm_i915_private *dev_priv = to_i915(dev);
  5127. struct drm_crtc *crtc;
  5128. struct drm_crtc_state *crtc_state;
  5129. unsigned max_pixclk = 0, i;
  5130. enum pipe pipe;
  5131. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5132. sizeof(intel_state->min_pixclk));
  5133. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5134. int pixclk = 0;
  5135. if (crtc_state->enable)
  5136. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5137. intel_state->min_pixclk[i] = pixclk;
  5138. }
  5139. for_each_pipe(dev_priv, pipe)
  5140. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5141. return max_pixclk;
  5142. }
  5143. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5144. {
  5145. struct drm_device *dev = state->dev;
  5146. struct drm_i915_private *dev_priv = to_i915(dev);
  5147. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5148. struct intel_atomic_state *intel_state =
  5149. to_intel_atomic_state(state);
  5150. intel_state->cdclk = intel_state->dev_cdclk =
  5151. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5152. if (!intel_state->active_crtcs)
  5153. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5154. return 0;
  5155. }
  5156. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  5157. {
  5158. int max_pixclk = ilk_max_pixel_rate(state);
  5159. struct intel_atomic_state *intel_state =
  5160. to_intel_atomic_state(state);
  5161. intel_state->cdclk = intel_state->dev_cdclk =
  5162. bxt_calc_cdclk(max_pixclk);
  5163. if (!intel_state->active_crtcs)
  5164. intel_state->dev_cdclk = bxt_calc_cdclk(0);
  5165. return 0;
  5166. }
  5167. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5168. {
  5169. unsigned int credits, default_credits;
  5170. if (IS_CHERRYVIEW(dev_priv))
  5171. default_credits = PFI_CREDIT(12);
  5172. else
  5173. default_credits = PFI_CREDIT(8);
  5174. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5175. /* CHV suggested value is 31 or 63 */
  5176. if (IS_CHERRYVIEW(dev_priv))
  5177. credits = PFI_CREDIT_63;
  5178. else
  5179. credits = PFI_CREDIT(15);
  5180. } else {
  5181. credits = default_credits;
  5182. }
  5183. /*
  5184. * WA - write default credits before re-programming
  5185. * FIXME: should we also set the resend bit here?
  5186. */
  5187. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5188. default_credits);
  5189. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5190. credits | PFI_CREDIT_RESEND);
  5191. /*
  5192. * FIXME is this guaranteed to clear
  5193. * immediately or should we poll for it?
  5194. */
  5195. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5196. }
  5197. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5198. {
  5199. struct drm_device *dev = old_state->dev;
  5200. struct drm_i915_private *dev_priv = to_i915(dev);
  5201. struct intel_atomic_state *old_intel_state =
  5202. to_intel_atomic_state(old_state);
  5203. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5204. /*
  5205. * FIXME: We can end up here with all power domains off, yet
  5206. * with a CDCLK frequency other than the minimum. To account
  5207. * for this take the PIPE-A power domain, which covers the HW
  5208. * blocks needed for the following programming. This can be
  5209. * removed once it's guaranteed that we get here either with
  5210. * the minimum CDCLK set, or the required power domains
  5211. * enabled.
  5212. */
  5213. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5214. if (IS_CHERRYVIEW(dev))
  5215. cherryview_set_cdclk(dev, req_cdclk);
  5216. else
  5217. valleyview_set_cdclk(dev, req_cdclk);
  5218. vlv_program_pfi_credits(dev_priv);
  5219. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5220. }
  5221. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5222. {
  5223. struct drm_device *dev = crtc->dev;
  5224. struct drm_i915_private *dev_priv = to_i915(dev);
  5225. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5226. struct intel_encoder *encoder;
  5227. struct intel_crtc_state *pipe_config =
  5228. to_intel_crtc_state(crtc->state);
  5229. int pipe = intel_crtc->pipe;
  5230. if (WARN_ON(intel_crtc->active))
  5231. return;
  5232. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5233. intel_dp_set_m_n(intel_crtc, M1_N1);
  5234. intel_set_pipe_timings(intel_crtc);
  5235. intel_set_pipe_src_size(intel_crtc);
  5236. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5237. struct drm_i915_private *dev_priv = to_i915(dev);
  5238. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5239. I915_WRITE(CHV_CANVAS(pipe), 0);
  5240. }
  5241. i9xx_set_pipeconf(intel_crtc);
  5242. intel_crtc->active = true;
  5243. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5244. for_each_encoder_on_crtc(dev, crtc, encoder)
  5245. if (encoder->pre_pll_enable)
  5246. encoder->pre_pll_enable(encoder);
  5247. if (IS_CHERRYVIEW(dev)) {
  5248. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5249. chv_enable_pll(intel_crtc, intel_crtc->config);
  5250. } else {
  5251. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5252. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5253. }
  5254. for_each_encoder_on_crtc(dev, crtc, encoder)
  5255. if (encoder->pre_enable)
  5256. encoder->pre_enable(encoder);
  5257. i9xx_pfit_enable(intel_crtc);
  5258. intel_color_load_luts(&pipe_config->base);
  5259. intel_update_watermarks(crtc);
  5260. intel_enable_pipe(intel_crtc);
  5261. assert_vblank_disabled(crtc);
  5262. drm_crtc_vblank_on(crtc);
  5263. for_each_encoder_on_crtc(dev, crtc, encoder)
  5264. encoder->enable(encoder);
  5265. }
  5266. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5267. {
  5268. struct drm_device *dev = crtc->base.dev;
  5269. struct drm_i915_private *dev_priv = to_i915(dev);
  5270. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5271. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5272. }
  5273. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5274. {
  5275. struct drm_device *dev = crtc->dev;
  5276. struct drm_i915_private *dev_priv = to_i915(dev);
  5277. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5278. struct intel_encoder *encoder;
  5279. struct intel_crtc_state *pipe_config =
  5280. to_intel_crtc_state(crtc->state);
  5281. enum pipe pipe = intel_crtc->pipe;
  5282. if (WARN_ON(intel_crtc->active))
  5283. return;
  5284. i9xx_set_pll_dividers(intel_crtc);
  5285. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5286. intel_dp_set_m_n(intel_crtc, M1_N1);
  5287. intel_set_pipe_timings(intel_crtc);
  5288. intel_set_pipe_src_size(intel_crtc);
  5289. i9xx_set_pipeconf(intel_crtc);
  5290. intel_crtc->active = true;
  5291. if (!IS_GEN2(dev))
  5292. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5293. for_each_encoder_on_crtc(dev, crtc, encoder)
  5294. if (encoder->pre_enable)
  5295. encoder->pre_enable(encoder);
  5296. i9xx_enable_pll(intel_crtc);
  5297. i9xx_pfit_enable(intel_crtc);
  5298. intel_color_load_luts(&pipe_config->base);
  5299. intel_update_watermarks(crtc);
  5300. intel_enable_pipe(intel_crtc);
  5301. assert_vblank_disabled(crtc);
  5302. drm_crtc_vblank_on(crtc);
  5303. for_each_encoder_on_crtc(dev, crtc, encoder)
  5304. encoder->enable(encoder);
  5305. }
  5306. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5307. {
  5308. struct drm_device *dev = crtc->base.dev;
  5309. struct drm_i915_private *dev_priv = to_i915(dev);
  5310. if (!crtc->config->gmch_pfit.control)
  5311. return;
  5312. assert_pipe_disabled(dev_priv, crtc->pipe);
  5313. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5314. I915_READ(PFIT_CONTROL));
  5315. I915_WRITE(PFIT_CONTROL, 0);
  5316. }
  5317. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5318. {
  5319. struct drm_device *dev = crtc->dev;
  5320. struct drm_i915_private *dev_priv = to_i915(dev);
  5321. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5322. struct intel_encoder *encoder;
  5323. int pipe = intel_crtc->pipe;
  5324. /*
  5325. * On gen2 planes are double buffered but the pipe isn't, so we must
  5326. * wait for planes to fully turn off before disabling the pipe.
  5327. */
  5328. if (IS_GEN2(dev))
  5329. intel_wait_for_vblank(dev, pipe);
  5330. for_each_encoder_on_crtc(dev, crtc, encoder)
  5331. encoder->disable(encoder);
  5332. drm_crtc_vblank_off(crtc);
  5333. assert_vblank_disabled(crtc);
  5334. intel_disable_pipe(intel_crtc);
  5335. i9xx_pfit_disable(intel_crtc);
  5336. for_each_encoder_on_crtc(dev, crtc, encoder)
  5337. if (encoder->post_disable)
  5338. encoder->post_disable(encoder);
  5339. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5340. if (IS_CHERRYVIEW(dev))
  5341. chv_disable_pll(dev_priv, pipe);
  5342. else if (IS_VALLEYVIEW(dev))
  5343. vlv_disable_pll(dev_priv, pipe);
  5344. else
  5345. i9xx_disable_pll(intel_crtc);
  5346. }
  5347. for_each_encoder_on_crtc(dev, crtc, encoder)
  5348. if (encoder->post_pll_disable)
  5349. encoder->post_pll_disable(encoder);
  5350. if (!IS_GEN2(dev))
  5351. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5352. }
  5353. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5354. {
  5355. struct intel_encoder *encoder;
  5356. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5357. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5358. enum intel_display_power_domain domain;
  5359. unsigned long domains;
  5360. if (!intel_crtc->active)
  5361. return;
  5362. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5363. WARN_ON(intel_crtc->flip_work);
  5364. intel_pre_disable_primary_noatomic(crtc);
  5365. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5366. to_intel_plane_state(crtc->primary->state)->visible = false;
  5367. }
  5368. dev_priv->display.crtc_disable(crtc);
  5369. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5370. crtc->base.id, crtc->name);
  5371. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5372. crtc->state->active = false;
  5373. intel_crtc->active = false;
  5374. crtc->enabled = false;
  5375. crtc->state->connector_mask = 0;
  5376. crtc->state->encoder_mask = 0;
  5377. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5378. encoder->base.crtc = NULL;
  5379. intel_fbc_disable(intel_crtc);
  5380. intel_update_watermarks(crtc);
  5381. intel_disable_shared_dpll(intel_crtc);
  5382. domains = intel_crtc->enabled_power_domains;
  5383. for_each_power_domain(domain, domains)
  5384. intel_display_power_put(dev_priv, domain);
  5385. intel_crtc->enabled_power_domains = 0;
  5386. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5387. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5388. }
  5389. /*
  5390. * turn all crtc's off, but do not adjust state
  5391. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5392. */
  5393. int intel_display_suspend(struct drm_device *dev)
  5394. {
  5395. struct drm_i915_private *dev_priv = to_i915(dev);
  5396. struct drm_atomic_state *state;
  5397. int ret;
  5398. state = drm_atomic_helper_suspend(dev);
  5399. ret = PTR_ERR_OR_ZERO(state);
  5400. if (ret)
  5401. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5402. else
  5403. dev_priv->modeset_restore_state = state;
  5404. return ret;
  5405. }
  5406. void intel_encoder_destroy(struct drm_encoder *encoder)
  5407. {
  5408. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5409. drm_encoder_cleanup(encoder);
  5410. kfree(intel_encoder);
  5411. }
  5412. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5413. * internal consistency). */
  5414. static void intel_connector_verify_state(struct intel_connector *connector)
  5415. {
  5416. struct drm_crtc *crtc = connector->base.state->crtc;
  5417. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5418. connector->base.base.id,
  5419. connector->base.name);
  5420. if (connector->get_hw_state(connector)) {
  5421. struct intel_encoder *encoder = connector->encoder;
  5422. struct drm_connector_state *conn_state = connector->base.state;
  5423. I915_STATE_WARN(!crtc,
  5424. "connector enabled without attached crtc\n");
  5425. if (!crtc)
  5426. return;
  5427. I915_STATE_WARN(!crtc->state->active,
  5428. "connector is active, but attached crtc isn't\n");
  5429. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5430. return;
  5431. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5432. "atomic encoder doesn't match attached encoder\n");
  5433. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5434. "attached encoder crtc differs from connector crtc\n");
  5435. } else {
  5436. I915_STATE_WARN(crtc && crtc->state->active,
  5437. "attached crtc is active, but connector isn't\n");
  5438. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5439. "best encoder set without crtc!\n");
  5440. }
  5441. }
  5442. int intel_connector_init(struct intel_connector *connector)
  5443. {
  5444. drm_atomic_helper_connector_reset(&connector->base);
  5445. if (!connector->base.state)
  5446. return -ENOMEM;
  5447. return 0;
  5448. }
  5449. struct intel_connector *intel_connector_alloc(void)
  5450. {
  5451. struct intel_connector *connector;
  5452. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5453. if (!connector)
  5454. return NULL;
  5455. if (intel_connector_init(connector) < 0) {
  5456. kfree(connector);
  5457. return NULL;
  5458. }
  5459. return connector;
  5460. }
  5461. /* Simple connector->get_hw_state implementation for encoders that support only
  5462. * one connector and no cloning and hence the encoder state determines the state
  5463. * of the connector. */
  5464. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5465. {
  5466. enum pipe pipe = 0;
  5467. struct intel_encoder *encoder = connector->encoder;
  5468. return encoder->get_hw_state(encoder, &pipe);
  5469. }
  5470. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5471. {
  5472. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5473. return crtc_state->fdi_lanes;
  5474. return 0;
  5475. }
  5476. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5477. struct intel_crtc_state *pipe_config)
  5478. {
  5479. struct drm_atomic_state *state = pipe_config->base.state;
  5480. struct intel_crtc *other_crtc;
  5481. struct intel_crtc_state *other_crtc_state;
  5482. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5483. pipe_name(pipe), pipe_config->fdi_lanes);
  5484. if (pipe_config->fdi_lanes > 4) {
  5485. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5486. pipe_name(pipe), pipe_config->fdi_lanes);
  5487. return -EINVAL;
  5488. }
  5489. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5490. if (pipe_config->fdi_lanes > 2) {
  5491. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5492. pipe_config->fdi_lanes);
  5493. return -EINVAL;
  5494. } else {
  5495. return 0;
  5496. }
  5497. }
  5498. if (INTEL_INFO(dev)->num_pipes == 2)
  5499. return 0;
  5500. /* Ivybridge 3 pipe is really complicated */
  5501. switch (pipe) {
  5502. case PIPE_A:
  5503. return 0;
  5504. case PIPE_B:
  5505. if (pipe_config->fdi_lanes <= 2)
  5506. return 0;
  5507. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5508. other_crtc_state =
  5509. intel_atomic_get_crtc_state(state, other_crtc);
  5510. if (IS_ERR(other_crtc_state))
  5511. return PTR_ERR(other_crtc_state);
  5512. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5513. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5514. pipe_name(pipe), pipe_config->fdi_lanes);
  5515. return -EINVAL;
  5516. }
  5517. return 0;
  5518. case PIPE_C:
  5519. if (pipe_config->fdi_lanes > 2) {
  5520. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5521. pipe_name(pipe), pipe_config->fdi_lanes);
  5522. return -EINVAL;
  5523. }
  5524. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5525. other_crtc_state =
  5526. intel_atomic_get_crtc_state(state, other_crtc);
  5527. if (IS_ERR(other_crtc_state))
  5528. return PTR_ERR(other_crtc_state);
  5529. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5530. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5531. return -EINVAL;
  5532. }
  5533. return 0;
  5534. default:
  5535. BUG();
  5536. }
  5537. }
  5538. #define RETRY 1
  5539. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5540. struct intel_crtc_state *pipe_config)
  5541. {
  5542. struct drm_device *dev = intel_crtc->base.dev;
  5543. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5544. int lane, link_bw, fdi_dotclock, ret;
  5545. bool needs_recompute = false;
  5546. retry:
  5547. /* FDI is a binary signal running at ~2.7GHz, encoding
  5548. * each output octet as 10 bits. The actual frequency
  5549. * is stored as a divider into a 100MHz clock, and the
  5550. * mode pixel clock is stored in units of 1KHz.
  5551. * Hence the bw of each lane in terms of the mode signal
  5552. * is:
  5553. */
  5554. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5555. fdi_dotclock = adjusted_mode->crtc_clock;
  5556. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5557. pipe_config->pipe_bpp);
  5558. pipe_config->fdi_lanes = lane;
  5559. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5560. link_bw, &pipe_config->fdi_m_n);
  5561. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5562. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5563. pipe_config->pipe_bpp -= 2*3;
  5564. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5565. pipe_config->pipe_bpp);
  5566. needs_recompute = true;
  5567. pipe_config->bw_constrained = true;
  5568. goto retry;
  5569. }
  5570. if (needs_recompute)
  5571. return RETRY;
  5572. return ret;
  5573. }
  5574. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5575. struct intel_crtc_state *pipe_config)
  5576. {
  5577. if (pipe_config->pipe_bpp > 24)
  5578. return false;
  5579. /* HSW can handle pixel rate up to cdclk? */
  5580. if (IS_HASWELL(dev_priv))
  5581. return true;
  5582. /*
  5583. * We compare against max which means we must take
  5584. * the increased cdclk requirement into account when
  5585. * calculating the new cdclk.
  5586. *
  5587. * Should measure whether using a lower cdclk w/o IPS
  5588. */
  5589. return ilk_pipe_pixel_rate(pipe_config) <=
  5590. dev_priv->max_cdclk_freq * 95 / 100;
  5591. }
  5592. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5593. struct intel_crtc_state *pipe_config)
  5594. {
  5595. struct drm_device *dev = crtc->base.dev;
  5596. struct drm_i915_private *dev_priv = to_i915(dev);
  5597. pipe_config->ips_enabled = i915.enable_ips &&
  5598. hsw_crtc_supports_ips(crtc) &&
  5599. pipe_config_supports_ips(dev_priv, pipe_config);
  5600. }
  5601. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5602. {
  5603. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5604. /* GDG double wide on either pipe, otherwise pipe A only */
  5605. return INTEL_INFO(dev_priv)->gen < 4 &&
  5606. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5607. }
  5608. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5609. struct intel_crtc_state *pipe_config)
  5610. {
  5611. struct drm_device *dev = crtc->base.dev;
  5612. struct drm_i915_private *dev_priv = to_i915(dev);
  5613. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5614. int clock_limit = dev_priv->max_dotclk_freq;
  5615. if (INTEL_INFO(dev)->gen < 4) {
  5616. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5617. /*
  5618. * Enable double wide mode when the dot clock
  5619. * is > 90% of the (display) core speed.
  5620. */
  5621. if (intel_crtc_supports_double_wide(crtc) &&
  5622. adjusted_mode->crtc_clock > clock_limit) {
  5623. clock_limit = dev_priv->max_dotclk_freq;
  5624. pipe_config->double_wide = true;
  5625. }
  5626. }
  5627. if (adjusted_mode->crtc_clock > clock_limit) {
  5628. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5629. adjusted_mode->crtc_clock, clock_limit,
  5630. yesno(pipe_config->double_wide));
  5631. return -EINVAL;
  5632. }
  5633. /*
  5634. * Pipe horizontal size must be even in:
  5635. * - DVO ganged mode
  5636. * - LVDS dual channel mode
  5637. * - Double wide pipe
  5638. */
  5639. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5640. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5641. pipe_config->pipe_src_w &= ~1;
  5642. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5643. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5644. */
  5645. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5646. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5647. return -EINVAL;
  5648. if (HAS_IPS(dev))
  5649. hsw_compute_ips_config(crtc, pipe_config);
  5650. if (pipe_config->has_pch_encoder)
  5651. return ironlake_fdi_compute_config(crtc, pipe_config);
  5652. return 0;
  5653. }
  5654. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5655. {
  5656. struct drm_i915_private *dev_priv = to_i915(dev);
  5657. uint32_t cdctl;
  5658. skl_dpll0_update(dev_priv);
  5659. if (dev_priv->cdclk_pll.vco == 0)
  5660. return dev_priv->cdclk_pll.ref;
  5661. cdctl = I915_READ(CDCLK_CTL);
  5662. if (dev_priv->cdclk_pll.vco == 8640000) {
  5663. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5664. case CDCLK_FREQ_450_432:
  5665. return 432000;
  5666. case CDCLK_FREQ_337_308:
  5667. return 308571;
  5668. case CDCLK_FREQ_540:
  5669. return 540000;
  5670. case CDCLK_FREQ_675_617:
  5671. return 617143;
  5672. default:
  5673. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5674. }
  5675. } else {
  5676. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5677. case CDCLK_FREQ_450_432:
  5678. return 450000;
  5679. case CDCLK_FREQ_337_308:
  5680. return 337500;
  5681. case CDCLK_FREQ_540:
  5682. return 540000;
  5683. case CDCLK_FREQ_675_617:
  5684. return 675000;
  5685. default:
  5686. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5687. }
  5688. }
  5689. return dev_priv->cdclk_pll.ref;
  5690. }
  5691. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  5692. {
  5693. u32 val;
  5694. dev_priv->cdclk_pll.ref = 19200;
  5695. dev_priv->cdclk_pll.vco = 0;
  5696. val = I915_READ(BXT_DE_PLL_ENABLE);
  5697. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  5698. return;
  5699. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  5700. return;
  5701. val = I915_READ(BXT_DE_PLL_CTL);
  5702. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  5703. dev_priv->cdclk_pll.ref;
  5704. }
  5705. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5706. {
  5707. struct drm_i915_private *dev_priv = to_i915(dev);
  5708. u32 divider;
  5709. int div, vco;
  5710. bxt_de_pll_update(dev_priv);
  5711. vco = dev_priv->cdclk_pll.vco;
  5712. if (vco == 0)
  5713. return dev_priv->cdclk_pll.ref;
  5714. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  5715. switch (divider) {
  5716. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5717. div = 2;
  5718. break;
  5719. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5720. div = 3;
  5721. break;
  5722. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5723. div = 4;
  5724. break;
  5725. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5726. div = 8;
  5727. break;
  5728. default:
  5729. MISSING_CASE(divider);
  5730. return dev_priv->cdclk_pll.ref;
  5731. }
  5732. return DIV_ROUND_CLOSEST(vco, div);
  5733. }
  5734. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5735. {
  5736. struct drm_i915_private *dev_priv = to_i915(dev);
  5737. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5738. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5739. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5740. return 800000;
  5741. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5742. return 450000;
  5743. else if (freq == LCPLL_CLK_FREQ_450)
  5744. return 450000;
  5745. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5746. return 540000;
  5747. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5748. return 337500;
  5749. else
  5750. return 675000;
  5751. }
  5752. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5753. {
  5754. struct drm_i915_private *dev_priv = to_i915(dev);
  5755. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5756. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5757. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5758. return 800000;
  5759. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5760. return 450000;
  5761. else if (freq == LCPLL_CLK_FREQ_450)
  5762. return 450000;
  5763. else if (IS_HSW_ULT(dev))
  5764. return 337500;
  5765. else
  5766. return 540000;
  5767. }
  5768. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5769. {
  5770. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5771. CCK_DISPLAY_CLOCK_CONTROL);
  5772. }
  5773. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5774. {
  5775. return 450000;
  5776. }
  5777. static int i945_get_display_clock_speed(struct drm_device *dev)
  5778. {
  5779. return 400000;
  5780. }
  5781. static int i915_get_display_clock_speed(struct drm_device *dev)
  5782. {
  5783. return 333333;
  5784. }
  5785. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5786. {
  5787. return 200000;
  5788. }
  5789. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5790. {
  5791. u16 gcfgc = 0;
  5792. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5793. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5794. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5795. return 266667;
  5796. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5797. return 333333;
  5798. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5799. return 444444;
  5800. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5801. return 200000;
  5802. default:
  5803. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5804. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5805. return 133333;
  5806. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5807. return 166667;
  5808. }
  5809. }
  5810. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5811. {
  5812. u16 gcfgc = 0;
  5813. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5814. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5815. return 133333;
  5816. else {
  5817. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5818. case GC_DISPLAY_CLOCK_333_MHZ:
  5819. return 333333;
  5820. default:
  5821. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5822. return 190000;
  5823. }
  5824. }
  5825. }
  5826. static int i865_get_display_clock_speed(struct drm_device *dev)
  5827. {
  5828. return 266667;
  5829. }
  5830. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5831. {
  5832. u16 hpllcc = 0;
  5833. /*
  5834. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5835. * encoding is different :(
  5836. * FIXME is this the right way to detect 852GM/852GMV?
  5837. */
  5838. if (dev->pdev->revision == 0x1)
  5839. return 133333;
  5840. pci_bus_read_config_word(dev->pdev->bus,
  5841. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5842. /* Assume that the hardware is in the high speed state. This
  5843. * should be the default.
  5844. */
  5845. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5846. case GC_CLOCK_133_200:
  5847. case GC_CLOCK_133_200_2:
  5848. case GC_CLOCK_100_200:
  5849. return 200000;
  5850. case GC_CLOCK_166_250:
  5851. return 250000;
  5852. case GC_CLOCK_100_133:
  5853. return 133333;
  5854. case GC_CLOCK_133_266:
  5855. case GC_CLOCK_133_266_2:
  5856. case GC_CLOCK_166_266:
  5857. return 266667;
  5858. }
  5859. /* Shouldn't happen */
  5860. return 0;
  5861. }
  5862. static int i830_get_display_clock_speed(struct drm_device *dev)
  5863. {
  5864. return 133333;
  5865. }
  5866. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5867. {
  5868. struct drm_i915_private *dev_priv = to_i915(dev);
  5869. static const unsigned int blb_vco[8] = {
  5870. [0] = 3200000,
  5871. [1] = 4000000,
  5872. [2] = 5333333,
  5873. [3] = 4800000,
  5874. [4] = 6400000,
  5875. };
  5876. static const unsigned int pnv_vco[8] = {
  5877. [0] = 3200000,
  5878. [1] = 4000000,
  5879. [2] = 5333333,
  5880. [3] = 4800000,
  5881. [4] = 2666667,
  5882. };
  5883. static const unsigned int cl_vco[8] = {
  5884. [0] = 3200000,
  5885. [1] = 4000000,
  5886. [2] = 5333333,
  5887. [3] = 6400000,
  5888. [4] = 3333333,
  5889. [5] = 3566667,
  5890. [6] = 4266667,
  5891. };
  5892. static const unsigned int elk_vco[8] = {
  5893. [0] = 3200000,
  5894. [1] = 4000000,
  5895. [2] = 5333333,
  5896. [3] = 4800000,
  5897. };
  5898. static const unsigned int ctg_vco[8] = {
  5899. [0] = 3200000,
  5900. [1] = 4000000,
  5901. [2] = 5333333,
  5902. [3] = 6400000,
  5903. [4] = 2666667,
  5904. [5] = 4266667,
  5905. };
  5906. const unsigned int *vco_table;
  5907. unsigned int vco;
  5908. uint8_t tmp = 0;
  5909. /* FIXME other chipsets? */
  5910. if (IS_GM45(dev))
  5911. vco_table = ctg_vco;
  5912. else if (IS_G4X(dev))
  5913. vco_table = elk_vco;
  5914. else if (IS_CRESTLINE(dev))
  5915. vco_table = cl_vco;
  5916. else if (IS_PINEVIEW(dev))
  5917. vco_table = pnv_vco;
  5918. else if (IS_G33(dev))
  5919. vco_table = blb_vco;
  5920. else
  5921. return 0;
  5922. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5923. vco = vco_table[tmp & 0x7];
  5924. if (vco == 0)
  5925. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5926. else
  5927. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5928. return vco;
  5929. }
  5930. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5931. {
  5932. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5933. uint16_t tmp = 0;
  5934. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5935. cdclk_sel = (tmp >> 12) & 0x1;
  5936. switch (vco) {
  5937. case 2666667:
  5938. case 4000000:
  5939. case 5333333:
  5940. return cdclk_sel ? 333333 : 222222;
  5941. case 3200000:
  5942. return cdclk_sel ? 320000 : 228571;
  5943. default:
  5944. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5945. return 222222;
  5946. }
  5947. }
  5948. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5949. {
  5950. static const uint8_t div_3200[] = { 16, 10, 8 };
  5951. static const uint8_t div_4000[] = { 20, 12, 10 };
  5952. static const uint8_t div_5333[] = { 24, 16, 14 };
  5953. const uint8_t *div_table;
  5954. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5955. uint16_t tmp = 0;
  5956. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5957. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5958. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5959. goto fail;
  5960. switch (vco) {
  5961. case 3200000:
  5962. div_table = div_3200;
  5963. break;
  5964. case 4000000:
  5965. div_table = div_4000;
  5966. break;
  5967. case 5333333:
  5968. div_table = div_5333;
  5969. break;
  5970. default:
  5971. goto fail;
  5972. }
  5973. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5974. fail:
  5975. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5976. return 200000;
  5977. }
  5978. static int g33_get_display_clock_speed(struct drm_device *dev)
  5979. {
  5980. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5981. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5982. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5983. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5984. const uint8_t *div_table;
  5985. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5986. uint16_t tmp = 0;
  5987. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5988. cdclk_sel = (tmp >> 4) & 0x7;
  5989. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5990. goto fail;
  5991. switch (vco) {
  5992. case 3200000:
  5993. div_table = div_3200;
  5994. break;
  5995. case 4000000:
  5996. div_table = div_4000;
  5997. break;
  5998. case 4800000:
  5999. div_table = div_4800;
  6000. break;
  6001. case 5333333:
  6002. div_table = div_5333;
  6003. break;
  6004. default:
  6005. goto fail;
  6006. }
  6007. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6008. fail:
  6009. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  6010. return 190476;
  6011. }
  6012. static void
  6013. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  6014. {
  6015. while (*num > DATA_LINK_M_N_MASK ||
  6016. *den > DATA_LINK_M_N_MASK) {
  6017. *num >>= 1;
  6018. *den >>= 1;
  6019. }
  6020. }
  6021. static void compute_m_n(unsigned int m, unsigned int n,
  6022. uint32_t *ret_m, uint32_t *ret_n)
  6023. {
  6024. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  6025. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  6026. intel_reduce_m_n_ratio(ret_m, ret_n);
  6027. }
  6028. void
  6029. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  6030. int pixel_clock, int link_clock,
  6031. struct intel_link_m_n *m_n)
  6032. {
  6033. m_n->tu = 64;
  6034. compute_m_n(bits_per_pixel * pixel_clock,
  6035. link_clock * nlanes * 8,
  6036. &m_n->gmch_m, &m_n->gmch_n);
  6037. compute_m_n(pixel_clock, link_clock,
  6038. &m_n->link_m, &m_n->link_n);
  6039. }
  6040. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  6041. {
  6042. if (i915.panel_use_ssc >= 0)
  6043. return i915.panel_use_ssc != 0;
  6044. return dev_priv->vbt.lvds_use_ssc
  6045. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  6046. }
  6047. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6048. {
  6049. return (1 << dpll->n) << 16 | dpll->m2;
  6050. }
  6051. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6052. {
  6053. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6054. }
  6055. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6056. struct intel_crtc_state *crtc_state,
  6057. struct dpll *reduced_clock)
  6058. {
  6059. struct drm_device *dev = crtc->base.dev;
  6060. u32 fp, fp2 = 0;
  6061. if (IS_PINEVIEW(dev)) {
  6062. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6063. if (reduced_clock)
  6064. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6065. } else {
  6066. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6067. if (reduced_clock)
  6068. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6069. }
  6070. crtc_state->dpll_hw_state.fp0 = fp;
  6071. crtc->lowfreq_avail = false;
  6072. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6073. reduced_clock) {
  6074. crtc_state->dpll_hw_state.fp1 = fp2;
  6075. crtc->lowfreq_avail = true;
  6076. } else {
  6077. crtc_state->dpll_hw_state.fp1 = fp;
  6078. }
  6079. }
  6080. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6081. pipe)
  6082. {
  6083. u32 reg_val;
  6084. /*
  6085. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6086. * and set it to a reasonable value instead.
  6087. */
  6088. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6089. reg_val &= 0xffffff00;
  6090. reg_val |= 0x00000030;
  6091. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6092. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6093. reg_val &= 0x8cffffff;
  6094. reg_val = 0x8c000000;
  6095. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6096. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6097. reg_val &= 0xffffff00;
  6098. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6099. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6100. reg_val &= 0x00ffffff;
  6101. reg_val |= 0xb0000000;
  6102. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6103. }
  6104. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6105. struct intel_link_m_n *m_n)
  6106. {
  6107. struct drm_device *dev = crtc->base.dev;
  6108. struct drm_i915_private *dev_priv = to_i915(dev);
  6109. int pipe = crtc->pipe;
  6110. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6111. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6112. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6113. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6114. }
  6115. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6116. struct intel_link_m_n *m_n,
  6117. struct intel_link_m_n *m2_n2)
  6118. {
  6119. struct drm_device *dev = crtc->base.dev;
  6120. struct drm_i915_private *dev_priv = to_i915(dev);
  6121. int pipe = crtc->pipe;
  6122. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6123. if (INTEL_INFO(dev)->gen >= 5) {
  6124. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6125. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6126. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6127. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6128. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6129. * for gen < 8) and if DRRS is supported (to make sure the
  6130. * registers are not unnecessarily accessed).
  6131. */
  6132. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6133. crtc->config->has_drrs) {
  6134. I915_WRITE(PIPE_DATA_M2(transcoder),
  6135. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6136. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6137. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6138. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6139. }
  6140. } else {
  6141. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6142. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6143. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6144. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6145. }
  6146. }
  6147. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6148. {
  6149. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6150. if (m_n == M1_N1) {
  6151. dp_m_n = &crtc->config->dp_m_n;
  6152. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6153. } else if (m_n == M2_N2) {
  6154. /*
  6155. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6156. * needs to be programmed into M1_N1.
  6157. */
  6158. dp_m_n = &crtc->config->dp_m2_n2;
  6159. } else {
  6160. DRM_ERROR("Unsupported divider value\n");
  6161. return;
  6162. }
  6163. if (crtc->config->has_pch_encoder)
  6164. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6165. else
  6166. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6167. }
  6168. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6169. struct intel_crtc_state *pipe_config)
  6170. {
  6171. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6172. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6173. if (crtc->pipe != PIPE_A)
  6174. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6175. /* DPLL not used with DSI, but still need the rest set up */
  6176. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6177. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6178. DPLL_EXT_BUFFER_ENABLE_VLV;
  6179. pipe_config->dpll_hw_state.dpll_md =
  6180. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6181. }
  6182. static void chv_compute_dpll(struct intel_crtc *crtc,
  6183. struct intel_crtc_state *pipe_config)
  6184. {
  6185. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6186. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6187. if (crtc->pipe != PIPE_A)
  6188. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6189. /* DPLL not used with DSI, but still need the rest set up */
  6190. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6191. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6192. pipe_config->dpll_hw_state.dpll_md =
  6193. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6194. }
  6195. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6196. const struct intel_crtc_state *pipe_config)
  6197. {
  6198. struct drm_device *dev = crtc->base.dev;
  6199. struct drm_i915_private *dev_priv = to_i915(dev);
  6200. enum pipe pipe = crtc->pipe;
  6201. u32 mdiv;
  6202. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6203. u32 coreclk, reg_val;
  6204. /* Enable Refclk */
  6205. I915_WRITE(DPLL(pipe),
  6206. pipe_config->dpll_hw_state.dpll &
  6207. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6208. /* No need to actually set up the DPLL with DSI */
  6209. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6210. return;
  6211. mutex_lock(&dev_priv->sb_lock);
  6212. bestn = pipe_config->dpll.n;
  6213. bestm1 = pipe_config->dpll.m1;
  6214. bestm2 = pipe_config->dpll.m2;
  6215. bestp1 = pipe_config->dpll.p1;
  6216. bestp2 = pipe_config->dpll.p2;
  6217. /* See eDP HDMI DPIO driver vbios notes doc */
  6218. /* PLL B needs special handling */
  6219. if (pipe == PIPE_B)
  6220. vlv_pllb_recal_opamp(dev_priv, pipe);
  6221. /* Set up Tx target for periodic Rcomp update */
  6222. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6223. /* Disable target IRef on PLL */
  6224. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6225. reg_val &= 0x00ffffff;
  6226. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6227. /* Disable fast lock */
  6228. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6229. /* Set idtafcrecal before PLL is enabled */
  6230. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6231. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6232. mdiv |= ((bestn << DPIO_N_SHIFT));
  6233. mdiv |= (1 << DPIO_K_SHIFT);
  6234. /*
  6235. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6236. * but we don't support that).
  6237. * Note: don't use the DAC post divider as it seems unstable.
  6238. */
  6239. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6240. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6241. mdiv |= DPIO_ENABLE_CALIBRATION;
  6242. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6243. /* Set HBR and RBR LPF coefficients */
  6244. if (pipe_config->port_clock == 162000 ||
  6245. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  6246. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  6247. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6248. 0x009f0003);
  6249. else
  6250. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6251. 0x00d0000f);
  6252. if (intel_crtc_has_dp_encoder(pipe_config)) {
  6253. /* Use SSC source */
  6254. if (pipe == PIPE_A)
  6255. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6256. 0x0df40000);
  6257. else
  6258. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6259. 0x0df70000);
  6260. } else { /* HDMI or VGA */
  6261. /* Use bend source */
  6262. if (pipe == PIPE_A)
  6263. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6264. 0x0df70000);
  6265. else
  6266. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6267. 0x0df40000);
  6268. }
  6269. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6270. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6271. if (intel_crtc_has_dp_encoder(crtc->config))
  6272. coreclk |= 0x01000000;
  6273. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6274. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6275. mutex_unlock(&dev_priv->sb_lock);
  6276. }
  6277. static void chv_prepare_pll(struct intel_crtc *crtc,
  6278. const struct intel_crtc_state *pipe_config)
  6279. {
  6280. struct drm_device *dev = crtc->base.dev;
  6281. struct drm_i915_private *dev_priv = to_i915(dev);
  6282. enum pipe pipe = crtc->pipe;
  6283. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6284. u32 loopfilter, tribuf_calcntr;
  6285. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6286. u32 dpio_val;
  6287. int vco;
  6288. /* Enable Refclk and SSC */
  6289. I915_WRITE(DPLL(pipe),
  6290. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6291. /* No need to actually set up the DPLL with DSI */
  6292. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6293. return;
  6294. bestn = pipe_config->dpll.n;
  6295. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6296. bestm1 = pipe_config->dpll.m1;
  6297. bestm2 = pipe_config->dpll.m2 >> 22;
  6298. bestp1 = pipe_config->dpll.p1;
  6299. bestp2 = pipe_config->dpll.p2;
  6300. vco = pipe_config->dpll.vco;
  6301. dpio_val = 0;
  6302. loopfilter = 0;
  6303. mutex_lock(&dev_priv->sb_lock);
  6304. /* p1 and p2 divider */
  6305. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6306. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6307. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6308. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6309. 1 << DPIO_CHV_K_DIV_SHIFT);
  6310. /* Feedback post-divider - m2 */
  6311. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6312. /* Feedback refclk divider - n and m1 */
  6313. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6314. DPIO_CHV_M1_DIV_BY_2 |
  6315. 1 << DPIO_CHV_N_DIV_SHIFT);
  6316. /* M2 fraction division */
  6317. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6318. /* M2 fraction division enable */
  6319. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6320. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6321. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6322. if (bestm2_frac)
  6323. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6324. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6325. /* Program digital lock detect threshold */
  6326. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6327. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6328. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6329. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6330. if (!bestm2_frac)
  6331. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6332. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6333. /* Loop filter */
  6334. if (vco == 5400000) {
  6335. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6336. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6337. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6338. tribuf_calcntr = 0x9;
  6339. } else if (vco <= 6200000) {
  6340. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6341. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6342. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6343. tribuf_calcntr = 0x9;
  6344. } else if (vco <= 6480000) {
  6345. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6346. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6347. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6348. tribuf_calcntr = 0x8;
  6349. } else {
  6350. /* Not supported. Apply the same limits as in the max case */
  6351. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6352. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6353. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6354. tribuf_calcntr = 0;
  6355. }
  6356. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6357. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6358. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6359. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6360. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6361. /* AFC Recal */
  6362. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6363. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6364. DPIO_AFC_RECAL);
  6365. mutex_unlock(&dev_priv->sb_lock);
  6366. }
  6367. /**
  6368. * vlv_force_pll_on - forcibly enable just the PLL
  6369. * @dev_priv: i915 private structure
  6370. * @pipe: pipe PLL to enable
  6371. * @dpll: PLL configuration
  6372. *
  6373. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6374. * in cases where we need the PLL enabled even when @pipe is not going to
  6375. * be enabled.
  6376. */
  6377. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6378. const struct dpll *dpll)
  6379. {
  6380. struct intel_crtc *crtc =
  6381. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6382. struct intel_crtc_state *pipe_config;
  6383. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6384. if (!pipe_config)
  6385. return -ENOMEM;
  6386. pipe_config->base.crtc = &crtc->base;
  6387. pipe_config->pixel_multiplier = 1;
  6388. pipe_config->dpll = *dpll;
  6389. if (IS_CHERRYVIEW(dev)) {
  6390. chv_compute_dpll(crtc, pipe_config);
  6391. chv_prepare_pll(crtc, pipe_config);
  6392. chv_enable_pll(crtc, pipe_config);
  6393. } else {
  6394. vlv_compute_dpll(crtc, pipe_config);
  6395. vlv_prepare_pll(crtc, pipe_config);
  6396. vlv_enable_pll(crtc, pipe_config);
  6397. }
  6398. kfree(pipe_config);
  6399. return 0;
  6400. }
  6401. /**
  6402. * vlv_force_pll_off - forcibly disable just the PLL
  6403. * @dev_priv: i915 private structure
  6404. * @pipe: pipe PLL to disable
  6405. *
  6406. * Disable the PLL for @pipe. To be used in cases where we need
  6407. * the PLL enabled even when @pipe is not going to be enabled.
  6408. */
  6409. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6410. {
  6411. if (IS_CHERRYVIEW(dev))
  6412. chv_disable_pll(to_i915(dev), pipe);
  6413. else
  6414. vlv_disable_pll(to_i915(dev), pipe);
  6415. }
  6416. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6417. struct intel_crtc_state *crtc_state,
  6418. struct dpll *reduced_clock)
  6419. {
  6420. struct drm_device *dev = crtc->base.dev;
  6421. struct drm_i915_private *dev_priv = to_i915(dev);
  6422. u32 dpll;
  6423. struct dpll *clock = &crtc_state->dpll;
  6424. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6425. dpll = DPLL_VGA_MODE_DIS;
  6426. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6427. dpll |= DPLLB_MODE_LVDS;
  6428. else
  6429. dpll |= DPLLB_MODE_DAC_SERIAL;
  6430. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6431. dpll |= (crtc_state->pixel_multiplier - 1)
  6432. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6433. }
  6434. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6435. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6436. dpll |= DPLL_SDVO_HIGH_SPEED;
  6437. if (intel_crtc_has_dp_encoder(crtc_state))
  6438. dpll |= DPLL_SDVO_HIGH_SPEED;
  6439. /* compute bitmask from p1 value */
  6440. if (IS_PINEVIEW(dev))
  6441. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6442. else {
  6443. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6444. if (IS_G4X(dev) && reduced_clock)
  6445. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6446. }
  6447. switch (clock->p2) {
  6448. case 5:
  6449. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6450. break;
  6451. case 7:
  6452. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6453. break;
  6454. case 10:
  6455. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6456. break;
  6457. case 14:
  6458. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6459. break;
  6460. }
  6461. if (INTEL_INFO(dev)->gen >= 4)
  6462. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6463. if (crtc_state->sdvo_tv_clock)
  6464. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6465. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6466. intel_panel_use_ssc(dev_priv))
  6467. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6468. else
  6469. dpll |= PLL_REF_INPUT_DREFCLK;
  6470. dpll |= DPLL_VCO_ENABLE;
  6471. crtc_state->dpll_hw_state.dpll = dpll;
  6472. if (INTEL_INFO(dev)->gen >= 4) {
  6473. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6474. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6475. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6476. }
  6477. }
  6478. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6479. struct intel_crtc_state *crtc_state,
  6480. struct dpll *reduced_clock)
  6481. {
  6482. struct drm_device *dev = crtc->base.dev;
  6483. struct drm_i915_private *dev_priv = to_i915(dev);
  6484. u32 dpll;
  6485. struct dpll *clock = &crtc_state->dpll;
  6486. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6487. dpll = DPLL_VGA_MODE_DIS;
  6488. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6489. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6490. } else {
  6491. if (clock->p1 == 2)
  6492. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6493. else
  6494. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6495. if (clock->p2 == 4)
  6496. dpll |= PLL_P2_DIVIDE_BY_4;
  6497. }
  6498. if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6499. dpll |= DPLL_DVO_2X_MODE;
  6500. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6501. intel_panel_use_ssc(dev_priv))
  6502. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6503. else
  6504. dpll |= PLL_REF_INPUT_DREFCLK;
  6505. dpll |= DPLL_VCO_ENABLE;
  6506. crtc_state->dpll_hw_state.dpll = dpll;
  6507. }
  6508. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6509. {
  6510. struct drm_device *dev = intel_crtc->base.dev;
  6511. struct drm_i915_private *dev_priv = to_i915(dev);
  6512. enum pipe pipe = intel_crtc->pipe;
  6513. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6514. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6515. uint32_t crtc_vtotal, crtc_vblank_end;
  6516. int vsyncshift = 0;
  6517. /* We need to be careful not to changed the adjusted mode, for otherwise
  6518. * the hw state checker will get angry at the mismatch. */
  6519. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6520. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6521. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6522. /* the chip adds 2 halflines automatically */
  6523. crtc_vtotal -= 1;
  6524. crtc_vblank_end -= 1;
  6525. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6526. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6527. else
  6528. vsyncshift = adjusted_mode->crtc_hsync_start -
  6529. adjusted_mode->crtc_htotal / 2;
  6530. if (vsyncshift < 0)
  6531. vsyncshift += adjusted_mode->crtc_htotal;
  6532. }
  6533. if (INTEL_INFO(dev)->gen > 3)
  6534. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6535. I915_WRITE(HTOTAL(cpu_transcoder),
  6536. (adjusted_mode->crtc_hdisplay - 1) |
  6537. ((adjusted_mode->crtc_htotal - 1) << 16));
  6538. I915_WRITE(HBLANK(cpu_transcoder),
  6539. (adjusted_mode->crtc_hblank_start - 1) |
  6540. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6541. I915_WRITE(HSYNC(cpu_transcoder),
  6542. (adjusted_mode->crtc_hsync_start - 1) |
  6543. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6544. I915_WRITE(VTOTAL(cpu_transcoder),
  6545. (adjusted_mode->crtc_vdisplay - 1) |
  6546. ((crtc_vtotal - 1) << 16));
  6547. I915_WRITE(VBLANK(cpu_transcoder),
  6548. (adjusted_mode->crtc_vblank_start - 1) |
  6549. ((crtc_vblank_end - 1) << 16));
  6550. I915_WRITE(VSYNC(cpu_transcoder),
  6551. (adjusted_mode->crtc_vsync_start - 1) |
  6552. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6553. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6554. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6555. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6556. * bits. */
  6557. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6558. (pipe == PIPE_B || pipe == PIPE_C))
  6559. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6560. }
  6561. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6562. {
  6563. struct drm_device *dev = intel_crtc->base.dev;
  6564. struct drm_i915_private *dev_priv = to_i915(dev);
  6565. enum pipe pipe = intel_crtc->pipe;
  6566. /* pipesrc controls the size that is scaled from, which should
  6567. * always be the user's requested size.
  6568. */
  6569. I915_WRITE(PIPESRC(pipe),
  6570. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6571. (intel_crtc->config->pipe_src_h - 1));
  6572. }
  6573. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6574. struct intel_crtc_state *pipe_config)
  6575. {
  6576. struct drm_device *dev = crtc->base.dev;
  6577. struct drm_i915_private *dev_priv = to_i915(dev);
  6578. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6579. uint32_t tmp;
  6580. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6581. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6582. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6583. tmp = I915_READ(HBLANK(cpu_transcoder));
  6584. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6585. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6586. tmp = I915_READ(HSYNC(cpu_transcoder));
  6587. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6588. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6589. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6590. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6591. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6592. tmp = I915_READ(VBLANK(cpu_transcoder));
  6593. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6594. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6595. tmp = I915_READ(VSYNC(cpu_transcoder));
  6596. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6597. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6598. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6599. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6600. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6601. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6602. }
  6603. }
  6604. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6605. struct intel_crtc_state *pipe_config)
  6606. {
  6607. struct drm_device *dev = crtc->base.dev;
  6608. struct drm_i915_private *dev_priv = to_i915(dev);
  6609. u32 tmp;
  6610. tmp = I915_READ(PIPESRC(crtc->pipe));
  6611. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6612. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6613. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6614. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6615. }
  6616. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6617. struct intel_crtc_state *pipe_config)
  6618. {
  6619. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6620. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6621. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6622. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6623. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6624. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6625. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6626. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6627. mode->flags = pipe_config->base.adjusted_mode.flags;
  6628. mode->type = DRM_MODE_TYPE_DRIVER;
  6629. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6630. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6631. mode->hsync = drm_mode_hsync(mode);
  6632. mode->vrefresh = drm_mode_vrefresh(mode);
  6633. drm_mode_set_name(mode);
  6634. }
  6635. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6636. {
  6637. struct drm_device *dev = intel_crtc->base.dev;
  6638. struct drm_i915_private *dev_priv = to_i915(dev);
  6639. uint32_t pipeconf;
  6640. pipeconf = 0;
  6641. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6642. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6643. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6644. if (intel_crtc->config->double_wide)
  6645. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6646. /* only g4x and later have fancy bpc/dither controls */
  6647. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6648. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6649. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6650. pipeconf |= PIPECONF_DITHER_EN |
  6651. PIPECONF_DITHER_TYPE_SP;
  6652. switch (intel_crtc->config->pipe_bpp) {
  6653. case 18:
  6654. pipeconf |= PIPECONF_6BPC;
  6655. break;
  6656. case 24:
  6657. pipeconf |= PIPECONF_8BPC;
  6658. break;
  6659. case 30:
  6660. pipeconf |= PIPECONF_10BPC;
  6661. break;
  6662. default:
  6663. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6664. BUG();
  6665. }
  6666. }
  6667. if (HAS_PIPE_CXSR(dev)) {
  6668. if (intel_crtc->lowfreq_avail) {
  6669. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6670. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6671. } else {
  6672. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6673. }
  6674. }
  6675. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6676. if (INTEL_INFO(dev)->gen < 4 ||
  6677. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6678. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6679. else
  6680. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6681. } else
  6682. pipeconf |= PIPECONF_PROGRESSIVE;
  6683. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6684. intel_crtc->config->limited_color_range)
  6685. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6686. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6687. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6688. }
  6689. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6690. struct intel_crtc_state *crtc_state)
  6691. {
  6692. struct drm_device *dev = crtc->base.dev;
  6693. struct drm_i915_private *dev_priv = to_i915(dev);
  6694. const struct intel_limit *limit;
  6695. int refclk = 48000;
  6696. memset(&crtc_state->dpll_hw_state, 0,
  6697. sizeof(crtc_state->dpll_hw_state));
  6698. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6699. if (intel_panel_use_ssc(dev_priv)) {
  6700. refclk = dev_priv->vbt.lvds_ssc_freq;
  6701. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6702. }
  6703. limit = &intel_limits_i8xx_lvds;
  6704. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6705. limit = &intel_limits_i8xx_dvo;
  6706. } else {
  6707. limit = &intel_limits_i8xx_dac;
  6708. }
  6709. if (!crtc_state->clock_set &&
  6710. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6711. refclk, NULL, &crtc_state->dpll)) {
  6712. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6713. return -EINVAL;
  6714. }
  6715. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6716. return 0;
  6717. }
  6718. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6719. struct intel_crtc_state *crtc_state)
  6720. {
  6721. struct drm_device *dev = crtc->base.dev;
  6722. struct drm_i915_private *dev_priv = to_i915(dev);
  6723. const struct intel_limit *limit;
  6724. int refclk = 96000;
  6725. memset(&crtc_state->dpll_hw_state, 0,
  6726. sizeof(crtc_state->dpll_hw_state));
  6727. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6728. if (intel_panel_use_ssc(dev_priv)) {
  6729. refclk = dev_priv->vbt.lvds_ssc_freq;
  6730. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6731. }
  6732. if (intel_is_dual_link_lvds(dev))
  6733. limit = &intel_limits_g4x_dual_channel_lvds;
  6734. else
  6735. limit = &intel_limits_g4x_single_channel_lvds;
  6736. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6737. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6738. limit = &intel_limits_g4x_hdmi;
  6739. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6740. limit = &intel_limits_g4x_sdvo;
  6741. } else {
  6742. /* The option is for other outputs */
  6743. limit = &intel_limits_i9xx_sdvo;
  6744. }
  6745. if (!crtc_state->clock_set &&
  6746. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6747. refclk, NULL, &crtc_state->dpll)) {
  6748. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6749. return -EINVAL;
  6750. }
  6751. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6752. return 0;
  6753. }
  6754. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6755. struct intel_crtc_state *crtc_state)
  6756. {
  6757. struct drm_device *dev = crtc->base.dev;
  6758. struct drm_i915_private *dev_priv = to_i915(dev);
  6759. const struct intel_limit *limit;
  6760. int refclk = 96000;
  6761. memset(&crtc_state->dpll_hw_state, 0,
  6762. sizeof(crtc_state->dpll_hw_state));
  6763. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6764. if (intel_panel_use_ssc(dev_priv)) {
  6765. refclk = dev_priv->vbt.lvds_ssc_freq;
  6766. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6767. }
  6768. limit = &intel_limits_pineview_lvds;
  6769. } else {
  6770. limit = &intel_limits_pineview_sdvo;
  6771. }
  6772. if (!crtc_state->clock_set &&
  6773. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6774. refclk, NULL, &crtc_state->dpll)) {
  6775. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6776. return -EINVAL;
  6777. }
  6778. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6779. return 0;
  6780. }
  6781. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6782. struct intel_crtc_state *crtc_state)
  6783. {
  6784. struct drm_device *dev = crtc->base.dev;
  6785. struct drm_i915_private *dev_priv = to_i915(dev);
  6786. const struct intel_limit *limit;
  6787. int refclk = 96000;
  6788. memset(&crtc_state->dpll_hw_state, 0,
  6789. sizeof(crtc_state->dpll_hw_state));
  6790. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6791. if (intel_panel_use_ssc(dev_priv)) {
  6792. refclk = dev_priv->vbt.lvds_ssc_freq;
  6793. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6794. }
  6795. limit = &intel_limits_i9xx_lvds;
  6796. } else {
  6797. limit = &intel_limits_i9xx_sdvo;
  6798. }
  6799. if (!crtc_state->clock_set &&
  6800. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6801. refclk, NULL, &crtc_state->dpll)) {
  6802. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6803. return -EINVAL;
  6804. }
  6805. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6806. return 0;
  6807. }
  6808. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6809. struct intel_crtc_state *crtc_state)
  6810. {
  6811. int refclk = 100000;
  6812. const struct intel_limit *limit = &intel_limits_chv;
  6813. memset(&crtc_state->dpll_hw_state, 0,
  6814. sizeof(crtc_state->dpll_hw_state));
  6815. if (!crtc_state->clock_set &&
  6816. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6817. refclk, NULL, &crtc_state->dpll)) {
  6818. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6819. return -EINVAL;
  6820. }
  6821. chv_compute_dpll(crtc, crtc_state);
  6822. return 0;
  6823. }
  6824. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6825. struct intel_crtc_state *crtc_state)
  6826. {
  6827. int refclk = 100000;
  6828. const struct intel_limit *limit = &intel_limits_vlv;
  6829. memset(&crtc_state->dpll_hw_state, 0,
  6830. sizeof(crtc_state->dpll_hw_state));
  6831. if (!crtc_state->clock_set &&
  6832. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6833. refclk, NULL, &crtc_state->dpll)) {
  6834. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6835. return -EINVAL;
  6836. }
  6837. vlv_compute_dpll(crtc, crtc_state);
  6838. return 0;
  6839. }
  6840. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6841. struct intel_crtc_state *pipe_config)
  6842. {
  6843. struct drm_device *dev = crtc->base.dev;
  6844. struct drm_i915_private *dev_priv = to_i915(dev);
  6845. uint32_t tmp;
  6846. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6847. return;
  6848. tmp = I915_READ(PFIT_CONTROL);
  6849. if (!(tmp & PFIT_ENABLE))
  6850. return;
  6851. /* Check whether the pfit is attached to our pipe. */
  6852. if (INTEL_INFO(dev)->gen < 4) {
  6853. if (crtc->pipe != PIPE_B)
  6854. return;
  6855. } else {
  6856. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6857. return;
  6858. }
  6859. pipe_config->gmch_pfit.control = tmp;
  6860. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6861. }
  6862. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6863. struct intel_crtc_state *pipe_config)
  6864. {
  6865. struct drm_device *dev = crtc->base.dev;
  6866. struct drm_i915_private *dev_priv = to_i915(dev);
  6867. int pipe = pipe_config->cpu_transcoder;
  6868. struct dpll clock;
  6869. u32 mdiv;
  6870. int refclk = 100000;
  6871. /* In case of DSI, DPLL will not be used */
  6872. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6873. return;
  6874. mutex_lock(&dev_priv->sb_lock);
  6875. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6876. mutex_unlock(&dev_priv->sb_lock);
  6877. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6878. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6879. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6880. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6881. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6882. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6883. }
  6884. static void
  6885. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6886. struct intel_initial_plane_config *plane_config)
  6887. {
  6888. struct drm_device *dev = crtc->base.dev;
  6889. struct drm_i915_private *dev_priv = to_i915(dev);
  6890. u32 val, base, offset;
  6891. int pipe = crtc->pipe, plane = crtc->plane;
  6892. int fourcc, pixel_format;
  6893. unsigned int aligned_height;
  6894. struct drm_framebuffer *fb;
  6895. struct intel_framebuffer *intel_fb;
  6896. val = I915_READ(DSPCNTR(plane));
  6897. if (!(val & DISPLAY_PLANE_ENABLE))
  6898. return;
  6899. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6900. if (!intel_fb) {
  6901. DRM_DEBUG_KMS("failed to alloc fb\n");
  6902. return;
  6903. }
  6904. fb = &intel_fb->base;
  6905. if (INTEL_INFO(dev)->gen >= 4) {
  6906. if (val & DISPPLANE_TILED) {
  6907. plane_config->tiling = I915_TILING_X;
  6908. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6909. }
  6910. }
  6911. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6912. fourcc = i9xx_format_to_fourcc(pixel_format);
  6913. fb->pixel_format = fourcc;
  6914. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6915. if (INTEL_INFO(dev)->gen >= 4) {
  6916. if (plane_config->tiling)
  6917. offset = I915_READ(DSPTILEOFF(plane));
  6918. else
  6919. offset = I915_READ(DSPLINOFF(plane));
  6920. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6921. } else {
  6922. base = I915_READ(DSPADDR(plane));
  6923. }
  6924. plane_config->base = base;
  6925. val = I915_READ(PIPESRC(pipe));
  6926. fb->width = ((val >> 16) & 0xfff) + 1;
  6927. fb->height = ((val >> 0) & 0xfff) + 1;
  6928. val = I915_READ(DSPSTRIDE(pipe));
  6929. fb->pitches[0] = val & 0xffffffc0;
  6930. aligned_height = intel_fb_align_height(dev, fb->height,
  6931. fb->pixel_format,
  6932. fb->modifier[0]);
  6933. plane_config->size = fb->pitches[0] * aligned_height;
  6934. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6935. pipe_name(pipe), plane, fb->width, fb->height,
  6936. fb->bits_per_pixel, base, fb->pitches[0],
  6937. plane_config->size);
  6938. plane_config->fb = intel_fb;
  6939. }
  6940. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6941. struct intel_crtc_state *pipe_config)
  6942. {
  6943. struct drm_device *dev = crtc->base.dev;
  6944. struct drm_i915_private *dev_priv = to_i915(dev);
  6945. int pipe = pipe_config->cpu_transcoder;
  6946. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6947. struct dpll clock;
  6948. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6949. int refclk = 100000;
  6950. /* In case of DSI, DPLL will not be used */
  6951. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6952. return;
  6953. mutex_lock(&dev_priv->sb_lock);
  6954. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6955. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6956. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6957. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6958. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6959. mutex_unlock(&dev_priv->sb_lock);
  6960. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6961. clock.m2 = (pll_dw0 & 0xff) << 22;
  6962. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6963. clock.m2 |= pll_dw2 & 0x3fffff;
  6964. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6965. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6966. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6967. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6968. }
  6969. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6970. struct intel_crtc_state *pipe_config)
  6971. {
  6972. struct drm_device *dev = crtc->base.dev;
  6973. struct drm_i915_private *dev_priv = to_i915(dev);
  6974. enum intel_display_power_domain power_domain;
  6975. uint32_t tmp;
  6976. bool ret;
  6977. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6978. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6979. return false;
  6980. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6981. pipe_config->shared_dpll = NULL;
  6982. ret = false;
  6983. tmp = I915_READ(PIPECONF(crtc->pipe));
  6984. if (!(tmp & PIPECONF_ENABLE))
  6985. goto out;
  6986. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6987. switch (tmp & PIPECONF_BPC_MASK) {
  6988. case PIPECONF_6BPC:
  6989. pipe_config->pipe_bpp = 18;
  6990. break;
  6991. case PIPECONF_8BPC:
  6992. pipe_config->pipe_bpp = 24;
  6993. break;
  6994. case PIPECONF_10BPC:
  6995. pipe_config->pipe_bpp = 30;
  6996. break;
  6997. default:
  6998. break;
  6999. }
  7000. }
  7001. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  7002. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  7003. pipe_config->limited_color_range = true;
  7004. if (INTEL_INFO(dev)->gen < 4)
  7005. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  7006. intel_get_pipe_timings(crtc, pipe_config);
  7007. intel_get_pipe_src_size(crtc, pipe_config);
  7008. i9xx_get_pfit_config(crtc, pipe_config);
  7009. if (INTEL_INFO(dev)->gen >= 4) {
  7010. /* No way to read it out on pipes B and C */
  7011. if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
  7012. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  7013. else
  7014. tmp = I915_READ(DPLL_MD(crtc->pipe));
  7015. pipe_config->pixel_multiplier =
  7016. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  7017. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  7018. pipe_config->dpll_hw_state.dpll_md = tmp;
  7019. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  7020. tmp = I915_READ(DPLL(crtc->pipe));
  7021. pipe_config->pixel_multiplier =
  7022. ((tmp & SDVO_MULTIPLIER_MASK)
  7023. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  7024. } else {
  7025. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  7026. * port and will be fixed up in the encoder->get_config
  7027. * function. */
  7028. pipe_config->pixel_multiplier = 1;
  7029. }
  7030. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  7031. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  7032. /*
  7033. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  7034. * on 830. Filter it out here so that we don't
  7035. * report errors due to that.
  7036. */
  7037. if (IS_I830(dev))
  7038. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  7039. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  7040. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  7041. } else {
  7042. /* Mask out read-only status bits. */
  7043. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  7044. DPLL_PORTC_READY_MASK |
  7045. DPLL_PORTB_READY_MASK);
  7046. }
  7047. if (IS_CHERRYVIEW(dev))
  7048. chv_crtc_clock_get(crtc, pipe_config);
  7049. else if (IS_VALLEYVIEW(dev))
  7050. vlv_crtc_clock_get(crtc, pipe_config);
  7051. else
  7052. i9xx_crtc_clock_get(crtc, pipe_config);
  7053. /*
  7054. * Normally the dotclock is filled in by the encoder .get_config()
  7055. * but in case the pipe is enabled w/o any ports we need a sane
  7056. * default.
  7057. */
  7058. pipe_config->base.adjusted_mode.crtc_clock =
  7059. pipe_config->port_clock / pipe_config->pixel_multiplier;
  7060. ret = true;
  7061. out:
  7062. intel_display_power_put(dev_priv, power_domain);
  7063. return ret;
  7064. }
  7065. static void ironlake_init_pch_refclk(struct drm_device *dev)
  7066. {
  7067. struct drm_i915_private *dev_priv = to_i915(dev);
  7068. struct intel_encoder *encoder;
  7069. int i;
  7070. u32 val, final;
  7071. bool has_lvds = false;
  7072. bool has_cpu_edp = false;
  7073. bool has_panel = false;
  7074. bool has_ck505 = false;
  7075. bool can_ssc = false;
  7076. bool using_ssc_source = false;
  7077. /* We need to take the global config into account */
  7078. for_each_intel_encoder(dev, encoder) {
  7079. switch (encoder->type) {
  7080. case INTEL_OUTPUT_LVDS:
  7081. has_panel = true;
  7082. has_lvds = true;
  7083. break;
  7084. case INTEL_OUTPUT_EDP:
  7085. has_panel = true;
  7086. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7087. has_cpu_edp = true;
  7088. break;
  7089. default:
  7090. break;
  7091. }
  7092. }
  7093. if (HAS_PCH_IBX(dev)) {
  7094. has_ck505 = dev_priv->vbt.display_clock_mode;
  7095. can_ssc = has_ck505;
  7096. } else {
  7097. has_ck505 = false;
  7098. can_ssc = true;
  7099. }
  7100. /* Check if any DPLLs are using the SSC source */
  7101. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7102. u32 temp = I915_READ(PCH_DPLL(i));
  7103. if (!(temp & DPLL_VCO_ENABLE))
  7104. continue;
  7105. if ((temp & PLL_REF_INPUT_MASK) ==
  7106. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7107. using_ssc_source = true;
  7108. break;
  7109. }
  7110. }
  7111. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7112. has_panel, has_lvds, has_ck505, using_ssc_source);
  7113. /* Ironlake: try to setup display ref clock before DPLL
  7114. * enabling. This is only under driver's control after
  7115. * PCH B stepping, previous chipset stepping should be
  7116. * ignoring this setting.
  7117. */
  7118. val = I915_READ(PCH_DREF_CONTROL);
  7119. /* As we must carefully and slowly disable/enable each source in turn,
  7120. * compute the final state we want first and check if we need to
  7121. * make any changes at all.
  7122. */
  7123. final = val;
  7124. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7125. if (has_ck505)
  7126. final |= DREF_NONSPREAD_CK505_ENABLE;
  7127. else
  7128. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7129. final &= ~DREF_SSC_SOURCE_MASK;
  7130. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7131. final &= ~DREF_SSC1_ENABLE;
  7132. if (has_panel) {
  7133. final |= DREF_SSC_SOURCE_ENABLE;
  7134. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7135. final |= DREF_SSC1_ENABLE;
  7136. if (has_cpu_edp) {
  7137. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7138. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7139. else
  7140. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7141. } else
  7142. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7143. } else if (using_ssc_source) {
  7144. final |= DREF_SSC_SOURCE_ENABLE;
  7145. final |= DREF_SSC1_ENABLE;
  7146. }
  7147. if (final == val)
  7148. return;
  7149. /* Always enable nonspread source */
  7150. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7151. if (has_ck505)
  7152. val |= DREF_NONSPREAD_CK505_ENABLE;
  7153. else
  7154. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7155. if (has_panel) {
  7156. val &= ~DREF_SSC_SOURCE_MASK;
  7157. val |= DREF_SSC_SOURCE_ENABLE;
  7158. /* SSC must be turned on before enabling the CPU output */
  7159. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7160. DRM_DEBUG_KMS("Using SSC on panel\n");
  7161. val |= DREF_SSC1_ENABLE;
  7162. } else
  7163. val &= ~DREF_SSC1_ENABLE;
  7164. /* Get SSC going before enabling the outputs */
  7165. I915_WRITE(PCH_DREF_CONTROL, val);
  7166. POSTING_READ(PCH_DREF_CONTROL);
  7167. udelay(200);
  7168. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7169. /* Enable CPU source on CPU attached eDP */
  7170. if (has_cpu_edp) {
  7171. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7172. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7173. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7174. } else
  7175. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7176. } else
  7177. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7178. I915_WRITE(PCH_DREF_CONTROL, val);
  7179. POSTING_READ(PCH_DREF_CONTROL);
  7180. udelay(200);
  7181. } else {
  7182. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7183. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7184. /* Turn off CPU output */
  7185. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7186. I915_WRITE(PCH_DREF_CONTROL, val);
  7187. POSTING_READ(PCH_DREF_CONTROL);
  7188. udelay(200);
  7189. if (!using_ssc_source) {
  7190. DRM_DEBUG_KMS("Disabling SSC source\n");
  7191. /* Turn off the SSC source */
  7192. val &= ~DREF_SSC_SOURCE_MASK;
  7193. val |= DREF_SSC_SOURCE_DISABLE;
  7194. /* Turn off SSC1 */
  7195. val &= ~DREF_SSC1_ENABLE;
  7196. I915_WRITE(PCH_DREF_CONTROL, val);
  7197. POSTING_READ(PCH_DREF_CONTROL);
  7198. udelay(200);
  7199. }
  7200. }
  7201. BUG_ON(val != final);
  7202. }
  7203. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7204. {
  7205. uint32_t tmp;
  7206. tmp = I915_READ(SOUTH_CHICKEN2);
  7207. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7208. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7209. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  7210. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7211. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7212. tmp = I915_READ(SOUTH_CHICKEN2);
  7213. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7214. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7215. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  7216. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7217. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7218. }
  7219. /* WaMPhyProgramming:hsw */
  7220. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7221. {
  7222. uint32_t tmp;
  7223. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7224. tmp &= ~(0xFF << 24);
  7225. tmp |= (0x12 << 24);
  7226. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7227. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7228. tmp |= (1 << 11);
  7229. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7230. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7231. tmp |= (1 << 11);
  7232. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7233. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7234. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7235. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7236. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7237. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7238. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7239. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7240. tmp &= ~(7 << 13);
  7241. tmp |= (5 << 13);
  7242. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7243. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7244. tmp &= ~(7 << 13);
  7245. tmp |= (5 << 13);
  7246. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7247. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7248. tmp &= ~0xFF;
  7249. tmp |= 0x1C;
  7250. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7251. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7252. tmp &= ~0xFF;
  7253. tmp |= 0x1C;
  7254. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7255. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7256. tmp &= ~(0xFF << 16);
  7257. tmp |= (0x1C << 16);
  7258. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7259. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7260. tmp &= ~(0xFF << 16);
  7261. tmp |= (0x1C << 16);
  7262. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7263. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7264. tmp |= (1 << 27);
  7265. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7266. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7267. tmp |= (1 << 27);
  7268. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7269. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7270. tmp &= ~(0xF << 28);
  7271. tmp |= (4 << 28);
  7272. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7273. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7274. tmp &= ~(0xF << 28);
  7275. tmp |= (4 << 28);
  7276. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7277. }
  7278. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7279. * Programming" based on the parameters passed:
  7280. * - Sequence to enable CLKOUT_DP
  7281. * - Sequence to enable CLKOUT_DP without spread
  7282. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7283. */
  7284. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7285. bool with_fdi)
  7286. {
  7287. struct drm_i915_private *dev_priv = to_i915(dev);
  7288. uint32_t reg, tmp;
  7289. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7290. with_spread = true;
  7291. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7292. with_fdi = false;
  7293. mutex_lock(&dev_priv->sb_lock);
  7294. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7295. tmp &= ~SBI_SSCCTL_DISABLE;
  7296. tmp |= SBI_SSCCTL_PATHALT;
  7297. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7298. udelay(24);
  7299. if (with_spread) {
  7300. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7301. tmp &= ~SBI_SSCCTL_PATHALT;
  7302. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7303. if (with_fdi) {
  7304. lpt_reset_fdi_mphy(dev_priv);
  7305. lpt_program_fdi_mphy(dev_priv);
  7306. }
  7307. }
  7308. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7309. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7310. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7311. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7312. mutex_unlock(&dev_priv->sb_lock);
  7313. }
  7314. /* Sequence to disable CLKOUT_DP */
  7315. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7316. {
  7317. struct drm_i915_private *dev_priv = to_i915(dev);
  7318. uint32_t reg, tmp;
  7319. mutex_lock(&dev_priv->sb_lock);
  7320. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7321. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7322. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7323. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7324. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7325. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7326. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7327. tmp |= SBI_SSCCTL_PATHALT;
  7328. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7329. udelay(32);
  7330. }
  7331. tmp |= SBI_SSCCTL_DISABLE;
  7332. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7333. }
  7334. mutex_unlock(&dev_priv->sb_lock);
  7335. }
  7336. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7337. static const uint16_t sscdivintphase[] = {
  7338. [BEND_IDX( 50)] = 0x3B23,
  7339. [BEND_IDX( 45)] = 0x3B23,
  7340. [BEND_IDX( 40)] = 0x3C23,
  7341. [BEND_IDX( 35)] = 0x3C23,
  7342. [BEND_IDX( 30)] = 0x3D23,
  7343. [BEND_IDX( 25)] = 0x3D23,
  7344. [BEND_IDX( 20)] = 0x3E23,
  7345. [BEND_IDX( 15)] = 0x3E23,
  7346. [BEND_IDX( 10)] = 0x3F23,
  7347. [BEND_IDX( 5)] = 0x3F23,
  7348. [BEND_IDX( 0)] = 0x0025,
  7349. [BEND_IDX( -5)] = 0x0025,
  7350. [BEND_IDX(-10)] = 0x0125,
  7351. [BEND_IDX(-15)] = 0x0125,
  7352. [BEND_IDX(-20)] = 0x0225,
  7353. [BEND_IDX(-25)] = 0x0225,
  7354. [BEND_IDX(-30)] = 0x0325,
  7355. [BEND_IDX(-35)] = 0x0325,
  7356. [BEND_IDX(-40)] = 0x0425,
  7357. [BEND_IDX(-45)] = 0x0425,
  7358. [BEND_IDX(-50)] = 0x0525,
  7359. };
  7360. /*
  7361. * Bend CLKOUT_DP
  7362. * steps -50 to 50 inclusive, in steps of 5
  7363. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7364. * change in clock period = -(steps / 10) * 5.787 ps
  7365. */
  7366. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7367. {
  7368. uint32_t tmp;
  7369. int idx = BEND_IDX(steps);
  7370. if (WARN_ON(steps % 5 != 0))
  7371. return;
  7372. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7373. return;
  7374. mutex_lock(&dev_priv->sb_lock);
  7375. if (steps % 10 != 0)
  7376. tmp = 0xAAAAAAAB;
  7377. else
  7378. tmp = 0x00000000;
  7379. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7380. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7381. tmp &= 0xffff0000;
  7382. tmp |= sscdivintphase[idx];
  7383. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7384. mutex_unlock(&dev_priv->sb_lock);
  7385. }
  7386. #undef BEND_IDX
  7387. static void lpt_init_pch_refclk(struct drm_device *dev)
  7388. {
  7389. struct intel_encoder *encoder;
  7390. bool has_vga = false;
  7391. for_each_intel_encoder(dev, encoder) {
  7392. switch (encoder->type) {
  7393. case INTEL_OUTPUT_ANALOG:
  7394. has_vga = true;
  7395. break;
  7396. default:
  7397. break;
  7398. }
  7399. }
  7400. if (has_vga) {
  7401. lpt_bend_clkout_dp(to_i915(dev), 0);
  7402. lpt_enable_clkout_dp(dev, true, true);
  7403. } else {
  7404. lpt_disable_clkout_dp(dev);
  7405. }
  7406. }
  7407. /*
  7408. * Initialize reference clocks when the driver loads
  7409. */
  7410. void intel_init_pch_refclk(struct drm_device *dev)
  7411. {
  7412. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7413. ironlake_init_pch_refclk(dev);
  7414. else if (HAS_PCH_LPT(dev))
  7415. lpt_init_pch_refclk(dev);
  7416. }
  7417. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7418. {
  7419. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7420. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7421. int pipe = intel_crtc->pipe;
  7422. uint32_t val;
  7423. val = 0;
  7424. switch (intel_crtc->config->pipe_bpp) {
  7425. case 18:
  7426. val |= PIPECONF_6BPC;
  7427. break;
  7428. case 24:
  7429. val |= PIPECONF_8BPC;
  7430. break;
  7431. case 30:
  7432. val |= PIPECONF_10BPC;
  7433. break;
  7434. case 36:
  7435. val |= PIPECONF_12BPC;
  7436. break;
  7437. default:
  7438. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7439. BUG();
  7440. }
  7441. if (intel_crtc->config->dither)
  7442. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7443. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7444. val |= PIPECONF_INTERLACED_ILK;
  7445. else
  7446. val |= PIPECONF_PROGRESSIVE;
  7447. if (intel_crtc->config->limited_color_range)
  7448. val |= PIPECONF_COLOR_RANGE_SELECT;
  7449. I915_WRITE(PIPECONF(pipe), val);
  7450. POSTING_READ(PIPECONF(pipe));
  7451. }
  7452. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7453. {
  7454. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7455. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7456. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7457. u32 val = 0;
  7458. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7459. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7460. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7461. val |= PIPECONF_INTERLACED_ILK;
  7462. else
  7463. val |= PIPECONF_PROGRESSIVE;
  7464. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7465. POSTING_READ(PIPECONF(cpu_transcoder));
  7466. }
  7467. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7468. {
  7469. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7470. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7471. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7472. u32 val = 0;
  7473. switch (intel_crtc->config->pipe_bpp) {
  7474. case 18:
  7475. val |= PIPEMISC_DITHER_6_BPC;
  7476. break;
  7477. case 24:
  7478. val |= PIPEMISC_DITHER_8_BPC;
  7479. break;
  7480. case 30:
  7481. val |= PIPEMISC_DITHER_10_BPC;
  7482. break;
  7483. case 36:
  7484. val |= PIPEMISC_DITHER_12_BPC;
  7485. break;
  7486. default:
  7487. /* Case prevented by pipe_config_set_bpp. */
  7488. BUG();
  7489. }
  7490. if (intel_crtc->config->dither)
  7491. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7492. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7493. }
  7494. }
  7495. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7496. {
  7497. /*
  7498. * Account for spread spectrum to avoid
  7499. * oversubscribing the link. Max center spread
  7500. * is 2.5%; use 5% for safety's sake.
  7501. */
  7502. u32 bps = target_clock * bpp * 21 / 20;
  7503. return DIV_ROUND_UP(bps, link_bw * 8);
  7504. }
  7505. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7506. {
  7507. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7508. }
  7509. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7510. struct intel_crtc_state *crtc_state,
  7511. struct dpll *reduced_clock)
  7512. {
  7513. struct drm_crtc *crtc = &intel_crtc->base;
  7514. struct drm_device *dev = crtc->dev;
  7515. struct drm_i915_private *dev_priv = to_i915(dev);
  7516. u32 dpll, fp, fp2;
  7517. int factor;
  7518. /* Enable autotuning of the PLL clock (if permissible) */
  7519. factor = 21;
  7520. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7521. if ((intel_panel_use_ssc(dev_priv) &&
  7522. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7523. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7524. factor = 25;
  7525. } else if (crtc_state->sdvo_tv_clock)
  7526. factor = 20;
  7527. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7528. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7529. fp |= FP_CB_TUNE;
  7530. if (reduced_clock) {
  7531. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7532. if (reduced_clock->m < factor * reduced_clock->n)
  7533. fp2 |= FP_CB_TUNE;
  7534. } else {
  7535. fp2 = fp;
  7536. }
  7537. dpll = 0;
  7538. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7539. dpll |= DPLLB_MODE_LVDS;
  7540. else
  7541. dpll |= DPLLB_MODE_DAC_SERIAL;
  7542. dpll |= (crtc_state->pixel_multiplier - 1)
  7543. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7544. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7545. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7546. dpll |= DPLL_SDVO_HIGH_SPEED;
  7547. if (intel_crtc_has_dp_encoder(crtc_state))
  7548. dpll |= DPLL_SDVO_HIGH_SPEED;
  7549. /* compute bitmask from p1 value */
  7550. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7551. /* also FPA1 */
  7552. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7553. switch (crtc_state->dpll.p2) {
  7554. case 5:
  7555. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7556. break;
  7557. case 7:
  7558. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7559. break;
  7560. case 10:
  7561. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7562. break;
  7563. case 14:
  7564. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7565. break;
  7566. }
  7567. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7568. intel_panel_use_ssc(dev_priv))
  7569. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7570. else
  7571. dpll |= PLL_REF_INPUT_DREFCLK;
  7572. dpll |= DPLL_VCO_ENABLE;
  7573. crtc_state->dpll_hw_state.dpll = dpll;
  7574. crtc_state->dpll_hw_state.fp0 = fp;
  7575. crtc_state->dpll_hw_state.fp1 = fp2;
  7576. }
  7577. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7578. struct intel_crtc_state *crtc_state)
  7579. {
  7580. struct drm_device *dev = crtc->base.dev;
  7581. struct drm_i915_private *dev_priv = to_i915(dev);
  7582. struct dpll reduced_clock;
  7583. bool has_reduced_clock = false;
  7584. struct intel_shared_dpll *pll;
  7585. const struct intel_limit *limit;
  7586. int refclk = 120000;
  7587. memset(&crtc_state->dpll_hw_state, 0,
  7588. sizeof(crtc_state->dpll_hw_state));
  7589. crtc->lowfreq_avail = false;
  7590. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7591. if (!crtc_state->has_pch_encoder)
  7592. return 0;
  7593. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7594. if (intel_panel_use_ssc(dev_priv)) {
  7595. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7596. dev_priv->vbt.lvds_ssc_freq);
  7597. refclk = dev_priv->vbt.lvds_ssc_freq;
  7598. }
  7599. if (intel_is_dual_link_lvds(dev)) {
  7600. if (refclk == 100000)
  7601. limit = &intel_limits_ironlake_dual_lvds_100m;
  7602. else
  7603. limit = &intel_limits_ironlake_dual_lvds;
  7604. } else {
  7605. if (refclk == 100000)
  7606. limit = &intel_limits_ironlake_single_lvds_100m;
  7607. else
  7608. limit = &intel_limits_ironlake_single_lvds;
  7609. }
  7610. } else {
  7611. limit = &intel_limits_ironlake_dac;
  7612. }
  7613. if (!crtc_state->clock_set &&
  7614. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7615. refclk, NULL, &crtc_state->dpll)) {
  7616. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7617. return -EINVAL;
  7618. }
  7619. ironlake_compute_dpll(crtc, crtc_state,
  7620. has_reduced_clock ? &reduced_clock : NULL);
  7621. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  7622. if (pll == NULL) {
  7623. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7624. pipe_name(crtc->pipe));
  7625. return -EINVAL;
  7626. }
  7627. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7628. has_reduced_clock)
  7629. crtc->lowfreq_avail = true;
  7630. return 0;
  7631. }
  7632. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7633. struct intel_link_m_n *m_n)
  7634. {
  7635. struct drm_device *dev = crtc->base.dev;
  7636. struct drm_i915_private *dev_priv = to_i915(dev);
  7637. enum pipe pipe = crtc->pipe;
  7638. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7639. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7640. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7641. & ~TU_SIZE_MASK;
  7642. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7643. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7644. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7645. }
  7646. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7647. enum transcoder transcoder,
  7648. struct intel_link_m_n *m_n,
  7649. struct intel_link_m_n *m2_n2)
  7650. {
  7651. struct drm_device *dev = crtc->base.dev;
  7652. struct drm_i915_private *dev_priv = to_i915(dev);
  7653. enum pipe pipe = crtc->pipe;
  7654. if (INTEL_INFO(dev)->gen >= 5) {
  7655. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7656. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7657. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7658. & ~TU_SIZE_MASK;
  7659. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7660. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7661. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7662. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7663. * gen < 8) and if DRRS is supported (to make sure the
  7664. * registers are not unnecessarily read).
  7665. */
  7666. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7667. crtc->config->has_drrs) {
  7668. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7669. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7670. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7671. & ~TU_SIZE_MASK;
  7672. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7673. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7674. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7675. }
  7676. } else {
  7677. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7678. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7679. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7680. & ~TU_SIZE_MASK;
  7681. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7682. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7683. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7684. }
  7685. }
  7686. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7687. struct intel_crtc_state *pipe_config)
  7688. {
  7689. if (pipe_config->has_pch_encoder)
  7690. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7691. else
  7692. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7693. &pipe_config->dp_m_n,
  7694. &pipe_config->dp_m2_n2);
  7695. }
  7696. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7697. struct intel_crtc_state *pipe_config)
  7698. {
  7699. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7700. &pipe_config->fdi_m_n, NULL);
  7701. }
  7702. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7703. struct intel_crtc_state *pipe_config)
  7704. {
  7705. struct drm_device *dev = crtc->base.dev;
  7706. struct drm_i915_private *dev_priv = to_i915(dev);
  7707. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7708. uint32_t ps_ctrl = 0;
  7709. int id = -1;
  7710. int i;
  7711. /* find scaler attached to this pipe */
  7712. for (i = 0; i < crtc->num_scalers; i++) {
  7713. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7714. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7715. id = i;
  7716. pipe_config->pch_pfit.enabled = true;
  7717. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7718. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7719. break;
  7720. }
  7721. }
  7722. scaler_state->scaler_id = id;
  7723. if (id >= 0) {
  7724. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7725. } else {
  7726. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7727. }
  7728. }
  7729. static void
  7730. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7731. struct intel_initial_plane_config *plane_config)
  7732. {
  7733. struct drm_device *dev = crtc->base.dev;
  7734. struct drm_i915_private *dev_priv = to_i915(dev);
  7735. u32 val, base, offset, stride_mult, tiling;
  7736. int pipe = crtc->pipe;
  7737. int fourcc, pixel_format;
  7738. unsigned int aligned_height;
  7739. struct drm_framebuffer *fb;
  7740. struct intel_framebuffer *intel_fb;
  7741. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7742. if (!intel_fb) {
  7743. DRM_DEBUG_KMS("failed to alloc fb\n");
  7744. return;
  7745. }
  7746. fb = &intel_fb->base;
  7747. val = I915_READ(PLANE_CTL(pipe, 0));
  7748. if (!(val & PLANE_CTL_ENABLE))
  7749. goto error;
  7750. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7751. fourcc = skl_format_to_fourcc(pixel_format,
  7752. val & PLANE_CTL_ORDER_RGBX,
  7753. val & PLANE_CTL_ALPHA_MASK);
  7754. fb->pixel_format = fourcc;
  7755. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7756. tiling = val & PLANE_CTL_TILED_MASK;
  7757. switch (tiling) {
  7758. case PLANE_CTL_TILED_LINEAR:
  7759. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7760. break;
  7761. case PLANE_CTL_TILED_X:
  7762. plane_config->tiling = I915_TILING_X;
  7763. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7764. break;
  7765. case PLANE_CTL_TILED_Y:
  7766. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7767. break;
  7768. case PLANE_CTL_TILED_YF:
  7769. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7770. break;
  7771. default:
  7772. MISSING_CASE(tiling);
  7773. goto error;
  7774. }
  7775. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7776. plane_config->base = base;
  7777. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7778. val = I915_READ(PLANE_SIZE(pipe, 0));
  7779. fb->height = ((val >> 16) & 0xfff) + 1;
  7780. fb->width = ((val >> 0) & 0x1fff) + 1;
  7781. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7782. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  7783. fb->pixel_format);
  7784. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7785. aligned_height = intel_fb_align_height(dev, fb->height,
  7786. fb->pixel_format,
  7787. fb->modifier[0]);
  7788. plane_config->size = fb->pitches[0] * aligned_height;
  7789. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7790. pipe_name(pipe), fb->width, fb->height,
  7791. fb->bits_per_pixel, base, fb->pitches[0],
  7792. plane_config->size);
  7793. plane_config->fb = intel_fb;
  7794. return;
  7795. error:
  7796. kfree(fb);
  7797. }
  7798. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7799. struct intel_crtc_state *pipe_config)
  7800. {
  7801. struct drm_device *dev = crtc->base.dev;
  7802. struct drm_i915_private *dev_priv = to_i915(dev);
  7803. uint32_t tmp;
  7804. tmp = I915_READ(PF_CTL(crtc->pipe));
  7805. if (tmp & PF_ENABLE) {
  7806. pipe_config->pch_pfit.enabled = true;
  7807. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7808. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7809. /* We currently do not free assignements of panel fitters on
  7810. * ivb/hsw (since we don't use the higher upscaling modes which
  7811. * differentiates them) so just WARN about this case for now. */
  7812. if (IS_GEN7(dev)) {
  7813. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7814. PF_PIPE_SEL_IVB(crtc->pipe));
  7815. }
  7816. }
  7817. }
  7818. static void
  7819. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7820. struct intel_initial_plane_config *plane_config)
  7821. {
  7822. struct drm_device *dev = crtc->base.dev;
  7823. struct drm_i915_private *dev_priv = to_i915(dev);
  7824. u32 val, base, offset;
  7825. int pipe = crtc->pipe;
  7826. int fourcc, pixel_format;
  7827. unsigned int aligned_height;
  7828. struct drm_framebuffer *fb;
  7829. struct intel_framebuffer *intel_fb;
  7830. val = I915_READ(DSPCNTR(pipe));
  7831. if (!(val & DISPLAY_PLANE_ENABLE))
  7832. return;
  7833. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7834. if (!intel_fb) {
  7835. DRM_DEBUG_KMS("failed to alloc fb\n");
  7836. return;
  7837. }
  7838. fb = &intel_fb->base;
  7839. if (INTEL_INFO(dev)->gen >= 4) {
  7840. if (val & DISPPLANE_TILED) {
  7841. plane_config->tiling = I915_TILING_X;
  7842. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7843. }
  7844. }
  7845. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7846. fourcc = i9xx_format_to_fourcc(pixel_format);
  7847. fb->pixel_format = fourcc;
  7848. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7849. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7850. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7851. offset = I915_READ(DSPOFFSET(pipe));
  7852. } else {
  7853. if (plane_config->tiling)
  7854. offset = I915_READ(DSPTILEOFF(pipe));
  7855. else
  7856. offset = I915_READ(DSPLINOFF(pipe));
  7857. }
  7858. plane_config->base = base;
  7859. val = I915_READ(PIPESRC(pipe));
  7860. fb->width = ((val >> 16) & 0xfff) + 1;
  7861. fb->height = ((val >> 0) & 0xfff) + 1;
  7862. val = I915_READ(DSPSTRIDE(pipe));
  7863. fb->pitches[0] = val & 0xffffffc0;
  7864. aligned_height = intel_fb_align_height(dev, fb->height,
  7865. fb->pixel_format,
  7866. fb->modifier[0]);
  7867. plane_config->size = fb->pitches[0] * aligned_height;
  7868. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7869. pipe_name(pipe), fb->width, fb->height,
  7870. fb->bits_per_pixel, base, fb->pitches[0],
  7871. plane_config->size);
  7872. plane_config->fb = intel_fb;
  7873. }
  7874. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7875. struct intel_crtc_state *pipe_config)
  7876. {
  7877. struct drm_device *dev = crtc->base.dev;
  7878. struct drm_i915_private *dev_priv = to_i915(dev);
  7879. enum intel_display_power_domain power_domain;
  7880. uint32_t tmp;
  7881. bool ret;
  7882. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7883. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7884. return false;
  7885. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7886. pipe_config->shared_dpll = NULL;
  7887. ret = false;
  7888. tmp = I915_READ(PIPECONF(crtc->pipe));
  7889. if (!(tmp & PIPECONF_ENABLE))
  7890. goto out;
  7891. switch (tmp & PIPECONF_BPC_MASK) {
  7892. case PIPECONF_6BPC:
  7893. pipe_config->pipe_bpp = 18;
  7894. break;
  7895. case PIPECONF_8BPC:
  7896. pipe_config->pipe_bpp = 24;
  7897. break;
  7898. case PIPECONF_10BPC:
  7899. pipe_config->pipe_bpp = 30;
  7900. break;
  7901. case PIPECONF_12BPC:
  7902. pipe_config->pipe_bpp = 36;
  7903. break;
  7904. default:
  7905. break;
  7906. }
  7907. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7908. pipe_config->limited_color_range = true;
  7909. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7910. struct intel_shared_dpll *pll;
  7911. enum intel_dpll_id pll_id;
  7912. pipe_config->has_pch_encoder = true;
  7913. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7914. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7915. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7916. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7917. if (HAS_PCH_IBX(dev_priv)) {
  7918. /*
  7919. * The pipe->pch transcoder and pch transcoder->pll
  7920. * mapping is fixed.
  7921. */
  7922. pll_id = (enum intel_dpll_id) crtc->pipe;
  7923. } else {
  7924. tmp = I915_READ(PCH_DPLL_SEL);
  7925. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7926. pll_id = DPLL_ID_PCH_PLL_B;
  7927. else
  7928. pll_id= DPLL_ID_PCH_PLL_A;
  7929. }
  7930. pipe_config->shared_dpll =
  7931. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7932. pll = pipe_config->shared_dpll;
  7933. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7934. &pipe_config->dpll_hw_state));
  7935. tmp = pipe_config->dpll_hw_state.dpll;
  7936. pipe_config->pixel_multiplier =
  7937. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7938. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7939. ironlake_pch_clock_get(crtc, pipe_config);
  7940. } else {
  7941. pipe_config->pixel_multiplier = 1;
  7942. }
  7943. intel_get_pipe_timings(crtc, pipe_config);
  7944. intel_get_pipe_src_size(crtc, pipe_config);
  7945. ironlake_get_pfit_config(crtc, pipe_config);
  7946. ret = true;
  7947. out:
  7948. intel_display_power_put(dev_priv, power_domain);
  7949. return ret;
  7950. }
  7951. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7952. {
  7953. struct drm_device *dev = &dev_priv->drm;
  7954. struct intel_crtc *crtc;
  7955. for_each_intel_crtc(dev, crtc)
  7956. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7957. pipe_name(crtc->pipe));
  7958. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7959. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7960. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7961. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7962. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7963. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7964. "CPU PWM1 enabled\n");
  7965. if (IS_HASWELL(dev))
  7966. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7967. "CPU PWM2 enabled\n");
  7968. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7969. "PCH PWM1 enabled\n");
  7970. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7971. "Utility pin enabled\n");
  7972. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7973. /*
  7974. * In theory we can still leave IRQs enabled, as long as only the HPD
  7975. * interrupts remain enabled. We used to check for that, but since it's
  7976. * gen-specific and since we only disable LCPLL after we fully disable
  7977. * the interrupts, the check below should be enough.
  7978. */
  7979. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7980. }
  7981. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7982. {
  7983. struct drm_device *dev = &dev_priv->drm;
  7984. if (IS_HASWELL(dev))
  7985. return I915_READ(D_COMP_HSW);
  7986. else
  7987. return I915_READ(D_COMP_BDW);
  7988. }
  7989. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7990. {
  7991. struct drm_device *dev = &dev_priv->drm;
  7992. if (IS_HASWELL(dev)) {
  7993. mutex_lock(&dev_priv->rps.hw_lock);
  7994. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7995. val))
  7996. DRM_ERROR("Failed to write to D_COMP\n");
  7997. mutex_unlock(&dev_priv->rps.hw_lock);
  7998. } else {
  7999. I915_WRITE(D_COMP_BDW, val);
  8000. POSTING_READ(D_COMP_BDW);
  8001. }
  8002. }
  8003. /*
  8004. * This function implements pieces of two sequences from BSpec:
  8005. * - Sequence for display software to disable LCPLL
  8006. * - Sequence for display software to allow package C8+
  8007. * The steps implemented here are just the steps that actually touch the LCPLL
  8008. * register. Callers should take care of disabling all the display engine
  8009. * functions, doing the mode unset, fixing interrupts, etc.
  8010. */
  8011. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  8012. bool switch_to_fclk, bool allow_power_down)
  8013. {
  8014. uint32_t val;
  8015. assert_can_disable_lcpll(dev_priv);
  8016. val = I915_READ(LCPLL_CTL);
  8017. if (switch_to_fclk) {
  8018. val |= LCPLL_CD_SOURCE_FCLK;
  8019. I915_WRITE(LCPLL_CTL, val);
  8020. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8021. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8022. DRM_ERROR("Switching to FCLK failed\n");
  8023. val = I915_READ(LCPLL_CTL);
  8024. }
  8025. val |= LCPLL_PLL_DISABLE;
  8026. I915_WRITE(LCPLL_CTL, val);
  8027. POSTING_READ(LCPLL_CTL);
  8028. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  8029. DRM_ERROR("LCPLL still locked\n");
  8030. val = hsw_read_dcomp(dev_priv);
  8031. val |= D_COMP_COMP_DISABLE;
  8032. hsw_write_dcomp(dev_priv, val);
  8033. ndelay(100);
  8034. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  8035. 1))
  8036. DRM_ERROR("D_COMP RCOMP still in progress\n");
  8037. if (allow_power_down) {
  8038. val = I915_READ(LCPLL_CTL);
  8039. val |= LCPLL_POWER_DOWN_ALLOW;
  8040. I915_WRITE(LCPLL_CTL, val);
  8041. POSTING_READ(LCPLL_CTL);
  8042. }
  8043. }
  8044. /*
  8045. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  8046. * source.
  8047. */
  8048. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8049. {
  8050. uint32_t val;
  8051. val = I915_READ(LCPLL_CTL);
  8052. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8053. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8054. return;
  8055. /*
  8056. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8057. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8058. */
  8059. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8060. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8061. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8062. I915_WRITE(LCPLL_CTL, val);
  8063. POSTING_READ(LCPLL_CTL);
  8064. }
  8065. val = hsw_read_dcomp(dev_priv);
  8066. val |= D_COMP_COMP_FORCE;
  8067. val &= ~D_COMP_COMP_DISABLE;
  8068. hsw_write_dcomp(dev_priv, val);
  8069. val = I915_READ(LCPLL_CTL);
  8070. val &= ~LCPLL_PLL_DISABLE;
  8071. I915_WRITE(LCPLL_CTL, val);
  8072. if (intel_wait_for_register(dev_priv,
  8073. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  8074. 5))
  8075. DRM_ERROR("LCPLL not locked yet\n");
  8076. if (val & LCPLL_CD_SOURCE_FCLK) {
  8077. val = I915_READ(LCPLL_CTL);
  8078. val &= ~LCPLL_CD_SOURCE_FCLK;
  8079. I915_WRITE(LCPLL_CTL, val);
  8080. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8081. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8082. DRM_ERROR("Switching back to LCPLL failed\n");
  8083. }
  8084. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8085. intel_update_cdclk(&dev_priv->drm);
  8086. }
  8087. /*
  8088. * Package states C8 and deeper are really deep PC states that can only be
  8089. * reached when all the devices on the system allow it, so even if the graphics
  8090. * device allows PC8+, it doesn't mean the system will actually get to these
  8091. * states. Our driver only allows PC8+ when going into runtime PM.
  8092. *
  8093. * The requirements for PC8+ are that all the outputs are disabled, the power
  8094. * well is disabled and most interrupts are disabled, and these are also
  8095. * requirements for runtime PM. When these conditions are met, we manually do
  8096. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8097. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8098. * hang the machine.
  8099. *
  8100. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8101. * the state of some registers, so when we come back from PC8+ we need to
  8102. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8103. * need to take care of the registers kept by RC6. Notice that this happens even
  8104. * if we don't put the device in PCI D3 state (which is what currently happens
  8105. * because of the runtime PM support).
  8106. *
  8107. * For more, read "Display Sequences for Package C8" on the hardware
  8108. * documentation.
  8109. */
  8110. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8111. {
  8112. struct drm_device *dev = &dev_priv->drm;
  8113. uint32_t val;
  8114. DRM_DEBUG_KMS("Enabling package C8+\n");
  8115. if (HAS_PCH_LPT_LP(dev)) {
  8116. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8117. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8118. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8119. }
  8120. lpt_disable_clkout_dp(dev);
  8121. hsw_disable_lcpll(dev_priv, true, true);
  8122. }
  8123. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8124. {
  8125. struct drm_device *dev = &dev_priv->drm;
  8126. uint32_t val;
  8127. DRM_DEBUG_KMS("Disabling package C8+\n");
  8128. hsw_restore_lcpll(dev_priv);
  8129. lpt_init_pch_refclk(dev);
  8130. if (HAS_PCH_LPT_LP(dev)) {
  8131. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8132. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8133. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8134. }
  8135. }
  8136. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8137. {
  8138. struct drm_device *dev = old_state->dev;
  8139. struct intel_atomic_state *old_intel_state =
  8140. to_intel_atomic_state(old_state);
  8141. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8142. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8143. }
  8144. /* compute the max rate for new configuration */
  8145. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8146. {
  8147. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8148. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8149. struct drm_crtc *crtc;
  8150. struct drm_crtc_state *cstate;
  8151. struct intel_crtc_state *crtc_state;
  8152. unsigned max_pixel_rate = 0, i;
  8153. enum pipe pipe;
  8154. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8155. sizeof(intel_state->min_pixclk));
  8156. for_each_crtc_in_state(state, crtc, cstate, i) {
  8157. int pixel_rate;
  8158. crtc_state = to_intel_crtc_state(cstate);
  8159. if (!crtc_state->base.enable) {
  8160. intel_state->min_pixclk[i] = 0;
  8161. continue;
  8162. }
  8163. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8164. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8165. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8166. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8167. intel_state->min_pixclk[i] = pixel_rate;
  8168. }
  8169. for_each_pipe(dev_priv, pipe)
  8170. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8171. return max_pixel_rate;
  8172. }
  8173. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8174. {
  8175. struct drm_i915_private *dev_priv = to_i915(dev);
  8176. uint32_t val, data;
  8177. int ret;
  8178. if (WARN((I915_READ(LCPLL_CTL) &
  8179. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8180. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8181. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8182. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8183. "trying to change cdclk frequency with cdclk not enabled\n"))
  8184. return;
  8185. mutex_lock(&dev_priv->rps.hw_lock);
  8186. ret = sandybridge_pcode_write(dev_priv,
  8187. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8188. mutex_unlock(&dev_priv->rps.hw_lock);
  8189. if (ret) {
  8190. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8191. return;
  8192. }
  8193. val = I915_READ(LCPLL_CTL);
  8194. val |= LCPLL_CD_SOURCE_FCLK;
  8195. I915_WRITE(LCPLL_CTL, val);
  8196. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8197. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8198. DRM_ERROR("Switching to FCLK failed\n");
  8199. val = I915_READ(LCPLL_CTL);
  8200. val &= ~LCPLL_CLK_FREQ_MASK;
  8201. switch (cdclk) {
  8202. case 450000:
  8203. val |= LCPLL_CLK_FREQ_450;
  8204. data = 0;
  8205. break;
  8206. case 540000:
  8207. val |= LCPLL_CLK_FREQ_54O_BDW;
  8208. data = 1;
  8209. break;
  8210. case 337500:
  8211. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8212. data = 2;
  8213. break;
  8214. case 675000:
  8215. val |= LCPLL_CLK_FREQ_675_BDW;
  8216. data = 3;
  8217. break;
  8218. default:
  8219. WARN(1, "invalid cdclk frequency\n");
  8220. return;
  8221. }
  8222. I915_WRITE(LCPLL_CTL, val);
  8223. val = I915_READ(LCPLL_CTL);
  8224. val &= ~LCPLL_CD_SOURCE_FCLK;
  8225. I915_WRITE(LCPLL_CTL, val);
  8226. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8227. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8228. DRM_ERROR("Switching back to LCPLL failed\n");
  8229. mutex_lock(&dev_priv->rps.hw_lock);
  8230. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8231. mutex_unlock(&dev_priv->rps.hw_lock);
  8232. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8233. intel_update_cdclk(dev);
  8234. WARN(cdclk != dev_priv->cdclk_freq,
  8235. "cdclk requested %d kHz but got %d kHz\n",
  8236. cdclk, dev_priv->cdclk_freq);
  8237. }
  8238. static int broadwell_calc_cdclk(int max_pixclk)
  8239. {
  8240. if (max_pixclk > 540000)
  8241. return 675000;
  8242. else if (max_pixclk > 450000)
  8243. return 540000;
  8244. else if (max_pixclk > 337500)
  8245. return 450000;
  8246. else
  8247. return 337500;
  8248. }
  8249. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8250. {
  8251. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8252. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8253. int max_pixclk = ilk_max_pixel_rate(state);
  8254. int cdclk;
  8255. /*
  8256. * FIXME should also account for plane ratio
  8257. * once 64bpp pixel formats are supported.
  8258. */
  8259. cdclk = broadwell_calc_cdclk(max_pixclk);
  8260. if (cdclk > dev_priv->max_cdclk_freq) {
  8261. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8262. cdclk, dev_priv->max_cdclk_freq);
  8263. return -EINVAL;
  8264. }
  8265. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8266. if (!intel_state->active_crtcs)
  8267. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8268. return 0;
  8269. }
  8270. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8271. {
  8272. struct drm_device *dev = old_state->dev;
  8273. struct intel_atomic_state *old_intel_state =
  8274. to_intel_atomic_state(old_state);
  8275. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8276. broadwell_set_cdclk(dev, req_cdclk);
  8277. }
  8278. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8279. {
  8280. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8281. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8282. const int max_pixclk = ilk_max_pixel_rate(state);
  8283. int vco = intel_state->cdclk_pll_vco;
  8284. int cdclk;
  8285. /*
  8286. * FIXME should also account for plane ratio
  8287. * once 64bpp pixel formats are supported.
  8288. */
  8289. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8290. /*
  8291. * FIXME move the cdclk caclulation to
  8292. * compute_config() so we can fail gracegully.
  8293. */
  8294. if (cdclk > dev_priv->max_cdclk_freq) {
  8295. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8296. cdclk, dev_priv->max_cdclk_freq);
  8297. cdclk = dev_priv->max_cdclk_freq;
  8298. }
  8299. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8300. if (!intel_state->active_crtcs)
  8301. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8302. return 0;
  8303. }
  8304. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8305. {
  8306. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8307. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8308. unsigned int req_cdclk = intel_state->dev_cdclk;
  8309. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8310. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8311. }
  8312. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8313. struct intel_crtc_state *crtc_state)
  8314. {
  8315. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  8316. if (!intel_ddi_pll_select(crtc, crtc_state))
  8317. return -EINVAL;
  8318. }
  8319. crtc->lowfreq_avail = false;
  8320. return 0;
  8321. }
  8322. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8323. enum port port,
  8324. struct intel_crtc_state *pipe_config)
  8325. {
  8326. enum intel_dpll_id id;
  8327. switch (port) {
  8328. case PORT_A:
  8329. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8330. id = DPLL_ID_SKL_DPLL0;
  8331. break;
  8332. case PORT_B:
  8333. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8334. id = DPLL_ID_SKL_DPLL1;
  8335. break;
  8336. case PORT_C:
  8337. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8338. id = DPLL_ID_SKL_DPLL2;
  8339. break;
  8340. default:
  8341. DRM_ERROR("Incorrect port type\n");
  8342. return;
  8343. }
  8344. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8345. }
  8346. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8347. enum port port,
  8348. struct intel_crtc_state *pipe_config)
  8349. {
  8350. enum intel_dpll_id id;
  8351. u32 temp;
  8352. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8353. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8354. switch (pipe_config->ddi_pll_sel) {
  8355. case SKL_DPLL0:
  8356. id = DPLL_ID_SKL_DPLL0;
  8357. break;
  8358. case SKL_DPLL1:
  8359. id = DPLL_ID_SKL_DPLL1;
  8360. break;
  8361. case SKL_DPLL2:
  8362. id = DPLL_ID_SKL_DPLL2;
  8363. break;
  8364. case SKL_DPLL3:
  8365. id = DPLL_ID_SKL_DPLL3;
  8366. break;
  8367. default:
  8368. MISSING_CASE(pipe_config->ddi_pll_sel);
  8369. return;
  8370. }
  8371. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8372. }
  8373. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8374. enum port port,
  8375. struct intel_crtc_state *pipe_config)
  8376. {
  8377. enum intel_dpll_id id;
  8378. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8379. switch (pipe_config->ddi_pll_sel) {
  8380. case PORT_CLK_SEL_WRPLL1:
  8381. id = DPLL_ID_WRPLL1;
  8382. break;
  8383. case PORT_CLK_SEL_WRPLL2:
  8384. id = DPLL_ID_WRPLL2;
  8385. break;
  8386. case PORT_CLK_SEL_SPLL:
  8387. id = DPLL_ID_SPLL;
  8388. break;
  8389. case PORT_CLK_SEL_LCPLL_810:
  8390. id = DPLL_ID_LCPLL_810;
  8391. break;
  8392. case PORT_CLK_SEL_LCPLL_1350:
  8393. id = DPLL_ID_LCPLL_1350;
  8394. break;
  8395. case PORT_CLK_SEL_LCPLL_2700:
  8396. id = DPLL_ID_LCPLL_2700;
  8397. break;
  8398. default:
  8399. MISSING_CASE(pipe_config->ddi_pll_sel);
  8400. /* fall through */
  8401. case PORT_CLK_SEL_NONE:
  8402. return;
  8403. }
  8404. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8405. }
  8406. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8407. struct intel_crtc_state *pipe_config,
  8408. unsigned long *power_domain_mask)
  8409. {
  8410. struct drm_device *dev = crtc->base.dev;
  8411. struct drm_i915_private *dev_priv = to_i915(dev);
  8412. enum intel_display_power_domain power_domain;
  8413. u32 tmp;
  8414. /*
  8415. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8416. * transcoder handled below.
  8417. */
  8418. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8419. /*
  8420. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8421. * consistency and less surprising code; it's in always on power).
  8422. */
  8423. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8424. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8425. enum pipe trans_edp_pipe;
  8426. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8427. default:
  8428. WARN(1, "unknown pipe linked to edp transcoder\n");
  8429. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8430. case TRANS_DDI_EDP_INPUT_A_ON:
  8431. trans_edp_pipe = PIPE_A;
  8432. break;
  8433. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8434. trans_edp_pipe = PIPE_B;
  8435. break;
  8436. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8437. trans_edp_pipe = PIPE_C;
  8438. break;
  8439. }
  8440. if (trans_edp_pipe == crtc->pipe)
  8441. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8442. }
  8443. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8444. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8445. return false;
  8446. *power_domain_mask |= BIT(power_domain);
  8447. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8448. return tmp & PIPECONF_ENABLE;
  8449. }
  8450. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8451. struct intel_crtc_state *pipe_config,
  8452. unsigned long *power_domain_mask)
  8453. {
  8454. struct drm_device *dev = crtc->base.dev;
  8455. struct drm_i915_private *dev_priv = to_i915(dev);
  8456. enum intel_display_power_domain power_domain;
  8457. enum port port;
  8458. enum transcoder cpu_transcoder;
  8459. u32 tmp;
  8460. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8461. if (port == PORT_A)
  8462. cpu_transcoder = TRANSCODER_DSI_A;
  8463. else
  8464. cpu_transcoder = TRANSCODER_DSI_C;
  8465. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8466. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8467. continue;
  8468. *power_domain_mask |= BIT(power_domain);
  8469. /*
  8470. * The PLL needs to be enabled with a valid divider
  8471. * configuration, otherwise accessing DSI registers will hang
  8472. * the machine. See BSpec North Display Engine
  8473. * registers/MIPI[BXT]. We can break out here early, since we
  8474. * need the same DSI PLL to be enabled for both DSI ports.
  8475. */
  8476. if (!intel_dsi_pll_is_enabled(dev_priv))
  8477. break;
  8478. /* XXX: this works for video mode only */
  8479. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8480. if (!(tmp & DPI_ENABLE))
  8481. continue;
  8482. tmp = I915_READ(MIPI_CTRL(port));
  8483. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8484. continue;
  8485. pipe_config->cpu_transcoder = cpu_transcoder;
  8486. break;
  8487. }
  8488. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  8489. }
  8490. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8491. struct intel_crtc_state *pipe_config)
  8492. {
  8493. struct drm_device *dev = crtc->base.dev;
  8494. struct drm_i915_private *dev_priv = to_i915(dev);
  8495. struct intel_shared_dpll *pll;
  8496. enum port port;
  8497. uint32_t tmp;
  8498. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8499. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8500. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8501. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8502. else if (IS_BROXTON(dev))
  8503. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8504. else
  8505. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8506. pll = pipe_config->shared_dpll;
  8507. if (pll) {
  8508. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8509. &pipe_config->dpll_hw_state));
  8510. }
  8511. /*
  8512. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8513. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8514. * the PCH transcoder is on.
  8515. */
  8516. if (INTEL_INFO(dev)->gen < 9 &&
  8517. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8518. pipe_config->has_pch_encoder = true;
  8519. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8520. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8521. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8522. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8523. }
  8524. }
  8525. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8526. struct intel_crtc_state *pipe_config)
  8527. {
  8528. struct drm_device *dev = crtc->base.dev;
  8529. struct drm_i915_private *dev_priv = to_i915(dev);
  8530. enum intel_display_power_domain power_domain;
  8531. unsigned long power_domain_mask;
  8532. bool active;
  8533. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8534. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8535. return false;
  8536. power_domain_mask = BIT(power_domain);
  8537. pipe_config->shared_dpll = NULL;
  8538. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8539. if (IS_BROXTON(dev_priv) &&
  8540. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  8541. WARN_ON(active);
  8542. active = true;
  8543. }
  8544. if (!active)
  8545. goto out;
  8546. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8547. haswell_get_ddi_port_state(crtc, pipe_config);
  8548. intel_get_pipe_timings(crtc, pipe_config);
  8549. }
  8550. intel_get_pipe_src_size(crtc, pipe_config);
  8551. pipe_config->gamma_mode =
  8552. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8553. if (INTEL_INFO(dev)->gen >= 9) {
  8554. skl_init_scalers(dev, crtc, pipe_config);
  8555. }
  8556. if (INTEL_INFO(dev)->gen >= 9) {
  8557. pipe_config->scaler_state.scaler_id = -1;
  8558. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8559. }
  8560. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8561. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8562. power_domain_mask |= BIT(power_domain);
  8563. if (INTEL_INFO(dev)->gen >= 9)
  8564. skylake_get_pfit_config(crtc, pipe_config);
  8565. else
  8566. ironlake_get_pfit_config(crtc, pipe_config);
  8567. }
  8568. if (IS_HASWELL(dev))
  8569. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8570. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8571. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8572. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8573. pipe_config->pixel_multiplier =
  8574. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8575. } else {
  8576. pipe_config->pixel_multiplier = 1;
  8577. }
  8578. out:
  8579. for_each_power_domain(power_domain, power_domain_mask)
  8580. intel_display_power_put(dev_priv, power_domain);
  8581. return active;
  8582. }
  8583. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  8584. const struct intel_plane_state *plane_state)
  8585. {
  8586. struct drm_device *dev = crtc->dev;
  8587. struct drm_i915_private *dev_priv = to_i915(dev);
  8588. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8589. uint32_t cntl = 0, size = 0;
  8590. if (plane_state && plane_state->visible) {
  8591. unsigned int width = plane_state->base.crtc_w;
  8592. unsigned int height = plane_state->base.crtc_h;
  8593. unsigned int stride = roundup_pow_of_two(width) * 4;
  8594. switch (stride) {
  8595. default:
  8596. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8597. width, stride);
  8598. stride = 256;
  8599. /* fallthrough */
  8600. case 256:
  8601. case 512:
  8602. case 1024:
  8603. case 2048:
  8604. break;
  8605. }
  8606. cntl |= CURSOR_ENABLE |
  8607. CURSOR_GAMMA_ENABLE |
  8608. CURSOR_FORMAT_ARGB |
  8609. CURSOR_STRIDE(stride);
  8610. size = (height << 12) | width;
  8611. }
  8612. if (intel_crtc->cursor_cntl != 0 &&
  8613. (intel_crtc->cursor_base != base ||
  8614. intel_crtc->cursor_size != size ||
  8615. intel_crtc->cursor_cntl != cntl)) {
  8616. /* On these chipsets we can only modify the base/size/stride
  8617. * whilst the cursor is disabled.
  8618. */
  8619. I915_WRITE(CURCNTR(PIPE_A), 0);
  8620. POSTING_READ(CURCNTR(PIPE_A));
  8621. intel_crtc->cursor_cntl = 0;
  8622. }
  8623. if (intel_crtc->cursor_base != base) {
  8624. I915_WRITE(CURBASE(PIPE_A), base);
  8625. intel_crtc->cursor_base = base;
  8626. }
  8627. if (intel_crtc->cursor_size != size) {
  8628. I915_WRITE(CURSIZE, size);
  8629. intel_crtc->cursor_size = size;
  8630. }
  8631. if (intel_crtc->cursor_cntl != cntl) {
  8632. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8633. POSTING_READ(CURCNTR(PIPE_A));
  8634. intel_crtc->cursor_cntl = cntl;
  8635. }
  8636. }
  8637. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  8638. const struct intel_plane_state *plane_state)
  8639. {
  8640. struct drm_device *dev = crtc->dev;
  8641. struct drm_i915_private *dev_priv = to_i915(dev);
  8642. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8643. int pipe = intel_crtc->pipe;
  8644. uint32_t cntl = 0;
  8645. if (plane_state && plane_state->visible) {
  8646. cntl = MCURSOR_GAMMA_ENABLE;
  8647. switch (plane_state->base.crtc_w) {
  8648. case 64:
  8649. cntl |= CURSOR_MODE_64_ARGB_AX;
  8650. break;
  8651. case 128:
  8652. cntl |= CURSOR_MODE_128_ARGB_AX;
  8653. break;
  8654. case 256:
  8655. cntl |= CURSOR_MODE_256_ARGB_AX;
  8656. break;
  8657. default:
  8658. MISSING_CASE(plane_state->base.crtc_w);
  8659. return;
  8660. }
  8661. cntl |= pipe << 28; /* Connect to correct pipe */
  8662. if (HAS_DDI(dev))
  8663. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8664. if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
  8665. cntl |= CURSOR_ROTATE_180;
  8666. }
  8667. if (intel_crtc->cursor_cntl != cntl) {
  8668. I915_WRITE(CURCNTR(pipe), cntl);
  8669. POSTING_READ(CURCNTR(pipe));
  8670. intel_crtc->cursor_cntl = cntl;
  8671. }
  8672. /* and commit changes on next vblank */
  8673. I915_WRITE(CURBASE(pipe), base);
  8674. POSTING_READ(CURBASE(pipe));
  8675. intel_crtc->cursor_base = base;
  8676. }
  8677. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8678. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8679. const struct intel_plane_state *plane_state)
  8680. {
  8681. struct drm_device *dev = crtc->dev;
  8682. struct drm_i915_private *dev_priv = to_i915(dev);
  8683. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8684. int pipe = intel_crtc->pipe;
  8685. u32 base = intel_crtc->cursor_addr;
  8686. u32 pos = 0;
  8687. if (plane_state) {
  8688. int x = plane_state->base.crtc_x;
  8689. int y = plane_state->base.crtc_y;
  8690. if (x < 0) {
  8691. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8692. x = -x;
  8693. }
  8694. pos |= x << CURSOR_X_SHIFT;
  8695. if (y < 0) {
  8696. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8697. y = -y;
  8698. }
  8699. pos |= y << CURSOR_Y_SHIFT;
  8700. /* ILK+ do this automagically */
  8701. if (HAS_GMCH_DISPLAY(dev) &&
  8702. plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  8703. base += (plane_state->base.crtc_h *
  8704. plane_state->base.crtc_w - 1) * 4;
  8705. }
  8706. }
  8707. I915_WRITE(CURPOS(pipe), pos);
  8708. if (IS_845G(dev) || IS_I865G(dev))
  8709. i845_update_cursor(crtc, base, plane_state);
  8710. else
  8711. i9xx_update_cursor(crtc, base, plane_state);
  8712. }
  8713. static bool cursor_size_ok(struct drm_device *dev,
  8714. uint32_t width, uint32_t height)
  8715. {
  8716. if (width == 0 || height == 0)
  8717. return false;
  8718. /*
  8719. * 845g/865g are special in that they are only limited by
  8720. * the width of their cursors, the height is arbitrary up to
  8721. * the precision of the register. Everything else requires
  8722. * square cursors, limited to a few power-of-two sizes.
  8723. */
  8724. if (IS_845G(dev) || IS_I865G(dev)) {
  8725. if ((width & 63) != 0)
  8726. return false;
  8727. if (width > (IS_845G(dev) ? 64 : 512))
  8728. return false;
  8729. if (height > 1023)
  8730. return false;
  8731. } else {
  8732. switch (width | height) {
  8733. case 256:
  8734. case 128:
  8735. if (IS_GEN2(dev))
  8736. return false;
  8737. case 64:
  8738. break;
  8739. default:
  8740. return false;
  8741. }
  8742. }
  8743. return true;
  8744. }
  8745. /* VESA 640x480x72Hz mode to set on the pipe */
  8746. static struct drm_display_mode load_detect_mode = {
  8747. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8748. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8749. };
  8750. struct drm_framebuffer *
  8751. __intel_framebuffer_create(struct drm_device *dev,
  8752. struct drm_mode_fb_cmd2 *mode_cmd,
  8753. struct drm_i915_gem_object *obj)
  8754. {
  8755. struct intel_framebuffer *intel_fb;
  8756. int ret;
  8757. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8758. if (!intel_fb)
  8759. return ERR_PTR(-ENOMEM);
  8760. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8761. if (ret)
  8762. goto err;
  8763. return &intel_fb->base;
  8764. err:
  8765. kfree(intel_fb);
  8766. return ERR_PTR(ret);
  8767. }
  8768. static struct drm_framebuffer *
  8769. intel_framebuffer_create(struct drm_device *dev,
  8770. struct drm_mode_fb_cmd2 *mode_cmd,
  8771. struct drm_i915_gem_object *obj)
  8772. {
  8773. struct drm_framebuffer *fb;
  8774. int ret;
  8775. ret = i915_mutex_lock_interruptible(dev);
  8776. if (ret)
  8777. return ERR_PTR(ret);
  8778. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8779. mutex_unlock(&dev->struct_mutex);
  8780. return fb;
  8781. }
  8782. static u32
  8783. intel_framebuffer_pitch_for_width(int width, int bpp)
  8784. {
  8785. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8786. return ALIGN(pitch, 64);
  8787. }
  8788. static u32
  8789. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8790. {
  8791. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8792. return PAGE_ALIGN(pitch * mode->vdisplay);
  8793. }
  8794. static struct drm_framebuffer *
  8795. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8796. struct drm_display_mode *mode,
  8797. int depth, int bpp)
  8798. {
  8799. struct drm_framebuffer *fb;
  8800. struct drm_i915_gem_object *obj;
  8801. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8802. obj = i915_gem_object_create(dev,
  8803. intel_framebuffer_size_for_mode(mode, bpp));
  8804. if (IS_ERR(obj))
  8805. return ERR_CAST(obj);
  8806. mode_cmd.width = mode->hdisplay;
  8807. mode_cmd.height = mode->vdisplay;
  8808. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8809. bpp);
  8810. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8811. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8812. if (IS_ERR(fb))
  8813. drm_gem_object_unreference_unlocked(&obj->base);
  8814. return fb;
  8815. }
  8816. static struct drm_framebuffer *
  8817. mode_fits_in_fbdev(struct drm_device *dev,
  8818. struct drm_display_mode *mode)
  8819. {
  8820. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8821. struct drm_i915_private *dev_priv = to_i915(dev);
  8822. struct drm_i915_gem_object *obj;
  8823. struct drm_framebuffer *fb;
  8824. if (!dev_priv->fbdev)
  8825. return NULL;
  8826. if (!dev_priv->fbdev->fb)
  8827. return NULL;
  8828. obj = dev_priv->fbdev->fb->obj;
  8829. BUG_ON(!obj);
  8830. fb = &dev_priv->fbdev->fb->base;
  8831. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8832. fb->bits_per_pixel))
  8833. return NULL;
  8834. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8835. return NULL;
  8836. drm_framebuffer_reference(fb);
  8837. return fb;
  8838. #else
  8839. return NULL;
  8840. #endif
  8841. }
  8842. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8843. struct drm_crtc *crtc,
  8844. struct drm_display_mode *mode,
  8845. struct drm_framebuffer *fb,
  8846. int x, int y)
  8847. {
  8848. struct drm_plane_state *plane_state;
  8849. int hdisplay, vdisplay;
  8850. int ret;
  8851. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8852. if (IS_ERR(plane_state))
  8853. return PTR_ERR(plane_state);
  8854. if (mode)
  8855. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8856. else
  8857. hdisplay = vdisplay = 0;
  8858. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8859. if (ret)
  8860. return ret;
  8861. drm_atomic_set_fb_for_plane(plane_state, fb);
  8862. plane_state->crtc_x = 0;
  8863. plane_state->crtc_y = 0;
  8864. plane_state->crtc_w = hdisplay;
  8865. plane_state->crtc_h = vdisplay;
  8866. plane_state->src_x = x << 16;
  8867. plane_state->src_y = y << 16;
  8868. plane_state->src_w = hdisplay << 16;
  8869. plane_state->src_h = vdisplay << 16;
  8870. return 0;
  8871. }
  8872. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8873. struct drm_display_mode *mode,
  8874. struct intel_load_detect_pipe *old,
  8875. struct drm_modeset_acquire_ctx *ctx)
  8876. {
  8877. struct intel_crtc *intel_crtc;
  8878. struct intel_encoder *intel_encoder =
  8879. intel_attached_encoder(connector);
  8880. struct drm_crtc *possible_crtc;
  8881. struct drm_encoder *encoder = &intel_encoder->base;
  8882. struct drm_crtc *crtc = NULL;
  8883. struct drm_device *dev = encoder->dev;
  8884. struct drm_framebuffer *fb;
  8885. struct drm_mode_config *config = &dev->mode_config;
  8886. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8887. struct drm_connector_state *connector_state;
  8888. struct intel_crtc_state *crtc_state;
  8889. int ret, i = -1;
  8890. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8891. connector->base.id, connector->name,
  8892. encoder->base.id, encoder->name);
  8893. old->restore_state = NULL;
  8894. retry:
  8895. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8896. if (ret)
  8897. goto fail;
  8898. /*
  8899. * Algorithm gets a little messy:
  8900. *
  8901. * - if the connector already has an assigned crtc, use it (but make
  8902. * sure it's on first)
  8903. *
  8904. * - try to find the first unused crtc that can drive this connector,
  8905. * and use that if we find one
  8906. */
  8907. /* See if we already have a CRTC for this connector */
  8908. if (connector->state->crtc) {
  8909. crtc = connector->state->crtc;
  8910. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8911. if (ret)
  8912. goto fail;
  8913. /* Make sure the crtc and connector are running */
  8914. goto found;
  8915. }
  8916. /* Find an unused one (if possible) */
  8917. for_each_crtc(dev, possible_crtc) {
  8918. i++;
  8919. if (!(encoder->possible_crtcs & (1 << i)))
  8920. continue;
  8921. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8922. if (ret)
  8923. goto fail;
  8924. if (possible_crtc->state->enable) {
  8925. drm_modeset_unlock(&possible_crtc->mutex);
  8926. continue;
  8927. }
  8928. crtc = possible_crtc;
  8929. break;
  8930. }
  8931. /*
  8932. * If we didn't find an unused CRTC, don't use any.
  8933. */
  8934. if (!crtc) {
  8935. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8936. goto fail;
  8937. }
  8938. found:
  8939. intel_crtc = to_intel_crtc(crtc);
  8940. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8941. if (ret)
  8942. goto fail;
  8943. state = drm_atomic_state_alloc(dev);
  8944. restore_state = drm_atomic_state_alloc(dev);
  8945. if (!state || !restore_state) {
  8946. ret = -ENOMEM;
  8947. goto fail;
  8948. }
  8949. state->acquire_ctx = ctx;
  8950. restore_state->acquire_ctx = ctx;
  8951. connector_state = drm_atomic_get_connector_state(state, connector);
  8952. if (IS_ERR(connector_state)) {
  8953. ret = PTR_ERR(connector_state);
  8954. goto fail;
  8955. }
  8956. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8957. if (ret)
  8958. goto fail;
  8959. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8960. if (IS_ERR(crtc_state)) {
  8961. ret = PTR_ERR(crtc_state);
  8962. goto fail;
  8963. }
  8964. crtc_state->base.active = crtc_state->base.enable = true;
  8965. if (!mode)
  8966. mode = &load_detect_mode;
  8967. /* We need a framebuffer large enough to accommodate all accesses
  8968. * that the plane may generate whilst we perform load detection.
  8969. * We can not rely on the fbcon either being present (we get called
  8970. * during its initialisation to detect all boot displays, or it may
  8971. * not even exist) or that it is large enough to satisfy the
  8972. * requested mode.
  8973. */
  8974. fb = mode_fits_in_fbdev(dev, mode);
  8975. if (fb == NULL) {
  8976. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8977. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8978. } else
  8979. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8980. if (IS_ERR(fb)) {
  8981. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8982. goto fail;
  8983. }
  8984. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8985. if (ret)
  8986. goto fail;
  8987. drm_framebuffer_unreference(fb);
  8988. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8989. if (ret)
  8990. goto fail;
  8991. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8992. if (!ret)
  8993. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8994. if (!ret)
  8995. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8996. if (ret) {
  8997. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8998. goto fail;
  8999. }
  9000. ret = drm_atomic_commit(state);
  9001. if (ret) {
  9002. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  9003. goto fail;
  9004. }
  9005. old->restore_state = restore_state;
  9006. /* let the connector get through one full cycle before testing */
  9007. intel_wait_for_vblank(dev, intel_crtc->pipe);
  9008. return true;
  9009. fail:
  9010. drm_atomic_state_free(state);
  9011. drm_atomic_state_free(restore_state);
  9012. restore_state = state = NULL;
  9013. if (ret == -EDEADLK) {
  9014. drm_modeset_backoff(ctx);
  9015. goto retry;
  9016. }
  9017. return false;
  9018. }
  9019. void intel_release_load_detect_pipe(struct drm_connector *connector,
  9020. struct intel_load_detect_pipe *old,
  9021. struct drm_modeset_acquire_ctx *ctx)
  9022. {
  9023. struct intel_encoder *intel_encoder =
  9024. intel_attached_encoder(connector);
  9025. struct drm_encoder *encoder = &intel_encoder->base;
  9026. struct drm_atomic_state *state = old->restore_state;
  9027. int ret;
  9028. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9029. connector->base.id, connector->name,
  9030. encoder->base.id, encoder->name);
  9031. if (!state)
  9032. return;
  9033. ret = drm_atomic_commit(state);
  9034. if (ret) {
  9035. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  9036. drm_atomic_state_free(state);
  9037. }
  9038. }
  9039. static int i9xx_pll_refclk(struct drm_device *dev,
  9040. const struct intel_crtc_state *pipe_config)
  9041. {
  9042. struct drm_i915_private *dev_priv = to_i915(dev);
  9043. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9044. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  9045. return dev_priv->vbt.lvds_ssc_freq;
  9046. else if (HAS_PCH_SPLIT(dev))
  9047. return 120000;
  9048. else if (!IS_GEN2(dev))
  9049. return 96000;
  9050. else
  9051. return 48000;
  9052. }
  9053. /* Returns the clock of the currently programmed mode of the given pipe. */
  9054. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  9055. struct intel_crtc_state *pipe_config)
  9056. {
  9057. struct drm_device *dev = crtc->base.dev;
  9058. struct drm_i915_private *dev_priv = to_i915(dev);
  9059. int pipe = pipe_config->cpu_transcoder;
  9060. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9061. u32 fp;
  9062. struct dpll clock;
  9063. int port_clock;
  9064. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9065. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9066. fp = pipe_config->dpll_hw_state.fp0;
  9067. else
  9068. fp = pipe_config->dpll_hw_state.fp1;
  9069. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9070. if (IS_PINEVIEW(dev)) {
  9071. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9072. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9073. } else {
  9074. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9075. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9076. }
  9077. if (!IS_GEN2(dev)) {
  9078. if (IS_PINEVIEW(dev))
  9079. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9080. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9081. else
  9082. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9083. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9084. switch (dpll & DPLL_MODE_MASK) {
  9085. case DPLLB_MODE_DAC_SERIAL:
  9086. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9087. 5 : 10;
  9088. break;
  9089. case DPLLB_MODE_LVDS:
  9090. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9091. 7 : 14;
  9092. break;
  9093. default:
  9094. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9095. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9096. return;
  9097. }
  9098. if (IS_PINEVIEW(dev))
  9099. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9100. else
  9101. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9102. } else {
  9103. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  9104. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9105. if (is_lvds) {
  9106. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9107. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9108. if (lvds & LVDS_CLKB_POWER_UP)
  9109. clock.p2 = 7;
  9110. else
  9111. clock.p2 = 14;
  9112. } else {
  9113. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9114. clock.p1 = 2;
  9115. else {
  9116. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9117. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9118. }
  9119. if (dpll & PLL_P2_DIVIDE_BY_4)
  9120. clock.p2 = 4;
  9121. else
  9122. clock.p2 = 2;
  9123. }
  9124. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9125. }
  9126. /*
  9127. * This value includes pixel_multiplier. We will use
  9128. * port_clock to compute adjusted_mode.crtc_clock in the
  9129. * encoder's get_config() function.
  9130. */
  9131. pipe_config->port_clock = port_clock;
  9132. }
  9133. int intel_dotclock_calculate(int link_freq,
  9134. const struct intel_link_m_n *m_n)
  9135. {
  9136. /*
  9137. * The calculation for the data clock is:
  9138. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9139. * But we want to avoid losing precison if possible, so:
  9140. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9141. *
  9142. * and the link clock is simpler:
  9143. * link_clock = (m * link_clock) / n
  9144. */
  9145. if (!m_n->link_n)
  9146. return 0;
  9147. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9148. }
  9149. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9150. struct intel_crtc_state *pipe_config)
  9151. {
  9152. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9153. /* read out port_clock from the DPLL */
  9154. i9xx_crtc_clock_get(crtc, pipe_config);
  9155. /*
  9156. * In case there is an active pipe without active ports,
  9157. * we may need some idea for the dotclock anyway.
  9158. * Calculate one based on the FDI configuration.
  9159. */
  9160. pipe_config->base.adjusted_mode.crtc_clock =
  9161. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9162. &pipe_config->fdi_m_n);
  9163. }
  9164. /** Returns the currently programmed mode of the given pipe. */
  9165. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9166. struct drm_crtc *crtc)
  9167. {
  9168. struct drm_i915_private *dev_priv = to_i915(dev);
  9169. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9170. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9171. struct drm_display_mode *mode;
  9172. struct intel_crtc_state *pipe_config;
  9173. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9174. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9175. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9176. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9177. enum pipe pipe = intel_crtc->pipe;
  9178. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9179. if (!mode)
  9180. return NULL;
  9181. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9182. if (!pipe_config) {
  9183. kfree(mode);
  9184. return NULL;
  9185. }
  9186. /*
  9187. * Construct a pipe_config sufficient for getting the clock info
  9188. * back out of crtc_clock_get.
  9189. *
  9190. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9191. * to use a real value here instead.
  9192. */
  9193. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9194. pipe_config->pixel_multiplier = 1;
  9195. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9196. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9197. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9198. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9199. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9200. mode->hdisplay = (htot & 0xffff) + 1;
  9201. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9202. mode->hsync_start = (hsync & 0xffff) + 1;
  9203. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9204. mode->vdisplay = (vtot & 0xffff) + 1;
  9205. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9206. mode->vsync_start = (vsync & 0xffff) + 1;
  9207. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9208. drm_mode_set_name(mode);
  9209. kfree(pipe_config);
  9210. return mode;
  9211. }
  9212. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9213. {
  9214. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9215. struct drm_device *dev = crtc->dev;
  9216. struct intel_flip_work *work;
  9217. spin_lock_irq(&dev->event_lock);
  9218. work = intel_crtc->flip_work;
  9219. intel_crtc->flip_work = NULL;
  9220. spin_unlock_irq(&dev->event_lock);
  9221. if (work) {
  9222. cancel_work_sync(&work->mmio_work);
  9223. cancel_work_sync(&work->unpin_work);
  9224. kfree(work);
  9225. }
  9226. drm_crtc_cleanup(crtc);
  9227. kfree(intel_crtc);
  9228. }
  9229. static void intel_unpin_work_fn(struct work_struct *__work)
  9230. {
  9231. struct intel_flip_work *work =
  9232. container_of(__work, struct intel_flip_work, unpin_work);
  9233. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9234. struct drm_device *dev = crtc->base.dev;
  9235. struct drm_plane *primary = crtc->base.primary;
  9236. if (is_mmio_work(work))
  9237. flush_work(&work->mmio_work);
  9238. mutex_lock(&dev->struct_mutex);
  9239. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9240. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9241. if (work->flip_queued_req)
  9242. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9243. mutex_unlock(&dev->struct_mutex);
  9244. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  9245. intel_fbc_post_update(crtc);
  9246. drm_framebuffer_unreference(work->old_fb);
  9247. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9248. atomic_dec(&crtc->unpin_work_count);
  9249. kfree(work);
  9250. }
  9251. /* Is 'a' after or equal to 'b'? */
  9252. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9253. {
  9254. return !((a - b) & 0x80000000);
  9255. }
  9256. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9257. struct intel_flip_work *work)
  9258. {
  9259. struct drm_device *dev = crtc->base.dev;
  9260. struct drm_i915_private *dev_priv = to_i915(dev);
  9261. unsigned reset_counter;
  9262. reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9263. if (crtc->reset_counter != reset_counter)
  9264. return true;
  9265. /*
  9266. * The relevant registers doen't exist on pre-ctg.
  9267. * As the flip done interrupt doesn't trigger for mmio
  9268. * flips on gmch platforms, a flip count check isn't
  9269. * really needed there. But since ctg has the registers,
  9270. * include it in the check anyway.
  9271. */
  9272. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9273. return true;
  9274. /*
  9275. * BDW signals flip done immediately if the plane
  9276. * is disabled, even if the plane enable is already
  9277. * armed to occur at the next vblank :(
  9278. */
  9279. /*
  9280. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9281. * used the same base address. In that case the mmio flip might
  9282. * have completed, but the CS hasn't even executed the flip yet.
  9283. *
  9284. * A flip count check isn't enough as the CS might have updated
  9285. * the base address just after start of vblank, but before we
  9286. * managed to process the interrupt. This means we'd complete the
  9287. * CS flip too soon.
  9288. *
  9289. * Combining both checks should get us a good enough result. It may
  9290. * still happen that the CS flip has been executed, but has not
  9291. * yet actually completed. But in case the base address is the same
  9292. * anyway, we don't really care.
  9293. */
  9294. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9295. crtc->flip_work->gtt_offset &&
  9296. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9297. crtc->flip_work->flip_count);
  9298. }
  9299. static bool
  9300. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9301. struct intel_flip_work *work)
  9302. {
  9303. /*
  9304. * MMIO work completes when vblank is different from
  9305. * flip_queued_vblank.
  9306. *
  9307. * Reset counter value doesn't matter, this is handled by
  9308. * i915_wait_request finishing early, so no need to handle
  9309. * reset here.
  9310. */
  9311. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9312. }
  9313. static bool pageflip_finished(struct intel_crtc *crtc,
  9314. struct intel_flip_work *work)
  9315. {
  9316. if (!atomic_read(&work->pending))
  9317. return false;
  9318. smp_rmb();
  9319. if (is_mmio_work(work))
  9320. return __pageflip_finished_mmio(crtc, work);
  9321. else
  9322. return __pageflip_finished_cs(crtc, work);
  9323. }
  9324. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9325. {
  9326. struct drm_device *dev = &dev_priv->drm;
  9327. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9328. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9329. struct intel_flip_work *work;
  9330. unsigned long flags;
  9331. /* Ignore early vblank irqs */
  9332. if (!crtc)
  9333. return;
  9334. /*
  9335. * This is called both by irq handlers and the reset code (to complete
  9336. * lost pageflips) so needs the full irqsave spinlocks.
  9337. */
  9338. spin_lock_irqsave(&dev->event_lock, flags);
  9339. work = intel_crtc->flip_work;
  9340. if (work != NULL &&
  9341. !is_mmio_work(work) &&
  9342. pageflip_finished(intel_crtc, work))
  9343. page_flip_completed(intel_crtc);
  9344. spin_unlock_irqrestore(&dev->event_lock, flags);
  9345. }
  9346. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9347. {
  9348. struct drm_device *dev = &dev_priv->drm;
  9349. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9350. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9351. struct intel_flip_work *work;
  9352. unsigned long flags;
  9353. /* Ignore early vblank irqs */
  9354. if (!crtc)
  9355. return;
  9356. /*
  9357. * This is called both by irq handlers and the reset code (to complete
  9358. * lost pageflips) so needs the full irqsave spinlocks.
  9359. */
  9360. spin_lock_irqsave(&dev->event_lock, flags);
  9361. work = intel_crtc->flip_work;
  9362. if (work != NULL &&
  9363. is_mmio_work(work) &&
  9364. pageflip_finished(intel_crtc, work))
  9365. page_flip_completed(intel_crtc);
  9366. spin_unlock_irqrestore(&dev->event_lock, flags);
  9367. }
  9368. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9369. struct intel_flip_work *work)
  9370. {
  9371. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9372. /* Ensure that the work item is consistent when activating it ... */
  9373. smp_mb__before_atomic();
  9374. atomic_set(&work->pending, 1);
  9375. }
  9376. static int intel_gen2_queue_flip(struct drm_device *dev,
  9377. struct drm_crtc *crtc,
  9378. struct drm_framebuffer *fb,
  9379. struct drm_i915_gem_object *obj,
  9380. struct drm_i915_gem_request *req,
  9381. uint32_t flags)
  9382. {
  9383. struct intel_engine_cs *engine = req->engine;
  9384. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9385. u32 flip_mask;
  9386. int ret;
  9387. ret = intel_ring_begin(req, 6);
  9388. if (ret)
  9389. return ret;
  9390. /* Can't queue multiple flips, so wait for the previous
  9391. * one to finish before executing the next.
  9392. */
  9393. if (intel_crtc->plane)
  9394. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9395. else
  9396. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9397. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9398. intel_ring_emit(engine, MI_NOOP);
  9399. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9400. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9401. intel_ring_emit(engine, fb->pitches[0]);
  9402. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9403. intel_ring_emit(engine, 0); /* aux display base address, unused */
  9404. return 0;
  9405. }
  9406. static int intel_gen3_queue_flip(struct drm_device *dev,
  9407. struct drm_crtc *crtc,
  9408. struct drm_framebuffer *fb,
  9409. struct drm_i915_gem_object *obj,
  9410. struct drm_i915_gem_request *req,
  9411. uint32_t flags)
  9412. {
  9413. struct intel_engine_cs *engine = req->engine;
  9414. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9415. u32 flip_mask;
  9416. int ret;
  9417. ret = intel_ring_begin(req, 6);
  9418. if (ret)
  9419. return ret;
  9420. if (intel_crtc->plane)
  9421. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9422. else
  9423. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9424. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9425. intel_ring_emit(engine, MI_NOOP);
  9426. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
  9427. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9428. intel_ring_emit(engine, fb->pitches[0]);
  9429. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9430. intel_ring_emit(engine, MI_NOOP);
  9431. return 0;
  9432. }
  9433. static int intel_gen4_queue_flip(struct drm_device *dev,
  9434. struct drm_crtc *crtc,
  9435. struct drm_framebuffer *fb,
  9436. struct drm_i915_gem_object *obj,
  9437. struct drm_i915_gem_request *req,
  9438. uint32_t flags)
  9439. {
  9440. struct intel_engine_cs *engine = req->engine;
  9441. struct drm_i915_private *dev_priv = to_i915(dev);
  9442. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9443. uint32_t pf, pipesrc;
  9444. int ret;
  9445. ret = intel_ring_begin(req, 4);
  9446. if (ret)
  9447. return ret;
  9448. /* i965+ uses the linear or tiled offsets from the
  9449. * Display Registers (which do not change across a page-flip)
  9450. * so we need only reprogram the base address.
  9451. */
  9452. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9453. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9454. intel_ring_emit(engine, fb->pitches[0]);
  9455. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
  9456. obj->tiling_mode);
  9457. /* XXX Enabling the panel-fitter across page-flip is so far
  9458. * untested on non-native modes, so ignore it for now.
  9459. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9460. */
  9461. pf = 0;
  9462. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9463. intel_ring_emit(engine, pf | pipesrc);
  9464. return 0;
  9465. }
  9466. static int intel_gen6_queue_flip(struct drm_device *dev,
  9467. struct drm_crtc *crtc,
  9468. struct drm_framebuffer *fb,
  9469. struct drm_i915_gem_object *obj,
  9470. struct drm_i915_gem_request *req,
  9471. uint32_t flags)
  9472. {
  9473. struct intel_engine_cs *engine = req->engine;
  9474. struct drm_i915_private *dev_priv = to_i915(dev);
  9475. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9476. uint32_t pf, pipesrc;
  9477. int ret;
  9478. ret = intel_ring_begin(req, 4);
  9479. if (ret)
  9480. return ret;
  9481. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9482. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9483. intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
  9484. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9485. /* Contrary to the suggestions in the documentation,
  9486. * "Enable Panel Fitter" does not seem to be required when page
  9487. * flipping with a non-native mode, and worse causes a normal
  9488. * modeset to fail.
  9489. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9490. */
  9491. pf = 0;
  9492. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9493. intel_ring_emit(engine, pf | pipesrc);
  9494. return 0;
  9495. }
  9496. static int intel_gen7_queue_flip(struct drm_device *dev,
  9497. struct drm_crtc *crtc,
  9498. struct drm_framebuffer *fb,
  9499. struct drm_i915_gem_object *obj,
  9500. struct drm_i915_gem_request *req,
  9501. uint32_t flags)
  9502. {
  9503. struct intel_engine_cs *engine = req->engine;
  9504. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9505. uint32_t plane_bit = 0;
  9506. int len, ret;
  9507. switch (intel_crtc->plane) {
  9508. case PLANE_A:
  9509. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9510. break;
  9511. case PLANE_B:
  9512. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9513. break;
  9514. case PLANE_C:
  9515. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9516. break;
  9517. default:
  9518. WARN_ONCE(1, "unknown plane in flip command\n");
  9519. return -ENODEV;
  9520. }
  9521. len = 4;
  9522. if (engine->id == RCS) {
  9523. len += 6;
  9524. /*
  9525. * On Gen 8, SRM is now taking an extra dword to accommodate
  9526. * 48bits addresses, and we need a NOOP for the batch size to
  9527. * stay even.
  9528. */
  9529. if (IS_GEN8(dev))
  9530. len += 2;
  9531. }
  9532. /*
  9533. * BSpec MI_DISPLAY_FLIP for IVB:
  9534. * "The full packet must be contained within the same cache line."
  9535. *
  9536. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9537. * cacheline, if we ever start emitting more commands before
  9538. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9539. * then do the cacheline alignment, and finally emit the
  9540. * MI_DISPLAY_FLIP.
  9541. */
  9542. ret = intel_ring_cacheline_align(req);
  9543. if (ret)
  9544. return ret;
  9545. ret = intel_ring_begin(req, len);
  9546. if (ret)
  9547. return ret;
  9548. /* Unmask the flip-done completion message. Note that the bspec says that
  9549. * we should do this for both the BCS and RCS, and that we must not unmask
  9550. * more than one flip event at any time (or ensure that one flip message
  9551. * can be sent by waiting for flip-done prior to queueing new flips).
  9552. * Experimentation says that BCS works despite DERRMR masking all
  9553. * flip-done completion events and that unmasking all planes at once
  9554. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9555. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9556. */
  9557. if (engine->id == RCS) {
  9558. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
  9559. intel_ring_emit_reg(engine, DERRMR);
  9560. intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9561. DERRMR_PIPEB_PRI_FLIP_DONE |
  9562. DERRMR_PIPEC_PRI_FLIP_DONE));
  9563. if (IS_GEN8(dev))
  9564. intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
  9565. MI_SRM_LRM_GLOBAL_GTT);
  9566. else
  9567. intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
  9568. MI_SRM_LRM_GLOBAL_GTT);
  9569. intel_ring_emit_reg(engine, DERRMR);
  9570. intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
  9571. if (IS_GEN8(dev)) {
  9572. intel_ring_emit(engine, 0);
  9573. intel_ring_emit(engine, MI_NOOP);
  9574. }
  9575. }
  9576. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
  9577. intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
  9578. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9579. intel_ring_emit(engine, (MI_NOOP));
  9580. return 0;
  9581. }
  9582. static bool use_mmio_flip(struct intel_engine_cs *engine,
  9583. struct drm_i915_gem_object *obj)
  9584. {
  9585. struct reservation_object *resv;
  9586. /*
  9587. * This is not being used for older platforms, because
  9588. * non-availability of flip done interrupt forces us to use
  9589. * CS flips. Older platforms derive flip done using some clever
  9590. * tricks involving the flip_pending status bits and vblank irqs.
  9591. * So using MMIO flips there would disrupt this mechanism.
  9592. */
  9593. if (engine == NULL)
  9594. return true;
  9595. if (INTEL_GEN(engine->i915) < 5)
  9596. return false;
  9597. if (i915.use_mmio_flip < 0)
  9598. return false;
  9599. else if (i915.use_mmio_flip > 0)
  9600. return true;
  9601. else if (i915.enable_execlists)
  9602. return true;
  9603. resv = i915_gem_object_get_dmabuf_resv(obj);
  9604. if (resv && !reservation_object_test_signaled_rcu(resv, false))
  9605. return true;
  9606. return engine != i915_gem_request_get_engine(obj->last_write_req);
  9607. }
  9608. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9609. unsigned int rotation,
  9610. struct intel_flip_work *work)
  9611. {
  9612. struct drm_device *dev = intel_crtc->base.dev;
  9613. struct drm_i915_private *dev_priv = to_i915(dev);
  9614. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9615. const enum pipe pipe = intel_crtc->pipe;
  9616. u32 ctl, stride, tile_height;
  9617. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9618. ctl &= ~PLANE_CTL_TILED_MASK;
  9619. switch (fb->modifier[0]) {
  9620. case DRM_FORMAT_MOD_NONE:
  9621. break;
  9622. case I915_FORMAT_MOD_X_TILED:
  9623. ctl |= PLANE_CTL_TILED_X;
  9624. break;
  9625. case I915_FORMAT_MOD_Y_TILED:
  9626. ctl |= PLANE_CTL_TILED_Y;
  9627. break;
  9628. case I915_FORMAT_MOD_Yf_TILED:
  9629. ctl |= PLANE_CTL_TILED_YF;
  9630. break;
  9631. default:
  9632. MISSING_CASE(fb->modifier[0]);
  9633. }
  9634. /*
  9635. * The stride is either expressed as a multiple of 64 bytes chunks for
  9636. * linear buffers or in number of tiles for tiled buffers.
  9637. */
  9638. if (intel_rotation_90_or_270(rotation)) {
  9639. /* stride = Surface height in tiles */
  9640. tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
  9641. stride = DIV_ROUND_UP(fb->height, tile_height);
  9642. } else {
  9643. stride = fb->pitches[0] /
  9644. intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  9645. fb->pixel_format);
  9646. }
  9647. /*
  9648. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9649. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9650. */
  9651. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9652. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9653. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9654. POSTING_READ(PLANE_SURF(pipe, 0));
  9655. }
  9656. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9657. struct intel_flip_work *work)
  9658. {
  9659. struct drm_device *dev = intel_crtc->base.dev;
  9660. struct drm_i915_private *dev_priv = to_i915(dev);
  9661. struct intel_framebuffer *intel_fb =
  9662. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9663. struct drm_i915_gem_object *obj = intel_fb->obj;
  9664. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  9665. u32 dspcntr;
  9666. dspcntr = I915_READ(reg);
  9667. if (obj->tiling_mode != I915_TILING_NONE)
  9668. dspcntr |= DISPPLANE_TILED;
  9669. else
  9670. dspcntr &= ~DISPPLANE_TILED;
  9671. I915_WRITE(reg, dspcntr);
  9672. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9673. POSTING_READ(DSPSURF(intel_crtc->plane));
  9674. }
  9675. static void intel_mmio_flip_work_func(struct work_struct *w)
  9676. {
  9677. struct intel_flip_work *work =
  9678. container_of(w, struct intel_flip_work, mmio_work);
  9679. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9680. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9681. struct intel_framebuffer *intel_fb =
  9682. to_intel_framebuffer(crtc->base.primary->fb);
  9683. struct drm_i915_gem_object *obj = intel_fb->obj;
  9684. struct reservation_object *resv;
  9685. if (work->flip_queued_req)
  9686. WARN_ON(__i915_wait_request(work->flip_queued_req,
  9687. false, NULL,
  9688. &dev_priv->rps.mmioflips));
  9689. /* For framebuffer backed by dmabuf, wait for fence */
  9690. resv = i915_gem_object_get_dmabuf_resv(obj);
  9691. if (resv)
  9692. WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
  9693. MAX_SCHEDULE_TIMEOUT) < 0);
  9694. intel_pipe_update_start(crtc);
  9695. if (INTEL_GEN(dev_priv) >= 9)
  9696. skl_do_mmio_flip(crtc, work->rotation, work);
  9697. else
  9698. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9699. ilk_do_mmio_flip(crtc, work);
  9700. intel_pipe_update_end(crtc, work);
  9701. }
  9702. static int intel_default_queue_flip(struct drm_device *dev,
  9703. struct drm_crtc *crtc,
  9704. struct drm_framebuffer *fb,
  9705. struct drm_i915_gem_object *obj,
  9706. struct drm_i915_gem_request *req,
  9707. uint32_t flags)
  9708. {
  9709. return -ENODEV;
  9710. }
  9711. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  9712. struct intel_crtc *intel_crtc,
  9713. struct intel_flip_work *work)
  9714. {
  9715. u32 addr, vblank;
  9716. if (!atomic_read(&work->pending))
  9717. return false;
  9718. smp_rmb();
  9719. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  9720. if (work->flip_ready_vblank == 0) {
  9721. if (work->flip_queued_req &&
  9722. !i915_gem_request_completed(work->flip_queued_req))
  9723. return false;
  9724. work->flip_ready_vblank = vblank;
  9725. }
  9726. if (vblank - work->flip_ready_vblank < 3)
  9727. return false;
  9728. /* Potential stall - if we see that the flip has happened,
  9729. * assume a missed interrupt. */
  9730. if (INTEL_GEN(dev_priv) >= 4)
  9731. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9732. else
  9733. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9734. /* There is a potential issue here with a false positive after a flip
  9735. * to the same address. We could address this by checking for a
  9736. * non-incrementing frame counter.
  9737. */
  9738. return addr == work->gtt_offset;
  9739. }
  9740. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  9741. {
  9742. struct drm_device *dev = &dev_priv->drm;
  9743. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9744. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9745. struct intel_flip_work *work;
  9746. WARN_ON(!in_interrupt());
  9747. if (crtc == NULL)
  9748. return;
  9749. spin_lock(&dev->event_lock);
  9750. work = intel_crtc->flip_work;
  9751. if (work != NULL && !is_mmio_work(work) &&
  9752. __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
  9753. WARN_ONCE(1,
  9754. "Kicking stuck page flip: queued at %d, now %d\n",
  9755. work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
  9756. page_flip_completed(intel_crtc);
  9757. work = NULL;
  9758. }
  9759. if (work != NULL && !is_mmio_work(work) &&
  9760. intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
  9761. intel_queue_rps_boost_for_request(work->flip_queued_req);
  9762. spin_unlock(&dev->event_lock);
  9763. }
  9764. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9765. struct drm_framebuffer *fb,
  9766. struct drm_pending_vblank_event *event,
  9767. uint32_t page_flip_flags)
  9768. {
  9769. struct drm_device *dev = crtc->dev;
  9770. struct drm_i915_private *dev_priv = to_i915(dev);
  9771. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9772. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9773. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9774. struct drm_plane *primary = crtc->primary;
  9775. enum pipe pipe = intel_crtc->pipe;
  9776. struct intel_flip_work *work;
  9777. struct intel_engine_cs *engine;
  9778. bool mmio_flip;
  9779. struct drm_i915_gem_request *request = NULL;
  9780. int ret;
  9781. /*
  9782. * drm_mode_page_flip_ioctl() should already catch this, but double
  9783. * check to be safe. In the future we may enable pageflipping from
  9784. * a disabled primary plane.
  9785. */
  9786. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9787. return -EBUSY;
  9788. /* Can't change pixel format via MI display flips. */
  9789. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9790. return -EINVAL;
  9791. /*
  9792. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9793. * Note that pitch changes could also affect these register.
  9794. */
  9795. if (INTEL_INFO(dev)->gen > 3 &&
  9796. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9797. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9798. return -EINVAL;
  9799. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9800. goto out_hang;
  9801. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9802. if (work == NULL)
  9803. return -ENOMEM;
  9804. work->event = event;
  9805. work->crtc = crtc;
  9806. work->old_fb = old_fb;
  9807. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  9808. ret = drm_crtc_vblank_get(crtc);
  9809. if (ret)
  9810. goto free_work;
  9811. /* We borrow the event spin lock for protecting flip_work */
  9812. spin_lock_irq(&dev->event_lock);
  9813. if (intel_crtc->flip_work) {
  9814. /* Before declaring the flip queue wedged, check if
  9815. * the hardware completed the operation behind our backs.
  9816. */
  9817. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  9818. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9819. page_flip_completed(intel_crtc);
  9820. } else {
  9821. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9822. spin_unlock_irq(&dev->event_lock);
  9823. drm_crtc_vblank_put(crtc);
  9824. kfree(work);
  9825. return -EBUSY;
  9826. }
  9827. }
  9828. intel_crtc->flip_work = work;
  9829. spin_unlock_irq(&dev->event_lock);
  9830. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9831. flush_workqueue(dev_priv->wq);
  9832. /* Reference the objects for the scheduled work. */
  9833. drm_framebuffer_reference(work->old_fb);
  9834. drm_gem_object_reference(&obj->base);
  9835. crtc->primary->fb = fb;
  9836. update_state_fb(crtc->primary);
  9837. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  9838. to_intel_plane_state(primary->state));
  9839. work->pending_flip_obj = obj;
  9840. ret = i915_mutex_lock_interruptible(dev);
  9841. if (ret)
  9842. goto cleanup;
  9843. intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9844. if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
  9845. ret = -EIO;
  9846. goto cleanup;
  9847. }
  9848. atomic_inc(&intel_crtc->unpin_work_count);
  9849. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9850. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9851. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  9852. engine = &dev_priv->engine[BCS];
  9853. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9854. /* vlv: DISPLAY_FLIP fails to change tiling */
  9855. engine = NULL;
  9856. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9857. engine = &dev_priv->engine[BCS];
  9858. } else if (INTEL_INFO(dev)->gen >= 7) {
  9859. engine = i915_gem_request_get_engine(obj->last_write_req);
  9860. if (engine == NULL || engine->id != RCS)
  9861. engine = &dev_priv->engine[BCS];
  9862. } else {
  9863. engine = &dev_priv->engine[RCS];
  9864. }
  9865. mmio_flip = use_mmio_flip(engine, obj);
  9866. /* When using CS flips, we want to emit semaphores between rings.
  9867. * However, when using mmio flips we will create a task to do the
  9868. * synchronisation, so all we want here is to pin the framebuffer
  9869. * into the display plane and skip any waits.
  9870. */
  9871. if (!mmio_flip) {
  9872. ret = i915_gem_object_sync(obj, engine, &request);
  9873. if (!ret && !request) {
  9874. request = i915_gem_request_alloc(engine, NULL);
  9875. ret = PTR_ERR_OR_ZERO(request);
  9876. }
  9877. if (ret)
  9878. goto cleanup_pending;
  9879. }
  9880. ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  9881. if (ret)
  9882. goto cleanup_pending;
  9883. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9884. obj, 0);
  9885. work->gtt_offset += intel_crtc->dspaddr_offset;
  9886. work->rotation = crtc->primary->state->rotation;
  9887. if (mmio_flip) {
  9888. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  9889. i915_gem_request_assign(&work->flip_queued_req,
  9890. obj->last_write_req);
  9891. schedule_work(&work->mmio_work);
  9892. } else {
  9893. i915_gem_request_assign(&work->flip_queued_req, request);
  9894. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9895. page_flip_flags);
  9896. if (ret)
  9897. goto cleanup_unpin;
  9898. intel_mark_page_flip_active(intel_crtc, work);
  9899. i915_add_request_no_flush(request);
  9900. }
  9901. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  9902. to_intel_plane(primary)->frontbuffer_bit);
  9903. mutex_unlock(&dev->struct_mutex);
  9904. intel_frontbuffer_flip_prepare(dev,
  9905. to_intel_plane(primary)->frontbuffer_bit);
  9906. trace_i915_flip_request(intel_crtc->plane, obj);
  9907. return 0;
  9908. cleanup_unpin:
  9909. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  9910. cleanup_pending:
  9911. if (!IS_ERR_OR_NULL(request))
  9912. i915_add_request_no_flush(request);
  9913. atomic_dec(&intel_crtc->unpin_work_count);
  9914. mutex_unlock(&dev->struct_mutex);
  9915. cleanup:
  9916. crtc->primary->fb = old_fb;
  9917. update_state_fb(crtc->primary);
  9918. drm_gem_object_unreference_unlocked(&obj->base);
  9919. drm_framebuffer_unreference(work->old_fb);
  9920. spin_lock_irq(&dev->event_lock);
  9921. intel_crtc->flip_work = NULL;
  9922. spin_unlock_irq(&dev->event_lock);
  9923. drm_crtc_vblank_put(crtc);
  9924. free_work:
  9925. kfree(work);
  9926. if (ret == -EIO) {
  9927. struct drm_atomic_state *state;
  9928. struct drm_plane_state *plane_state;
  9929. out_hang:
  9930. state = drm_atomic_state_alloc(dev);
  9931. if (!state)
  9932. return -ENOMEM;
  9933. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9934. retry:
  9935. plane_state = drm_atomic_get_plane_state(state, primary);
  9936. ret = PTR_ERR_OR_ZERO(plane_state);
  9937. if (!ret) {
  9938. drm_atomic_set_fb_for_plane(plane_state, fb);
  9939. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9940. if (!ret)
  9941. ret = drm_atomic_commit(state);
  9942. }
  9943. if (ret == -EDEADLK) {
  9944. drm_modeset_backoff(state->acquire_ctx);
  9945. drm_atomic_state_clear(state);
  9946. goto retry;
  9947. }
  9948. if (ret)
  9949. drm_atomic_state_free(state);
  9950. if (ret == 0 && event) {
  9951. spin_lock_irq(&dev->event_lock);
  9952. drm_crtc_send_vblank_event(crtc, event);
  9953. spin_unlock_irq(&dev->event_lock);
  9954. }
  9955. }
  9956. return ret;
  9957. }
  9958. /**
  9959. * intel_wm_need_update - Check whether watermarks need updating
  9960. * @plane: drm plane
  9961. * @state: new plane state
  9962. *
  9963. * Check current plane state versus the new one to determine whether
  9964. * watermarks need to be recalculated.
  9965. *
  9966. * Returns true or false.
  9967. */
  9968. static bool intel_wm_need_update(struct drm_plane *plane,
  9969. struct drm_plane_state *state)
  9970. {
  9971. struct intel_plane_state *new = to_intel_plane_state(state);
  9972. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9973. /* Update watermarks on tiling or size changes. */
  9974. if (new->visible != cur->visible)
  9975. return true;
  9976. if (!cur->base.fb || !new->base.fb)
  9977. return false;
  9978. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  9979. cur->base.rotation != new->base.rotation ||
  9980. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9981. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9982. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9983. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9984. return true;
  9985. return false;
  9986. }
  9987. static bool needs_scaling(struct intel_plane_state *state)
  9988. {
  9989. int src_w = drm_rect_width(&state->src) >> 16;
  9990. int src_h = drm_rect_height(&state->src) >> 16;
  9991. int dst_w = drm_rect_width(&state->dst);
  9992. int dst_h = drm_rect_height(&state->dst);
  9993. return (src_w != dst_w || src_h != dst_h);
  9994. }
  9995. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9996. struct drm_plane_state *plane_state)
  9997. {
  9998. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9999. struct drm_crtc *crtc = crtc_state->crtc;
  10000. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10001. struct drm_plane *plane = plane_state->plane;
  10002. struct drm_device *dev = crtc->dev;
  10003. struct drm_i915_private *dev_priv = to_i915(dev);
  10004. struct intel_plane_state *old_plane_state =
  10005. to_intel_plane_state(plane->state);
  10006. bool mode_changed = needs_modeset(crtc_state);
  10007. bool was_crtc_enabled = crtc->state->active;
  10008. bool is_crtc_enabled = crtc_state->active;
  10009. bool turn_off, turn_on, visible, was_visible;
  10010. struct drm_framebuffer *fb = plane_state->fb;
  10011. int ret;
  10012. if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  10013. ret = skl_update_scaler_plane(
  10014. to_intel_crtc_state(crtc_state),
  10015. to_intel_plane_state(plane_state));
  10016. if (ret)
  10017. return ret;
  10018. }
  10019. was_visible = old_plane_state->visible;
  10020. visible = to_intel_plane_state(plane_state)->visible;
  10021. if (!was_crtc_enabled && WARN_ON(was_visible))
  10022. was_visible = false;
  10023. /*
  10024. * Visibility is calculated as if the crtc was on, but
  10025. * after scaler setup everything depends on it being off
  10026. * when the crtc isn't active.
  10027. *
  10028. * FIXME this is wrong for watermarks. Watermarks should also
  10029. * be computed as if the pipe would be active. Perhaps move
  10030. * per-plane wm computation to the .check_plane() hook, and
  10031. * only combine the results from all planes in the current place?
  10032. */
  10033. if (!is_crtc_enabled)
  10034. to_intel_plane_state(plane_state)->visible = visible = false;
  10035. if (!was_visible && !visible)
  10036. return 0;
  10037. if (fb != old_plane_state->base.fb)
  10038. pipe_config->fb_changed = true;
  10039. turn_off = was_visible && (!visible || mode_changed);
  10040. turn_on = visible && (!was_visible || mode_changed);
  10041. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  10042. intel_crtc->base.base.id,
  10043. intel_crtc->base.name,
  10044. plane->base.id, plane->name,
  10045. fb ? fb->base.id : -1);
  10046. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  10047. plane->base.id, plane->name,
  10048. was_visible, visible,
  10049. turn_off, turn_on, mode_changed);
  10050. if (turn_on) {
  10051. pipe_config->update_wm_pre = true;
  10052. /* must disable cxsr around plane enable/disable */
  10053. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10054. pipe_config->disable_cxsr = true;
  10055. } else if (turn_off) {
  10056. pipe_config->update_wm_post = true;
  10057. /* must disable cxsr around plane enable/disable */
  10058. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10059. pipe_config->disable_cxsr = true;
  10060. } else if (intel_wm_need_update(plane, plane_state)) {
  10061. /* FIXME bollocks */
  10062. pipe_config->update_wm_pre = true;
  10063. pipe_config->update_wm_post = true;
  10064. }
  10065. /* Pre-gen9 platforms need two-step watermark updates */
  10066. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10067. INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
  10068. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10069. if (visible || was_visible)
  10070. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10071. /*
  10072. * WaCxSRDisabledForSpriteScaling:ivb
  10073. *
  10074. * cstate->update_wm was already set above, so this flag will
  10075. * take effect when we commit and program watermarks.
  10076. */
  10077. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
  10078. needs_scaling(to_intel_plane_state(plane_state)) &&
  10079. !needs_scaling(old_plane_state))
  10080. pipe_config->disable_lp_wm = true;
  10081. return 0;
  10082. }
  10083. static bool encoders_cloneable(const struct intel_encoder *a,
  10084. const struct intel_encoder *b)
  10085. {
  10086. /* masks could be asymmetric, so check both ways */
  10087. return a == b || (a->cloneable & (1 << b->type) &&
  10088. b->cloneable & (1 << a->type));
  10089. }
  10090. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10091. struct intel_crtc *crtc,
  10092. struct intel_encoder *encoder)
  10093. {
  10094. struct intel_encoder *source_encoder;
  10095. struct drm_connector *connector;
  10096. struct drm_connector_state *connector_state;
  10097. int i;
  10098. for_each_connector_in_state(state, connector, connector_state, i) {
  10099. if (connector_state->crtc != &crtc->base)
  10100. continue;
  10101. source_encoder =
  10102. to_intel_encoder(connector_state->best_encoder);
  10103. if (!encoders_cloneable(encoder, source_encoder))
  10104. return false;
  10105. }
  10106. return true;
  10107. }
  10108. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10109. struct drm_crtc_state *crtc_state)
  10110. {
  10111. struct drm_device *dev = crtc->dev;
  10112. struct drm_i915_private *dev_priv = to_i915(dev);
  10113. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10114. struct intel_crtc_state *pipe_config =
  10115. to_intel_crtc_state(crtc_state);
  10116. struct drm_atomic_state *state = crtc_state->state;
  10117. int ret;
  10118. bool mode_changed = needs_modeset(crtc_state);
  10119. if (mode_changed && !crtc_state->active)
  10120. pipe_config->update_wm_post = true;
  10121. if (mode_changed && crtc_state->enable &&
  10122. dev_priv->display.crtc_compute_clock &&
  10123. !WARN_ON(pipe_config->shared_dpll)) {
  10124. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10125. pipe_config);
  10126. if (ret)
  10127. return ret;
  10128. }
  10129. if (crtc_state->color_mgmt_changed) {
  10130. ret = intel_color_check(crtc, crtc_state);
  10131. if (ret)
  10132. return ret;
  10133. /*
  10134. * Changing color management on Intel hardware is
  10135. * handled as part of planes update.
  10136. */
  10137. crtc_state->planes_changed = true;
  10138. }
  10139. ret = 0;
  10140. if (dev_priv->display.compute_pipe_wm) {
  10141. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10142. if (ret) {
  10143. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10144. return ret;
  10145. }
  10146. }
  10147. if (dev_priv->display.compute_intermediate_wm &&
  10148. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10149. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10150. return 0;
  10151. /*
  10152. * Calculate 'intermediate' watermarks that satisfy both the
  10153. * old state and the new state. We can program these
  10154. * immediately.
  10155. */
  10156. ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
  10157. intel_crtc,
  10158. pipe_config);
  10159. if (ret) {
  10160. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10161. return ret;
  10162. }
  10163. } else if (dev_priv->display.compute_intermediate_wm) {
  10164. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10165. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10166. }
  10167. if (INTEL_INFO(dev)->gen >= 9) {
  10168. if (mode_changed)
  10169. ret = skl_update_scaler_crtc(pipe_config);
  10170. if (!ret)
  10171. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10172. pipe_config);
  10173. }
  10174. return ret;
  10175. }
  10176. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10177. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10178. .atomic_begin = intel_begin_crtc_commit,
  10179. .atomic_flush = intel_finish_crtc_commit,
  10180. .atomic_check = intel_crtc_atomic_check,
  10181. };
  10182. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10183. {
  10184. struct intel_connector *connector;
  10185. for_each_intel_connector(dev, connector) {
  10186. if (connector->base.state->crtc)
  10187. drm_connector_unreference(&connector->base);
  10188. if (connector->base.encoder) {
  10189. connector->base.state->best_encoder =
  10190. connector->base.encoder;
  10191. connector->base.state->crtc =
  10192. connector->base.encoder->crtc;
  10193. drm_connector_reference(&connector->base);
  10194. } else {
  10195. connector->base.state->best_encoder = NULL;
  10196. connector->base.state->crtc = NULL;
  10197. }
  10198. }
  10199. }
  10200. static void
  10201. connected_sink_compute_bpp(struct intel_connector *connector,
  10202. struct intel_crtc_state *pipe_config)
  10203. {
  10204. int bpp = pipe_config->pipe_bpp;
  10205. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10206. connector->base.base.id,
  10207. connector->base.name);
  10208. /* Don't use an invalid EDID bpc value */
  10209. if (connector->base.display_info.bpc &&
  10210. connector->base.display_info.bpc * 3 < bpp) {
  10211. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10212. bpp, connector->base.display_info.bpc*3);
  10213. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10214. }
  10215. /* Clamp bpp to 8 on screens without EDID 1.4 */
  10216. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  10217. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  10218. bpp);
  10219. pipe_config->pipe_bpp = 24;
  10220. }
  10221. }
  10222. static int
  10223. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10224. struct intel_crtc_state *pipe_config)
  10225. {
  10226. struct drm_device *dev = crtc->base.dev;
  10227. struct drm_atomic_state *state;
  10228. struct drm_connector *connector;
  10229. struct drm_connector_state *connector_state;
  10230. int bpp, i;
  10231. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  10232. bpp = 10*3;
  10233. else if (INTEL_INFO(dev)->gen >= 5)
  10234. bpp = 12*3;
  10235. else
  10236. bpp = 8*3;
  10237. pipe_config->pipe_bpp = bpp;
  10238. state = pipe_config->base.state;
  10239. /* Clamp display bpp to EDID value */
  10240. for_each_connector_in_state(state, connector, connector_state, i) {
  10241. if (connector_state->crtc != &crtc->base)
  10242. continue;
  10243. connected_sink_compute_bpp(to_intel_connector(connector),
  10244. pipe_config);
  10245. }
  10246. return bpp;
  10247. }
  10248. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10249. {
  10250. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10251. "type: 0x%x flags: 0x%x\n",
  10252. mode->crtc_clock,
  10253. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10254. mode->crtc_hsync_end, mode->crtc_htotal,
  10255. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10256. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10257. }
  10258. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10259. struct intel_crtc_state *pipe_config,
  10260. const char *context)
  10261. {
  10262. struct drm_device *dev = crtc->base.dev;
  10263. struct drm_plane *plane;
  10264. struct intel_plane *intel_plane;
  10265. struct intel_plane_state *state;
  10266. struct drm_framebuffer *fb;
  10267. DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
  10268. crtc->base.base.id, crtc->base.name,
  10269. context, pipe_config, pipe_name(crtc->pipe));
  10270. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  10271. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10272. pipe_config->pipe_bpp, pipe_config->dither);
  10273. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10274. pipe_config->has_pch_encoder,
  10275. pipe_config->fdi_lanes,
  10276. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10277. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10278. pipe_config->fdi_m_n.tu);
  10279. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10280. intel_crtc_has_dp_encoder(pipe_config),
  10281. pipe_config->lane_count,
  10282. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10283. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10284. pipe_config->dp_m_n.tu);
  10285. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10286. intel_crtc_has_dp_encoder(pipe_config),
  10287. pipe_config->lane_count,
  10288. pipe_config->dp_m2_n2.gmch_m,
  10289. pipe_config->dp_m2_n2.gmch_n,
  10290. pipe_config->dp_m2_n2.link_m,
  10291. pipe_config->dp_m2_n2.link_n,
  10292. pipe_config->dp_m2_n2.tu);
  10293. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10294. pipe_config->has_audio,
  10295. pipe_config->has_infoframe);
  10296. DRM_DEBUG_KMS("requested mode:\n");
  10297. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10298. DRM_DEBUG_KMS("adjusted mode:\n");
  10299. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10300. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10301. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10302. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10303. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10304. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10305. crtc->num_scalers,
  10306. pipe_config->scaler_state.scaler_users,
  10307. pipe_config->scaler_state.scaler_id);
  10308. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10309. pipe_config->gmch_pfit.control,
  10310. pipe_config->gmch_pfit.pgm_ratios,
  10311. pipe_config->gmch_pfit.lvds_border_bits);
  10312. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10313. pipe_config->pch_pfit.pos,
  10314. pipe_config->pch_pfit.size,
  10315. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10316. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10317. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10318. if (IS_BROXTON(dev)) {
  10319. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10320. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10321. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10322. pipe_config->ddi_pll_sel,
  10323. pipe_config->dpll_hw_state.ebb0,
  10324. pipe_config->dpll_hw_state.ebb4,
  10325. pipe_config->dpll_hw_state.pll0,
  10326. pipe_config->dpll_hw_state.pll1,
  10327. pipe_config->dpll_hw_state.pll2,
  10328. pipe_config->dpll_hw_state.pll3,
  10329. pipe_config->dpll_hw_state.pll6,
  10330. pipe_config->dpll_hw_state.pll8,
  10331. pipe_config->dpll_hw_state.pll9,
  10332. pipe_config->dpll_hw_state.pll10,
  10333. pipe_config->dpll_hw_state.pcsdw12);
  10334. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10335. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10336. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10337. pipe_config->ddi_pll_sel,
  10338. pipe_config->dpll_hw_state.ctrl1,
  10339. pipe_config->dpll_hw_state.cfgcr1,
  10340. pipe_config->dpll_hw_state.cfgcr2);
  10341. } else if (HAS_DDI(dev)) {
  10342. DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10343. pipe_config->ddi_pll_sel,
  10344. pipe_config->dpll_hw_state.wrpll,
  10345. pipe_config->dpll_hw_state.spll);
  10346. } else {
  10347. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10348. "fp0: 0x%x, fp1: 0x%x\n",
  10349. pipe_config->dpll_hw_state.dpll,
  10350. pipe_config->dpll_hw_state.dpll_md,
  10351. pipe_config->dpll_hw_state.fp0,
  10352. pipe_config->dpll_hw_state.fp1);
  10353. }
  10354. DRM_DEBUG_KMS("planes on this crtc\n");
  10355. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10356. intel_plane = to_intel_plane(plane);
  10357. if (intel_plane->pipe != crtc->pipe)
  10358. continue;
  10359. state = to_intel_plane_state(plane->state);
  10360. fb = state->base.fb;
  10361. if (!fb) {
  10362. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10363. plane->base.id, plane->name, state->scaler_id);
  10364. continue;
  10365. }
  10366. DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
  10367. plane->base.id, plane->name);
  10368. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
  10369. fb->base.id, fb->width, fb->height,
  10370. drm_get_format_name(fb->pixel_format));
  10371. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10372. state->scaler_id,
  10373. state->src.x1 >> 16, state->src.y1 >> 16,
  10374. drm_rect_width(&state->src) >> 16,
  10375. drm_rect_height(&state->src) >> 16,
  10376. state->dst.x1, state->dst.y1,
  10377. drm_rect_width(&state->dst),
  10378. drm_rect_height(&state->dst));
  10379. }
  10380. }
  10381. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10382. {
  10383. struct drm_device *dev = state->dev;
  10384. struct drm_connector *connector;
  10385. unsigned int used_ports = 0;
  10386. /*
  10387. * Walk the connector list instead of the encoder
  10388. * list to detect the problem on ddi platforms
  10389. * where there's just one encoder per digital port.
  10390. */
  10391. drm_for_each_connector(connector, dev) {
  10392. struct drm_connector_state *connector_state;
  10393. struct intel_encoder *encoder;
  10394. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10395. if (!connector_state)
  10396. connector_state = connector->state;
  10397. if (!connector_state->best_encoder)
  10398. continue;
  10399. encoder = to_intel_encoder(connector_state->best_encoder);
  10400. WARN_ON(!connector_state->crtc);
  10401. switch (encoder->type) {
  10402. unsigned int port_mask;
  10403. case INTEL_OUTPUT_UNKNOWN:
  10404. if (WARN_ON(!HAS_DDI(dev)))
  10405. break;
  10406. case INTEL_OUTPUT_DP:
  10407. case INTEL_OUTPUT_HDMI:
  10408. case INTEL_OUTPUT_EDP:
  10409. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10410. /* the same port mustn't appear more than once */
  10411. if (used_ports & port_mask)
  10412. return false;
  10413. used_ports |= port_mask;
  10414. default:
  10415. break;
  10416. }
  10417. }
  10418. return true;
  10419. }
  10420. static void
  10421. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10422. {
  10423. struct drm_crtc_state tmp_state;
  10424. struct intel_crtc_scaler_state scaler_state;
  10425. struct intel_dpll_hw_state dpll_hw_state;
  10426. struct intel_shared_dpll *shared_dpll;
  10427. uint32_t ddi_pll_sel;
  10428. bool force_thru;
  10429. /* FIXME: before the switch to atomic started, a new pipe_config was
  10430. * kzalloc'd. Code that depends on any field being zero should be
  10431. * fixed, so that the crtc_state can be safely duplicated. For now,
  10432. * only fields that are know to not cause problems are preserved. */
  10433. tmp_state = crtc_state->base;
  10434. scaler_state = crtc_state->scaler_state;
  10435. shared_dpll = crtc_state->shared_dpll;
  10436. dpll_hw_state = crtc_state->dpll_hw_state;
  10437. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10438. force_thru = crtc_state->pch_pfit.force_thru;
  10439. memset(crtc_state, 0, sizeof *crtc_state);
  10440. crtc_state->base = tmp_state;
  10441. crtc_state->scaler_state = scaler_state;
  10442. crtc_state->shared_dpll = shared_dpll;
  10443. crtc_state->dpll_hw_state = dpll_hw_state;
  10444. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10445. crtc_state->pch_pfit.force_thru = force_thru;
  10446. }
  10447. static int
  10448. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10449. struct intel_crtc_state *pipe_config)
  10450. {
  10451. struct drm_atomic_state *state = pipe_config->base.state;
  10452. struct intel_encoder *encoder;
  10453. struct drm_connector *connector;
  10454. struct drm_connector_state *connector_state;
  10455. int base_bpp, ret = -EINVAL;
  10456. int i;
  10457. bool retry = true;
  10458. clear_intel_crtc_state(pipe_config);
  10459. pipe_config->cpu_transcoder =
  10460. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10461. /*
  10462. * Sanitize sync polarity flags based on requested ones. If neither
  10463. * positive or negative polarity is requested, treat this as meaning
  10464. * negative polarity.
  10465. */
  10466. if (!(pipe_config->base.adjusted_mode.flags &
  10467. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10468. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10469. if (!(pipe_config->base.adjusted_mode.flags &
  10470. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10471. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10472. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10473. pipe_config);
  10474. if (base_bpp < 0)
  10475. goto fail;
  10476. /*
  10477. * Determine the real pipe dimensions. Note that stereo modes can
  10478. * increase the actual pipe size due to the frame doubling and
  10479. * insertion of additional space for blanks between the frame. This
  10480. * is stored in the crtc timings. We use the requested mode to do this
  10481. * computation to clearly distinguish it from the adjusted mode, which
  10482. * can be changed by the connectors in the below retry loop.
  10483. */
  10484. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10485. &pipe_config->pipe_src_w,
  10486. &pipe_config->pipe_src_h);
  10487. for_each_connector_in_state(state, connector, connector_state, i) {
  10488. if (connector_state->crtc != crtc)
  10489. continue;
  10490. encoder = to_intel_encoder(connector_state->best_encoder);
  10491. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  10492. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10493. goto fail;
  10494. }
  10495. /*
  10496. * Determine output_types before calling the .compute_config()
  10497. * hooks so that the hooks can use this information safely.
  10498. */
  10499. pipe_config->output_types |= 1 << encoder->type;
  10500. }
  10501. encoder_retry:
  10502. /* Ensure the port clock defaults are reset when retrying. */
  10503. pipe_config->port_clock = 0;
  10504. pipe_config->pixel_multiplier = 1;
  10505. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10506. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10507. CRTC_STEREO_DOUBLE);
  10508. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10509. * adjust it according to limitations or connector properties, and also
  10510. * a chance to reject the mode entirely.
  10511. */
  10512. for_each_connector_in_state(state, connector, connector_state, i) {
  10513. if (connector_state->crtc != crtc)
  10514. continue;
  10515. encoder = to_intel_encoder(connector_state->best_encoder);
  10516. if (!(encoder->compute_config(encoder, pipe_config))) {
  10517. DRM_DEBUG_KMS("Encoder config failure\n");
  10518. goto fail;
  10519. }
  10520. }
  10521. /* Set default port clock if not overwritten by the encoder. Needs to be
  10522. * done afterwards in case the encoder adjusts the mode. */
  10523. if (!pipe_config->port_clock)
  10524. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10525. * pipe_config->pixel_multiplier;
  10526. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10527. if (ret < 0) {
  10528. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10529. goto fail;
  10530. }
  10531. if (ret == RETRY) {
  10532. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10533. ret = -EINVAL;
  10534. goto fail;
  10535. }
  10536. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10537. retry = false;
  10538. goto encoder_retry;
  10539. }
  10540. /* Dithering seems to not pass-through bits correctly when it should, so
  10541. * only enable it on 6bpc panels. */
  10542. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10543. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10544. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10545. fail:
  10546. return ret;
  10547. }
  10548. static void
  10549. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10550. {
  10551. struct drm_crtc *crtc;
  10552. struct drm_crtc_state *crtc_state;
  10553. int i;
  10554. /* Double check state. */
  10555. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10556. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10557. /* Update hwmode for vblank functions */
  10558. if (crtc->state->active)
  10559. crtc->hwmode = crtc->state->adjusted_mode;
  10560. else
  10561. crtc->hwmode.crtc_clock = 0;
  10562. /*
  10563. * Update legacy state to satisfy fbc code. This can
  10564. * be removed when fbc uses the atomic state.
  10565. */
  10566. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10567. struct drm_plane_state *plane_state = crtc->primary->state;
  10568. crtc->primary->fb = plane_state->fb;
  10569. crtc->x = plane_state->src_x >> 16;
  10570. crtc->y = plane_state->src_y >> 16;
  10571. }
  10572. }
  10573. }
  10574. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10575. {
  10576. int diff;
  10577. if (clock1 == clock2)
  10578. return true;
  10579. if (!clock1 || !clock2)
  10580. return false;
  10581. diff = abs(clock1 - clock2);
  10582. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10583. return true;
  10584. return false;
  10585. }
  10586. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10587. list_for_each_entry((intel_crtc), \
  10588. &(dev)->mode_config.crtc_list, \
  10589. base.head) \
  10590. for_each_if (mask & (1 <<(intel_crtc)->pipe))
  10591. static bool
  10592. intel_compare_m_n(unsigned int m, unsigned int n,
  10593. unsigned int m2, unsigned int n2,
  10594. bool exact)
  10595. {
  10596. if (m == m2 && n == n2)
  10597. return true;
  10598. if (exact || !m || !n || !m2 || !n2)
  10599. return false;
  10600. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10601. if (n > n2) {
  10602. while (n > n2) {
  10603. m2 <<= 1;
  10604. n2 <<= 1;
  10605. }
  10606. } else if (n < n2) {
  10607. while (n < n2) {
  10608. m <<= 1;
  10609. n <<= 1;
  10610. }
  10611. }
  10612. if (n != n2)
  10613. return false;
  10614. return intel_fuzzy_clock_check(m, m2);
  10615. }
  10616. static bool
  10617. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10618. struct intel_link_m_n *m2_n2,
  10619. bool adjust)
  10620. {
  10621. if (m_n->tu == m2_n2->tu &&
  10622. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10623. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10624. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10625. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10626. if (adjust)
  10627. *m2_n2 = *m_n;
  10628. return true;
  10629. }
  10630. return false;
  10631. }
  10632. static bool
  10633. intel_pipe_config_compare(struct drm_device *dev,
  10634. struct intel_crtc_state *current_config,
  10635. struct intel_crtc_state *pipe_config,
  10636. bool adjust)
  10637. {
  10638. bool ret = true;
  10639. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10640. do { \
  10641. if (!adjust) \
  10642. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10643. else \
  10644. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10645. } while (0)
  10646. #define PIPE_CONF_CHECK_X(name) \
  10647. if (current_config->name != pipe_config->name) { \
  10648. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10649. "(expected 0x%08x, found 0x%08x)\n", \
  10650. current_config->name, \
  10651. pipe_config->name); \
  10652. ret = false; \
  10653. }
  10654. #define PIPE_CONF_CHECK_I(name) \
  10655. if (current_config->name != pipe_config->name) { \
  10656. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10657. "(expected %i, found %i)\n", \
  10658. current_config->name, \
  10659. pipe_config->name); \
  10660. ret = false; \
  10661. }
  10662. #define PIPE_CONF_CHECK_P(name) \
  10663. if (current_config->name != pipe_config->name) { \
  10664. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10665. "(expected %p, found %p)\n", \
  10666. current_config->name, \
  10667. pipe_config->name); \
  10668. ret = false; \
  10669. }
  10670. #define PIPE_CONF_CHECK_M_N(name) \
  10671. if (!intel_compare_link_m_n(&current_config->name, \
  10672. &pipe_config->name,\
  10673. adjust)) { \
  10674. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10675. "(expected tu %i gmch %i/%i link %i/%i, " \
  10676. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10677. current_config->name.tu, \
  10678. current_config->name.gmch_m, \
  10679. current_config->name.gmch_n, \
  10680. current_config->name.link_m, \
  10681. current_config->name.link_n, \
  10682. pipe_config->name.tu, \
  10683. pipe_config->name.gmch_m, \
  10684. pipe_config->name.gmch_n, \
  10685. pipe_config->name.link_m, \
  10686. pipe_config->name.link_n); \
  10687. ret = false; \
  10688. }
  10689. /* This is required for BDW+ where there is only one set of registers for
  10690. * switching between high and low RR.
  10691. * This macro can be used whenever a comparison has to be made between one
  10692. * hw state and multiple sw state variables.
  10693. */
  10694. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10695. if (!intel_compare_link_m_n(&current_config->name, \
  10696. &pipe_config->name, adjust) && \
  10697. !intel_compare_link_m_n(&current_config->alt_name, \
  10698. &pipe_config->name, adjust)) { \
  10699. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10700. "(expected tu %i gmch %i/%i link %i/%i, " \
  10701. "or tu %i gmch %i/%i link %i/%i, " \
  10702. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10703. current_config->name.tu, \
  10704. current_config->name.gmch_m, \
  10705. current_config->name.gmch_n, \
  10706. current_config->name.link_m, \
  10707. current_config->name.link_n, \
  10708. current_config->alt_name.tu, \
  10709. current_config->alt_name.gmch_m, \
  10710. current_config->alt_name.gmch_n, \
  10711. current_config->alt_name.link_m, \
  10712. current_config->alt_name.link_n, \
  10713. pipe_config->name.tu, \
  10714. pipe_config->name.gmch_m, \
  10715. pipe_config->name.gmch_n, \
  10716. pipe_config->name.link_m, \
  10717. pipe_config->name.link_n); \
  10718. ret = false; \
  10719. }
  10720. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10721. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10722. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10723. "(expected %i, found %i)\n", \
  10724. current_config->name & (mask), \
  10725. pipe_config->name & (mask)); \
  10726. ret = false; \
  10727. }
  10728. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10729. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10730. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10731. "(expected %i, found %i)\n", \
  10732. current_config->name, \
  10733. pipe_config->name); \
  10734. ret = false; \
  10735. }
  10736. #define PIPE_CONF_QUIRK(quirk) \
  10737. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10738. PIPE_CONF_CHECK_I(cpu_transcoder);
  10739. PIPE_CONF_CHECK_I(has_pch_encoder);
  10740. PIPE_CONF_CHECK_I(fdi_lanes);
  10741. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10742. PIPE_CONF_CHECK_I(lane_count);
  10743. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  10744. if (INTEL_INFO(dev)->gen < 8) {
  10745. PIPE_CONF_CHECK_M_N(dp_m_n);
  10746. if (current_config->has_drrs)
  10747. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10748. } else
  10749. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10750. PIPE_CONF_CHECK_X(output_types);
  10751. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10752. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10753. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10754. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10755. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10756. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10757. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10758. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10759. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10760. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10761. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10762. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10763. PIPE_CONF_CHECK_I(pixel_multiplier);
  10764. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10765. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10766. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  10767. PIPE_CONF_CHECK_I(limited_color_range);
  10768. PIPE_CONF_CHECK_I(has_infoframe);
  10769. PIPE_CONF_CHECK_I(has_audio);
  10770. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10771. DRM_MODE_FLAG_INTERLACE);
  10772. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10773. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10774. DRM_MODE_FLAG_PHSYNC);
  10775. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10776. DRM_MODE_FLAG_NHSYNC);
  10777. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10778. DRM_MODE_FLAG_PVSYNC);
  10779. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10780. DRM_MODE_FLAG_NVSYNC);
  10781. }
  10782. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10783. /* pfit ratios are autocomputed by the hw on gen4+ */
  10784. if (INTEL_INFO(dev)->gen < 4)
  10785. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  10786. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10787. if (!adjust) {
  10788. PIPE_CONF_CHECK_I(pipe_src_w);
  10789. PIPE_CONF_CHECK_I(pipe_src_h);
  10790. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10791. if (current_config->pch_pfit.enabled) {
  10792. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10793. PIPE_CONF_CHECK_X(pch_pfit.size);
  10794. }
  10795. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10796. }
  10797. /* BDW+ don't expose a synchronous way to read the state */
  10798. if (IS_HASWELL(dev))
  10799. PIPE_CONF_CHECK_I(ips_enabled);
  10800. PIPE_CONF_CHECK_I(double_wide);
  10801. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10802. PIPE_CONF_CHECK_P(shared_dpll);
  10803. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10804. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10805. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10806. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10807. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10808. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10809. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10810. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10811. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10812. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  10813. PIPE_CONF_CHECK_X(dsi_pll.div);
  10814. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10815. PIPE_CONF_CHECK_I(pipe_bpp);
  10816. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10817. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10818. #undef PIPE_CONF_CHECK_X
  10819. #undef PIPE_CONF_CHECK_I
  10820. #undef PIPE_CONF_CHECK_P
  10821. #undef PIPE_CONF_CHECK_FLAGS
  10822. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10823. #undef PIPE_CONF_QUIRK
  10824. #undef INTEL_ERR_OR_DBG_KMS
  10825. return ret;
  10826. }
  10827. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  10828. const struct intel_crtc_state *pipe_config)
  10829. {
  10830. if (pipe_config->has_pch_encoder) {
  10831. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  10832. &pipe_config->fdi_m_n);
  10833. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  10834. /*
  10835. * FDI already provided one idea for the dotclock.
  10836. * Yell if the encoder disagrees.
  10837. */
  10838. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  10839. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10840. fdi_dotclock, dotclock);
  10841. }
  10842. }
  10843. static void verify_wm_state(struct drm_crtc *crtc,
  10844. struct drm_crtc_state *new_state)
  10845. {
  10846. struct drm_device *dev = crtc->dev;
  10847. struct drm_i915_private *dev_priv = to_i915(dev);
  10848. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10849. struct skl_ddb_entry *hw_entry, *sw_entry;
  10850. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10851. const enum pipe pipe = intel_crtc->pipe;
  10852. int plane;
  10853. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  10854. return;
  10855. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10856. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10857. /* planes */
  10858. for_each_plane(dev_priv, pipe, plane) {
  10859. hw_entry = &hw_ddb.plane[pipe][plane];
  10860. sw_entry = &sw_ddb->plane[pipe][plane];
  10861. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10862. continue;
  10863. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10864. "(expected (%u,%u), found (%u,%u))\n",
  10865. pipe_name(pipe), plane + 1,
  10866. sw_entry->start, sw_entry->end,
  10867. hw_entry->start, hw_entry->end);
  10868. }
  10869. /* cursor */
  10870. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10871. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10872. if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
  10873. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10874. "(expected (%u,%u), found (%u,%u))\n",
  10875. pipe_name(pipe),
  10876. sw_entry->start, sw_entry->end,
  10877. hw_entry->start, hw_entry->end);
  10878. }
  10879. }
  10880. static void
  10881. verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
  10882. {
  10883. struct drm_connector *connector;
  10884. drm_for_each_connector(connector, dev) {
  10885. struct drm_encoder *encoder = connector->encoder;
  10886. struct drm_connector_state *state = connector->state;
  10887. if (state->crtc != crtc)
  10888. continue;
  10889. intel_connector_verify_state(to_intel_connector(connector));
  10890. I915_STATE_WARN(state->best_encoder != encoder,
  10891. "connector's atomic encoder doesn't match legacy encoder\n");
  10892. }
  10893. }
  10894. static void
  10895. verify_encoder_state(struct drm_device *dev)
  10896. {
  10897. struct intel_encoder *encoder;
  10898. struct intel_connector *connector;
  10899. for_each_intel_encoder(dev, encoder) {
  10900. bool enabled = false;
  10901. enum pipe pipe;
  10902. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10903. encoder->base.base.id,
  10904. encoder->base.name);
  10905. for_each_intel_connector(dev, connector) {
  10906. if (connector->base.state->best_encoder != &encoder->base)
  10907. continue;
  10908. enabled = true;
  10909. I915_STATE_WARN(connector->base.state->crtc !=
  10910. encoder->base.crtc,
  10911. "connector's crtc doesn't match encoder crtc\n");
  10912. }
  10913. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10914. "encoder's enabled state mismatch "
  10915. "(expected %i, found %i)\n",
  10916. !!encoder->base.crtc, enabled);
  10917. if (!encoder->base.crtc) {
  10918. bool active;
  10919. active = encoder->get_hw_state(encoder, &pipe);
  10920. I915_STATE_WARN(active,
  10921. "encoder detached but still enabled on pipe %c.\n",
  10922. pipe_name(pipe));
  10923. }
  10924. }
  10925. }
  10926. static void
  10927. verify_crtc_state(struct drm_crtc *crtc,
  10928. struct drm_crtc_state *old_crtc_state,
  10929. struct drm_crtc_state *new_crtc_state)
  10930. {
  10931. struct drm_device *dev = crtc->dev;
  10932. struct drm_i915_private *dev_priv = to_i915(dev);
  10933. struct intel_encoder *encoder;
  10934. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10935. struct intel_crtc_state *pipe_config, *sw_config;
  10936. struct drm_atomic_state *old_state;
  10937. bool active;
  10938. old_state = old_crtc_state->state;
  10939. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10940. pipe_config = to_intel_crtc_state(old_crtc_state);
  10941. memset(pipe_config, 0, sizeof(*pipe_config));
  10942. pipe_config->base.crtc = crtc;
  10943. pipe_config->base.state = old_state;
  10944. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  10945. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10946. /* hw state is inconsistent with the pipe quirk */
  10947. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10948. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10949. active = new_crtc_state->active;
  10950. I915_STATE_WARN(new_crtc_state->active != active,
  10951. "crtc active state doesn't match with hw state "
  10952. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10953. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10954. "transitional active state does not match atomic hw state "
  10955. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10956. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10957. enum pipe pipe;
  10958. active = encoder->get_hw_state(encoder, &pipe);
  10959. I915_STATE_WARN(active != new_crtc_state->active,
  10960. "[ENCODER:%i] active %i with crtc active %i\n",
  10961. encoder->base.base.id, active, new_crtc_state->active);
  10962. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10963. "Encoder connected to wrong pipe %c\n",
  10964. pipe_name(pipe));
  10965. if (active) {
  10966. pipe_config->output_types |= 1 << encoder->type;
  10967. encoder->get_config(encoder, pipe_config);
  10968. }
  10969. }
  10970. if (!new_crtc_state->active)
  10971. return;
  10972. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10973. sw_config = to_intel_crtc_state(crtc->state);
  10974. if (!intel_pipe_config_compare(dev, sw_config,
  10975. pipe_config, false)) {
  10976. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10977. intel_dump_pipe_config(intel_crtc, pipe_config,
  10978. "[hw state]");
  10979. intel_dump_pipe_config(intel_crtc, sw_config,
  10980. "[sw state]");
  10981. }
  10982. }
  10983. static void
  10984. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10985. struct intel_shared_dpll *pll,
  10986. struct drm_crtc *crtc,
  10987. struct drm_crtc_state *new_state)
  10988. {
  10989. struct intel_dpll_hw_state dpll_hw_state;
  10990. unsigned crtc_mask;
  10991. bool active;
  10992. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10993. DRM_DEBUG_KMS("%s\n", pll->name);
  10994. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10995. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10996. I915_STATE_WARN(!pll->on && pll->active_mask,
  10997. "pll in active use but not on in sw tracking\n");
  10998. I915_STATE_WARN(pll->on && !pll->active_mask,
  10999. "pll is on but not used by any active crtc\n");
  11000. I915_STATE_WARN(pll->on != active,
  11001. "pll on state mismatch (expected %i, found %i)\n",
  11002. pll->on, active);
  11003. }
  11004. if (!crtc) {
  11005. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  11006. "more active pll users than references: %x vs %x\n",
  11007. pll->active_mask, pll->config.crtc_mask);
  11008. return;
  11009. }
  11010. crtc_mask = 1 << drm_crtc_index(crtc);
  11011. if (new_state->active)
  11012. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  11013. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  11014. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11015. else
  11016. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11017. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  11018. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11019. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  11020. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  11021. crtc_mask, pll->config.crtc_mask);
  11022. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  11023. &dpll_hw_state,
  11024. sizeof(dpll_hw_state)),
  11025. "pll hw state mismatch\n");
  11026. }
  11027. static void
  11028. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  11029. struct drm_crtc_state *old_crtc_state,
  11030. struct drm_crtc_state *new_crtc_state)
  11031. {
  11032. struct drm_i915_private *dev_priv = to_i915(dev);
  11033. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  11034. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  11035. if (new_state->shared_dpll)
  11036. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  11037. if (old_state->shared_dpll &&
  11038. old_state->shared_dpll != new_state->shared_dpll) {
  11039. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  11040. struct intel_shared_dpll *pll = old_state->shared_dpll;
  11041. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11042. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  11043. pipe_name(drm_crtc_index(crtc)));
  11044. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  11045. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  11046. pipe_name(drm_crtc_index(crtc)));
  11047. }
  11048. }
  11049. static void
  11050. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11051. struct drm_crtc_state *old_state,
  11052. struct drm_crtc_state *new_state)
  11053. {
  11054. if (!needs_modeset(new_state) &&
  11055. !to_intel_crtc_state(new_state)->update_pipe)
  11056. return;
  11057. verify_wm_state(crtc, new_state);
  11058. verify_connector_state(crtc->dev, crtc);
  11059. verify_crtc_state(crtc, old_state, new_state);
  11060. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11061. }
  11062. static void
  11063. verify_disabled_dpll_state(struct drm_device *dev)
  11064. {
  11065. struct drm_i915_private *dev_priv = to_i915(dev);
  11066. int i;
  11067. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11068. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11069. }
  11070. static void
  11071. intel_modeset_verify_disabled(struct drm_device *dev)
  11072. {
  11073. verify_encoder_state(dev);
  11074. verify_connector_state(dev, NULL);
  11075. verify_disabled_dpll_state(dev);
  11076. }
  11077. static void update_scanline_offset(struct intel_crtc *crtc)
  11078. {
  11079. struct drm_device *dev = crtc->base.dev;
  11080. /*
  11081. * The scanline counter increments at the leading edge of hsync.
  11082. *
  11083. * On most platforms it starts counting from vtotal-1 on the
  11084. * first active line. That means the scanline counter value is
  11085. * always one less than what we would expect. Ie. just after
  11086. * start of vblank, which also occurs at start of hsync (on the
  11087. * last active line), the scanline counter will read vblank_start-1.
  11088. *
  11089. * On gen2 the scanline counter starts counting from 1 instead
  11090. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11091. * to keep the value positive), instead of adding one.
  11092. *
  11093. * On HSW+ the behaviour of the scanline counter depends on the output
  11094. * type. For DP ports it behaves like most other platforms, but on HDMI
  11095. * there's an extra 1 line difference. So we need to add two instead of
  11096. * one to the value.
  11097. */
  11098. if (IS_GEN2(dev)) {
  11099. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11100. int vtotal;
  11101. vtotal = adjusted_mode->crtc_vtotal;
  11102. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11103. vtotal /= 2;
  11104. crtc->scanline_offset = vtotal - 1;
  11105. } else if (HAS_DDI(dev) &&
  11106. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  11107. crtc->scanline_offset = 2;
  11108. } else
  11109. crtc->scanline_offset = 1;
  11110. }
  11111. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11112. {
  11113. struct drm_device *dev = state->dev;
  11114. struct drm_i915_private *dev_priv = to_i915(dev);
  11115. struct intel_shared_dpll_config *shared_dpll = NULL;
  11116. struct drm_crtc *crtc;
  11117. struct drm_crtc_state *crtc_state;
  11118. int i;
  11119. if (!dev_priv->display.crtc_compute_clock)
  11120. return;
  11121. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11122. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11123. struct intel_shared_dpll *old_dpll =
  11124. to_intel_crtc_state(crtc->state)->shared_dpll;
  11125. if (!needs_modeset(crtc_state))
  11126. continue;
  11127. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11128. if (!old_dpll)
  11129. continue;
  11130. if (!shared_dpll)
  11131. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11132. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11133. }
  11134. }
  11135. /*
  11136. * This implements the workaround described in the "notes" section of the mode
  11137. * set sequence documentation. When going from no pipes or single pipe to
  11138. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11139. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11140. */
  11141. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11142. {
  11143. struct drm_crtc_state *crtc_state;
  11144. struct intel_crtc *intel_crtc;
  11145. struct drm_crtc *crtc;
  11146. struct intel_crtc_state *first_crtc_state = NULL;
  11147. struct intel_crtc_state *other_crtc_state = NULL;
  11148. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11149. int i;
  11150. /* look at all crtc's that are going to be enabled in during modeset */
  11151. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11152. intel_crtc = to_intel_crtc(crtc);
  11153. if (!crtc_state->active || !needs_modeset(crtc_state))
  11154. continue;
  11155. if (first_crtc_state) {
  11156. other_crtc_state = to_intel_crtc_state(crtc_state);
  11157. break;
  11158. } else {
  11159. first_crtc_state = to_intel_crtc_state(crtc_state);
  11160. first_pipe = intel_crtc->pipe;
  11161. }
  11162. }
  11163. /* No workaround needed? */
  11164. if (!first_crtc_state)
  11165. return 0;
  11166. /* w/a possibly needed, check how many crtc's are already enabled. */
  11167. for_each_intel_crtc(state->dev, intel_crtc) {
  11168. struct intel_crtc_state *pipe_config;
  11169. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11170. if (IS_ERR(pipe_config))
  11171. return PTR_ERR(pipe_config);
  11172. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11173. if (!pipe_config->base.active ||
  11174. needs_modeset(&pipe_config->base))
  11175. continue;
  11176. /* 2 or more enabled crtcs means no need for w/a */
  11177. if (enabled_pipe != INVALID_PIPE)
  11178. return 0;
  11179. enabled_pipe = intel_crtc->pipe;
  11180. }
  11181. if (enabled_pipe != INVALID_PIPE)
  11182. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11183. else if (other_crtc_state)
  11184. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11185. return 0;
  11186. }
  11187. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11188. {
  11189. struct drm_crtc *crtc;
  11190. struct drm_crtc_state *crtc_state;
  11191. int ret = 0;
  11192. /* add all active pipes to the state */
  11193. for_each_crtc(state->dev, crtc) {
  11194. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11195. if (IS_ERR(crtc_state))
  11196. return PTR_ERR(crtc_state);
  11197. if (!crtc_state->active || needs_modeset(crtc_state))
  11198. continue;
  11199. crtc_state->mode_changed = true;
  11200. ret = drm_atomic_add_affected_connectors(state, crtc);
  11201. if (ret)
  11202. break;
  11203. ret = drm_atomic_add_affected_planes(state, crtc);
  11204. if (ret)
  11205. break;
  11206. }
  11207. return ret;
  11208. }
  11209. static int intel_modeset_checks(struct drm_atomic_state *state)
  11210. {
  11211. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11212. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11213. struct drm_crtc *crtc;
  11214. struct drm_crtc_state *crtc_state;
  11215. int ret = 0, i;
  11216. if (!check_digital_port_conflicts(state)) {
  11217. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11218. return -EINVAL;
  11219. }
  11220. intel_state->modeset = true;
  11221. intel_state->active_crtcs = dev_priv->active_crtcs;
  11222. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11223. if (crtc_state->active)
  11224. intel_state->active_crtcs |= 1 << i;
  11225. else
  11226. intel_state->active_crtcs &= ~(1 << i);
  11227. if (crtc_state->active != crtc->state->active)
  11228. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11229. }
  11230. /*
  11231. * See if the config requires any additional preparation, e.g.
  11232. * to adjust global state with pipes off. We need to do this
  11233. * here so we can get the modeset_pipe updated config for the new
  11234. * mode set on this crtc. For other crtcs we need to use the
  11235. * adjusted_mode bits in the crtc directly.
  11236. */
  11237. if (dev_priv->display.modeset_calc_cdclk) {
  11238. if (!intel_state->cdclk_pll_vco)
  11239. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11240. if (!intel_state->cdclk_pll_vco)
  11241. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11242. ret = dev_priv->display.modeset_calc_cdclk(state);
  11243. if (ret < 0)
  11244. return ret;
  11245. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11246. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
  11247. ret = intel_modeset_all_pipes(state);
  11248. if (ret < 0)
  11249. return ret;
  11250. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11251. intel_state->cdclk, intel_state->dev_cdclk);
  11252. } else
  11253. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11254. intel_modeset_clear_plls(state);
  11255. if (IS_HASWELL(dev_priv))
  11256. return haswell_mode_set_planes_workaround(state);
  11257. return 0;
  11258. }
  11259. /*
  11260. * Handle calculation of various watermark data at the end of the atomic check
  11261. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11262. * handlers to ensure that all derived state has been updated.
  11263. */
  11264. static int calc_watermark_data(struct drm_atomic_state *state)
  11265. {
  11266. struct drm_device *dev = state->dev;
  11267. struct drm_i915_private *dev_priv = to_i915(dev);
  11268. /* Is there platform-specific watermark information to calculate? */
  11269. if (dev_priv->display.compute_global_watermarks)
  11270. return dev_priv->display.compute_global_watermarks(state);
  11271. return 0;
  11272. }
  11273. /**
  11274. * intel_atomic_check - validate state object
  11275. * @dev: drm device
  11276. * @state: state to validate
  11277. */
  11278. static int intel_atomic_check(struct drm_device *dev,
  11279. struct drm_atomic_state *state)
  11280. {
  11281. struct drm_i915_private *dev_priv = to_i915(dev);
  11282. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11283. struct drm_crtc *crtc;
  11284. struct drm_crtc_state *crtc_state;
  11285. int ret, i;
  11286. bool any_ms = false;
  11287. ret = drm_atomic_helper_check_modeset(dev, state);
  11288. if (ret)
  11289. return ret;
  11290. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11291. struct intel_crtc_state *pipe_config =
  11292. to_intel_crtc_state(crtc_state);
  11293. /* Catch I915_MODE_FLAG_INHERITED */
  11294. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11295. crtc_state->mode_changed = true;
  11296. if (!needs_modeset(crtc_state))
  11297. continue;
  11298. if (!crtc_state->enable) {
  11299. any_ms = true;
  11300. continue;
  11301. }
  11302. /* FIXME: For only active_changed we shouldn't need to do any
  11303. * state recomputation at all. */
  11304. ret = drm_atomic_add_affected_connectors(state, crtc);
  11305. if (ret)
  11306. return ret;
  11307. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11308. if (ret) {
  11309. intel_dump_pipe_config(to_intel_crtc(crtc),
  11310. pipe_config, "[failed]");
  11311. return ret;
  11312. }
  11313. if (i915.fastboot &&
  11314. intel_pipe_config_compare(dev,
  11315. to_intel_crtc_state(crtc->state),
  11316. pipe_config, true)) {
  11317. crtc_state->mode_changed = false;
  11318. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11319. }
  11320. if (needs_modeset(crtc_state))
  11321. any_ms = true;
  11322. ret = drm_atomic_add_affected_planes(state, crtc);
  11323. if (ret)
  11324. return ret;
  11325. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11326. needs_modeset(crtc_state) ?
  11327. "[modeset]" : "[fastset]");
  11328. }
  11329. if (any_ms) {
  11330. ret = intel_modeset_checks(state);
  11331. if (ret)
  11332. return ret;
  11333. } else
  11334. intel_state->cdclk = dev_priv->cdclk_freq;
  11335. ret = drm_atomic_helper_check_planes(dev, state);
  11336. if (ret)
  11337. return ret;
  11338. intel_fbc_choose_crtc(dev_priv, state);
  11339. return calc_watermark_data(state);
  11340. }
  11341. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11342. struct drm_atomic_state *state,
  11343. bool nonblock)
  11344. {
  11345. struct drm_i915_private *dev_priv = to_i915(dev);
  11346. struct drm_plane_state *plane_state;
  11347. struct drm_crtc_state *crtc_state;
  11348. struct drm_plane *plane;
  11349. struct drm_crtc *crtc;
  11350. int i, ret;
  11351. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11352. if (state->legacy_cursor_update)
  11353. continue;
  11354. ret = intel_crtc_wait_for_pending_flips(crtc);
  11355. if (ret)
  11356. return ret;
  11357. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11358. flush_workqueue(dev_priv->wq);
  11359. }
  11360. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11361. if (ret)
  11362. return ret;
  11363. ret = drm_atomic_helper_prepare_planes(dev, state);
  11364. mutex_unlock(&dev->struct_mutex);
  11365. if (!ret && !nonblock) {
  11366. for_each_plane_in_state(state, plane, plane_state, i) {
  11367. struct intel_plane_state *intel_plane_state =
  11368. to_intel_plane_state(plane_state);
  11369. if (!intel_plane_state->wait_req)
  11370. continue;
  11371. ret = __i915_wait_request(intel_plane_state->wait_req,
  11372. true, NULL, NULL);
  11373. if (ret) {
  11374. /* Any hang should be swallowed by the wait */
  11375. WARN_ON(ret == -EIO);
  11376. mutex_lock(&dev->struct_mutex);
  11377. drm_atomic_helper_cleanup_planes(dev, state);
  11378. mutex_unlock(&dev->struct_mutex);
  11379. break;
  11380. }
  11381. }
  11382. }
  11383. return ret;
  11384. }
  11385. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11386. {
  11387. struct drm_device *dev = crtc->base.dev;
  11388. if (!dev->max_vblank_count)
  11389. return drm_accurate_vblank_count(&crtc->base);
  11390. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11391. }
  11392. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11393. struct drm_i915_private *dev_priv,
  11394. unsigned crtc_mask)
  11395. {
  11396. unsigned last_vblank_count[I915_MAX_PIPES];
  11397. enum pipe pipe;
  11398. int ret;
  11399. if (!crtc_mask)
  11400. return;
  11401. for_each_pipe(dev_priv, pipe) {
  11402. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11403. if (!((1 << pipe) & crtc_mask))
  11404. continue;
  11405. ret = drm_crtc_vblank_get(crtc);
  11406. if (WARN_ON(ret != 0)) {
  11407. crtc_mask &= ~(1 << pipe);
  11408. continue;
  11409. }
  11410. last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
  11411. }
  11412. for_each_pipe(dev_priv, pipe) {
  11413. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11414. long lret;
  11415. if (!((1 << pipe) & crtc_mask))
  11416. continue;
  11417. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11418. last_vblank_count[pipe] !=
  11419. drm_crtc_vblank_count(crtc),
  11420. msecs_to_jiffies(50));
  11421. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11422. drm_crtc_vblank_put(crtc);
  11423. }
  11424. }
  11425. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11426. {
  11427. /* fb updated, need to unpin old fb */
  11428. if (crtc_state->fb_changed)
  11429. return true;
  11430. /* wm changes, need vblank before final wm's */
  11431. if (crtc_state->update_wm_post)
  11432. return true;
  11433. /*
  11434. * cxsr is re-enabled after vblank.
  11435. * This is already handled by crtc_state->update_wm_post,
  11436. * but added for clarity.
  11437. */
  11438. if (crtc_state->disable_cxsr)
  11439. return true;
  11440. return false;
  11441. }
  11442. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  11443. {
  11444. struct drm_device *dev = state->dev;
  11445. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11446. struct drm_i915_private *dev_priv = to_i915(dev);
  11447. struct drm_crtc_state *old_crtc_state;
  11448. struct drm_crtc *crtc;
  11449. struct intel_crtc_state *intel_cstate;
  11450. struct drm_plane *plane;
  11451. struct drm_plane_state *plane_state;
  11452. bool hw_check = intel_state->modeset;
  11453. unsigned long put_domains[I915_MAX_PIPES] = {};
  11454. unsigned crtc_vblank_mask = 0;
  11455. int i, ret;
  11456. for_each_plane_in_state(state, plane, plane_state, i) {
  11457. struct intel_plane_state *intel_plane_state =
  11458. to_intel_plane_state(plane_state);
  11459. if (!intel_plane_state->wait_req)
  11460. continue;
  11461. ret = __i915_wait_request(intel_plane_state->wait_req,
  11462. true, NULL, NULL);
  11463. /* EIO should be eaten, and we can't get interrupted in the
  11464. * worker, and blocking commits have waited already. */
  11465. WARN_ON(ret);
  11466. }
  11467. drm_atomic_helper_wait_for_dependencies(state);
  11468. if (intel_state->modeset) {
  11469. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  11470. sizeof(intel_state->min_pixclk));
  11471. dev_priv->active_crtcs = intel_state->active_crtcs;
  11472. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  11473. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  11474. }
  11475. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11476. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11477. if (needs_modeset(crtc->state) ||
  11478. to_intel_crtc_state(crtc->state)->update_pipe) {
  11479. hw_check = true;
  11480. put_domains[to_intel_crtc(crtc)->pipe] =
  11481. modeset_get_crtc_power_domains(crtc,
  11482. to_intel_crtc_state(crtc->state));
  11483. }
  11484. if (!needs_modeset(crtc->state))
  11485. continue;
  11486. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11487. if (old_crtc_state->active) {
  11488. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  11489. dev_priv->display.crtc_disable(crtc);
  11490. intel_crtc->active = false;
  11491. intel_fbc_disable(intel_crtc);
  11492. intel_disable_shared_dpll(intel_crtc);
  11493. /*
  11494. * Underruns don't always raise
  11495. * interrupts, so check manually.
  11496. */
  11497. intel_check_cpu_fifo_underruns(dev_priv);
  11498. intel_check_pch_fifo_underruns(dev_priv);
  11499. if (!crtc->state->active)
  11500. intel_update_watermarks(crtc);
  11501. }
  11502. }
  11503. /* Only after disabling all output pipelines that will be changed can we
  11504. * update the the output configuration. */
  11505. intel_modeset_update_crtc_state(state);
  11506. if (intel_state->modeset) {
  11507. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11508. if (dev_priv->display.modeset_commit_cdclk &&
  11509. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11510. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  11511. dev_priv->display.modeset_commit_cdclk(state);
  11512. /*
  11513. * SKL workaround: bspec recommends we disable the SAGV when we
  11514. * have more then one pipe enabled
  11515. */
  11516. if (IS_SKYLAKE(dev_priv) && !skl_can_enable_sagv(state))
  11517. skl_disable_sagv(dev_priv);
  11518. intel_modeset_verify_disabled(dev);
  11519. }
  11520. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11521. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11522. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11523. bool modeset = needs_modeset(crtc->state);
  11524. struct intel_crtc_state *pipe_config =
  11525. to_intel_crtc_state(crtc->state);
  11526. if (modeset && crtc->state->active) {
  11527. update_scanline_offset(to_intel_crtc(crtc));
  11528. dev_priv->display.crtc_enable(crtc);
  11529. }
  11530. /* Complete events for now disable pipes here. */
  11531. if (modeset && !crtc->state->active && crtc->state->event) {
  11532. spin_lock_irq(&dev->event_lock);
  11533. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  11534. spin_unlock_irq(&dev->event_lock);
  11535. crtc->state->event = NULL;
  11536. }
  11537. if (!modeset)
  11538. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11539. if (crtc->state->active &&
  11540. drm_atomic_get_existing_plane_state(state, crtc->primary))
  11541. intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
  11542. if (crtc->state->active)
  11543. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11544. if (pipe_config->base.active && needs_vblank_wait(pipe_config))
  11545. crtc_vblank_mask |= 1 << i;
  11546. }
  11547. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  11548. * already, but still need the state for the delayed optimization. To
  11549. * fix this:
  11550. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  11551. * - schedule that vblank worker _before_ calling hw_done
  11552. * - at the start of commit_tail, cancel it _synchrously
  11553. * - switch over to the vblank wait helper in the core after that since
  11554. * we don't need out special handling any more.
  11555. */
  11556. if (!state->legacy_cursor_update)
  11557. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  11558. /*
  11559. * Now that the vblank has passed, we can go ahead and program the
  11560. * optimal watermarks on platforms that need two-step watermark
  11561. * programming.
  11562. *
  11563. * TODO: Move this (and other cleanup) to an async worker eventually.
  11564. */
  11565. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11566. intel_cstate = to_intel_crtc_state(crtc->state);
  11567. if (dev_priv->display.optimize_watermarks)
  11568. dev_priv->display.optimize_watermarks(intel_cstate);
  11569. }
  11570. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11571. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  11572. if (put_domains[i])
  11573. modeset_put_power_domains(dev_priv, put_domains[i]);
  11574. intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
  11575. }
  11576. if (IS_SKYLAKE(dev_priv) && intel_state->modeset &&
  11577. skl_can_enable_sagv(state))
  11578. skl_enable_sagv(dev_priv);
  11579. drm_atomic_helper_commit_hw_done(state);
  11580. if (intel_state->modeset)
  11581. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  11582. mutex_lock(&dev->struct_mutex);
  11583. drm_atomic_helper_cleanup_planes(dev, state);
  11584. mutex_unlock(&dev->struct_mutex);
  11585. drm_atomic_helper_commit_cleanup_done(state);
  11586. drm_atomic_state_free(state);
  11587. /* As one of the primary mmio accessors, KMS has a high likelihood
  11588. * of triggering bugs in unclaimed access. After we finish
  11589. * modesetting, see if an error has been flagged, and if so
  11590. * enable debugging for the next modeset - and hope we catch
  11591. * the culprit.
  11592. *
  11593. * XXX note that we assume display power is on at this point.
  11594. * This might hold true now but we need to add pm helper to check
  11595. * unclaimed only when the hardware is on, as atomic commits
  11596. * can happen also when the device is completely off.
  11597. */
  11598. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  11599. }
  11600. static void intel_atomic_commit_work(struct work_struct *work)
  11601. {
  11602. struct drm_atomic_state *state = container_of(work,
  11603. struct drm_atomic_state,
  11604. commit_work);
  11605. intel_atomic_commit_tail(state);
  11606. }
  11607. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  11608. {
  11609. struct drm_plane_state *old_plane_state;
  11610. struct drm_plane *plane;
  11611. struct drm_i915_gem_object *obj, *old_obj;
  11612. struct intel_plane *intel_plane;
  11613. int i;
  11614. mutex_lock(&state->dev->struct_mutex);
  11615. for_each_plane_in_state(state, plane, old_plane_state, i) {
  11616. obj = intel_fb_obj(plane->state->fb);
  11617. old_obj = intel_fb_obj(old_plane_state->fb);
  11618. intel_plane = to_intel_plane(plane);
  11619. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11620. }
  11621. mutex_unlock(&state->dev->struct_mutex);
  11622. }
  11623. /**
  11624. * intel_atomic_commit - commit validated state object
  11625. * @dev: DRM device
  11626. * @state: the top-level driver state object
  11627. * @nonblock: nonblocking commit
  11628. *
  11629. * This function commits a top-level state object that has been validated
  11630. * with drm_atomic_helper_check().
  11631. *
  11632. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11633. * nonblocking commits are only safe for pure plane updates. Everything else
  11634. * should work though.
  11635. *
  11636. * RETURNS
  11637. * Zero for success or -errno.
  11638. */
  11639. static int intel_atomic_commit(struct drm_device *dev,
  11640. struct drm_atomic_state *state,
  11641. bool nonblock)
  11642. {
  11643. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11644. struct drm_i915_private *dev_priv = to_i915(dev);
  11645. int ret = 0;
  11646. if (intel_state->modeset && nonblock) {
  11647. DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
  11648. return -EINVAL;
  11649. }
  11650. ret = drm_atomic_helper_setup_commit(state, nonblock);
  11651. if (ret)
  11652. return ret;
  11653. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  11654. ret = intel_atomic_prepare_commit(dev, state, nonblock);
  11655. if (ret) {
  11656. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11657. return ret;
  11658. }
  11659. drm_atomic_helper_swap_state(state, true);
  11660. dev_priv->wm.distrust_bios_wm = false;
  11661. dev_priv->wm.skl_results = intel_state->wm_results;
  11662. intel_shared_dpll_commit(state);
  11663. intel_atomic_track_fbs(state);
  11664. if (nonblock)
  11665. queue_work(system_unbound_wq, &state->commit_work);
  11666. else
  11667. intel_atomic_commit_tail(state);
  11668. return 0;
  11669. }
  11670. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11671. {
  11672. struct drm_device *dev = crtc->dev;
  11673. struct drm_atomic_state *state;
  11674. struct drm_crtc_state *crtc_state;
  11675. int ret;
  11676. state = drm_atomic_state_alloc(dev);
  11677. if (!state) {
  11678. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  11679. crtc->base.id, crtc->name);
  11680. return;
  11681. }
  11682. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11683. retry:
  11684. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11685. ret = PTR_ERR_OR_ZERO(crtc_state);
  11686. if (!ret) {
  11687. if (!crtc_state->active)
  11688. goto out;
  11689. crtc_state->mode_changed = true;
  11690. ret = drm_atomic_commit(state);
  11691. }
  11692. if (ret == -EDEADLK) {
  11693. drm_atomic_state_clear(state);
  11694. drm_modeset_backoff(state->acquire_ctx);
  11695. goto retry;
  11696. }
  11697. if (ret)
  11698. out:
  11699. drm_atomic_state_free(state);
  11700. }
  11701. #undef for_each_intel_crtc_masked
  11702. /*
  11703. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  11704. * drm_atomic_helper_legacy_gamma_set() directly.
  11705. */
  11706. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  11707. u16 *red, u16 *green, u16 *blue,
  11708. uint32_t size)
  11709. {
  11710. struct drm_device *dev = crtc->dev;
  11711. struct drm_mode_config *config = &dev->mode_config;
  11712. struct drm_crtc_state *state;
  11713. int ret;
  11714. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  11715. if (ret)
  11716. return ret;
  11717. /*
  11718. * Make sure we update the legacy properties so this works when
  11719. * atomic is not enabled.
  11720. */
  11721. state = crtc->state;
  11722. drm_object_property_set_value(&crtc->base,
  11723. config->degamma_lut_property,
  11724. (state->degamma_lut) ?
  11725. state->degamma_lut->base.id : 0);
  11726. drm_object_property_set_value(&crtc->base,
  11727. config->ctm_property,
  11728. (state->ctm) ?
  11729. state->ctm->base.id : 0);
  11730. drm_object_property_set_value(&crtc->base,
  11731. config->gamma_lut_property,
  11732. (state->gamma_lut) ?
  11733. state->gamma_lut->base.id : 0);
  11734. return 0;
  11735. }
  11736. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11737. .gamma_set = intel_atomic_legacy_gamma_set,
  11738. .set_config = drm_atomic_helper_set_config,
  11739. .set_property = drm_atomic_helper_crtc_set_property,
  11740. .destroy = intel_crtc_destroy,
  11741. .page_flip = intel_crtc_page_flip,
  11742. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11743. .atomic_destroy_state = intel_crtc_destroy_state,
  11744. };
  11745. /**
  11746. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11747. * @plane: drm plane to prepare for
  11748. * @fb: framebuffer to prepare for presentation
  11749. *
  11750. * Prepares a framebuffer for usage on a display plane. Generally this
  11751. * involves pinning the underlying object and updating the frontbuffer tracking
  11752. * bits. Some older platforms need special physical address handling for
  11753. * cursor planes.
  11754. *
  11755. * Must be called with struct_mutex held.
  11756. *
  11757. * Returns 0 on success, negative error code on failure.
  11758. */
  11759. int
  11760. intel_prepare_plane_fb(struct drm_plane *plane,
  11761. const struct drm_plane_state *new_state)
  11762. {
  11763. struct drm_device *dev = plane->dev;
  11764. struct drm_framebuffer *fb = new_state->fb;
  11765. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11766. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11767. struct reservation_object *resv;
  11768. int ret = 0;
  11769. if (!obj && !old_obj)
  11770. return 0;
  11771. if (old_obj) {
  11772. struct drm_crtc_state *crtc_state =
  11773. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11774. /* Big Hammer, we also need to ensure that any pending
  11775. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11776. * current scanout is retired before unpinning the old
  11777. * framebuffer. Note that we rely on userspace rendering
  11778. * into the buffer attached to the pipe they are waiting
  11779. * on. If not, userspace generates a GPU hang with IPEHR
  11780. * point to the MI_WAIT_FOR_EVENT.
  11781. *
  11782. * This should only fail upon a hung GPU, in which case we
  11783. * can safely continue.
  11784. */
  11785. if (needs_modeset(crtc_state))
  11786. ret = i915_gem_object_wait_rendering(old_obj, true);
  11787. if (ret) {
  11788. /* GPU hangs should have been swallowed by the wait */
  11789. WARN_ON(ret == -EIO);
  11790. return ret;
  11791. }
  11792. }
  11793. if (!obj)
  11794. return 0;
  11795. /* For framebuffer backed by dmabuf, wait for fence */
  11796. resv = i915_gem_object_get_dmabuf_resv(obj);
  11797. if (resv) {
  11798. long lret;
  11799. lret = reservation_object_wait_timeout_rcu(resv, false, true,
  11800. MAX_SCHEDULE_TIMEOUT);
  11801. if (lret == -ERESTARTSYS)
  11802. return lret;
  11803. WARN(lret < 0, "waiting returns %li\n", lret);
  11804. }
  11805. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11806. INTEL_INFO(dev)->cursor_needs_physical) {
  11807. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11808. ret = i915_gem_object_attach_phys(obj, align);
  11809. if (ret)
  11810. DRM_DEBUG_KMS("failed to attach phys object\n");
  11811. } else {
  11812. ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  11813. }
  11814. if (ret == 0) {
  11815. struct intel_plane_state *plane_state =
  11816. to_intel_plane_state(new_state);
  11817. i915_gem_request_assign(&plane_state->wait_req,
  11818. obj->last_write_req);
  11819. }
  11820. return ret;
  11821. }
  11822. /**
  11823. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11824. * @plane: drm plane to clean up for
  11825. * @fb: old framebuffer that was on plane
  11826. *
  11827. * Cleans up a framebuffer that has just been removed from a plane.
  11828. *
  11829. * Must be called with struct_mutex held.
  11830. */
  11831. void
  11832. intel_cleanup_plane_fb(struct drm_plane *plane,
  11833. const struct drm_plane_state *old_state)
  11834. {
  11835. struct drm_device *dev = plane->dev;
  11836. struct intel_plane_state *old_intel_state;
  11837. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11838. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11839. old_intel_state = to_intel_plane_state(old_state);
  11840. if (!obj && !old_obj)
  11841. return;
  11842. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11843. !INTEL_INFO(dev)->cursor_needs_physical))
  11844. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  11845. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11846. }
  11847. int
  11848. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11849. {
  11850. int max_scale;
  11851. int crtc_clock, cdclk;
  11852. if (!intel_crtc || !crtc_state->base.enable)
  11853. return DRM_PLANE_HELPER_NO_SCALING;
  11854. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11855. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11856. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11857. return DRM_PLANE_HELPER_NO_SCALING;
  11858. /*
  11859. * skl max scale is lower of:
  11860. * close to 3 but not 3, -1 is for that purpose
  11861. * or
  11862. * cdclk/crtc_clock
  11863. */
  11864. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11865. return max_scale;
  11866. }
  11867. static int
  11868. intel_check_primary_plane(struct drm_plane *plane,
  11869. struct intel_crtc_state *crtc_state,
  11870. struct intel_plane_state *state)
  11871. {
  11872. struct drm_crtc *crtc = state->base.crtc;
  11873. struct drm_framebuffer *fb = state->base.fb;
  11874. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11875. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11876. bool can_position = false;
  11877. if (INTEL_INFO(plane->dev)->gen >= 9) {
  11878. /* use scaler when colorkey is not required */
  11879. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11880. min_scale = 1;
  11881. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11882. }
  11883. can_position = true;
  11884. }
  11885. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11886. &state->dst, &state->clip,
  11887. state->base.rotation,
  11888. min_scale, max_scale,
  11889. can_position, true,
  11890. &state->visible);
  11891. }
  11892. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11893. struct drm_crtc_state *old_crtc_state)
  11894. {
  11895. struct drm_device *dev = crtc->dev;
  11896. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11897. struct intel_crtc_state *old_intel_state =
  11898. to_intel_crtc_state(old_crtc_state);
  11899. bool modeset = needs_modeset(crtc->state);
  11900. /* Perform vblank evasion around commit operation */
  11901. intel_pipe_update_start(intel_crtc);
  11902. if (modeset)
  11903. return;
  11904. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  11905. intel_color_set_csc(crtc->state);
  11906. intel_color_load_luts(crtc->state);
  11907. }
  11908. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11909. intel_update_pipe_config(intel_crtc, old_intel_state);
  11910. else if (INTEL_INFO(dev)->gen >= 9)
  11911. skl_detach_scalers(intel_crtc);
  11912. }
  11913. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11914. struct drm_crtc_state *old_crtc_state)
  11915. {
  11916. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11917. intel_pipe_update_end(intel_crtc, NULL);
  11918. }
  11919. /**
  11920. * intel_plane_destroy - destroy a plane
  11921. * @plane: plane to destroy
  11922. *
  11923. * Common destruction function for all types of planes (primary, cursor,
  11924. * sprite).
  11925. */
  11926. void intel_plane_destroy(struct drm_plane *plane)
  11927. {
  11928. if (!plane)
  11929. return;
  11930. drm_plane_cleanup(plane);
  11931. kfree(to_intel_plane(plane));
  11932. }
  11933. const struct drm_plane_funcs intel_plane_funcs = {
  11934. .update_plane = drm_atomic_helper_update_plane,
  11935. .disable_plane = drm_atomic_helper_disable_plane,
  11936. .destroy = intel_plane_destroy,
  11937. .set_property = drm_atomic_helper_plane_set_property,
  11938. .atomic_get_property = intel_plane_atomic_get_property,
  11939. .atomic_set_property = intel_plane_atomic_set_property,
  11940. .atomic_duplicate_state = intel_plane_duplicate_state,
  11941. .atomic_destroy_state = intel_plane_destroy_state,
  11942. };
  11943. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11944. int pipe)
  11945. {
  11946. struct intel_plane *primary = NULL;
  11947. struct intel_plane_state *state = NULL;
  11948. const uint32_t *intel_primary_formats;
  11949. unsigned int num_formats;
  11950. int ret;
  11951. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11952. if (!primary)
  11953. goto fail;
  11954. state = intel_create_plane_state(&primary->base);
  11955. if (!state)
  11956. goto fail;
  11957. primary->base.state = &state->base;
  11958. primary->can_scale = false;
  11959. primary->max_downscale = 1;
  11960. if (INTEL_INFO(dev)->gen >= 9) {
  11961. primary->can_scale = true;
  11962. state->scaler_id = -1;
  11963. }
  11964. primary->pipe = pipe;
  11965. primary->plane = pipe;
  11966. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11967. primary->check_plane = intel_check_primary_plane;
  11968. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11969. primary->plane = !pipe;
  11970. if (INTEL_INFO(dev)->gen >= 9) {
  11971. intel_primary_formats = skl_primary_formats;
  11972. num_formats = ARRAY_SIZE(skl_primary_formats);
  11973. primary->update_plane = skylake_update_primary_plane;
  11974. primary->disable_plane = skylake_disable_primary_plane;
  11975. } else if (HAS_PCH_SPLIT(dev)) {
  11976. intel_primary_formats = i965_primary_formats;
  11977. num_formats = ARRAY_SIZE(i965_primary_formats);
  11978. primary->update_plane = ironlake_update_primary_plane;
  11979. primary->disable_plane = i9xx_disable_primary_plane;
  11980. } else if (INTEL_INFO(dev)->gen >= 4) {
  11981. intel_primary_formats = i965_primary_formats;
  11982. num_formats = ARRAY_SIZE(i965_primary_formats);
  11983. primary->update_plane = i9xx_update_primary_plane;
  11984. primary->disable_plane = i9xx_disable_primary_plane;
  11985. } else {
  11986. intel_primary_formats = i8xx_primary_formats;
  11987. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11988. primary->update_plane = i9xx_update_primary_plane;
  11989. primary->disable_plane = i9xx_disable_primary_plane;
  11990. }
  11991. if (INTEL_INFO(dev)->gen >= 9)
  11992. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11993. &intel_plane_funcs,
  11994. intel_primary_formats, num_formats,
  11995. DRM_PLANE_TYPE_PRIMARY,
  11996. "plane 1%c", pipe_name(pipe));
  11997. else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  11998. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11999. &intel_plane_funcs,
  12000. intel_primary_formats, num_formats,
  12001. DRM_PLANE_TYPE_PRIMARY,
  12002. "primary %c", pipe_name(pipe));
  12003. else
  12004. ret = drm_universal_plane_init(dev, &primary->base, 0,
  12005. &intel_plane_funcs,
  12006. intel_primary_formats, num_formats,
  12007. DRM_PLANE_TYPE_PRIMARY,
  12008. "plane %c", plane_name(primary->plane));
  12009. if (ret)
  12010. goto fail;
  12011. if (INTEL_INFO(dev)->gen >= 4)
  12012. intel_create_rotation_property(dev, primary);
  12013. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  12014. return &primary->base;
  12015. fail:
  12016. kfree(state);
  12017. kfree(primary);
  12018. return NULL;
  12019. }
  12020. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  12021. {
  12022. if (!dev->mode_config.rotation_property) {
  12023. unsigned long flags = BIT(DRM_ROTATE_0) |
  12024. BIT(DRM_ROTATE_180);
  12025. if (INTEL_INFO(dev)->gen >= 9)
  12026. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  12027. dev->mode_config.rotation_property =
  12028. drm_mode_create_rotation_property(dev, flags);
  12029. }
  12030. if (dev->mode_config.rotation_property)
  12031. drm_object_attach_property(&plane->base.base,
  12032. dev->mode_config.rotation_property,
  12033. plane->base.state->rotation);
  12034. }
  12035. static int
  12036. intel_check_cursor_plane(struct drm_plane *plane,
  12037. struct intel_crtc_state *crtc_state,
  12038. struct intel_plane_state *state)
  12039. {
  12040. struct drm_crtc *crtc = crtc_state->base.crtc;
  12041. struct drm_framebuffer *fb = state->base.fb;
  12042. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12043. enum pipe pipe = to_intel_plane(plane)->pipe;
  12044. unsigned stride;
  12045. int ret;
  12046. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  12047. &state->dst, &state->clip,
  12048. state->base.rotation,
  12049. DRM_PLANE_HELPER_NO_SCALING,
  12050. DRM_PLANE_HELPER_NO_SCALING,
  12051. true, true, &state->visible);
  12052. if (ret)
  12053. return ret;
  12054. /* if we want to turn off the cursor ignore width and height */
  12055. if (!obj)
  12056. return 0;
  12057. /* Check for which cursor types we support */
  12058. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  12059. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  12060. state->base.crtc_w, state->base.crtc_h);
  12061. return -EINVAL;
  12062. }
  12063. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  12064. if (obj->base.size < stride * state->base.crtc_h) {
  12065. DRM_DEBUG_KMS("buffer is too small\n");
  12066. return -ENOMEM;
  12067. }
  12068. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  12069. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  12070. return -EINVAL;
  12071. }
  12072. /*
  12073. * There's something wrong with the cursor on CHV pipe C.
  12074. * If it straddles the left edge of the screen then
  12075. * moving it away from the edge or disabling it often
  12076. * results in a pipe underrun, and often that can lead to
  12077. * dead pipe (constant underrun reported, and it scans
  12078. * out just a solid color). To recover from that, the
  12079. * display power well must be turned off and on again.
  12080. * Refuse the put the cursor into that compromised position.
  12081. */
  12082. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  12083. state->visible && state->base.crtc_x < 0) {
  12084. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12085. return -EINVAL;
  12086. }
  12087. return 0;
  12088. }
  12089. static void
  12090. intel_disable_cursor_plane(struct drm_plane *plane,
  12091. struct drm_crtc *crtc)
  12092. {
  12093. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12094. intel_crtc->cursor_addr = 0;
  12095. intel_crtc_update_cursor(crtc, NULL);
  12096. }
  12097. static void
  12098. intel_update_cursor_plane(struct drm_plane *plane,
  12099. const struct intel_crtc_state *crtc_state,
  12100. const struct intel_plane_state *state)
  12101. {
  12102. struct drm_crtc *crtc = crtc_state->base.crtc;
  12103. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12104. struct drm_device *dev = plane->dev;
  12105. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12106. uint32_t addr;
  12107. if (!obj)
  12108. addr = 0;
  12109. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  12110. addr = i915_gem_obj_ggtt_offset(obj);
  12111. else
  12112. addr = obj->phys_handle->busaddr;
  12113. intel_crtc->cursor_addr = addr;
  12114. intel_crtc_update_cursor(crtc, state);
  12115. }
  12116. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  12117. int pipe)
  12118. {
  12119. struct intel_plane *cursor = NULL;
  12120. struct intel_plane_state *state = NULL;
  12121. int ret;
  12122. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12123. if (!cursor)
  12124. goto fail;
  12125. state = intel_create_plane_state(&cursor->base);
  12126. if (!state)
  12127. goto fail;
  12128. cursor->base.state = &state->base;
  12129. cursor->can_scale = false;
  12130. cursor->max_downscale = 1;
  12131. cursor->pipe = pipe;
  12132. cursor->plane = pipe;
  12133. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12134. cursor->check_plane = intel_check_cursor_plane;
  12135. cursor->update_plane = intel_update_cursor_plane;
  12136. cursor->disable_plane = intel_disable_cursor_plane;
  12137. ret = drm_universal_plane_init(dev, &cursor->base, 0,
  12138. &intel_plane_funcs,
  12139. intel_cursor_formats,
  12140. ARRAY_SIZE(intel_cursor_formats),
  12141. DRM_PLANE_TYPE_CURSOR,
  12142. "cursor %c", pipe_name(pipe));
  12143. if (ret)
  12144. goto fail;
  12145. if (INTEL_INFO(dev)->gen >= 4) {
  12146. if (!dev->mode_config.rotation_property)
  12147. dev->mode_config.rotation_property =
  12148. drm_mode_create_rotation_property(dev,
  12149. BIT(DRM_ROTATE_0) |
  12150. BIT(DRM_ROTATE_180));
  12151. if (dev->mode_config.rotation_property)
  12152. drm_object_attach_property(&cursor->base.base,
  12153. dev->mode_config.rotation_property,
  12154. state->base.rotation);
  12155. }
  12156. if (INTEL_INFO(dev)->gen >=9)
  12157. state->scaler_id = -1;
  12158. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12159. return &cursor->base;
  12160. fail:
  12161. kfree(state);
  12162. kfree(cursor);
  12163. return NULL;
  12164. }
  12165. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  12166. struct intel_crtc_state *crtc_state)
  12167. {
  12168. int i;
  12169. struct intel_scaler *intel_scaler;
  12170. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  12171. for (i = 0; i < intel_crtc->num_scalers; i++) {
  12172. intel_scaler = &scaler_state->scalers[i];
  12173. intel_scaler->in_use = 0;
  12174. intel_scaler->mode = PS_SCALER_MODE_DYN;
  12175. }
  12176. scaler_state->scaler_id = -1;
  12177. }
  12178. static void intel_crtc_init(struct drm_device *dev, int pipe)
  12179. {
  12180. struct drm_i915_private *dev_priv = to_i915(dev);
  12181. struct intel_crtc *intel_crtc;
  12182. struct intel_crtc_state *crtc_state = NULL;
  12183. struct drm_plane *primary = NULL;
  12184. struct drm_plane *cursor = NULL;
  12185. int ret;
  12186. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12187. if (intel_crtc == NULL)
  12188. return;
  12189. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12190. if (!crtc_state)
  12191. goto fail;
  12192. intel_crtc->config = crtc_state;
  12193. intel_crtc->base.state = &crtc_state->base;
  12194. crtc_state->base.crtc = &intel_crtc->base;
  12195. /* initialize shared scalers */
  12196. if (INTEL_INFO(dev)->gen >= 9) {
  12197. if (pipe == PIPE_C)
  12198. intel_crtc->num_scalers = 1;
  12199. else
  12200. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  12201. skl_init_scalers(dev, intel_crtc, crtc_state);
  12202. }
  12203. primary = intel_primary_plane_create(dev, pipe);
  12204. if (!primary)
  12205. goto fail;
  12206. cursor = intel_cursor_plane_create(dev, pipe);
  12207. if (!cursor)
  12208. goto fail;
  12209. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  12210. cursor, &intel_crtc_funcs,
  12211. "pipe %c", pipe_name(pipe));
  12212. if (ret)
  12213. goto fail;
  12214. /*
  12215. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  12216. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  12217. */
  12218. intel_crtc->pipe = pipe;
  12219. intel_crtc->plane = pipe;
  12220. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  12221. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  12222. intel_crtc->plane = !pipe;
  12223. }
  12224. intel_crtc->cursor_base = ~0;
  12225. intel_crtc->cursor_cntl = ~0;
  12226. intel_crtc->cursor_size = ~0;
  12227. intel_crtc->wm.cxsr_allowed = true;
  12228. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12229. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12230. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  12231. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  12232. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12233. intel_color_init(&intel_crtc->base);
  12234. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12235. return;
  12236. fail:
  12237. intel_plane_destroy(primary);
  12238. intel_plane_destroy(cursor);
  12239. kfree(crtc_state);
  12240. kfree(intel_crtc);
  12241. }
  12242. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12243. {
  12244. struct drm_encoder *encoder = connector->base.encoder;
  12245. struct drm_device *dev = connector->base.dev;
  12246. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12247. if (!encoder || WARN_ON(!encoder->crtc))
  12248. return INVALID_PIPE;
  12249. return to_intel_crtc(encoder->crtc)->pipe;
  12250. }
  12251. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12252. struct drm_file *file)
  12253. {
  12254. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12255. struct drm_crtc *drmmode_crtc;
  12256. struct intel_crtc *crtc;
  12257. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12258. if (!drmmode_crtc)
  12259. return -ENOENT;
  12260. crtc = to_intel_crtc(drmmode_crtc);
  12261. pipe_from_crtc_id->pipe = crtc->pipe;
  12262. return 0;
  12263. }
  12264. static int intel_encoder_clones(struct intel_encoder *encoder)
  12265. {
  12266. struct drm_device *dev = encoder->base.dev;
  12267. struct intel_encoder *source_encoder;
  12268. int index_mask = 0;
  12269. int entry = 0;
  12270. for_each_intel_encoder(dev, source_encoder) {
  12271. if (encoders_cloneable(encoder, source_encoder))
  12272. index_mask |= (1 << entry);
  12273. entry++;
  12274. }
  12275. return index_mask;
  12276. }
  12277. static bool has_edp_a(struct drm_device *dev)
  12278. {
  12279. struct drm_i915_private *dev_priv = to_i915(dev);
  12280. if (!IS_MOBILE(dev))
  12281. return false;
  12282. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12283. return false;
  12284. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12285. return false;
  12286. return true;
  12287. }
  12288. static bool intel_crt_present(struct drm_device *dev)
  12289. {
  12290. struct drm_i915_private *dev_priv = to_i915(dev);
  12291. if (INTEL_INFO(dev)->gen >= 9)
  12292. return false;
  12293. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  12294. return false;
  12295. if (IS_CHERRYVIEW(dev))
  12296. return false;
  12297. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12298. return false;
  12299. /* DDI E can't be used if DDI A requires 4 lanes */
  12300. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12301. return false;
  12302. if (!dev_priv->vbt.int_crt_support)
  12303. return false;
  12304. return true;
  12305. }
  12306. static void intel_setup_outputs(struct drm_device *dev)
  12307. {
  12308. struct drm_i915_private *dev_priv = to_i915(dev);
  12309. struct intel_encoder *encoder;
  12310. bool dpd_is_edp = false;
  12311. /*
  12312. * intel_edp_init_connector() depends on this completing first, to
  12313. * prevent the registeration of both eDP and LVDS and the incorrect
  12314. * sharing of the PPS.
  12315. */
  12316. intel_lvds_init(dev);
  12317. if (intel_crt_present(dev))
  12318. intel_crt_init(dev);
  12319. if (IS_BROXTON(dev)) {
  12320. /*
  12321. * FIXME: Broxton doesn't support port detection via the
  12322. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12323. * detect the ports.
  12324. */
  12325. intel_ddi_init(dev, PORT_A);
  12326. intel_ddi_init(dev, PORT_B);
  12327. intel_ddi_init(dev, PORT_C);
  12328. intel_dsi_init(dev);
  12329. } else if (HAS_DDI(dev)) {
  12330. int found;
  12331. /*
  12332. * Haswell uses DDI functions to detect digital outputs.
  12333. * On SKL pre-D0 the strap isn't connected, so we assume
  12334. * it's there.
  12335. */
  12336. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12337. /* WaIgnoreDDIAStrap: skl */
  12338. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12339. intel_ddi_init(dev, PORT_A);
  12340. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12341. * register */
  12342. found = I915_READ(SFUSE_STRAP);
  12343. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12344. intel_ddi_init(dev, PORT_B);
  12345. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12346. intel_ddi_init(dev, PORT_C);
  12347. if (found & SFUSE_STRAP_DDID_DETECTED)
  12348. intel_ddi_init(dev, PORT_D);
  12349. /*
  12350. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12351. */
  12352. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  12353. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12354. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12355. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12356. intel_ddi_init(dev, PORT_E);
  12357. } else if (HAS_PCH_SPLIT(dev)) {
  12358. int found;
  12359. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12360. if (has_edp_a(dev))
  12361. intel_dp_init(dev, DP_A, PORT_A);
  12362. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12363. /* PCH SDVOB multiplex with HDMIB */
  12364. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12365. if (!found)
  12366. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12367. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12368. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12369. }
  12370. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12371. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12372. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12373. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12374. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12375. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12376. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12377. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12378. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12379. bool has_edp, has_port;
  12380. /*
  12381. * The DP_DETECTED bit is the latched state of the DDC
  12382. * SDA pin at boot. However since eDP doesn't require DDC
  12383. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12384. * eDP ports may have been muxed to an alternate function.
  12385. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12386. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12387. * detect eDP ports.
  12388. *
  12389. * Sadly the straps seem to be missing sometimes even for HDMI
  12390. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  12391. * and VBT for the presence of the port. Additionally we can't
  12392. * trust the port type the VBT declares as we've seen at least
  12393. * HDMI ports that the VBT claim are DP or eDP.
  12394. */
  12395. has_edp = intel_dp_is_edp(dev, PORT_B);
  12396. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  12397. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  12398. has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
  12399. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  12400. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  12401. has_edp = intel_dp_is_edp(dev, PORT_C);
  12402. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  12403. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  12404. has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
  12405. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  12406. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  12407. if (IS_CHERRYVIEW(dev)) {
  12408. /*
  12409. * eDP not supported on port D,
  12410. * so no need to worry about it
  12411. */
  12412. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  12413. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  12414. intel_dp_init(dev, CHV_DP_D, PORT_D);
  12415. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  12416. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  12417. }
  12418. intel_dsi_init(dev);
  12419. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12420. bool found = false;
  12421. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12422. DRM_DEBUG_KMS("probing SDVOB\n");
  12423. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  12424. if (!found && IS_G4X(dev)) {
  12425. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12426. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12427. }
  12428. if (!found && IS_G4X(dev))
  12429. intel_dp_init(dev, DP_B, PORT_B);
  12430. }
  12431. /* Before G4X SDVOC doesn't have its own detect register */
  12432. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12433. DRM_DEBUG_KMS("probing SDVOC\n");
  12434. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  12435. }
  12436. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12437. if (IS_G4X(dev)) {
  12438. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12439. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12440. }
  12441. if (IS_G4X(dev))
  12442. intel_dp_init(dev, DP_C, PORT_C);
  12443. }
  12444. if (IS_G4X(dev) &&
  12445. (I915_READ(DP_D) & DP_DETECTED))
  12446. intel_dp_init(dev, DP_D, PORT_D);
  12447. } else if (IS_GEN2(dev))
  12448. intel_dvo_init(dev);
  12449. if (SUPPORTS_TV(dev))
  12450. intel_tv_init(dev);
  12451. intel_psr_init(dev);
  12452. for_each_intel_encoder(dev, encoder) {
  12453. encoder->base.possible_crtcs = encoder->crtc_mask;
  12454. encoder->base.possible_clones =
  12455. intel_encoder_clones(encoder);
  12456. }
  12457. intel_init_pch_refclk(dev);
  12458. drm_helper_move_panel_connectors_to_head(dev);
  12459. }
  12460. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12461. {
  12462. struct drm_device *dev = fb->dev;
  12463. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12464. drm_framebuffer_cleanup(fb);
  12465. mutex_lock(&dev->struct_mutex);
  12466. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12467. drm_gem_object_unreference(&intel_fb->obj->base);
  12468. mutex_unlock(&dev->struct_mutex);
  12469. kfree(intel_fb);
  12470. }
  12471. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12472. struct drm_file *file,
  12473. unsigned int *handle)
  12474. {
  12475. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12476. struct drm_i915_gem_object *obj = intel_fb->obj;
  12477. if (obj->userptr.mm) {
  12478. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  12479. return -EINVAL;
  12480. }
  12481. return drm_gem_handle_create(file, &obj->base, handle);
  12482. }
  12483. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12484. struct drm_file *file,
  12485. unsigned flags, unsigned color,
  12486. struct drm_clip_rect *clips,
  12487. unsigned num_clips)
  12488. {
  12489. struct drm_device *dev = fb->dev;
  12490. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12491. struct drm_i915_gem_object *obj = intel_fb->obj;
  12492. mutex_lock(&dev->struct_mutex);
  12493. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  12494. mutex_unlock(&dev->struct_mutex);
  12495. return 0;
  12496. }
  12497. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12498. .destroy = intel_user_framebuffer_destroy,
  12499. .create_handle = intel_user_framebuffer_create_handle,
  12500. .dirty = intel_user_framebuffer_dirty,
  12501. };
  12502. static
  12503. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12504. uint32_t pixel_format)
  12505. {
  12506. u32 gen = INTEL_INFO(dev)->gen;
  12507. if (gen >= 9) {
  12508. int cpp = drm_format_plane_cpp(pixel_format, 0);
  12509. /* "The stride in bytes must not exceed the of the size of 8K
  12510. * pixels and 32K bytes."
  12511. */
  12512. return min(8192 * cpp, 32768);
  12513. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12514. return 32*1024;
  12515. } else if (gen >= 4) {
  12516. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12517. return 16*1024;
  12518. else
  12519. return 32*1024;
  12520. } else if (gen >= 3) {
  12521. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12522. return 8*1024;
  12523. else
  12524. return 16*1024;
  12525. } else {
  12526. /* XXX DSPC is limited to 4k tiled */
  12527. return 8*1024;
  12528. }
  12529. }
  12530. static int intel_framebuffer_init(struct drm_device *dev,
  12531. struct intel_framebuffer *intel_fb,
  12532. struct drm_mode_fb_cmd2 *mode_cmd,
  12533. struct drm_i915_gem_object *obj)
  12534. {
  12535. struct drm_i915_private *dev_priv = to_i915(dev);
  12536. unsigned int aligned_height;
  12537. int ret;
  12538. u32 pitch_limit, stride_alignment;
  12539. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12540. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12541. /* Enforce that fb modifier and tiling mode match, but only for
  12542. * X-tiled. This is needed for FBC. */
  12543. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12544. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12545. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12546. return -EINVAL;
  12547. }
  12548. } else {
  12549. if (obj->tiling_mode == I915_TILING_X)
  12550. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12551. else if (obj->tiling_mode == I915_TILING_Y) {
  12552. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12553. return -EINVAL;
  12554. }
  12555. }
  12556. /* Passed in modifier sanity checking. */
  12557. switch (mode_cmd->modifier[0]) {
  12558. case I915_FORMAT_MOD_Y_TILED:
  12559. case I915_FORMAT_MOD_Yf_TILED:
  12560. if (INTEL_INFO(dev)->gen < 9) {
  12561. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12562. mode_cmd->modifier[0]);
  12563. return -EINVAL;
  12564. }
  12565. case DRM_FORMAT_MOD_NONE:
  12566. case I915_FORMAT_MOD_X_TILED:
  12567. break;
  12568. default:
  12569. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12570. mode_cmd->modifier[0]);
  12571. return -EINVAL;
  12572. }
  12573. stride_alignment = intel_fb_stride_alignment(dev_priv,
  12574. mode_cmd->modifier[0],
  12575. mode_cmd->pixel_format);
  12576. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12577. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12578. mode_cmd->pitches[0], stride_alignment);
  12579. return -EINVAL;
  12580. }
  12581. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12582. mode_cmd->pixel_format);
  12583. if (mode_cmd->pitches[0] > pitch_limit) {
  12584. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12585. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12586. "tiled" : "linear",
  12587. mode_cmd->pitches[0], pitch_limit);
  12588. return -EINVAL;
  12589. }
  12590. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12591. mode_cmd->pitches[0] != obj->stride) {
  12592. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12593. mode_cmd->pitches[0], obj->stride);
  12594. return -EINVAL;
  12595. }
  12596. /* Reject formats not supported by any plane early. */
  12597. switch (mode_cmd->pixel_format) {
  12598. case DRM_FORMAT_C8:
  12599. case DRM_FORMAT_RGB565:
  12600. case DRM_FORMAT_XRGB8888:
  12601. case DRM_FORMAT_ARGB8888:
  12602. break;
  12603. case DRM_FORMAT_XRGB1555:
  12604. if (INTEL_INFO(dev)->gen > 3) {
  12605. DRM_DEBUG("unsupported pixel format: %s\n",
  12606. drm_get_format_name(mode_cmd->pixel_format));
  12607. return -EINVAL;
  12608. }
  12609. break;
  12610. case DRM_FORMAT_ABGR8888:
  12611. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  12612. INTEL_INFO(dev)->gen < 9) {
  12613. DRM_DEBUG("unsupported pixel format: %s\n",
  12614. drm_get_format_name(mode_cmd->pixel_format));
  12615. return -EINVAL;
  12616. }
  12617. break;
  12618. case DRM_FORMAT_XBGR8888:
  12619. case DRM_FORMAT_XRGB2101010:
  12620. case DRM_FORMAT_XBGR2101010:
  12621. if (INTEL_INFO(dev)->gen < 4) {
  12622. DRM_DEBUG("unsupported pixel format: %s\n",
  12623. drm_get_format_name(mode_cmd->pixel_format));
  12624. return -EINVAL;
  12625. }
  12626. break;
  12627. case DRM_FORMAT_ABGR2101010:
  12628. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12629. DRM_DEBUG("unsupported pixel format: %s\n",
  12630. drm_get_format_name(mode_cmd->pixel_format));
  12631. return -EINVAL;
  12632. }
  12633. break;
  12634. case DRM_FORMAT_YUYV:
  12635. case DRM_FORMAT_UYVY:
  12636. case DRM_FORMAT_YVYU:
  12637. case DRM_FORMAT_VYUY:
  12638. if (INTEL_INFO(dev)->gen < 5) {
  12639. DRM_DEBUG("unsupported pixel format: %s\n",
  12640. drm_get_format_name(mode_cmd->pixel_format));
  12641. return -EINVAL;
  12642. }
  12643. break;
  12644. default:
  12645. DRM_DEBUG("unsupported pixel format: %s\n",
  12646. drm_get_format_name(mode_cmd->pixel_format));
  12647. return -EINVAL;
  12648. }
  12649. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12650. if (mode_cmd->offsets[0] != 0)
  12651. return -EINVAL;
  12652. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12653. mode_cmd->pixel_format,
  12654. mode_cmd->modifier[0]);
  12655. /* FIXME drm helper for size checks (especially planar formats)? */
  12656. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12657. return -EINVAL;
  12658. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12659. intel_fb->obj = obj;
  12660. intel_fill_fb_info(dev_priv, &intel_fb->base);
  12661. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12662. if (ret) {
  12663. DRM_ERROR("framebuffer init failed %d\n", ret);
  12664. return ret;
  12665. }
  12666. intel_fb->obj->framebuffer_references++;
  12667. return 0;
  12668. }
  12669. static struct drm_framebuffer *
  12670. intel_user_framebuffer_create(struct drm_device *dev,
  12671. struct drm_file *filp,
  12672. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12673. {
  12674. struct drm_framebuffer *fb;
  12675. struct drm_i915_gem_object *obj;
  12676. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12677. obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
  12678. if (&obj->base == NULL)
  12679. return ERR_PTR(-ENOENT);
  12680. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  12681. if (IS_ERR(fb))
  12682. drm_gem_object_unreference_unlocked(&obj->base);
  12683. return fb;
  12684. }
  12685. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12686. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12687. {
  12688. }
  12689. #endif
  12690. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12691. .fb_create = intel_user_framebuffer_create,
  12692. .output_poll_changed = intel_fbdev_output_poll_changed,
  12693. .atomic_check = intel_atomic_check,
  12694. .atomic_commit = intel_atomic_commit,
  12695. .atomic_state_alloc = intel_atomic_state_alloc,
  12696. .atomic_state_clear = intel_atomic_state_clear,
  12697. };
  12698. /**
  12699. * intel_init_display_hooks - initialize the display modesetting hooks
  12700. * @dev_priv: device private
  12701. */
  12702. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12703. {
  12704. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12705. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12706. dev_priv->display.get_initial_plane_config =
  12707. skylake_get_initial_plane_config;
  12708. dev_priv->display.crtc_compute_clock =
  12709. haswell_crtc_compute_clock;
  12710. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12711. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12712. } else if (HAS_DDI(dev_priv)) {
  12713. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12714. dev_priv->display.get_initial_plane_config =
  12715. ironlake_get_initial_plane_config;
  12716. dev_priv->display.crtc_compute_clock =
  12717. haswell_crtc_compute_clock;
  12718. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12719. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12720. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12721. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12722. dev_priv->display.get_initial_plane_config =
  12723. ironlake_get_initial_plane_config;
  12724. dev_priv->display.crtc_compute_clock =
  12725. ironlake_crtc_compute_clock;
  12726. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12727. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12728. } else if (IS_CHERRYVIEW(dev_priv)) {
  12729. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12730. dev_priv->display.get_initial_plane_config =
  12731. i9xx_get_initial_plane_config;
  12732. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12733. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12734. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12735. } else if (IS_VALLEYVIEW(dev_priv)) {
  12736. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12737. dev_priv->display.get_initial_plane_config =
  12738. i9xx_get_initial_plane_config;
  12739. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12740. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12741. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12742. } else if (IS_G4X(dev_priv)) {
  12743. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12744. dev_priv->display.get_initial_plane_config =
  12745. i9xx_get_initial_plane_config;
  12746. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12747. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12748. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12749. } else if (IS_PINEVIEW(dev_priv)) {
  12750. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12751. dev_priv->display.get_initial_plane_config =
  12752. i9xx_get_initial_plane_config;
  12753. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12754. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12755. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12756. } else if (!IS_GEN2(dev_priv)) {
  12757. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12758. dev_priv->display.get_initial_plane_config =
  12759. i9xx_get_initial_plane_config;
  12760. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12761. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12762. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12763. } else {
  12764. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12765. dev_priv->display.get_initial_plane_config =
  12766. i9xx_get_initial_plane_config;
  12767. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12768. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12769. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12770. }
  12771. /* Returns the core display clock speed */
  12772. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12773. dev_priv->display.get_display_clock_speed =
  12774. skylake_get_display_clock_speed;
  12775. else if (IS_BROXTON(dev_priv))
  12776. dev_priv->display.get_display_clock_speed =
  12777. broxton_get_display_clock_speed;
  12778. else if (IS_BROADWELL(dev_priv))
  12779. dev_priv->display.get_display_clock_speed =
  12780. broadwell_get_display_clock_speed;
  12781. else if (IS_HASWELL(dev_priv))
  12782. dev_priv->display.get_display_clock_speed =
  12783. haswell_get_display_clock_speed;
  12784. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12785. dev_priv->display.get_display_clock_speed =
  12786. valleyview_get_display_clock_speed;
  12787. else if (IS_GEN5(dev_priv))
  12788. dev_priv->display.get_display_clock_speed =
  12789. ilk_get_display_clock_speed;
  12790. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  12791. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  12792. dev_priv->display.get_display_clock_speed =
  12793. i945_get_display_clock_speed;
  12794. else if (IS_GM45(dev_priv))
  12795. dev_priv->display.get_display_clock_speed =
  12796. gm45_get_display_clock_speed;
  12797. else if (IS_CRESTLINE(dev_priv))
  12798. dev_priv->display.get_display_clock_speed =
  12799. i965gm_get_display_clock_speed;
  12800. else if (IS_PINEVIEW(dev_priv))
  12801. dev_priv->display.get_display_clock_speed =
  12802. pnv_get_display_clock_speed;
  12803. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  12804. dev_priv->display.get_display_clock_speed =
  12805. g33_get_display_clock_speed;
  12806. else if (IS_I915G(dev_priv))
  12807. dev_priv->display.get_display_clock_speed =
  12808. i915_get_display_clock_speed;
  12809. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  12810. dev_priv->display.get_display_clock_speed =
  12811. i9xx_misc_get_display_clock_speed;
  12812. else if (IS_I915GM(dev_priv))
  12813. dev_priv->display.get_display_clock_speed =
  12814. i915gm_get_display_clock_speed;
  12815. else if (IS_I865G(dev_priv))
  12816. dev_priv->display.get_display_clock_speed =
  12817. i865_get_display_clock_speed;
  12818. else if (IS_I85X(dev_priv))
  12819. dev_priv->display.get_display_clock_speed =
  12820. i85x_get_display_clock_speed;
  12821. else { /* 830 */
  12822. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12823. dev_priv->display.get_display_clock_speed =
  12824. i830_get_display_clock_speed;
  12825. }
  12826. if (IS_GEN5(dev_priv)) {
  12827. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12828. } else if (IS_GEN6(dev_priv)) {
  12829. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12830. } else if (IS_IVYBRIDGE(dev_priv)) {
  12831. /* FIXME: detect B0+ stepping and use auto training */
  12832. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12833. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12834. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12835. }
  12836. if (IS_BROADWELL(dev_priv)) {
  12837. dev_priv->display.modeset_commit_cdclk =
  12838. broadwell_modeset_commit_cdclk;
  12839. dev_priv->display.modeset_calc_cdclk =
  12840. broadwell_modeset_calc_cdclk;
  12841. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12842. dev_priv->display.modeset_commit_cdclk =
  12843. valleyview_modeset_commit_cdclk;
  12844. dev_priv->display.modeset_calc_cdclk =
  12845. valleyview_modeset_calc_cdclk;
  12846. } else if (IS_BROXTON(dev_priv)) {
  12847. dev_priv->display.modeset_commit_cdclk =
  12848. bxt_modeset_commit_cdclk;
  12849. dev_priv->display.modeset_calc_cdclk =
  12850. bxt_modeset_calc_cdclk;
  12851. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  12852. dev_priv->display.modeset_commit_cdclk =
  12853. skl_modeset_commit_cdclk;
  12854. dev_priv->display.modeset_calc_cdclk =
  12855. skl_modeset_calc_cdclk;
  12856. }
  12857. switch (INTEL_INFO(dev_priv)->gen) {
  12858. case 2:
  12859. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12860. break;
  12861. case 3:
  12862. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12863. break;
  12864. case 4:
  12865. case 5:
  12866. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12867. break;
  12868. case 6:
  12869. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12870. break;
  12871. case 7:
  12872. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12873. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12874. break;
  12875. case 9:
  12876. /* Drop through - unsupported since execlist only. */
  12877. default:
  12878. /* Default just returns -ENODEV to indicate unsupported */
  12879. dev_priv->display.queue_flip = intel_default_queue_flip;
  12880. }
  12881. }
  12882. /*
  12883. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12884. * resume, or other times. This quirk makes sure that's the case for
  12885. * affected systems.
  12886. */
  12887. static void quirk_pipea_force(struct drm_device *dev)
  12888. {
  12889. struct drm_i915_private *dev_priv = to_i915(dev);
  12890. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12891. DRM_INFO("applying pipe a force quirk\n");
  12892. }
  12893. static void quirk_pipeb_force(struct drm_device *dev)
  12894. {
  12895. struct drm_i915_private *dev_priv = to_i915(dev);
  12896. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12897. DRM_INFO("applying pipe b force quirk\n");
  12898. }
  12899. /*
  12900. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12901. */
  12902. static void quirk_ssc_force_disable(struct drm_device *dev)
  12903. {
  12904. struct drm_i915_private *dev_priv = to_i915(dev);
  12905. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12906. DRM_INFO("applying lvds SSC disable quirk\n");
  12907. }
  12908. /*
  12909. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12910. * brightness value
  12911. */
  12912. static void quirk_invert_brightness(struct drm_device *dev)
  12913. {
  12914. struct drm_i915_private *dev_priv = to_i915(dev);
  12915. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12916. DRM_INFO("applying inverted panel brightness quirk\n");
  12917. }
  12918. /* Some VBT's incorrectly indicate no backlight is present */
  12919. static void quirk_backlight_present(struct drm_device *dev)
  12920. {
  12921. struct drm_i915_private *dev_priv = to_i915(dev);
  12922. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12923. DRM_INFO("applying backlight present quirk\n");
  12924. }
  12925. struct intel_quirk {
  12926. int device;
  12927. int subsystem_vendor;
  12928. int subsystem_device;
  12929. void (*hook)(struct drm_device *dev);
  12930. };
  12931. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12932. struct intel_dmi_quirk {
  12933. void (*hook)(struct drm_device *dev);
  12934. const struct dmi_system_id (*dmi_id_list)[];
  12935. };
  12936. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12937. {
  12938. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12939. return 1;
  12940. }
  12941. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12942. {
  12943. .dmi_id_list = &(const struct dmi_system_id[]) {
  12944. {
  12945. .callback = intel_dmi_reverse_brightness,
  12946. .ident = "NCR Corporation",
  12947. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12948. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12949. },
  12950. },
  12951. { } /* terminating entry */
  12952. },
  12953. .hook = quirk_invert_brightness,
  12954. },
  12955. };
  12956. static struct intel_quirk intel_quirks[] = {
  12957. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12958. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12959. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12960. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12961. /* 830 needs to leave pipe A & dpll A up */
  12962. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12963. /* 830 needs to leave pipe B & dpll B up */
  12964. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12965. /* Lenovo U160 cannot use SSC on LVDS */
  12966. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12967. /* Sony Vaio Y cannot use SSC on LVDS */
  12968. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12969. /* Acer Aspire 5734Z must invert backlight brightness */
  12970. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12971. /* Acer/eMachines G725 */
  12972. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12973. /* Acer/eMachines e725 */
  12974. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12975. /* Acer/Packard Bell NCL20 */
  12976. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12977. /* Acer Aspire 4736Z */
  12978. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12979. /* Acer Aspire 5336 */
  12980. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12981. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12982. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12983. /* Acer C720 Chromebook (Core i3 4005U) */
  12984. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12985. /* Apple Macbook 2,1 (Core 2 T7400) */
  12986. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12987. /* Apple Macbook 4,1 */
  12988. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12989. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12990. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12991. /* HP Chromebook 14 (Celeron 2955U) */
  12992. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12993. /* Dell Chromebook 11 */
  12994. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12995. /* Dell Chromebook 11 (2015 version) */
  12996. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12997. };
  12998. static void intel_init_quirks(struct drm_device *dev)
  12999. {
  13000. struct pci_dev *d = dev->pdev;
  13001. int i;
  13002. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  13003. struct intel_quirk *q = &intel_quirks[i];
  13004. if (d->device == q->device &&
  13005. (d->subsystem_vendor == q->subsystem_vendor ||
  13006. q->subsystem_vendor == PCI_ANY_ID) &&
  13007. (d->subsystem_device == q->subsystem_device ||
  13008. q->subsystem_device == PCI_ANY_ID))
  13009. q->hook(dev);
  13010. }
  13011. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  13012. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  13013. intel_dmi_quirks[i].hook(dev);
  13014. }
  13015. }
  13016. /* Disable the VGA plane that we never use */
  13017. static void i915_disable_vga(struct drm_device *dev)
  13018. {
  13019. struct drm_i915_private *dev_priv = to_i915(dev);
  13020. u8 sr1;
  13021. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13022. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  13023. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  13024. outb(SR01, VGA_SR_INDEX);
  13025. sr1 = inb(VGA_SR_DATA);
  13026. outb(sr1 | 1<<5, VGA_SR_DATA);
  13027. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  13028. udelay(300);
  13029. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  13030. POSTING_READ(vga_reg);
  13031. }
  13032. void intel_modeset_init_hw(struct drm_device *dev)
  13033. {
  13034. struct drm_i915_private *dev_priv = to_i915(dev);
  13035. intel_update_cdclk(dev);
  13036. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13037. intel_init_clock_gating(dev);
  13038. intel_enable_gt_powersave(dev_priv);
  13039. }
  13040. /*
  13041. * Calculate what we think the watermarks should be for the state we've read
  13042. * out of the hardware and then immediately program those watermarks so that
  13043. * we ensure the hardware settings match our internal state.
  13044. *
  13045. * We can calculate what we think WM's should be by creating a duplicate of the
  13046. * current state (which was constructed during hardware readout) and running it
  13047. * through the atomic check code to calculate new watermark values in the
  13048. * state object.
  13049. */
  13050. static void sanitize_watermarks(struct drm_device *dev)
  13051. {
  13052. struct drm_i915_private *dev_priv = to_i915(dev);
  13053. struct drm_atomic_state *state;
  13054. struct drm_crtc *crtc;
  13055. struct drm_crtc_state *cstate;
  13056. struct drm_modeset_acquire_ctx ctx;
  13057. int ret;
  13058. int i;
  13059. /* Only supported on platforms that use atomic watermark design */
  13060. if (!dev_priv->display.optimize_watermarks)
  13061. return;
  13062. /*
  13063. * We need to hold connection_mutex before calling duplicate_state so
  13064. * that the connector loop is protected.
  13065. */
  13066. drm_modeset_acquire_init(&ctx, 0);
  13067. retry:
  13068. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13069. if (ret == -EDEADLK) {
  13070. drm_modeset_backoff(&ctx);
  13071. goto retry;
  13072. } else if (WARN_ON(ret)) {
  13073. goto fail;
  13074. }
  13075. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13076. if (WARN_ON(IS_ERR(state)))
  13077. goto fail;
  13078. /*
  13079. * Hardware readout is the only time we don't want to calculate
  13080. * intermediate watermarks (since we don't trust the current
  13081. * watermarks).
  13082. */
  13083. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13084. ret = intel_atomic_check(dev, state);
  13085. if (ret) {
  13086. /*
  13087. * If we fail here, it means that the hardware appears to be
  13088. * programmed in a way that shouldn't be possible, given our
  13089. * understanding of watermark requirements. This might mean a
  13090. * mistake in the hardware readout code or a mistake in the
  13091. * watermark calculations for a given platform. Raise a WARN
  13092. * so that this is noticeable.
  13093. *
  13094. * If this actually happens, we'll have to just leave the
  13095. * BIOS-programmed watermarks untouched and hope for the best.
  13096. */
  13097. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13098. goto fail;
  13099. }
  13100. /* Write calculated watermark values back */
  13101. for_each_crtc_in_state(state, crtc, cstate, i) {
  13102. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13103. cs->wm.need_postvbl_update = true;
  13104. dev_priv->display.optimize_watermarks(cs);
  13105. }
  13106. drm_atomic_state_free(state);
  13107. fail:
  13108. drm_modeset_drop_locks(&ctx);
  13109. drm_modeset_acquire_fini(&ctx);
  13110. }
  13111. void intel_modeset_init(struct drm_device *dev)
  13112. {
  13113. struct drm_i915_private *dev_priv = to_i915(dev);
  13114. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13115. int sprite, ret;
  13116. enum pipe pipe;
  13117. struct intel_crtc *crtc;
  13118. drm_mode_config_init(dev);
  13119. dev->mode_config.min_width = 0;
  13120. dev->mode_config.min_height = 0;
  13121. dev->mode_config.preferred_depth = 24;
  13122. dev->mode_config.prefer_shadow = 1;
  13123. dev->mode_config.allow_fb_modifiers = true;
  13124. dev->mode_config.funcs = &intel_mode_funcs;
  13125. intel_init_quirks(dev);
  13126. intel_init_pm(dev);
  13127. if (INTEL_INFO(dev)->num_pipes == 0)
  13128. return;
  13129. /*
  13130. * There may be no VBT; and if the BIOS enabled SSC we can
  13131. * just keep using it to avoid unnecessary flicker. Whereas if the
  13132. * BIOS isn't using it, don't assume it will work even if the VBT
  13133. * indicates as much.
  13134. */
  13135. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  13136. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13137. DREF_SSC1_ENABLE);
  13138. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13139. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13140. bios_lvds_use_ssc ? "en" : "dis",
  13141. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13142. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13143. }
  13144. }
  13145. if (IS_GEN2(dev)) {
  13146. dev->mode_config.max_width = 2048;
  13147. dev->mode_config.max_height = 2048;
  13148. } else if (IS_GEN3(dev)) {
  13149. dev->mode_config.max_width = 4096;
  13150. dev->mode_config.max_height = 4096;
  13151. } else {
  13152. dev->mode_config.max_width = 8192;
  13153. dev->mode_config.max_height = 8192;
  13154. }
  13155. if (IS_845G(dev) || IS_I865G(dev)) {
  13156. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  13157. dev->mode_config.cursor_height = 1023;
  13158. } else if (IS_GEN2(dev)) {
  13159. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13160. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13161. } else {
  13162. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13163. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13164. }
  13165. dev->mode_config.fb_base = ggtt->mappable_base;
  13166. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13167. INTEL_INFO(dev)->num_pipes,
  13168. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  13169. for_each_pipe(dev_priv, pipe) {
  13170. intel_crtc_init(dev, pipe);
  13171. for_each_sprite(dev_priv, pipe, sprite) {
  13172. ret = intel_plane_init(dev, pipe, sprite);
  13173. if (ret)
  13174. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  13175. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  13176. }
  13177. }
  13178. intel_update_czclk(dev_priv);
  13179. intel_update_cdclk(dev);
  13180. intel_shared_dpll_init(dev);
  13181. if (dev_priv->max_cdclk_freq == 0)
  13182. intel_update_max_cdclk(dev);
  13183. /* Just disable it once at startup */
  13184. i915_disable_vga(dev);
  13185. intel_setup_outputs(dev);
  13186. drm_modeset_lock_all(dev);
  13187. intel_modeset_setup_hw_state(dev);
  13188. drm_modeset_unlock_all(dev);
  13189. for_each_intel_crtc(dev, crtc) {
  13190. struct intel_initial_plane_config plane_config = {};
  13191. if (!crtc->active)
  13192. continue;
  13193. /*
  13194. * Note that reserving the BIOS fb up front prevents us
  13195. * from stuffing other stolen allocations like the ring
  13196. * on top. This prevents some ugliness at boot time, and
  13197. * can even allow for smooth boot transitions if the BIOS
  13198. * fb is large enough for the active pipe configuration.
  13199. */
  13200. dev_priv->display.get_initial_plane_config(crtc,
  13201. &plane_config);
  13202. /*
  13203. * If the fb is shared between multiple heads, we'll
  13204. * just get the first one.
  13205. */
  13206. intel_find_initial_plane_obj(crtc, &plane_config);
  13207. }
  13208. /*
  13209. * Make sure hardware watermarks really match the state we read out.
  13210. * Note that we need to do this after reconstructing the BIOS fb's
  13211. * since the watermark calculation done here will use pstate->fb.
  13212. */
  13213. sanitize_watermarks(dev);
  13214. }
  13215. static void intel_enable_pipe_a(struct drm_device *dev)
  13216. {
  13217. struct intel_connector *connector;
  13218. struct drm_connector *crt = NULL;
  13219. struct intel_load_detect_pipe load_detect_temp;
  13220. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13221. /* We can't just switch on the pipe A, we need to set things up with a
  13222. * proper mode and output configuration. As a gross hack, enable pipe A
  13223. * by enabling the load detect pipe once. */
  13224. for_each_intel_connector(dev, connector) {
  13225. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  13226. crt = &connector->base;
  13227. break;
  13228. }
  13229. }
  13230. if (!crt)
  13231. return;
  13232. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  13233. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  13234. }
  13235. static bool
  13236. intel_check_plane_mapping(struct intel_crtc *crtc)
  13237. {
  13238. struct drm_device *dev = crtc->base.dev;
  13239. struct drm_i915_private *dev_priv = to_i915(dev);
  13240. u32 val;
  13241. if (INTEL_INFO(dev)->num_pipes == 1)
  13242. return true;
  13243. val = I915_READ(DSPCNTR(!crtc->plane));
  13244. if ((val & DISPLAY_PLANE_ENABLE) &&
  13245. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13246. return false;
  13247. return true;
  13248. }
  13249. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13250. {
  13251. struct drm_device *dev = crtc->base.dev;
  13252. struct intel_encoder *encoder;
  13253. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13254. return true;
  13255. return false;
  13256. }
  13257. static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
  13258. {
  13259. struct drm_device *dev = encoder->base.dev;
  13260. struct intel_connector *connector;
  13261. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13262. return true;
  13263. return false;
  13264. }
  13265. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13266. {
  13267. struct drm_device *dev = crtc->base.dev;
  13268. struct drm_i915_private *dev_priv = to_i915(dev);
  13269. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13270. /* Clear any frame start delays used for debugging left by the BIOS */
  13271. if (!transcoder_is_dsi(cpu_transcoder)) {
  13272. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13273. I915_WRITE(reg,
  13274. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13275. }
  13276. /* restore vblank interrupts to correct state */
  13277. drm_crtc_vblank_reset(&crtc->base);
  13278. if (crtc->active) {
  13279. struct intel_plane *plane;
  13280. drm_crtc_vblank_on(&crtc->base);
  13281. /* Disable everything but the primary plane */
  13282. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13283. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13284. continue;
  13285. plane->disable_plane(&plane->base, &crtc->base);
  13286. }
  13287. }
  13288. /* We need to sanitize the plane -> pipe mapping first because this will
  13289. * disable the crtc (and hence change the state) if it is wrong. Note
  13290. * that gen4+ has a fixed plane -> pipe mapping. */
  13291. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  13292. bool plane;
  13293. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  13294. crtc->base.base.id, crtc->base.name);
  13295. /* Pipe has the wrong plane attached and the plane is active.
  13296. * Temporarily change the plane mapping and disable everything
  13297. * ... */
  13298. plane = crtc->plane;
  13299. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  13300. crtc->plane = !plane;
  13301. intel_crtc_disable_noatomic(&crtc->base);
  13302. crtc->plane = plane;
  13303. }
  13304. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13305. crtc->pipe == PIPE_A && !crtc->active) {
  13306. /* BIOS forgot to enable pipe A, this mostly happens after
  13307. * resume. Force-enable the pipe to fix this, the update_dpms
  13308. * call below we restore the pipe to the right state, but leave
  13309. * the required bits on. */
  13310. intel_enable_pipe_a(dev);
  13311. }
  13312. /* Adjust the state of the output pipe according to whether we
  13313. * have active connectors/encoders. */
  13314. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13315. intel_crtc_disable_noatomic(&crtc->base);
  13316. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  13317. /*
  13318. * We start out with underrun reporting disabled to avoid races.
  13319. * For correct bookkeeping mark this on active crtcs.
  13320. *
  13321. * Also on gmch platforms we dont have any hardware bits to
  13322. * disable the underrun reporting. Which means we need to start
  13323. * out with underrun reporting disabled also on inactive pipes,
  13324. * since otherwise we'll complain about the garbage we read when
  13325. * e.g. coming up after runtime pm.
  13326. *
  13327. * No protection against concurrent access is required - at
  13328. * worst a fifo underrun happens which also sets this to false.
  13329. */
  13330. crtc->cpu_fifo_underrun_disabled = true;
  13331. crtc->pch_fifo_underrun_disabled = true;
  13332. }
  13333. }
  13334. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13335. {
  13336. struct intel_connector *connector;
  13337. struct drm_device *dev = encoder->base.dev;
  13338. /* We need to check both for a crtc link (meaning that the
  13339. * encoder is active and trying to read from a pipe) and the
  13340. * pipe itself being active. */
  13341. bool has_active_crtc = encoder->base.crtc &&
  13342. to_intel_crtc(encoder->base.crtc)->active;
  13343. if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
  13344. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13345. encoder->base.base.id,
  13346. encoder->base.name);
  13347. /* Connector is active, but has no active pipe. This is
  13348. * fallout from our resume register restoring. Disable
  13349. * the encoder manually again. */
  13350. if (encoder->base.crtc) {
  13351. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13352. encoder->base.base.id,
  13353. encoder->base.name);
  13354. encoder->disable(encoder);
  13355. if (encoder->post_disable)
  13356. encoder->post_disable(encoder);
  13357. }
  13358. encoder->base.crtc = NULL;
  13359. /* Inconsistent output/port/pipe state happens presumably due to
  13360. * a bug in one of the get_hw_state functions. Or someplace else
  13361. * in our code, like the register restore mess on resume. Clamp
  13362. * things to off as a safer default. */
  13363. for_each_intel_connector(dev, connector) {
  13364. if (connector->encoder != encoder)
  13365. continue;
  13366. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13367. connector->base.encoder = NULL;
  13368. }
  13369. }
  13370. /* Enabled encoders without active connectors will be fixed in
  13371. * the crtc fixup. */
  13372. }
  13373. void i915_redisable_vga_power_on(struct drm_device *dev)
  13374. {
  13375. struct drm_i915_private *dev_priv = to_i915(dev);
  13376. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13377. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13378. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13379. i915_disable_vga(dev);
  13380. }
  13381. }
  13382. void i915_redisable_vga(struct drm_device *dev)
  13383. {
  13384. struct drm_i915_private *dev_priv = to_i915(dev);
  13385. /* This function can be called both from intel_modeset_setup_hw_state or
  13386. * at a very early point in our resume sequence, where the power well
  13387. * structures are not yet restored. Since this function is at a very
  13388. * paranoid "someone might have enabled VGA while we were not looking"
  13389. * level, just check if the power well is enabled instead of trying to
  13390. * follow the "don't touch the power well if we don't need it" policy
  13391. * the rest of the driver uses. */
  13392. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  13393. return;
  13394. i915_redisable_vga_power_on(dev);
  13395. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  13396. }
  13397. static bool primary_get_hw_state(struct intel_plane *plane)
  13398. {
  13399. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  13400. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  13401. }
  13402. /* FIXME read out full plane state for all planes */
  13403. static void readout_plane_state(struct intel_crtc *crtc)
  13404. {
  13405. struct drm_plane *primary = crtc->base.primary;
  13406. struct intel_plane_state *plane_state =
  13407. to_intel_plane_state(primary->state);
  13408. plane_state->visible = crtc->active &&
  13409. primary_get_hw_state(to_intel_plane(primary));
  13410. if (plane_state->visible)
  13411. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  13412. }
  13413. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13414. {
  13415. struct drm_i915_private *dev_priv = to_i915(dev);
  13416. enum pipe pipe;
  13417. struct intel_crtc *crtc;
  13418. struct intel_encoder *encoder;
  13419. struct intel_connector *connector;
  13420. int i;
  13421. dev_priv->active_crtcs = 0;
  13422. for_each_intel_crtc(dev, crtc) {
  13423. struct intel_crtc_state *crtc_state = crtc->config;
  13424. int pixclk = 0;
  13425. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  13426. memset(crtc_state, 0, sizeof(*crtc_state));
  13427. crtc_state->base.crtc = &crtc->base;
  13428. crtc_state->base.active = crtc_state->base.enable =
  13429. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13430. crtc->base.enabled = crtc_state->base.enable;
  13431. crtc->active = crtc_state->base.active;
  13432. if (crtc_state->base.active) {
  13433. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13434. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  13435. pixclk = ilk_pipe_pixel_rate(crtc_state);
  13436. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13437. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  13438. else
  13439. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  13440. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  13441. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  13442. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  13443. }
  13444. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  13445. readout_plane_state(crtc);
  13446. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  13447. crtc->base.base.id, crtc->base.name,
  13448. crtc->active ? "enabled" : "disabled");
  13449. }
  13450. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13451. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13452. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  13453. &pll->config.hw_state);
  13454. pll->config.crtc_mask = 0;
  13455. for_each_intel_crtc(dev, crtc) {
  13456. if (crtc->active && crtc->config->shared_dpll == pll)
  13457. pll->config.crtc_mask |= 1 << crtc->pipe;
  13458. }
  13459. pll->active_mask = pll->config.crtc_mask;
  13460. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13461. pll->name, pll->config.crtc_mask, pll->on);
  13462. }
  13463. for_each_intel_encoder(dev, encoder) {
  13464. pipe = 0;
  13465. if (encoder->get_hw_state(encoder, &pipe)) {
  13466. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13467. encoder->base.crtc = &crtc->base;
  13468. crtc->config->output_types |= 1 << encoder->type;
  13469. encoder->get_config(encoder, crtc->config);
  13470. } else {
  13471. encoder->base.crtc = NULL;
  13472. }
  13473. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13474. encoder->base.base.id,
  13475. encoder->base.name,
  13476. encoder->base.crtc ? "enabled" : "disabled",
  13477. pipe_name(pipe));
  13478. }
  13479. for_each_intel_connector(dev, connector) {
  13480. if (connector->get_hw_state(connector)) {
  13481. connector->base.dpms = DRM_MODE_DPMS_ON;
  13482. encoder = connector->encoder;
  13483. connector->base.encoder = &encoder->base;
  13484. if (encoder->base.crtc &&
  13485. encoder->base.crtc->state->active) {
  13486. /*
  13487. * This has to be done during hardware readout
  13488. * because anything calling .crtc_disable may
  13489. * rely on the connector_mask being accurate.
  13490. */
  13491. encoder->base.crtc->state->connector_mask |=
  13492. 1 << drm_connector_index(&connector->base);
  13493. encoder->base.crtc->state->encoder_mask |=
  13494. 1 << drm_encoder_index(&encoder->base);
  13495. }
  13496. } else {
  13497. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13498. connector->base.encoder = NULL;
  13499. }
  13500. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13501. connector->base.base.id,
  13502. connector->base.name,
  13503. connector->base.encoder ? "enabled" : "disabled");
  13504. }
  13505. for_each_intel_crtc(dev, crtc) {
  13506. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  13507. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13508. if (crtc->base.state->active) {
  13509. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  13510. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  13511. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13512. /*
  13513. * The initial mode needs to be set in order to keep
  13514. * the atomic core happy. It wants a valid mode if the
  13515. * crtc's enabled, so we do the above call.
  13516. *
  13517. * At this point some state updated by the connectors
  13518. * in their ->detect() callback has not run yet, so
  13519. * no recalculation can be done yet.
  13520. *
  13521. * Even if we could do a recalculation and modeset
  13522. * right now it would cause a double modeset if
  13523. * fbdev or userspace chooses a different initial mode.
  13524. *
  13525. * If that happens, someone indicated they wanted a
  13526. * mode change, which means it's safe to do a full
  13527. * recalculation.
  13528. */
  13529. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  13530. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  13531. update_scanline_offset(crtc);
  13532. }
  13533. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  13534. }
  13535. }
  13536. /* Scan out the current hw modeset state,
  13537. * and sanitizes it to the current state
  13538. */
  13539. static void
  13540. intel_modeset_setup_hw_state(struct drm_device *dev)
  13541. {
  13542. struct drm_i915_private *dev_priv = to_i915(dev);
  13543. enum pipe pipe;
  13544. struct intel_crtc *crtc;
  13545. struct intel_encoder *encoder;
  13546. int i;
  13547. intel_modeset_readout_hw_state(dev);
  13548. /* HW state is read out, now we need to sanitize this mess. */
  13549. for_each_intel_encoder(dev, encoder) {
  13550. intel_sanitize_encoder(encoder);
  13551. }
  13552. for_each_pipe(dev_priv, pipe) {
  13553. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13554. intel_sanitize_crtc(crtc);
  13555. intel_dump_pipe_config(crtc, crtc->config,
  13556. "[setup_hw_state]");
  13557. }
  13558. intel_modeset_update_connector_atomic_state(dev);
  13559. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13560. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13561. if (!pll->on || pll->active_mask)
  13562. continue;
  13563. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13564. pll->funcs.disable(dev_priv, pll);
  13565. pll->on = false;
  13566. }
  13567. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  13568. vlv_wm_get_hw_state(dev);
  13569. else if (IS_GEN9(dev))
  13570. skl_wm_get_hw_state(dev);
  13571. else if (HAS_PCH_SPLIT(dev))
  13572. ilk_wm_get_hw_state(dev);
  13573. for_each_intel_crtc(dev, crtc) {
  13574. unsigned long put_domains;
  13575. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13576. if (WARN_ON(put_domains))
  13577. modeset_put_power_domains(dev_priv, put_domains);
  13578. }
  13579. intel_display_set_init_power(dev_priv, false);
  13580. intel_fbc_init_pipe_state(dev_priv);
  13581. }
  13582. void intel_display_resume(struct drm_device *dev)
  13583. {
  13584. struct drm_i915_private *dev_priv = to_i915(dev);
  13585. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13586. struct drm_modeset_acquire_ctx ctx;
  13587. int ret;
  13588. dev_priv->modeset_restore_state = NULL;
  13589. if (state)
  13590. state->acquire_ctx = &ctx;
  13591. /*
  13592. * This is a cludge because with real atomic modeset mode_config.mutex
  13593. * won't be taken. Unfortunately some probed state like
  13594. * audio_codec_enable is still protected by mode_config.mutex, so lock
  13595. * it here for now.
  13596. */
  13597. mutex_lock(&dev->mode_config.mutex);
  13598. drm_modeset_acquire_init(&ctx, 0);
  13599. while (1) {
  13600. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13601. if (ret != -EDEADLK)
  13602. break;
  13603. drm_modeset_backoff(&ctx);
  13604. }
  13605. if (!ret)
  13606. ret = __intel_display_resume(dev, state);
  13607. drm_modeset_drop_locks(&ctx);
  13608. drm_modeset_acquire_fini(&ctx);
  13609. mutex_unlock(&dev->mode_config.mutex);
  13610. if (ret) {
  13611. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13612. drm_atomic_state_free(state);
  13613. }
  13614. }
  13615. void intel_modeset_gem_init(struct drm_device *dev)
  13616. {
  13617. struct drm_i915_private *dev_priv = to_i915(dev);
  13618. struct drm_crtc *c;
  13619. struct drm_i915_gem_object *obj;
  13620. int ret;
  13621. intel_init_gt_powersave(dev_priv);
  13622. intel_modeset_init_hw(dev);
  13623. intel_setup_overlay(dev_priv);
  13624. /*
  13625. * Make sure any fbs we allocated at startup are properly
  13626. * pinned & fenced. When we do the allocation it's too early
  13627. * for this.
  13628. */
  13629. for_each_crtc(dev, c) {
  13630. obj = intel_fb_obj(c->primary->fb);
  13631. if (obj == NULL)
  13632. continue;
  13633. mutex_lock(&dev->struct_mutex);
  13634. ret = intel_pin_and_fence_fb_obj(c->primary->fb,
  13635. c->primary->state->rotation);
  13636. mutex_unlock(&dev->struct_mutex);
  13637. if (ret) {
  13638. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13639. to_intel_crtc(c)->pipe);
  13640. drm_framebuffer_unreference(c->primary->fb);
  13641. c->primary->fb = NULL;
  13642. c->primary->crtc = c->primary->state->crtc = NULL;
  13643. update_state_fb(c->primary);
  13644. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13645. }
  13646. }
  13647. }
  13648. int intel_connector_register(struct drm_connector *connector)
  13649. {
  13650. struct intel_connector *intel_connector = to_intel_connector(connector);
  13651. int ret;
  13652. ret = intel_backlight_device_register(intel_connector);
  13653. if (ret)
  13654. goto err;
  13655. return 0;
  13656. err:
  13657. return ret;
  13658. }
  13659. void intel_connector_unregister(struct drm_connector *connector)
  13660. {
  13661. struct intel_connector *intel_connector = to_intel_connector(connector);
  13662. intel_backlight_device_unregister(intel_connector);
  13663. intel_panel_destroy_backlight(connector);
  13664. }
  13665. void intel_modeset_cleanup(struct drm_device *dev)
  13666. {
  13667. struct drm_i915_private *dev_priv = to_i915(dev);
  13668. intel_disable_gt_powersave(dev_priv);
  13669. /*
  13670. * Interrupts and polling as the first thing to avoid creating havoc.
  13671. * Too much stuff here (turning of connectors, ...) would
  13672. * experience fancy races otherwise.
  13673. */
  13674. intel_irq_uninstall(dev_priv);
  13675. /*
  13676. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13677. * poll handlers. Hence disable polling after hpd handling is shut down.
  13678. */
  13679. drm_kms_helper_poll_fini(dev);
  13680. intel_unregister_dsm_handler();
  13681. intel_fbc_global_disable(dev_priv);
  13682. /* flush any delayed tasks or pending work */
  13683. flush_scheduled_work();
  13684. drm_mode_config_cleanup(dev);
  13685. intel_cleanup_overlay(dev_priv);
  13686. intel_cleanup_gt_powersave(dev_priv);
  13687. intel_teardown_gmbus(dev);
  13688. }
  13689. void intel_connector_attach_encoder(struct intel_connector *connector,
  13690. struct intel_encoder *encoder)
  13691. {
  13692. connector->encoder = encoder;
  13693. drm_mode_connector_attach_encoder(&connector->base,
  13694. &encoder->base);
  13695. }
  13696. /*
  13697. * set vga decode state - true == enable VGA decode
  13698. */
  13699. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13700. {
  13701. struct drm_i915_private *dev_priv = to_i915(dev);
  13702. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13703. u16 gmch_ctrl;
  13704. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13705. DRM_ERROR("failed to read control word\n");
  13706. return -EIO;
  13707. }
  13708. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13709. return 0;
  13710. if (state)
  13711. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13712. else
  13713. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13714. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13715. DRM_ERROR("failed to write control word\n");
  13716. return -EIO;
  13717. }
  13718. return 0;
  13719. }
  13720. struct intel_display_error_state {
  13721. u32 power_well_driver;
  13722. int num_transcoders;
  13723. struct intel_cursor_error_state {
  13724. u32 control;
  13725. u32 position;
  13726. u32 base;
  13727. u32 size;
  13728. } cursor[I915_MAX_PIPES];
  13729. struct intel_pipe_error_state {
  13730. bool power_domain_on;
  13731. u32 source;
  13732. u32 stat;
  13733. } pipe[I915_MAX_PIPES];
  13734. struct intel_plane_error_state {
  13735. u32 control;
  13736. u32 stride;
  13737. u32 size;
  13738. u32 pos;
  13739. u32 addr;
  13740. u32 surface;
  13741. u32 tile_offset;
  13742. } plane[I915_MAX_PIPES];
  13743. struct intel_transcoder_error_state {
  13744. bool power_domain_on;
  13745. enum transcoder cpu_transcoder;
  13746. u32 conf;
  13747. u32 htotal;
  13748. u32 hblank;
  13749. u32 hsync;
  13750. u32 vtotal;
  13751. u32 vblank;
  13752. u32 vsync;
  13753. } transcoder[4];
  13754. };
  13755. struct intel_display_error_state *
  13756. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13757. {
  13758. struct intel_display_error_state *error;
  13759. int transcoders[] = {
  13760. TRANSCODER_A,
  13761. TRANSCODER_B,
  13762. TRANSCODER_C,
  13763. TRANSCODER_EDP,
  13764. };
  13765. int i;
  13766. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13767. return NULL;
  13768. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13769. if (error == NULL)
  13770. return NULL;
  13771. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13772. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13773. for_each_pipe(dev_priv, i) {
  13774. error->pipe[i].power_domain_on =
  13775. __intel_display_power_is_enabled(dev_priv,
  13776. POWER_DOMAIN_PIPE(i));
  13777. if (!error->pipe[i].power_domain_on)
  13778. continue;
  13779. error->cursor[i].control = I915_READ(CURCNTR(i));
  13780. error->cursor[i].position = I915_READ(CURPOS(i));
  13781. error->cursor[i].base = I915_READ(CURBASE(i));
  13782. error->plane[i].control = I915_READ(DSPCNTR(i));
  13783. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13784. if (INTEL_GEN(dev_priv) <= 3) {
  13785. error->plane[i].size = I915_READ(DSPSIZE(i));
  13786. error->plane[i].pos = I915_READ(DSPPOS(i));
  13787. }
  13788. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13789. error->plane[i].addr = I915_READ(DSPADDR(i));
  13790. if (INTEL_GEN(dev_priv) >= 4) {
  13791. error->plane[i].surface = I915_READ(DSPSURF(i));
  13792. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13793. }
  13794. error->pipe[i].source = I915_READ(PIPESRC(i));
  13795. if (HAS_GMCH_DISPLAY(dev_priv))
  13796. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13797. }
  13798. /* Note: this does not include DSI transcoders. */
  13799. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13800. if (HAS_DDI(dev_priv))
  13801. error->num_transcoders++; /* Account for eDP. */
  13802. for (i = 0; i < error->num_transcoders; i++) {
  13803. enum transcoder cpu_transcoder = transcoders[i];
  13804. error->transcoder[i].power_domain_on =
  13805. __intel_display_power_is_enabled(dev_priv,
  13806. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13807. if (!error->transcoder[i].power_domain_on)
  13808. continue;
  13809. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13810. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13811. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13812. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13813. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13814. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13815. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13816. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13817. }
  13818. return error;
  13819. }
  13820. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13821. void
  13822. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13823. struct drm_device *dev,
  13824. struct intel_display_error_state *error)
  13825. {
  13826. struct drm_i915_private *dev_priv = to_i915(dev);
  13827. int i;
  13828. if (!error)
  13829. return;
  13830. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13831. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13832. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13833. error->power_well_driver);
  13834. for_each_pipe(dev_priv, i) {
  13835. err_printf(m, "Pipe [%d]:\n", i);
  13836. err_printf(m, " Power: %s\n",
  13837. onoff(error->pipe[i].power_domain_on));
  13838. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13839. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13840. err_printf(m, "Plane [%d]:\n", i);
  13841. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13842. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13843. if (INTEL_INFO(dev)->gen <= 3) {
  13844. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13845. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13846. }
  13847. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13848. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13849. if (INTEL_INFO(dev)->gen >= 4) {
  13850. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13851. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13852. }
  13853. err_printf(m, "Cursor [%d]:\n", i);
  13854. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13855. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13856. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13857. }
  13858. for (i = 0; i < error->num_transcoders; i++) {
  13859. err_printf(m, "CPU transcoder: %s\n",
  13860. transcoder_name(error->transcoder[i].cpu_transcoder));
  13861. err_printf(m, " Power: %s\n",
  13862. onoff(error->transcoder[i].power_domain_on));
  13863. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13864. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13865. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13866. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13867. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13868. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13869. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13870. }
  13871. }