intel_display.c 461 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551
  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. drm_modeset_lock_crtc(crtc, &plane->base);
  2667. plane_state = to_intel_plane_state(plane->base.state);
  2668. if (plane_state->visible)
  2669. plane->update_plane(&plane->base,
  2670. to_intel_crtc_state(crtc->state),
  2671. plane_state);
  2672. drm_modeset_unlock_crtc(crtc);
  2673. }
  2674. }
  2675. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  2676. {
  2677. /* no reset support for gen2 */
  2678. if (IS_GEN2(dev_priv))
  2679. return;
  2680. /* reset doesn't touch the display */
  2681. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  2682. return;
  2683. drm_modeset_lock_all(&dev_priv->drm);
  2684. /*
  2685. * Disabling the crtcs gracefully seems nicer. Also the
  2686. * g33 docs say we should at least disable all the planes.
  2687. */
  2688. intel_display_suspend(&dev_priv->drm);
  2689. }
  2690. void intel_finish_reset(struct drm_i915_private *dev_priv)
  2691. {
  2692. /*
  2693. * Flips in the rings will be nuked by the reset,
  2694. * so complete all pending flips so that user space
  2695. * will get its events and not get stuck.
  2696. */
  2697. intel_complete_page_flips(dev_priv);
  2698. /* no reset support for gen2 */
  2699. if (IS_GEN2(dev_priv))
  2700. return;
  2701. /* reset doesn't touch the display */
  2702. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
  2703. /*
  2704. * Flips in the rings have been nuked by the reset,
  2705. * so update the base address of all primary
  2706. * planes to the the last fb to make sure we're
  2707. * showing the correct fb after a reset.
  2708. *
  2709. * FIXME: Atomic will make this obsolete since we won't schedule
  2710. * CS-based flips (which might get lost in gpu resets) any more.
  2711. */
  2712. intel_update_primary_planes(&dev_priv->drm);
  2713. return;
  2714. }
  2715. /*
  2716. * The display has been reset as well,
  2717. * so need a full re-initialization.
  2718. */
  2719. intel_runtime_pm_disable_interrupts(dev_priv);
  2720. intel_runtime_pm_enable_interrupts(dev_priv);
  2721. intel_modeset_init_hw(&dev_priv->drm);
  2722. spin_lock_irq(&dev_priv->irq_lock);
  2723. if (dev_priv->display.hpd_irq_setup)
  2724. dev_priv->display.hpd_irq_setup(dev_priv);
  2725. spin_unlock_irq(&dev_priv->irq_lock);
  2726. intel_display_resume(&dev_priv->drm);
  2727. intel_hpd_init(dev_priv);
  2728. drm_modeset_unlock_all(&dev_priv->drm);
  2729. }
  2730. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2731. {
  2732. struct drm_device *dev = crtc->dev;
  2733. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2734. unsigned reset_counter;
  2735. bool pending;
  2736. reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
  2737. if (intel_crtc->reset_counter != reset_counter)
  2738. return false;
  2739. spin_lock_irq(&dev->event_lock);
  2740. pending = to_intel_crtc(crtc)->flip_work != NULL;
  2741. spin_unlock_irq(&dev->event_lock);
  2742. return pending;
  2743. }
  2744. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2745. struct intel_crtc_state *old_crtc_state)
  2746. {
  2747. struct drm_device *dev = crtc->base.dev;
  2748. struct drm_i915_private *dev_priv = to_i915(dev);
  2749. struct intel_crtc_state *pipe_config =
  2750. to_intel_crtc_state(crtc->base.state);
  2751. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2752. crtc->base.mode = crtc->base.state->mode;
  2753. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2754. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2755. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2756. /*
  2757. * Update pipe size and adjust fitter if needed: the reason for this is
  2758. * that in compute_mode_changes we check the native mode (not the pfit
  2759. * mode) to see if we can flip rather than do a full mode set. In the
  2760. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2761. * pfit state, we'll end up with a big fb scanned out into the wrong
  2762. * sized surface.
  2763. */
  2764. I915_WRITE(PIPESRC(crtc->pipe),
  2765. ((pipe_config->pipe_src_w - 1) << 16) |
  2766. (pipe_config->pipe_src_h - 1));
  2767. /* on skylake this is done by detaching scalers */
  2768. if (INTEL_INFO(dev)->gen >= 9) {
  2769. skl_detach_scalers(crtc);
  2770. if (pipe_config->pch_pfit.enabled)
  2771. skylake_pfit_enable(crtc);
  2772. } else if (HAS_PCH_SPLIT(dev)) {
  2773. if (pipe_config->pch_pfit.enabled)
  2774. ironlake_pfit_enable(crtc);
  2775. else if (old_crtc_state->pch_pfit.enabled)
  2776. ironlake_pfit_disable(crtc, true);
  2777. }
  2778. }
  2779. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2780. {
  2781. struct drm_device *dev = crtc->dev;
  2782. struct drm_i915_private *dev_priv = to_i915(dev);
  2783. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2784. int pipe = intel_crtc->pipe;
  2785. i915_reg_t reg;
  2786. u32 temp;
  2787. /* enable normal train */
  2788. reg = FDI_TX_CTL(pipe);
  2789. temp = I915_READ(reg);
  2790. if (IS_IVYBRIDGE(dev)) {
  2791. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2792. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2793. } else {
  2794. temp &= ~FDI_LINK_TRAIN_NONE;
  2795. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2796. }
  2797. I915_WRITE(reg, temp);
  2798. reg = FDI_RX_CTL(pipe);
  2799. temp = I915_READ(reg);
  2800. if (HAS_PCH_CPT(dev)) {
  2801. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2802. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2803. } else {
  2804. temp &= ~FDI_LINK_TRAIN_NONE;
  2805. temp |= FDI_LINK_TRAIN_NONE;
  2806. }
  2807. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2808. /* wait one idle pattern time */
  2809. POSTING_READ(reg);
  2810. udelay(1000);
  2811. /* IVB wants error correction enabled */
  2812. if (IS_IVYBRIDGE(dev))
  2813. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2814. FDI_FE_ERRC_ENABLE);
  2815. }
  2816. /* The FDI link training functions for ILK/Ibexpeak. */
  2817. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2818. {
  2819. struct drm_device *dev = crtc->dev;
  2820. struct drm_i915_private *dev_priv = to_i915(dev);
  2821. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2822. int pipe = intel_crtc->pipe;
  2823. i915_reg_t reg;
  2824. u32 temp, tries;
  2825. /* FDI needs bits from pipe first */
  2826. assert_pipe_enabled(dev_priv, pipe);
  2827. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2828. for train result */
  2829. reg = FDI_RX_IMR(pipe);
  2830. temp = I915_READ(reg);
  2831. temp &= ~FDI_RX_SYMBOL_LOCK;
  2832. temp &= ~FDI_RX_BIT_LOCK;
  2833. I915_WRITE(reg, temp);
  2834. I915_READ(reg);
  2835. udelay(150);
  2836. /* enable CPU FDI TX and PCH FDI RX */
  2837. reg = FDI_TX_CTL(pipe);
  2838. temp = I915_READ(reg);
  2839. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2840. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2841. temp &= ~FDI_LINK_TRAIN_NONE;
  2842. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2843. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2844. reg = FDI_RX_CTL(pipe);
  2845. temp = I915_READ(reg);
  2846. temp &= ~FDI_LINK_TRAIN_NONE;
  2847. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2848. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2849. POSTING_READ(reg);
  2850. udelay(150);
  2851. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2852. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2853. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2854. FDI_RX_PHASE_SYNC_POINTER_EN);
  2855. reg = FDI_RX_IIR(pipe);
  2856. for (tries = 0; tries < 5; tries++) {
  2857. temp = I915_READ(reg);
  2858. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2859. if ((temp & FDI_RX_BIT_LOCK)) {
  2860. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2861. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2862. break;
  2863. }
  2864. }
  2865. if (tries == 5)
  2866. DRM_ERROR("FDI train 1 fail!\n");
  2867. /* Train 2 */
  2868. reg = FDI_TX_CTL(pipe);
  2869. temp = I915_READ(reg);
  2870. temp &= ~FDI_LINK_TRAIN_NONE;
  2871. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2872. I915_WRITE(reg, temp);
  2873. reg = FDI_RX_CTL(pipe);
  2874. temp = I915_READ(reg);
  2875. temp &= ~FDI_LINK_TRAIN_NONE;
  2876. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2877. I915_WRITE(reg, temp);
  2878. POSTING_READ(reg);
  2879. udelay(150);
  2880. reg = FDI_RX_IIR(pipe);
  2881. for (tries = 0; tries < 5; tries++) {
  2882. temp = I915_READ(reg);
  2883. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2884. if (temp & FDI_RX_SYMBOL_LOCK) {
  2885. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2886. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2887. break;
  2888. }
  2889. }
  2890. if (tries == 5)
  2891. DRM_ERROR("FDI train 2 fail!\n");
  2892. DRM_DEBUG_KMS("FDI train done\n");
  2893. }
  2894. static const int snb_b_fdi_train_param[] = {
  2895. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2896. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2897. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2898. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2899. };
  2900. /* The FDI link training functions for SNB/Cougarpoint. */
  2901. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2902. {
  2903. struct drm_device *dev = crtc->dev;
  2904. struct drm_i915_private *dev_priv = to_i915(dev);
  2905. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2906. int pipe = intel_crtc->pipe;
  2907. i915_reg_t reg;
  2908. u32 temp, i, retry;
  2909. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2910. for train result */
  2911. reg = FDI_RX_IMR(pipe);
  2912. temp = I915_READ(reg);
  2913. temp &= ~FDI_RX_SYMBOL_LOCK;
  2914. temp &= ~FDI_RX_BIT_LOCK;
  2915. I915_WRITE(reg, temp);
  2916. POSTING_READ(reg);
  2917. udelay(150);
  2918. /* enable CPU FDI TX and PCH FDI RX */
  2919. reg = FDI_TX_CTL(pipe);
  2920. temp = I915_READ(reg);
  2921. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2922. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2923. temp &= ~FDI_LINK_TRAIN_NONE;
  2924. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2925. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2926. /* SNB-B */
  2927. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2928. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2929. I915_WRITE(FDI_RX_MISC(pipe),
  2930. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2931. reg = FDI_RX_CTL(pipe);
  2932. temp = I915_READ(reg);
  2933. if (HAS_PCH_CPT(dev)) {
  2934. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2935. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2936. } else {
  2937. temp &= ~FDI_LINK_TRAIN_NONE;
  2938. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2939. }
  2940. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2941. POSTING_READ(reg);
  2942. udelay(150);
  2943. for (i = 0; i < 4; i++) {
  2944. reg = FDI_TX_CTL(pipe);
  2945. temp = I915_READ(reg);
  2946. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2947. temp |= snb_b_fdi_train_param[i];
  2948. I915_WRITE(reg, temp);
  2949. POSTING_READ(reg);
  2950. udelay(500);
  2951. for (retry = 0; retry < 5; retry++) {
  2952. reg = FDI_RX_IIR(pipe);
  2953. temp = I915_READ(reg);
  2954. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2955. if (temp & FDI_RX_BIT_LOCK) {
  2956. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2957. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2958. break;
  2959. }
  2960. udelay(50);
  2961. }
  2962. if (retry < 5)
  2963. break;
  2964. }
  2965. if (i == 4)
  2966. DRM_ERROR("FDI train 1 fail!\n");
  2967. /* Train 2 */
  2968. reg = FDI_TX_CTL(pipe);
  2969. temp = I915_READ(reg);
  2970. temp &= ~FDI_LINK_TRAIN_NONE;
  2971. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2972. if (IS_GEN6(dev)) {
  2973. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2974. /* SNB-B */
  2975. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2976. }
  2977. I915_WRITE(reg, temp);
  2978. reg = FDI_RX_CTL(pipe);
  2979. temp = I915_READ(reg);
  2980. if (HAS_PCH_CPT(dev)) {
  2981. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2982. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2983. } else {
  2984. temp &= ~FDI_LINK_TRAIN_NONE;
  2985. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2986. }
  2987. I915_WRITE(reg, temp);
  2988. POSTING_READ(reg);
  2989. udelay(150);
  2990. for (i = 0; i < 4; i++) {
  2991. reg = FDI_TX_CTL(pipe);
  2992. temp = I915_READ(reg);
  2993. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2994. temp |= snb_b_fdi_train_param[i];
  2995. I915_WRITE(reg, temp);
  2996. POSTING_READ(reg);
  2997. udelay(500);
  2998. for (retry = 0; retry < 5; retry++) {
  2999. reg = FDI_RX_IIR(pipe);
  3000. temp = I915_READ(reg);
  3001. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3002. if (temp & FDI_RX_SYMBOL_LOCK) {
  3003. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3004. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3005. break;
  3006. }
  3007. udelay(50);
  3008. }
  3009. if (retry < 5)
  3010. break;
  3011. }
  3012. if (i == 4)
  3013. DRM_ERROR("FDI train 2 fail!\n");
  3014. DRM_DEBUG_KMS("FDI train done.\n");
  3015. }
  3016. /* Manual link training for Ivy Bridge A0 parts */
  3017. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3018. {
  3019. struct drm_device *dev = crtc->dev;
  3020. struct drm_i915_private *dev_priv = to_i915(dev);
  3021. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3022. int pipe = intel_crtc->pipe;
  3023. i915_reg_t reg;
  3024. u32 temp, i, j;
  3025. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3026. for train result */
  3027. reg = FDI_RX_IMR(pipe);
  3028. temp = I915_READ(reg);
  3029. temp &= ~FDI_RX_SYMBOL_LOCK;
  3030. temp &= ~FDI_RX_BIT_LOCK;
  3031. I915_WRITE(reg, temp);
  3032. POSTING_READ(reg);
  3033. udelay(150);
  3034. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3035. I915_READ(FDI_RX_IIR(pipe)));
  3036. /* Try each vswing and preemphasis setting twice before moving on */
  3037. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3038. /* disable first in case we need to retry */
  3039. reg = FDI_TX_CTL(pipe);
  3040. temp = I915_READ(reg);
  3041. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3042. temp &= ~FDI_TX_ENABLE;
  3043. I915_WRITE(reg, temp);
  3044. reg = FDI_RX_CTL(pipe);
  3045. temp = I915_READ(reg);
  3046. temp &= ~FDI_LINK_TRAIN_AUTO;
  3047. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3048. temp &= ~FDI_RX_ENABLE;
  3049. I915_WRITE(reg, temp);
  3050. /* enable CPU FDI TX and PCH FDI RX */
  3051. reg = FDI_TX_CTL(pipe);
  3052. temp = I915_READ(reg);
  3053. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3054. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3055. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3056. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3057. temp |= snb_b_fdi_train_param[j/2];
  3058. temp |= FDI_COMPOSITE_SYNC;
  3059. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3060. I915_WRITE(FDI_RX_MISC(pipe),
  3061. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3062. reg = FDI_RX_CTL(pipe);
  3063. temp = I915_READ(reg);
  3064. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3065. temp |= FDI_COMPOSITE_SYNC;
  3066. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3067. POSTING_READ(reg);
  3068. udelay(1); /* should be 0.5us */
  3069. for (i = 0; i < 4; i++) {
  3070. reg = FDI_RX_IIR(pipe);
  3071. temp = I915_READ(reg);
  3072. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3073. if (temp & FDI_RX_BIT_LOCK ||
  3074. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3075. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3076. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3077. i);
  3078. break;
  3079. }
  3080. udelay(1); /* should be 0.5us */
  3081. }
  3082. if (i == 4) {
  3083. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3084. continue;
  3085. }
  3086. /* Train 2 */
  3087. reg = FDI_TX_CTL(pipe);
  3088. temp = I915_READ(reg);
  3089. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3090. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3091. I915_WRITE(reg, temp);
  3092. reg = FDI_RX_CTL(pipe);
  3093. temp = I915_READ(reg);
  3094. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3095. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3096. I915_WRITE(reg, temp);
  3097. POSTING_READ(reg);
  3098. udelay(2); /* should be 1.5us */
  3099. for (i = 0; i < 4; i++) {
  3100. reg = FDI_RX_IIR(pipe);
  3101. temp = I915_READ(reg);
  3102. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3103. if (temp & FDI_RX_SYMBOL_LOCK ||
  3104. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3105. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3106. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3107. i);
  3108. goto train_done;
  3109. }
  3110. udelay(2); /* should be 1.5us */
  3111. }
  3112. if (i == 4)
  3113. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3114. }
  3115. train_done:
  3116. DRM_DEBUG_KMS("FDI train done.\n");
  3117. }
  3118. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3119. {
  3120. struct drm_device *dev = intel_crtc->base.dev;
  3121. struct drm_i915_private *dev_priv = to_i915(dev);
  3122. int pipe = intel_crtc->pipe;
  3123. i915_reg_t reg;
  3124. u32 temp;
  3125. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3126. reg = FDI_RX_CTL(pipe);
  3127. temp = I915_READ(reg);
  3128. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3129. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3130. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3131. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3132. POSTING_READ(reg);
  3133. udelay(200);
  3134. /* Switch from Rawclk to PCDclk */
  3135. temp = I915_READ(reg);
  3136. I915_WRITE(reg, temp | FDI_PCDCLK);
  3137. POSTING_READ(reg);
  3138. udelay(200);
  3139. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3140. reg = FDI_TX_CTL(pipe);
  3141. temp = I915_READ(reg);
  3142. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3143. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3144. POSTING_READ(reg);
  3145. udelay(100);
  3146. }
  3147. }
  3148. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3149. {
  3150. struct drm_device *dev = intel_crtc->base.dev;
  3151. struct drm_i915_private *dev_priv = to_i915(dev);
  3152. int pipe = intel_crtc->pipe;
  3153. i915_reg_t reg;
  3154. u32 temp;
  3155. /* Switch from PCDclk to Rawclk */
  3156. reg = FDI_RX_CTL(pipe);
  3157. temp = I915_READ(reg);
  3158. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3159. /* Disable CPU FDI TX PLL */
  3160. reg = FDI_TX_CTL(pipe);
  3161. temp = I915_READ(reg);
  3162. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3163. POSTING_READ(reg);
  3164. udelay(100);
  3165. reg = FDI_RX_CTL(pipe);
  3166. temp = I915_READ(reg);
  3167. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3168. /* Wait for the clocks to turn off. */
  3169. POSTING_READ(reg);
  3170. udelay(100);
  3171. }
  3172. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3173. {
  3174. struct drm_device *dev = crtc->dev;
  3175. struct drm_i915_private *dev_priv = to_i915(dev);
  3176. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3177. int pipe = intel_crtc->pipe;
  3178. i915_reg_t reg;
  3179. u32 temp;
  3180. /* disable CPU FDI tx and PCH FDI rx */
  3181. reg = FDI_TX_CTL(pipe);
  3182. temp = I915_READ(reg);
  3183. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3184. POSTING_READ(reg);
  3185. reg = FDI_RX_CTL(pipe);
  3186. temp = I915_READ(reg);
  3187. temp &= ~(0x7 << 16);
  3188. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3189. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3190. POSTING_READ(reg);
  3191. udelay(100);
  3192. /* Ironlake workaround, disable clock pointer after downing FDI */
  3193. if (HAS_PCH_IBX(dev))
  3194. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3195. /* still set train pattern 1 */
  3196. reg = FDI_TX_CTL(pipe);
  3197. temp = I915_READ(reg);
  3198. temp &= ~FDI_LINK_TRAIN_NONE;
  3199. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3200. I915_WRITE(reg, temp);
  3201. reg = FDI_RX_CTL(pipe);
  3202. temp = I915_READ(reg);
  3203. if (HAS_PCH_CPT(dev)) {
  3204. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3205. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3206. } else {
  3207. temp &= ~FDI_LINK_TRAIN_NONE;
  3208. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3209. }
  3210. /* BPC in FDI rx is consistent with that in PIPECONF */
  3211. temp &= ~(0x07 << 16);
  3212. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3213. I915_WRITE(reg, temp);
  3214. POSTING_READ(reg);
  3215. udelay(100);
  3216. }
  3217. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3218. {
  3219. struct intel_crtc *crtc;
  3220. /* Note that we don't need to be called with mode_config.lock here
  3221. * as our list of CRTC objects is static for the lifetime of the
  3222. * device and so cannot disappear as we iterate. Similarly, we can
  3223. * happily treat the predicates as racy, atomic checks as userspace
  3224. * cannot claim and pin a new fb without at least acquring the
  3225. * struct_mutex and so serialising with us.
  3226. */
  3227. for_each_intel_crtc(dev, crtc) {
  3228. if (atomic_read(&crtc->unpin_work_count) == 0)
  3229. continue;
  3230. if (crtc->flip_work)
  3231. intel_wait_for_vblank(dev, crtc->pipe);
  3232. return true;
  3233. }
  3234. return false;
  3235. }
  3236. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3237. {
  3238. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3239. struct intel_flip_work *work = intel_crtc->flip_work;
  3240. intel_crtc->flip_work = NULL;
  3241. if (work->event)
  3242. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3243. drm_crtc_vblank_put(&intel_crtc->base);
  3244. wake_up_all(&dev_priv->pending_flip_queue);
  3245. queue_work(dev_priv->wq, &work->unpin_work);
  3246. trace_i915_flip_complete(intel_crtc->plane,
  3247. work->pending_flip_obj);
  3248. }
  3249. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3250. {
  3251. struct drm_device *dev = crtc->dev;
  3252. struct drm_i915_private *dev_priv = to_i915(dev);
  3253. long ret;
  3254. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3255. ret = wait_event_interruptible_timeout(
  3256. dev_priv->pending_flip_queue,
  3257. !intel_crtc_has_pending_flip(crtc),
  3258. 60*HZ);
  3259. if (ret < 0)
  3260. return ret;
  3261. if (ret == 0) {
  3262. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3263. struct intel_flip_work *work;
  3264. spin_lock_irq(&dev->event_lock);
  3265. work = intel_crtc->flip_work;
  3266. if (work && !is_mmio_work(work)) {
  3267. WARN_ONCE(1, "Removing stuck page flip\n");
  3268. page_flip_completed(intel_crtc);
  3269. }
  3270. spin_unlock_irq(&dev->event_lock);
  3271. }
  3272. return 0;
  3273. }
  3274. static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3275. {
  3276. u32 temp;
  3277. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3278. mutex_lock(&dev_priv->sb_lock);
  3279. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3280. temp |= SBI_SSCCTL_DISABLE;
  3281. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3282. mutex_unlock(&dev_priv->sb_lock);
  3283. }
  3284. /* Program iCLKIP clock to the desired frequency */
  3285. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3286. {
  3287. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3288. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3289. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3290. u32 temp;
  3291. lpt_disable_iclkip(dev_priv);
  3292. /* The iCLK virtual clock root frequency is in MHz,
  3293. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3294. * divisors, it is necessary to divide one by another, so we
  3295. * convert the virtual clock precision to KHz here for higher
  3296. * precision.
  3297. */
  3298. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3299. u32 iclk_virtual_root_freq = 172800 * 1000;
  3300. u32 iclk_pi_range = 64;
  3301. u32 desired_divisor;
  3302. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3303. clock << auxdiv);
  3304. divsel = (desired_divisor / iclk_pi_range) - 2;
  3305. phaseinc = desired_divisor % iclk_pi_range;
  3306. /*
  3307. * Near 20MHz is a corner case which is
  3308. * out of range for the 7-bit divisor
  3309. */
  3310. if (divsel <= 0x7f)
  3311. break;
  3312. }
  3313. /* This should not happen with any sane values */
  3314. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3315. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3316. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3317. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3318. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3319. clock,
  3320. auxdiv,
  3321. divsel,
  3322. phasedir,
  3323. phaseinc);
  3324. mutex_lock(&dev_priv->sb_lock);
  3325. /* Program SSCDIVINTPHASE6 */
  3326. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3327. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3328. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3329. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3330. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3331. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3332. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3333. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3334. /* Program SSCAUXDIV */
  3335. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3336. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3337. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3338. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3339. /* Enable modulator and associated divider */
  3340. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3341. temp &= ~SBI_SSCCTL_DISABLE;
  3342. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3343. mutex_unlock(&dev_priv->sb_lock);
  3344. /* Wait for initialization time */
  3345. udelay(24);
  3346. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3347. }
  3348. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3349. {
  3350. u32 divsel, phaseinc, auxdiv;
  3351. u32 iclk_virtual_root_freq = 172800 * 1000;
  3352. u32 iclk_pi_range = 64;
  3353. u32 desired_divisor;
  3354. u32 temp;
  3355. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3356. return 0;
  3357. mutex_lock(&dev_priv->sb_lock);
  3358. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3359. if (temp & SBI_SSCCTL_DISABLE) {
  3360. mutex_unlock(&dev_priv->sb_lock);
  3361. return 0;
  3362. }
  3363. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3364. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3365. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3366. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3367. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3368. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3369. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3370. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3371. mutex_unlock(&dev_priv->sb_lock);
  3372. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3373. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3374. desired_divisor << auxdiv);
  3375. }
  3376. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3377. enum pipe pch_transcoder)
  3378. {
  3379. struct drm_device *dev = crtc->base.dev;
  3380. struct drm_i915_private *dev_priv = to_i915(dev);
  3381. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3382. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3383. I915_READ(HTOTAL(cpu_transcoder)));
  3384. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3385. I915_READ(HBLANK(cpu_transcoder)));
  3386. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3387. I915_READ(HSYNC(cpu_transcoder)));
  3388. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3389. I915_READ(VTOTAL(cpu_transcoder)));
  3390. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3391. I915_READ(VBLANK(cpu_transcoder)));
  3392. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3393. I915_READ(VSYNC(cpu_transcoder)));
  3394. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3395. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3396. }
  3397. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3398. {
  3399. struct drm_i915_private *dev_priv = to_i915(dev);
  3400. uint32_t temp;
  3401. temp = I915_READ(SOUTH_CHICKEN1);
  3402. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3403. return;
  3404. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3405. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3406. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3407. if (enable)
  3408. temp |= FDI_BC_BIFURCATION_SELECT;
  3409. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3410. I915_WRITE(SOUTH_CHICKEN1, temp);
  3411. POSTING_READ(SOUTH_CHICKEN1);
  3412. }
  3413. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3414. {
  3415. struct drm_device *dev = intel_crtc->base.dev;
  3416. switch (intel_crtc->pipe) {
  3417. case PIPE_A:
  3418. break;
  3419. case PIPE_B:
  3420. if (intel_crtc->config->fdi_lanes > 2)
  3421. cpt_set_fdi_bc_bifurcation(dev, false);
  3422. else
  3423. cpt_set_fdi_bc_bifurcation(dev, true);
  3424. break;
  3425. case PIPE_C:
  3426. cpt_set_fdi_bc_bifurcation(dev, true);
  3427. break;
  3428. default:
  3429. BUG();
  3430. }
  3431. }
  3432. /* Return which DP Port should be selected for Transcoder DP control */
  3433. static enum port
  3434. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3435. {
  3436. struct drm_device *dev = crtc->dev;
  3437. struct intel_encoder *encoder;
  3438. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3439. if (encoder->type == INTEL_OUTPUT_DP ||
  3440. encoder->type == INTEL_OUTPUT_EDP)
  3441. return enc_to_dig_port(&encoder->base)->port;
  3442. }
  3443. return -1;
  3444. }
  3445. /*
  3446. * Enable PCH resources required for PCH ports:
  3447. * - PCH PLLs
  3448. * - FDI training & RX/TX
  3449. * - update transcoder timings
  3450. * - DP transcoding bits
  3451. * - transcoder
  3452. */
  3453. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3454. {
  3455. struct drm_device *dev = crtc->dev;
  3456. struct drm_i915_private *dev_priv = to_i915(dev);
  3457. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3458. int pipe = intel_crtc->pipe;
  3459. u32 temp;
  3460. assert_pch_transcoder_disabled(dev_priv, pipe);
  3461. if (IS_IVYBRIDGE(dev))
  3462. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3463. /* Write the TU size bits before fdi link training, so that error
  3464. * detection works. */
  3465. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3466. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3467. /* For PCH output, training FDI link */
  3468. dev_priv->display.fdi_link_train(crtc);
  3469. /* We need to program the right clock selection before writing the pixel
  3470. * mutliplier into the DPLL. */
  3471. if (HAS_PCH_CPT(dev)) {
  3472. u32 sel;
  3473. temp = I915_READ(PCH_DPLL_SEL);
  3474. temp |= TRANS_DPLL_ENABLE(pipe);
  3475. sel = TRANS_DPLLB_SEL(pipe);
  3476. if (intel_crtc->config->shared_dpll ==
  3477. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3478. temp |= sel;
  3479. else
  3480. temp &= ~sel;
  3481. I915_WRITE(PCH_DPLL_SEL, temp);
  3482. }
  3483. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3484. * transcoder, and we actually should do this to not upset any PCH
  3485. * transcoder that already use the clock when we share it.
  3486. *
  3487. * Note that enable_shared_dpll tries to do the right thing, but
  3488. * get_shared_dpll unconditionally resets the pll - we need that to have
  3489. * the right LVDS enable sequence. */
  3490. intel_enable_shared_dpll(intel_crtc);
  3491. /* set transcoder timing, panel must allow it */
  3492. assert_panel_unlocked(dev_priv, pipe);
  3493. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3494. intel_fdi_normal_train(crtc);
  3495. /* For PCH DP, enable TRANS_DP_CTL */
  3496. if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3497. const struct drm_display_mode *adjusted_mode =
  3498. &intel_crtc->config->base.adjusted_mode;
  3499. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3500. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3501. temp = I915_READ(reg);
  3502. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3503. TRANS_DP_SYNC_MASK |
  3504. TRANS_DP_BPC_MASK);
  3505. temp |= TRANS_DP_OUTPUT_ENABLE;
  3506. temp |= bpc << 9; /* same format but at 11:9 */
  3507. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3508. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3509. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3510. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3511. switch (intel_trans_dp_port_sel(crtc)) {
  3512. case PORT_B:
  3513. temp |= TRANS_DP_PORT_SEL_B;
  3514. break;
  3515. case PORT_C:
  3516. temp |= TRANS_DP_PORT_SEL_C;
  3517. break;
  3518. case PORT_D:
  3519. temp |= TRANS_DP_PORT_SEL_D;
  3520. break;
  3521. default:
  3522. BUG();
  3523. }
  3524. I915_WRITE(reg, temp);
  3525. }
  3526. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3527. }
  3528. static void lpt_pch_enable(struct drm_crtc *crtc)
  3529. {
  3530. struct drm_device *dev = crtc->dev;
  3531. struct drm_i915_private *dev_priv = to_i915(dev);
  3532. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3533. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3534. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3535. lpt_program_iclkip(crtc);
  3536. /* Set transcoder timing. */
  3537. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3538. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3539. }
  3540. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3541. {
  3542. struct drm_i915_private *dev_priv = to_i915(dev);
  3543. i915_reg_t dslreg = PIPEDSL(pipe);
  3544. u32 temp;
  3545. temp = I915_READ(dslreg);
  3546. udelay(500);
  3547. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3548. if (wait_for(I915_READ(dslreg) != temp, 5))
  3549. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3550. }
  3551. }
  3552. static int
  3553. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3554. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3555. int src_w, int src_h, int dst_w, int dst_h)
  3556. {
  3557. struct intel_crtc_scaler_state *scaler_state =
  3558. &crtc_state->scaler_state;
  3559. struct intel_crtc *intel_crtc =
  3560. to_intel_crtc(crtc_state->base.crtc);
  3561. int need_scaling;
  3562. need_scaling = intel_rotation_90_or_270(rotation) ?
  3563. (src_h != dst_w || src_w != dst_h):
  3564. (src_w != dst_w || src_h != dst_h);
  3565. /*
  3566. * if plane is being disabled or scaler is no more required or force detach
  3567. * - free scaler binded to this plane/crtc
  3568. * - in order to do this, update crtc->scaler_usage
  3569. *
  3570. * Here scaler state in crtc_state is set free so that
  3571. * scaler can be assigned to other user. Actual register
  3572. * update to free the scaler is done in plane/panel-fit programming.
  3573. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3574. */
  3575. if (force_detach || !need_scaling) {
  3576. if (*scaler_id >= 0) {
  3577. scaler_state->scaler_users &= ~(1 << scaler_user);
  3578. scaler_state->scalers[*scaler_id].in_use = 0;
  3579. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3580. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3581. intel_crtc->pipe, scaler_user, *scaler_id,
  3582. scaler_state->scaler_users);
  3583. *scaler_id = -1;
  3584. }
  3585. return 0;
  3586. }
  3587. /* range checks */
  3588. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3589. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3590. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3591. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3592. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3593. "size is out of scaler range\n",
  3594. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3595. return -EINVAL;
  3596. }
  3597. /* mark this plane as a scaler user in crtc_state */
  3598. scaler_state->scaler_users |= (1 << scaler_user);
  3599. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3600. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3601. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3602. scaler_state->scaler_users);
  3603. return 0;
  3604. }
  3605. /**
  3606. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3607. *
  3608. * @state: crtc's scaler state
  3609. *
  3610. * Return
  3611. * 0 - scaler_usage updated successfully
  3612. * error - requested scaling cannot be supported or other error condition
  3613. */
  3614. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3615. {
  3616. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3617. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3618. DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
  3619. intel_crtc->base.base.id, intel_crtc->base.name,
  3620. intel_crtc->pipe, SKL_CRTC_INDEX);
  3621. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3622. &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
  3623. state->pipe_src_w, state->pipe_src_h,
  3624. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3625. }
  3626. /**
  3627. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3628. *
  3629. * @state: crtc's scaler state
  3630. * @plane_state: atomic plane state to update
  3631. *
  3632. * Return
  3633. * 0 - scaler_usage updated successfully
  3634. * error - requested scaling cannot be supported or other error condition
  3635. */
  3636. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3637. struct intel_plane_state *plane_state)
  3638. {
  3639. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3640. struct intel_plane *intel_plane =
  3641. to_intel_plane(plane_state->base.plane);
  3642. struct drm_framebuffer *fb = plane_state->base.fb;
  3643. int ret;
  3644. bool force_detach = !fb || !plane_state->visible;
  3645. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
  3646. intel_plane->base.base.id, intel_plane->base.name,
  3647. intel_crtc->pipe, drm_plane_index(&intel_plane->base));
  3648. ret = skl_update_scaler(crtc_state, force_detach,
  3649. drm_plane_index(&intel_plane->base),
  3650. &plane_state->scaler_id,
  3651. plane_state->base.rotation,
  3652. drm_rect_width(&plane_state->src) >> 16,
  3653. drm_rect_height(&plane_state->src) >> 16,
  3654. drm_rect_width(&plane_state->dst),
  3655. drm_rect_height(&plane_state->dst));
  3656. if (ret || plane_state->scaler_id < 0)
  3657. return ret;
  3658. /* check colorkey */
  3659. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3660. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  3661. intel_plane->base.base.id,
  3662. intel_plane->base.name);
  3663. return -EINVAL;
  3664. }
  3665. /* Check src format */
  3666. switch (fb->pixel_format) {
  3667. case DRM_FORMAT_RGB565:
  3668. case DRM_FORMAT_XBGR8888:
  3669. case DRM_FORMAT_XRGB8888:
  3670. case DRM_FORMAT_ABGR8888:
  3671. case DRM_FORMAT_ARGB8888:
  3672. case DRM_FORMAT_XRGB2101010:
  3673. case DRM_FORMAT_XBGR2101010:
  3674. case DRM_FORMAT_YUYV:
  3675. case DRM_FORMAT_YVYU:
  3676. case DRM_FORMAT_UYVY:
  3677. case DRM_FORMAT_VYUY:
  3678. break;
  3679. default:
  3680. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  3681. intel_plane->base.base.id, intel_plane->base.name,
  3682. fb->base.id, fb->pixel_format);
  3683. return -EINVAL;
  3684. }
  3685. return 0;
  3686. }
  3687. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3688. {
  3689. int i;
  3690. for (i = 0; i < crtc->num_scalers; i++)
  3691. skl_detach_scaler(crtc, i);
  3692. }
  3693. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3694. {
  3695. struct drm_device *dev = crtc->base.dev;
  3696. struct drm_i915_private *dev_priv = to_i915(dev);
  3697. int pipe = crtc->pipe;
  3698. struct intel_crtc_scaler_state *scaler_state =
  3699. &crtc->config->scaler_state;
  3700. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3701. if (crtc->config->pch_pfit.enabled) {
  3702. int id;
  3703. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3704. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3705. return;
  3706. }
  3707. id = scaler_state->scaler_id;
  3708. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3709. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3710. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3711. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3712. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3713. }
  3714. }
  3715. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3716. {
  3717. struct drm_device *dev = crtc->base.dev;
  3718. struct drm_i915_private *dev_priv = to_i915(dev);
  3719. int pipe = crtc->pipe;
  3720. if (crtc->config->pch_pfit.enabled) {
  3721. /* Force use of hard-coded filter coefficients
  3722. * as some pre-programmed values are broken,
  3723. * e.g. x201.
  3724. */
  3725. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3726. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3727. PF_PIPE_SEL_IVB(pipe));
  3728. else
  3729. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3730. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3731. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3732. }
  3733. }
  3734. void hsw_enable_ips(struct intel_crtc *crtc)
  3735. {
  3736. struct drm_device *dev = crtc->base.dev;
  3737. struct drm_i915_private *dev_priv = to_i915(dev);
  3738. if (!crtc->config->ips_enabled)
  3739. return;
  3740. /*
  3741. * We can only enable IPS after we enable a plane and wait for a vblank
  3742. * This function is called from post_plane_update, which is run after
  3743. * a vblank wait.
  3744. */
  3745. assert_plane_enabled(dev_priv, crtc->plane);
  3746. if (IS_BROADWELL(dev)) {
  3747. mutex_lock(&dev_priv->rps.hw_lock);
  3748. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3749. mutex_unlock(&dev_priv->rps.hw_lock);
  3750. /* Quoting Art Runyan: "its not safe to expect any particular
  3751. * value in IPS_CTL bit 31 after enabling IPS through the
  3752. * mailbox." Moreover, the mailbox may return a bogus state,
  3753. * so we need to just enable it and continue on.
  3754. */
  3755. } else {
  3756. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3757. /* The bit only becomes 1 in the next vblank, so this wait here
  3758. * is essentially intel_wait_for_vblank. If we don't have this
  3759. * and don't wait for vblanks until the end of crtc_enable, then
  3760. * the HW state readout code will complain that the expected
  3761. * IPS_CTL value is not the one we read. */
  3762. if (intel_wait_for_register(dev_priv,
  3763. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  3764. 50))
  3765. DRM_ERROR("Timed out waiting for IPS enable\n");
  3766. }
  3767. }
  3768. void hsw_disable_ips(struct intel_crtc *crtc)
  3769. {
  3770. struct drm_device *dev = crtc->base.dev;
  3771. struct drm_i915_private *dev_priv = to_i915(dev);
  3772. if (!crtc->config->ips_enabled)
  3773. return;
  3774. assert_plane_enabled(dev_priv, crtc->plane);
  3775. if (IS_BROADWELL(dev)) {
  3776. mutex_lock(&dev_priv->rps.hw_lock);
  3777. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3778. mutex_unlock(&dev_priv->rps.hw_lock);
  3779. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3780. if (intel_wait_for_register(dev_priv,
  3781. IPS_CTL, IPS_ENABLE, 0,
  3782. 42))
  3783. DRM_ERROR("Timed out waiting for IPS disable\n");
  3784. } else {
  3785. I915_WRITE(IPS_CTL, 0);
  3786. POSTING_READ(IPS_CTL);
  3787. }
  3788. /* We need to wait for a vblank before we can disable the plane. */
  3789. intel_wait_for_vblank(dev, crtc->pipe);
  3790. }
  3791. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3792. {
  3793. if (intel_crtc->overlay) {
  3794. struct drm_device *dev = intel_crtc->base.dev;
  3795. struct drm_i915_private *dev_priv = to_i915(dev);
  3796. mutex_lock(&dev->struct_mutex);
  3797. dev_priv->mm.interruptible = false;
  3798. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3799. dev_priv->mm.interruptible = true;
  3800. mutex_unlock(&dev->struct_mutex);
  3801. }
  3802. /* Let userspace switch the overlay on again. In most cases userspace
  3803. * has to recompute where to put it anyway.
  3804. */
  3805. }
  3806. /**
  3807. * intel_post_enable_primary - Perform operations after enabling primary plane
  3808. * @crtc: the CRTC whose primary plane was just enabled
  3809. *
  3810. * Performs potentially sleeping operations that must be done after the primary
  3811. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3812. * called due to an explicit primary plane update, or due to an implicit
  3813. * re-enable that is caused when a sprite plane is updated to no longer
  3814. * completely hide the primary plane.
  3815. */
  3816. static void
  3817. intel_post_enable_primary(struct drm_crtc *crtc)
  3818. {
  3819. struct drm_device *dev = crtc->dev;
  3820. struct drm_i915_private *dev_priv = to_i915(dev);
  3821. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3822. int pipe = intel_crtc->pipe;
  3823. /*
  3824. * FIXME IPS should be fine as long as one plane is
  3825. * enabled, but in practice it seems to have problems
  3826. * when going from primary only to sprite only and vice
  3827. * versa.
  3828. */
  3829. hsw_enable_ips(intel_crtc);
  3830. /*
  3831. * Gen2 reports pipe underruns whenever all planes are disabled.
  3832. * So don't enable underrun reporting before at least some planes
  3833. * are enabled.
  3834. * FIXME: Need to fix the logic to work when we turn off all planes
  3835. * but leave the pipe running.
  3836. */
  3837. if (IS_GEN2(dev))
  3838. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3839. /* Underruns don't always raise interrupts, so check manually. */
  3840. intel_check_cpu_fifo_underruns(dev_priv);
  3841. intel_check_pch_fifo_underruns(dev_priv);
  3842. }
  3843. /* FIXME move all this to pre_plane_update() with proper state tracking */
  3844. static void
  3845. intel_pre_disable_primary(struct drm_crtc *crtc)
  3846. {
  3847. struct drm_device *dev = crtc->dev;
  3848. struct drm_i915_private *dev_priv = to_i915(dev);
  3849. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3850. int pipe = intel_crtc->pipe;
  3851. /*
  3852. * Gen2 reports pipe underruns whenever all planes are disabled.
  3853. * So diasble underrun reporting before all the planes get disabled.
  3854. * FIXME: Need to fix the logic to work when we turn off all planes
  3855. * but leave the pipe running.
  3856. */
  3857. if (IS_GEN2(dev))
  3858. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3859. /*
  3860. * FIXME IPS should be fine as long as one plane is
  3861. * enabled, but in practice it seems to have problems
  3862. * when going from primary only to sprite only and vice
  3863. * versa.
  3864. */
  3865. hsw_disable_ips(intel_crtc);
  3866. }
  3867. /* FIXME get rid of this and use pre_plane_update */
  3868. static void
  3869. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  3870. {
  3871. struct drm_device *dev = crtc->dev;
  3872. struct drm_i915_private *dev_priv = to_i915(dev);
  3873. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3874. int pipe = intel_crtc->pipe;
  3875. intel_pre_disable_primary(crtc);
  3876. /*
  3877. * Vblank time updates from the shadow to live plane control register
  3878. * are blocked if the memory self-refresh mode is active at that
  3879. * moment. So to make sure the plane gets truly disabled, disable
  3880. * first the self-refresh mode. The self-refresh enable bit in turn
  3881. * will be checked/applied by the HW only at the next frame start
  3882. * event which is after the vblank start event, so we need to have a
  3883. * wait-for-vblank between disabling the plane and the pipe.
  3884. */
  3885. if (HAS_GMCH_DISPLAY(dev)) {
  3886. intel_set_memory_cxsr(dev_priv, false);
  3887. dev_priv->wm.vlv.cxsr = false;
  3888. intel_wait_for_vblank(dev, pipe);
  3889. }
  3890. }
  3891. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  3892. {
  3893. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3894. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3895. struct intel_crtc_state *pipe_config =
  3896. to_intel_crtc_state(crtc->base.state);
  3897. struct drm_device *dev = crtc->base.dev;
  3898. struct drm_plane *primary = crtc->base.primary;
  3899. struct drm_plane_state *old_pri_state =
  3900. drm_atomic_get_existing_plane_state(old_state, primary);
  3901. intel_frontbuffer_flip(dev, pipe_config->fb_bits);
  3902. crtc->wm.cxsr_allowed = true;
  3903. if (pipe_config->update_wm_post && pipe_config->base.active)
  3904. intel_update_watermarks(&crtc->base);
  3905. if (old_pri_state) {
  3906. struct intel_plane_state *primary_state =
  3907. to_intel_plane_state(primary->state);
  3908. struct intel_plane_state *old_primary_state =
  3909. to_intel_plane_state(old_pri_state);
  3910. intel_fbc_post_update(crtc);
  3911. if (primary_state->visible &&
  3912. (needs_modeset(&pipe_config->base) ||
  3913. !old_primary_state->visible))
  3914. intel_post_enable_primary(&crtc->base);
  3915. }
  3916. }
  3917. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  3918. {
  3919. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3920. struct drm_device *dev = crtc->base.dev;
  3921. struct drm_i915_private *dev_priv = to_i915(dev);
  3922. struct intel_crtc_state *pipe_config =
  3923. to_intel_crtc_state(crtc->base.state);
  3924. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3925. struct drm_plane *primary = crtc->base.primary;
  3926. struct drm_plane_state *old_pri_state =
  3927. drm_atomic_get_existing_plane_state(old_state, primary);
  3928. bool modeset = needs_modeset(&pipe_config->base);
  3929. if (old_pri_state) {
  3930. struct intel_plane_state *primary_state =
  3931. to_intel_plane_state(primary->state);
  3932. struct intel_plane_state *old_primary_state =
  3933. to_intel_plane_state(old_pri_state);
  3934. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  3935. if (old_primary_state->visible &&
  3936. (modeset || !primary_state->visible))
  3937. intel_pre_disable_primary(&crtc->base);
  3938. }
  3939. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
  3940. crtc->wm.cxsr_allowed = false;
  3941. /*
  3942. * Vblank time updates from the shadow to live plane control register
  3943. * are blocked if the memory self-refresh mode is active at that
  3944. * moment. So to make sure the plane gets truly disabled, disable
  3945. * first the self-refresh mode. The self-refresh enable bit in turn
  3946. * will be checked/applied by the HW only at the next frame start
  3947. * event which is after the vblank start event, so we need to have a
  3948. * wait-for-vblank between disabling the plane and the pipe.
  3949. */
  3950. if (old_crtc_state->base.active) {
  3951. intel_set_memory_cxsr(dev_priv, false);
  3952. dev_priv->wm.vlv.cxsr = false;
  3953. intel_wait_for_vblank(dev, crtc->pipe);
  3954. }
  3955. }
  3956. /*
  3957. * IVB workaround: must disable low power watermarks for at least
  3958. * one frame before enabling scaling. LP watermarks can be re-enabled
  3959. * when scaling is disabled.
  3960. *
  3961. * WaCxSRDisabledForSpriteScaling:ivb
  3962. */
  3963. if (pipe_config->disable_lp_wm) {
  3964. ilk_disable_lp_wm(dev);
  3965. intel_wait_for_vblank(dev, crtc->pipe);
  3966. }
  3967. /*
  3968. * If we're doing a modeset, we're done. No need to do any pre-vblank
  3969. * watermark programming here.
  3970. */
  3971. if (needs_modeset(&pipe_config->base))
  3972. return;
  3973. /*
  3974. * For platforms that support atomic watermarks, program the
  3975. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  3976. * will be the intermediate values that are safe for both pre- and
  3977. * post- vblank; when vblank happens, the 'active' values will be set
  3978. * to the final 'target' values and we'll do this again to get the
  3979. * optimal watermarks. For gen9+ platforms, the values we program here
  3980. * will be the final target values which will get automatically latched
  3981. * at vblank time; no further programming will be necessary.
  3982. *
  3983. * If a platform hasn't been transitioned to atomic watermarks yet,
  3984. * we'll continue to update watermarks the old way, if flags tell
  3985. * us to.
  3986. */
  3987. if (dev_priv->display.initial_watermarks != NULL)
  3988. dev_priv->display.initial_watermarks(pipe_config);
  3989. else if (pipe_config->update_wm_pre)
  3990. intel_update_watermarks(&crtc->base);
  3991. }
  3992. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  3993. {
  3994. struct drm_device *dev = crtc->dev;
  3995. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3996. struct drm_plane *p;
  3997. int pipe = intel_crtc->pipe;
  3998. intel_crtc_dpms_overlay_disable(intel_crtc);
  3999. drm_for_each_plane_mask(p, dev, plane_mask)
  4000. to_intel_plane(p)->disable_plane(p, crtc);
  4001. /*
  4002. * FIXME: Once we grow proper nuclear flip support out of this we need
  4003. * to compute the mask of flip planes precisely. For the time being
  4004. * consider this a flip to a NULL plane.
  4005. */
  4006. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4007. }
  4008. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4009. {
  4010. struct drm_device *dev = crtc->dev;
  4011. struct drm_i915_private *dev_priv = to_i915(dev);
  4012. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4013. struct intel_encoder *encoder;
  4014. int pipe = intel_crtc->pipe;
  4015. struct intel_crtc_state *pipe_config =
  4016. to_intel_crtc_state(crtc->state);
  4017. if (WARN_ON(intel_crtc->active))
  4018. return;
  4019. /*
  4020. * Sometimes spurious CPU pipe underruns happen during FDI
  4021. * training, at least with VGA+HDMI cloning. Suppress them.
  4022. *
  4023. * On ILK we get an occasional spurious CPU pipe underruns
  4024. * between eDP port A enable and vdd enable. Also PCH port
  4025. * enable seems to result in the occasional CPU pipe underrun.
  4026. *
  4027. * Spurious PCH underruns also occur during PCH enabling.
  4028. */
  4029. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4030. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4031. if (intel_crtc->config->has_pch_encoder)
  4032. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4033. if (intel_crtc->config->has_pch_encoder)
  4034. intel_prepare_shared_dpll(intel_crtc);
  4035. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4036. intel_dp_set_m_n(intel_crtc, M1_N1);
  4037. intel_set_pipe_timings(intel_crtc);
  4038. intel_set_pipe_src_size(intel_crtc);
  4039. if (intel_crtc->config->has_pch_encoder) {
  4040. intel_cpu_transcoder_set_m_n(intel_crtc,
  4041. &intel_crtc->config->fdi_m_n, NULL);
  4042. }
  4043. ironlake_set_pipeconf(crtc);
  4044. intel_crtc->active = true;
  4045. for_each_encoder_on_crtc(dev, crtc, encoder)
  4046. if (encoder->pre_enable)
  4047. encoder->pre_enable(encoder);
  4048. if (intel_crtc->config->has_pch_encoder) {
  4049. /* Note: FDI PLL enabling _must_ be done before we enable the
  4050. * cpu pipes, hence this is separate from all the other fdi/pch
  4051. * enabling. */
  4052. ironlake_fdi_pll_enable(intel_crtc);
  4053. } else {
  4054. assert_fdi_tx_disabled(dev_priv, pipe);
  4055. assert_fdi_rx_disabled(dev_priv, pipe);
  4056. }
  4057. ironlake_pfit_enable(intel_crtc);
  4058. /*
  4059. * On ILK+ LUT must be loaded before the pipe is running but with
  4060. * clocks enabled
  4061. */
  4062. intel_color_load_luts(&pipe_config->base);
  4063. if (dev_priv->display.initial_watermarks != NULL)
  4064. dev_priv->display.initial_watermarks(intel_crtc->config);
  4065. intel_enable_pipe(intel_crtc);
  4066. if (intel_crtc->config->has_pch_encoder)
  4067. ironlake_pch_enable(crtc);
  4068. assert_vblank_disabled(crtc);
  4069. drm_crtc_vblank_on(crtc);
  4070. for_each_encoder_on_crtc(dev, crtc, encoder)
  4071. encoder->enable(encoder);
  4072. if (HAS_PCH_CPT(dev))
  4073. cpt_verify_modeset(dev, intel_crtc->pipe);
  4074. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4075. if (intel_crtc->config->has_pch_encoder)
  4076. intel_wait_for_vblank(dev, pipe);
  4077. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4078. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4079. }
  4080. /* IPS only exists on ULT machines and is tied to pipe A. */
  4081. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4082. {
  4083. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4084. }
  4085. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4086. {
  4087. struct drm_device *dev = crtc->dev;
  4088. struct drm_i915_private *dev_priv = to_i915(dev);
  4089. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4090. struct intel_encoder *encoder;
  4091. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4092. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4093. struct intel_crtc_state *pipe_config =
  4094. to_intel_crtc_state(crtc->state);
  4095. if (WARN_ON(intel_crtc->active))
  4096. return;
  4097. if (intel_crtc->config->has_pch_encoder)
  4098. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4099. false);
  4100. for_each_encoder_on_crtc(dev, crtc, encoder)
  4101. if (encoder->pre_pll_enable)
  4102. encoder->pre_pll_enable(encoder);
  4103. if (intel_crtc->config->shared_dpll)
  4104. intel_enable_shared_dpll(intel_crtc);
  4105. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4106. intel_dp_set_m_n(intel_crtc, M1_N1);
  4107. if (!transcoder_is_dsi(cpu_transcoder))
  4108. intel_set_pipe_timings(intel_crtc);
  4109. intel_set_pipe_src_size(intel_crtc);
  4110. if (cpu_transcoder != TRANSCODER_EDP &&
  4111. !transcoder_is_dsi(cpu_transcoder)) {
  4112. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4113. intel_crtc->config->pixel_multiplier - 1);
  4114. }
  4115. if (intel_crtc->config->has_pch_encoder) {
  4116. intel_cpu_transcoder_set_m_n(intel_crtc,
  4117. &intel_crtc->config->fdi_m_n, NULL);
  4118. }
  4119. if (!transcoder_is_dsi(cpu_transcoder))
  4120. haswell_set_pipeconf(crtc);
  4121. haswell_set_pipemisc(crtc);
  4122. intel_color_set_csc(&pipe_config->base);
  4123. intel_crtc->active = true;
  4124. if (intel_crtc->config->has_pch_encoder)
  4125. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4126. else
  4127. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4128. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4129. if (encoder->pre_enable)
  4130. encoder->pre_enable(encoder);
  4131. }
  4132. if (intel_crtc->config->has_pch_encoder)
  4133. dev_priv->display.fdi_link_train(crtc);
  4134. if (!transcoder_is_dsi(cpu_transcoder))
  4135. intel_ddi_enable_pipe_clock(intel_crtc);
  4136. if (INTEL_INFO(dev)->gen >= 9)
  4137. skylake_pfit_enable(intel_crtc);
  4138. else
  4139. ironlake_pfit_enable(intel_crtc);
  4140. /*
  4141. * On ILK+ LUT must be loaded before the pipe is running but with
  4142. * clocks enabled
  4143. */
  4144. intel_color_load_luts(&pipe_config->base);
  4145. intel_ddi_set_pipe_settings(crtc);
  4146. if (!transcoder_is_dsi(cpu_transcoder))
  4147. intel_ddi_enable_transcoder_func(crtc);
  4148. if (dev_priv->display.initial_watermarks != NULL)
  4149. dev_priv->display.initial_watermarks(pipe_config);
  4150. else
  4151. intel_update_watermarks(crtc);
  4152. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4153. if (!transcoder_is_dsi(cpu_transcoder))
  4154. intel_enable_pipe(intel_crtc);
  4155. if (intel_crtc->config->has_pch_encoder)
  4156. lpt_pch_enable(crtc);
  4157. if (intel_crtc->config->dp_encoder_is_mst)
  4158. intel_ddi_set_vc_payload_alloc(crtc, true);
  4159. assert_vblank_disabled(crtc);
  4160. drm_crtc_vblank_on(crtc);
  4161. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4162. encoder->enable(encoder);
  4163. intel_opregion_notify_encoder(encoder, true);
  4164. }
  4165. if (intel_crtc->config->has_pch_encoder) {
  4166. intel_wait_for_vblank(dev, pipe);
  4167. intel_wait_for_vblank(dev, pipe);
  4168. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4169. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4170. true);
  4171. }
  4172. /* If we change the relative order between pipe/planes enabling, we need
  4173. * to change the workaround. */
  4174. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4175. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4176. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4177. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4178. }
  4179. }
  4180. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4181. {
  4182. struct drm_device *dev = crtc->base.dev;
  4183. struct drm_i915_private *dev_priv = to_i915(dev);
  4184. int pipe = crtc->pipe;
  4185. /* To avoid upsetting the power well on haswell only disable the pfit if
  4186. * it's in use. The hw state code will make sure we get this right. */
  4187. if (force || crtc->config->pch_pfit.enabled) {
  4188. I915_WRITE(PF_CTL(pipe), 0);
  4189. I915_WRITE(PF_WIN_POS(pipe), 0);
  4190. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4191. }
  4192. }
  4193. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4194. {
  4195. struct drm_device *dev = crtc->dev;
  4196. struct drm_i915_private *dev_priv = to_i915(dev);
  4197. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4198. struct intel_encoder *encoder;
  4199. int pipe = intel_crtc->pipe;
  4200. /*
  4201. * Sometimes spurious CPU pipe underruns happen when the
  4202. * pipe is already disabled, but FDI RX/TX is still enabled.
  4203. * Happens at least with VGA+HDMI cloning. Suppress them.
  4204. */
  4205. if (intel_crtc->config->has_pch_encoder) {
  4206. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4207. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4208. }
  4209. for_each_encoder_on_crtc(dev, crtc, encoder)
  4210. encoder->disable(encoder);
  4211. drm_crtc_vblank_off(crtc);
  4212. assert_vblank_disabled(crtc);
  4213. intel_disable_pipe(intel_crtc);
  4214. ironlake_pfit_disable(intel_crtc, false);
  4215. if (intel_crtc->config->has_pch_encoder)
  4216. ironlake_fdi_disable(crtc);
  4217. for_each_encoder_on_crtc(dev, crtc, encoder)
  4218. if (encoder->post_disable)
  4219. encoder->post_disable(encoder);
  4220. if (intel_crtc->config->has_pch_encoder) {
  4221. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4222. if (HAS_PCH_CPT(dev)) {
  4223. i915_reg_t reg;
  4224. u32 temp;
  4225. /* disable TRANS_DP_CTL */
  4226. reg = TRANS_DP_CTL(pipe);
  4227. temp = I915_READ(reg);
  4228. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4229. TRANS_DP_PORT_SEL_MASK);
  4230. temp |= TRANS_DP_PORT_SEL_NONE;
  4231. I915_WRITE(reg, temp);
  4232. /* disable DPLL_SEL */
  4233. temp = I915_READ(PCH_DPLL_SEL);
  4234. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4235. I915_WRITE(PCH_DPLL_SEL, temp);
  4236. }
  4237. ironlake_fdi_pll_disable(intel_crtc);
  4238. }
  4239. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4240. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4241. }
  4242. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4243. {
  4244. struct drm_device *dev = crtc->dev;
  4245. struct drm_i915_private *dev_priv = to_i915(dev);
  4246. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4247. struct intel_encoder *encoder;
  4248. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4249. if (intel_crtc->config->has_pch_encoder)
  4250. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4251. false);
  4252. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4253. intel_opregion_notify_encoder(encoder, false);
  4254. encoder->disable(encoder);
  4255. }
  4256. drm_crtc_vblank_off(crtc);
  4257. assert_vblank_disabled(crtc);
  4258. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4259. if (!transcoder_is_dsi(cpu_transcoder))
  4260. intel_disable_pipe(intel_crtc);
  4261. if (intel_crtc->config->dp_encoder_is_mst)
  4262. intel_ddi_set_vc_payload_alloc(crtc, false);
  4263. if (!transcoder_is_dsi(cpu_transcoder))
  4264. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4265. if (INTEL_INFO(dev)->gen >= 9)
  4266. skylake_scaler_disable(intel_crtc);
  4267. else
  4268. ironlake_pfit_disable(intel_crtc, false);
  4269. if (!transcoder_is_dsi(cpu_transcoder))
  4270. intel_ddi_disable_pipe_clock(intel_crtc);
  4271. for_each_encoder_on_crtc(dev, crtc, encoder)
  4272. if (encoder->post_disable)
  4273. encoder->post_disable(encoder);
  4274. if (intel_crtc->config->has_pch_encoder) {
  4275. lpt_disable_pch_transcoder(dev_priv);
  4276. lpt_disable_iclkip(dev_priv);
  4277. intel_ddi_fdi_disable(crtc);
  4278. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4279. true);
  4280. }
  4281. }
  4282. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4283. {
  4284. struct drm_device *dev = crtc->base.dev;
  4285. struct drm_i915_private *dev_priv = to_i915(dev);
  4286. struct intel_crtc_state *pipe_config = crtc->config;
  4287. if (!pipe_config->gmch_pfit.control)
  4288. return;
  4289. /*
  4290. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4291. * according to register description and PRM.
  4292. */
  4293. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4294. assert_pipe_disabled(dev_priv, crtc->pipe);
  4295. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4296. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4297. /* Border color in case we don't scale up to the full screen. Black by
  4298. * default, change to something else for debugging. */
  4299. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4300. }
  4301. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4302. {
  4303. switch (port) {
  4304. case PORT_A:
  4305. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4306. case PORT_B:
  4307. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4308. case PORT_C:
  4309. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4310. case PORT_D:
  4311. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4312. case PORT_E:
  4313. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4314. default:
  4315. MISSING_CASE(port);
  4316. return POWER_DOMAIN_PORT_OTHER;
  4317. }
  4318. }
  4319. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4320. {
  4321. switch (port) {
  4322. case PORT_A:
  4323. return POWER_DOMAIN_AUX_A;
  4324. case PORT_B:
  4325. return POWER_DOMAIN_AUX_B;
  4326. case PORT_C:
  4327. return POWER_DOMAIN_AUX_C;
  4328. case PORT_D:
  4329. return POWER_DOMAIN_AUX_D;
  4330. case PORT_E:
  4331. /* FIXME: Check VBT for actual wiring of PORT E */
  4332. return POWER_DOMAIN_AUX_D;
  4333. default:
  4334. MISSING_CASE(port);
  4335. return POWER_DOMAIN_AUX_A;
  4336. }
  4337. }
  4338. enum intel_display_power_domain
  4339. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4340. {
  4341. struct drm_device *dev = intel_encoder->base.dev;
  4342. struct intel_digital_port *intel_dig_port;
  4343. switch (intel_encoder->type) {
  4344. case INTEL_OUTPUT_UNKNOWN:
  4345. /* Only DDI platforms should ever use this output type */
  4346. WARN_ON_ONCE(!HAS_DDI(dev));
  4347. case INTEL_OUTPUT_DP:
  4348. case INTEL_OUTPUT_HDMI:
  4349. case INTEL_OUTPUT_EDP:
  4350. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4351. return port_to_power_domain(intel_dig_port->port);
  4352. case INTEL_OUTPUT_DP_MST:
  4353. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4354. return port_to_power_domain(intel_dig_port->port);
  4355. case INTEL_OUTPUT_ANALOG:
  4356. return POWER_DOMAIN_PORT_CRT;
  4357. case INTEL_OUTPUT_DSI:
  4358. return POWER_DOMAIN_PORT_DSI;
  4359. default:
  4360. return POWER_DOMAIN_PORT_OTHER;
  4361. }
  4362. }
  4363. enum intel_display_power_domain
  4364. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4365. {
  4366. struct drm_device *dev = intel_encoder->base.dev;
  4367. struct intel_digital_port *intel_dig_port;
  4368. switch (intel_encoder->type) {
  4369. case INTEL_OUTPUT_UNKNOWN:
  4370. case INTEL_OUTPUT_HDMI:
  4371. /*
  4372. * Only DDI platforms should ever use these output types.
  4373. * We can get here after the HDMI detect code has already set
  4374. * the type of the shared encoder. Since we can't be sure
  4375. * what's the status of the given connectors, play safe and
  4376. * run the DP detection too.
  4377. */
  4378. WARN_ON_ONCE(!HAS_DDI(dev));
  4379. case INTEL_OUTPUT_DP:
  4380. case INTEL_OUTPUT_EDP:
  4381. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4382. return port_to_aux_power_domain(intel_dig_port->port);
  4383. case INTEL_OUTPUT_DP_MST:
  4384. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4385. return port_to_aux_power_domain(intel_dig_port->port);
  4386. default:
  4387. MISSING_CASE(intel_encoder->type);
  4388. return POWER_DOMAIN_AUX_A;
  4389. }
  4390. }
  4391. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4392. struct intel_crtc_state *crtc_state)
  4393. {
  4394. struct drm_device *dev = crtc->dev;
  4395. struct drm_encoder *encoder;
  4396. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4397. enum pipe pipe = intel_crtc->pipe;
  4398. unsigned long mask;
  4399. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4400. if (!crtc_state->base.active)
  4401. return 0;
  4402. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4403. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4404. if (crtc_state->pch_pfit.enabled ||
  4405. crtc_state->pch_pfit.force_thru)
  4406. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4407. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4408. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4409. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4410. }
  4411. if (crtc_state->shared_dpll)
  4412. mask |= BIT(POWER_DOMAIN_PLLS);
  4413. return mask;
  4414. }
  4415. static unsigned long
  4416. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4417. struct intel_crtc_state *crtc_state)
  4418. {
  4419. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4420. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4421. enum intel_display_power_domain domain;
  4422. unsigned long domains, new_domains, old_domains;
  4423. old_domains = intel_crtc->enabled_power_domains;
  4424. intel_crtc->enabled_power_domains = new_domains =
  4425. get_crtc_power_domains(crtc, crtc_state);
  4426. domains = new_domains & ~old_domains;
  4427. for_each_power_domain(domain, domains)
  4428. intel_display_power_get(dev_priv, domain);
  4429. return old_domains & ~new_domains;
  4430. }
  4431. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4432. unsigned long domains)
  4433. {
  4434. enum intel_display_power_domain domain;
  4435. for_each_power_domain(domain, domains)
  4436. intel_display_power_put(dev_priv, domain);
  4437. }
  4438. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4439. {
  4440. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4441. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4442. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4443. return max_cdclk_freq;
  4444. else if (IS_CHERRYVIEW(dev_priv))
  4445. return max_cdclk_freq*95/100;
  4446. else if (INTEL_INFO(dev_priv)->gen < 4)
  4447. return 2*max_cdclk_freq*90/100;
  4448. else
  4449. return max_cdclk_freq*90/100;
  4450. }
  4451. static int skl_calc_cdclk(int max_pixclk, int vco);
  4452. static void intel_update_max_cdclk(struct drm_device *dev)
  4453. {
  4454. struct drm_i915_private *dev_priv = to_i915(dev);
  4455. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4456. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4457. int max_cdclk, vco;
  4458. vco = dev_priv->skl_preferred_vco_freq;
  4459. WARN_ON(vco != 8100000 && vco != 8640000);
  4460. /*
  4461. * Use the lower (vco 8640) cdclk values as a
  4462. * first guess. skl_calc_cdclk() will correct it
  4463. * if the preferred vco is 8100 instead.
  4464. */
  4465. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4466. max_cdclk = 617143;
  4467. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4468. max_cdclk = 540000;
  4469. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4470. max_cdclk = 432000;
  4471. else
  4472. max_cdclk = 308571;
  4473. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4474. } else if (IS_BROXTON(dev)) {
  4475. dev_priv->max_cdclk_freq = 624000;
  4476. } else if (IS_BROADWELL(dev)) {
  4477. /*
  4478. * FIXME with extra cooling we can allow
  4479. * 540 MHz for ULX and 675 Mhz for ULT.
  4480. * How can we know if extra cooling is
  4481. * available? PCI ID, VTB, something else?
  4482. */
  4483. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4484. dev_priv->max_cdclk_freq = 450000;
  4485. else if (IS_BDW_ULX(dev))
  4486. dev_priv->max_cdclk_freq = 450000;
  4487. else if (IS_BDW_ULT(dev))
  4488. dev_priv->max_cdclk_freq = 540000;
  4489. else
  4490. dev_priv->max_cdclk_freq = 675000;
  4491. } else if (IS_CHERRYVIEW(dev)) {
  4492. dev_priv->max_cdclk_freq = 320000;
  4493. } else if (IS_VALLEYVIEW(dev)) {
  4494. dev_priv->max_cdclk_freq = 400000;
  4495. } else {
  4496. /* otherwise assume cdclk is fixed */
  4497. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4498. }
  4499. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4500. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4501. dev_priv->max_cdclk_freq);
  4502. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4503. dev_priv->max_dotclk_freq);
  4504. }
  4505. static void intel_update_cdclk(struct drm_device *dev)
  4506. {
  4507. struct drm_i915_private *dev_priv = to_i915(dev);
  4508. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4509. if (INTEL_GEN(dev_priv) >= 9)
  4510. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  4511. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  4512. dev_priv->cdclk_pll.ref);
  4513. else
  4514. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4515. dev_priv->cdclk_freq);
  4516. /*
  4517. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4518. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4519. * of cdclk that generates 4MHz reference clock freq which is used to
  4520. * generate GMBus clock. This will vary with the cdclk freq.
  4521. */
  4522. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4523. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4524. }
  4525. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4526. static int skl_cdclk_decimal(int cdclk)
  4527. {
  4528. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  4529. }
  4530. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  4531. {
  4532. int ratio;
  4533. if (cdclk == dev_priv->cdclk_pll.ref)
  4534. return 0;
  4535. switch (cdclk) {
  4536. default:
  4537. MISSING_CASE(cdclk);
  4538. case 144000:
  4539. case 288000:
  4540. case 384000:
  4541. case 576000:
  4542. ratio = 60;
  4543. break;
  4544. case 624000:
  4545. ratio = 65;
  4546. break;
  4547. }
  4548. return dev_priv->cdclk_pll.ref * ratio;
  4549. }
  4550. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  4551. {
  4552. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  4553. /* Timeout 200us */
  4554. if (intel_wait_for_register(dev_priv,
  4555. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  4556. 1))
  4557. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  4558. dev_priv->cdclk_pll.vco = 0;
  4559. }
  4560. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  4561. {
  4562. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  4563. u32 val;
  4564. val = I915_READ(BXT_DE_PLL_CTL);
  4565. val &= ~BXT_DE_PLL_RATIO_MASK;
  4566. val |= BXT_DE_PLL_RATIO(ratio);
  4567. I915_WRITE(BXT_DE_PLL_CTL, val);
  4568. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4569. /* Timeout 200us */
  4570. if (intel_wait_for_register(dev_priv,
  4571. BXT_DE_PLL_ENABLE,
  4572. BXT_DE_PLL_LOCK,
  4573. BXT_DE_PLL_LOCK,
  4574. 1))
  4575. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4576. dev_priv->cdclk_pll.vco = vco;
  4577. }
  4578. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  4579. {
  4580. u32 val, divider;
  4581. int vco, ret;
  4582. vco = bxt_de_pll_vco(dev_priv, cdclk);
  4583. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  4584. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  4585. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  4586. case 8:
  4587. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4588. break;
  4589. case 4:
  4590. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4591. break;
  4592. case 3:
  4593. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4594. break;
  4595. case 2:
  4596. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4597. break;
  4598. default:
  4599. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  4600. WARN_ON(vco != 0);
  4601. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4602. break;
  4603. }
  4604. /* Inform power controller of upcoming frequency change */
  4605. mutex_lock(&dev_priv->rps.hw_lock);
  4606. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4607. 0x80000000);
  4608. mutex_unlock(&dev_priv->rps.hw_lock);
  4609. if (ret) {
  4610. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4611. ret, cdclk);
  4612. return;
  4613. }
  4614. if (dev_priv->cdclk_pll.vco != 0 &&
  4615. dev_priv->cdclk_pll.vco != vco)
  4616. bxt_de_pll_disable(dev_priv);
  4617. if (dev_priv->cdclk_pll.vco != vco)
  4618. bxt_de_pll_enable(dev_priv, vco);
  4619. val = divider | skl_cdclk_decimal(cdclk);
  4620. /*
  4621. * FIXME if only the cd2x divider needs changing, it could be done
  4622. * without shutting off the pipe (if only one pipe is active).
  4623. */
  4624. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  4625. /*
  4626. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4627. * enable otherwise.
  4628. */
  4629. if (cdclk >= 500000)
  4630. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4631. I915_WRITE(CDCLK_CTL, val);
  4632. mutex_lock(&dev_priv->rps.hw_lock);
  4633. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4634. DIV_ROUND_UP(cdclk, 25000));
  4635. mutex_unlock(&dev_priv->rps.hw_lock);
  4636. if (ret) {
  4637. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4638. ret, cdclk);
  4639. return;
  4640. }
  4641. intel_update_cdclk(&dev_priv->drm);
  4642. }
  4643. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4644. {
  4645. u32 cdctl, expected;
  4646. intel_update_cdclk(&dev_priv->drm);
  4647. if (dev_priv->cdclk_pll.vco == 0 ||
  4648. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  4649. goto sanitize;
  4650. /* DPLL okay; verify the cdclock
  4651. *
  4652. * Some BIOS versions leave an incorrect decimal frequency value and
  4653. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  4654. * so sanitize this register.
  4655. */
  4656. cdctl = I915_READ(CDCLK_CTL);
  4657. /*
  4658. * Let's ignore the pipe field, since BIOS could have configured the
  4659. * dividers both synching to an active pipe, or asynchronously
  4660. * (PIPE_NONE).
  4661. */
  4662. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  4663. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  4664. skl_cdclk_decimal(dev_priv->cdclk_freq);
  4665. /*
  4666. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4667. * enable otherwise.
  4668. */
  4669. if (dev_priv->cdclk_freq >= 500000)
  4670. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4671. if (cdctl == expected)
  4672. /* All well; nothing to sanitize */
  4673. return;
  4674. sanitize:
  4675. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  4676. /* force cdclk programming */
  4677. dev_priv->cdclk_freq = 0;
  4678. /* force full PLL disable + enable */
  4679. dev_priv->cdclk_pll.vco = -1;
  4680. }
  4681. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  4682. {
  4683. bxt_sanitize_cdclk(dev_priv);
  4684. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  4685. return;
  4686. /*
  4687. * FIXME:
  4688. * - The initial CDCLK needs to be read from VBT.
  4689. * Need to make this change after VBT has changes for BXT.
  4690. */
  4691. bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
  4692. }
  4693. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  4694. {
  4695. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  4696. }
  4697. static int skl_calc_cdclk(int max_pixclk, int vco)
  4698. {
  4699. if (vco == 8640000) {
  4700. if (max_pixclk > 540000)
  4701. return 617143;
  4702. else if (max_pixclk > 432000)
  4703. return 540000;
  4704. else if (max_pixclk > 308571)
  4705. return 432000;
  4706. else
  4707. return 308571;
  4708. } else {
  4709. if (max_pixclk > 540000)
  4710. return 675000;
  4711. else if (max_pixclk > 450000)
  4712. return 540000;
  4713. else if (max_pixclk > 337500)
  4714. return 450000;
  4715. else
  4716. return 337500;
  4717. }
  4718. }
  4719. static void
  4720. skl_dpll0_update(struct drm_i915_private *dev_priv)
  4721. {
  4722. u32 val;
  4723. dev_priv->cdclk_pll.ref = 24000;
  4724. dev_priv->cdclk_pll.vco = 0;
  4725. val = I915_READ(LCPLL1_CTL);
  4726. if ((val & LCPLL_PLL_ENABLE) == 0)
  4727. return;
  4728. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  4729. return;
  4730. val = I915_READ(DPLL_CTRL1);
  4731. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  4732. DPLL_CTRL1_SSC(SKL_DPLL0) |
  4733. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  4734. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  4735. return;
  4736. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  4737. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  4738. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  4739. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  4740. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  4741. dev_priv->cdclk_pll.vco = 8100000;
  4742. break;
  4743. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  4744. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  4745. dev_priv->cdclk_pll.vco = 8640000;
  4746. break;
  4747. default:
  4748. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4749. break;
  4750. }
  4751. }
  4752. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  4753. {
  4754. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  4755. dev_priv->skl_preferred_vco_freq = vco;
  4756. if (changed)
  4757. intel_update_max_cdclk(&dev_priv->drm);
  4758. }
  4759. static void
  4760. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  4761. {
  4762. int min_cdclk = skl_calc_cdclk(0, vco);
  4763. u32 val;
  4764. WARN_ON(vco != 8100000 && vco != 8640000);
  4765. /* select the minimum CDCLK before enabling DPLL 0 */
  4766. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  4767. I915_WRITE(CDCLK_CTL, val);
  4768. POSTING_READ(CDCLK_CTL);
  4769. /*
  4770. * We always enable DPLL0 with the lowest link rate possible, but still
  4771. * taking into account the VCO required to operate the eDP panel at the
  4772. * desired frequency. The usual DP link rates operate with a VCO of
  4773. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4774. * The modeset code is responsible for the selection of the exact link
  4775. * rate later on, with the constraint of choosing a frequency that
  4776. * works with vco.
  4777. */
  4778. val = I915_READ(DPLL_CTRL1);
  4779. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4780. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4781. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4782. if (vco == 8640000)
  4783. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4784. SKL_DPLL0);
  4785. else
  4786. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4787. SKL_DPLL0);
  4788. I915_WRITE(DPLL_CTRL1, val);
  4789. POSTING_READ(DPLL_CTRL1);
  4790. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4791. if (intel_wait_for_register(dev_priv,
  4792. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  4793. 5))
  4794. DRM_ERROR("DPLL0 not locked\n");
  4795. dev_priv->cdclk_pll.vco = vco;
  4796. /* We'll want to keep using the current vco from now on. */
  4797. skl_set_preferred_cdclk_vco(dev_priv, vco);
  4798. }
  4799. static void
  4800. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  4801. {
  4802. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4803. if (intel_wait_for_register(dev_priv,
  4804. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  4805. 1))
  4806. DRM_ERROR("Couldn't disable DPLL0\n");
  4807. dev_priv->cdclk_pll.vco = 0;
  4808. }
  4809. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4810. {
  4811. int ret;
  4812. u32 val;
  4813. /* inform PCU we want to change CDCLK */
  4814. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4815. mutex_lock(&dev_priv->rps.hw_lock);
  4816. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4817. mutex_unlock(&dev_priv->rps.hw_lock);
  4818. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4819. }
  4820. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4821. {
  4822. unsigned int i;
  4823. for (i = 0; i < 15; i++) {
  4824. if (skl_cdclk_pcu_ready(dev_priv))
  4825. return true;
  4826. udelay(10);
  4827. }
  4828. return false;
  4829. }
  4830. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  4831. {
  4832. struct drm_device *dev = &dev_priv->drm;
  4833. u32 freq_select, pcu_ack;
  4834. WARN_ON((cdclk == 24000) != (vco == 0));
  4835. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  4836. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4837. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4838. return;
  4839. }
  4840. /* set CDCLK_CTL */
  4841. switch (cdclk) {
  4842. case 450000:
  4843. case 432000:
  4844. freq_select = CDCLK_FREQ_450_432;
  4845. pcu_ack = 1;
  4846. break;
  4847. case 540000:
  4848. freq_select = CDCLK_FREQ_540;
  4849. pcu_ack = 2;
  4850. break;
  4851. case 308571:
  4852. case 337500:
  4853. default:
  4854. freq_select = CDCLK_FREQ_337_308;
  4855. pcu_ack = 0;
  4856. break;
  4857. case 617143:
  4858. case 675000:
  4859. freq_select = CDCLK_FREQ_675_617;
  4860. pcu_ack = 3;
  4861. break;
  4862. }
  4863. if (dev_priv->cdclk_pll.vco != 0 &&
  4864. dev_priv->cdclk_pll.vco != vco)
  4865. skl_dpll0_disable(dev_priv);
  4866. if (dev_priv->cdclk_pll.vco != vco)
  4867. skl_dpll0_enable(dev_priv, vco);
  4868. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  4869. POSTING_READ(CDCLK_CTL);
  4870. /* inform PCU of the change */
  4871. mutex_lock(&dev_priv->rps.hw_lock);
  4872. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4873. mutex_unlock(&dev_priv->rps.hw_lock);
  4874. intel_update_cdclk(dev);
  4875. }
  4876. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  4877. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4878. {
  4879. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  4880. }
  4881. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4882. {
  4883. int cdclk, vco;
  4884. skl_sanitize_cdclk(dev_priv);
  4885. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  4886. /*
  4887. * Use the current vco as our initial
  4888. * guess as to what the preferred vco is.
  4889. */
  4890. if (dev_priv->skl_preferred_vco_freq == 0)
  4891. skl_set_preferred_cdclk_vco(dev_priv,
  4892. dev_priv->cdclk_pll.vco);
  4893. return;
  4894. }
  4895. vco = dev_priv->skl_preferred_vco_freq;
  4896. if (vco == 0)
  4897. vco = 8100000;
  4898. cdclk = skl_calc_cdclk(0, vco);
  4899. skl_set_cdclk(dev_priv, cdclk, vco);
  4900. }
  4901. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4902. {
  4903. uint32_t cdctl, expected;
  4904. /*
  4905. * check if the pre-os intialized the display
  4906. * There is SWF18 scratchpad register defined which is set by the
  4907. * pre-os which can be used by the OS drivers to check the status
  4908. */
  4909. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4910. goto sanitize;
  4911. intel_update_cdclk(&dev_priv->drm);
  4912. /* Is PLL enabled and locked ? */
  4913. if (dev_priv->cdclk_pll.vco == 0 ||
  4914. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  4915. goto sanitize;
  4916. /* DPLL okay; verify the cdclock
  4917. *
  4918. * Noticed in some instances that the freq selection is correct but
  4919. * decimal part is programmed wrong from BIOS where pre-os does not
  4920. * enable display. Verify the same as well.
  4921. */
  4922. cdctl = I915_READ(CDCLK_CTL);
  4923. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  4924. skl_cdclk_decimal(dev_priv->cdclk_freq);
  4925. if (cdctl == expected)
  4926. /* All well; nothing to sanitize */
  4927. return;
  4928. sanitize:
  4929. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  4930. /* force cdclk programming */
  4931. dev_priv->cdclk_freq = 0;
  4932. /* force full PLL disable + enable */
  4933. dev_priv->cdclk_pll.vco = -1;
  4934. }
  4935. /* Adjust CDclk dividers to allow high res or save power if possible */
  4936. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4937. {
  4938. struct drm_i915_private *dev_priv = to_i915(dev);
  4939. u32 val, cmd;
  4940. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4941. != dev_priv->cdclk_freq);
  4942. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4943. cmd = 2;
  4944. else if (cdclk == 266667)
  4945. cmd = 1;
  4946. else
  4947. cmd = 0;
  4948. mutex_lock(&dev_priv->rps.hw_lock);
  4949. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4950. val &= ~DSPFREQGUAR_MASK;
  4951. val |= (cmd << DSPFREQGUAR_SHIFT);
  4952. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4953. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4954. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4955. 50)) {
  4956. DRM_ERROR("timed out waiting for CDclk change\n");
  4957. }
  4958. mutex_unlock(&dev_priv->rps.hw_lock);
  4959. mutex_lock(&dev_priv->sb_lock);
  4960. if (cdclk == 400000) {
  4961. u32 divider;
  4962. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4963. /* adjust cdclk divider */
  4964. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4965. val &= ~CCK_FREQUENCY_VALUES;
  4966. val |= divider;
  4967. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4968. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4969. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4970. 50))
  4971. DRM_ERROR("timed out waiting for CDclk change\n");
  4972. }
  4973. /* adjust self-refresh exit latency value */
  4974. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4975. val &= ~0x7f;
  4976. /*
  4977. * For high bandwidth configs, we set a higher latency in the bunit
  4978. * so that the core display fetch happens in time to avoid underruns.
  4979. */
  4980. if (cdclk == 400000)
  4981. val |= 4500 / 250; /* 4.5 usec */
  4982. else
  4983. val |= 3000 / 250; /* 3.0 usec */
  4984. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4985. mutex_unlock(&dev_priv->sb_lock);
  4986. intel_update_cdclk(dev);
  4987. }
  4988. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4989. {
  4990. struct drm_i915_private *dev_priv = to_i915(dev);
  4991. u32 val, cmd;
  4992. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4993. != dev_priv->cdclk_freq);
  4994. switch (cdclk) {
  4995. case 333333:
  4996. case 320000:
  4997. case 266667:
  4998. case 200000:
  4999. break;
  5000. default:
  5001. MISSING_CASE(cdclk);
  5002. return;
  5003. }
  5004. /*
  5005. * Specs are full of misinformation, but testing on actual
  5006. * hardware has shown that we just need to write the desired
  5007. * CCK divider into the Punit register.
  5008. */
  5009. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5010. mutex_lock(&dev_priv->rps.hw_lock);
  5011. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5012. val &= ~DSPFREQGUAR_MASK_CHV;
  5013. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5014. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5015. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5016. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5017. 50)) {
  5018. DRM_ERROR("timed out waiting for CDclk change\n");
  5019. }
  5020. mutex_unlock(&dev_priv->rps.hw_lock);
  5021. intel_update_cdclk(dev);
  5022. }
  5023. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5024. int max_pixclk)
  5025. {
  5026. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5027. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5028. /*
  5029. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5030. * 200MHz
  5031. * 267MHz
  5032. * 320/333MHz (depends on HPLL freq)
  5033. * 400MHz (VLV only)
  5034. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5035. * of the lower bin and adjust if needed.
  5036. *
  5037. * We seem to get an unstable or solid color picture at 200MHz.
  5038. * Not sure what's wrong. For now use 200MHz only when all pipes
  5039. * are off.
  5040. */
  5041. if (!IS_CHERRYVIEW(dev_priv) &&
  5042. max_pixclk > freq_320*limit/100)
  5043. return 400000;
  5044. else if (max_pixclk > 266667*limit/100)
  5045. return freq_320;
  5046. else if (max_pixclk > 0)
  5047. return 266667;
  5048. else
  5049. return 200000;
  5050. }
  5051. static int bxt_calc_cdclk(int max_pixclk)
  5052. {
  5053. if (max_pixclk > 576000)
  5054. return 624000;
  5055. else if (max_pixclk > 384000)
  5056. return 576000;
  5057. else if (max_pixclk > 288000)
  5058. return 384000;
  5059. else if (max_pixclk > 144000)
  5060. return 288000;
  5061. else
  5062. return 144000;
  5063. }
  5064. /* Compute the max pixel clock for new configuration. */
  5065. static int intel_mode_max_pixclk(struct drm_device *dev,
  5066. struct drm_atomic_state *state)
  5067. {
  5068. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5069. struct drm_i915_private *dev_priv = to_i915(dev);
  5070. struct drm_crtc *crtc;
  5071. struct drm_crtc_state *crtc_state;
  5072. unsigned max_pixclk = 0, i;
  5073. enum pipe pipe;
  5074. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5075. sizeof(intel_state->min_pixclk));
  5076. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5077. int pixclk = 0;
  5078. if (crtc_state->enable)
  5079. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5080. intel_state->min_pixclk[i] = pixclk;
  5081. }
  5082. for_each_pipe(dev_priv, pipe)
  5083. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5084. return max_pixclk;
  5085. }
  5086. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5087. {
  5088. struct drm_device *dev = state->dev;
  5089. struct drm_i915_private *dev_priv = to_i915(dev);
  5090. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5091. struct intel_atomic_state *intel_state =
  5092. to_intel_atomic_state(state);
  5093. intel_state->cdclk = intel_state->dev_cdclk =
  5094. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5095. if (!intel_state->active_crtcs)
  5096. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5097. return 0;
  5098. }
  5099. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  5100. {
  5101. int max_pixclk = ilk_max_pixel_rate(state);
  5102. struct intel_atomic_state *intel_state =
  5103. to_intel_atomic_state(state);
  5104. intel_state->cdclk = intel_state->dev_cdclk =
  5105. bxt_calc_cdclk(max_pixclk);
  5106. if (!intel_state->active_crtcs)
  5107. intel_state->dev_cdclk = bxt_calc_cdclk(0);
  5108. return 0;
  5109. }
  5110. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5111. {
  5112. unsigned int credits, default_credits;
  5113. if (IS_CHERRYVIEW(dev_priv))
  5114. default_credits = PFI_CREDIT(12);
  5115. else
  5116. default_credits = PFI_CREDIT(8);
  5117. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5118. /* CHV suggested value is 31 or 63 */
  5119. if (IS_CHERRYVIEW(dev_priv))
  5120. credits = PFI_CREDIT_63;
  5121. else
  5122. credits = PFI_CREDIT(15);
  5123. } else {
  5124. credits = default_credits;
  5125. }
  5126. /*
  5127. * WA - write default credits before re-programming
  5128. * FIXME: should we also set the resend bit here?
  5129. */
  5130. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5131. default_credits);
  5132. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5133. credits | PFI_CREDIT_RESEND);
  5134. /*
  5135. * FIXME is this guaranteed to clear
  5136. * immediately or should we poll for it?
  5137. */
  5138. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5139. }
  5140. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5141. {
  5142. struct drm_device *dev = old_state->dev;
  5143. struct drm_i915_private *dev_priv = to_i915(dev);
  5144. struct intel_atomic_state *old_intel_state =
  5145. to_intel_atomic_state(old_state);
  5146. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5147. /*
  5148. * FIXME: We can end up here with all power domains off, yet
  5149. * with a CDCLK frequency other than the minimum. To account
  5150. * for this take the PIPE-A power domain, which covers the HW
  5151. * blocks needed for the following programming. This can be
  5152. * removed once it's guaranteed that we get here either with
  5153. * the minimum CDCLK set, or the required power domains
  5154. * enabled.
  5155. */
  5156. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5157. if (IS_CHERRYVIEW(dev))
  5158. cherryview_set_cdclk(dev, req_cdclk);
  5159. else
  5160. valleyview_set_cdclk(dev, req_cdclk);
  5161. vlv_program_pfi_credits(dev_priv);
  5162. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5163. }
  5164. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5165. {
  5166. struct drm_device *dev = crtc->dev;
  5167. struct drm_i915_private *dev_priv = to_i915(dev);
  5168. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5169. struct intel_encoder *encoder;
  5170. struct intel_crtc_state *pipe_config =
  5171. to_intel_crtc_state(crtc->state);
  5172. int pipe = intel_crtc->pipe;
  5173. if (WARN_ON(intel_crtc->active))
  5174. return;
  5175. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5176. intel_dp_set_m_n(intel_crtc, M1_N1);
  5177. intel_set_pipe_timings(intel_crtc);
  5178. intel_set_pipe_src_size(intel_crtc);
  5179. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5180. struct drm_i915_private *dev_priv = to_i915(dev);
  5181. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5182. I915_WRITE(CHV_CANVAS(pipe), 0);
  5183. }
  5184. i9xx_set_pipeconf(intel_crtc);
  5185. intel_crtc->active = true;
  5186. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5187. for_each_encoder_on_crtc(dev, crtc, encoder)
  5188. if (encoder->pre_pll_enable)
  5189. encoder->pre_pll_enable(encoder);
  5190. if (IS_CHERRYVIEW(dev)) {
  5191. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5192. chv_enable_pll(intel_crtc, intel_crtc->config);
  5193. } else {
  5194. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5195. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5196. }
  5197. for_each_encoder_on_crtc(dev, crtc, encoder)
  5198. if (encoder->pre_enable)
  5199. encoder->pre_enable(encoder);
  5200. i9xx_pfit_enable(intel_crtc);
  5201. intel_color_load_luts(&pipe_config->base);
  5202. intel_update_watermarks(crtc);
  5203. intel_enable_pipe(intel_crtc);
  5204. assert_vblank_disabled(crtc);
  5205. drm_crtc_vblank_on(crtc);
  5206. for_each_encoder_on_crtc(dev, crtc, encoder)
  5207. encoder->enable(encoder);
  5208. }
  5209. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5210. {
  5211. struct drm_device *dev = crtc->base.dev;
  5212. struct drm_i915_private *dev_priv = to_i915(dev);
  5213. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5214. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5215. }
  5216. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5217. {
  5218. struct drm_device *dev = crtc->dev;
  5219. struct drm_i915_private *dev_priv = to_i915(dev);
  5220. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5221. struct intel_encoder *encoder;
  5222. struct intel_crtc_state *pipe_config =
  5223. to_intel_crtc_state(crtc->state);
  5224. enum pipe pipe = intel_crtc->pipe;
  5225. if (WARN_ON(intel_crtc->active))
  5226. return;
  5227. i9xx_set_pll_dividers(intel_crtc);
  5228. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5229. intel_dp_set_m_n(intel_crtc, M1_N1);
  5230. intel_set_pipe_timings(intel_crtc);
  5231. intel_set_pipe_src_size(intel_crtc);
  5232. i9xx_set_pipeconf(intel_crtc);
  5233. intel_crtc->active = true;
  5234. if (!IS_GEN2(dev))
  5235. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5236. for_each_encoder_on_crtc(dev, crtc, encoder)
  5237. if (encoder->pre_enable)
  5238. encoder->pre_enable(encoder);
  5239. i9xx_enable_pll(intel_crtc);
  5240. i9xx_pfit_enable(intel_crtc);
  5241. intel_color_load_luts(&pipe_config->base);
  5242. intel_update_watermarks(crtc);
  5243. intel_enable_pipe(intel_crtc);
  5244. assert_vblank_disabled(crtc);
  5245. drm_crtc_vblank_on(crtc);
  5246. for_each_encoder_on_crtc(dev, crtc, encoder)
  5247. encoder->enable(encoder);
  5248. }
  5249. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5250. {
  5251. struct drm_device *dev = crtc->base.dev;
  5252. struct drm_i915_private *dev_priv = to_i915(dev);
  5253. if (!crtc->config->gmch_pfit.control)
  5254. return;
  5255. assert_pipe_disabled(dev_priv, crtc->pipe);
  5256. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5257. I915_READ(PFIT_CONTROL));
  5258. I915_WRITE(PFIT_CONTROL, 0);
  5259. }
  5260. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5261. {
  5262. struct drm_device *dev = crtc->dev;
  5263. struct drm_i915_private *dev_priv = to_i915(dev);
  5264. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5265. struct intel_encoder *encoder;
  5266. int pipe = intel_crtc->pipe;
  5267. /*
  5268. * On gen2 planes are double buffered but the pipe isn't, so we must
  5269. * wait for planes to fully turn off before disabling the pipe.
  5270. */
  5271. if (IS_GEN2(dev))
  5272. intel_wait_for_vblank(dev, pipe);
  5273. for_each_encoder_on_crtc(dev, crtc, encoder)
  5274. encoder->disable(encoder);
  5275. drm_crtc_vblank_off(crtc);
  5276. assert_vblank_disabled(crtc);
  5277. intel_disable_pipe(intel_crtc);
  5278. i9xx_pfit_disable(intel_crtc);
  5279. for_each_encoder_on_crtc(dev, crtc, encoder)
  5280. if (encoder->post_disable)
  5281. encoder->post_disable(encoder);
  5282. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5283. if (IS_CHERRYVIEW(dev))
  5284. chv_disable_pll(dev_priv, pipe);
  5285. else if (IS_VALLEYVIEW(dev))
  5286. vlv_disable_pll(dev_priv, pipe);
  5287. else
  5288. i9xx_disable_pll(intel_crtc);
  5289. }
  5290. for_each_encoder_on_crtc(dev, crtc, encoder)
  5291. if (encoder->post_pll_disable)
  5292. encoder->post_pll_disable(encoder);
  5293. if (!IS_GEN2(dev))
  5294. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5295. }
  5296. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5297. {
  5298. struct intel_encoder *encoder;
  5299. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5300. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5301. enum intel_display_power_domain domain;
  5302. unsigned long domains;
  5303. if (!intel_crtc->active)
  5304. return;
  5305. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5306. WARN_ON(intel_crtc->flip_work);
  5307. intel_pre_disable_primary_noatomic(crtc);
  5308. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5309. to_intel_plane_state(crtc->primary->state)->visible = false;
  5310. }
  5311. dev_priv->display.crtc_disable(crtc);
  5312. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5313. crtc->base.id, crtc->name);
  5314. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5315. crtc->state->active = false;
  5316. intel_crtc->active = false;
  5317. crtc->enabled = false;
  5318. crtc->state->connector_mask = 0;
  5319. crtc->state->encoder_mask = 0;
  5320. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5321. encoder->base.crtc = NULL;
  5322. intel_fbc_disable(intel_crtc);
  5323. intel_update_watermarks(crtc);
  5324. intel_disable_shared_dpll(intel_crtc);
  5325. domains = intel_crtc->enabled_power_domains;
  5326. for_each_power_domain(domain, domains)
  5327. intel_display_power_put(dev_priv, domain);
  5328. intel_crtc->enabled_power_domains = 0;
  5329. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5330. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5331. }
  5332. /*
  5333. * turn all crtc's off, but do not adjust state
  5334. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5335. */
  5336. int intel_display_suspend(struct drm_device *dev)
  5337. {
  5338. struct drm_i915_private *dev_priv = to_i915(dev);
  5339. struct drm_atomic_state *state;
  5340. int ret;
  5341. state = drm_atomic_helper_suspend(dev);
  5342. ret = PTR_ERR_OR_ZERO(state);
  5343. if (ret)
  5344. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5345. else
  5346. dev_priv->modeset_restore_state = state;
  5347. return ret;
  5348. }
  5349. void intel_encoder_destroy(struct drm_encoder *encoder)
  5350. {
  5351. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5352. drm_encoder_cleanup(encoder);
  5353. kfree(intel_encoder);
  5354. }
  5355. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5356. * internal consistency). */
  5357. static void intel_connector_verify_state(struct intel_connector *connector)
  5358. {
  5359. struct drm_crtc *crtc = connector->base.state->crtc;
  5360. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5361. connector->base.base.id,
  5362. connector->base.name);
  5363. if (connector->get_hw_state(connector)) {
  5364. struct intel_encoder *encoder = connector->encoder;
  5365. struct drm_connector_state *conn_state = connector->base.state;
  5366. I915_STATE_WARN(!crtc,
  5367. "connector enabled without attached crtc\n");
  5368. if (!crtc)
  5369. return;
  5370. I915_STATE_WARN(!crtc->state->active,
  5371. "connector is active, but attached crtc isn't\n");
  5372. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5373. return;
  5374. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5375. "atomic encoder doesn't match attached encoder\n");
  5376. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5377. "attached encoder crtc differs from connector crtc\n");
  5378. } else {
  5379. I915_STATE_WARN(crtc && crtc->state->active,
  5380. "attached crtc is active, but connector isn't\n");
  5381. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5382. "best encoder set without crtc!\n");
  5383. }
  5384. }
  5385. int intel_connector_init(struct intel_connector *connector)
  5386. {
  5387. drm_atomic_helper_connector_reset(&connector->base);
  5388. if (!connector->base.state)
  5389. return -ENOMEM;
  5390. return 0;
  5391. }
  5392. struct intel_connector *intel_connector_alloc(void)
  5393. {
  5394. struct intel_connector *connector;
  5395. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5396. if (!connector)
  5397. return NULL;
  5398. if (intel_connector_init(connector) < 0) {
  5399. kfree(connector);
  5400. return NULL;
  5401. }
  5402. return connector;
  5403. }
  5404. /* Simple connector->get_hw_state implementation for encoders that support only
  5405. * one connector and no cloning and hence the encoder state determines the state
  5406. * of the connector. */
  5407. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5408. {
  5409. enum pipe pipe = 0;
  5410. struct intel_encoder *encoder = connector->encoder;
  5411. return encoder->get_hw_state(encoder, &pipe);
  5412. }
  5413. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5414. {
  5415. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5416. return crtc_state->fdi_lanes;
  5417. return 0;
  5418. }
  5419. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5420. struct intel_crtc_state *pipe_config)
  5421. {
  5422. struct drm_atomic_state *state = pipe_config->base.state;
  5423. struct intel_crtc *other_crtc;
  5424. struct intel_crtc_state *other_crtc_state;
  5425. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5426. pipe_name(pipe), pipe_config->fdi_lanes);
  5427. if (pipe_config->fdi_lanes > 4) {
  5428. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5429. pipe_name(pipe), pipe_config->fdi_lanes);
  5430. return -EINVAL;
  5431. }
  5432. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5433. if (pipe_config->fdi_lanes > 2) {
  5434. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5435. pipe_config->fdi_lanes);
  5436. return -EINVAL;
  5437. } else {
  5438. return 0;
  5439. }
  5440. }
  5441. if (INTEL_INFO(dev)->num_pipes == 2)
  5442. return 0;
  5443. /* Ivybridge 3 pipe is really complicated */
  5444. switch (pipe) {
  5445. case PIPE_A:
  5446. return 0;
  5447. case PIPE_B:
  5448. if (pipe_config->fdi_lanes <= 2)
  5449. return 0;
  5450. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5451. other_crtc_state =
  5452. intel_atomic_get_crtc_state(state, other_crtc);
  5453. if (IS_ERR(other_crtc_state))
  5454. return PTR_ERR(other_crtc_state);
  5455. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5456. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5457. pipe_name(pipe), pipe_config->fdi_lanes);
  5458. return -EINVAL;
  5459. }
  5460. return 0;
  5461. case PIPE_C:
  5462. if (pipe_config->fdi_lanes > 2) {
  5463. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5464. pipe_name(pipe), pipe_config->fdi_lanes);
  5465. return -EINVAL;
  5466. }
  5467. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5468. other_crtc_state =
  5469. intel_atomic_get_crtc_state(state, other_crtc);
  5470. if (IS_ERR(other_crtc_state))
  5471. return PTR_ERR(other_crtc_state);
  5472. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5473. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5474. return -EINVAL;
  5475. }
  5476. return 0;
  5477. default:
  5478. BUG();
  5479. }
  5480. }
  5481. #define RETRY 1
  5482. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5483. struct intel_crtc_state *pipe_config)
  5484. {
  5485. struct drm_device *dev = intel_crtc->base.dev;
  5486. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5487. int lane, link_bw, fdi_dotclock, ret;
  5488. bool needs_recompute = false;
  5489. retry:
  5490. /* FDI is a binary signal running at ~2.7GHz, encoding
  5491. * each output octet as 10 bits. The actual frequency
  5492. * is stored as a divider into a 100MHz clock, and the
  5493. * mode pixel clock is stored in units of 1KHz.
  5494. * Hence the bw of each lane in terms of the mode signal
  5495. * is:
  5496. */
  5497. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5498. fdi_dotclock = adjusted_mode->crtc_clock;
  5499. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5500. pipe_config->pipe_bpp);
  5501. pipe_config->fdi_lanes = lane;
  5502. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5503. link_bw, &pipe_config->fdi_m_n);
  5504. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5505. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5506. pipe_config->pipe_bpp -= 2*3;
  5507. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5508. pipe_config->pipe_bpp);
  5509. needs_recompute = true;
  5510. pipe_config->bw_constrained = true;
  5511. goto retry;
  5512. }
  5513. if (needs_recompute)
  5514. return RETRY;
  5515. return ret;
  5516. }
  5517. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5518. struct intel_crtc_state *pipe_config)
  5519. {
  5520. if (pipe_config->pipe_bpp > 24)
  5521. return false;
  5522. /* HSW can handle pixel rate up to cdclk? */
  5523. if (IS_HASWELL(dev_priv))
  5524. return true;
  5525. /*
  5526. * We compare against max which means we must take
  5527. * the increased cdclk requirement into account when
  5528. * calculating the new cdclk.
  5529. *
  5530. * Should measure whether using a lower cdclk w/o IPS
  5531. */
  5532. return ilk_pipe_pixel_rate(pipe_config) <=
  5533. dev_priv->max_cdclk_freq * 95 / 100;
  5534. }
  5535. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5536. struct intel_crtc_state *pipe_config)
  5537. {
  5538. struct drm_device *dev = crtc->base.dev;
  5539. struct drm_i915_private *dev_priv = to_i915(dev);
  5540. pipe_config->ips_enabled = i915.enable_ips &&
  5541. hsw_crtc_supports_ips(crtc) &&
  5542. pipe_config_supports_ips(dev_priv, pipe_config);
  5543. }
  5544. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5545. {
  5546. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5547. /* GDG double wide on either pipe, otherwise pipe A only */
  5548. return INTEL_INFO(dev_priv)->gen < 4 &&
  5549. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5550. }
  5551. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5552. struct intel_crtc_state *pipe_config)
  5553. {
  5554. struct drm_device *dev = crtc->base.dev;
  5555. struct drm_i915_private *dev_priv = to_i915(dev);
  5556. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5557. int clock_limit = dev_priv->max_dotclk_freq;
  5558. if (INTEL_INFO(dev)->gen < 4) {
  5559. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5560. /*
  5561. * Enable double wide mode when the dot clock
  5562. * is > 90% of the (display) core speed.
  5563. */
  5564. if (intel_crtc_supports_double_wide(crtc) &&
  5565. adjusted_mode->crtc_clock > clock_limit) {
  5566. clock_limit = dev_priv->max_dotclk_freq;
  5567. pipe_config->double_wide = true;
  5568. }
  5569. }
  5570. if (adjusted_mode->crtc_clock > clock_limit) {
  5571. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5572. adjusted_mode->crtc_clock, clock_limit,
  5573. yesno(pipe_config->double_wide));
  5574. return -EINVAL;
  5575. }
  5576. /*
  5577. * Pipe horizontal size must be even in:
  5578. * - DVO ganged mode
  5579. * - LVDS dual channel mode
  5580. * - Double wide pipe
  5581. */
  5582. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5583. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5584. pipe_config->pipe_src_w &= ~1;
  5585. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5586. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5587. */
  5588. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5589. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5590. return -EINVAL;
  5591. if (HAS_IPS(dev))
  5592. hsw_compute_ips_config(crtc, pipe_config);
  5593. if (pipe_config->has_pch_encoder)
  5594. return ironlake_fdi_compute_config(crtc, pipe_config);
  5595. return 0;
  5596. }
  5597. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5598. {
  5599. struct drm_i915_private *dev_priv = to_i915(dev);
  5600. uint32_t cdctl;
  5601. skl_dpll0_update(dev_priv);
  5602. if (dev_priv->cdclk_pll.vco == 0)
  5603. return dev_priv->cdclk_pll.ref;
  5604. cdctl = I915_READ(CDCLK_CTL);
  5605. if (dev_priv->cdclk_pll.vco == 8640000) {
  5606. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5607. case CDCLK_FREQ_450_432:
  5608. return 432000;
  5609. case CDCLK_FREQ_337_308:
  5610. return 308571;
  5611. case CDCLK_FREQ_540:
  5612. return 540000;
  5613. case CDCLK_FREQ_675_617:
  5614. return 617143;
  5615. default:
  5616. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5617. }
  5618. } else {
  5619. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5620. case CDCLK_FREQ_450_432:
  5621. return 450000;
  5622. case CDCLK_FREQ_337_308:
  5623. return 337500;
  5624. case CDCLK_FREQ_540:
  5625. return 540000;
  5626. case CDCLK_FREQ_675_617:
  5627. return 675000;
  5628. default:
  5629. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5630. }
  5631. }
  5632. return dev_priv->cdclk_pll.ref;
  5633. }
  5634. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  5635. {
  5636. u32 val;
  5637. dev_priv->cdclk_pll.ref = 19200;
  5638. dev_priv->cdclk_pll.vco = 0;
  5639. val = I915_READ(BXT_DE_PLL_ENABLE);
  5640. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  5641. return;
  5642. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  5643. return;
  5644. val = I915_READ(BXT_DE_PLL_CTL);
  5645. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  5646. dev_priv->cdclk_pll.ref;
  5647. }
  5648. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5649. {
  5650. struct drm_i915_private *dev_priv = to_i915(dev);
  5651. u32 divider;
  5652. int div, vco;
  5653. bxt_de_pll_update(dev_priv);
  5654. vco = dev_priv->cdclk_pll.vco;
  5655. if (vco == 0)
  5656. return dev_priv->cdclk_pll.ref;
  5657. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  5658. switch (divider) {
  5659. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5660. div = 2;
  5661. break;
  5662. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5663. div = 3;
  5664. break;
  5665. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5666. div = 4;
  5667. break;
  5668. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5669. div = 8;
  5670. break;
  5671. default:
  5672. MISSING_CASE(divider);
  5673. return dev_priv->cdclk_pll.ref;
  5674. }
  5675. return DIV_ROUND_CLOSEST(vco, div);
  5676. }
  5677. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5678. {
  5679. struct drm_i915_private *dev_priv = to_i915(dev);
  5680. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5681. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5682. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5683. return 800000;
  5684. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5685. return 450000;
  5686. else if (freq == LCPLL_CLK_FREQ_450)
  5687. return 450000;
  5688. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5689. return 540000;
  5690. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5691. return 337500;
  5692. else
  5693. return 675000;
  5694. }
  5695. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5696. {
  5697. struct drm_i915_private *dev_priv = to_i915(dev);
  5698. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5699. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5700. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5701. return 800000;
  5702. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5703. return 450000;
  5704. else if (freq == LCPLL_CLK_FREQ_450)
  5705. return 450000;
  5706. else if (IS_HSW_ULT(dev))
  5707. return 337500;
  5708. else
  5709. return 540000;
  5710. }
  5711. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5712. {
  5713. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5714. CCK_DISPLAY_CLOCK_CONTROL);
  5715. }
  5716. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5717. {
  5718. return 450000;
  5719. }
  5720. static int i945_get_display_clock_speed(struct drm_device *dev)
  5721. {
  5722. return 400000;
  5723. }
  5724. static int i915_get_display_clock_speed(struct drm_device *dev)
  5725. {
  5726. return 333333;
  5727. }
  5728. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5729. {
  5730. return 200000;
  5731. }
  5732. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5733. {
  5734. u16 gcfgc = 0;
  5735. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5736. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5737. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5738. return 266667;
  5739. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5740. return 333333;
  5741. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5742. return 444444;
  5743. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5744. return 200000;
  5745. default:
  5746. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5747. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5748. return 133333;
  5749. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5750. return 166667;
  5751. }
  5752. }
  5753. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5754. {
  5755. u16 gcfgc = 0;
  5756. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5757. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5758. return 133333;
  5759. else {
  5760. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5761. case GC_DISPLAY_CLOCK_333_MHZ:
  5762. return 333333;
  5763. default:
  5764. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5765. return 190000;
  5766. }
  5767. }
  5768. }
  5769. static int i865_get_display_clock_speed(struct drm_device *dev)
  5770. {
  5771. return 266667;
  5772. }
  5773. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5774. {
  5775. u16 hpllcc = 0;
  5776. /*
  5777. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5778. * encoding is different :(
  5779. * FIXME is this the right way to detect 852GM/852GMV?
  5780. */
  5781. if (dev->pdev->revision == 0x1)
  5782. return 133333;
  5783. pci_bus_read_config_word(dev->pdev->bus,
  5784. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5785. /* Assume that the hardware is in the high speed state. This
  5786. * should be the default.
  5787. */
  5788. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5789. case GC_CLOCK_133_200:
  5790. case GC_CLOCK_133_200_2:
  5791. case GC_CLOCK_100_200:
  5792. return 200000;
  5793. case GC_CLOCK_166_250:
  5794. return 250000;
  5795. case GC_CLOCK_100_133:
  5796. return 133333;
  5797. case GC_CLOCK_133_266:
  5798. case GC_CLOCK_133_266_2:
  5799. case GC_CLOCK_166_266:
  5800. return 266667;
  5801. }
  5802. /* Shouldn't happen */
  5803. return 0;
  5804. }
  5805. static int i830_get_display_clock_speed(struct drm_device *dev)
  5806. {
  5807. return 133333;
  5808. }
  5809. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5810. {
  5811. struct drm_i915_private *dev_priv = to_i915(dev);
  5812. static const unsigned int blb_vco[8] = {
  5813. [0] = 3200000,
  5814. [1] = 4000000,
  5815. [2] = 5333333,
  5816. [3] = 4800000,
  5817. [4] = 6400000,
  5818. };
  5819. static const unsigned int pnv_vco[8] = {
  5820. [0] = 3200000,
  5821. [1] = 4000000,
  5822. [2] = 5333333,
  5823. [3] = 4800000,
  5824. [4] = 2666667,
  5825. };
  5826. static const unsigned int cl_vco[8] = {
  5827. [0] = 3200000,
  5828. [1] = 4000000,
  5829. [2] = 5333333,
  5830. [3] = 6400000,
  5831. [4] = 3333333,
  5832. [5] = 3566667,
  5833. [6] = 4266667,
  5834. };
  5835. static const unsigned int elk_vco[8] = {
  5836. [0] = 3200000,
  5837. [1] = 4000000,
  5838. [2] = 5333333,
  5839. [3] = 4800000,
  5840. };
  5841. static const unsigned int ctg_vco[8] = {
  5842. [0] = 3200000,
  5843. [1] = 4000000,
  5844. [2] = 5333333,
  5845. [3] = 6400000,
  5846. [4] = 2666667,
  5847. [5] = 4266667,
  5848. };
  5849. const unsigned int *vco_table;
  5850. unsigned int vco;
  5851. uint8_t tmp = 0;
  5852. /* FIXME other chipsets? */
  5853. if (IS_GM45(dev))
  5854. vco_table = ctg_vco;
  5855. else if (IS_G4X(dev))
  5856. vco_table = elk_vco;
  5857. else if (IS_CRESTLINE(dev))
  5858. vco_table = cl_vco;
  5859. else if (IS_PINEVIEW(dev))
  5860. vco_table = pnv_vco;
  5861. else if (IS_G33(dev))
  5862. vco_table = blb_vco;
  5863. else
  5864. return 0;
  5865. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5866. vco = vco_table[tmp & 0x7];
  5867. if (vco == 0)
  5868. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5869. else
  5870. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5871. return vco;
  5872. }
  5873. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5874. {
  5875. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5876. uint16_t tmp = 0;
  5877. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5878. cdclk_sel = (tmp >> 12) & 0x1;
  5879. switch (vco) {
  5880. case 2666667:
  5881. case 4000000:
  5882. case 5333333:
  5883. return cdclk_sel ? 333333 : 222222;
  5884. case 3200000:
  5885. return cdclk_sel ? 320000 : 228571;
  5886. default:
  5887. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5888. return 222222;
  5889. }
  5890. }
  5891. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5892. {
  5893. static const uint8_t div_3200[] = { 16, 10, 8 };
  5894. static const uint8_t div_4000[] = { 20, 12, 10 };
  5895. static const uint8_t div_5333[] = { 24, 16, 14 };
  5896. const uint8_t *div_table;
  5897. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5898. uint16_t tmp = 0;
  5899. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5900. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5901. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5902. goto fail;
  5903. switch (vco) {
  5904. case 3200000:
  5905. div_table = div_3200;
  5906. break;
  5907. case 4000000:
  5908. div_table = div_4000;
  5909. break;
  5910. case 5333333:
  5911. div_table = div_5333;
  5912. break;
  5913. default:
  5914. goto fail;
  5915. }
  5916. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5917. fail:
  5918. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5919. return 200000;
  5920. }
  5921. static int g33_get_display_clock_speed(struct drm_device *dev)
  5922. {
  5923. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5924. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5925. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5926. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5927. const uint8_t *div_table;
  5928. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5929. uint16_t tmp = 0;
  5930. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5931. cdclk_sel = (tmp >> 4) & 0x7;
  5932. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5933. goto fail;
  5934. switch (vco) {
  5935. case 3200000:
  5936. div_table = div_3200;
  5937. break;
  5938. case 4000000:
  5939. div_table = div_4000;
  5940. break;
  5941. case 4800000:
  5942. div_table = div_4800;
  5943. break;
  5944. case 5333333:
  5945. div_table = div_5333;
  5946. break;
  5947. default:
  5948. goto fail;
  5949. }
  5950. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5951. fail:
  5952. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5953. return 190476;
  5954. }
  5955. static void
  5956. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5957. {
  5958. while (*num > DATA_LINK_M_N_MASK ||
  5959. *den > DATA_LINK_M_N_MASK) {
  5960. *num >>= 1;
  5961. *den >>= 1;
  5962. }
  5963. }
  5964. static void compute_m_n(unsigned int m, unsigned int n,
  5965. uint32_t *ret_m, uint32_t *ret_n)
  5966. {
  5967. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5968. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5969. intel_reduce_m_n_ratio(ret_m, ret_n);
  5970. }
  5971. void
  5972. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5973. int pixel_clock, int link_clock,
  5974. struct intel_link_m_n *m_n)
  5975. {
  5976. m_n->tu = 64;
  5977. compute_m_n(bits_per_pixel * pixel_clock,
  5978. link_clock * nlanes * 8,
  5979. &m_n->gmch_m, &m_n->gmch_n);
  5980. compute_m_n(pixel_clock, link_clock,
  5981. &m_n->link_m, &m_n->link_n);
  5982. }
  5983. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5984. {
  5985. if (i915.panel_use_ssc >= 0)
  5986. return i915.panel_use_ssc != 0;
  5987. return dev_priv->vbt.lvds_use_ssc
  5988. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5989. }
  5990. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5991. {
  5992. return (1 << dpll->n) << 16 | dpll->m2;
  5993. }
  5994. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5995. {
  5996. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5997. }
  5998. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5999. struct intel_crtc_state *crtc_state,
  6000. struct dpll *reduced_clock)
  6001. {
  6002. struct drm_device *dev = crtc->base.dev;
  6003. u32 fp, fp2 = 0;
  6004. if (IS_PINEVIEW(dev)) {
  6005. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6006. if (reduced_clock)
  6007. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6008. } else {
  6009. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6010. if (reduced_clock)
  6011. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6012. }
  6013. crtc_state->dpll_hw_state.fp0 = fp;
  6014. crtc->lowfreq_avail = false;
  6015. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6016. reduced_clock) {
  6017. crtc_state->dpll_hw_state.fp1 = fp2;
  6018. crtc->lowfreq_avail = true;
  6019. } else {
  6020. crtc_state->dpll_hw_state.fp1 = fp;
  6021. }
  6022. }
  6023. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6024. pipe)
  6025. {
  6026. u32 reg_val;
  6027. /*
  6028. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6029. * and set it to a reasonable value instead.
  6030. */
  6031. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6032. reg_val &= 0xffffff00;
  6033. reg_val |= 0x00000030;
  6034. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6035. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6036. reg_val &= 0x8cffffff;
  6037. reg_val = 0x8c000000;
  6038. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6039. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6040. reg_val &= 0xffffff00;
  6041. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6042. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6043. reg_val &= 0x00ffffff;
  6044. reg_val |= 0xb0000000;
  6045. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6046. }
  6047. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6048. struct intel_link_m_n *m_n)
  6049. {
  6050. struct drm_device *dev = crtc->base.dev;
  6051. struct drm_i915_private *dev_priv = to_i915(dev);
  6052. int pipe = crtc->pipe;
  6053. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6054. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6055. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6056. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6057. }
  6058. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6059. struct intel_link_m_n *m_n,
  6060. struct intel_link_m_n *m2_n2)
  6061. {
  6062. struct drm_device *dev = crtc->base.dev;
  6063. struct drm_i915_private *dev_priv = to_i915(dev);
  6064. int pipe = crtc->pipe;
  6065. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6066. if (INTEL_INFO(dev)->gen >= 5) {
  6067. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6068. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6069. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6070. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6071. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6072. * for gen < 8) and if DRRS is supported (to make sure the
  6073. * registers are not unnecessarily accessed).
  6074. */
  6075. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6076. crtc->config->has_drrs) {
  6077. I915_WRITE(PIPE_DATA_M2(transcoder),
  6078. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6079. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6080. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6081. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6082. }
  6083. } else {
  6084. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6085. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6086. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6087. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6088. }
  6089. }
  6090. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6091. {
  6092. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6093. if (m_n == M1_N1) {
  6094. dp_m_n = &crtc->config->dp_m_n;
  6095. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6096. } else if (m_n == M2_N2) {
  6097. /*
  6098. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6099. * needs to be programmed into M1_N1.
  6100. */
  6101. dp_m_n = &crtc->config->dp_m2_n2;
  6102. } else {
  6103. DRM_ERROR("Unsupported divider value\n");
  6104. return;
  6105. }
  6106. if (crtc->config->has_pch_encoder)
  6107. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6108. else
  6109. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6110. }
  6111. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6112. struct intel_crtc_state *pipe_config)
  6113. {
  6114. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6115. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6116. if (crtc->pipe != PIPE_A)
  6117. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6118. /* DPLL not used with DSI, but still need the rest set up */
  6119. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6120. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6121. DPLL_EXT_BUFFER_ENABLE_VLV;
  6122. pipe_config->dpll_hw_state.dpll_md =
  6123. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6124. }
  6125. static void chv_compute_dpll(struct intel_crtc *crtc,
  6126. struct intel_crtc_state *pipe_config)
  6127. {
  6128. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6129. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6130. if (crtc->pipe != PIPE_A)
  6131. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6132. /* DPLL not used with DSI, but still need the rest set up */
  6133. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6134. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6135. pipe_config->dpll_hw_state.dpll_md =
  6136. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6137. }
  6138. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6139. const struct intel_crtc_state *pipe_config)
  6140. {
  6141. struct drm_device *dev = crtc->base.dev;
  6142. struct drm_i915_private *dev_priv = to_i915(dev);
  6143. enum pipe pipe = crtc->pipe;
  6144. u32 mdiv;
  6145. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6146. u32 coreclk, reg_val;
  6147. /* Enable Refclk */
  6148. I915_WRITE(DPLL(pipe),
  6149. pipe_config->dpll_hw_state.dpll &
  6150. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6151. /* No need to actually set up the DPLL with DSI */
  6152. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6153. return;
  6154. mutex_lock(&dev_priv->sb_lock);
  6155. bestn = pipe_config->dpll.n;
  6156. bestm1 = pipe_config->dpll.m1;
  6157. bestm2 = pipe_config->dpll.m2;
  6158. bestp1 = pipe_config->dpll.p1;
  6159. bestp2 = pipe_config->dpll.p2;
  6160. /* See eDP HDMI DPIO driver vbios notes doc */
  6161. /* PLL B needs special handling */
  6162. if (pipe == PIPE_B)
  6163. vlv_pllb_recal_opamp(dev_priv, pipe);
  6164. /* Set up Tx target for periodic Rcomp update */
  6165. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6166. /* Disable target IRef on PLL */
  6167. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6168. reg_val &= 0x00ffffff;
  6169. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6170. /* Disable fast lock */
  6171. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6172. /* Set idtafcrecal before PLL is enabled */
  6173. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6174. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6175. mdiv |= ((bestn << DPIO_N_SHIFT));
  6176. mdiv |= (1 << DPIO_K_SHIFT);
  6177. /*
  6178. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6179. * but we don't support that).
  6180. * Note: don't use the DAC post divider as it seems unstable.
  6181. */
  6182. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6183. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6184. mdiv |= DPIO_ENABLE_CALIBRATION;
  6185. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6186. /* Set HBR and RBR LPF coefficients */
  6187. if (pipe_config->port_clock == 162000 ||
  6188. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  6189. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  6190. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6191. 0x009f0003);
  6192. else
  6193. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6194. 0x00d0000f);
  6195. if (intel_crtc_has_dp_encoder(pipe_config)) {
  6196. /* Use SSC source */
  6197. if (pipe == PIPE_A)
  6198. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6199. 0x0df40000);
  6200. else
  6201. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6202. 0x0df70000);
  6203. } else { /* HDMI or VGA */
  6204. /* Use bend source */
  6205. if (pipe == PIPE_A)
  6206. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6207. 0x0df70000);
  6208. else
  6209. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6210. 0x0df40000);
  6211. }
  6212. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6213. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6214. if (intel_crtc_has_dp_encoder(crtc->config))
  6215. coreclk |= 0x01000000;
  6216. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6217. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6218. mutex_unlock(&dev_priv->sb_lock);
  6219. }
  6220. static void chv_prepare_pll(struct intel_crtc *crtc,
  6221. const struct intel_crtc_state *pipe_config)
  6222. {
  6223. struct drm_device *dev = crtc->base.dev;
  6224. struct drm_i915_private *dev_priv = to_i915(dev);
  6225. enum pipe pipe = crtc->pipe;
  6226. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6227. u32 loopfilter, tribuf_calcntr;
  6228. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6229. u32 dpio_val;
  6230. int vco;
  6231. /* Enable Refclk and SSC */
  6232. I915_WRITE(DPLL(pipe),
  6233. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6234. /* No need to actually set up the DPLL with DSI */
  6235. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6236. return;
  6237. bestn = pipe_config->dpll.n;
  6238. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6239. bestm1 = pipe_config->dpll.m1;
  6240. bestm2 = pipe_config->dpll.m2 >> 22;
  6241. bestp1 = pipe_config->dpll.p1;
  6242. bestp2 = pipe_config->dpll.p2;
  6243. vco = pipe_config->dpll.vco;
  6244. dpio_val = 0;
  6245. loopfilter = 0;
  6246. mutex_lock(&dev_priv->sb_lock);
  6247. /* p1 and p2 divider */
  6248. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6249. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6250. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6251. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6252. 1 << DPIO_CHV_K_DIV_SHIFT);
  6253. /* Feedback post-divider - m2 */
  6254. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6255. /* Feedback refclk divider - n and m1 */
  6256. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6257. DPIO_CHV_M1_DIV_BY_2 |
  6258. 1 << DPIO_CHV_N_DIV_SHIFT);
  6259. /* M2 fraction division */
  6260. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6261. /* M2 fraction division enable */
  6262. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6263. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6264. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6265. if (bestm2_frac)
  6266. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6267. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6268. /* Program digital lock detect threshold */
  6269. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6270. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6271. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6272. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6273. if (!bestm2_frac)
  6274. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6275. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6276. /* Loop filter */
  6277. if (vco == 5400000) {
  6278. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6279. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6280. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6281. tribuf_calcntr = 0x9;
  6282. } else if (vco <= 6200000) {
  6283. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6284. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6285. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6286. tribuf_calcntr = 0x9;
  6287. } else if (vco <= 6480000) {
  6288. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6289. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6290. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6291. tribuf_calcntr = 0x8;
  6292. } else {
  6293. /* Not supported. Apply the same limits as in the max case */
  6294. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6295. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6296. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6297. tribuf_calcntr = 0;
  6298. }
  6299. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6300. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6301. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6302. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6303. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6304. /* AFC Recal */
  6305. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6306. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6307. DPIO_AFC_RECAL);
  6308. mutex_unlock(&dev_priv->sb_lock);
  6309. }
  6310. /**
  6311. * vlv_force_pll_on - forcibly enable just the PLL
  6312. * @dev_priv: i915 private structure
  6313. * @pipe: pipe PLL to enable
  6314. * @dpll: PLL configuration
  6315. *
  6316. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6317. * in cases where we need the PLL enabled even when @pipe is not going to
  6318. * be enabled.
  6319. */
  6320. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6321. const struct dpll *dpll)
  6322. {
  6323. struct intel_crtc *crtc =
  6324. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6325. struct intel_crtc_state *pipe_config;
  6326. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6327. if (!pipe_config)
  6328. return -ENOMEM;
  6329. pipe_config->base.crtc = &crtc->base;
  6330. pipe_config->pixel_multiplier = 1;
  6331. pipe_config->dpll = *dpll;
  6332. if (IS_CHERRYVIEW(dev)) {
  6333. chv_compute_dpll(crtc, pipe_config);
  6334. chv_prepare_pll(crtc, pipe_config);
  6335. chv_enable_pll(crtc, pipe_config);
  6336. } else {
  6337. vlv_compute_dpll(crtc, pipe_config);
  6338. vlv_prepare_pll(crtc, pipe_config);
  6339. vlv_enable_pll(crtc, pipe_config);
  6340. }
  6341. kfree(pipe_config);
  6342. return 0;
  6343. }
  6344. /**
  6345. * vlv_force_pll_off - forcibly disable just the PLL
  6346. * @dev_priv: i915 private structure
  6347. * @pipe: pipe PLL to disable
  6348. *
  6349. * Disable the PLL for @pipe. To be used in cases where we need
  6350. * the PLL enabled even when @pipe is not going to be enabled.
  6351. */
  6352. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6353. {
  6354. if (IS_CHERRYVIEW(dev))
  6355. chv_disable_pll(to_i915(dev), pipe);
  6356. else
  6357. vlv_disable_pll(to_i915(dev), pipe);
  6358. }
  6359. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6360. struct intel_crtc_state *crtc_state,
  6361. struct dpll *reduced_clock)
  6362. {
  6363. struct drm_device *dev = crtc->base.dev;
  6364. struct drm_i915_private *dev_priv = to_i915(dev);
  6365. u32 dpll;
  6366. struct dpll *clock = &crtc_state->dpll;
  6367. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6368. dpll = DPLL_VGA_MODE_DIS;
  6369. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6370. dpll |= DPLLB_MODE_LVDS;
  6371. else
  6372. dpll |= DPLLB_MODE_DAC_SERIAL;
  6373. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6374. dpll |= (crtc_state->pixel_multiplier - 1)
  6375. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6376. }
  6377. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6378. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6379. dpll |= DPLL_SDVO_HIGH_SPEED;
  6380. if (intel_crtc_has_dp_encoder(crtc_state))
  6381. dpll |= DPLL_SDVO_HIGH_SPEED;
  6382. /* compute bitmask from p1 value */
  6383. if (IS_PINEVIEW(dev))
  6384. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6385. else {
  6386. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6387. if (IS_G4X(dev) && reduced_clock)
  6388. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6389. }
  6390. switch (clock->p2) {
  6391. case 5:
  6392. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6393. break;
  6394. case 7:
  6395. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6396. break;
  6397. case 10:
  6398. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6399. break;
  6400. case 14:
  6401. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6402. break;
  6403. }
  6404. if (INTEL_INFO(dev)->gen >= 4)
  6405. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6406. if (crtc_state->sdvo_tv_clock)
  6407. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6408. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6409. intel_panel_use_ssc(dev_priv))
  6410. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6411. else
  6412. dpll |= PLL_REF_INPUT_DREFCLK;
  6413. dpll |= DPLL_VCO_ENABLE;
  6414. crtc_state->dpll_hw_state.dpll = dpll;
  6415. if (INTEL_INFO(dev)->gen >= 4) {
  6416. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6417. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6418. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6419. }
  6420. }
  6421. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6422. struct intel_crtc_state *crtc_state,
  6423. struct dpll *reduced_clock)
  6424. {
  6425. struct drm_device *dev = crtc->base.dev;
  6426. struct drm_i915_private *dev_priv = to_i915(dev);
  6427. u32 dpll;
  6428. struct dpll *clock = &crtc_state->dpll;
  6429. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6430. dpll = DPLL_VGA_MODE_DIS;
  6431. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6432. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6433. } else {
  6434. if (clock->p1 == 2)
  6435. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6436. else
  6437. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6438. if (clock->p2 == 4)
  6439. dpll |= PLL_P2_DIVIDE_BY_4;
  6440. }
  6441. if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6442. dpll |= DPLL_DVO_2X_MODE;
  6443. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6444. intel_panel_use_ssc(dev_priv))
  6445. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6446. else
  6447. dpll |= PLL_REF_INPUT_DREFCLK;
  6448. dpll |= DPLL_VCO_ENABLE;
  6449. crtc_state->dpll_hw_state.dpll = dpll;
  6450. }
  6451. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6452. {
  6453. struct drm_device *dev = intel_crtc->base.dev;
  6454. struct drm_i915_private *dev_priv = to_i915(dev);
  6455. enum pipe pipe = intel_crtc->pipe;
  6456. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6457. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6458. uint32_t crtc_vtotal, crtc_vblank_end;
  6459. int vsyncshift = 0;
  6460. /* We need to be careful not to changed the adjusted mode, for otherwise
  6461. * the hw state checker will get angry at the mismatch. */
  6462. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6463. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6464. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6465. /* the chip adds 2 halflines automatically */
  6466. crtc_vtotal -= 1;
  6467. crtc_vblank_end -= 1;
  6468. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6469. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6470. else
  6471. vsyncshift = adjusted_mode->crtc_hsync_start -
  6472. adjusted_mode->crtc_htotal / 2;
  6473. if (vsyncshift < 0)
  6474. vsyncshift += adjusted_mode->crtc_htotal;
  6475. }
  6476. if (INTEL_INFO(dev)->gen > 3)
  6477. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6478. I915_WRITE(HTOTAL(cpu_transcoder),
  6479. (adjusted_mode->crtc_hdisplay - 1) |
  6480. ((adjusted_mode->crtc_htotal - 1) << 16));
  6481. I915_WRITE(HBLANK(cpu_transcoder),
  6482. (adjusted_mode->crtc_hblank_start - 1) |
  6483. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6484. I915_WRITE(HSYNC(cpu_transcoder),
  6485. (adjusted_mode->crtc_hsync_start - 1) |
  6486. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6487. I915_WRITE(VTOTAL(cpu_transcoder),
  6488. (adjusted_mode->crtc_vdisplay - 1) |
  6489. ((crtc_vtotal - 1) << 16));
  6490. I915_WRITE(VBLANK(cpu_transcoder),
  6491. (adjusted_mode->crtc_vblank_start - 1) |
  6492. ((crtc_vblank_end - 1) << 16));
  6493. I915_WRITE(VSYNC(cpu_transcoder),
  6494. (adjusted_mode->crtc_vsync_start - 1) |
  6495. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6496. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6497. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6498. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6499. * bits. */
  6500. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6501. (pipe == PIPE_B || pipe == PIPE_C))
  6502. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6503. }
  6504. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6505. {
  6506. struct drm_device *dev = intel_crtc->base.dev;
  6507. struct drm_i915_private *dev_priv = to_i915(dev);
  6508. enum pipe pipe = intel_crtc->pipe;
  6509. /* pipesrc controls the size that is scaled from, which should
  6510. * always be the user's requested size.
  6511. */
  6512. I915_WRITE(PIPESRC(pipe),
  6513. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6514. (intel_crtc->config->pipe_src_h - 1));
  6515. }
  6516. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6517. struct intel_crtc_state *pipe_config)
  6518. {
  6519. struct drm_device *dev = crtc->base.dev;
  6520. struct drm_i915_private *dev_priv = to_i915(dev);
  6521. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6522. uint32_t tmp;
  6523. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6524. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6525. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6526. tmp = I915_READ(HBLANK(cpu_transcoder));
  6527. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6528. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6529. tmp = I915_READ(HSYNC(cpu_transcoder));
  6530. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6531. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6532. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6533. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6534. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6535. tmp = I915_READ(VBLANK(cpu_transcoder));
  6536. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6537. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6538. tmp = I915_READ(VSYNC(cpu_transcoder));
  6539. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6540. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6541. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6542. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6543. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6544. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6545. }
  6546. }
  6547. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6548. struct intel_crtc_state *pipe_config)
  6549. {
  6550. struct drm_device *dev = crtc->base.dev;
  6551. struct drm_i915_private *dev_priv = to_i915(dev);
  6552. u32 tmp;
  6553. tmp = I915_READ(PIPESRC(crtc->pipe));
  6554. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6555. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6556. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6557. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6558. }
  6559. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6560. struct intel_crtc_state *pipe_config)
  6561. {
  6562. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6563. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6564. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6565. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6566. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6567. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6568. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6569. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6570. mode->flags = pipe_config->base.adjusted_mode.flags;
  6571. mode->type = DRM_MODE_TYPE_DRIVER;
  6572. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6573. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6574. mode->hsync = drm_mode_hsync(mode);
  6575. mode->vrefresh = drm_mode_vrefresh(mode);
  6576. drm_mode_set_name(mode);
  6577. }
  6578. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6579. {
  6580. struct drm_device *dev = intel_crtc->base.dev;
  6581. struct drm_i915_private *dev_priv = to_i915(dev);
  6582. uint32_t pipeconf;
  6583. pipeconf = 0;
  6584. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6585. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6586. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6587. if (intel_crtc->config->double_wide)
  6588. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6589. /* only g4x and later have fancy bpc/dither controls */
  6590. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6591. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6592. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6593. pipeconf |= PIPECONF_DITHER_EN |
  6594. PIPECONF_DITHER_TYPE_SP;
  6595. switch (intel_crtc->config->pipe_bpp) {
  6596. case 18:
  6597. pipeconf |= PIPECONF_6BPC;
  6598. break;
  6599. case 24:
  6600. pipeconf |= PIPECONF_8BPC;
  6601. break;
  6602. case 30:
  6603. pipeconf |= PIPECONF_10BPC;
  6604. break;
  6605. default:
  6606. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6607. BUG();
  6608. }
  6609. }
  6610. if (HAS_PIPE_CXSR(dev)) {
  6611. if (intel_crtc->lowfreq_avail) {
  6612. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6613. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6614. } else {
  6615. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6616. }
  6617. }
  6618. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6619. if (INTEL_INFO(dev)->gen < 4 ||
  6620. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6621. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6622. else
  6623. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6624. } else
  6625. pipeconf |= PIPECONF_PROGRESSIVE;
  6626. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6627. intel_crtc->config->limited_color_range)
  6628. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6629. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6630. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6631. }
  6632. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6633. struct intel_crtc_state *crtc_state)
  6634. {
  6635. struct drm_device *dev = crtc->base.dev;
  6636. struct drm_i915_private *dev_priv = to_i915(dev);
  6637. const struct intel_limit *limit;
  6638. int refclk = 48000;
  6639. memset(&crtc_state->dpll_hw_state, 0,
  6640. sizeof(crtc_state->dpll_hw_state));
  6641. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6642. if (intel_panel_use_ssc(dev_priv)) {
  6643. refclk = dev_priv->vbt.lvds_ssc_freq;
  6644. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6645. }
  6646. limit = &intel_limits_i8xx_lvds;
  6647. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6648. limit = &intel_limits_i8xx_dvo;
  6649. } else {
  6650. limit = &intel_limits_i8xx_dac;
  6651. }
  6652. if (!crtc_state->clock_set &&
  6653. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6654. refclk, NULL, &crtc_state->dpll)) {
  6655. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6656. return -EINVAL;
  6657. }
  6658. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6659. return 0;
  6660. }
  6661. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6662. struct intel_crtc_state *crtc_state)
  6663. {
  6664. struct drm_device *dev = crtc->base.dev;
  6665. struct drm_i915_private *dev_priv = to_i915(dev);
  6666. const struct intel_limit *limit;
  6667. int refclk = 96000;
  6668. memset(&crtc_state->dpll_hw_state, 0,
  6669. sizeof(crtc_state->dpll_hw_state));
  6670. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6671. if (intel_panel_use_ssc(dev_priv)) {
  6672. refclk = dev_priv->vbt.lvds_ssc_freq;
  6673. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6674. }
  6675. if (intel_is_dual_link_lvds(dev))
  6676. limit = &intel_limits_g4x_dual_channel_lvds;
  6677. else
  6678. limit = &intel_limits_g4x_single_channel_lvds;
  6679. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6680. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6681. limit = &intel_limits_g4x_hdmi;
  6682. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6683. limit = &intel_limits_g4x_sdvo;
  6684. } else {
  6685. /* The option is for other outputs */
  6686. limit = &intel_limits_i9xx_sdvo;
  6687. }
  6688. if (!crtc_state->clock_set &&
  6689. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6690. refclk, NULL, &crtc_state->dpll)) {
  6691. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6692. return -EINVAL;
  6693. }
  6694. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6695. return 0;
  6696. }
  6697. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6698. struct intel_crtc_state *crtc_state)
  6699. {
  6700. struct drm_device *dev = crtc->base.dev;
  6701. struct drm_i915_private *dev_priv = to_i915(dev);
  6702. const struct intel_limit *limit;
  6703. int refclk = 96000;
  6704. memset(&crtc_state->dpll_hw_state, 0,
  6705. sizeof(crtc_state->dpll_hw_state));
  6706. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6707. if (intel_panel_use_ssc(dev_priv)) {
  6708. refclk = dev_priv->vbt.lvds_ssc_freq;
  6709. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6710. }
  6711. limit = &intel_limits_pineview_lvds;
  6712. } else {
  6713. limit = &intel_limits_pineview_sdvo;
  6714. }
  6715. if (!crtc_state->clock_set &&
  6716. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6717. refclk, NULL, &crtc_state->dpll)) {
  6718. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6719. return -EINVAL;
  6720. }
  6721. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6722. return 0;
  6723. }
  6724. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6725. struct intel_crtc_state *crtc_state)
  6726. {
  6727. struct drm_device *dev = crtc->base.dev;
  6728. struct drm_i915_private *dev_priv = to_i915(dev);
  6729. const struct intel_limit *limit;
  6730. int refclk = 96000;
  6731. memset(&crtc_state->dpll_hw_state, 0,
  6732. sizeof(crtc_state->dpll_hw_state));
  6733. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6734. if (intel_panel_use_ssc(dev_priv)) {
  6735. refclk = dev_priv->vbt.lvds_ssc_freq;
  6736. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6737. }
  6738. limit = &intel_limits_i9xx_lvds;
  6739. } else {
  6740. limit = &intel_limits_i9xx_sdvo;
  6741. }
  6742. if (!crtc_state->clock_set &&
  6743. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6744. refclk, NULL, &crtc_state->dpll)) {
  6745. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6746. return -EINVAL;
  6747. }
  6748. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6749. return 0;
  6750. }
  6751. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6752. struct intel_crtc_state *crtc_state)
  6753. {
  6754. int refclk = 100000;
  6755. const struct intel_limit *limit = &intel_limits_chv;
  6756. memset(&crtc_state->dpll_hw_state, 0,
  6757. sizeof(crtc_state->dpll_hw_state));
  6758. if (!crtc_state->clock_set &&
  6759. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6760. refclk, NULL, &crtc_state->dpll)) {
  6761. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6762. return -EINVAL;
  6763. }
  6764. chv_compute_dpll(crtc, crtc_state);
  6765. return 0;
  6766. }
  6767. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6768. struct intel_crtc_state *crtc_state)
  6769. {
  6770. int refclk = 100000;
  6771. const struct intel_limit *limit = &intel_limits_vlv;
  6772. memset(&crtc_state->dpll_hw_state, 0,
  6773. sizeof(crtc_state->dpll_hw_state));
  6774. if (!crtc_state->clock_set &&
  6775. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6776. refclk, NULL, &crtc_state->dpll)) {
  6777. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6778. return -EINVAL;
  6779. }
  6780. vlv_compute_dpll(crtc, crtc_state);
  6781. return 0;
  6782. }
  6783. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6784. struct intel_crtc_state *pipe_config)
  6785. {
  6786. struct drm_device *dev = crtc->base.dev;
  6787. struct drm_i915_private *dev_priv = to_i915(dev);
  6788. uint32_t tmp;
  6789. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6790. return;
  6791. tmp = I915_READ(PFIT_CONTROL);
  6792. if (!(tmp & PFIT_ENABLE))
  6793. return;
  6794. /* Check whether the pfit is attached to our pipe. */
  6795. if (INTEL_INFO(dev)->gen < 4) {
  6796. if (crtc->pipe != PIPE_B)
  6797. return;
  6798. } else {
  6799. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6800. return;
  6801. }
  6802. pipe_config->gmch_pfit.control = tmp;
  6803. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6804. }
  6805. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6806. struct intel_crtc_state *pipe_config)
  6807. {
  6808. struct drm_device *dev = crtc->base.dev;
  6809. struct drm_i915_private *dev_priv = to_i915(dev);
  6810. int pipe = pipe_config->cpu_transcoder;
  6811. struct dpll clock;
  6812. u32 mdiv;
  6813. int refclk = 100000;
  6814. /* In case of DSI, DPLL will not be used */
  6815. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6816. return;
  6817. mutex_lock(&dev_priv->sb_lock);
  6818. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6819. mutex_unlock(&dev_priv->sb_lock);
  6820. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6821. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6822. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6823. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6824. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6825. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6826. }
  6827. static void
  6828. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6829. struct intel_initial_plane_config *plane_config)
  6830. {
  6831. struct drm_device *dev = crtc->base.dev;
  6832. struct drm_i915_private *dev_priv = to_i915(dev);
  6833. u32 val, base, offset;
  6834. int pipe = crtc->pipe, plane = crtc->plane;
  6835. int fourcc, pixel_format;
  6836. unsigned int aligned_height;
  6837. struct drm_framebuffer *fb;
  6838. struct intel_framebuffer *intel_fb;
  6839. val = I915_READ(DSPCNTR(plane));
  6840. if (!(val & DISPLAY_PLANE_ENABLE))
  6841. return;
  6842. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6843. if (!intel_fb) {
  6844. DRM_DEBUG_KMS("failed to alloc fb\n");
  6845. return;
  6846. }
  6847. fb = &intel_fb->base;
  6848. if (INTEL_INFO(dev)->gen >= 4) {
  6849. if (val & DISPPLANE_TILED) {
  6850. plane_config->tiling = I915_TILING_X;
  6851. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6852. }
  6853. }
  6854. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6855. fourcc = i9xx_format_to_fourcc(pixel_format);
  6856. fb->pixel_format = fourcc;
  6857. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6858. if (INTEL_INFO(dev)->gen >= 4) {
  6859. if (plane_config->tiling)
  6860. offset = I915_READ(DSPTILEOFF(plane));
  6861. else
  6862. offset = I915_READ(DSPLINOFF(plane));
  6863. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6864. } else {
  6865. base = I915_READ(DSPADDR(plane));
  6866. }
  6867. plane_config->base = base;
  6868. val = I915_READ(PIPESRC(pipe));
  6869. fb->width = ((val >> 16) & 0xfff) + 1;
  6870. fb->height = ((val >> 0) & 0xfff) + 1;
  6871. val = I915_READ(DSPSTRIDE(pipe));
  6872. fb->pitches[0] = val & 0xffffffc0;
  6873. aligned_height = intel_fb_align_height(dev, fb->height,
  6874. fb->pixel_format,
  6875. fb->modifier[0]);
  6876. plane_config->size = fb->pitches[0] * aligned_height;
  6877. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6878. pipe_name(pipe), plane, fb->width, fb->height,
  6879. fb->bits_per_pixel, base, fb->pitches[0],
  6880. plane_config->size);
  6881. plane_config->fb = intel_fb;
  6882. }
  6883. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6884. struct intel_crtc_state *pipe_config)
  6885. {
  6886. struct drm_device *dev = crtc->base.dev;
  6887. struct drm_i915_private *dev_priv = to_i915(dev);
  6888. int pipe = pipe_config->cpu_transcoder;
  6889. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6890. struct dpll clock;
  6891. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6892. int refclk = 100000;
  6893. /* In case of DSI, DPLL will not be used */
  6894. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6895. return;
  6896. mutex_lock(&dev_priv->sb_lock);
  6897. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6898. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6899. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6900. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6901. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6902. mutex_unlock(&dev_priv->sb_lock);
  6903. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6904. clock.m2 = (pll_dw0 & 0xff) << 22;
  6905. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6906. clock.m2 |= pll_dw2 & 0x3fffff;
  6907. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6908. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6909. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6910. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6911. }
  6912. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6913. struct intel_crtc_state *pipe_config)
  6914. {
  6915. struct drm_device *dev = crtc->base.dev;
  6916. struct drm_i915_private *dev_priv = to_i915(dev);
  6917. enum intel_display_power_domain power_domain;
  6918. uint32_t tmp;
  6919. bool ret;
  6920. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6921. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6922. return false;
  6923. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6924. pipe_config->shared_dpll = NULL;
  6925. ret = false;
  6926. tmp = I915_READ(PIPECONF(crtc->pipe));
  6927. if (!(tmp & PIPECONF_ENABLE))
  6928. goto out;
  6929. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6930. switch (tmp & PIPECONF_BPC_MASK) {
  6931. case PIPECONF_6BPC:
  6932. pipe_config->pipe_bpp = 18;
  6933. break;
  6934. case PIPECONF_8BPC:
  6935. pipe_config->pipe_bpp = 24;
  6936. break;
  6937. case PIPECONF_10BPC:
  6938. pipe_config->pipe_bpp = 30;
  6939. break;
  6940. default:
  6941. break;
  6942. }
  6943. }
  6944. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6945. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6946. pipe_config->limited_color_range = true;
  6947. if (INTEL_INFO(dev)->gen < 4)
  6948. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6949. intel_get_pipe_timings(crtc, pipe_config);
  6950. intel_get_pipe_src_size(crtc, pipe_config);
  6951. i9xx_get_pfit_config(crtc, pipe_config);
  6952. if (INTEL_INFO(dev)->gen >= 4) {
  6953. /* No way to read it out on pipes B and C */
  6954. if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
  6955. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6956. else
  6957. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6958. pipe_config->pixel_multiplier =
  6959. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6960. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6961. pipe_config->dpll_hw_state.dpll_md = tmp;
  6962. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6963. tmp = I915_READ(DPLL(crtc->pipe));
  6964. pipe_config->pixel_multiplier =
  6965. ((tmp & SDVO_MULTIPLIER_MASK)
  6966. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6967. } else {
  6968. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6969. * port and will be fixed up in the encoder->get_config
  6970. * function. */
  6971. pipe_config->pixel_multiplier = 1;
  6972. }
  6973. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6974. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  6975. /*
  6976. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6977. * on 830. Filter it out here so that we don't
  6978. * report errors due to that.
  6979. */
  6980. if (IS_I830(dev))
  6981. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6982. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6983. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6984. } else {
  6985. /* Mask out read-only status bits. */
  6986. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6987. DPLL_PORTC_READY_MASK |
  6988. DPLL_PORTB_READY_MASK);
  6989. }
  6990. if (IS_CHERRYVIEW(dev))
  6991. chv_crtc_clock_get(crtc, pipe_config);
  6992. else if (IS_VALLEYVIEW(dev))
  6993. vlv_crtc_clock_get(crtc, pipe_config);
  6994. else
  6995. i9xx_crtc_clock_get(crtc, pipe_config);
  6996. /*
  6997. * Normally the dotclock is filled in by the encoder .get_config()
  6998. * but in case the pipe is enabled w/o any ports we need a sane
  6999. * default.
  7000. */
  7001. pipe_config->base.adjusted_mode.crtc_clock =
  7002. pipe_config->port_clock / pipe_config->pixel_multiplier;
  7003. ret = true;
  7004. out:
  7005. intel_display_power_put(dev_priv, power_domain);
  7006. return ret;
  7007. }
  7008. static void ironlake_init_pch_refclk(struct drm_device *dev)
  7009. {
  7010. struct drm_i915_private *dev_priv = to_i915(dev);
  7011. struct intel_encoder *encoder;
  7012. int i;
  7013. u32 val, final;
  7014. bool has_lvds = false;
  7015. bool has_cpu_edp = false;
  7016. bool has_panel = false;
  7017. bool has_ck505 = false;
  7018. bool can_ssc = false;
  7019. bool using_ssc_source = false;
  7020. /* We need to take the global config into account */
  7021. for_each_intel_encoder(dev, encoder) {
  7022. switch (encoder->type) {
  7023. case INTEL_OUTPUT_LVDS:
  7024. has_panel = true;
  7025. has_lvds = true;
  7026. break;
  7027. case INTEL_OUTPUT_EDP:
  7028. has_panel = true;
  7029. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7030. has_cpu_edp = true;
  7031. break;
  7032. default:
  7033. break;
  7034. }
  7035. }
  7036. if (HAS_PCH_IBX(dev)) {
  7037. has_ck505 = dev_priv->vbt.display_clock_mode;
  7038. can_ssc = has_ck505;
  7039. } else {
  7040. has_ck505 = false;
  7041. can_ssc = true;
  7042. }
  7043. /* Check if any DPLLs are using the SSC source */
  7044. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7045. u32 temp = I915_READ(PCH_DPLL(i));
  7046. if (!(temp & DPLL_VCO_ENABLE))
  7047. continue;
  7048. if ((temp & PLL_REF_INPUT_MASK) ==
  7049. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7050. using_ssc_source = true;
  7051. break;
  7052. }
  7053. }
  7054. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7055. has_panel, has_lvds, has_ck505, using_ssc_source);
  7056. /* Ironlake: try to setup display ref clock before DPLL
  7057. * enabling. This is only under driver's control after
  7058. * PCH B stepping, previous chipset stepping should be
  7059. * ignoring this setting.
  7060. */
  7061. val = I915_READ(PCH_DREF_CONTROL);
  7062. /* As we must carefully and slowly disable/enable each source in turn,
  7063. * compute the final state we want first and check if we need to
  7064. * make any changes at all.
  7065. */
  7066. final = val;
  7067. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7068. if (has_ck505)
  7069. final |= DREF_NONSPREAD_CK505_ENABLE;
  7070. else
  7071. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7072. final &= ~DREF_SSC_SOURCE_MASK;
  7073. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7074. final &= ~DREF_SSC1_ENABLE;
  7075. if (has_panel) {
  7076. final |= DREF_SSC_SOURCE_ENABLE;
  7077. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7078. final |= DREF_SSC1_ENABLE;
  7079. if (has_cpu_edp) {
  7080. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7081. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7082. else
  7083. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7084. } else
  7085. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7086. } else if (using_ssc_source) {
  7087. final |= DREF_SSC_SOURCE_ENABLE;
  7088. final |= DREF_SSC1_ENABLE;
  7089. }
  7090. if (final == val)
  7091. return;
  7092. /* Always enable nonspread source */
  7093. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7094. if (has_ck505)
  7095. val |= DREF_NONSPREAD_CK505_ENABLE;
  7096. else
  7097. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7098. if (has_panel) {
  7099. val &= ~DREF_SSC_SOURCE_MASK;
  7100. val |= DREF_SSC_SOURCE_ENABLE;
  7101. /* SSC must be turned on before enabling the CPU output */
  7102. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7103. DRM_DEBUG_KMS("Using SSC on panel\n");
  7104. val |= DREF_SSC1_ENABLE;
  7105. } else
  7106. val &= ~DREF_SSC1_ENABLE;
  7107. /* Get SSC going before enabling the outputs */
  7108. I915_WRITE(PCH_DREF_CONTROL, val);
  7109. POSTING_READ(PCH_DREF_CONTROL);
  7110. udelay(200);
  7111. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7112. /* Enable CPU source on CPU attached eDP */
  7113. if (has_cpu_edp) {
  7114. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7115. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7116. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7117. } else
  7118. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7119. } else
  7120. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7121. I915_WRITE(PCH_DREF_CONTROL, val);
  7122. POSTING_READ(PCH_DREF_CONTROL);
  7123. udelay(200);
  7124. } else {
  7125. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7126. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7127. /* Turn off CPU output */
  7128. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7129. I915_WRITE(PCH_DREF_CONTROL, val);
  7130. POSTING_READ(PCH_DREF_CONTROL);
  7131. udelay(200);
  7132. if (!using_ssc_source) {
  7133. DRM_DEBUG_KMS("Disabling SSC source\n");
  7134. /* Turn off the SSC source */
  7135. val &= ~DREF_SSC_SOURCE_MASK;
  7136. val |= DREF_SSC_SOURCE_DISABLE;
  7137. /* Turn off SSC1 */
  7138. val &= ~DREF_SSC1_ENABLE;
  7139. I915_WRITE(PCH_DREF_CONTROL, val);
  7140. POSTING_READ(PCH_DREF_CONTROL);
  7141. udelay(200);
  7142. }
  7143. }
  7144. BUG_ON(val != final);
  7145. }
  7146. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7147. {
  7148. uint32_t tmp;
  7149. tmp = I915_READ(SOUTH_CHICKEN2);
  7150. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7151. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7152. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  7153. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7154. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7155. tmp = I915_READ(SOUTH_CHICKEN2);
  7156. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7157. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7158. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  7159. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7160. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7161. }
  7162. /* WaMPhyProgramming:hsw */
  7163. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7164. {
  7165. uint32_t tmp;
  7166. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7167. tmp &= ~(0xFF << 24);
  7168. tmp |= (0x12 << 24);
  7169. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7170. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7171. tmp |= (1 << 11);
  7172. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7173. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7174. tmp |= (1 << 11);
  7175. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7176. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7177. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7178. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7179. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7180. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7181. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7182. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7183. tmp &= ~(7 << 13);
  7184. tmp |= (5 << 13);
  7185. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7186. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7187. tmp &= ~(7 << 13);
  7188. tmp |= (5 << 13);
  7189. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7190. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7191. tmp &= ~0xFF;
  7192. tmp |= 0x1C;
  7193. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7194. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7195. tmp &= ~0xFF;
  7196. tmp |= 0x1C;
  7197. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7198. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7199. tmp &= ~(0xFF << 16);
  7200. tmp |= (0x1C << 16);
  7201. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7202. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7203. tmp &= ~(0xFF << 16);
  7204. tmp |= (0x1C << 16);
  7205. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7206. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7207. tmp |= (1 << 27);
  7208. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7209. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7210. tmp |= (1 << 27);
  7211. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7212. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7213. tmp &= ~(0xF << 28);
  7214. tmp |= (4 << 28);
  7215. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7216. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7217. tmp &= ~(0xF << 28);
  7218. tmp |= (4 << 28);
  7219. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7220. }
  7221. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7222. * Programming" based on the parameters passed:
  7223. * - Sequence to enable CLKOUT_DP
  7224. * - Sequence to enable CLKOUT_DP without spread
  7225. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7226. */
  7227. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7228. bool with_fdi)
  7229. {
  7230. struct drm_i915_private *dev_priv = to_i915(dev);
  7231. uint32_t reg, tmp;
  7232. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7233. with_spread = true;
  7234. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7235. with_fdi = false;
  7236. mutex_lock(&dev_priv->sb_lock);
  7237. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7238. tmp &= ~SBI_SSCCTL_DISABLE;
  7239. tmp |= SBI_SSCCTL_PATHALT;
  7240. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7241. udelay(24);
  7242. if (with_spread) {
  7243. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7244. tmp &= ~SBI_SSCCTL_PATHALT;
  7245. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7246. if (with_fdi) {
  7247. lpt_reset_fdi_mphy(dev_priv);
  7248. lpt_program_fdi_mphy(dev_priv);
  7249. }
  7250. }
  7251. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7252. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7253. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7254. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7255. mutex_unlock(&dev_priv->sb_lock);
  7256. }
  7257. /* Sequence to disable CLKOUT_DP */
  7258. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7259. {
  7260. struct drm_i915_private *dev_priv = to_i915(dev);
  7261. uint32_t reg, tmp;
  7262. mutex_lock(&dev_priv->sb_lock);
  7263. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7264. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7265. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7266. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7267. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7268. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7269. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7270. tmp |= SBI_SSCCTL_PATHALT;
  7271. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7272. udelay(32);
  7273. }
  7274. tmp |= SBI_SSCCTL_DISABLE;
  7275. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7276. }
  7277. mutex_unlock(&dev_priv->sb_lock);
  7278. }
  7279. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7280. static const uint16_t sscdivintphase[] = {
  7281. [BEND_IDX( 50)] = 0x3B23,
  7282. [BEND_IDX( 45)] = 0x3B23,
  7283. [BEND_IDX( 40)] = 0x3C23,
  7284. [BEND_IDX( 35)] = 0x3C23,
  7285. [BEND_IDX( 30)] = 0x3D23,
  7286. [BEND_IDX( 25)] = 0x3D23,
  7287. [BEND_IDX( 20)] = 0x3E23,
  7288. [BEND_IDX( 15)] = 0x3E23,
  7289. [BEND_IDX( 10)] = 0x3F23,
  7290. [BEND_IDX( 5)] = 0x3F23,
  7291. [BEND_IDX( 0)] = 0x0025,
  7292. [BEND_IDX( -5)] = 0x0025,
  7293. [BEND_IDX(-10)] = 0x0125,
  7294. [BEND_IDX(-15)] = 0x0125,
  7295. [BEND_IDX(-20)] = 0x0225,
  7296. [BEND_IDX(-25)] = 0x0225,
  7297. [BEND_IDX(-30)] = 0x0325,
  7298. [BEND_IDX(-35)] = 0x0325,
  7299. [BEND_IDX(-40)] = 0x0425,
  7300. [BEND_IDX(-45)] = 0x0425,
  7301. [BEND_IDX(-50)] = 0x0525,
  7302. };
  7303. /*
  7304. * Bend CLKOUT_DP
  7305. * steps -50 to 50 inclusive, in steps of 5
  7306. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7307. * change in clock period = -(steps / 10) * 5.787 ps
  7308. */
  7309. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7310. {
  7311. uint32_t tmp;
  7312. int idx = BEND_IDX(steps);
  7313. if (WARN_ON(steps % 5 != 0))
  7314. return;
  7315. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7316. return;
  7317. mutex_lock(&dev_priv->sb_lock);
  7318. if (steps % 10 != 0)
  7319. tmp = 0xAAAAAAAB;
  7320. else
  7321. tmp = 0x00000000;
  7322. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7323. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7324. tmp &= 0xffff0000;
  7325. tmp |= sscdivintphase[idx];
  7326. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7327. mutex_unlock(&dev_priv->sb_lock);
  7328. }
  7329. #undef BEND_IDX
  7330. static void lpt_init_pch_refclk(struct drm_device *dev)
  7331. {
  7332. struct intel_encoder *encoder;
  7333. bool has_vga = false;
  7334. for_each_intel_encoder(dev, encoder) {
  7335. switch (encoder->type) {
  7336. case INTEL_OUTPUT_ANALOG:
  7337. has_vga = true;
  7338. break;
  7339. default:
  7340. break;
  7341. }
  7342. }
  7343. if (has_vga) {
  7344. lpt_bend_clkout_dp(to_i915(dev), 0);
  7345. lpt_enable_clkout_dp(dev, true, true);
  7346. } else {
  7347. lpt_disable_clkout_dp(dev);
  7348. }
  7349. }
  7350. /*
  7351. * Initialize reference clocks when the driver loads
  7352. */
  7353. void intel_init_pch_refclk(struct drm_device *dev)
  7354. {
  7355. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7356. ironlake_init_pch_refclk(dev);
  7357. else if (HAS_PCH_LPT(dev))
  7358. lpt_init_pch_refclk(dev);
  7359. }
  7360. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7361. {
  7362. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7363. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7364. int pipe = intel_crtc->pipe;
  7365. uint32_t val;
  7366. val = 0;
  7367. switch (intel_crtc->config->pipe_bpp) {
  7368. case 18:
  7369. val |= PIPECONF_6BPC;
  7370. break;
  7371. case 24:
  7372. val |= PIPECONF_8BPC;
  7373. break;
  7374. case 30:
  7375. val |= PIPECONF_10BPC;
  7376. break;
  7377. case 36:
  7378. val |= PIPECONF_12BPC;
  7379. break;
  7380. default:
  7381. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7382. BUG();
  7383. }
  7384. if (intel_crtc->config->dither)
  7385. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7386. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7387. val |= PIPECONF_INTERLACED_ILK;
  7388. else
  7389. val |= PIPECONF_PROGRESSIVE;
  7390. if (intel_crtc->config->limited_color_range)
  7391. val |= PIPECONF_COLOR_RANGE_SELECT;
  7392. I915_WRITE(PIPECONF(pipe), val);
  7393. POSTING_READ(PIPECONF(pipe));
  7394. }
  7395. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7396. {
  7397. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7398. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7399. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7400. u32 val = 0;
  7401. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7402. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7403. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7404. val |= PIPECONF_INTERLACED_ILK;
  7405. else
  7406. val |= PIPECONF_PROGRESSIVE;
  7407. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7408. POSTING_READ(PIPECONF(cpu_transcoder));
  7409. }
  7410. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7411. {
  7412. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7413. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7414. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7415. u32 val = 0;
  7416. switch (intel_crtc->config->pipe_bpp) {
  7417. case 18:
  7418. val |= PIPEMISC_DITHER_6_BPC;
  7419. break;
  7420. case 24:
  7421. val |= PIPEMISC_DITHER_8_BPC;
  7422. break;
  7423. case 30:
  7424. val |= PIPEMISC_DITHER_10_BPC;
  7425. break;
  7426. case 36:
  7427. val |= PIPEMISC_DITHER_12_BPC;
  7428. break;
  7429. default:
  7430. /* Case prevented by pipe_config_set_bpp. */
  7431. BUG();
  7432. }
  7433. if (intel_crtc->config->dither)
  7434. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7435. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7436. }
  7437. }
  7438. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7439. {
  7440. /*
  7441. * Account for spread spectrum to avoid
  7442. * oversubscribing the link. Max center spread
  7443. * is 2.5%; use 5% for safety's sake.
  7444. */
  7445. u32 bps = target_clock * bpp * 21 / 20;
  7446. return DIV_ROUND_UP(bps, link_bw * 8);
  7447. }
  7448. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7449. {
  7450. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7451. }
  7452. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7453. struct intel_crtc_state *crtc_state,
  7454. struct dpll *reduced_clock)
  7455. {
  7456. struct drm_crtc *crtc = &intel_crtc->base;
  7457. struct drm_device *dev = crtc->dev;
  7458. struct drm_i915_private *dev_priv = to_i915(dev);
  7459. u32 dpll, fp, fp2;
  7460. int factor;
  7461. /* Enable autotuning of the PLL clock (if permissible) */
  7462. factor = 21;
  7463. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7464. if ((intel_panel_use_ssc(dev_priv) &&
  7465. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7466. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7467. factor = 25;
  7468. } else if (crtc_state->sdvo_tv_clock)
  7469. factor = 20;
  7470. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7471. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7472. fp |= FP_CB_TUNE;
  7473. if (reduced_clock) {
  7474. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7475. if (reduced_clock->m < factor * reduced_clock->n)
  7476. fp2 |= FP_CB_TUNE;
  7477. } else {
  7478. fp2 = fp;
  7479. }
  7480. dpll = 0;
  7481. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7482. dpll |= DPLLB_MODE_LVDS;
  7483. else
  7484. dpll |= DPLLB_MODE_DAC_SERIAL;
  7485. dpll |= (crtc_state->pixel_multiplier - 1)
  7486. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7487. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7488. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7489. dpll |= DPLL_SDVO_HIGH_SPEED;
  7490. if (intel_crtc_has_dp_encoder(crtc_state))
  7491. dpll |= DPLL_SDVO_HIGH_SPEED;
  7492. /* compute bitmask from p1 value */
  7493. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7494. /* also FPA1 */
  7495. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7496. switch (crtc_state->dpll.p2) {
  7497. case 5:
  7498. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7499. break;
  7500. case 7:
  7501. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7502. break;
  7503. case 10:
  7504. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7505. break;
  7506. case 14:
  7507. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7508. break;
  7509. }
  7510. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7511. intel_panel_use_ssc(dev_priv))
  7512. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7513. else
  7514. dpll |= PLL_REF_INPUT_DREFCLK;
  7515. dpll |= DPLL_VCO_ENABLE;
  7516. crtc_state->dpll_hw_state.dpll = dpll;
  7517. crtc_state->dpll_hw_state.fp0 = fp;
  7518. crtc_state->dpll_hw_state.fp1 = fp2;
  7519. }
  7520. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7521. struct intel_crtc_state *crtc_state)
  7522. {
  7523. struct drm_device *dev = crtc->base.dev;
  7524. struct drm_i915_private *dev_priv = to_i915(dev);
  7525. struct dpll reduced_clock;
  7526. bool has_reduced_clock = false;
  7527. struct intel_shared_dpll *pll;
  7528. const struct intel_limit *limit;
  7529. int refclk = 120000;
  7530. memset(&crtc_state->dpll_hw_state, 0,
  7531. sizeof(crtc_state->dpll_hw_state));
  7532. crtc->lowfreq_avail = false;
  7533. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7534. if (!crtc_state->has_pch_encoder)
  7535. return 0;
  7536. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7537. if (intel_panel_use_ssc(dev_priv)) {
  7538. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7539. dev_priv->vbt.lvds_ssc_freq);
  7540. refclk = dev_priv->vbt.lvds_ssc_freq;
  7541. }
  7542. if (intel_is_dual_link_lvds(dev)) {
  7543. if (refclk == 100000)
  7544. limit = &intel_limits_ironlake_dual_lvds_100m;
  7545. else
  7546. limit = &intel_limits_ironlake_dual_lvds;
  7547. } else {
  7548. if (refclk == 100000)
  7549. limit = &intel_limits_ironlake_single_lvds_100m;
  7550. else
  7551. limit = &intel_limits_ironlake_single_lvds;
  7552. }
  7553. } else {
  7554. limit = &intel_limits_ironlake_dac;
  7555. }
  7556. if (!crtc_state->clock_set &&
  7557. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7558. refclk, NULL, &crtc_state->dpll)) {
  7559. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7560. return -EINVAL;
  7561. }
  7562. ironlake_compute_dpll(crtc, crtc_state,
  7563. has_reduced_clock ? &reduced_clock : NULL);
  7564. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  7565. if (pll == NULL) {
  7566. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7567. pipe_name(crtc->pipe));
  7568. return -EINVAL;
  7569. }
  7570. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7571. has_reduced_clock)
  7572. crtc->lowfreq_avail = true;
  7573. return 0;
  7574. }
  7575. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7576. struct intel_link_m_n *m_n)
  7577. {
  7578. struct drm_device *dev = crtc->base.dev;
  7579. struct drm_i915_private *dev_priv = to_i915(dev);
  7580. enum pipe pipe = crtc->pipe;
  7581. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7582. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7583. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7584. & ~TU_SIZE_MASK;
  7585. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7586. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7587. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7588. }
  7589. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7590. enum transcoder transcoder,
  7591. struct intel_link_m_n *m_n,
  7592. struct intel_link_m_n *m2_n2)
  7593. {
  7594. struct drm_device *dev = crtc->base.dev;
  7595. struct drm_i915_private *dev_priv = to_i915(dev);
  7596. enum pipe pipe = crtc->pipe;
  7597. if (INTEL_INFO(dev)->gen >= 5) {
  7598. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7599. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7600. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7601. & ~TU_SIZE_MASK;
  7602. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7603. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7604. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7605. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7606. * gen < 8) and if DRRS is supported (to make sure the
  7607. * registers are not unnecessarily read).
  7608. */
  7609. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7610. crtc->config->has_drrs) {
  7611. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7612. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7613. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7614. & ~TU_SIZE_MASK;
  7615. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7616. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7617. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7618. }
  7619. } else {
  7620. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7621. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7622. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7623. & ~TU_SIZE_MASK;
  7624. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7625. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7626. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7627. }
  7628. }
  7629. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7630. struct intel_crtc_state *pipe_config)
  7631. {
  7632. if (pipe_config->has_pch_encoder)
  7633. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7634. else
  7635. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7636. &pipe_config->dp_m_n,
  7637. &pipe_config->dp_m2_n2);
  7638. }
  7639. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7640. struct intel_crtc_state *pipe_config)
  7641. {
  7642. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7643. &pipe_config->fdi_m_n, NULL);
  7644. }
  7645. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7646. struct intel_crtc_state *pipe_config)
  7647. {
  7648. struct drm_device *dev = crtc->base.dev;
  7649. struct drm_i915_private *dev_priv = to_i915(dev);
  7650. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7651. uint32_t ps_ctrl = 0;
  7652. int id = -1;
  7653. int i;
  7654. /* find scaler attached to this pipe */
  7655. for (i = 0; i < crtc->num_scalers; i++) {
  7656. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7657. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7658. id = i;
  7659. pipe_config->pch_pfit.enabled = true;
  7660. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7661. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7662. break;
  7663. }
  7664. }
  7665. scaler_state->scaler_id = id;
  7666. if (id >= 0) {
  7667. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7668. } else {
  7669. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7670. }
  7671. }
  7672. static void
  7673. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7674. struct intel_initial_plane_config *plane_config)
  7675. {
  7676. struct drm_device *dev = crtc->base.dev;
  7677. struct drm_i915_private *dev_priv = to_i915(dev);
  7678. u32 val, base, offset, stride_mult, tiling;
  7679. int pipe = crtc->pipe;
  7680. int fourcc, pixel_format;
  7681. unsigned int aligned_height;
  7682. struct drm_framebuffer *fb;
  7683. struct intel_framebuffer *intel_fb;
  7684. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7685. if (!intel_fb) {
  7686. DRM_DEBUG_KMS("failed to alloc fb\n");
  7687. return;
  7688. }
  7689. fb = &intel_fb->base;
  7690. val = I915_READ(PLANE_CTL(pipe, 0));
  7691. if (!(val & PLANE_CTL_ENABLE))
  7692. goto error;
  7693. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7694. fourcc = skl_format_to_fourcc(pixel_format,
  7695. val & PLANE_CTL_ORDER_RGBX,
  7696. val & PLANE_CTL_ALPHA_MASK);
  7697. fb->pixel_format = fourcc;
  7698. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7699. tiling = val & PLANE_CTL_TILED_MASK;
  7700. switch (tiling) {
  7701. case PLANE_CTL_TILED_LINEAR:
  7702. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7703. break;
  7704. case PLANE_CTL_TILED_X:
  7705. plane_config->tiling = I915_TILING_X;
  7706. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7707. break;
  7708. case PLANE_CTL_TILED_Y:
  7709. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7710. break;
  7711. case PLANE_CTL_TILED_YF:
  7712. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7713. break;
  7714. default:
  7715. MISSING_CASE(tiling);
  7716. goto error;
  7717. }
  7718. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7719. plane_config->base = base;
  7720. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7721. val = I915_READ(PLANE_SIZE(pipe, 0));
  7722. fb->height = ((val >> 16) & 0xfff) + 1;
  7723. fb->width = ((val >> 0) & 0x1fff) + 1;
  7724. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7725. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  7726. fb->pixel_format);
  7727. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7728. aligned_height = intel_fb_align_height(dev, fb->height,
  7729. fb->pixel_format,
  7730. fb->modifier[0]);
  7731. plane_config->size = fb->pitches[0] * aligned_height;
  7732. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7733. pipe_name(pipe), fb->width, fb->height,
  7734. fb->bits_per_pixel, base, fb->pitches[0],
  7735. plane_config->size);
  7736. plane_config->fb = intel_fb;
  7737. return;
  7738. error:
  7739. kfree(fb);
  7740. }
  7741. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7742. struct intel_crtc_state *pipe_config)
  7743. {
  7744. struct drm_device *dev = crtc->base.dev;
  7745. struct drm_i915_private *dev_priv = to_i915(dev);
  7746. uint32_t tmp;
  7747. tmp = I915_READ(PF_CTL(crtc->pipe));
  7748. if (tmp & PF_ENABLE) {
  7749. pipe_config->pch_pfit.enabled = true;
  7750. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7751. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7752. /* We currently do not free assignements of panel fitters on
  7753. * ivb/hsw (since we don't use the higher upscaling modes which
  7754. * differentiates them) so just WARN about this case for now. */
  7755. if (IS_GEN7(dev)) {
  7756. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7757. PF_PIPE_SEL_IVB(crtc->pipe));
  7758. }
  7759. }
  7760. }
  7761. static void
  7762. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7763. struct intel_initial_plane_config *plane_config)
  7764. {
  7765. struct drm_device *dev = crtc->base.dev;
  7766. struct drm_i915_private *dev_priv = to_i915(dev);
  7767. u32 val, base, offset;
  7768. int pipe = crtc->pipe;
  7769. int fourcc, pixel_format;
  7770. unsigned int aligned_height;
  7771. struct drm_framebuffer *fb;
  7772. struct intel_framebuffer *intel_fb;
  7773. val = I915_READ(DSPCNTR(pipe));
  7774. if (!(val & DISPLAY_PLANE_ENABLE))
  7775. return;
  7776. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7777. if (!intel_fb) {
  7778. DRM_DEBUG_KMS("failed to alloc fb\n");
  7779. return;
  7780. }
  7781. fb = &intel_fb->base;
  7782. if (INTEL_INFO(dev)->gen >= 4) {
  7783. if (val & DISPPLANE_TILED) {
  7784. plane_config->tiling = I915_TILING_X;
  7785. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7786. }
  7787. }
  7788. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7789. fourcc = i9xx_format_to_fourcc(pixel_format);
  7790. fb->pixel_format = fourcc;
  7791. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7792. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7793. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7794. offset = I915_READ(DSPOFFSET(pipe));
  7795. } else {
  7796. if (plane_config->tiling)
  7797. offset = I915_READ(DSPTILEOFF(pipe));
  7798. else
  7799. offset = I915_READ(DSPLINOFF(pipe));
  7800. }
  7801. plane_config->base = base;
  7802. val = I915_READ(PIPESRC(pipe));
  7803. fb->width = ((val >> 16) & 0xfff) + 1;
  7804. fb->height = ((val >> 0) & 0xfff) + 1;
  7805. val = I915_READ(DSPSTRIDE(pipe));
  7806. fb->pitches[0] = val & 0xffffffc0;
  7807. aligned_height = intel_fb_align_height(dev, fb->height,
  7808. fb->pixel_format,
  7809. fb->modifier[0]);
  7810. plane_config->size = fb->pitches[0] * aligned_height;
  7811. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7812. pipe_name(pipe), fb->width, fb->height,
  7813. fb->bits_per_pixel, base, fb->pitches[0],
  7814. plane_config->size);
  7815. plane_config->fb = intel_fb;
  7816. }
  7817. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7818. struct intel_crtc_state *pipe_config)
  7819. {
  7820. struct drm_device *dev = crtc->base.dev;
  7821. struct drm_i915_private *dev_priv = to_i915(dev);
  7822. enum intel_display_power_domain power_domain;
  7823. uint32_t tmp;
  7824. bool ret;
  7825. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7826. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7827. return false;
  7828. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7829. pipe_config->shared_dpll = NULL;
  7830. ret = false;
  7831. tmp = I915_READ(PIPECONF(crtc->pipe));
  7832. if (!(tmp & PIPECONF_ENABLE))
  7833. goto out;
  7834. switch (tmp & PIPECONF_BPC_MASK) {
  7835. case PIPECONF_6BPC:
  7836. pipe_config->pipe_bpp = 18;
  7837. break;
  7838. case PIPECONF_8BPC:
  7839. pipe_config->pipe_bpp = 24;
  7840. break;
  7841. case PIPECONF_10BPC:
  7842. pipe_config->pipe_bpp = 30;
  7843. break;
  7844. case PIPECONF_12BPC:
  7845. pipe_config->pipe_bpp = 36;
  7846. break;
  7847. default:
  7848. break;
  7849. }
  7850. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7851. pipe_config->limited_color_range = true;
  7852. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7853. struct intel_shared_dpll *pll;
  7854. enum intel_dpll_id pll_id;
  7855. pipe_config->has_pch_encoder = true;
  7856. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7857. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7858. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7859. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7860. if (HAS_PCH_IBX(dev_priv)) {
  7861. /*
  7862. * The pipe->pch transcoder and pch transcoder->pll
  7863. * mapping is fixed.
  7864. */
  7865. pll_id = (enum intel_dpll_id) crtc->pipe;
  7866. } else {
  7867. tmp = I915_READ(PCH_DPLL_SEL);
  7868. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7869. pll_id = DPLL_ID_PCH_PLL_B;
  7870. else
  7871. pll_id= DPLL_ID_PCH_PLL_A;
  7872. }
  7873. pipe_config->shared_dpll =
  7874. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7875. pll = pipe_config->shared_dpll;
  7876. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7877. &pipe_config->dpll_hw_state));
  7878. tmp = pipe_config->dpll_hw_state.dpll;
  7879. pipe_config->pixel_multiplier =
  7880. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7881. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7882. ironlake_pch_clock_get(crtc, pipe_config);
  7883. } else {
  7884. pipe_config->pixel_multiplier = 1;
  7885. }
  7886. intel_get_pipe_timings(crtc, pipe_config);
  7887. intel_get_pipe_src_size(crtc, pipe_config);
  7888. ironlake_get_pfit_config(crtc, pipe_config);
  7889. ret = true;
  7890. out:
  7891. intel_display_power_put(dev_priv, power_domain);
  7892. return ret;
  7893. }
  7894. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7895. {
  7896. struct drm_device *dev = &dev_priv->drm;
  7897. struct intel_crtc *crtc;
  7898. for_each_intel_crtc(dev, crtc)
  7899. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7900. pipe_name(crtc->pipe));
  7901. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7902. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7903. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7904. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7905. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7906. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7907. "CPU PWM1 enabled\n");
  7908. if (IS_HASWELL(dev))
  7909. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7910. "CPU PWM2 enabled\n");
  7911. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7912. "PCH PWM1 enabled\n");
  7913. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7914. "Utility pin enabled\n");
  7915. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7916. /*
  7917. * In theory we can still leave IRQs enabled, as long as only the HPD
  7918. * interrupts remain enabled. We used to check for that, but since it's
  7919. * gen-specific and since we only disable LCPLL after we fully disable
  7920. * the interrupts, the check below should be enough.
  7921. */
  7922. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7923. }
  7924. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7925. {
  7926. struct drm_device *dev = &dev_priv->drm;
  7927. if (IS_HASWELL(dev))
  7928. return I915_READ(D_COMP_HSW);
  7929. else
  7930. return I915_READ(D_COMP_BDW);
  7931. }
  7932. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7933. {
  7934. struct drm_device *dev = &dev_priv->drm;
  7935. if (IS_HASWELL(dev)) {
  7936. mutex_lock(&dev_priv->rps.hw_lock);
  7937. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7938. val))
  7939. DRM_ERROR("Failed to write to D_COMP\n");
  7940. mutex_unlock(&dev_priv->rps.hw_lock);
  7941. } else {
  7942. I915_WRITE(D_COMP_BDW, val);
  7943. POSTING_READ(D_COMP_BDW);
  7944. }
  7945. }
  7946. /*
  7947. * This function implements pieces of two sequences from BSpec:
  7948. * - Sequence for display software to disable LCPLL
  7949. * - Sequence for display software to allow package C8+
  7950. * The steps implemented here are just the steps that actually touch the LCPLL
  7951. * register. Callers should take care of disabling all the display engine
  7952. * functions, doing the mode unset, fixing interrupts, etc.
  7953. */
  7954. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7955. bool switch_to_fclk, bool allow_power_down)
  7956. {
  7957. uint32_t val;
  7958. assert_can_disable_lcpll(dev_priv);
  7959. val = I915_READ(LCPLL_CTL);
  7960. if (switch_to_fclk) {
  7961. val |= LCPLL_CD_SOURCE_FCLK;
  7962. I915_WRITE(LCPLL_CTL, val);
  7963. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7964. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7965. DRM_ERROR("Switching to FCLK failed\n");
  7966. val = I915_READ(LCPLL_CTL);
  7967. }
  7968. val |= LCPLL_PLL_DISABLE;
  7969. I915_WRITE(LCPLL_CTL, val);
  7970. POSTING_READ(LCPLL_CTL);
  7971. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7972. DRM_ERROR("LCPLL still locked\n");
  7973. val = hsw_read_dcomp(dev_priv);
  7974. val |= D_COMP_COMP_DISABLE;
  7975. hsw_write_dcomp(dev_priv, val);
  7976. ndelay(100);
  7977. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7978. 1))
  7979. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7980. if (allow_power_down) {
  7981. val = I915_READ(LCPLL_CTL);
  7982. val |= LCPLL_POWER_DOWN_ALLOW;
  7983. I915_WRITE(LCPLL_CTL, val);
  7984. POSTING_READ(LCPLL_CTL);
  7985. }
  7986. }
  7987. /*
  7988. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7989. * source.
  7990. */
  7991. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7992. {
  7993. uint32_t val;
  7994. val = I915_READ(LCPLL_CTL);
  7995. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7996. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7997. return;
  7998. /*
  7999. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8000. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8001. */
  8002. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8003. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8004. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8005. I915_WRITE(LCPLL_CTL, val);
  8006. POSTING_READ(LCPLL_CTL);
  8007. }
  8008. val = hsw_read_dcomp(dev_priv);
  8009. val |= D_COMP_COMP_FORCE;
  8010. val &= ~D_COMP_COMP_DISABLE;
  8011. hsw_write_dcomp(dev_priv, val);
  8012. val = I915_READ(LCPLL_CTL);
  8013. val &= ~LCPLL_PLL_DISABLE;
  8014. I915_WRITE(LCPLL_CTL, val);
  8015. if (intel_wait_for_register(dev_priv,
  8016. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  8017. 5))
  8018. DRM_ERROR("LCPLL not locked yet\n");
  8019. if (val & LCPLL_CD_SOURCE_FCLK) {
  8020. val = I915_READ(LCPLL_CTL);
  8021. val &= ~LCPLL_CD_SOURCE_FCLK;
  8022. I915_WRITE(LCPLL_CTL, val);
  8023. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8024. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8025. DRM_ERROR("Switching back to LCPLL failed\n");
  8026. }
  8027. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8028. intel_update_cdclk(&dev_priv->drm);
  8029. }
  8030. /*
  8031. * Package states C8 and deeper are really deep PC states that can only be
  8032. * reached when all the devices on the system allow it, so even if the graphics
  8033. * device allows PC8+, it doesn't mean the system will actually get to these
  8034. * states. Our driver only allows PC8+ when going into runtime PM.
  8035. *
  8036. * The requirements for PC8+ are that all the outputs are disabled, the power
  8037. * well is disabled and most interrupts are disabled, and these are also
  8038. * requirements for runtime PM. When these conditions are met, we manually do
  8039. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8040. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8041. * hang the machine.
  8042. *
  8043. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8044. * the state of some registers, so when we come back from PC8+ we need to
  8045. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8046. * need to take care of the registers kept by RC6. Notice that this happens even
  8047. * if we don't put the device in PCI D3 state (which is what currently happens
  8048. * because of the runtime PM support).
  8049. *
  8050. * For more, read "Display Sequences for Package C8" on the hardware
  8051. * documentation.
  8052. */
  8053. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8054. {
  8055. struct drm_device *dev = &dev_priv->drm;
  8056. uint32_t val;
  8057. DRM_DEBUG_KMS("Enabling package C8+\n");
  8058. if (HAS_PCH_LPT_LP(dev)) {
  8059. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8060. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8061. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8062. }
  8063. lpt_disable_clkout_dp(dev);
  8064. hsw_disable_lcpll(dev_priv, true, true);
  8065. }
  8066. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8067. {
  8068. struct drm_device *dev = &dev_priv->drm;
  8069. uint32_t val;
  8070. DRM_DEBUG_KMS("Disabling package C8+\n");
  8071. hsw_restore_lcpll(dev_priv);
  8072. lpt_init_pch_refclk(dev);
  8073. if (HAS_PCH_LPT_LP(dev)) {
  8074. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8075. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8076. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8077. }
  8078. }
  8079. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8080. {
  8081. struct drm_device *dev = old_state->dev;
  8082. struct intel_atomic_state *old_intel_state =
  8083. to_intel_atomic_state(old_state);
  8084. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8085. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8086. }
  8087. /* compute the max rate for new configuration */
  8088. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8089. {
  8090. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8091. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8092. struct drm_crtc *crtc;
  8093. struct drm_crtc_state *cstate;
  8094. struct intel_crtc_state *crtc_state;
  8095. unsigned max_pixel_rate = 0, i;
  8096. enum pipe pipe;
  8097. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8098. sizeof(intel_state->min_pixclk));
  8099. for_each_crtc_in_state(state, crtc, cstate, i) {
  8100. int pixel_rate;
  8101. crtc_state = to_intel_crtc_state(cstate);
  8102. if (!crtc_state->base.enable) {
  8103. intel_state->min_pixclk[i] = 0;
  8104. continue;
  8105. }
  8106. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8107. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8108. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8109. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8110. intel_state->min_pixclk[i] = pixel_rate;
  8111. }
  8112. for_each_pipe(dev_priv, pipe)
  8113. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8114. return max_pixel_rate;
  8115. }
  8116. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8117. {
  8118. struct drm_i915_private *dev_priv = to_i915(dev);
  8119. uint32_t val, data;
  8120. int ret;
  8121. if (WARN((I915_READ(LCPLL_CTL) &
  8122. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8123. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8124. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8125. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8126. "trying to change cdclk frequency with cdclk not enabled\n"))
  8127. return;
  8128. mutex_lock(&dev_priv->rps.hw_lock);
  8129. ret = sandybridge_pcode_write(dev_priv,
  8130. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8131. mutex_unlock(&dev_priv->rps.hw_lock);
  8132. if (ret) {
  8133. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8134. return;
  8135. }
  8136. val = I915_READ(LCPLL_CTL);
  8137. val |= LCPLL_CD_SOURCE_FCLK;
  8138. I915_WRITE(LCPLL_CTL, val);
  8139. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8140. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8141. DRM_ERROR("Switching to FCLK failed\n");
  8142. val = I915_READ(LCPLL_CTL);
  8143. val &= ~LCPLL_CLK_FREQ_MASK;
  8144. switch (cdclk) {
  8145. case 450000:
  8146. val |= LCPLL_CLK_FREQ_450;
  8147. data = 0;
  8148. break;
  8149. case 540000:
  8150. val |= LCPLL_CLK_FREQ_54O_BDW;
  8151. data = 1;
  8152. break;
  8153. case 337500:
  8154. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8155. data = 2;
  8156. break;
  8157. case 675000:
  8158. val |= LCPLL_CLK_FREQ_675_BDW;
  8159. data = 3;
  8160. break;
  8161. default:
  8162. WARN(1, "invalid cdclk frequency\n");
  8163. return;
  8164. }
  8165. I915_WRITE(LCPLL_CTL, val);
  8166. val = I915_READ(LCPLL_CTL);
  8167. val &= ~LCPLL_CD_SOURCE_FCLK;
  8168. I915_WRITE(LCPLL_CTL, val);
  8169. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8170. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8171. DRM_ERROR("Switching back to LCPLL failed\n");
  8172. mutex_lock(&dev_priv->rps.hw_lock);
  8173. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8174. mutex_unlock(&dev_priv->rps.hw_lock);
  8175. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8176. intel_update_cdclk(dev);
  8177. WARN(cdclk != dev_priv->cdclk_freq,
  8178. "cdclk requested %d kHz but got %d kHz\n",
  8179. cdclk, dev_priv->cdclk_freq);
  8180. }
  8181. static int broadwell_calc_cdclk(int max_pixclk)
  8182. {
  8183. if (max_pixclk > 540000)
  8184. return 675000;
  8185. else if (max_pixclk > 450000)
  8186. return 540000;
  8187. else if (max_pixclk > 337500)
  8188. return 450000;
  8189. else
  8190. return 337500;
  8191. }
  8192. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8193. {
  8194. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8195. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8196. int max_pixclk = ilk_max_pixel_rate(state);
  8197. int cdclk;
  8198. /*
  8199. * FIXME should also account for plane ratio
  8200. * once 64bpp pixel formats are supported.
  8201. */
  8202. cdclk = broadwell_calc_cdclk(max_pixclk);
  8203. if (cdclk > dev_priv->max_cdclk_freq) {
  8204. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8205. cdclk, dev_priv->max_cdclk_freq);
  8206. return -EINVAL;
  8207. }
  8208. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8209. if (!intel_state->active_crtcs)
  8210. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8211. return 0;
  8212. }
  8213. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8214. {
  8215. struct drm_device *dev = old_state->dev;
  8216. struct intel_atomic_state *old_intel_state =
  8217. to_intel_atomic_state(old_state);
  8218. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8219. broadwell_set_cdclk(dev, req_cdclk);
  8220. }
  8221. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8222. {
  8223. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8224. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8225. const int max_pixclk = ilk_max_pixel_rate(state);
  8226. int vco = intel_state->cdclk_pll_vco;
  8227. int cdclk;
  8228. /*
  8229. * FIXME should also account for plane ratio
  8230. * once 64bpp pixel formats are supported.
  8231. */
  8232. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8233. /*
  8234. * FIXME move the cdclk caclulation to
  8235. * compute_config() so we can fail gracegully.
  8236. */
  8237. if (cdclk > dev_priv->max_cdclk_freq) {
  8238. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8239. cdclk, dev_priv->max_cdclk_freq);
  8240. cdclk = dev_priv->max_cdclk_freq;
  8241. }
  8242. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8243. if (!intel_state->active_crtcs)
  8244. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8245. return 0;
  8246. }
  8247. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8248. {
  8249. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8250. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8251. unsigned int req_cdclk = intel_state->dev_cdclk;
  8252. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8253. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8254. }
  8255. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8256. struct intel_crtc_state *crtc_state)
  8257. {
  8258. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  8259. if (!intel_ddi_pll_select(crtc, crtc_state))
  8260. return -EINVAL;
  8261. }
  8262. crtc->lowfreq_avail = false;
  8263. return 0;
  8264. }
  8265. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8266. enum port port,
  8267. struct intel_crtc_state *pipe_config)
  8268. {
  8269. enum intel_dpll_id id;
  8270. switch (port) {
  8271. case PORT_A:
  8272. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8273. id = DPLL_ID_SKL_DPLL0;
  8274. break;
  8275. case PORT_B:
  8276. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8277. id = DPLL_ID_SKL_DPLL1;
  8278. break;
  8279. case PORT_C:
  8280. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8281. id = DPLL_ID_SKL_DPLL2;
  8282. break;
  8283. default:
  8284. DRM_ERROR("Incorrect port type\n");
  8285. return;
  8286. }
  8287. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8288. }
  8289. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8290. enum port port,
  8291. struct intel_crtc_state *pipe_config)
  8292. {
  8293. enum intel_dpll_id id;
  8294. u32 temp;
  8295. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8296. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8297. switch (pipe_config->ddi_pll_sel) {
  8298. case SKL_DPLL0:
  8299. id = DPLL_ID_SKL_DPLL0;
  8300. break;
  8301. case SKL_DPLL1:
  8302. id = DPLL_ID_SKL_DPLL1;
  8303. break;
  8304. case SKL_DPLL2:
  8305. id = DPLL_ID_SKL_DPLL2;
  8306. break;
  8307. case SKL_DPLL3:
  8308. id = DPLL_ID_SKL_DPLL3;
  8309. break;
  8310. default:
  8311. MISSING_CASE(pipe_config->ddi_pll_sel);
  8312. return;
  8313. }
  8314. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8315. }
  8316. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8317. enum port port,
  8318. struct intel_crtc_state *pipe_config)
  8319. {
  8320. enum intel_dpll_id id;
  8321. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8322. switch (pipe_config->ddi_pll_sel) {
  8323. case PORT_CLK_SEL_WRPLL1:
  8324. id = DPLL_ID_WRPLL1;
  8325. break;
  8326. case PORT_CLK_SEL_WRPLL2:
  8327. id = DPLL_ID_WRPLL2;
  8328. break;
  8329. case PORT_CLK_SEL_SPLL:
  8330. id = DPLL_ID_SPLL;
  8331. break;
  8332. case PORT_CLK_SEL_LCPLL_810:
  8333. id = DPLL_ID_LCPLL_810;
  8334. break;
  8335. case PORT_CLK_SEL_LCPLL_1350:
  8336. id = DPLL_ID_LCPLL_1350;
  8337. break;
  8338. case PORT_CLK_SEL_LCPLL_2700:
  8339. id = DPLL_ID_LCPLL_2700;
  8340. break;
  8341. default:
  8342. MISSING_CASE(pipe_config->ddi_pll_sel);
  8343. /* fall through */
  8344. case PORT_CLK_SEL_NONE:
  8345. return;
  8346. }
  8347. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8348. }
  8349. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8350. struct intel_crtc_state *pipe_config,
  8351. unsigned long *power_domain_mask)
  8352. {
  8353. struct drm_device *dev = crtc->base.dev;
  8354. struct drm_i915_private *dev_priv = to_i915(dev);
  8355. enum intel_display_power_domain power_domain;
  8356. u32 tmp;
  8357. /*
  8358. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8359. * transcoder handled below.
  8360. */
  8361. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8362. /*
  8363. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8364. * consistency and less surprising code; it's in always on power).
  8365. */
  8366. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8367. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8368. enum pipe trans_edp_pipe;
  8369. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8370. default:
  8371. WARN(1, "unknown pipe linked to edp transcoder\n");
  8372. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8373. case TRANS_DDI_EDP_INPUT_A_ON:
  8374. trans_edp_pipe = PIPE_A;
  8375. break;
  8376. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8377. trans_edp_pipe = PIPE_B;
  8378. break;
  8379. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8380. trans_edp_pipe = PIPE_C;
  8381. break;
  8382. }
  8383. if (trans_edp_pipe == crtc->pipe)
  8384. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8385. }
  8386. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8387. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8388. return false;
  8389. *power_domain_mask |= BIT(power_domain);
  8390. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8391. return tmp & PIPECONF_ENABLE;
  8392. }
  8393. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8394. struct intel_crtc_state *pipe_config,
  8395. unsigned long *power_domain_mask)
  8396. {
  8397. struct drm_device *dev = crtc->base.dev;
  8398. struct drm_i915_private *dev_priv = to_i915(dev);
  8399. enum intel_display_power_domain power_domain;
  8400. enum port port;
  8401. enum transcoder cpu_transcoder;
  8402. u32 tmp;
  8403. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8404. if (port == PORT_A)
  8405. cpu_transcoder = TRANSCODER_DSI_A;
  8406. else
  8407. cpu_transcoder = TRANSCODER_DSI_C;
  8408. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8409. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8410. continue;
  8411. *power_domain_mask |= BIT(power_domain);
  8412. /*
  8413. * The PLL needs to be enabled with a valid divider
  8414. * configuration, otherwise accessing DSI registers will hang
  8415. * the machine. See BSpec North Display Engine
  8416. * registers/MIPI[BXT]. We can break out here early, since we
  8417. * need the same DSI PLL to be enabled for both DSI ports.
  8418. */
  8419. if (!intel_dsi_pll_is_enabled(dev_priv))
  8420. break;
  8421. /* XXX: this works for video mode only */
  8422. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8423. if (!(tmp & DPI_ENABLE))
  8424. continue;
  8425. tmp = I915_READ(MIPI_CTRL(port));
  8426. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8427. continue;
  8428. pipe_config->cpu_transcoder = cpu_transcoder;
  8429. break;
  8430. }
  8431. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  8432. }
  8433. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8434. struct intel_crtc_state *pipe_config)
  8435. {
  8436. struct drm_device *dev = crtc->base.dev;
  8437. struct drm_i915_private *dev_priv = to_i915(dev);
  8438. struct intel_shared_dpll *pll;
  8439. enum port port;
  8440. uint32_t tmp;
  8441. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8442. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8443. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8444. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8445. else if (IS_BROXTON(dev))
  8446. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8447. else
  8448. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8449. pll = pipe_config->shared_dpll;
  8450. if (pll) {
  8451. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8452. &pipe_config->dpll_hw_state));
  8453. }
  8454. /*
  8455. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8456. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8457. * the PCH transcoder is on.
  8458. */
  8459. if (INTEL_INFO(dev)->gen < 9 &&
  8460. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8461. pipe_config->has_pch_encoder = true;
  8462. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8463. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8464. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8465. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8466. }
  8467. }
  8468. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8469. struct intel_crtc_state *pipe_config)
  8470. {
  8471. struct drm_device *dev = crtc->base.dev;
  8472. struct drm_i915_private *dev_priv = to_i915(dev);
  8473. enum intel_display_power_domain power_domain;
  8474. unsigned long power_domain_mask;
  8475. bool active;
  8476. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8477. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8478. return false;
  8479. power_domain_mask = BIT(power_domain);
  8480. pipe_config->shared_dpll = NULL;
  8481. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8482. if (IS_BROXTON(dev_priv) &&
  8483. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  8484. WARN_ON(active);
  8485. active = true;
  8486. }
  8487. if (!active)
  8488. goto out;
  8489. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8490. haswell_get_ddi_port_state(crtc, pipe_config);
  8491. intel_get_pipe_timings(crtc, pipe_config);
  8492. }
  8493. intel_get_pipe_src_size(crtc, pipe_config);
  8494. pipe_config->gamma_mode =
  8495. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8496. if (INTEL_INFO(dev)->gen >= 9) {
  8497. skl_init_scalers(dev, crtc, pipe_config);
  8498. }
  8499. if (INTEL_INFO(dev)->gen >= 9) {
  8500. pipe_config->scaler_state.scaler_id = -1;
  8501. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8502. }
  8503. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8504. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8505. power_domain_mask |= BIT(power_domain);
  8506. if (INTEL_INFO(dev)->gen >= 9)
  8507. skylake_get_pfit_config(crtc, pipe_config);
  8508. else
  8509. ironlake_get_pfit_config(crtc, pipe_config);
  8510. }
  8511. if (IS_HASWELL(dev))
  8512. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8513. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8514. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8515. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8516. pipe_config->pixel_multiplier =
  8517. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8518. } else {
  8519. pipe_config->pixel_multiplier = 1;
  8520. }
  8521. out:
  8522. for_each_power_domain(power_domain, power_domain_mask)
  8523. intel_display_power_put(dev_priv, power_domain);
  8524. return active;
  8525. }
  8526. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  8527. const struct intel_plane_state *plane_state)
  8528. {
  8529. struct drm_device *dev = crtc->dev;
  8530. struct drm_i915_private *dev_priv = to_i915(dev);
  8531. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8532. uint32_t cntl = 0, size = 0;
  8533. if (plane_state && plane_state->visible) {
  8534. unsigned int width = plane_state->base.crtc_w;
  8535. unsigned int height = plane_state->base.crtc_h;
  8536. unsigned int stride = roundup_pow_of_two(width) * 4;
  8537. switch (stride) {
  8538. default:
  8539. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8540. width, stride);
  8541. stride = 256;
  8542. /* fallthrough */
  8543. case 256:
  8544. case 512:
  8545. case 1024:
  8546. case 2048:
  8547. break;
  8548. }
  8549. cntl |= CURSOR_ENABLE |
  8550. CURSOR_GAMMA_ENABLE |
  8551. CURSOR_FORMAT_ARGB |
  8552. CURSOR_STRIDE(stride);
  8553. size = (height << 12) | width;
  8554. }
  8555. if (intel_crtc->cursor_cntl != 0 &&
  8556. (intel_crtc->cursor_base != base ||
  8557. intel_crtc->cursor_size != size ||
  8558. intel_crtc->cursor_cntl != cntl)) {
  8559. /* On these chipsets we can only modify the base/size/stride
  8560. * whilst the cursor is disabled.
  8561. */
  8562. I915_WRITE(CURCNTR(PIPE_A), 0);
  8563. POSTING_READ(CURCNTR(PIPE_A));
  8564. intel_crtc->cursor_cntl = 0;
  8565. }
  8566. if (intel_crtc->cursor_base != base) {
  8567. I915_WRITE(CURBASE(PIPE_A), base);
  8568. intel_crtc->cursor_base = base;
  8569. }
  8570. if (intel_crtc->cursor_size != size) {
  8571. I915_WRITE(CURSIZE, size);
  8572. intel_crtc->cursor_size = size;
  8573. }
  8574. if (intel_crtc->cursor_cntl != cntl) {
  8575. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8576. POSTING_READ(CURCNTR(PIPE_A));
  8577. intel_crtc->cursor_cntl = cntl;
  8578. }
  8579. }
  8580. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  8581. const struct intel_plane_state *plane_state)
  8582. {
  8583. struct drm_device *dev = crtc->dev;
  8584. struct drm_i915_private *dev_priv = to_i915(dev);
  8585. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8586. int pipe = intel_crtc->pipe;
  8587. uint32_t cntl = 0;
  8588. if (plane_state && plane_state->visible) {
  8589. cntl = MCURSOR_GAMMA_ENABLE;
  8590. switch (plane_state->base.crtc_w) {
  8591. case 64:
  8592. cntl |= CURSOR_MODE_64_ARGB_AX;
  8593. break;
  8594. case 128:
  8595. cntl |= CURSOR_MODE_128_ARGB_AX;
  8596. break;
  8597. case 256:
  8598. cntl |= CURSOR_MODE_256_ARGB_AX;
  8599. break;
  8600. default:
  8601. MISSING_CASE(plane_state->base.crtc_w);
  8602. return;
  8603. }
  8604. cntl |= pipe << 28; /* Connect to correct pipe */
  8605. if (HAS_DDI(dev))
  8606. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8607. if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
  8608. cntl |= CURSOR_ROTATE_180;
  8609. }
  8610. if (intel_crtc->cursor_cntl != cntl) {
  8611. I915_WRITE(CURCNTR(pipe), cntl);
  8612. POSTING_READ(CURCNTR(pipe));
  8613. intel_crtc->cursor_cntl = cntl;
  8614. }
  8615. /* and commit changes on next vblank */
  8616. I915_WRITE(CURBASE(pipe), base);
  8617. POSTING_READ(CURBASE(pipe));
  8618. intel_crtc->cursor_base = base;
  8619. }
  8620. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8621. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8622. const struct intel_plane_state *plane_state)
  8623. {
  8624. struct drm_device *dev = crtc->dev;
  8625. struct drm_i915_private *dev_priv = to_i915(dev);
  8626. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8627. int pipe = intel_crtc->pipe;
  8628. u32 base = intel_crtc->cursor_addr;
  8629. u32 pos = 0;
  8630. if (plane_state) {
  8631. int x = plane_state->base.crtc_x;
  8632. int y = plane_state->base.crtc_y;
  8633. if (x < 0) {
  8634. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8635. x = -x;
  8636. }
  8637. pos |= x << CURSOR_X_SHIFT;
  8638. if (y < 0) {
  8639. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8640. y = -y;
  8641. }
  8642. pos |= y << CURSOR_Y_SHIFT;
  8643. /* ILK+ do this automagically */
  8644. if (HAS_GMCH_DISPLAY(dev) &&
  8645. plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  8646. base += (plane_state->base.crtc_h *
  8647. plane_state->base.crtc_w - 1) * 4;
  8648. }
  8649. }
  8650. I915_WRITE(CURPOS(pipe), pos);
  8651. if (IS_845G(dev) || IS_I865G(dev))
  8652. i845_update_cursor(crtc, base, plane_state);
  8653. else
  8654. i9xx_update_cursor(crtc, base, plane_state);
  8655. }
  8656. static bool cursor_size_ok(struct drm_device *dev,
  8657. uint32_t width, uint32_t height)
  8658. {
  8659. if (width == 0 || height == 0)
  8660. return false;
  8661. /*
  8662. * 845g/865g are special in that they are only limited by
  8663. * the width of their cursors, the height is arbitrary up to
  8664. * the precision of the register. Everything else requires
  8665. * square cursors, limited to a few power-of-two sizes.
  8666. */
  8667. if (IS_845G(dev) || IS_I865G(dev)) {
  8668. if ((width & 63) != 0)
  8669. return false;
  8670. if (width > (IS_845G(dev) ? 64 : 512))
  8671. return false;
  8672. if (height > 1023)
  8673. return false;
  8674. } else {
  8675. switch (width | height) {
  8676. case 256:
  8677. case 128:
  8678. if (IS_GEN2(dev))
  8679. return false;
  8680. case 64:
  8681. break;
  8682. default:
  8683. return false;
  8684. }
  8685. }
  8686. return true;
  8687. }
  8688. /* VESA 640x480x72Hz mode to set on the pipe */
  8689. static struct drm_display_mode load_detect_mode = {
  8690. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8691. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8692. };
  8693. struct drm_framebuffer *
  8694. __intel_framebuffer_create(struct drm_device *dev,
  8695. struct drm_mode_fb_cmd2 *mode_cmd,
  8696. struct drm_i915_gem_object *obj)
  8697. {
  8698. struct intel_framebuffer *intel_fb;
  8699. int ret;
  8700. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8701. if (!intel_fb)
  8702. return ERR_PTR(-ENOMEM);
  8703. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8704. if (ret)
  8705. goto err;
  8706. return &intel_fb->base;
  8707. err:
  8708. kfree(intel_fb);
  8709. return ERR_PTR(ret);
  8710. }
  8711. static struct drm_framebuffer *
  8712. intel_framebuffer_create(struct drm_device *dev,
  8713. struct drm_mode_fb_cmd2 *mode_cmd,
  8714. struct drm_i915_gem_object *obj)
  8715. {
  8716. struct drm_framebuffer *fb;
  8717. int ret;
  8718. ret = i915_mutex_lock_interruptible(dev);
  8719. if (ret)
  8720. return ERR_PTR(ret);
  8721. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8722. mutex_unlock(&dev->struct_mutex);
  8723. return fb;
  8724. }
  8725. static u32
  8726. intel_framebuffer_pitch_for_width(int width, int bpp)
  8727. {
  8728. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8729. return ALIGN(pitch, 64);
  8730. }
  8731. static u32
  8732. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8733. {
  8734. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8735. return PAGE_ALIGN(pitch * mode->vdisplay);
  8736. }
  8737. static struct drm_framebuffer *
  8738. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8739. struct drm_display_mode *mode,
  8740. int depth, int bpp)
  8741. {
  8742. struct drm_framebuffer *fb;
  8743. struct drm_i915_gem_object *obj;
  8744. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8745. obj = i915_gem_object_create(dev,
  8746. intel_framebuffer_size_for_mode(mode, bpp));
  8747. if (IS_ERR(obj))
  8748. return ERR_CAST(obj);
  8749. mode_cmd.width = mode->hdisplay;
  8750. mode_cmd.height = mode->vdisplay;
  8751. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8752. bpp);
  8753. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8754. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8755. if (IS_ERR(fb))
  8756. drm_gem_object_unreference_unlocked(&obj->base);
  8757. return fb;
  8758. }
  8759. static struct drm_framebuffer *
  8760. mode_fits_in_fbdev(struct drm_device *dev,
  8761. struct drm_display_mode *mode)
  8762. {
  8763. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8764. struct drm_i915_private *dev_priv = to_i915(dev);
  8765. struct drm_i915_gem_object *obj;
  8766. struct drm_framebuffer *fb;
  8767. if (!dev_priv->fbdev)
  8768. return NULL;
  8769. if (!dev_priv->fbdev->fb)
  8770. return NULL;
  8771. obj = dev_priv->fbdev->fb->obj;
  8772. BUG_ON(!obj);
  8773. fb = &dev_priv->fbdev->fb->base;
  8774. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8775. fb->bits_per_pixel))
  8776. return NULL;
  8777. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8778. return NULL;
  8779. drm_framebuffer_reference(fb);
  8780. return fb;
  8781. #else
  8782. return NULL;
  8783. #endif
  8784. }
  8785. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8786. struct drm_crtc *crtc,
  8787. struct drm_display_mode *mode,
  8788. struct drm_framebuffer *fb,
  8789. int x, int y)
  8790. {
  8791. struct drm_plane_state *plane_state;
  8792. int hdisplay, vdisplay;
  8793. int ret;
  8794. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8795. if (IS_ERR(plane_state))
  8796. return PTR_ERR(plane_state);
  8797. if (mode)
  8798. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8799. else
  8800. hdisplay = vdisplay = 0;
  8801. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8802. if (ret)
  8803. return ret;
  8804. drm_atomic_set_fb_for_plane(plane_state, fb);
  8805. plane_state->crtc_x = 0;
  8806. plane_state->crtc_y = 0;
  8807. plane_state->crtc_w = hdisplay;
  8808. plane_state->crtc_h = vdisplay;
  8809. plane_state->src_x = x << 16;
  8810. plane_state->src_y = y << 16;
  8811. plane_state->src_w = hdisplay << 16;
  8812. plane_state->src_h = vdisplay << 16;
  8813. return 0;
  8814. }
  8815. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8816. struct drm_display_mode *mode,
  8817. struct intel_load_detect_pipe *old,
  8818. struct drm_modeset_acquire_ctx *ctx)
  8819. {
  8820. struct intel_crtc *intel_crtc;
  8821. struct intel_encoder *intel_encoder =
  8822. intel_attached_encoder(connector);
  8823. struct drm_crtc *possible_crtc;
  8824. struct drm_encoder *encoder = &intel_encoder->base;
  8825. struct drm_crtc *crtc = NULL;
  8826. struct drm_device *dev = encoder->dev;
  8827. struct drm_framebuffer *fb;
  8828. struct drm_mode_config *config = &dev->mode_config;
  8829. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8830. struct drm_connector_state *connector_state;
  8831. struct intel_crtc_state *crtc_state;
  8832. int ret, i = -1;
  8833. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8834. connector->base.id, connector->name,
  8835. encoder->base.id, encoder->name);
  8836. old->restore_state = NULL;
  8837. retry:
  8838. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8839. if (ret)
  8840. goto fail;
  8841. /*
  8842. * Algorithm gets a little messy:
  8843. *
  8844. * - if the connector already has an assigned crtc, use it (but make
  8845. * sure it's on first)
  8846. *
  8847. * - try to find the first unused crtc that can drive this connector,
  8848. * and use that if we find one
  8849. */
  8850. /* See if we already have a CRTC for this connector */
  8851. if (connector->state->crtc) {
  8852. crtc = connector->state->crtc;
  8853. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8854. if (ret)
  8855. goto fail;
  8856. /* Make sure the crtc and connector are running */
  8857. goto found;
  8858. }
  8859. /* Find an unused one (if possible) */
  8860. for_each_crtc(dev, possible_crtc) {
  8861. i++;
  8862. if (!(encoder->possible_crtcs & (1 << i)))
  8863. continue;
  8864. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8865. if (ret)
  8866. goto fail;
  8867. if (possible_crtc->state->enable) {
  8868. drm_modeset_unlock(&possible_crtc->mutex);
  8869. continue;
  8870. }
  8871. crtc = possible_crtc;
  8872. break;
  8873. }
  8874. /*
  8875. * If we didn't find an unused CRTC, don't use any.
  8876. */
  8877. if (!crtc) {
  8878. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8879. goto fail;
  8880. }
  8881. found:
  8882. intel_crtc = to_intel_crtc(crtc);
  8883. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8884. if (ret)
  8885. goto fail;
  8886. state = drm_atomic_state_alloc(dev);
  8887. restore_state = drm_atomic_state_alloc(dev);
  8888. if (!state || !restore_state) {
  8889. ret = -ENOMEM;
  8890. goto fail;
  8891. }
  8892. state->acquire_ctx = ctx;
  8893. restore_state->acquire_ctx = ctx;
  8894. connector_state = drm_atomic_get_connector_state(state, connector);
  8895. if (IS_ERR(connector_state)) {
  8896. ret = PTR_ERR(connector_state);
  8897. goto fail;
  8898. }
  8899. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8900. if (ret)
  8901. goto fail;
  8902. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8903. if (IS_ERR(crtc_state)) {
  8904. ret = PTR_ERR(crtc_state);
  8905. goto fail;
  8906. }
  8907. crtc_state->base.active = crtc_state->base.enable = true;
  8908. if (!mode)
  8909. mode = &load_detect_mode;
  8910. /* We need a framebuffer large enough to accommodate all accesses
  8911. * that the plane may generate whilst we perform load detection.
  8912. * We can not rely on the fbcon either being present (we get called
  8913. * during its initialisation to detect all boot displays, or it may
  8914. * not even exist) or that it is large enough to satisfy the
  8915. * requested mode.
  8916. */
  8917. fb = mode_fits_in_fbdev(dev, mode);
  8918. if (fb == NULL) {
  8919. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8920. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8921. } else
  8922. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8923. if (IS_ERR(fb)) {
  8924. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8925. goto fail;
  8926. }
  8927. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8928. if (ret)
  8929. goto fail;
  8930. drm_framebuffer_unreference(fb);
  8931. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8932. if (ret)
  8933. goto fail;
  8934. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8935. if (!ret)
  8936. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8937. if (!ret)
  8938. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8939. if (ret) {
  8940. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8941. goto fail;
  8942. }
  8943. ret = drm_atomic_commit(state);
  8944. if (ret) {
  8945. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8946. goto fail;
  8947. }
  8948. old->restore_state = restore_state;
  8949. /* let the connector get through one full cycle before testing */
  8950. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8951. return true;
  8952. fail:
  8953. drm_atomic_state_free(state);
  8954. drm_atomic_state_free(restore_state);
  8955. restore_state = state = NULL;
  8956. if (ret == -EDEADLK) {
  8957. drm_modeset_backoff(ctx);
  8958. goto retry;
  8959. }
  8960. return false;
  8961. }
  8962. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8963. struct intel_load_detect_pipe *old,
  8964. struct drm_modeset_acquire_ctx *ctx)
  8965. {
  8966. struct intel_encoder *intel_encoder =
  8967. intel_attached_encoder(connector);
  8968. struct drm_encoder *encoder = &intel_encoder->base;
  8969. struct drm_atomic_state *state = old->restore_state;
  8970. int ret;
  8971. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8972. connector->base.id, connector->name,
  8973. encoder->base.id, encoder->name);
  8974. if (!state)
  8975. return;
  8976. ret = drm_atomic_commit(state);
  8977. if (ret) {
  8978. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8979. drm_atomic_state_free(state);
  8980. }
  8981. }
  8982. static int i9xx_pll_refclk(struct drm_device *dev,
  8983. const struct intel_crtc_state *pipe_config)
  8984. {
  8985. struct drm_i915_private *dev_priv = to_i915(dev);
  8986. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8987. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8988. return dev_priv->vbt.lvds_ssc_freq;
  8989. else if (HAS_PCH_SPLIT(dev))
  8990. return 120000;
  8991. else if (!IS_GEN2(dev))
  8992. return 96000;
  8993. else
  8994. return 48000;
  8995. }
  8996. /* Returns the clock of the currently programmed mode of the given pipe. */
  8997. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8998. struct intel_crtc_state *pipe_config)
  8999. {
  9000. struct drm_device *dev = crtc->base.dev;
  9001. struct drm_i915_private *dev_priv = to_i915(dev);
  9002. int pipe = pipe_config->cpu_transcoder;
  9003. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9004. u32 fp;
  9005. struct dpll clock;
  9006. int port_clock;
  9007. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9008. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9009. fp = pipe_config->dpll_hw_state.fp0;
  9010. else
  9011. fp = pipe_config->dpll_hw_state.fp1;
  9012. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9013. if (IS_PINEVIEW(dev)) {
  9014. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9015. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9016. } else {
  9017. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9018. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9019. }
  9020. if (!IS_GEN2(dev)) {
  9021. if (IS_PINEVIEW(dev))
  9022. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9023. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9024. else
  9025. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9026. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9027. switch (dpll & DPLL_MODE_MASK) {
  9028. case DPLLB_MODE_DAC_SERIAL:
  9029. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9030. 5 : 10;
  9031. break;
  9032. case DPLLB_MODE_LVDS:
  9033. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9034. 7 : 14;
  9035. break;
  9036. default:
  9037. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9038. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9039. return;
  9040. }
  9041. if (IS_PINEVIEW(dev))
  9042. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9043. else
  9044. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9045. } else {
  9046. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  9047. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9048. if (is_lvds) {
  9049. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9050. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9051. if (lvds & LVDS_CLKB_POWER_UP)
  9052. clock.p2 = 7;
  9053. else
  9054. clock.p2 = 14;
  9055. } else {
  9056. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9057. clock.p1 = 2;
  9058. else {
  9059. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9060. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9061. }
  9062. if (dpll & PLL_P2_DIVIDE_BY_4)
  9063. clock.p2 = 4;
  9064. else
  9065. clock.p2 = 2;
  9066. }
  9067. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9068. }
  9069. /*
  9070. * This value includes pixel_multiplier. We will use
  9071. * port_clock to compute adjusted_mode.crtc_clock in the
  9072. * encoder's get_config() function.
  9073. */
  9074. pipe_config->port_clock = port_clock;
  9075. }
  9076. int intel_dotclock_calculate(int link_freq,
  9077. const struct intel_link_m_n *m_n)
  9078. {
  9079. /*
  9080. * The calculation for the data clock is:
  9081. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9082. * But we want to avoid losing precison if possible, so:
  9083. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9084. *
  9085. * and the link clock is simpler:
  9086. * link_clock = (m * link_clock) / n
  9087. */
  9088. if (!m_n->link_n)
  9089. return 0;
  9090. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9091. }
  9092. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9093. struct intel_crtc_state *pipe_config)
  9094. {
  9095. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9096. /* read out port_clock from the DPLL */
  9097. i9xx_crtc_clock_get(crtc, pipe_config);
  9098. /*
  9099. * In case there is an active pipe without active ports,
  9100. * we may need some idea for the dotclock anyway.
  9101. * Calculate one based on the FDI configuration.
  9102. */
  9103. pipe_config->base.adjusted_mode.crtc_clock =
  9104. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9105. &pipe_config->fdi_m_n);
  9106. }
  9107. /** Returns the currently programmed mode of the given pipe. */
  9108. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9109. struct drm_crtc *crtc)
  9110. {
  9111. struct drm_i915_private *dev_priv = to_i915(dev);
  9112. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9113. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9114. struct drm_display_mode *mode;
  9115. struct intel_crtc_state *pipe_config;
  9116. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9117. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9118. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9119. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9120. enum pipe pipe = intel_crtc->pipe;
  9121. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9122. if (!mode)
  9123. return NULL;
  9124. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9125. if (!pipe_config) {
  9126. kfree(mode);
  9127. return NULL;
  9128. }
  9129. /*
  9130. * Construct a pipe_config sufficient for getting the clock info
  9131. * back out of crtc_clock_get.
  9132. *
  9133. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9134. * to use a real value here instead.
  9135. */
  9136. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9137. pipe_config->pixel_multiplier = 1;
  9138. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9139. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9140. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9141. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9142. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9143. mode->hdisplay = (htot & 0xffff) + 1;
  9144. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9145. mode->hsync_start = (hsync & 0xffff) + 1;
  9146. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9147. mode->vdisplay = (vtot & 0xffff) + 1;
  9148. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9149. mode->vsync_start = (vsync & 0xffff) + 1;
  9150. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9151. drm_mode_set_name(mode);
  9152. kfree(pipe_config);
  9153. return mode;
  9154. }
  9155. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9156. {
  9157. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9158. struct drm_device *dev = crtc->dev;
  9159. struct intel_flip_work *work;
  9160. spin_lock_irq(&dev->event_lock);
  9161. work = intel_crtc->flip_work;
  9162. intel_crtc->flip_work = NULL;
  9163. spin_unlock_irq(&dev->event_lock);
  9164. if (work) {
  9165. cancel_work_sync(&work->mmio_work);
  9166. cancel_work_sync(&work->unpin_work);
  9167. kfree(work);
  9168. }
  9169. drm_crtc_cleanup(crtc);
  9170. kfree(intel_crtc);
  9171. }
  9172. static void intel_unpin_work_fn(struct work_struct *__work)
  9173. {
  9174. struct intel_flip_work *work =
  9175. container_of(__work, struct intel_flip_work, unpin_work);
  9176. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9177. struct drm_device *dev = crtc->base.dev;
  9178. struct drm_plane *primary = crtc->base.primary;
  9179. if (is_mmio_work(work))
  9180. flush_work(&work->mmio_work);
  9181. mutex_lock(&dev->struct_mutex);
  9182. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9183. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9184. mutex_unlock(&dev->struct_mutex);
  9185. i915_gem_request_put(work->flip_queued_req);
  9186. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  9187. intel_fbc_post_update(crtc);
  9188. drm_framebuffer_unreference(work->old_fb);
  9189. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9190. atomic_dec(&crtc->unpin_work_count);
  9191. kfree(work);
  9192. }
  9193. /* Is 'a' after or equal to 'b'? */
  9194. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9195. {
  9196. return !((a - b) & 0x80000000);
  9197. }
  9198. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9199. struct intel_flip_work *work)
  9200. {
  9201. struct drm_device *dev = crtc->base.dev;
  9202. struct drm_i915_private *dev_priv = to_i915(dev);
  9203. unsigned reset_counter;
  9204. reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9205. if (crtc->reset_counter != reset_counter)
  9206. return true;
  9207. /*
  9208. * The relevant registers doen't exist on pre-ctg.
  9209. * As the flip done interrupt doesn't trigger for mmio
  9210. * flips on gmch platforms, a flip count check isn't
  9211. * really needed there. But since ctg has the registers,
  9212. * include it in the check anyway.
  9213. */
  9214. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9215. return true;
  9216. /*
  9217. * BDW signals flip done immediately if the plane
  9218. * is disabled, even if the plane enable is already
  9219. * armed to occur at the next vblank :(
  9220. */
  9221. /*
  9222. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9223. * used the same base address. In that case the mmio flip might
  9224. * have completed, but the CS hasn't even executed the flip yet.
  9225. *
  9226. * A flip count check isn't enough as the CS might have updated
  9227. * the base address just after start of vblank, but before we
  9228. * managed to process the interrupt. This means we'd complete the
  9229. * CS flip too soon.
  9230. *
  9231. * Combining both checks should get us a good enough result. It may
  9232. * still happen that the CS flip has been executed, but has not
  9233. * yet actually completed. But in case the base address is the same
  9234. * anyway, we don't really care.
  9235. */
  9236. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9237. crtc->flip_work->gtt_offset &&
  9238. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9239. crtc->flip_work->flip_count);
  9240. }
  9241. static bool
  9242. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9243. struct intel_flip_work *work)
  9244. {
  9245. /*
  9246. * MMIO work completes when vblank is different from
  9247. * flip_queued_vblank.
  9248. *
  9249. * Reset counter value doesn't matter, this is handled by
  9250. * i915_wait_request finishing early, so no need to handle
  9251. * reset here.
  9252. */
  9253. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9254. }
  9255. static bool pageflip_finished(struct intel_crtc *crtc,
  9256. struct intel_flip_work *work)
  9257. {
  9258. if (!atomic_read(&work->pending))
  9259. return false;
  9260. smp_rmb();
  9261. if (is_mmio_work(work))
  9262. return __pageflip_finished_mmio(crtc, work);
  9263. else
  9264. return __pageflip_finished_cs(crtc, work);
  9265. }
  9266. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9267. {
  9268. struct drm_device *dev = &dev_priv->drm;
  9269. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9270. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9271. struct intel_flip_work *work;
  9272. unsigned long flags;
  9273. /* Ignore early vblank irqs */
  9274. if (!crtc)
  9275. return;
  9276. /*
  9277. * This is called both by irq handlers and the reset code (to complete
  9278. * lost pageflips) so needs the full irqsave spinlocks.
  9279. */
  9280. spin_lock_irqsave(&dev->event_lock, flags);
  9281. work = intel_crtc->flip_work;
  9282. if (work != NULL &&
  9283. !is_mmio_work(work) &&
  9284. pageflip_finished(intel_crtc, work))
  9285. page_flip_completed(intel_crtc);
  9286. spin_unlock_irqrestore(&dev->event_lock, flags);
  9287. }
  9288. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9289. {
  9290. struct drm_device *dev = &dev_priv->drm;
  9291. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9292. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9293. struct intel_flip_work *work;
  9294. unsigned long flags;
  9295. /* Ignore early vblank irqs */
  9296. if (!crtc)
  9297. return;
  9298. /*
  9299. * This is called both by irq handlers and the reset code (to complete
  9300. * lost pageflips) so needs the full irqsave spinlocks.
  9301. */
  9302. spin_lock_irqsave(&dev->event_lock, flags);
  9303. work = intel_crtc->flip_work;
  9304. if (work != NULL &&
  9305. is_mmio_work(work) &&
  9306. pageflip_finished(intel_crtc, work))
  9307. page_flip_completed(intel_crtc);
  9308. spin_unlock_irqrestore(&dev->event_lock, flags);
  9309. }
  9310. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9311. struct intel_flip_work *work)
  9312. {
  9313. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9314. /* Ensure that the work item is consistent when activating it ... */
  9315. smp_mb__before_atomic();
  9316. atomic_set(&work->pending, 1);
  9317. }
  9318. static int intel_gen2_queue_flip(struct drm_device *dev,
  9319. struct drm_crtc *crtc,
  9320. struct drm_framebuffer *fb,
  9321. struct drm_i915_gem_object *obj,
  9322. struct drm_i915_gem_request *req,
  9323. uint32_t flags)
  9324. {
  9325. struct intel_engine_cs *engine = req->engine;
  9326. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9327. u32 flip_mask;
  9328. int ret;
  9329. ret = intel_ring_begin(req, 6);
  9330. if (ret)
  9331. return ret;
  9332. /* Can't queue multiple flips, so wait for the previous
  9333. * one to finish before executing the next.
  9334. */
  9335. if (intel_crtc->plane)
  9336. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9337. else
  9338. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9339. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9340. intel_ring_emit(engine, MI_NOOP);
  9341. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9342. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9343. intel_ring_emit(engine, fb->pitches[0]);
  9344. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9345. intel_ring_emit(engine, 0); /* aux display base address, unused */
  9346. return 0;
  9347. }
  9348. static int intel_gen3_queue_flip(struct drm_device *dev,
  9349. struct drm_crtc *crtc,
  9350. struct drm_framebuffer *fb,
  9351. struct drm_i915_gem_object *obj,
  9352. struct drm_i915_gem_request *req,
  9353. uint32_t flags)
  9354. {
  9355. struct intel_engine_cs *engine = req->engine;
  9356. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9357. u32 flip_mask;
  9358. int ret;
  9359. ret = intel_ring_begin(req, 6);
  9360. if (ret)
  9361. return ret;
  9362. if (intel_crtc->plane)
  9363. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9364. else
  9365. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9366. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9367. intel_ring_emit(engine, MI_NOOP);
  9368. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
  9369. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9370. intel_ring_emit(engine, fb->pitches[0]);
  9371. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9372. intel_ring_emit(engine, MI_NOOP);
  9373. return 0;
  9374. }
  9375. static int intel_gen4_queue_flip(struct drm_device *dev,
  9376. struct drm_crtc *crtc,
  9377. struct drm_framebuffer *fb,
  9378. struct drm_i915_gem_object *obj,
  9379. struct drm_i915_gem_request *req,
  9380. uint32_t flags)
  9381. {
  9382. struct intel_engine_cs *engine = req->engine;
  9383. struct drm_i915_private *dev_priv = to_i915(dev);
  9384. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9385. uint32_t pf, pipesrc;
  9386. int ret;
  9387. ret = intel_ring_begin(req, 4);
  9388. if (ret)
  9389. return ret;
  9390. /* i965+ uses the linear or tiled offsets from the
  9391. * Display Registers (which do not change across a page-flip)
  9392. * so we need only reprogram the base address.
  9393. */
  9394. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9395. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9396. intel_ring_emit(engine, fb->pitches[0]);
  9397. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
  9398. obj->tiling_mode);
  9399. /* XXX Enabling the panel-fitter across page-flip is so far
  9400. * untested on non-native modes, so ignore it for now.
  9401. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9402. */
  9403. pf = 0;
  9404. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9405. intel_ring_emit(engine, pf | pipesrc);
  9406. return 0;
  9407. }
  9408. static int intel_gen6_queue_flip(struct drm_device *dev,
  9409. struct drm_crtc *crtc,
  9410. struct drm_framebuffer *fb,
  9411. struct drm_i915_gem_object *obj,
  9412. struct drm_i915_gem_request *req,
  9413. uint32_t flags)
  9414. {
  9415. struct intel_engine_cs *engine = req->engine;
  9416. struct drm_i915_private *dev_priv = to_i915(dev);
  9417. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9418. uint32_t pf, pipesrc;
  9419. int ret;
  9420. ret = intel_ring_begin(req, 4);
  9421. if (ret)
  9422. return ret;
  9423. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9424. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9425. intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
  9426. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9427. /* Contrary to the suggestions in the documentation,
  9428. * "Enable Panel Fitter" does not seem to be required when page
  9429. * flipping with a non-native mode, and worse causes a normal
  9430. * modeset to fail.
  9431. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9432. */
  9433. pf = 0;
  9434. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9435. intel_ring_emit(engine, pf | pipesrc);
  9436. return 0;
  9437. }
  9438. static int intel_gen7_queue_flip(struct drm_device *dev,
  9439. struct drm_crtc *crtc,
  9440. struct drm_framebuffer *fb,
  9441. struct drm_i915_gem_object *obj,
  9442. struct drm_i915_gem_request *req,
  9443. uint32_t flags)
  9444. {
  9445. struct intel_engine_cs *engine = req->engine;
  9446. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9447. uint32_t plane_bit = 0;
  9448. int len, ret;
  9449. switch (intel_crtc->plane) {
  9450. case PLANE_A:
  9451. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9452. break;
  9453. case PLANE_B:
  9454. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9455. break;
  9456. case PLANE_C:
  9457. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9458. break;
  9459. default:
  9460. WARN_ONCE(1, "unknown plane in flip command\n");
  9461. return -ENODEV;
  9462. }
  9463. len = 4;
  9464. if (engine->id == RCS) {
  9465. len += 6;
  9466. /*
  9467. * On Gen 8, SRM is now taking an extra dword to accommodate
  9468. * 48bits addresses, and we need a NOOP for the batch size to
  9469. * stay even.
  9470. */
  9471. if (IS_GEN8(dev))
  9472. len += 2;
  9473. }
  9474. /*
  9475. * BSpec MI_DISPLAY_FLIP for IVB:
  9476. * "The full packet must be contained within the same cache line."
  9477. *
  9478. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9479. * cacheline, if we ever start emitting more commands before
  9480. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9481. * then do the cacheline alignment, and finally emit the
  9482. * MI_DISPLAY_FLIP.
  9483. */
  9484. ret = intel_ring_cacheline_align(req);
  9485. if (ret)
  9486. return ret;
  9487. ret = intel_ring_begin(req, len);
  9488. if (ret)
  9489. return ret;
  9490. /* Unmask the flip-done completion message. Note that the bspec says that
  9491. * we should do this for both the BCS and RCS, and that we must not unmask
  9492. * more than one flip event at any time (or ensure that one flip message
  9493. * can be sent by waiting for flip-done prior to queueing new flips).
  9494. * Experimentation says that BCS works despite DERRMR masking all
  9495. * flip-done completion events and that unmasking all planes at once
  9496. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9497. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9498. */
  9499. if (engine->id == RCS) {
  9500. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
  9501. intel_ring_emit_reg(engine, DERRMR);
  9502. intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9503. DERRMR_PIPEB_PRI_FLIP_DONE |
  9504. DERRMR_PIPEC_PRI_FLIP_DONE));
  9505. if (IS_GEN8(dev))
  9506. intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
  9507. MI_SRM_LRM_GLOBAL_GTT);
  9508. else
  9509. intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
  9510. MI_SRM_LRM_GLOBAL_GTT);
  9511. intel_ring_emit_reg(engine, DERRMR);
  9512. intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
  9513. if (IS_GEN8(dev)) {
  9514. intel_ring_emit(engine, 0);
  9515. intel_ring_emit(engine, MI_NOOP);
  9516. }
  9517. }
  9518. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
  9519. intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
  9520. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9521. intel_ring_emit(engine, (MI_NOOP));
  9522. return 0;
  9523. }
  9524. static bool use_mmio_flip(struct intel_engine_cs *engine,
  9525. struct drm_i915_gem_object *obj)
  9526. {
  9527. struct reservation_object *resv;
  9528. /*
  9529. * This is not being used for older platforms, because
  9530. * non-availability of flip done interrupt forces us to use
  9531. * CS flips. Older platforms derive flip done using some clever
  9532. * tricks involving the flip_pending status bits and vblank irqs.
  9533. * So using MMIO flips there would disrupt this mechanism.
  9534. */
  9535. if (engine == NULL)
  9536. return true;
  9537. if (INTEL_GEN(engine->i915) < 5)
  9538. return false;
  9539. if (i915.use_mmio_flip < 0)
  9540. return false;
  9541. else if (i915.use_mmio_flip > 0)
  9542. return true;
  9543. else if (i915.enable_execlists)
  9544. return true;
  9545. resv = i915_gem_object_get_dmabuf_resv(obj);
  9546. if (resv && !reservation_object_test_signaled_rcu(resv, false))
  9547. return true;
  9548. return engine != i915_gem_request_get_engine(obj->last_write_req);
  9549. }
  9550. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9551. unsigned int rotation,
  9552. struct intel_flip_work *work)
  9553. {
  9554. struct drm_device *dev = intel_crtc->base.dev;
  9555. struct drm_i915_private *dev_priv = to_i915(dev);
  9556. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9557. const enum pipe pipe = intel_crtc->pipe;
  9558. u32 ctl, stride, tile_height;
  9559. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9560. ctl &= ~PLANE_CTL_TILED_MASK;
  9561. switch (fb->modifier[0]) {
  9562. case DRM_FORMAT_MOD_NONE:
  9563. break;
  9564. case I915_FORMAT_MOD_X_TILED:
  9565. ctl |= PLANE_CTL_TILED_X;
  9566. break;
  9567. case I915_FORMAT_MOD_Y_TILED:
  9568. ctl |= PLANE_CTL_TILED_Y;
  9569. break;
  9570. case I915_FORMAT_MOD_Yf_TILED:
  9571. ctl |= PLANE_CTL_TILED_YF;
  9572. break;
  9573. default:
  9574. MISSING_CASE(fb->modifier[0]);
  9575. }
  9576. /*
  9577. * The stride is either expressed as a multiple of 64 bytes chunks for
  9578. * linear buffers or in number of tiles for tiled buffers.
  9579. */
  9580. if (intel_rotation_90_or_270(rotation)) {
  9581. /* stride = Surface height in tiles */
  9582. tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
  9583. stride = DIV_ROUND_UP(fb->height, tile_height);
  9584. } else {
  9585. stride = fb->pitches[0] /
  9586. intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  9587. fb->pixel_format);
  9588. }
  9589. /*
  9590. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9591. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9592. */
  9593. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9594. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9595. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9596. POSTING_READ(PLANE_SURF(pipe, 0));
  9597. }
  9598. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9599. struct intel_flip_work *work)
  9600. {
  9601. struct drm_device *dev = intel_crtc->base.dev;
  9602. struct drm_i915_private *dev_priv = to_i915(dev);
  9603. struct intel_framebuffer *intel_fb =
  9604. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9605. struct drm_i915_gem_object *obj = intel_fb->obj;
  9606. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  9607. u32 dspcntr;
  9608. dspcntr = I915_READ(reg);
  9609. if (obj->tiling_mode != I915_TILING_NONE)
  9610. dspcntr |= DISPPLANE_TILED;
  9611. else
  9612. dspcntr &= ~DISPPLANE_TILED;
  9613. I915_WRITE(reg, dspcntr);
  9614. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9615. POSTING_READ(DSPSURF(intel_crtc->plane));
  9616. }
  9617. static void intel_mmio_flip_work_func(struct work_struct *w)
  9618. {
  9619. struct intel_flip_work *work =
  9620. container_of(w, struct intel_flip_work, mmio_work);
  9621. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9622. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9623. struct intel_framebuffer *intel_fb =
  9624. to_intel_framebuffer(crtc->base.primary->fb);
  9625. struct drm_i915_gem_object *obj = intel_fb->obj;
  9626. struct reservation_object *resv;
  9627. if (work->flip_queued_req)
  9628. WARN_ON(__i915_wait_request(work->flip_queued_req,
  9629. false, NULL,
  9630. NO_WAITBOOST));
  9631. /* For framebuffer backed by dmabuf, wait for fence */
  9632. resv = i915_gem_object_get_dmabuf_resv(obj);
  9633. if (resv)
  9634. WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
  9635. MAX_SCHEDULE_TIMEOUT) < 0);
  9636. intel_pipe_update_start(crtc);
  9637. if (INTEL_GEN(dev_priv) >= 9)
  9638. skl_do_mmio_flip(crtc, work->rotation, work);
  9639. else
  9640. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9641. ilk_do_mmio_flip(crtc, work);
  9642. intel_pipe_update_end(crtc, work);
  9643. }
  9644. static int intel_default_queue_flip(struct drm_device *dev,
  9645. struct drm_crtc *crtc,
  9646. struct drm_framebuffer *fb,
  9647. struct drm_i915_gem_object *obj,
  9648. struct drm_i915_gem_request *req,
  9649. uint32_t flags)
  9650. {
  9651. return -ENODEV;
  9652. }
  9653. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  9654. struct intel_crtc *intel_crtc,
  9655. struct intel_flip_work *work)
  9656. {
  9657. u32 addr, vblank;
  9658. if (!atomic_read(&work->pending))
  9659. return false;
  9660. smp_rmb();
  9661. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  9662. if (work->flip_ready_vblank == 0) {
  9663. if (work->flip_queued_req &&
  9664. !i915_gem_request_completed(work->flip_queued_req))
  9665. return false;
  9666. work->flip_ready_vblank = vblank;
  9667. }
  9668. if (vblank - work->flip_ready_vblank < 3)
  9669. return false;
  9670. /* Potential stall - if we see that the flip has happened,
  9671. * assume a missed interrupt. */
  9672. if (INTEL_GEN(dev_priv) >= 4)
  9673. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9674. else
  9675. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9676. /* There is a potential issue here with a false positive after a flip
  9677. * to the same address. We could address this by checking for a
  9678. * non-incrementing frame counter.
  9679. */
  9680. return addr == work->gtt_offset;
  9681. }
  9682. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  9683. {
  9684. struct drm_device *dev = &dev_priv->drm;
  9685. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9686. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9687. struct intel_flip_work *work;
  9688. WARN_ON(!in_interrupt());
  9689. if (crtc == NULL)
  9690. return;
  9691. spin_lock(&dev->event_lock);
  9692. work = intel_crtc->flip_work;
  9693. if (work != NULL && !is_mmio_work(work) &&
  9694. __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
  9695. WARN_ONCE(1,
  9696. "Kicking stuck page flip: queued at %d, now %d\n",
  9697. work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
  9698. page_flip_completed(intel_crtc);
  9699. work = NULL;
  9700. }
  9701. if (work != NULL && !is_mmio_work(work) &&
  9702. intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
  9703. intel_queue_rps_boost_for_request(work->flip_queued_req);
  9704. spin_unlock(&dev->event_lock);
  9705. }
  9706. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9707. struct drm_framebuffer *fb,
  9708. struct drm_pending_vblank_event *event,
  9709. uint32_t page_flip_flags)
  9710. {
  9711. struct drm_device *dev = crtc->dev;
  9712. struct drm_i915_private *dev_priv = to_i915(dev);
  9713. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9714. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9715. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9716. struct drm_plane *primary = crtc->primary;
  9717. enum pipe pipe = intel_crtc->pipe;
  9718. struct intel_flip_work *work;
  9719. struct intel_engine_cs *engine;
  9720. bool mmio_flip;
  9721. struct drm_i915_gem_request *request = NULL;
  9722. int ret;
  9723. /*
  9724. * drm_mode_page_flip_ioctl() should already catch this, but double
  9725. * check to be safe. In the future we may enable pageflipping from
  9726. * a disabled primary plane.
  9727. */
  9728. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9729. return -EBUSY;
  9730. /* Can't change pixel format via MI display flips. */
  9731. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9732. return -EINVAL;
  9733. /*
  9734. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9735. * Note that pitch changes could also affect these register.
  9736. */
  9737. if (INTEL_INFO(dev)->gen > 3 &&
  9738. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9739. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9740. return -EINVAL;
  9741. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9742. goto out_hang;
  9743. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9744. if (work == NULL)
  9745. return -ENOMEM;
  9746. work->event = event;
  9747. work->crtc = crtc;
  9748. work->old_fb = old_fb;
  9749. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  9750. ret = drm_crtc_vblank_get(crtc);
  9751. if (ret)
  9752. goto free_work;
  9753. /* We borrow the event spin lock for protecting flip_work */
  9754. spin_lock_irq(&dev->event_lock);
  9755. if (intel_crtc->flip_work) {
  9756. /* Before declaring the flip queue wedged, check if
  9757. * the hardware completed the operation behind our backs.
  9758. */
  9759. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  9760. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9761. page_flip_completed(intel_crtc);
  9762. } else {
  9763. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9764. spin_unlock_irq(&dev->event_lock);
  9765. drm_crtc_vblank_put(crtc);
  9766. kfree(work);
  9767. return -EBUSY;
  9768. }
  9769. }
  9770. intel_crtc->flip_work = work;
  9771. spin_unlock_irq(&dev->event_lock);
  9772. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9773. flush_workqueue(dev_priv->wq);
  9774. /* Reference the objects for the scheduled work. */
  9775. drm_framebuffer_reference(work->old_fb);
  9776. drm_gem_object_reference(&obj->base);
  9777. crtc->primary->fb = fb;
  9778. update_state_fb(crtc->primary);
  9779. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  9780. to_intel_plane_state(primary->state));
  9781. work->pending_flip_obj = obj;
  9782. ret = i915_mutex_lock_interruptible(dev);
  9783. if (ret)
  9784. goto cleanup;
  9785. intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9786. if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
  9787. ret = -EIO;
  9788. goto cleanup;
  9789. }
  9790. atomic_inc(&intel_crtc->unpin_work_count);
  9791. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9792. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9793. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  9794. engine = &dev_priv->engine[BCS];
  9795. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9796. /* vlv: DISPLAY_FLIP fails to change tiling */
  9797. engine = NULL;
  9798. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9799. engine = &dev_priv->engine[BCS];
  9800. } else if (INTEL_INFO(dev)->gen >= 7) {
  9801. engine = i915_gem_request_get_engine(obj->last_write_req);
  9802. if (engine == NULL || engine->id != RCS)
  9803. engine = &dev_priv->engine[BCS];
  9804. } else {
  9805. engine = &dev_priv->engine[RCS];
  9806. }
  9807. mmio_flip = use_mmio_flip(engine, obj);
  9808. /* When using CS flips, we want to emit semaphores between rings.
  9809. * However, when using mmio flips we will create a task to do the
  9810. * synchronisation, so all we want here is to pin the framebuffer
  9811. * into the display plane and skip any waits.
  9812. */
  9813. if (!mmio_flip) {
  9814. ret = i915_gem_object_sync(obj, engine, &request);
  9815. if (!ret && !request) {
  9816. request = i915_gem_request_alloc(engine, NULL);
  9817. ret = PTR_ERR_OR_ZERO(request);
  9818. }
  9819. if (ret)
  9820. goto cleanup_pending;
  9821. }
  9822. ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  9823. if (ret)
  9824. goto cleanup_pending;
  9825. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9826. obj, 0);
  9827. work->gtt_offset += intel_crtc->dspaddr_offset;
  9828. work->rotation = crtc->primary->state->rotation;
  9829. if (mmio_flip) {
  9830. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  9831. i915_gem_request_assign(&work->flip_queued_req,
  9832. obj->last_write_req);
  9833. schedule_work(&work->mmio_work);
  9834. } else {
  9835. i915_gem_request_assign(&work->flip_queued_req, request);
  9836. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9837. page_flip_flags);
  9838. if (ret)
  9839. goto cleanup_unpin;
  9840. intel_mark_page_flip_active(intel_crtc, work);
  9841. i915_add_request_no_flush(request);
  9842. }
  9843. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  9844. to_intel_plane(primary)->frontbuffer_bit);
  9845. mutex_unlock(&dev->struct_mutex);
  9846. intel_frontbuffer_flip_prepare(dev,
  9847. to_intel_plane(primary)->frontbuffer_bit);
  9848. trace_i915_flip_request(intel_crtc->plane, obj);
  9849. return 0;
  9850. cleanup_unpin:
  9851. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  9852. cleanup_pending:
  9853. if (!IS_ERR_OR_NULL(request))
  9854. i915_add_request_no_flush(request);
  9855. atomic_dec(&intel_crtc->unpin_work_count);
  9856. mutex_unlock(&dev->struct_mutex);
  9857. cleanup:
  9858. crtc->primary->fb = old_fb;
  9859. update_state_fb(crtc->primary);
  9860. drm_gem_object_unreference_unlocked(&obj->base);
  9861. drm_framebuffer_unreference(work->old_fb);
  9862. spin_lock_irq(&dev->event_lock);
  9863. intel_crtc->flip_work = NULL;
  9864. spin_unlock_irq(&dev->event_lock);
  9865. drm_crtc_vblank_put(crtc);
  9866. free_work:
  9867. kfree(work);
  9868. if (ret == -EIO) {
  9869. struct drm_atomic_state *state;
  9870. struct drm_plane_state *plane_state;
  9871. out_hang:
  9872. state = drm_atomic_state_alloc(dev);
  9873. if (!state)
  9874. return -ENOMEM;
  9875. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9876. retry:
  9877. plane_state = drm_atomic_get_plane_state(state, primary);
  9878. ret = PTR_ERR_OR_ZERO(plane_state);
  9879. if (!ret) {
  9880. drm_atomic_set_fb_for_plane(plane_state, fb);
  9881. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9882. if (!ret)
  9883. ret = drm_atomic_commit(state);
  9884. }
  9885. if (ret == -EDEADLK) {
  9886. drm_modeset_backoff(state->acquire_ctx);
  9887. drm_atomic_state_clear(state);
  9888. goto retry;
  9889. }
  9890. if (ret)
  9891. drm_atomic_state_free(state);
  9892. if (ret == 0 && event) {
  9893. spin_lock_irq(&dev->event_lock);
  9894. drm_crtc_send_vblank_event(crtc, event);
  9895. spin_unlock_irq(&dev->event_lock);
  9896. }
  9897. }
  9898. return ret;
  9899. }
  9900. /**
  9901. * intel_wm_need_update - Check whether watermarks need updating
  9902. * @plane: drm plane
  9903. * @state: new plane state
  9904. *
  9905. * Check current plane state versus the new one to determine whether
  9906. * watermarks need to be recalculated.
  9907. *
  9908. * Returns true or false.
  9909. */
  9910. static bool intel_wm_need_update(struct drm_plane *plane,
  9911. struct drm_plane_state *state)
  9912. {
  9913. struct intel_plane_state *new = to_intel_plane_state(state);
  9914. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9915. /* Update watermarks on tiling or size changes. */
  9916. if (new->visible != cur->visible)
  9917. return true;
  9918. if (!cur->base.fb || !new->base.fb)
  9919. return false;
  9920. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  9921. cur->base.rotation != new->base.rotation ||
  9922. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9923. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9924. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9925. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9926. return true;
  9927. return false;
  9928. }
  9929. static bool needs_scaling(struct intel_plane_state *state)
  9930. {
  9931. int src_w = drm_rect_width(&state->src) >> 16;
  9932. int src_h = drm_rect_height(&state->src) >> 16;
  9933. int dst_w = drm_rect_width(&state->dst);
  9934. int dst_h = drm_rect_height(&state->dst);
  9935. return (src_w != dst_w || src_h != dst_h);
  9936. }
  9937. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9938. struct drm_plane_state *plane_state)
  9939. {
  9940. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9941. struct drm_crtc *crtc = crtc_state->crtc;
  9942. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9943. struct drm_plane *plane = plane_state->plane;
  9944. struct drm_device *dev = crtc->dev;
  9945. struct drm_i915_private *dev_priv = to_i915(dev);
  9946. struct intel_plane_state *old_plane_state =
  9947. to_intel_plane_state(plane->state);
  9948. bool mode_changed = needs_modeset(crtc_state);
  9949. bool was_crtc_enabled = crtc->state->active;
  9950. bool is_crtc_enabled = crtc_state->active;
  9951. bool turn_off, turn_on, visible, was_visible;
  9952. struct drm_framebuffer *fb = plane_state->fb;
  9953. int ret;
  9954. if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  9955. ret = skl_update_scaler_plane(
  9956. to_intel_crtc_state(crtc_state),
  9957. to_intel_plane_state(plane_state));
  9958. if (ret)
  9959. return ret;
  9960. }
  9961. was_visible = old_plane_state->visible;
  9962. visible = to_intel_plane_state(plane_state)->visible;
  9963. if (!was_crtc_enabled && WARN_ON(was_visible))
  9964. was_visible = false;
  9965. /*
  9966. * Visibility is calculated as if the crtc was on, but
  9967. * after scaler setup everything depends on it being off
  9968. * when the crtc isn't active.
  9969. *
  9970. * FIXME this is wrong for watermarks. Watermarks should also
  9971. * be computed as if the pipe would be active. Perhaps move
  9972. * per-plane wm computation to the .check_plane() hook, and
  9973. * only combine the results from all planes in the current place?
  9974. */
  9975. if (!is_crtc_enabled)
  9976. to_intel_plane_state(plane_state)->visible = visible = false;
  9977. if (!was_visible && !visible)
  9978. return 0;
  9979. if (fb != old_plane_state->base.fb)
  9980. pipe_config->fb_changed = true;
  9981. turn_off = was_visible && (!visible || mode_changed);
  9982. turn_on = visible && (!was_visible || mode_changed);
  9983. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  9984. intel_crtc->base.base.id,
  9985. intel_crtc->base.name,
  9986. plane->base.id, plane->name,
  9987. fb ? fb->base.id : -1);
  9988. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  9989. plane->base.id, plane->name,
  9990. was_visible, visible,
  9991. turn_off, turn_on, mode_changed);
  9992. if (turn_on) {
  9993. pipe_config->update_wm_pre = true;
  9994. /* must disable cxsr around plane enable/disable */
  9995. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9996. pipe_config->disable_cxsr = true;
  9997. } else if (turn_off) {
  9998. pipe_config->update_wm_post = true;
  9999. /* must disable cxsr around plane enable/disable */
  10000. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10001. pipe_config->disable_cxsr = true;
  10002. } else if (intel_wm_need_update(plane, plane_state)) {
  10003. /* FIXME bollocks */
  10004. pipe_config->update_wm_pre = true;
  10005. pipe_config->update_wm_post = true;
  10006. }
  10007. /* Pre-gen9 platforms need two-step watermark updates */
  10008. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10009. INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
  10010. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10011. if (visible || was_visible)
  10012. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10013. /*
  10014. * WaCxSRDisabledForSpriteScaling:ivb
  10015. *
  10016. * cstate->update_wm was already set above, so this flag will
  10017. * take effect when we commit and program watermarks.
  10018. */
  10019. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
  10020. needs_scaling(to_intel_plane_state(plane_state)) &&
  10021. !needs_scaling(old_plane_state))
  10022. pipe_config->disable_lp_wm = true;
  10023. return 0;
  10024. }
  10025. static bool encoders_cloneable(const struct intel_encoder *a,
  10026. const struct intel_encoder *b)
  10027. {
  10028. /* masks could be asymmetric, so check both ways */
  10029. return a == b || (a->cloneable & (1 << b->type) &&
  10030. b->cloneable & (1 << a->type));
  10031. }
  10032. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10033. struct intel_crtc *crtc,
  10034. struct intel_encoder *encoder)
  10035. {
  10036. struct intel_encoder *source_encoder;
  10037. struct drm_connector *connector;
  10038. struct drm_connector_state *connector_state;
  10039. int i;
  10040. for_each_connector_in_state(state, connector, connector_state, i) {
  10041. if (connector_state->crtc != &crtc->base)
  10042. continue;
  10043. source_encoder =
  10044. to_intel_encoder(connector_state->best_encoder);
  10045. if (!encoders_cloneable(encoder, source_encoder))
  10046. return false;
  10047. }
  10048. return true;
  10049. }
  10050. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10051. struct drm_crtc_state *crtc_state)
  10052. {
  10053. struct drm_device *dev = crtc->dev;
  10054. struct drm_i915_private *dev_priv = to_i915(dev);
  10055. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10056. struct intel_crtc_state *pipe_config =
  10057. to_intel_crtc_state(crtc_state);
  10058. struct drm_atomic_state *state = crtc_state->state;
  10059. int ret;
  10060. bool mode_changed = needs_modeset(crtc_state);
  10061. if (mode_changed && !crtc_state->active)
  10062. pipe_config->update_wm_post = true;
  10063. if (mode_changed && crtc_state->enable &&
  10064. dev_priv->display.crtc_compute_clock &&
  10065. !WARN_ON(pipe_config->shared_dpll)) {
  10066. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10067. pipe_config);
  10068. if (ret)
  10069. return ret;
  10070. }
  10071. if (crtc_state->color_mgmt_changed) {
  10072. ret = intel_color_check(crtc, crtc_state);
  10073. if (ret)
  10074. return ret;
  10075. /*
  10076. * Changing color management on Intel hardware is
  10077. * handled as part of planes update.
  10078. */
  10079. crtc_state->planes_changed = true;
  10080. }
  10081. ret = 0;
  10082. if (dev_priv->display.compute_pipe_wm) {
  10083. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10084. if (ret) {
  10085. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10086. return ret;
  10087. }
  10088. }
  10089. if (dev_priv->display.compute_intermediate_wm &&
  10090. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10091. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10092. return 0;
  10093. /*
  10094. * Calculate 'intermediate' watermarks that satisfy both the
  10095. * old state and the new state. We can program these
  10096. * immediately.
  10097. */
  10098. ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
  10099. intel_crtc,
  10100. pipe_config);
  10101. if (ret) {
  10102. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10103. return ret;
  10104. }
  10105. } else if (dev_priv->display.compute_intermediate_wm) {
  10106. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10107. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10108. }
  10109. if (INTEL_INFO(dev)->gen >= 9) {
  10110. if (mode_changed)
  10111. ret = skl_update_scaler_crtc(pipe_config);
  10112. if (!ret)
  10113. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10114. pipe_config);
  10115. }
  10116. return ret;
  10117. }
  10118. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10119. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10120. .atomic_begin = intel_begin_crtc_commit,
  10121. .atomic_flush = intel_finish_crtc_commit,
  10122. .atomic_check = intel_crtc_atomic_check,
  10123. };
  10124. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10125. {
  10126. struct intel_connector *connector;
  10127. for_each_intel_connector(dev, connector) {
  10128. if (connector->base.state->crtc)
  10129. drm_connector_unreference(&connector->base);
  10130. if (connector->base.encoder) {
  10131. connector->base.state->best_encoder =
  10132. connector->base.encoder;
  10133. connector->base.state->crtc =
  10134. connector->base.encoder->crtc;
  10135. drm_connector_reference(&connector->base);
  10136. } else {
  10137. connector->base.state->best_encoder = NULL;
  10138. connector->base.state->crtc = NULL;
  10139. }
  10140. }
  10141. }
  10142. static void
  10143. connected_sink_compute_bpp(struct intel_connector *connector,
  10144. struct intel_crtc_state *pipe_config)
  10145. {
  10146. int bpp = pipe_config->pipe_bpp;
  10147. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10148. connector->base.base.id,
  10149. connector->base.name);
  10150. /* Don't use an invalid EDID bpc value */
  10151. if (connector->base.display_info.bpc &&
  10152. connector->base.display_info.bpc * 3 < bpp) {
  10153. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10154. bpp, connector->base.display_info.bpc*3);
  10155. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10156. }
  10157. /* Clamp bpp to default limit on screens without EDID 1.4 */
  10158. if (connector->base.display_info.bpc == 0) {
  10159. int type = connector->base.connector_type;
  10160. int clamp_bpp = 24;
  10161. /* Fall back to 18 bpp when DP sink capability is unknown. */
  10162. if (type == DRM_MODE_CONNECTOR_DisplayPort ||
  10163. type == DRM_MODE_CONNECTOR_eDP)
  10164. clamp_bpp = 18;
  10165. if (bpp > clamp_bpp) {
  10166. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
  10167. bpp, clamp_bpp);
  10168. pipe_config->pipe_bpp = clamp_bpp;
  10169. }
  10170. }
  10171. }
  10172. static int
  10173. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10174. struct intel_crtc_state *pipe_config)
  10175. {
  10176. struct drm_device *dev = crtc->base.dev;
  10177. struct drm_atomic_state *state;
  10178. struct drm_connector *connector;
  10179. struct drm_connector_state *connector_state;
  10180. int bpp, i;
  10181. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  10182. bpp = 10*3;
  10183. else if (INTEL_INFO(dev)->gen >= 5)
  10184. bpp = 12*3;
  10185. else
  10186. bpp = 8*3;
  10187. pipe_config->pipe_bpp = bpp;
  10188. state = pipe_config->base.state;
  10189. /* Clamp display bpp to EDID value */
  10190. for_each_connector_in_state(state, connector, connector_state, i) {
  10191. if (connector_state->crtc != &crtc->base)
  10192. continue;
  10193. connected_sink_compute_bpp(to_intel_connector(connector),
  10194. pipe_config);
  10195. }
  10196. return bpp;
  10197. }
  10198. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10199. {
  10200. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10201. "type: 0x%x flags: 0x%x\n",
  10202. mode->crtc_clock,
  10203. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10204. mode->crtc_hsync_end, mode->crtc_htotal,
  10205. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10206. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10207. }
  10208. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10209. struct intel_crtc_state *pipe_config,
  10210. const char *context)
  10211. {
  10212. struct drm_device *dev = crtc->base.dev;
  10213. struct drm_plane *plane;
  10214. struct intel_plane *intel_plane;
  10215. struct intel_plane_state *state;
  10216. struct drm_framebuffer *fb;
  10217. DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
  10218. crtc->base.base.id, crtc->base.name,
  10219. context, pipe_config, pipe_name(crtc->pipe));
  10220. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  10221. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10222. pipe_config->pipe_bpp, pipe_config->dither);
  10223. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10224. pipe_config->has_pch_encoder,
  10225. pipe_config->fdi_lanes,
  10226. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10227. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10228. pipe_config->fdi_m_n.tu);
  10229. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10230. intel_crtc_has_dp_encoder(pipe_config),
  10231. pipe_config->lane_count,
  10232. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10233. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10234. pipe_config->dp_m_n.tu);
  10235. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10236. intel_crtc_has_dp_encoder(pipe_config),
  10237. pipe_config->lane_count,
  10238. pipe_config->dp_m2_n2.gmch_m,
  10239. pipe_config->dp_m2_n2.gmch_n,
  10240. pipe_config->dp_m2_n2.link_m,
  10241. pipe_config->dp_m2_n2.link_n,
  10242. pipe_config->dp_m2_n2.tu);
  10243. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10244. pipe_config->has_audio,
  10245. pipe_config->has_infoframe);
  10246. DRM_DEBUG_KMS("requested mode:\n");
  10247. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10248. DRM_DEBUG_KMS("adjusted mode:\n");
  10249. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10250. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10251. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10252. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10253. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10254. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10255. crtc->num_scalers,
  10256. pipe_config->scaler_state.scaler_users,
  10257. pipe_config->scaler_state.scaler_id);
  10258. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10259. pipe_config->gmch_pfit.control,
  10260. pipe_config->gmch_pfit.pgm_ratios,
  10261. pipe_config->gmch_pfit.lvds_border_bits);
  10262. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10263. pipe_config->pch_pfit.pos,
  10264. pipe_config->pch_pfit.size,
  10265. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10266. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10267. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10268. if (IS_BROXTON(dev)) {
  10269. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10270. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10271. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10272. pipe_config->ddi_pll_sel,
  10273. pipe_config->dpll_hw_state.ebb0,
  10274. pipe_config->dpll_hw_state.ebb4,
  10275. pipe_config->dpll_hw_state.pll0,
  10276. pipe_config->dpll_hw_state.pll1,
  10277. pipe_config->dpll_hw_state.pll2,
  10278. pipe_config->dpll_hw_state.pll3,
  10279. pipe_config->dpll_hw_state.pll6,
  10280. pipe_config->dpll_hw_state.pll8,
  10281. pipe_config->dpll_hw_state.pll9,
  10282. pipe_config->dpll_hw_state.pll10,
  10283. pipe_config->dpll_hw_state.pcsdw12);
  10284. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10285. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10286. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10287. pipe_config->ddi_pll_sel,
  10288. pipe_config->dpll_hw_state.ctrl1,
  10289. pipe_config->dpll_hw_state.cfgcr1,
  10290. pipe_config->dpll_hw_state.cfgcr2);
  10291. } else if (HAS_DDI(dev)) {
  10292. DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10293. pipe_config->ddi_pll_sel,
  10294. pipe_config->dpll_hw_state.wrpll,
  10295. pipe_config->dpll_hw_state.spll);
  10296. } else {
  10297. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10298. "fp0: 0x%x, fp1: 0x%x\n",
  10299. pipe_config->dpll_hw_state.dpll,
  10300. pipe_config->dpll_hw_state.dpll_md,
  10301. pipe_config->dpll_hw_state.fp0,
  10302. pipe_config->dpll_hw_state.fp1);
  10303. }
  10304. DRM_DEBUG_KMS("planes on this crtc\n");
  10305. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10306. intel_plane = to_intel_plane(plane);
  10307. if (intel_plane->pipe != crtc->pipe)
  10308. continue;
  10309. state = to_intel_plane_state(plane->state);
  10310. fb = state->base.fb;
  10311. if (!fb) {
  10312. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10313. plane->base.id, plane->name, state->scaler_id);
  10314. continue;
  10315. }
  10316. DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
  10317. plane->base.id, plane->name);
  10318. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
  10319. fb->base.id, fb->width, fb->height,
  10320. drm_get_format_name(fb->pixel_format));
  10321. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10322. state->scaler_id,
  10323. state->src.x1 >> 16, state->src.y1 >> 16,
  10324. drm_rect_width(&state->src) >> 16,
  10325. drm_rect_height(&state->src) >> 16,
  10326. state->dst.x1, state->dst.y1,
  10327. drm_rect_width(&state->dst),
  10328. drm_rect_height(&state->dst));
  10329. }
  10330. }
  10331. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10332. {
  10333. struct drm_device *dev = state->dev;
  10334. struct drm_connector *connector;
  10335. unsigned int used_ports = 0;
  10336. /*
  10337. * Walk the connector list instead of the encoder
  10338. * list to detect the problem on ddi platforms
  10339. * where there's just one encoder per digital port.
  10340. */
  10341. drm_for_each_connector(connector, dev) {
  10342. struct drm_connector_state *connector_state;
  10343. struct intel_encoder *encoder;
  10344. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10345. if (!connector_state)
  10346. connector_state = connector->state;
  10347. if (!connector_state->best_encoder)
  10348. continue;
  10349. encoder = to_intel_encoder(connector_state->best_encoder);
  10350. WARN_ON(!connector_state->crtc);
  10351. switch (encoder->type) {
  10352. unsigned int port_mask;
  10353. case INTEL_OUTPUT_UNKNOWN:
  10354. if (WARN_ON(!HAS_DDI(dev)))
  10355. break;
  10356. case INTEL_OUTPUT_DP:
  10357. case INTEL_OUTPUT_HDMI:
  10358. case INTEL_OUTPUT_EDP:
  10359. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10360. /* the same port mustn't appear more than once */
  10361. if (used_ports & port_mask)
  10362. return false;
  10363. used_ports |= port_mask;
  10364. default:
  10365. break;
  10366. }
  10367. }
  10368. return true;
  10369. }
  10370. static void
  10371. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10372. {
  10373. struct drm_crtc_state tmp_state;
  10374. struct intel_crtc_scaler_state scaler_state;
  10375. struct intel_dpll_hw_state dpll_hw_state;
  10376. struct intel_shared_dpll *shared_dpll;
  10377. uint32_t ddi_pll_sel;
  10378. bool force_thru;
  10379. /* FIXME: before the switch to atomic started, a new pipe_config was
  10380. * kzalloc'd. Code that depends on any field being zero should be
  10381. * fixed, so that the crtc_state can be safely duplicated. For now,
  10382. * only fields that are know to not cause problems are preserved. */
  10383. tmp_state = crtc_state->base;
  10384. scaler_state = crtc_state->scaler_state;
  10385. shared_dpll = crtc_state->shared_dpll;
  10386. dpll_hw_state = crtc_state->dpll_hw_state;
  10387. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10388. force_thru = crtc_state->pch_pfit.force_thru;
  10389. memset(crtc_state, 0, sizeof *crtc_state);
  10390. crtc_state->base = tmp_state;
  10391. crtc_state->scaler_state = scaler_state;
  10392. crtc_state->shared_dpll = shared_dpll;
  10393. crtc_state->dpll_hw_state = dpll_hw_state;
  10394. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10395. crtc_state->pch_pfit.force_thru = force_thru;
  10396. }
  10397. static int
  10398. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10399. struct intel_crtc_state *pipe_config)
  10400. {
  10401. struct drm_atomic_state *state = pipe_config->base.state;
  10402. struct intel_encoder *encoder;
  10403. struct drm_connector *connector;
  10404. struct drm_connector_state *connector_state;
  10405. int base_bpp, ret = -EINVAL;
  10406. int i;
  10407. bool retry = true;
  10408. clear_intel_crtc_state(pipe_config);
  10409. pipe_config->cpu_transcoder =
  10410. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10411. /*
  10412. * Sanitize sync polarity flags based on requested ones. If neither
  10413. * positive or negative polarity is requested, treat this as meaning
  10414. * negative polarity.
  10415. */
  10416. if (!(pipe_config->base.adjusted_mode.flags &
  10417. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10418. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10419. if (!(pipe_config->base.adjusted_mode.flags &
  10420. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10421. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10422. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10423. pipe_config);
  10424. if (base_bpp < 0)
  10425. goto fail;
  10426. /*
  10427. * Determine the real pipe dimensions. Note that stereo modes can
  10428. * increase the actual pipe size due to the frame doubling and
  10429. * insertion of additional space for blanks between the frame. This
  10430. * is stored in the crtc timings. We use the requested mode to do this
  10431. * computation to clearly distinguish it from the adjusted mode, which
  10432. * can be changed by the connectors in the below retry loop.
  10433. */
  10434. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10435. &pipe_config->pipe_src_w,
  10436. &pipe_config->pipe_src_h);
  10437. for_each_connector_in_state(state, connector, connector_state, i) {
  10438. if (connector_state->crtc != crtc)
  10439. continue;
  10440. encoder = to_intel_encoder(connector_state->best_encoder);
  10441. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  10442. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10443. goto fail;
  10444. }
  10445. /*
  10446. * Determine output_types before calling the .compute_config()
  10447. * hooks so that the hooks can use this information safely.
  10448. */
  10449. pipe_config->output_types |= 1 << encoder->type;
  10450. }
  10451. encoder_retry:
  10452. /* Ensure the port clock defaults are reset when retrying. */
  10453. pipe_config->port_clock = 0;
  10454. pipe_config->pixel_multiplier = 1;
  10455. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10456. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10457. CRTC_STEREO_DOUBLE);
  10458. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10459. * adjust it according to limitations or connector properties, and also
  10460. * a chance to reject the mode entirely.
  10461. */
  10462. for_each_connector_in_state(state, connector, connector_state, i) {
  10463. if (connector_state->crtc != crtc)
  10464. continue;
  10465. encoder = to_intel_encoder(connector_state->best_encoder);
  10466. if (!(encoder->compute_config(encoder, pipe_config))) {
  10467. DRM_DEBUG_KMS("Encoder config failure\n");
  10468. goto fail;
  10469. }
  10470. }
  10471. /* Set default port clock if not overwritten by the encoder. Needs to be
  10472. * done afterwards in case the encoder adjusts the mode. */
  10473. if (!pipe_config->port_clock)
  10474. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10475. * pipe_config->pixel_multiplier;
  10476. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10477. if (ret < 0) {
  10478. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10479. goto fail;
  10480. }
  10481. if (ret == RETRY) {
  10482. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10483. ret = -EINVAL;
  10484. goto fail;
  10485. }
  10486. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10487. retry = false;
  10488. goto encoder_retry;
  10489. }
  10490. /* Dithering seems to not pass-through bits correctly when it should, so
  10491. * only enable it on 6bpc panels. */
  10492. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10493. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10494. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10495. fail:
  10496. return ret;
  10497. }
  10498. static void
  10499. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10500. {
  10501. struct drm_crtc *crtc;
  10502. struct drm_crtc_state *crtc_state;
  10503. int i;
  10504. /* Double check state. */
  10505. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10506. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10507. /* Update hwmode for vblank functions */
  10508. if (crtc->state->active)
  10509. crtc->hwmode = crtc->state->adjusted_mode;
  10510. else
  10511. crtc->hwmode.crtc_clock = 0;
  10512. /*
  10513. * Update legacy state to satisfy fbc code. This can
  10514. * be removed when fbc uses the atomic state.
  10515. */
  10516. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10517. struct drm_plane_state *plane_state = crtc->primary->state;
  10518. crtc->primary->fb = plane_state->fb;
  10519. crtc->x = plane_state->src_x >> 16;
  10520. crtc->y = plane_state->src_y >> 16;
  10521. }
  10522. }
  10523. }
  10524. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10525. {
  10526. int diff;
  10527. if (clock1 == clock2)
  10528. return true;
  10529. if (!clock1 || !clock2)
  10530. return false;
  10531. diff = abs(clock1 - clock2);
  10532. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10533. return true;
  10534. return false;
  10535. }
  10536. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10537. list_for_each_entry((intel_crtc), \
  10538. &(dev)->mode_config.crtc_list, \
  10539. base.head) \
  10540. for_each_if (mask & (1 <<(intel_crtc)->pipe))
  10541. static bool
  10542. intel_compare_m_n(unsigned int m, unsigned int n,
  10543. unsigned int m2, unsigned int n2,
  10544. bool exact)
  10545. {
  10546. if (m == m2 && n == n2)
  10547. return true;
  10548. if (exact || !m || !n || !m2 || !n2)
  10549. return false;
  10550. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10551. if (n > n2) {
  10552. while (n > n2) {
  10553. m2 <<= 1;
  10554. n2 <<= 1;
  10555. }
  10556. } else if (n < n2) {
  10557. while (n < n2) {
  10558. m <<= 1;
  10559. n <<= 1;
  10560. }
  10561. }
  10562. if (n != n2)
  10563. return false;
  10564. return intel_fuzzy_clock_check(m, m2);
  10565. }
  10566. static bool
  10567. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10568. struct intel_link_m_n *m2_n2,
  10569. bool adjust)
  10570. {
  10571. if (m_n->tu == m2_n2->tu &&
  10572. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10573. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10574. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10575. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10576. if (adjust)
  10577. *m2_n2 = *m_n;
  10578. return true;
  10579. }
  10580. return false;
  10581. }
  10582. static bool
  10583. intel_pipe_config_compare(struct drm_device *dev,
  10584. struct intel_crtc_state *current_config,
  10585. struct intel_crtc_state *pipe_config,
  10586. bool adjust)
  10587. {
  10588. bool ret = true;
  10589. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10590. do { \
  10591. if (!adjust) \
  10592. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10593. else \
  10594. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10595. } while (0)
  10596. #define PIPE_CONF_CHECK_X(name) \
  10597. if (current_config->name != pipe_config->name) { \
  10598. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10599. "(expected 0x%08x, found 0x%08x)\n", \
  10600. current_config->name, \
  10601. pipe_config->name); \
  10602. ret = false; \
  10603. }
  10604. #define PIPE_CONF_CHECK_I(name) \
  10605. if (current_config->name != pipe_config->name) { \
  10606. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10607. "(expected %i, found %i)\n", \
  10608. current_config->name, \
  10609. pipe_config->name); \
  10610. ret = false; \
  10611. }
  10612. #define PIPE_CONF_CHECK_P(name) \
  10613. if (current_config->name != pipe_config->name) { \
  10614. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10615. "(expected %p, found %p)\n", \
  10616. current_config->name, \
  10617. pipe_config->name); \
  10618. ret = false; \
  10619. }
  10620. #define PIPE_CONF_CHECK_M_N(name) \
  10621. if (!intel_compare_link_m_n(&current_config->name, \
  10622. &pipe_config->name,\
  10623. adjust)) { \
  10624. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10625. "(expected tu %i gmch %i/%i link %i/%i, " \
  10626. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10627. current_config->name.tu, \
  10628. current_config->name.gmch_m, \
  10629. current_config->name.gmch_n, \
  10630. current_config->name.link_m, \
  10631. current_config->name.link_n, \
  10632. pipe_config->name.tu, \
  10633. pipe_config->name.gmch_m, \
  10634. pipe_config->name.gmch_n, \
  10635. pipe_config->name.link_m, \
  10636. pipe_config->name.link_n); \
  10637. ret = false; \
  10638. }
  10639. /* This is required for BDW+ where there is only one set of registers for
  10640. * switching between high and low RR.
  10641. * This macro can be used whenever a comparison has to be made between one
  10642. * hw state and multiple sw state variables.
  10643. */
  10644. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10645. if (!intel_compare_link_m_n(&current_config->name, \
  10646. &pipe_config->name, adjust) && \
  10647. !intel_compare_link_m_n(&current_config->alt_name, \
  10648. &pipe_config->name, adjust)) { \
  10649. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10650. "(expected tu %i gmch %i/%i link %i/%i, " \
  10651. "or tu %i gmch %i/%i link %i/%i, " \
  10652. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10653. current_config->name.tu, \
  10654. current_config->name.gmch_m, \
  10655. current_config->name.gmch_n, \
  10656. current_config->name.link_m, \
  10657. current_config->name.link_n, \
  10658. current_config->alt_name.tu, \
  10659. current_config->alt_name.gmch_m, \
  10660. current_config->alt_name.gmch_n, \
  10661. current_config->alt_name.link_m, \
  10662. current_config->alt_name.link_n, \
  10663. pipe_config->name.tu, \
  10664. pipe_config->name.gmch_m, \
  10665. pipe_config->name.gmch_n, \
  10666. pipe_config->name.link_m, \
  10667. pipe_config->name.link_n); \
  10668. ret = false; \
  10669. }
  10670. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10671. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10672. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10673. "(expected %i, found %i)\n", \
  10674. current_config->name & (mask), \
  10675. pipe_config->name & (mask)); \
  10676. ret = false; \
  10677. }
  10678. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10679. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10680. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10681. "(expected %i, found %i)\n", \
  10682. current_config->name, \
  10683. pipe_config->name); \
  10684. ret = false; \
  10685. }
  10686. #define PIPE_CONF_QUIRK(quirk) \
  10687. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10688. PIPE_CONF_CHECK_I(cpu_transcoder);
  10689. PIPE_CONF_CHECK_I(has_pch_encoder);
  10690. PIPE_CONF_CHECK_I(fdi_lanes);
  10691. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10692. PIPE_CONF_CHECK_I(lane_count);
  10693. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  10694. if (INTEL_INFO(dev)->gen < 8) {
  10695. PIPE_CONF_CHECK_M_N(dp_m_n);
  10696. if (current_config->has_drrs)
  10697. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10698. } else
  10699. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10700. PIPE_CONF_CHECK_X(output_types);
  10701. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10702. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10703. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10704. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10705. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10706. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10707. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10708. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10709. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10710. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10711. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10712. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10713. PIPE_CONF_CHECK_I(pixel_multiplier);
  10714. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10715. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10716. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  10717. PIPE_CONF_CHECK_I(limited_color_range);
  10718. PIPE_CONF_CHECK_I(has_infoframe);
  10719. PIPE_CONF_CHECK_I(has_audio);
  10720. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10721. DRM_MODE_FLAG_INTERLACE);
  10722. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10723. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10724. DRM_MODE_FLAG_PHSYNC);
  10725. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10726. DRM_MODE_FLAG_NHSYNC);
  10727. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10728. DRM_MODE_FLAG_PVSYNC);
  10729. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10730. DRM_MODE_FLAG_NVSYNC);
  10731. }
  10732. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10733. /* pfit ratios are autocomputed by the hw on gen4+ */
  10734. if (INTEL_INFO(dev)->gen < 4)
  10735. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  10736. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10737. if (!adjust) {
  10738. PIPE_CONF_CHECK_I(pipe_src_w);
  10739. PIPE_CONF_CHECK_I(pipe_src_h);
  10740. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10741. if (current_config->pch_pfit.enabled) {
  10742. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10743. PIPE_CONF_CHECK_X(pch_pfit.size);
  10744. }
  10745. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10746. }
  10747. /* BDW+ don't expose a synchronous way to read the state */
  10748. if (IS_HASWELL(dev))
  10749. PIPE_CONF_CHECK_I(ips_enabled);
  10750. PIPE_CONF_CHECK_I(double_wide);
  10751. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10752. PIPE_CONF_CHECK_P(shared_dpll);
  10753. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10754. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10755. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10756. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10757. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10758. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10759. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10760. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10761. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10762. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  10763. PIPE_CONF_CHECK_X(dsi_pll.div);
  10764. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10765. PIPE_CONF_CHECK_I(pipe_bpp);
  10766. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10767. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10768. #undef PIPE_CONF_CHECK_X
  10769. #undef PIPE_CONF_CHECK_I
  10770. #undef PIPE_CONF_CHECK_P
  10771. #undef PIPE_CONF_CHECK_FLAGS
  10772. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10773. #undef PIPE_CONF_QUIRK
  10774. #undef INTEL_ERR_OR_DBG_KMS
  10775. return ret;
  10776. }
  10777. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  10778. const struct intel_crtc_state *pipe_config)
  10779. {
  10780. if (pipe_config->has_pch_encoder) {
  10781. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  10782. &pipe_config->fdi_m_n);
  10783. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  10784. /*
  10785. * FDI already provided one idea for the dotclock.
  10786. * Yell if the encoder disagrees.
  10787. */
  10788. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  10789. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10790. fdi_dotclock, dotclock);
  10791. }
  10792. }
  10793. static void verify_wm_state(struct drm_crtc *crtc,
  10794. struct drm_crtc_state *new_state)
  10795. {
  10796. struct drm_device *dev = crtc->dev;
  10797. struct drm_i915_private *dev_priv = to_i915(dev);
  10798. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10799. struct skl_ddb_entry *hw_entry, *sw_entry;
  10800. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10801. const enum pipe pipe = intel_crtc->pipe;
  10802. int plane;
  10803. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  10804. return;
  10805. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10806. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10807. /* planes */
  10808. for_each_plane(dev_priv, pipe, plane) {
  10809. hw_entry = &hw_ddb.plane[pipe][plane];
  10810. sw_entry = &sw_ddb->plane[pipe][plane];
  10811. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10812. continue;
  10813. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10814. "(expected (%u,%u), found (%u,%u))\n",
  10815. pipe_name(pipe), plane + 1,
  10816. sw_entry->start, sw_entry->end,
  10817. hw_entry->start, hw_entry->end);
  10818. }
  10819. /* cursor */
  10820. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10821. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10822. if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
  10823. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10824. "(expected (%u,%u), found (%u,%u))\n",
  10825. pipe_name(pipe),
  10826. sw_entry->start, sw_entry->end,
  10827. hw_entry->start, hw_entry->end);
  10828. }
  10829. }
  10830. static void
  10831. verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
  10832. {
  10833. struct drm_connector *connector;
  10834. drm_for_each_connector(connector, dev) {
  10835. struct drm_encoder *encoder = connector->encoder;
  10836. struct drm_connector_state *state = connector->state;
  10837. if (state->crtc != crtc)
  10838. continue;
  10839. intel_connector_verify_state(to_intel_connector(connector));
  10840. I915_STATE_WARN(state->best_encoder != encoder,
  10841. "connector's atomic encoder doesn't match legacy encoder\n");
  10842. }
  10843. }
  10844. static void
  10845. verify_encoder_state(struct drm_device *dev)
  10846. {
  10847. struct intel_encoder *encoder;
  10848. struct intel_connector *connector;
  10849. for_each_intel_encoder(dev, encoder) {
  10850. bool enabled = false;
  10851. enum pipe pipe;
  10852. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10853. encoder->base.base.id,
  10854. encoder->base.name);
  10855. for_each_intel_connector(dev, connector) {
  10856. if (connector->base.state->best_encoder != &encoder->base)
  10857. continue;
  10858. enabled = true;
  10859. I915_STATE_WARN(connector->base.state->crtc !=
  10860. encoder->base.crtc,
  10861. "connector's crtc doesn't match encoder crtc\n");
  10862. }
  10863. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10864. "encoder's enabled state mismatch "
  10865. "(expected %i, found %i)\n",
  10866. !!encoder->base.crtc, enabled);
  10867. if (!encoder->base.crtc) {
  10868. bool active;
  10869. active = encoder->get_hw_state(encoder, &pipe);
  10870. I915_STATE_WARN(active,
  10871. "encoder detached but still enabled on pipe %c.\n",
  10872. pipe_name(pipe));
  10873. }
  10874. }
  10875. }
  10876. static void
  10877. verify_crtc_state(struct drm_crtc *crtc,
  10878. struct drm_crtc_state *old_crtc_state,
  10879. struct drm_crtc_state *new_crtc_state)
  10880. {
  10881. struct drm_device *dev = crtc->dev;
  10882. struct drm_i915_private *dev_priv = to_i915(dev);
  10883. struct intel_encoder *encoder;
  10884. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10885. struct intel_crtc_state *pipe_config, *sw_config;
  10886. struct drm_atomic_state *old_state;
  10887. bool active;
  10888. old_state = old_crtc_state->state;
  10889. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10890. pipe_config = to_intel_crtc_state(old_crtc_state);
  10891. memset(pipe_config, 0, sizeof(*pipe_config));
  10892. pipe_config->base.crtc = crtc;
  10893. pipe_config->base.state = old_state;
  10894. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  10895. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10896. /* hw state is inconsistent with the pipe quirk */
  10897. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10898. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10899. active = new_crtc_state->active;
  10900. I915_STATE_WARN(new_crtc_state->active != active,
  10901. "crtc active state doesn't match with hw state "
  10902. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10903. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10904. "transitional active state does not match atomic hw state "
  10905. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10906. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10907. enum pipe pipe;
  10908. active = encoder->get_hw_state(encoder, &pipe);
  10909. I915_STATE_WARN(active != new_crtc_state->active,
  10910. "[ENCODER:%i] active %i with crtc active %i\n",
  10911. encoder->base.base.id, active, new_crtc_state->active);
  10912. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10913. "Encoder connected to wrong pipe %c\n",
  10914. pipe_name(pipe));
  10915. if (active) {
  10916. pipe_config->output_types |= 1 << encoder->type;
  10917. encoder->get_config(encoder, pipe_config);
  10918. }
  10919. }
  10920. if (!new_crtc_state->active)
  10921. return;
  10922. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10923. sw_config = to_intel_crtc_state(crtc->state);
  10924. if (!intel_pipe_config_compare(dev, sw_config,
  10925. pipe_config, false)) {
  10926. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10927. intel_dump_pipe_config(intel_crtc, pipe_config,
  10928. "[hw state]");
  10929. intel_dump_pipe_config(intel_crtc, sw_config,
  10930. "[sw state]");
  10931. }
  10932. }
  10933. static void
  10934. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10935. struct intel_shared_dpll *pll,
  10936. struct drm_crtc *crtc,
  10937. struct drm_crtc_state *new_state)
  10938. {
  10939. struct intel_dpll_hw_state dpll_hw_state;
  10940. unsigned crtc_mask;
  10941. bool active;
  10942. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10943. DRM_DEBUG_KMS("%s\n", pll->name);
  10944. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10945. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10946. I915_STATE_WARN(!pll->on && pll->active_mask,
  10947. "pll in active use but not on in sw tracking\n");
  10948. I915_STATE_WARN(pll->on && !pll->active_mask,
  10949. "pll is on but not used by any active crtc\n");
  10950. I915_STATE_WARN(pll->on != active,
  10951. "pll on state mismatch (expected %i, found %i)\n",
  10952. pll->on, active);
  10953. }
  10954. if (!crtc) {
  10955. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  10956. "more active pll users than references: %x vs %x\n",
  10957. pll->active_mask, pll->config.crtc_mask);
  10958. return;
  10959. }
  10960. crtc_mask = 1 << drm_crtc_index(crtc);
  10961. if (new_state->active)
  10962. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10963. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10964. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10965. else
  10966. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10967. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10968. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10969. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  10970. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10971. crtc_mask, pll->config.crtc_mask);
  10972. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  10973. &dpll_hw_state,
  10974. sizeof(dpll_hw_state)),
  10975. "pll hw state mismatch\n");
  10976. }
  10977. static void
  10978. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10979. struct drm_crtc_state *old_crtc_state,
  10980. struct drm_crtc_state *new_crtc_state)
  10981. {
  10982. struct drm_i915_private *dev_priv = to_i915(dev);
  10983. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10984. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10985. if (new_state->shared_dpll)
  10986. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10987. if (old_state->shared_dpll &&
  10988. old_state->shared_dpll != new_state->shared_dpll) {
  10989. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  10990. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10991. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10992. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10993. pipe_name(drm_crtc_index(crtc)));
  10994. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  10995. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10996. pipe_name(drm_crtc_index(crtc)));
  10997. }
  10998. }
  10999. static void
  11000. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11001. struct drm_crtc_state *old_state,
  11002. struct drm_crtc_state *new_state)
  11003. {
  11004. if (!needs_modeset(new_state) &&
  11005. !to_intel_crtc_state(new_state)->update_pipe)
  11006. return;
  11007. verify_wm_state(crtc, new_state);
  11008. verify_connector_state(crtc->dev, crtc);
  11009. verify_crtc_state(crtc, old_state, new_state);
  11010. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11011. }
  11012. static void
  11013. verify_disabled_dpll_state(struct drm_device *dev)
  11014. {
  11015. struct drm_i915_private *dev_priv = to_i915(dev);
  11016. int i;
  11017. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11018. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11019. }
  11020. static void
  11021. intel_modeset_verify_disabled(struct drm_device *dev)
  11022. {
  11023. verify_encoder_state(dev);
  11024. verify_connector_state(dev, NULL);
  11025. verify_disabled_dpll_state(dev);
  11026. }
  11027. static void update_scanline_offset(struct intel_crtc *crtc)
  11028. {
  11029. struct drm_device *dev = crtc->base.dev;
  11030. /*
  11031. * The scanline counter increments at the leading edge of hsync.
  11032. *
  11033. * On most platforms it starts counting from vtotal-1 on the
  11034. * first active line. That means the scanline counter value is
  11035. * always one less than what we would expect. Ie. just after
  11036. * start of vblank, which also occurs at start of hsync (on the
  11037. * last active line), the scanline counter will read vblank_start-1.
  11038. *
  11039. * On gen2 the scanline counter starts counting from 1 instead
  11040. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11041. * to keep the value positive), instead of adding one.
  11042. *
  11043. * On HSW+ the behaviour of the scanline counter depends on the output
  11044. * type. For DP ports it behaves like most other platforms, but on HDMI
  11045. * there's an extra 1 line difference. So we need to add two instead of
  11046. * one to the value.
  11047. */
  11048. if (IS_GEN2(dev)) {
  11049. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11050. int vtotal;
  11051. vtotal = adjusted_mode->crtc_vtotal;
  11052. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11053. vtotal /= 2;
  11054. crtc->scanline_offset = vtotal - 1;
  11055. } else if (HAS_DDI(dev) &&
  11056. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  11057. crtc->scanline_offset = 2;
  11058. } else
  11059. crtc->scanline_offset = 1;
  11060. }
  11061. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11062. {
  11063. struct drm_device *dev = state->dev;
  11064. struct drm_i915_private *dev_priv = to_i915(dev);
  11065. struct intel_shared_dpll_config *shared_dpll = NULL;
  11066. struct drm_crtc *crtc;
  11067. struct drm_crtc_state *crtc_state;
  11068. int i;
  11069. if (!dev_priv->display.crtc_compute_clock)
  11070. return;
  11071. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11072. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11073. struct intel_shared_dpll *old_dpll =
  11074. to_intel_crtc_state(crtc->state)->shared_dpll;
  11075. if (!needs_modeset(crtc_state))
  11076. continue;
  11077. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11078. if (!old_dpll)
  11079. continue;
  11080. if (!shared_dpll)
  11081. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11082. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11083. }
  11084. }
  11085. /*
  11086. * This implements the workaround described in the "notes" section of the mode
  11087. * set sequence documentation. When going from no pipes or single pipe to
  11088. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11089. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11090. */
  11091. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11092. {
  11093. struct drm_crtc_state *crtc_state;
  11094. struct intel_crtc *intel_crtc;
  11095. struct drm_crtc *crtc;
  11096. struct intel_crtc_state *first_crtc_state = NULL;
  11097. struct intel_crtc_state *other_crtc_state = NULL;
  11098. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11099. int i;
  11100. /* look at all crtc's that are going to be enabled in during modeset */
  11101. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11102. intel_crtc = to_intel_crtc(crtc);
  11103. if (!crtc_state->active || !needs_modeset(crtc_state))
  11104. continue;
  11105. if (first_crtc_state) {
  11106. other_crtc_state = to_intel_crtc_state(crtc_state);
  11107. break;
  11108. } else {
  11109. first_crtc_state = to_intel_crtc_state(crtc_state);
  11110. first_pipe = intel_crtc->pipe;
  11111. }
  11112. }
  11113. /* No workaround needed? */
  11114. if (!first_crtc_state)
  11115. return 0;
  11116. /* w/a possibly needed, check how many crtc's are already enabled. */
  11117. for_each_intel_crtc(state->dev, intel_crtc) {
  11118. struct intel_crtc_state *pipe_config;
  11119. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11120. if (IS_ERR(pipe_config))
  11121. return PTR_ERR(pipe_config);
  11122. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11123. if (!pipe_config->base.active ||
  11124. needs_modeset(&pipe_config->base))
  11125. continue;
  11126. /* 2 or more enabled crtcs means no need for w/a */
  11127. if (enabled_pipe != INVALID_PIPE)
  11128. return 0;
  11129. enabled_pipe = intel_crtc->pipe;
  11130. }
  11131. if (enabled_pipe != INVALID_PIPE)
  11132. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11133. else if (other_crtc_state)
  11134. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11135. return 0;
  11136. }
  11137. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11138. {
  11139. struct drm_crtc *crtc;
  11140. struct drm_crtc_state *crtc_state;
  11141. int ret = 0;
  11142. /* add all active pipes to the state */
  11143. for_each_crtc(state->dev, crtc) {
  11144. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11145. if (IS_ERR(crtc_state))
  11146. return PTR_ERR(crtc_state);
  11147. if (!crtc_state->active || needs_modeset(crtc_state))
  11148. continue;
  11149. crtc_state->mode_changed = true;
  11150. ret = drm_atomic_add_affected_connectors(state, crtc);
  11151. if (ret)
  11152. break;
  11153. ret = drm_atomic_add_affected_planes(state, crtc);
  11154. if (ret)
  11155. break;
  11156. }
  11157. return ret;
  11158. }
  11159. static int intel_modeset_checks(struct drm_atomic_state *state)
  11160. {
  11161. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11162. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11163. struct drm_crtc *crtc;
  11164. struct drm_crtc_state *crtc_state;
  11165. int ret = 0, i;
  11166. if (!check_digital_port_conflicts(state)) {
  11167. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11168. return -EINVAL;
  11169. }
  11170. intel_state->modeset = true;
  11171. intel_state->active_crtcs = dev_priv->active_crtcs;
  11172. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11173. if (crtc_state->active)
  11174. intel_state->active_crtcs |= 1 << i;
  11175. else
  11176. intel_state->active_crtcs &= ~(1 << i);
  11177. if (crtc_state->active != crtc->state->active)
  11178. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11179. }
  11180. /*
  11181. * See if the config requires any additional preparation, e.g.
  11182. * to adjust global state with pipes off. We need to do this
  11183. * here so we can get the modeset_pipe updated config for the new
  11184. * mode set on this crtc. For other crtcs we need to use the
  11185. * adjusted_mode bits in the crtc directly.
  11186. */
  11187. if (dev_priv->display.modeset_calc_cdclk) {
  11188. if (!intel_state->cdclk_pll_vco)
  11189. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11190. if (!intel_state->cdclk_pll_vco)
  11191. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11192. ret = dev_priv->display.modeset_calc_cdclk(state);
  11193. if (ret < 0)
  11194. return ret;
  11195. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11196. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
  11197. ret = intel_modeset_all_pipes(state);
  11198. if (ret < 0)
  11199. return ret;
  11200. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11201. intel_state->cdclk, intel_state->dev_cdclk);
  11202. } else
  11203. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11204. intel_modeset_clear_plls(state);
  11205. if (IS_HASWELL(dev_priv))
  11206. return haswell_mode_set_planes_workaround(state);
  11207. return 0;
  11208. }
  11209. /*
  11210. * Handle calculation of various watermark data at the end of the atomic check
  11211. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11212. * handlers to ensure that all derived state has been updated.
  11213. */
  11214. static int calc_watermark_data(struct drm_atomic_state *state)
  11215. {
  11216. struct drm_device *dev = state->dev;
  11217. struct drm_i915_private *dev_priv = to_i915(dev);
  11218. /* Is there platform-specific watermark information to calculate? */
  11219. if (dev_priv->display.compute_global_watermarks)
  11220. return dev_priv->display.compute_global_watermarks(state);
  11221. return 0;
  11222. }
  11223. /**
  11224. * intel_atomic_check - validate state object
  11225. * @dev: drm device
  11226. * @state: state to validate
  11227. */
  11228. static int intel_atomic_check(struct drm_device *dev,
  11229. struct drm_atomic_state *state)
  11230. {
  11231. struct drm_i915_private *dev_priv = to_i915(dev);
  11232. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11233. struct drm_crtc *crtc;
  11234. struct drm_crtc_state *crtc_state;
  11235. int ret, i;
  11236. bool any_ms = false;
  11237. ret = drm_atomic_helper_check_modeset(dev, state);
  11238. if (ret)
  11239. return ret;
  11240. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11241. struct intel_crtc_state *pipe_config =
  11242. to_intel_crtc_state(crtc_state);
  11243. /* Catch I915_MODE_FLAG_INHERITED */
  11244. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11245. crtc_state->mode_changed = true;
  11246. if (!needs_modeset(crtc_state))
  11247. continue;
  11248. if (!crtc_state->enable) {
  11249. any_ms = true;
  11250. continue;
  11251. }
  11252. /* FIXME: For only active_changed we shouldn't need to do any
  11253. * state recomputation at all. */
  11254. ret = drm_atomic_add_affected_connectors(state, crtc);
  11255. if (ret)
  11256. return ret;
  11257. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11258. if (ret) {
  11259. intel_dump_pipe_config(to_intel_crtc(crtc),
  11260. pipe_config, "[failed]");
  11261. return ret;
  11262. }
  11263. if (i915.fastboot &&
  11264. intel_pipe_config_compare(dev,
  11265. to_intel_crtc_state(crtc->state),
  11266. pipe_config, true)) {
  11267. crtc_state->mode_changed = false;
  11268. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11269. }
  11270. if (needs_modeset(crtc_state))
  11271. any_ms = true;
  11272. ret = drm_atomic_add_affected_planes(state, crtc);
  11273. if (ret)
  11274. return ret;
  11275. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11276. needs_modeset(crtc_state) ?
  11277. "[modeset]" : "[fastset]");
  11278. }
  11279. if (any_ms) {
  11280. ret = intel_modeset_checks(state);
  11281. if (ret)
  11282. return ret;
  11283. } else
  11284. intel_state->cdclk = dev_priv->cdclk_freq;
  11285. ret = drm_atomic_helper_check_planes(dev, state);
  11286. if (ret)
  11287. return ret;
  11288. intel_fbc_choose_crtc(dev_priv, state);
  11289. return calc_watermark_data(state);
  11290. }
  11291. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11292. struct drm_atomic_state *state,
  11293. bool nonblock)
  11294. {
  11295. struct drm_i915_private *dev_priv = to_i915(dev);
  11296. struct drm_plane_state *plane_state;
  11297. struct drm_crtc_state *crtc_state;
  11298. struct drm_plane *plane;
  11299. struct drm_crtc *crtc;
  11300. int i, ret;
  11301. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11302. if (state->legacy_cursor_update)
  11303. continue;
  11304. ret = intel_crtc_wait_for_pending_flips(crtc);
  11305. if (ret)
  11306. return ret;
  11307. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11308. flush_workqueue(dev_priv->wq);
  11309. }
  11310. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11311. if (ret)
  11312. return ret;
  11313. ret = drm_atomic_helper_prepare_planes(dev, state);
  11314. mutex_unlock(&dev->struct_mutex);
  11315. if (!ret && !nonblock) {
  11316. for_each_plane_in_state(state, plane, plane_state, i) {
  11317. struct intel_plane_state *intel_plane_state =
  11318. to_intel_plane_state(plane_state);
  11319. if (!intel_plane_state->wait_req)
  11320. continue;
  11321. ret = __i915_wait_request(intel_plane_state->wait_req,
  11322. true, NULL, NULL);
  11323. if (ret) {
  11324. /* Any hang should be swallowed by the wait */
  11325. WARN_ON(ret == -EIO);
  11326. mutex_lock(&dev->struct_mutex);
  11327. drm_atomic_helper_cleanup_planes(dev, state);
  11328. mutex_unlock(&dev->struct_mutex);
  11329. break;
  11330. }
  11331. }
  11332. }
  11333. return ret;
  11334. }
  11335. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11336. {
  11337. struct drm_device *dev = crtc->base.dev;
  11338. if (!dev->max_vblank_count)
  11339. return drm_accurate_vblank_count(&crtc->base);
  11340. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11341. }
  11342. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11343. struct drm_i915_private *dev_priv,
  11344. unsigned crtc_mask)
  11345. {
  11346. unsigned last_vblank_count[I915_MAX_PIPES];
  11347. enum pipe pipe;
  11348. int ret;
  11349. if (!crtc_mask)
  11350. return;
  11351. for_each_pipe(dev_priv, pipe) {
  11352. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11353. if (!((1 << pipe) & crtc_mask))
  11354. continue;
  11355. ret = drm_crtc_vblank_get(crtc);
  11356. if (WARN_ON(ret != 0)) {
  11357. crtc_mask &= ~(1 << pipe);
  11358. continue;
  11359. }
  11360. last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
  11361. }
  11362. for_each_pipe(dev_priv, pipe) {
  11363. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11364. long lret;
  11365. if (!((1 << pipe) & crtc_mask))
  11366. continue;
  11367. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11368. last_vblank_count[pipe] !=
  11369. drm_crtc_vblank_count(crtc),
  11370. msecs_to_jiffies(50));
  11371. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11372. drm_crtc_vblank_put(crtc);
  11373. }
  11374. }
  11375. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11376. {
  11377. /* fb updated, need to unpin old fb */
  11378. if (crtc_state->fb_changed)
  11379. return true;
  11380. /* wm changes, need vblank before final wm's */
  11381. if (crtc_state->update_wm_post)
  11382. return true;
  11383. /*
  11384. * cxsr is re-enabled after vblank.
  11385. * This is already handled by crtc_state->update_wm_post,
  11386. * but added for clarity.
  11387. */
  11388. if (crtc_state->disable_cxsr)
  11389. return true;
  11390. return false;
  11391. }
  11392. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  11393. {
  11394. struct drm_device *dev = state->dev;
  11395. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11396. struct drm_i915_private *dev_priv = to_i915(dev);
  11397. struct drm_crtc_state *old_crtc_state;
  11398. struct drm_crtc *crtc;
  11399. struct intel_crtc_state *intel_cstate;
  11400. struct drm_plane *plane;
  11401. struct drm_plane_state *plane_state;
  11402. bool hw_check = intel_state->modeset;
  11403. unsigned long put_domains[I915_MAX_PIPES] = {};
  11404. unsigned crtc_vblank_mask = 0;
  11405. int i, ret;
  11406. for_each_plane_in_state(state, plane, plane_state, i) {
  11407. struct intel_plane_state *intel_plane_state =
  11408. to_intel_plane_state(plane_state);
  11409. if (!intel_plane_state->wait_req)
  11410. continue;
  11411. ret = __i915_wait_request(intel_plane_state->wait_req,
  11412. true, NULL, NULL);
  11413. /* EIO should be eaten, and we can't get interrupted in the
  11414. * worker, and blocking commits have waited already. */
  11415. WARN_ON(ret);
  11416. }
  11417. drm_atomic_helper_wait_for_dependencies(state);
  11418. if (intel_state->modeset) {
  11419. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  11420. sizeof(intel_state->min_pixclk));
  11421. dev_priv->active_crtcs = intel_state->active_crtcs;
  11422. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  11423. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  11424. }
  11425. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11426. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11427. if (needs_modeset(crtc->state) ||
  11428. to_intel_crtc_state(crtc->state)->update_pipe) {
  11429. hw_check = true;
  11430. put_domains[to_intel_crtc(crtc)->pipe] =
  11431. modeset_get_crtc_power_domains(crtc,
  11432. to_intel_crtc_state(crtc->state));
  11433. }
  11434. if (!needs_modeset(crtc->state))
  11435. continue;
  11436. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11437. if (old_crtc_state->active) {
  11438. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  11439. dev_priv->display.crtc_disable(crtc);
  11440. intel_crtc->active = false;
  11441. intel_fbc_disable(intel_crtc);
  11442. intel_disable_shared_dpll(intel_crtc);
  11443. /*
  11444. * Underruns don't always raise
  11445. * interrupts, so check manually.
  11446. */
  11447. intel_check_cpu_fifo_underruns(dev_priv);
  11448. intel_check_pch_fifo_underruns(dev_priv);
  11449. if (!crtc->state->active)
  11450. intel_update_watermarks(crtc);
  11451. }
  11452. }
  11453. /* Only after disabling all output pipelines that will be changed can we
  11454. * update the the output configuration. */
  11455. intel_modeset_update_crtc_state(state);
  11456. if (intel_state->modeset) {
  11457. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11458. if (dev_priv->display.modeset_commit_cdclk &&
  11459. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11460. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  11461. dev_priv->display.modeset_commit_cdclk(state);
  11462. intel_modeset_verify_disabled(dev);
  11463. }
  11464. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11465. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11466. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11467. bool modeset = needs_modeset(crtc->state);
  11468. struct intel_crtc_state *pipe_config =
  11469. to_intel_crtc_state(crtc->state);
  11470. if (modeset && crtc->state->active) {
  11471. update_scanline_offset(to_intel_crtc(crtc));
  11472. dev_priv->display.crtc_enable(crtc);
  11473. }
  11474. /* Complete events for now disable pipes here. */
  11475. if (modeset && !crtc->state->active && crtc->state->event) {
  11476. spin_lock_irq(&dev->event_lock);
  11477. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  11478. spin_unlock_irq(&dev->event_lock);
  11479. crtc->state->event = NULL;
  11480. }
  11481. if (!modeset)
  11482. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11483. if (crtc->state->active &&
  11484. drm_atomic_get_existing_plane_state(state, crtc->primary))
  11485. intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
  11486. if (crtc->state->active)
  11487. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11488. if (pipe_config->base.active && needs_vblank_wait(pipe_config))
  11489. crtc_vblank_mask |= 1 << i;
  11490. }
  11491. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  11492. * already, but still need the state for the delayed optimization. To
  11493. * fix this:
  11494. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  11495. * - schedule that vblank worker _before_ calling hw_done
  11496. * - at the start of commit_tail, cancel it _synchrously
  11497. * - switch over to the vblank wait helper in the core after that since
  11498. * we don't need out special handling any more.
  11499. */
  11500. if (!state->legacy_cursor_update)
  11501. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  11502. /*
  11503. * Now that the vblank has passed, we can go ahead and program the
  11504. * optimal watermarks on platforms that need two-step watermark
  11505. * programming.
  11506. *
  11507. * TODO: Move this (and other cleanup) to an async worker eventually.
  11508. */
  11509. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11510. intel_cstate = to_intel_crtc_state(crtc->state);
  11511. if (dev_priv->display.optimize_watermarks)
  11512. dev_priv->display.optimize_watermarks(intel_cstate);
  11513. }
  11514. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11515. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  11516. if (put_domains[i])
  11517. modeset_put_power_domains(dev_priv, put_domains[i]);
  11518. intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
  11519. }
  11520. drm_atomic_helper_commit_hw_done(state);
  11521. if (intel_state->modeset)
  11522. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  11523. mutex_lock(&dev->struct_mutex);
  11524. drm_atomic_helper_cleanup_planes(dev, state);
  11525. mutex_unlock(&dev->struct_mutex);
  11526. drm_atomic_helper_commit_cleanup_done(state);
  11527. drm_atomic_state_free(state);
  11528. /* As one of the primary mmio accessors, KMS has a high likelihood
  11529. * of triggering bugs in unclaimed access. After we finish
  11530. * modesetting, see if an error has been flagged, and if so
  11531. * enable debugging for the next modeset - and hope we catch
  11532. * the culprit.
  11533. *
  11534. * XXX note that we assume display power is on at this point.
  11535. * This might hold true now but we need to add pm helper to check
  11536. * unclaimed only when the hardware is on, as atomic commits
  11537. * can happen also when the device is completely off.
  11538. */
  11539. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  11540. }
  11541. static void intel_atomic_commit_work(struct work_struct *work)
  11542. {
  11543. struct drm_atomic_state *state = container_of(work,
  11544. struct drm_atomic_state,
  11545. commit_work);
  11546. intel_atomic_commit_tail(state);
  11547. }
  11548. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  11549. {
  11550. struct drm_plane_state *old_plane_state;
  11551. struct drm_plane *plane;
  11552. struct drm_i915_gem_object *obj, *old_obj;
  11553. struct intel_plane *intel_plane;
  11554. int i;
  11555. mutex_lock(&state->dev->struct_mutex);
  11556. for_each_plane_in_state(state, plane, old_plane_state, i) {
  11557. obj = intel_fb_obj(plane->state->fb);
  11558. old_obj = intel_fb_obj(old_plane_state->fb);
  11559. intel_plane = to_intel_plane(plane);
  11560. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11561. }
  11562. mutex_unlock(&state->dev->struct_mutex);
  11563. }
  11564. /**
  11565. * intel_atomic_commit - commit validated state object
  11566. * @dev: DRM device
  11567. * @state: the top-level driver state object
  11568. * @nonblock: nonblocking commit
  11569. *
  11570. * This function commits a top-level state object that has been validated
  11571. * with drm_atomic_helper_check().
  11572. *
  11573. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11574. * nonblocking commits are only safe for pure plane updates. Everything else
  11575. * should work though.
  11576. *
  11577. * RETURNS
  11578. * Zero for success or -errno.
  11579. */
  11580. static int intel_atomic_commit(struct drm_device *dev,
  11581. struct drm_atomic_state *state,
  11582. bool nonblock)
  11583. {
  11584. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11585. struct drm_i915_private *dev_priv = to_i915(dev);
  11586. int ret = 0;
  11587. if (intel_state->modeset && nonblock) {
  11588. DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
  11589. return -EINVAL;
  11590. }
  11591. ret = drm_atomic_helper_setup_commit(state, nonblock);
  11592. if (ret)
  11593. return ret;
  11594. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  11595. ret = intel_atomic_prepare_commit(dev, state, nonblock);
  11596. if (ret) {
  11597. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11598. return ret;
  11599. }
  11600. drm_atomic_helper_swap_state(state, true);
  11601. dev_priv->wm.distrust_bios_wm = false;
  11602. dev_priv->wm.skl_results = intel_state->wm_results;
  11603. intel_shared_dpll_commit(state);
  11604. intel_atomic_track_fbs(state);
  11605. if (nonblock)
  11606. queue_work(system_unbound_wq, &state->commit_work);
  11607. else
  11608. intel_atomic_commit_tail(state);
  11609. return 0;
  11610. }
  11611. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11612. {
  11613. struct drm_device *dev = crtc->dev;
  11614. struct drm_atomic_state *state;
  11615. struct drm_crtc_state *crtc_state;
  11616. int ret;
  11617. state = drm_atomic_state_alloc(dev);
  11618. if (!state) {
  11619. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  11620. crtc->base.id, crtc->name);
  11621. return;
  11622. }
  11623. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11624. retry:
  11625. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11626. ret = PTR_ERR_OR_ZERO(crtc_state);
  11627. if (!ret) {
  11628. if (!crtc_state->active)
  11629. goto out;
  11630. crtc_state->mode_changed = true;
  11631. ret = drm_atomic_commit(state);
  11632. }
  11633. if (ret == -EDEADLK) {
  11634. drm_atomic_state_clear(state);
  11635. drm_modeset_backoff(state->acquire_ctx);
  11636. goto retry;
  11637. }
  11638. if (ret)
  11639. out:
  11640. drm_atomic_state_free(state);
  11641. }
  11642. #undef for_each_intel_crtc_masked
  11643. /*
  11644. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  11645. * drm_atomic_helper_legacy_gamma_set() directly.
  11646. */
  11647. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  11648. u16 *red, u16 *green, u16 *blue,
  11649. uint32_t size)
  11650. {
  11651. struct drm_device *dev = crtc->dev;
  11652. struct drm_mode_config *config = &dev->mode_config;
  11653. struct drm_crtc_state *state;
  11654. int ret;
  11655. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  11656. if (ret)
  11657. return ret;
  11658. /*
  11659. * Make sure we update the legacy properties so this works when
  11660. * atomic is not enabled.
  11661. */
  11662. state = crtc->state;
  11663. drm_object_property_set_value(&crtc->base,
  11664. config->degamma_lut_property,
  11665. (state->degamma_lut) ?
  11666. state->degamma_lut->base.id : 0);
  11667. drm_object_property_set_value(&crtc->base,
  11668. config->ctm_property,
  11669. (state->ctm) ?
  11670. state->ctm->base.id : 0);
  11671. drm_object_property_set_value(&crtc->base,
  11672. config->gamma_lut_property,
  11673. (state->gamma_lut) ?
  11674. state->gamma_lut->base.id : 0);
  11675. return 0;
  11676. }
  11677. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11678. .gamma_set = intel_atomic_legacy_gamma_set,
  11679. .set_config = drm_atomic_helper_set_config,
  11680. .set_property = drm_atomic_helper_crtc_set_property,
  11681. .destroy = intel_crtc_destroy,
  11682. .page_flip = intel_crtc_page_flip,
  11683. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11684. .atomic_destroy_state = intel_crtc_destroy_state,
  11685. };
  11686. /**
  11687. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11688. * @plane: drm plane to prepare for
  11689. * @fb: framebuffer to prepare for presentation
  11690. *
  11691. * Prepares a framebuffer for usage on a display plane. Generally this
  11692. * involves pinning the underlying object and updating the frontbuffer tracking
  11693. * bits. Some older platforms need special physical address handling for
  11694. * cursor planes.
  11695. *
  11696. * Must be called with struct_mutex held.
  11697. *
  11698. * Returns 0 on success, negative error code on failure.
  11699. */
  11700. int
  11701. intel_prepare_plane_fb(struct drm_plane *plane,
  11702. const struct drm_plane_state *new_state)
  11703. {
  11704. struct drm_device *dev = plane->dev;
  11705. struct drm_framebuffer *fb = new_state->fb;
  11706. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11707. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11708. struct reservation_object *resv;
  11709. int ret = 0;
  11710. if (!obj && !old_obj)
  11711. return 0;
  11712. if (old_obj) {
  11713. struct drm_crtc_state *crtc_state =
  11714. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11715. /* Big Hammer, we also need to ensure that any pending
  11716. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11717. * current scanout is retired before unpinning the old
  11718. * framebuffer. Note that we rely on userspace rendering
  11719. * into the buffer attached to the pipe they are waiting
  11720. * on. If not, userspace generates a GPU hang with IPEHR
  11721. * point to the MI_WAIT_FOR_EVENT.
  11722. *
  11723. * This should only fail upon a hung GPU, in which case we
  11724. * can safely continue.
  11725. */
  11726. if (needs_modeset(crtc_state))
  11727. ret = i915_gem_object_wait_rendering(old_obj, true);
  11728. if (ret) {
  11729. /* GPU hangs should have been swallowed by the wait */
  11730. WARN_ON(ret == -EIO);
  11731. return ret;
  11732. }
  11733. }
  11734. if (!obj)
  11735. return 0;
  11736. /* For framebuffer backed by dmabuf, wait for fence */
  11737. resv = i915_gem_object_get_dmabuf_resv(obj);
  11738. if (resv) {
  11739. long lret;
  11740. lret = reservation_object_wait_timeout_rcu(resv, false, true,
  11741. MAX_SCHEDULE_TIMEOUT);
  11742. if (lret == -ERESTARTSYS)
  11743. return lret;
  11744. WARN(lret < 0, "waiting returns %li\n", lret);
  11745. }
  11746. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11747. INTEL_INFO(dev)->cursor_needs_physical) {
  11748. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11749. ret = i915_gem_object_attach_phys(obj, align);
  11750. if (ret)
  11751. DRM_DEBUG_KMS("failed to attach phys object\n");
  11752. } else {
  11753. ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  11754. }
  11755. if (ret == 0) {
  11756. struct intel_plane_state *plane_state =
  11757. to_intel_plane_state(new_state);
  11758. i915_gem_request_assign(&plane_state->wait_req,
  11759. obj->last_write_req);
  11760. }
  11761. return ret;
  11762. }
  11763. /**
  11764. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11765. * @plane: drm plane to clean up for
  11766. * @fb: old framebuffer that was on plane
  11767. *
  11768. * Cleans up a framebuffer that has just been removed from a plane.
  11769. *
  11770. * Must be called with struct_mutex held.
  11771. */
  11772. void
  11773. intel_cleanup_plane_fb(struct drm_plane *plane,
  11774. const struct drm_plane_state *old_state)
  11775. {
  11776. struct drm_device *dev = plane->dev;
  11777. struct intel_plane_state *old_intel_state;
  11778. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11779. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11780. old_intel_state = to_intel_plane_state(old_state);
  11781. if (!obj && !old_obj)
  11782. return;
  11783. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11784. !INTEL_INFO(dev)->cursor_needs_physical))
  11785. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  11786. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11787. }
  11788. int
  11789. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11790. {
  11791. int max_scale;
  11792. int crtc_clock, cdclk;
  11793. if (!intel_crtc || !crtc_state->base.enable)
  11794. return DRM_PLANE_HELPER_NO_SCALING;
  11795. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11796. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11797. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11798. return DRM_PLANE_HELPER_NO_SCALING;
  11799. /*
  11800. * skl max scale is lower of:
  11801. * close to 3 but not 3, -1 is for that purpose
  11802. * or
  11803. * cdclk/crtc_clock
  11804. */
  11805. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11806. return max_scale;
  11807. }
  11808. static int
  11809. intel_check_primary_plane(struct drm_plane *plane,
  11810. struct intel_crtc_state *crtc_state,
  11811. struct intel_plane_state *state)
  11812. {
  11813. struct drm_crtc *crtc = state->base.crtc;
  11814. struct drm_framebuffer *fb = state->base.fb;
  11815. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11816. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11817. bool can_position = false;
  11818. if (INTEL_INFO(plane->dev)->gen >= 9) {
  11819. /* use scaler when colorkey is not required */
  11820. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11821. min_scale = 1;
  11822. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11823. }
  11824. can_position = true;
  11825. }
  11826. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11827. &state->dst, &state->clip,
  11828. state->base.rotation,
  11829. min_scale, max_scale,
  11830. can_position, true,
  11831. &state->visible);
  11832. }
  11833. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11834. struct drm_crtc_state *old_crtc_state)
  11835. {
  11836. struct drm_device *dev = crtc->dev;
  11837. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11838. struct intel_crtc_state *old_intel_state =
  11839. to_intel_crtc_state(old_crtc_state);
  11840. bool modeset = needs_modeset(crtc->state);
  11841. /* Perform vblank evasion around commit operation */
  11842. intel_pipe_update_start(intel_crtc);
  11843. if (modeset)
  11844. return;
  11845. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  11846. intel_color_set_csc(crtc->state);
  11847. intel_color_load_luts(crtc->state);
  11848. }
  11849. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11850. intel_update_pipe_config(intel_crtc, old_intel_state);
  11851. else if (INTEL_INFO(dev)->gen >= 9)
  11852. skl_detach_scalers(intel_crtc);
  11853. }
  11854. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11855. struct drm_crtc_state *old_crtc_state)
  11856. {
  11857. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11858. intel_pipe_update_end(intel_crtc, NULL);
  11859. }
  11860. /**
  11861. * intel_plane_destroy - destroy a plane
  11862. * @plane: plane to destroy
  11863. *
  11864. * Common destruction function for all types of planes (primary, cursor,
  11865. * sprite).
  11866. */
  11867. void intel_plane_destroy(struct drm_plane *plane)
  11868. {
  11869. if (!plane)
  11870. return;
  11871. drm_plane_cleanup(plane);
  11872. kfree(to_intel_plane(plane));
  11873. }
  11874. const struct drm_plane_funcs intel_plane_funcs = {
  11875. .update_plane = drm_atomic_helper_update_plane,
  11876. .disable_plane = drm_atomic_helper_disable_plane,
  11877. .destroy = intel_plane_destroy,
  11878. .set_property = drm_atomic_helper_plane_set_property,
  11879. .atomic_get_property = intel_plane_atomic_get_property,
  11880. .atomic_set_property = intel_plane_atomic_set_property,
  11881. .atomic_duplicate_state = intel_plane_duplicate_state,
  11882. .atomic_destroy_state = intel_plane_destroy_state,
  11883. };
  11884. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11885. int pipe)
  11886. {
  11887. struct intel_plane *primary = NULL;
  11888. struct intel_plane_state *state = NULL;
  11889. const uint32_t *intel_primary_formats;
  11890. unsigned int num_formats;
  11891. int ret;
  11892. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11893. if (!primary)
  11894. goto fail;
  11895. state = intel_create_plane_state(&primary->base);
  11896. if (!state)
  11897. goto fail;
  11898. primary->base.state = &state->base;
  11899. primary->can_scale = false;
  11900. primary->max_downscale = 1;
  11901. if (INTEL_INFO(dev)->gen >= 9) {
  11902. primary->can_scale = true;
  11903. state->scaler_id = -1;
  11904. }
  11905. primary->pipe = pipe;
  11906. primary->plane = pipe;
  11907. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11908. primary->check_plane = intel_check_primary_plane;
  11909. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11910. primary->plane = !pipe;
  11911. if (INTEL_INFO(dev)->gen >= 9) {
  11912. intel_primary_formats = skl_primary_formats;
  11913. num_formats = ARRAY_SIZE(skl_primary_formats);
  11914. primary->update_plane = skylake_update_primary_plane;
  11915. primary->disable_plane = skylake_disable_primary_plane;
  11916. } else if (HAS_PCH_SPLIT(dev)) {
  11917. intel_primary_formats = i965_primary_formats;
  11918. num_formats = ARRAY_SIZE(i965_primary_formats);
  11919. primary->update_plane = ironlake_update_primary_plane;
  11920. primary->disable_plane = i9xx_disable_primary_plane;
  11921. } else if (INTEL_INFO(dev)->gen >= 4) {
  11922. intel_primary_formats = i965_primary_formats;
  11923. num_formats = ARRAY_SIZE(i965_primary_formats);
  11924. primary->update_plane = i9xx_update_primary_plane;
  11925. primary->disable_plane = i9xx_disable_primary_plane;
  11926. } else {
  11927. intel_primary_formats = i8xx_primary_formats;
  11928. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11929. primary->update_plane = i9xx_update_primary_plane;
  11930. primary->disable_plane = i9xx_disable_primary_plane;
  11931. }
  11932. if (INTEL_INFO(dev)->gen >= 9)
  11933. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11934. &intel_plane_funcs,
  11935. intel_primary_formats, num_formats,
  11936. DRM_PLANE_TYPE_PRIMARY,
  11937. "plane 1%c", pipe_name(pipe));
  11938. else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  11939. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11940. &intel_plane_funcs,
  11941. intel_primary_formats, num_formats,
  11942. DRM_PLANE_TYPE_PRIMARY,
  11943. "primary %c", pipe_name(pipe));
  11944. else
  11945. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11946. &intel_plane_funcs,
  11947. intel_primary_formats, num_formats,
  11948. DRM_PLANE_TYPE_PRIMARY,
  11949. "plane %c", plane_name(primary->plane));
  11950. if (ret)
  11951. goto fail;
  11952. if (INTEL_INFO(dev)->gen >= 4)
  11953. intel_create_rotation_property(dev, primary);
  11954. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11955. return &primary->base;
  11956. fail:
  11957. kfree(state);
  11958. kfree(primary);
  11959. return NULL;
  11960. }
  11961. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11962. {
  11963. if (!dev->mode_config.rotation_property) {
  11964. unsigned long flags = BIT(DRM_ROTATE_0) |
  11965. BIT(DRM_ROTATE_180);
  11966. if (INTEL_INFO(dev)->gen >= 9)
  11967. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11968. dev->mode_config.rotation_property =
  11969. drm_mode_create_rotation_property(dev, flags);
  11970. }
  11971. if (dev->mode_config.rotation_property)
  11972. drm_object_attach_property(&plane->base.base,
  11973. dev->mode_config.rotation_property,
  11974. plane->base.state->rotation);
  11975. }
  11976. static int
  11977. intel_check_cursor_plane(struct drm_plane *plane,
  11978. struct intel_crtc_state *crtc_state,
  11979. struct intel_plane_state *state)
  11980. {
  11981. struct drm_crtc *crtc = crtc_state->base.crtc;
  11982. struct drm_framebuffer *fb = state->base.fb;
  11983. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11984. enum pipe pipe = to_intel_plane(plane)->pipe;
  11985. unsigned stride;
  11986. int ret;
  11987. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11988. &state->dst, &state->clip,
  11989. state->base.rotation,
  11990. DRM_PLANE_HELPER_NO_SCALING,
  11991. DRM_PLANE_HELPER_NO_SCALING,
  11992. true, true, &state->visible);
  11993. if (ret)
  11994. return ret;
  11995. /* if we want to turn off the cursor ignore width and height */
  11996. if (!obj)
  11997. return 0;
  11998. /* Check for which cursor types we support */
  11999. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  12000. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  12001. state->base.crtc_w, state->base.crtc_h);
  12002. return -EINVAL;
  12003. }
  12004. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  12005. if (obj->base.size < stride * state->base.crtc_h) {
  12006. DRM_DEBUG_KMS("buffer is too small\n");
  12007. return -ENOMEM;
  12008. }
  12009. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  12010. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  12011. return -EINVAL;
  12012. }
  12013. /*
  12014. * There's something wrong with the cursor on CHV pipe C.
  12015. * If it straddles the left edge of the screen then
  12016. * moving it away from the edge or disabling it often
  12017. * results in a pipe underrun, and often that can lead to
  12018. * dead pipe (constant underrun reported, and it scans
  12019. * out just a solid color). To recover from that, the
  12020. * display power well must be turned off and on again.
  12021. * Refuse the put the cursor into that compromised position.
  12022. */
  12023. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  12024. state->visible && state->base.crtc_x < 0) {
  12025. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12026. return -EINVAL;
  12027. }
  12028. return 0;
  12029. }
  12030. static void
  12031. intel_disable_cursor_plane(struct drm_plane *plane,
  12032. struct drm_crtc *crtc)
  12033. {
  12034. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12035. intel_crtc->cursor_addr = 0;
  12036. intel_crtc_update_cursor(crtc, NULL);
  12037. }
  12038. static void
  12039. intel_update_cursor_plane(struct drm_plane *plane,
  12040. const struct intel_crtc_state *crtc_state,
  12041. const struct intel_plane_state *state)
  12042. {
  12043. struct drm_crtc *crtc = crtc_state->base.crtc;
  12044. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12045. struct drm_device *dev = plane->dev;
  12046. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12047. uint32_t addr;
  12048. if (!obj)
  12049. addr = 0;
  12050. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  12051. addr = i915_gem_obj_ggtt_offset(obj);
  12052. else
  12053. addr = obj->phys_handle->busaddr;
  12054. intel_crtc->cursor_addr = addr;
  12055. intel_crtc_update_cursor(crtc, state);
  12056. }
  12057. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  12058. int pipe)
  12059. {
  12060. struct intel_plane *cursor = NULL;
  12061. struct intel_plane_state *state = NULL;
  12062. int ret;
  12063. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12064. if (!cursor)
  12065. goto fail;
  12066. state = intel_create_plane_state(&cursor->base);
  12067. if (!state)
  12068. goto fail;
  12069. cursor->base.state = &state->base;
  12070. cursor->can_scale = false;
  12071. cursor->max_downscale = 1;
  12072. cursor->pipe = pipe;
  12073. cursor->plane = pipe;
  12074. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12075. cursor->check_plane = intel_check_cursor_plane;
  12076. cursor->update_plane = intel_update_cursor_plane;
  12077. cursor->disable_plane = intel_disable_cursor_plane;
  12078. ret = drm_universal_plane_init(dev, &cursor->base, 0,
  12079. &intel_plane_funcs,
  12080. intel_cursor_formats,
  12081. ARRAY_SIZE(intel_cursor_formats),
  12082. DRM_PLANE_TYPE_CURSOR,
  12083. "cursor %c", pipe_name(pipe));
  12084. if (ret)
  12085. goto fail;
  12086. if (INTEL_INFO(dev)->gen >= 4) {
  12087. if (!dev->mode_config.rotation_property)
  12088. dev->mode_config.rotation_property =
  12089. drm_mode_create_rotation_property(dev,
  12090. BIT(DRM_ROTATE_0) |
  12091. BIT(DRM_ROTATE_180));
  12092. if (dev->mode_config.rotation_property)
  12093. drm_object_attach_property(&cursor->base.base,
  12094. dev->mode_config.rotation_property,
  12095. state->base.rotation);
  12096. }
  12097. if (INTEL_INFO(dev)->gen >=9)
  12098. state->scaler_id = -1;
  12099. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12100. return &cursor->base;
  12101. fail:
  12102. kfree(state);
  12103. kfree(cursor);
  12104. return NULL;
  12105. }
  12106. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  12107. struct intel_crtc_state *crtc_state)
  12108. {
  12109. int i;
  12110. struct intel_scaler *intel_scaler;
  12111. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  12112. for (i = 0; i < intel_crtc->num_scalers; i++) {
  12113. intel_scaler = &scaler_state->scalers[i];
  12114. intel_scaler->in_use = 0;
  12115. intel_scaler->mode = PS_SCALER_MODE_DYN;
  12116. }
  12117. scaler_state->scaler_id = -1;
  12118. }
  12119. static void intel_crtc_init(struct drm_device *dev, int pipe)
  12120. {
  12121. struct drm_i915_private *dev_priv = to_i915(dev);
  12122. struct intel_crtc *intel_crtc;
  12123. struct intel_crtc_state *crtc_state = NULL;
  12124. struct drm_plane *primary = NULL;
  12125. struct drm_plane *cursor = NULL;
  12126. int ret;
  12127. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12128. if (intel_crtc == NULL)
  12129. return;
  12130. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12131. if (!crtc_state)
  12132. goto fail;
  12133. intel_crtc->config = crtc_state;
  12134. intel_crtc->base.state = &crtc_state->base;
  12135. crtc_state->base.crtc = &intel_crtc->base;
  12136. /* initialize shared scalers */
  12137. if (INTEL_INFO(dev)->gen >= 9) {
  12138. if (pipe == PIPE_C)
  12139. intel_crtc->num_scalers = 1;
  12140. else
  12141. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  12142. skl_init_scalers(dev, intel_crtc, crtc_state);
  12143. }
  12144. primary = intel_primary_plane_create(dev, pipe);
  12145. if (!primary)
  12146. goto fail;
  12147. cursor = intel_cursor_plane_create(dev, pipe);
  12148. if (!cursor)
  12149. goto fail;
  12150. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  12151. cursor, &intel_crtc_funcs,
  12152. "pipe %c", pipe_name(pipe));
  12153. if (ret)
  12154. goto fail;
  12155. /*
  12156. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  12157. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  12158. */
  12159. intel_crtc->pipe = pipe;
  12160. intel_crtc->plane = pipe;
  12161. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  12162. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  12163. intel_crtc->plane = !pipe;
  12164. }
  12165. intel_crtc->cursor_base = ~0;
  12166. intel_crtc->cursor_cntl = ~0;
  12167. intel_crtc->cursor_size = ~0;
  12168. intel_crtc->wm.cxsr_allowed = true;
  12169. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12170. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12171. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  12172. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  12173. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12174. intel_color_init(&intel_crtc->base);
  12175. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12176. return;
  12177. fail:
  12178. intel_plane_destroy(primary);
  12179. intel_plane_destroy(cursor);
  12180. kfree(crtc_state);
  12181. kfree(intel_crtc);
  12182. }
  12183. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12184. {
  12185. struct drm_encoder *encoder = connector->base.encoder;
  12186. struct drm_device *dev = connector->base.dev;
  12187. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12188. if (!encoder || WARN_ON(!encoder->crtc))
  12189. return INVALID_PIPE;
  12190. return to_intel_crtc(encoder->crtc)->pipe;
  12191. }
  12192. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12193. struct drm_file *file)
  12194. {
  12195. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12196. struct drm_crtc *drmmode_crtc;
  12197. struct intel_crtc *crtc;
  12198. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12199. if (!drmmode_crtc)
  12200. return -ENOENT;
  12201. crtc = to_intel_crtc(drmmode_crtc);
  12202. pipe_from_crtc_id->pipe = crtc->pipe;
  12203. return 0;
  12204. }
  12205. static int intel_encoder_clones(struct intel_encoder *encoder)
  12206. {
  12207. struct drm_device *dev = encoder->base.dev;
  12208. struct intel_encoder *source_encoder;
  12209. int index_mask = 0;
  12210. int entry = 0;
  12211. for_each_intel_encoder(dev, source_encoder) {
  12212. if (encoders_cloneable(encoder, source_encoder))
  12213. index_mask |= (1 << entry);
  12214. entry++;
  12215. }
  12216. return index_mask;
  12217. }
  12218. static bool has_edp_a(struct drm_device *dev)
  12219. {
  12220. struct drm_i915_private *dev_priv = to_i915(dev);
  12221. if (!IS_MOBILE(dev))
  12222. return false;
  12223. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12224. return false;
  12225. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12226. return false;
  12227. return true;
  12228. }
  12229. static bool intel_crt_present(struct drm_device *dev)
  12230. {
  12231. struct drm_i915_private *dev_priv = to_i915(dev);
  12232. if (INTEL_INFO(dev)->gen >= 9)
  12233. return false;
  12234. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  12235. return false;
  12236. if (IS_CHERRYVIEW(dev))
  12237. return false;
  12238. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12239. return false;
  12240. /* DDI E can't be used if DDI A requires 4 lanes */
  12241. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12242. return false;
  12243. if (!dev_priv->vbt.int_crt_support)
  12244. return false;
  12245. return true;
  12246. }
  12247. static void intel_setup_outputs(struct drm_device *dev)
  12248. {
  12249. struct drm_i915_private *dev_priv = to_i915(dev);
  12250. struct intel_encoder *encoder;
  12251. bool dpd_is_edp = false;
  12252. /*
  12253. * intel_edp_init_connector() depends on this completing first, to
  12254. * prevent the registeration of both eDP and LVDS and the incorrect
  12255. * sharing of the PPS.
  12256. */
  12257. intel_lvds_init(dev);
  12258. if (intel_crt_present(dev))
  12259. intel_crt_init(dev);
  12260. if (IS_BROXTON(dev)) {
  12261. /*
  12262. * FIXME: Broxton doesn't support port detection via the
  12263. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12264. * detect the ports.
  12265. */
  12266. intel_ddi_init(dev, PORT_A);
  12267. intel_ddi_init(dev, PORT_B);
  12268. intel_ddi_init(dev, PORT_C);
  12269. intel_dsi_init(dev);
  12270. } else if (HAS_DDI(dev)) {
  12271. int found;
  12272. /*
  12273. * Haswell uses DDI functions to detect digital outputs.
  12274. * On SKL pre-D0 the strap isn't connected, so we assume
  12275. * it's there.
  12276. */
  12277. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12278. /* WaIgnoreDDIAStrap: skl */
  12279. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12280. intel_ddi_init(dev, PORT_A);
  12281. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12282. * register */
  12283. found = I915_READ(SFUSE_STRAP);
  12284. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12285. intel_ddi_init(dev, PORT_B);
  12286. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12287. intel_ddi_init(dev, PORT_C);
  12288. if (found & SFUSE_STRAP_DDID_DETECTED)
  12289. intel_ddi_init(dev, PORT_D);
  12290. /*
  12291. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12292. */
  12293. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  12294. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12295. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12296. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12297. intel_ddi_init(dev, PORT_E);
  12298. } else if (HAS_PCH_SPLIT(dev)) {
  12299. int found;
  12300. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12301. if (has_edp_a(dev))
  12302. intel_dp_init(dev, DP_A, PORT_A);
  12303. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12304. /* PCH SDVOB multiplex with HDMIB */
  12305. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12306. if (!found)
  12307. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12308. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12309. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12310. }
  12311. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12312. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12313. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12314. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12315. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12316. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12317. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12318. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12319. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12320. bool has_edp, has_port;
  12321. /*
  12322. * The DP_DETECTED bit is the latched state of the DDC
  12323. * SDA pin at boot. However since eDP doesn't require DDC
  12324. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12325. * eDP ports may have been muxed to an alternate function.
  12326. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12327. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12328. * detect eDP ports.
  12329. *
  12330. * Sadly the straps seem to be missing sometimes even for HDMI
  12331. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  12332. * and VBT for the presence of the port. Additionally we can't
  12333. * trust the port type the VBT declares as we've seen at least
  12334. * HDMI ports that the VBT claim are DP or eDP.
  12335. */
  12336. has_edp = intel_dp_is_edp(dev, PORT_B);
  12337. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  12338. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  12339. has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
  12340. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  12341. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  12342. has_edp = intel_dp_is_edp(dev, PORT_C);
  12343. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  12344. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  12345. has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
  12346. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  12347. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  12348. if (IS_CHERRYVIEW(dev)) {
  12349. /*
  12350. * eDP not supported on port D,
  12351. * so no need to worry about it
  12352. */
  12353. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  12354. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  12355. intel_dp_init(dev, CHV_DP_D, PORT_D);
  12356. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  12357. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  12358. }
  12359. intel_dsi_init(dev);
  12360. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12361. bool found = false;
  12362. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12363. DRM_DEBUG_KMS("probing SDVOB\n");
  12364. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  12365. if (!found && IS_G4X(dev)) {
  12366. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12367. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12368. }
  12369. if (!found && IS_G4X(dev))
  12370. intel_dp_init(dev, DP_B, PORT_B);
  12371. }
  12372. /* Before G4X SDVOC doesn't have its own detect register */
  12373. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12374. DRM_DEBUG_KMS("probing SDVOC\n");
  12375. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  12376. }
  12377. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12378. if (IS_G4X(dev)) {
  12379. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12380. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12381. }
  12382. if (IS_G4X(dev))
  12383. intel_dp_init(dev, DP_C, PORT_C);
  12384. }
  12385. if (IS_G4X(dev) &&
  12386. (I915_READ(DP_D) & DP_DETECTED))
  12387. intel_dp_init(dev, DP_D, PORT_D);
  12388. } else if (IS_GEN2(dev))
  12389. intel_dvo_init(dev);
  12390. if (SUPPORTS_TV(dev))
  12391. intel_tv_init(dev);
  12392. intel_psr_init(dev);
  12393. for_each_intel_encoder(dev, encoder) {
  12394. encoder->base.possible_crtcs = encoder->crtc_mask;
  12395. encoder->base.possible_clones =
  12396. intel_encoder_clones(encoder);
  12397. }
  12398. intel_init_pch_refclk(dev);
  12399. drm_helper_move_panel_connectors_to_head(dev);
  12400. }
  12401. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12402. {
  12403. struct drm_device *dev = fb->dev;
  12404. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12405. drm_framebuffer_cleanup(fb);
  12406. mutex_lock(&dev->struct_mutex);
  12407. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12408. drm_gem_object_unreference(&intel_fb->obj->base);
  12409. mutex_unlock(&dev->struct_mutex);
  12410. kfree(intel_fb);
  12411. }
  12412. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12413. struct drm_file *file,
  12414. unsigned int *handle)
  12415. {
  12416. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12417. struct drm_i915_gem_object *obj = intel_fb->obj;
  12418. if (obj->userptr.mm) {
  12419. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  12420. return -EINVAL;
  12421. }
  12422. return drm_gem_handle_create(file, &obj->base, handle);
  12423. }
  12424. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12425. struct drm_file *file,
  12426. unsigned flags, unsigned color,
  12427. struct drm_clip_rect *clips,
  12428. unsigned num_clips)
  12429. {
  12430. struct drm_device *dev = fb->dev;
  12431. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12432. struct drm_i915_gem_object *obj = intel_fb->obj;
  12433. mutex_lock(&dev->struct_mutex);
  12434. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  12435. mutex_unlock(&dev->struct_mutex);
  12436. return 0;
  12437. }
  12438. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12439. .destroy = intel_user_framebuffer_destroy,
  12440. .create_handle = intel_user_framebuffer_create_handle,
  12441. .dirty = intel_user_framebuffer_dirty,
  12442. };
  12443. static
  12444. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12445. uint32_t pixel_format)
  12446. {
  12447. u32 gen = INTEL_INFO(dev)->gen;
  12448. if (gen >= 9) {
  12449. int cpp = drm_format_plane_cpp(pixel_format, 0);
  12450. /* "The stride in bytes must not exceed the of the size of 8K
  12451. * pixels and 32K bytes."
  12452. */
  12453. return min(8192 * cpp, 32768);
  12454. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12455. return 32*1024;
  12456. } else if (gen >= 4) {
  12457. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12458. return 16*1024;
  12459. else
  12460. return 32*1024;
  12461. } else if (gen >= 3) {
  12462. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12463. return 8*1024;
  12464. else
  12465. return 16*1024;
  12466. } else {
  12467. /* XXX DSPC is limited to 4k tiled */
  12468. return 8*1024;
  12469. }
  12470. }
  12471. static int intel_framebuffer_init(struct drm_device *dev,
  12472. struct intel_framebuffer *intel_fb,
  12473. struct drm_mode_fb_cmd2 *mode_cmd,
  12474. struct drm_i915_gem_object *obj)
  12475. {
  12476. struct drm_i915_private *dev_priv = to_i915(dev);
  12477. unsigned int aligned_height;
  12478. int ret;
  12479. u32 pitch_limit, stride_alignment;
  12480. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12481. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12482. /* Enforce that fb modifier and tiling mode match, but only for
  12483. * X-tiled. This is needed for FBC. */
  12484. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12485. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12486. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12487. return -EINVAL;
  12488. }
  12489. } else {
  12490. if (obj->tiling_mode == I915_TILING_X)
  12491. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12492. else if (obj->tiling_mode == I915_TILING_Y) {
  12493. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12494. return -EINVAL;
  12495. }
  12496. }
  12497. /* Passed in modifier sanity checking. */
  12498. switch (mode_cmd->modifier[0]) {
  12499. case I915_FORMAT_MOD_Y_TILED:
  12500. case I915_FORMAT_MOD_Yf_TILED:
  12501. if (INTEL_INFO(dev)->gen < 9) {
  12502. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12503. mode_cmd->modifier[0]);
  12504. return -EINVAL;
  12505. }
  12506. case DRM_FORMAT_MOD_NONE:
  12507. case I915_FORMAT_MOD_X_TILED:
  12508. break;
  12509. default:
  12510. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12511. mode_cmd->modifier[0]);
  12512. return -EINVAL;
  12513. }
  12514. stride_alignment = intel_fb_stride_alignment(dev_priv,
  12515. mode_cmd->modifier[0],
  12516. mode_cmd->pixel_format);
  12517. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12518. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12519. mode_cmd->pitches[0], stride_alignment);
  12520. return -EINVAL;
  12521. }
  12522. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12523. mode_cmd->pixel_format);
  12524. if (mode_cmd->pitches[0] > pitch_limit) {
  12525. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12526. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12527. "tiled" : "linear",
  12528. mode_cmd->pitches[0], pitch_limit);
  12529. return -EINVAL;
  12530. }
  12531. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12532. mode_cmd->pitches[0] != obj->stride) {
  12533. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12534. mode_cmd->pitches[0], obj->stride);
  12535. return -EINVAL;
  12536. }
  12537. /* Reject formats not supported by any plane early. */
  12538. switch (mode_cmd->pixel_format) {
  12539. case DRM_FORMAT_C8:
  12540. case DRM_FORMAT_RGB565:
  12541. case DRM_FORMAT_XRGB8888:
  12542. case DRM_FORMAT_ARGB8888:
  12543. break;
  12544. case DRM_FORMAT_XRGB1555:
  12545. if (INTEL_INFO(dev)->gen > 3) {
  12546. DRM_DEBUG("unsupported pixel format: %s\n",
  12547. drm_get_format_name(mode_cmd->pixel_format));
  12548. return -EINVAL;
  12549. }
  12550. break;
  12551. case DRM_FORMAT_ABGR8888:
  12552. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  12553. INTEL_INFO(dev)->gen < 9) {
  12554. DRM_DEBUG("unsupported pixel format: %s\n",
  12555. drm_get_format_name(mode_cmd->pixel_format));
  12556. return -EINVAL;
  12557. }
  12558. break;
  12559. case DRM_FORMAT_XBGR8888:
  12560. case DRM_FORMAT_XRGB2101010:
  12561. case DRM_FORMAT_XBGR2101010:
  12562. if (INTEL_INFO(dev)->gen < 4) {
  12563. DRM_DEBUG("unsupported pixel format: %s\n",
  12564. drm_get_format_name(mode_cmd->pixel_format));
  12565. return -EINVAL;
  12566. }
  12567. break;
  12568. case DRM_FORMAT_ABGR2101010:
  12569. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12570. DRM_DEBUG("unsupported pixel format: %s\n",
  12571. drm_get_format_name(mode_cmd->pixel_format));
  12572. return -EINVAL;
  12573. }
  12574. break;
  12575. case DRM_FORMAT_YUYV:
  12576. case DRM_FORMAT_UYVY:
  12577. case DRM_FORMAT_YVYU:
  12578. case DRM_FORMAT_VYUY:
  12579. if (INTEL_INFO(dev)->gen < 5) {
  12580. DRM_DEBUG("unsupported pixel format: %s\n",
  12581. drm_get_format_name(mode_cmd->pixel_format));
  12582. return -EINVAL;
  12583. }
  12584. break;
  12585. default:
  12586. DRM_DEBUG("unsupported pixel format: %s\n",
  12587. drm_get_format_name(mode_cmd->pixel_format));
  12588. return -EINVAL;
  12589. }
  12590. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12591. if (mode_cmd->offsets[0] != 0)
  12592. return -EINVAL;
  12593. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12594. mode_cmd->pixel_format,
  12595. mode_cmd->modifier[0]);
  12596. /* FIXME drm helper for size checks (especially planar formats)? */
  12597. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12598. return -EINVAL;
  12599. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12600. intel_fb->obj = obj;
  12601. intel_fill_fb_info(dev_priv, &intel_fb->base);
  12602. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12603. if (ret) {
  12604. DRM_ERROR("framebuffer init failed %d\n", ret);
  12605. return ret;
  12606. }
  12607. intel_fb->obj->framebuffer_references++;
  12608. return 0;
  12609. }
  12610. static struct drm_framebuffer *
  12611. intel_user_framebuffer_create(struct drm_device *dev,
  12612. struct drm_file *filp,
  12613. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12614. {
  12615. struct drm_framebuffer *fb;
  12616. struct drm_i915_gem_object *obj;
  12617. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12618. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  12619. if (!obj)
  12620. return ERR_PTR(-ENOENT);
  12621. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  12622. if (IS_ERR(fb))
  12623. drm_gem_object_unreference_unlocked(&obj->base);
  12624. return fb;
  12625. }
  12626. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12627. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12628. {
  12629. }
  12630. #endif
  12631. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12632. .fb_create = intel_user_framebuffer_create,
  12633. .output_poll_changed = intel_fbdev_output_poll_changed,
  12634. .atomic_check = intel_atomic_check,
  12635. .atomic_commit = intel_atomic_commit,
  12636. .atomic_state_alloc = intel_atomic_state_alloc,
  12637. .atomic_state_clear = intel_atomic_state_clear,
  12638. };
  12639. /**
  12640. * intel_init_display_hooks - initialize the display modesetting hooks
  12641. * @dev_priv: device private
  12642. */
  12643. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12644. {
  12645. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12646. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12647. dev_priv->display.get_initial_plane_config =
  12648. skylake_get_initial_plane_config;
  12649. dev_priv->display.crtc_compute_clock =
  12650. haswell_crtc_compute_clock;
  12651. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12652. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12653. } else if (HAS_DDI(dev_priv)) {
  12654. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12655. dev_priv->display.get_initial_plane_config =
  12656. ironlake_get_initial_plane_config;
  12657. dev_priv->display.crtc_compute_clock =
  12658. haswell_crtc_compute_clock;
  12659. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12660. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12661. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12662. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12663. dev_priv->display.get_initial_plane_config =
  12664. ironlake_get_initial_plane_config;
  12665. dev_priv->display.crtc_compute_clock =
  12666. ironlake_crtc_compute_clock;
  12667. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12668. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12669. } else if (IS_CHERRYVIEW(dev_priv)) {
  12670. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12671. dev_priv->display.get_initial_plane_config =
  12672. i9xx_get_initial_plane_config;
  12673. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12674. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12675. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12676. } else if (IS_VALLEYVIEW(dev_priv)) {
  12677. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12678. dev_priv->display.get_initial_plane_config =
  12679. i9xx_get_initial_plane_config;
  12680. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12681. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12682. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12683. } else if (IS_G4X(dev_priv)) {
  12684. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12685. dev_priv->display.get_initial_plane_config =
  12686. i9xx_get_initial_plane_config;
  12687. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12688. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12689. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12690. } else if (IS_PINEVIEW(dev_priv)) {
  12691. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12692. dev_priv->display.get_initial_plane_config =
  12693. i9xx_get_initial_plane_config;
  12694. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12695. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12696. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12697. } else if (!IS_GEN2(dev_priv)) {
  12698. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12699. dev_priv->display.get_initial_plane_config =
  12700. i9xx_get_initial_plane_config;
  12701. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12702. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12703. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12704. } else {
  12705. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12706. dev_priv->display.get_initial_plane_config =
  12707. i9xx_get_initial_plane_config;
  12708. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12709. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12710. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12711. }
  12712. /* Returns the core display clock speed */
  12713. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12714. dev_priv->display.get_display_clock_speed =
  12715. skylake_get_display_clock_speed;
  12716. else if (IS_BROXTON(dev_priv))
  12717. dev_priv->display.get_display_clock_speed =
  12718. broxton_get_display_clock_speed;
  12719. else if (IS_BROADWELL(dev_priv))
  12720. dev_priv->display.get_display_clock_speed =
  12721. broadwell_get_display_clock_speed;
  12722. else if (IS_HASWELL(dev_priv))
  12723. dev_priv->display.get_display_clock_speed =
  12724. haswell_get_display_clock_speed;
  12725. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12726. dev_priv->display.get_display_clock_speed =
  12727. valleyview_get_display_clock_speed;
  12728. else if (IS_GEN5(dev_priv))
  12729. dev_priv->display.get_display_clock_speed =
  12730. ilk_get_display_clock_speed;
  12731. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  12732. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  12733. dev_priv->display.get_display_clock_speed =
  12734. i945_get_display_clock_speed;
  12735. else if (IS_GM45(dev_priv))
  12736. dev_priv->display.get_display_clock_speed =
  12737. gm45_get_display_clock_speed;
  12738. else if (IS_CRESTLINE(dev_priv))
  12739. dev_priv->display.get_display_clock_speed =
  12740. i965gm_get_display_clock_speed;
  12741. else if (IS_PINEVIEW(dev_priv))
  12742. dev_priv->display.get_display_clock_speed =
  12743. pnv_get_display_clock_speed;
  12744. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  12745. dev_priv->display.get_display_clock_speed =
  12746. g33_get_display_clock_speed;
  12747. else if (IS_I915G(dev_priv))
  12748. dev_priv->display.get_display_clock_speed =
  12749. i915_get_display_clock_speed;
  12750. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  12751. dev_priv->display.get_display_clock_speed =
  12752. i9xx_misc_get_display_clock_speed;
  12753. else if (IS_I915GM(dev_priv))
  12754. dev_priv->display.get_display_clock_speed =
  12755. i915gm_get_display_clock_speed;
  12756. else if (IS_I865G(dev_priv))
  12757. dev_priv->display.get_display_clock_speed =
  12758. i865_get_display_clock_speed;
  12759. else if (IS_I85X(dev_priv))
  12760. dev_priv->display.get_display_clock_speed =
  12761. i85x_get_display_clock_speed;
  12762. else { /* 830 */
  12763. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12764. dev_priv->display.get_display_clock_speed =
  12765. i830_get_display_clock_speed;
  12766. }
  12767. if (IS_GEN5(dev_priv)) {
  12768. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12769. } else if (IS_GEN6(dev_priv)) {
  12770. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12771. } else if (IS_IVYBRIDGE(dev_priv)) {
  12772. /* FIXME: detect B0+ stepping and use auto training */
  12773. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12774. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12775. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12776. }
  12777. if (IS_BROADWELL(dev_priv)) {
  12778. dev_priv->display.modeset_commit_cdclk =
  12779. broadwell_modeset_commit_cdclk;
  12780. dev_priv->display.modeset_calc_cdclk =
  12781. broadwell_modeset_calc_cdclk;
  12782. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12783. dev_priv->display.modeset_commit_cdclk =
  12784. valleyview_modeset_commit_cdclk;
  12785. dev_priv->display.modeset_calc_cdclk =
  12786. valleyview_modeset_calc_cdclk;
  12787. } else if (IS_BROXTON(dev_priv)) {
  12788. dev_priv->display.modeset_commit_cdclk =
  12789. bxt_modeset_commit_cdclk;
  12790. dev_priv->display.modeset_calc_cdclk =
  12791. bxt_modeset_calc_cdclk;
  12792. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  12793. dev_priv->display.modeset_commit_cdclk =
  12794. skl_modeset_commit_cdclk;
  12795. dev_priv->display.modeset_calc_cdclk =
  12796. skl_modeset_calc_cdclk;
  12797. }
  12798. switch (INTEL_INFO(dev_priv)->gen) {
  12799. case 2:
  12800. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12801. break;
  12802. case 3:
  12803. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12804. break;
  12805. case 4:
  12806. case 5:
  12807. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12808. break;
  12809. case 6:
  12810. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12811. break;
  12812. case 7:
  12813. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12814. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12815. break;
  12816. case 9:
  12817. /* Drop through - unsupported since execlist only. */
  12818. default:
  12819. /* Default just returns -ENODEV to indicate unsupported */
  12820. dev_priv->display.queue_flip = intel_default_queue_flip;
  12821. }
  12822. }
  12823. /*
  12824. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12825. * resume, or other times. This quirk makes sure that's the case for
  12826. * affected systems.
  12827. */
  12828. static void quirk_pipea_force(struct drm_device *dev)
  12829. {
  12830. struct drm_i915_private *dev_priv = to_i915(dev);
  12831. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12832. DRM_INFO("applying pipe a force quirk\n");
  12833. }
  12834. static void quirk_pipeb_force(struct drm_device *dev)
  12835. {
  12836. struct drm_i915_private *dev_priv = to_i915(dev);
  12837. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12838. DRM_INFO("applying pipe b force quirk\n");
  12839. }
  12840. /*
  12841. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12842. */
  12843. static void quirk_ssc_force_disable(struct drm_device *dev)
  12844. {
  12845. struct drm_i915_private *dev_priv = to_i915(dev);
  12846. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12847. DRM_INFO("applying lvds SSC disable quirk\n");
  12848. }
  12849. /*
  12850. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12851. * brightness value
  12852. */
  12853. static void quirk_invert_brightness(struct drm_device *dev)
  12854. {
  12855. struct drm_i915_private *dev_priv = to_i915(dev);
  12856. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12857. DRM_INFO("applying inverted panel brightness quirk\n");
  12858. }
  12859. /* Some VBT's incorrectly indicate no backlight is present */
  12860. static void quirk_backlight_present(struct drm_device *dev)
  12861. {
  12862. struct drm_i915_private *dev_priv = to_i915(dev);
  12863. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12864. DRM_INFO("applying backlight present quirk\n");
  12865. }
  12866. struct intel_quirk {
  12867. int device;
  12868. int subsystem_vendor;
  12869. int subsystem_device;
  12870. void (*hook)(struct drm_device *dev);
  12871. };
  12872. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12873. struct intel_dmi_quirk {
  12874. void (*hook)(struct drm_device *dev);
  12875. const struct dmi_system_id (*dmi_id_list)[];
  12876. };
  12877. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12878. {
  12879. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12880. return 1;
  12881. }
  12882. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12883. {
  12884. .dmi_id_list = &(const struct dmi_system_id[]) {
  12885. {
  12886. .callback = intel_dmi_reverse_brightness,
  12887. .ident = "NCR Corporation",
  12888. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12889. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12890. },
  12891. },
  12892. { } /* terminating entry */
  12893. },
  12894. .hook = quirk_invert_brightness,
  12895. },
  12896. };
  12897. static struct intel_quirk intel_quirks[] = {
  12898. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12899. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12900. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12901. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12902. /* 830 needs to leave pipe A & dpll A up */
  12903. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12904. /* 830 needs to leave pipe B & dpll B up */
  12905. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12906. /* Lenovo U160 cannot use SSC on LVDS */
  12907. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12908. /* Sony Vaio Y cannot use SSC on LVDS */
  12909. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12910. /* Acer Aspire 5734Z must invert backlight brightness */
  12911. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12912. /* Acer/eMachines G725 */
  12913. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12914. /* Acer/eMachines e725 */
  12915. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12916. /* Acer/Packard Bell NCL20 */
  12917. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12918. /* Acer Aspire 4736Z */
  12919. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12920. /* Acer Aspire 5336 */
  12921. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12922. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12923. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12924. /* Acer C720 Chromebook (Core i3 4005U) */
  12925. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12926. /* Apple Macbook 2,1 (Core 2 T7400) */
  12927. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12928. /* Apple Macbook 4,1 */
  12929. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12930. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12931. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12932. /* HP Chromebook 14 (Celeron 2955U) */
  12933. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12934. /* Dell Chromebook 11 */
  12935. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12936. /* Dell Chromebook 11 (2015 version) */
  12937. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12938. };
  12939. static void intel_init_quirks(struct drm_device *dev)
  12940. {
  12941. struct pci_dev *d = dev->pdev;
  12942. int i;
  12943. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12944. struct intel_quirk *q = &intel_quirks[i];
  12945. if (d->device == q->device &&
  12946. (d->subsystem_vendor == q->subsystem_vendor ||
  12947. q->subsystem_vendor == PCI_ANY_ID) &&
  12948. (d->subsystem_device == q->subsystem_device ||
  12949. q->subsystem_device == PCI_ANY_ID))
  12950. q->hook(dev);
  12951. }
  12952. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12953. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12954. intel_dmi_quirks[i].hook(dev);
  12955. }
  12956. }
  12957. /* Disable the VGA plane that we never use */
  12958. static void i915_disable_vga(struct drm_device *dev)
  12959. {
  12960. struct drm_i915_private *dev_priv = to_i915(dev);
  12961. u8 sr1;
  12962. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  12963. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12964. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12965. outb(SR01, VGA_SR_INDEX);
  12966. sr1 = inb(VGA_SR_DATA);
  12967. outb(sr1 | 1<<5, VGA_SR_DATA);
  12968. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12969. udelay(300);
  12970. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12971. POSTING_READ(vga_reg);
  12972. }
  12973. void intel_modeset_init_hw(struct drm_device *dev)
  12974. {
  12975. struct drm_i915_private *dev_priv = to_i915(dev);
  12976. intel_update_cdclk(dev);
  12977. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  12978. intel_init_clock_gating(dev);
  12979. }
  12980. /*
  12981. * Calculate what we think the watermarks should be for the state we've read
  12982. * out of the hardware and then immediately program those watermarks so that
  12983. * we ensure the hardware settings match our internal state.
  12984. *
  12985. * We can calculate what we think WM's should be by creating a duplicate of the
  12986. * current state (which was constructed during hardware readout) and running it
  12987. * through the atomic check code to calculate new watermark values in the
  12988. * state object.
  12989. */
  12990. static void sanitize_watermarks(struct drm_device *dev)
  12991. {
  12992. struct drm_i915_private *dev_priv = to_i915(dev);
  12993. struct drm_atomic_state *state;
  12994. struct drm_crtc *crtc;
  12995. struct drm_crtc_state *cstate;
  12996. struct drm_modeset_acquire_ctx ctx;
  12997. int ret;
  12998. int i;
  12999. /* Only supported on platforms that use atomic watermark design */
  13000. if (!dev_priv->display.optimize_watermarks)
  13001. return;
  13002. /*
  13003. * We need to hold connection_mutex before calling duplicate_state so
  13004. * that the connector loop is protected.
  13005. */
  13006. drm_modeset_acquire_init(&ctx, 0);
  13007. retry:
  13008. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13009. if (ret == -EDEADLK) {
  13010. drm_modeset_backoff(&ctx);
  13011. goto retry;
  13012. } else if (WARN_ON(ret)) {
  13013. goto fail;
  13014. }
  13015. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13016. if (WARN_ON(IS_ERR(state)))
  13017. goto fail;
  13018. /*
  13019. * Hardware readout is the only time we don't want to calculate
  13020. * intermediate watermarks (since we don't trust the current
  13021. * watermarks).
  13022. */
  13023. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13024. ret = intel_atomic_check(dev, state);
  13025. if (ret) {
  13026. /*
  13027. * If we fail here, it means that the hardware appears to be
  13028. * programmed in a way that shouldn't be possible, given our
  13029. * understanding of watermark requirements. This might mean a
  13030. * mistake in the hardware readout code or a mistake in the
  13031. * watermark calculations for a given platform. Raise a WARN
  13032. * so that this is noticeable.
  13033. *
  13034. * If this actually happens, we'll have to just leave the
  13035. * BIOS-programmed watermarks untouched and hope for the best.
  13036. */
  13037. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13038. goto fail;
  13039. }
  13040. /* Write calculated watermark values back */
  13041. for_each_crtc_in_state(state, crtc, cstate, i) {
  13042. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13043. cs->wm.need_postvbl_update = true;
  13044. dev_priv->display.optimize_watermarks(cs);
  13045. }
  13046. drm_atomic_state_free(state);
  13047. fail:
  13048. drm_modeset_drop_locks(&ctx);
  13049. drm_modeset_acquire_fini(&ctx);
  13050. }
  13051. void intel_modeset_init(struct drm_device *dev)
  13052. {
  13053. struct drm_i915_private *dev_priv = to_i915(dev);
  13054. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13055. int sprite, ret;
  13056. enum pipe pipe;
  13057. struct intel_crtc *crtc;
  13058. drm_mode_config_init(dev);
  13059. dev->mode_config.min_width = 0;
  13060. dev->mode_config.min_height = 0;
  13061. dev->mode_config.preferred_depth = 24;
  13062. dev->mode_config.prefer_shadow = 1;
  13063. dev->mode_config.allow_fb_modifiers = true;
  13064. dev->mode_config.funcs = &intel_mode_funcs;
  13065. intel_init_quirks(dev);
  13066. intel_init_pm(dev);
  13067. if (INTEL_INFO(dev)->num_pipes == 0)
  13068. return;
  13069. /*
  13070. * There may be no VBT; and if the BIOS enabled SSC we can
  13071. * just keep using it to avoid unnecessary flicker. Whereas if the
  13072. * BIOS isn't using it, don't assume it will work even if the VBT
  13073. * indicates as much.
  13074. */
  13075. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  13076. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13077. DREF_SSC1_ENABLE);
  13078. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13079. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13080. bios_lvds_use_ssc ? "en" : "dis",
  13081. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13082. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13083. }
  13084. }
  13085. if (IS_GEN2(dev)) {
  13086. dev->mode_config.max_width = 2048;
  13087. dev->mode_config.max_height = 2048;
  13088. } else if (IS_GEN3(dev)) {
  13089. dev->mode_config.max_width = 4096;
  13090. dev->mode_config.max_height = 4096;
  13091. } else {
  13092. dev->mode_config.max_width = 8192;
  13093. dev->mode_config.max_height = 8192;
  13094. }
  13095. if (IS_845G(dev) || IS_I865G(dev)) {
  13096. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  13097. dev->mode_config.cursor_height = 1023;
  13098. } else if (IS_GEN2(dev)) {
  13099. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13100. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13101. } else {
  13102. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13103. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13104. }
  13105. dev->mode_config.fb_base = ggtt->mappable_base;
  13106. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13107. INTEL_INFO(dev)->num_pipes,
  13108. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  13109. for_each_pipe(dev_priv, pipe) {
  13110. intel_crtc_init(dev, pipe);
  13111. for_each_sprite(dev_priv, pipe, sprite) {
  13112. ret = intel_plane_init(dev, pipe, sprite);
  13113. if (ret)
  13114. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  13115. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  13116. }
  13117. }
  13118. intel_update_czclk(dev_priv);
  13119. intel_update_cdclk(dev);
  13120. intel_shared_dpll_init(dev);
  13121. if (dev_priv->max_cdclk_freq == 0)
  13122. intel_update_max_cdclk(dev);
  13123. /* Just disable it once at startup */
  13124. i915_disable_vga(dev);
  13125. intel_setup_outputs(dev);
  13126. drm_modeset_lock_all(dev);
  13127. intel_modeset_setup_hw_state(dev);
  13128. drm_modeset_unlock_all(dev);
  13129. for_each_intel_crtc(dev, crtc) {
  13130. struct intel_initial_plane_config plane_config = {};
  13131. if (!crtc->active)
  13132. continue;
  13133. /*
  13134. * Note that reserving the BIOS fb up front prevents us
  13135. * from stuffing other stolen allocations like the ring
  13136. * on top. This prevents some ugliness at boot time, and
  13137. * can even allow for smooth boot transitions if the BIOS
  13138. * fb is large enough for the active pipe configuration.
  13139. */
  13140. dev_priv->display.get_initial_plane_config(crtc,
  13141. &plane_config);
  13142. /*
  13143. * If the fb is shared between multiple heads, we'll
  13144. * just get the first one.
  13145. */
  13146. intel_find_initial_plane_obj(crtc, &plane_config);
  13147. }
  13148. /*
  13149. * Make sure hardware watermarks really match the state we read out.
  13150. * Note that we need to do this after reconstructing the BIOS fb's
  13151. * since the watermark calculation done here will use pstate->fb.
  13152. */
  13153. sanitize_watermarks(dev);
  13154. }
  13155. static void intel_enable_pipe_a(struct drm_device *dev)
  13156. {
  13157. struct intel_connector *connector;
  13158. struct drm_connector *crt = NULL;
  13159. struct intel_load_detect_pipe load_detect_temp;
  13160. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13161. /* We can't just switch on the pipe A, we need to set things up with a
  13162. * proper mode and output configuration. As a gross hack, enable pipe A
  13163. * by enabling the load detect pipe once. */
  13164. for_each_intel_connector(dev, connector) {
  13165. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  13166. crt = &connector->base;
  13167. break;
  13168. }
  13169. }
  13170. if (!crt)
  13171. return;
  13172. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  13173. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  13174. }
  13175. static bool
  13176. intel_check_plane_mapping(struct intel_crtc *crtc)
  13177. {
  13178. struct drm_device *dev = crtc->base.dev;
  13179. struct drm_i915_private *dev_priv = to_i915(dev);
  13180. u32 val;
  13181. if (INTEL_INFO(dev)->num_pipes == 1)
  13182. return true;
  13183. val = I915_READ(DSPCNTR(!crtc->plane));
  13184. if ((val & DISPLAY_PLANE_ENABLE) &&
  13185. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13186. return false;
  13187. return true;
  13188. }
  13189. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13190. {
  13191. struct drm_device *dev = crtc->base.dev;
  13192. struct intel_encoder *encoder;
  13193. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13194. return true;
  13195. return false;
  13196. }
  13197. static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
  13198. {
  13199. struct drm_device *dev = encoder->base.dev;
  13200. struct intel_connector *connector;
  13201. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13202. return true;
  13203. return false;
  13204. }
  13205. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13206. {
  13207. struct drm_device *dev = crtc->base.dev;
  13208. struct drm_i915_private *dev_priv = to_i915(dev);
  13209. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13210. /* Clear any frame start delays used for debugging left by the BIOS */
  13211. if (!transcoder_is_dsi(cpu_transcoder)) {
  13212. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13213. I915_WRITE(reg,
  13214. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13215. }
  13216. /* restore vblank interrupts to correct state */
  13217. drm_crtc_vblank_reset(&crtc->base);
  13218. if (crtc->active) {
  13219. struct intel_plane *plane;
  13220. drm_crtc_vblank_on(&crtc->base);
  13221. /* Disable everything but the primary plane */
  13222. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13223. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13224. continue;
  13225. plane->disable_plane(&plane->base, &crtc->base);
  13226. }
  13227. }
  13228. /* We need to sanitize the plane -> pipe mapping first because this will
  13229. * disable the crtc (and hence change the state) if it is wrong. Note
  13230. * that gen4+ has a fixed plane -> pipe mapping. */
  13231. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  13232. bool plane;
  13233. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  13234. crtc->base.base.id, crtc->base.name);
  13235. /* Pipe has the wrong plane attached and the plane is active.
  13236. * Temporarily change the plane mapping and disable everything
  13237. * ... */
  13238. plane = crtc->plane;
  13239. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  13240. crtc->plane = !plane;
  13241. intel_crtc_disable_noatomic(&crtc->base);
  13242. crtc->plane = plane;
  13243. }
  13244. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13245. crtc->pipe == PIPE_A && !crtc->active) {
  13246. /* BIOS forgot to enable pipe A, this mostly happens after
  13247. * resume. Force-enable the pipe to fix this, the update_dpms
  13248. * call below we restore the pipe to the right state, but leave
  13249. * the required bits on. */
  13250. intel_enable_pipe_a(dev);
  13251. }
  13252. /* Adjust the state of the output pipe according to whether we
  13253. * have active connectors/encoders. */
  13254. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13255. intel_crtc_disable_noatomic(&crtc->base);
  13256. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  13257. /*
  13258. * We start out with underrun reporting disabled to avoid races.
  13259. * For correct bookkeeping mark this on active crtcs.
  13260. *
  13261. * Also on gmch platforms we dont have any hardware bits to
  13262. * disable the underrun reporting. Which means we need to start
  13263. * out with underrun reporting disabled also on inactive pipes,
  13264. * since otherwise we'll complain about the garbage we read when
  13265. * e.g. coming up after runtime pm.
  13266. *
  13267. * No protection against concurrent access is required - at
  13268. * worst a fifo underrun happens which also sets this to false.
  13269. */
  13270. crtc->cpu_fifo_underrun_disabled = true;
  13271. crtc->pch_fifo_underrun_disabled = true;
  13272. }
  13273. }
  13274. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13275. {
  13276. struct intel_connector *connector;
  13277. struct drm_device *dev = encoder->base.dev;
  13278. /* We need to check both for a crtc link (meaning that the
  13279. * encoder is active and trying to read from a pipe) and the
  13280. * pipe itself being active. */
  13281. bool has_active_crtc = encoder->base.crtc &&
  13282. to_intel_crtc(encoder->base.crtc)->active;
  13283. if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
  13284. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13285. encoder->base.base.id,
  13286. encoder->base.name);
  13287. /* Connector is active, but has no active pipe. This is
  13288. * fallout from our resume register restoring. Disable
  13289. * the encoder manually again. */
  13290. if (encoder->base.crtc) {
  13291. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13292. encoder->base.base.id,
  13293. encoder->base.name);
  13294. encoder->disable(encoder);
  13295. if (encoder->post_disable)
  13296. encoder->post_disable(encoder);
  13297. }
  13298. encoder->base.crtc = NULL;
  13299. /* Inconsistent output/port/pipe state happens presumably due to
  13300. * a bug in one of the get_hw_state functions. Or someplace else
  13301. * in our code, like the register restore mess on resume. Clamp
  13302. * things to off as a safer default. */
  13303. for_each_intel_connector(dev, connector) {
  13304. if (connector->encoder != encoder)
  13305. continue;
  13306. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13307. connector->base.encoder = NULL;
  13308. }
  13309. }
  13310. /* Enabled encoders without active connectors will be fixed in
  13311. * the crtc fixup. */
  13312. }
  13313. void i915_redisable_vga_power_on(struct drm_device *dev)
  13314. {
  13315. struct drm_i915_private *dev_priv = to_i915(dev);
  13316. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13317. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13318. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13319. i915_disable_vga(dev);
  13320. }
  13321. }
  13322. void i915_redisable_vga(struct drm_device *dev)
  13323. {
  13324. struct drm_i915_private *dev_priv = to_i915(dev);
  13325. /* This function can be called both from intel_modeset_setup_hw_state or
  13326. * at a very early point in our resume sequence, where the power well
  13327. * structures are not yet restored. Since this function is at a very
  13328. * paranoid "someone might have enabled VGA while we were not looking"
  13329. * level, just check if the power well is enabled instead of trying to
  13330. * follow the "don't touch the power well if we don't need it" policy
  13331. * the rest of the driver uses. */
  13332. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  13333. return;
  13334. i915_redisable_vga_power_on(dev);
  13335. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  13336. }
  13337. static bool primary_get_hw_state(struct intel_plane *plane)
  13338. {
  13339. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  13340. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  13341. }
  13342. /* FIXME read out full plane state for all planes */
  13343. static void readout_plane_state(struct intel_crtc *crtc)
  13344. {
  13345. struct drm_plane *primary = crtc->base.primary;
  13346. struct intel_plane_state *plane_state =
  13347. to_intel_plane_state(primary->state);
  13348. plane_state->visible = crtc->active &&
  13349. primary_get_hw_state(to_intel_plane(primary));
  13350. if (plane_state->visible)
  13351. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  13352. }
  13353. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13354. {
  13355. struct drm_i915_private *dev_priv = to_i915(dev);
  13356. enum pipe pipe;
  13357. struct intel_crtc *crtc;
  13358. struct intel_encoder *encoder;
  13359. struct intel_connector *connector;
  13360. int i;
  13361. dev_priv->active_crtcs = 0;
  13362. for_each_intel_crtc(dev, crtc) {
  13363. struct intel_crtc_state *crtc_state = crtc->config;
  13364. int pixclk = 0;
  13365. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  13366. memset(crtc_state, 0, sizeof(*crtc_state));
  13367. crtc_state->base.crtc = &crtc->base;
  13368. crtc_state->base.active = crtc_state->base.enable =
  13369. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13370. crtc->base.enabled = crtc_state->base.enable;
  13371. crtc->active = crtc_state->base.active;
  13372. if (crtc_state->base.active) {
  13373. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13374. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  13375. pixclk = ilk_pipe_pixel_rate(crtc_state);
  13376. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13377. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  13378. else
  13379. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  13380. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  13381. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  13382. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  13383. }
  13384. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  13385. readout_plane_state(crtc);
  13386. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  13387. crtc->base.base.id, crtc->base.name,
  13388. crtc->active ? "enabled" : "disabled");
  13389. }
  13390. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13391. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13392. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  13393. &pll->config.hw_state);
  13394. pll->config.crtc_mask = 0;
  13395. for_each_intel_crtc(dev, crtc) {
  13396. if (crtc->active && crtc->config->shared_dpll == pll)
  13397. pll->config.crtc_mask |= 1 << crtc->pipe;
  13398. }
  13399. pll->active_mask = pll->config.crtc_mask;
  13400. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13401. pll->name, pll->config.crtc_mask, pll->on);
  13402. }
  13403. for_each_intel_encoder(dev, encoder) {
  13404. pipe = 0;
  13405. if (encoder->get_hw_state(encoder, &pipe)) {
  13406. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13407. encoder->base.crtc = &crtc->base;
  13408. crtc->config->output_types |= 1 << encoder->type;
  13409. encoder->get_config(encoder, crtc->config);
  13410. } else {
  13411. encoder->base.crtc = NULL;
  13412. }
  13413. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13414. encoder->base.base.id,
  13415. encoder->base.name,
  13416. encoder->base.crtc ? "enabled" : "disabled",
  13417. pipe_name(pipe));
  13418. }
  13419. for_each_intel_connector(dev, connector) {
  13420. if (connector->get_hw_state(connector)) {
  13421. connector->base.dpms = DRM_MODE_DPMS_ON;
  13422. encoder = connector->encoder;
  13423. connector->base.encoder = &encoder->base;
  13424. if (encoder->base.crtc &&
  13425. encoder->base.crtc->state->active) {
  13426. /*
  13427. * This has to be done during hardware readout
  13428. * because anything calling .crtc_disable may
  13429. * rely on the connector_mask being accurate.
  13430. */
  13431. encoder->base.crtc->state->connector_mask |=
  13432. 1 << drm_connector_index(&connector->base);
  13433. encoder->base.crtc->state->encoder_mask |=
  13434. 1 << drm_encoder_index(&encoder->base);
  13435. }
  13436. } else {
  13437. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13438. connector->base.encoder = NULL;
  13439. }
  13440. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13441. connector->base.base.id,
  13442. connector->base.name,
  13443. connector->base.encoder ? "enabled" : "disabled");
  13444. }
  13445. for_each_intel_crtc(dev, crtc) {
  13446. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  13447. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13448. if (crtc->base.state->active) {
  13449. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  13450. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  13451. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13452. /*
  13453. * The initial mode needs to be set in order to keep
  13454. * the atomic core happy. It wants a valid mode if the
  13455. * crtc's enabled, so we do the above call.
  13456. *
  13457. * At this point some state updated by the connectors
  13458. * in their ->detect() callback has not run yet, so
  13459. * no recalculation can be done yet.
  13460. *
  13461. * Even if we could do a recalculation and modeset
  13462. * right now it would cause a double modeset if
  13463. * fbdev or userspace chooses a different initial mode.
  13464. *
  13465. * If that happens, someone indicated they wanted a
  13466. * mode change, which means it's safe to do a full
  13467. * recalculation.
  13468. */
  13469. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  13470. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  13471. update_scanline_offset(crtc);
  13472. }
  13473. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  13474. }
  13475. }
  13476. /* Scan out the current hw modeset state,
  13477. * and sanitizes it to the current state
  13478. */
  13479. static void
  13480. intel_modeset_setup_hw_state(struct drm_device *dev)
  13481. {
  13482. struct drm_i915_private *dev_priv = to_i915(dev);
  13483. enum pipe pipe;
  13484. struct intel_crtc *crtc;
  13485. struct intel_encoder *encoder;
  13486. int i;
  13487. intel_modeset_readout_hw_state(dev);
  13488. /* HW state is read out, now we need to sanitize this mess. */
  13489. for_each_intel_encoder(dev, encoder) {
  13490. intel_sanitize_encoder(encoder);
  13491. }
  13492. for_each_pipe(dev_priv, pipe) {
  13493. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13494. intel_sanitize_crtc(crtc);
  13495. intel_dump_pipe_config(crtc, crtc->config,
  13496. "[setup_hw_state]");
  13497. }
  13498. intel_modeset_update_connector_atomic_state(dev);
  13499. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13500. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13501. if (!pll->on || pll->active_mask)
  13502. continue;
  13503. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13504. pll->funcs.disable(dev_priv, pll);
  13505. pll->on = false;
  13506. }
  13507. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  13508. vlv_wm_get_hw_state(dev);
  13509. else if (IS_GEN9(dev))
  13510. skl_wm_get_hw_state(dev);
  13511. else if (HAS_PCH_SPLIT(dev))
  13512. ilk_wm_get_hw_state(dev);
  13513. for_each_intel_crtc(dev, crtc) {
  13514. unsigned long put_domains;
  13515. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13516. if (WARN_ON(put_domains))
  13517. modeset_put_power_domains(dev_priv, put_domains);
  13518. }
  13519. intel_display_set_init_power(dev_priv, false);
  13520. intel_fbc_init_pipe_state(dev_priv);
  13521. }
  13522. void intel_display_resume(struct drm_device *dev)
  13523. {
  13524. struct drm_i915_private *dev_priv = to_i915(dev);
  13525. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13526. struct drm_modeset_acquire_ctx ctx;
  13527. int ret;
  13528. bool setup = false;
  13529. dev_priv->modeset_restore_state = NULL;
  13530. /*
  13531. * This is a cludge because with real atomic modeset mode_config.mutex
  13532. * won't be taken. Unfortunately some probed state like
  13533. * audio_codec_enable is still protected by mode_config.mutex, so lock
  13534. * it here for now.
  13535. */
  13536. mutex_lock(&dev->mode_config.mutex);
  13537. drm_modeset_acquire_init(&ctx, 0);
  13538. retry:
  13539. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13540. if (ret == 0 && !setup) {
  13541. setup = true;
  13542. intel_modeset_setup_hw_state(dev);
  13543. i915_redisable_vga(dev);
  13544. }
  13545. if (ret == 0 && state) {
  13546. struct drm_crtc_state *crtc_state;
  13547. struct drm_crtc *crtc;
  13548. int i;
  13549. state->acquire_ctx = &ctx;
  13550. /* ignore any reset values/BIOS leftovers in the WM registers */
  13551. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13552. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  13553. /*
  13554. * Force recalculation even if we restore
  13555. * current state. With fast modeset this may not result
  13556. * in a modeset when the state is compatible.
  13557. */
  13558. crtc_state->mode_changed = true;
  13559. }
  13560. ret = drm_atomic_commit(state);
  13561. }
  13562. if (ret == -EDEADLK) {
  13563. drm_modeset_backoff(&ctx);
  13564. goto retry;
  13565. }
  13566. drm_modeset_drop_locks(&ctx);
  13567. drm_modeset_acquire_fini(&ctx);
  13568. mutex_unlock(&dev->mode_config.mutex);
  13569. if (ret) {
  13570. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13571. drm_atomic_state_free(state);
  13572. }
  13573. }
  13574. void intel_modeset_gem_init(struct drm_device *dev)
  13575. {
  13576. struct drm_i915_private *dev_priv = to_i915(dev);
  13577. struct drm_crtc *c;
  13578. struct drm_i915_gem_object *obj;
  13579. int ret;
  13580. intel_init_gt_powersave(dev_priv);
  13581. intel_modeset_init_hw(dev);
  13582. intel_setup_overlay(dev_priv);
  13583. /*
  13584. * Make sure any fbs we allocated at startup are properly
  13585. * pinned & fenced. When we do the allocation it's too early
  13586. * for this.
  13587. */
  13588. for_each_crtc(dev, c) {
  13589. obj = intel_fb_obj(c->primary->fb);
  13590. if (obj == NULL)
  13591. continue;
  13592. mutex_lock(&dev->struct_mutex);
  13593. ret = intel_pin_and_fence_fb_obj(c->primary->fb,
  13594. c->primary->state->rotation);
  13595. mutex_unlock(&dev->struct_mutex);
  13596. if (ret) {
  13597. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13598. to_intel_crtc(c)->pipe);
  13599. drm_framebuffer_unreference(c->primary->fb);
  13600. c->primary->fb = NULL;
  13601. c->primary->crtc = c->primary->state->crtc = NULL;
  13602. update_state_fb(c->primary);
  13603. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13604. }
  13605. }
  13606. }
  13607. int intel_connector_register(struct drm_connector *connector)
  13608. {
  13609. struct intel_connector *intel_connector = to_intel_connector(connector);
  13610. int ret;
  13611. ret = intel_backlight_device_register(intel_connector);
  13612. if (ret)
  13613. goto err;
  13614. return 0;
  13615. err:
  13616. return ret;
  13617. }
  13618. void intel_connector_unregister(struct drm_connector *connector)
  13619. {
  13620. struct intel_connector *intel_connector = to_intel_connector(connector);
  13621. intel_backlight_device_unregister(intel_connector);
  13622. intel_panel_destroy_backlight(connector);
  13623. }
  13624. void intel_modeset_cleanup(struct drm_device *dev)
  13625. {
  13626. struct drm_i915_private *dev_priv = to_i915(dev);
  13627. intel_disable_gt_powersave(dev_priv);
  13628. /*
  13629. * Interrupts and polling as the first thing to avoid creating havoc.
  13630. * Too much stuff here (turning of connectors, ...) would
  13631. * experience fancy races otherwise.
  13632. */
  13633. intel_irq_uninstall(dev_priv);
  13634. /*
  13635. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13636. * poll handlers. Hence disable polling after hpd handling is shut down.
  13637. */
  13638. drm_kms_helper_poll_fini(dev);
  13639. intel_unregister_dsm_handler();
  13640. intel_fbc_global_disable(dev_priv);
  13641. /* flush any delayed tasks or pending work */
  13642. flush_scheduled_work();
  13643. drm_mode_config_cleanup(dev);
  13644. intel_cleanup_overlay(dev_priv);
  13645. intel_cleanup_gt_powersave(dev_priv);
  13646. intel_teardown_gmbus(dev);
  13647. }
  13648. void intel_connector_attach_encoder(struct intel_connector *connector,
  13649. struct intel_encoder *encoder)
  13650. {
  13651. connector->encoder = encoder;
  13652. drm_mode_connector_attach_encoder(&connector->base,
  13653. &encoder->base);
  13654. }
  13655. /*
  13656. * set vga decode state - true == enable VGA decode
  13657. */
  13658. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13659. {
  13660. struct drm_i915_private *dev_priv = to_i915(dev);
  13661. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13662. u16 gmch_ctrl;
  13663. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13664. DRM_ERROR("failed to read control word\n");
  13665. return -EIO;
  13666. }
  13667. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13668. return 0;
  13669. if (state)
  13670. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13671. else
  13672. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13673. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13674. DRM_ERROR("failed to write control word\n");
  13675. return -EIO;
  13676. }
  13677. return 0;
  13678. }
  13679. struct intel_display_error_state {
  13680. u32 power_well_driver;
  13681. int num_transcoders;
  13682. struct intel_cursor_error_state {
  13683. u32 control;
  13684. u32 position;
  13685. u32 base;
  13686. u32 size;
  13687. } cursor[I915_MAX_PIPES];
  13688. struct intel_pipe_error_state {
  13689. bool power_domain_on;
  13690. u32 source;
  13691. u32 stat;
  13692. } pipe[I915_MAX_PIPES];
  13693. struct intel_plane_error_state {
  13694. u32 control;
  13695. u32 stride;
  13696. u32 size;
  13697. u32 pos;
  13698. u32 addr;
  13699. u32 surface;
  13700. u32 tile_offset;
  13701. } plane[I915_MAX_PIPES];
  13702. struct intel_transcoder_error_state {
  13703. bool power_domain_on;
  13704. enum transcoder cpu_transcoder;
  13705. u32 conf;
  13706. u32 htotal;
  13707. u32 hblank;
  13708. u32 hsync;
  13709. u32 vtotal;
  13710. u32 vblank;
  13711. u32 vsync;
  13712. } transcoder[4];
  13713. };
  13714. struct intel_display_error_state *
  13715. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13716. {
  13717. struct intel_display_error_state *error;
  13718. int transcoders[] = {
  13719. TRANSCODER_A,
  13720. TRANSCODER_B,
  13721. TRANSCODER_C,
  13722. TRANSCODER_EDP,
  13723. };
  13724. int i;
  13725. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13726. return NULL;
  13727. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13728. if (error == NULL)
  13729. return NULL;
  13730. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13731. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13732. for_each_pipe(dev_priv, i) {
  13733. error->pipe[i].power_domain_on =
  13734. __intel_display_power_is_enabled(dev_priv,
  13735. POWER_DOMAIN_PIPE(i));
  13736. if (!error->pipe[i].power_domain_on)
  13737. continue;
  13738. error->cursor[i].control = I915_READ(CURCNTR(i));
  13739. error->cursor[i].position = I915_READ(CURPOS(i));
  13740. error->cursor[i].base = I915_READ(CURBASE(i));
  13741. error->plane[i].control = I915_READ(DSPCNTR(i));
  13742. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13743. if (INTEL_GEN(dev_priv) <= 3) {
  13744. error->plane[i].size = I915_READ(DSPSIZE(i));
  13745. error->plane[i].pos = I915_READ(DSPPOS(i));
  13746. }
  13747. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13748. error->plane[i].addr = I915_READ(DSPADDR(i));
  13749. if (INTEL_GEN(dev_priv) >= 4) {
  13750. error->plane[i].surface = I915_READ(DSPSURF(i));
  13751. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13752. }
  13753. error->pipe[i].source = I915_READ(PIPESRC(i));
  13754. if (HAS_GMCH_DISPLAY(dev_priv))
  13755. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13756. }
  13757. /* Note: this does not include DSI transcoders. */
  13758. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13759. if (HAS_DDI(dev_priv))
  13760. error->num_transcoders++; /* Account for eDP. */
  13761. for (i = 0; i < error->num_transcoders; i++) {
  13762. enum transcoder cpu_transcoder = transcoders[i];
  13763. error->transcoder[i].power_domain_on =
  13764. __intel_display_power_is_enabled(dev_priv,
  13765. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13766. if (!error->transcoder[i].power_domain_on)
  13767. continue;
  13768. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13769. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13770. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13771. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13772. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13773. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13774. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13775. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13776. }
  13777. return error;
  13778. }
  13779. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13780. void
  13781. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13782. struct drm_device *dev,
  13783. struct intel_display_error_state *error)
  13784. {
  13785. struct drm_i915_private *dev_priv = to_i915(dev);
  13786. int i;
  13787. if (!error)
  13788. return;
  13789. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13790. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13791. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13792. error->power_well_driver);
  13793. for_each_pipe(dev_priv, i) {
  13794. err_printf(m, "Pipe [%d]:\n", i);
  13795. err_printf(m, " Power: %s\n",
  13796. onoff(error->pipe[i].power_domain_on));
  13797. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13798. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13799. err_printf(m, "Plane [%d]:\n", i);
  13800. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13801. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13802. if (INTEL_INFO(dev)->gen <= 3) {
  13803. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13804. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13805. }
  13806. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13807. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13808. if (INTEL_INFO(dev)->gen >= 4) {
  13809. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13810. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13811. }
  13812. err_printf(m, "Cursor [%d]:\n", i);
  13813. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13814. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13815. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13816. }
  13817. for (i = 0; i < error->num_transcoders; i++) {
  13818. err_printf(m, "CPU transcoder: %s\n",
  13819. transcoder_name(error->transcoder[i].cpu_transcoder));
  13820. err_printf(m, " Power: %s\n",
  13821. onoff(error->transcoder[i].power_domain_on));
  13822. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13823. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13824. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13825. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13826. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13827. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13828. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13829. }
  13830. }