intel_display.c 460 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "intel_dsi.h"
  39. #include "i915_trace.h"
  40. #include <drm/drm_atomic.h>
  41. #include <drm/drm_atomic_helper.h>
  42. #include <drm/drm_dp_helper.h>
  43. #include <drm/drm_crtc_helper.h>
  44. #include <drm/drm_plane_helper.h>
  45. #include <drm/drm_rect.h>
  46. #include <linux/dma_remapping.h>
  47. #include <linux/reservation.h>
  48. #include <linux/dma-buf.h>
  49. 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 broxton_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. * Returns whether any output on the specified pipe is of the specified type
  483. */
  484. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  485. {
  486. struct drm_device *dev = crtc->base.dev;
  487. struct intel_encoder *encoder;
  488. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  489. if (encoder->type == type)
  490. return true;
  491. return false;
  492. }
  493. /**
  494. * Returns whether any output on the specified pipe will have the specified
  495. * type after a staged modeset is complete, i.e., the same as
  496. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  497. * encoder->crtc.
  498. */
  499. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  500. int type)
  501. {
  502. struct drm_atomic_state *state = crtc_state->base.state;
  503. struct drm_connector *connector;
  504. struct drm_connector_state *connector_state;
  505. struct intel_encoder *encoder;
  506. int i, num_connectors = 0;
  507. for_each_connector_in_state(state, connector, connector_state, i) {
  508. if (connector_state->crtc != crtc_state->base.crtc)
  509. continue;
  510. num_connectors++;
  511. encoder = to_intel_encoder(connector_state->best_encoder);
  512. if (encoder->type == type)
  513. return true;
  514. }
  515. WARN_ON(num_connectors == 0);
  516. return false;
  517. }
  518. /*
  519. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  520. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  521. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  522. * The helpers' return value is the rate of the clock that is fed to the
  523. * display engine's pipe which can be the above fast dot clock rate or a
  524. * divided-down version of it.
  525. */
  526. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  527. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  528. {
  529. clock->m = clock->m2 + 2;
  530. clock->p = clock->p1 * clock->p2;
  531. if (WARN_ON(clock->n == 0 || clock->p == 0))
  532. return 0;
  533. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  534. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  535. return clock->dot;
  536. }
  537. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  538. {
  539. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  540. }
  541. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  542. {
  543. clock->m = i9xx_dpll_compute_m(clock);
  544. clock->p = clock->p1 * clock->p2;
  545. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  546. return 0;
  547. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  548. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  549. return clock->dot;
  550. }
  551. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  552. {
  553. clock->m = clock->m1 * clock->m2;
  554. clock->p = clock->p1 * clock->p2;
  555. if (WARN_ON(clock->n == 0 || clock->p == 0))
  556. return 0;
  557. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  558. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  559. return clock->dot / 5;
  560. }
  561. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  562. {
  563. clock->m = clock->m1 * clock->m2;
  564. clock->p = clock->p1 * clock->p2;
  565. if (WARN_ON(clock->n == 0 || clock->p == 0))
  566. return 0;
  567. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  568. clock->n << 22);
  569. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  570. return clock->dot / 5;
  571. }
  572. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  573. /**
  574. * Returns whether the given set of divisors are valid for a given refclk with
  575. * the given connectors.
  576. */
  577. static bool intel_PLL_is_valid(struct drm_device *dev,
  578. const struct intel_limit *limit,
  579. const struct dpll *clock)
  580. {
  581. if (clock->n < limit->n.min || limit->n.max < clock->n)
  582. INTELPllInvalid("n out of range\n");
  583. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  584. INTELPllInvalid("p1 out of range\n");
  585. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  586. INTELPllInvalid("m2 out of range\n");
  587. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  588. INTELPllInvalid("m1 out of range\n");
  589. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
  590. !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
  591. if (clock->m1 <= clock->m2)
  592. INTELPllInvalid("m1 <= m2\n");
  593. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
  594. if (clock->p < limit->p.min || limit->p.max < clock->p)
  595. INTELPllInvalid("p out of range\n");
  596. if (clock->m < limit->m.min || limit->m.max < clock->m)
  597. INTELPllInvalid("m out of range\n");
  598. }
  599. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  600. INTELPllInvalid("vco out of range\n");
  601. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  602. * connector, etc., rather than just a single range.
  603. */
  604. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  605. INTELPllInvalid("dot out of range\n");
  606. return true;
  607. }
  608. static int
  609. i9xx_select_p2_div(const struct intel_limit *limit,
  610. const struct intel_crtc_state *crtc_state,
  611. int target)
  612. {
  613. struct drm_device *dev = crtc_state->base.crtc->dev;
  614. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  615. /*
  616. * For LVDS just rely on its current settings for dual-channel.
  617. * We haven't figured out how to reliably set up different
  618. * single/dual channel state, if we even can.
  619. */
  620. if (intel_is_dual_link_lvds(dev))
  621. return limit->p2.p2_fast;
  622. else
  623. return limit->p2.p2_slow;
  624. } else {
  625. if (target < limit->p2.dot_limit)
  626. return limit->p2.p2_slow;
  627. else
  628. return limit->p2.p2_fast;
  629. }
  630. }
  631. /*
  632. * Returns a set of divisors for the desired target clock with the given
  633. * refclk, or FALSE. The returned values represent the clock equation:
  634. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  635. *
  636. * Target and reference clocks are specified in kHz.
  637. *
  638. * If match_clock is provided, then best_clock P divider must match the P
  639. * divider from @match_clock used for LVDS downclocking.
  640. */
  641. static bool
  642. i9xx_find_best_dpll(const struct intel_limit *limit,
  643. struct intel_crtc_state *crtc_state,
  644. int target, int refclk, struct dpll *match_clock,
  645. struct dpll *best_clock)
  646. {
  647. struct drm_device *dev = crtc_state->base.crtc->dev;
  648. struct dpll clock;
  649. int err = target;
  650. memset(best_clock, 0, sizeof(*best_clock));
  651. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  652. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  653. clock.m1++) {
  654. for (clock.m2 = limit->m2.min;
  655. clock.m2 <= limit->m2.max; clock.m2++) {
  656. if (clock.m2 >= clock.m1)
  657. break;
  658. for (clock.n = limit->n.min;
  659. clock.n <= limit->n.max; clock.n++) {
  660. for (clock.p1 = limit->p1.min;
  661. clock.p1 <= limit->p1.max; clock.p1++) {
  662. int this_err;
  663. i9xx_calc_dpll_params(refclk, &clock);
  664. if (!intel_PLL_is_valid(dev, limit,
  665. &clock))
  666. continue;
  667. if (match_clock &&
  668. clock.p != match_clock->p)
  669. continue;
  670. this_err = abs(clock.dot - target);
  671. if (this_err < err) {
  672. *best_clock = clock;
  673. err = this_err;
  674. }
  675. }
  676. }
  677. }
  678. }
  679. return (err != target);
  680. }
  681. /*
  682. * Returns a set of divisors for the desired target clock with the given
  683. * refclk, or FALSE. The returned values represent the clock equation:
  684. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  685. *
  686. * Target and reference clocks are specified in kHz.
  687. *
  688. * If match_clock is provided, then best_clock P divider must match the P
  689. * divider from @match_clock used for LVDS downclocking.
  690. */
  691. static bool
  692. pnv_find_best_dpll(const struct intel_limit *limit,
  693. struct intel_crtc_state *crtc_state,
  694. int target, int refclk, struct dpll *match_clock,
  695. struct dpll *best_clock)
  696. {
  697. struct drm_device *dev = crtc_state->base.crtc->dev;
  698. struct dpll clock;
  699. int err = target;
  700. memset(best_clock, 0, sizeof(*best_clock));
  701. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  702. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  703. clock.m1++) {
  704. for (clock.m2 = limit->m2.min;
  705. clock.m2 <= limit->m2.max; clock.m2++) {
  706. for (clock.n = limit->n.min;
  707. clock.n <= limit->n.max; clock.n++) {
  708. for (clock.p1 = limit->p1.min;
  709. clock.p1 <= limit->p1.max; clock.p1++) {
  710. int this_err;
  711. pnv_calc_dpll_params(refclk, &clock);
  712. if (!intel_PLL_is_valid(dev, limit,
  713. &clock))
  714. continue;
  715. if (match_clock &&
  716. clock.p != match_clock->p)
  717. continue;
  718. this_err = abs(clock.dot - target);
  719. if (this_err < err) {
  720. *best_clock = clock;
  721. err = this_err;
  722. }
  723. }
  724. }
  725. }
  726. }
  727. return (err != target);
  728. }
  729. /*
  730. * Returns a set of divisors for the desired target clock with the given
  731. * refclk, or FALSE. The returned values represent the clock equation:
  732. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  733. *
  734. * Target and reference clocks are specified in kHz.
  735. *
  736. * If match_clock is provided, then best_clock P divider must match the P
  737. * divider from @match_clock used for LVDS downclocking.
  738. */
  739. static bool
  740. g4x_find_best_dpll(const struct intel_limit *limit,
  741. struct intel_crtc_state *crtc_state,
  742. int target, int refclk, struct dpll *match_clock,
  743. struct dpll *best_clock)
  744. {
  745. struct drm_device *dev = crtc_state->base.crtc->dev;
  746. struct dpll clock;
  747. int max_n;
  748. bool found = false;
  749. /* approximately equals target * 0.00585 */
  750. int err_most = (target >> 8) + (target >> 9);
  751. memset(best_clock, 0, sizeof(*best_clock));
  752. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  753. max_n = limit->n.max;
  754. /* based on hardware requirement, prefer smaller n to precision */
  755. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  756. /* based on hardware requirement, prefere larger m1,m2 */
  757. for (clock.m1 = limit->m1.max;
  758. clock.m1 >= limit->m1.min; clock.m1--) {
  759. for (clock.m2 = limit->m2.max;
  760. clock.m2 >= limit->m2.min; clock.m2--) {
  761. for (clock.p1 = limit->p1.max;
  762. clock.p1 >= limit->p1.min; clock.p1--) {
  763. int this_err;
  764. i9xx_calc_dpll_params(refclk, &clock);
  765. if (!intel_PLL_is_valid(dev, limit,
  766. &clock))
  767. continue;
  768. this_err = abs(clock.dot - target);
  769. if (this_err < err_most) {
  770. *best_clock = clock;
  771. err_most = this_err;
  772. max_n = clock.n;
  773. found = true;
  774. }
  775. }
  776. }
  777. }
  778. }
  779. return found;
  780. }
  781. /*
  782. * Check if the calculated PLL configuration is more optimal compared to the
  783. * best configuration and error found so far. Return the calculated error.
  784. */
  785. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  786. const struct dpll *calculated_clock,
  787. const struct dpll *best_clock,
  788. unsigned int best_error_ppm,
  789. unsigned int *error_ppm)
  790. {
  791. /*
  792. * For CHV ignore the error and consider only the P value.
  793. * Prefer a bigger P value based on HW requirements.
  794. */
  795. if (IS_CHERRYVIEW(dev)) {
  796. *error_ppm = 0;
  797. return calculated_clock->p > best_clock->p;
  798. }
  799. if (WARN_ON_ONCE(!target_freq))
  800. return false;
  801. *error_ppm = div_u64(1000000ULL *
  802. abs(target_freq - calculated_clock->dot),
  803. target_freq);
  804. /*
  805. * Prefer a better P value over a better (smaller) error if the error
  806. * is small. Ensure this preference for future configurations too by
  807. * setting the error to 0.
  808. */
  809. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  810. *error_ppm = 0;
  811. return true;
  812. }
  813. return *error_ppm + 10 < best_error_ppm;
  814. }
  815. /*
  816. * Returns a set of divisors for the desired target clock with the given
  817. * refclk, or FALSE. The returned values represent the clock equation:
  818. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  819. */
  820. static bool
  821. vlv_find_best_dpll(const struct intel_limit *limit,
  822. struct intel_crtc_state *crtc_state,
  823. int target, int refclk, struct dpll *match_clock,
  824. struct dpll *best_clock)
  825. {
  826. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  827. struct drm_device *dev = crtc->base.dev;
  828. struct dpll clock;
  829. unsigned int bestppm = 1000000;
  830. /* min update 19.2 MHz */
  831. int max_n = min(limit->n.max, refclk / 19200);
  832. bool found = false;
  833. target *= 5; /* fast clock */
  834. memset(best_clock, 0, sizeof(*best_clock));
  835. /* based on hardware requirement, prefer smaller n to precision */
  836. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  837. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  838. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  839. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  840. clock.p = clock.p1 * clock.p2;
  841. /* based on hardware requirement, prefer bigger m1,m2 values */
  842. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  843. unsigned int ppm;
  844. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  845. refclk * clock.m1);
  846. vlv_calc_dpll_params(refclk, &clock);
  847. if (!intel_PLL_is_valid(dev, limit,
  848. &clock))
  849. continue;
  850. if (!vlv_PLL_is_optimal(dev, target,
  851. &clock,
  852. best_clock,
  853. bestppm, &ppm))
  854. continue;
  855. *best_clock = clock;
  856. bestppm = ppm;
  857. found = true;
  858. }
  859. }
  860. }
  861. }
  862. return found;
  863. }
  864. /*
  865. * Returns a set of divisors for the desired target clock with the given
  866. * refclk, or FALSE. The returned values represent the clock equation:
  867. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  868. */
  869. static bool
  870. chv_find_best_dpll(const struct intel_limit *limit,
  871. struct intel_crtc_state *crtc_state,
  872. int target, int refclk, struct dpll *match_clock,
  873. struct dpll *best_clock)
  874. {
  875. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  876. struct drm_device *dev = crtc->base.dev;
  877. unsigned int best_error_ppm;
  878. struct dpll clock;
  879. uint64_t m2;
  880. int found = false;
  881. memset(best_clock, 0, sizeof(*best_clock));
  882. best_error_ppm = 1000000;
  883. /*
  884. * Based on hardware doc, the n always set to 1, and m1 always
  885. * set to 2. If requires to support 200Mhz refclk, we need to
  886. * revisit this because n may not 1 anymore.
  887. */
  888. clock.n = 1, clock.m1 = 2;
  889. target *= 5; /* fast clock */
  890. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  891. for (clock.p2 = limit->p2.p2_fast;
  892. clock.p2 >= limit->p2.p2_slow;
  893. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  894. unsigned int error_ppm;
  895. clock.p = clock.p1 * clock.p2;
  896. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  897. clock.n) << 22, refclk * clock.m1);
  898. if (m2 > INT_MAX/clock.m1)
  899. continue;
  900. clock.m2 = m2;
  901. chv_calc_dpll_params(refclk, &clock);
  902. if (!intel_PLL_is_valid(dev, limit, &clock))
  903. continue;
  904. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  905. best_error_ppm, &error_ppm))
  906. continue;
  907. *best_clock = clock;
  908. best_error_ppm = error_ppm;
  909. found = true;
  910. }
  911. }
  912. return found;
  913. }
  914. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  915. struct dpll *best_clock)
  916. {
  917. int refclk = 100000;
  918. const struct intel_limit *limit = &intel_limits_bxt;
  919. return chv_find_best_dpll(limit, crtc_state,
  920. target_clock, refclk, NULL, best_clock);
  921. }
  922. bool intel_crtc_active(struct drm_crtc *crtc)
  923. {
  924. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  925. /* Be paranoid as we can arrive here with only partial
  926. * state retrieved from the hardware during setup.
  927. *
  928. * We can ditch the adjusted_mode.crtc_clock check as soon
  929. * as Haswell has gained clock readout/fastboot support.
  930. *
  931. * We can ditch the crtc->primary->fb check as soon as we can
  932. * properly reconstruct framebuffers.
  933. *
  934. * FIXME: The intel_crtc->active here should be switched to
  935. * crtc->state->active once we have proper CRTC states wired up
  936. * for atomic.
  937. */
  938. return intel_crtc->active && crtc->primary->state->fb &&
  939. intel_crtc->config->base.adjusted_mode.crtc_clock;
  940. }
  941. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  942. enum pipe pipe)
  943. {
  944. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  945. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  946. return intel_crtc->config->cpu_transcoder;
  947. }
  948. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  949. {
  950. struct drm_i915_private *dev_priv = dev->dev_private;
  951. i915_reg_t reg = PIPEDSL(pipe);
  952. u32 line1, line2;
  953. u32 line_mask;
  954. if (IS_GEN2(dev))
  955. line_mask = DSL_LINEMASK_GEN2;
  956. else
  957. line_mask = DSL_LINEMASK_GEN3;
  958. line1 = I915_READ(reg) & line_mask;
  959. msleep(5);
  960. line2 = I915_READ(reg) & line_mask;
  961. return line1 == line2;
  962. }
  963. /*
  964. * intel_wait_for_pipe_off - wait for pipe to turn off
  965. * @crtc: crtc whose pipe to wait for
  966. *
  967. * After disabling a pipe, we can't wait for vblank in the usual way,
  968. * spinning on the vblank interrupt status bit, since we won't actually
  969. * see an interrupt when the pipe is disabled.
  970. *
  971. * On Gen4 and above:
  972. * wait for the pipe register state bit to turn off
  973. *
  974. * Otherwise:
  975. * wait for the display line value to settle (it usually
  976. * ends up stopping at the start of the next frame).
  977. *
  978. */
  979. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  980. {
  981. struct drm_device *dev = crtc->base.dev;
  982. struct drm_i915_private *dev_priv = dev->dev_private;
  983. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  984. enum pipe pipe = crtc->pipe;
  985. if (INTEL_INFO(dev)->gen >= 4) {
  986. i915_reg_t reg = PIPECONF(cpu_transcoder);
  987. /* Wait for the Pipe State to go off */
  988. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  989. 100))
  990. WARN(1, "pipe_off wait timed out\n");
  991. } else {
  992. /* Wait for the display line to settle */
  993. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  994. WARN(1, "pipe_off wait timed out\n");
  995. }
  996. }
  997. /* Only for pre-ILK configs */
  998. void assert_pll(struct drm_i915_private *dev_priv,
  999. enum pipe pipe, bool state)
  1000. {
  1001. u32 val;
  1002. bool cur_state;
  1003. val = I915_READ(DPLL(pipe));
  1004. cur_state = !!(val & DPLL_VCO_ENABLE);
  1005. I915_STATE_WARN(cur_state != state,
  1006. "PLL state assertion failure (expected %s, current %s)\n",
  1007. onoff(state), onoff(cur_state));
  1008. }
  1009. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1010. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1011. {
  1012. u32 val;
  1013. bool cur_state;
  1014. mutex_lock(&dev_priv->sb_lock);
  1015. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1016. mutex_unlock(&dev_priv->sb_lock);
  1017. cur_state = val & DSI_PLL_VCO_EN;
  1018. I915_STATE_WARN(cur_state != state,
  1019. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1020. onoff(state), onoff(cur_state));
  1021. }
  1022. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1023. enum pipe pipe, bool state)
  1024. {
  1025. bool cur_state;
  1026. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1027. pipe);
  1028. if (HAS_DDI(dev_priv)) {
  1029. /* DDI does not have a specific FDI_TX register */
  1030. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1031. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1032. } else {
  1033. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1034. cur_state = !!(val & FDI_TX_ENABLE);
  1035. }
  1036. I915_STATE_WARN(cur_state != state,
  1037. "FDI TX state assertion failure (expected %s, current %s)\n",
  1038. onoff(state), onoff(cur_state));
  1039. }
  1040. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1041. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1042. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1043. enum pipe pipe, bool state)
  1044. {
  1045. u32 val;
  1046. bool cur_state;
  1047. val = I915_READ(FDI_RX_CTL(pipe));
  1048. cur_state = !!(val & FDI_RX_ENABLE);
  1049. I915_STATE_WARN(cur_state != state,
  1050. "FDI RX state assertion failure (expected %s, current %s)\n",
  1051. onoff(state), onoff(cur_state));
  1052. }
  1053. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1054. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1055. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1056. enum pipe pipe)
  1057. {
  1058. u32 val;
  1059. /* ILK FDI PLL is always enabled */
  1060. if (IS_GEN5(dev_priv))
  1061. return;
  1062. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1063. if (HAS_DDI(dev_priv))
  1064. return;
  1065. val = I915_READ(FDI_TX_CTL(pipe));
  1066. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1067. }
  1068. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1069. enum pipe pipe, bool state)
  1070. {
  1071. u32 val;
  1072. bool cur_state;
  1073. val = I915_READ(FDI_RX_CTL(pipe));
  1074. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1075. I915_STATE_WARN(cur_state != state,
  1076. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1077. onoff(state), onoff(cur_state));
  1078. }
  1079. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1080. enum pipe pipe)
  1081. {
  1082. struct drm_device *dev = dev_priv->dev;
  1083. i915_reg_t pp_reg;
  1084. u32 val;
  1085. enum pipe panel_pipe = PIPE_A;
  1086. bool locked = true;
  1087. if (WARN_ON(HAS_DDI(dev)))
  1088. return;
  1089. if (HAS_PCH_SPLIT(dev)) {
  1090. u32 port_sel;
  1091. pp_reg = PCH_PP_CONTROL;
  1092. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1093. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1094. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1095. panel_pipe = PIPE_B;
  1096. /* XXX: else fix for eDP */
  1097. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1098. /* presumably write lock depends on pipe, not port select */
  1099. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1100. panel_pipe = pipe;
  1101. } else {
  1102. pp_reg = PP_CONTROL;
  1103. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1104. panel_pipe = PIPE_B;
  1105. }
  1106. val = I915_READ(pp_reg);
  1107. if (!(val & PANEL_POWER_ON) ||
  1108. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1109. locked = false;
  1110. I915_STATE_WARN(panel_pipe == pipe && locked,
  1111. "panel assertion failure, pipe %c regs locked\n",
  1112. pipe_name(pipe));
  1113. }
  1114. static void assert_cursor(struct drm_i915_private *dev_priv,
  1115. enum pipe pipe, bool state)
  1116. {
  1117. struct drm_device *dev = dev_priv->dev;
  1118. bool cur_state;
  1119. if (IS_845G(dev) || IS_I865G(dev))
  1120. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1121. else
  1122. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1123. I915_STATE_WARN(cur_state != state,
  1124. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1125. pipe_name(pipe), onoff(state), onoff(cur_state));
  1126. }
  1127. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1128. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1129. void assert_pipe(struct drm_i915_private *dev_priv,
  1130. enum pipe pipe, bool state)
  1131. {
  1132. bool cur_state;
  1133. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1134. pipe);
  1135. enum intel_display_power_domain power_domain;
  1136. /* if we need the pipe quirk it must be always on */
  1137. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1138. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1139. state = true;
  1140. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1141. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1142. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1143. cur_state = !!(val & PIPECONF_ENABLE);
  1144. intel_display_power_put(dev_priv, power_domain);
  1145. } else {
  1146. cur_state = false;
  1147. }
  1148. I915_STATE_WARN(cur_state != state,
  1149. "pipe %c assertion failure (expected %s, current %s)\n",
  1150. pipe_name(pipe), onoff(state), onoff(cur_state));
  1151. }
  1152. static void assert_plane(struct drm_i915_private *dev_priv,
  1153. enum plane plane, bool state)
  1154. {
  1155. u32 val;
  1156. bool cur_state;
  1157. val = I915_READ(DSPCNTR(plane));
  1158. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1159. I915_STATE_WARN(cur_state != state,
  1160. "plane %c assertion failure (expected %s, current %s)\n",
  1161. plane_name(plane), onoff(state), onoff(cur_state));
  1162. }
  1163. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1164. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1165. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1166. enum pipe pipe)
  1167. {
  1168. struct drm_device *dev = dev_priv->dev;
  1169. int i;
  1170. /* Primary planes are fixed to pipes on gen4+ */
  1171. if (INTEL_INFO(dev)->gen >= 4) {
  1172. u32 val = I915_READ(DSPCNTR(pipe));
  1173. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1174. "plane %c assertion failure, should be disabled but not\n",
  1175. plane_name(pipe));
  1176. return;
  1177. }
  1178. /* Need to check both planes against the pipe */
  1179. for_each_pipe(dev_priv, i) {
  1180. u32 val = I915_READ(DSPCNTR(i));
  1181. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1182. DISPPLANE_SEL_PIPE_SHIFT;
  1183. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1184. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1185. plane_name(i), pipe_name(pipe));
  1186. }
  1187. }
  1188. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1189. enum pipe pipe)
  1190. {
  1191. struct drm_device *dev = dev_priv->dev;
  1192. int sprite;
  1193. if (INTEL_INFO(dev)->gen >= 9) {
  1194. for_each_sprite(dev_priv, pipe, sprite) {
  1195. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1196. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1197. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1198. sprite, pipe_name(pipe));
  1199. }
  1200. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1201. for_each_sprite(dev_priv, pipe, sprite) {
  1202. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1203. I915_STATE_WARN(val & SP_ENABLE,
  1204. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1205. sprite_name(pipe, sprite), pipe_name(pipe));
  1206. }
  1207. } else if (INTEL_INFO(dev)->gen >= 7) {
  1208. u32 val = I915_READ(SPRCTL(pipe));
  1209. I915_STATE_WARN(val & SPRITE_ENABLE,
  1210. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1211. plane_name(pipe), pipe_name(pipe));
  1212. } else if (INTEL_INFO(dev)->gen >= 5) {
  1213. u32 val = I915_READ(DVSCNTR(pipe));
  1214. I915_STATE_WARN(val & DVS_ENABLE,
  1215. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1216. plane_name(pipe), pipe_name(pipe));
  1217. }
  1218. }
  1219. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1220. {
  1221. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1222. drm_crtc_vblank_put(crtc);
  1223. }
  1224. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1225. enum pipe pipe)
  1226. {
  1227. u32 val;
  1228. bool enabled;
  1229. val = I915_READ(PCH_TRANSCONF(pipe));
  1230. enabled = !!(val & TRANS_ENABLE);
  1231. I915_STATE_WARN(enabled,
  1232. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1233. pipe_name(pipe));
  1234. }
  1235. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1236. enum pipe pipe, u32 port_sel, u32 val)
  1237. {
  1238. if ((val & DP_PORT_EN) == 0)
  1239. return false;
  1240. if (HAS_PCH_CPT(dev_priv)) {
  1241. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1242. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1243. return false;
  1244. } else if (IS_CHERRYVIEW(dev_priv)) {
  1245. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1246. return false;
  1247. } else {
  1248. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1249. return false;
  1250. }
  1251. return true;
  1252. }
  1253. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1254. enum pipe pipe, u32 val)
  1255. {
  1256. if ((val & SDVO_ENABLE) == 0)
  1257. return false;
  1258. if (HAS_PCH_CPT(dev_priv)) {
  1259. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1260. return false;
  1261. } else if (IS_CHERRYVIEW(dev_priv)) {
  1262. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1263. return false;
  1264. } else {
  1265. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1266. return false;
  1267. }
  1268. return true;
  1269. }
  1270. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1271. enum pipe pipe, u32 val)
  1272. {
  1273. if ((val & LVDS_PORT_EN) == 0)
  1274. return false;
  1275. if (HAS_PCH_CPT(dev_priv)) {
  1276. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1277. return false;
  1278. } else {
  1279. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1280. return false;
  1281. }
  1282. return true;
  1283. }
  1284. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1285. enum pipe pipe, u32 val)
  1286. {
  1287. if ((val & ADPA_DAC_ENABLE) == 0)
  1288. return false;
  1289. if (HAS_PCH_CPT(dev_priv)) {
  1290. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1291. return false;
  1292. } else {
  1293. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1294. return false;
  1295. }
  1296. return true;
  1297. }
  1298. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1299. enum pipe pipe, i915_reg_t reg,
  1300. u32 port_sel)
  1301. {
  1302. u32 val = I915_READ(reg);
  1303. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1304. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1305. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1306. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1307. && (val & DP_PIPEB_SELECT),
  1308. "IBX PCH dp port still using transcoder B\n");
  1309. }
  1310. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1311. enum pipe pipe, i915_reg_t reg)
  1312. {
  1313. u32 val = I915_READ(reg);
  1314. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1315. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1316. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1317. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1318. && (val & SDVO_PIPE_B_SELECT),
  1319. "IBX PCH hdmi port still using transcoder B\n");
  1320. }
  1321. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1322. enum pipe pipe)
  1323. {
  1324. u32 val;
  1325. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1326. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1327. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1328. val = I915_READ(PCH_ADPA);
  1329. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1330. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1331. pipe_name(pipe));
  1332. val = I915_READ(PCH_LVDS);
  1333. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1334. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1335. pipe_name(pipe));
  1336. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1337. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1338. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1339. }
  1340. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1341. const struct intel_crtc_state *pipe_config)
  1342. {
  1343. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1344. enum pipe pipe = crtc->pipe;
  1345. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1346. POSTING_READ(DPLL(pipe));
  1347. udelay(150);
  1348. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1349. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1350. }
  1351. static void vlv_enable_pll(struct intel_crtc *crtc,
  1352. const struct intel_crtc_state *pipe_config)
  1353. {
  1354. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1355. enum pipe pipe = crtc->pipe;
  1356. assert_pipe_disabled(dev_priv, pipe);
  1357. /* PLL is protected by panel, make sure we can write it */
  1358. assert_panel_unlocked(dev_priv, pipe);
  1359. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1360. _vlv_enable_pll(crtc, pipe_config);
  1361. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1362. POSTING_READ(DPLL_MD(pipe));
  1363. }
  1364. static void _chv_enable_pll(struct intel_crtc *crtc,
  1365. const struct intel_crtc_state *pipe_config)
  1366. {
  1367. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1368. enum pipe pipe = crtc->pipe;
  1369. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1370. u32 tmp;
  1371. mutex_lock(&dev_priv->sb_lock);
  1372. /* Enable back the 10bit clock to display controller */
  1373. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1374. tmp |= DPIO_DCLKP_EN;
  1375. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1376. mutex_unlock(&dev_priv->sb_lock);
  1377. /*
  1378. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1379. */
  1380. udelay(1);
  1381. /* Enable PLL */
  1382. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1383. /* Check PLL is locked */
  1384. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1385. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1386. }
  1387. static void chv_enable_pll(struct intel_crtc *crtc,
  1388. const struct intel_crtc_state *pipe_config)
  1389. {
  1390. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1391. enum pipe pipe = crtc->pipe;
  1392. assert_pipe_disabled(dev_priv, pipe);
  1393. /* PLL is protected by panel, make sure we can write it */
  1394. assert_panel_unlocked(dev_priv, pipe);
  1395. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1396. _chv_enable_pll(crtc, pipe_config);
  1397. if (pipe != PIPE_A) {
  1398. /*
  1399. * WaPixelRepeatModeFixForC0:chv
  1400. *
  1401. * DPLLCMD is AWOL. Use chicken bits to propagate
  1402. * the value from DPLLBMD to either pipe B or C.
  1403. */
  1404. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1405. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1406. I915_WRITE(CBR4_VLV, 0);
  1407. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1408. /*
  1409. * DPLLB VGA mode also seems to cause problems.
  1410. * We should always have it disabled.
  1411. */
  1412. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1413. } else {
  1414. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1415. POSTING_READ(DPLL_MD(pipe));
  1416. }
  1417. }
  1418. static int intel_num_dvo_pipes(struct drm_device *dev)
  1419. {
  1420. struct intel_crtc *crtc;
  1421. int count = 0;
  1422. for_each_intel_crtc(dev, crtc)
  1423. count += crtc->base.state->active &&
  1424. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1425. return count;
  1426. }
  1427. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1428. {
  1429. struct drm_device *dev = crtc->base.dev;
  1430. struct drm_i915_private *dev_priv = dev->dev_private;
  1431. i915_reg_t reg = DPLL(crtc->pipe);
  1432. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1433. assert_pipe_disabled(dev_priv, crtc->pipe);
  1434. /* PLL is protected by panel, make sure we can write it */
  1435. if (IS_MOBILE(dev) && !IS_I830(dev))
  1436. assert_panel_unlocked(dev_priv, crtc->pipe);
  1437. /* Enable DVO 2x clock on both PLLs if necessary */
  1438. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1439. /*
  1440. * It appears to be important that we don't enable this
  1441. * for the current pipe before otherwise configuring the
  1442. * PLL. No idea how this should be handled if multiple
  1443. * DVO outputs are enabled simultaneosly.
  1444. */
  1445. dpll |= DPLL_DVO_2X_MODE;
  1446. I915_WRITE(DPLL(!crtc->pipe),
  1447. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1448. }
  1449. /*
  1450. * Apparently we need to have VGA mode enabled prior to changing
  1451. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1452. * dividers, even though the register value does change.
  1453. */
  1454. I915_WRITE(reg, 0);
  1455. I915_WRITE(reg, dpll);
  1456. /* Wait for the clocks to stabilize. */
  1457. POSTING_READ(reg);
  1458. udelay(150);
  1459. if (INTEL_INFO(dev)->gen >= 4) {
  1460. I915_WRITE(DPLL_MD(crtc->pipe),
  1461. crtc->config->dpll_hw_state.dpll_md);
  1462. } else {
  1463. /* The pixel multiplier can only be updated once the
  1464. * DPLL is enabled and the clocks are stable.
  1465. *
  1466. * So write it again.
  1467. */
  1468. I915_WRITE(reg, dpll);
  1469. }
  1470. /* We do this three times for luck */
  1471. I915_WRITE(reg, dpll);
  1472. POSTING_READ(reg);
  1473. udelay(150); /* wait for warmup */
  1474. I915_WRITE(reg, dpll);
  1475. POSTING_READ(reg);
  1476. udelay(150); /* wait for warmup */
  1477. I915_WRITE(reg, dpll);
  1478. POSTING_READ(reg);
  1479. udelay(150); /* wait for warmup */
  1480. }
  1481. /**
  1482. * i9xx_disable_pll - disable a PLL
  1483. * @dev_priv: i915 private structure
  1484. * @pipe: pipe PLL to disable
  1485. *
  1486. * Disable the PLL for @pipe, making sure the pipe is off first.
  1487. *
  1488. * Note! This is for pre-ILK only.
  1489. */
  1490. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1491. {
  1492. struct drm_device *dev = crtc->base.dev;
  1493. struct drm_i915_private *dev_priv = dev->dev_private;
  1494. enum pipe pipe = crtc->pipe;
  1495. /* Disable DVO 2x clock on both PLLs if necessary */
  1496. if (IS_I830(dev) &&
  1497. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1498. !intel_num_dvo_pipes(dev)) {
  1499. I915_WRITE(DPLL(PIPE_B),
  1500. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1501. I915_WRITE(DPLL(PIPE_A),
  1502. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1503. }
  1504. /* Don't disable pipe or pipe PLLs if needed */
  1505. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1506. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1507. return;
  1508. /* Make sure the pipe isn't still relying on us */
  1509. assert_pipe_disabled(dev_priv, pipe);
  1510. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1511. POSTING_READ(DPLL(pipe));
  1512. }
  1513. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1514. {
  1515. u32 val;
  1516. /* Make sure the pipe isn't still relying on us */
  1517. assert_pipe_disabled(dev_priv, pipe);
  1518. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1519. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1520. if (pipe != PIPE_A)
  1521. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1522. I915_WRITE(DPLL(pipe), val);
  1523. POSTING_READ(DPLL(pipe));
  1524. }
  1525. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1526. {
  1527. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1528. u32 val;
  1529. /* Make sure the pipe isn't still relying on us */
  1530. assert_pipe_disabled(dev_priv, pipe);
  1531. val = DPLL_SSC_REF_CLK_CHV |
  1532. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1533. if (pipe != PIPE_A)
  1534. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1535. I915_WRITE(DPLL(pipe), val);
  1536. POSTING_READ(DPLL(pipe));
  1537. mutex_lock(&dev_priv->sb_lock);
  1538. /* Disable 10bit clock to display controller */
  1539. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1540. val &= ~DPIO_DCLKP_EN;
  1541. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1542. mutex_unlock(&dev_priv->sb_lock);
  1543. }
  1544. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1545. struct intel_digital_port *dport,
  1546. unsigned int expected_mask)
  1547. {
  1548. u32 port_mask;
  1549. i915_reg_t dpll_reg;
  1550. switch (dport->port) {
  1551. case PORT_B:
  1552. port_mask = DPLL_PORTB_READY_MASK;
  1553. dpll_reg = DPLL(0);
  1554. break;
  1555. case PORT_C:
  1556. port_mask = DPLL_PORTC_READY_MASK;
  1557. dpll_reg = DPLL(0);
  1558. expected_mask <<= 4;
  1559. break;
  1560. case PORT_D:
  1561. port_mask = DPLL_PORTD_READY_MASK;
  1562. dpll_reg = DPIO_PHY_STATUS;
  1563. break;
  1564. default:
  1565. BUG();
  1566. }
  1567. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1568. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1569. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1570. }
  1571. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1572. enum pipe pipe)
  1573. {
  1574. struct drm_device *dev = dev_priv->dev;
  1575. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1576. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1577. i915_reg_t reg;
  1578. uint32_t val, pipeconf_val;
  1579. /* Make sure PCH DPLL is enabled */
  1580. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1581. /* FDI must be feeding us bits for PCH ports */
  1582. assert_fdi_tx_enabled(dev_priv, pipe);
  1583. assert_fdi_rx_enabled(dev_priv, pipe);
  1584. if (HAS_PCH_CPT(dev)) {
  1585. /* Workaround: Set the timing override bit before enabling the
  1586. * pch transcoder. */
  1587. reg = TRANS_CHICKEN2(pipe);
  1588. val = I915_READ(reg);
  1589. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1590. I915_WRITE(reg, val);
  1591. }
  1592. reg = PCH_TRANSCONF(pipe);
  1593. val = I915_READ(reg);
  1594. pipeconf_val = I915_READ(PIPECONF(pipe));
  1595. if (HAS_PCH_IBX(dev_priv)) {
  1596. /*
  1597. * Make the BPC in transcoder be consistent with
  1598. * that in pipeconf reg. For HDMI we must use 8bpc
  1599. * here for both 8bpc and 12bpc.
  1600. */
  1601. val &= ~PIPECONF_BPC_MASK;
  1602. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1603. val |= PIPECONF_8BPC;
  1604. else
  1605. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1606. }
  1607. val &= ~TRANS_INTERLACE_MASK;
  1608. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1609. if (HAS_PCH_IBX(dev_priv) &&
  1610. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1611. val |= TRANS_LEGACY_INTERLACED_ILK;
  1612. else
  1613. val |= TRANS_INTERLACED;
  1614. else
  1615. val |= TRANS_PROGRESSIVE;
  1616. I915_WRITE(reg, val | TRANS_ENABLE);
  1617. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1618. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1619. }
  1620. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1621. enum transcoder cpu_transcoder)
  1622. {
  1623. u32 val, pipeconf_val;
  1624. /* FDI must be feeding us bits for PCH ports */
  1625. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1626. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1627. /* Workaround: set timing override bit. */
  1628. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1629. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1630. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1631. val = TRANS_ENABLE;
  1632. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1633. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1634. PIPECONF_INTERLACED_ILK)
  1635. val |= TRANS_INTERLACED;
  1636. else
  1637. val |= TRANS_PROGRESSIVE;
  1638. I915_WRITE(LPT_TRANSCONF, val);
  1639. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1640. DRM_ERROR("Failed to enable PCH transcoder\n");
  1641. }
  1642. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1643. enum pipe pipe)
  1644. {
  1645. struct drm_device *dev = dev_priv->dev;
  1646. i915_reg_t reg;
  1647. uint32_t val;
  1648. /* FDI relies on the transcoder */
  1649. assert_fdi_tx_disabled(dev_priv, pipe);
  1650. assert_fdi_rx_disabled(dev_priv, pipe);
  1651. /* Ports must be off as well */
  1652. assert_pch_ports_disabled(dev_priv, pipe);
  1653. reg = PCH_TRANSCONF(pipe);
  1654. val = I915_READ(reg);
  1655. val &= ~TRANS_ENABLE;
  1656. I915_WRITE(reg, val);
  1657. /* wait for PCH transcoder off, transcoder state */
  1658. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1659. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1660. if (HAS_PCH_CPT(dev)) {
  1661. /* Workaround: Clear the timing override chicken bit again. */
  1662. reg = TRANS_CHICKEN2(pipe);
  1663. val = I915_READ(reg);
  1664. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1665. I915_WRITE(reg, val);
  1666. }
  1667. }
  1668. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1669. {
  1670. u32 val;
  1671. val = I915_READ(LPT_TRANSCONF);
  1672. val &= ~TRANS_ENABLE;
  1673. I915_WRITE(LPT_TRANSCONF, val);
  1674. /* wait for PCH transcoder off, transcoder state */
  1675. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1676. DRM_ERROR("Failed to disable PCH transcoder\n");
  1677. /* Workaround: clear timing override bit. */
  1678. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1679. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1680. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1681. }
  1682. /**
  1683. * intel_enable_pipe - enable a pipe, asserting requirements
  1684. * @crtc: crtc responsible for the pipe
  1685. *
  1686. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1687. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1688. */
  1689. static void intel_enable_pipe(struct intel_crtc *crtc)
  1690. {
  1691. struct drm_device *dev = crtc->base.dev;
  1692. struct drm_i915_private *dev_priv = dev->dev_private;
  1693. enum pipe pipe = crtc->pipe;
  1694. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1695. enum pipe pch_transcoder;
  1696. i915_reg_t reg;
  1697. u32 val;
  1698. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1699. assert_planes_disabled(dev_priv, pipe);
  1700. assert_cursor_disabled(dev_priv, pipe);
  1701. assert_sprites_disabled(dev_priv, pipe);
  1702. if (HAS_PCH_LPT(dev_priv))
  1703. pch_transcoder = TRANSCODER_A;
  1704. else
  1705. pch_transcoder = pipe;
  1706. /*
  1707. * A pipe without a PLL won't actually be able to drive bits from
  1708. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1709. * need the check.
  1710. */
  1711. if (HAS_GMCH_DISPLAY(dev_priv))
  1712. if (crtc->config->has_dsi_encoder)
  1713. assert_dsi_pll_enabled(dev_priv);
  1714. else
  1715. assert_pll_enabled(dev_priv, pipe);
  1716. else {
  1717. if (crtc->config->has_pch_encoder) {
  1718. /* if driving the PCH, we need FDI enabled */
  1719. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1720. assert_fdi_tx_pll_enabled(dev_priv,
  1721. (enum pipe) cpu_transcoder);
  1722. }
  1723. /* FIXME: assert CPU port conditions for SNB+ */
  1724. }
  1725. reg = PIPECONF(cpu_transcoder);
  1726. val = I915_READ(reg);
  1727. if (val & PIPECONF_ENABLE) {
  1728. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1729. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1730. return;
  1731. }
  1732. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1733. POSTING_READ(reg);
  1734. /*
  1735. * Until the pipe starts DSL will read as 0, which would cause
  1736. * an apparent vblank timestamp jump, which messes up also the
  1737. * frame count when it's derived from the timestamps. So let's
  1738. * wait for the pipe to start properly before we call
  1739. * drm_crtc_vblank_on()
  1740. */
  1741. if (dev->max_vblank_count == 0 &&
  1742. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1743. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1744. }
  1745. /**
  1746. * intel_disable_pipe - disable a pipe, asserting requirements
  1747. * @crtc: crtc whose pipes is to be disabled
  1748. *
  1749. * Disable the pipe of @crtc, making sure that various hardware
  1750. * specific requirements are met, if applicable, e.g. plane
  1751. * disabled, panel fitter off, etc.
  1752. *
  1753. * Will wait until the pipe has shut down before returning.
  1754. */
  1755. static void intel_disable_pipe(struct intel_crtc *crtc)
  1756. {
  1757. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1758. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1759. enum pipe pipe = crtc->pipe;
  1760. i915_reg_t reg;
  1761. u32 val;
  1762. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1763. /*
  1764. * Make sure planes won't keep trying to pump pixels to us,
  1765. * or we might hang the display.
  1766. */
  1767. assert_planes_disabled(dev_priv, pipe);
  1768. assert_cursor_disabled(dev_priv, pipe);
  1769. assert_sprites_disabled(dev_priv, pipe);
  1770. reg = PIPECONF(cpu_transcoder);
  1771. val = I915_READ(reg);
  1772. if ((val & PIPECONF_ENABLE) == 0)
  1773. return;
  1774. /*
  1775. * Double wide has implications for planes
  1776. * so best keep it disabled when not needed.
  1777. */
  1778. if (crtc->config->double_wide)
  1779. val &= ~PIPECONF_DOUBLE_WIDE;
  1780. /* Don't disable pipe or pipe PLLs if needed */
  1781. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1782. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1783. val &= ~PIPECONF_ENABLE;
  1784. I915_WRITE(reg, val);
  1785. if ((val & PIPECONF_ENABLE) == 0)
  1786. intel_wait_for_pipe_off(crtc);
  1787. }
  1788. static bool need_vtd_wa(struct drm_device *dev)
  1789. {
  1790. #ifdef CONFIG_INTEL_IOMMU
  1791. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1792. return true;
  1793. #endif
  1794. return false;
  1795. }
  1796. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1797. {
  1798. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1799. }
  1800. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1801. uint64_t fb_modifier, unsigned int cpp)
  1802. {
  1803. switch (fb_modifier) {
  1804. case DRM_FORMAT_MOD_NONE:
  1805. return cpp;
  1806. case I915_FORMAT_MOD_X_TILED:
  1807. if (IS_GEN2(dev_priv))
  1808. return 128;
  1809. else
  1810. return 512;
  1811. case I915_FORMAT_MOD_Y_TILED:
  1812. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1813. return 128;
  1814. else
  1815. return 512;
  1816. case I915_FORMAT_MOD_Yf_TILED:
  1817. switch (cpp) {
  1818. case 1:
  1819. return 64;
  1820. case 2:
  1821. case 4:
  1822. return 128;
  1823. case 8:
  1824. case 16:
  1825. return 256;
  1826. default:
  1827. MISSING_CASE(cpp);
  1828. return cpp;
  1829. }
  1830. break;
  1831. default:
  1832. MISSING_CASE(fb_modifier);
  1833. return cpp;
  1834. }
  1835. }
  1836. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1837. uint64_t fb_modifier, unsigned int cpp)
  1838. {
  1839. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1840. return 1;
  1841. else
  1842. return intel_tile_size(dev_priv) /
  1843. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1844. }
  1845. /* Return the tile dimensions in pixel units */
  1846. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1847. unsigned int *tile_width,
  1848. unsigned int *tile_height,
  1849. uint64_t fb_modifier,
  1850. unsigned int cpp)
  1851. {
  1852. unsigned int tile_width_bytes =
  1853. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1854. *tile_width = tile_width_bytes / cpp;
  1855. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1856. }
  1857. unsigned int
  1858. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1859. uint32_t pixel_format, uint64_t fb_modifier)
  1860. {
  1861. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1862. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1863. return ALIGN(height, tile_height);
  1864. }
  1865. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1866. {
  1867. unsigned int size = 0;
  1868. int i;
  1869. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1870. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1871. return size;
  1872. }
  1873. static void
  1874. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1875. const struct drm_framebuffer *fb,
  1876. unsigned int rotation)
  1877. {
  1878. if (intel_rotation_90_or_270(rotation)) {
  1879. *view = i915_ggtt_view_rotated;
  1880. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1881. } else {
  1882. *view = i915_ggtt_view_normal;
  1883. }
  1884. }
  1885. static void
  1886. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  1887. struct drm_framebuffer *fb)
  1888. {
  1889. struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
  1890. unsigned int tile_size, tile_width, tile_height, cpp;
  1891. tile_size = intel_tile_size(dev_priv);
  1892. cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  1893. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1894. fb->modifier[0], cpp);
  1895. info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
  1896. info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
  1897. if (info->pixel_format == DRM_FORMAT_NV12) {
  1898. cpp = drm_format_plane_cpp(fb->pixel_format, 1);
  1899. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1900. fb->modifier[1], cpp);
  1901. info->uv_offset = fb->offsets[1];
  1902. info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
  1903. info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
  1904. }
  1905. }
  1906. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1907. {
  1908. if (INTEL_INFO(dev_priv)->gen >= 9)
  1909. return 256 * 1024;
  1910. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1911. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1912. return 128 * 1024;
  1913. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1914. return 4 * 1024;
  1915. else
  1916. return 0;
  1917. }
  1918. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1919. uint64_t fb_modifier)
  1920. {
  1921. switch (fb_modifier) {
  1922. case DRM_FORMAT_MOD_NONE:
  1923. return intel_linear_alignment(dev_priv);
  1924. case I915_FORMAT_MOD_X_TILED:
  1925. if (INTEL_INFO(dev_priv)->gen >= 9)
  1926. return 256 * 1024;
  1927. return 0;
  1928. case I915_FORMAT_MOD_Y_TILED:
  1929. case I915_FORMAT_MOD_Yf_TILED:
  1930. return 1 * 1024 * 1024;
  1931. default:
  1932. MISSING_CASE(fb_modifier);
  1933. return 0;
  1934. }
  1935. }
  1936. int
  1937. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1938. unsigned int rotation)
  1939. {
  1940. struct drm_device *dev = fb->dev;
  1941. struct drm_i915_private *dev_priv = dev->dev_private;
  1942. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1943. struct i915_ggtt_view view;
  1944. u32 alignment;
  1945. int ret;
  1946. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1947. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  1948. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1949. /* Note that the w/a also requires 64 PTE of padding following the
  1950. * bo. We currently fill all unused PTE with the shadow page and so
  1951. * we should always have valid PTE following the scanout preventing
  1952. * the VT-d warning.
  1953. */
  1954. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1955. alignment = 256 * 1024;
  1956. /*
  1957. * Global gtt pte registers are special registers which actually forward
  1958. * writes to a chunk of system memory. Which means that there is no risk
  1959. * that the register values disappear as soon as we call
  1960. * intel_runtime_pm_put(), so it is correct to wrap only the
  1961. * pin/unpin/fence and not more.
  1962. */
  1963. intel_runtime_pm_get(dev_priv);
  1964. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  1965. &view);
  1966. if (ret)
  1967. goto err_pm;
  1968. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1969. * fence, whereas 965+ only requires a fence if using
  1970. * framebuffer compression. For simplicity, we always install
  1971. * a fence as the cost is not that onerous.
  1972. */
  1973. if (view.type == I915_GGTT_VIEW_NORMAL) {
  1974. ret = i915_gem_object_get_fence(obj);
  1975. if (ret == -EDEADLK) {
  1976. /*
  1977. * -EDEADLK means there are no free fences
  1978. * no pending flips.
  1979. *
  1980. * This is propagated to atomic, but it uses
  1981. * -EDEADLK to force a locking recovery, so
  1982. * change the returned error to -EBUSY.
  1983. */
  1984. ret = -EBUSY;
  1985. goto err_unpin;
  1986. } else if (ret)
  1987. goto err_unpin;
  1988. i915_gem_object_pin_fence(obj);
  1989. }
  1990. intel_runtime_pm_put(dev_priv);
  1991. return 0;
  1992. err_unpin:
  1993. i915_gem_object_unpin_from_display_plane(obj, &view);
  1994. err_pm:
  1995. intel_runtime_pm_put(dev_priv);
  1996. return ret;
  1997. }
  1998. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1999. {
  2000. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2001. struct i915_ggtt_view view;
  2002. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2003. intel_fill_fb_ggtt_view(&view, fb, rotation);
  2004. if (view.type == I915_GGTT_VIEW_NORMAL)
  2005. i915_gem_object_unpin_fence(obj);
  2006. i915_gem_object_unpin_from_display_plane(obj, &view);
  2007. }
  2008. /*
  2009. * Adjust the tile offset by moving the difference into
  2010. * the x/y offsets.
  2011. *
  2012. * Input tile dimensions and pitch must already be
  2013. * rotated to match x and y, and in pixel units.
  2014. */
  2015. static u32 intel_adjust_tile_offset(int *x, int *y,
  2016. unsigned int tile_width,
  2017. unsigned int tile_height,
  2018. unsigned int tile_size,
  2019. unsigned int pitch_tiles,
  2020. u32 old_offset,
  2021. u32 new_offset)
  2022. {
  2023. unsigned int tiles;
  2024. WARN_ON(old_offset & (tile_size - 1));
  2025. WARN_ON(new_offset & (tile_size - 1));
  2026. WARN_ON(new_offset > old_offset);
  2027. tiles = (old_offset - new_offset) / tile_size;
  2028. *y += tiles / pitch_tiles * tile_height;
  2029. *x += tiles % pitch_tiles * tile_width;
  2030. return new_offset;
  2031. }
  2032. /*
  2033. * Computes the linear offset to the base tile and adjusts
  2034. * x, y. bytes per pixel is assumed to be a power-of-two.
  2035. *
  2036. * In the 90/270 rotated case, x and y are assumed
  2037. * to be already rotated to match the rotated GTT view, and
  2038. * pitch is the tile_height aligned framebuffer height.
  2039. */
  2040. u32 intel_compute_tile_offset(int *x, int *y,
  2041. const struct drm_framebuffer *fb, int plane,
  2042. unsigned int pitch,
  2043. unsigned int rotation)
  2044. {
  2045. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2046. uint64_t fb_modifier = fb->modifier[plane];
  2047. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2048. u32 offset, offset_aligned, alignment;
  2049. alignment = intel_surf_alignment(dev_priv, fb_modifier);
  2050. if (alignment)
  2051. alignment--;
  2052. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2053. unsigned int tile_size, tile_width, tile_height;
  2054. unsigned int tile_rows, tiles, pitch_tiles;
  2055. tile_size = intel_tile_size(dev_priv);
  2056. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2057. fb_modifier, cpp);
  2058. if (intel_rotation_90_or_270(rotation)) {
  2059. pitch_tiles = pitch / tile_height;
  2060. swap(tile_width, tile_height);
  2061. } else {
  2062. pitch_tiles = pitch / (tile_width * cpp);
  2063. }
  2064. tile_rows = *y / tile_height;
  2065. *y %= tile_height;
  2066. tiles = *x / tile_width;
  2067. *x %= tile_width;
  2068. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2069. offset_aligned = offset & ~alignment;
  2070. intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2071. tile_size, pitch_tiles,
  2072. offset, offset_aligned);
  2073. } else {
  2074. offset = *y * pitch + *x * cpp;
  2075. offset_aligned = offset & ~alignment;
  2076. *y = (offset & alignment) / pitch;
  2077. *x = ((offset & alignment) - *y * pitch) / cpp;
  2078. }
  2079. return offset_aligned;
  2080. }
  2081. static int i9xx_format_to_fourcc(int format)
  2082. {
  2083. switch (format) {
  2084. case DISPPLANE_8BPP:
  2085. return DRM_FORMAT_C8;
  2086. case DISPPLANE_BGRX555:
  2087. return DRM_FORMAT_XRGB1555;
  2088. case DISPPLANE_BGRX565:
  2089. return DRM_FORMAT_RGB565;
  2090. default:
  2091. case DISPPLANE_BGRX888:
  2092. return DRM_FORMAT_XRGB8888;
  2093. case DISPPLANE_RGBX888:
  2094. return DRM_FORMAT_XBGR8888;
  2095. case DISPPLANE_BGRX101010:
  2096. return DRM_FORMAT_XRGB2101010;
  2097. case DISPPLANE_RGBX101010:
  2098. return DRM_FORMAT_XBGR2101010;
  2099. }
  2100. }
  2101. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2102. {
  2103. switch (format) {
  2104. case PLANE_CTL_FORMAT_RGB_565:
  2105. return DRM_FORMAT_RGB565;
  2106. default:
  2107. case PLANE_CTL_FORMAT_XRGB_8888:
  2108. if (rgb_order) {
  2109. if (alpha)
  2110. return DRM_FORMAT_ABGR8888;
  2111. else
  2112. return DRM_FORMAT_XBGR8888;
  2113. } else {
  2114. if (alpha)
  2115. return DRM_FORMAT_ARGB8888;
  2116. else
  2117. return DRM_FORMAT_XRGB8888;
  2118. }
  2119. case PLANE_CTL_FORMAT_XRGB_2101010:
  2120. if (rgb_order)
  2121. return DRM_FORMAT_XBGR2101010;
  2122. else
  2123. return DRM_FORMAT_XRGB2101010;
  2124. }
  2125. }
  2126. static bool
  2127. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2128. struct intel_initial_plane_config *plane_config)
  2129. {
  2130. struct drm_device *dev = crtc->base.dev;
  2131. struct drm_i915_private *dev_priv = to_i915(dev);
  2132. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2133. struct drm_i915_gem_object *obj = NULL;
  2134. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2135. struct drm_framebuffer *fb = &plane_config->fb->base;
  2136. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2137. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2138. PAGE_SIZE);
  2139. size_aligned -= base_aligned;
  2140. if (plane_config->size == 0)
  2141. return false;
  2142. /* If the FB is too big, just don't use it since fbdev is not very
  2143. * important and we should probably use that space with FBC or other
  2144. * features. */
  2145. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2146. return false;
  2147. mutex_lock(&dev->struct_mutex);
  2148. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2149. base_aligned,
  2150. base_aligned,
  2151. size_aligned);
  2152. if (!obj) {
  2153. mutex_unlock(&dev->struct_mutex);
  2154. return false;
  2155. }
  2156. obj->tiling_mode = plane_config->tiling;
  2157. if (obj->tiling_mode == I915_TILING_X)
  2158. obj->stride = fb->pitches[0];
  2159. mode_cmd.pixel_format = fb->pixel_format;
  2160. mode_cmd.width = fb->width;
  2161. mode_cmd.height = fb->height;
  2162. mode_cmd.pitches[0] = fb->pitches[0];
  2163. mode_cmd.modifier[0] = fb->modifier[0];
  2164. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2165. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2166. &mode_cmd, obj)) {
  2167. DRM_DEBUG_KMS("intel fb init failed\n");
  2168. goto out_unref_obj;
  2169. }
  2170. mutex_unlock(&dev->struct_mutex);
  2171. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2172. return true;
  2173. out_unref_obj:
  2174. drm_gem_object_unreference(&obj->base);
  2175. mutex_unlock(&dev->struct_mutex);
  2176. return false;
  2177. }
  2178. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2179. static void
  2180. update_state_fb(struct drm_plane *plane)
  2181. {
  2182. if (plane->fb == plane->state->fb)
  2183. return;
  2184. if (plane->state->fb)
  2185. drm_framebuffer_unreference(plane->state->fb);
  2186. plane->state->fb = plane->fb;
  2187. if (plane->state->fb)
  2188. drm_framebuffer_reference(plane->state->fb);
  2189. }
  2190. static void
  2191. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2192. struct intel_initial_plane_config *plane_config)
  2193. {
  2194. struct drm_device *dev = intel_crtc->base.dev;
  2195. struct drm_i915_private *dev_priv = dev->dev_private;
  2196. struct drm_crtc *c;
  2197. struct intel_crtc *i;
  2198. struct drm_i915_gem_object *obj;
  2199. struct drm_plane *primary = intel_crtc->base.primary;
  2200. struct drm_plane_state *plane_state = primary->state;
  2201. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2202. struct intel_plane *intel_plane = to_intel_plane(primary);
  2203. struct intel_plane_state *intel_state =
  2204. to_intel_plane_state(plane_state);
  2205. struct drm_framebuffer *fb;
  2206. if (!plane_config->fb)
  2207. return;
  2208. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2209. fb = &plane_config->fb->base;
  2210. goto valid_fb;
  2211. }
  2212. kfree(plane_config->fb);
  2213. /*
  2214. * Failed to alloc the obj, check to see if we should share
  2215. * an fb with another CRTC instead
  2216. */
  2217. for_each_crtc(dev, c) {
  2218. i = to_intel_crtc(c);
  2219. if (c == &intel_crtc->base)
  2220. continue;
  2221. if (!i->active)
  2222. continue;
  2223. fb = c->primary->fb;
  2224. if (!fb)
  2225. continue;
  2226. obj = intel_fb_obj(fb);
  2227. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2228. drm_framebuffer_reference(fb);
  2229. goto valid_fb;
  2230. }
  2231. }
  2232. /*
  2233. * We've failed to reconstruct the BIOS FB. Current display state
  2234. * indicates that the primary plane is visible, but has a NULL FB,
  2235. * which will lead to problems later if we don't fix it up. The
  2236. * simplest solution is to just disable the primary plane now and
  2237. * pretend the BIOS never had it enabled.
  2238. */
  2239. to_intel_plane_state(plane_state)->visible = false;
  2240. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2241. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2242. intel_plane->disable_plane(primary, &intel_crtc->base);
  2243. return;
  2244. valid_fb:
  2245. plane_state->src_x = 0;
  2246. plane_state->src_y = 0;
  2247. plane_state->src_w = fb->width << 16;
  2248. plane_state->src_h = fb->height << 16;
  2249. plane_state->crtc_x = 0;
  2250. plane_state->crtc_y = 0;
  2251. plane_state->crtc_w = fb->width;
  2252. plane_state->crtc_h = fb->height;
  2253. intel_state->src.x1 = plane_state->src_x;
  2254. intel_state->src.y1 = plane_state->src_y;
  2255. intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
  2256. intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
  2257. intel_state->dst.x1 = plane_state->crtc_x;
  2258. intel_state->dst.y1 = plane_state->crtc_y;
  2259. intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2260. intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2261. obj = intel_fb_obj(fb);
  2262. if (obj->tiling_mode != I915_TILING_NONE)
  2263. dev_priv->preserve_bios_swizzle = true;
  2264. drm_framebuffer_reference(fb);
  2265. primary->fb = primary->state->fb = fb;
  2266. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2267. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2268. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2269. }
  2270. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2271. const struct intel_crtc_state *crtc_state,
  2272. const struct intel_plane_state *plane_state)
  2273. {
  2274. struct drm_device *dev = primary->dev;
  2275. struct drm_i915_private *dev_priv = dev->dev_private;
  2276. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2277. struct drm_framebuffer *fb = plane_state->base.fb;
  2278. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2279. int plane = intel_crtc->plane;
  2280. u32 linear_offset;
  2281. u32 dspcntr;
  2282. i915_reg_t reg = DSPCNTR(plane);
  2283. unsigned int rotation = plane_state->base.rotation;
  2284. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2285. int x = plane_state->src.x1 >> 16;
  2286. int y = plane_state->src.y1 >> 16;
  2287. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2288. dspcntr |= DISPLAY_PLANE_ENABLE;
  2289. if (INTEL_INFO(dev)->gen < 4) {
  2290. if (intel_crtc->pipe == PIPE_B)
  2291. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2292. /* pipesrc and dspsize control the size that is scaled from,
  2293. * which should always be the user's requested size.
  2294. */
  2295. I915_WRITE(DSPSIZE(plane),
  2296. ((crtc_state->pipe_src_h - 1) << 16) |
  2297. (crtc_state->pipe_src_w - 1));
  2298. I915_WRITE(DSPPOS(plane), 0);
  2299. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2300. I915_WRITE(PRIMSIZE(plane),
  2301. ((crtc_state->pipe_src_h - 1) << 16) |
  2302. (crtc_state->pipe_src_w - 1));
  2303. I915_WRITE(PRIMPOS(plane), 0);
  2304. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2305. }
  2306. switch (fb->pixel_format) {
  2307. case DRM_FORMAT_C8:
  2308. dspcntr |= DISPPLANE_8BPP;
  2309. break;
  2310. case DRM_FORMAT_XRGB1555:
  2311. dspcntr |= DISPPLANE_BGRX555;
  2312. break;
  2313. case DRM_FORMAT_RGB565:
  2314. dspcntr |= DISPPLANE_BGRX565;
  2315. break;
  2316. case DRM_FORMAT_XRGB8888:
  2317. dspcntr |= DISPPLANE_BGRX888;
  2318. break;
  2319. case DRM_FORMAT_XBGR8888:
  2320. dspcntr |= DISPPLANE_RGBX888;
  2321. break;
  2322. case DRM_FORMAT_XRGB2101010:
  2323. dspcntr |= DISPPLANE_BGRX101010;
  2324. break;
  2325. case DRM_FORMAT_XBGR2101010:
  2326. dspcntr |= DISPPLANE_RGBX101010;
  2327. break;
  2328. default:
  2329. BUG();
  2330. }
  2331. if (INTEL_INFO(dev)->gen >= 4 &&
  2332. obj->tiling_mode != I915_TILING_NONE)
  2333. dspcntr |= DISPPLANE_TILED;
  2334. if (IS_G4X(dev))
  2335. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2336. linear_offset = y * fb->pitches[0] + x * cpp;
  2337. if (INTEL_INFO(dev)->gen >= 4) {
  2338. intel_crtc->dspaddr_offset =
  2339. intel_compute_tile_offset(&x, &y, fb, 0,
  2340. fb->pitches[0], rotation);
  2341. linear_offset -= intel_crtc->dspaddr_offset;
  2342. } else {
  2343. intel_crtc->dspaddr_offset = linear_offset;
  2344. }
  2345. if (rotation == BIT(DRM_ROTATE_180)) {
  2346. dspcntr |= DISPPLANE_ROTATE_180;
  2347. x += (crtc_state->pipe_src_w - 1);
  2348. y += (crtc_state->pipe_src_h - 1);
  2349. /* Finding the last pixel of the last line of the display
  2350. data and adding to linear_offset*/
  2351. linear_offset +=
  2352. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2353. (crtc_state->pipe_src_w - 1) * cpp;
  2354. }
  2355. intel_crtc->adjusted_x = x;
  2356. intel_crtc->adjusted_y = y;
  2357. I915_WRITE(reg, dspcntr);
  2358. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2359. if (INTEL_INFO(dev)->gen >= 4) {
  2360. I915_WRITE(DSPSURF(plane),
  2361. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2362. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2363. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2364. } else
  2365. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2366. POSTING_READ(reg);
  2367. }
  2368. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2369. struct drm_crtc *crtc)
  2370. {
  2371. struct drm_device *dev = crtc->dev;
  2372. struct drm_i915_private *dev_priv = dev->dev_private;
  2373. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2374. int plane = intel_crtc->plane;
  2375. I915_WRITE(DSPCNTR(plane), 0);
  2376. if (INTEL_INFO(dev_priv)->gen >= 4)
  2377. I915_WRITE(DSPSURF(plane), 0);
  2378. else
  2379. I915_WRITE(DSPADDR(plane), 0);
  2380. POSTING_READ(DSPCNTR(plane));
  2381. }
  2382. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2383. const struct intel_crtc_state *crtc_state,
  2384. const struct intel_plane_state *plane_state)
  2385. {
  2386. struct drm_device *dev = primary->dev;
  2387. struct drm_i915_private *dev_priv = dev->dev_private;
  2388. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2389. struct drm_framebuffer *fb = plane_state->base.fb;
  2390. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2391. int plane = intel_crtc->plane;
  2392. u32 linear_offset;
  2393. u32 dspcntr;
  2394. i915_reg_t reg = DSPCNTR(plane);
  2395. unsigned int rotation = plane_state->base.rotation;
  2396. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2397. int x = plane_state->src.x1 >> 16;
  2398. int y = plane_state->src.y1 >> 16;
  2399. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2400. dspcntr |= DISPLAY_PLANE_ENABLE;
  2401. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2402. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2403. switch (fb->pixel_format) {
  2404. case DRM_FORMAT_C8:
  2405. dspcntr |= DISPPLANE_8BPP;
  2406. break;
  2407. case DRM_FORMAT_RGB565:
  2408. dspcntr |= DISPPLANE_BGRX565;
  2409. break;
  2410. case DRM_FORMAT_XRGB8888:
  2411. dspcntr |= DISPPLANE_BGRX888;
  2412. break;
  2413. case DRM_FORMAT_XBGR8888:
  2414. dspcntr |= DISPPLANE_RGBX888;
  2415. break;
  2416. case DRM_FORMAT_XRGB2101010:
  2417. dspcntr |= DISPPLANE_BGRX101010;
  2418. break;
  2419. case DRM_FORMAT_XBGR2101010:
  2420. dspcntr |= DISPPLANE_RGBX101010;
  2421. break;
  2422. default:
  2423. BUG();
  2424. }
  2425. if (obj->tiling_mode != I915_TILING_NONE)
  2426. dspcntr |= DISPPLANE_TILED;
  2427. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2428. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2429. linear_offset = y * fb->pitches[0] + x * cpp;
  2430. intel_crtc->dspaddr_offset =
  2431. intel_compute_tile_offset(&x, &y, fb, 0,
  2432. fb->pitches[0], rotation);
  2433. linear_offset -= intel_crtc->dspaddr_offset;
  2434. if (rotation == BIT(DRM_ROTATE_180)) {
  2435. dspcntr |= DISPPLANE_ROTATE_180;
  2436. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2437. x += (crtc_state->pipe_src_w - 1);
  2438. y += (crtc_state->pipe_src_h - 1);
  2439. /* Finding the last pixel of the last line of the display
  2440. data and adding to linear_offset*/
  2441. linear_offset +=
  2442. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2443. (crtc_state->pipe_src_w - 1) * cpp;
  2444. }
  2445. }
  2446. intel_crtc->adjusted_x = x;
  2447. intel_crtc->adjusted_y = y;
  2448. I915_WRITE(reg, dspcntr);
  2449. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2450. I915_WRITE(DSPSURF(plane),
  2451. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2452. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2453. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2454. } else {
  2455. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2456. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2457. }
  2458. POSTING_READ(reg);
  2459. }
  2460. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2461. uint64_t fb_modifier, uint32_t pixel_format)
  2462. {
  2463. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2464. return 64;
  2465. } else {
  2466. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2467. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2468. }
  2469. }
  2470. u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
  2471. struct drm_i915_gem_object *obj,
  2472. unsigned int plane)
  2473. {
  2474. struct i915_ggtt_view view;
  2475. struct i915_vma *vma;
  2476. u64 offset;
  2477. intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
  2478. intel_plane->base.state->rotation);
  2479. vma = i915_gem_obj_to_ggtt_view(obj, &view);
  2480. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2481. view.type))
  2482. return -1;
  2483. offset = vma->node.start;
  2484. if (plane == 1) {
  2485. offset += vma->ggtt_view.params.rotated.uv_start_page *
  2486. PAGE_SIZE;
  2487. }
  2488. WARN_ON(upper_32_bits(offset));
  2489. return lower_32_bits(offset);
  2490. }
  2491. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2492. {
  2493. struct drm_device *dev = intel_crtc->base.dev;
  2494. struct drm_i915_private *dev_priv = dev->dev_private;
  2495. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2496. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2497. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2498. }
  2499. /*
  2500. * This function detaches (aka. unbinds) unused scalers in hardware
  2501. */
  2502. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2503. {
  2504. struct intel_crtc_scaler_state *scaler_state;
  2505. int i;
  2506. scaler_state = &intel_crtc->config->scaler_state;
  2507. /* loop through and disable scalers that aren't in use */
  2508. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2509. if (!scaler_state->scalers[i].in_use)
  2510. skl_detach_scaler(intel_crtc, i);
  2511. }
  2512. }
  2513. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2514. {
  2515. switch (pixel_format) {
  2516. case DRM_FORMAT_C8:
  2517. return PLANE_CTL_FORMAT_INDEXED;
  2518. case DRM_FORMAT_RGB565:
  2519. return PLANE_CTL_FORMAT_RGB_565;
  2520. case DRM_FORMAT_XBGR8888:
  2521. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2522. case DRM_FORMAT_XRGB8888:
  2523. return PLANE_CTL_FORMAT_XRGB_8888;
  2524. /*
  2525. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2526. * to be already pre-multiplied. We need to add a knob (or a different
  2527. * DRM_FORMAT) for user-space to configure that.
  2528. */
  2529. case DRM_FORMAT_ABGR8888:
  2530. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2531. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2532. case DRM_FORMAT_ARGB8888:
  2533. return PLANE_CTL_FORMAT_XRGB_8888 |
  2534. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2535. case DRM_FORMAT_XRGB2101010:
  2536. return PLANE_CTL_FORMAT_XRGB_2101010;
  2537. case DRM_FORMAT_XBGR2101010:
  2538. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2539. case DRM_FORMAT_YUYV:
  2540. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2541. case DRM_FORMAT_YVYU:
  2542. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2543. case DRM_FORMAT_UYVY:
  2544. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2545. case DRM_FORMAT_VYUY:
  2546. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2547. default:
  2548. MISSING_CASE(pixel_format);
  2549. }
  2550. return 0;
  2551. }
  2552. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2553. {
  2554. switch (fb_modifier) {
  2555. case DRM_FORMAT_MOD_NONE:
  2556. break;
  2557. case I915_FORMAT_MOD_X_TILED:
  2558. return PLANE_CTL_TILED_X;
  2559. case I915_FORMAT_MOD_Y_TILED:
  2560. return PLANE_CTL_TILED_Y;
  2561. case I915_FORMAT_MOD_Yf_TILED:
  2562. return PLANE_CTL_TILED_YF;
  2563. default:
  2564. MISSING_CASE(fb_modifier);
  2565. }
  2566. return 0;
  2567. }
  2568. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2569. {
  2570. switch (rotation) {
  2571. case BIT(DRM_ROTATE_0):
  2572. break;
  2573. /*
  2574. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2575. * while i915 HW rotation is clockwise, thats why this swapping.
  2576. */
  2577. case BIT(DRM_ROTATE_90):
  2578. return PLANE_CTL_ROTATE_270;
  2579. case BIT(DRM_ROTATE_180):
  2580. return PLANE_CTL_ROTATE_180;
  2581. case BIT(DRM_ROTATE_270):
  2582. return PLANE_CTL_ROTATE_90;
  2583. default:
  2584. MISSING_CASE(rotation);
  2585. }
  2586. return 0;
  2587. }
  2588. static void skylake_update_primary_plane(struct drm_plane *plane,
  2589. const struct intel_crtc_state *crtc_state,
  2590. const struct intel_plane_state *plane_state)
  2591. {
  2592. struct drm_device *dev = plane->dev;
  2593. struct drm_i915_private *dev_priv = dev->dev_private;
  2594. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2595. struct drm_framebuffer *fb = plane_state->base.fb;
  2596. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2597. int pipe = intel_crtc->pipe;
  2598. u32 plane_ctl, stride_div, stride;
  2599. u32 tile_height, plane_offset, plane_size;
  2600. unsigned int rotation = plane_state->base.rotation;
  2601. int x_offset, y_offset;
  2602. u32 surf_addr;
  2603. int scaler_id = plane_state->scaler_id;
  2604. int src_x = plane_state->src.x1 >> 16;
  2605. int src_y = plane_state->src.y1 >> 16;
  2606. int src_w = drm_rect_width(&plane_state->src) >> 16;
  2607. int src_h = drm_rect_height(&plane_state->src) >> 16;
  2608. int dst_x = plane_state->dst.x1;
  2609. int dst_y = plane_state->dst.y1;
  2610. int dst_w = drm_rect_width(&plane_state->dst);
  2611. int dst_h = drm_rect_height(&plane_state->dst);
  2612. plane_ctl = PLANE_CTL_ENABLE |
  2613. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2614. PLANE_CTL_PIPE_CSC_ENABLE;
  2615. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2616. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2617. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2618. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2619. stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2620. fb->pixel_format);
  2621. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2622. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2623. if (intel_rotation_90_or_270(rotation)) {
  2624. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2625. /* stride = Surface height in tiles */
  2626. tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2627. stride = DIV_ROUND_UP(fb->height, tile_height);
  2628. x_offset = stride * tile_height - src_y - src_h;
  2629. y_offset = src_x;
  2630. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2631. } else {
  2632. stride = fb->pitches[0] / stride_div;
  2633. x_offset = src_x;
  2634. y_offset = src_y;
  2635. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2636. }
  2637. plane_offset = y_offset << 16 | x_offset;
  2638. intel_crtc->adjusted_x = x_offset;
  2639. intel_crtc->adjusted_y = y_offset;
  2640. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2641. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2642. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2643. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2644. if (scaler_id >= 0) {
  2645. uint32_t ps_ctrl = 0;
  2646. WARN_ON(!dst_w || !dst_h);
  2647. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2648. crtc_state->scaler_state.scalers[scaler_id].mode;
  2649. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2650. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2651. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2652. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2653. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2654. } else {
  2655. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2656. }
  2657. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2658. POSTING_READ(PLANE_SURF(pipe, 0));
  2659. }
  2660. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2661. struct drm_crtc *crtc)
  2662. {
  2663. struct drm_device *dev = crtc->dev;
  2664. struct drm_i915_private *dev_priv = dev->dev_private;
  2665. int pipe = to_intel_crtc(crtc)->pipe;
  2666. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2667. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2668. POSTING_READ(PLANE_SURF(pipe, 0));
  2669. }
  2670. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2671. static int
  2672. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2673. int x, int y, enum mode_set_atomic state)
  2674. {
  2675. /* Support for kgdboc is disabled, this needs a major rework. */
  2676. DRM_ERROR("legacy panic handler not supported any more.\n");
  2677. return -ENODEV;
  2678. }
  2679. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2680. {
  2681. struct intel_crtc *crtc;
  2682. for_each_intel_crtc(dev_priv->dev, crtc)
  2683. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2684. }
  2685. static void intel_update_primary_planes(struct drm_device *dev)
  2686. {
  2687. struct drm_crtc *crtc;
  2688. for_each_crtc(dev, crtc) {
  2689. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2690. struct intel_plane_state *plane_state;
  2691. drm_modeset_lock_crtc(crtc, &plane->base);
  2692. plane_state = to_intel_plane_state(plane->base.state);
  2693. if (plane_state->visible)
  2694. plane->update_plane(&plane->base,
  2695. to_intel_crtc_state(crtc->state),
  2696. plane_state);
  2697. drm_modeset_unlock_crtc(crtc);
  2698. }
  2699. }
  2700. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  2701. {
  2702. /* no reset support for gen2 */
  2703. if (IS_GEN2(dev_priv))
  2704. return;
  2705. /* reset doesn't touch the display */
  2706. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  2707. return;
  2708. drm_modeset_lock_all(dev_priv->dev);
  2709. /*
  2710. * Disabling the crtcs gracefully seems nicer. Also the
  2711. * g33 docs say we should at least disable all the planes.
  2712. */
  2713. intel_display_suspend(dev_priv->dev);
  2714. }
  2715. void intel_finish_reset(struct drm_i915_private *dev_priv)
  2716. {
  2717. /*
  2718. * Flips in the rings will be nuked by the reset,
  2719. * so complete all pending flips so that user space
  2720. * will get its events and not get stuck.
  2721. */
  2722. intel_complete_page_flips(dev_priv);
  2723. /* no reset support for gen2 */
  2724. if (IS_GEN2(dev_priv))
  2725. return;
  2726. /* reset doesn't touch the display */
  2727. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
  2728. /*
  2729. * Flips in the rings have been nuked by the reset,
  2730. * so update the base address of all primary
  2731. * planes to the the last fb to make sure we're
  2732. * showing the correct fb after a reset.
  2733. *
  2734. * FIXME: Atomic will make this obsolete since we won't schedule
  2735. * CS-based flips (which might get lost in gpu resets) any more.
  2736. */
  2737. intel_update_primary_planes(dev_priv->dev);
  2738. return;
  2739. }
  2740. /*
  2741. * The display has been reset as well,
  2742. * so need a full re-initialization.
  2743. */
  2744. intel_runtime_pm_disable_interrupts(dev_priv);
  2745. intel_runtime_pm_enable_interrupts(dev_priv);
  2746. intel_modeset_init_hw(dev_priv->dev);
  2747. spin_lock_irq(&dev_priv->irq_lock);
  2748. if (dev_priv->display.hpd_irq_setup)
  2749. dev_priv->display.hpd_irq_setup(dev_priv);
  2750. spin_unlock_irq(&dev_priv->irq_lock);
  2751. intel_display_resume(dev_priv->dev);
  2752. intel_hpd_init(dev_priv);
  2753. drm_modeset_unlock_all(dev_priv->dev);
  2754. }
  2755. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2756. {
  2757. struct drm_device *dev = crtc->dev;
  2758. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2759. unsigned reset_counter;
  2760. bool pending;
  2761. reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
  2762. if (intel_crtc->reset_counter != reset_counter)
  2763. return false;
  2764. spin_lock_irq(&dev->event_lock);
  2765. pending = to_intel_crtc(crtc)->flip_work != NULL;
  2766. spin_unlock_irq(&dev->event_lock);
  2767. return pending;
  2768. }
  2769. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2770. struct intel_crtc_state *old_crtc_state)
  2771. {
  2772. struct drm_device *dev = crtc->base.dev;
  2773. struct drm_i915_private *dev_priv = dev->dev_private;
  2774. struct intel_crtc_state *pipe_config =
  2775. to_intel_crtc_state(crtc->base.state);
  2776. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2777. crtc->base.mode = crtc->base.state->mode;
  2778. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2779. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2780. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2781. /*
  2782. * Update pipe size and adjust fitter if needed: the reason for this is
  2783. * that in compute_mode_changes we check the native mode (not the pfit
  2784. * mode) to see if we can flip rather than do a full mode set. In the
  2785. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2786. * pfit state, we'll end up with a big fb scanned out into the wrong
  2787. * sized surface.
  2788. */
  2789. I915_WRITE(PIPESRC(crtc->pipe),
  2790. ((pipe_config->pipe_src_w - 1) << 16) |
  2791. (pipe_config->pipe_src_h - 1));
  2792. /* on skylake this is done by detaching scalers */
  2793. if (INTEL_INFO(dev)->gen >= 9) {
  2794. skl_detach_scalers(crtc);
  2795. if (pipe_config->pch_pfit.enabled)
  2796. skylake_pfit_enable(crtc);
  2797. } else if (HAS_PCH_SPLIT(dev)) {
  2798. if (pipe_config->pch_pfit.enabled)
  2799. ironlake_pfit_enable(crtc);
  2800. else if (old_crtc_state->pch_pfit.enabled)
  2801. ironlake_pfit_disable(crtc, true);
  2802. }
  2803. }
  2804. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2805. {
  2806. struct drm_device *dev = crtc->dev;
  2807. struct drm_i915_private *dev_priv = dev->dev_private;
  2808. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2809. int pipe = intel_crtc->pipe;
  2810. i915_reg_t reg;
  2811. u32 temp;
  2812. /* enable normal train */
  2813. reg = FDI_TX_CTL(pipe);
  2814. temp = I915_READ(reg);
  2815. if (IS_IVYBRIDGE(dev)) {
  2816. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2817. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2818. } else {
  2819. temp &= ~FDI_LINK_TRAIN_NONE;
  2820. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2821. }
  2822. I915_WRITE(reg, temp);
  2823. reg = FDI_RX_CTL(pipe);
  2824. temp = I915_READ(reg);
  2825. if (HAS_PCH_CPT(dev)) {
  2826. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2827. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2828. } else {
  2829. temp &= ~FDI_LINK_TRAIN_NONE;
  2830. temp |= FDI_LINK_TRAIN_NONE;
  2831. }
  2832. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2833. /* wait one idle pattern time */
  2834. POSTING_READ(reg);
  2835. udelay(1000);
  2836. /* IVB wants error correction enabled */
  2837. if (IS_IVYBRIDGE(dev))
  2838. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2839. FDI_FE_ERRC_ENABLE);
  2840. }
  2841. /* The FDI link training functions for ILK/Ibexpeak. */
  2842. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2843. {
  2844. struct drm_device *dev = crtc->dev;
  2845. struct drm_i915_private *dev_priv = dev->dev_private;
  2846. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2847. int pipe = intel_crtc->pipe;
  2848. i915_reg_t reg;
  2849. u32 temp, tries;
  2850. /* FDI needs bits from pipe first */
  2851. assert_pipe_enabled(dev_priv, pipe);
  2852. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2853. for train result */
  2854. reg = FDI_RX_IMR(pipe);
  2855. temp = I915_READ(reg);
  2856. temp &= ~FDI_RX_SYMBOL_LOCK;
  2857. temp &= ~FDI_RX_BIT_LOCK;
  2858. I915_WRITE(reg, temp);
  2859. I915_READ(reg);
  2860. udelay(150);
  2861. /* enable CPU FDI TX and PCH FDI RX */
  2862. reg = FDI_TX_CTL(pipe);
  2863. temp = I915_READ(reg);
  2864. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2865. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2866. temp &= ~FDI_LINK_TRAIN_NONE;
  2867. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2868. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2869. reg = FDI_RX_CTL(pipe);
  2870. temp = I915_READ(reg);
  2871. temp &= ~FDI_LINK_TRAIN_NONE;
  2872. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2873. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2874. POSTING_READ(reg);
  2875. udelay(150);
  2876. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2877. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2878. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2879. FDI_RX_PHASE_SYNC_POINTER_EN);
  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_BIT_LOCK)) {
  2885. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2886. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2887. break;
  2888. }
  2889. }
  2890. if (tries == 5)
  2891. DRM_ERROR("FDI train 1 fail!\n");
  2892. /* Train 2 */
  2893. reg = FDI_TX_CTL(pipe);
  2894. temp = I915_READ(reg);
  2895. temp &= ~FDI_LINK_TRAIN_NONE;
  2896. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2897. I915_WRITE(reg, temp);
  2898. reg = FDI_RX_CTL(pipe);
  2899. temp = I915_READ(reg);
  2900. temp &= ~FDI_LINK_TRAIN_NONE;
  2901. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2902. I915_WRITE(reg, temp);
  2903. POSTING_READ(reg);
  2904. udelay(150);
  2905. reg = FDI_RX_IIR(pipe);
  2906. for (tries = 0; tries < 5; tries++) {
  2907. temp = I915_READ(reg);
  2908. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2909. if (temp & FDI_RX_SYMBOL_LOCK) {
  2910. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2911. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2912. break;
  2913. }
  2914. }
  2915. if (tries == 5)
  2916. DRM_ERROR("FDI train 2 fail!\n");
  2917. DRM_DEBUG_KMS("FDI train done\n");
  2918. }
  2919. static const int snb_b_fdi_train_param[] = {
  2920. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2921. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2922. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2923. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2924. };
  2925. /* The FDI link training functions for SNB/Cougarpoint. */
  2926. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2927. {
  2928. struct drm_device *dev = crtc->dev;
  2929. struct drm_i915_private *dev_priv = dev->dev_private;
  2930. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2931. int pipe = intel_crtc->pipe;
  2932. i915_reg_t reg;
  2933. u32 temp, i, retry;
  2934. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2935. for train result */
  2936. reg = FDI_RX_IMR(pipe);
  2937. temp = I915_READ(reg);
  2938. temp &= ~FDI_RX_SYMBOL_LOCK;
  2939. temp &= ~FDI_RX_BIT_LOCK;
  2940. I915_WRITE(reg, temp);
  2941. POSTING_READ(reg);
  2942. udelay(150);
  2943. /* enable CPU FDI TX and PCH FDI RX */
  2944. reg = FDI_TX_CTL(pipe);
  2945. temp = I915_READ(reg);
  2946. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2947. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2948. temp &= ~FDI_LINK_TRAIN_NONE;
  2949. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2950. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2951. /* SNB-B */
  2952. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2953. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2954. I915_WRITE(FDI_RX_MISC(pipe),
  2955. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2956. reg = FDI_RX_CTL(pipe);
  2957. temp = I915_READ(reg);
  2958. if (HAS_PCH_CPT(dev)) {
  2959. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2960. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2961. } else {
  2962. temp &= ~FDI_LINK_TRAIN_NONE;
  2963. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2964. }
  2965. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2966. POSTING_READ(reg);
  2967. udelay(150);
  2968. for (i = 0; i < 4; i++) {
  2969. reg = FDI_TX_CTL(pipe);
  2970. temp = I915_READ(reg);
  2971. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2972. temp |= snb_b_fdi_train_param[i];
  2973. I915_WRITE(reg, temp);
  2974. POSTING_READ(reg);
  2975. udelay(500);
  2976. for (retry = 0; retry < 5; retry++) {
  2977. reg = FDI_RX_IIR(pipe);
  2978. temp = I915_READ(reg);
  2979. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2980. if (temp & FDI_RX_BIT_LOCK) {
  2981. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2982. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2983. break;
  2984. }
  2985. udelay(50);
  2986. }
  2987. if (retry < 5)
  2988. break;
  2989. }
  2990. if (i == 4)
  2991. DRM_ERROR("FDI train 1 fail!\n");
  2992. /* Train 2 */
  2993. reg = FDI_TX_CTL(pipe);
  2994. temp = I915_READ(reg);
  2995. temp &= ~FDI_LINK_TRAIN_NONE;
  2996. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2997. if (IS_GEN6(dev)) {
  2998. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2999. /* SNB-B */
  3000. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3001. }
  3002. I915_WRITE(reg, temp);
  3003. reg = FDI_RX_CTL(pipe);
  3004. temp = I915_READ(reg);
  3005. if (HAS_PCH_CPT(dev)) {
  3006. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3007. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3008. } else {
  3009. temp &= ~FDI_LINK_TRAIN_NONE;
  3010. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3011. }
  3012. I915_WRITE(reg, temp);
  3013. POSTING_READ(reg);
  3014. udelay(150);
  3015. for (i = 0; i < 4; i++) {
  3016. reg = FDI_TX_CTL(pipe);
  3017. temp = I915_READ(reg);
  3018. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3019. temp |= snb_b_fdi_train_param[i];
  3020. I915_WRITE(reg, temp);
  3021. POSTING_READ(reg);
  3022. udelay(500);
  3023. for (retry = 0; retry < 5; retry++) {
  3024. reg = FDI_RX_IIR(pipe);
  3025. temp = I915_READ(reg);
  3026. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3027. if (temp & FDI_RX_SYMBOL_LOCK) {
  3028. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3029. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3030. break;
  3031. }
  3032. udelay(50);
  3033. }
  3034. if (retry < 5)
  3035. break;
  3036. }
  3037. if (i == 4)
  3038. DRM_ERROR("FDI train 2 fail!\n");
  3039. DRM_DEBUG_KMS("FDI train done.\n");
  3040. }
  3041. /* Manual link training for Ivy Bridge A0 parts */
  3042. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3043. {
  3044. struct drm_device *dev = crtc->dev;
  3045. struct drm_i915_private *dev_priv = dev->dev_private;
  3046. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3047. int pipe = intel_crtc->pipe;
  3048. i915_reg_t reg;
  3049. u32 temp, i, j;
  3050. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3051. for train result */
  3052. reg = FDI_RX_IMR(pipe);
  3053. temp = I915_READ(reg);
  3054. temp &= ~FDI_RX_SYMBOL_LOCK;
  3055. temp &= ~FDI_RX_BIT_LOCK;
  3056. I915_WRITE(reg, temp);
  3057. POSTING_READ(reg);
  3058. udelay(150);
  3059. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3060. I915_READ(FDI_RX_IIR(pipe)));
  3061. /* Try each vswing and preemphasis setting twice before moving on */
  3062. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3063. /* disable first in case we need to retry */
  3064. reg = FDI_TX_CTL(pipe);
  3065. temp = I915_READ(reg);
  3066. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3067. temp &= ~FDI_TX_ENABLE;
  3068. I915_WRITE(reg, temp);
  3069. reg = FDI_RX_CTL(pipe);
  3070. temp = I915_READ(reg);
  3071. temp &= ~FDI_LINK_TRAIN_AUTO;
  3072. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3073. temp &= ~FDI_RX_ENABLE;
  3074. I915_WRITE(reg, temp);
  3075. /* enable CPU FDI TX and PCH FDI RX */
  3076. reg = FDI_TX_CTL(pipe);
  3077. temp = I915_READ(reg);
  3078. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3079. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3080. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3081. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3082. temp |= snb_b_fdi_train_param[j/2];
  3083. temp |= FDI_COMPOSITE_SYNC;
  3084. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3085. I915_WRITE(FDI_RX_MISC(pipe),
  3086. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3087. reg = FDI_RX_CTL(pipe);
  3088. temp = I915_READ(reg);
  3089. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3090. temp |= FDI_COMPOSITE_SYNC;
  3091. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3092. POSTING_READ(reg);
  3093. udelay(1); /* should be 0.5us */
  3094. for (i = 0; i < 4; i++) {
  3095. reg = FDI_RX_IIR(pipe);
  3096. temp = I915_READ(reg);
  3097. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3098. if (temp & FDI_RX_BIT_LOCK ||
  3099. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3100. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3101. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3102. i);
  3103. break;
  3104. }
  3105. udelay(1); /* should be 0.5us */
  3106. }
  3107. if (i == 4) {
  3108. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3109. continue;
  3110. }
  3111. /* Train 2 */
  3112. reg = FDI_TX_CTL(pipe);
  3113. temp = I915_READ(reg);
  3114. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3115. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3116. I915_WRITE(reg, temp);
  3117. reg = FDI_RX_CTL(pipe);
  3118. temp = I915_READ(reg);
  3119. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3120. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3121. I915_WRITE(reg, temp);
  3122. POSTING_READ(reg);
  3123. udelay(2); /* should be 1.5us */
  3124. for (i = 0; i < 4; i++) {
  3125. reg = FDI_RX_IIR(pipe);
  3126. temp = I915_READ(reg);
  3127. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3128. if (temp & FDI_RX_SYMBOL_LOCK ||
  3129. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3130. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3131. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3132. i);
  3133. goto train_done;
  3134. }
  3135. udelay(2); /* should be 1.5us */
  3136. }
  3137. if (i == 4)
  3138. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3139. }
  3140. train_done:
  3141. DRM_DEBUG_KMS("FDI train done.\n");
  3142. }
  3143. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3144. {
  3145. struct drm_device *dev = intel_crtc->base.dev;
  3146. struct drm_i915_private *dev_priv = dev->dev_private;
  3147. int pipe = intel_crtc->pipe;
  3148. i915_reg_t reg;
  3149. u32 temp;
  3150. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3151. reg = FDI_RX_CTL(pipe);
  3152. temp = I915_READ(reg);
  3153. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3154. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3155. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3156. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3157. POSTING_READ(reg);
  3158. udelay(200);
  3159. /* Switch from Rawclk to PCDclk */
  3160. temp = I915_READ(reg);
  3161. I915_WRITE(reg, temp | FDI_PCDCLK);
  3162. POSTING_READ(reg);
  3163. udelay(200);
  3164. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3165. reg = FDI_TX_CTL(pipe);
  3166. temp = I915_READ(reg);
  3167. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3168. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3169. POSTING_READ(reg);
  3170. udelay(100);
  3171. }
  3172. }
  3173. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3174. {
  3175. struct drm_device *dev = intel_crtc->base.dev;
  3176. struct drm_i915_private *dev_priv = dev->dev_private;
  3177. int pipe = intel_crtc->pipe;
  3178. i915_reg_t reg;
  3179. u32 temp;
  3180. /* Switch from PCDclk to Rawclk */
  3181. reg = FDI_RX_CTL(pipe);
  3182. temp = I915_READ(reg);
  3183. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3184. /* Disable CPU FDI TX PLL */
  3185. reg = FDI_TX_CTL(pipe);
  3186. temp = I915_READ(reg);
  3187. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3188. POSTING_READ(reg);
  3189. udelay(100);
  3190. reg = FDI_RX_CTL(pipe);
  3191. temp = I915_READ(reg);
  3192. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3193. /* Wait for the clocks to turn off. */
  3194. POSTING_READ(reg);
  3195. udelay(100);
  3196. }
  3197. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3198. {
  3199. struct drm_device *dev = crtc->dev;
  3200. struct drm_i915_private *dev_priv = dev->dev_private;
  3201. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3202. int pipe = intel_crtc->pipe;
  3203. i915_reg_t reg;
  3204. u32 temp;
  3205. /* disable CPU FDI tx and PCH FDI rx */
  3206. reg = FDI_TX_CTL(pipe);
  3207. temp = I915_READ(reg);
  3208. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3209. POSTING_READ(reg);
  3210. reg = FDI_RX_CTL(pipe);
  3211. temp = I915_READ(reg);
  3212. temp &= ~(0x7 << 16);
  3213. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3214. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3215. POSTING_READ(reg);
  3216. udelay(100);
  3217. /* Ironlake workaround, disable clock pointer after downing FDI */
  3218. if (HAS_PCH_IBX(dev))
  3219. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3220. /* still set train pattern 1 */
  3221. reg = FDI_TX_CTL(pipe);
  3222. temp = I915_READ(reg);
  3223. temp &= ~FDI_LINK_TRAIN_NONE;
  3224. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3225. I915_WRITE(reg, temp);
  3226. reg = FDI_RX_CTL(pipe);
  3227. temp = I915_READ(reg);
  3228. if (HAS_PCH_CPT(dev)) {
  3229. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3230. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3231. } else {
  3232. temp &= ~FDI_LINK_TRAIN_NONE;
  3233. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3234. }
  3235. /* BPC in FDI rx is consistent with that in PIPECONF */
  3236. temp &= ~(0x07 << 16);
  3237. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3238. I915_WRITE(reg, temp);
  3239. POSTING_READ(reg);
  3240. udelay(100);
  3241. }
  3242. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3243. {
  3244. struct intel_crtc *crtc;
  3245. /* Note that we don't need to be called with mode_config.lock here
  3246. * as our list of CRTC objects is static for the lifetime of the
  3247. * device and so cannot disappear as we iterate. Similarly, we can
  3248. * happily treat the predicates as racy, atomic checks as userspace
  3249. * cannot claim and pin a new fb without at least acquring the
  3250. * struct_mutex and so serialising with us.
  3251. */
  3252. for_each_intel_crtc(dev, crtc) {
  3253. if (atomic_read(&crtc->unpin_work_count) == 0)
  3254. continue;
  3255. if (crtc->flip_work)
  3256. intel_wait_for_vblank(dev, crtc->pipe);
  3257. return true;
  3258. }
  3259. return false;
  3260. }
  3261. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3262. {
  3263. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3264. struct intel_flip_work *work = intel_crtc->flip_work;
  3265. intel_crtc->flip_work = NULL;
  3266. if (work->event)
  3267. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3268. drm_crtc_vblank_put(&intel_crtc->base);
  3269. wake_up_all(&dev_priv->pending_flip_queue);
  3270. queue_work(dev_priv->wq, &work->unpin_work);
  3271. trace_i915_flip_complete(intel_crtc->plane,
  3272. work->pending_flip_obj);
  3273. }
  3274. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3275. {
  3276. struct drm_device *dev = crtc->dev;
  3277. struct drm_i915_private *dev_priv = dev->dev_private;
  3278. long ret;
  3279. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3280. ret = wait_event_interruptible_timeout(
  3281. dev_priv->pending_flip_queue,
  3282. !intel_crtc_has_pending_flip(crtc),
  3283. 60*HZ);
  3284. if (ret < 0)
  3285. return ret;
  3286. if (ret == 0) {
  3287. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3288. struct intel_flip_work *work;
  3289. spin_lock_irq(&dev->event_lock);
  3290. work = intel_crtc->flip_work;
  3291. if (work && !is_mmio_work(work)) {
  3292. WARN_ONCE(1, "Removing stuck page flip\n");
  3293. page_flip_completed(intel_crtc);
  3294. }
  3295. spin_unlock_irq(&dev->event_lock);
  3296. }
  3297. return 0;
  3298. }
  3299. static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3300. {
  3301. u32 temp;
  3302. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3303. mutex_lock(&dev_priv->sb_lock);
  3304. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3305. temp |= SBI_SSCCTL_DISABLE;
  3306. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3307. mutex_unlock(&dev_priv->sb_lock);
  3308. }
  3309. /* Program iCLKIP clock to the desired frequency */
  3310. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3311. {
  3312. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3313. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3314. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3315. u32 temp;
  3316. lpt_disable_iclkip(dev_priv);
  3317. /* The iCLK virtual clock root frequency is in MHz,
  3318. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3319. * divisors, it is necessary to divide one by another, so we
  3320. * convert the virtual clock precision to KHz here for higher
  3321. * precision.
  3322. */
  3323. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3324. u32 iclk_virtual_root_freq = 172800 * 1000;
  3325. u32 iclk_pi_range = 64;
  3326. u32 desired_divisor;
  3327. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3328. clock << auxdiv);
  3329. divsel = (desired_divisor / iclk_pi_range) - 2;
  3330. phaseinc = desired_divisor % iclk_pi_range;
  3331. /*
  3332. * Near 20MHz is a corner case which is
  3333. * out of range for the 7-bit divisor
  3334. */
  3335. if (divsel <= 0x7f)
  3336. break;
  3337. }
  3338. /* This should not happen with any sane values */
  3339. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3340. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3341. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3342. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3343. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3344. clock,
  3345. auxdiv,
  3346. divsel,
  3347. phasedir,
  3348. phaseinc);
  3349. mutex_lock(&dev_priv->sb_lock);
  3350. /* Program SSCDIVINTPHASE6 */
  3351. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3352. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3353. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3354. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3355. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3356. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3357. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3358. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3359. /* Program SSCAUXDIV */
  3360. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3361. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3362. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3363. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3364. /* Enable modulator and associated divider */
  3365. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3366. temp &= ~SBI_SSCCTL_DISABLE;
  3367. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3368. mutex_unlock(&dev_priv->sb_lock);
  3369. /* Wait for initialization time */
  3370. udelay(24);
  3371. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3372. }
  3373. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3374. {
  3375. u32 divsel, phaseinc, auxdiv;
  3376. u32 iclk_virtual_root_freq = 172800 * 1000;
  3377. u32 iclk_pi_range = 64;
  3378. u32 desired_divisor;
  3379. u32 temp;
  3380. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3381. return 0;
  3382. mutex_lock(&dev_priv->sb_lock);
  3383. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3384. if (temp & SBI_SSCCTL_DISABLE) {
  3385. mutex_unlock(&dev_priv->sb_lock);
  3386. return 0;
  3387. }
  3388. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3389. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3390. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3391. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3392. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3393. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3394. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3395. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3396. mutex_unlock(&dev_priv->sb_lock);
  3397. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3398. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3399. desired_divisor << auxdiv);
  3400. }
  3401. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3402. enum pipe pch_transcoder)
  3403. {
  3404. struct drm_device *dev = crtc->base.dev;
  3405. struct drm_i915_private *dev_priv = dev->dev_private;
  3406. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3407. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3408. I915_READ(HTOTAL(cpu_transcoder)));
  3409. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3410. I915_READ(HBLANK(cpu_transcoder)));
  3411. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3412. I915_READ(HSYNC(cpu_transcoder)));
  3413. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3414. I915_READ(VTOTAL(cpu_transcoder)));
  3415. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3416. I915_READ(VBLANK(cpu_transcoder)));
  3417. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3418. I915_READ(VSYNC(cpu_transcoder)));
  3419. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3420. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3421. }
  3422. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3423. {
  3424. struct drm_i915_private *dev_priv = dev->dev_private;
  3425. uint32_t temp;
  3426. temp = I915_READ(SOUTH_CHICKEN1);
  3427. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3428. return;
  3429. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3430. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3431. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3432. if (enable)
  3433. temp |= FDI_BC_BIFURCATION_SELECT;
  3434. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3435. I915_WRITE(SOUTH_CHICKEN1, temp);
  3436. POSTING_READ(SOUTH_CHICKEN1);
  3437. }
  3438. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3439. {
  3440. struct drm_device *dev = intel_crtc->base.dev;
  3441. switch (intel_crtc->pipe) {
  3442. case PIPE_A:
  3443. break;
  3444. case PIPE_B:
  3445. if (intel_crtc->config->fdi_lanes > 2)
  3446. cpt_set_fdi_bc_bifurcation(dev, false);
  3447. else
  3448. cpt_set_fdi_bc_bifurcation(dev, true);
  3449. break;
  3450. case PIPE_C:
  3451. cpt_set_fdi_bc_bifurcation(dev, true);
  3452. break;
  3453. default:
  3454. BUG();
  3455. }
  3456. }
  3457. /* Return which DP Port should be selected for Transcoder DP control */
  3458. static enum port
  3459. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3460. {
  3461. struct drm_device *dev = crtc->dev;
  3462. struct intel_encoder *encoder;
  3463. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3464. if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
  3465. encoder->type == INTEL_OUTPUT_EDP)
  3466. return enc_to_dig_port(&encoder->base)->port;
  3467. }
  3468. return -1;
  3469. }
  3470. /*
  3471. * Enable PCH resources required for PCH ports:
  3472. * - PCH PLLs
  3473. * - FDI training & RX/TX
  3474. * - update transcoder timings
  3475. * - DP transcoding bits
  3476. * - transcoder
  3477. */
  3478. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3479. {
  3480. struct drm_device *dev = crtc->dev;
  3481. struct drm_i915_private *dev_priv = dev->dev_private;
  3482. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3483. int pipe = intel_crtc->pipe;
  3484. u32 temp;
  3485. assert_pch_transcoder_disabled(dev_priv, pipe);
  3486. if (IS_IVYBRIDGE(dev))
  3487. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3488. /* Write the TU size bits before fdi link training, so that error
  3489. * detection works. */
  3490. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3491. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3492. /* For PCH output, training FDI link */
  3493. dev_priv->display.fdi_link_train(crtc);
  3494. /* We need to program the right clock selection before writing the pixel
  3495. * mutliplier into the DPLL. */
  3496. if (HAS_PCH_CPT(dev)) {
  3497. u32 sel;
  3498. temp = I915_READ(PCH_DPLL_SEL);
  3499. temp |= TRANS_DPLL_ENABLE(pipe);
  3500. sel = TRANS_DPLLB_SEL(pipe);
  3501. if (intel_crtc->config->shared_dpll ==
  3502. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3503. temp |= sel;
  3504. else
  3505. temp &= ~sel;
  3506. I915_WRITE(PCH_DPLL_SEL, temp);
  3507. }
  3508. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3509. * transcoder, and we actually should do this to not upset any PCH
  3510. * transcoder that already use the clock when we share it.
  3511. *
  3512. * Note that enable_shared_dpll tries to do the right thing, but
  3513. * get_shared_dpll unconditionally resets the pll - we need that to have
  3514. * the right LVDS enable sequence. */
  3515. intel_enable_shared_dpll(intel_crtc);
  3516. /* set transcoder timing, panel must allow it */
  3517. assert_panel_unlocked(dev_priv, pipe);
  3518. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3519. intel_fdi_normal_train(crtc);
  3520. /* For PCH DP, enable TRANS_DP_CTL */
  3521. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3522. const struct drm_display_mode *adjusted_mode =
  3523. &intel_crtc->config->base.adjusted_mode;
  3524. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3525. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3526. temp = I915_READ(reg);
  3527. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3528. TRANS_DP_SYNC_MASK |
  3529. TRANS_DP_BPC_MASK);
  3530. temp |= TRANS_DP_OUTPUT_ENABLE;
  3531. temp |= bpc << 9; /* same format but at 11:9 */
  3532. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3533. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3534. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3535. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3536. switch (intel_trans_dp_port_sel(crtc)) {
  3537. case PORT_B:
  3538. temp |= TRANS_DP_PORT_SEL_B;
  3539. break;
  3540. case PORT_C:
  3541. temp |= TRANS_DP_PORT_SEL_C;
  3542. break;
  3543. case PORT_D:
  3544. temp |= TRANS_DP_PORT_SEL_D;
  3545. break;
  3546. default:
  3547. BUG();
  3548. }
  3549. I915_WRITE(reg, temp);
  3550. }
  3551. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3552. }
  3553. static void lpt_pch_enable(struct drm_crtc *crtc)
  3554. {
  3555. struct drm_device *dev = crtc->dev;
  3556. struct drm_i915_private *dev_priv = dev->dev_private;
  3557. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3558. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3559. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3560. lpt_program_iclkip(crtc);
  3561. /* Set transcoder timing. */
  3562. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3563. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3564. }
  3565. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3566. {
  3567. struct drm_i915_private *dev_priv = dev->dev_private;
  3568. i915_reg_t dslreg = PIPEDSL(pipe);
  3569. u32 temp;
  3570. temp = I915_READ(dslreg);
  3571. udelay(500);
  3572. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3573. if (wait_for(I915_READ(dslreg) != temp, 5))
  3574. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3575. }
  3576. }
  3577. static int
  3578. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3579. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3580. int src_w, int src_h, int dst_w, int dst_h)
  3581. {
  3582. struct intel_crtc_scaler_state *scaler_state =
  3583. &crtc_state->scaler_state;
  3584. struct intel_crtc *intel_crtc =
  3585. to_intel_crtc(crtc_state->base.crtc);
  3586. int need_scaling;
  3587. need_scaling = intel_rotation_90_or_270(rotation) ?
  3588. (src_h != dst_w || src_w != dst_h):
  3589. (src_w != dst_w || src_h != dst_h);
  3590. /*
  3591. * if plane is being disabled or scaler is no more required or force detach
  3592. * - free scaler binded to this plane/crtc
  3593. * - in order to do this, update crtc->scaler_usage
  3594. *
  3595. * Here scaler state in crtc_state is set free so that
  3596. * scaler can be assigned to other user. Actual register
  3597. * update to free the scaler is done in plane/panel-fit programming.
  3598. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3599. */
  3600. if (force_detach || !need_scaling) {
  3601. if (*scaler_id >= 0) {
  3602. scaler_state->scaler_users &= ~(1 << scaler_user);
  3603. scaler_state->scalers[*scaler_id].in_use = 0;
  3604. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3605. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3606. intel_crtc->pipe, scaler_user, *scaler_id,
  3607. scaler_state->scaler_users);
  3608. *scaler_id = -1;
  3609. }
  3610. return 0;
  3611. }
  3612. /* range checks */
  3613. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3614. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3615. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3616. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3617. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3618. "size is out of scaler range\n",
  3619. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3620. return -EINVAL;
  3621. }
  3622. /* mark this plane as a scaler user in crtc_state */
  3623. scaler_state->scaler_users |= (1 << scaler_user);
  3624. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3625. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3626. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3627. scaler_state->scaler_users);
  3628. return 0;
  3629. }
  3630. /**
  3631. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3632. *
  3633. * @state: crtc's scaler state
  3634. *
  3635. * Return
  3636. * 0 - scaler_usage updated successfully
  3637. * error - requested scaling cannot be supported or other error condition
  3638. */
  3639. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3640. {
  3641. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3642. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3643. DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
  3644. intel_crtc->base.base.id, intel_crtc->base.name,
  3645. intel_crtc->pipe, SKL_CRTC_INDEX);
  3646. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3647. &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
  3648. state->pipe_src_w, state->pipe_src_h,
  3649. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3650. }
  3651. /**
  3652. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3653. *
  3654. * @state: crtc's scaler state
  3655. * @plane_state: atomic plane state to update
  3656. *
  3657. * Return
  3658. * 0 - scaler_usage updated successfully
  3659. * error - requested scaling cannot be supported or other error condition
  3660. */
  3661. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3662. struct intel_plane_state *plane_state)
  3663. {
  3664. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3665. struct intel_plane *intel_plane =
  3666. to_intel_plane(plane_state->base.plane);
  3667. struct drm_framebuffer *fb = plane_state->base.fb;
  3668. int ret;
  3669. bool force_detach = !fb || !plane_state->visible;
  3670. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
  3671. intel_plane->base.base.id, intel_plane->base.name,
  3672. intel_crtc->pipe, drm_plane_index(&intel_plane->base));
  3673. ret = skl_update_scaler(crtc_state, force_detach,
  3674. drm_plane_index(&intel_plane->base),
  3675. &plane_state->scaler_id,
  3676. plane_state->base.rotation,
  3677. drm_rect_width(&plane_state->src) >> 16,
  3678. drm_rect_height(&plane_state->src) >> 16,
  3679. drm_rect_width(&plane_state->dst),
  3680. drm_rect_height(&plane_state->dst));
  3681. if (ret || plane_state->scaler_id < 0)
  3682. return ret;
  3683. /* check colorkey */
  3684. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3685. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  3686. intel_plane->base.base.id,
  3687. intel_plane->base.name);
  3688. return -EINVAL;
  3689. }
  3690. /* Check src format */
  3691. switch (fb->pixel_format) {
  3692. case DRM_FORMAT_RGB565:
  3693. case DRM_FORMAT_XBGR8888:
  3694. case DRM_FORMAT_XRGB8888:
  3695. case DRM_FORMAT_ABGR8888:
  3696. case DRM_FORMAT_ARGB8888:
  3697. case DRM_FORMAT_XRGB2101010:
  3698. case DRM_FORMAT_XBGR2101010:
  3699. case DRM_FORMAT_YUYV:
  3700. case DRM_FORMAT_YVYU:
  3701. case DRM_FORMAT_UYVY:
  3702. case DRM_FORMAT_VYUY:
  3703. break;
  3704. default:
  3705. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  3706. intel_plane->base.base.id, intel_plane->base.name,
  3707. fb->base.id, fb->pixel_format);
  3708. return -EINVAL;
  3709. }
  3710. return 0;
  3711. }
  3712. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3713. {
  3714. int i;
  3715. for (i = 0; i < crtc->num_scalers; i++)
  3716. skl_detach_scaler(crtc, i);
  3717. }
  3718. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3719. {
  3720. struct drm_device *dev = crtc->base.dev;
  3721. struct drm_i915_private *dev_priv = dev->dev_private;
  3722. int pipe = crtc->pipe;
  3723. struct intel_crtc_scaler_state *scaler_state =
  3724. &crtc->config->scaler_state;
  3725. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3726. if (crtc->config->pch_pfit.enabled) {
  3727. int id;
  3728. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3729. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3730. return;
  3731. }
  3732. id = scaler_state->scaler_id;
  3733. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3734. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3735. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3736. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3737. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3738. }
  3739. }
  3740. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3741. {
  3742. struct drm_device *dev = crtc->base.dev;
  3743. struct drm_i915_private *dev_priv = dev->dev_private;
  3744. int pipe = crtc->pipe;
  3745. if (crtc->config->pch_pfit.enabled) {
  3746. /* Force use of hard-coded filter coefficients
  3747. * as some pre-programmed values are broken,
  3748. * e.g. x201.
  3749. */
  3750. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3751. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3752. PF_PIPE_SEL_IVB(pipe));
  3753. else
  3754. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3755. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3756. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3757. }
  3758. }
  3759. void hsw_enable_ips(struct intel_crtc *crtc)
  3760. {
  3761. struct drm_device *dev = crtc->base.dev;
  3762. struct drm_i915_private *dev_priv = dev->dev_private;
  3763. if (!crtc->config->ips_enabled)
  3764. return;
  3765. /*
  3766. * We can only enable IPS after we enable a plane and wait for a vblank
  3767. * This function is called from post_plane_update, which is run after
  3768. * a vblank wait.
  3769. */
  3770. assert_plane_enabled(dev_priv, crtc->plane);
  3771. if (IS_BROADWELL(dev)) {
  3772. mutex_lock(&dev_priv->rps.hw_lock);
  3773. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3774. mutex_unlock(&dev_priv->rps.hw_lock);
  3775. /* Quoting Art Runyan: "its not safe to expect any particular
  3776. * value in IPS_CTL bit 31 after enabling IPS through the
  3777. * mailbox." Moreover, the mailbox may return a bogus state,
  3778. * so we need to just enable it and continue on.
  3779. */
  3780. } else {
  3781. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3782. /* The bit only becomes 1 in the next vblank, so this wait here
  3783. * is essentially intel_wait_for_vblank. If we don't have this
  3784. * and don't wait for vblanks until the end of crtc_enable, then
  3785. * the HW state readout code will complain that the expected
  3786. * IPS_CTL value is not the one we read. */
  3787. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3788. DRM_ERROR("Timed out waiting for IPS enable\n");
  3789. }
  3790. }
  3791. void hsw_disable_ips(struct intel_crtc *crtc)
  3792. {
  3793. struct drm_device *dev = crtc->base.dev;
  3794. struct drm_i915_private *dev_priv = dev->dev_private;
  3795. if (!crtc->config->ips_enabled)
  3796. return;
  3797. assert_plane_enabled(dev_priv, crtc->plane);
  3798. if (IS_BROADWELL(dev)) {
  3799. mutex_lock(&dev_priv->rps.hw_lock);
  3800. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3801. mutex_unlock(&dev_priv->rps.hw_lock);
  3802. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3803. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3804. DRM_ERROR("Timed out waiting for IPS disable\n");
  3805. } else {
  3806. I915_WRITE(IPS_CTL, 0);
  3807. POSTING_READ(IPS_CTL);
  3808. }
  3809. /* We need to wait for a vblank before we can disable the plane. */
  3810. intel_wait_for_vblank(dev, crtc->pipe);
  3811. }
  3812. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3813. {
  3814. if (intel_crtc->overlay) {
  3815. struct drm_device *dev = intel_crtc->base.dev;
  3816. struct drm_i915_private *dev_priv = dev->dev_private;
  3817. mutex_lock(&dev->struct_mutex);
  3818. dev_priv->mm.interruptible = false;
  3819. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3820. dev_priv->mm.interruptible = true;
  3821. mutex_unlock(&dev->struct_mutex);
  3822. }
  3823. /* Let userspace switch the overlay on again. In most cases userspace
  3824. * has to recompute where to put it anyway.
  3825. */
  3826. }
  3827. /**
  3828. * intel_post_enable_primary - Perform operations after enabling primary plane
  3829. * @crtc: the CRTC whose primary plane was just enabled
  3830. *
  3831. * Performs potentially sleeping operations that must be done after the primary
  3832. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3833. * called due to an explicit primary plane update, or due to an implicit
  3834. * re-enable that is caused when a sprite plane is updated to no longer
  3835. * completely hide the primary plane.
  3836. */
  3837. static void
  3838. intel_post_enable_primary(struct drm_crtc *crtc)
  3839. {
  3840. struct drm_device *dev = crtc->dev;
  3841. struct drm_i915_private *dev_priv = dev->dev_private;
  3842. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3843. int pipe = intel_crtc->pipe;
  3844. /*
  3845. * FIXME IPS should be fine as long as one plane is
  3846. * enabled, but in practice it seems to have problems
  3847. * when going from primary only to sprite only and vice
  3848. * versa.
  3849. */
  3850. hsw_enable_ips(intel_crtc);
  3851. /*
  3852. * Gen2 reports pipe underruns whenever all planes are disabled.
  3853. * So don't enable underrun reporting before at least some planes
  3854. * are enabled.
  3855. * FIXME: Need to fix the logic to work when we turn off all planes
  3856. * but leave the pipe running.
  3857. */
  3858. if (IS_GEN2(dev))
  3859. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3860. /* Underruns don't always raise interrupts, so check manually. */
  3861. intel_check_cpu_fifo_underruns(dev_priv);
  3862. intel_check_pch_fifo_underruns(dev_priv);
  3863. }
  3864. /* FIXME move all this to pre_plane_update() with proper state tracking */
  3865. static void
  3866. intel_pre_disable_primary(struct drm_crtc *crtc)
  3867. {
  3868. struct drm_device *dev = crtc->dev;
  3869. struct drm_i915_private *dev_priv = dev->dev_private;
  3870. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3871. int pipe = intel_crtc->pipe;
  3872. /*
  3873. * Gen2 reports pipe underruns whenever all planes are disabled.
  3874. * So diasble underrun reporting before all the planes get disabled.
  3875. * FIXME: Need to fix the logic to work when we turn off all planes
  3876. * but leave the pipe running.
  3877. */
  3878. if (IS_GEN2(dev))
  3879. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3880. /*
  3881. * FIXME IPS should be fine as long as one plane is
  3882. * enabled, but in practice it seems to have problems
  3883. * when going from primary only to sprite only and vice
  3884. * versa.
  3885. */
  3886. hsw_disable_ips(intel_crtc);
  3887. }
  3888. /* FIXME get rid of this and use pre_plane_update */
  3889. static void
  3890. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  3891. {
  3892. struct drm_device *dev = crtc->dev;
  3893. struct drm_i915_private *dev_priv = dev->dev_private;
  3894. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3895. int pipe = intel_crtc->pipe;
  3896. intel_pre_disable_primary(crtc);
  3897. /*
  3898. * Vblank time updates from the shadow to live plane control register
  3899. * are blocked if the memory self-refresh mode is active at that
  3900. * moment. So to make sure the plane gets truly disabled, disable
  3901. * first the self-refresh mode. The self-refresh enable bit in turn
  3902. * will be checked/applied by the HW only at the next frame start
  3903. * event which is after the vblank start event, so we need to have a
  3904. * wait-for-vblank between disabling the plane and the pipe.
  3905. */
  3906. if (HAS_GMCH_DISPLAY(dev)) {
  3907. intel_set_memory_cxsr(dev_priv, false);
  3908. dev_priv->wm.vlv.cxsr = false;
  3909. intel_wait_for_vblank(dev, pipe);
  3910. }
  3911. }
  3912. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  3913. {
  3914. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3915. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3916. struct intel_crtc_state *pipe_config =
  3917. to_intel_crtc_state(crtc->base.state);
  3918. struct drm_device *dev = crtc->base.dev;
  3919. struct drm_plane *primary = crtc->base.primary;
  3920. struct drm_plane_state *old_pri_state =
  3921. drm_atomic_get_existing_plane_state(old_state, primary);
  3922. intel_frontbuffer_flip(dev, pipe_config->fb_bits);
  3923. crtc->wm.cxsr_allowed = true;
  3924. if (pipe_config->update_wm_post && pipe_config->base.active)
  3925. intel_update_watermarks(&crtc->base);
  3926. if (old_pri_state) {
  3927. struct intel_plane_state *primary_state =
  3928. to_intel_plane_state(primary->state);
  3929. struct intel_plane_state *old_primary_state =
  3930. to_intel_plane_state(old_pri_state);
  3931. intel_fbc_post_update(crtc);
  3932. if (primary_state->visible &&
  3933. (needs_modeset(&pipe_config->base) ||
  3934. !old_primary_state->visible))
  3935. intel_post_enable_primary(&crtc->base);
  3936. }
  3937. }
  3938. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  3939. {
  3940. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3941. struct drm_device *dev = crtc->base.dev;
  3942. struct drm_i915_private *dev_priv = dev->dev_private;
  3943. struct intel_crtc_state *pipe_config =
  3944. to_intel_crtc_state(crtc->base.state);
  3945. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3946. struct drm_plane *primary = crtc->base.primary;
  3947. struct drm_plane_state *old_pri_state =
  3948. drm_atomic_get_existing_plane_state(old_state, primary);
  3949. bool modeset = needs_modeset(&pipe_config->base);
  3950. if (old_pri_state) {
  3951. struct intel_plane_state *primary_state =
  3952. to_intel_plane_state(primary->state);
  3953. struct intel_plane_state *old_primary_state =
  3954. to_intel_plane_state(old_pri_state);
  3955. intel_fbc_pre_update(crtc);
  3956. if (old_primary_state->visible &&
  3957. (modeset || !primary_state->visible))
  3958. intel_pre_disable_primary(&crtc->base);
  3959. }
  3960. if (pipe_config->disable_cxsr) {
  3961. crtc->wm.cxsr_allowed = false;
  3962. /*
  3963. * Vblank time updates from the shadow to live plane control register
  3964. * are blocked if the memory self-refresh mode is active at that
  3965. * moment. So to make sure the plane gets truly disabled, disable
  3966. * first the self-refresh mode. The self-refresh enable bit in turn
  3967. * will be checked/applied by the HW only at the next frame start
  3968. * event which is after the vblank start event, so we need to have a
  3969. * wait-for-vblank between disabling the plane and the pipe.
  3970. */
  3971. if (old_crtc_state->base.active) {
  3972. intel_set_memory_cxsr(dev_priv, false);
  3973. dev_priv->wm.vlv.cxsr = false;
  3974. intel_wait_for_vblank(dev, crtc->pipe);
  3975. }
  3976. }
  3977. /*
  3978. * IVB workaround: must disable low power watermarks for at least
  3979. * one frame before enabling scaling. LP watermarks can be re-enabled
  3980. * when scaling is disabled.
  3981. *
  3982. * WaCxSRDisabledForSpriteScaling:ivb
  3983. */
  3984. if (pipe_config->disable_lp_wm) {
  3985. ilk_disable_lp_wm(dev);
  3986. intel_wait_for_vblank(dev, crtc->pipe);
  3987. }
  3988. /*
  3989. * If we're doing a modeset, we're done. No need to do any pre-vblank
  3990. * watermark programming here.
  3991. */
  3992. if (needs_modeset(&pipe_config->base))
  3993. return;
  3994. /*
  3995. * For platforms that support atomic watermarks, program the
  3996. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  3997. * will be the intermediate values that are safe for both pre- and
  3998. * post- vblank; when vblank happens, the 'active' values will be set
  3999. * to the final 'target' values and we'll do this again to get the
  4000. * optimal watermarks. For gen9+ platforms, the values we program here
  4001. * will be the final target values which will get automatically latched
  4002. * at vblank time; no further programming will be necessary.
  4003. *
  4004. * If a platform hasn't been transitioned to atomic watermarks yet,
  4005. * we'll continue to update watermarks the old way, if flags tell
  4006. * us to.
  4007. */
  4008. if (dev_priv->display.initial_watermarks != NULL)
  4009. dev_priv->display.initial_watermarks(pipe_config);
  4010. else if (pipe_config->update_wm_pre)
  4011. intel_update_watermarks(&crtc->base);
  4012. }
  4013. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4014. {
  4015. struct drm_device *dev = crtc->dev;
  4016. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4017. struct drm_plane *p;
  4018. int pipe = intel_crtc->pipe;
  4019. intel_crtc_dpms_overlay_disable(intel_crtc);
  4020. drm_for_each_plane_mask(p, dev, plane_mask)
  4021. to_intel_plane(p)->disable_plane(p, crtc);
  4022. /*
  4023. * FIXME: Once we grow proper nuclear flip support out of this we need
  4024. * to compute the mask of flip planes precisely. For the time being
  4025. * consider this a flip to a NULL plane.
  4026. */
  4027. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4028. }
  4029. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4030. {
  4031. struct drm_device *dev = crtc->dev;
  4032. struct drm_i915_private *dev_priv = dev->dev_private;
  4033. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4034. struct intel_encoder *encoder;
  4035. int pipe = intel_crtc->pipe;
  4036. struct intel_crtc_state *pipe_config =
  4037. to_intel_crtc_state(crtc->state);
  4038. if (WARN_ON(intel_crtc->active))
  4039. return;
  4040. /*
  4041. * Sometimes spurious CPU pipe underruns happen during FDI
  4042. * training, at least with VGA+HDMI cloning. Suppress them.
  4043. *
  4044. * On ILK we get an occasional spurious CPU pipe underruns
  4045. * between eDP port A enable and vdd enable. Also PCH port
  4046. * enable seems to result in the occasional CPU pipe underrun.
  4047. *
  4048. * Spurious PCH underruns also occur during PCH enabling.
  4049. */
  4050. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4051. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4052. if (intel_crtc->config->has_pch_encoder)
  4053. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4054. if (intel_crtc->config->has_pch_encoder)
  4055. intel_prepare_shared_dpll(intel_crtc);
  4056. if (intel_crtc->config->has_dp_encoder)
  4057. intel_dp_set_m_n(intel_crtc, M1_N1);
  4058. intel_set_pipe_timings(intel_crtc);
  4059. intel_set_pipe_src_size(intel_crtc);
  4060. if (intel_crtc->config->has_pch_encoder) {
  4061. intel_cpu_transcoder_set_m_n(intel_crtc,
  4062. &intel_crtc->config->fdi_m_n, NULL);
  4063. }
  4064. ironlake_set_pipeconf(crtc);
  4065. intel_crtc->active = true;
  4066. for_each_encoder_on_crtc(dev, crtc, encoder)
  4067. if (encoder->pre_enable)
  4068. encoder->pre_enable(encoder);
  4069. if (intel_crtc->config->has_pch_encoder) {
  4070. /* Note: FDI PLL enabling _must_ be done before we enable the
  4071. * cpu pipes, hence this is separate from all the other fdi/pch
  4072. * enabling. */
  4073. ironlake_fdi_pll_enable(intel_crtc);
  4074. } else {
  4075. assert_fdi_tx_disabled(dev_priv, pipe);
  4076. assert_fdi_rx_disabled(dev_priv, pipe);
  4077. }
  4078. ironlake_pfit_enable(intel_crtc);
  4079. /*
  4080. * On ILK+ LUT must be loaded before the pipe is running but with
  4081. * clocks enabled
  4082. */
  4083. intel_color_load_luts(&pipe_config->base);
  4084. if (dev_priv->display.initial_watermarks != NULL)
  4085. dev_priv->display.initial_watermarks(intel_crtc->config);
  4086. intel_enable_pipe(intel_crtc);
  4087. if (intel_crtc->config->has_pch_encoder)
  4088. ironlake_pch_enable(crtc);
  4089. assert_vblank_disabled(crtc);
  4090. drm_crtc_vblank_on(crtc);
  4091. for_each_encoder_on_crtc(dev, crtc, encoder)
  4092. encoder->enable(encoder);
  4093. if (HAS_PCH_CPT(dev))
  4094. cpt_verify_modeset(dev, intel_crtc->pipe);
  4095. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4096. if (intel_crtc->config->has_pch_encoder)
  4097. intel_wait_for_vblank(dev, pipe);
  4098. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4099. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4100. }
  4101. /* IPS only exists on ULT machines and is tied to pipe A. */
  4102. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4103. {
  4104. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4105. }
  4106. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4107. {
  4108. struct drm_device *dev = crtc->dev;
  4109. struct drm_i915_private *dev_priv = dev->dev_private;
  4110. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4111. struct intel_encoder *encoder;
  4112. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4113. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4114. struct intel_crtc_state *pipe_config =
  4115. to_intel_crtc_state(crtc->state);
  4116. if (WARN_ON(intel_crtc->active))
  4117. return;
  4118. if (intel_crtc->config->has_pch_encoder)
  4119. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4120. false);
  4121. if (intel_crtc->config->shared_dpll)
  4122. intel_enable_shared_dpll(intel_crtc);
  4123. if (intel_crtc->config->has_dp_encoder)
  4124. intel_dp_set_m_n(intel_crtc, M1_N1);
  4125. if (!intel_crtc->config->has_dsi_encoder)
  4126. intel_set_pipe_timings(intel_crtc);
  4127. intel_set_pipe_src_size(intel_crtc);
  4128. if (cpu_transcoder != TRANSCODER_EDP &&
  4129. !transcoder_is_dsi(cpu_transcoder)) {
  4130. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4131. intel_crtc->config->pixel_multiplier - 1);
  4132. }
  4133. if (intel_crtc->config->has_pch_encoder) {
  4134. intel_cpu_transcoder_set_m_n(intel_crtc,
  4135. &intel_crtc->config->fdi_m_n, NULL);
  4136. }
  4137. if (!intel_crtc->config->has_dsi_encoder)
  4138. haswell_set_pipeconf(crtc);
  4139. haswell_set_pipemisc(crtc);
  4140. intel_color_set_csc(&pipe_config->base);
  4141. intel_crtc->active = true;
  4142. if (intel_crtc->config->has_pch_encoder)
  4143. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4144. else
  4145. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4146. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4147. if (encoder->pre_enable)
  4148. encoder->pre_enable(encoder);
  4149. }
  4150. if (intel_crtc->config->has_pch_encoder)
  4151. dev_priv->display.fdi_link_train(crtc);
  4152. if (!intel_crtc->config->has_dsi_encoder)
  4153. intel_ddi_enable_pipe_clock(intel_crtc);
  4154. if (INTEL_INFO(dev)->gen >= 9)
  4155. skylake_pfit_enable(intel_crtc);
  4156. else
  4157. ironlake_pfit_enable(intel_crtc);
  4158. /*
  4159. * On ILK+ LUT must be loaded before the pipe is running but with
  4160. * clocks enabled
  4161. */
  4162. intel_color_load_luts(&pipe_config->base);
  4163. intel_ddi_set_pipe_settings(crtc);
  4164. if (!intel_crtc->config->has_dsi_encoder)
  4165. intel_ddi_enable_transcoder_func(crtc);
  4166. if (dev_priv->display.initial_watermarks != NULL)
  4167. dev_priv->display.initial_watermarks(pipe_config);
  4168. else
  4169. intel_update_watermarks(crtc);
  4170. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4171. if (!intel_crtc->config->has_dsi_encoder)
  4172. intel_enable_pipe(intel_crtc);
  4173. if (intel_crtc->config->has_pch_encoder)
  4174. lpt_pch_enable(crtc);
  4175. if (intel_crtc->config->dp_encoder_is_mst)
  4176. intel_ddi_set_vc_payload_alloc(crtc, true);
  4177. assert_vblank_disabled(crtc);
  4178. drm_crtc_vblank_on(crtc);
  4179. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4180. encoder->enable(encoder);
  4181. intel_opregion_notify_encoder(encoder, true);
  4182. }
  4183. if (intel_crtc->config->has_pch_encoder) {
  4184. intel_wait_for_vblank(dev, pipe);
  4185. intel_wait_for_vblank(dev, pipe);
  4186. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4187. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4188. true);
  4189. }
  4190. /* If we change the relative order between pipe/planes enabling, we need
  4191. * to change the workaround. */
  4192. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4193. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4194. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4195. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4196. }
  4197. }
  4198. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4199. {
  4200. struct drm_device *dev = crtc->base.dev;
  4201. struct drm_i915_private *dev_priv = dev->dev_private;
  4202. int pipe = crtc->pipe;
  4203. /* To avoid upsetting the power well on haswell only disable the pfit if
  4204. * it's in use. The hw state code will make sure we get this right. */
  4205. if (force || crtc->config->pch_pfit.enabled) {
  4206. I915_WRITE(PF_CTL(pipe), 0);
  4207. I915_WRITE(PF_WIN_POS(pipe), 0);
  4208. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4209. }
  4210. }
  4211. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4212. {
  4213. struct drm_device *dev = crtc->dev;
  4214. struct drm_i915_private *dev_priv = dev->dev_private;
  4215. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4216. struct intel_encoder *encoder;
  4217. int pipe = intel_crtc->pipe;
  4218. /*
  4219. * Sometimes spurious CPU pipe underruns happen when the
  4220. * pipe is already disabled, but FDI RX/TX is still enabled.
  4221. * Happens at least with VGA+HDMI cloning. Suppress them.
  4222. */
  4223. if (intel_crtc->config->has_pch_encoder) {
  4224. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4225. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4226. }
  4227. for_each_encoder_on_crtc(dev, crtc, encoder)
  4228. encoder->disable(encoder);
  4229. drm_crtc_vblank_off(crtc);
  4230. assert_vblank_disabled(crtc);
  4231. intel_disable_pipe(intel_crtc);
  4232. ironlake_pfit_disable(intel_crtc, false);
  4233. if (intel_crtc->config->has_pch_encoder)
  4234. ironlake_fdi_disable(crtc);
  4235. for_each_encoder_on_crtc(dev, crtc, encoder)
  4236. if (encoder->post_disable)
  4237. encoder->post_disable(encoder);
  4238. if (intel_crtc->config->has_pch_encoder) {
  4239. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4240. if (HAS_PCH_CPT(dev)) {
  4241. i915_reg_t reg;
  4242. u32 temp;
  4243. /* disable TRANS_DP_CTL */
  4244. reg = TRANS_DP_CTL(pipe);
  4245. temp = I915_READ(reg);
  4246. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4247. TRANS_DP_PORT_SEL_MASK);
  4248. temp |= TRANS_DP_PORT_SEL_NONE;
  4249. I915_WRITE(reg, temp);
  4250. /* disable DPLL_SEL */
  4251. temp = I915_READ(PCH_DPLL_SEL);
  4252. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4253. I915_WRITE(PCH_DPLL_SEL, temp);
  4254. }
  4255. ironlake_fdi_pll_disable(intel_crtc);
  4256. }
  4257. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4258. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4259. }
  4260. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4261. {
  4262. struct drm_device *dev = crtc->dev;
  4263. struct drm_i915_private *dev_priv = dev->dev_private;
  4264. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4265. struct intel_encoder *encoder;
  4266. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4267. if (intel_crtc->config->has_pch_encoder)
  4268. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4269. false);
  4270. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4271. intel_opregion_notify_encoder(encoder, false);
  4272. encoder->disable(encoder);
  4273. }
  4274. drm_crtc_vblank_off(crtc);
  4275. assert_vblank_disabled(crtc);
  4276. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4277. if (!intel_crtc->config->has_dsi_encoder)
  4278. intel_disable_pipe(intel_crtc);
  4279. if (intel_crtc->config->dp_encoder_is_mst)
  4280. intel_ddi_set_vc_payload_alloc(crtc, false);
  4281. if (!intel_crtc->config->has_dsi_encoder)
  4282. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4283. if (INTEL_INFO(dev)->gen >= 9)
  4284. skylake_scaler_disable(intel_crtc);
  4285. else
  4286. ironlake_pfit_disable(intel_crtc, false);
  4287. if (!intel_crtc->config->has_dsi_encoder)
  4288. intel_ddi_disable_pipe_clock(intel_crtc);
  4289. for_each_encoder_on_crtc(dev, crtc, encoder)
  4290. if (encoder->post_disable)
  4291. encoder->post_disable(encoder);
  4292. if (intel_crtc->config->has_pch_encoder) {
  4293. lpt_disable_pch_transcoder(dev_priv);
  4294. lpt_disable_iclkip(dev_priv);
  4295. intel_ddi_fdi_disable(crtc);
  4296. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4297. true);
  4298. }
  4299. }
  4300. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4301. {
  4302. struct drm_device *dev = crtc->base.dev;
  4303. struct drm_i915_private *dev_priv = dev->dev_private;
  4304. struct intel_crtc_state *pipe_config = crtc->config;
  4305. if (!pipe_config->gmch_pfit.control)
  4306. return;
  4307. /*
  4308. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4309. * according to register description and PRM.
  4310. */
  4311. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4312. assert_pipe_disabled(dev_priv, crtc->pipe);
  4313. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4314. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4315. /* Border color in case we don't scale up to the full screen. Black by
  4316. * default, change to something else for debugging. */
  4317. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4318. }
  4319. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4320. {
  4321. switch (port) {
  4322. case PORT_A:
  4323. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4324. case PORT_B:
  4325. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4326. case PORT_C:
  4327. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4328. case PORT_D:
  4329. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4330. case PORT_E:
  4331. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4332. default:
  4333. MISSING_CASE(port);
  4334. return POWER_DOMAIN_PORT_OTHER;
  4335. }
  4336. }
  4337. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4338. {
  4339. switch (port) {
  4340. case PORT_A:
  4341. return POWER_DOMAIN_AUX_A;
  4342. case PORT_B:
  4343. return POWER_DOMAIN_AUX_B;
  4344. case PORT_C:
  4345. return POWER_DOMAIN_AUX_C;
  4346. case PORT_D:
  4347. return POWER_DOMAIN_AUX_D;
  4348. case PORT_E:
  4349. /* FIXME: Check VBT for actual wiring of PORT E */
  4350. return POWER_DOMAIN_AUX_D;
  4351. default:
  4352. MISSING_CASE(port);
  4353. return POWER_DOMAIN_AUX_A;
  4354. }
  4355. }
  4356. enum intel_display_power_domain
  4357. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4358. {
  4359. struct drm_device *dev = intel_encoder->base.dev;
  4360. struct intel_digital_port *intel_dig_port;
  4361. switch (intel_encoder->type) {
  4362. case INTEL_OUTPUT_UNKNOWN:
  4363. /* Only DDI platforms should ever use this output type */
  4364. WARN_ON_ONCE(!HAS_DDI(dev));
  4365. case INTEL_OUTPUT_DISPLAYPORT:
  4366. case INTEL_OUTPUT_HDMI:
  4367. case INTEL_OUTPUT_EDP:
  4368. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4369. return port_to_power_domain(intel_dig_port->port);
  4370. case INTEL_OUTPUT_DP_MST:
  4371. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4372. return port_to_power_domain(intel_dig_port->port);
  4373. case INTEL_OUTPUT_ANALOG:
  4374. return POWER_DOMAIN_PORT_CRT;
  4375. case INTEL_OUTPUT_DSI:
  4376. return POWER_DOMAIN_PORT_DSI;
  4377. default:
  4378. return POWER_DOMAIN_PORT_OTHER;
  4379. }
  4380. }
  4381. enum intel_display_power_domain
  4382. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4383. {
  4384. struct drm_device *dev = intel_encoder->base.dev;
  4385. struct intel_digital_port *intel_dig_port;
  4386. switch (intel_encoder->type) {
  4387. case INTEL_OUTPUT_UNKNOWN:
  4388. case INTEL_OUTPUT_HDMI:
  4389. /*
  4390. * Only DDI platforms should ever use these output types.
  4391. * We can get here after the HDMI detect code has already set
  4392. * the type of the shared encoder. Since we can't be sure
  4393. * what's the status of the given connectors, play safe and
  4394. * run the DP detection too.
  4395. */
  4396. WARN_ON_ONCE(!HAS_DDI(dev));
  4397. case INTEL_OUTPUT_DISPLAYPORT:
  4398. case INTEL_OUTPUT_EDP:
  4399. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4400. return port_to_aux_power_domain(intel_dig_port->port);
  4401. case INTEL_OUTPUT_DP_MST:
  4402. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4403. return port_to_aux_power_domain(intel_dig_port->port);
  4404. default:
  4405. MISSING_CASE(intel_encoder->type);
  4406. return POWER_DOMAIN_AUX_A;
  4407. }
  4408. }
  4409. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4410. struct intel_crtc_state *crtc_state)
  4411. {
  4412. struct drm_device *dev = crtc->dev;
  4413. struct drm_encoder *encoder;
  4414. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4415. enum pipe pipe = intel_crtc->pipe;
  4416. unsigned long mask;
  4417. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4418. if (!crtc_state->base.active)
  4419. return 0;
  4420. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4421. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4422. if (crtc_state->pch_pfit.enabled ||
  4423. crtc_state->pch_pfit.force_thru)
  4424. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4425. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4426. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4427. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4428. }
  4429. if (crtc_state->shared_dpll)
  4430. mask |= BIT(POWER_DOMAIN_PLLS);
  4431. return mask;
  4432. }
  4433. static unsigned long
  4434. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4435. struct intel_crtc_state *crtc_state)
  4436. {
  4437. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4438. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4439. enum intel_display_power_domain domain;
  4440. unsigned long domains, new_domains, old_domains;
  4441. old_domains = intel_crtc->enabled_power_domains;
  4442. intel_crtc->enabled_power_domains = new_domains =
  4443. get_crtc_power_domains(crtc, crtc_state);
  4444. domains = new_domains & ~old_domains;
  4445. for_each_power_domain(domain, domains)
  4446. intel_display_power_get(dev_priv, domain);
  4447. return old_domains & ~new_domains;
  4448. }
  4449. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4450. unsigned long domains)
  4451. {
  4452. enum intel_display_power_domain domain;
  4453. for_each_power_domain(domain, domains)
  4454. intel_display_power_put(dev_priv, domain);
  4455. }
  4456. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4457. {
  4458. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4459. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4460. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4461. return max_cdclk_freq;
  4462. else if (IS_CHERRYVIEW(dev_priv))
  4463. return max_cdclk_freq*95/100;
  4464. else if (INTEL_INFO(dev_priv)->gen < 4)
  4465. return 2*max_cdclk_freq*90/100;
  4466. else
  4467. return max_cdclk_freq*90/100;
  4468. }
  4469. static int skl_calc_cdclk(int max_pixclk, int vco);
  4470. static void intel_update_max_cdclk(struct drm_device *dev)
  4471. {
  4472. struct drm_i915_private *dev_priv = dev->dev_private;
  4473. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4474. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4475. int max_cdclk, vco;
  4476. vco = dev_priv->skl_preferred_vco_freq;
  4477. WARN_ON(vco != 8100000 && vco != 8640000);
  4478. /*
  4479. * Use the lower (vco 8640) cdclk values as a
  4480. * first guess. skl_calc_cdclk() will correct it
  4481. * if the preferred vco is 8100 instead.
  4482. */
  4483. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4484. max_cdclk = 617143;
  4485. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4486. max_cdclk = 540000;
  4487. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4488. max_cdclk = 432000;
  4489. else
  4490. max_cdclk = 308571;
  4491. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4492. } else if (IS_BROXTON(dev)) {
  4493. dev_priv->max_cdclk_freq = 624000;
  4494. } else if (IS_BROADWELL(dev)) {
  4495. /*
  4496. * FIXME with extra cooling we can allow
  4497. * 540 MHz for ULX and 675 Mhz for ULT.
  4498. * How can we know if extra cooling is
  4499. * available? PCI ID, VTB, something else?
  4500. */
  4501. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4502. dev_priv->max_cdclk_freq = 450000;
  4503. else if (IS_BDW_ULX(dev))
  4504. dev_priv->max_cdclk_freq = 450000;
  4505. else if (IS_BDW_ULT(dev))
  4506. dev_priv->max_cdclk_freq = 540000;
  4507. else
  4508. dev_priv->max_cdclk_freq = 675000;
  4509. } else if (IS_CHERRYVIEW(dev)) {
  4510. dev_priv->max_cdclk_freq = 320000;
  4511. } else if (IS_VALLEYVIEW(dev)) {
  4512. dev_priv->max_cdclk_freq = 400000;
  4513. } else {
  4514. /* otherwise assume cdclk is fixed */
  4515. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4516. }
  4517. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4518. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4519. dev_priv->max_cdclk_freq);
  4520. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4521. dev_priv->max_dotclk_freq);
  4522. }
  4523. static void intel_update_cdclk(struct drm_device *dev)
  4524. {
  4525. struct drm_i915_private *dev_priv = dev->dev_private;
  4526. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4527. if (INTEL_GEN(dev_priv) >= 9)
  4528. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  4529. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  4530. dev_priv->cdclk_pll.ref);
  4531. else
  4532. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4533. dev_priv->cdclk_freq);
  4534. /*
  4535. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4536. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4537. * of cdclk that generates 4MHz reference clock freq which is used to
  4538. * generate GMBus clock. This will vary with the cdclk freq.
  4539. */
  4540. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4541. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4542. }
  4543. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4544. static int skl_cdclk_decimal(int cdclk)
  4545. {
  4546. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  4547. }
  4548. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  4549. {
  4550. int ratio;
  4551. if (cdclk == dev_priv->cdclk_pll.ref)
  4552. return 0;
  4553. switch (cdclk) {
  4554. default:
  4555. MISSING_CASE(cdclk);
  4556. case 144000:
  4557. case 288000:
  4558. case 384000:
  4559. case 576000:
  4560. ratio = 60;
  4561. break;
  4562. case 624000:
  4563. ratio = 65;
  4564. break;
  4565. }
  4566. return dev_priv->cdclk_pll.ref * ratio;
  4567. }
  4568. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  4569. {
  4570. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  4571. /* Timeout 200us */
  4572. if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
  4573. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  4574. dev_priv->cdclk_pll.vco = 0;
  4575. }
  4576. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  4577. {
  4578. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  4579. u32 val;
  4580. val = I915_READ(BXT_DE_PLL_CTL);
  4581. val &= ~BXT_DE_PLL_RATIO_MASK;
  4582. val |= BXT_DE_PLL_RATIO(ratio);
  4583. I915_WRITE(BXT_DE_PLL_CTL, val);
  4584. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4585. /* Timeout 200us */
  4586. if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
  4587. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4588. dev_priv->cdclk_pll.vco = vco;
  4589. }
  4590. static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  4591. {
  4592. u32 val, divider;
  4593. int vco, ret;
  4594. vco = bxt_de_pll_vco(dev_priv, cdclk);
  4595. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  4596. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  4597. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  4598. case 8:
  4599. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4600. break;
  4601. case 4:
  4602. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4603. break;
  4604. case 3:
  4605. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4606. break;
  4607. case 2:
  4608. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4609. break;
  4610. default:
  4611. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  4612. WARN_ON(vco != 0);
  4613. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4614. break;
  4615. }
  4616. /* Inform power controller of upcoming frequency change */
  4617. mutex_lock(&dev_priv->rps.hw_lock);
  4618. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4619. 0x80000000);
  4620. mutex_unlock(&dev_priv->rps.hw_lock);
  4621. if (ret) {
  4622. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4623. ret, cdclk);
  4624. return;
  4625. }
  4626. if (dev_priv->cdclk_pll.vco != 0 &&
  4627. dev_priv->cdclk_pll.vco != vco)
  4628. bxt_de_pll_disable(dev_priv);
  4629. if (dev_priv->cdclk_pll.vco != vco)
  4630. bxt_de_pll_enable(dev_priv, vco);
  4631. val = divider | skl_cdclk_decimal(cdclk);
  4632. /*
  4633. * FIXME if only the cd2x divider needs changing, it could be done
  4634. * without shutting off the pipe (if only one pipe is active).
  4635. */
  4636. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  4637. /*
  4638. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4639. * enable otherwise.
  4640. */
  4641. if (cdclk >= 500000)
  4642. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4643. I915_WRITE(CDCLK_CTL, val);
  4644. mutex_lock(&dev_priv->rps.hw_lock);
  4645. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4646. DIV_ROUND_UP(cdclk, 25000));
  4647. mutex_unlock(&dev_priv->rps.hw_lock);
  4648. if (ret) {
  4649. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4650. ret, cdclk);
  4651. return;
  4652. }
  4653. intel_update_cdclk(dev_priv->dev);
  4654. }
  4655. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4656. {
  4657. u32 cdctl, expected;
  4658. intel_update_cdclk(dev_priv->dev);
  4659. if (dev_priv->cdclk_pll.vco == 0 ||
  4660. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  4661. goto sanitize;
  4662. /* DPLL okay; verify the cdclock
  4663. *
  4664. * Some BIOS versions leave an incorrect decimal frequency value and
  4665. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  4666. * so sanitize this register.
  4667. */
  4668. cdctl = I915_READ(CDCLK_CTL);
  4669. /*
  4670. * Let's ignore the pipe field, since BIOS could have configured the
  4671. * dividers both synching to an active pipe, or asynchronously
  4672. * (PIPE_NONE).
  4673. */
  4674. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  4675. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  4676. skl_cdclk_decimal(dev_priv->cdclk_freq);
  4677. /*
  4678. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4679. * enable otherwise.
  4680. */
  4681. if (dev_priv->cdclk_freq >= 500000)
  4682. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4683. if (cdctl == expected)
  4684. /* All well; nothing to sanitize */
  4685. return;
  4686. sanitize:
  4687. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  4688. /* force cdclk programming */
  4689. dev_priv->cdclk_freq = 0;
  4690. /* force full PLL disable + enable */
  4691. dev_priv->cdclk_pll.vco = -1;
  4692. }
  4693. void broxton_init_cdclk(struct drm_i915_private *dev_priv)
  4694. {
  4695. bxt_sanitize_cdclk(dev_priv);
  4696. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  4697. return;
  4698. /*
  4699. * FIXME:
  4700. * - The initial CDCLK needs to be read from VBT.
  4701. * Need to make this change after VBT has changes for BXT.
  4702. */
  4703. broxton_set_cdclk(dev_priv, broxton_calc_cdclk(0));
  4704. }
  4705. void broxton_uninit_cdclk(struct drm_i915_private *dev_priv)
  4706. {
  4707. broxton_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  4708. }
  4709. static int skl_calc_cdclk(int max_pixclk, int vco)
  4710. {
  4711. if (vco == 8640000) {
  4712. if (max_pixclk > 540000)
  4713. return 617143;
  4714. else if (max_pixclk > 432000)
  4715. return 540000;
  4716. else if (max_pixclk > 308571)
  4717. return 432000;
  4718. else
  4719. return 308571;
  4720. } else {
  4721. if (max_pixclk > 540000)
  4722. return 675000;
  4723. else if (max_pixclk > 450000)
  4724. return 540000;
  4725. else if (max_pixclk > 337500)
  4726. return 450000;
  4727. else
  4728. return 337500;
  4729. }
  4730. }
  4731. static void
  4732. skl_dpll0_update(struct drm_i915_private *dev_priv)
  4733. {
  4734. u32 val;
  4735. dev_priv->cdclk_pll.ref = 24000;
  4736. dev_priv->cdclk_pll.vco = 0;
  4737. val = I915_READ(LCPLL1_CTL);
  4738. if ((val & LCPLL_PLL_ENABLE) == 0)
  4739. return;
  4740. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  4741. return;
  4742. val = I915_READ(DPLL_CTRL1);
  4743. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  4744. DPLL_CTRL1_SSC(SKL_DPLL0) |
  4745. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  4746. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  4747. return;
  4748. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  4749. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  4750. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  4751. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  4752. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  4753. dev_priv->cdclk_pll.vco = 8100000;
  4754. break;
  4755. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  4756. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  4757. dev_priv->cdclk_pll.vco = 8640000;
  4758. break;
  4759. default:
  4760. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4761. break;
  4762. }
  4763. }
  4764. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  4765. {
  4766. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  4767. dev_priv->skl_preferred_vco_freq = vco;
  4768. if (changed)
  4769. intel_update_max_cdclk(dev_priv->dev);
  4770. }
  4771. static void
  4772. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  4773. {
  4774. int min_cdclk = skl_calc_cdclk(0, vco);
  4775. u32 val;
  4776. WARN_ON(vco != 8100000 && vco != 8640000);
  4777. /* select the minimum CDCLK before enabling DPLL 0 */
  4778. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  4779. I915_WRITE(CDCLK_CTL, val);
  4780. POSTING_READ(CDCLK_CTL);
  4781. /*
  4782. * We always enable DPLL0 with the lowest link rate possible, but still
  4783. * taking into account the VCO required to operate the eDP panel at the
  4784. * desired frequency. The usual DP link rates operate with a VCO of
  4785. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4786. * The modeset code is responsible for the selection of the exact link
  4787. * rate later on, with the constraint of choosing a frequency that
  4788. * works with vco.
  4789. */
  4790. val = I915_READ(DPLL_CTRL1);
  4791. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4792. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4793. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4794. if (vco == 8640000)
  4795. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4796. SKL_DPLL0);
  4797. else
  4798. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4799. SKL_DPLL0);
  4800. I915_WRITE(DPLL_CTRL1, val);
  4801. POSTING_READ(DPLL_CTRL1);
  4802. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4803. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4804. DRM_ERROR("DPLL0 not locked\n");
  4805. dev_priv->cdclk_pll.vco = vco;
  4806. /* We'll want to keep using the current vco from now on. */
  4807. skl_set_preferred_cdclk_vco(dev_priv, vco);
  4808. }
  4809. static void
  4810. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  4811. {
  4812. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4813. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4814. DRM_ERROR("Couldn't disable DPLL0\n");
  4815. dev_priv->cdclk_pll.vco = 0;
  4816. }
  4817. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4818. {
  4819. int ret;
  4820. u32 val;
  4821. /* inform PCU we want to change CDCLK */
  4822. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4823. mutex_lock(&dev_priv->rps.hw_lock);
  4824. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4825. mutex_unlock(&dev_priv->rps.hw_lock);
  4826. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4827. }
  4828. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4829. {
  4830. unsigned int i;
  4831. for (i = 0; i < 15; i++) {
  4832. if (skl_cdclk_pcu_ready(dev_priv))
  4833. return true;
  4834. udelay(10);
  4835. }
  4836. return false;
  4837. }
  4838. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  4839. {
  4840. struct drm_device *dev = dev_priv->dev;
  4841. u32 freq_select, pcu_ack;
  4842. WARN_ON((cdclk == 24000) != (vco == 0));
  4843. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  4844. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4845. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4846. return;
  4847. }
  4848. /* set CDCLK_CTL */
  4849. switch (cdclk) {
  4850. case 450000:
  4851. case 432000:
  4852. freq_select = CDCLK_FREQ_450_432;
  4853. pcu_ack = 1;
  4854. break;
  4855. case 540000:
  4856. freq_select = CDCLK_FREQ_540;
  4857. pcu_ack = 2;
  4858. break;
  4859. case 308571:
  4860. case 337500:
  4861. default:
  4862. freq_select = CDCLK_FREQ_337_308;
  4863. pcu_ack = 0;
  4864. break;
  4865. case 617143:
  4866. case 675000:
  4867. freq_select = CDCLK_FREQ_675_617;
  4868. pcu_ack = 3;
  4869. break;
  4870. }
  4871. if (dev_priv->cdclk_pll.vco != 0 &&
  4872. dev_priv->cdclk_pll.vco != vco)
  4873. skl_dpll0_disable(dev_priv);
  4874. if (dev_priv->cdclk_pll.vco != vco)
  4875. skl_dpll0_enable(dev_priv, vco);
  4876. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  4877. POSTING_READ(CDCLK_CTL);
  4878. /* inform PCU of the change */
  4879. mutex_lock(&dev_priv->rps.hw_lock);
  4880. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4881. mutex_unlock(&dev_priv->rps.hw_lock);
  4882. intel_update_cdclk(dev);
  4883. }
  4884. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  4885. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4886. {
  4887. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  4888. }
  4889. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4890. {
  4891. int cdclk, vco;
  4892. skl_sanitize_cdclk(dev_priv);
  4893. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  4894. /*
  4895. * Use the current vco as our initial
  4896. * guess as to what the preferred vco is.
  4897. */
  4898. if (dev_priv->skl_preferred_vco_freq == 0)
  4899. skl_set_preferred_cdclk_vco(dev_priv,
  4900. dev_priv->cdclk_pll.vco);
  4901. return;
  4902. }
  4903. vco = dev_priv->skl_preferred_vco_freq;
  4904. if (vco == 0)
  4905. vco = 8100000;
  4906. cdclk = skl_calc_cdclk(0, vco);
  4907. skl_set_cdclk(dev_priv, cdclk, vco);
  4908. }
  4909. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4910. {
  4911. uint32_t cdctl, expected;
  4912. /*
  4913. * check if the pre-os intialized the display
  4914. * There is SWF18 scratchpad register defined which is set by the
  4915. * pre-os which can be used by the OS drivers to check the status
  4916. */
  4917. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4918. goto sanitize;
  4919. intel_update_cdclk(dev_priv->dev);
  4920. /* Is PLL enabled and locked ? */
  4921. if (dev_priv->cdclk_pll.vco == 0 ||
  4922. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  4923. goto sanitize;
  4924. /* DPLL okay; verify the cdclock
  4925. *
  4926. * Noticed in some instances that the freq selection is correct but
  4927. * decimal part is programmed wrong from BIOS where pre-os does not
  4928. * enable display. Verify the same as well.
  4929. */
  4930. cdctl = I915_READ(CDCLK_CTL);
  4931. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  4932. skl_cdclk_decimal(dev_priv->cdclk_freq);
  4933. if (cdctl == expected)
  4934. /* All well; nothing to sanitize */
  4935. return;
  4936. sanitize:
  4937. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  4938. /* force cdclk programming */
  4939. dev_priv->cdclk_freq = 0;
  4940. /* force full PLL disable + enable */
  4941. dev_priv->cdclk_pll.vco = -1;
  4942. }
  4943. /* Adjust CDclk dividers to allow high res or save power if possible */
  4944. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4945. {
  4946. struct drm_i915_private *dev_priv = dev->dev_private;
  4947. u32 val, cmd;
  4948. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4949. != dev_priv->cdclk_freq);
  4950. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4951. cmd = 2;
  4952. else if (cdclk == 266667)
  4953. cmd = 1;
  4954. else
  4955. cmd = 0;
  4956. mutex_lock(&dev_priv->rps.hw_lock);
  4957. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4958. val &= ~DSPFREQGUAR_MASK;
  4959. val |= (cmd << DSPFREQGUAR_SHIFT);
  4960. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4961. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4962. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4963. 50)) {
  4964. DRM_ERROR("timed out waiting for CDclk change\n");
  4965. }
  4966. mutex_unlock(&dev_priv->rps.hw_lock);
  4967. mutex_lock(&dev_priv->sb_lock);
  4968. if (cdclk == 400000) {
  4969. u32 divider;
  4970. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4971. /* adjust cdclk divider */
  4972. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4973. val &= ~CCK_FREQUENCY_VALUES;
  4974. val |= divider;
  4975. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4976. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4977. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4978. 50))
  4979. DRM_ERROR("timed out waiting for CDclk change\n");
  4980. }
  4981. /* adjust self-refresh exit latency value */
  4982. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4983. val &= ~0x7f;
  4984. /*
  4985. * For high bandwidth configs, we set a higher latency in the bunit
  4986. * so that the core display fetch happens in time to avoid underruns.
  4987. */
  4988. if (cdclk == 400000)
  4989. val |= 4500 / 250; /* 4.5 usec */
  4990. else
  4991. val |= 3000 / 250; /* 3.0 usec */
  4992. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4993. mutex_unlock(&dev_priv->sb_lock);
  4994. intel_update_cdclk(dev);
  4995. }
  4996. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4997. {
  4998. struct drm_i915_private *dev_priv = dev->dev_private;
  4999. u32 val, cmd;
  5000. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  5001. != dev_priv->cdclk_freq);
  5002. switch (cdclk) {
  5003. case 333333:
  5004. case 320000:
  5005. case 266667:
  5006. case 200000:
  5007. break;
  5008. default:
  5009. MISSING_CASE(cdclk);
  5010. return;
  5011. }
  5012. /*
  5013. * Specs are full of misinformation, but testing on actual
  5014. * hardware has shown that we just need to write the desired
  5015. * CCK divider into the Punit register.
  5016. */
  5017. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5018. mutex_lock(&dev_priv->rps.hw_lock);
  5019. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5020. val &= ~DSPFREQGUAR_MASK_CHV;
  5021. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5022. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5023. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5024. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5025. 50)) {
  5026. DRM_ERROR("timed out waiting for CDclk change\n");
  5027. }
  5028. mutex_unlock(&dev_priv->rps.hw_lock);
  5029. intel_update_cdclk(dev);
  5030. }
  5031. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5032. int max_pixclk)
  5033. {
  5034. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5035. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5036. /*
  5037. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5038. * 200MHz
  5039. * 267MHz
  5040. * 320/333MHz (depends on HPLL freq)
  5041. * 400MHz (VLV only)
  5042. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5043. * of the lower bin and adjust if needed.
  5044. *
  5045. * We seem to get an unstable or solid color picture at 200MHz.
  5046. * Not sure what's wrong. For now use 200MHz only when all pipes
  5047. * are off.
  5048. */
  5049. if (!IS_CHERRYVIEW(dev_priv) &&
  5050. max_pixclk > freq_320*limit/100)
  5051. return 400000;
  5052. else if (max_pixclk > 266667*limit/100)
  5053. return freq_320;
  5054. else if (max_pixclk > 0)
  5055. return 266667;
  5056. else
  5057. return 200000;
  5058. }
  5059. static int broxton_calc_cdclk(int max_pixclk)
  5060. {
  5061. if (max_pixclk > 576000)
  5062. return 624000;
  5063. else if (max_pixclk > 384000)
  5064. return 576000;
  5065. else if (max_pixclk > 288000)
  5066. return 384000;
  5067. else if (max_pixclk > 144000)
  5068. return 288000;
  5069. else
  5070. return 144000;
  5071. }
  5072. /* Compute the max pixel clock for new configuration. */
  5073. static int intel_mode_max_pixclk(struct drm_device *dev,
  5074. struct drm_atomic_state *state)
  5075. {
  5076. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5077. struct drm_i915_private *dev_priv = dev->dev_private;
  5078. struct drm_crtc *crtc;
  5079. struct drm_crtc_state *crtc_state;
  5080. unsigned max_pixclk = 0, i;
  5081. enum pipe pipe;
  5082. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5083. sizeof(intel_state->min_pixclk));
  5084. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5085. int pixclk = 0;
  5086. if (crtc_state->enable)
  5087. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5088. intel_state->min_pixclk[i] = pixclk;
  5089. }
  5090. for_each_pipe(dev_priv, pipe)
  5091. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5092. return max_pixclk;
  5093. }
  5094. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5095. {
  5096. struct drm_device *dev = state->dev;
  5097. struct drm_i915_private *dev_priv = dev->dev_private;
  5098. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5099. struct intel_atomic_state *intel_state =
  5100. to_intel_atomic_state(state);
  5101. intel_state->cdclk = intel_state->dev_cdclk =
  5102. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5103. if (!intel_state->active_crtcs)
  5104. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5105. return 0;
  5106. }
  5107. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5108. {
  5109. int max_pixclk = ilk_max_pixel_rate(state);
  5110. struct intel_atomic_state *intel_state =
  5111. to_intel_atomic_state(state);
  5112. intel_state->cdclk = intel_state->dev_cdclk =
  5113. broxton_calc_cdclk(max_pixclk);
  5114. if (!intel_state->active_crtcs)
  5115. intel_state->dev_cdclk = broxton_calc_cdclk(0);
  5116. return 0;
  5117. }
  5118. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5119. {
  5120. unsigned int credits, default_credits;
  5121. if (IS_CHERRYVIEW(dev_priv))
  5122. default_credits = PFI_CREDIT(12);
  5123. else
  5124. default_credits = PFI_CREDIT(8);
  5125. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5126. /* CHV suggested value is 31 or 63 */
  5127. if (IS_CHERRYVIEW(dev_priv))
  5128. credits = PFI_CREDIT_63;
  5129. else
  5130. credits = PFI_CREDIT(15);
  5131. } else {
  5132. credits = default_credits;
  5133. }
  5134. /*
  5135. * WA - write default credits before re-programming
  5136. * FIXME: should we also set the resend bit here?
  5137. */
  5138. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5139. default_credits);
  5140. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5141. credits | PFI_CREDIT_RESEND);
  5142. /*
  5143. * FIXME is this guaranteed to clear
  5144. * immediately or should we poll for it?
  5145. */
  5146. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5147. }
  5148. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5149. {
  5150. struct drm_device *dev = old_state->dev;
  5151. struct drm_i915_private *dev_priv = dev->dev_private;
  5152. struct intel_atomic_state *old_intel_state =
  5153. to_intel_atomic_state(old_state);
  5154. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5155. /*
  5156. * FIXME: We can end up here with all power domains off, yet
  5157. * with a CDCLK frequency other than the minimum. To account
  5158. * for this take the PIPE-A power domain, which covers the HW
  5159. * blocks needed for the following programming. This can be
  5160. * removed once it's guaranteed that we get here either with
  5161. * the minimum CDCLK set, or the required power domains
  5162. * enabled.
  5163. */
  5164. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5165. if (IS_CHERRYVIEW(dev))
  5166. cherryview_set_cdclk(dev, req_cdclk);
  5167. else
  5168. valleyview_set_cdclk(dev, req_cdclk);
  5169. vlv_program_pfi_credits(dev_priv);
  5170. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5171. }
  5172. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5173. {
  5174. struct drm_device *dev = crtc->dev;
  5175. struct drm_i915_private *dev_priv = to_i915(dev);
  5176. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5177. struct intel_encoder *encoder;
  5178. struct intel_crtc_state *pipe_config =
  5179. to_intel_crtc_state(crtc->state);
  5180. int pipe = intel_crtc->pipe;
  5181. if (WARN_ON(intel_crtc->active))
  5182. return;
  5183. if (intel_crtc->config->has_dp_encoder)
  5184. intel_dp_set_m_n(intel_crtc, M1_N1);
  5185. intel_set_pipe_timings(intel_crtc);
  5186. intel_set_pipe_src_size(intel_crtc);
  5187. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5188. struct drm_i915_private *dev_priv = dev->dev_private;
  5189. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5190. I915_WRITE(CHV_CANVAS(pipe), 0);
  5191. }
  5192. i9xx_set_pipeconf(intel_crtc);
  5193. intel_crtc->active = true;
  5194. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5195. for_each_encoder_on_crtc(dev, crtc, encoder)
  5196. if (encoder->pre_pll_enable)
  5197. encoder->pre_pll_enable(encoder);
  5198. if (IS_CHERRYVIEW(dev)) {
  5199. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5200. chv_enable_pll(intel_crtc, intel_crtc->config);
  5201. } else {
  5202. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5203. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5204. }
  5205. for_each_encoder_on_crtc(dev, crtc, encoder)
  5206. if (encoder->pre_enable)
  5207. encoder->pre_enable(encoder);
  5208. i9xx_pfit_enable(intel_crtc);
  5209. intel_color_load_luts(&pipe_config->base);
  5210. intel_update_watermarks(crtc);
  5211. intel_enable_pipe(intel_crtc);
  5212. assert_vblank_disabled(crtc);
  5213. drm_crtc_vblank_on(crtc);
  5214. for_each_encoder_on_crtc(dev, crtc, encoder)
  5215. encoder->enable(encoder);
  5216. }
  5217. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5218. {
  5219. struct drm_device *dev = crtc->base.dev;
  5220. struct drm_i915_private *dev_priv = dev->dev_private;
  5221. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5222. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5223. }
  5224. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5225. {
  5226. struct drm_device *dev = crtc->dev;
  5227. struct drm_i915_private *dev_priv = to_i915(dev);
  5228. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5229. struct intel_encoder *encoder;
  5230. struct intel_crtc_state *pipe_config =
  5231. to_intel_crtc_state(crtc->state);
  5232. enum pipe pipe = intel_crtc->pipe;
  5233. if (WARN_ON(intel_crtc->active))
  5234. return;
  5235. i9xx_set_pll_dividers(intel_crtc);
  5236. if (intel_crtc->config->has_dp_encoder)
  5237. intel_dp_set_m_n(intel_crtc, M1_N1);
  5238. intel_set_pipe_timings(intel_crtc);
  5239. intel_set_pipe_src_size(intel_crtc);
  5240. i9xx_set_pipeconf(intel_crtc);
  5241. intel_crtc->active = true;
  5242. if (!IS_GEN2(dev))
  5243. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5244. for_each_encoder_on_crtc(dev, crtc, encoder)
  5245. if (encoder->pre_enable)
  5246. encoder->pre_enable(encoder);
  5247. i9xx_enable_pll(intel_crtc);
  5248. i9xx_pfit_enable(intel_crtc);
  5249. intel_color_load_luts(&pipe_config->base);
  5250. intel_update_watermarks(crtc);
  5251. intel_enable_pipe(intel_crtc);
  5252. assert_vblank_disabled(crtc);
  5253. drm_crtc_vblank_on(crtc);
  5254. for_each_encoder_on_crtc(dev, crtc, encoder)
  5255. encoder->enable(encoder);
  5256. }
  5257. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5258. {
  5259. struct drm_device *dev = crtc->base.dev;
  5260. struct drm_i915_private *dev_priv = dev->dev_private;
  5261. if (!crtc->config->gmch_pfit.control)
  5262. return;
  5263. assert_pipe_disabled(dev_priv, crtc->pipe);
  5264. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5265. I915_READ(PFIT_CONTROL));
  5266. I915_WRITE(PFIT_CONTROL, 0);
  5267. }
  5268. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5269. {
  5270. struct drm_device *dev = crtc->dev;
  5271. struct drm_i915_private *dev_priv = dev->dev_private;
  5272. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5273. struct intel_encoder *encoder;
  5274. int pipe = intel_crtc->pipe;
  5275. /*
  5276. * On gen2 planes are double buffered but the pipe isn't, so we must
  5277. * wait for planes to fully turn off before disabling the pipe.
  5278. */
  5279. if (IS_GEN2(dev))
  5280. intel_wait_for_vblank(dev, pipe);
  5281. for_each_encoder_on_crtc(dev, crtc, encoder)
  5282. encoder->disable(encoder);
  5283. drm_crtc_vblank_off(crtc);
  5284. assert_vblank_disabled(crtc);
  5285. intel_disable_pipe(intel_crtc);
  5286. i9xx_pfit_disable(intel_crtc);
  5287. for_each_encoder_on_crtc(dev, crtc, encoder)
  5288. if (encoder->post_disable)
  5289. encoder->post_disable(encoder);
  5290. if (!intel_crtc->config->has_dsi_encoder) {
  5291. if (IS_CHERRYVIEW(dev))
  5292. chv_disable_pll(dev_priv, pipe);
  5293. else if (IS_VALLEYVIEW(dev))
  5294. vlv_disable_pll(dev_priv, pipe);
  5295. else
  5296. i9xx_disable_pll(intel_crtc);
  5297. }
  5298. for_each_encoder_on_crtc(dev, crtc, encoder)
  5299. if (encoder->post_pll_disable)
  5300. encoder->post_pll_disable(encoder);
  5301. if (!IS_GEN2(dev))
  5302. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5303. }
  5304. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5305. {
  5306. struct intel_encoder *encoder;
  5307. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5308. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5309. enum intel_display_power_domain domain;
  5310. unsigned long domains;
  5311. if (!intel_crtc->active)
  5312. return;
  5313. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5314. WARN_ON(intel_crtc->flip_work);
  5315. intel_pre_disable_primary_noatomic(crtc);
  5316. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5317. to_intel_plane_state(crtc->primary->state)->visible = false;
  5318. }
  5319. dev_priv->display.crtc_disable(crtc);
  5320. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5321. crtc->base.id, crtc->name);
  5322. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5323. crtc->state->active = false;
  5324. intel_crtc->active = false;
  5325. crtc->enabled = false;
  5326. crtc->state->connector_mask = 0;
  5327. crtc->state->encoder_mask = 0;
  5328. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5329. encoder->base.crtc = NULL;
  5330. intel_fbc_disable(intel_crtc);
  5331. intel_update_watermarks(crtc);
  5332. intel_disable_shared_dpll(intel_crtc);
  5333. domains = intel_crtc->enabled_power_domains;
  5334. for_each_power_domain(domain, domains)
  5335. intel_display_power_put(dev_priv, domain);
  5336. intel_crtc->enabled_power_domains = 0;
  5337. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5338. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5339. }
  5340. /*
  5341. * turn all crtc's off, but do not adjust state
  5342. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5343. */
  5344. int intel_display_suspend(struct drm_device *dev)
  5345. {
  5346. struct drm_i915_private *dev_priv = to_i915(dev);
  5347. struct drm_atomic_state *state;
  5348. int ret;
  5349. state = drm_atomic_helper_suspend(dev);
  5350. ret = PTR_ERR_OR_ZERO(state);
  5351. if (ret)
  5352. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5353. else
  5354. dev_priv->modeset_restore_state = state;
  5355. return ret;
  5356. }
  5357. void intel_encoder_destroy(struct drm_encoder *encoder)
  5358. {
  5359. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5360. drm_encoder_cleanup(encoder);
  5361. kfree(intel_encoder);
  5362. }
  5363. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5364. * internal consistency). */
  5365. static void intel_connector_verify_state(struct intel_connector *connector)
  5366. {
  5367. struct drm_crtc *crtc = connector->base.state->crtc;
  5368. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5369. connector->base.base.id,
  5370. connector->base.name);
  5371. if (connector->get_hw_state(connector)) {
  5372. struct intel_encoder *encoder = connector->encoder;
  5373. struct drm_connector_state *conn_state = connector->base.state;
  5374. I915_STATE_WARN(!crtc,
  5375. "connector enabled without attached crtc\n");
  5376. if (!crtc)
  5377. return;
  5378. I915_STATE_WARN(!crtc->state->active,
  5379. "connector is active, but attached crtc isn't\n");
  5380. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5381. return;
  5382. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5383. "atomic encoder doesn't match attached encoder\n");
  5384. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5385. "attached encoder crtc differs from connector crtc\n");
  5386. } else {
  5387. I915_STATE_WARN(crtc && crtc->state->active,
  5388. "attached crtc is active, but connector isn't\n");
  5389. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5390. "best encoder set without crtc!\n");
  5391. }
  5392. }
  5393. int intel_connector_init(struct intel_connector *connector)
  5394. {
  5395. drm_atomic_helper_connector_reset(&connector->base);
  5396. if (!connector->base.state)
  5397. return -ENOMEM;
  5398. return 0;
  5399. }
  5400. struct intel_connector *intel_connector_alloc(void)
  5401. {
  5402. struct intel_connector *connector;
  5403. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5404. if (!connector)
  5405. return NULL;
  5406. if (intel_connector_init(connector) < 0) {
  5407. kfree(connector);
  5408. return NULL;
  5409. }
  5410. return connector;
  5411. }
  5412. /* Simple connector->get_hw_state implementation for encoders that support only
  5413. * one connector and no cloning and hence the encoder state determines the state
  5414. * of the connector. */
  5415. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5416. {
  5417. enum pipe pipe = 0;
  5418. struct intel_encoder *encoder = connector->encoder;
  5419. return encoder->get_hw_state(encoder, &pipe);
  5420. }
  5421. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5422. {
  5423. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5424. return crtc_state->fdi_lanes;
  5425. return 0;
  5426. }
  5427. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5428. struct intel_crtc_state *pipe_config)
  5429. {
  5430. struct drm_atomic_state *state = pipe_config->base.state;
  5431. struct intel_crtc *other_crtc;
  5432. struct intel_crtc_state *other_crtc_state;
  5433. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5434. pipe_name(pipe), pipe_config->fdi_lanes);
  5435. if (pipe_config->fdi_lanes > 4) {
  5436. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5437. pipe_name(pipe), pipe_config->fdi_lanes);
  5438. return -EINVAL;
  5439. }
  5440. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5441. if (pipe_config->fdi_lanes > 2) {
  5442. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5443. pipe_config->fdi_lanes);
  5444. return -EINVAL;
  5445. } else {
  5446. return 0;
  5447. }
  5448. }
  5449. if (INTEL_INFO(dev)->num_pipes == 2)
  5450. return 0;
  5451. /* Ivybridge 3 pipe is really complicated */
  5452. switch (pipe) {
  5453. case PIPE_A:
  5454. return 0;
  5455. case PIPE_B:
  5456. if (pipe_config->fdi_lanes <= 2)
  5457. return 0;
  5458. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5459. other_crtc_state =
  5460. intel_atomic_get_crtc_state(state, other_crtc);
  5461. if (IS_ERR(other_crtc_state))
  5462. return PTR_ERR(other_crtc_state);
  5463. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5464. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5465. pipe_name(pipe), pipe_config->fdi_lanes);
  5466. return -EINVAL;
  5467. }
  5468. return 0;
  5469. case PIPE_C:
  5470. if (pipe_config->fdi_lanes > 2) {
  5471. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5472. pipe_name(pipe), pipe_config->fdi_lanes);
  5473. return -EINVAL;
  5474. }
  5475. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5476. other_crtc_state =
  5477. intel_atomic_get_crtc_state(state, other_crtc);
  5478. if (IS_ERR(other_crtc_state))
  5479. return PTR_ERR(other_crtc_state);
  5480. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5481. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5482. return -EINVAL;
  5483. }
  5484. return 0;
  5485. default:
  5486. BUG();
  5487. }
  5488. }
  5489. #define RETRY 1
  5490. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5491. struct intel_crtc_state *pipe_config)
  5492. {
  5493. struct drm_device *dev = intel_crtc->base.dev;
  5494. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5495. int lane, link_bw, fdi_dotclock, ret;
  5496. bool needs_recompute = false;
  5497. retry:
  5498. /* FDI is a binary signal running at ~2.7GHz, encoding
  5499. * each output octet as 10 bits. The actual frequency
  5500. * is stored as a divider into a 100MHz clock, and the
  5501. * mode pixel clock is stored in units of 1KHz.
  5502. * Hence the bw of each lane in terms of the mode signal
  5503. * is:
  5504. */
  5505. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5506. fdi_dotclock = adjusted_mode->crtc_clock;
  5507. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5508. pipe_config->pipe_bpp);
  5509. pipe_config->fdi_lanes = lane;
  5510. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5511. link_bw, &pipe_config->fdi_m_n);
  5512. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5513. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5514. pipe_config->pipe_bpp -= 2*3;
  5515. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5516. pipe_config->pipe_bpp);
  5517. needs_recompute = true;
  5518. pipe_config->bw_constrained = true;
  5519. goto retry;
  5520. }
  5521. if (needs_recompute)
  5522. return RETRY;
  5523. return ret;
  5524. }
  5525. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5526. struct intel_crtc_state *pipe_config)
  5527. {
  5528. if (pipe_config->pipe_bpp > 24)
  5529. return false;
  5530. /* HSW can handle pixel rate up to cdclk? */
  5531. if (IS_HASWELL(dev_priv))
  5532. return true;
  5533. /*
  5534. * We compare against max which means we must take
  5535. * the increased cdclk requirement into account when
  5536. * calculating the new cdclk.
  5537. *
  5538. * Should measure whether using a lower cdclk w/o IPS
  5539. */
  5540. return ilk_pipe_pixel_rate(pipe_config) <=
  5541. dev_priv->max_cdclk_freq * 95 / 100;
  5542. }
  5543. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5544. struct intel_crtc_state *pipe_config)
  5545. {
  5546. struct drm_device *dev = crtc->base.dev;
  5547. struct drm_i915_private *dev_priv = dev->dev_private;
  5548. pipe_config->ips_enabled = i915.enable_ips &&
  5549. hsw_crtc_supports_ips(crtc) &&
  5550. pipe_config_supports_ips(dev_priv, pipe_config);
  5551. }
  5552. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5553. {
  5554. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5555. /* GDG double wide on either pipe, otherwise pipe A only */
  5556. return INTEL_INFO(dev_priv)->gen < 4 &&
  5557. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5558. }
  5559. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5560. struct intel_crtc_state *pipe_config)
  5561. {
  5562. struct drm_device *dev = crtc->base.dev;
  5563. struct drm_i915_private *dev_priv = dev->dev_private;
  5564. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5565. int clock_limit = dev_priv->max_dotclk_freq;
  5566. if (INTEL_INFO(dev)->gen < 4) {
  5567. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5568. /*
  5569. * Enable double wide mode when the dot clock
  5570. * is > 90% of the (display) core speed.
  5571. */
  5572. if (intel_crtc_supports_double_wide(crtc) &&
  5573. adjusted_mode->crtc_clock > clock_limit) {
  5574. clock_limit = dev_priv->max_dotclk_freq;
  5575. pipe_config->double_wide = true;
  5576. }
  5577. }
  5578. if (adjusted_mode->crtc_clock > clock_limit) {
  5579. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5580. adjusted_mode->crtc_clock, clock_limit,
  5581. yesno(pipe_config->double_wide));
  5582. return -EINVAL;
  5583. }
  5584. /*
  5585. * Pipe horizontal size must be even in:
  5586. * - DVO ganged mode
  5587. * - LVDS dual channel mode
  5588. * - Double wide pipe
  5589. */
  5590. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5591. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5592. pipe_config->pipe_src_w &= ~1;
  5593. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5594. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5595. */
  5596. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5597. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5598. return -EINVAL;
  5599. if (HAS_IPS(dev))
  5600. hsw_compute_ips_config(crtc, pipe_config);
  5601. if (pipe_config->has_pch_encoder)
  5602. return ironlake_fdi_compute_config(crtc, pipe_config);
  5603. return 0;
  5604. }
  5605. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5606. {
  5607. struct drm_i915_private *dev_priv = to_i915(dev);
  5608. uint32_t cdctl;
  5609. skl_dpll0_update(dev_priv);
  5610. if (dev_priv->cdclk_pll.vco == 0)
  5611. return dev_priv->cdclk_pll.ref;
  5612. cdctl = I915_READ(CDCLK_CTL);
  5613. if (dev_priv->cdclk_pll.vco == 8640000) {
  5614. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5615. case CDCLK_FREQ_450_432:
  5616. return 432000;
  5617. case CDCLK_FREQ_337_308:
  5618. return 308571;
  5619. case CDCLK_FREQ_540:
  5620. return 540000;
  5621. case CDCLK_FREQ_675_617:
  5622. return 617143;
  5623. default:
  5624. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5625. }
  5626. } else {
  5627. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5628. case CDCLK_FREQ_450_432:
  5629. return 450000;
  5630. case CDCLK_FREQ_337_308:
  5631. return 337500;
  5632. case CDCLK_FREQ_540:
  5633. return 540000;
  5634. case CDCLK_FREQ_675_617:
  5635. return 675000;
  5636. default:
  5637. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5638. }
  5639. }
  5640. return dev_priv->cdclk_pll.ref;
  5641. }
  5642. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  5643. {
  5644. u32 val;
  5645. dev_priv->cdclk_pll.ref = 19200;
  5646. dev_priv->cdclk_pll.vco = 0;
  5647. val = I915_READ(BXT_DE_PLL_ENABLE);
  5648. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  5649. return;
  5650. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  5651. return;
  5652. val = I915_READ(BXT_DE_PLL_CTL);
  5653. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  5654. dev_priv->cdclk_pll.ref;
  5655. }
  5656. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5657. {
  5658. struct drm_i915_private *dev_priv = to_i915(dev);
  5659. u32 divider;
  5660. int div, vco;
  5661. bxt_de_pll_update(dev_priv);
  5662. vco = dev_priv->cdclk_pll.vco;
  5663. if (vco == 0)
  5664. return dev_priv->cdclk_pll.ref;
  5665. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  5666. switch (divider) {
  5667. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5668. div = 2;
  5669. break;
  5670. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5671. div = 3;
  5672. break;
  5673. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5674. div = 4;
  5675. break;
  5676. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5677. div = 8;
  5678. break;
  5679. default:
  5680. MISSING_CASE(divider);
  5681. return dev_priv->cdclk_pll.ref;
  5682. }
  5683. return DIV_ROUND_CLOSEST(vco, div);
  5684. }
  5685. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5686. {
  5687. struct drm_i915_private *dev_priv = dev->dev_private;
  5688. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5689. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5690. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5691. return 800000;
  5692. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5693. return 450000;
  5694. else if (freq == LCPLL_CLK_FREQ_450)
  5695. return 450000;
  5696. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5697. return 540000;
  5698. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5699. return 337500;
  5700. else
  5701. return 675000;
  5702. }
  5703. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5704. {
  5705. struct drm_i915_private *dev_priv = dev->dev_private;
  5706. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5707. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5708. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5709. return 800000;
  5710. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5711. return 450000;
  5712. else if (freq == LCPLL_CLK_FREQ_450)
  5713. return 450000;
  5714. else if (IS_HSW_ULT(dev))
  5715. return 337500;
  5716. else
  5717. return 540000;
  5718. }
  5719. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5720. {
  5721. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5722. CCK_DISPLAY_CLOCK_CONTROL);
  5723. }
  5724. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5725. {
  5726. return 450000;
  5727. }
  5728. static int i945_get_display_clock_speed(struct drm_device *dev)
  5729. {
  5730. return 400000;
  5731. }
  5732. static int i915_get_display_clock_speed(struct drm_device *dev)
  5733. {
  5734. return 333333;
  5735. }
  5736. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5737. {
  5738. return 200000;
  5739. }
  5740. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5741. {
  5742. u16 gcfgc = 0;
  5743. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5744. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5745. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5746. return 266667;
  5747. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5748. return 333333;
  5749. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5750. return 444444;
  5751. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5752. return 200000;
  5753. default:
  5754. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5755. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5756. return 133333;
  5757. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5758. return 166667;
  5759. }
  5760. }
  5761. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5762. {
  5763. u16 gcfgc = 0;
  5764. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5765. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5766. return 133333;
  5767. else {
  5768. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5769. case GC_DISPLAY_CLOCK_333_MHZ:
  5770. return 333333;
  5771. default:
  5772. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5773. return 190000;
  5774. }
  5775. }
  5776. }
  5777. static int i865_get_display_clock_speed(struct drm_device *dev)
  5778. {
  5779. return 266667;
  5780. }
  5781. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5782. {
  5783. u16 hpllcc = 0;
  5784. /*
  5785. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5786. * encoding is different :(
  5787. * FIXME is this the right way to detect 852GM/852GMV?
  5788. */
  5789. if (dev->pdev->revision == 0x1)
  5790. return 133333;
  5791. pci_bus_read_config_word(dev->pdev->bus,
  5792. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5793. /* Assume that the hardware is in the high speed state. This
  5794. * should be the default.
  5795. */
  5796. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5797. case GC_CLOCK_133_200:
  5798. case GC_CLOCK_133_200_2:
  5799. case GC_CLOCK_100_200:
  5800. return 200000;
  5801. case GC_CLOCK_166_250:
  5802. return 250000;
  5803. case GC_CLOCK_100_133:
  5804. return 133333;
  5805. case GC_CLOCK_133_266:
  5806. case GC_CLOCK_133_266_2:
  5807. case GC_CLOCK_166_266:
  5808. return 266667;
  5809. }
  5810. /* Shouldn't happen */
  5811. return 0;
  5812. }
  5813. static int i830_get_display_clock_speed(struct drm_device *dev)
  5814. {
  5815. return 133333;
  5816. }
  5817. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5818. {
  5819. struct drm_i915_private *dev_priv = dev->dev_private;
  5820. static const unsigned int blb_vco[8] = {
  5821. [0] = 3200000,
  5822. [1] = 4000000,
  5823. [2] = 5333333,
  5824. [3] = 4800000,
  5825. [4] = 6400000,
  5826. };
  5827. static const unsigned int pnv_vco[8] = {
  5828. [0] = 3200000,
  5829. [1] = 4000000,
  5830. [2] = 5333333,
  5831. [3] = 4800000,
  5832. [4] = 2666667,
  5833. };
  5834. static const unsigned int cl_vco[8] = {
  5835. [0] = 3200000,
  5836. [1] = 4000000,
  5837. [2] = 5333333,
  5838. [3] = 6400000,
  5839. [4] = 3333333,
  5840. [5] = 3566667,
  5841. [6] = 4266667,
  5842. };
  5843. static const unsigned int elk_vco[8] = {
  5844. [0] = 3200000,
  5845. [1] = 4000000,
  5846. [2] = 5333333,
  5847. [3] = 4800000,
  5848. };
  5849. static const unsigned int ctg_vco[8] = {
  5850. [0] = 3200000,
  5851. [1] = 4000000,
  5852. [2] = 5333333,
  5853. [3] = 6400000,
  5854. [4] = 2666667,
  5855. [5] = 4266667,
  5856. };
  5857. const unsigned int *vco_table;
  5858. unsigned int vco;
  5859. uint8_t tmp = 0;
  5860. /* FIXME other chipsets? */
  5861. if (IS_GM45(dev))
  5862. vco_table = ctg_vco;
  5863. else if (IS_G4X(dev))
  5864. vco_table = elk_vco;
  5865. else if (IS_CRESTLINE(dev))
  5866. vco_table = cl_vco;
  5867. else if (IS_PINEVIEW(dev))
  5868. vco_table = pnv_vco;
  5869. else if (IS_G33(dev))
  5870. vco_table = blb_vco;
  5871. else
  5872. return 0;
  5873. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5874. vco = vco_table[tmp & 0x7];
  5875. if (vco == 0)
  5876. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5877. else
  5878. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5879. return vco;
  5880. }
  5881. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5882. {
  5883. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5884. uint16_t tmp = 0;
  5885. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5886. cdclk_sel = (tmp >> 12) & 0x1;
  5887. switch (vco) {
  5888. case 2666667:
  5889. case 4000000:
  5890. case 5333333:
  5891. return cdclk_sel ? 333333 : 222222;
  5892. case 3200000:
  5893. return cdclk_sel ? 320000 : 228571;
  5894. default:
  5895. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5896. return 222222;
  5897. }
  5898. }
  5899. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5900. {
  5901. static const uint8_t div_3200[] = { 16, 10, 8 };
  5902. static const uint8_t div_4000[] = { 20, 12, 10 };
  5903. static const uint8_t div_5333[] = { 24, 16, 14 };
  5904. const uint8_t *div_table;
  5905. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5906. uint16_t tmp = 0;
  5907. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5908. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5909. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5910. goto fail;
  5911. switch (vco) {
  5912. case 3200000:
  5913. div_table = div_3200;
  5914. break;
  5915. case 4000000:
  5916. div_table = div_4000;
  5917. break;
  5918. case 5333333:
  5919. div_table = div_5333;
  5920. break;
  5921. default:
  5922. goto fail;
  5923. }
  5924. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5925. fail:
  5926. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5927. return 200000;
  5928. }
  5929. static int g33_get_display_clock_speed(struct drm_device *dev)
  5930. {
  5931. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5932. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5933. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5934. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5935. const uint8_t *div_table;
  5936. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5937. uint16_t tmp = 0;
  5938. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5939. cdclk_sel = (tmp >> 4) & 0x7;
  5940. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5941. goto fail;
  5942. switch (vco) {
  5943. case 3200000:
  5944. div_table = div_3200;
  5945. break;
  5946. case 4000000:
  5947. div_table = div_4000;
  5948. break;
  5949. case 4800000:
  5950. div_table = div_4800;
  5951. break;
  5952. case 5333333:
  5953. div_table = div_5333;
  5954. break;
  5955. default:
  5956. goto fail;
  5957. }
  5958. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5959. fail:
  5960. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5961. return 190476;
  5962. }
  5963. static void
  5964. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5965. {
  5966. while (*num > DATA_LINK_M_N_MASK ||
  5967. *den > DATA_LINK_M_N_MASK) {
  5968. *num >>= 1;
  5969. *den >>= 1;
  5970. }
  5971. }
  5972. static void compute_m_n(unsigned int m, unsigned int n,
  5973. uint32_t *ret_m, uint32_t *ret_n)
  5974. {
  5975. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5976. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5977. intel_reduce_m_n_ratio(ret_m, ret_n);
  5978. }
  5979. void
  5980. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5981. int pixel_clock, int link_clock,
  5982. struct intel_link_m_n *m_n)
  5983. {
  5984. m_n->tu = 64;
  5985. compute_m_n(bits_per_pixel * pixel_clock,
  5986. link_clock * nlanes * 8,
  5987. &m_n->gmch_m, &m_n->gmch_n);
  5988. compute_m_n(pixel_clock, link_clock,
  5989. &m_n->link_m, &m_n->link_n);
  5990. }
  5991. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5992. {
  5993. if (i915.panel_use_ssc >= 0)
  5994. return i915.panel_use_ssc != 0;
  5995. return dev_priv->vbt.lvds_use_ssc
  5996. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5997. }
  5998. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5999. {
  6000. return (1 << dpll->n) << 16 | dpll->m2;
  6001. }
  6002. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6003. {
  6004. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6005. }
  6006. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6007. struct intel_crtc_state *crtc_state,
  6008. struct dpll *reduced_clock)
  6009. {
  6010. struct drm_device *dev = crtc->base.dev;
  6011. u32 fp, fp2 = 0;
  6012. if (IS_PINEVIEW(dev)) {
  6013. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6014. if (reduced_clock)
  6015. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6016. } else {
  6017. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6018. if (reduced_clock)
  6019. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6020. }
  6021. crtc_state->dpll_hw_state.fp0 = fp;
  6022. crtc->lowfreq_avail = false;
  6023. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6024. reduced_clock) {
  6025. crtc_state->dpll_hw_state.fp1 = fp2;
  6026. crtc->lowfreq_avail = true;
  6027. } else {
  6028. crtc_state->dpll_hw_state.fp1 = fp;
  6029. }
  6030. }
  6031. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6032. pipe)
  6033. {
  6034. u32 reg_val;
  6035. /*
  6036. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6037. * and set it to a reasonable value instead.
  6038. */
  6039. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6040. reg_val &= 0xffffff00;
  6041. reg_val |= 0x00000030;
  6042. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6043. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6044. reg_val &= 0x8cffffff;
  6045. reg_val = 0x8c000000;
  6046. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6047. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6048. reg_val &= 0xffffff00;
  6049. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6050. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6051. reg_val &= 0x00ffffff;
  6052. reg_val |= 0xb0000000;
  6053. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6054. }
  6055. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6056. struct intel_link_m_n *m_n)
  6057. {
  6058. struct drm_device *dev = crtc->base.dev;
  6059. struct drm_i915_private *dev_priv = dev->dev_private;
  6060. int pipe = crtc->pipe;
  6061. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6062. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6063. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6064. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6065. }
  6066. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6067. struct intel_link_m_n *m_n,
  6068. struct intel_link_m_n *m2_n2)
  6069. {
  6070. struct drm_device *dev = crtc->base.dev;
  6071. struct drm_i915_private *dev_priv = dev->dev_private;
  6072. int pipe = crtc->pipe;
  6073. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6074. if (INTEL_INFO(dev)->gen >= 5) {
  6075. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6076. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6077. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6078. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6079. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6080. * for gen < 8) and if DRRS is supported (to make sure the
  6081. * registers are not unnecessarily accessed).
  6082. */
  6083. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6084. crtc->config->has_drrs) {
  6085. I915_WRITE(PIPE_DATA_M2(transcoder),
  6086. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6087. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6088. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6089. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6090. }
  6091. } else {
  6092. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6093. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6094. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6095. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6096. }
  6097. }
  6098. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6099. {
  6100. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6101. if (m_n == M1_N1) {
  6102. dp_m_n = &crtc->config->dp_m_n;
  6103. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6104. } else if (m_n == M2_N2) {
  6105. /*
  6106. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6107. * needs to be programmed into M1_N1.
  6108. */
  6109. dp_m_n = &crtc->config->dp_m2_n2;
  6110. } else {
  6111. DRM_ERROR("Unsupported divider value\n");
  6112. return;
  6113. }
  6114. if (crtc->config->has_pch_encoder)
  6115. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6116. else
  6117. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6118. }
  6119. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6120. struct intel_crtc_state *pipe_config)
  6121. {
  6122. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6123. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6124. if (crtc->pipe != PIPE_A)
  6125. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6126. /* DPLL not used with DSI, but still need the rest set up */
  6127. if (!pipe_config->has_dsi_encoder)
  6128. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6129. DPLL_EXT_BUFFER_ENABLE_VLV;
  6130. pipe_config->dpll_hw_state.dpll_md =
  6131. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6132. }
  6133. static void chv_compute_dpll(struct intel_crtc *crtc,
  6134. struct intel_crtc_state *pipe_config)
  6135. {
  6136. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6137. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6138. if (crtc->pipe != PIPE_A)
  6139. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6140. /* DPLL not used with DSI, but still need the rest set up */
  6141. if (!pipe_config->has_dsi_encoder)
  6142. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6143. pipe_config->dpll_hw_state.dpll_md =
  6144. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6145. }
  6146. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6147. const struct intel_crtc_state *pipe_config)
  6148. {
  6149. struct drm_device *dev = crtc->base.dev;
  6150. struct drm_i915_private *dev_priv = dev->dev_private;
  6151. enum pipe pipe = crtc->pipe;
  6152. u32 mdiv;
  6153. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6154. u32 coreclk, reg_val;
  6155. /* Enable Refclk */
  6156. I915_WRITE(DPLL(pipe),
  6157. pipe_config->dpll_hw_state.dpll &
  6158. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6159. /* No need to actually set up the DPLL with DSI */
  6160. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6161. return;
  6162. mutex_lock(&dev_priv->sb_lock);
  6163. bestn = pipe_config->dpll.n;
  6164. bestm1 = pipe_config->dpll.m1;
  6165. bestm2 = pipe_config->dpll.m2;
  6166. bestp1 = pipe_config->dpll.p1;
  6167. bestp2 = pipe_config->dpll.p2;
  6168. /* See eDP HDMI DPIO driver vbios notes doc */
  6169. /* PLL B needs special handling */
  6170. if (pipe == PIPE_B)
  6171. vlv_pllb_recal_opamp(dev_priv, pipe);
  6172. /* Set up Tx target for periodic Rcomp update */
  6173. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6174. /* Disable target IRef on PLL */
  6175. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6176. reg_val &= 0x00ffffff;
  6177. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6178. /* Disable fast lock */
  6179. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6180. /* Set idtafcrecal before PLL is enabled */
  6181. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6182. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6183. mdiv |= ((bestn << DPIO_N_SHIFT));
  6184. mdiv |= (1 << DPIO_K_SHIFT);
  6185. /*
  6186. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6187. * but we don't support that).
  6188. * Note: don't use the DAC post divider as it seems unstable.
  6189. */
  6190. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6191. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6192. mdiv |= DPIO_ENABLE_CALIBRATION;
  6193. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6194. /* Set HBR and RBR LPF coefficients */
  6195. if (pipe_config->port_clock == 162000 ||
  6196. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6197. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6198. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6199. 0x009f0003);
  6200. else
  6201. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6202. 0x00d0000f);
  6203. if (pipe_config->has_dp_encoder) {
  6204. /* Use SSC source */
  6205. if (pipe == PIPE_A)
  6206. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6207. 0x0df40000);
  6208. else
  6209. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6210. 0x0df70000);
  6211. } else { /* HDMI or VGA */
  6212. /* Use bend source */
  6213. if (pipe == PIPE_A)
  6214. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6215. 0x0df70000);
  6216. else
  6217. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6218. 0x0df40000);
  6219. }
  6220. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6221. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6222. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6223. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6224. coreclk |= 0x01000000;
  6225. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6226. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6227. mutex_unlock(&dev_priv->sb_lock);
  6228. }
  6229. static void chv_prepare_pll(struct intel_crtc *crtc,
  6230. const struct intel_crtc_state *pipe_config)
  6231. {
  6232. struct drm_device *dev = crtc->base.dev;
  6233. struct drm_i915_private *dev_priv = dev->dev_private;
  6234. enum pipe pipe = crtc->pipe;
  6235. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6236. u32 loopfilter, tribuf_calcntr;
  6237. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6238. u32 dpio_val;
  6239. int vco;
  6240. /* Enable Refclk and SSC */
  6241. I915_WRITE(DPLL(pipe),
  6242. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6243. /* No need to actually set up the DPLL with DSI */
  6244. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6245. return;
  6246. bestn = pipe_config->dpll.n;
  6247. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6248. bestm1 = pipe_config->dpll.m1;
  6249. bestm2 = pipe_config->dpll.m2 >> 22;
  6250. bestp1 = pipe_config->dpll.p1;
  6251. bestp2 = pipe_config->dpll.p2;
  6252. vco = pipe_config->dpll.vco;
  6253. dpio_val = 0;
  6254. loopfilter = 0;
  6255. mutex_lock(&dev_priv->sb_lock);
  6256. /* p1 and p2 divider */
  6257. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6258. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6259. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6260. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6261. 1 << DPIO_CHV_K_DIV_SHIFT);
  6262. /* Feedback post-divider - m2 */
  6263. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6264. /* Feedback refclk divider - n and m1 */
  6265. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6266. DPIO_CHV_M1_DIV_BY_2 |
  6267. 1 << DPIO_CHV_N_DIV_SHIFT);
  6268. /* M2 fraction division */
  6269. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6270. /* M2 fraction division enable */
  6271. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6272. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6273. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6274. if (bestm2_frac)
  6275. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6276. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6277. /* Program digital lock detect threshold */
  6278. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6279. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6280. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6281. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6282. if (!bestm2_frac)
  6283. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6284. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6285. /* Loop filter */
  6286. if (vco == 5400000) {
  6287. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6288. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6289. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6290. tribuf_calcntr = 0x9;
  6291. } else if (vco <= 6200000) {
  6292. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6293. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6294. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6295. tribuf_calcntr = 0x9;
  6296. } else if (vco <= 6480000) {
  6297. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6298. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6299. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6300. tribuf_calcntr = 0x8;
  6301. } else {
  6302. /* Not supported. Apply the same limits as in the max case */
  6303. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6304. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6305. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6306. tribuf_calcntr = 0;
  6307. }
  6308. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6309. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6310. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6311. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6312. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6313. /* AFC Recal */
  6314. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6315. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6316. DPIO_AFC_RECAL);
  6317. mutex_unlock(&dev_priv->sb_lock);
  6318. }
  6319. /**
  6320. * vlv_force_pll_on - forcibly enable just the PLL
  6321. * @dev_priv: i915 private structure
  6322. * @pipe: pipe PLL to enable
  6323. * @dpll: PLL configuration
  6324. *
  6325. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6326. * in cases where we need the PLL enabled even when @pipe is not going to
  6327. * be enabled.
  6328. */
  6329. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6330. const struct dpll *dpll)
  6331. {
  6332. struct intel_crtc *crtc =
  6333. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6334. struct intel_crtc_state *pipe_config;
  6335. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6336. if (!pipe_config)
  6337. return -ENOMEM;
  6338. pipe_config->base.crtc = &crtc->base;
  6339. pipe_config->pixel_multiplier = 1;
  6340. pipe_config->dpll = *dpll;
  6341. if (IS_CHERRYVIEW(dev)) {
  6342. chv_compute_dpll(crtc, pipe_config);
  6343. chv_prepare_pll(crtc, pipe_config);
  6344. chv_enable_pll(crtc, pipe_config);
  6345. } else {
  6346. vlv_compute_dpll(crtc, pipe_config);
  6347. vlv_prepare_pll(crtc, pipe_config);
  6348. vlv_enable_pll(crtc, pipe_config);
  6349. }
  6350. kfree(pipe_config);
  6351. return 0;
  6352. }
  6353. /**
  6354. * vlv_force_pll_off - forcibly disable just the PLL
  6355. * @dev_priv: i915 private structure
  6356. * @pipe: pipe PLL to disable
  6357. *
  6358. * Disable the PLL for @pipe. To be used in cases where we need
  6359. * the PLL enabled even when @pipe is not going to be enabled.
  6360. */
  6361. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6362. {
  6363. if (IS_CHERRYVIEW(dev))
  6364. chv_disable_pll(to_i915(dev), pipe);
  6365. else
  6366. vlv_disable_pll(to_i915(dev), pipe);
  6367. }
  6368. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6369. struct intel_crtc_state *crtc_state,
  6370. struct dpll *reduced_clock)
  6371. {
  6372. struct drm_device *dev = crtc->base.dev;
  6373. struct drm_i915_private *dev_priv = dev->dev_private;
  6374. u32 dpll;
  6375. bool is_sdvo;
  6376. struct dpll *clock = &crtc_state->dpll;
  6377. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6378. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6379. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6380. dpll = DPLL_VGA_MODE_DIS;
  6381. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6382. dpll |= DPLLB_MODE_LVDS;
  6383. else
  6384. dpll |= DPLLB_MODE_DAC_SERIAL;
  6385. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6386. dpll |= (crtc_state->pixel_multiplier - 1)
  6387. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6388. }
  6389. if (is_sdvo)
  6390. dpll |= DPLL_SDVO_HIGH_SPEED;
  6391. if (crtc_state->has_dp_encoder)
  6392. dpll |= DPLL_SDVO_HIGH_SPEED;
  6393. /* compute bitmask from p1 value */
  6394. if (IS_PINEVIEW(dev))
  6395. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6396. else {
  6397. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6398. if (IS_G4X(dev) && reduced_clock)
  6399. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6400. }
  6401. switch (clock->p2) {
  6402. case 5:
  6403. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6404. break;
  6405. case 7:
  6406. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6407. break;
  6408. case 10:
  6409. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6410. break;
  6411. case 14:
  6412. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6413. break;
  6414. }
  6415. if (INTEL_INFO(dev)->gen >= 4)
  6416. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6417. if (crtc_state->sdvo_tv_clock)
  6418. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6419. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6420. intel_panel_use_ssc(dev_priv))
  6421. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6422. else
  6423. dpll |= PLL_REF_INPUT_DREFCLK;
  6424. dpll |= DPLL_VCO_ENABLE;
  6425. crtc_state->dpll_hw_state.dpll = dpll;
  6426. if (INTEL_INFO(dev)->gen >= 4) {
  6427. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6428. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6429. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6430. }
  6431. }
  6432. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6433. struct intel_crtc_state *crtc_state,
  6434. struct dpll *reduced_clock)
  6435. {
  6436. struct drm_device *dev = crtc->base.dev;
  6437. struct drm_i915_private *dev_priv = dev->dev_private;
  6438. u32 dpll;
  6439. struct dpll *clock = &crtc_state->dpll;
  6440. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6441. dpll = DPLL_VGA_MODE_DIS;
  6442. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6443. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6444. } else {
  6445. if (clock->p1 == 2)
  6446. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6447. else
  6448. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6449. if (clock->p2 == 4)
  6450. dpll |= PLL_P2_DIVIDE_BY_4;
  6451. }
  6452. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6453. dpll |= DPLL_DVO_2X_MODE;
  6454. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6455. intel_panel_use_ssc(dev_priv))
  6456. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6457. else
  6458. dpll |= PLL_REF_INPUT_DREFCLK;
  6459. dpll |= DPLL_VCO_ENABLE;
  6460. crtc_state->dpll_hw_state.dpll = dpll;
  6461. }
  6462. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6463. {
  6464. struct drm_device *dev = intel_crtc->base.dev;
  6465. struct drm_i915_private *dev_priv = dev->dev_private;
  6466. enum pipe pipe = intel_crtc->pipe;
  6467. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6468. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6469. uint32_t crtc_vtotal, crtc_vblank_end;
  6470. int vsyncshift = 0;
  6471. /* We need to be careful not to changed the adjusted mode, for otherwise
  6472. * the hw state checker will get angry at the mismatch. */
  6473. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6474. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6475. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6476. /* the chip adds 2 halflines automatically */
  6477. crtc_vtotal -= 1;
  6478. crtc_vblank_end -= 1;
  6479. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6480. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6481. else
  6482. vsyncshift = adjusted_mode->crtc_hsync_start -
  6483. adjusted_mode->crtc_htotal / 2;
  6484. if (vsyncshift < 0)
  6485. vsyncshift += adjusted_mode->crtc_htotal;
  6486. }
  6487. if (INTEL_INFO(dev)->gen > 3)
  6488. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6489. I915_WRITE(HTOTAL(cpu_transcoder),
  6490. (adjusted_mode->crtc_hdisplay - 1) |
  6491. ((adjusted_mode->crtc_htotal - 1) << 16));
  6492. I915_WRITE(HBLANK(cpu_transcoder),
  6493. (adjusted_mode->crtc_hblank_start - 1) |
  6494. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6495. I915_WRITE(HSYNC(cpu_transcoder),
  6496. (adjusted_mode->crtc_hsync_start - 1) |
  6497. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6498. I915_WRITE(VTOTAL(cpu_transcoder),
  6499. (adjusted_mode->crtc_vdisplay - 1) |
  6500. ((crtc_vtotal - 1) << 16));
  6501. I915_WRITE(VBLANK(cpu_transcoder),
  6502. (adjusted_mode->crtc_vblank_start - 1) |
  6503. ((crtc_vblank_end - 1) << 16));
  6504. I915_WRITE(VSYNC(cpu_transcoder),
  6505. (adjusted_mode->crtc_vsync_start - 1) |
  6506. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6507. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6508. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6509. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6510. * bits. */
  6511. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6512. (pipe == PIPE_B || pipe == PIPE_C))
  6513. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6514. }
  6515. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6516. {
  6517. struct drm_device *dev = intel_crtc->base.dev;
  6518. struct drm_i915_private *dev_priv = dev->dev_private;
  6519. enum pipe pipe = intel_crtc->pipe;
  6520. /* pipesrc controls the size that is scaled from, which should
  6521. * always be the user's requested size.
  6522. */
  6523. I915_WRITE(PIPESRC(pipe),
  6524. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6525. (intel_crtc->config->pipe_src_h - 1));
  6526. }
  6527. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6528. struct intel_crtc_state *pipe_config)
  6529. {
  6530. struct drm_device *dev = crtc->base.dev;
  6531. struct drm_i915_private *dev_priv = dev->dev_private;
  6532. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6533. uint32_t tmp;
  6534. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6535. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6536. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6537. tmp = I915_READ(HBLANK(cpu_transcoder));
  6538. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6539. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6540. tmp = I915_READ(HSYNC(cpu_transcoder));
  6541. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6542. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6543. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6544. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6545. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6546. tmp = I915_READ(VBLANK(cpu_transcoder));
  6547. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6548. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6549. tmp = I915_READ(VSYNC(cpu_transcoder));
  6550. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6551. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6552. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6553. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6554. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6555. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6556. }
  6557. }
  6558. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6559. struct intel_crtc_state *pipe_config)
  6560. {
  6561. struct drm_device *dev = crtc->base.dev;
  6562. struct drm_i915_private *dev_priv = dev->dev_private;
  6563. u32 tmp;
  6564. tmp = I915_READ(PIPESRC(crtc->pipe));
  6565. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6566. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6567. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6568. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6569. }
  6570. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6571. struct intel_crtc_state *pipe_config)
  6572. {
  6573. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6574. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6575. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6576. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6577. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6578. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6579. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6580. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6581. mode->flags = pipe_config->base.adjusted_mode.flags;
  6582. mode->type = DRM_MODE_TYPE_DRIVER;
  6583. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6584. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6585. mode->hsync = drm_mode_hsync(mode);
  6586. mode->vrefresh = drm_mode_vrefresh(mode);
  6587. drm_mode_set_name(mode);
  6588. }
  6589. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6590. {
  6591. struct drm_device *dev = intel_crtc->base.dev;
  6592. struct drm_i915_private *dev_priv = dev->dev_private;
  6593. uint32_t pipeconf;
  6594. pipeconf = 0;
  6595. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6596. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6597. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6598. if (intel_crtc->config->double_wide)
  6599. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6600. /* only g4x and later have fancy bpc/dither controls */
  6601. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6602. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6603. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6604. pipeconf |= PIPECONF_DITHER_EN |
  6605. PIPECONF_DITHER_TYPE_SP;
  6606. switch (intel_crtc->config->pipe_bpp) {
  6607. case 18:
  6608. pipeconf |= PIPECONF_6BPC;
  6609. break;
  6610. case 24:
  6611. pipeconf |= PIPECONF_8BPC;
  6612. break;
  6613. case 30:
  6614. pipeconf |= PIPECONF_10BPC;
  6615. break;
  6616. default:
  6617. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6618. BUG();
  6619. }
  6620. }
  6621. if (HAS_PIPE_CXSR(dev)) {
  6622. if (intel_crtc->lowfreq_avail) {
  6623. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6624. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6625. } else {
  6626. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6627. }
  6628. }
  6629. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6630. if (INTEL_INFO(dev)->gen < 4 ||
  6631. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6632. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6633. else
  6634. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6635. } else
  6636. pipeconf |= PIPECONF_PROGRESSIVE;
  6637. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6638. intel_crtc->config->limited_color_range)
  6639. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6640. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6641. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6642. }
  6643. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6644. struct intel_crtc_state *crtc_state)
  6645. {
  6646. struct drm_device *dev = crtc->base.dev;
  6647. struct drm_i915_private *dev_priv = dev->dev_private;
  6648. const struct intel_limit *limit;
  6649. int refclk = 48000;
  6650. memset(&crtc_state->dpll_hw_state, 0,
  6651. sizeof(crtc_state->dpll_hw_state));
  6652. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6653. if (intel_panel_use_ssc(dev_priv)) {
  6654. refclk = dev_priv->vbt.lvds_ssc_freq;
  6655. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6656. }
  6657. limit = &intel_limits_i8xx_lvds;
  6658. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6659. limit = &intel_limits_i8xx_dvo;
  6660. } else {
  6661. limit = &intel_limits_i8xx_dac;
  6662. }
  6663. if (!crtc_state->clock_set &&
  6664. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6665. refclk, NULL, &crtc_state->dpll)) {
  6666. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6667. return -EINVAL;
  6668. }
  6669. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6670. return 0;
  6671. }
  6672. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6673. struct intel_crtc_state *crtc_state)
  6674. {
  6675. struct drm_device *dev = crtc->base.dev;
  6676. struct drm_i915_private *dev_priv = dev->dev_private;
  6677. const struct intel_limit *limit;
  6678. int refclk = 96000;
  6679. memset(&crtc_state->dpll_hw_state, 0,
  6680. sizeof(crtc_state->dpll_hw_state));
  6681. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6682. if (intel_panel_use_ssc(dev_priv)) {
  6683. refclk = dev_priv->vbt.lvds_ssc_freq;
  6684. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6685. }
  6686. if (intel_is_dual_link_lvds(dev))
  6687. limit = &intel_limits_g4x_dual_channel_lvds;
  6688. else
  6689. limit = &intel_limits_g4x_single_channel_lvds;
  6690. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6691. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6692. limit = &intel_limits_g4x_hdmi;
  6693. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6694. limit = &intel_limits_g4x_sdvo;
  6695. } else {
  6696. /* The option is for other outputs */
  6697. limit = &intel_limits_i9xx_sdvo;
  6698. }
  6699. if (!crtc_state->clock_set &&
  6700. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6701. refclk, NULL, &crtc_state->dpll)) {
  6702. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6703. return -EINVAL;
  6704. }
  6705. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6706. return 0;
  6707. }
  6708. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6709. struct intel_crtc_state *crtc_state)
  6710. {
  6711. struct drm_device *dev = crtc->base.dev;
  6712. struct drm_i915_private *dev_priv = dev->dev_private;
  6713. const struct intel_limit *limit;
  6714. int refclk = 96000;
  6715. memset(&crtc_state->dpll_hw_state, 0,
  6716. sizeof(crtc_state->dpll_hw_state));
  6717. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6718. if (intel_panel_use_ssc(dev_priv)) {
  6719. refclk = dev_priv->vbt.lvds_ssc_freq;
  6720. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6721. }
  6722. limit = &intel_limits_pineview_lvds;
  6723. } else {
  6724. limit = &intel_limits_pineview_sdvo;
  6725. }
  6726. if (!crtc_state->clock_set &&
  6727. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6728. refclk, NULL, &crtc_state->dpll)) {
  6729. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6730. return -EINVAL;
  6731. }
  6732. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6733. return 0;
  6734. }
  6735. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6736. struct intel_crtc_state *crtc_state)
  6737. {
  6738. struct drm_device *dev = crtc->base.dev;
  6739. struct drm_i915_private *dev_priv = dev->dev_private;
  6740. const struct intel_limit *limit;
  6741. int refclk = 96000;
  6742. memset(&crtc_state->dpll_hw_state, 0,
  6743. sizeof(crtc_state->dpll_hw_state));
  6744. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6745. if (intel_panel_use_ssc(dev_priv)) {
  6746. refclk = dev_priv->vbt.lvds_ssc_freq;
  6747. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6748. }
  6749. limit = &intel_limits_i9xx_lvds;
  6750. } else {
  6751. limit = &intel_limits_i9xx_sdvo;
  6752. }
  6753. if (!crtc_state->clock_set &&
  6754. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6755. refclk, NULL, &crtc_state->dpll)) {
  6756. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6757. return -EINVAL;
  6758. }
  6759. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6760. return 0;
  6761. }
  6762. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6763. struct intel_crtc_state *crtc_state)
  6764. {
  6765. int refclk = 100000;
  6766. const struct intel_limit *limit = &intel_limits_chv;
  6767. memset(&crtc_state->dpll_hw_state, 0,
  6768. sizeof(crtc_state->dpll_hw_state));
  6769. if (!crtc_state->clock_set &&
  6770. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6771. refclk, NULL, &crtc_state->dpll)) {
  6772. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6773. return -EINVAL;
  6774. }
  6775. chv_compute_dpll(crtc, crtc_state);
  6776. return 0;
  6777. }
  6778. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6779. struct intel_crtc_state *crtc_state)
  6780. {
  6781. int refclk = 100000;
  6782. const struct intel_limit *limit = &intel_limits_vlv;
  6783. memset(&crtc_state->dpll_hw_state, 0,
  6784. sizeof(crtc_state->dpll_hw_state));
  6785. if (!crtc_state->clock_set &&
  6786. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6787. refclk, NULL, &crtc_state->dpll)) {
  6788. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6789. return -EINVAL;
  6790. }
  6791. vlv_compute_dpll(crtc, crtc_state);
  6792. return 0;
  6793. }
  6794. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6795. struct intel_crtc_state *pipe_config)
  6796. {
  6797. struct drm_device *dev = crtc->base.dev;
  6798. struct drm_i915_private *dev_priv = dev->dev_private;
  6799. uint32_t tmp;
  6800. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6801. return;
  6802. tmp = I915_READ(PFIT_CONTROL);
  6803. if (!(tmp & PFIT_ENABLE))
  6804. return;
  6805. /* Check whether the pfit is attached to our pipe. */
  6806. if (INTEL_INFO(dev)->gen < 4) {
  6807. if (crtc->pipe != PIPE_B)
  6808. return;
  6809. } else {
  6810. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6811. return;
  6812. }
  6813. pipe_config->gmch_pfit.control = tmp;
  6814. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6815. }
  6816. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6817. struct intel_crtc_state *pipe_config)
  6818. {
  6819. struct drm_device *dev = crtc->base.dev;
  6820. struct drm_i915_private *dev_priv = dev->dev_private;
  6821. int pipe = pipe_config->cpu_transcoder;
  6822. struct dpll clock;
  6823. u32 mdiv;
  6824. int refclk = 100000;
  6825. /* In case of DSI, DPLL will not be used */
  6826. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6827. return;
  6828. mutex_lock(&dev_priv->sb_lock);
  6829. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6830. mutex_unlock(&dev_priv->sb_lock);
  6831. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6832. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6833. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6834. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6835. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6836. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6837. }
  6838. static void
  6839. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6840. struct intel_initial_plane_config *plane_config)
  6841. {
  6842. struct drm_device *dev = crtc->base.dev;
  6843. struct drm_i915_private *dev_priv = dev->dev_private;
  6844. u32 val, base, offset;
  6845. int pipe = crtc->pipe, plane = crtc->plane;
  6846. int fourcc, pixel_format;
  6847. unsigned int aligned_height;
  6848. struct drm_framebuffer *fb;
  6849. struct intel_framebuffer *intel_fb;
  6850. val = I915_READ(DSPCNTR(plane));
  6851. if (!(val & DISPLAY_PLANE_ENABLE))
  6852. return;
  6853. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6854. if (!intel_fb) {
  6855. DRM_DEBUG_KMS("failed to alloc fb\n");
  6856. return;
  6857. }
  6858. fb = &intel_fb->base;
  6859. if (INTEL_INFO(dev)->gen >= 4) {
  6860. if (val & DISPPLANE_TILED) {
  6861. plane_config->tiling = I915_TILING_X;
  6862. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6863. }
  6864. }
  6865. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6866. fourcc = i9xx_format_to_fourcc(pixel_format);
  6867. fb->pixel_format = fourcc;
  6868. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6869. if (INTEL_INFO(dev)->gen >= 4) {
  6870. if (plane_config->tiling)
  6871. offset = I915_READ(DSPTILEOFF(plane));
  6872. else
  6873. offset = I915_READ(DSPLINOFF(plane));
  6874. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6875. } else {
  6876. base = I915_READ(DSPADDR(plane));
  6877. }
  6878. plane_config->base = base;
  6879. val = I915_READ(PIPESRC(pipe));
  6880. fb->width = ((val >> 16) & 0xfff) + 1;
  6881. fb->height = ((val >> 0) & 0xfff) + 1;
  6882. val = I915_READ(DSPSTRIDE(pipe));
  6883. fb->pitches[0] = val & 0xffffffc0;
  6884. aligned_height = intel_fb_align_height(dev, fb->height,
  6885. fb->pixel_format,
  6886. fb->modifier[0]);
  6887. plane_config->size = fb->pitches[0] * aligned_height;
  6888. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6889. pipe_name(pipe), plane, fb->width, fb->height,
  6890. fb->bits_per_pixel, base, fb->pitches[0],
  6891. plane_config->size);
  6892. plane_config->fb = intel_fb;
  6893. }
  6894. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6895. struct intel_crtc_state *pipe_config)
  6896. {
  6897. struct drm_device *dev = crtc->base.dev;
  6898. struct drm_i915_private *dev_priv = dev->dev_private;
  6899. int pipe = pipe_config->cpu_transcoder;
  6900. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6901. struct dpll clock;
  6902. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6903. int refclk = 100000;
  6904. /* In case of DSI, DPLL will not be used */
  6905. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6906. return;
  6907. mutex_lock(&dev_priv->sb_lock);
  6908. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6909. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6910. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6911. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6912. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6913. mutex_unlock(&dev_priv->sb_lock);
  6914. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6915. clock.m2 = (pll_dw0 & 0xff) << 22;
  6916. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6917. clock.m2 |= pll_dw2 & 0x3fffff;
  6918. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6919. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6920. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6921. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6922. }
  6923. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6924. struct intel_crtc_state *pipe_config)
  6925. {
  6926. struct drm_device *dev = crtc->base.dev;
  6927. struct drm_i915_private *dev_priv = dev->dev_private;
  6928. enum intel_display_power_domain power_domain;
  6929. uint32_t tmp;
  6930. bool ret;
  6931. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6932. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6933. return false;
  6934. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6935. pipe_config->shared_dpll = NULL;
  6936. ret = false;
  6937. tmp = I915_READ(PIPECONF(crtc->pipe));
  6938. if (!(tmp & PIPECONF_ENABLE))
  6939. goto out;
  6940. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6941. switch (tmp & PIPECONF_BPC_MASK) {
  6942. case PIPECONF_6BPC:
  6943. pipe_config->pipe_bpp = 18;
  6944. break;
  6945. case PIPECONF_8BPC:
  6946. pipe_config->pipe_bpp = 24;
  6947. break;
  6948. case PIPECONF_10BPC:
  6949. pipe_config->pipe_bpp = 30;
  6950. break;
  6951. default:
  6952. break;
  6953. }
  6954. }
  6955. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6956. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6957. pipe_config->limited_color_range = true;
  6958. if (INTEL_INFO(dev)->gen < 4)
  6959. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6960. intel_get_pipe_timings(crtc, pipe_config);
  6961. intel_get_pipe_src_size(crtc, pipe_config);
  6962. i9xx_get_pfit_config(crtc, pipe_config);
  6963. if (INTEL_INFO(dev)->gen >= 4) {
  6964. /* No way to read it out on pipes B and C */
  6965. if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
  6966. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6967. else
  6968. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6969. pipe_config->pixel_multiplier =
  6970. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6971. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6972. pipe_config->dpll_hw_state.dpll_md = tmp;
  6973. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6974. tmp = I915_READ(DPLL(crtc->pipe));
  6975. pipe_config->pixel_multiplier =
  6976. ((tmp & SDVO_MULTIPLIER_MASK)
  6977. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6978. } else {
  6979. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6980. * port and will be fixed up in the encoder->get_config
  6981. * function. */
  6982. pipe_config->pixel_multiplier = 1;
  6983. }
  6984. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6985. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  6986. /*
  6987. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6988. * on 830. Filter it out here so that we don't
  6989. * report errors due to that.
  6990. */
  6991. if (IS_I830(dev))
  6992. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6993. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6994. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6995. } else {
  6996. /* Mask out read-only status bits. */
  6997. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6998. DPLL_PORTC_READY_MASK |
  6999. DPLL_PORTB_READY_MASK);
  7000. }
  7001. if (IS_CHERRYVIEW(dev))
  7002. chv_crtc_clock_get(crtc, pipe_config);
  7003. else if (IS_VALLEYVIEW(dev))
  7004. vlv_crtc_clock_get(crtc, pipe_config);
  7005. else
  7006. i9xx_crtc_clock_get(crtc, pipe_config);
  7007. /*
  7008. * Normally the dotclock is filled in by the encoder .get_config()
  7009. * but in case the pipe is enabled w/o any ports we need a sane
  7010. * default.
  7011. */
  7012. pipe_config->base.adjusted_mode.crtc_clock =
  7013. pipe_config->port_clock / pipe_config->pixel_multiplier;
  7014. ret = true;
  7015. out:
  7016. intel_display_power_put(dev_priv, power_domain);
  7017. return ret;
  7018. }
  7019. static void ironlake_init_pch_refclk(struct drm_device *dev)
  7020. {
  7021. struct drm_i915_private *dev_priv = dev->dev_private;
  7022. struct intel_encoder *encoder;
  7023. int i;
  7024. u32 val, final;
  7025. bool has_lvds = false;
  7026. bool has_cpu_edp = false;
  7027. bool has_panel = false;
  7028. bool has_ck505 = false;
  7029. bool can_ssc = false;
  7030. bool using_ssc_source = false;
  7031. /* We need to take the global config into account */
  7032. for_each_intel_encoder(dev, encoder) {
  7033. switch (encoder->type) {
  7034. case INTEL_OUTPUT_LVDS:
  7035. has_panel = true;
  7036. has_lvds = true;
  7037. break;
  7038. case INTEL_OUTPUT_EDP:
  7039. has_panel = true;
  7040. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7041. has_cpu_edp = true;
  7042. break;
  7043. default:
  7044. break;
  7045. }
  7046. }
  7047. if (HAS_PCH_IBX(dev)) {
  7048. has_ck505 = dev_priv->vbt.display_clock_mode;
  7049. can_ssc = has_ck505;
  7050. } else {
  7051. has_ck505 = false;
  7052. can_ssc = true;
  7053. }
  7054. /* Check if any DPLLs are using the SSC source */
  7055. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7056. u32 temp = I915_READ(PCH_DPLL(i));
  7057. if (!(temp & DPLL_VCO_ENABLE))
  7058. continue;
  7059. if ((temp & PLL_REF_INPUT_MASK) ==
  7060. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7061. using_ssc_source = true;
  7062. break;
  7063. }
  7064. }
  7065. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7066. has_panel, has_lvds, has_ck505, using_ssc_source);
  7067. /* Ironlake: try to setup display ref clock before DPLL
  7068. * enabling. This is only under driver's control after
  7069. * PCH B stepping, previous chipset stepping should be
  7070. * ignoring this setting.
  7071. */
  7072. val = I915_READ(PCH_DREF_CONTROL);
  7073. /* As we must carefully and slowly disable/enable each source in turn,
  7074. * compute the final state we want first and check if we need to
  7075. * make any changes at all.
  7076. */
  7077. final = val;
  7078. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7079. if (has_ck505)
  7080. final |= DREF_NONSPREAD_CK505_ENABLE;
  7081. else
  7082. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7083. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7084. if (!using_ssc_source) {
  7085. final &= ~DREF_SSC_SOURCE_MASK;
  7086. final &= ~DREF_SSC1_ENABLE;
  7087. }
  7088. if (has_panel) {
  7089. final |= DREF_SSC_SOURCE_ENABLE;
  7090. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7091. final |= DREF_SSC1_ENABLE;
  7092. if (has_cpu_edp) {
  7093. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7094. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7095. else
  7096. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7097. } else
  7098. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7099. } else {
  7100. final |= DREF_SSC_SOURCE_DISABLE;
  7101. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7102. }
  7103. if (final == val)
  7104. return;
  7105. /* Always enable nonspread source */
  7106. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7107. if (has_ck505)
  7108. val |= DREF_NONSPREAD_CK505_ENABLE;
  7109. else
  7110. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7111. if (has_panel) {
  7112. val &= ~DREF_SSC_SOURCE_MASK;
  7113. val |= DREF_SSC_SOURCE_ENABLE;
  7114. /* SSC must be turned on before enabling the CPU output */
  7115. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7116. DRM_DEBUG_KMS("Using SSC on panel\n");
  7117. val |= DREF_SSC1_ENABLE;
  7118. } else
  7119. val &= ~DREF_SSC1_ENABLE;
  7120. /* Get SSC going before enabling the outputs */
  7121. I915_WRITE(PCH_DREF_CONTROL, val);
  7122. POSTING_READ(PCH_DREF_CONTROL);
  7123. udelay(200);
  7124. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7125. /* Enable CPU source on CPU attached eDP */
  7126. if (has_cpu_edp) {
  7127. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7128. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7129. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7130. } else
  7131. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7132. } else
  7133. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7134. I915_WRITE(PCH_DREF_CONTROL, val);
  7135. POSTING_READ(PCH_DREF_CONTROL);
  7136. udelay(200);
  7137. } else {
  7138. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7139. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7140. /* Turn off CPU output */
  7141. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7142. I915_WRITE(PCH_DREF_CONTROL, val);
  7143. POSTING_READ(PCH_DREF_CONTROL);
  7144. udelay(200);
  7145. if (!using_ssc_source) {
  7146. DRM_DEBUG_KMS("Disabling SSC source\n");
  7147. /* Turn off the SSC source */
  7148. val &= ~DREF_SSC_SOURCE_MASK;
  7149. val |= DREF_SSC_SOURCE_DISABLE;
  7150. /* Turn off SSC1 */
  7151. val &= ~DREF_SSC1_ENABLE;
  7152. I915_WRITE(PCH_DREF_CONTROL, val);
  7153. POSTING_READ(PCH_DREF_CONTROL);
  7154. udelay(200);
  7155. }
  7156. }
  7157. BUG_ON(val != final);
  7158. }
  7159. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7160. {
  7161. uint32_t tmp;
  7162. tmp = I915_READ(SOUTH_CHICKEN2);
  7163. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7164. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7165. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7166. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7167. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7168. tmp = I915_READ(SOUTH_CHICKEN2);
  7169. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7170. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7171. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7172. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7173. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7174. }
  7175. /* WaMPhyProgramming:hsw */
  7176. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7177. {
  7178. uint32_t tmp;
  7179. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7180. tmp &= ~(0xFF << 24);
  7181. tmp |= (0x12 << 24);
  7182. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7183. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7184. tmp |= (1 << 11);
  7185. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7186. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7187. tmp |= (1 << 11);
  7188. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7189. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7190. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7191. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7192. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7193. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7194. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7195. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7196. tmp &= ~(7 << 13);
  7197. tmp |= (5 << 13);
  7198. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7199. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7200. tmp &= ~(7 << 13);
  7201. tmp |= (5 << 13);
  7202. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7203. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7204. tmp &= ~0xFF;
  7205. tmp |= 0x1C;
  7206. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7207. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7208. tmp &= ~0xFF;
  7209. tmp |= 0x1C;
  7210. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7211. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7212. tmp &= ~(0xFF << 16);
  7213. tmp |= (0x1C << 16);
  7214. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7215. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7216. tmp &= ~(0xFF << 16);
  7217. tmp |= (0x1C << 16);
  7218. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7219. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7220. tmp |= (1 << 27);
  7221. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7222. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7223. tmp |= (1 << 27);
  7224. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7225. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7226. tmp &= ~(0xF << 28);
  7227. tmp |= (4 << 28);
  7228. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7229. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7230. tmp &= ~(0xF << 28);
  7231. tmp |= (4 << 28);
  7232. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7233. }
  7234. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7235. * Programming" based on the parameters passed:
  7236. * - Sequence to enable CLKOUT_DP
  7237. * - Sequence to enable CLKOUT_DP without spread
  7238. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7239. */
  7240. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7241. bool with_fdi)
  7242. {
  7243. struct drm_i915_private *dev_priv = dev->dev_private;
  7244. uint32_t reg, tmp;
  7245. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7246. with_spread = true;
  7247. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7248. with_fdi = false;
  7249. mutex_lock(&dev_priv->sb_lock);
  7250. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7251. tmp &= ~SBI_SSCCTL_DISABLE;
  7252. tmp |= SBI_SSCCTL_PATHALT;
  7253. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7254. udelay(24);
  7255. if (with_spread) {
  7256. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7257. tmp &= ~SBI_SSCCTL_PATHALT;
  7258. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7259. if (with_fdi) {
  7260. lpt_reset_fdi_mphy(dev_priv);
  7261. lpt_program_fdi_mphy(dev_priv);
  7262. }
  7263. }
  7264. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7265. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7266. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7267. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7268. mutex_unlock(&dev_priv->sb_lock);
  7269. }
  7270. /* Sequence to disable CLKOUT_DP */
  7271. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7272. {
  7273. struct drm_i915_private *dev_priv = dev->dev_private;
  7274. uint32_t reg, tmp;
  7275. mutex_lock(&dev_priv->sb_lock);
  7276. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7277. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7278. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7279. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7280. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7281. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7282. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7283. tmp |= SBI_SSCCTL_PATHALT;
  7284. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7285. udelay(32);
  7286. }
  7287. tmp |= SBI_SSCCTL_DISABLE;
  7288. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7289. }
  7290. mutex_unlock(&dev_priv->sb_lock);
  7291. }
  7292. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7293. static const uint16_t sscdivintphase[] = {
  7294. [BEND_IDX( 50)] = 0x3B23,
  7295. [BEND_IDX( 45)] = 0x3B23,
  7296. [BEND_IDX( 40)] = 0x3C23,
  7297. [BEND_IDX( 35)] = 0x3C23,
  7298. [BEND_IDX( 30)] = 0x3D23,
  7299. [BEND_IDX( 25)] = 0x3D23,
  7300. [BEND_IDX( 20)] = 0x3E23,
  7301. [BEND_IDX( 15)] = 0x3E23,
  7302. [BEND_IDX( 10)] = 0x3F23,
  7303. [BEND_IDX( 5)] = 0x3F23,
  7304. [BEND_IDX( 0)] = 0x0025,
  7305. [BEND_IDX( -5)] = 0x0025,
  7306. [BEND_IDX(-10)] = 0x0125,
  7307. [BEND_IDX(-15)] = 0x0125,
  7308. [BEND_IDX(-20)] = 0x0225,
  7309. [BEND_IDX(-25)] = 0x0225,
  7310. [BEND_IDX(-30)] = 0x0325,
  7311. [BEND_IDX(-35)] = 0x0325,
  7312. [BEND_IDX(-40)] = 0x0425,
  7313. [BEND_IDX(-45)] = 0x0425,
  7314. [BEND_IDX(-50)] = 0x0525,
  7315. };
  7316. /*
  7317. * Bend CLKOUT_DP
  7318. * steps -50 to 50 inclusive, in steps of 5
  7319. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7320. * change in clock period = -(steps / 10) * 5.787 ps
  7321. */
  7322. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7323. {
  7324. uint32_t tmp;
  7325. int idx = BEND_IDX(steps);
  7326. if (WARN_ON(steps % 5 != 0))
  7327. return;
  7328. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7329. return;
  7330. mutex_lock(&dev_priv->sb_lock);
  7331. if (steps % 10 != 0)
  7332. tmp = 0xAAAAAAAB;
  7333. else
  7334. tmp = 0x00000000;
  7335. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7336. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7337. tmp &= 0xffff0000;
  7338. tmp |= sscdivintphase[idx];
  7339. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7340. mutex_unlock(&dev_priv->sb_lock);
  7341. }
  7342. #undef BEND_IDX
  7343. static void lpt_init_pch_refclk(struct drm_device *dev)
  7344. {
  7345. struct intel_encoder *encoder;
  7346. bool has_vga = false;
  7347. for_each_intel_encoder(dev, encoder) {
  7348. switch (encoder->type) {
  7349. case INTEL_OUTPUT_ANALOG:
  7350. has_vga = true;
  7351. break;
  7352. default:
  7353. break;
  7354. }
  7355. }
  7356. if (has_vga) {
  7357. lpt_bend_clkout_dp(to_i915(dev), 0);
  7358. lpt_enable_clkout_dp(dev, true, true);
  7359. } else {
  7360. lpt_disable_clkout_dp(dev);
  7361. }
  7362. }
  7363. /*
  7364. * Initialize reference clocks when the driver loads
  7365. */
  7366. void intel_init_pch_refclk(struct drm_device *dev)
  7367. {
  7368. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7369. ironlake_init_pch_refclk(dev);
  7370. else if (HAS_PCH_LPT(dev))
  7371. lpt_init_pch_refclk(dev);
  7372. }
  7373. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7374. {
  7375. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7376. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7377. int pipe = intel_crtc->pipe;
  7378. uint32_t val;
  7379. val = 0;
  7380. switch (intel_crtc->config->pipe_bpp) {
  7381. case 18:
  7382. val |= PIPECONF_6BPC;
  7383. break;
  7384. case 24:
  7385. val |= PIPECONF_8BPC;
  7386. break;
  7387. case 30:
  7388. val |= PIPECONF_10BPC;
  7389. break;
  7390. case 36:
  7391. val |= PIPECONF_12BPC;
  7392. break;
  7393. default:
  7394. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7395. BUG();
  7396. }
  7397. if (intel_crtc->config->dither)
  7398. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7399. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7400. val |= PIPECONF_INTERLACED_ILK;
  7401. else
  7402. val |= PIPECONF_PROGRESSIVE;
  7403. if (intel_crtc->config->limited_color_range)
  7404. val |= PIPECONF_COLOR_RANGE_SELECT;
  7405. I915_WRITE(PIPECONF(pipe), val);
  7406. POSTING_READ(PIPECONF(pipe));
  7407. }
  7408. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7409. {
  7410. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7411. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7412. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7413. u32 val = 0;
  7414. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7415. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7416. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7417. val |= PIPECONF_INTERLACED_ILK;
  7418. else
  7419. val |= PIPECONF_PROGRESSIVE;
  7420. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7421. POSTING_READ(PIPECONF(cpu_transcoder));
  7422. }
  7423. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7424. {
  7425. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7426. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7427. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7428. u32 val = 0;
  7429. switch (intel_crtc->config->pipe_bpp) {
  7430. case 18:
  7431. val |= PIPEMISC_DITHER_6_BPC;
  7432. break;
  7433. case 24:
  7434. val |= PIPEMISC_DITHER_8_BPC;
  7435. break;
  7436. case 30:
  7437. val |= PIPEMISC_DITHER_10_BPC;
  7438. break;
  7439. case 36:
  7440. val |= PIPEMISC_DITHER_12_BPC;
  7441. break;
  7442. default:
  7443. /* Case prevented by pipe_config_set_bpp. */
  7444. BUG();
  7445. }
  7446. if (intel_crtc->config->dither)
  7447. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7448. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7449. }
  7450. }
  7451. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7452. {
  7453. /*
  7454. * Account for spread spectrum to avoid
  7455. * oversubscribing the link. Max center spread
  7456. * is 2.5%; use 5% for safety's sake.
  7457. */
  7458. u32 bps = target_clock * bpp * 21 / 20;
  7459. return DIV_ROUND_UP(bps, link_bw * 8);
  7460. }
  7461. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7462. {
  7463. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7464. }
  7465. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7466. struct intel_crtc_state *crtc_state,
  7467. struct dpll *reduced_clock)
  7468. {
  7469. struct drm_crtc *crtc = &intel_crtc->base;
  7470. struct drm_device *dev = crtc->dev;
  7471. struct drm_i915_private *dev_priv = dev->dev_private;
  7472. struct drm_atomic_state *state = crtc_state->base.state;
  7473. struct drm_connector *connector;
  7474. struct drm_connector_state *connector_state;
  7475. struct intel_encoder *encoder;
  7476. u32 dpll, fp, fp2;
  7477. int factor, i;
  7478. bool is_lvds = false, is_sdvo = false;
  7479. for_each_connector_in_state(state, connector, connector_state, i) {
  7480. if (connector_state->crtc != crtc_state->base.crtc)
  7481. continue;
  7482. encoder = to_intel_encoder(connector_state->best_encoder);
  7483. switch (encoder->type) {
  7484. case INTEL_OUTPUT_LVDS:
  7485. is_lvds = true;
  7486. break;
  7487. case INTEL_OUTPUT_SDVO:
  7488. case INTEL_OUTPUT_HDMI:
  7489. is_sdvo = true;
  7490. break;
  7491. default:
  7492. break;
  7493. }
  7494. }
  7495. /* Enable autotuning of the PLL clock (if permissible) */
  7496. factor = 21;
  7497. if (is_lvds) {
  7498. if ((intel_panel_use_ssc(dev_priv) &&
  7499. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7500. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7501. factor = 25;
  7502. } else if (crtc_state->sdvo_tv_clock)
  7503. factor = 20;
  7504. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7505. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7506. fp |= FP_CB_TUNE;
  7507. if (reduced_clock) {
  7508. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7509. if (reduced_clock->m < factor * reduced_clock->n)
  7510. fp2 |= FP_CB_TUNE;
  7511. } else {
  7512. fp2 = fp;
  7513. }
  7514. dpll = 0;
  7515. if (is_lvds)
  7516. dpll |= DPLLB_MODE_LVDS;
  7517. else
  7518. dpll |= DPLLB_MODE_DAC_SERIAL;
  7519. dpll |= (crtc_state->pixel_multiplier - 1)
  7520. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7521. if (is_sdvo)
  7522. dpll |= DPLL_SDVO_HIGH_SPEED;
  7523. if (crtc_state->has_dp_encoder)
  7524. dpll |= DPLL_SDVO_HIGH_SPEED;
  7525. /* compute bitmask from p1 value */
  7526. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7527. /* also FPA1 */
  7528. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7529. switch (crtc_state->dpll.p2) {
  7530. case 5:
  7531. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7532. break;
  7533. case 7:
  7534. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7535. break;
  7536. case 10:
  7537. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7538. break;
  7539. case 14:
  7540. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7541. break;
  7542. }
  7543. if (is_lvds && intel_panel_use_ssc(dev_priv))
  7544. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7545. else
  7546. dpll |= PLL_REF_INPUT_DREFCLK;
  7547. dpll |= DPLL_VCO_ENABLE;
  7548. crtc_state->dpll_hw_state.dpll = dpll;
  7549. crtc_state->dpll_hw_state.fp0 = fp;
  7550. crtc_state->dpll_hw_state.fp1 = fp2;
  7551. }
  7552. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7553. struct intel_crtc_state *crtc_state)
  7554. {
  7555. struct drm_device *dev = crtc->base.dev;
  7556. struct drm_i915_private *dev_priv = dev->dev_private;
  7557. struct dpll reduced_clock;
  7558. bool has_reduced_clock = false;
  7559. struct intel_shared_dpll *pll;
  7560. const struct intel_limit *limit;
  7561. int refclk = 120000;
  7562. memset(&crtc_state->dpll_hw_state, 0,
  7563. sizeof(crtc_state->dpll_hw_state));
  7564. crtc->lowfreq_avail = false;
  7565. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7566. if (!crtc_state->has_pch_encoder)
  7567. return 0;
  7568. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7569. if (intel_panel_use_ssc(dev_priv)) {
  7570. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7571. dev_priv->vbt.lvds_ssc_freq);
  7572. refclk = dev_priv->vbt.lvds_ssc_freq;
  7573. }
  7574. if (intel_is_dual_link_lvds(dev)) {
  7575. if (refclk == 100000)
  7576. limit = &intel_limits_ironlake_dual_lvds_100m;
  7577. else
  7578. limit = &intel_limits_ironlake_dual_lvds;
  7579. } else {
  7580. if (refclk == 100000)
  7581. limit = &intel_limits_ironlake_single_lvds_100m;
  7582. else
  7583. limit = &intel_limits_ironlake_single_lvds;
  7584. }
  7585. } else {
  7586. limit = &intel_limits_ironlake_dac;
  7587. }
  7588. if (!crtc_state->clock_set &&
  7589. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7590. refclk, NULL, &crtc_state->dpll)) {
  7591. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7592. return -EINVAL;
  7593. }
  7594. ironlake_compute_dpll(crtc, crtc_state,
  7595. has_reduced_clock ? &reduced_clock : NULL);
  7596. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  7597. if (pll == NULL) {
  7598. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7599. pipe_name(crtc->pipe));
  7600. return -EINVAL;
  7601. }
  7602. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7603. has_reduced_clock)
  7604. crtc->lowfreq_avail = true;
  7605. return 0;
  7606. }
  7607. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7608. struct intel_link_m_n *m_n)
  7609. {
  7610. struct drm_device *dev = crtc->base.dev;
  7611. struct drm_i915_private *dev_priv = dev->dev_private;
  7612. enum pipe pipe = crtc->pipe;
  7613. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7614. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7615. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7616. & ~TU_SIZE_MASK;
  7617. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7618. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7619. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7620. }
  7621. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7622. enum transcoder transcoder,
  7623. struct intel_link_m_n *m_n,
  7624. struct intel_link_m_n *m2_n2)
  7625. {
  7626. struct drm_device *dev = crtc->base.dev;
  7627. struct drm_i915_private *dev_priv = dev->dev_private;
  7628. enum pipe pipe = crtc->pipe;
  7629. if (INTEL_INFO(dev)->gen >= 5) {
  7630. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7631. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7632. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7633. & ~TU_SIZE_MASK;
  7634. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7635. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7636. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7637. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7638. * gen < 8) and if DRRS is supported (to make sure the
  7639. * registers are not unnecessarily read).
  7640. */
  7641. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7642. crtc->config->has_drrs) {
  7643. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7644. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7645. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7646. & ~TU_SIZE_MASK;
  7647. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7648. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7649. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7650. }
  7651. } else {
  7652. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7653. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7654. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7655. & ~TU_SIZE_MASK;
  7656. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7657. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7658. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7659. }
  7660. }
  7661. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7662. struct intel_crtc_state *pipe_config)
  7663. {
  7664. if (pipe_config->has_pch_encoder)
  7665. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7666. else
  7667. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7668. &pipe_config->dp_m_n,
  7669. &pipe_config->dp_m2_n2);
  7670. }
  7671. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7672. struct intel_crtc_state *pipe_config)
  7673. {
  7674. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7675. &pipe_config->fdi_m_n, NULL);
  7676. }
  7677. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7678. struct intel_crtc_state *pipe_config)
  7679. {
  7680. struct drm_device *dev = crtc->base.dev;
  7681. struct drm_i915_private *dev_priv = dev->dev_private;
  7682. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7683. uint32_t ps_ctrl = 0;
  7684. int id = -1;
  7685. int i;
  7686. /* find scaler attached to this pipe */
  7687. for (i = 0; i < crtc->num_scalers; i++) {
  7688. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7689. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7690. id = i;
  7691. pipe_config->pch_pfit.enabled = true;
  7692. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7693. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7694. break;
  7695. }
  7696. }
  7697. scaler_state->scaler_id = id;
  7698. if (id >= 0) {
  7699. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7700. } else {
  7701. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7702. }
  7703. }
  7704. static void
  7705. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7706. struct intel_initial_plane_config *plane_config)
  7707. {
  7708. struct drm_device *dev = crtc->base.dev;
  7709. struct drm_i915_private *dev_priv = dev->dev_private;
  7710. u32 val, base, offset, stride_mult, tiling;
  7711. int pipe = crtc->pipe;
  7712. int fourcc, pixel_format;
  7713. unsigned int aligned_height;
  7714. struct drm_framebuffer *fb;
  7715. struct intel_framebuffer *intel_fb;
  7716. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7717. if (!intel_fb) {
  7718. DRM_DEBUG_KMS("failed to alloc fb\n");
  7719. return;
  7720. }
  7721. fb = &intel_fb->base;
  7722. val = I915_READ(PLANE_CTL(pipe, 0));
  7723. if (!(val & PLANE_CTL_ENABLE))
  7724. goto error;
  7725. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7726. fourcc = skl_format_to_fourcc(pixel_format,
  7727. val & PLANE_CTL_ORDER_RGBX,
  7728. val & PLANE_CTL_ALPHA_MASK);
  7729. fb->pixel_format = fourcc;
  7730. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7731. tiling = val & PLANE_CTL_TILED_MASK;
  7732. switch (tiling) {
  7733. case PLANE_CTL_TILED_LINEAR:
  7734. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7735. break;
  7736. case PLANE_CTL_TILED_X:
  7737. plane_config->tiling = I915_TILING_X;
  7738. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7739. break;
  7740. case PLANE_CTL_TILED_Y:
  7741. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7742. break;
  7743. case PLANE_CTL_TILED_YF:
  7744. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7745. break;
  7746. default:
  7747. MISSING_CASE(tiling);
  7748. goto error;
  7749. }
  7750. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7751. plane_config->base = base;
  7752. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7753. val = I915_READ(PLANE_SIZE(pipe, 0));
  7754. fb->height = ((val >> 16) & 0xfff) + 1;
  7755. fb->width = ((val >> 0) & 0x1fff) + 1;
  7756. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7757. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  7758. fb->pixel_format);
  7759. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7760. aligned_height = intel_fb_align_height(dev, fb->height,
  7761. fb->pixel_format,
  7762. fb->modifier[0]);
  7763. plane_config->size = fb->pitches[0] * aligned_height;
  7764. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7765. pipe_name(pipe), fb->width, fb->height,
  7766. fb->bits_per_pixel, base, fb->pitches[0],
  7767. plane_config->size);
  7768. plane_config->fb = intel_fb;
  7769. return;
  7770. error:
  7771. kfree(fb);
  7772. }
  7773. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7774. struct intel_crtc_state *pipe_config)
  7775. {
  7776. struct drm_device *dev = crtc->base.dev;
  7777. struct drm_i915_private *dev_priv = dev->dev_private;
  7778. uint32_t tmp;
  7779. tmp = I915_READ(PF_CTL(crtc->pipe));
  7780. if (tmp & PF_ENABLE) {
  7781. pipe_config->pch_pfit.enabled = true;
  7782. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7783. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7784. /* We currently do not free assignements of panel fitters on
  7785. * ivb/hsw (since we don't use the higher upscaling modes which
  7786. * differentiates them) so just WARN about this case for now. */
  7787. if (IS_GEN7(dev)) {
  7788. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7789. PF_PIPE_SEL_IVB(crtc->pipe));
  7790. }
  7791. }
  7792. }
  7793. static void
  7794. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7795. struct intel_initial_plane_config *plane_config)
  7796. {
  7797. struct drm_device *dev = crtc->base.dev;
  7798. struct drm_i915_private *dev_priv = dev->dev_private;
  7799. u32 val, base, offset;
  7800. int pipe = crtc->pipe;
  7801. int fourcc, pixel_format;
  7802. unsigned int aligned_height;
  7803. struct drm_framebuffer *fb;
  7804. struct intel_framebuffer *intel_fb;
  7805. val = I915_READ(DSPCNTR(pipe));
  7806. if (!(val & DISPLAY_PLANE_ENABLE))
  7807. return;
  7808. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7809. if (!intel_fb) {
  7810. DRM_DEBUG_KMS("failed to alloc fb\n");
  7811. return;
  7812. }
  7813. fb = &intel_fb->base;
  7814. if (INTEL_INFO(dev)->gen >= 4) {
  7815. if (val & DISPPLANE_TILED) {
  7816. plane_config->tiling = I915_TILING_X;
  7817. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7818. }
  7819. }
  7820. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7821. fourcc = i9xx_format_to_fourcc(pixel_format);
  7822. fb->pixel_format = fourcc;
  7823. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7824. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7825. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7826. offset = I915_READ(DSPOFFSET(pipe));
  7827. } else {
  7828. if (plane_config->tiling)
  7829. offset = I915_READ(DSPTILEOFF(pipe));
  7830. else
  7831. offset = I915_READ(DSPLINOFF(pipe));
  7832. }
  7833. plane_config->base = base;
  7834. val = I915_READ(PIPESRC(pipe));
  7835. fb->width = ((val >> 16) & 0xfff) + 1;
  7836. fb->height = ((val >> 0) & 0xfff) + 1;
  7837. val = I915_READ(DSPSTRIDE(pipe));
  7838. fb->pitches[0] = val & 0xffffffc0;
  7839. aligned_height = intel_fb_align_height(dev, fb->height,
  7840. fb->pixel_format,
  7841. fb->modifier[0]);
  7842. plane_config->size = fb->pitches[0] * aligned_height;
  7843. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7844. pipe_name(pipe), fb->width, fb->height,
  7845. fb->bits_per_pixel, base, fb->pitches[0],
  7846. plane_config->size);
  7847. plane_config->fb = intel_fb;
  7848. }
  7849. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7850. struct intel_crtc_state *pipe_config)
  7851. {
  7852. struct drm_device *dev = crtc->base.dev;
  7853. struct drm_i915_private *dev_priv = dev->dev_private;
  7854. enum intel_display_power_domain power_domain;
  7855. uint32_t tmp;
  7856. bool ret;
  7857. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7858. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7859. return false;
  7860. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7861. pipe_config->shared_dpll = NULL;
  7862. ret = false;
  7863. tmp = I915_READ(PIPECONF(crtc->pipe));
  7864. if (!(tmp & PIPECONF_ENABLE))
  7865. goto out;
  7866. switch (tmp & PIPECONF_BPC_MASK) {
  7867. case PIPECONF_6BPC:
  7868. pipe_config->pipe_bpp = 18;
  7869. break;
  7870. case PIPECONF_8BPC:
  7871. pipe_config->pipe_bpp = 24;
  7872. break;
  7873. case PIPECONF_10BPC:
  7874. pipe_config->pipe_bpp = 30;
  7875. break;
  7876. case PIPECONF_12BPC:
  7877. pipe_config->pipe_bpp = 36;
  7878. break;
  7879. default:
  7880. break;
  7881. }
  7882. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7883. pipe_config->limited_color_range = true;
  7884. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7885. struct intel_shared_dpll *pll;
  7886. enum intel_dpll_id pll_id;
  7887. pipe_config->has_pch_encoder = true;
  7888. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7889. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7890. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7891. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7892. if (HAS_PCH_IBX(dev_priv)) {
  7893. /*
  7894. * The pipe->pch transcoder and pch transcoder->pll
  7895. * mapping is fixed.
  7896. */
  7897. pll_id = (enum intel_dpll_id) crtc->pipe;
  7898. } else {
  7899. tmp = I915_READ(PCH_DPLL_SEL);
  7900. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7901. pll_id = DPLL_ID_PCH_PLL_B;
  7902. else
  7903. pll_id= DPLL_ID_PCH_PLL_A;
  7904. }
  7905. pipe_config->shared_dpll =
  7906. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7907. pll = pipe_config->shared_dpll;
  7908. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7909. &pipe_config->dpll_hw_state));
  7910. tmp = pipe_config->dpll_hw_state.dpll;
  7911. pipe_config->pixel_multiplier =
  7912. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7913. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7914. ironlake_pch_clock_get(crtc, pipe_config);
  7915. } else {
  7916. pipe_config->pixel_multiplier = 1;
  7917. }
  7918. intel_get_pipe_timings(crtc, pipe_config);
  7919. intel_get_pipe_src_size(crtc, pipe_config);
  7920. ironlake_get_pfit_config(crtc, pipe_config);
  7921. ret = true;
  7922. out:
  7923. intel_display_power_put(dev_priv, power_domain);
  7924. return ret;
  7925. }
  7926. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7927. {
  7928. struct drm_device *dev = dev_priv->dev;
  7929. struct intel_crtc *crtc;
  7930. for_each_intel_crtc(dev, crtc)
  7931. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7932. pipe_name(crtc->pipe));
  7933. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7934. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7935. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7936. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7937. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7938. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7939. "CPU PWM1 enabled\n");
  7940. if (IS_HASWELL(dev))
  7941. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7942. "CPU PWM2 enabled\n");
  7943. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7944. "PCH PWM1 enabled\n");
  7945. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7946. "Utility pin enabled\n");
  7947. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7948. /*
  7949. * In theory we can still leave IRQs enabled, as long as only the HPD
  7950. * interrupts remain enabled. We used to check for that, but since it's
  7951. * gen-specific and since we only disable LCPLL after we fully disable
  7952. * the interrupts, the check below should be enough.
  7953. */
  7954. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7955. }
  7956. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7957. {
  7958. struct drm_device *dev = dev_priv->dev;
  7959. if (IS_HASWELL(dev))
  7960. return I915_READ(D_COMP_HSW);
  7961. else
  7962. return I915_READ(D_COMP_BDW);
  7963. }
  7964. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7965. {
  7966. struct drm_device *dev = dev_priv->dev;
  7967. if (IS_HASWELL(dev)) {
  7968. mutex_lock(&dev_priv->rps.hw_lock);
  7969. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7970. val))
  7971. DRM_ERROR("Failed to write to D_COMP\n");
  7972. mutex_unlock(&dev_priv->rps.hw_lock);
  7973. } else {
  7974. I915_WRITE(D_COMP_BDW, val);
  7975. POSTING_READ(D_COMP_BDW);
  7976. }
  7977. }
  7978. /*
  7979. * This function implements pieces of two sequences from BSpec:
  7980. * - Sequence for display software to disable LCPLL
  7981. * - Sequence for display software to allow package C8+
  7982. * The steps implemented here are just the steps that actually touch the LCPLL
  7983. * register. Callers should take care of disabling all the display engine
  7984. * functions, doing the mode unset, fixing interrupts, etc.
  7985. */
  7986. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7987. bool switch_to_fclk, bool allow_power_down)
  7988. {
  7989. uint32_t val;
  7990. assert_can_disable_lcpll(dev_priv);
  7991. val = I915_READ(LCPLL_CTL);
  7992. if (switch_to_fclk) {
  7993. val |= LCPLL_CD_SOURCE_FCLK;
  7994. I915_WRITE(LCPLL_CTL, val);
  7995. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7996. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7997. DRM_ERROR("Switching to FCLK failed\n");
  7998. val = I915_READ(LCPLL_CTL);
  7999. }
  8000. val |= LCPLL_PLL_DISABLE;
  8001. I915_WRITE(LCPLL_CTL, val);
  8002. POSTING_READ(LCPLL_CTL);
  8003. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  8004. DRM_ERROR("LCPLL still locked\n");
  8005. val = hsw_read_dcomp(dev_priv);
  8006. val |= D_COMP_COMP_DISABLE;
  8007. hsw_write_dcomp(dev_priv, val);
  8008. ndelay(100);
  8009. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  8010. 1))
  8011. DRM_ERROR("D_COMP RCOMP still in progress\n");
  8012. if (allow_power_down) {
  8013. val = I915_READ(LCPLL_CTL);
  8014. val |= LCPLL_POWER_DOWN_ALLOW;
  8015. I915_WRITE(LCPLL_CTL, val);
  8016. POSTING_READ(LCPLL_CTL);
  8017. }
  8018. }
  8019. /*
  8020. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  8021. * source.
  8022. */
  8023. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8024. {
  8025. uint32_t val;
  8026. val = I915_READ(LCPLL_CTL);
  8027. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8028. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8029. return;
  8030. /*
  8031. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8032. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8033. */
  8034. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8035. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8036. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8037. I915_WRITE(LCPLL_CTL, val);
  8038. POSTING_READ(LCPLL_CTL);
  8039. }
  8040. val = hsw_read_dcomp(dev_priv);
  8041. val |= D_COMP_COMP_FORCE;
  8042. val &= ~D_COMP_COMP_DISABLE;
  8043. hsw_write_dcomp(dev_priv, val);
  8044. val = I915_READ(LCPLL_CTL);
  8045. val &= ~LCPLL_PLL_DISABLE;
  8046. I915_WRITE(LCPLL_CTL, val);
  8047. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  8048. DRM_ERROR("LCPLL not locked yet\n");
  8049. if (val & LCPLL_CD_SOURCE_FCLK) {
  8050. val = I915_READ(LCPLL_CTL);
  8051. val &= ~LCPLL_CD_SOURCE_FCLK;
  8052. I915_WRITE(LCPLL_CTL, val);
  8053. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8054. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8055. DRM_ERROR("Switching back to LCPLL failed\n");
  8056. }
  8057. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8058. intel_update_cdclk(dev_priv->dev);
  8059. }
  8060. /*
  8061. * Package states C8 and deeper are really deep PC states that can only be
  8062. * reached when all the devices on the system allow it, so even if the graphics
  8063. * device allows PC8+, it doesn't mean the system will actually get to these
  8064. * states. Our driver only allows PC8+ when going into runtime PM.
  8065. *
  8066. * The requirements for PC8+ are that all the outputs are disabled, the power
  8067. * well is disabled and most interrupts are disabled, and these are also
  8068. * requirements for runtime PM. When these conditions are met, we manually do
  8069. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8070. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8071. * hang the machine.
  8072. *
  8073. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8074. * the state of some registers, so when we come back from PC8+ we need to
  8075. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8076. * need to take care of the registers kept by RC6. Notice that this happens even
  8077. * if we don't put the device in PCI D3 state (which is what currently happens
  8078. * because of the runtime PM support).
  8079. *
  8080. * For more, read "Display Sequences for Package C8" on the hardware
  8081. * documentation.
  8082. */
  8083. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8084. {
  8085. struct drm_device *dev = dev_priv->dev;
  8086. uint32_t val;
  8087. DRM_DEBUG_KMS("Enabling package C8+\n");
  8088. if (HAS_PCH_LPT_LP(dev)) {
  8089. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8090. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8091. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8092. }
  8093. lpt_disable_clkout_dp(dev);
  8094. hsw_disable_lcpll(dev_priv, true, true);
  8095. }
  8096. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8097. {
  8098. struct drm_device *dev = dev_priv->dev;
  8099. uint32_t val;
  8100. DRM_DEBUG_KMS("Disabling package C8+\n");
  8101. hsw_restore_lcpll(dev_priv);
  8102. lpt_init_pch_refclk(dev);
  8103. if (HAS_PCH_LPT_LP(dev)) {
  8104. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8105. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8106. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8107. }
  8108. }
  8109. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8110. {
  8111. struct drm_device *dev = old_state->dev;
  8112. struct intel_atomic_state *old_intel_state =
  8113. to_intel_atomic_state(old_state);
  8114. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8115. broxton_set_cdclk(to_i915(dev), req_cdclk);
  8116. }
  8117. /* compute the max rate for new configuration */
  8118. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8119. {
  8120. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8121. struct drm_i915_private *dev_priv = state->dev->dev_private;
  8122. struct drm_crtc *crtc;
  8123. struct drm_crtc_state *cstate;
  8124. struct intel_crtc_state *crtc_state;
  8125. unsigned max_pixel_rate = 0, i;
  8126. enum pipe pipe;
  8127. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8128. sizeof(intel_state->min_pixclk));
  8129. for_each_crtc_in_state(state, crtc, cstate, i) {
  8130. int pixel_rate;
  8131. crtc_state = to_intel_crtc_state(cstate);
  8132. if (!crtc_state->base.enable) {
  8133. intel_state->min_pixclk[i] = 0;
  8134. continue;
  8135. }
  8136. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8137. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8138. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8139. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8140. intel_state->min_pixclk[i] = pixel_rate;
  8141. }
  8142. for_each_pipe(dev_priv, pipe)
  8143. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8144. return max_pixel_rate;
  8145. }
  8146. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8147. {
  8148. struct drm_i915_private *dev_priv = dev->dev_private;
  8149. uint32_t val, data;
  8150. int ret;
  8151. if (WARN((I915_READ(LCPLL_CTL) &
  8152. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8153. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8154. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8155. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8156. "trying to change cdclk frequency with cdclk not enabled\n"))
  8157. return;
  8158. mutex_lock(&dev_priv->rps.hw_lock);
  8159. ret = sandybridge_pcode_write(dev_priv,
  8160. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8161. mutex_unlock(&dev_priv->rps.hw_lock);
  8162. if (ret) {
  8163. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8164. return;
  8165. }
  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, 1))
  8171. DRM_ERROR("Switching to FCLK failed\n");
  8172. val = I915_READ(LCPLL_CTL);
  8173. val &= ~LCPLL_CLK_FREQ_MASK;
  8174. switch (cdclk) {
  8175. case 450000:
  8176. val |= LCPLL_CLK_FREQ_450;
  8177. data = 0;
  8178. break;
  8179. case 540000:
  8180. val |= LCPLL_CLK_FREQ_54O_BDW;
  8181. data = 1;
  8182. break;
  8183. case 337500:
  8184. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8185. data = 2;
  8186. break;
  8187. case 675000:
  8188. val |= LCPLL_CLK_FREQ_675_BDW;
  8189. data = 3;
  8190. break;
  8191. default:
  8192. WARN(1, "invalid cdclk frequency\n");
  8193. return;
  8194. }
  8195. I915_WRITE(LCPLL_CTL, val);
  8196. val = I915_READ(LCPLL_CTL);
  8197. val &= ~LCPLL_CD_SOURCE_FCLK;
  8198. I915_WRITE(LCPLL_CTL, val);
  8199. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8200. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8201. DRM_ERROR("Switching back to LCPLL failed\n");
  8202. mutex_lock(&dev_priv->rps.hw_lock);
  8203. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8204. mutex_unlock(&dev_priv->rps.hw_lock);
  8205. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8206. intel_update_cdclk(dev);
  8207. WARN(cdclk != dev_priv->cdclk_freq,
  8208. "cdclk requested %d kHz but got %d kHz\n",
  8209. cdclk, dev_priv->cdclk_freq);
  8210. }
  8211. static int broadwell_calc_cdclk(int max_pixclk)
  8212. {
  8213. if (max_pixclk > 540000)
  8214. return 675000;
  8215. else if (max_pixclk > 450000)
  8216. return 540000;
  8217. else if (max_pixclk > 337500)
  8218. return 450000;
  8219. else
  8220. return 337500;
  8221. }
  8222. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8223. {
  8224. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8225. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8226. int max_pixclk = ilk_max_pixel_rate(state);
  8227. int cdclk;
  8228. /*
  8229. * FIXME should also account for plane ratio
  8230. * once 64bpp pixel formats are supported.
  8231. */
  8232. cdclk = broadwell_calc_cdclk(max_pixclk);
  8233. if (cdclk > dev_priv->max_cdclk_freq) {
  8234. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8235. cdclk, dev_priv->max_cdclk_freq);
  8236. return -EINVAL;
  8237. }
  8238. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8239. if (!intel_state->active_crtcs)
  8240. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8241. return 0;
  8242. }
  8243. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8244. {
  8245. struct drm_device *dev = old_state->dev;
  8246. struct intel_atomic_state *old_intel_state =
  8247. to_intel_atomic_state(old_state);
  8248. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8249. broadwell_set_cdclk(dev, req_cdclk);
  8250. }
  8251. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8252. {
  8253. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8254. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8255. const int max_pixclk = ilk_max_pixel_rate(state);
  8256. int vco = intel_state->cdclk_pll_vco;
  8257. int cdclk;
  8258. /*
  8259. * FIXME should also account for plane ratio
  8260. * once 64bpp pixel formats are supported.
  8261. */
  8262. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8263. /*
  8264. * FIXME move the cdclk caclulation to
  8265. * compute_config() so we can fail gracegully.
  8266. */
  8267. if (cdclk > dev_priv->max_cdclk_freq) {
  8268. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8269. cdclk, dev_priv->max_cdclk_freq);
  8270. cdclk = dev_priv->max_cdclk_freq;
  8271. }
  8272. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8273. if (!intel_state->active_crtcs)
  8274. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8275. return 0;
  8276. }
  8277. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8278. {
  8279. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8280. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8281. unsigned int req_cdclk = intel_state->dev_cdclk;
  8282. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8283. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8284. }
  8285. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8286. struct intel_crtc_state *crtc_state)
  8287. {
  8288. struct intel_encoder *intel_encoder =
  8289. intel_ddi_get_crtc_new_encoder(crtc_state);
  8290. if (intel_encoder->type != INTEL_OUTPUT_DSI) {
  8291. if (!intel_ddi_pll_select(crtc, crtc_state))
  8292. return -EINVAL;
  8293. }
  8294. crtc->lowfreq_avail = false;
  8295. return 0;
  8296. }
  8297. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8298. enum port port,
  8299. struct intel_crtc_state *pipe_config)
  8300. {
  8301. enum intel_dpll_id id;
  8302. switch (port) {
  8303. case PORT_A:
  8304. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8305. id = DPLL_ID_SKL_DPLL0;
  8306. break;
  8307. case PORT_B:
  8308. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8309. id = DPLL_ID_SKL_DPLL1;
  8310. break;
  8311. case PORT_C:
  8312. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8313. id = DPLL_ID_SKL_DPLL2;
  8314. break;
  8315. default:
  8316. DRM_ERROR("Incorrect port type\n");
  8317. return;
  8318. }
  8319. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8320. }
  8321. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8322. enum port port,
  8323. struct intel_crtc_state *pipe_config)
  8324. {
  8325. enum intel_dpll_id id;
  8326. u32 temp;
  8327. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8328. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8329. switch (pipe_config->ddi_pll_sel) {
  8330. case SKL_DPLL0:
  8331. id = DPLL_ID_SKL_DPLL0;
  8332. break;
  8333. case SKL_DPLL1:
  8334. id = DPLL_ID_SKL_DPLL1;
  8335. break;
  8336. case SKL_DPLL2:
  8337. id = DPLL_ID_SKL_DPLL2;
  8338. break;
  8339. case SKL_DPLL3:
  8340. id = DPLL_ID_SKL_DPLL3;
  8341. break;
  8342. default:
  8343. MISSING_CASE(pipe_config->ddi_pll_sel);
  8344. return;
  8345. }
  8346. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8347. }
  8348. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8349. enum port port,
  8350. struct intel_crtc_state *pipe_config)
  8351. {
  8352. enum intel_dpll_id id;
  8353. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8354. switch (pipe_config->ddi_pll_sel) {
  8355. case PORT_CLK_SEL_WRPLL1:
  8356. id = DPLL_ID_WRPLL1;
  8357. break;
  8358. case PORT_CLK_SEL_WRPLL2:
  8359. id = DPLL_ID_WRPLL2;
  8360. break;
  8361. case PORT_CLK_SEL_SPLL:
  8362. id = DPLL_ID_SPLL;
  8363. break;
  8364. case PORT_CLK_SEL_LCPLL_810:
  8365. id = DPLL_ID_LCPLL_810;
  8366. break;
  8367. case PORT_CLK_SEL_LCPLL_1350:
  8368. id = DPLL_ID_LCPLL_1350;
  8369. break;
  8370. case PORT_CLK_SEL_LCPLL_2700:
  8371. id = DPLL_ID_LCPLL_2700;
  8372. break;
  8373. default:
  8374. MISSING_CASE(pipe_config->ddi_pll_sel);
  8375. /* fall through */
  8376. case PORT_CLK_SEL_NONE:
  8377. return;
  8378. }
  8379. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8380. }
  8381. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8382. struct intel_crtc_state *pipe_config,
  8383. unsigned long *power_domain_mask)
  8384. {
  8385. struct drm_device *dev = crtc->base.dev;
  8386. struct drm_i915_private *dev_priv = dev->dev_private;
  8387. enum intel_display_power_domain power_domain;
  8388. u32 tmp;
  8389. /*
  8390. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8391. * transcoder handled below.
  8392. */
  8393. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8394. /*
  8395. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8396. * consistency and less surprising code; it's in always on power).
  8397. */
  8398. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8399. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8400. enum pipe trans_edp_pipe;
  8401. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8402. default:
  8403. WARN(1, "unknown pipe linked to edp transcoder\n");
  8404. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8405. case TRANS_DDI_EDP_INPUT_A_ON:
  8406. trans_edp_pipe = PIPE_A;
  8407. break;
  8408. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8409. trans_edp_pipe = PIPE_B;
  8410. break;
  8411. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8412. trans_edp_pipe = PIPE_C;
  8413. break;
  8414. }
  8415. if (trans_edp_pipe == crtc->pipe)
  8416. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8417. }
  8418. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8419. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8420. return false;
  8421. *power_domain_mask |= BIT(power_domain);
  8422. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8423. return tmp & PIPECONF_ENABLE;
  8424. }
  8425. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8426. struct intel_crtc_state *pipe_config,
  8427. unsigned long *power_domain_mask)
  8428. {
  8429. struct drm_device *dev = crtc->base.dev;
  8430. struct drm_i915_private *dev_priv = dev->dev_private;
  8431. enum intel_display_power_domain power_domain;
  8432. enum port port;
  8433. enum transcoder cpu_transcoder;
  8434. u32 tmp;
  8435. pipe_config->has_dsi_encoder = false;
  8436. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8437. if (port == PORT_A)
  8438. cpu_transcoder = TRANSCODER_DSI_A;
  8439. else
  8440. cpu_transcoder = TRANSCODER_DSI_C;
  8441. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8442. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8443. continue;
  8444. *power_domain_mask |= BIT(power_domain);
  8445. /*
  8446. * The PLL needs to be enabled with a valid divider
  8447. * configuration, otherwise accessing DSI registers will hang
  8448. * the machine. See BSpec North Display Engine
  8449. * registers/MIPI[BXT]. We can break out here early, since we
  8450. * need the same DSI PLL to be enabled for both DSI ports.
  8451. */
  8452. if (!intel_dsi_pll_is_enabled(dev_priv))
  8453. break;
  8454. /* XXX: this works for video mode only */
  8455. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8456. if (!(tmp & DPI_ENABLE))
  8457. continue;
  8458. tmp = I915_READ(MIPI_CTRL(port));
  8459. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8460. continue;
  8461. pipe_config->cpu_transcoder = cpu_transcoder;
  8462. pipe_config->has_dsi_encoder = true;
  8463. break;
  8464. }
  8465. return pipe_config->has_dsi_encoder;
  8466. }
  8467. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8468. struct intel_crtc_state *pipe_config)
  8469. {
  8470. struct drm_device *dev = crtc->base.dev;
  8471. struct drm_i915_private *dev_priv = dev->dev_private;
  8472. struct intel_shared_dpll *pll;
  8473. enum port port;
  8474. uint32_t tmp;
  8475. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8476. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8477. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8478. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8479. else if (IS_BROXTON(dev))
  8480. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8481. else
  8482. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8483. pll = pipe_config->shared_dpll;
  8484. if (pll) {
  8485. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8486. &pipe_config->dpll_hw_state));
  8487. }
  8488. /*
  8489. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8490. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8491. * the PCH transcoder is on.
  8492. */
  8493. if (INTEL_INFO(dev)->gen < 9 &&
  8494. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8495. pipe_config->has_pch_encoder = true;
  8496. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8497. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8498. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8499. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8500. }
  8501. }
  8502. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8503. struct intel_crtc_state *pipe_config)
  8504. {
  8505. struct drm_device *dev = crtc->base.dev;
  8506. struct drm_i915_private *dev_priv = dev->dev_private;
  8507. enum intel_display_power_domain power_domain;
  8508. unsigned long power_domain_mask;
  8509. bool active;
  8510. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8511. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8512. return false;
  8513. power_domain_mask = BIT(power_domain);
  8514. pipe_config->shared_dpll = NULL;
  8515. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8516. if (IS_BROXTON(dev_priv)) {
  8517. bxt_get_dsi_transcoder_state(crtc, pipe_config,
  8518. &power_domain_mask);
  8519. WARN_ON(active && pipe_config->has_dsi_encoder);
  8520. if (pipe_config->has_dsi_encoder)
  8521. active = true;
  8522. }
  8523. if (!active)
  8524. goto out;
  8525. if (!pipe_config->has_dsi_encoder) {
  8526. haswell_get_ddi_port_state(crtc, pipe_config);
  8527. intel_get_pipe_timings(crtc, pipe_config);
  8528. }
  8529. intel_get_pipe_src_size(crtc, pipe_config);
  8530. pipe_config->gamma_mode =
  8531. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8532. if (INTEL_INFO(dev)->gen >= 9) {
  8533. skl_init_scalers(dev, crtc, pipe_config);
  8534. }
  8535. if (INTEL_INFO(dev)->gen >= 9) {
  8536. pipe_config->scaler_state.scaler_id = -1;
  8537. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8538. }
  8539. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8540. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8541. power_domain_mask |= BIT(power_domain);
  8542. if (INTEL_INFO(dev)->gen >= 9)
  8543. skylake_get_pfit_config(crtc, pipe_config);
  8544. else
  8545. ironlake_get_pfit_config(crtc, pipe_config);
  8546. }
  8547. if (IS_HASWELL(dev))
  8548. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8549. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8550. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8551. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8552. pipe_config->pixel_multiplier =
  8553. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8554. } else {
  8555. pipe_config->pixel_multiplier = 1;
  8556. }
  8557. out:
  8558. for_each_power_domain(power_domain, power_domain_mask)
  8559. intel_display_power_put(dev_priv, power_domain);
  8560. return active;
  8561. }
  8562. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  8563. const struct intel_plane_state *plane_state)
  8564. {
  8565. struct drm_device *dev = crtc->dev;
  8566. struct drm_i915_private *dev_priv = dev->dev_private;
  8567. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8568. uint32_t cntl = 0, size = 0;
  8569. if (plane_state && plane_state->visible) {
  8570. unsigned int width = plane_state->base.crtc_w;
  8571. unsigned int height = plane_state->base.crtc_h;
  8572. unsigned int stride = roundup_pow_of_two(width) * 4;
  8573. switch (stride) {
  8574. default:
  8575. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8576. width, stride);
  8577. stride = 256;
  8578. /* fallthrough */
  8579. case 256:
  8580. case 512:
  8581. case 1024:
  8582. case 2048:
  8583. break;
  8584. }
  8585. cntl |= CURSOR_ENABLE |
  8586. CURSOR_GAMMA_ENABLE |
  8587. CURSOR_FORMAT_ARGB |
  8588. CURSOR_STRIDE(stride);
  8589. size = (height << 12) | width;
  8590. }
  8591. if (intel_crtc->cursor_cntl != 0 &&
  8592. (intel_crtc->cursor_base != base ||
  8593. intel_crtc->cursor_size != size ||
  8594. intel_crtc->cursor_cntl != cntl)) {
  8595. /* On these chipsets we can only modify the base/size/stride
  8596. * whilst the cursor is disabled.
  8597. */
  8598. I915_WRITE(CURCNTR(PIPE_A), 0);
  8599. POSTING_READ(CURCNTR(PIPE_A));
  8600. intel_crtc->cursor_cntl = 0;
  8601. }
  8602. if (intel_crtc->cursor_base != base) {
  8603. I915_WRITE(CURBASE(PIPE_A), base);
  8604. intel_crtc->cursor_base = base;
  8605. }
  8606. if (intel_crtc->cursor_size != size) {
  8607. I915_WRITE(CURSIZE, size);
  8608. intel_crtc->cursor_size = size;
  8609. }
  8610. if (intel_crtc->cursor_cntl != cntl) {
  8611. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8612. POSTING_READ(CURCNTR(PIPE_A));
  8613. intel_crtc->cursor_cntl = cntl;
  8614. }
  8615. }
  8616. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  8617. const struct intel_plane_state *plane_state)
  8618. {
  8619. struct drm_device *dev = crtc->dev;
  8620. struct drm_i915_private *dev_priv = dev->dev_private;
  8621. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8622. int pipe = intel_crtc->pipe;
  8623. uint32_t cntl = 0;
  8624. if (plane_state && plane_state->visible) {
  8625. cntl = MCURSOR_GAMMA_ENABLE;
  8626. switch (plane_state->base.crtc_w) {
  8627. case 64:
  8628. cntl |= CURSOR_MODE_64_ARGB_AX;
  8629. break;
  8630. case 128:
  8631. cntl |= CURSOR_MODE_128_ARGB_AX;
  8632. break;
  8633. case 256:
  8634. cntl |= CURSOR_MODE_256_ARGB_AX;
  8635. break;
  8636. default:
  8637. MISSING_CASE(plane_state->base.crtc_w);
  8638. return;
  8639. }
  8640. cntl |= pipe << 28; /* Connect to correct pipe */
  8641. if (HAS_DDI(dev))
  8642. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8643. if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
  8644. cntl |= CURSOR_ROTATE_180;
  8645. }
  8646. if (intel_crtc->cursor_cntl != cntl) {
  8647. I915_WRITE(CURCNTR(pipe), cntl);
  8648. POSTING_READ(CURCNTR(pipe));
  8649. intel_crtc->cursor_cntl = cntl;
  8650. }
  8651. /* and commit changes on next vblank */
  8652. I915_WRITE(CURBASE(pipe), base);
  8653. POSTING_READ(CURBASE(pipe));
  8654. intel_crtc->cursor_base = base;
  8655. }
  8656. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8657. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8658. const struct intel_plane_state *plane_state)
  8659. {
  8660. struct drm_device *dev = crtc->dev;
  8661. struct drm_i915_private *dev_priv = dev->dev_private;
  8662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8663. int pipe = intel_crtc->pipe;
  8664. u32 base = intel_crtc->cursor_addr;
  8665. u32 pos = 0;
  8666. if (plane_state) {
  8667. int x = plane_state->base.crtc_x;
  8668. int y = plane_state->base.crtc_y;
  8669. if (x < 0) {
  8670. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8671. x = -x;
  8672. }
  8673. pos |= x << CURSOR_X_SHIFT;
  8674. if (y < 0) {
  8675. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8676. y = -y;
  8677. }
  8678. pos |= y << CURSOR_Y_SHIFT;
  8679. /* ILK+ do this automagically */
  8680. if (HAS_GMCH_DISPLAY(dev) &&
  8681. plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  8682. base += (plane_state->base.crtc_h *
  8683. plane_state->base.crtc_w - 1) * 4;
  8684. }
  8685. }
  8686. I915_WRITE(CURPOS(pipe), pos);
  8687. if (IS_845G(dev) || IS_I865G(dev))
  8688. i845_update_cursor(crtc, base, plane_state);
  8689. else
  8690. i9xx_update_cursor(crtc, base, plane_state);
  8691. }
  8692. static bool cursor_size_ok(struct drm_device *dev,
  8693. uint32_t width, uint32_t height)
  8694. {
  8695. if (width == 0 || height == 0)
  8696. return false;
  8697. /*
  8698. * 845g/865g are special in that they are only limited by
  8699. * the width of their cursors, the height is arbitrary up to
  8700. * the precision of the register. Everything else requires
  8701. * square cursors, limited to a few power-of-two sizes.
  8702. */
  8703. if (IS_845G(dev) || IS_I865G(dev)) {
  8704. if ((width & 63) != 0)
  8705. return false;
  8706. if (width > (IS_845G(dev) ? 64 : 512))
  8707. return false;
  8708. if (height > 1023)
  8709. return false;
  8710. } else {
  8711. switch (width | height) {
  8712. case 256:
  8713. case 128:
  8714. if (IS_GEN2(dev))
  8715. return false;
  8716. case 64:
  8717. break;
  8718. default:
  8719. return false;
  8720. }
  8721. }
  8722. return true;
  8723. }
  8724. /* VESA 640x480x72Hz mode to set on the pipe */
  8725. static struct drm_display_mode load_detect_mode = {
  8726. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8727. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8728. };
  8729. struct drm_framebuffer *
  8730. __intel_framebuffer_create(struct drm_device *dev,
  8731. struct drm_mode_fb_cmd2 *mode_cmd,
  8732. struct drm_i915_gem_object *obj)
  8733. {
  8734. struct intel_framebuffer *intel_fb;
  8735. int ret;
  8736. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8737. if (!intel_fb)
  8738. return ERR_PTR(-ENOMEM);
  8739. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8740. if (ret)
  8741. goto err;
  8742. return &intel_fb->base;
  8743. err:
  8744. kfree(intel_fb);
  8745. return ERR_PTR(ret);
  8746. }
  8747. static struct drm_framebuffer *
  8748. intel_framebuffer_create(struct drm_device *dev,
  8749. struct drm_mode_fb_cmd2 *mode_cmd,
  8750. struct drm_i915_gem_object *obj)
  8751. {
  8752. struct drm_framebuffer *fb;
  8753. int ret;
  8754. ret = i915_mutex_lock_interruptible(dev);
  8755. if (ret)
  8756. return ERR_PTR(ret);
  8757. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8758. mutex_unlock(&dev->struct_mutex);
  8759. return fb;
  8760. }
  8761. static u32
  8762. intel_framebuffer_pitch_for_width(int width, int bpp)
  8763. {
  8764. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8765. return ALIGN(pitch, 64);
  8766. }
  8767. static u32
  8768. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8769. {
  8770. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8771. return PAGE_ALIGN(pitch * mode->vdisplay);
  8772. }
  8773. static struct drm_framebuffer *
  8774. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8775. struct drm_display_mode *mode,
  8776. int depth, int bpp)
  8777. {
  8778. struct drm_framebuffer *fb;
  8779. struct drm_i915_gem_object *obj;
  8780. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8781. obj = i915_gem_object_create(dev,
  8782. intel_framebuffer_size_for_mode(mode, bpp));
  8783. if (IS_ERR(obj))
  8784. return ERR_CAST(obj);
  8785. mode_cmd.width = mode->hdisplay;
  8786. mode_cmd.height = mode->vdisplay;
  8787. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8788. bpp);
  8789. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8790. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8791. if (IS_ERR(fb))
  8792. drm_gem_object_unreference_unlocked(&obj->base);
  8793. return fb;
  8794. }
  8795. static struct drm_framebuffer *
  8796. mode_fits_in_fbdev(struct drm_device *dev,
  8797. struct drm_display_mode *mode)
  8798. {
  8799. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8800. struct drm_i915_private *dev_priv = dev->dev_private;
  8801. struct drm_i915_gem_object *obj;
  8802. struct drm_framebuffer *fb;
  8803. if (!dev_priv->fbdev)
  8804. return NULL;
  8805. if (!dev_priv->fbdev->fb)
  8806. return NULL;
  8807. obj = dev_priv->fbdev->fb->obj;
  8808. BUG_ON(!obj);
  8809. fb = &dev_priv->fbdev->fb->base;
  8810. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8811. fb->bits_per_pixel))
  8812. return NULL;
  8813. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8814. return NULL;
  8815. drm_framebuffer_reference(fb);
  8816. return fb;
  8817. #else
  8818. return NULL;
  8819. #endif
  8820. }
  8821. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8822. struct drm_crtc *crtc,
  8823. struct drm_display_mode *mode,
  8824. struct drm_framebuffer *fb,
  8825. int x, int y)
  8826. {
  8827. struct drm_plane_state *plane_state;
  8828. int hdisplay, vdisplay;
  8829. int ret;
  8830. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8831. if (IS_ERR(plane_state))
  8832. return PTR_ERR(plane_state);
  8833. if (mode)
  8834. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8835. else
  8836. hdisplay = vdisplay = 0;
  8837. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8838. if (ret)
  8839. return ret;
  8840. drm_atomic_set_fb_for_plane(plane_state, fb);
  8841. plane_state->crtc_x = 0;
  8842. plane_state->crtc_y = 0;
  8843. plane_state->crtc_w = hdisplay;
  8844. plane_state->crtc_h = vdisplay;
  8845. plane_state->src_x = x << 16;
  8846. plane_state->src_y = y << 16;
  8847. plane_state->src_w = hdisplay << 16;
  8848. plane_state->src_h = vdisplay << 16;
  8849. return 0;
  8850. }
  8851. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8852. struct drm_display_mode *mode,
  8853. struct intel_load_detect_pipe *old,
  8854. struct drm_modeset_acquire_ctx *ctx)
  8855. {
  8856. struct intel_crtc *intel_crtc;
  8857. struct intel_encoder *intel_encoder =
  8858. intel_attached_encoder(connector);
  8859. struct drm_crtc *possible_crtc;
  8860. struct drm_encoder *encoder = &intel_encoder->base;
  8861. struct drm_crtc *crtc = NULL;
  8862. struct drm_device *dev = encoder->dev;
  8863. struct drm_framebuffer *fb;
  8864. struct drm_mode_config *config = &dev->mode_config;
  8865. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8866. struct drm_connector_state *connector_state;
  8867. struct intel_crtc_state *crtc_state;
  8868. int ret, i = -1;
  8869. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8870. connector->base.id, connector->name,
  8871. encoder->base.id, encoder->name);
  8872. old->restore_state = NULL;
  8873. retry:
  8874. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8875. if (ret)
  8876. goto fail;
  8877. /*
  8878. * Algorithm gets a little messy:
  8879. *
  8880. * - if the connector already has an assigned crtc, use it (but make
  8881. * sure it's on first)
  8882. *
  8883. * - try to find the first unused crtc that can drive this connector,
  8884. * and use that if we find one
  8885. */
  8886. /* See if we already have a CRTC for this connector */
  8887. if (connector->state->crtc) {
  8888. crtc = connector->state->crtc;
  8889. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8890. if (ret)
  8891. goto fail;
  8892. /* Make sure the crtc and connector are running */
  8893. goto found;
  8894. }
  8895. /* Find an unused one (if possible) */
  8896. for_each_crtc(dev, possible_crtc) {
  8897. i++;
  8898. if (!(encoder->possible_crtcs & (1 << i)))
  8899. continue;
  8900. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8901. if (ret)
  8902. goto fail;
  8903. if (possible_crtc->state->enable) {
  8904. drm_modeset_unlock(&possible_crtc->mutex);
  8905. continue;
  8906. }
  8907. crtc = possible_crtc;
  8908. break;
  8909. }
  8910. /*
  8911. * If we didn't find an unused CRTC, don't use any.
  8912. */
  8913. if (!crtc) {
  8914. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8915. goto fail;
  8916. }
  8917. found:
  8918. intel_crtc = to_intel_crtc(crtc);
  8919. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8920. if (ret)
  8921. goto fail;
  8922. state = drm_atomic_state_alloc(dev);
  8923. restore_state = drm_atomic_state_alloc(dev);
  8924. if (!state || !restore_state) {
  8925. ret = -ENOMEM;
  8926. goto fail;
  8927. }
  8928. state->acquire_ctx = ctx;
  8929. restore_state->acquire_ctx = ctx;
  8930. connector_state = drm_atomic_get_connector_state(state, connector);
  8931. if (IS_ERR(connector_state)) {
  8932. ret = PTR_ERR(connector_state);
  8933. goto fail;
  8934. }
  8935. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8936. if (ret)
  8937. goto fail;
  8938. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8939. if (IS_ERR(crtc_state)) {
  8940. ret = PTR_ERR(crtc_state);
  8941. goto fail;
  8942. }
  8943. crtc_state->base.active = crtc_state->base.enable = true;
  8944. if (!mode)
  8945. mode = &load_detect_mode;
  8946. /* We need a framebuffer large enough to accommodate all accesses
  8947. * that the plane may generate whilst we perform load detection.
  8948. * We can not rely on the fbcon either being present (we get called
  8949. * during its initialisation to detect all boot displays, or it may
  8950. * not even exist) or that it is large enough to satisfy the
  8951. * requested mode.
  8952. */
  8953. fb = mode_fits_in_fbdev(dev, mode);
  8954. if (fb == NULL) {
  8955. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8956. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8957. } else
  8958. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8959. if (IS_ERR(fb)) {
  8960. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8961. goto fail;
  8962. }
  8963. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8964. if (ret)
  8965. goto fail;
  8966. drm_framebuffer_unreference(fb);
  8967. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8968. if (ret)
  8969. goto fail;
  8970. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8971. if (!ret)
  8972. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8973. if (!ret)
  8974. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8975. if (ret) {
  8976. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8977. goto fail;
  8978. }
  8979. ret = drm_atomic_commit(state);
  8980. if (ret) {
  8981. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8982. goto fail;
  8983. }
  8984. old->restore_state = restore_state;
  8985. /* let the connector get through one full cycle before testing */
  8986. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8987. return true;
  8988. fail:
  8989. drm_atomic_state_free(state);
  8990. drm_atomic_state_free(restore_state);
  8991. restore_state = state = NULL;
  8992. if (ret == -EDEADLK) {
  8993. drm_modeset_backoff(ctx);
  8994. goto retry;
  8995. }
  8996. return false;
  8997. }
  8998. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8999. struct intel_load_detect_pipe *old,
  9000. struct drm_modeset_acquire_ctx *ctx)
  9001. {
  9002. struct intel_encoder *intel_encoder =
  9003. intel_attached_encoder(connector);
  9004. struct drm_encoder *encoder = &intel_encoder->base;
  9005. struct drm_atomic_state *state = old->restore_state;
  9006. int ret;
  9007. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9008. connector->base.id, connector->name,
  9009. encoder->base.id, encoder->name);
  9010. if (!state)
  9011. return;
  9012. ret = drm_atomic_commit(state);
  9013. if (ret) {
  9014. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  9015. drm_atomic_state_free(state);
  9016. }
  9017. }
  9018. static int i9xx_pll_refclk(struct drm_device *dev,
  9019. const struct intel_crtc_state *pipe_config)
  9020. {
  9021. struct drm_i915_private *dev_priv = dev->dev_private;
  9022. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9023. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  9024. return dev_priv->vbt.lvds_ssc_freq;
  9025. else if (HAS_PCH_SPLIT(dev))
  9026. return 120000;
  9027. else if (!IS_GEN2(dev))
  9028. return 96000;
  9029. else
  9030. return 48000;
  9031. }
  9032. /* Returns the clock of the currently programmed mode of the given pipe. */
  9033. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  9034. struct intel_crtc_state *pipe_config)
  9035. {
  9036. struct drm_device *dev = crtc->base.dev;
  9037. struct drm_i915_private *dev_priv = dev->dev_private;
  9038. int pipe = pipe_config->cpu_transcoder;
  9039. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9040. u32 fp;
  9041. struct dpll clock;
  9042. int port_clock;
  9043. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9044. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9045. fp = pipe_config->dpll_hw_state.fp0;
  9046. else
  9047. fp = pipe_config->dpll_hw_state.fp1;
  9048. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9049. if (IS_PINEVIEW(dev)) {
  9050. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9051. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9052. } else {
  9053. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9054. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9055. }
  9056. if (!IS_GEN2(dev)) {
  9057. if (IS_PINEVIEW(dev))
  9058. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9059. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9060. else
  9061. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9062. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9063. switch (dpll & DPLL_MODE_MASK) {
  9064. case DPLLB_MODE_DAC_SERIAL:
  9065. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9066. 5 : 10;
  9067. break;
  9068. case DPLLB_MODE_LVDS:
  9069. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9070. 7 : 14;
  9071. break;
  9072. default:
  9073. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9074. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9075. return;
  9076. }
  9077. if (IS_PINEVIEW(dev))
  9078. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9079. else
  9080. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9081. } else {
  9082. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  9083. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9084. if (is_lvds) {
  9085. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9086. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9087. if (lvds & LVDS_CLKB_POWER_UP)
  9088. clock.p2 = 7;
  9089. else
  9090. clock.p2 = 14;
  9091. } else {
  9092. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9093. clock.p1 = 2;
  9094. else {
  9095. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9096. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9097. }
  9098. if (dpll & PLL_P2_DIVIDE_BY_4)
  9099. clock.p2 = 4;
  9100. else
  9101. clock.p2 = 2;
  9102. }
  9103. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9104. }
  9105. /*
  9106. * This value includes pixel_multiplier. We will use
  9107. * port_clock to compute adjusted_mode.crtc_clock in the
  9108. * encoder's get_config() function.
  9109. */
  9110. pipe_config->port_clock = port_clock;
  9111. }
  9112. int intel_dotclock_calculate(int link_freq,
  9113. const struct intel_link_m_n *m_n)
  9114. {
  9115. /*
  9116. * The calculation for the data clock is:
  9117. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9118. * But we want to avoid losing precison if possible, so:
  9119. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9120. *
  9121. * and the link clock is simpler:
  9122. * link_clock = (m * link_clock) / n
  9123. */
  9124. if (!m_n->link_n)
  9125. return 0;
  9126. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9127. }
  9128. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9129. struct intel_crtc_state *pipe_config)
  9130. {
  9131. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9132. /* read out port_clock from the DPLL */
  9133. i9xx_crtc_clock_get(crtc, pipe_config);
  9134. /*
  9135. * In case there is an active pipe without active ports,
  9136. * we may need some idea for the dotclock anyway.
  9137. * Calculate one based on the FDI configuration.
  9138. */
  9139. pipe_config->base.adjusted_mode.crtc_clock =
  9140. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9141. &pipe_config->fdi_m_n);
  9142. }
  9143. /** Returns the currently programmed mode of the given pipe. */
  9144. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9145. struct drm_crtc *crtc)
  9146. {
  9147. struct drm_i915_private *dev_priv = dev->dev_private;
  9148. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9149. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9150. struct drm_display_mode *mode;
  9151. struct intel_crtc_state *pipe_config;
  9152. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9153. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9154. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9155. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9156. enum pipe pipe = intel_crtc->pipe;
  9157. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9158. if (!mode)
  9159. return NULL;
  9160. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9161. if (!pipe_config) {
  9162. kfree(mode);
  9163. return NULL;
  9164. }
  9165. /*
  9166. * Construct a pipe_config sufficient for getting the clock info
  9167. * back out of crtc_clock_get.
  9168. *
  9169. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9170. * to use a real value here instead.
  9171. */
  9172. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9173. pipe_config->pixel_multiplier = 1;
  9174. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9175. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9176. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9177. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9178. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9179. mode->hdisplay = (htot & 0xffff) + 1;
  9180. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9181. mode->hsync_start = (hsync & 0xffff) + 1;
  9182. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9183. mode->vdisplay = (vtot & 0xffff) + 1;
  9184. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9185. mode->vsync_start = (vsync & 0xffff) + 1;
  9186. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9187. drm_mode_set_name(mode);
  9188. kfree(pipe_config);
  9189. return mode;
  9190. }
  9191. void intel_mark_busy(struct drm_i915_private *dev_priv)
  9192. {
  9193. if (dev_priv->mm.busy)
  9194. return;
  9195. intel_runtime_pm_get(dev_priv);
  9196. i915_update_gfx_val(dev_priv);
  9197. if (INTEL_GEN(dev_priv) >= 6)
  9198. gen6_rps_busy(dev_priv);
  9199. dev_priv->mm.busy = true;
  9200. }
  9201. void intel_mark_idle(struct drm_i915_private *dev_priv)
  9202. {
  9203. if (!dev_priv->mm.busy)
  9204. return;
  9205. dev_priv->mm.busy = false;
  9206. if (INTEL_GEN(dev_priv) >= 6)
  9207. gen6_rps_idle(dev_priv);
  9208. intel_runtime_pm_put(dev_priv);
  9209. }
  9210. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9211. {
  9212. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9213. struct drm_device *dev = crtc->dev;
  9214. struct intel_flip_work *work;
  9215. spin_lock_irq(&dev->event_lock);
  9216. work = intel_crtc->flip_work;
  9217. intel_crtc->flip_work = NULL;
  9218. spin_unlock_irq(&dev->event_lock);
  9219. if (work) {
  9220. cancel_work_sync(&work->mmio_work);
  9221. cancel_work_sync(&work->unpin_work);
  9222. kfree(work);
  9223. }
  9224. drm_crtc_cleanup(crtc);
  9225. kfree(intel_crtc);
  9226. }
  9227. static void intel_unpin_work_fn(struct work_struct *__work)
  9228. {
  9229. struct intel_flip_work *work =
  9230. container_of(__work, struct intel_flip_work, unpin_work);
  9231. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9232. struct drm_device *dev = crtc->base.dev;
  9233. struct drm_plane *primary = crtc->base.primary;
  9234. if (is_mmio_work(work))
  9235. flush_work(&work->mmio_work);
  9236. mutex_lock(&dev->struct_mutex);
  9237. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9238. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9239. if (work->flip_queued_req)
  9240. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9241. mutex_unlock(&dev->struct_mutex);
  9242. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  9243. intel_fbc_post_update(crtc);
  9244. drm_framebuffer_unreference(work->old_fb);
  9245. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9246. atomic_dec(&crtc->unpin_work_count);
  9247. kfree(work);
  9248. }
  9249. /* Is 'a' after or equal to 'b'? */
  9250. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9251. {
  9252. return !((a - b) & 0x80000000);
  9253. }
  9254. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9255. struct intel_flip_work *work)
  9256. {
  9257. struct drm_device *dev = crtc->base.dev;
  9258. struct drm_i915_private *dev_priv = dev->dev_private;
  9259. unsigned reset_counter;
  9260. reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9261. if (crtc->reset_counter != reset_counter)
  9262. return true;
  9263. /*
  9264. * The relevant registers doen't exist on pre-ctg.
  9265. * As the flip done interrupt doesn't trigger for mmio
  9266. * flips on gmch platforms, a flip count check isn't
  9267. * really needed there. But since ctg has the registers,
  9268. * include it in the check anyway.
  9269. */
  9270. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9271. return true;
  9272. /*
  9273. * BDW signals flip done immediately if the plane
  9274. * is disabled, even if the plane enable is already
  9275. * armed to occur at the next vblank :(
  9276. */
  9277. /*
  9278. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9279. * used the same base address. In that case the mmio flip might
  9280. * have completed, but the CS hasn't even executed the flip yet.
  9281. *
  9282. * A flip count check isn't enough as the CS might have updated
  9283. * the base address just after start of vblank, but before we
  9284. * managed to process the interrupt. This means we'd complete the
  9285. * CS flip too soon.
  9286. *
  9287. * Combining both checks should get us a good enough result. It may
  9288. * still happen that the CS flip has been executed, but has not
  9289. * yet actually completed. But in case the base address is the same
  9290. * anyway, we don't really care.
  9291. */
  9292. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9293. crtc->flip_work->gtt_offset &&
  9294. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9295. crtc->flip_work->flip_count);
  9296. }
  9297. static bool
  9298. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9299. struct intel_flip_work *work)
  9300. {
  9301. /*
  9302. * MMIO work completes when vblank is different from
  9303. * flip_queued_vblank.
  9304. *
  9305. * Reset counter value doesn't matter, this is handled by
  9306. * i915_wait_request finishing early, so no need to handle
  9307. * reset here.
  9308. */
  9309. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9310. }
  9311. static bool pageflip_finished(struct intel_crtc *crtc,
  9312. struct intel_flip_work *work)
  9313. {
  9314. if (!atomic_read(&work->pending))
  9315. return false;
  9316. smp_rmb();
  9317. if (is_mmio_work(work))
  9318. return __pageflip_finished_mmio(crtc, work);
  9319. else
  9320. return __pageflip_finished_cs(crtc, work);
  9321. }
  9322. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9323. {
  9324. struct drm_device *dev = dev_priv->dev;
  9325. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9326. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9327. struct intel_flip_work *work;
  9328. unsigned long flags;
  9329. /* Ignore early vblank irqs */
  9330. if (!crtc)
  9331. return;
  9332. /*
  9333. * This is called both by irq handlers and the reset code (to complete
  9334. * lost pageflips) so needs the full irqsave spinlocks.
  9335. */
  9336. spin_lock_irqsave(&dev->event_lock, flags);
  9337. work = intel_crtc->flip_work;
  9338. if (work != NULL &&
  9339. !is_mmio_work(work) &&
  9340. pageflip_finished(intel_crtc, work))
  9341. page_flip_completed(intel_crtc);
  9342. spin_unlock_irqrestore(&dev->event_lock, flags);
  9343. }
  9344. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9345. {
  9346. struct drm_device *dev = dev_priv->dev;
  9347. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9348. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9349. struct intel_flip_work *work;
  9350. unsigned long flags;
  9351. /* Ignore early vblank irqs */
  9352. if (!crtc)
  9353. return;
  9354. /*
  9355. * This is called both by irq handlers and the reset code (to complete
  9356. * lost pageflips) so needs the full irqsave spinlocks.
  9357. */
  9358. spin_lock_irqsave(&dev->event_lock, flags);
  9359. work = intel_crtc->flip_work;
  9360. if (work != NULL &&
  9361. is_mmio_work(work) &&
  9362. pageflip_finished(intel_crtc, work))
  9363. page_flip_completed(intel_crtc);
  9364. spin_unlock_irqrestore(&dev->event_lock, flags);
  9365. }
  9366. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9367. struct intel_flip_work *work)
  9368. {
  9369. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9370. /* Ensure that the work item is consistent when activating it ... */
  9371. smp_mb__before_atomic();
  9372. atomic_set(&work->pending, 1);
  9373. }
  9374. static int intel_gen2_queue_flip(struct drm_device *dev,
  9375. struct drm_crtc *crtc,
  9376. struct drm_framebuffer *fb,
  9377. struct drm_i915_gem_object *obj,
  9378. struct drm_i915_gem_request *req,
  9379. uint32_t flags)
  9380. {
  9381. struct intel_engine_cs *engine = req->engine;
  9382. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9383. u32 flip_mask;
  9384. int ret;
  9385. ret = intel_ring_begin(req, 6);
  9386. if (ret)
  9387. return ret;
  9388. /* Can't queue multiple flips, so wait for the previous
  9389. * one to finish before executing the next.
  9390. */
  9391. if (intel_crtc->plane)
  9392. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9393. else
  9394. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9395. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9396. intel_ring_emit(engine, MI_NOOP);
  9397. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9398. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9399. intel_ring_emit(engine, fb->pitches[0]);
  9400. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9401. intel_ring_emit(engine, 0); /* aux display base address, unused */
  9402. return 0;
  9403. }
  9404. static int intel_gen3_queue_flip(struct drm_device *dev,
  9405. struct drm_crtc *crtc,
  9406. struct drm_framebuffer *fb,
  9407. struct drm_i915_gem_object *obj,
  9408. struct drm_i915_gem_request *req,
  9409. uint32_t flags)
  9410. {
  9411. struct intel_engine_cs *engine = req->engine;
  9412. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9413. u32 flip_mask;
  9414. int ret;
  9415. ret = intel_ring_begin(req, 6);
  9416. if (ret)
  9417. return ret;
  9418. if (intel_crtc->plane)
  9419. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9420. else
  9421. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9422. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9423. intel_ring_emit(engine, MI_NOOP);
  9424. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
  9425. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9426. intel_ring_emit(engine, fb->pitches[0]);
  9427. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9428. intel_ring_emit(engine, MI_NOOP);
  9429. return 0;
  9430. }
  9431. static int intel_gen4_queue_flip(struct drm_device *dev,
  9432. struct drm_crtc *crtc,
  9433. struct drm_framebuffer *fb,
  9434. struct drm_i915_gem_object *obj,
  9435. struct drm_i915_gem_request *req,
  9436. uint32_t flags)
  9437. {
  9438. struct intel_engine_cs *engine = req->engine;
  9439. struct drm_i915_private *dev_priv = dev->dev_private;
  9440. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9441. uint32_t pf, pipesrc;
  9442. int ret;
  9443. ret = intel_ring_begin(req, 4);
  9444. if (ret)
  9445. return ret;
  9446. /* i965+ uses the linear or tiled offsets from the
  9447. * Display Registers (which do not change across a page-flip)
  9448. * so we need only reprogram the base address.
  9449. */
  9450. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9451. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9452. intel_ring_emit(engine, fb->pitches[0]);
  9453. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
  9454. obj->tiling_mode);
  9455. /* XXX Enabling the panel-fitter across page-flip is so far
  9456. * untested on non-native modes, so ignore it for now.
  9457. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9458. */
  9459. pf = 0;
  9460. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9461. intel_ring_emit(engine, pf | pipesrc);
  9462. return 0;
  9463. }
  9464. static int intel_gen6_queue_flip(struct drm_device *dev,
  9465. struct drm_crtc *crtc,
  9466. struct drm_framebuffer *fb,
  9467. struct drm_i915_gem_object *obj,
  9468. struct drm_i915_gem_request *req,
  9469. uint32_t flags)
  9470. {
  9471. struct intel_engine_cs *engine = req->engine;
  9472. struct drm_i915_private *dev_priv = dev->dev_private;
  9473. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9474. uint32_t pf, pipesrc;
  9475. int ret;
  9476. ret = intel_ring_begin(req, 4);
  9477. if (ret)
  9478. return ret;
  9479. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9480. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9481. intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
  9482. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9483. /* Contrary to the suggestions in the documentation,
  9484. * "Enable Panel Fitter" does not seem to be required when page
  9485. * flipping with a non-native mode, and worse causes a normal
  9486. * modeset to fail.
  9487. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9488. */
  9489. pf = 0;
  9490. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9491. intel_ring_emit(engine, pf | pipesrc);
  9492. return 0;
  9493. }
  9494. static int intel_gen7_queue_flip(struct drm_device *dev,
  9495. struct drm_crtc *crtc,
  9496. struct drm_framebuffer *fb,
  9497. struct drm_i915_gem_object *obj,
  9498. struct drm_i915_gem_request *req,
  9499. uint32_t flags)
  9500. {
  9501. struct intel_engine_cs *engine = req->engine;
  9502. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9503. uint32_t plane_bit = 0;
  9504. int len, ret;
  9505. switch (intel_crtc->plane) {
  9506. case PLANE_A:
  9507. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9508. break;
  9509. case PLANE_B:
  9510. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9511. break;
  9512. case PLANE_C:
  9513. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9514. break;
  9515. default:
  9516. WARN_ONCE(1, "unknown plane in flip command\n");
  9517. return -ENODEV;
  9518. }
  9519. len = 4;
  9520. if (engine->id == RCS) {
  9521. len += 6;
  9522. /*
  9523. * On Gen 8, SRM is now taking an extra dword to accommodate
  9524. * 48bits addresses, and we need a NOOP for the batch size to
  9525. * stay even.
  9526. */
  9527. if (IS_GEN8(dev))
  9528. len += 2;
  9529. }
  9530. /*
  9531. * BSpec MI_DISPLAY_FLIP for IVB:
  9532. * "The full packet must be contained within the same cache line."
  9533. *
  9534. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9535. * cacheline, if we ever start emitting more commands before
  9536. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9537. * then do the cacheline alignment, and finally emit the
  9538. * MI_DISPLAY_FLIP.
  9539. */
  9540. ret = intel_ring_cacheline_align(req);
  9541. if (ret)
  9542. return ret;
  9543. ret = intel_ring_begin(req, len);
  9544. if (ret)
  9545. return ret;
  9546. /* Unmask the flip-done completion message. Note that the bspec says that
  9547. * we should do this for both the BCS and RCS, and that we must not unmask
  9548. * more than one flip event at any time (or ensure that one flip message
  9549. * can be sent by waiting for flip-done prior to queueing new flips).
  9550. * Experimentation says that BCS works despite DERRMR masking all
  9551. * flip-done completion events and that unmasking all planes at once
  9552. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9553. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9554. */
  9555. if (engine->id == RCS) {
  9556. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
  9557. intel_ring_emit_reg(engine, DERRMR);
  9558. intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9559. DERRMR_PIPEB_PRI_FLIP_DONE |
  9560. DERRMR_PIPEC_PRI_FLIP_DONE));
  9561. if (IS_GEN8(dev))
  9562. intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
  9563. MI_SRM_LRM_GLOBAL_GTT);
  9564. else
  9565. intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
  9566. MI_SRM_LRM_GLOBAL_GTT);
  9567. intel_ring_emit_reg(engine, DERRMR);
  9568. intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
  9569. if (IS_GEN8(dev)) {
  9570. intel_ring_emit(engine, 0);
  9571. intel_ring_emit(engine, MI_NOOP);
  9572. }
  9573. }
  9574. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
  9575. intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
  9576. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9577. intel_ring_emit(engine, (MI_NOOP));
  9578. return 0;
  9579. }
  9580. static bool use_mmio_flip(struct intel_engine_cs *engine,
  9581. struct drm_i915_gem_object *obj)
  9582. {
  9583. /*
  9584. * This is not being used for older platforms, because
  9585. * non-availability of flip done interrupt forces us to use
  9586. * CS flips. Older platforms derive flip done using some clever
  9587. * tricks involving the flip_pending status bits and vblank irqs.
  9588. * So using MMIO flips there would disrupt this mechanism.
  9589. */
  9590. if (engine == NULL)
  9591. return true;
  9592. if (INTEL_GEN(engine->i915) < 5)
  9593. return false;
  9594. if (i915.use_mmio_flip < 0)
  9595. return false;
  9596. else if (i915.use_mmio_flip > 0)
  9597. return true;
  9598. else if (i915.enable_execlists)
  9599. return true;
  9600. else if (obj->base.dma_buf &&
  9601. !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
  9602. false))
  9603. return true;
  9604. else
  9605. return engine != i915_gem_request_get_engine(obj->last_write_req);
  9606. }
  9607. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9608. unsigned int rotation,
  9609. struct intel_flip_work *work)
  9610. {
  9611. struct drm_device *dev = intel_crtc->base.dev;
  9612. struct drm_i915_private *dev_priv = dev->dev_private;
  9613. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9614. const enum pipe pipe = intel_crtc->pipe;
  9615. u32 ctl, stride, tile_height;
  9616. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9617. ctl &= ~PLANE_CTL_TILED_MASK;
  9618. switch (fb->modifier[0]) {
  9619. case DRM_FORMAT_MOD_NONE:
  9620. break;
  9621. case I915_FORMAT_MOD_X_TILED:
  9622. ctl |= PLANE_CTL_TILED_X;
  9623. break;
  9624. case I915_FORMAT_MOD_Y_TILED:
  9625. ctl |= PLANE_CTL_TILED_Y;
  9626. break;
  9627. case I915_FORMAT_MOD_Yf_TILED:
  9628. ctl |= PLANE_CTL_TILED_YF;
  9629. break;
  9630. default:
  9631. MISSING_CASE(fb->modifier[0]);
  9632. }
  9633. /*
  9634. * The stride is either expressed as a multiple of 64 bytes chunks for
  9635. * linear buffers or in number of tiles for tiled buffers.
  9636. */
  9637. if (intel_rotation_90_or_270(rotation)) {
  9638. /* stride = Surface height in tiles */
  9639. tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
  9640. stride = DIV_ROUND_UP(fb->height, tile_height);
  9641. } else {
  9642. stride = fb->pitches[0] /
  9643. intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  9644. fb->pixel_format);
  9645. }
  9646. /*
  9647. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9648. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9649. */
  9650. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9651. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9652. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9653. POSTING_READ(PLANE_SURF(pipe, 0));
  9654. }
  9655. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9656. struct intel_flip_work *work)
  9657. {
  9658. struct drm_device *dev = intel_crtc->base.dev;
  9659. struct drm_i915_private *dev_priv = dev->dev_private;
  9660. struct intel_framebuffer *intel_fb =
  9661. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9662. struct drm_i915_gem_object *obj = intel_fb->obj;
  9663. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  9664. u32 dspcntr;
  9665. dspcntr = I915_READ(reg);
  9666. if (obj->tiling_mode != I915_TILING_NONE)
  9667. dspcntr |= DISPPLANE_TILED;
  9668. else
  9669. dspcntr &= ~DISPPLANE_TILED;
  9670. I915_WRITE(reg, dspcntr);
  9671. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9672. POSTING_READ(DSPSURF(intel_crtc->plane));
  9673. }
  9674. static void intel_mmio_flip_work_func(struct work_struct *w)
  9675. {
  9676. struct intel_flip_work *work =
  9677. container_of(w, struct intel_flip_work, mmio_work);
  9678. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9679. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9680. struct intel_framebuffer *intel_fb =
  9681. to_intel_framebuffer(crtc->base.primary->fb);
  9682. struct drm_i915_gem_object *obj = intel_fb->obj;
  9683. if (work->flip_queued_req)
  9684. WARN_ON(__i915_wait_request(work->flip_queued_req,
  9685. false, NULL,
  9686. &dev_priv->rps.mmioflips));
  9687. /* For framebuffer backed by dmabuf, wait for fence */
  9688. if (obj->base.dma_buf)
  9689. WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  9690. false, false,
  9691. MAX_SCHEDULE_TIMEOUT) < 0);
  9692. intel_pipe_update_start(crtc);
  9693. if (INTEL_GEN(dev_priv) >= 9)
  9694. skl_do_mmio_flip(crtc, work->rotation, work);
  9695. else
  9696. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9697. ilk_do_mmio_flip(crtc, work);
  9698. intel_pipe_update_end(crtc, work);
  9699. }
  9700. static int intel_default_queue_flip(struct drm_device *dev,
  9701. struct drm_crtc *crtc,
  9702. struct drm_framebuffer *fb,
  9703. struct drm_i915_gem_object *obj,
  9704. struct drm_i915_gem_request *req,
  9705. uint32_t flags)
  9706. {
  9707. return -ENODEV;
  9708. }
  9709. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  9710. struct intel_crtc *intel_crtc,
  9711. struct intel_flip_work *work)
  9712. {
  9713. u32 addr, vblank;
  9714. if (!atomic_read(&work->pending))
  9715. return false;
  9716. smp_rmb();
  9717. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  9718. if (work->flip_ready_vblank == 0) {
  9719. if (work->flip_queued_req &&
  9720. !i915_gem_request_completed(work->flip_queued_req, true))
  9721. return false;
  9722. work->flip_ready_vblank = vblank;
  9723. }
  9724. if (vblank - work->flip_ready_vblank < 3)
  9725. return false;
  9726. /* Potential stall - if we see that the flip has happened,
  9727. * assume a missed interrupt. */
  9728. if (INTEL_GEN(dev_priv) >= 4)
  9729. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9730. else
  9731. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9732. /* There is a potential issue here with a false positive after a flip
  9733. * to the same address. We could address this by checking for a
  9734. * non-incrementing frame counter.
  9735. */
  9736. return addr == work->gtt_offset;
  9737. }
  9738. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  9739. {
  9740. struct drm_device *dev = dev_priv->dev;
  9741. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9742. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9743. struct intel_flip_work *work;
  9744. WARN_ON(!in_interrupt());
  9745. if (crtc == NULL)
  9746. return;
  9747. spin_lock(&dev->event_lock);
  9748. work = intel_crtc->flip_work;
  9749. if (work != NULL && !is_mmio_work(work) &&
  9750. __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
  9751. WARN_ONCE(1,
  9752. "Kicking stuck page flip: queued at %d, now %d\n",
  9753. work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
  9754. page_flip_completed(intel_crtc);
  9755. work = NULL;
  9756. }
  9757. if (work != NULL && !is_mmio_work(work) &&
  9758. intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
  9759. intel_queue_rps_boost_for_request(work->flip_queued_req);
  9760. spin_unlock(&dev->event_lock);
  9761. }
  9762. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9763. struct drm_framebuffer *fb,
  9764. struct drm_pending_vblank_event *event,
  9765. uint32_t page_flip_flags)
  9766. {
  9767. struct drm_device *dev = crtc->dev;
  9768. struct drm_i915_private *dev_priv = dev->dev_private;
  9769. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9770. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9771. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9772. struct drm_plane *primary = crtc->primary;
  9773. enum pipe pipe = intel_crtc->pipe;
  9774. struct intel_flip_work *work;
  9775. struct intel_engine_cs *engine;
  9776. bool mmio_flip;
  9777. struct drm_i915_gem_request *request = NULL;
  9778. int ret;
  9779. /*
  9780. * drm_mode_page_flip_ioctl() should already catch this, but double
  9781. * check to be safe. In the future we may enable pageflipping from
  9782. * a disabled primary plane.
  9783. */
  9784. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9785. return -EBUSY;
  9786. /* Can't change pixel format via MI display flips. */
  9787. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9788. return -EINVAL;
  9789. /*
  9790. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9791. * Note that pitch changes could also affect these register.
  9792. */
  9793. if (INTEL_INFO(dev)->gen > 3 &&
  9794. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9795. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9796. return -EINVAL;
  9797. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9798. goto out_hang;
  9799. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9800. if (work == NULL)
  9801. return -ENOMEM;
  9802. work->event = event;
  9803. work->crtc = crtc;
  9804. work->old_fb = old_fb;
  9805. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  9806. ret = drm_crtc_vblank_get(crtc);
  9807. if (ret)
  9808. goto free_work;
  9809. /* We borrow the event spin lock for protecting flip_work */
  9810. spin_lock_irq(&dev->event_lock);
  9811. if (intel_crtc->flip_work) {
  9812. /* Before declaring the flip queue wedged, check if
  9813. * the hardware completed the operation behind our backs.
  9814. */
  9815. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  9816. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9817. page_flip_completed(intel_crtc);
  9818. } else {
  9819. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9820. spin_unlock_irq(&dev->event_lock);
  9821. drm_crtc_vblank_put(crtc);
  9822. kfree(work);
  9823. return -EBUSY;
  9824. }
  9825. }
  9826. intel_crtc->flip_work = work;
  9827. spin_unlock_irq(&dev->event_lock);
  9828. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9829. flush_workqueue(dev_priv->wq);
  9830. /* Reference the objects for the scheduled work. */
  9831. drm_framebuffer_reference(work->old_fb);
  9832. drm_gem_object_reference(&obj->base);
  9833. crtc->primary->fb = fb;
  9834. update_state_fb(crtc->primary);
  9835. intel_fbc_pre_update(intel_crtc);
  9836. work->pending_flip_obj = obj;
  9837. ret = i915_mutex_lock_interruptible(dev);
  9838. if (ret)
  9839. goto cleanup;
  9840. intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9841. if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
  9842. ret = -EIO;
  9843. goto cleanup;
  9844. }
  9845. atomic_inc(&intel_crtc->unpin_work_count);
  9846. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9847. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9848. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  9849. engine = &dev_priv->engine[BCS];
  9850. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9851. /* vlv: DISPLAY_FLIP fails to change tiling */
  9852. engine = NULL;
  9853. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9854. engine = &dev_priv->engine[BCS];
  9855. } else if (INTEL_INFO(dev)->gen >= 7) {
  9856. engine = i915_gem_request_get_engine(obj->last_write_req);
  9857. if (engine == NULL || engine->id != RCS)
  9858. engine = &dev_priv->engine[BCS];
  9859. } else {
  9860. engine = &dev_priv->engine[RCS];
  9861. }
  9862. mmio_flip = use_mmio_flip(engine, obj);
  9863. /* When using CS flips, we want to emit semaphores between rings.
  9864. * However, when using mmio flips we will create a task to do the
  9865. * synchronisation, so all we want here is to pin the framebuffer
  9866. * into the display plane and skip any waits.
  9867. */
  9868. if (!mmio_flip) {
  9869. ret = i915_gem_object_sync(obj, engine, &request);
  9870. if (!ret && !request) {
  9871. request = i915_gem_request_alloc(engine, NULL);
  9872. ret = PTR_ERR_OR_ZERO(request);
  9873. }
  9874. if (ret)
  9875. goto cleanup_pending;
  9876. }
  9877. ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  9878. if (ret)
  9879. goto cleanup_pending;
  9880. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9881. obj, 0);
  9882. work->gtt_offset += intel_crtc->dspaddr_offset;
  9883. work->rotation = crtc->primary->state->rotation;
  9884. if (mmio_flip) {
  9885. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  9886. i915_gem_request_assign(&work->flip_queued_req,
  9887. obj->last_write_req);
  9888. schedule_work(&work->mmio_work);
  9889. } else {
  9890. i915_gem_request_assign(&work->flip_queued_req, request);
  9891. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9892. page_flip_flags);
  9893. if (ret)
  9894. goto cleanup_unpin;
  9895. intel_mark_page_flip_active(intel_crtc, work);
  9896. i915_add_request_no_flush(request);
  9897. }
  9898. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  9899. to_intel_plane(primary)->frontbuffer_bit);
  9900. mutex_unlock(&dev->struct_mutex);
  9901. intel_frontbuffer_flip_prepare(dev,
  9902. to_intel_plane(primary)->frontbuffer_bit);
  9903. trace_i915_flip_request(intel_crtc->plane, obj);
  9904. return 0;
  9905. cleanup_unpin:
  9906. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  9907. cleanup_pending:
  9908. if (!IS_ERR_OR_NULL(request))
  9909. i915_add_request_no_flush(request);
  9910. atomic_dec(&intel_crtc->unpin_work_count);
  9911. mutex_unlock(&dev->struct_mutex);
  9912. cleanup:
  9913. crtc->primary->fb = old_fb;
  9914. update_state_fb(crtc->primary);
  9915. drm_gem_object_unreference_unlocked(&obj->base);
  9916. drm_framebuffer_unreference(work->old_fb);
  9917. spin_lock_irq(&dev->event_lock);
  9918. intel_crtc->flip_work = NULL;
  9919. spin_unlock_irq(&dev->event_lock);
  9920. drm_crtc_vblank_put(crtc);
  9921. free_work:
  9922. kfree(work);
  9923. if (ret == -EIO) {
  9924. struct drm_atomic_state *state;
  9925. struct drm_plane_state *plane_state;
  9926. out_hang:
  9927. state = drm_atomic_state_alloc(dev);
  9928. if (!state)
  9929. return -ENOMEM;
  9930. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9931. retry:
  9932. plane_state = drm_atomic_get_plane_state(state, primary);
  9933. ret = PTR_ERR_OR_ZERO(plane_state);
  9934. if (!ret) {
  9935. drm_atomic_set_fb_for_plane(plane_state, fb);
  9936. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9937. if (!ret)
  9938. ret = drm_atomic_commit(state);
  9939. }
  9940. if (ret == -EDEADLK) {
  9941. drm_modeset_backoff(state->acquire_ctx);
  9942. drm_atomic_state_clear(state);
  9943. goto retry;
  9944. }
  9945. if (ret)
  9946. drm_atomic_state_free(state);
  9947. if (ret == 0 && event) {
  9948. spin_lock_irq(&dev->event_lock);
  9949. drm_crtc_send_vblank_event(crtc, event);
  9950. spin_unlock_irq(&dev->event_lock);
  9951. }
  9952. }
  9953. return ret;
  9954. }
  9955. /**
  9956. * intel_wm_need_update - Check whether watermarks need updating
  9957. * @plane: drm plane
  9958. * @state: new plane state
  9959. *
  9960. * Check current plane state versus the new one to determine whether
  9961. * watermarks need to be recalculated.
  9962. *
  9963. * Returns true or false.
  9964. */
  9965. static bool intel_wm_need_update(struct drm_plane *plane,
  9966. struct drm_plane_state *state)
  9967. {
  9968. struct intel_plane_state *new = to_intel_plane_state(state);
  9969. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9970. /* Update watermarks on tiling or size changes. */
  9971. if (new->visible != cur->visible)
  9972. return true;
  9973. if (!cur->base.fb || !new->base.fb)
  9974. return false;
  9975. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  9976. cur->base.rotation != new->base.rotation ||
  9977. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9978. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9979. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9980. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9981. return true;
  9982. return false;
  9983. }
  9984. static bool needs_scaling(struct intel_plane_state *state)
  9985. {
  9986. int src_w = drm_rect_width(&state->src) >> 16;
  9987. int src_h = drm_rect_height(&state->src) >> 16;
  9988. int dst_w = drm_rect_width(&state->dst);
  9989. int dst_h = drm_rect_height(&state->dst);
  9990. return (src_w != dst_w || src_h != dst_h);
  9991. }
  9992. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9993. struct drm_plane_state *plane_state)
  9994. {
  9995. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9996. struct drm_crtc *crtc = crtc_state->crtc;
  9997. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9998. struct drm_plane *plane = plane_state->plane;
  9999. struct drm_device *dev = crtc->dev;
  10000. struct drm_i915_private *dev_priv = to_i915(dev);
  10001. struct intel_plane_state *old_plane_state =
  10002. to_intel_plane_state(plane->state);
  10003. bool mode_changed = needs_modeset(crtc_state);
  10004. bool was_crtc_enabled = crtc->state->active;
  10005. bool is_crtc_enabled = crtc_state->active;
  10006. bool turn_off, turn_on, visible, was_visible;
  10007. struct drm_framebuffer *fb = plane_state->fb;
  10008. int ret;
  10009. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  10010. plane->type != DRM_PLANE_TYPE_CURSOR) {
  10011. ret = skl_update_scaler_plane(
  10012. to_intel_crtc_state(crtc_state),
  10013. to_intel_plane_state(plane_state));
  10014. if (ret)
  10015. return ret;
  10016. }
  10017. was_visible = old_plane_state->visible;
  10018. visible = to_intel_plane_state(plane_state)->visible;
  10019. if (!was_crtc_enabled && WARN_ON(was_visible))
  10020. was_visible = false;
  10021. /*
  10022. * Visibility is calculated as if the crtc was on, but
  10023. * after scaler setup everything depends on it being off
  10024. * when the crtc isn't active.
  10025. *
  10026. * FIXME this is wrong for watermarks. Watermarks should also
  10027. * be computed as if the pipe would be active. Perhaps move
  10028. * per-plane wm computation to the .check_plane() hook, and
  10029. * only combine the results from all planes in the current place?
  10030. */
  10031. if (!is_crtc_enabled)
  10032. to_intel_plane_state(plane_state)->visible = visible = false;
  10033. if (!was_visible && !visible)
  10034. return 0;
  10035. if (fb != old_plane_state->base.fb)
  10036. pipe_config->fb_changed = true;
  10037. turn_off = was_visible && (!visible || mode_changed);
  10038. turn_on = visible && (!was_visible || mode_changed);
  10039. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  10040. intel_crtc->base.base.id,
  10041. intel_crtc->base.name,
  10042. plane->base.id, plane->name,
  10043. fb ? fb->base.id : -1);
  10044. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  10045. plane->base.id, plane->name,
  10046. was_visible, visible,
  10047. turn_off, turn_on, mode_changed);
  10048. if (turn_on) {
  10049. pipe_config->update_wm_pre = true;
  10050. /* must disable cxsr around plane enable/disable */
  10051. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10052. pipe_config->disable_cxsr = true;
  10053. } else if (turn_off) {
  10054. pipe_config->update_wm_post = true;
  10055. /* must disable cxsr around plane enable/disable */
  10056. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10057. pipe_config->disable_cxsr = true;
  10058. } else if (intel_wm_need_update(plane, plane_state)) {
  10059. /* FIXME bollocks */
  10060. pipe_config->update_wm_pre = true;
  10061. pipe_config->update_wm_post = true;
  10062. }
  10063. /* Pre-gen9 platforms need two-step watermark updates */
  10064. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10065. INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
  10066. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10067. if (visible || was_visible)
  10068. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10069. /*
  10070. * WaCxSRDisabledForSpriteScaling:ivb
  10071. *
  10072. * cstate->update_wm was already set above, so this flag will
  10073. * take effect when we commit and program watermarks.
  10074. */
  10075. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
  10076. needs_scaling(to_intel_plane_state(plane_state)) &&
  10077. !needs_scaling(old_plane_state))
  10078. pipe_config->disable_lp_wm = true;
  10079. return 0;
  10080. }
  10081. static bool encoders_cloneable(const struct intel_encoder *a,
  10082. const struct intel_encoder *b)
  10083. {
  10084. /* masks could be asymmetric, so check both ways */
  10085. return a == b || (a->cloneable & (1 << b->type) &&
  10086. b->cloneable & (1 << a->type));
  10087. }
  10088. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10089. struct intel_crtc *crtc,
  10090. struct intel_encoder *encoder)
  10091. {
  10092. struct intel_encoder *source_encoder;
  10093. struct drm_connector *connector;
  10094. struct drm_connector_state *connector_state;
  10095. int i;
  10096. for_each_connector_in_state(state, connector, connector_state, i) {
  10097. if (connector_state->crtc != &crtc->base)
  10098. continue;
  10099. source_encoder =
  10100. to_intel_encoder(connector_state->best_encoder);
  10101. if (!encoders_cloneable(encoder, source_encoder))
  10102. return false;
  10103. }
  10104. return true;
  10105. }
  10106. static bool check_encoder_cloning(struct drm_atomic_state *state,
  10107. struct intel_crtc *crtc)
  10108. {
  10109. struct intel_encoder *encoder;
  10110. struct drm_connector *connector;
  10111. struct drm_connector_state *connector_state;
  10112. int i;
  10113. for_each_connector_in_state(state, connector, connector_state, i) {
  10114. if (connector_state->crtc != &crtc->base)
  10115. continue;
  10116. encoder = to_intel_encoder(connector_state->best_encoder);
  10117. if (!check_single_encoder_cloning(state, crtc, encoder))
  10118. return false;
  10119. }
  10120. return true;
  10121. }
  10122. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10123. struct drm_crtc_state *crtc_state)
  10124. {
  10125. struct drm_device *dev = crtc->dev;
  10126. struct drm_i915_private *dev_priv = dev->dev_private;
  10127. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10128. struct intel_crtc_state *pipe_config =
  10129. to_intel_crtc_state(crtc_state);
  10130. struct drm_atomic_state *state = crtc_state->state;
  10131. int ret;
  10132. bool mode_changed = needs_modeset(crtc_state);
  10133. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  10134. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10135. return -EINVAL;
  10136. }
  10137. if (mode_changed && !crtc_state->active)
  10138. pipe_config->update_wm_post = true;
  10139. if (mode_changed && crtc_state->enable &&
  10140. dev_priv->display.crtc_compute_clock &&
  10141. !WARN_ON(pipe_config->shared_dpll)) {
  10142. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10143. pipe_config);
  10144. if (ret)
  10145. return ret;
  10146. }
  10147. if (crtc_state->color_mgmt_changed) {
  10148. ret = intel_color_check(crtc, crtc_state);
  10149. if (ret)
  10150. return ret;
  10151. }
  10152. ret = 0;
  10153. if (dev_priv->display.compute_pipe_wm) {
  10154. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10155. if (ret) {
  10156. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10157. return ret;
  10158. }
  10159. }
  10160. if (dev_priv->display.compute_intermediate_wm &&
  10161. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10162. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10163. return 0;
  10164. /*
  10165. * Calculate 'intermediate' watermarks that satisfy both the
  10166. * old state and the new state. We can program these
  10167. * immediately.
  10168. */
  10169. ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
  10170. intel_crtc,
  10171. pipe_config);
  10172. if (ret) {
  10173. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10174. return ret;
  10175. }
  10176. } else if (dev_priv->display.compute_intermediate_wm) {
  10177. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10178. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10179. }
  10180. if (INTEL_INFO(dev)->gen >= 9) {
  10181. if (mode_changed)
  10182. ret = skl_update_scaler_crtc(pipe_config);
  10183. if (!ret)
  10184. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10185. pipe_config);
  10186. }
  10187. return ret;
  10188. }
  10189. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10190. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10191. .atomic_begin = intel_begin_crtc_commit,
  10192. .atomic_flush = intel_finish_crtc_commit,
  10193. .atomic_check = intel_crtc_atomic_check,
  10194. };
  10195. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10196. {
  10197. struct intel_connector *connector;
  10198. for_each_intel_connector(dev, connector) {
  10199. if (connector->base.state->crtc)
  10200. drm_connector_unreference(&connector->base);
  10201. if (connector->base.encoder) {
  10202. connector->base.state->best_encoder =
  10203. connector->base.encoder;
  10204. connector->base.state->crtc =
  10205. connector->base.encoder->crtc;
  10206. drm_connector_reference(&connector->base);
  10207. } else {
  10208. connector->base.state->best_encoder = NULL;
  10209. connector->base.state->crtc = NULL;
  10210. }
  10211. }
  10212. }
  10213. static void
  10214. connected_sink_compute_bpp(struct intel_connector *connector,
  10215. struct intel_crtc_state *pipe_config)
  10216. {
  10217. int bpp = pipe_config->pipe_bpp;
  10218. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10219. connector->base.base.id,
  10220. connector->base.name);
  10221. /* Don't use an invalid EDID bpc value */
  10222. if (connector->base.display_info.bpc &&
  10223. connector->base.display_info.bpc * 3 < bpp) {
  10224. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10225. bpp, connector->base.display_info.bpc*3);
  10226. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10227. }
  10228. /* Clamp bpp to default limit on screens without EDID 1.4 */
  10229. if (connector->base.display_info.bpc == 0) {
  10230. int type = connector->base.connector_type;
  10231. int clamp_bpp = 24;
  10232. /* Fall back to 18 bpp when DP sink capability is unknown. */
  10233. if (type == DRM_MODE_CONNECTOR_DisplayPort ||
  10234. type == DRM_MODE_CONNECTOR_eDP)
  10235. clamp_bpp = 18;
  10236. if (bpp > clamp_bpp) {
  10237. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
  10238. bpp, clamp_bpp);
  10239. pipe_config->pipe_bpp = clamp_bpp;
  10240. }
  10241. }
  10242. }
  10243. static int
  10244. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10245. struct intel_crtc_state *pipe_config)
  10246. {
  10247. struct drm_device *dev = crtc->base.dev;
  10248. struct drm_atomic_state *state;
  10249. struct drm_connector *connector;
  10250. struct drm_connector_state *connector_state;
  10251. int bpp, i;
  10252. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  10253. bpp = 10*3;
  10254. else if (INTEL_INFO(dev)->gen >= 5)
  10255. bpp = 12*3;
  10256. else
  10257. bpp = 8*3;
  10258. pipe_config->pipe_bpp = bpp;
  10259. state = pipe_config->base.state;
  10260. /* Clamp display bpp to EDID value */
  10261. for_each_connector_in_state(state, connector, connector_state, i) {
  10262. if (connector_state->crtc != &crtc->base)
  10263. continue;
  10264. connected_sink_compute_bpp(to_intel_connector(connector),
  10265. pipe_config);
  10266. }
  10267. return bpp;
  10268. }
  10269. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10270. {
  10271. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10272. "type: 0x%x flags: 0x%x\n",
  10273. mode->crtc_clock,
  10274. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10275. mode->crtc_hsync_end, mode->crtc_htotal,
  10276. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10277. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10278. }
  10279. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10280. struct intel_crtc_state *pipe_config,
  10281. const char *context)
  10282. {
  10283. struct drm_device *dev = crtc->base.dev;
  10284. struct drm_plane *plane;
  10285. struct intel_plane *intel_plane;
  10286. struct intel_plane_state *state;
  10287. struct drm_framebuffer *fb;
  10288. DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
  10289. crtc->base.base.id, crtc->base.name,
  10290. context, pipe_config, pipe_name(crtc->pipe));
  10291. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  10292. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10293. pipe_config->pipe_bpp, pipe_config->dither);
  10294. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10295. pipe_config->has_pch_encoder,
  10296. pipe_config->fdi_lanes,
  10297. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10298. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10299. pipe_config->fdi_m_n.tu);
  10300. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10301. pipe_config->has_dp_encoder,
  10302. pipe_config->lane_count,
  10303. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10304. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10305. pipe_config->dp_m_n.tu);
  10306. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10307. pipe_config->has_dp_encoder,
  10308. pipe_config->lane_count,
  10309. pipe_config->dp_m2_n2.gmch_m,
  10310. pipe_config->dp_m2_n2.gmch_n,
  10311. pipe_config->dp_m2_n2.link_m,
  10312. pipe_config->dp_m2_n2.link_n,
  10313. pipe_config->dp_m2_n2.tu);
  10314. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10315. pipe_config->has_audio,
  10316. pipe_config->has_infoframe);
  10317. DRM_DEBUG_KMS("requested mode:\n");
  10318. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10319. DRM_DEBUG_KMS("adjusted mode:\n");
  10320. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10321. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10322. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10323. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10324. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10325. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10326. crtc->num_scalers,
  10327. pipe_config->scaler_state.scaler_users,
  10328. pipe_config->scaler_state.scaler_id);
  10329. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10330. pipe_config->gmch_pfit.control,
  10331. pipe_config->gmch_pfit.pgm_ratios,
  10332. pipe_config->gmch_pfit.lvds_border_bits);
  10333. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10334. pipe_config->pch_pfit.pos,
  10335. pipe_config->pch_pfit.size,
  10336. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10337. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10338. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10339. if (IS_BROXTON(dev)) {
  10340. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10341. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10342. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10343. pipe_config->ddi_pll_sel,
  10344. pipe_config->dpll_hw_state.ebb0,
  10345. pipe_config->dpll_hw_state.ebb4,
  10346. pipe_config->dpll_hw_state.pll0,
  10347. pipe_config->dpll_hw_state.pll1,
  10348. pipe_config->dpll_hw_state.pll2,
  10349. pipe_config->dpll_hw_state.pll3,
  10350. pipe_config->dpll_hw_state.pll6,
  10351. pipe_config->dpll_hw_state.pll8,
  10352. pipe_config->dpll_hw_state.pll9,
  10353. pipe_config->dpll_hw_state.pll10,
  10354. pipe_config->dpll_hw_state.pcsdw12);
  10355. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10356. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10357. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10358. pipe_config->ddi_pll_sel,
  10359. pipe_config->dpll_hw_state.ctrl1,
  10360. pipe_config->dpll_hw_state.cfgcr1,
  10361. pipe_config->dpll_hw_state.cfgcr2);
  10362. } else if (HAS_DDI(dev)) {
  10363. DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10364. pipe_config->ddi_pll_sel,
  10365. pipe_config->dpll_hw_state.wrpll,
  10366. pipe_config->dpll_hw_state.spll);
  10367. } else {
  10368. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10369. "fp0: 0x%x, fp1: 0x%x\n",
  10370. pipe_config->dpll_hw_state.dpll,
  10371. pipe_config->dpll_hw_state.dpll_md,
  10372. pipe_config->dpll_hw_state.fp0,
  10373. pipe_config->dpll_hw_state.fp1);
  10374. }
  10375. DRM_DEBUG_KMS("planes on this crtc\n");
  10376. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10377. intel_plane = to_intel_plane(plane);
  10378. if (intel_plane->pipe != crtc->pipe)
  10379. continue;
  10380. state = to_intel_plane_state(plane->state);
  10381. fb = state->base.fb;
  10382. if (!fb) {
  10383. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10384. plane->base.id, plane->name, state->scaler_id);
  10385. continue;
  10386. }
  10387. DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
  10388. plane->base.id, plane->name);
  10389. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
  10390. fb->base.id, fb->width, fb->height,
  10391. drm_get_format_name(fb->pixel_format));
  10392. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10393. state->scaler_id,
  10394. state->src.x1 >> 16, state->src.y1 >> 16,
  10395. drm_rect_width(&state->src) >> 16,
  10396. drm_rect_height(&state->src) >> 16,
  10397. state->dst.x1, state->dst.y1,
  10398. drm_rect_width(&state->dst),
  10399. drm_rect_height(&state->dst));
  10400. }
  10401. }
  10402. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10403. {
  10404. struct drm_device *dev = state->dev;
  10405. struct drm_connector *connector;
  10406. unsigned int used_ports = 0;
  10407. /*
  10408. * Walk the connector list instead of the encoder
  10409. * list to detect the problem on ddi platforms
  10410. * where there's just one encoder per digital port.
  10411. */
  10412. drm_for_each_connector(connector, dev) {
  10413. struct drm_connector_state *connector_state;
  10414. struct intel_encoder *encoder;
  10415. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10416. if (!connector_state)
  10417. connector_state = connector->state;
  10418. if (!connector_state->best_encoder)
  10419. continue;
  10420. encoder = to_intel_encoder(connector_state->best_encoder);
  10421. WARN_ON(!connector_state->crtc);
  10422. switch (encoder->type) {
  10423. unsigned int port_mask;
  10424. case INTEL_OUTPUT_UNKNOWN:
  10425. if (WARN_ON(!HAS_DDI(dev)))
  10426. break;
  10427. case INTEL_OUTPUT_DISPLAYPORT:
  10428. case INTEL_OUTPUT_HDMI:
  10429. case INTEL_OUTPUT_EDP:
  10430. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10431. /* the same port mustn't appear more than once */
  10432. if (used_ports & port_mask)
  10433. return false;
  10434. used_ports |= port_mask;
  10435. default:
  10436. break;
  10437. }
  10438. }
  10439. return true;
  10440. }
  10441. static void
  10442. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10443. {
  10444. struct drm_crtc_state tmp_state;
  10445. struct intel_crtc_scaler_state scaler_state;
  10446. struct intel_dpll_hw_state dpll_hw_state;
  10447. struct intel_shared_dpll *shared_dpll;
  10448. uint32_t ddi_pll_sel;
  10449. bool force_thru;
  10450. /* FIXME: before the switch to atomic started, a new pipe_config was
  10451. * kzalloc'd. Code that depends on any field being zero should be
  10452. * fixed, so that the crtc_state can be safely duplicated. For now,
  10453. * only fields that are know to not cause problems are preserved. */
  10454. tmp_state = crtc_state->base;
  10455. scaler_state = crtc_state->scaler_state;
  10456. shared_dpll = crtc_state->shared_dpll;
  10457. dpll_hw_state = crtc_state->dpll_hw_state;
  10458. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10459. force_thru = crtc_state->pch_pfit.force_thru;
  10460. memset(crtc_state, 0, sizeof *crtc_state);
  10461. crtc_state->base = tmp_state;
  10462. crtc_state->scaler_state = scaler_state;
  10463. crtc_state->shared_dpll = shared_dpll;
  10464. crtc_state->dpll_hw_state = dpll_hw_state;
  10465. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10466. crtc_state->pch_pfit.force_thru = force_thru;
  10467. }
  10468. static int
  10469. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10470. struct intel_crtc_state *pipe_config)
  10471. {
  10472. struct drm_atomic_state *state = pipe_config->base.state;
  10473. struct intel_encoder *encoder;
  10474. struct drm_connector *connector;
  10475. struct drm_connector_state *connector_state;
  10476. int base_bpp, ret = -EINVAL;
  10477. int i;
  10478. bool retry = true;
  10479. clear_intel_crtc_state(pipe_config);
  10480. pipe_config->cpu_transcoder =
  10481. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10482. /*
  10483. * Sanitize sync polarity flags based on requested ones. If neither
  10484. * positive or negative polarity is requested, treat this as meaning
  10485. * negative polarity.
  10486. */
  10487. if (!(pipe_config->base.adjusted_mode.flags &
  10488. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10489. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10490. if (!(pipe_config->base.adjusted_mode.flags &
  10491. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10492. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10493. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10494. pipe_config);
  10495. if (base_bpp < 0)
  10496. goto fail;
  10497. /*
  10498. * Determine the real pipe dimensions. Note that stereo modes can
  10499. * increase the actual pipe size due to the frame doubling and
  10500. * insertion of additional space for blanks between the frame. This
  10501. * is stored in the crtc timings. We use the requested mode to do this
  10502. * computation to clearly distinguish it from the adjusted mode, which
  10503. * can be changed by the connectors in the below retry loop.
  10504. */
  10505. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10506. &pipe_config->pipe_src_w,
  10507. &pipe_config->pipe_src_h);
  10508. encoder_retry:
  10509. /* Ensure the port clock defaults are reset when retrying. */
  10510. pipe_config->port_clock = 0;
  10511. pipe_config->pixel_multiplier = 1;
  10512. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10513. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10514. CRTC_STEREO_DOUBLE);
  10515. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10516. * adjust it according to limitations or connector properties, and also
  10517. * a chance to reject the mode entirely.
  10518. */
  10519. for_each_connector_in_state(state, connector, connector_state, i) {
  10520. if (connector_state->crtc != crtc)
  10521. continue;
  10522. encoder = to_intel_encoder(connector_state->best_encoder);
  10523. if (!(encoder->compute_config(encoder, pipe_config))) {
  10524. DRM_DEBUG_KMS("Encoder config failure\n");
  10525. goto fail;
  10526. }
  10527. }
  10528. /* Set default port clock if not overwritten by the encoder. Needs to be
  10529. * done afterwards in case the encoder adjusts the mode. */
  10530. if (!pipe_config->port_clock)
  10531. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10532. * pipe_config->pixel_multiplier;
  10533. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10534. if (ret < 0) {
  10535. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10536. goto fail;
  10537. }
  10538. if (ret == RETRY) {
  10539. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10540. ret = -EINVAL;
  10541. goto fail;
  10542. }
  10543. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10544. retry = false;
  10545. goto encoder_retry;
  10546. }
  10547. /* Dithering seems to not pass-through bits correctly when it should, so
  10548. * only enable it on 6bpc panels. */
  10549. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10550. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10551. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10552. fail:
  10553. return ret;
  10554. }
  10555. static void
  10556. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10557. {
  10558. struct drm_crtc *crtc;
  10559. struct drm_crtc_state *crtc_state;
  10560. int i;
  10561. /* Double check state. */
  10562. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10563. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10564. /* Update hwmode for vblank functions */
  10565. if (crtc->state->active)
  10566. crtc->hwmode = crtc->state->adjusted_mode;
  10567. else
  10568. crtc->hwmode.crtc_clock = 0;
  10569. /*
  10570. * Update legacy state to satisfy fbc code. This can
  10571. * be removed when fbc uses the atomic state.
  10572. */
  10573. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10574. struct drm_plane_state *plane_state = crtc->primary->state;
  10575. crtc->primary->fb = plane_state->fb;
  10576. crtc->x = plane_state->src_x >> 16;
  10577. crtc->y = plane_state->src_y >> 16;
  10578. }
  10579. }
  10580. }
  10581. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10582. {
  10583. int diff;
  10584. if (clock1 == clock2)
  10585. return true;
  10586. if (!clock1 || !clock2)
  10587. return false;
  10588. diff = abs(clock1 - clock2);
  10589. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10590. return true;
  10591. return false;
  10592. }
  10593. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10594. list_for_each_entry((intel_crtc), \
  10595. &(dev)->mode_config.crtc_list, \
  10596. base.head) \
  10597. for_each_if (mask & (1 <<(intel_crtc)->pipe))
  10598. static bool
  10599. intel_compare_m_n(unsigned int m, unsigned int n,
  10600. unsigned int m2, unsigned int n2,
  10601. bool exact)
  10602. {
  10603. if (m == m2 && n == n2)
  10604. return true;
  10605. if (exact || !m || !n || !m2 || !n2)
  10606. return false;
  10607. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10608. if (n > n2) {
  10609. while (n > n2) {
  10610. m2 <<= 1;
  10611. n2 <<= 1;
  10612. }
  10613. } else if (n < n2) {
  10614. while (n < n2) {
  10615. m <<= 1;
  10616. n <<= 1;
  10617. }
  10618. }
  10619. if (n != n2)
  10620. return false;
  10621. return intel_fuzzy_clock_check(m, m2);
  10622. }
  10623. static bool
  10624. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10625. struct intel_link_m_n *m2_n2,
  10626. bool adjust)
  10627. {
  10628. if (m_n->tu == m2_n2->tu &&
  10629. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10630. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10631. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10632. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10633. if (adjust)
  10634. *m2_n2 = *m_n;
  10635. return true;
  10636. }
  10637. return false;
  10638. }
  10639. static bool
  10640. intel_pipe_config_compare(struct drm_device *dev,
  10641. struct intel_crtc_state *current_config,
  10642. struct intel_crtc_state *pipe_config,
  10643. bool adjust)
  10644. {
  10645. bool ret = true;
  10646. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10647. do { \
  10648. if (!adjust) \
  10649. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10650. else \
  10651. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10652. } while (0)
  10653. #define PIPE_CONF_CHECK_X(name) \
  10654. if (current_config->name != pipe_config->name) { \
  10655. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10656. "(expected 0x%08x, found 0x%08x)\n", \
  10657. current_config->name, \
  10658. pipe_config->name); \
  10659. ret = false; \
  10660. }
  10661. #define PIPE_CONF_CHECK_I(name) \
  10662. if (current_config->name != pipe_config->name) { \
  10663. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10664. "(expected %i, found %i)\n", \
  10665. current_config->name, \
  10666. pipe_config->name); \
  10667. ret = false; \
  10668. }
  10669. #define PIPE_CONF_CHECK_P(name) \
  10670. if (current_config->name != pipe_config->name) { \
  10671. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10672. "(expected %p, found %p)\n", \
  10673. current_config->name, \
  10674. pipe_config->name); \
  10675. ret = false; \
  10676. }
  10677. #define PIPE_CONF_CHECK_M_N(name) \
  10678. if (!intel_compare_link_m_n(&current_config->name, \
  10679. &pipe_config->name,\
  10680. adjust)) { \
  10681. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10682. "(expected tu %i gmch %i/%i link %i/%i, " \
  10683. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10684. current_config->name.tu, \
  10685. current_config->name.gmch_m, \
  10686. current_config->name.gmch_n, \
  10687. current_config->name.link_m, \
  10688. current_config->name.link_n, \
  10689. pipe_config->name.tu, \
  10690. pipe_config->name.gmch_m, \
  10691. pipe_config->name.gmch_n, \
  10692. pipe_config->name.link_m, \
  10693. pipe_config->name.link_n); \
  10694. ret = false; \
  10695. }
  10696. /* This is required for BDW+ where there is only one set of registers for
  10697. * switching between high and low RR.
  10698. * This macro can be used whenever a comparison has to be made between one
  10699. * hw state and multiple sw state variables.
  10700. */
  10701. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10702. if (!intel_compare_link_m_n(&current_config->name, \
  10703. &pipe_config->name, adjust) && \
  10704. !intel_compare_link_m_n(&current_config->alt_name, \
  10705. &pipe_config->name, adjust)) { \
  10706. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10707. "(expected tu %i gmch %i/%i link %i/%i, " \
  10708. "or tu %i gmch %i/%i link %i/%i, " \
  10709. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10710. current_config->name.tu, \
  10711. current_config->name.gmch_m, \
  10712. current_config->name.gmch_n, \
  10713. current_config->name.link_m, \
  10714. current_config->name.link_n, \
  10715. current_config->alt_name.tu, \
  10716. current_config->alt_name.gmch_m, \
  10717. current_config->alt_name.gmch_n, \
  10718. current_config->alt_name.link_m, \
  10719. current_config->alt_name.link_n, \
  10720. pipe_config->name.tu, \
  10721. pipe_config->name.gmch_m, \
  10722. pipe_config->name.gmch_n, \
  10723. pipe_config->name.link_m, \
  10724. pipe_config->name.link_n); \
  10725. ret = false; \
  10726. }
  10727. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10728. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10729. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10730. "(expected %i, found %i)\n", \
  10731. current_config->name & (mask), \
  10732. pipe_config->name & (mask)); \
  10733. ret = false; \
  10734. }
  10735. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10736. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10737. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10738. "(expected %i, found %i)\n", \
  10739. current_config->name, \
  10740. pipe_config->name); \
  10741. ret = false; \
  10742. }
  10743. #define PIPE_CONF_QUIRK(quirk) \
  10744. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10745. PIPE_CONF_CHECK_I(cpu_transcoder);
  10746. PIPE_CONF_CHECK_I(has_pch_encoder);
  10747. PIPE_CONF_CHECK_I(fdi_lanes);
  10748. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10749. PIPE_CONF_CHECK_I(has_dp_encoder);
  10750. PIPE_CONF_CHECK_I(lane_count);
  10751. if (INTEL_INFO(dev)->gen < 8) {
  10752. PIPE_CONF_CHECK_M_N(dp_m_n);
  10753. if (current_config->has_drrs)
  10754. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10755. } else
  10756. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10757. PIPE_CONF_CHECK_I(has_dsi_encoder);
  10758. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10759. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10760. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10761. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10762. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10763. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10764. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10765. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10766. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10767. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10768. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10769. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10770. PIPE_CONF_CHECK_I(pixel_multiplier);
  10771. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10772. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10773. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  10774. PIPE_CONF_CHECK_I(limited_color_range);
  10775. PIPE_CONF_CHECK_I(has_infoframe);
  10776. PIPE_CONF_CHECK_I(has_audio);
  10777. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10778. DRM_MODE_FLAG_INTERLACE);
  10779. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10780. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10781. DRM_MODE_FLAG_PHSYNC);
  10782. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10783. DRM_MODE_FLAG_NHSYNC);
  10784. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10785. DRM_MODE_FLAG_PVSYNC);
  10786. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10787. DRM_MODE_FLAG_NVSYNC);
  10788. }
  10789. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10790. /* pfit ratios are autocomputed by the hw on gen4+ */
  10791. if (INTEL_INFO(dev)->gen < 4)
  10792. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  10793. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10794. if (!adjust) {
  10795. PIPE_CONF_CHECK_I(pipe_src_w);
  10796. PIPE_CONF_CHECK_I(pipe_src_h);
  10797. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10798. if (current_config->pch_pfit.enabled) {
  10799. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10800. PIPE_CONF_CHECK_X(pch_pfit.size);
  10801. }
  10802. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10803. }
  10804. /* BDW+ don't expose a synchronous way to read the state */
  10805. if (IS_HASWELL(dev))
  10806. PIPE_CONF_CHECK_I(ips_enabled);
  10807. PIPE_CONF_CHECK_I(double_wide);
  10808. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10809. PIPE_CONF_CHECK_P(shared_dpll);
  10810. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10811. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10812. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10813. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10814. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10815. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10816. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10817. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10818. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10819. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  10820. PIPE_CONF_CHECK_X(dsi_pll.div);
  10821. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10822. PIPE_CONF_CHECK_I(pipe_bpp);
  10823. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10824. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10825. #undef PIPE_CONF_CHECK_X
  10826. #undef PIPE_CONF_CHECK_I
  10827. #undef PIPE_CONF_CHECK_P
  10828. #undef PIPE_CONF_CHECK_FLAGS
  10829. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10830. #undef PIPE_CONF_QUIRK
  10831. #undef INTEL_ERR_OR_DBG_KMS
  10832. return ret;
  10833. }
  10834. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  10835. const struct intel_crtc_state *pipe_config)
  10836. {
  10837. if (pipe_config->has_pch_encoder) {
  10838. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  10839. &pipe_config->fdi_m_n);
  10840. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  10841. /*
  10842. * FDI already provided one idea for the dotclock.
  10843. * Yell if the encoder disagrees.
  10844. */
  10845. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  10846. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10847. fdi_dotclock, dotclock);
  10848. }
  10849. }
  10850. static void verify_wm_state(struct drm_crtc *crtc,
  10851. struct drm_crtc_state *new_state)
  10852. {
  10853. struct drm_device *dev = crtc->dev;
  10854. struct drm_i915_private *dev_priv = dev->dev_private;
  10855. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10856. struct skl_ddb_entry *hw_entry, *sw_entry;
  10857. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10858. const enum pipe pipe = intel_crtc->pipe;
  10859. int plane;
  10860. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  10861. return;
  10862. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10863. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10864. /* planes */
  10865. for_each_plane(dev_priv, pipe, plane) {
  10866. hw_entry = &hw_ddb.plane[pipe][plane];
  10867. sw_entry = &sw_ddb->plane[pipe][plane];
  10868. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10869. continue;
  10870. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10871. "(expected (%u,%u), found (%u,%u))\n",
  10872. pipe_name(pipe), plane + 1,
  10873. sw_entry->start, sw_entry->end,
  10874. hw_entry->start, hw_entry->end);
  10875. }
  10876. /* cursor */
  10877. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10878. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10879. if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
  10880. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10881. "(expected (%u,%u), found (%u,%u))\n",
  10882. pipe_name(pipe),
  10883. sw_entry->start, sw_entry->end,
  10884. hw_entry->start, hw_entry->end);
  10885. }
  10886. }
  10887. static void
  10888. verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
  10889. {
  10890. struct drm_connector *connector;
  10891. drm_for_each_connector(connector, dev) {
  10892. struct drm_encoder *encoder = connector->encoder;
  10893. struct drm_connector_state *state = connector->state;
  10894. if (state->crtc != crtc)
  10895. continue;
  10896. intel_connector_verify_state(to_intel_connector(connector));
  10897. I915_STATE_WARN(state->best_encoder != encoder,
  10898. "connector's atomic encoder doesn't match legacy encoder\n");
  10899. }
  10900. }
  10901. static void
  10902. verify_encoder_state(struct drm_device *dev)
  10903. {
  10904. struct intel_encoder *encoder;
  10905. struct intel_connector *connector;
  10906. for_each_intel_encoder(dev, encoder) {
  10907. bool enabled = false;
  10908. enum pipe pipe;
  10909. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10910. encoder->base.base.id,
  10911. encoder->base.name);
  10912. for_each_intel_connector(dev, connector) {
  10913. if (connector->base.state->best_encoder != &encoder->base)
  10914. continue;
  10915. enabled = true;
  10916. I915_STATE_WARN(connector->base.state->crtc !=
  10917. encoder->base.crtc,
  10918. "connector's crtc doesn't match encoder crtc\n");
  10919. }
  10920. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10921. "encoder's enabled state mismatch "
  10922. "(expected %i, found %i)\n",
  10923. !!encoder->base.crtc, enabled);
  10924. if (!encoder->base.crtc) {
  10925. bool active;
  10926. active = encoder->get_hw_state(encoder, &pipe);
  10927. I915_STATE_WARN(active,
  10928. "encoder detached but still enabled on pipe %c.\n",
  10929. pipe_name(pipe));
  10930. }
  10931. }
  10932. }
  10933. static void
  10934. verify_crtc_state(struct drm_crtc *crtc,
  10935. struct drm_crtc_state *old_crtc_state,
  10936. struct drm_crtc_state *new_crtc_state)
  10937. {
  10938. struct drm_device *dev = crtc->dev;
  10939. struct drm_i915_private *dev_priv = dev->dev_private;
  10940. struct intel_encoder *encoder;
  10941. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10942. struct intel_crtc_state *pipe_config, *sw_config;
  10943. struct drm_atomic_state *old_state;
  10944. bool active;
  10945. old_state = old_crtc_state->state;
  10946. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10947. pipe_config = to_intel_crtc_state(old_crtc_state);
  10948. memset(pipe_config, 0, sizeof(*pipe_config));
  10949. pipe_config->base.crtc = crtc;
  10950. pipe_config->base.state = old_state;
  10951. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  10952. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10953. /* hw state is inconsistent with the pipe quirk */
  10954. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10955. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10956. active = new_crtc_state->active;
  10957. I915_STATE_WARN(new_crtc_state->active != active,
  10958. "crtc active state doesn't match with hw state "
  10959. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10960. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10961. "transitional active state does not match atomic hw state "
  10962. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10963. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10964. enum pipe pipe;
  10965. active = encoder->get_hw_state(encoder, &pipe);
  10966. I915_STATE_WARN(active != new_crtc_state->active,
  10967. "[ENCODER:%i] active %i with crtc active %i\n",
  10968. encoder->base.base.id, active, new_crtc_state->active);
  10969. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10970. "Encoder connected to wrong pipe %c\n",
  10971. pipe_name(pipe));
  10972. if (active)
  10973. encoder->get_config(encoder, pipe_config);
  10974. }
  10975. if (!new_crtc_state->active)
  10976. return;
  10977. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10978. sw_config = to_intel_crtc_state(crtc->state);
  10979. if (!intel_pipe_config_compare(dev, sw_config,
  10980. pipe_config, false)) {
  10981. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10982. intel_dump_pipe_config(intel_crtc, pipe_config,
  10983. "[hw state]");
  10984. intel_dump_pipe_config(intel_crtc, sw_config,
  10985. "[sw state]");
  10986. }
  10987. }
  10988. static void
  10989. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10990. struct intel_shared_dpll *pll,
  10991. struct drm_crtc *crtc,
  10992. struct drm_crtc_state *new_state)
  10993. {
  10994. struct intel_dpll_hw_state dpll_hw_state;
  10995. unsigned crtc_mask;
  10996. bool active;
  10997. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10998. DRM_DEBUG_KMS("%s\n", pll->name);
  10999. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  11000. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  11001. I915_STATE_WARN(!pll->on && pll->active_mask,
  11002. "pll in active use but not on in sw tracking\n");
  11003. I915_STATE_WARN(pll->on && !pll->active_mask,
  11004. "pll is on but not used by any active crtc\n");
  11005. I915_STATE_WARN(pll->on != active,
  11006. "pll on state mismatch (expected %i, found %i)\n",
  11007. pll->on, active);
  11008. }
  11009. if (!crtc) {
  11010. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  11011. "more active pll users than references: %x vs %x\n",
  11012. pll->active_mask, pll->config.crtc_mask);
  11013. return;
  11014. }
  11015. crtc_mask = 1 << drm_crtc_index(crtc);
  11016. if (new_state->active)
  11017. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  11018. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  11019. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11020. else
  11021. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11022. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  11023. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11024. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  11025. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  11026. crtc_mask, pll->config.crtc_mask);
  11027. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  11028. &dpll_hw_state,
  11029. sizeof(dpll_hw_state)),
  11030. "pll hw state mismatch\n");
  11031. }
  11032. static void
  11033. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  11034. struct drm_crtc_state *old_crtc_state,
  11035. struct drm_crtc_state *new_crtc_state)
  11036. {
  11037. struct drm_i915_private *dev_priv = dev->dev_private;
  11038. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  11039. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  11040. if (new_state->shared_dpll)
  11041. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  11042. if (old_state->shared_dpll &&
  11043. old_state->shared_dpll != new_state->shared_dpll) {
  11044. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  11045. struct intel_shared_dpll *pll = old_state->shared_dpll;
  11046. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11047. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  11048. pipe_name(drm_crtc_index(crtc)));
  11049. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  11050. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  11051. pipe_name(drm_crtc_index(crtc)));
  11052. }
  11053. }
  11054. static void
  11055. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11056. struct drm_crtc_state *old_state,
  11057. struct drm_crtc_state *new_state)
  11058. {
  11059. if (!needs_modeset(new_state) &&
  11060. !to_intel_crtc_state(new_state)->update_pipe)
  11061. return;
  11062. verify_wm_state(crtc, new_state);
  11063. verify_connector_state(crtc->dev, crtc);
  11064. verify_crtc_state(crtc, old_state, new_state);
  11065. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11066. }
  11067. static void
  11068. verify_disabled_dpll_state(struct drm_device *dev)
  11069. {
  11070. struct drm_i915_private *dev_priv = dev->dev_private;
  11071. int i;
  11072. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11073. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11074. }
  11075. static void
  11076. intel_modeset_verify_disabled(struct drm_device *dev)
  11077. {
  11078. verify_encoder_state(dev);
  11079. verify_connector_state(dev, NULL);
  11080. verify_disabled_dpll_state(dev);
  11081. }
  11082. static void update_scanline_offset(struct intel_crtc *crtc)
  11083. {
  11084. struct drm_device *dev = crtc->base.dev;
  11085. /*
  11086. * The scanline counter increments at the leading edge of hsync.
  11087. *
  11088. * On most platforms it starts counting from vtotal-1 on the
  11089. * first active line. That means the scanline counter value is
  11090. * always one less than what we would expect. Ie. just after
  11091. * start of vblank, which also occurs at start of hsync (on the
  11092. * last active line), the scanline counter will read vblank_start-1.
  11093. *
  11094. * On gen2 the scanline counter starts counting from 1 instead
  11095. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11096. * to keep the value positive), instead of adding one.
  11097. *
  11098. * On HSW+ the behaviour of the scanline counter depends on the output
  11099. * type. For DP ports it behaves like most other platforms, but on HDMI
  11100. * there's an extra 1 line difference. So we need to add two instead of
  11101. * one to the value.
  11102. */
  11103. if (IS_GEN2(dev)) {
  11104. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11105. int vtotal;
  11106. vtotal = adjusted_mode->crtc_vtotal;
  11107. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11108. vtotal /= 2;
  11109. crtc->scanline_offset = vtotal - 1;
  11110. } else if (HAS_DDI(dev) &&
  11111. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  11112. crtc->scanline_offset = 2;
  11113. } else
  11114. crtc->scanline_offset = 1;
  11115. }
  11116. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11117. {
  11118. struct drm_device *dev = state->dev;
  11119. struct drm_i915_private *dev_priv = to_i915(dev);
  11120. struct intel_shared_dpll_config *shared_dpll = NULL;
  11121. struct drm_crtc *crtc;
  11122. struct drm_crtc_state *crtc_state;
  11123. int i;
  11124. if (!dev_priv->display.crtc_compute_clock)
  11125. return;
  11126. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11127. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11128. struct intel_shared_dpll *old_dpll =
  11129. to_intel_crtc_state(crtc->state)->shared_dpll;
  11130. if (!needs_modeset(crtc_state))
  11131. continue;
  11132. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11133. if (!old_dpll)
  11134. continue;
  11135. if (!shared_dpll)
  11136. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11137. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11138. }
  11139. }
  11140. /*
  11141. * This implements the workaround described in the "notes" section of the mode
  11142. * set sequence documentation. When going from no pipes or single pipe to
  11143. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11144. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11145. */
  11146. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11147. {
  11148. struct drm_crtc_state *crtc_state;
  11149. struct intel_crtc *intel_crtc;
  11150. struct drm_crtc *crtc;
  11151. struct intel_crtc_state *first_crtc_state = NULL;
  11152. struct intel_crtc_state *other_crtc_state = NULL;
  11153. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11154. int i;
  11155. /* look at all crtc's that are going to be enabled in during modeset */
  11156. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11157. intel_crtc = to_intel_crtc(crtc);
  11158. if (!crtc_state->active || !needs_modeset(crtc_state))
  11159. continue;
  11160. if (first_crtc_state) {
  11161. other_crtc_state = to_intel_crtc_state(crtc_state);
  11162. break;
  11163. } else {
  11164. first_crtc_state = to_intel_crtc_state(crtc_state);
  11165. first_pipe = intel_crtc->pipe;
  11166. }
  11167. }
  11168. /* No workaround needed? */
  11169. if (!first_crtc_state)
  11170. return 0;
  11171. /* w/a possibly needed, check how many crtc's are already enabled. */
  11172. for_each_intel_crtc(state->dev, intel_crtc) {
  11173. struct intel_crtc_state *pipe_config;
  11174. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11175. if (IS_ERR(pipe_config))
  11176. return PTR_ERR(pipe_config);
  11177. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11178. if (!pipe_config->base.active ||
  11179. needs_modeset(&pipe_config->base))
  11180. continue;
  11181. /* 2 or more enabled crtcs means no need for w/a */
  11182. if (enabled_pipe != INVALID_PIPE)
  11183. return 0;
  11184. enabled_pipe = intel_crtc->pipe;
  11185. }
  11186. if (enabled_pipe != INVALID_PIPE)
  11187. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11188. else if (other_crtc_state)
  11189. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11190. return 0;
  11191. }
  11192. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11193. {
  11194. struct drm_crtc *crtc;
  11195. struct drm_crtc_state *crtc_state;
  11196. int ret = 0;
  11197. /* add all active pipes to the state */
  11198. for_each_crtc(state->dev, crtc) {
  11199. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11200. if (IS_ERR(crtc_state))
  11201. return PTR_ERR(crtc_state);
  11202. if (!crtc_state->active || needs_modeset(crtc_state))
  11203. continue;
  11204. crtc_state->mode_changed = true;
  11205. ret = drm_atomic_add_affected_connectors(state, crtc);
  11206. if (ret)
  11207. break;
  11208. ret = drm_atomic_add_affected_planes(state, crtc);
  11209. if (ret)
  11210. break;
  11211. }
  11212. return ret;
  11213. }
  11214. static int intel_modeset_checks(struct drm_atomic_state *state)
  11215. {
  11216. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11217. struct drm_i915_private *dev_priv = state->dev->dev_private;
  11218. struct drm_crtc *crtc;
  11219. struct drm_crtc_state *crtc_state;
  11220. int ret = 0, i;
  11221. if (!check_digital_port_conflicts(state)) {
  11222. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11223. return -EINVAL;
  11224. }
  11225. intel_state->modeset = true;
  11226. intel_state->active_crtcs = dev_priv->active_crtcs;
  11227. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11228. if (crtc_state->active)
  11229. intel_state->active_crtcs |= 1 << i;
  11230. else
  11231. intel_state->active_crtcs &= ~(1 << i);
  11232. if (crtc_state->active != crtc->state->active)
  11233. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11234. }
  11235. /*
  11236. * See if the config requires any additional preparation, e.g.
  11237. * to adjust global state with pipes off. We need to do this
  11238. * here so we can get the modeset_pipe updated config for the new
  11239. * mode set on this crtc. For other crtcs we need to use the
  11240. * adjusted_mode bits in the crtc directly.
  11241. */
  11242. if (dev_priv->display.modeset_calc_cdclk) {
  11243. if (!intel_state->cdclk_pll_vco)
  11244. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11245. if (!intel_state->cdclk_pll_vco)
  11246. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11247. ret = dev_priv->display.modeset_calc_cdclk(state);
  11248. if (ret < 0)
  11249. return ret;
  11250. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11251. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
  11252. ret = intel_modeset_all_pipes(state);
  11253. if (ret < 0)
  11254. return ret;
  11255. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11256. intel_state->cdclk, intel_state->dev_cdclk);
  11257. } else
  11258. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11259. intel_modeset_clear_plls(state);
  11260. if (IS_HASWELL(dev_priv))
  11261. return haswell_mode_set_planes_workaround(state);
  11262. return 0;
  11263. }
  11264. /*
  11265. * Handle calculation of various watermark data at the end of the atomic check
  11266. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11267. * handlers to ensure that all derived state has been updated.
  11268. */
  11269. static int calc_watermark_data(struct drm_atomic_state *state)
  11270. {
  11271. struct drm_device *dev = state->dev;
  11272. struct drm_i915_private *dev_priv = to_i915(dev);
  11273. /* Is there platform-specific watermark information to calculate? */
  11274. if (dev_priv->display.compute_global_watermarks)
  11275. return dev_priv->display.compute_global_watermarks(state);
  11276. return 0;
  11277. }
  11278. /**
  11279. * intel_atomic_check - validate state object
  11280. * @dev: drm device
  11281. * @state: state to validate
  11282. */
  11283. static int intel_atomic_check(struct drm_device *dev,
  11284. struct drm_atomic_state *state)
  11285. {
  11286. struct drm_i915_private *dev_priv = to_i915(dev);
  11287. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11288. struct drm_crtc *crtc;
  11289. struct drm_crtc_state *crtc_state;
  11290. int ret, i;
  11291. bool any_ms = false;
  11292. ret = drm_atomic_helper_check_modeset(dev, state);
  11293. if (ret)
  11294. return ret;
  11295. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11296. struct intel_crtc_state *pipe_config =
  11297. to_intel_crtc_state(crtc_state);
  11298. /* Catch I915_MODE_FLAG_INHERITED */
  11299. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11300. crtc_state->mode_changed = true;
  11301. if (!needs_modeset(crtc_state))
  11302. continue;
  11303. if (!crtc_state->enable) {
  11304. any_ms = true;
  11305. continue;
  11306. }
  11307. /* FIXME: For only active_changed we shouldn't need to do any
  11308. * state recomputation at all. */
  11309. ret = drm_atomic_add_affected_connectors(state, crtc);
  11310. if (ret)
  11311. return ret;
  11312. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11313. if (ret) {
  11314. intel_dump_pipe_config(to_intel_crtc(crtc),
  11315. pipe_config, "[failed]");
  11316. return ret;
  11317. }
  11318. if (i915.fastboot &&
  11319. intel_pipe_config_compare(dev,
  11320. to_intel_crtc_state(crtc->state),
  11321. pipe_config, true)) {
  11322. crtc_state->mode_changed = false;
  11323. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11324. }
  11325. if (needs_modeset(crtc_state))
  11326. any_ms = true;
  11327. ret = drm_atomic_add_affected_planes(state, crtc);
  11328. if (ret)
  11329. return ret;
  11330. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11331. needs_modeset(crtc_state) ?
  11332. "[modeset]" : "[fastset]");
  11333. }
  11334. if (any_ms) {
  11335. ret = intel_modeset_checks(state);
  11336. if (ret)
  11337. return ret;
  11338. } else
  11339. intel_state->cdclk = dev_priv->cdclk_freq;
  11340. ret = drm_atomic_helper_check_planes(dev, state);
  11341. if (ret)
  11342. return ret;
  11343. intel_fbc_choose_crtc(dev_priv, state);
  11344. return calc_watermark_data(state);
  11345. }
  11346. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11347. struct drm_atomic_state *state,
  11348. bool nonblock)
  11349. {
  11350. struct drm_i915_private *dev_priv = dev->dev_private;
  11351. struct drm_plane_state *plane_state;
  11352. struct drm_crtc_state *crtc_state;
  11353. struct drm_plane *plane;
  11354. struct drm_crtc *crtc;
  11355. int i, ret;
  11356. if (nonblock) {
  11357. DRM_DEBUG_KMS("i915 does not yet support nonblocking commit\n");
  11358. return -EINVAL;
  11359. }
  11360. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11361. if (state->legacy_cursor_update)
  11362. continue;
  11363. ret = intel_crtc_wait_for_pending_flips(crtc);
  11364. if (ret)
  11365. return ret;
  11366. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11367. flush_workqueue(dev_priv->wq);
  11368. }
  11369. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11370. if (ret)
  11371. return ret;
  11372. ret = drm_atomic_helper_prepare_planes(dev, state);
  11373. mutex_unlock(&dev->struct_mutex);
  11374. if (!ret && !nonblock) {
  11375. for_each_plane_in_state(state, plane, plane_state, i) {
  11376. struct intel_plane_state *intel_plane_state =
  11377. to_intel_plane_state(plane_state);
  11378. if (!intel_plane_state->wait_req)
  11379. continue;
  11380. ret = __i915_wait_request(intel_plane_state->wait_req,
  11381. true, NULL, NULL);
  11382. if (ret) {
  11383. /* Any hang should be swallowed by the wait */
  11384. WARN_ON(ret == -EIO);
  11385. mutex_lock(&dev->struct_mutex);
  11386. drm_atomic_helper_cleanup_planes(dev, state);
  11387. mutex_unlock(&dev->struct_mutex);
  11388. break;
  11389. }
  11390. }
  11391. }
  11392. return ret;
  11393. }
  11394. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11395. {
  11396. struct drm_device *dev = crtc->base.dev;
  11397. if (!dev->max_vblank_count)
  11398. return drm_accurate_vblank_count(&crtc->base);
  11399. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11400. }
  11401. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11402. struct drm_i915_private *dev_priv,
  11403. unsigned crtc_mask)
  11404. {
  11405. unsigned last_vblank_count[I915_MAX_PIPES];
  11406. enum pipe pipe;
  11407. int ret;
  11408. if (!crtc_mask)
  11409. return;
  11410. for_each_pipe(dev_priv, pipe) {
  11411. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11412. if (!((1 << pipe) & crtc_mask))
  11413. continue;
  11414. ret = drm_crtc_vblank_get(crtc);
  11415. if (WARN_ON(ret != 0)) {
  11416. crtc_mask &= ~(1 << pipe);
  11417. continue;
  11418. }
  11419. last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
  11420. }
  11421. for_each_pipe(dev_priv, pipe) {
  11422. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11423. long lret;
  11424. if (!((1 << pipe) & crtc_mask))
  11425. continue;
  11426. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11427. last_vblank_count[pipe] !=
  11428. drm_crtc_vblank_count(crtc),
  11429. msecs_to_jiffies(50));
  11430. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11431. drm_crtc_vblank_put(crtc);
  11432. }
  11433. }
  11434. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11435. {
  11436. /* fb updated, need to unpin old fb */
  11437. if (crtc_state->fb_changed)
  11438. return true;
  11439. /* wm changes, need vblank before final wm's */
  11440. if (crtc_state->update_wm_post)
  11441. return true;
  11442. /*
  11443. * cxsr is re-enabled after vblank.
  11444. * This is already handled by crtc_state->update_wm_post,
  11445. * but added for clarity.
  11446. */
  11447. if (crtc_state->disable_cxsr)
  11448. return true;
  11449. return false;
  11450. }
  11451. /**
  11452. * intel_atomic_commit - commit validated state object
  11453. * @dev: DRM device
  11454. * @state: the top-level driver state object
  11455. * @nonblock: nonblocking commit
  11456. *
  11457. * This function commits a top-level state object that has been validated
  11458. * with drm_atomic_helper_check().
  11459. *
  11460. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11461. * we can only handle plane-related operations and do not yet support
  11462. * nonblocking commit.
  11463. *
  11464. * RETURNS
  11465. * Zero for success or -errno.
  11466. */
  11467. static int intel_atomic_commit(struct drm_device *dev,
  11468. struct drm_atomic_state *state,
  11469. bool nonblock)
  11470. {
  11471. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11472. struct drm_i915_private *dev_priv = dev->dev_private;
  11473. struct drm_crtc_state *old_crtc_state;
  11474. struct drm_crtc *crtc;
  11475. struct intel_crtc_state *intel_cstate;
  11476. int ret = 0, i;
  11477. bool hw_check = intel_state->modeset;
  11478. unsigned long put_domains[I915_MAX_PIPES] = {};
  11479. unsigned crtc_vblank_mask = 0;
  11480. ret = intel_atomic_prepare_commit(dev, state, nonblock);
  11481. if (ret) {
  11482. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11483. return ret;
  11484. }
  11485. drm_atomic_helper_swap_state(state, true);
  11486. dev_priv->wm.distrust_bios_wm = false;
  11487. dev_priv->wm.skl_results = intel_state->wm_results;
  11488. intel_shared_dpll_commit(state);
  11489. if (intel_state->modeset) {
  11490. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  11491. sizeof(intel_state->min_pixclk));
  11492. dev_priv->active_crtcs = intel_state->active_crtcs;
  11493. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  11494. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  11495. }
  11496. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11497. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11498. if (needs_modeset(crtc->state) ||
  11499. to_intel_crtc_state(crtc->state)->update_pipe) {
  11500. hw_check = true;
  11501. put_domains[to_intel_crtc(crtc)->pipe] =
  11502. modeset_get_crtc_power_domains(crtc,
  11503. to_intel_crtc_state(crtc->state));
  11504. }
  11505. if (!needs_modeset(crtc->state))
  11506. continue;
  11507. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11508. if (old_crtc_state->active) {
  11509. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  11510. dev_priv->display.crtc_disable(crtc);
  11511. intel_crtc->active = false;
  11512. intel_fbc_disable(intel_crtc);
  11513. intel_disable_shared_dpll(intel_crtc);
  11514. /*
  11515. * Underruns don't always raise
  11516. * interrupts, so check manually.
  11517. */
  11518. intel_check_cpu_fifo_underruns(dev_priv);
  11519. intel_check_pch_fifo_underruns(dev_priv);
  11520. if (!crtc->state->active)
  11521. intel_update_watermarks(crtc);
  11522. }
  11523. }
  11524. /* Only after disabling all output pipelines that will be changed can we
  11525. * update the the output configuration. */
  11526. intel_modeset_update_crtc_state(state);
  11527. if (intel_state->modeset) {
  11528. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11529. if (dev_priv->display.modeset_commit_cdclk &&
  11530. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11531. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  11532. dev_priv->display.modeset_commit_cdclk(state);
  11533. intel_modeset_verify_disabled(dev);
  11534. }
  11535. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11536. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11537. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11538. bool modeset = needs_modeset(crtc->state);
  11539. struct intel_crtc_state *pipe_config =
  11540. to_intel_crtc_state(crtc->state);
  11541. bool update_pipe = !modeset && pipe_config->update_pipe;
  11542. if (modeset && crtc->state->active) {
  11543. update_scanline_offset(to_intel_crtc(crtc));
  11544. dev_priv->display.crtc_enable(crtc);
  11545. }
  11546. if (!modeset)
  11547. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11548. if (crtc->state->active &&
  11549. drm_atomic_get_existing_plane_state(state, crtc->primary))
  11550. intel_fbc_enable(intel_crtc);
  11551. if (crtc->state->active &&
  11552. (crtc->state->planes_changed || update_pipe))
  11553. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11554. if (pipe_config->base.active && needs_vblank_wait(pipe_config))
  11555. crtc_vblank_mask |= 1 << i;
  11556. }
  11557. /* FIXME: add subpixel order */
  11558. if (!state->legacy_cursor_update)
  11559. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  11560. /*
  11561. * Now that the vblank has passed, we can go ahead and program the
  11562. * optimal watermarks on platforms that need two-step watermark
  11563. * programming.
  11564. *
  11565. * TODO: Move this (and other cleanup) to an async worker eventually.
  11566. */
  11567. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11568. intel_cstate = to_intel_crtc_state(crtc->state);
  11569. if (dev_priv->display.optimize_watermarks)
  11570. dev_priv->display.optimize_watermarks(intel_cstate);
  11571. }
  11572. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11573. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  11574. if (put_domains[i])
  11575. modeset_put_power_domains(dev_priv, put_domains[i]);
  11576. intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
  11577. }
  11578. if (intel_state->modeset)
  11579. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  11580. mutex_lock(&dev->struct_mutex);
  11581. drm_atomic_helper_cleanup_planes(dev, state);
  11582. mutex_unlock(&dev->struct_mutex);
  11583. drm_atomic_state_free(state);
  11584. /* As one of the primary mmio accessors, KMS has a high likelihood
  11585. * of triggering bugs in unclaimed access. After we finish
  11586. * modesetting, see if an error has been flagged, and if so
  11587. * enable debugging for the next modeset - and hope we catch
  11588. * the culprit.
  11589. *
  11590. * XXX note that we assume display power is on at this point.
  11591. * This might hold true now but we need to add pm helper to check
  11592. * unclaimed only when the hardware is on, as atomic commits
  11593. * can happen also when the device is completely off.
  11594. */
  11595. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  11596. return 0;
  11597. }
  11598. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11599. {
  11600. struct drm_device *dev = crtc->dev;
  11601. struct drm_atomic_state *state;
  11602. struct drm_crtc_state *crtc_state;
  11603. int ret;
  11604. state = drm_atomic_state_alloc(dev);
  11605. if (!state) {
  11606. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  11607. crtc->base.id, crtc->name);
  11608. return;
  11609. }
  11610. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11611. retry:
  11612. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11613. ret = PTR_ERR_OR_ZERO(crtc_state);
  11614. if (!ret) {
  11615. if (!crtc_state->active)
  11616. goto out;
  11617. crtc_state->mode_changed = true;
  11618. ret = drm_atomic_commit(state);
  11619. }
  11620. if (ret == -EDEADLK) {
  11621. drm_atomic_state_clear(state);
  11622. drm_modeset_backoff(state->acquire_ctx);
  11623. goto retry;
  11624. }
  11625. if (ret)
  11626. out:
  11627. drm_atomic_state_free(state);
  11628. }
  11629. #undef for_each_intel_crtc_masked
  11630. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11631. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  11632. .set_config = drm_atomic_helper_set_config,
  11633. .set_property = drm_atomic_helper_crtc_set_property,
  11634. .destroy = intel_crtc_destroy,
  11635. .page_flip = intel_crtc_page_flip,
  11636. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11637. .atomic_destroy_state = intel_crtc_destroy_state,
  11638. };
  11639. /**
  11640. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11641. * @plane: drm plane to prepare for
  11642. * @fb: framebuffer to prepare for presentation
  11643. *
  11644. * Prepares a framebuffer for usage on a display plane. Generally this
  11645. * involves pinning the underlying object and updating the frontbuffer tracking
  11646. * bits. Some older platforms need special physical address handling for
  11647. * cursor planes.
  11648. *
  11649. * Must be called with struct_mutex held.
  11650. *
  11651. * Returns 0 on success, negative error code on failure.
  11652. */
  11653. int
  11654. intel_prepare_plane_fb(struct drm_plane *plane,
  11655. const struct drm_plane_state *new_state)
  11656. {
  11657. struct drm_device *dev = plane->dev;
  11658. struct drm_framebuffer *fb = new_state->fb;
  11659. struct intel_plane *intel_plane = to_intel_plane(plane);
  11660. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11661. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11662. int ret = 0;
  11663. if (!obj && !old_obj)
  11664. return 0;
  11665. if (old_obj) {
  11666. struct drm_crtc_state *crtc_state =
  11667. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11668. /* Big Hammer, we also need to ensure that any pending
  11669. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11670. * current scanout is retired before unpinning the old
  11671. * framebuffer. Note that we rely on userspace rendering
  11672. * into the buffer attached to the pipe they are waiting
  11673. * on. If not, userspace generates a GPU hang with IPEHR
  11674. * point to the MI_WAIT_FOR_EVENT.
  11675. *
  11676. * This should only fail upon a hung GPU, in which case we
  11677. * can safely continue.
  11678. */
  11679. if (needs_modeset(crtc_state))
  11680. ret = i915_gem_object_wait_rendering(old_obj, true);
  11681. if (ret) {
  11682. /* GPU hangs should have been swallowed by the wait */
  11683. WARN_ON(ret == -EIO);
  11684. return ret;
  11685. }
  11686. }
  11687. /* For framebuffer backed by dmabuf, wait for fence */
  11688. if (obj && obj->base.dma_buf) {
  11689. long lret;
  11690. lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  11691. false, true,
  11692. MAX_SCHEDULE_TIMEOUT);
  11693. if (lret == -ERESTARTSYS)
  11694. return lret;
  11695. WARN(lret < 0, "waiting returns %li\n", lret);
  11696. }
  11697. if (!obj) {
  11698. ret = 0;
  11699. } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11700. INTEL_INFO(dev)->cursor_needs_physical) {
  11701. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11702. ret = i915_gem_object_attach_phys(obj, align);
  11703. if (ret)
  11704. DRM_DEBUG_KMS("failed to attach phys object\n");
  11705. } else {
  11706. ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  11707. }
  11708. if (ret == 0) {
  11709. if (obj) {
  11710. struct intel_plane_state *plane_state =
  11711. to_intel_plane_state(new_state);
  11712. i915_gem_request_assign(&plane_state->wait_req,
  11713. obj->last_write_req);
  11714. }
  11715. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11716. }
  11717. return ret;
  11718. }
  11719. /**
  11720. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11721. * @plane: drm plane to clean up for
  11722. * @fb: old framebuffer that was on plane
  11723. *
  11724. * Cleans up a framebuffer that has just been removed from a plane.
  11725. *
  11726. * Must be called with struct_mutex held.
  11727. */
  11728. void
  11729. intel_cleanup_plane_fb(struct drm_plane *plane,
  11730. const struct drm_plane_state *old_state)
  11731. {
  11732. struct drm_device *dev = plane->dev;
  11733. struct intel_plane *intel_plane = to_intel_plane(plane);
  11734. struct intel_plane_state *old_intel_state;
  11735. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11736. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11737. old_intel_state = to_intel_plane_state(old_state);
  11738. if (!obj && !old_obj)
  11739. return;
  11740. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11741. !INTEL_INFO(dev)->cursor_needs_physical))
  11742. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  11743. /* prepare_fb aborted? */
  11744. if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
  11745. (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
  11746. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11747. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11748. }
  11749. int
  11750. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11751. {
  11752. int max_scale;
  11753. struct drm_device *dev;
  11754. struct drm_i915_private *dev_priv;
  11755. int crtc_clock, cdclk;
  11756. if (!intel_crtc || !crtc_state->base.enable)
  11757. return DRM_PLANE_HELPER_NO_SCALING;
  11758. dev = intel_crtc->base.dev;
  11759. dev_priv = dev->dev_private;
  11760. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11761. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11762. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11763. return DRM_PLANE_HELPER_NO_SCALING;
  11764. /*
  11765. * skl max scale is lower of:
  11766. * close to 3 but not 3, -1 is for that purpose
  11767. * or
  11768. * cdclk/crtc_clock
  11769. */
  11770. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11771. return max_scale;
  11772. }
  11773. static int
  11774. intel_check_primary_plane(struct drm_plane *plane,
  11775. struct intel_crtc_state *crtc_state,
  11776. struct intel_plane_state *state)
  11777. {
  11778. struct drm_crtc *crtc = state->base.crtc;
  11779. struct drm_framebuffer *fb = state->base.fb;
  11780. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11781. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11782. bool can_position = false;
  11783. if (INTEL_INFO(plane->dev)->gen >= 9) {
  11784. /* use scaler when colorkey is not required */
  11785. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11786. min_scale = 1;
  11787. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11788. }
  11789. can_position = true;
  11790. }
  11791. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11792. &state->dst, &state->clip,
  11793. min_scale, max_scale,
  11794. can_position, true,
  11795. &state->visible);
  11796. }
  11797. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11798. struct drm_crtc_state *old_crtc_state)
  11799. {
  11800. struct drm_device *dev = crtc->dev;
  11801. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11802. struct intel_crtc_state *old_intel_state =
  11803. to_intel_crtc_state(old_crtc_state);
  11804. bool modeset = needs_modeset(crtc->state);
  11805. /* Perform vblank evasion around commit operation */
  11806. intel_pipe_update_start(intel_crtc);
  11807. if (modeset)
  11808. return;
  11809. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  11810. intel_color_set_csc(crtc->state);
  11811. intel_color_load_luts(crtc->state);
  11812. }
  11813. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11814. intel_update_pipe_config(intel_crtc, old_intel_state);
  11815. else if (INTEL_INFO(dev)->gen >= 9)
  11816. skl_detach_scalers(intel_crtc);
  11817. }
  11818. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11819. struct drm_crtc_state *old_crtc_state)
  11820. {
  11821. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11822. intel_pipe_update_end(intel_crtc, NULL);
  11823. }
  11824. /**
  11825. * intel_plane_destroy - destroy a plane
  11826. * @plane: plane to destroy
  11827. *
  11828. * Common destruction function for all types of planes (primary, cursor,
  11829. * sprite).
  11830. */
  11831. void intel_plane_destroy(struct drm_plane *plane)
  11832. {
  11833. if (!plane)
  11834. return;
  11835. drm_plane_cleanup(plane);
  11836. kfree(to_intel_plane(plane));
  11837. }
  11838. const struct drm_plane_funcs intel_plane_funcs = {
  11839. .update_plane = drm_atomic_helper_update_plane,
  11840. .disable_plane = drm_atomic_helper_disable_plane,
  11841. .destroy = intel_plane_destroy,
  11842. .set_property = drm_atomic_helper_plane_set_property,
  11843. .atomic_get_property = intel_plane_atomic_get_property,
  11844. .atomic_set_property = intel_plane_atomic_set_property,
  11845. .atomic_duplicate_state = intel_plane_duplicate_state,
  11846. .atomic_destroy_state = intel_plane_destroy_state,
  11847. };
  11848. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11849. int pipe)
  11850. {
  11851. struct intel_plane *primary = NULL;
  11852. struct intel_plane_state *state = NULL;
  11853. const uint32_t *intel_primary_formats;
  11854. unsigned int num_formats;
  11855. int ret;
  11856. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11857. if (!primary)
  11858. goto fail;
  11859. state = intel_create_plane_state(&primary->base);
  11860. if (!state)
  11861. goto fail;
  11862. primary->base.state = &state->base;
  11863. primary->can_scale = false;
  11864. primary->max_downscale = 1;
  11865. if (INTEL_INFO(dev)->gen >= 9) {
  11866. primary->can_scale = true;
  11867. state->scaler_id = -1;
  11868. }
  11869. primary->pipe = pipe;
  11870. primary->plane = pipe;
  11871. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11872. primary->check_plane = intel_check_primary_plane;
  11873. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11874. primary->plane = !pipe;
  11875. if (INTEL_INFO(dev)->gen >= 9) {
  11876. intel_primary_formats = skl_primary_formats;
  11877. num_formats = ARRAY_SIZE(skl_primary_formats);
  11878. primary->update_plane = skylake_update_primary_plane;
  11879. primary->disable_plane = skylake_disable_primary_plane;
  11880. } else if (HAS_PCH_SPLIT(dev)) {
  11881. intel_primary_formats = i965_primary_formats;
  11882. num_formats = ARRAY_SIZE(i965_primary_formats);
  11883. primary->update_plane = ironlake_update_primary_plane;
  11884. primary->disable_plane = i9xx_disable_primary_plane;
  11885. } else if (INTEL_INFO(dev)->gen >= 4) {
  11886. intel_primary_formats = i965_primary_formats;
  11887. num_formats = ARRAY_SIZE(i965_primary_formats);
  11888. primary->update_plane = i9xx_update_primary_plane;
  11889. primary->disable_plane = i9xx_disable_primary_plane;
  11890. } else {
  11891. intel_primary_formats = i8xx_primary_formats;
  11892. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11893. primary->update_plane = i9xx_update_primary_plane;
  11894. primary->disable_plane = i9xx_disable_primary_plane;
  11895. }
  11896. if (INTEL_INFO(dev)->gen >= 9)
  11897. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11898. &intel_plane_funcs,
  11899. intel_primary_formats, num_formats,
  11900. DRM_PLANE_TYPE_PRIMARY,
  11901. "plane 1%c", pipe_name(pipe));
  11902. else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  11903. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11904. &intel_plane_funcs,
  11905. intel_primary_formats, num_formats,
  11906. DRM_PLANE_TYPE_PRIMARY,
  11907. "primary %c", pipe_name(pipe));
  11908. else
  11909. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11910. &intel_plane_funcs,
  11911. intel_primary_formats, num_formats,
  11912. DRM_PLANE_TYPE_PRIMARY,
  11913. "plane %c", plane_name(primary->plane));
  11914. if (ret)
  11915. goto fail;
  11916. if (INTEL_INFO(dev)->gen >= 4)
  11917. intel_create_rotation_property(dev, primary);
  11918. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11919. return &primary->base;
  11920. fail:
  11921. kfree(state);
  11922. kfree(primary);
  11923. return NULL;
  11924. }
  11925. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11926. {
  11927. if (!dev->mode_config.rotation_property) {
  11928. unsigned long flags = BIT(DRM_ROTATE_0) |
  11929. BIT(DRM_ROTATE_180);
  11930. if (INTEL_INFO(dev)->gen >= 9)
  11931. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11932. dev->mode_config.rotation_property =
  11933. drm_mode_create_rotation_property(dev, flags);
  11934. }
  11935. if (dev->mode_config.rotation_property)
  11936. drm_object_attach_property(&plane->base.base,
  11937. dev->mode_config.rotation_property,
  11938. plane->base.state->rotation);
  11939. }
  11940. static int
  11941. intel_check_cursor_plane(struct drm_plane *plane,
  11942. struct intel_crtc_state *crtc_state,
  11943. struct intel_plane_state *state)
  11944. {
  11945. struct drm_crtc *crtc = crtc_state->base.crtc;
  11946. struct drm_framebuffer *fb = state->base.fb;
  11947. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11948. enum pipe pipe = to_intel_plane(plane)->pipe;
  11949. unsigned stride;
  11950. int ret;
  11951. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11952. &state->dst, &state->clip,
  11953. DRM_PLANE_HELPER_NO_SCALING,
  11954. DRM_PLANE_HELPER_NO_SCALING,
  11955. true, true, &state->visible);
  11956. if (ret)
  11957. return ret;
  11958. /* if we want to turn off the cursor ignore width and height */
  11959. if (!obj)
  11960. return 0;
  11961. /* Check for which cursor types we support */
  11962. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11963. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11964. state->base.crtc_w, state->base.crtc_h);
  11965. return -EINVAL;
  11966. }
  11967. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11968. if (obj->base.size < stride * state->base.crtc_h) {
  11969. DRM_DEBUG_KMS("buffer is too small\n");
  11970. return -ENOMEM;
  11971. }
  11972. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11973. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11974. return -EINVAL;
  11975. }
  11976. /*
  11977. * There's something wrong with the cursor on CHV pipe C.
  11978. * If it straddles the left edge of the screen then
  11979. * moving it away from the edge or disabling it often
  11980. * results in a pipe underrun, and often that can lead to
  11981. * dead pipe (constant underrun reported, and it scans
  11982. * out just a solid color). To recover from that, the
  11983. * display power well must be turned off and on again.
  11984. * Refuse the put the cursor into that compromised position.
  11985. */
  11986. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  11987. state->visible && state->base.crtc_x < 0) {
  11988. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  11989. return -EINVAL;
  11990. }
  11991. return 0;
  11992. }
  11993. static void
  11994. intel_disable_cursor_plane(struct drm_plane *plane,
  11995. struct drm_crtc *crtc)
  11996. {
  11997. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11998. intel_crtc->cursor_addr = 0;
  11999. intel_crtc_update_cursor(crtc, NULL);
  12000. }
  12001. static void
  12002. intel_update_cursor_plane(struct drm_plane *plane,
  12003. const struct intel_crtc_state *crtc_state,
  12004. const struct intel_plane_state *state)
  12005. {
  12006. struct drm_crtc *crtc = crtc_state->base.crtc;
  12007. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12008. struct drm_device *dev = plane->dev;
  12009. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12010. uint32_t addr;
  12011. if (!obj)
  12012. addr = 0;
  12013. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  12014. addr = i915_gem_obj_ggtt_offset(obj);
  12015. else
  12016. addr = obj->phys_handle->busaddr;
  12017. intel_crtc->cursor_addr = addr;
  12018. intel_crtc_update_cursor(crtc, state);
  12019. }
  12020. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  12021. int pipe)
  12022. {
  12023. struct intel_plane *cursor = NULL;
  12024. struct intel_plane_state *state = NULL;
  12025. int ret;
  12026. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12027. if (!cursor)
  12028. goto fail;
  12029. state = intel_create_plane_state(&cursor->base);
  12030. if (!state)
  12031. goto fail;
  12032. cursor->base.state = &state->base;
  12033. cursor->can_scale = false;
  12034. cursor->max_downscale = 1;
  12035. cursor->pipe = pipe;
  12036. cursor->plane = pipe;
  12037. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12038. cursor->check_plane = intel_check_cursor_plane;
  12039. cursor->update_plane = intel_update_cursor_plane;
  12040. cursor->disable_plane = intel_disable_cursor_plane;
  12041. ret = drm_universal_plane_init(dev, &cursor->base, 0,
  12042. &intel_plane_funcs,
  12043. intel_cursor_formats,
  12044. ARRAY_SIZE(intel_cursor_formats),
  12045. DRM_PLANE_TYPE_CURSOR,
  12046. "cursor %c", pipe_name(pipe));
  12047. if (ret)
  12048. goto fail;
  12049. if (INTEL_INFO(dev)->gen >= 4) {
  12050. if (!dev->mode_config.rotation_property)
  12051. dev->mode_config.rotation_property =
  12052. drm_mode_create_rotation_property(dev,
  12053. BIT(DRM_ROTATE_0) |
  12054. BIT(DRM_ROTATE_180));
  12055. if (dev->mode_config.rotation_property)
  12056. drm_object_attach_property(&cursor->base.base,
  12057. dev->mode_config.rotation_property,
  12058. state->base.rotation);
  12059. }
  12060. if (INTEL_INFO(dev)->gen >=9)
  12061. state->scaler_id = -1;
  12062. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12063. return &cursor->base;
  12064. fail:
  12065. kfree(state);
  12066. kfree(cursor);
  12067. return NULL;
  12068. }
  12069. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  12070. struct intel_crtc_state *crtc_state)
  12071. {
  12072. int i;
  12073. struct intel_scaler *intel_scaler;
  12074. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  12075. for (i = 0; i < intel_crtc->num_scalers; i++) {
  12076. intel_scaler = &scaler_state->scalers[i];
  12077. intel_scaler->in_use = 0;
  12078. intel_scaler->mode = PS_SCALER_MODE_DYN;
  12079. }
  12080. scaler_state->scaler_id = -1;
  12081. }
  12082. static void intel_crtc_init(struct drm_device *dev, int pipe)
  12083. {
  12084. struct drm_i915_private *dev_priv = dev->dev_private;
  12085. struct intel_crtc *intel_crtc;
  12086. struct intel_crtc_state *crtc_state = NULL;
  12087. struct drm_plane *primary = NULL;
  12088. struct drm_plane *cursor = NULL;
  12089. int ret;
  12090. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12091. if (intel_crtc == NULL)
  12092. return;
  12093. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12094. if (!crtc_state)
  12095. goto fail;
  12096. intel_crtc->config = crtc_state;
  12097. intel_crtc->base.state = &crtc_state->base;
  12098. crtc_state->base.crtc = &intel_crtc->base;
  12099. /* initialize shared scalers */
  12100. if (INTEL_INFO(dev)->gen >= 9) {
  12101. if (pipe == PIPE_C)
  12102. intel_crtc->num_scalers = 1;
  12103. else
  12104. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  12105. skl_init_scalers(dev, intel_crtc, crtc_state);
  12106. }
  12107. primary = intel_primary_plane_create(dev, pipe);
  12108. if (!primary)
  12109. goto fail;
  12110. cursor = intel_cursor_plane_create(dev, pipe);
  12111. if (!cursor)
  12112. goto fail;
  12113. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  12114. cursor, &intel_crtc_funcs,
  12115. "pipe %c", pipe_name(pipe));
  12116. if (ret)
  12117. goto fail;
  12118. /*
  12119. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  12120. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  12121. */
  12122. intel_crtc->pipe = pipe;
  12123. intel_crtc->plane = pipe;
  12124. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  12125. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  12126. intel_crtc->plane = !pipe;
  12127. }
  12128. intel_crtc->cursor_base = ~0;
  12129. intel_crtc->cursor_cntl = ~0;
  12130. intel_crtc->cursor_size = ~0;
  12131. intel_crtc->wm.cxsr_allowed = true;
  12132. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12133. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12134. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  12135. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  12136. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12137. intel_color_init(&intel_crtc->base);
  12138. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12139. return;
  12140. fail:
  12141. intel_plane_destroy(primary);
  12142. intel_plane_destroy(cursor);
  12143. kfree(crtc_state);
  12144. kfree(intel_crtc);
  12145. }
  12146. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12147. {
  12148. struct drm_encoder *encoder = connector->base.encoder;
  12149. struct drm_device *dev = connector->base.dev;
  12150. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12151. if (!encoder || WARN_ON(!encoder->crtc))
  12152. return INVALID_PIPE;
  12153. return to_intel_crtc(encoder->crtc)->pipe;
  12154. }
  12155. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12156. struct drm_file *file)
  12157. {
  12158. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12159. struct drm_crtc *drmmode_crtc;
  12160. struct intel_crtc *crtc;
  12161. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12162. if (!drmmode_crtc) {
  12163. DRM_ERROR("no such CRTC id\n");
  12164. return -ENOENT;
  12165. }
  12166. crtc = to_intel_crtc(drmmode_crtc);
  12167. pipe_from_crtc_id->pipe = crtc->pipe;
  12168. return 0;
  12169. }
  12170. static int intel_encoder_clones(struct intel_encoder *encoder)
  12171. {
  12172. struct drm_device *dev = encoder->base.dev;
  12173. struct intel_encoder *source_encoder;
  12174. int index_mask = 0;
  12175. int entry = 0;
  12176. for_each_intel_encoder(dev, source_encoder) {
  12177. if (encoders_cloneable(encoder, source_encoder))
  12178. index_mask |= (1 << entry);
  12179. entry++;
  12180. }
  12181. return index_mask;
  12182. }
  12183. static bool has_edp_a(struct drm_device *dev)
  12184. {
  12185. struct drm_i915_private *dev_priv = dev->dev_private;
  12186. if (!IS_MOBILE(dev))
  12187. return false;
  12188. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12189. return false;
  12190. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12191. return false;
  12192. return true;
  12193. }
  12194. static bool intel_crt_present(struct drm_device *dev)
  12195. {
  12196. struct drm_i915_private *dev_priv = dev->dev_private;
  12197. if (INTEL_INFO(dev)->gen >= 9)
  12198. return false;
  12199. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  12200. return false;
  12201. if (IS_CHERRYVIEW(dev))
  12202. return false;
  12203. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12204. return false;
  12205. /* DDI E can't be used if DDI A requires 4 lanes */
  12206. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12207. return false;
  12208. if (!dev_priv->vbt.int_crt_support)
  12209. return false;
  12210. return true;
  12211. }
  12212. static void intel_setup_outputs(struct drm_device *dev)
  12213. {
  12214. struct drm_i915_private *dev_priv = dev->dev_private;
  12215. struct intel_encoder *encoder;
  12216. bool dpd_is_edp = false;
  12217. intel_lvds_init(dev);
  12218. if (intel_crt_present(dev))
  12219. intel_crt_init(dev);
  12220. if (IS_BROXTON(dev)) {
  12221. /*
  12222. * FIXME: Broxton doesn't support port detection via the
  12223. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12224. * detect the ports.
  12225. */
  12226. intel_ddi_init(dev, PORT_A);
  12227. intel_ddi_init(dev, PORT_B);
  12228. intel_ddi_init(dev, PORT_C);
  12229. intel_dsi_init(dev);
  12230. } else if (HAS_DDI(dev)) {
  12231. int found;
  12232. /*
  12233. * Haswell uses DDI functions to detect digital outputs.
  12234. * On SKL pre-D0 the strap isn't connected, so we assume
  12235. * it's there.
  12236. */
  12237. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12238. /* WaIgnoreDDIAStrap: skl */
  12239. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12240. intel_ddi_init(dev, PORT_A);
  12241. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12242. * register */
  12243. found = I915_READ(SFUSE_STRAP);
  12244. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12245. intel_ddi_init(dev, PORT_B);
  12246. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12247. intel_ddi_init(dev, PORT_C);
  12248. if (found & SFUSE_STRAP_DDID_DETECTED)
  12249. intel_ddi_init(dev, PORT_D);
  12250. /*
  12251. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12252. */
  12253. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  12254. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12255. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12256. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12257. intel_ddi_init(dev, PORT_E);
  12258. } else if (HAS_PCH_SPLIT(dev)) {
  12259. int found;
  12260. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12261. if (has_edp_a(dev))
  12262. intel_dp_init(dev, DP_A, PORT_A);
  12263. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12264. /* PCH SDVOB multiplex with HDMIB */
  12265. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12266. if (!found)
  12267. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12268. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12269. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12270. }
  12271. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12272. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12273. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12274. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12275. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12276. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12277. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12278. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12279. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12280. bool has_edp;
  12281. /*
  12282. * The DP_DETECTED bit is the latched state of the DDC
  12283. * SDA pin at boot. However since eDP doesn't require DDC
  12284. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12285. * eDP ports may have been muxed to an alternate function.
  12286. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12287. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12288. * detect eDP ports.
  12289. */
  12290. has_edp = intel_dp_is_edp(dev, PORT_B);
  12291. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_edp)
  12292. has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
  12293. if (I915_READ(VLV_HDMIB) & SDVO_DETECTED && !has_edp)
  12294. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  12295. has_edp = intel_dp_is_edp(dev, PORT_C);
  12296. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_edp)
  12297. has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
  12298. if (I915_READ(VLV_HDMIC) & SDVO_DETECTED && !has_edp)
  12299. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  12300. if (IS_CHERRYVIEW(dev)) {
  12301. /* eDP not supported on port D, so don't check VBT */
  12302. if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
  12303. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  12304. if (I915_READ(CHV_DP_D) & DP_DETECTED)
  12305. intel_dp_init(dev, CHV_DP_D, PORT_D);
  12306. }
  12307. intel_dsi_init(dev);
  12308. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12309. bool found = false;
  12310. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12311. DRM_DEBUG_KMS("probing SDVOB\n");
  12312. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  12313. if (!found && IS_G4X(dev)) {
  12314. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12315. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12316. }
  12317. if (!found && IS_G4X(dev))
  12318. intel_dp_init(dev, DP_B, PORT_B);
  12319. }
  12320. /* Before G4X SDVOC doesn't have its own detect register */
  12321. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12322. DRM_DEBUG_KMS("probing SDVOC\n");
  12323. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  12324. }
  12325. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12326. if (IS_G4X(dev)) {
  12327. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12328. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12329. }
  12330. if (IS_G4X(dev))
  12331. intel_dp_init(dev, DP_C, PORT_C);
  12332. }
  12333. if (IS_G4X(dev) &&
  12334. (I915_READ(DP_D) & DP_DETECTED))
  12335. intel_dp_init(dev, DP_D, PORT_D);
  12336. } else if (IS_GEN2(dev))
  12337. intel_dvo_init(dev);
  12338. if (SUPPORTS_TV(dev))
  12339. intel_tv_init(dev);
  12340. intel_psr_init(dev);
  12341. for_each_intel_encoder(dev, encoder) {
  12342. encoder->base.possible_crtcs = encoder->crtc_mask;
  12343. encoder->base.possible_clones =
  12344. intel_encoder_clones(encoder);
  12345. }
  12346. intel_init_pch_refclk(dev);
  12347. drm_helper_move_panel_connectors_to_head(dev);
  12348. }
  12349. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12350. {
  12351. struct drm_device *dev = fb->dev;
  12352. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12353. drm_framebuffer_cleanup(fb);
  12354. mutex_lock(&dev->struct_mutex);
  12355. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12356. drm_gem_object_unreference(&intel_fb->obj->base);
  12357. mutex_unlock(&dev->struct_mutex);
  12358. kfree(intel_fb);
  12359. }
  12360. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12361. struct drm_file *file,
  12362. unsigned int *handle)
  12363. {
  12364. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12365. struct drm_i915_gem_object *obj = intel_fb->obj;
  12366. if (obj->userptr.mm) {
  12367. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  12368. return -EINVAL;
  12369. }
  12370. return drm_gem_handle_create(file, &obj->base, handle);
  12371. }
  12372. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12373. struct drm_file *file,
  12374. unsigned flags, unsigned color,
  12375. struct drm_clip_rect *clips,
  12376. unsigned num_clips)
  12377. {
  12378. struct drm_device *dev = fb->dev;
  12379. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12380. struct drm_i915_gem_object *obj = intel_fb->obj;
  12381. mutex_lock(&dev->struct_mutex);
  12382. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  12383. mutex_unlock(&dev->struct_mutex);
  12384. return 0;
  12385. }
  12386. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12387. .destroy = intel_user_framebuffer_destroy,
  12388. .create_handle = intel_user_framebuffer_create_handle,
  12389. .dirty = intel_user_framebuffer_dirty,
  12390. };
  12391. static
  12392. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12393. uint32_t pixel_format)
  12394. {
  12395. u32 gen = INTEL_INFO(dev)->gen;
  12396. if (gen >= 9) {
  12397. int cpp = drm_format_plane_cpp(pixel_format, 0);
  12398. /* "The stride in bytes must not exceed the of the size of 8K
  12399. * pixels and 32K bytes."
  12400. */
  12401. return min(8192 * cpp, 32768);
  12402. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12403. return 32*1024;
  12404. } else if (gen >= 4) {
  12405. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12406. return 16*1024;
  12407. else
  12408. return 32*1024;
  12409. } else if (gen >= 3) {
  12410. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12411. return 8*1024;
  12412. else
  12413. return 16*1024;
  12414. } else {
  12415. /* XXX DSPC is limited to 4k tiled */
  12416. return 8*1024;
  12417. }
  12418. }
  12419. static int intel_framebuffer_init(struct drm_device *dev,
  12420. struct intel_framebuffer *intel_fb,
  12421. struct drm_mode_fb_cmd2 *mode_cmd,
  12422. struct drm_i915_gem_object *obj)
  12423. {
  12424. struct drm_i915_private *dev_priv = to_i915(dev);
  12425. unsigned int aligned_height;
  12426. int ret;
  12427. u32 pitch_limit, stride_alignment;
  12428. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12429. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12430. /* Enforce that fb modifier and tiling mode match, but only for
  12431. * X-tiled. This is needed for FBC. */
  12432. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12433. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12434. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12435. return -EINVAL;
  12436. }
  12437. } else {
  12438. if (obj->tiling_mode == I915_TILING_X)
  12439. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12440. else if (obj->tiling_mode == I915_TILING_Y) {
  12441. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12442. return -EINVAL;
  12443. }
  12444. }
  12445. /* Passed in modifier sanity checking. */
  12446. switch (mode_cmd->modifier[0]) {
  12447. case I915_FORMAT_MOD_Y_TILED:
  12448. case I915_FORMAT_MOD_Yf_TILED:
  12449. if (INTEL_INFO(dev)->gen < 9) {
  12450. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12451. mode_cmd->modifier[0]);
  12452. return -EINVAL;
  12453. }
  12454. case DRM_FORMAT_MOD_NONE:
  12455. case I915_FORMAT_MOD_X_TILED:
  12456. break;
  12457. default:
  12458. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12459. mode_cmd->modifier[0]);
  12460. return -EINVAL;
  12461. }
  12462. stride_alignment = intel_fb_stride_alignment(dev_priv,
  12463. mode_cmd->modifier[0],
  12464. mode_cmd->pixel_format);
  12465. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12466. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12467. mode_cmd->pitches[0], stride_alignment);
  12468. return -EINVAL;
  12469. }
  12470. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12471. mode_cmd->pixel_format);
  12472. if (mode_cmd->pitches[0] > pitch_limit) {
  12473. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12474. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12475. "tiled" : "linear",
  12476. mode_cmd->pitches[0], pitch_limit);
  12477. return -EINVAL;
  12478. }
  12479. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12480. mode_cmd->pitches[0] != obj->stride) {
  12481. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12482. mode_cmd->pitches[0], obj->stride);
  12483. return -EINVAL;
  12484. }
  12485. /* Reject formats not supported by any plane early. */
  12486. switch (mode_cmd->pixel_format) {
  12487. case DRM_FORMAT_C8:
  12488. case DRM_FORMAT_RGB565:
  12489. case DRM_FORMAT_XRGB8888:
  12490. case DRM_FORMAT_ARGB8888:
  12491. break;
  12492. case DRM_FORMAT_XRGB1555:
  12493. if (INTEL_INFO(dev)->gen > 3) {
  12494. DRM_DEBUG("unsupported pixel format: %s\n",
  12495. drm_get_format_name(mode_cmd->pixel_format));
  12496. return -EINVAL;
  12497. }
  12498. break;
  12499. case DRM_FORMAT_ABGR8888:
  12500. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  12501. INTEL_INFO(dev)->gen < 9) {
  12502. DRM_DEBUG("unsupported pixel format: %s\n",
  12503. drm_get_format_name(mode_cmd->pixel_format));
  12504. return -EINVAL;
  12505. }
  12506. break;
  12507. case DRM_FORMAT_XBGR8888:
  12508. case DRM_FORMAT_XRGB2101010:
  12509. case DRM_FORMAT_XBGR2101010:
  12510. if (INTEL_INFO(dev)->gen < 4) {
  12511. DRM_DEBUG("unsupported pixel format: %s\n",
  12512. drm_get_format_name(mode_cmd->pixel_format));
  12513. return -EINVAL;
  12514. }
  12515. break;
  12516. case DRM_FORMAT_ABGR2101010:
  12517. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12518. DRM_DEBUG("unsupported pixel format: %s\n",
  12519. drm_get_format_name(mode_cmd->pixel_format));
  12520. return -EINVAL;
  12521. }
  12522. break;
  12523. case DRM_FORMAT_YUYV:
  12524. case DRM_FORMAT_UYVY:
  12525. case DRM_FORMAT_YVYU:
  12526. case DRM_FORMAT_VYUY:
  12527. if (INTEL_INFO(dev)->gen < 5) {
  12528. DRM_DEBUG("unsupported pixel format: %s\n",
  12529. drm_get_format_name(mode_cmd->pixel_format));
  12530. return -EINVAL;
  12531. }
  12532. break;
  12533. default:
  12534. DRM_DEBUG("unsupported pixel format: %s\n",
  12535. drm_get_format_name(mode_cmd->pixel_format));
  12536. return -EINVAL;
  12537. }
  12538. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12539. if (mode_cmd->offsets[0] != 0)
  12540. return -EINVAL;
  12541. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12542. mode_cmd->pixel_format,
  12543. mode_cmd->modifier[0]);
  12544. /* FIXME drm helper for size checks (especially planar formats)? */
  12545. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12546. return -EINVAL;
  12547. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12548. intel_fb->obj = obj;
  12549. intel_fill_fb_info(dev_priv, &intel_fb->base);
  12550. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12551. if (ret) {
  12552. DRM_ERROR("framebuffer init failed %d\n", ret);
  12553. return ret;
  12554. }
  12555. intel_fb->obj->framebuffer_references++;
  12556. return 0;
  12557. }
  12558. static struct drm_framebuffer *
  12559. intel_user_framebuffer_create(struct drm_device *dev,
  12560. struct drm_file *filp,
  12561. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12562. {
  12563. struct drm_framebuffer *fb;
  12564. struct drm_i915_gem_object *obj;
  12565. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12566. obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
  12567. if (&obj->base == NULL)
  12568. return ERR_PTR(-ENOENT);
  12569. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  12570. if (IS_ERR(fb))
  12571. drm_gem_object_unreference_unlocked(&obj->base);
  12572. return fb;
  12573. }
  12574. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12575. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12576. {
  12577. }
  12578. #endif
  12579. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12580. .fb_create = intel_user_framebuffer_create,
  12581. .output_poll_changed = intel_fbdev_output_poll_changed,
  12582. .atomic_check = intel_atomic_check,
  12583. .atomic_commit = intel_atomic_commit,
  12584. .atomic_state_alloc = intel_atomic_state_alloc,
  12585. .atomic_state_clear = intel_atomic_state_clear,
  12586. };
  12587. /**
  12588. * intel_init_display_hooks - initialize the display modesetting hooks
  12589. * @dev_priv: device private
  12590. */
  12591. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12592. {
  12593. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12594. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12595. dev_priv->display.get_initial_plane_config =
  12596. skylake_get_initial_plane_config;
  12597. dev_priv->display.crtc_compute_clock =
  12598. haswell_crtc_compute_clock;
  12599. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12600. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12601. } else if (HAS_DDI(dev_priv)) {
  12602. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12603. dev_priv->display.get_initial_plane_config =
  12604. ironlake_get_initial_plane_config;
  12605. dev_priv->display.crtc_compute_clock =
  12606. haswell_crtc_compute_clock;
  12607. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12608. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12609. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12610. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12611. dev_priv->display.get_initial_plane_config =
  12612. ironlake_get_initial_plane_config;
  12613. dev_priv->display.crtc_compute_clock =
  12614. ironlake_crtc_compute_clock;
  12615. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12616. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12617. } else if (IS_CHERRYVIEW(dev_priv)) {
  12618. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12619. dev_priv->display.get_initial_plane_config =
  12620. i9xx_get_initial_plane_config;
  12621. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12622. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12623. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12624. } else if (IS_VALLEYVIEW(dev_priv)) {
  12625. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12626. dev_priv->display.get_initial_plane_config =
  12627. i9xx_get_initial_plane_config;
  12628. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12629. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12630. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12631. } else if (IS_G4X(dev_priv)) {
  12632. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12633. dev_priv->display.get_initial_plane_config =
  12634. i9xx_get_initial_plane_config;
  12635. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12636. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12637. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12638. } else if (IS_PINEVIEW(dev_priv)) {
  12639. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12640. dev_priv->display.get_initial_plane_config =
  12641. i9xx_get_initial_plane_config;
  12642. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12643. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12644. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12645. } else if (!IS_GEN2(dev_priv)) {
  12646. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12647. dev_priv->display.get_initial_plane_config =
  12648. i9xx_get_initial_plane_config;
  12649. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12650. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12651. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12652. } else {
  12653. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12654. dev_priv->display.get_initial_plane_config =
  12655. i9xx_get_initial_plane_config;
  12656. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12657. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12658. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12659. }
  12660. /* Returns the core display clock speed */
  12661. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12662. dev_priv->display.get_display_clock_speed =
  12663. skylake_get_display_clock_speed;
  12664. else if (IS_BROXTON(dev_priv))
  12665. dev_priv->display.get_display_clock_speed =
  12666. broxton_get_display_clock_speed;
  12667. else if (IS_BROADWELL(dev_priv))
  12668. dev_priv->display.get_display_clock_speed =
  12669. broadwell_get_display_clock_speed;
  12670. else if (IS_HASWELL(dev_priv))
  12671. dev_priv->display.get_display_clock_speed =
  12672. haswell_get_display_clock_speed;
  12673. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12674. dev_priv->display.get_display_clock_speed =
  12675. valleyview_get_display_clock_speed;
  12676. else if (IS_GEN5(dev_priv))
  12677. dev_priv->display.get_display_clock_speed =
  12678. ilk_get_display_clock_speed;
  12679. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  12680. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  12681. dev_priv->display.get_display_clock_speed =
  12682. i945_get_display_clock_speed;
  12683. else if (IS_GM45(dev_priv))
  12684. dev_priv->display.get_display_clock_speed =
  12685. gm45_get_display_clock_speed;
  12686. else if (IS_CRESTLINE(dev_priv))
  12687. dev_priv->display.get_display_clock_speed =
  12688. i965gm_get_display_clock_speed;
  12689. else if (IS_PINEVIEW(dev_priv))
  12690. dev_priv->display.get_display_clock_speed =
  12691. pnv_get_display_clock_speed;
  12692. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  12693. dev_priv->display.get_display_clock_speed =
  12694. g33_get_display_clock_speed;
  12695. else if (IS_I915G(dev_priv))
  12696. dev_priv->display.get_display_clock_speed =
  12697. i915_get_display_clock_speed;
  12698. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  12699. dev_priv->display.get_display_clock_speed =
  12700. i9xx_misc_get_display_clock_speed;
  12701. else if (IS_I915GM(dev_priv))
  12702. dev_priv->display.get_display_clock_speed =
  12703. i915gm_get_display_clock_speed;
  12704. else if (IS_I865G(dev_priv))
  12705. dev_priv->display.get_display_clock_speed =
  12706. i865_get_display_clock_speed;
  12707. else if (IS_I85X(dev_priv))
  12708. dev_priv->display.get_display_clock_speed =
  12709. i85x_get_display_clock_speed;
  12710. else { /* 830 */
  12711. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12712. dev_priv->display.get_display_clock_speed =
  12713. i830_get_display_clock_speed;
  12714. }
  12715. if (IS_GEN5(dev_priv)) {
  12716. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12717. } else if (IS_GEN6(dev_priv)) {
  12718. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12719. } else if (IS_IVYBRIDGE(dev_priv)) {
  12720. /* FIXME: detect B0+ stepping and use auto training */
  12721. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12722. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12723. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12724. }
  12725. if (IS_BROADWELL(dev_priv)) {
  12726. dev_priv->display.modeset_commit_cdclk =
  12727. broadwell_modeset_commit_cdclk;
  12728. dev_priv->display.modeset_calc_cdclk =
  12729. broadwell_modeset_calc_cdclk;
  12730. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12731. dev_priv->display.modeset_commit_cdclk =
  12732. valleyview_modeset_commit_cdclk;
  12733. dev_priv->display.modeset_calc_cdclk =
  12734. valleyview_modeset_calc_cdclk;
  12735. } else if (IS_BROXTON(dev_priv)) {
  12736. dev_priv->display.modeset_commit_cdclk =
  12737. broxton_modeset_commit_cdclk;
  12738. dev_priv->display.modeset_calc_cdclk =
  12739. broxton_modeset_calc_cdclk;
  12740. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  12741. dev_priv->display.modeset_commit_cdclk =
  12742. skl_modeset_commit_cdclk;
  12743. dev_priv->display.modeset_calc_cdclk =
  12744. skl_modeset_calc_cdclk;
  12745. }
  12746. switch (INTEL_INFO(dev_priv)->gen) {
  12747. case 2:
  12748. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12749. break;
  12750. case 3:
  12751. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12752. break;
  12753. case 4:
  12754. case 5:
  12755. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12756. break;
  12757. case 6:
  12758. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12759. break;
  12760. case 7:
  12761. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12762. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12763. break;
  12764. case 9:
  12765. /* Drop through - unsupported since execlist only. */
  12766. default:
  12767. /* Default just returns -ENODEV to indicate unsupported */
  12768. dev_priv->display.queue_flip = intel_default_queue_flip;
  12769. }
  12770. }
  12771. /*
  12772. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12773. * resume, or other times. This quirk makes sure that's the case for
  12774. * affected systems.
  12775. */
  12776. static void quirk_pipea_force(struct drm_device *dev)
  12777. {
  12778. struct drm_i915_private *dev_priv = dev->dev_private;
  12779. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12780. DRM_INFO("applying pipe a force quirk\n");
  12781. }
  12782. static void quirk_pipeb_force(struct drm_device *dev)
  12783. {
  12784. struct drm_i915_private *dev_priv = dev->dev_private;
  12785. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12786. DRM_INFO("applying pipe b force quirk\n");
  12787. }
  12788. /*
  12789. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12790. */
  12791. static void quirk_ssc_force_disable(struct drm_device *dev)
  12792. {
  12793. struct drm_i915_private *dev_priv = dev->dev_private;
  12794. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12795. DRM_INFO("applying lvds SSC disable quirk\n");
  12796. }
  12797. /*
  12798. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12799. * brightness value
  12800. */
  12801. static void quirk_invert_brightness(struct drm_device *dev)
  12802. {
  12803. struct drm_i915_private *dev_priv = dev->dev_private;
  12804. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12805. DRM_INFO("applying inverted panel brightness quirk\n");
  12806. }
  12807. /* Some VBT's incorrectly indicate no backlight is present */
  12808. static void quirk_backlight_present(struct drm_device *dev)
  12809. {
  12810. struct drm_i915_private *dev_priv = dev->dev_private;
  12811. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12812. DRM_INFO("applying backlight present quirk\n");
  12813. }
  12814. struct intel_quirk {
  12815. int device;
  12816. int subsystem_vendor;
  12817. int subsystem_device;
  12818. void (*hook)(struct drm_device *dev);
  12819. };
  12820. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12821. struct intel_dmi_quirk {
  12822. void (*hook)(struct drm_device *dev);
  12823. const struct dmi_system_id (*dmi_id_list)[];
  12824. };
  12825. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12826. {
  12827. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12828. return 1;
  12829. }
  12830. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12831. {
  12832. .dmi_id_list = &(const struct dmi_system_id[]) {
  12833. {
  12834. .callback = intel_dmi_reverse_brightness,
  12835. .ident = "NCR Corporation",
  12836. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12837. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12838. },
  12839. },
  12840. { } /* terminating entry */
  12841. },
  12842. .hook = quirk_invert_brightness,
  12843. },
  12844. };
  12845. static struct intel_quirk intel_quirks[] = {
  12846. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12847. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12848. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12849. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12850. /* 830 needs to leave pipe A & dpll A up */
  12851. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12852. /* 830 needs to leave pipe B & dpll B up */
  12853. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12854. /* Lenovo U160 cannot use SSC on LVDS */
  12855. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12856. /* Sony Vaio Y cannot use SSC on LVDS */
  12857. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12858. /* Acer Aspire 5734Z must invert backlight brightness */
  12859. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12860. /* Acer/eMachines G725 */
  12861. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12862. /* Acer/eMachines e725 */
  12863. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12864. /* Acer/Packard Bell NCL20 */
  12865. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12866. /* Acer Aspire 4736Z */
  12867. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12868. /* Acer Aspire 5336 */
  12869. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12870. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12871. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12872. /* Acer C720 Chromebook (Core i3 4005U) */
  12873. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12874. /* Apple Macbook 2,1 (Core 2 T7400) */
  12875. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12876. /* Apple Macbook 4,1 */
  12877. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12878. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12879. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12880. /* HP Chromebook 14 (Celeron 2955U) */
  12881. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12882. /* Dell Chromebook 11 */
  12883. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12884. /* Dell Chromebook 11 (2015 version) */
  12885. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12886. };
  12887. static void intel_init_quirks(struct drm_device *dev)
  12888. {
  12889. struct pci_dev *d = dev->pdev;
  12890. int i;
  12891. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12892. struct intel_quirk *q = &intel_quirks[i];
  12893. if (d->device == q->device &&
  12894. (d->subsystem_vendor == q->subsystem_vendor ||
  12895. q->subsystem_vendor == PCI_ANY_ID) &&
  12896. (d->subsystem_device == q->subsystem_device ||
  12897. q->subsystem_device == PCI_ANY_ID))
  12898. q->hook(dev);
  12899. }
  12900. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12901. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12902. intel_dmi_quirks[i].hook(dev);
  12903. }
  12904. }
  12905. /* Disable the VGA plane that we never use */
  12906. static void i915_disable_vga(struct drm_device *dev)
  12907. {
  12908. struct drm_i915_private *dev_priv = dev->dev_private;
  12909. u8 sr1;
  12910. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  12911. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12912. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12913. outb(SR01, VGA_SR_INDEX);
  12914. sr1 = inb(VGA_SR_DATA);
  12915. outb(sr1 | 1<<5, VGA_SR_DATA);
  12916. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12917. udelay(300);
  12918. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12919. POSTING_READ(vga_reg);
  12920. }
  12921. void intel_modeset_init_hw(struct drm_device *dev)
  12922. {
  12923. struct drm_i915_private *dev_priv = dev->dev_private;
  12924. intel_update_cdclk(dev);
  12925. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  12926. intel_init_clock_gating(dev);
  12927. intel_enable_gt_powersave(dev_priv);
  12928. }
  12929. /*
  12930. * Calculate what we think the watermarks should be for the state we've read
  12931. * out of the hardware and then immediately program those watermarks so that
  12932. * we ensure the hardware settings match our internal state.
  12933. *
  12934. * We can calculate what we think WM's should be by creating a duplicate of the
  12935. * current state (which was constructed during hardware readout) and running it
  12936. * through the atomic check code to calculate new watermark values in the
  12937. * state object.
  12938. */
  12939. static void sanitize_watermarks(struct drm_device *dev)
  12940. {
  12941. struct drm_i915_private *dev_priv = to_i915(dev);
  12942. struct drm_atomic_state *state;
  12943. struct drm_crtc *crtc;
  12944. struct drm_crtc_state *cstate;
  12945. struct drm_modeset_acquire_ctx ctx;
  12946. int ret;
  12947. int i;
  12948. /* Only supported on platforms that use atomic watermark design */
  12949. if (!dev_priv->display.optimize_watermarks)
  12950. return;
  12951. /*
  12952. * We need to hold connection_mutex before calling duplicate_state so
  12953. * that the connector loop is protected.
  12954. */
  12955. drm_modeset_acquire_init(&ctx, 0);
  12956. retry:
  12957. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12958. if (ret == -EDEADLK) {
  12959. drm_modeset_backoff(&ctx);
  12960. goto retry;
  12961. } else if (WARN_ON(ret)) {
  12962. goto fail;
  12963. }
  12964. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12965. if (WARN_ON(IS_ERR(state)))
  12966. goto fail;
  12967. /*
  12968. * Hardware readout is the only time we don't want to calculate
  12969. * intermediate watermarks (since we don't trust the current
  12970. * watermarks).
  12971. */
  12972. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  12973. ret = intel_atomic_check(dev, state);
  12974. if (ret) {
  12975. /*
  12976. * If we fail here, it means that the hardware appears to be
  12977. * programmed in a way that shouldn't be possible, given our
  12978. * understanding of watermark requirements. This might mean a
  12979. * mistake in the hardware readout code or a mistake in the
  12980. * watermark calculations for a given platform. Raise a WARN
  12981. * so that this is noticeable.
  12982. *
  12983. * If this actually happens, we'll have to just leave the
  12984. * BIOS-programmed watermarks untouched and hope for the best.
  12985. */
  12986. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12987. goto fail;
  12988. }
  12989. /* Write calculated watermark values back */
  12990. for_each_crtc_in_state(state, crtc, cstate, i) {
  12991. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12992. cs->wm.need_postvbl_update = true;
  12993. dev_priv->display.optimize_watermarks(cs);
  12994. }
  12995. drm_atomic_state_free(state);
  12996. fail:
  12997. drm_modeset_drop_locks(&ctx);
  12998. drm_modeset_acquire_fini(&ctx);
  12999. }
  13000. void intel_modeset_init(struct drm_device *dev)
  13001. {
  13002. struct drm_i915_private *dev_priv = to_i915(dev);
  13003. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13004. int sprite, ret;
  13005. enum pipe pipe;
  13006. struct intel_crtc *crtc;
  13007. drm_mode_config_init(dev);
  13008. dev->mode_config.min_width = 0;
  13009. dev->mode_config.min_height = 0;
  13010. dev->mode_config.preferred_depth = 24;
  13011. dev->mode_config.prefer_shadow = 1;
  13012. dev->mode_config.allow_fb_modifiers = true;
  13013. dev->mode_config.funcs = &intel_mode_funcs;
  13014. intel_init_quirks(dev);
  13015. intel_init_pm(dev);
  13016. if (INTEL_INFO(dev)->num_pipes == 0)
  13017. return;
  13018. /*
  13019. * There may be no VBT; and if the BIOS enabled SSC we can
  13020. * just keep using it to avoid unnecessary flicker. Whereas if the
  13021. * BIOS isn't using it, don't assume it will work even if the VBT
  13022. * indicates as much.
  13023. */
  13024. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  13025. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13026. DREF_SSC1_ENABLE);
  13027. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13028. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13029. bios_lvds_use_ssc ? "en" : "dis",
  13030. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13031. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13032. }
  13033. }
  13034. if (IS_GEN2(dev)) {
  13035. dev->mode_config.max_width = 2048;
  13036. dev->mode_config.max_height = 2048;
  13037. } else if (IS_GEN3(dev)) {
  13038. dev->mode_config.max_width = 4096;
  13039. dev->mode_config.max_height = 4096;
  13040. } else {
  13041. dev->mode_config.max_width = 8192;
  13042. dev->mode_config.max_height = 8192;
  13043. }
  13044. if (IS_845G(dev) || IS_I865G(dev)) {
  13045. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  13046. dev->mode_config.cursor_height = 1023;
  13047. } else if (IS_GEN2(dev)) {
  13048. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13049. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13050. } else {
  13051. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13052. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13053. }
  13054. dev->mode_config.fb_base = ggtt->mappable_base;
  13055. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13056. INTEL_INFO(dev)->num_pipes,
  13057. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  13058. for_each_pipe(dev_priv, pipe) {
  13059. intel_crtc_init(dev, pipe);
  13060. for_each_sprite(dev_priv, pipe, sprite) {
  13061. ret = intel_plane_init(dev, pipe, sprite);
  13062. if (ret)
  13063. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  13064. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  13065. }
  13066. }
  13067. intel_update_czclk(dev_priv);
  13068. intel_update_cdclk(dev);
  13069. intel_shared_dpll_init(dev);
  13070. if (dev_priv->max_cdclk_freq == 0)
  13071. intel_update_max_cdclk(dev);
  13072. /* Just disable it once at startup */
  13073. i915_disable_vga(dev);
  13074. intel_setup_outputs(dev);
  13075. drm_modeset_lock_all(dev);
  13076. intel_modeset_setup_hw_state(dev);
  13077. drm_modeset_unlock_all(dev);
  13078. for_each_intel_crtc(dev, crtc) {
  13079. struct intel_initial_plane_config plane_config = {};
  13080. if (!crtc->active)
  13081. continue;
  13082. /*
  13083. * Note that reserving the BIOS fb up front prevents us
  13084. * from stuffing other stolen allocations like the ring
  13085. * on top. This prevents some ugliness at boot time, and
  13086. * can even allow for smooth boot transitions if the BIOS
  13087. * fb is large enough for the active pipe configuration.
  13088. */
  13089. dev_priv->display.get_initial_plane_config(crtc,
  13090. &plane_config);
  13091. /*
  13092. * If the fb is shared between multiple heads, we'll
  13093. * just get the first one.
  13094. */
  13095. intel_find_initial_plane_obj(crtc, &plane_config);
  13096. }
  13097. /*
  13098. * Make sure hardware watermarks really match the state we read out.
  13099. * Note that we need to do this after reconstructing the BIOS fb's
  13100. * since the watermark calculation done here will use pstate->fb.
  13101. */
  13102. sanitize_watermarks(dev);
  13103. }
  13104. static void intel_enable_pipe_a(struct drm_device *dev)
  13105. {
  13106. struct intel_connector *connector;
  13107. struct drm_connector *crt = NULL;
  13108. struct intel_load_detect_pipe load_detect_temp;
  13109. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13110. /* We can't just switch on the pipe A, we need to set things up with a
  13111. * proper mode and output configuration. As a gross hack, enable pipe A
  13112. * by enabling the load detect pipe once. */
  13113. for_each_intel_connector(dev, connector) {
  13114. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  13115. crt = &connector->base;
  13116. break;
  13117. }
  13118. }
  13119. if (!crt)
  13120. return;
  13121. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  13122. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  13123. }
  13124. static bool
  13125. intel_check_plane_mapping(struct intel_crtc *crtc)
  13126. {
  13127. struct drm_device *dev = crtc->base.dev;
  13128. struct drm_i915_private *dev_priv = dev->dev_private;
  13129. u32 val;
  13130. if (INTEL_INFO(dev)->num_pipes == 1)
  13131. return true;
  13132. val = I915_READ(DSPCNTR(!crtc->plane));
  13133. if ((val & DISPLAY_PLANE_ENABLE) &&
  13134. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13135. return false;
  13136. return true;
  13137. }
  13138. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13139. {
  13140. struct drm_device *dev = crtc->base.dev;
  13141. struct intel_encoder *encoder;
  13142. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13143. return true;
  13144. return false;
  13145. }
  13146. static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
  13147. {
  13148. struct drm_device *dev = encoder->base.dev;
  13149. struct intel_connector *connector;
  13150. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13151. return true;
  13152. return false;
  13153. }
  13154. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13155. {
  13156. struct drm_device *dev = crtc->base.dev;
  13157. struct drm_i915_private *dev_priv = dev->dev_private;
  13158. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13159. /* Clear any frame start delays used for debugging left by the BIOS */
  13160. if (!transcoder_is_dsi(cpu_transcoder)) {
  13161. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13162. I915_WRITE(reg,
  13163. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13164. }
  13165. /* restore vblank interrupts to correct state */
  13166. drm_crtc_vblank_reset(&crtc->base);
  13167. if (crtc->active) {
  13168. struct intel_plane *plane;
  13169. drm_crtc_vblank_on(&crtc->base);
  13170. /* Disable everything but the primary plane */
  13171. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13172. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13173. continue;
  13174. plane->disable_plane(&plane->base, &crtc->base);
  13175. }
  13176. }
  13177. /* We need to sanitize the plane -> pipe mapping first because this will
  13178. * disable the crtc (and hence change the state) if it is wrong. Note
  13179. * that gen4+ has a fixed plane -> pipe mapping. */
  13180. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  13181. bool plane;
  13182. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  13183. crtc->base.base.id, crtc->base.name);
  13184. /* Pipe has the wrong plane attached and the plane is active.
  13185. * Temporarily change the plane mapping and disable everything
  13186. * ... */
  13187. plane = crtc->plane;
  13188. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  13189. crtc->plane = !plane;
  13190. intel_crtc_disable_noatomic(&crtc->base);
  13191. crtc->plane = plane;
  13192. }
  13193. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13194. crtc->pipe == PIPE_A && !crtc->active) {
  13195. /* BIOS forgot to enable pipe A, this mostly happens after
  13196. * resume. Force-enable the pipe to fix this, the update_dpms
  13197. * call below we restore the pipe to the right state, but leave
  13198. * the required bits on. */
  13199. intel_enable_pipe_a(dev);
  13200. }
  13201. /* Adjust the state of the output pipe according to whether we
  13202. * have active connectors/encoders. */
  13203. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13204. intel_crtc_disable_noatomic(&crtc->base);
  13205. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  13206. /*
  13207. * We start out with underrun reporting disabled to avoid races.
  13208. * For correct bookkeeping mark this on active crtcs.
  13209. *
  13210. * Also on gmch platforms we dont have any hardware bits to
  13211. * disable the underrun reporting. Which means we need to start
  13212. * out with underrun reporting disabled also on inactive pipes,
  13213. * since otherwise we'll complain about the garbage we read when
  13214. * e.g. coming up after runtime pm.
  13215. *
  13216. * No protection against concurrent access is required - at
  13217. * worst a fifo underrun happens which also sets this to false.
  13218. */
  13219. crtc->cpu_fifo_underrun_disabled = true;
  13220. crtc->pch_fifo_underrun_disabled = true;
  13221. }
  13222. }
  13223. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13224. {
  13225. struct intel_connector *connector;
  13226. struct drm_device *dev = encoder->base.dev;
  13227. /* We need to check both for a crtc link (meaning that the
  13228. * encoder is active and trying to read from a pipe) and the
  13229. * pipe itself being active. */
  13230. bool has_active_crtc = encoder->base.crtc &&
  13231. to_intel_crtc(encoder->base.crtc)->active;
  13232. if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
  13233. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13234. encoder->base.base.id,
  13235. encoder->base.name);
  13236. /* Connector is active, but has no active pipe. This is
  13237. * fallout from our resume register restoring. Disable
  13238. * the encoder manually again. */
  13239. if (encoder->base.crtc) {
  13240. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13241. encoder->base.base.id,
  13242. encoder->base.name);
  13243. encoder->disable(encoder);
  13244. if (encoder->post_disable)
  13245. encoder->post_disable(encoder);
  13246. }
  13247. encoder->base.crtc = NULL;
  13248. /* Inconsistent output/port/pipe state happens presumably due to
  13249. * a bug in one of the get_hw_state functions. Or someplace else
  13250. * in our code, like the register restore mess on resume. Clamp
  13251. * things to off as a safer default. */
  13252. for_each_intel_connector(dev, connector) {
  13253. if (connector->encoder != encoder)
  13254. continue;
  13255. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13256. connector->base.encoder = NULL;
  13257. }
  13258. }
  13259. /* Enabled encoders without active connectors will be fixed in
  13260. * the crtc fixup. */
  13261. }
  13262. void i915_redisable_vga_power_on(struct drm_device *dev)
  13263. {
  13264. struct drm_i915_private *dev_priv = dev->dev_private;
  13265. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13266. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13267. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13268. i915_disable_vga(dev);
  13269. }
  13270. }
  13271. void i915_redisable_vga(struct drm_device *dev)
  13272. {
  13273. struct drm_i915_private *dev_priv = dev->dev_private;
  13274. /* This function can be called both from intel_modeset_setup_hw_state or
  13275. * at a very early point in our resume sequence, where the power well
  13276. * structures are not yet restored. Since this function is at a very
  13277. * paranoid "someone might have enabled VGA while we were not looking"
  13278. * level, just check if the power well is enabled instead of trying to
  13279. * follow the "don't touch the power well if we don't need it" policy
  13280. * the rest of the driver uses. */
  13281. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  13282. return;
  13283. i915_redisable_vga_power_on(dev);
  13284. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  13285. }
  13286. static bool primary_get_hw_state(struct intel_plane *plane)
  13287. {
  13288. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  13289. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  13290. }
  13291. /* FIXME read out full plane state for all planes */
  13292. static void readout_plane_state(struct intel_crtc *crtc)
  13293. {
  13294. struct drm_plane *primary = crtc->base.primary;
  13295. struct intel_plane_state *plane_state =
  13296. to_intel_plane_state(primary->state);
  13297. plane_state->visible = crtc->active &&
  13298. primary_get_hw_state(to_intel_plane(primary));
  13299. if (plane_state->visible)
  13300. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  13301. }
  13302. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13303. {
  13304. struct drm_i915_private *dev_priv = dev->dev_private;
  13305. enum pipe pipe;
  13306. struct intel_crtc *crtc;
  13307. struct intel_encoder *encoder;
  13308. struct intel_connector *connector;
  13309. int i;
  13310. dev_priv->active_crtcs = 0;
  13311. for_each_intel_crtc(dev, crtc) {
  13312. struct intel_crtc_state *crtc_state = crtc->config;
  13313. int pixclk = 0;
  13314. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  13315. memset(crtc_state, 0, sizeof(*crtc_state));
  13316. crtc_state->base.crtc = &crtc->base;
  13317. crtc_state->base.active = crtc_state->base.enable =
  13318. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13319. crtc->base.enabled = crtc_state->base.enable;
  13320. crtc->active = crtc_state->base.active;
  13321. if (crtc_state->base.active) {
  13322. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13323. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  13324. pixclk = ilk_pipe_pixel_rate(crtc_state);
  13325. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13326. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  13327. else
  13328. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  13329. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  13330. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  13331. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  13332. }
  13333. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  13334. readout_plane_state(crtc);
  13335. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  13336. crtc->base.base.id, crtc->base.name,
  13337. crtc->active ? "enabled" : "disabled");
  13338. }
  13339. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13340. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13341. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  13342. &pll->config.hw_state);
  13343. pll->config.crtc_mask = 0;
  13344. for_each_intel_crtc(dev, crtc) {
  13345. if (crtc->active && crtc->config->shared_dpll == pll)
  13346. pll->config.crtc_mask |= 1 << crtc->pipe;
  13347. }
  13348. pll->active_mask = pll->config.crtc_mask;
  13349. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13350. pll->name, pll->config.crtc_mask, pll->on);
  13351. }
  13352. for_each_intel_encoder(dev, encoder) {
  13353. pipe = 0;
  13354. if (encoder->get_hw_state(encoder, &pipe)) {
  13355. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13356. encoder->base.crtc = &crtc->base;
  13357. encoder->get_config(encoder, crtc->config);
  13358. } else {
  13359. encoder->base.crtc = NULL;
  13360. }
  13361. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13362. encoder->base.base.id,
  13363. encoder->base.name,
  13364. encoder->base.crtc ? "enabled" : "disabled",
  13365. pipe_name(pipe));
  13366. }
  13367. for_each_intel_connector(dev, connector) {
  13368. if (connector->get_hw_state(connector)) {
  13369. connector->base.dpms = DRM_MODE_DPMS_ON;
  13370. encoder = connector->encoder;
  13371. connector->base.encoder = &encoder->base;
  13372. if (encoder->base.crtc &&
  13373. encoder->base.crtc->state->active) {
  13374. /*
  13375. * This has to be done during hardware readout
  13376. * because anything calling .crtc_disable may
  13377. * rely on the connector_mask being accurate.
  13378. */
  13379. encoder->base.crtc->state->connector_mask |=
  13380. 1 << drm_connector_index(&connector->base);
  13381. encoder->base.crtc->state->encoder_mask |=
  13382. 1 << drm_encoder_index(&encoder->base);
  13383. }
  13384. } else {
  13385. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13386. connector->base.encoder = NULL;
  13387. }
  13388. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13389. connector->base.base.id,
  13390. connector->base.name,
  13391. connector->base.encoder ? "enabled" : "disabled");
  13392. }
  13393. for_each_intel_crtc(dev, crtc) {
  13394. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  13395. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13396. if (crtc->base.state->active) {
  13397. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  13398. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  13399. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13400. /*
  13401. * The initial mode needs to be set in order to keep
  13402. * the atomic core happy. It wants a valid mode if the
  13403. * crtc's enabled, so we do the above call.
  13404. *
  13405. * At this point some state updated by the connectors
  13406. * in their ->detect() callback has not run yet, so
  13407. * no recalculation can be done yet.
  13408. *
  13409. * Even if we could do a recalculation and modeset
  13410. * right now it would cause a double modeset if
  13411. * fbdev or userspace chooses a different initial mode.
  13412. *
  13413. * If that happens, someone indicated they wanted a
  13414. * mode change, which means it's safe to do a full
  13415. * recalculation.
  13416. */
  13417. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  13418. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  13419. update_scanline_offset(crtc);
  13420. }
  13421. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  13422. }
  13423. }
  13424. /* Scan out the current hw modeset state,
  13425. * and sanitizes it to the current state
  13426. */
  13427. static void
  13428. intel_modeset_setup_hw_state(struct drm_device *dev)
  13429. {
  13430. struct drm_i915_private *dev_priv = dev->dev_private;
  13431. enum pipe pipe;
  13432. struct intel_crtc *crtc;
  13433. struct intel_encoder *encoder;
  13434. int i;
  13435. intel_modeset_readout_hw_state(dev);
  13436. /* HW state is read out, now we need to sanitize this mess. */
  13437. for_each_intel_encoder(dev, encoder) {
  13438. intel_sanitize_encoder(encoder);
  13439. }
  13440. for_each_pipe(dev_priv, pipe) {
  13441. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13442. intel_sanitize_crtc(crtc);
  13443. intel_dump_pipe_config(crtc, crtc->config,
  13444. "[setup_hw_state]");
  13445. }
  13446. intel_modeset_update_connector_atomic_state(dev);
  13447. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13448. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13449. if (!pll->on || pll->active_mask)
  13450. continue;
  13451. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13452. pll->funcs.disable(dev_priv, pll);
  13453. pll->on = false;
  13454. }
  13455. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  13456. vlv_wm_get_hw_state(dev);
  13457. else if (IS_GEN9(dev))
  13458. skl_wm_get_hw_state(dev);
  13459. else if (HAS_PCH_SPLIT(dev))
  13460. ilk_wm_get_hw_state(dev);
  13461. for_each_intel_crtc(dev, crtc) {
  13462. unsigned long put_domains;
  13463. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13464. if (WARN_ON(put_domains))
  13465. modeset_put_power_domains(dev_priv, put_domains);
  13466. }
  13467. intel_display_set_init_power(dev_priv, false);
  13468. intel_fbc_init_pipe_state(dev_priv);
  13469. }
  13470. void intel_display_resume(struct drm_device *dev)
  13471. {
  13472. struct drm_i915_private *dev_priv = to_i915(dev);
  13473. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13474. struct drm_modeset_acquire_ctx ctx;
  13475. int ret;
  13476. bool setup = false;
  13477. dev_priv->modeset_restore_state = NULL;
  13478. /*
  13479. * This is a cludge because with real atomic modeset mode_config.mutex
  13480. * won't be taken. Unfortunately some probed state like
  13481. * audio_codec_enable is still protected by mode_config.mutex, so lock
  13482. * it here for now.
  13483. */
  13484. mutex_lock(&dev->mode_config.mutex);
  13485. drm_modeset_acquire_init(&ctx, 0);
  13486. retry:
  13487. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13488. if (ret == 0 && !setup) {
  13489. setup = true;
  13490. intel_modeset_setup_hw_state(dev);
  13491. i915_redisable_vga(dev);
  13492. }
  13493. if (ret == 0 && state) {
  13494. struct drm_crtc_state *crtc_state;
  13495. struct drm_crtc *crtc;
  13496. int i;
  13497. state->acquire_ctx = &ctx;
  13498. /* ignore any reset values/BIOS leftovers in the WM registers */
  13499. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13500. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  13501. /*
  13502. * Force recalculation even if we restore
  13503. * current state. With fast modeset this may not result
  13504. * in a modeset when the state is compatible.
  13505. */
  13506. crtc_state->mode_changed = true;
  13507. }
  13508. ret = drm_atomic_commit(state);
  13509. }
  13510. if (ret == -EDEADLK) {
  13511. drm_modeset_backoff(&ctx);
  13512. goto retry;
  13513. }
  13514. drm_modeset_drop_locks(&ctx);
  13515. drm_modeset_acquire_fini(&ctx);
  13516. mutex_unlock(&dev->mode_config.mutex);
  13517. if (ret) {
  13518. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13519. drm_atomic_state_free(state);
  13520. }
  13521. }
  13522. void intel_modeset_gem_init(struct drm_device *dev)
  13523. {
  13524. struct drm_i915_private *dev_priv = to_i915(dev);
  13525. struct drm_crtc *c;
  13526. struct drm_i915_gem_object *obj;
  13527. int ret;
  13528. intel_init_gt_powersave(dev_priv);
  13529. intel_modeset_init_hw(dev);
  13530. intel_setup_overlay(dev_priv);
  13531. /*
  13532. * Make sure any fbs we allocated at startup are properly
  13533. * pinned & fenced. When we do the allocation it's too early
  13534. * for this.
  13535. */
  13536. for_each_crtc(dev, c) {
  13537. obj = intel_fb_obj(c->primary->fb);
  13538. if (obj == NULL)
  13539. continue;
  13540. mutex_lock(&dev->struct_mutex);
  13541. ret = intel_pin_and_fence_fb_obj(c->primary->fb,
  13542. c->primary->state->rotation);
  13543. mutex_unlock(&dev->struct_mutex);
  13544. if (ret) {
  13545. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13546. to_intel_crtc(c)->pipe);
  13547. drm_framebuffer_unreference(c->primary->fb);
  13548. c->primary->fb = NULL;
  13549. c->primary->crtc = c->primary->state->crtc = NULL;
  13550. update_state_fb(c->primary);
  13551. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13552. }
  13553. }
  13554. intel_backlight_register(dev);
  13555. }
  13556. void intel_connector_unregister(struct intel_connector *intel_connector)
  13557. {
  13558. struct drm_connector *connector = &intel_connector->base;
  13559. intel_panel_destroy_backlight(connector);
  13560. drm_connector_unregister(connector);
  13561. }
  13562. void intel_modeset_cleanup(struct drm_device *dev)
  13563. {
  13564. struct drm_i915_private *dev_priv = dev->dev_private;
  13565. struct intel_connector *connector;
  13566. intel_disable_gt_powersave(dev_priv);
  13567. intel_backlight_unregister(dev);
  13568. /*
  13569. * Interrupts and polling as the first thing to avoid creating havoc.
  13570. * Too much stuff here (turning of connectors, ...) would
  13571. * experience fancy races otherwise.
  13572. */
  13573. intel_irq_uninstall(dev_priv);
  13574. /*
  13575. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13576. * poll handlers. Hence disable polling after hpd handling is shut down.
  13577. */
  13578. drm_kms_helper_poll_fini(dev);
  13579. intel_unregister_dsm_handler();
  13580. intel_fbc_global_disable(dev_priv);
  13581. /* flush any delayed tasks or pending work */
  13582. flush_scheduled_work();
  13583. /* destroy the backlight and sysfs files before encoders/connectors */
  13584. for_each_intel_connector(dev, connector)
  13585. connector->unregister(connector);
  13586. drm_mode_config_cleanup(dev);
  13587. intel_cleanup_overlay(dev_priv);
  13588. intel_cleanup_gt_powersave(dev_priv);
  13589. intel_teardown_gmbus(dev);
  13590. }
  13591. /*
  13592. * Return which encoder is currently attached for connector.
  13593. */
  13594. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13595. {
  13596. return &intel_attached_encoder(connector)->base;
  13597. }
  13598. void intel_connector_attach_encoder(struct intel_connector *connector,
  13599. struct intel_encoder *encoder)
  13600. {
  13601. connector->encoder = encoder;
  13602. drm_mode_connector_attach_encoder(&connector->base,
  13603. &encoder->base);
  13604. }
  13605. /*
  13606. * set vga decode state - true == enable VGA decode
  13607. */
  13608. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13609. {
  13610. struct drm_i915_private *dev_priv = dev->dev_private;
  13611. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13612. u16 gmch_ctrl;
  13613. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13614. DRM_ERROR("failed to read control word\n");
  13615. return -EIO;
  13616. }
  13617. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13618. return 0;
  13619. if (state)
  13620. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13621. else
  13622. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13623. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13624. DRM_ERROR("failed to write control word\n");
  13625. return -EIO;
  13626. }
  13627. return 0;
  13628. }
  13629. struct intel_display_error_state {
  13630. u32 power_well_driver;
  13631. int num_transcoders;
  13632. struct intel_cursor_error_state {
  13633. u32 control;
  13634. u32 position;
  13635. u32 base;
  13636. u32 size;
  13637. } cursor[I915_MAX_PIPES];
  13638. struct intel_pipe_error_state {
  13639. bool power_domain_on;
  13640. u32 source;
  13641. u32 stat;
  13642. } pipe[I915_MAX_PIPES];
  13643. struct intel_plane_error_state {
  13644. u32 control;
  13645. u32 stride;
  13646. u32 size;
  13647. u32 pos;
  13648. u32 addr;
  13649. u32 surface;
  13650. u32 tile_offset;
  13651. } plane[I915_MAX_PIPES];
  13652. struct intel_transcoder_error_state {
  13653. bool power_domain_on;
  13654. enum transcoder cpu_transcoder;
  13655. u32 conf;
  13656. u32 htotal;
  13657. u32 hblank;
  13658. u32 hsync;
  13659. u32 vtotal;
  13660. u32 vblank;
  13661. u32 vsync;
  13662. } transcoder[4];
  13663. };
  13664. struct intel_display_error_state *
  13665. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13666. {
  13667. struct intel_display_error_state *error;
  13668. int transcoders[] = {
  13669. TRANSCODER_A,
  13670. TRANSCODER_B,
  13671. TRANSCODER_C,
  13672. TRANSCODER_EDP,
  13673. };
  13674. int i;
  13675. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13676. return NULL;
  13677. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13678. if (error == NULL)
  13679. return NULL;
  13680. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13681. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13682. for_each_pipe(dev_priv, i) {
  13683. error->pipe[i].power_domain_on =
  13684. __intel_display_power_is_enabled(dev_priv,
  13685. POWER_DOMAIN_PIPE(i));
  13686. if (!error->pipe[i].power_domain_on)
  13687. continue;
  13688. error->cursor[i].control = I915_READ(CURCNTR(i));
  13689. error->cursor[i].position = I915_READ(CURPOS(i));
  13690. error->cursor[i].base = I915_READ(CURBASE(i));
  13691. error->plane[i].control = I915_READ(DSPCNTR(i));
  13692. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13693. if (INTEL_GEN(dev_priv) <= 3) {
  13694. error->plane[i].size = I915_READ(DSPSIZE(i));
  13695. error->plane[i].pos = I915_READ(DSPPOS(i));
  13696. }
  13697. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13698. error->plane[i].addr = I915_READ(DSPADDR(i));
  13699. if (INTEL_GEN(dev_priv) >= 4) {
  13700. error->plane[i].surface = I915_READ(DSPSURF(i));
  13701. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13702. }
  13703. error->pipe[i].source = I915_READ(PIPESRC(i));
  13704. if (HAS_GMCH_DISPLAY(dev_priv))
  13705. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13706. }
  13707. /* Note: this does not include DSI transcoders. */
  13708. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13709. if (HAS_DDI(dev_priv))
  13710. error->num_transcoders++; /* Account for eDP. */
  13711. for (i = 0; i < error->num_transcoders; i++) {
  13712. enum transcoder cpu_transcoder = transcoders[i];
  13713. error->transcoder[i].power_domain_on =
  13714. __intel_display_power_is_enabled(dev_priv,
  13715. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13716. if (!error->transcoder[i].power_domain_on)
  13717. continue;
  13718. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13719. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13720. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13721. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13722. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13723. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13724. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13725. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13726. }
  13727. return error;
  13728. }
  13729. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13730. void
  13731. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13732. struct drm_device *dev,
  13733. struct intel_display_error_state *error)
  13734. {
  13735. struct drm_i915_private *dev_priv = dev->dev_private;
  13736. int i;
  13737. if (!error)
  13738. return;
  13739. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13740. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13741. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13742. error->power_well_driver);
  13743. for_each_pipe(dev_priv, i) {
  13744. err_printf(m, "Pipe [%d]:\n", i);
  13745. err_printf(m, " Power: %s\n",
  13746. onoff(error->pipe[i].power_domain_on));
  13747. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13748. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13749. err_printf(m, "Plane [%d]:\n", i);
  13750. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13751. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13752. if (INTEL_INFO(dev)->gen <= 3) {
  13753. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13754. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13755. }
  13756. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13757. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13758. if (INTEL_INFO(dev)->gen >= 4) {
  13759. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13760. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13761. }
  13762. err_printf(m, "Cursor [%d]:\n", i);
  13763. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13764. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13765. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13766. }
  13767. for (i = 0; i < error->num_transcoders; i++) {
  13768. err_printf(m, "CPU transcoder: %s\n",
  13769. transcoder_name(error->transcoder[i].cpu_transcoder));
  13770. err_printf(m, " Power: %s\n",
  13771. onoff(error->transcoder[i].power_domain_on));
  13772. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13773. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13774. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13775. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13776. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13777. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13778. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13779. }
  13780. }