intel_display.c 483 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "i915_gem_dmabuf.h"
  40. #include "intel_dsi.h"
  41. #include "i915_trace.h"
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_dp_helper.h>
  45. #include <drm/drm_crtc_helper.h>
  46. #include <drm/drm_plane_helper.h>
  47. #include <drm/drm_rect.h>
  48. #include <linux/dma_remapping.h>
  49. #include <linux/reservation.h>
  50. static bool is_mmio_work(struct intel_flip_work *work)
  51. {
  52. return work->mmio_work.func;
  53. }
  54. /* Primary plane formats for gen <= 3 */
  55. static const uint32_t i8xx_primary_formats[] = {
  56. DRM_FORMAT_C8,
  57. DRM_FORMAT_RGB565,
  58. DRM_FORMAT_XRGB1555,
  59. DRM_FORMAT_XRGB8888,
  60. };
  61. /* Primary plane formats for gen >= 4 */
  62. static const uint32_t i965_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_XRGB2101010,
  68. DRM_FORMAT_XBGR2101010,
  69. };
  70. static const uint32_t skl_primary_formats[] = {
  71. DRM_FORMAT_C8,
  72. DRM_FORMAT_RGB565,
  73. DRM_FORMAT_XRGB8888,
  74. DRM_FORMAT_XBGR8888,
  75. DRM_FORMAT_ARGB8888,
  76. DRM_FORMAT_ABGR8888,
  77. DRM_FORMAT_XRGB2101010,
  78. DRM_FORMAT_XBGR2101010,
  79. DRM_FORMAT_YUYV,
  80. DRM_FORMAT_YVYU,
  81. DRM_FORMAT_UYVY,
  82. DRM_FORMAT_VYUY,
  83. };
  84. /* Cursor formats */
  85. static const uint32_t intel_cursor_formats[] = {
  86. DRM_FORMAT_ARGB8888,
  87. };
  88. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  89. struct intel_crtc_state *pipe_config);
  90. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  91. struct intel_crtc_state *pipe_config);
  92. static int intel_framebuffer_init(struct drm_device *dev,
  93. struct intel_framebuffer *ifb,
  94. struct drm_mode_fb_cmd2 *mode_cmd,
  95. struct drm_i915_gem_object *obj);
  96. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  98. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  99. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  100. struct intel_link_m_n *m_n,
  101. struct intel_link_m_n *m2_n2);
  102. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  104. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  105. static void vlv_prepare_pll(struct intel_crtc *crtc,
  106. const struct intel_crtc_state *pipe_config);
  107. static void chv_prepare_pll(struct intel_crtc *crtc,
  108. const struct intel_crtc_state *pipe_config);
  109. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  111. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  112. struct intel_crtc_state *crtc_state);
  113. static void skylake_pfit_enable(struct intel_crtc *crtc);
  114. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  115. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  116. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  117. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  118. static int ilk_max_pixel_rate(struct drm_atomic_state *state);
  119. static int bxt_calc_cdclk(int max_pixclk);
  120. struct intel_limit {
  121. struct {
  122. int min, max;
  123. } dot, vco, n, m, m1, m2, p, p1;
  124. struct {
  125. int dot_limit;
  126. int p2_slow, p2_fast;
  127. } p2;
  128. };
  129. /* returns HPLL frequency in kHz */
  130. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  131. {
  132. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  133. /* Obtain SKU information */
  134. mutex_lock(&dev_priv->sb_lock);
  135. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  136. CCK_FUSE_HPLL_FREQ_MASK;
  137. mutex_unlock(&dev_priv->sb_lock);
  138. return vco_freq[hpll_freq] * 1000;
  139. }
  140. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  141. const char *name, u32 reg, int ref_freq)
  142. {
  143. u32 val;
  144. int divider;
  145. mutex_lock(&dev_priv->sb_lock);
  146. val = vlv_cck_read(dev_priv, reg);
  147. mutex_unlock(&dev_priv->sb_lock);
  148. divider = val & CCK_FREQUENCY_VALUES;
  149. WARN((val & CCK_FREQUENCY_STATUS) !=
  150. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  151. "%s change in progress\n", name);
  152. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  153. }
  154. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  155. const char *name, u32 reg)
  156. {
  157. if (dev_priv->hpll_freq == 0)
  158. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  159. return vlv_get_cck_clock(dev_priv, name, reg,
  160. dev_priv->hpll_freq);
  161. }
  162. static int
  163. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  164. {
  165. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  166. }
  167. static int
  168. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  169. {
  170. /* RAWCLK_FREQ_VLV register updated from power well code */
  171. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  172. CCK_DISPLAY_REF_CLOCK_CONTROL);
  173. }
  174. static int
  175. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  176. {
  177. uint32_t clkcfg;
  178. /* hrawclock is 1/4 the FSB frequency */
  179. clkcfg = I915_READ(CLKCFG);
  180. switch (clkcfg & CLKCFG_FSB_MASK) {
  181. case CLKCFG_FSB_400:
  182. return 100000;
  183. case CLKCFG_FSB_533:
  184. return 133333;
  185. case CLKCFG_FSB_667:
  186. return 166667;
  187. case CLKCFG_FSB_800:
  188. return 200000;
  189. case CLKCFG_FSB_1067:
  190. return 266667;
  191. case CLKCFG_FSB_1333:
  192. return 333333;
  193. /* these two are just a guess; one of them might be right */
  194. case CLKCFG_FSB_1600:
  195. case CLKCFG_FSB_1600_ALT:
  196. return 400000;
  197. default:
  198. return 133333;
  199. }
  200. }
  201. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  202. {
  203. if (HAS_PCH_SPLIT(dev_priv))
  204. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  205. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  206. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  207. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  208. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  209. else
  210. return; /* no rawclk on other platforms, or no need to know it */
  211. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  212. }
  213. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  214. {
  215. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  216. return;
  217. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  218. CCK_CZ_CLOCK_CONTROL);
  219. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  220. }
  221. static inline u32 /* units of 100MHz */
  222. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  223. const struct intel_crtc_state *pipe_config)
  224. {
  225. if (HAS_DDI(dev_priv))
  226. return pipe_config->port_clock; /* SPLL */
  227. else if (IS_GEN5(dev_priv))
  228. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  229. else
  230. return 270000;
  231. }
  232. static const struct intel_limit intel_limits_i8xx_dac = {
  233. .dot = { .min = 25000, .max = 350000 },
  234. .vco = { .min = 908000, .max = 1512000 },
  235. .n = { .min = 2, .max = 16 },
  236. .m = { .min = 96, .max = 140 },
  237. .m1 = { .min = 18, .max = 26 },
  238. .m2 = { .min = 6, .max = 16 },
  239. .p = { .min = 4, .max = 128 },
  240. .p1 = { .min = 2, .max = 33 },
  241. .p2 = { .dot_limit = 165000,
  242. .p2_slow = 4, .p2_fast = 2 },
  243. };
  244. static const struct intel_limit intel_limits_i8xx_dvo = {
  245. .dot = { .min = 25000, .max = 350000 },
  246. .vco = { .min = 908000, .max = 1512000 },
  247. .n = { .min = 2, .max = 16 },
  248. .m = { .min = 96, .max = 140 },
  249. .m1 = { .min = 18, .max = 26 },
  250. .m2 = { .min = 6, .max = 16 },
  251. .p = { .min = 4, .max = 128 },
  252. .p1 = { .min = 2, .max = 33 },
  253. .p2 = { .dot_limit = 165000,
  254. .p2_slow = 4, .p2_fast = 4 },
  255. };
  256. static const struct intel_limit intel_limits_i8xx_lvds = {
  257. .dot = { .min = 25000, .max = 350000 },
  258. .vco = { .min = 908000, .max = 1512000 },
  259. .n = { .min = 2, .max = 16 },
  260. .m = { .min = 96, .max = 140 },
  261. .m1 = { .min = 18, .max = 26 },
  262. .m2 = { .min = 6, .max = 16 },
  263. .p = { .min = 4, .max = 128 },
  264. .p1 = { .min = 1, .max = 6 },
  265. .p2 = { .dot_limit = 165000,
  266. .p2_slow = 14, .p2_fast = 7 },
  267. };
  268. static const struct intel_limit intel_limits_i9xx_sdvo = {
  269. .dot = { .min = 20000, .max = 400000 },
  270. .vco = { .min = 1400000, .max = 2800000 },
  271. .n = { .min = 1, .max = 6 },
  272. .m = { .min = 70, .max = 120 },
  273. .m1 = { .min = 8, .max = 18 },
  274. .m2 = { .min = 3, .max = 7 },
  275. .p = { .min = 5, .max = 80 },
  276. .p1 = { .min = 1, .max = 8 },
  277. .p2 = { .dot_limit = 200000,
  278. .p2_slow = 10, .p2_fast = 5 },
  279. };
  280. static const struct intel_limit intel_limits_i9xx_lvds = {
  281. .dot = { .min = 20000, .max = 400000 },
  282. .vco = { .min = 1400000, .max = 2800000 },
  283. .n = { .min = 1, .max = 6 },
  284. .m = { .min = 70, .max = 120 },
  285. .m1 = { .min = 8, .max = 18 },
  286. .m2 = { .min = 3, .max = 7 },
  287. .p = { .min = 7, .max = 98 },
  288. .p1 = { .min = 1, .max = 8 },
  289. .p2 = { .dot_limit = 112000,
  290. .p2_slow = 14, .p2_fast = 7 },
  291. };
  292. static const struct intel_limit intel_limits_g4x_sdvo = {
  293. .dot = { .min = 25000, .max = 270000 },
  294. .vco = { .min = 1750000, .max = 3500000},
  295. .n = { .min = 1, .max = 4 },
  296. .m = { .min = 104, .max = 138 },
  297. .m1 = { .min = 17, .max = 23 },
  298. .m2 = { .min = 5, .max = 11 },
  299. .p = { .min = 10, .max = 30 },
  300. .p1 = { .min = 1, .max = 3},
  301. .p2 = { .dot_limit = 270000,
  302. .p2_slow = 10,
  303. .p2_fast = 10
  304. },
  305. };
  306. static const struct intel_limit intel_limits_g4x_hdmi = {
  307. .dot = { .min = 22000, .max = 400000 },
  308. .vco = { .min = 1750000, .max = 3500000},
  309. .n = { .min = 1, .max = 4 },
  310. .m = { .min = 104, .max = 138 },
  311. .m1 = { .min = 16, .max = 23 },
  312. .m2 = { .min = 5, .max = 11 },
  313. .p = { .min = 5, .max = 80 },
  314. .p1 = { .min = 1, .max = 8},
  315. .p2 = { .dot_limit = 165000,
  316. .p2_slow = 10, .p2_fast = 5 },
  317. };
  318. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  319. .dot = { .min = 20000, .max = 115000 },
  320. .vco = { .min = 1750000, .max = 3500000 },
  321. .n = { .min = 1, .max = 3 },
  322. .m = { .min = 104, .max = 138 },
  323. .m1 = { .min = 17, .max = 23 },
  324. .m2 = { .min = 5, .max = 11 },
  325. .p = { .min = 28, .max = 112 },
  326. .p1 = { .min = 2, .max = 8 },
  327. .p2 = { .dot_limit = 0,
  328. .p2_slow = 14, .p2_fast = 14
  329. },
  330. };
  331. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  332. .dot = { .min = 80000, .max = 224000 },
  333. .vco = { .min = 1750000, .max = 3500000 },
  334. .n = { .min = 1, .max = 3 },
  335. .m = { .min = 104, .max = 138 },
  336. .m1 = { .min = 17, .max = 23 },
  337. .m2 = { .min = 5, .max = 11 },
  338. .p = { .min = 14, .max = 42 },
  339. .p1 = { .min = 2, .max = 6 },
  340. .p2 = { .dot_limit = 0,
  341. .p2_slow = 7, .p2_fast = 7
  342. },
  343. };
  344. static const struct intel_limit intel_limits_pineview_sdvo = {
  345. .dot = { .min = 20000, .max = 400000},
  346. .vco = { .min = 1700000, .max = 3500000 },
  347. /* Pineview's Ncounter is a ring counter */
  348. .n = { .min = 3, .max = 6 },
  349. .m = { .min = 2, .max = 256 },
  350. /* Pineview only has one combined m divider, which we treat as m2. */
  351. .m1 = { .min = 0, .max = 0 },
  352. .m2 = { .min = 0, .max = 254 },
  353. .p = { .min = 5, .max = 80 },
  354. .p1 = { .min = 1, .max = 8 },
  355. .p2 = { .dot_limit = 200000,
  356. .p2_slow = 10, .p2_fast = 5 },
  357. };
  358. static const struct intel_limit intel_limits_pineview_lvds = {
  359. .dot = { .min = 20000, .max = 400000 },
  360. .vco = { .min = 1700000, .max = 3500000 },
  361. .n = { .min = 3, .max = 6 },
  362. .m = { .min = 2, .max = 256 },
  363. .m1 = { .min = 0, .max = 0 },
  364. .m2 = { .min = 0, .max = 254 },
  365. .p = { .min = 7, .max = 112 },
  366. .p1 = { .min = 1, .max = 8 },
  367. .p2 = { .dot_limit = 112000,
  368. .p2_slow = 14, .p2_fast = 14 },
  369. };
  370. /* Ironlake / Sandybridge
  371. *
  372. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  373. * the range value for them is (actual_value - 2).
  374. */
  375. static const struct intel_limit intel_limits_ironlake_dac = {
  376. .dot = { .min = 25000, .max = 350000 },
  377. .vco = { .min = 1760000, .max = 3510000 },
  378. .n = { .min = 1, .max = 5 },
  379. .m = { .min = 79, .max = 127 },
  380. .m1 = { .min = 12, .max = 22 },
  381. .m2 = { .min = 5, .max = 9 },
  382. .p = { .min = 5, .max = 80 },
  383. .p1 = { .min = 1, .max = 8 },
  384. .p2 = { .dot_limit = 225000,
  385. .p2_slow = 10, .p2_fast = 5 },
  386. };
  387. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  388. .dot = { .min = 25000, .max = 350000 },
  389. .vco = { .min = 1760000, .max = 3510000 },
  390. .n = { .min = 1, .max = 3 },
  391. .m = { .min = 79, .max = 118 },
  392. .m1 = { .min = 12, .max = 22 },
  393. .m2 = { .min = 5, .max = 9 },
  394. .p = { .min = 28, .max = 112 },
  395. .p1 = { .min = 2, .max = 8 },
  396. .p2 = { .dot_limit = 225000,
  397. .p2_slow = 14, .p2_fast = 14 },
  398. };
  399. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  400. .dot = { .min = 25000, .max = 350000 },
  401. .vco = { .min = 1760000, .max = 3510000 },
  402. .n = { .min = 1, .max = 3 },
  403. .m = { .min = 79, .max = 127 },
  404. .m1 = { .min = 12, .max = 22 },
  405. .m2 = { .min = 5, .max = 9 },
  406. .p = { .min = 14, .max = 56 },
  407. .p1 = { .min = 2, .max = 8 },
  408. .p2 = { .dot_limit = 225000,
  409. .p2_slow = 7, .p2_fast = 7 },
  410. };
  411. /* LVDS 100mhz refclk limits. */
  412. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  413. .dot = { .min = 25000, .max = 350000 },
  414. .vco = { .min = 1760000, .max = 3510000 },
  415. .n = { .min = 1, .max = 2 },
  416. .m = { .min = 79, .max = 126 },
  417. .m1 = { .min = 12, .max = 22 },
  418. .m2 = { .min = 5, .max = 9 },
  419. .p = { .min = 28, .max = 112 },
  420. .p1 = { .min = 2, .max = 8 },
  421. .p2 = { .dot_limit = 225000,
  422. .p2_slow = 14, .p2_fast = 14 },
  423. };
  424. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  425. .dot = { .min = 25000, .max = 350000 },
  426. .vco = { .min = 1760000, .max = 3510000 },
  427. .n = { .min = 1, .max = 3 },
  428. .m = { .min = 79, .max = 126 },
  429. .m1 = { .min = 12, .max = 22 },
  430. .m2 = { .min = 5, .max = 9 },
  431. .p = { .min = 14, .max = 42 },
  432. .p1 = { .min = 2, .max = 6 },
  433. .p2 = { .dot_limit = 225000,
  434. .p2_slow = 7, .p2_fast = 7 },
  435. };
  436. static const struct intel_limit intel_limits_vlv = {
  437. /*
  438. * These are the data rate limits (measured in fast clocks)
  439. * since those are the strictest limits we have. The fast
  440. * clock and actual rate limits are more relaxed, so checking
  441. * them would make no difference.
  442. */
  443. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  444. .vco = { .min = 4000000, .max = 6000000 },
  445. .n = { .min = 1, .max = 7 },
  446. .m1 = { .min = 2, .max = 3 },
  447. .m2 = { .min = 11, .max = 156 },
  448. .p1 = { .min = 2, .max = 3 },
  449. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  450. };
  451. static const struct intel_limit intel_limits_chv = {
  452. /*
  453. * These are the data rate limits (measured in fast clocks)
  454. * since those are the strictest limits we have. The fast
  455. * clock and actual rate limits are more relaxed, so checking
  456. * them would make no difference.
  457. */
  458. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  459. .vco = { .min = 4800000, .max = 6480000 },
  460. .n = { .min = 1, .max = 1 },
  461. .m1 = { .min = 2, .max = 2 },
  462. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  463. .p1 = { .min = 2, .max = 4 },
  464. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  465. };
  466. static const struct intel_limit intel_limits_bxt = {
  467. /* FIXME: find real dot limits */
  468. .dot = { .min = 0, .max = INT_MAX },
  469. .vco = { .min = 4800000, .max = 6700000 },
  470. .n = { .min = 1, .max = 1 },
  471. .m1 = { .min = 2, .max = 2 },
  472. /* FIXME: find real m2 limits */
  473. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  474. .p1 = { .min = 2, .max = 4 },
  475. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  476. };
  477. static bool
  478. needs_modeset(struct drm_crtc_state *state)
  479. {
  480. return drm_atomic_crtc_needs_modeset(state);
  481. }
  482. /*
  483. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  484. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  485. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  486. * The helpers' return value is the rate of the clock that is fed to the
  487. * display engine's pipe which can be the above fast dot clock rate or a
  488. * divided-down version of it.
  489. */
  490. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  491. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  492. {
  493. clock->m = clock->m2 + 2;
  494. clock->p = clock->p1 * clock->p2;
  495. if (WARN_ON(clock->n == 0 || clock->p == 0))
  496. return 0;
  497. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  498. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  499. return clock->dot;
  500. }
  501. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  502. {
  503. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  504. }
  505. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  506. {
  507. clock->m = i9xx_dpll_compute_m(clock);
  508. clock->p = clock->p1 * clock->p2;
  509. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  510. return 0;
  511. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  512. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  513. return clock->dot;
  514. }
  515. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  516. {
  517. clock->m = clock->m1 * clock->m2;
  518. clock->p = clock->p1 * clock->p2;
  519. if (WARN_ON(clock->n == 0 || clock->p == 0))
  520. return 0;
  521. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  522. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  523. return clock->dot / 5;
  524. }
  525. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  526. {
  527. clock->m = clock->m1 * clock->m2;
  528. clock->p = clock->p1 * clock->p2;
  529. if (WARN_ON(clock->n == 0 || clock->p == 0))
  530. return 0;
  531. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  532. clock->n << 22);
  533. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  534. return clock->dot / 5;
  535. }
  536. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  537. /**
  538. * Returns whether the given set of divisors are valid for a given refclk with
  539. * the given connectors.
  540. */
  541. static bool intel_PLL_is_valid(struct drm_device *dev,
  542. const struct intel_limit *limit,
  543. const struct dpll *clock)
  544. {
  545. if (clock->n < limit->n.min || limit->n.max < clock->n)
  546. INTELPllInvalid("n out of range\n");
  547. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  548. INTELPllInvalid("p1 out of range\n");
  549. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  550. INTELPllInvalid("m2 out of range\n");
  551. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  552. INTELPllInvalid("m1 out of range\n");
  553. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
  554. !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
  555. if (clock->m1 <= clock->m2)
  556. INTELPllInvalid("m1 <= m2\n");
  557. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
  558. if (clock->p < limit->p.min || limit->p.max < clock->p)
  559. INTELPllInvalid("p out of range\n");
  560. if (clock->m < limit->m.min || limit->m.max < clock->m)
  561. INTELPllInvalid("m out of range\n");
  562. }
  563. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  564. INTELPllInvalid("vco out of range\n");
  565. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  566. * connector, etc., rather than just a single range.
  567. */
  568. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  569. INTELPllInvalid("dot out of range\n");
  570. return true;
  571. }
  572. static int
  573. i9xx_select_p2_div(const struct intel_limit *limit,
  574. const struct intel_crtc_state *crtc_state,
  575. int target)
  576. {
  577. struct drm_device *dev = crtc_state->base.crtc->dev;
  578. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  579. /*
  580. * For LVDS just rely on its current settings for dual-channel.
  581. * We haven't figured out how to reliably set up different
  582. * single/dual channel state, if we even can.
  583. */
  584. if (intel_is_dual_link_lvds(dev))
  585. return limit->p2.p2_fast;
  586. else
  587. return limit->p2.p2_slow;
  588. } else {
  589. if (target < limit->p2.dot_limit)
  590. return limit->p2.p2_slow;
  591. else
  592. return limit->p2.p2_fast;
  593. }
  594. }
  595. /*
  596. * Returns a set of divisors for the desired target clock with the given
  597. * refclk, or FALSE. The returned values represent the clock equation:
  598. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  599. *
  600. * Target and reference clocks are specified in kHz.
  601. *
  602. * If match_clock is provided, then best_clock P divider must match the P
  603. * divider from @match_clock used for LVDS downclocking.
  604. */
  605. static bool
  606. i9xx_find_best_dpll(const struct intel_limit *limit,
  607. struct intel_crtc_state *crtc_state,
  608. int target, int refclk, struct dpll *match_clock,
  609. struct dpll *best_clock)
  610. {
  611. struct drm_device *dev = crtc_state->base.crtc->dev;
  612. struct dpll clock;
  613. int err = target;
  614. memset(best_clock, 0, sizeof(*best_clock));
  615. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  616. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  617. clock.m1++) {
  618. for (clock.m2 = limit->m2.min;
  619. clock.m2 <= limit->m2.max; clock.m2++) {
  620. if (clock.m2 >= clock.m1)
  621. break;
  622. for (clock.n = limit->n.min;
  623. clock.n <= limit->n.max; clock.n++) {
  624. for (clock.p1 = limit->p1.min;
  625. clock.p1 <= limit->p1.max; clock.p1++) {
  626. int this_err;
  627. i9xx_calc_dpll_params(refclk, &clock);
  628. if (!intel_PLL_is_valid(dev, limit,
  629. &clock))
  630. continue;
  631. if (match_clock &&
  632. clock.p != match_clock->p)
  633. continue;
  634. this_err = abs(clock.dot - target);
  635. if (this_err < err) {
  636. *best_clock = clock;
  637. err = this_err;
  638. }
  639. }
  640. }
  641. }
  642. }
  643. return (err != target);
  644. }
  645. /*
  646. * Returns a set of divisors for the desired target clock with the given
  647. * refclk, or FALSE. The returned values represent the clock equation:
  648. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  649. *
  650. * Target and reference clocks are specified in kHz.
  651. *
  652. * If match_clock is provided, then best_clock P divider must match the P
  653. * divider from @match_clock used for LVDS downclocking.
  654. */
  655. static bool
  656. pnv_find_best_dpll(const struct intel_limit *limit,
  657. struct intel_crtc_state *crtc_state,
  658. int target, int refclk, struct dpll *match_clock,
  659. struct dpll *best_clock)
  660. {
  661. struct drm_device *dev = crtc_state->base.crtc->dev;
  662. struct dpll clock;
  663. int err = target;
  664. memset(best_clock, 0, sizeof(*best_clock));
  665. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  666. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  667. clock.m1++) {
  668. for (clock.m2 = limit->m2.min;
  669. clock.m2 <= limit->m2.max; clock.m2++) {
  670. for (clock.n = limit->n.min;
  671. clock.n <= limit->n.max; clock.n++) {
  672. for (clock.p1 = limit->p1.min;
  673. clock.p1 <= limit->p1.max; clock.p1++) {
  674. int this_err;
  675. pnv_calc_dpll_params(refclk, &clock);
  676. if (!intel_PLL_is_valid(dev, limit,
  677. &clock))
  678. continue;
  679. if (match_clock &&
  680. clock.p != match_clock->p)
  681. continue;
  682. this_err = abs(clock.dot - target);
  683. if (this_err < err) {
  684. *best_clock = clock;
  685. err = this_err;
  686. }
  687. }
  688. }
  689. }
  690. }
  691. return (err != target);
  692. }
  693. /*
  694. * Returns a set of divisors for the desired target clock with the given
  695. * refclk, or FALSE. The returned values represent the clock equation:
  696. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  697. *
  698. * Target and reference clocks are specified in kHz.
  699. *
  700. * If match_clock is provided, then best_clock P divider must match the P
  701. * divider from @match_clock used for LVDS downclocking.
  702. */
  703. static bool
  704. g4x_find_best_dpll(const struct intel_limit *limit,
  705. struct intel_crtc_state *crtc_state,
  706. int target, int refclk, struct dpll *match_clock,
  707. struct dpll *best_clock)
  708. {
  709. struct drm_device *dev = crtc_state->base.crtc->dev;
  710. struct dpll clock;
  711. int max_n;
  712. bool found = false;
  713. /* approximately equals target * 0.00585 */
  714. int err_most = (target >> 8) + (target >> 9);
  715. memset(best_clock, 0, sizeof(*best_clock));
  716. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  717. max_n = limit->n.max;
  718. /* based on hardware requirement, prefer smaller n to precision */
  719. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  720. /* based on hardware requirement, prefere larger m1,m2 */
  721. for (clock.m1 = limit->m1.max;
  722. clock.m1 >= limit->m1.min; clock.m1--) {
  723. for (clock.m2 = limit->m2.max;
  724. clock.m2 >= limit->m2.min; clock.m2--) {
  725. for (clock.p1 = limit->p1.max;
  726. clock.p1 >= limit->p1.min; clock.p1--) {
  727. int this_err;
  728. i9xx_calc_dpll_params(refclk, &clock);
  729. if (!intel_PLL_is_valid(dev, limit,
  730. &clock))
  731. continue;
  732. this_err = abs(clock.dot - target);
  733. if (this_err < err_most) {
  734. *best_clock = clock;
  735. err_most = this_err;
  736. max_n = clock.n;
  737. found = true;
  738. }
  739. }
  740. }
  741. }
  742. }
  743. return found;
  744. }
  745. /*
  746. * Check if the calculated PLL configuration is more optimal compared to the
  747. * best configuration and error found so far. Return the calculated error.
  748. */
  749. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  750. const struct dpll *calculated_clock,
  751. const struct dpll *best_clock,
  752. unsigned int best_error_ppm,
  753. unsigned int *error_ppm)
  754. {
  755. /*
  756. * For CHV ignore the error and consider only the P value.
  757. * Prefer a bigger P value based on HW requirements.
  758. */
  759. if (IS_CHERRYVIEW(dev)) {
  760. *error_ppm = 0;
  761. return calculated_clock->p > best_clock->p;
  762. }
  763. if (WARN_ON_ONCE(!target_freq))
  764. return false;
  765. *error_ppm = div_u64(1000000ULL *
  766. abs(target_freq - calculated_clock->dot),
  767. target_freq);
  768. /*
  769. * Prefer a better P value over a better (smaller) error if the error
  770. * is small. Ensure this preference for future configurations too by
  771. * setting the error to 0.
  772. */
  773. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  774. *error_ppm = 0;
  775. return true;
  776. }
  777. return *error_ppm + 10 < best_error_ppm;
  778. }
  779. /*
  780. * Returns a set of divisors for the desired target clock with the given
  781. * refclk, or FALSE. The returned values represent the clock equation:
  782. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  783. */
  784. static bool
  785. vlv_find_best_dpll(const struct intel_limit *limit,
  786. struct intel_crtc_state *crtc_state,
  787. int target, int refclk, struct dpll *match_clock,
  788. struct dpll *best_clock)
  789. {
  790. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  791. struct drm_device *dev = crtc->base.dev;
  792. struct dpll clock;
  793. unsigned int bestppm = 1000000;
  794. /* min update 19.2 MHz */
  795. int max_n = min(limit->n.max, refclk / 19200);
  796. bool found = false;
  797. target *= 5; /* fast clock */
  798. memset(best_clock, 0, sizeof(*best_clock));
  799. /* based on hardware requirement, prefer smaller n to precision */
  800. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  801. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  802. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  803. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  804. clock.p = clock.p1 * clock.p2;
  805. /* based on hardware requirement, prefer bigger m1,m2 values */
  806. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  807. unsigned int ppm;
  808. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  809. refclk * clock.m1);
  810. vlv_calc_dpll_params(refclk, &clock);
  811. if (!intel_PLL_is_valid(dev, limit,
  812. &clock))
  813. continue;
  814. if (!vlv_PLL_is_optimal(dev, target,
  815. &clock,
  816. best_clock,
  817. bestppm, &ppm))
  818. continue;
  819. *best_clock = clock;
  820. bestppm = ppm;
  821. found = true;
  822. }
  823. }
  824. }
  825. }
  826. return found;
  827. }
  828. /*
  829. * Returns a set of divisors for the desired target clock with the given
  830. * refclk, or FALSE. The returned values represent the clock equation:
  831. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  832. */
  833. static bool
  834. chv_find_best_dpll(const struct intel_limit *limit,
  835. struct intel_crtc_state *crtc_state,
  836. int target, int refclk, struct dpll *match_clock,
  837. struct dpll *best_clock)
  838. {
  839. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  840. struct drm_device *dev = crtc->base.dev;
  841. unsigned int best_error_ppm;
  842. struct dpll clock;
  843. uint64_t m2;
  844. int found = false;
  845. memset(best_clock, 0, sizeof(*best_clock));
  846. best_error_ppm = 1000000;
  847. /*
  848. * Based on hardware doc, the n always set to 1, and m1 always
  849. * set to 2. If requires to support 200Mhz refclk, we need to
  850. * revisit this because n may not 1 anymore.
  851. */
  852. clock.n = 1, clock.m1 = 2;
  853. target *= 5; /* fast clock */
  854. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  855. for (clock.p2 = limit->p2.p2_fast;
  856. clock.p2 >= limit->p2.p2_slow;
  857. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  858. unsigned int error_ppm;
  859. clock.p = clock.p1 * clock.p2;
  860. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  861. clock.n) << 22, refclk * clock.m1);
  862. if (m2 > INT_MAX/clock.m1)
  863. continue;
  864. clock.m2 = m2;
  865. chv_calc_dpll_params(refclk, &clock);
  866. if (!intel_PLL_is_valid(dev, limit, &clock))
  867. continue;
  868. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  869. best_error_ppm, &error_ppm))
  870. continue;
  871. *best_clock = clock;
  872. best_error_ppm = error_ppm;
  873. found = true;
  874. }
  875. }
  876. return found;
  877. }
  878. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  879. struct dpll *best_clock)
  880. {
  881. int refclk = 100000;
  882. const struct intel_limit *limit = &intel_limits_bxt;
  883. return chv_find_best_dpll(limit, crtc_state,
  884. target_clock, refclk, NULL, best_clock);
  885. }
  886. bool intel_crtc_active(struct drm_crtc *crtc)
  887. {
  888. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  889. /* Be paranoid as we can arrive here with only partial
  890. * state retrieved from the hardware during setup.
  891. *
  892. * We can ditch the adjusted_mode.crtc_clock check as soon
  893. * as Haswell has gained clock readout/fastboot support.
  894. *
  895. * We can ditch the crtc->primary->fb check as soon as we can
  896. * properly reconstruct framebuffers.
  897. *
  898. * FIXME: The intel_crtc->active here should be switched to
  899. * crtc->state->active once we have proper CRTC states wired up
  900. * for atomic.
  901. */
  902. return intel_crtc->active && crtc->primary->state->fb &&
  903. intel_crtc->config->base.adjusted_mode.crtc_clock;
  904. }
  905. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  906. enum pipe pipe)
  907. {
  908. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  909. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  910. return intel_crtc->config->cpu_transcoder;
  911. }
  912. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  913. {
  914. struct drm_i915_private *dev_priv = to_i915(dev);
  915. i915_reg_t reg = PIPEDSL(pipe);
  916. u32 line1, line2;
  917. u32 line_mask;
  918. if (IS_GEN2(dev))
  919. line_mask = DSL_LINEMASK_GEN2;
  920. else
  921. line_mask = DSL_LINEMASK_GEN3;
  922. line1 = I915_READ(reg) & line_mask;
  923. msleep(5);
  924. line2 = I915_READ(reg) & line_mask;
  925. return line1 == line2;
  926. }
  927. /*
  928. * intel_wait_for_pipe_off - wait for pipe to turn off
  929. * @crtc: crtc whose pipe to wait for
  930. *
  931. * After disabling a pipe, we can't wait for vblank in the usual way,
  932. * spinning on the vblank interrupt status bit, since we won't actually
  933. * see an interrupt when the pipe is disabled.
  934. *
  935. * On Gen4 and above:
  936. * wait for the pipe register state bit to turn off
  937. *
  938. * Otherwise:
  939. * wait for the display line value to settle (it usually
  940. * ends up stopping at the start of the next frame).
  941. *
  942. */
  943. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  944. {
  945. struct drm_device *dev = crtc->base.dev;
  946. struct drm_i915_private *dev_priv = to_i915(dev);
  947. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  948. enum pipe pipe = crtc->pipe;
  949. if (INTEL_INFO(dev)->gen >= 4) {
  950. i915_reg_t reg = PIPECONF(cpu_transcoder);
  951. /* Wait for the Pipe State to go off */
  952. if (intel_wait_for_register(dev_priv,
  953. reg, I965_PIPECONF_ACTIVE, 0,
  954. 100))
  955. WARN(1, "pipe_off wait timed out\n");
  956. } else {
  957. /* Wait for the display line to settle */
  958. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  959. WARN(1, "pipe_off wait timed out\n");
  960. }
  961. }
  962. /* Only for pre-ILK configs */
  963. void assert_pll(struct drm_i915_private *dev_priv,
  964. enum pipe pipe, bool state)
  965. {
  966. u32 val;
  967. bool cur_state;
  968. val = I915_READ(DPLL(pipe));
  969. cur_state = !!(val & DPLL_VCO_ENABLE);
  970. I915_STATE_WARN(cur_state != state,
  971. "PLL state assertion failure (expected %s, current %s)\n",
  972. onoff(state), onoff(cur_state));
  973. }
  974. /* XXX: the dsi pll is shared between MIPI DSI ports */
  975. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  976. {
  977. u32 val;
  978. bool cur_state;
  979. mutex_lock(&dev_priv->sb_lock);
  980. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  981. mutex_unlock(&dev_priv->sb_lock);
  982. cur_state = val & DSI_PLL_VCO_EN;
  983. I915_STATE_WARN(cur_state != state,
  984. "DSI PLL state assertion failure (expected %s, current %s)\n",
  985. onoff(state), onoff(cur_state));
  986. }
  987. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  988. enum pipe pipe, bool state)
  989. {
  990. bool cur_state;
  991. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  992. pipe);
  993. if (HAS_DDI(dev_priv)) {
  994. /* DDI does not have a specific FDI_TX register */
  995. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  996. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  997. } else {
  998. u32 val = I915_READ(FDI_TX_CTL(pipe));
  999. cur_state = !!(val & FDI_TX_ENABLE);
  1000. }
  1001. I915_STATE_WARN(cur_state != state,
  1002. "FDI TX state assertion failure (expected %s, current %s)\n",
  1003. onoff(state), onoff(cur_state));
  1004. }
  1005. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1006. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1007. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1008. enum pipe pipe, bool state)
  1009. {
  1010. u32 val;
  1011. bool cur_state;
  1012. val = I915_READ(FDI_RX_CTL(pipe));
  1013. cur_state = !!(val & FDI_RX_ENABLE);
  1014. I915_STATE_WARN(cur_state != state,
  1015. "FDI RX state assertion failure (expected %s, current %s)\n",
  1016. onoff(state), onoff(cur_state));
  1017. }
  1018. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1019. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1020. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1021. enum pipe pipe)
  1022. {
  1023. u32 val;
  1024. /* ILK FDI PLL is always enabled */
  1025. if (IS_GEN5(dev_priv))
  1026. return;
  1027. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1028. if (HAS_DDI(dev_priv))
  1029. return;
  1030. val = I915_READ(FDI_TX_CTL(pipe));
  1031. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1032. }
  1033. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1034. enum pipe pipe, bool state)
  1035. {
  1036. u32 val;
  1037. bool cur_state;
  1038. val = I915_READ(FDI_RX_CTL(pipe));
  1039. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1040. I915_STATE_WARN(cur_state != state,
  1041. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1042. onoff(state), onoff(cur_state));
  1043. }
  1044. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1045. enum pipe pipe)
  1046. {
  1047. struct drm_device *dev = &dev_priv->drm;
  1048. i915_reg_t pp_reg;
  1049. u32 val;
  1050. enum pipe panel_pipe = PIPE_A;
  1051. bool locked = true;
  1052. if (WARN_ON(HAS_DDI(dev)))
  1053. return;
  1054. if (HAS_PCH_SPLIT(dev)) {
  1055. u32 port_sel;
  1056. pp_reg = PP_CONTROL(0);
  1057. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1058. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1059. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1060. panel_pipe = PIPE_B;
  1061. /* XXX: else fix for eDP */
  1062. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1063. /* presumably write lock depends on pipe, not port select */
  1064. pp_reg = PP_CONTROL(pipe);
  1065. panel_pipe = pipe;
  1066. } else {
  1067. pp_reg = PP_CONTROL(0);
  1068. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1069. panel_pipe = PIPE_B;
  1070. }
  1071. val = I915_READ(pp_reg);
  1072. if (!(val & PANEL_POWER_ON) ||
  1073. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1074. locked = false;
  1075. I915_STATE_WARN(panel_pipe == pipe && locked,
  1076. "panel assertion failure, pipe %c regs locked\n",
  1077. pipe_name(pipe));
  1078. }
  1079. static void assert_cursor(struct drm_i915_private *dev_priv,
  1080. enum pipe pipe, bool state)
  1081. {
  1082. struct drm_device *dev = &dev_priv->drm;
  1083. bool cur_state;
  1084. if (IS_845G(dev) || IS_I865G(dev))
  1085. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1086. else
  1087. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1088. I915_STATE_WARN(cur_state != state,
  1089. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1090. pipe_name(pipe), onoff(state), onoff(cur_state));
  1091. }
  1092. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1093. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1094. void assert_pipe(struct drm_i915_private *dev_priv,
  1095. enum pipe pipe, bool state)
  1096. {
  1097. bool cur_state;
  1098. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1099. pipe);
  1100. enum intel_display_power_domain power_domain;
  1101. /* if we need the pipe quirk it must be always on */
  1102. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1103. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1104. state = true;
  1105. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1106. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1107. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1108. cur_state = !!(val & PIPECONF_ENABLE);
  1109. intel_display_power_put(dev_priv, power_domain);
  1110. } else {
  1111. cur_state = false;
  1112. }
  1113. I915_STATE_WARN(cur_state != state,
  1114. "pipe %c assertion failure (expected %s, current %s)\n",
  1115. pipe_name(pipe), onoff(state), onoff(cur_state));
  1116. }
  1117. static void assert_plane(struct drm_i915_private *dev_priv,
  1118. enum plane plane, bool state)
  1119. {
  1120. u32 val;
  1121. bool cur_state;
  1122. val = I915_READ(DSPCNTR(plane));
  1123. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1124. I915_STATE_WARN(cur_state != state,
  1125. "plane %c assertion failure (expected %s, current %s)\n",
  1126. plane_name(plane), onoff(state), onoff(cur_state));
  1127. }
  1128. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1129. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1130. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1131. enum pipe pipe)
  1132. {
  1133. struct drm_device *dev = &dev_priv->drm;
  1134. int i;
  1135. /* Primary planes are fixed to pipes on gen4+ */
  1136. if (INTEL_INFO(dev)->gen >= 4) {
  1137. u32 val = I915_READ(DSPCNTR(pipe));
  1138. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1139. "plane %c assertion failure, should be disabled but not\n",
  1140. plane_name(pipe));
  1141. return;
  1142. }
  1143. /* Need to check both planes against the pipe */
  1144. for_each_pipe(dev_priv, i) {
  1145. u32 val = I915_READ(DSPCNTR(i));
  1146. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1147. DISPPLANE_SEL_PIPE_SHIFT;
  1148. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1149. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1150. plane_name(i), pipe_name(pipe));
  1151. }
  1152. }
  1153. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1154. enum pipe pipe)
  1155. {
  1156. struct drm_device *dev = &dev_priv->drm;
  1157. int sprite;
  1158. if (INTEL_INFO(dev)->gen >= 9) {
  1159. for_each_sprite(dev_priv, pipe, sprite) {
  1160. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1161. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1162. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1163. sprite, pipe_name(pipe));
  1164. }
  1165. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1166. for_each_sprite(dev_priv, pipe, sprite) {
  1167. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1168. I915_STATE_WARN(val & SP_ENABLE,
  1169. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1170. sprite_name(pipe, sprite), pipe_name(pipe));
  1171. }
  1172. } else if (INTEL_INFO(dev)->gen >= 7) {
  1173. u32 val = I915_READ(SPRCTL(pipe));
  1174. I915_STATE_WARN(val & SPRITE_ENABLE,
  1175. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1176. plane_name(pipe), pipe_name(pipe));
  1177. } else if (INTEL_INFO(dev)->gen >= 5) {
  1178. u32 val = I915_READ(DVSCNTR(pipe));
  1179. I915_STATE_WARN(val & DVS_ENABLE,
  1180. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1181. plane_name(pipe), pipe_name(pipe));
  1182. }
  1183. }
  1184. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1185. {
  1186. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1187. drm_crtc_vblank_put(crtc);
  1188. }
  1189. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1190. enum pipe pipe)
  1191. {
  1192. u32 val;
  1193. bool enabled;
  1194. val = I915_READ(PCH_TRANSCONF(pipe));
  1195. enabled = !!(val & TRANS_ENABLE);
  1196. I915_STATE_WARN(enabled,
  1197. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1198. pipe_name(pipe));
  1199. }
  1200. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1201. enum pipe pipe, u32 port_sel, u32 val)
  1202. {
  1203. if ((val & DP_PORT_EN) == 0)
  1204. return false;
  1205. if (HAS_PCH_CPT(dev_priv)) {
  1206. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1207. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1208. return false;
  1209. } else if (IS_CHERRYVIEW(dev_priv)) {
  1210. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1211. return false;
  1212. } else {
  1213. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1214. return false;
  1215. }
  1216. return true;
  1217. }
  1218. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1219. enum pipe pipe, u32 val)
  1220. {
  1221. if ((val & SDVO_ENABLE) == 0)
  1222. return false;
  1223. if (HAS_PCH_CPT(dev_priv)) {
  1224. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1225. return false;
  1226. } else if (IS_CHERRYVIEW(dev_priv)) {
  1227. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1228. return false;
  1229. } else {
  1230. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1231. return false;
  1232. }
  1233. return true;
  1234. }
  1235. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1236. enum pipe pipe, u32 val)
  1237. {
  1238. if ((val & LVDS_PORT_EN) == 0)
  1239. return false;
  1240. if (HAS_PCH_CPT(dev_priv)) {
  1241. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1242. return false;
  1243. } else {
  1244. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1245. return false;
  1246. }
  1247. return true;
  1248. }
  1249. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1250. enum pipe pipe, u32 val)
  1251. {
  1252. if ((val & ADPA_DAC_ENABLE) == 0)
  1253. return false;
  1254. if (HAS_PCH_CPT(dev_priv)) {
  1255. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1256. return false;
  1257. } else {
  1258. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1259. return false;
  1260. }
  1261. return true;
  1262. }
  1263. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1264. enum pipe pipe, i915_reg_t reg,
  1265. u32 port_sel)
  1266. {
  1267. u32 val = I915_READ(reg);
  1268. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1269. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1270. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1271. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1272. && (val & DP_PIPEB_SELECT),
  1273. "IBX PCH dp port still using transcoder B\n");
  1274. }
  1275. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1276. enum pipe pipe, i915_reg_t reg)
  1277. {
  1278. u32 val = I915_READ(reg);
  1279. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1280. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1281. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1282. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1283. && (val & SDVO_PIPE_B_SELECT),
  1284. "IBX PCH hdmi port still using transcoder B\n");
  1285. }
  1286. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1287. enum pipe pipe)
  1288. {
  1289. u32 val;
  1290. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1291. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1292. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1293. val = I915_READ(PCH_ADPA);
  1294. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1295. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1296. pipe_name(pipe));
  1297. val = I915_READ(PCH_LVDS);
  1298. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1299. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1300. pipe_name(pipe));
  1301. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1302. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1303. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1304. }
  1305. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1306. const struct intel_crtc_state *pipe_config)
  1307. {
  1308. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1309. enum pipe pipe = crtc->pipe;
  1310. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1311. POSTING_READ(DPLL(pipe));
  1312. udelay(150);
  1313. if (intel_wait_for_register(dev_priv,
  1314. DPLL(pipe),
  1315. DPLL_LOCK_VLV,
  1316. DPLL_LOCK_VLV,
  1317. 1))
  1318. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1319. }
  1320. static void vlv_enable_pll(struct intel_crtc *crtc,
  1321. const struct intel_crtc_state *pipe_config)
  1322. {
  1323. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1324. enum pipe pipe = crtc->pipe;
  1325. assert_pipe_disabled(dev_priv, pipe);
  1326. /* PLL is protected by panel, make sure we can write it */
  1327. assert_panel_unlocked(dev_priv, pipe);
  1328. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1329. _vlv_enable_pll(crtc, pipe_config);
  1330. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1331. POSTING_READ(DPLL_MD(pipe));
  1332. }
  1333. static void _chv_enable_pll(struct intel_crtc *crtc,
  1334. const struct intel_crtc_state *pipe_config)
  1335. {
  1336. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1337. enum pipe pipe = crtc->pipe;
  1338. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1339. u32 tmp;
  1340. mutex_lock(&dev_priv->sb_lock);
  1341. /* Enable back the 10bit clock to display controller */
  1342. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1343. tmp |= DPIO_DCLKP_EN;
  1344. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1345. mutex_unlock(&dev_priv->sb_lock);
  1346. /*
  1347. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1348. */
  1349. udelay(1);
  1350. /* Enable PLL */
  1351. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1352. /* Check PLL is locked */
  1353. if (intel_wait_for_register(dev_priv,
  1354. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1355. 1))
  1356. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1357. }
  1358. static void chv_enable_pll(struct intel_crtc *crtc,
  1359. const struct intel_crtc_state *pipe_config)
  1360. {
  1361. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1362. enum pipe pipe = crtc->pipe;
  1363. assert_pipe_disabled(dev_priv, pipe);
  1364. /* PLL is protected by panel, make sure we can write it */
  1365. assert_panel_unlocked(dev_priv, pipe);
  1366. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1367. _chv_enable_pll(crtc, pipe_config);
  1368. if (pipe != PIPE_A) {
  1369. /*
  1370. * WaPixelRepeatModeFixForC0:chv
  1371. *
  1372. * DPLLCMD is AWOL. Use chicken bits to propagate
  1373. * the value from DPLLBMD to either pipe B or C.
  1374. */
  1375. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1376. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1377. I915_WRITE(CBR4_VLV, 0);
  1378. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1379. /*
  1380. * DPLLB VGA mode also seems to cause problems.
  1381. * We should always have it disabled.
  1382. */
  1383. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1384. } else {
  1385. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1386. POSTING_READ(DPLL_MD(pipe));
  1387. }
  1388. }
  1389. static int intel_num_dvo_pipes(struct drm_device *dev)
  1390. {
  1391. struct intel_crtc *crtc;
  1392. int count = 0;
  1393. for_each_intel_crtc(dev, crtc) {
  1394. count += crtc->base.state->active &&
  1395. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1396. }
  1397. return count;
  1398. }
  1399. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1400. {
  1401. struct drm_device *dev = crtc->base.dev;
  1402. struct drm_i915_private *dev_priv = to_i915(dev);
  1403. i915_reg_t reg = DPLL(crtc->pipe);
  1404. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1405. assert_pipe_disabled(dev_priv, crtc->pipe);
  1406. /* PLL is protected by panel, make sure we can write it */
  1407. if (IS_MOBILE(dev) && !IS_I830(dev))
  1408. assert_panel_unlocked(dev_priv, crtc->pipe);
  1409. /* Enable DVO 2x clock on both PLLs if necessary */
  1410. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1411. /*
  1412. * It appears to be important that we don't enable this
  1413. * for the current pipe before otherwise configuring the
  1414. * PLL. No idea how this should be handled if multiple
  1415. * DVO outputs are enabled simultaneosly.
  1416. */
  1417. dpll |= DPLL_DVO_2X_MODE;
  1418. I915_WRITE(DPLL(!crtc->pipe),
  1419. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1420. }
  1421. /*
  1422. * Apparently we need to have VGA mode enabled prior to changing
  1423. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1424. * dividers, even though the register value does change.
  1425. */
  1426. I915_WRITE(reg, 0);
  1427. I915_WRITE(reg, dpll);
  1428. /* Wait for the clocks to stabilize. */
  1429. POSTING_READ(reg);
  1430. udelay(150);
  1431. if (INTEL_INFO(dev)->gen >= 4) {
  1432. I915_WRITE(DPLL_MD(crtc->pipe),
  1433. crtc->config->dpll_hw_state.dpll_md);
  1434. } else {
  1435. /* The pixel multiplier can only be updated once the
  1436. * DPLL is enabled and the clocks are stable.
  1437. *
  1438. * So write it again.
  1439. */
  1440. I915_WRITE(reg, dpll);
  1441. }
  1442. /* We do this three times for luck */
  1443. I915_WRITE(reg, dpll);
  1444. POSTING_READ(reg);
  1445. udelay(150); /* wait for warmup */
  1446. I915_WRITE(reg, dpll);
  1447. POSTING_READ(reg);
  1448. udelay(150); /* wait for warmup */
  1449. I915_WRITE(reg, dpll);
  1450. POSTING_READ(reg);
  1451. udelay(150); /* wait for warmup */
  1452. }
  1453. /**
  1454. * i9xx_disable_pll - disable a PLL
  1455. * @dev_priv: i915 private structure
  1456. * @pipe: pipe PLL to disable
  1457. *
  1458. * Disable the PLL for @pipe, making sure the pipe is off first.
  1459. *
  1460. * Note! This is for pre-ILK only.
  1461. */
  1462. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1463. {
  1464. struct drm_device *dev = crtc->base.dev;
  1465. struct drm_i915_private *dev_priv = to_i915(dev);
  1466. enum pipe pipe = crtc->pipe;
  1467. /* Disable DVO 2x clock on both PLLs if necessary */
  1468. if (IS_I830(dev) &&
  1469. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1470. !intel_num_dvo_pipes(dev)) {
  1471. I915_WRITE(DPLL(PIPE_B),
  1472. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1473. I915_WRITE(DPLL(PIPE_A),
  1474. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1475. }
  1476. /* Don't disable pipe or pipe PLLs if needed */
  1477. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1478. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1479. return;
  1480. /* Make sure the pipe isn't still relying on us */
  1481. assert_pipe_disabled(dev_priv, pipe);
  1482. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1483. POSTING_READ(DPLL(pipe));
  1484. }
  1485. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1486. {
  1487. u32 val;
  1488. /* Make sure the pipe isn't still relying on us */
  1489. assert_pipe_disabled(dev_priv, pipe);
  1490. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1491. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1492. if (pipe != PIPE_A)
  1493. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1494. I915_WRITE(DPLL(pipe), val);
  1495. POSTING_READ(DPLL(pipe));
  1496. }
  1497. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1498. {
  1499. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1500. u32 val;
  1501. /* Make sure the pipe isn't still relying on us */
  1502. assert_pipe_disabled(dev_priv, pipe);
  1503. val = DPLL_SSC_REF_CLK_CHV |
  1504. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1505. if (pipe != PIPE_A)
  1506. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1507. I915_WRITE(DPLL(pipe), val);
  1508. POSTING_READ(DPLL(pipe));
  1509. mutex_lock(&dev_priv->sb_lock);
  1510. /* Disable 10bit clock to display controller */
  1511. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1512. val &= ~DPIO_DCLKP_EN;
  1513. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1514. mutex_unlock(&dev_priv->sb_lock);
  1515. }
  1516. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1517. struct intel_digital_port *dport,
  1518. unsigned int expected_mask)
  1519. {
  1520. u32 port_mask;
  1521. i915_reg_t dpll_reg;
  1522. switch (dport->port) {
  1523. case PORT_B:
  1524. port_mask = DPLL_PORTB_READY_MASK;
  1525. dpll_reg = DPLL(0);
  1526. break;
  1527. case PORT_C:
  1528. port_mask = DPLL_PORTC_READY_MASK;
  1529. dpll_reg = DPLL(0);
  1530. expected_mask <<= 4;
  1531. break;
  1532. case PORT_D:
  1533. port_mask = DPLL_PORTD_READY_MASK;
  1534. dpll_reg = DPIO_PHY_STATUS;
  1535. break;
  1536. default:
  1537. BUG();
  1538. }
  1539. if (intel_wait_for_register(dev_priv,
  1540. dpll_reg, port_mask, expected_mask,
  1541. 1000))
  1542. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1543. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1544. }
  1545. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1546. enum pipe pipe)
  1547. {
  1548. struct drm_device *dev = &dev_priv->drm;
  1549. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1550. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1551. i915_reg_t reg;
  1552. uint32_t val, pipeconf_val;
  1553. /* Make sure PCH DPLL is enabled */
  1554. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1555. /* FDI must be feeding us bits for PCH ports */
  1556. assert_fdi_tx_enabled(dev_priv, pipe);
  1557. assert_fdi_rx_enabled(dev_priv, pipe);
  1558. if (HAS_PCH_CPT(dev)) {
  1559. /* Workaround: Set the timing override bit before enabling the
  1560. * pch transcoder. */
  1561. reg = TRANS_CHICKEN2(pipe);
  1562. val = I915_READ(reg);
  1563. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1564. I915_WRITE(reg, val);
  1565. }
  1566. reg = PCH_TRANSCONF(pipe);
  1567. val = I915_READ(reg);
  1568. pipeconf_val = I915_READ(PIPECONF(pipe));
  1569. if (HAS_PCH_IBX(dev_priv)) {
  1570. /*
  1571. * Make the BPC in transcoder be consistent with
  1572. * that in pipeconf reg. For HDMI we must use 8bpc
  1573. * here for both 8bpc and 12bpc.
  1574. */
  1575. val &= ~PIPECONF_BPC_MASK;
  1576. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1577. val |= PIPECONF_8BPC;
  1578. else
  1579. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1580. }
  1581. val &= ~TRANS_INTERLACE_MASK;
  1582. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1583. if (HAS_PCH_IBX(dev_priv) &&
  1584. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1585. val |= TRANS_LEGACY_INTERLACED_ILK;
  1586. else
  1587. val |= TRANS_INTERLACED;
  1588. else
  1589. val |= TRANS_PROGRESSIVE;
  1590. I915_WRITE(reg, val | TRANS_ENABLE);
  1591. if (intel_wait_for_register(dev_priv,
  1592. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1593. 100))
  1594. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1595. }
  1596. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1597. enum transcoder cpu_transcoder)
  1598. {
  1599. u32 val, pipeconf_val;
  1600. /* FDI must be feeding us bits for PCH ports */
  1601. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1602. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1603. /* Workaround: set timing override bit. */
  1604. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1605. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1606. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1607. val = TRANS_ENABLE;
  1608. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1609. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1610. PIPECONF_INTERLACED_ILK)
  1611. val |= TRANS_INTERLACED;
  1612. else
  1613. val |= TRANS_PROGRESSIVE;
  1614. I915_WRITE(LPT_TRANSCONF, val);
  1615. if (intel_wait_for_register(dev_priv,
  1616. LPT_TRANSCONF,
  1617. TRANS_STATE_ENABLE,
  1618. TRANS_STATE_ENABLE,
  1619. 100))
  1620. DRM_ERROR("Failed to enable PCH transcoder\n");
  1621. }
  1622. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1623. enum pipe pipe)
  1624. {
  1625. struct drm_device *dev = &dev_priv->drm;
  1626. i915_reg_t reg;
  1627. uint32_t val;
  1628. /* FDI relies on the transcoder */
  1629. assert_fdi_tx_disabled(dev_priv, pipe);
  1630. assert_fdi_rx_disabled(dev_priv, pipe);
  1631. /* Ports must be off as well */
  1632. assert_pch_ports_disabled(dev_priv, pipe);
  1633. reg = PCH_TRANSCONF(pipe);
  1634. val = I915_READ(reg);
  1635. val &= ~TRANS_ENABLE;
  1636. I915_WRITE(reg, val);
  1637. /* wait for PCH transcoder off, transcoder state */
  1638. if (intel_wait_for_register(dev_priv,
  1639. reg, TRANS_STATE_ENABLE, 0,
  1640. 50))
  1641. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1642. if (HAS_PCH_CPT(dev)) {
  1643. /* Workaround: Clear the timing override chicken bit again. */
  1644. reg = TRANS_CHICKEN2(pipe);
  1645. val = I915_READ(reg);
  1646. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1647. I915_WRITE(reg, val);
  1648. }
  1649. }
  1650. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1651. {
  1652. u32 val;
  1653. val = I915_READ(LPT_TRANSCONF);
  1654. val &= ~TRANS_ENABLE;
  1655. I915_WRITE(LPT_TRANSCONF, val);
  1656. /* wait for PCH transcoder off, transcoder state */
  1657. if (intel_wait_for_register(dev_priv,
  1658. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1659. 50))
  1660. DRM_ERROR("Failed to disable PCH transcoder\n");
  1661. /* Workaround: clear timing override bit. */
  1662. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1663. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1664. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1665. }
  1666. /**
  1667. * intel_enable_pipe - enable a pipe, asserting requirements
  1668. * @crtc: crtc responsible for the pipe
  1669. *
  1670. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1671. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1672. */
  1673. static void intel_enable_pipe(struct intel_crtc *crtc)
  1674. {
  1675. struct drm_device *dev = crtc->base.dev;
  1676. struct drm_i915_private *dev_priv = to_i915(dev);
  1677. enum pipe pipe = crtc->pipe;
  1678. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1679. enum pipe pch_transcoder;
  1680. i915_reg_t reg;
  1681. u32 val;
  1682. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1683. assert_planes_disabled(dev_priv, pipe);
  1684. assert_cursor_disabled(dev_priv, pipe);
  1685. assert_sprites_disabled(dev_priv, pipe);
  1686. if (HAS_PCH_LPT(dev_priv))
  1687. pch_transcoder = TRANSCODER_A;
  1688. else
  1689. pch_transcoder = pipe;
  1690. /*
  1691. * A pipe without a PLL won't actually be able to drive bits from
  1692. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1693. * need the check.
  1694. */
  1695. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1696. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1697. assert_dsi_pll_enabled(dev_priv);
  1698. else
  1699. assert_pll_enabled(dev_priv, pipe);
  1700. } else {
  1701. if (crtc->config->has_pch_encoder) {
  1702. /* if driving the PCH, we need FDI enabled */
  1703. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1704. assert_fdi_tx_pll_enabled(dev_priv,
  1705. (enum pipe) cpu_transcoder);
  1706. }
  1707. /* FIXME: assert CPU port conditions for SNB+ */
  1708. }
  1709. reg = PIPECONF(cpu_transcoder);
  1710. val = I915_READ(reg);
  1711. if (val & PIPECONF_ENABLE) {
  1712. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1713. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1714. return;
  1715. }
  1716. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1717. POSTING_READ(reg);
  1718. /*
  1719. * Until the pipe starts DSL will read as 0, which would cause
  1720. * an apparent vblank timestamp jump, which messes up also the
  1721. * frame count when it's derived from the timestamps. So let's
  1722. * wait for the pipe to start properly before we call
  1723. * drm_crtc_vblank_on()
  1724. */
  1725. if (dev->max_vblank_count == 0 &&
  1726. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1727. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1728. }
  1729. /**
  1730. * intel_disable_pipe - disable a pipe, asserting requirements
  1731. * @crtc: crtc whose pipes is to be disabled
  1732. *
  1733. * Disable the pipe of @crtc, making sure that various hardware
  1734. * specific requirements are met, if applicable, e.g. plane
  1735. * disabled, panel fitter off, etc.
  1736. *
  1737. * Will wait until the pipe has shut down before returning.
  1738. */
  1739. static void intel_disable_pipe(struct intel_crtc *crtc)
  1740. {
  1741. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1742. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1743. enum pipe pipe = crtc->pipe;
  1744. i915_reg_t reg;
  1745. u32 val;
  1746. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1747. /*
  1748. * Make sure planes won't keep trying to pump pixels to us,
  1749. * or we might hang the display.
  1750. */
  1751. assert_planes_disabled(dev_priv, pipe);
  1752. assert_cursor_disabled(dev_priv, pipe);
  1753. assert_sprites_disabled(dev_priv, pipe);
  1754. reg = PIPECONF(cpu_transcoder);
  1755. val = I915_READ(reg);
  1756. if ((val & PIPECONF_ENABLE) == 0)
  1757. return;
  1758. /*
  1759. * Double wide has implications for planes
  1760. * so best keep it disabled when not needed.
  1761. */
  1762. if (crtc->config->double_wide)
  1763. val &= ~PIPECONF_DOUBLE_WIDE;
  1764. /* Don't disable pipe or pipe PLLs if needed */
  1765. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1766. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1767. val &= ~PIPECONF_ENABLE;
  1768. I915_WRITE(reg, val);
  1769. if ((val & PIPECONF_ENABLE) == 0)
  1770. intel_wait_for_pipe_off(crtc);
  1771. }
  1772. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1773. {
  1774. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1775. }
  1776. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1777. uint64_t fb_modifier, unsigned int cpp)
  1778. {
  1779. switch (fb_modifier) {
  1780. case DRM_FORMAT_MOD_NONE:
  1781. return cpp;
  1782. case I915_FORMAT_MOD_X_TILED:
  1783. if (IS_GEN2(dev_priv))
  1784. return 128;
  1785. else
  1786. return 512;
  1787. case I915_FORMAT_MOD_Y_TILED:
  1788. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1789. return 128;
  1790. else
  1791. return 512;
  1792. case I915_FORMAT_MOD_Yf_TILED:
  1793. switch (cpp) {
  1794. case 1:
  1795. return 64;
  1796. case 2:
  1797. case 4:
  1798. return 128;
  1799. case 8:
  1800. case 16:
  1801. return 256;
  1802. default:
  1803. MISSING_CASE(cpp);
  1804. return cpp;
  1805. }
  1806. break;
  1807. default:
  1808. MISSING_CASE(fb_modifier);
  1809. return cpp;
  1810. }
  1811. }
  1812. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1813. uint64_t fb_modifier, unsigned int cpp)
  1814. {
  1815. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1816. return 1;
  1817. else
  1818. return intel_tile_size(dev_priv) /
  1819. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1820. }
  1821. /* Return the tile dimensions in pixel units */
  1822. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1823. unsigned int *tile_width,
  1824. unsigned int *tile_height,
  1825. uint64_t fb_modifier,
  1826. unsigned int cpp)
  1827. {
  1828. unsigned int tile_width_bytes =
  1829. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1830. *tile_width = tile_width_bytes / cpp;
  1831. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1832. }
  1833. unsigned int
  1834. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1835. uint32_t pixel_format, uint64_t fb_modifier)
  1836. {
  1837. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1838. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1839. return ALIGN(height, tile_height);
  1840. }
  1841. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1842. {
  1843. unsigned int size = 0;
  1844. int i;
  1845. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1846. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1847. return size;
  1848. }
  1849. static void
  1850. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1851. const struct drm_framebuffer *fb,
  1852. unsigned int rotation)
  1853. {
  1854. if (intel_rotation_90_or_270(rotation)) {
  1855. *view = i915_ggtt_view_rotated;
  1856. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1857. } else {
  1858. *view = i915_ggtt_view_normal;
  1859. }
  1860. }
  1861. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1862. {
  1863. if (INTEL_INFO(dev_priv)->gen >= 9)
  1864. return 256 * 1024;
  1865. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1866. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1867. return 128 * 1024;
  1868. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1869. return 4 * 1024;
  1870. else
  1871. return 0;
  1872. }
  1873. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1874. uint64_t fb_modifier)
  1875. {
  1876. switch (fb_modifier) {
  1877. case DRM_FORMAT_MOD_NONE:
  1878. return intel_linear_alignment(dev_priv);
  1879. case I915_FORMAT_MOD_X_TILED:
  1880. if (INTEL_INFO(dev_priv)->gen >= 9)
  1881. return 256 * 1024;
  1882. return 0;
  1883. case I915_FORMAT_MOD_Y_TILED:
  1884. case I915_FORMAT_MOD_Yf_TILED:
  1885. return 1 * 1024 * 1024;
  1886. default:
  1887. MISSING_CASE(fb_modifier);
  1888. return 0;
  1889. }
  1890. }
  1891. struct i915_vma *
  1892. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1893. {
  1894. struct drm_device *dev = fb->dev;
  1895. struct drm_i915_private *dev_priv = to_i915(dev);
  1896. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1897. struct i915_ggtt_view view;
  1898. struct i915_vma *vma;
  1899. u32 alignment;
  1900. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1901. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  1902. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1903. /* Note that the w/a also requires 64 PTE of padding following the
  1904. * bo. We currently fill all unused PTE with the shadow page and so
  1905. * we should always have valid PTE following the scanout preventing
  1906. * the VT-d warning.
  1907. */
  1908. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1909. alignment = 256 * 1024;
  1910. /*
  1911. * Global gtt pte registers are special registers which actually forward
  1912. * writes to a chunk of system memory. Which means that there is no risk
  1913. * that the register values disappear as soon as we call
  1914. * intel_runtime_pm_put(), so it is correct to wrap only the
  1915. * pin/unpin/fence and not more.
  1916. */
  1917. intel_runtime_pm_get(dev_priv);
  1918. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1919. if (IS_ERR(vma))
  1920. goto err;
  1921. if (i915_vma_is_map_and_fenceable(vma)) {
  1922. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1923. * fence, whereas 965+ only requires a fence if using
  1924. * framebuffer compression. For simplicity, we always, when
  1925. * possible, install a fence as the cost is not that onerous.
  1926. *
  1927. * If we fail to fence the tiled scanout, then either the
  1928. * modeset will reject the change (which is highly unlikely as
  1929. * the affected systems, all but one, do not have unmappable
  1930. * space) or we will not be able to enable full powersaving
  1931. * techniques (also likely not to apply due to various limits
  1932. * FBC and the like impose on the size of the buffer, which
  1933. * presumably we violated anyway with this unmappable buffer).
  1934. * Anyway, it is presumably better to stumble onwards with
  1935. * something and try to run the system in a "less than optimal"
  1936. * mode that matches the user configuration.
  1937. */
  1938. if (i915_vma_get_fence(vma) == 0)
  1939. i915_vma_pin_fence(vma);
  1940. }
  1941. err:
  1942. intel_runtime_pm_put(dev_priv);
  1943. return vma;
  1944. }
  1945. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1946. {
  1947. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1948. struct i915_ggtt_view view;
  1949. struct i915_vma *vma;
  1950. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1951. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1952. vma = i915_gem_object_to_ggtt(obj, &view);
  1953. if (WARN_ON_ONCE(!vma))
  1954. return;
  1955. i915_vma_unpin_fence(vma);
  1956. i915_gem_object_unpin_from_display_plane(vma);
  1957. }
  1958. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1959. unsigned int rotation)
  1960. {
  1961. if (intel_rotation_90_or_270(rotation))
  1962. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1963. else
  1964. return fb->pitches[plane];
  1965. }
  1966. /*
  1967. * Convert the x/y offsets into a linear offset.
  1968. * Only valid with 0/180 degree rotation, which is fine since linear
  1969. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1970. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1971. */
  1972. u32 intel_fb_xy_to_linear(int x, int y,
  1973. const struct intel_plane_state *state,
  1974. int plane)
  1975. {
  1976. const struct drm_framebuffer *fb = state->base.fb;
  1977. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  1978. unsigned int pitch = fb->pitches[plane];
  1979. return y * pitch + x * cpp;
  1980. }
  1981. /*
  1982. * Add the x/y offsets derived from fb->offsets[] to the user
  1983. * specified plane src x/y offsets. The resulting x/y offsets
  1984. * specify the start of scanout from the beginning of the gtt mapping.
  1985. */
  1986. void intel_add_fb_offsets(int *x, int *y,
  1987. const struct intel_plane_state *state,
  1988. int plane)
  1989. {
  1990. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1991. unsigned int rotation = state->base.rotation;
  1992. if (intel_rotation_90_or_270(rotation)) {
  1993. *x += intel_fb->rotated[plane].x;
  1994. *y += intel_fb->rotated[plane].y;
  1995. } else {
  1996. *x += intel_fb->normal[plane].x;
  1997. *y += intel_fb->normal[plane].y;
  1998. }
  1999. }
  2000. /*
  2001. * Input tile dimensions and pitch must already be
  2002. * rotated to match x and y, and in pixel units.
  2003. */
  2004. static u32 _intel_adjust_tile_offset(int *x, int *y,
  2005. unsigned int tile_width,
  2006. unsigned int tile_height,
  2007. unsigned int tile_size,
  2008. unsigned int pitch_tiles,
  2009. u32 old_offset,
  2010. u32 new_offset)
  2011. {
  2012. unsigned int pitch_pixels = pitch_tiles * tile_width;
  2013. unsigned int tiles;
  2014. WARN_ON(old_offset & (tile_size - 1));
  2015. WARN_ON(new_offset & (tile_size - 1));
  2016. WARN_ON(new_offset > old_offset);
  2017. tiles = (old_offset - new_offset) / tile_size;
  2018. *y += tiles / pitch_tiles * tile_height;
  2019. *x += tiles % pitch_tiles * tile_width;
  2020. /* minimize x in case it got needlessly big */
  2021. *y += *x / pitch_pixels * tile_height;
  2022. *x %= pitch_pixels;
  2023. return new_offset;
  2024. }
  2025. /*
  2026. * Adjust the tile offset by moving the difference into
  2027. * the x/y offsets.
  2028. */
  2029. static u32 intel_adjust_tile_offset(int *x, int *y,
  2030. const struct intel_plane_state *state, int plane,
  2031. u32 old_offset, u32 new_offset)
  2032. {
  2033. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2034. const struct drm_framebuffer *fb = state->base.fb;
  2035. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2036. unsigned int rotation = state->base.rotation;
  2037. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  2038. WARN_ON(new_offset > old_offset);
  2039. if (fb->modifier[plane] != DRM_FORMAT_MOD_NONE) {
  2040. unsigned int tile_size, tile_width, tile_height;
  2041. unsigned int pitch_tiles;
  2042. tile_size = intel_tile_size(dev_priv);
  2043. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2044. fb->modifier[plane], cpp);
  2045. if (intel_rotation_90_or_270(rotation)) {
  2046. pitch_tiles = pitch / tile_height;
  2047. swap(tile_width, tile_height);
  2048. } else {
  2049. pitch_tiles = pitch / (tile_width * cpp);
  2050. }
  2051. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2052. tile_size, pitch_tiles,
  2053. old_offset, new_offset);
  2054. } else {
  2055. old_offset += *y * pitch + *x * cpp;
  2056. *y = (old_offset - new_offset) / pitch;
  2057. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2058. }
  2059. return new_offset;
  2060. }
  2061. /*
  2062. * Computes the linear offset to the base tile and adjusts
  2063. * x, y. bytes per pixel is assumed to be a power-of-two.
  2064. *
  2065. * In the 90/270 rotated case, x and y are assumed
  2066. * to be already rotated to match the rotated GTT view, and
  2067. * pitch is the tile_height aligned framebuffer height.
  2068. *
  2069. * This function is used when computing the derived information
  2070. * under intel_framebuffer, so using any of that information
  2071. * here is not allowed. Anything under drm_framebuffer can be
  2072. * used. This is why the user has to pass in the pitch since it
  2073. * is specified in the rotated orientation.
  2074. */
  2075. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2076. int *x, int *y,
  2077. const struct drm_framebuffer *fb, int plane,
  2078. unsigned int pitch,
  2079. unsigned int rotation,
  2080. u32 alignment)
  2081. {
  2082. uint64_t fb_modifier = fb->modifier[plane];
  2083. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2084. u32 offset, offset_aligned;
  2085. if (alignment)
  2086. alignment--;
  2087. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2088. unsigned int tile_size, tile_width, tile_height;
  2089. unsigned int tile_rows, tiles, pitch_tiles;
  2090. tile_size = intel_tile_size(dev_priv);
  2091. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2092. fb_modifier, cpp);
  2093. if (intel_rotation_90_or_270(rotation)) {
  2094. pitch_tiles = pitch / tile_height;
  2095. swap(tile_width, tile_height);
  2096. } else {
  2097. pitch_tiles = pitch / (tile_width * cpp);
  2098. }
  2099. tile_rows = *y / tile_height;
  2100. *y %= tile_height;
  2101. tiles = *x / tile_width;
  2102. *x %= tile_width;
  2103. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2104. offset_aligned = offset & ~alignment;
  2105. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2106. tile_size, pitch_tiles,
  2107. offset, offset_aligned);
  2108. } else {
  2109. offset = *y * pitch + *x * cpp;
  2110. offset_aligned = offset & ~alignment;
  2111. *y = (offset & alignment) / pitch;
  2112. *x = ((offset & alignment) - *y * pitch) / cpp;
  2113. }
  2114. return offset_aligned;
  2115. }
  2116. u32 intel_compute_tile_offset(int *x, int *y,
  2117. const struct intel_plane_state *state,
  2118. int plane)
  2119. {
  2120. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2121. const struct drm_framebuffer *fb = state->base.fb;
  2122. unsigned int rotation = state->base.rotation;
  2123. int pitch = intel_fb_pitch(fb, plane, rotation);
  2124. u32 alignment;
  2125. /* AUX_DIST needs only 4K alignment */
  2126. if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
  2127. alignment = 4096;
  2128. else
  2129. alignment = intel_surf_alignment(dev_priv, fb->modifier[plane]);
  2130. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2131. rotation, alignment);
  2132. }
  2133. /* Convert the fb->offset[] linear offset into x/y offsets */
  2134. static void intel_fb_offset_to_xy(int *x, int *y,
  2135. const struct drm_framebuffer *fb, int plane)
  2136. {
  2137. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2138. unsigned int pitch = fb->pitches[plane];
  2139. u32 linear_offset = fb->offsets[plane];
  2140. *y = linear_offset / pitch;
  2141. *x = linear_offset % pitch / cpp;
  2142. }
  2143. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2144. {
  2145. switch (fb_modifier) {
  2146. case I915_FORMAT_MOD_X_TILED:
  2147. return I915_TILING_X;
  2148. case I915_FORMAT_MOD_Y_TILED:
  2149. return I915_TILING_Y;
  2150. default:
  2151. return I915_TILING_NONE;
  2152. }
  2153. }
  2154. static int
  2155. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2156. struct drm_framebuffer *fb)
  2157. {
  2158. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2159. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2160. u32 gtt_offset_rotated = 0;
  2161. unsigned int max_size = 0;
  2162. uint32_t format = fb->pixel_format;
  2163. int i, num_planes = drm_format_num_planes(format);
  2164. unsigned int tile_size = intel_tile_size(dev_priv);
  2165. for (i = 0; i < num_planes; i++) {
  2166. unsigned int width, height;
  2167. unsigned int cpp, size;
  2168. u32 offset;
  2169. int x, y;
  2170. cpp = drm_format_plane_cpp(format, i);
  2171. width = drm_format_plane_width(fb->width, format, i);
  2172. height = drm_format_plane_height(fb->height, format, i);
  2173. intel_fb_offset_to_xy(&x, &y, fb, i);
  2174. /*
  2175. * The fence (if used) is aligned to the start of the object
  2176. * so having the framebuffer wrap around across the edge of the
  2177. * fenced region doesn't really work. We have no API to configure
  2178. * the fence start offset within the object (nor could we probably
  2179. * on gen2/3). So it's just easier if we just require that the
  2180. * fb layout agrees with the fence layout. We already check that the
  2181. * fb stride matches the fence stride elsewhere.
  2182. */
  2183. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2184. (x + width) * cpp > fb->pitches[i]) {
  2185. DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
  2186. i, fb->offsets[i]);
  2187. return -EINVAL;
  2188. }
  2189. /*
  2190. * First pixel of the framebuffer from
  2191. * the start of the normal gtt mapping.
  2192. */
  2193. intel_fb->normal[i].x = x;
  2194. intel_fb->normal[i].y = y;
  2195. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2196. fb, 0, fb->pitches[i],
  2197. DRM_ROTATE_0, tile_size);
  2198. offset /= tile_size;
  2199. if (fb->modifier[i] != DRM_FORMAT_MOD_NONE) {
  2200. unsigned int tile_width, tile_height;
  2201. unsigned int pitch_tiles;
  2202. struct drm_rect r;
  2203. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2204. fb->modifier[i], cpp);
  2205. rot_info->plane[i].offset = offset;
  2206. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2207. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2208. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2209. intel_fb->rotated[i].pitch =
  2210. rot_info->plane[i].height * tile_height;
  2211. /* how many tiles does this plane need */
  2212. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2213. /*
  2214. * If the plane isn't horizontally tile aligned,
  2215. * we need one more tile.
  2216. */
  2217. if (x != 0)
  2218. size++;
  2219. /* rotate the x/y offsets to match the GTT view */
  2220. r.x1 = x;
  2221. r.y1 = y;
  2222. r.x2 = x + width;
  2223. r.y2 = y + height;
  2224. drm_rect_rotate(&r,
  2225. rot_info->plane[i].width * tile_width,
  2226. rot_info->plane[i].height * tile_height,
  2227. DRM_ROTATE_270);
  2228. x = r.x1;
  2229. y = r.y1;
  2230. /* rotate the tile dimensions to match the GTT view */
  2231. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2232. swap(tile_width, tile_height);
  2233. /*
  2234. * We only keep the x/y offsets, so push all of the
  2235. * gtt offset into the x/y offsets.
  2236. */
  2237. _intel_adjust_tile_offset(&x, &y,
  2238. tile_width, tile_height,
  2239. tile_size, pitch_tiles,
  2240. gtt_offset_rotated * tile_size, 0);
  2241. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2242. /*
  2243. * First pixel of the framebuffer from
  2244. * the start of the rotated gtt mapping.
  2245. */
  2246. intel_fb->rotated[i].x = x;
  2247. intel_fb->rotated[i].y = y;
  2248. } else {
  2249. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2250. x * cpp, tile_size);
  2251. }
  2252. /* how many tiles in total needed in the bo */
  2253. max_size = max(max_size, offset + size);
  2254. }
  2255. if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
  2256. DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2257. max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
  2258. return -EINVAL;
  2259. }
  2260. return 0;
  2261. }
  2262. static int i9xx_format_to_fourcc(int format)
  2263. {
  2264. switch (format) {
  2265. case DISPPLANE_8BPP:
  2266. return DRM_FORMAT_C8;
  2267. case DISPPLANE_BGRX555:
  2268. return DRM_FORMAT_XRGB1555;
  2269. case DISPPLANE_BGRX565:
  2270. return DRM_FORMAT_RGB565;
  2271. default:
  2272. case DISPPLANE_BGRX888:
  2273. return DRM_FORMAT_XRGB8888;
  2274. case DISPPLANE_RGBX888:
  2275. return DRM_FORMAT_XBGR8888;
  2276. case DISPPLANE_BGRX101010:
  2277. return DRM_FORMAT_XRGB2101010;
  2278. case DISPPLANE_RGBX101010:
  2279. return DRM_FORMAT_XBGR2101010;
  2280. }
  2281. }
  2282. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2283. {
  2284. switch (format) {
  2285. case PLANE_CTL_FORMAT_RGB_565:
  2286. return DRM_FORMAT_RGB565;
  2287. default:
  2288. case PLANE_CTL_FORMAT_XRGB_8888:
  2289. if (rgb_order) {
  2290. if (alpha)
  2291. return DRM_FORMAT_ABGR8888;
  2292. else
  2293. return DRM_FORMAT_XBGR8888;
  2294. } else {
  2295. if (alpha)
  2296. return DRM_FORMAT_ARGB8888;
  2297. else
  2298. return DRM_FORMAT_XRGB8888;
  2299. }
  2300. case PLANE_CTL_FORMAT_XRGB_2101010:
  2301. if (rgb_order)
  2302. return DRM_FORMAT_XBGR2101010;
  2303. else
  2304. return DRM_FORMAT_XRGB2101010;
  2305. }
  2306. }
  2307. static bool
  2308. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2309. struct intel_initial_plane_config *plane_config)
  2310. {
  2311. struct drm_device *dev = crtc->base.dev;
  2312. struct drm_i915_private *dev_priv = to_i915(dev);
  2313. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2314. struct drm_i915_gem_object *obj = NULL;
  2315. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2316. struct drm_framebuffer *fb = &plane_config->fb->base;
  2317. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2318. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2319. PAGE_SIZE);
  2320. size_aligned -= base_aligned;
  2321. if (plane_config->size == 0)
  2322. return false;
  2323. /* If the FB is too big, just don't use it since fbdev is not very
  2324. * important and we should probably use that space with FBC or other
  2325. * features. */
  2326. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2327. return false;
  2328. mutex_lock(&dev->struct_mutex);
  2329. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2330. base_aligned,
  2331. base_aligned,
  2332. size_aligned);
  2333. if (!obj) {
  2334. mutex_unlock(&dev->struct_mutex);
  2335. return false;
  2336. }
  2337. if (plane_config->tiling == I915_TILING_X)
  2338. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2339. mode_cmd.pixel_format = fb->pixel_format;
  2340. mode_cmd.width = fb->width;
  2341. mode_cmd.height = fb->height;
  2342. mode_cmd.pitches[0] = fb->pitches[0];
  2343. mode_cmd.modifier[0] = fb->modifier[0];
  2344. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2345. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2346. &mode_cmd, obj)) {
  2347. DRM_DEBUG_KMS("intel fb init failed\n");
  2348. goto out_unref_obj;
  2349. }
  2350. mutex_unlock(&dev->struct_mutex);
  2351. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2352. return true;
  2353. out_unref_obj:
  2354. i915_gem_object_put(obj);
  2355. mutex_unlock(&dev->struct_mutex);
  2356. return false;
  2357. }
  2358. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2359. static void
  2360. update_state_fb(struct drm_plane *plane)
  2361. {
  2362. if (plane->fb == plane->state->fb)
  2363. return;
  2364. if (plane->state->fb)
  2365. drm_framebuffer_unreference(plane->state->fb);
  2366. plane->state->fb = plane->fb;
  2367. if (plane->state->fb)
  2368. drm_framebuffer_reference(plane->state->fb);
  2369. }
  2370. static void
  2371. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2372. struct intel_initial_plane_config *plane_config)
  2373. {
  2374. struct drm_device *dev = intel_crtc->base.dev;
  2375. struct drm_i915_private *dev_priv = to_i915(dev);
  2376. struct drm_crtc *c;
  2377. struct intel_crtc *i;
  2378. struct drm_i915_gem_object *obj;
  2379. struct drm_plane *primary = intel_crtc->base.primary;
  2380. struct drm_plane_state *plane_state = primary->state;
  2381. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2382. struct intel_plane *intel_plane = to_intel_plane(primary);
  2383. struct intel_plane_state *intel_state =
  2384. to_intel_plane_state(plane_state);
  2385. struct drm_framebuffer *fb;
  2386. if (!plane_config->fb)
  2387. return;
  2388. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2389. fb = &plane_config->fb->base;
  2390. goto valid_fb;
  2391. }
  2392. kfree(plane_config->fb);
  2393. /*
  2394. * Failed to alloc the obj, check to see if we should share
  2395. * an fb with another CRTC instead
  2396. */
  2397. for_each_crtc(dev, c) {
  2398. i = to_intel_crtc(c);
  2399. if (c == &intel_crtc->base)
  2400. continue;
  2401. if (!i->active)
  2402. continue;
  2403. fb = c->primary->fb;
  2404. if (!fb)
  2405. continue;
  2406. obj = intel_fb_obj(fb);
  2407. if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
  2408. drm_framebuffer_reference(fb);
  2409. goto valid_fb;
  2410. }
  2411. }
  2412. /*
  2413. * We've failed to reconstruct the BIOS FB. Current display state
  2414. * indicates that the primary plane is visible, but has a NULL FB,
  2415. * which will lead to problems later if we don't fix it up. The
  2416. * simplest solution is to just disable the primary plane now and
  2417. * pretend the BIOS never had it enabled.
  2418. */
  2419. to_intel_plane_state(plane_state)->base.visible = false;
  2420. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2421. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2422. intel_plane->disable_plane(primary, &intel_crtc->base);
  2423. return;
  2424. valid_fb:
  2425. plane_state->src_x = 0;
  2426. plane_state->src_y = 0;
  2427. plane_state->src_w = fb->width << 16;
  2428. plane_state->src_h = fb->height << 16;
  2429. plane_state->crtc_x = 0;
  2430. plane_state->crtc_y = 0;
  2431. plane_state->crtc_w = fb->width;
  2432. plane_state->crtc_h = fb->height;
  2433. intel_state->base.src.x1 = plane_state->src_x;
  2434. intel_state->base.src.y1 = plane_state->src_y;
  2435. intel_state->base.src.x2 = plane_state->src_x + plane_state->src_w;
  2436. intel_state->base.src.y2 = plane_state->src_y + plane_state->src_h;
  2437. intel_state->base.dst.x1 = plane_state->crtc_x;
  2438. intel_state->base.dst.y1 = plane_state->crtc_y;
  2439. intel_state->base.dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2440. intel_state->base.dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2441. obj = intel_fb_obj(fb);
  2442. if (i915_gem_object_is_tiled(obj))
  2443. dev_priv->preserve_bios_swizzle = true;
  2444. drm_framebuffer_reference(fb);
  2445. primary->fb = primary->state->fb = fb;
  2446. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2447. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2448. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2449. &obj->frontbuffer_bits);
  2450. }
  2451. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2452. unsigned int rotation)
  2453. {
  2454. int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2455. switch (fb->modifier[plane]) {
  2456. case DRM_FORMAT_MOD_NONE:
  2457. case I915_FORMAT_MOD_X_TILED:
  2458. switch (cpp) {
  2459. case 8:
  2460. return 4096;
  2461. case 4:
  2462. case 2:
  2463. case 1:
  2464. return 8192;
  2465. default:
  2466. MISSING_CASE(cpp);
  2467. break;
  2468. }
  2469. break;
  2470. case I915_FORMAT_MOD_Y_TILED:
  2471. case I915_FORMAT_MOD_Yf_TILED:
  2472. switch (cpp) {
  2473. case 8:
  2474. return 2048;
  2475. case 4:
  2476. return 4096;
  2477. case 2:
  2478. case 1:
  2479. return 8192;
  2480. default:
  2481. MISSING_CASE(cpp);
  2482. break;
  2483. }
  2484. break;
  2485. default:
  2486. MISSING_CASE(fb->modifier[plane]);
  2487. }
  2488. return 2048;
  2489. }
  2490. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2491. {
  2492. const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
  2493. const struct drm_framebuffer *fb = plane_state->base.fb;
  2494. unsigned int rotation = plane_state->base.rotation;
  2495. int x = plane_state->base.src.x1 >> 16;
  2496. int y = plane_state->base.src.y1 >> 16;
  2497. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2498. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2499. int max_width = skl_max_plane_width(fb, 0, rotation);
  2500. int max_height = 4096;
  2501. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2502. if (w > max_width || h > max_height) {
  2503. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2504. w, h, max_width, max_height);
  2505. return -EINVAL;
  2506. }
  2507. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2508. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2509. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  2510. /*
  2511. * AUX surface offset is specified as the distance from the
  2512. * main surface offset, and it must be non-negative. Make
  2513. * sure that is what we will get.
  2514. */
  2515. if (offset > aux_offset)
  2516. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2517. offset, aux_offset & ~(alignment - 1));
  2518. /*
  2519. * When using an X-tiled surface, the plane blows up
  2520. * if the x offset + width exceed the stride.
  2521. *
  2522. * TODO: linear and Y-tiled seem fine, Yf untested,
  2523. */
  2524. if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED) {
  2525. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2526. while ((x + w) * cpp > fb->pitches[0]) {
  2527. if (offset == 0) {
  2528. DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
  2529. return -EINVAL;
  2530. }
  2531. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2532. offset, offset - alignment);
  2533. }
  2534. }
  2535. plane_state->main.offset = offset;
  2536. plane_state->main.x = x;
  2537. plane_state->main.y = y;
  2538. return 0;
  2539. }
  2540. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2541. {
  2542. const struct drm_framebuffer *fb = plane_state->base.fb;
  2543. unsigned int rotation = plane_state->base.rotation;
  2544. int max_width = skl_max_plane_width(fb, 1, rotation);
  2545. int max_height = 4096;
  2546. int x = plane_state->base.src.x1 >> 17;
  2547. int y = plane_state->base.src.y1 >> 17;
  2548. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2549. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2550. u32 offset;
  2551. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2552. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2553. /* FIXME not quite sure how/if these apply to the chroma plane */
  2554. if (w > max_width || h > max_height) {
  2555. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2556. w, h, max_width, max_height);
  2557. return -EINVAL;
  2558. }
  2559. plane_state->aux.offset = offset;
  2560. plane_state->aux.x = x;
  2561. plane_state->aux.y = y;
  2562. return 0;
  2563. }
  2564. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2565. {
  2566. const struct drm_framebuffer *fb = plane_state->base.fb;
  2567. unsigned int rotation = plane_state->base.rotation;
  2568. int ret;
  2569. if (!plane_state->base.visible)
  2570. return 0;
  2571. /* Rotate src coordinates to match rotated GTT view */
  2572. if (intel_rotation_90_or_270(rotation))
  2573. drm_rect_rotate(&plane_state->base.src,
  2574. fb->width << 16, fb->height << 16,
  2575. DRM_ROTATE_270);
  2576. /*
  2577. * Handle the AUX surface first since
  2578. * the main surface setup depends on it.
  2579. */
  2580. if (fb->pixel_format == DRM_FORMAT_NV12) {
  2581. ret = skl_check_nv12_aux_surface(plane_state);
  2582. if (ret)
  2583. return ret;
  2584. } else {
  2585. plane_state->aux.offset = ~0xfff;
  2586. plane_state->aux.x = 0;
  2587. plane_state->aux.y = 0;
  2588. }
  2589. ret = skl_check_main_surface(plane_state);
  2590. if (ret)
  2591. return ret;
  2592. return 0;
  2593. }
  2594. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2595. const struct intel_crtc_state *crtc_state,
  2596. const struct intel_plane_state *plane_state)
  2597. {
  2598. struct drm_device *dev = primary->dev;
  2599. struct drm_i915_private *dev_priv = to_i915(dev);
  2600. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2601. struct drm_framebuffer *fb = plane_state->base.fb;
  2602. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2603. int plane = intel_crtc->plane;
  2604. u32 linear_offset;
  2605. u32 dspcntr;
  2606. i915_reg_t reg = DSPCNTR(plane);
  2607. unsigned int rotation = plane_state->base.rotation;
  2608. int x = plane_state->base.src.x1 >> 16;
  2609. int y = plane_state->base.src.y1 >> 16;
  2610. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2611. dspcntr |= DISPLAY_PLANE_ENABLE;
  2612. if (INTEL_INFO(dev)->gen < 4) {
  2613. if (intel_crtc->pipe == PIPE_B)
  2614. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2615. /* pipesrc and dspsize control the size that is scaled from,
  2616. * which should always be the user's requested size.
  2617. */
  2618. I915_WRITE(DSPSIZE(plane),
  2619. ((crtc_state->pipe_src_h - 1) << 16) |
  2620. (crtc_state->pipe_src_w - 1));
  2621. I915_WRITE(DSPPOS(plane), 0);
  2622. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2623. I915_WRITE(PRIMSIZE(plane),
  2624. ((crtc_state->pipe_src_h - 1) << 16) |
  2625. (crtc_state->pipe_src_w - 1));
  2626. I915_WRITE(PRIMPOS(plane), 0);
  2627. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2628. }
  2629. switch (fb->pixel_format) {
  2630. case DRM_FORMAT_C8:
  2631. dspcntr |= DISPPLANE_8BPP;
  2632. break;
  2633. case DRM_FORMAT_XRGB1555:
  2634. dspcntr |= DISPPLANE_BGRX555;
  2635. break;
  2636. case DRM_FORMAT_RGB565:
  2637. dspcntr |= DISPPLANE_BGRX565;
  2638. break;
  2639. case DRM_FORMAT_XRGB8888:
  2640. dspcntr |= DISPPLANE_BGRX888;
  2641. break;
  2642. case DRM_FORMAT_XBGR8888:
  2643. dspcntr |= DISPPLANE_RGBX888;
  2644. break;
  2645. case DRM_FORMAT_XRGB2101010:
  2646. dspcntr |= DISPPLANE_BGRX101010;
  2647. break;
  2648. case DRM_FORMAT_XBGR2101010:
  2649. dspcntr |= DISPPLANE_RGBX101010;
  2650. break;
  2651. default:
  2652. BUG();
  2653. }
  2654. if (INTEL_GEN(dev_priv) >= 4 &&
  2655. fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
  2656. dspcntr |= DISPPLANE_TILED;
  2657. if (IS_G4X(dev))
  2658. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2659. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2660. if (INTEL_INFO(dev)->gen >= 4)
  2661. intel_crtc->dspaddr_offset =
  2662. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2663. if (rotation == DRM_ROTATE_180) {
  2664. dspcntr |= DISPPLANE_ROTATE_180;
  2665. x += (crtc_state->pipe_src_w - 1);
  2666. y += (crtc_state->pipe_src_h - 1);
  2667. }
  2668. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2669. if (INTEL_INFO(dev)->gen < 4)
  2670. intel_crtc->dspaddr_offset = linear_offset;
  2671. intel_crtc->adjusted_x = x;
  2672. intel_crtc->adjusted_y = y;
  2673. I915_WRITE(reg, dspcntr);
  2674. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2675. if (INTEL_INFO(dev)->gen >= 4) {
  2676. I915_WRITE(DSPSURF(plane),
  2677. intel_fb_gtt_offset(fb, rotation) +
  2678. intel_crtc->dspaddr_offset);
  2679. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2680. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2681. } else
  2682. I915_WRITE(DSPADDR(plane), i915_gem_object_ggtt_offset(obj, NULL) + linear_offset);
  2683. POSTING_READ(reg);
  2684. }
  2685. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2686. struct drm_crtc *crtc)
  2687. {
  2688. struct drm_device *dev = crtc->dev;
  2689. struct drm_i915_private *dev_priv = to_i915(dev);
  2690. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2691. int plane = intel_crtc->plane;
  2692. I915_WRITE(DSPCNTR(plane), 0);
  2693. if (INTEL_INFO(dev_priv)->gen >= 4)
  2694. I915_WRITE(DSPSURF(plane), 0);
  2695. else
  2696. I915_WRITE(DSPADDR(plane), 0);
  2697. POSTING_READ(DSPCNTR(plane));
  2698. }
  2699. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2700. const struct intel_crtc_state *crtc_state,
  2701. const struct intel_plane_state *plane_state)
  2702. {
  2703. struct drm_device *dev = primary->dev;
  2704. struct drm_i915_private *dev_priv = to_i915(dev);
  2705. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2706. struct drm_framebuffer *fb = plane_state->base.fb;
  2707. int plane = intel_crtc->plane;
  2708. u32 linear_offset;
  2709. u32 dspcntr;
  2710. i915_reg_t reg = DSPCNTR(plane);
  2711. unsigned int rotation = plane_state->base.rotation;
  2712. int x = plane_state->base.src.x1 >> 16;
  2713. int y = plane_state->base.src.y1 >> 16;
  2714. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2715. dspcntr |= DISPLAY_PLANE_ENABLE;
  2716. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2717. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2718. switch (fb->pixel_format) {
  2719. case DRM_FORMAT_C8:
  2720. dspcntr |= DISPPLANE_8BPP;
  2721. break;
  2722. case DRM_FORMAT_RGB565:
  2723. dspcntr |= DISPPLANE_BGRX565;
  2724. break;
  2725. case DRM_FORMAT_XRGB8888:
  2726. dspcntr |= DISPPLANE_BGRX888;
  2727. break;
  2728. case DRM_FORMAT_XBGR8888:
  2729. dspcntr |= DISPPLANE_RGBX888;
  2730. break;
  2731. case DRM_FORMAT_XRGB2101010:
  2732. dspcntr |= DISPPLANE_BGRX101010;
  2733. break;
  2734. case DRM_FORMAT_XBGR2101010:
  2735. dspcntr |= DISPPLANE_RGBX101010;
  2736. break;
  2737. default:
  2738. BUG();
  2739. }
  2740. if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
  2741. dspcntr |= DISPPLANE_TILED;
  2742. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2743. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2744. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2745. intel_crtc->dspaddr_offset =
  2746. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2747. if (rotation == DRM_ROTATE_180) {
  2748. dspcntr |= DISPPLANE_ROTATE_180;
  2749. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2750. x += (crtc_state->pipe_src_w - 1);
  2751. y += (crtc_state->pipe_src_h - 1);
  2752. }
  2753. }
  2754. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2755. intel_crtc->adjusted_x = x;
  2756. intel_crtc->adjusted_y = y;
  2757. I915_WRITE(reg, dspcntr);
  2758. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2759. I915_WRITE(DSPSURF(plane),
  2760. intel_fb_gtt_offset(fb, rotation) +
  2761. intel_crtc->dspaddr_offset);
  2762. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2763. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2764. } else {
  2765. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2766. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2767. }
  2768. POSTING_READ(reg);
  2769. }
  2770. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2771. uint64_t fb_modifier, uint32_t pixel_format)
  2772. {
  2773. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2774. return 64;
  2775. } else {
  2776. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2777. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2778. }
  2779. }
  2780. u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
  2781. unsigned int rotation)
  2782. {
  2783. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2784. struct i915_ggtt_view view;
  2785. struct i915_vma *vma;
  2786. intel_fill_fb_ggtt_view(&view, fb, rotation);
  2787. vma = i915_gem_object_to_ggtt(obj, &view);
  2788. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2789. view.type))
  2790. return -1;
  2791. return i915_ggtt_offset(vma);
  2792. }
  2793. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2794. {
  2795. struct drm_device *dev = intel_crtc->base.dev;
  2796. struct drm_i915_private *dev_priv = to_i915(dev);
  2797. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2798. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2799. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2800. }
  2801. /*
  2802. * This function detaches (aka. unbinds) unused scalers in hardware
  2803. */
  2804. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2805. {
  2806. struct intel_crtc_scaler_state *scaler_state;
  2807. int i;
  2808. scaler_state = &intel_crtc->config->scaler_state;
  2809. /* loop through and disable scalers that aren't in use */
  2810. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2811. if (!scaler_state->scalers[i].in_use)
  2812. skl_detach_scaler(intel_crtc, i);
  2813. }
  2814. }
  2815. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2816. unsigned int rotation)
  2817. {
  2818. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2819. u32 stride = intel_fb_pitch(fb, plane, rotation);
  2820. /*
  2821. * The stride is either expressed as a multiple of 64 bytes chunks for
  2822. * linear buffers or in number of tiles for tiled buffers.
  2823. */
  2824. if (intel_rotation_90_or_270(rotation)) {
  2825. int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2826. stride /= intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2827. } else {
  2828. stride /= intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2829. fb->pixel_format);
  2830. }
  2831. return stride;
  2832. }
  2833. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2834. {
  2835. switch (pixel_format) {
  2836. case DRM_FORMAT_C8:
  2837. return PLANE_CTL_FORMAT_INDEXED;
  2838. case DRM_FORMAT_RGB565:
  2839. return PLANE_CTL_FORMAT_RGB_565;
  2840. case DRM_FORMAT_XBGR8888:
  2841. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2842. case DRM_FORMAT_XRGB8888:
  2843. return PLANE_CTL_FORMAT_XRGB_8888;
  2844. /*
  2845. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2846. * to be already pre-multiplied. We need to add a knob (or a different
  2847. * DRM_FORMAT) for user-space to configure that.
  2848. */
  2849. case DRM_FORMAT_ABGR8888:
  2850. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2851. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2852. case DRM_FORMAT_ARGB8888:
  2853. return PLANE_CTL_FORMAT_XRGB_8888 |
  2854. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2855. case DRM_FORMAT_XRGB2101010:
  2856. return PLANE_CTL_FORMAT_XRGB_2101010;
  2857. case DRM_FORMAT_XBGR2101010:
  2858. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2859. case DRM_FORMAT_YUYV:
  2860. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2861. case DRM_FORMAT_YVYU:
  2862. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2863. case DRM_FORMAT_UYVY:
  2864. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2865. case DRM_FORMAT_VYUY:
  2866. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2867. default:
  2868. MISSING_CASE(pixel_format);
  2869. }
  2870. return 0;
  2871. }
  2872. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2873. {
  2874. switch (fb_modifier) {
  2875. case DRM_FORMAT_MOD_NONE:
  2876. break;
  2877. case I915_FORMAT_MOD_X_TILED:
  2878. return PLANE_CTL_TILED_X;
  2879. case I915_FORMAT_MOD_Y_TILED:
  2880. return PLANE_CTL_TILED_Y;
  2881. case I915_FORMAT_MOD_Yf_TILED:
  2882. return PLANE_CTL_TILED_YF;
  2883. default:
  2884. MISSING_CASE(fb_modifier);
  2885. }
  2886. return 0;
  2887. }
  2888. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2889. {
  2890. switch (rotation) {
  2891. case DRM_ROTATE_0:
  2892. break;
  2893. /*
  2894. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2895. * while i915 HW rotation is clockwise, thats why this swapping.
  2896. */
  2897. case DRM_ROTATE_90:
  2898. return PLANE_CTL_ROTATE_270;
  2899. case DRM_ROTATE_180:
  2900. return PLANE_CTL_ROTATE_180;
  2901. case DRM_ROTATE_270:
  2902. return PLANE_CTL_ROTATE_90;
  2903. default:
  2904. MISSING_CASE(rotation);
  2905. }
  2906. return 0;
  2907. }
  2908. static void skylake_update_primary_plane(struct drm_plane *plane,
  2909. const struct intel_crtc_state *crtc_state,
  2910. const struct intel_plane_state *plane_state)
  2911. {
  2912. struct drm_device *dev = plane->dev;
  2913. struct drm_i915_private *dev_priv = to_i915(dev);
  2914. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2915. struct drm_framebuffer *fb = plane_state->base.fb;
  2916. const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
  2917. int pipe = intel_crtc->pipe;
  2918. u32 plane_ctl;
  2919. unsigned int rotation = plane_state->base.rotation;
  2920. u32 stride = skl_plane_stride(fb, 0, rotation);
  2921. u32 surf_addr = plane_state->main.offset;
  2922. int scaler_id = plane_state->scaler_id;
  2923. int src_x = plane_state->main.x;
  2924. int src_y = plane_state->main.y;
  2925. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2926. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2927. int dst_x = plane_state->base.dst.x1;
  2928. int dst_y = plane_state->base.dst.y1;
  2929. int dst_w = drm_rect_width(&plane_state->base.dst);
  2930. int dst_h = drm_rect_height(&plane_state->base.dst);
  2931. plane_ctl = PLANE_CTL_ENABLE |
  2932. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2933. PLANE_CTL_PIPE_CSC_ENABLE;
  2934. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2935. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2936. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2937. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2938. /* Sizes are 0 based */
  2939. src_w--;
  2940. src_h--;
  2941. dst_w--;
  2942. dst_h--;
  2943. intel_crtc->dspaddr_offset = surf_addr;
  2944. intel_crtc->adjusted_x = src_x;
  2945. intel_crtc->adjusted_y = src_y;
  2946. if (wm->dirty_pipes & drm_crtc_mask(&intel_crtc->base))
  2947. skl_write_plane_wm(intel_crtc, wm, 0);
  2948. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2949. I915_WRITE(PLANE_OFFSET(pipe, 0), (src_y << 16) | src_x);
  2950. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2951. I915_WRITE(PLANE_SIZE(pipe, 0), (src_h << 16) | src_w);
  2952. if (scaler_id >= 0) {
  2953. uint32_t ps_ctrl = 0;
  2954. WARN_ON(!dst_w || !dst_h);
  2955. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2956. crtc_state->scaler_state.scalers[scaler_id].mode;
  2957. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2958. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2959. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2960. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2961. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2962. } else {
  2963. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2964. }
  2965. I915_WRITE(PLANE_SURF(pipe, 0),
  2966. intel_fb_gtt_offset(fb, rotation) + surf_addr);
  2967. POSTING_READ(PLANE_SURF(pipe, 0));
  2968. }
  2969. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2970. struct drm_crtc *crtc)
  2971. {
  2972. struct drm_device *dev = crtc->dev;
  2973. struct drm_i915_private *dev_priv = to_i915(dev);
  2974. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2975. int pipe = intel_crtc->pipe;
  2976. /*
  2977. * We only populate skl_results on watermark updates, and if the
  2978. * plane's visiblity isn't actually changing neither is its watermarks.
  2979. */
  2980. if (!crtc->primary->state->visible)
  2981. skl_write_plane_wm(intel_crtc, &dev_priv->wm.skl_results, 0);
  2982. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2983. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2984. POSTING_READ(PLANE_SURF(pipe, 0));
  2985. }
  2986. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2987. static int
  2988. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2989. int x, int y, enum mode_set_atomic state)
  2990. {
  2991. /* Support for kgdboc is disabled, this needs a major rework. */
  2992. DRM_ERROR("legacy panic handler not supported any more.\n");
  2993. return -ENODEV;
  2994. }
  2995. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2996. {
  2997. struct intel_crtc *crtc;
  2998. for_each_intel_crtc(&dev_priv->drm, crtc)
  2999. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  3000. }
  3001. static void intel_update_primary_planes(struct drm_device *dev)
  3002. {
  3003. struct drm_crtc *crtc;
  3004. for_each_crtc(dev, crtc) {
  3005. struct intel_plane *plane = to_intel_plane(crtc->primary);
  3006. struct intel_plane_state *plane_state =
  3007. to_intel_plane_state(plane->base.state);
  3008. if (plane_state->base.visible)
  3009. plane->update_plane(&plane->base,
  3010. to_intel_crtc_state(crtc->state),
  3011. plane_state);
  3012. }
  3013. }
  3014. static int
  3015. __intel_display_resume(struct drm_device *dev,
  3016. struct drm_atomic_state *state)
  3017. {
  3018. struct drm_crtc_state *crtc_state;
  3019. struct drm_crtc *crtc;
  3020. int i, ret;
  3021. intel_modeset_setup_hw_state(dev);
  3022. i915_redisable_vga(dev);
  3023. if (!state)
  3024. return 0;
  3025. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3026. /*
  3027. * Force recalculation even if we restore
  3028. * current state. With fast modeset this may not result
  3029. * in a modeset when the state is compatible.
  3030. */
  3031. crtc_state->mode_changed = true;
  3032. }
  3033. /* ignore any reset values/BIOS leftovers in the WM registers */
  3034. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3035. ret = drm_atomic_commit(state);
  3036. WARN_ON(ret == -EDEADLK);
  3037. return ret;
  3038. }
  3039. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3040. {
  3041. return intel_has_gpu_reset(dev_priv) &&
  3042. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3043. }
  3044. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3045. {
  3046. struct drm_device *dev = &dev_priv->drm;
  3047. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3048. struct drm_atomic_state *state;
  3049. int ret;
  3050. /*
  3051. * Need mode_config.mutex so that we don't
  3052. * trample ongoing ->detect() and whatnot.
  3053. */
  3054. mutex_lock(&dev->mode_config.mutex);
  3055. drm_modeset_acquire_init(ctx, 0);
  3056. while (1) {
  3057. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3058. if (ret != -EDEADLK)
  3059. break;
  3060. drm_modeset_backoff(ctx);
  3061. }
  3062. /* reset doesn't touch the display, but flips might get nuked anyway, */
  3063. if (!i915.force_reset_modeset_test &&
  3064. !gpu_reset_clobbers_display(dev_priv))
  3065. return;
  3066. /*
  3067. * Disabling the crtcs gracefully seems nicer. Also the
  3068. * g33 docs say we should at least disable all the planes.
  3069. */
  3070. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3071. if (IS_ERR(state)) {
  3072. ret = PTR_ERR(state);
  3073. state = NULL;
  3074. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3075. goto err;
  3076. }
  3077. ret = drm_atomic_helper_disable_all(dev, ctx);
  3078. if (ret) {
  3079. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3080. goto err;
  3081. }
  3082. dev_priv->modeset_restore_state = state;
  3083. state->acquire_ctx = ctx;
  3084. return;
  3085. err:
  3086. drm_atomic_state_free(state);
  3087. }
  3088. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3089. {
  3090. struct drm_device *dev = &dev_priv->drm;
  3091. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3092. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3093. int ret;
  3094. /*
  3095. * Flips in the rings will be nuked by the reset,
  3096. * so complete all pending flips so that user space
  3097. * will get its events and not get stuck.
  3098. */
  3099. intel_complete_page_flips(dev_priv);
  3100. dev_priv->modeset_restore_state = NULL;
  3101. dev_priv->modeset_restore_state = NULL;
  3102. /* reset doesn't touch the display */
  3103. if (!gpu_reset_clobbers_display(dev_priv)) {
  3104. if (!state) {
  3105. /*
  3106. * Flips in the rings have been nuked by the reset,
  3107. * so update the base address of all primary
  3108. * planes to the the last fb to make sure we're
  3109. * showing the correct fb after a reset.
  3110. *
  3111. * FIXME: Atomic will make this obsolete since we won't schedule
  3112. * CS-based flips (which might get lost in gpu resets) any more.
  3113. */
  3114. intel_update_primary_planes(dev);
  3115. } else {
  3116. ret = __intel_display_resume(dev, state);
  3117. if (ret)
  3118. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3119. }
  3120. } else {
  3121. /*
  3122. * The display has been reset as well,
  3123. * so need a full re-initialization.
  3124. */
  3125. intel_runtime_pm_disable_interrupts(dev_priv);
  3126. intel_runtime_pm_enable_interrupts(dev_priv);
  3127. intel_pps_unlock_regs_wa(dev_priv);
  3128. intel_modeset_init_hw(dev);
  3129. spin_lock_irq(&dev_priv->irq_lock);
  3130. if (dev_priv->display.hpd_irq_setup)
  3131. dev_priv->display.hpd_irq_setup(dev_priv);
  3132. spin_unlock_irq(&dev_priv->irq_lock);
  3133. ret = __intel_display_resume(dev, state);
  3134. if (ret)
  3135. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3136. intel_hpd_init(dev_priv);
  3137. }
  3138. drm_modeset_drop_locks(ctx);
  3139. drm_modeset_acquire_fini(ctx);
  3140. mutex_unlock(&dev->mode_config.mutex);
  3141. }
  3142. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3143. {
  3144. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3145. if (i915_reset_in_progress(error))
  3146. return true;
  3147. if (crtc->reset_count != i915_reset_count(error))
  3148. return true;
  3149. return false;
  3150. }
  3151. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3152. {
  3153. struct drm_device *dev = crtc->dev;
  3154. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3155. bool pending;
  3156. if (abort_flip_on_reset(intel_crtc))
  3157. return false;
  3158. spin_lock_irq(&dev->event_lock);
  3159. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3160. spin_unlock_irq(&dev->event_lock);
  3161. return pending;
  3162. }
  3163. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3164. struct intel_crtc_state *old_crtc_state)
  3165. {
  3166. struct drm_device *dev = crtc->base.dev;
  3167. struct drm_i915_private *dev_priv = to_i915(dev);
  3168. struct intel_crtc_state *pipe_config =
  3169. to_intel_crtc_state(crtc->base.state);
  3170. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3171. crtc->base.mode = crtc->base.state->mode;
  3172. /*
  3173. * Update pipe size and adjust fitter if needed: the reason for this is
  3174. * that in compute_mode_changes we check the native mode (not the pfit
  3175. * mode) to see if we can flip rather than do a full mode set. In the
  3176. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3177. * pfit state, we'll end up with a big fb scanned out into the wrong
  3178. * sized surface.
  3179. */
  3180. I915_WRITE(PIPESRC(crtc->pipe),
  3181. ((pipe_config->pipe_src_w - 1) << 16) |
  3182. (pipe_config->pipe_src_h - 1));
  3183. /* on skylake this is done by detaching scalers */
  3184. if (INTEL_INFO(dev)->gen >= 9) {
  3185. skl_detach_scalers(crtc);
  3186. if (pipe_config->pch_pfit.enabled)
  3187. skylake_pfit_enable(crtc);
  3188. } else if (HAS_PCH_SPLIT(dev)) {
  3189. if (pipe_config->pch_pfit.enabled)
  3190. ironlake_pfit_enable(crtc);
  3191. else if (old_crtc_state->pch_pfit.enabled)
  3192. ironlake_pfit_disable(crtc, true);
  3193. }
  3194. }
  3195. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  3196. {
  3197. struct drm_device *dev = crtc->dev;
  3198. struct drm_i915_private *dev_priv = to_i915(dev);
  3199. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3200. int pipe = intel_crtc->pipe;
  3201. i915_reg_t reg;
  3202. u32 temp;
  3203. /* enable normal train */
  3204. reg = FDI_TX_CTL(pipe);
  3205. temp = I915_READ(reg);
  3206. if (IS_IVYBRIDGE(dev)) {
  3207. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3208. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3209. } else {
  3210. temp &= ~FDI_LINK_TRAIN_NONE;
  3211. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3212. }
  3213. I915_WRITE(reg, temp);
  3214. reg = FDI_RX_CTL(pipe);
  3215. temp = I915_READ(reg);
  3216. if (HAS_PCH_CPT(dev)) {
  3217. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3218. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3219. } else {
  3220. temp &= ~FDI_LINK_TRAIN_NONE;
  3221. temp |= FDI_LINK_TRAIN_NONE;
  3222. }
  3223. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3224. /* wait one idle pattern time */
  3225. POSTING_READ(reg);
  3226. udelay(1000);
  3227. /* IVB wants error correction enabled */
  3228. if (IS_IVYBRIDGE(dev))
  3229. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3230. FDI_FE_ERRC_ENABLE);
  3231. }
  3232. /* The FDI link training functions for ILK/Ibexpeak. */
  3233. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  3234. {
  3235. struct drm_device *dev = crtc->dev;
  3236. struct drm_i915_private *dev_priv = to_i915(dev);
  3237. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3238. int pipe = intel_crtc->pipe;
  3239. i915_reg_t reg;
  3240. u32 temp, tries;
  3241. /* FDI needs bits from pipe first */
  3242. assert_pipe_enabled(dev_priv, pipe);
  3243. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3244. for train result */
  3245. reg = FDI_RX_IMR(pipe);
  3246. temp = I915_READ(reg);
  3247. temp &= ~FDI_RX_SYMBOL_LOCK;
  3248. temp &= ~FDI_RX_BIT_LOCK;
  3249. I915_WRITE(reg, temp);
  3250. I915_READ(reg);
  3251. udelay(150);
  3252. /* enable CPU FDI TX and PCH FDI RX */
  3253. reg = FDI_TX_CTL(pipe);
  3254. temp = I915_READ(reg);
  3255. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3256. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3257. temp &= ~FDI_LINK_TRAIN_NONE;
  3258. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3259. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3260. reg = FDI_RX_CTL(pipe);
  3261. temp = I915_READ(reg);
  3262. temp &= ~FDI_LINK_TRAIN_NONE;
  3263. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3264. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3265. POSTING_READ(reg);
  3266. udelay(150);
  3267. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3268. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3269. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3270. FDI_RX_PHASE_SYNC_POINTER_EN);
  3271. reg = FDI_RX_IIR(pipe);
  3272. for (tries = 0; tries < 5; tries++) {
  3273. temp = I915_READ(reg);
  3274. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3275. if ((temp & FDI_RX_BIT_LOCK)) {
  3276. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3277. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3278. break;
  3279. }
  3280. }
  3281. if (tries == 5)
  3282. DRM_ERROR("FDI train 1 fail!\n");
  3283. /* Train 2 */
  3284. reg = FDI_TX_CTL(pipe);
  3285. temp = I915_READ(reg);
  3286. temp &= ~FDI_LINK_TRAIN_NONE;
  3287. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3288. I915_WRITE(reg, temp);
  3289. reg = FDI_RX_CTL(pipe);
  3290. temp = I915_READ(reg);
  3291. temp &= ~FDI_LINK_TRAIN_NONE;
  3292. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3293. I915_WRITE(reg, temp);
  3294. POSTING_READ(reg);
  3295. udelay(150);
  3296. reg = FDI_RX_IIR(pipe);
  3297. for (tries = 0; tries < 5; tries++) {
  3298. temp = I915_READ(reg);
  3299. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3300. if (temp & FDI_RX_SYMBOL_LOCK) {
  3301. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3302. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3303. break;
  3304. }
  3305. }
  3306. if (tries == 5)
  3307. DRM_ERROR("FDI train 2 fail!\n");
  3308. DRM_DEBUG_KMS("FDI train done\n");
  3309. }
  3310. static const int snb_b_fdi_train_param[] = {
  3311. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3312. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3313. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3314. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3315. };
  3316. /* The FDI link training functions for SNB/Cougarpoint. */
  3317. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3318. {
  3319. struct drm_device *dev = crtc->dev;
  3320. struct drm_i915_private *dev_priv = to_i915(dev);
  3321. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3322. int pipe = intel_crtc->pipe;
  3323. i915_reg_t reg;
  3324. u32 temp, i, retry;
  3325. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3326. for train result */
  3327. reg = FDI_RX_IMR(pipe);
  3328. temp = I915_READ(reg);
  3329. temp &= ~FDI_RX_SYMBOL_LOCK;
  3330. temp &= ~FDI_RX_BIT_LOCK;
  3331. I915_WRITE(reg, temp);
  3332. POSTING_READ(reg);
  3333. udelay(150);
  3334. /* enable CPU FDI TX and PCH FDI RX */
  3335. reg = FDI_TX_CTL(pipe);
  3336. temp = I915_READ(reg);
  3337. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3338. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3339. temp &= ~FDI_LINK_TRAIN_NONE;
  3340. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3341. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3342. /* SNB-B */
  3343. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3344. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3345. I915_WRITE(FDI_RX_MISC(pipe),
  3346. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3347. reg = FDI_RX_CTL(pipe);
  3348. temp = I915_READ(reg);
  3349. if (HAS_PCH_CPT(dev)) {
  3350. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3351. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3352. } else {
  3353. temp &= ~FDI_LINK_TRAIN_NONE;
  3354. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3355. }
  3356. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3357. POSTING_READ(reg);
  3358. udelay(150);
  3359. for (i = 0; i < 4; i++) {
  3360. reg = FDI_TX_CTL(pipe);
  3361. temp = I915_READ(reg);
  3362. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3363. temp |= snb_b_fdi_train_param[i];
  3364. I915_WRITE(reg, temp);
  3365. POSTING_READ(reg);
  3366. udelay(500);
  3367. for (retry = 0; retry < 5; retry++) {
  3368. reg = FDI_RX_IIR(pipe);
  3369. temp = I915_READ(reg);
  3370. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3371. if (temp & FDI_RX_BIT_LOCK) {
  3372. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3373. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3374. break;
  3375. }
  3376. udelay(50);
  3377. }
  3378. if (retry < 5)
  3379. break;
  3380. }
  3381. if (i == 4)
  3382. DRM_ERROR("FDI train 1 fail!\n");
  3383. /* Train 2 */
  3384. reg = FDI_TX_CTL(pipe);
  3385. temp = I915_READ(reg);
  3386. temp &= ~FDI_LINK_TRAIN_NONE;
  3387. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3388. if (IS_GEN6(dev)) {
  3389. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3390. /* SNB-B */
  3391. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3392. }
  3393. I915_WRITE(reg, temp);
  3394. reg = FDI_RX_CTL(pipe);
  3395. temp = I915_READ(reg);
  3396. if (HAS_PCH_CPT(dev)) {
  3397. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3398. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3399. } else {
  3400. temp &= ~FDI_LINK_TRAIN_NONE;
  3401. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3402. }
  3403. I915_WRITE(reg, temp);
  3404. POSTING_READ(reg);
  3405. udelay(150);
  3406. for (i = 0; i < 4; i++) {
  3407. reg = FDI_TX_CTL(pipe);
  3408. temp = I915_READ(reg);
  3409. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3410. temp |= snb_b_fdi_train_param[i];
  3411. I915_WRITE(reg, temp);
  3412. POSTING_READ(reg);
  3413. udelay(500);
  3414. for (retry = 0; retry < 5; retry++) {
  3415. reg = FDI_RX_IIR(pipe);
  3416. temp = I915_READ(reg);
  3417. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3418. if (temp & FDI_RX_SYMBOL_LOCK) {
  3419. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3420. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3421. break;
  3422. }
  3423. udelay(50);
  3424. }
  3425. if (retry < 5)
  3426. break;
  3427. }
  3428. if (i == 4)
  3429. DRM_ERROR("FDI train 2 fail!\n");
  3430. DRM_DEBUG_KMS("FDI train done.\n");
  3431. }
  3432. /* Manual link training for Ivy Bridge A0 parts */
  3433. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3434. {
  3435. struct drm_device *dev = crtc->dev;
  3436. struct drm_i915_private *dev_priv = to_i915(dev);
  3437. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3438. int pipe = intel_crtc->pipe;
  3439. i915_reg_t reg;
  3440. u32 temp, i, j;
  3441. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3442. for train result */
  3443. reg = FDI_RX_IMR(pipe);
  3444. temp = I915_READ(reg);
  3445. temp &= ~FDI_RX_SYMBOL_LOCK;
  3446. temp &= ~FDI_RX_BIT_LOCK;
  3447. I915_WRITE(reg, temp);
  3448. POSTING_READ(reg);
  3449. udelay(150);
  3450. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3451. I915_READ(FDI_RX_IIR(pipe)));
  3452. /* Try each vswing and preemphasis setting twice before moving on */
  3453. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3454. /* disable first in case we need to retry */
  3455. reg = FDI_TX_CTL(pipe);
  3456. temp = I915_READ(reg);
  3457. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3458. temp &= ~FDI_TX_ENABLE;
  3459. I915_WRITE(reg, temp);
  3460. reg = FDI_RX_CTL(pipe);
  3461. temp = I915_READ(reg);
  3462. temp &= ~FDI_LINK_TRAIN_AUTO;
  3463. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3464. temp &= ~FDI_RX_ENABLE;
  3465. I915_WRITE(reg, temp);
  3466. /* enable CPU FDI TX and PCH FDI RX */
  3467. reg = FDI_TX_CTL(pipe);
  3468. temp = I915_READ(reg);
  3469. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3470. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3471. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3472. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3473. temp |= snb_b_fdi_train_param[j/2];
  3474. temp |= FDI_COMPOSITE_SYNC;
  3475. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3476. I915_WRITE(FDI_RX_MISC(pipe),
  3477. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3478. reg = FDI_RX_CTL(pipe);
  3479. temp = I915_READ(reg);
  3480. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3481. temp |= FDI_COMPOSITE_SYNC;
  3482. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3483. POSTING_READ(reg);
  3484. udelay(1); /* should be 0.5us */
  3485. for (i = 0; i < 4; i++) {
  3486. reg = FDI_RX_IIR(pipe);
  3487. temp = I915_READ(reg);
  3488. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3489. if (temp & FDI_RX_BIT_LOCK ||
  3490. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3491. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3492. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3493. i);
  3494. break;
  3495. }
  3496. udelay(1); /* should be 0.5us */
  3497. }
  3498. if (i == 4) {
  3499. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3500. continue;
  3501. }
  3502. /* Train 2 */
  3503. reg = FDI_TX_CTL(pipe);
  3504. temp = I915_READ(reg);
  3505. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3506. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3507. I915_WRITE(reg, temp);
  3508. reg = FDI_RX_CTL(pipe);
  3509. temp = I915_READ(reg);
  3510. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3511. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3512. I915_WRITE(reg, temp);
  3513. POSTING_READ(reg);
  3514. udelay(2); /* should be 1.5us */
  3515. for (i = 0; i < 4; i++) {
  3516. reg = FDI_RX_IIR(pipe);
  3517. temp = I915_READ(reg);
  3518. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3519. if (temp & FDI_RX_SYMBOL_LOCK ||
  3520. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3521. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3522. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3523. i);
  3524. goto train_done;
  3525. }
  3526. udelay(2); /* should be 1.5us */
  3527. }
  3528. if (i == 4)
  3529. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3530. }
  3531. train_done:
  3532. DRM_DEBUG_KMS("FDI train done.\n");
  3533. }
  3534. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3535. {
  3536. struct drm_device *dev = intel_crtc->base.dev;
  3537. struct drm_i915_private *dev_priv = to_i915(dev);
  3538. int pipe = intel_crtc->pipe;
  3539. i915_reg_t reg;
  3540. u32 temp;
  3541. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3542. reg = FDI_RX_CTL(pipe);
  3543. temp = I915_READ(reg);
  3544. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3545. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3546. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3547. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3548. POSTING_READ(reg);
  3549. udelay(200);
  3550. /* Switch from Rawclk to PCDclk */
  3551. temp = I915_READ(reg);
  3552. I915_WRITE(reg, temp | FDI_PCDCLK);
  3553. POSTING_READ(reg);
  3554. udelay(200);
  3555. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3556. reg = FDI_TX_CTL(pipe);
  3557. temp = I915_READ(reg);
  3558. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3559. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3560. POSTING_READ(reg);
  3561. udelay(100);
  3562. }
  3563. }
  3564. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3565. {
  3566. struct drm_device *dev = intel_crtc->base.dev;
  3567. struct drm_i915_private *dev_priv = to_i915(dev);
  3568. int pipe = intel_crtc->pipe;
  3569. i915_reg_t reg;
  3570. u32 temp;
  3571. /* Switch from PCDclk to Rawclk */
  3572. reg = FDI_RX_CTL(pipe);
  3573. temp = I915_READ(reg);
  3574. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3575. /* Disable CPU FDI TX PLL */
  3576. reg = FDI_TX_CTL(pipe);
  3577. temp = I915_READ(reg);
  3578. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3579. POSTING_READ(reg);
  3580. udelay(100);
  3581. reg = FDI_RX_CTL(pipe);
  3582. temp = I915_READ(reg);
  3583. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3584. /* Wait for the clocks to turn off. */
  3585. POSTING_READ(reg);
  3586. udelay(100);
  3587. }
  3588. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3589. {
  3590. struct drm_device *dev = crtc->dev;
  3591. struct drm_i915_private *dev_priv = to_i915(dev);
  3592. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3593. int pipe = intel_crtc->pipe;
  3594. i915_reg_t reg;
  3595. u32 temp;
  3596. /* disable CPU FDI tx and PCH FDI rx */
  3597. reg = FDI_TX_CTL(pipe);
  3598. temp = I915_READ(reg);
  3599. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3600. POSTING_READ(reg);
  3601. reg = FDI_RX_CTL(pipe);
  3602. temp = I915_READ(reg);
  3603. temp &= ~(0x7 << 16);
  3604. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3605. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3606. POSTING_READ(reg);
  3607. udelay(100);
  3608. /* Ironlake workaround, disable clock pointer after downing FDI */
  3609. if (HAS_PCH_IBX(dev))
  3610. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3611. /* still set train pattern 1 */
  3612. reg = FDI_TX_CTL(pipe);
  3613. temp = I915_READ(reg);
  3614. temp &= ~FDI_LINK_TRAIN_NONE;
  3615. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3616. I915_WRITE(reg, temp);
  3617. reg = FDI_RX_CTL(pipe);
  3618. temp = I915_READ(reg);
  3619. if (HAS_PCH_CPT(dev)) {
  3620. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3621. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3622. } else {
  3623. temp &= ~FDI_LINK_TRAIN_NONE;
  3624. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3625. }
  3626. /* BPC in FDI rx is consistent with that in PIPECONF */
  3627. temp &= ~(0x07 << 16);
  3628. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3629. I915_WRITE(reg, temp);
  3630. POSTING_READ(reg);
  3631. udelay(100);
  3632. }
  3633. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3634. {
  3635. struct intel_crtc *crtc;
  3636. /* Note that we don't need to be called with mode_config.lock here
  3637. * as our list of CRTC objects is static for the lifetime of the
  3638. * device and so cannot disappear as we iterate. Similarly, we can
  3639. * happily treat the predicates as racy, atomic checks as userspace
  3640. * cannot claim and pin a new fb without at least acquring the
  3641. * struct_mutex and so serialising with us.
  3642. */
  3643. for_each_intel_crtc(dev, crtc) {
  3644. if (atomic_read(&crtc->unpin_work_count) == 0)
  3645. continue;
  3646. if (crtc->flip_work)
  3647. intel_wait_for_vblank(dev, crtc->pipe);
  3648. return true;
  3649. }
  3650. return false;
  3651. }
  3652. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3653. {
  3654. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3655. struct intel_flip_work *work = intel_crtc->flip_work;
  3656. intel_crtc->flip_work = NULL;
  3657. if (work->event)
  3658. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3659. drm_crtc_vblank_put(&intel_crtc->base);
  3660. wake_up_all(&dev_priv->pending_flip_queue);
  3661. trace_i915_flip_complete(intel_crtc->plane,
  3662. work->pending_flip_obj);
  3663. queue_work(dev_priv->wq, &work->unpin_work);
  3664. }
  3665. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3666. {
  3667. struct drm_device *dev = crtc->dev;
  3668. struct drm_i915_private *dev_priv = to_i915(dev);
  3669. long ret;
  3670. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3671. ret = wait_event_interruptible_timeout(
  3672. dev_priv->pending_flip_queue,
  3673. !intel_crtc_has_pending_flip(crtc),
  3674. 60*HZ);
  3675. if (ret < 0)
  3676. return ret;
  3677. if (ret == 0) {
  3678. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3679. struct intel_flip_work *work;
  3680. spin_lock_irq(&dev->event_lock);
  3681. work = intel_crtc->flip_work;
  3682. if (work && !is_mmio_work(work)) {
  3683. WARN_ONCE(1, "Removing stuck page flip\n");
  3684. page_flip_completed(intel_crtc);
  3685. }
  3686. spin_unlock_irq(&dev->event_lock);
  3687. }
  3688. return 0;
  3689. }
  3690. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3691. {
  3692. u32 temp;
  3693. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3694. mutex_lock(&dev_priv->sb_lock);
  3695. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3696. temp |= SBI_SSCCTL_DISABLE;
  3697. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3698. mutex_unlock(&dev_priv->sb_lock);
  3699. }
  3700. /* Program iCLKIP clock to the desired frequency */
  3701. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3702. {
  3703. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3704. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3705. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3706. u32 temp;
  3707. lpt_disable_iclkip(dev_priv);
  3708. /* The iCLK virtual clock root frequency is in MHz,
  3709. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3710. * divisors, it is necessary to divide one by another, so we
  3711. * convert the virtual clock precision to KHz here for higher
  3712. * precision.
  3713. */
  3714. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3715. u32 iclk_virtual_root_freq = 172800 * 1000;
  3716. u32 iclk_pi_range = 64;
  3717. u32 desired_divisor;
  3718. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3719. clock << auxdiv);
  3720. divsel = (desired_divisor / iclk_pi_range) - 2;
  3721. phaseinc = desired_divisor % iclk_pi_range;
  3722. /*
  3723. * Near 20MHz is a corner case which is
  3724. * out of range for the 7-bit divisor
  3725. */
  3726. if (divsel <= 0x7f)
  3727. break;
  3728. }
  3729. /* This should not happen with any sane values */
  3730. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3731. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3732. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3733. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3734. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3735. clock,
  3736. auxdiv,
  3737. divsel,
  3738. phasedir,
  3739. phaseinc);
  3740. mutex_lock(&dev_priv->sb_lock);
  3741. /* Program SSCDIVINTPHASE6 */
  3742. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3743. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3744. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3745. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3746. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3747. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3748. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3749. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3750. /* Program SSCAUXDIV */
  3751. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3752. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3753. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3754. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3755. /* Enable modulator and associated divider */
  3756. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3757. temp &= ~SBI_SSCCTL_DISABLE;
  3758. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3759. mutex_unlock(&dev_priv->sb_lock);
  3760. /* Wait for initialization time */
  3761. udelay(24);
  3762. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3763. }
  3764. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3765. {
  3766. u32 divsel, phaseinc, auxdiv;
  3767. u32 iclk_virtual_root_freq = 172800 * 1000;
  3768. u32 iclk_pi_range = 64;
  3769. u32 desired_divisor;
  3770. u32 temp;
  3771. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3772. return 0;
  3773. mutex_lock(&dev_priv->sb_lock);
  3774. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3775. if (temp & SBI_SSCCTL_DISABLE) {
  3776. mutex_unlock(&dev_priv->sb_lock);
  3777. return 0;
  3778. }
  3779. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3780. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3781. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3782. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3783. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3784. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3785. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3786. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3787. mutex_unlock(&dev_priv->sb_lock);
  3788. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3789. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3790. desired_divisor << auxdiv);
  3791. }
  3792. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3793. enum pipe pch_transcoder)
  3794. {
  3795. struct drm_device *dev = crtc->base.dev;
  3796. struct drm_i915_private *dev_priv = to_i915(dev);
  3797. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3798. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3799. I915_READ(HTOTAL(cpu_transcoder)));
  3800. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3801. I915_READ(HBLANK(cpu_transcoder)));
  3802. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3803. I915_READ(HSYNC(cpu_transcoder)));
  3804. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3805. I915_READ(VTOTAL(cpu_transcoder)));
  3806. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3807. I915_READ(VBLANK(cpu_transcoder)));
  3808. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3809. I915_READ(VSYNC(cpu_transcoder)));
  3810. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3811. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3812. }
  3813. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3814. {
  3815. struct drm_i915_private *dev_priv = to_i915(dev);
  3816. uint32_t temp;
  3817. temp = I915_READ(SOUTH_CHICKEN1);
  3818. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3819. return;
  3820. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3821. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3822. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3823. if (enable)
  3824. temp |= FDI_BC_BIFURCATION_SELECT;
  3825. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3826. I915_WRITE(SOUTH_CHICKEN1, temp);
  3827. POSTING_READ(SOUTH_CHICKEN1);
  3828. }
  3829. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3830. {
  3831. struct drm_device *dev = intel_crtc->base.dev;
  3832. switch (intel_crtc->pipe) {
  3833. case PIPE_A:
  3834. break;
  3835. case PIPE_B:
  3836. if (intel_crtc->config->fdi_lanes > 2)
  3837. cpt_set_fdi_bc_bifurcation(dev, false);
  3838. else
  3839. cpt_set_fdi_bc_bifurcation(dev, true);
  3840. break;
  3841. case PIPE_C:
  3842. cpt_set_fdi_bc_bifurcation(dev, true);
  3843. break;
  3844. default:
  3845. BUG();
  3846. }
  3847. }
  3848. /* Return which DP Port should be selected for Transcoder DP control */
  3849. static enum port
  3850. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3851. {
  3852. struct drm_device *dev = crtc->dev;
  3853. struct intel_encoder *encoder;
  3854. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3855. if (encoder->type == INTEL_OUTPUT_DP ||
  3856. encoder->type == INTEL_OUTPUT_EDP)
  3857. return enc_to_dig_port(&encoder->base)->port;
  3858. }
  3859. return -1;
  3860. }
  3861. /*
  3862. * Enable PCH resources required for PCH ports:
  3863. * - PCH PLLs
  3864. * - FDI training & RX/TX
  3865. * - update transcoder timings
  3866. * - DP transcoding bits
  3867. * - transcoder
  3868. */
  3869. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3870. {
  3871. struct drm_device *dev = crtc->dev;
  3872. struct drm_i915_private *dev_priv = to_i915(dev);
  3873. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3874. int pipe = intel_crtc->pipe;
  3875. u32 temp;
  3876. assert_pch_transcoder_disabled(dev_priv, pipe);
  3877. if (IS_IVYBRIDGE(dev))
  3878. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3879. /* Write the TU size bits before fdi link training, so that error
  3880. * detection works. */
  3881. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3882. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3883. /* For PCH output, training FDI link */
  3884. dev_priv->display.fdi_link_train(crtc);
  3885. /* We need to program the right clock selection before writing the pixel
  3886. * mutliplier into the DPLL. */
  3887. if (HAS_PCH_CPT(dev)) {
  3888. u32 sel;
  3889. temp = I915_READ(PCH_DPLL_SEL);
  3890. temp |= TRANS_DPLL_ENABLE(pipe);
  3891. sel = TRANS_DPLLB_SEL(pipe);
  3892. if (intel_crtc->config->shared_dpll ==
  3893. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3894. temp |= sel;
  3895. else
  3896. temp &= ~sel;
  3897. I915_WRITE(PCH_DPLL_SEL, temp);
  3898. }
  3899. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3900. * transcoder, and we actually should do this to not upset any PCH
  3901. * transcoder that already use the clock when we share it.
  3902. *
  3903. * Note that enable_shared_dpll tries to do the right thing, but
  3904. * get_shared_dpll unconditionally resets the pll - we need that to have
  3905. * the right LVDS enable sequence. */
  3906. intel_enable_shared_dpll(intel_crtc);
  3907. /* set transcoder timing, panel must allow it */
  3908. assert_panel_unlocked(dev_priv, pipe);
  3909. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3910. intel_fdi_normal_train(crtc);
  3911. /* For PCH DP, enable TRANS_DP_CTL */
  3912. if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3913. const struct drm_display_mode *adjusted_mode =
  3914. &intel_crtc->config->base.adjusted_mode;
  3915. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3916. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3917. temp = I915_READ(reg);
  3918. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3919. TRANS_DP_SYNC_MASK |
  3920. TRANS_DP_BPC_MASK);
  3921. temp |= TRANS_DP_OUTPUT_ENABLE;
  3922. temp |= bpc << 9; /* same format but at 11:9 */
  3923. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3924. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3925. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3926. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3927. switch (intel_trans_dp_port_sel(crtc)) {
  3928. case PORT_B:
  3929. temp |= TRANS_DP_PORT_SEL_B;
  3930. break;
  3931. case PORT_C:
  3932. temp |= TRANS_DP_PORT_SEL_C;
  3933. break;
  3934. case PORT_D:
  3935. temp |= TRANS_DP_PORT_SEL_D;
  3936. break;
  3937. default:
  3938. BUG();
  3939. }
  3940. I915_WRITE(reg, temp);
  3941. }
  3942. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3943. }
  3944. static void lpt_pch_enable(struct drm_crtc *crtc)
  3945. {
  3946. struct drm_device *dev = crtc->dev;
  3947. struct drm_i915_private *dev_priv = to_i915(dev);
  3948. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3949. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3950. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3951. lpt_program_iclkip(crtc);
  3952. /* Set transcoder timing. */
  3953. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3954. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3955. }
  3956. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3957. {
  3958. struct drm_i915_private *dev_priv = to_i915(dev);
  3959. i915_reg_t dslreg = PIPEDSL(pipe);
  3960. u32 temp;
  3961. temp = I915_READ(dslreg);
  3962. udelay(500);
  3963. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3964. if (wait_for(I915_READ(dslreg) != temp, 5))
  3965. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3966. }
  3967. }
  3968. static int
  3969. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3970. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3971. int src_w, int src_h, int dst_w, int dst_h)
  3972. {
  3973. struct intel_crtc_scaler_state *scaler_state =
  3974. &crtc_state->scaler_state;
  3975. struct intel_crtc *intel_crtc =
  3976. to_intel_crtc(crtc_state->base.crtc);
  3977. int need_scaling;
  3978. need_scaling = intel_rotation_90_or_270(rotation) ?
  3979. (src_h != dst_w || src_w != dst_h):
  3980. (src_w != dst_w || src_h != dst_h);
  3981. /*
  3982. * if plane is being disabled or scaler is no more required or force detach
  3983. * - free scaler binded to this plane/crtc
  3984. * - in order to do this, update crtc->scaler_usage
  3985. *
  3986. * Here scaler state in crtc_state is set free so that
  3987. * scaler can be assigned to other user. Actual register
  3988. * update to free the scaler is done in plane/panel-fit programming.
  3989. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3990. */
  3991. if (force_detach || !need_scaling) {
  3992. if (*scaler_id >= 0) {
  3993. scaler_state->scaler_users &= ~(1 << scaler_user);
  3994. scaler_state->scalers[*scaler_id].in_use = 0;
  3995. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3996. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3997. intel_crtc->pipe, scaler_user, *scaler_id,
  3998. scaler_state->scaler_users);
  3999. *scaler_id = -1;
  4000. }
  4001. return 0;
  4002. }
  4003. /* range checks */
  4004. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  4005. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  4006. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  4007. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  4008. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4009. "size is out of scaler range\n",
  4010. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4011. return -EINVAL;
  4012. }
  4013. /* mark this plane as a scaler user in crtc_state */
  4014. scaler_state->scaler_users |= (1 << scaler_user);
  4015. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4016. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4017. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4018. scaler_state->scaler_users);
  4019. return 0;
  4020. }
  4021. /**
  4022. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4023. *
  4024. * @state: crtc's scaler state
  4025. *
  4026. * Return
  4027. * 0 - scaler_usage updated successfully
  4028. * error - requested scaling cannot be supported or other error condition
  4029. */
  4030. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4031. {
  4032. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  4033. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4034. DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
  4035. intel_crtc->base.base.id, intel_crtc->base.name,
  4036. intel_crtc->pipe, SKL_CRTC_INDEX);
  4037. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4038. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  4039. state->pipe_src_w, state->pipe_src_h,
  4040. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4041. }
  4042. /**
  4043. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4044. *
  4045. * @state: crtc's scaler state
  4046. * @plane_state: atomic plane state to update
  4047. *
  4048. * Return
  4049. * 0 - scaler_usage updated successfully
  4050. * error - requested scaling cannot be supported or other error condition
  4051. */
  4052. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4053. struct intel_plane_state *plane_state)
  4054. {
  4055. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  4056. struct intel_plane *intel_plane =
  4057. to_intel_plane(plane_state->base.plane);
  4058. struct drm_framebuffer *fb = plane_state->base.fb;
  4059. int ret;
  4060. bool force_detach = !fb || !plane_state->base.visible;
  4061. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
  4062. intel_plane->base.base.id, intel_plane->base.name,
  4063. intel_crtc->pipe, drm_plane_index(&intel_plane->base));
  4064. ret = skl_update_scaler(crtc_state, force_detach,
  4065. drm_plane_index(&intel_plane->base),
  4066. &plane_state->scaler_id,
  4067. plane_state->base.rotation,
  4068. drm_rect_width(&plane_state->base.src) >> 16,
  4069. drm_rect_height(&plane_state->base.src) >> 16,
  4070. drm_rect_width(&plane_state->base.dst),
  4071. drm_rect_height(&plane_state->base.dst));
  4072. if (ret || plane_state->scaler_id < 0)
  4073. return ret;
  4074. /* check colorkey */
  4075. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4076. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4077. intel_plane->base.base.id,
  4078. intel_plane->base.name);
  4079. return -EINVAL;
  4080. }
  4081. /* Check src format */
  4082. switch (fb->pixel_format) {
  4083. case DRM_FORMAT_RGB565:
  4084. case DRM_FORMAT_XBGR8888:
  4085. case DRM_FORMAT_XRGB8888:
  4086. case DRM_FORMAT_ABGR8888:
  4087. case DRM_FORMAT_ARGB8888:
  4088. case DRM_FORMAT_XRGB2101010:
  4089. case DRM_FORMAT_XBGR2101010:
  4090. case DRM_FORMAT_YUYV:
  4091. case DRM_FORMAT_YVYU:
  4092. case DRM_FORMAT_UYVY:
  4093. case DRM_FORMAT_VYUY:
  4094. break;
  4095. default:
  4096. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4097. intel_plane->base.base.id, intel_plane->base.name,
  4098. fb->base.id, fb->pixel_format);
  4099. return -EINVAL;
  4100. }
  4101. return 0;
  4102. }
  4103. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4104. {
  4105. int i;
  4106. for (i = 0; i < crtc->num_scalers; i++)
  4107. skl_detach_scaler(crtc, i);
  4108. }
  4109. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4110. {
  4111. struct drm_device *dev = crtc->base.dev;
  4112. struct drm_i915_private *dev_priv = to_i915(dev);
  4113. int pipe = crtc->pipe;
  4114. struct intel_crtc_scaler_state *scaler_state =
  4115. &crtc->config->scaler_state;
  4116. if (crtc->config->pch_pfit.enabled) {
  4117. int id;
  4118. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4119. return;
  4120. id = scaler_state->scaler_id;
  4121. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4122. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4123. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4124. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4125. }
  4126. }
  4127. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4128. {
  4129. struct drm_device *dev = crtc->base.dev;
  4130. struct drm_i915_private *dev_priv = to_i915(dev);
  4131. int pipe = crtc->pipe;
  4132. if (crtc->config->pch_pfit.enabled) {
  4133. /* Force use of hard-coded filter coefficients
  4134. * as some pre-programmed values are broken,
  4135. * e.g. x201.
  4136. */
  4137. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  4138. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4139. PF_PIPE_SEL_IVB(pipe));
  4140. else
  4141. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4142. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4143. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4144. }
  4145. }
  4146. void hsw_enable_ips(struct intel_crtc *crtc)
  4147. {
  4148. struct drm_device *dev = crtc->base.dev;
  4149. struct drm_i915_private *dev_priv = to_i915(dev);
  4150. if (!crtc->config->ips_enabled)
  4151. return;
  4152. /*
  4153. * We can only enable IPS after we enable a plane and wait for a vblank
  4154. * This function is called from post_plane_update, which is run after
  4155. * a vblank wait.
  4156. */
  4157. assert_plane_enabled(dev_priv, crtc->plane);
  4158. if (IS_BROADWELL(dev)) {
  4159. mutex_lock(&dev_priv->rps.hw_lock);
  4160. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4161. mutex_unlock(&dev_priv->rps.hw_lock);
  4162. /* Quoting Art Runyan: "its not safe to expect any particular
  4163. * value in IPS_CTL bit 31 after enabling IPS through the
  4164. * mailbox." Moreover, the mailbox may return a bogus state,
  4165. * so we need to just enable it and continue on.
  4166. */
  4167. } else {
  4168. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4169. /* The bit only becomes 1 in the next vblank, so this wait here
  4170. * is essentially intel_wait_for_vblank. If we don't have this
  4171. * and don't wait for vblanks until the end of crtc_enable, then
  4172. * the HW state readout code will complain that the expected
  4173. * IPS_CTL value is not the one we read. */
  4174. if (intel_wait_for_register(dev_priv,
  4175. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4176. 50))
  4177. DRM_ERROR("Timed out waiting for IPS enable\n");
  4178. }
  4179. }
  4180. void hsw_disable_ips(struct intel_crtc *crtc)
  4181. {
  4182. struct drm_device *dev = crtc->base.dev;
  4183. struct drm_i915_private *dev_priv = to_i915(dev);
  4184. if (!crtc->config->ips_enabled)
  4185. return;
  4186. assert_plane_enabled(dev_priv, crtc->plane);
  4187. if (IS_BROADWELL(dev)) {
  4188. mutex_lock(&dev_priv->rps.hw_lock);
  4189. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4190. mutex_unlock(&dev_priv->rps.hw_lock);
  4191. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4192. if (intel_wait_for_register(dev_priv,
  4193. IPS_CTL, IPS_ENABLE, 0,
  4194. 42))
  4195. DRM_ERROR("Timed out waiting for IPS disable\n");
  4196. } else {
  4197. I915_WRITE(IPS_CTL, 0);
  4198. POSTING_READ(IPS_CTL);
  4199. }
  4200. /* We need to wait for a vblank before we can disable the plane. */
  4201. intel_wait_for_vblank(dev, crtc->pipe);
  4202. }
  4203. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4204. {
  4205. if (intel_crtc->overlay) {
  4206. struct drm_device *dev = intel_crtc->base.dev;
  4207. struct drm_i915_private *dev_priv = to_i915(dev);
  4208. mutex_lock(&dev->struct_mutex);
  4209. dev_priv->mm.interruptible = false;
  4210. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4211. dev_priv->mm.interruptible = true;
  4212. mutex_unlock(&dev->struct_mutex);
  4213. }
  4214. /* Let userspace switch the overlay on again. In most cases userspace
  4215. * has to recompute where to put it anyway.
  4216. */
  4217. }
  4218. /**
  4219. * intel_post_enable_primary - Perform operations after enabling primary plane
  4220. * @crtc: the CRTC whose primary plane was just enabled
  4221. *
  4222. * Performs potentially sleeping operations that must be done after the primary
  4223. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4224. * called due to an explicit primary plane update, or due to an implicit
  4225. * re-enable that is caused when a sprite plane is updated to no longer
  4226. * completely hide the primary plane.
  4227. */
  4228. static void
  4229. intel_post_enable_primary(struct drm_crtc *crtc)
  4230. {
  4231. struct drm_device *dev = crtc->dev;
  4232. struct drm_i915_private *dev_priv = to_i915(dev);
  4233. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4234. int pipe = intel_crtc->pipe;
  4235. /*
  4236. * FIXME IPS should be fine as long as one plane is
  4237. * enabled, but in practice it seems to have problems
  4238. * when going from primary only to sprite only and vice
  4239. * versa.
  4240. */
  4241. hsw_enable_ips(intel_crtc);
  4242. /*
  4243. * Gen2 reports pipe underruns whenever all planes are disabled.
  4244. * So don't enable underrun reporting before at least some planes
  4245. * are enabled.
  4246. * FIXME: Need to fix the logic to work when we turn off all planes
  4247. * but leave the pipe running.
  4248. */
  4249. if (IS_GEN2(dev))
  4250. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4251. /* Underruns don't always raise interrupts, so check manually. */
  4252. intel_check_cpu_fifo_underruns(dev_priv);
  4253. intel_check_pch_fifo_underruns(dev_priv);
  4254. }
  4255. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4256. static void
  4257. intel_pre_disable_primary(struct drm_crtc *crtc)
  4258. {
  4259. struct drm_device *dev = crtc->dev;
  4260. struct drm_i915_private *dev_priv = to_i915(dev);
  4261. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4262. int pipe = intel_crtc->pipe;
  4263. /*
  4264. * Gen2 reports pipe underruns whenever all planes are disabled.
  4265. * So diasble underrun reporting before all the planes get disabled.
  4266. * FIXME: Need to fix the logic to work when we turn off all planes
  4267. * but leave the pipe running.
  4268. */
  4269. if (IS_GEN2(dev))
  4270. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4271. /*
  4272. * FIXME IPS should be fine as long as one plane is
  4273. * enabled, but in practice it seems to have problems
  4274. * when going from primary only to sprite only and vice
  4275. * versa.
  4276. */
  4277. hsw_disable_ips(intel_crtc);
  4278. }
  4279. /* FIXME get rid of this and use pre_plane_update */
  4280. static void
  4281. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4282. {
  4283. struct drm_device *dev = crtc->dev;
  4284. struct drm_i915_private *dev_priv = to_i915(dev);
  4285. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4286. int pipe = intel_crtc->pipe;
  4287. intel_pre_disable_primary(crtc);
  4288. /*
  4289. * Vblank time updates from the shadow to live plane control register
  4290. * are blocked if the memory self-refresh mode is active at that
  4291. * moment. So to make sure the plane gets truly disabled, disable
  4292. * first the self-refresh mode. The self-refresh enable bit in turn
  4293. * will be checked/applied by the HW only at the next frame start
  4294. * event which is after the vblank start event, so we need to have a
  4295. * wait-for-vblank between disabling the plane and the pipe.
  4296. */
  4297. if (HAS_GMCH_DISPLAY(dev)) {
  4298. intel_set_memory_cxsr(dev_priv, false);
  4299. dev_priv->wm.vlv.cxsr = false;
  4300. intel_wait_for_vblank(dev, pipe);
  4301. }
  4302. }
  4303. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4304. {
  4305. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4306. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4307. struct intel_crtc_state *pipe_config =
  4308. to_intel_crtc_state(crtc->base.state);
  4309. struct drm_plane *primary = crtc->base.primary;
  4310. struct drm_plane_state *old_pri_state =
  4311. drm_atomic_get_existing_plane_state(old_state, primary);
  4312. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4313. crtc->wm.cxsr_allowed = true;
  4314. if (pipe_config->update_wm_post && pipe_config->base.active)
  4315. intel_update_watermarks(&crtc->base);
  4316. if (old_pri_state) {
  4317. struct intel_plane_state *primary_state =
  4318. to_intel_plane_state(primary->state);
  4319. struct intel_plane_state *old_primary_state =
  4320. to_intel_plane_state(old_pri_state);
  4321. intel_fbc_post_update(crtc);
  4322. if (primary_state->base.visible &&
  4323. (needs_modeset(&pipe_config->base) ||
  4324. !old_primary_state->base.visible))
  4325. intel_post_enable_primary(&crtc->base);
  4326. }
  4327. }
  4328. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  4329. {
  4330. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4331. struct drm_device *dev = crtc->base.dev;
  4332. struct drm_i915_private *dev_priv = to_i915(dev);
  4333. struct intel_crtc_state *pipe_config =
  4334. to_intel_crtc_state(crtc->base.state);
  4335. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4336. struct drm_plane *primary = crtc->base.primary;
  4337. struct drm_plane_state *old_pri_state =
  4338. drm_atomic_get_existing_plane_state(old_state, primary);
  4339. bool modeset = needs_modeset(&pipe_config->base);
  4340. if (old_pri_state) {
  4341. struct intel_plane_state *primary_state =
  4342. to_intel_plane_state(primary->state);
  4343. struct intel_plane_state *old_primary_state =
  4344. to_intel_plane_state(old_pri_state);
  4345. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4346. if (old_primary_state->base.visible &&
  4347. (modeset || !primary_state->base.visible))
  4348. intel_pre_disable_primary(&crtc->base);
  4349. }
  4350. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
  4351. crtc->wm.cxsr_allowed = false;
  4352. /*
  4353. * Vblank time updates from the shadow to live plane control register
  4354. * are blocked if the memory self-refresh mode is active at that
  4355. * moment. So to make sure the plane gets truly disabled, disable
  4356. * first the self-refresh mode. The self-refresh enable bit in turn
  4357. * will be checked/applied by the HW only at the next frame start
  4358. * event which is after the vblank start event, so we need to have a
  4359. * wait-for-vblank between disabling the plane and the pipe.
  4360. */
  4361. if (old_crtc_state->base.active) {
  4362. intel_set_memory_cxsr(dev_priv, false);
  4363. dev_priv->wm.vlv.cxsr = false;
  4364. intel_wait_for_vblank(dev, crtc->pipe);
  4365. }
  4366. }
  4367. /*
  4368. * IVB workaround: must disable low power watermarks for at least
  4369. * one frame before enabling scaling. LP watermarks can be re-enabled
  4370. * when scaling is disabled.
  4371. *
  4372. * WaCxSRDisabledForSpriteScaling:ivb
  4373. */
  4374. if (pipe_config->disable_lp_wm) {
  4375. ilk_disable_lp_wm(dev);
  4376. intel_wait_for_vblank(dev, crtc->pipe);
  4377. }
  4378. /*
  4379. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4380. * watermark programming here.
  4381. */
  4382. if (needs_modeset(&pipe_config->base))
  4383. return;
  4384. /*
  4385. * For platforms that support atomic watermarks, program the
  4386. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4387. * will be the intermediate values that are safe for both pre- and
  4388. * post- vblank; when vblank happens, the 'active' values will be set
  4389. * to the final 'target' values and we'll do this again to get the
  4390. * optimal watermarks. For gen9+ platforms, the values we program here
  4391. * will be the final target values which will get automatically latched
  4392. * at vblank time; no further programming will be necessary.
  4393. *
  4394. * If a platform hasn't been transitioned to atomic watermarks yet,
  4395. * we'll continue to update watermarks the old way, if flags tell
  4396. * us to.
  4397. */
  4398. if (dev_priv->display.initial_watermarks != NULL)
  4399. dev_priv->display.initial_watermarks(pipe_config);
  4400. else if (pipe_config->update_wm_pre)
  4401. intel_update_watermarks(&crtc->base);
  4402. }
  4403. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4404. {
  4405. struct drm_device *dev = crtc->dev;
  4406. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4407. struct drm_plane *p;
  4408. int pipe = intel_crtc->pipe;
  4409. intel_crtc_dpms_overlay_disable(intel_crtc);
  4410. drm_for_each_plane_mask(p, dev, plane_mask)
  4411. to_intel_plane(p)->disable_plane(p, crtc);
  4412. /*
  4413. * FIXME: Once we grow proper nuclear flip support out of this we need
  4414. * to compute the mask of flip planes precisely. For the time being
  4415. * consider this a flip to a NULL plane.
  4416. */
  4417. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4418. }
  4419. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4420. struct intel_crtc_state *crtc_state,
  4421. struct drm_atomic_state *old_state)
  4422. {
  4423. struct drm_connector_state *old_conn_state;
  4424. struct drm_connector *conn;
  4425. int i;
  4426. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4427. struct drm_connector_state *conn_state = conn->state;
  4428. struct intel_encoder *encoder =
  4429. to_intel_encoder(conn_state->best_encoder);
  4430. if (conn_state->crtc != crtc)
  4431. continue;
  4432. if (encoder->pre_pll_enable)
  4433. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4434. }
  4435. }
  4436. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4437. struct intel_crtc_state *crtc_state,
  4438. struct drm_atomic_state *old_state)
  4439. {
  4440. struct drm_connector_state *old_conn_state;
  4441. struct drm_connector *conn;
  4442. int i;
  4443. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4444. struct drm_connector_state *conn_state = conn->state;
  4445. struct intel_encoder *encoder =
  4446. to_intel_encoder(conn_state->best_encoder);
  4447. if (conn_state->crtc != crtc)
  4448. continue;
  4449. if (encoder->pre_enable)
  4450. encoder->pre_enable(encoder, crtc_state, conn_state);
  4451. }
  4452. }
  4453. static void intel_encoders_enable(struct drm_crtc *crtc,
  4454. struct intel_crtc_state *crtc_state,
  4455. struct drm_atomic_state *old_state)
  4456. {
  4457. struct drm_connector_state *old_conn_state;
  4458. struct drm_connector *conn;
  4459. int i;
  4460. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4461. struct drm_connector_state *conn_state = conn->state;
  4462. struct intel_encoder *encoder =
  4463. to_intel_encoder(conn_state->best_encoder);
  4464. if (conn_state->crtc != crtc)
  4465. continue;
  4466. encoder->enable(encoder, crtc_state, conn_state);
  4467. intel_opregion_notify_encoder(encoder, true);
  4468. }
  4469. }
  4470. static void intel_encoders_disable(struct drm_crtc *crtc,
  4471. struct intel_crtc_state *old_crtc_state,
  4472. struct drm_atomic_state *old_state)
  4473. {
  4474. struct drm_connector_state *old_conn_state;
  4475. struct drm_connector *conn;
  4476. int i;
  4477. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4478. struct intel_encoder *encoder =
  4479. to_intel_encoder(old_conn_state->best_encoder);
  4480. if (old_conn_state->crtc != crtc)
  4481. continue;
  4482. intel_opregion_notify_encoder(encoder, false);
  4483. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4484. }
  4485. }
  4486. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4487. struct intel_crtc_state *old_crtc_state,
  4488. struct drm_atomic_state *old_state)
  4489. {
  4490. struct drm_connector_state *old_conn_state;
  4491. struct drm_connector *conn;
  4492. int i;
  4493. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4494. struct intel_encoder *encoder =
  4495. to_intel_encoder(old_conn_state->best_encoder);
  4496. if (old_conn_state->crtc != crtc)
  4497. continue;
  4498. if (encoder->post_disable)
  4499. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4500. }
  4501. }
  4502. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4503. struct intel_crtc_state *old_crtc_state,
  4504. struct drm_atomic_state *old_state)
  4505. {
  4506. struct drm_connector_state *old_conn_state;
  4507. struct drm_connector *conn;
  4508. int i;
  4509. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4510. struct intel_encoder *encoder =
  4511. to_intel_encoder(old_conn_state->best_encoder);
  4512. if (old_conn_state->crtc != crtc)
  4513. continue;
  4514. if (encoder->post_pll_disable)
  4515. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4516. }
  4517. }
  4518. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4519. struct drm_atomic_state *old_state)
  4520. {
  4521. struct drm_crtc *crtc = pipe_config->base.crtc;
  4522. struct drm_device *dev = crtc->dev;
  4523. struct drm_i915_private *dev_priv = to_i915(dev);
  4524. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4525. int pipe = intel_crtc->pipe;
  4526. if (WARN_ON(intel_crtc->active))
  4527. return;
  4528. /*
  4529. * Sometimes spurious CPU pipe underruns happen during FDI
  4530. * training, at least with VGA+HDMI cloning. Suppress them.
  4531. *
  4532. * On ILK we get an occasional spurious CPU pipe underruns
  4533. * between eDP port A enable and vdd enable. Also PCH port
  4534. * enable seems to result in the occasional CPU pipe underrun.
  4535. *
  4536. * Spurious PCH underruns also occur during PCH enabling.
  4537. */
  4538. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4539. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4540. if (intel_crtc->config->has_pch_encoder)
  4541. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4542. if (intel_crtc->config->has_pch_encoder)
  4543. intel_prepare_shared_dpll(intel_crtc);
  4544. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4545. intel_dp_set_m_n(intel_crtc, M1_N1);
  4546. intel_set_pipe_timings(intel_crtc);
  4547. intel_set_pipe_src_size(intel_crtc);
  4548. if (intel_crtc->config->has_pch_encoder) {
  4549. intel_cpu_transcoder_set_m_n(intel_crtc,
  4550. &intel_crtc->config->fdi_m_n, NULL);
  4551. }
  4552. ironlake_set_pipeconf(crtc);
  4553. intel_crtc->active = true;
  4554. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4555. if (intel_crtc->config->has_pch_encoder) {
  4556. /* Note: FDI PLL enabling _must_ be done before we enable the
  4557. * cpu pipes, hence this is separate from all the other fdi/pch
  4558. * enabling. */
  4559. ironlake_fdi_pll_enable(intel_crtc);
  4560. } else {
  4561. assert_fdi_tx_disabled(dev_priv, pipe);
  4562. assert_fdi_rx_disabled(dev_priv, pipe);
  4563. }
  4564. ironlake_pfit_enable(intel_crtc);
  4565. /*
  4566. * On ILK+ LUT must be loaded before the pipe is running but with
  4567. * clocks enabled
  4568. */
  4569. intel_color_load_luts(&pipe_config->base);
  4570. if (dev_priv->display.initial_watermarks != NULL)
  4571. dev_priv->display.initial_watermarks(intel_crtc->config);
  4572. intel_enable_pipe(intel_crtc);
  4573. if (intel_crtc->config->has_pch_encoder)
  4574. ironlake_pch_enable(crtc);
  4575. assert_vblank_disabled(crtc);
  4576. drm_crtc_vblank_on(crtc);
  4577. intel_encoders_enable(crtc, pipe_config, old_state);
  4578. if (HAS_PCH_CPT(dev))
  4579. cpt_verify_modeset(dev, intel_crtc->pipe);
  4580. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4581. if (intel_crtc->config->has_pch_encoder)
  4582. intel_wait_for_vblank(dev, pipe);
  4583. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4584. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4585. }
  4586. /* IPS only exists on ULT machines and is tied to pipe A. */
  4587. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4588. {
  4589. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4590. }
  4591. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4592. struct drm_atomic_state *old_state)
  4593. {
  4594. struct drm_crtc *crtc = pipe_config->base.crtc;
  4595. struct drm_device *dev = crtc->dev;
  4596. struct drm_i915_private *dev_priv = to_i915(dev);
  4597. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4598. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4599. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4600. if (WARN_ON(intel_crtc->active))
  4601. return;
  4602. if (intel_crtc->config->has_pch_encoder)
  4603. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4604. false);
  4605. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4606. if (intel_crtc->config->shared_dpll)
  4607. intel_enable_shared_dpll(intel_crtc);
  4608. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4609. intel_dp_set_m_n(intel_crtc, M1_N1);
  4610. if (!transcoder_is_dsi(cpu_transcoder))
  4611. intel_set_pipe_timings(intel_crtc);
  4612. intel_set_pipe_src_size(intel_crtc);
  4613. if (cpu_transcoder != TRANSCODER_EDP &&
  4614. !transcoder_is_dsi(cpu_transcoder)) {
  4615. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4616. intel_crtc->config->pixel_multiplier - 1);
  4617. }
  4618. if (intel_crtc->config->has_pch_encoder) {
  4619. intel_cpu_transcoder_set_m_n(intel_crtc,
  4620. &intel_crtc->config->fdi_m_n, NULL);
  4621. }
  4622. if (!transcoder_is_dsi(cpu_transcoder))
  4623. haswell_set_pipeconf(crtc);
  4624. haswell_set_pipemisc(crtc);
  4625. intel_color_set_csc(&pipe_config->base);
  4626. intel_crtc->active = true;
  4627. if (intel_crtc->config->has_pch_encoder)
  4628. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4629. else
  4630. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4631. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4632. if (intel_crtc->config->has_pch_encoder)
  4633. dev_priv->display.fdi_link_train(crtc);
  4634. if (!transcoder_is_dsi(cpu_transcoder))
  4635. intel_ddi_enable_pipe_clock(intel_crtc);
  4636. if (INTEL_INFO(dev)->gen >= 9)
  4637. skylake_pfit_enable(intel_crtc);
  4638. else
  4639. ironlake_pfit_enable(intel_crtc);
  4640. /*
  4641. * On ILK+ LUT must be loaded before the pipe is running but with
  4642. * clocks enabled
  4643. */
  4644. intel_color_load_luts(&pipe_config->base);
  4645. intel_ddi_set_pipe_settings(crtc);
  4646. if (!transcoder_is_dsi(cpu_transcoder))
  4647. intel_ddi_enable_transcoder_func(crtc);
  4648. if (dev_priv->display.initial_watermarks != NULL)
  4649. dev_priv->display.initial_watermarks(pipe_config);
  4650. else
  4651. intel_update_watermarks(crtc);
  4652. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4653. if (!transcoder_is_dsi(cpu_transcoder))
  4654. intel_enable_pipe(intel_crtc);
  4655. if (intel_crtc->config->has_pch_encoder)
  4656. lpt_pch_enable(crtc);
  4657. if (intel_crtc->config->dp_encoder_is_mst)
  4658. intel_ddi_set_vc_payload_alloc(crtc, true);
  4659. assert_vblank_disabled(crtc);
  4660. drm_crtc_vblank_on(crtc);
  4661. intel_encoders_enable(crtc, pipe_config, old_state);
  4662. if (intel_crtc->config->has_pch_encoder) {
  4663. intel_wait_for_vblank(dev, pipe);
  4664. intel_wait_for_vblank(dev, pipe);
  4665. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4666. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4667. true);
  4668. }
  4669. /* If we change the relative order between pipe/planes enabling, we need
  4670. * to change the workaround. */
  4671. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4672. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4673. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4674. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4675. }
  4676. }
  4677. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4678. {
  4679. struct drm_device *dev = crtc->base.dev;
  4680. struct drm_i915_private *dev_priv = to_i915(dev);
  4681. int pipe = crtc->pipe;
  4682. /* To avoid upsetting the power well on haswell only disable the pfit if
  4683. * it's in use. The hw state code will make sure we get this right. */
  4684. if (force || crtc->config->pch_pfit.enabled) {
  4685. I915_WRITE(PF_CTL(pipe), 0);
  4686. I915_WRITE(PF_WIN_POS(pipe), 0);
  4687. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4688. }
  4689. }
  4690. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4691. struct drm_atomic_state *old_state)
  4692. {
  4693. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4694. struct drm_device *dev = crtc->dev;
  4695. struct drm_i915_private *dev_priv = to_i915(dev);
  4696. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4697. int pipe = intel_crtc->pipe;
  4698. /*
  4699. * Sometimes spurious CPU pipe underruns happen when the
  4700. * pipe is already disabled, but FDI RX/TX is still enabled.
  4701. * Happens at least with VGA+HDMI cloning. Suppress them.
  4702. */
  4703. if (intel_crtc->config->has_pch_encoder) {
  4704. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4705. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4706. }
  4707. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4708. drm_crtc_vblank_off(crtc);
  4709. assert_vblank_disabled(crtc);
  4710. intel_disable_pipe(intel_crtc);
  4711. ironlake_pfit_disable(intel_crtc, false);
  4712. if (intel_crtc->config->has_pch_encoder)
  4713. ironlake_fdi_disable(crtc);
  4714. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4715. if (intel_crtc->config->has_pch_encoder) {
  4716. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4717. if (HAS_PCH_CPT(dev)) {
  4718. i915_reg_t reg;
  4719. u32 temp;
  4720. /* disable TRANS_DP_CTL */
  4721. reg = TRANS_DP_CTL(pipe);
  4722. temp = I915_READ(reg);
  4723. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4724. TRANS_DP_PORT_SEL_MASK);
  4725. temp |= TRANS_DP_PORT_SEL_NONE;
  4726. I915_WRITE(reg, temp);
  4727. /* disable DPLL_SEL */
  4728. temp = I915_READ(PCH_DPLL_SEL);
  4729. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4730. I915_WRITE(PCH_DPLL_SEL, temp);
  4731. }
  4732. ironlake_fdi_pll_disable(intel_crtc);
  4733. }
  4734. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4735. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4736. }
  4737. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4738. struct drm_atomic_state *old_state)
  4739. {
  4740. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4741. struct drm_device *dev = crtc->dev;
  4742. struct drm_i915_private *dev_priv = to_i915(dev);
  4743. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4744. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4745. if (intel_crtc->config->has_pch_encoder)
  4746. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4747. false);
  4748. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4749. drm_crtc_vblank_off(crtc);
  4750. assert_vblank_disabled(crtc);
  4751. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4752. if (!transcoder_is_dsi(cpu_transcoder))
  4753. intel_disable_pipe(intel_crtc);
  4754. if (intel_crtc->config->dp_encoder_is_mst)
  4755. intel_ddi_set_vc_payload_alloc(crtc, false);
  4756. if (!transcoder_is_dsi(cpu_transcoder))
  4757. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4758. if (INTEL_INFO(dev)->gen >= 9)
  4759. skylake_scaler_disable(intel_crtc);
  4760. else
  4761. ironlake_pfit_disable(intel_crtc, false);
  4762. if (!transcoder_is_dsi(cpu_transcoder))
  4763. intel_ddi_disable_pipe_clock(intel_crtc);
  4764. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4765. if (old_crtc_state->has_pch_encoder)
  4766. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4767. true);
  4768. }
  4769. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4770. {
  4771. struct drm_device *dev = crtc->base.dev;
  4772. struct drm_i915_private *dev_priv = to_i915(dev);
  4773. struct intel_crtc_state *pipe_config = crtc->config;
  4774. if (!pipe_config->gmch_pfit.control)
  4775. return;
  4776. /*
  4777. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4778. * according to register description and PRM.
  4779. */
  4780. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4781. assert_pipe_disabled(dev_priv, crtc->pipe);
  4782. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4783. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4784. /* Border color in case we don't scale up to the full screen. Black by
  4785. * default, change to something else for debugging. */
  4786. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4787. }
  4788. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4789. {
  4790. switch (port) {
  4791. case PORT_A:
  4792. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4793. case PORT_B:
  4794. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4795. case PORT_C:
  4796. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4797. case PORT_D:
  4798. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4799. case PORT_E:
  4800. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4801. default:
  4802. MISSING_CASE(port);
  4803. return POWER_DOMAIN_PORT_OTHER;
  4804. }
  4805. }
  4806. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4807. {
  4808. switch (port) {
  4809. case PORT_A:
  4810. return POWER_DOMAIN_AUX_A;
  4811. case PORT_B:
  4812. return POWER_DOMAIN_AUX_B;
  4813. case PORT_C:
  4814. return POWER_DOMAIN_AUX_C;
  4815. case PORT_D:
  4816. return POWER_DOMAIN_AUX_D;
  4817. case PORT_E:
  4818. /* FIXME: Check VBT for actual wiring of PORT E */
  4819. return POWER_DOMAIN_AUX_D;
  4820. default:
  4821. MISSING_CASE(port);
  4822. return POWER_DOMAIN_AUX_A;
  4823. }
  4824. }
  4825. enum intel_display_power_domain
  4826. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4827. {
  4828. struct drm_device *dev = intel_encoder->base.dev;
  4829. struct intel_digital_port *intel_dig_port;
  4830. switch (intel_encoder->type) {
  4831. case INTEL_OUTPUT_UNKNOWN:
  4832. /* Only DDI platforms should ever use this output type */
  4833. WARN_ON_ONCE(!HAS_DDI(dev));
  4834. case INTEL_OUTPUT_DP:
  4835. case INTEL_OUTPUT_HDMI:
  4836. case INTEL_OUTPUT_EDP:
  4837. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4838. return port_to_power_domain(intel_dig_port->port);
  4839. case INTEL_OUTPUT_DP_MST:
  4840. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4841. return port_to_power_domain(intel_dig_port->port);
  4842. case INTEL_OUTPUT_ANALOG:
  4843. return POWER_DOMAIN_PORT_CRT;
  4844. case INTEL_OUTPUT_DSI:
  4845. return POWER_DOMAIN_PORT_DSI;
  4846. default:
  4847. return POWER_DOMAIN_PORT_OTHER;
  4848. }
  4849. }
  4850. enum intel_display_power_domain
  4851. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4852. {
  4853. struct drm_device *dev = intel_encoder->base.dev;
  4854. struct intel_digital_port *intel_dig_port;
  4855. switch (intel_encoder->type) {
  4856. case INTEL_OUTPUT_UNKNOWN:
  4857. case INTEL_OUTPUT_HDMI:
  4858. /*
  4859. * Only DDI platforms should ever use these output types.
  4860. * We can get here after the HDMI detect code has already set
  4861. * the type of the shared encoder. Since we can't be sure
  4862. * what's the status of the given connectors, play safe and
  4863. * run the DP detection too.
  4864. */
  4865. WARN_ON_ONCE(!HAS_DDI(dev));
  4866. case INTEL_OUTPUT_DP:
  4867. case INTEL_OUTPUT_EDP:
  4868. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4869. return port_to_aux_power_domain(intel_dig_port->port);
  4870. case INTEL_OUTPUT_DP_MST:
  4871. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4872. return port_to_aux_power_domain(intel_dig_port->port);
  4873. default:
  4874. MISSING_CASE(intel_encoder->type);
  4875. return POWER_DOMAIN_AUX_A;
  4876. }
  4877. }
  4878. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4879. struct intel_crtc_state *crtc_state)
  4880. {
  4881. struct drm_device *dev = crtc->dev;
  4882. struct drm_encoder *encoder;
  4883. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4884. enum pipe pipe = intel_crtc->pipe;
  4885. unsigned long mask;
  4886. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4887. if (!crtc_state->base.active)
  4888. return 0;
  4889. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4890. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4891. if (crtc_state->pch_pfit.enabled ||
  4892. crtc_state->pch_pfit.force_thru)
  4893. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4894. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4895. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4896. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4897. }
  4898. if (crtc_state->shared_dpll)
  4899. mask |= BIT(POWER_DOMAIN_PLLS);
  4900. return mask;
  4901. }
  4902. static unsigned long
  4903. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4904. struct intel_crtc_state *crtc_state)
  4905. {
  4906. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4907. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4908. enum intel_display_power_domain domain;
  4909. unsigned long domains, new_domains, old_domains;
  4910. old_domains = intel_crtc->enabled_power_domains;
  4911. intel_crtc->enabled_power_domains = new_domains =
  4912. get_crtc_power_domains(crtc, crtc_state);
  4913. domains = new_domains & ~old_domains;
  4914. for_each_power_domain(domain, domains)
  4915. intel_display_power_get(dev_priv, domain);
  4916. return old_domains & ~new_domains;
  4917. }
  4918. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4919. unsigned long domains)
  4920. {
  4921. enum intel_display_power_domain domain;
  4922. for_each_power_domain(domain, domains)
  4923. intel_display_power_put(dev_priv, domain);
  4924. }
  4925. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4926. {
  4927. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4928. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4929. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4930. return max_cdclk_freq;
  4931. else if (IS_CHERRYVIEW(dev_priv))
  4932. return max_cdclk_freq*95/100;
  4933. else if (INTEL_INFO(dev_priv)->gen < 4)
  4934. return 2*max_cdclk_freq*90/100;
  4935. else
  4936. return max_cdclk_freq*90/100;
  4937. }
  4938. static int skl_calc_cdclk(int max_pixclk, int vco);
  4939. static void intel_update_max_cdclk(struct drm_device *dev)
  4940. {
  4941. struct drm_i915_private *dev_priv = to_i915(dev);
  4942. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4943. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4944. int max_cdclk, vco;
  4945. vco = dev_priv->skl_preferred_vco_freq;
  4946. WARN_ON(vco != 8100000 && vco != 8640000);
  4947. /*
  4948. * Use the lower (vco 8640) cdclk values as a
  4949. * first guess. skl_calc_cdclk() will correct it
  4950. * if the preferred vco is 8100 instead.
  4951. */
  4952. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4953. max_cdclk = 617143;
  4954. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4955. max_cdclk = 540000;
  4956. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4957. max_cdclk = 432000;
  4958. else
  4959. max_cdclk = 308571;
  4960. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4961. } else if (IS_BROXTON(dev)) {
  4962. dev_priv->max_cdclk_freq = 624000;
  4963. } else if (IS_BROADWELL(dev)) {
  4964. /*
  4965. * FIXME with extra cooling we can allow
  4966. * 540 MHz for ULX and 675 Mhz for ULT.
  4967. * How can we know if extra cooling is
  4968. * available? PCI ID, VTB, something else?
  4969. */
  4970. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4971. dev_priv->max_cdclk_freq = 450000;
  4972. else if (IS_BDW_ULX(dev))
  4973. dev_priv->max_cdclk_freq = 450000;
  4974. else if (IS_BDW_ULT(dev))
  4975. dev_priv->max_cdclk_freq = 540000;
  4976. else
  4977. dev_priv->max_cdclk_freq = 675000;
  4978. } else if (IS_CHERRYVIEW(dev)) {
  4979. dev_priv->max_cdclk_freq = 320000;
  4980. } else if (IS_VALLEYVIEW(dev)) {
  4981. dev_priv->max_cdclk_freq = 400000;
  4982. } else {
  4983. /* otherwise assume cdclk is fixed */
  4984. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4985. }
  4986. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4987. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4988. dev_priv->max_cdclk_freq);
  4989. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4990. dev_priv->max_dotclk_freq);
  4991. }
  4992. static void intel_update_cdclk(struct drm_device *dev)
  4993. {
  4994. struct drm_i915_private *dev_priv = to_i915(dev);
  4995. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4996. if (INTEL_GEN(dev_priv) >= 9)
  4997. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  4998. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  4999. dev_priv->cdclk_pll.ref);
  5000. else
  5001. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  5002. dev_priv->cdclk_freq);
  5003. /*
  5004. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  5005. * Programmng [sic] note: bit[9:2] should be programmed to the number
  5006. * of cdclk that generates 4MHz reference clock freq which is used to
  5007. * generate GMBus clock. This will vary with the cdclk freq.
  5008. */
  5009. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  5010. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  5011. }
  5012. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  5013. static int skl_cdclk_decimal(int cdclk)
  5014. {
  5015. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  5016. }
  5017. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  5018. {
  5019. int ratio;
  5020. if (cdclk == dev_priv->cdclk_pll.ref)
  5021. return 0;
  5022. switch (cdclk) {
  5023. default:
  5024. MISSING_CASE(cdclk);
  5025. case 144000:
  5026. case 288000:
  5027. case 384000:
  5028. case 576000:
  5029. ratio = 60;
  5030. break;
  5031. case 624000:
  5032. ratio = 65;
  5033. break;
  5034. }
  5035. return dev_priv->cdclk_pll.ref * ratio;
  5036. }
  5037. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  5038. {
  5039. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  5040. /* Timeout 200us */
  5041. if (intel_wait_for_register(dev_priv,
  5042. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  5043. 1))
  5044. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  5045. dev_priv->cdclk_pll.vco = 0;
  5046. }
  5047. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  5048. {
  5049. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  5050. u32 val;
  5051. val = I915_READ(BXT_DE_PLL_CTL);
  5052. val &= ~BXT_DE_PLL_RATIO_MASK;
  5053. val |= BXT_DE_PLL_RATIO(ratio);
  5054. I915_WRITE(BXT_DE_PLL_CTL, val);
  5055. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  5056. /* Timeout 200us */
  5057. if (intel_wait_for_register(dev_priv,
  5058. BXT_DE_PLL_ENABLE,
  5059. BXT_DE_PLL_LOCK,
  5060. BXT_DE_PLL_LOCK,
  5061. 1))
  5062. DRM_ERROR("timeout waiting for DE PLL lock\n");
  5063. dev_priv->cdclk_pll.vco = vco;
  5064. }
  5065. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  5066. {
  5067. u32 val, divider;
  5068. int vco, ret;
  5069. vco = bxt_de_pll_vco(dev_priv, cdclk);
  5070. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5071. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  5072. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  5073. case 8:
  5074. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  5075. break;
  5076. case 4:
  5077. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  5078. break;
  5079. case 3:
  5080. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  5081. break;
  5082. case 2:
  5083. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5084. break;
  5085. default:
  5086. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  5087. WARN_ON(vco != 0);
  5088. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5089. break;
  5090. }
  5091. /* Inform power controller of upcoming frequency change */
  5092. mutex_lock(&dev_priv->rps.hw_lock);
  5093. ret = sandybridge_pcode_write_timeout(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5094. 0x80000000, 2000);
  5095. mutex_unlock(&dev_priv->rps.hw_lock);
  5096. if (ret) {
  5097. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  5098. ret, cdclk);
  5099. return;
  5100. }
  5101. if (dev_priv->cdclk_pll.vco != 0 &&
  5102. dev_priv->cdclk_pll.vco != vco)
  5103. bxt_de_pll_disable(dev_priv);
  5104. if (dev_priv->cdclk_pll.vco != vco)
  5105. bxt_de_pll_enable(dev_priv, vco);
  5106. val = divider | skl_cdclk_decimal(cdclk);
  5107. /*
  5108. * FIXME if only the cd2x divider needs changing, it could be done
  5109. * without shutting off the pipe (if only one pipe is active).
  5110. */
  5111. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  5112. /*
  5113. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5114. * enable otherwise.
  5115. */
  5116. if (cdclk >= 500000)
  5117. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5118. I915_WRITE(CDCLK_CTL, val);
  5119. mutex_lock(&dev_priv->rps.hw_lock);
  5120. ret = sandybridge_pcode_write_timeout(dev_priv,
  5121. HSW_PCODE_DE_WRITE_FREQ_REQ,
  5122. DIV_ROUND_UP(cdclk, 25000), 2000);
  5123. mutex_unlock(&dev_priv->rps.hw_lock);
  5124. if (ret) {
  5125. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  5126. ret, cdclk);
  5127. return;
  5128. }
  5129. intel_update_cdclk(&dev_priv->drm);
  5130. }
  5131. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5132. {
  5133. u32 cdctl, expected;
  5134. intel_update_cdclk(&dev_priv->drm);
  5135. if (dev_priv->cdclk_pll.vco == 0 ||
  5136. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5137. goto sanitize;
  5138. /* DPLL okay; verify the cdclock
  5139. *
  5140. * Some BIOS versions leave an incorrect decimal frequency value and
  5141. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  5142. * so sanitize this register.
  5143. */
  5144. cdctl = I915_READ(CDCLK_CTL);
  5145. /*
  5146. * Let's ignore the pipe field, since BIOS could have configured the
  5147. * dividers both synching to an active pipe, or asynchronously
  5148. * (PIPE_NONE).
  5149. */
  5150. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  5151. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  5152. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5153. /*
  5154. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5155. * enable otherwise.
  5156. */
  5157. if (dev_priv->cdclk_freq >= 500000)
  5158. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5159. if (cdctl == expected)
  5160. /* All well; nothing to sanitize */
  5161. return;
  5162. sanitize:
  5163. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5164. /* force cdclk programming */
  5165. dev_priv->cdclk_freq = 0;
  5166. /* force full PLL disable + enable */
  5167. dev_priv->cdclk_pll.vco = -1;
  5168. }
  5169. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  5170. {
  5171. bxt_sanitize_cdclk(dev_priv);
  5172. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  5173. return;
  5174. /*
  5175. * FIXME:
  5176. * - The initial CDCLK needs to be read from VBT.
  5177. * Need to make this change after VBT has changes for BXT.
  5178. */
  5179. bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
  5180. }
  5181. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  5182. {
  5183. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  5184. }
  5185. static int skl_calc_cdclk(int max_pixclk, int vco)
  5186. {
  5187. if (vco == 8640000) {
  5188. if (max_pixclk > 540000)
  5189. return 617143;
  5190. else if (max_pixclk > 432000)
  5191. return 540000;
  5192. else if (max_pixclk > 308571)
  5193. return 432000;
  5194. else
  5195. return 308571;
  5196. } else {
  5197. if (max_pixclk > 540000)
  5198. return 675000;
  5199. else if (max_pixclk > 450000)
  5200. return 540000;
  5201. else if (max_pixclk > 337500)
  5202. return 450000;
  5203. else
  5204. return 337500;
  5205. }
  5206. }
  5207. static void
  5208. skl_dpll0_update(struct drm_i915_private *dev_priv)
  5209. {
  5210. u32 val;
  5211. dev_priv->cdclk_pll.ref = 24000;
  5212. dev_priv->cdclk_pll.vco = 0;
  5213. val = I915_READ(LCPLL1_CTL);
  5214. if ((val & LCPLL_PLL_ENABLE) == 0)
  5215. return;
  5216. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  5217. return;
  5218. val = I915_READ(DPLL_CTRL1);
  5219. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  5220. DPLL_CTRL1_SSC(SKL_DPLL0) |
  5221. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  5222. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  5223. return;
  5224. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  5225. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  5226. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  5227. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  5228. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  5229. dev_priv->cdclk_pll.vco = 8100000;
  5230. break;
  5231. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  5232. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  5233. dev_priv->cdclk_pll.vco = 8640000;
  5234. break;
  5235. default:
  5236. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5237. break;
  5238. }
  5239. }
  5240. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  5241. {
  5242. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  5243. dev_priv->skl_preferred_vco_freq = vco;
  5244. if (changed)
  5245. intel_update_max_cdclk(&dev_priv->drm);
  5246. }
  5247. static void
  5248. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  5249. {
  5250. int min_cdclk = skl_calc_cdclk(0, vco);
  5251. u32 val;
  5252. WARN_ON(vco != 8100000 && vco != 8640000);
  5253. /* select the minimum CDCLK before enabling DPLL 0 */
  5254. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  5255. I915_WRITE(CDCLK_CTL, val);
  5256. POSTING_READ(CDCLK_CTL);
  5257. /*
  5258. * We always enable DPLL0 with the lowest link rate possible, but still
  5259. * taking into account the VCO required to operate the eDP panel at the
  5260. * desired frequency. The usual DP link rates operate with a VCO of
  5261. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  5262. * The modeset code is responsible for the selection of the exact link
  5263. * rate later on, with the constraint of choosing a frequency that
  5264. * works with vco.
  5265. */
  5266. val = I915_READ(DPLL_CTRL1);
  5267. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  5268. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5269. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  5270. if (vco == 8640000)
  5271. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  5272. SKL_DPLL0);
  5273. else
  5274. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  5275. SKL_DPLL0);
  5276. I915_WRITE(DPLL_CTRL1, val);
  5277. POSTING_READ(DPLL_CTRL1);
  5278. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  5279. if (intel_wait_for_register(dev_priv,
  5280. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  5281. 5))
  5282. DRM_ERROR("DPLL0 not locked\n");
  5283. dev_priv->cdclk_pll.vco = vco;
  5284. /* We'll want to keep using the current vco from now on. */
  5285. skl_set_preferred_cdclk_vco(dev_priv, vco);
  5286. }
  5287. static void
  5288. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  5289. {
  5290. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  5291. if (intel_wait_for_register(dev_priv,
  5292. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  5293. 1))
  5294. DRM_ERROR("Couldn't disable DPLL0\n");
  5295. dev_priv->cdclk_pll.vco = 0;
  5296. }
  5297. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  5298. {
  5299. struct drm_device *dev = &dev_priv->drm;
  5300. u32 freq_select, pcu_ack;
  5301. int ret;
  5302. WARN_ON((cdclk == 24000) != (vco == 0));
  5303. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5304. mutex_lock(&dev_priv->rps.hw_lock);
  5305. ret = skl_pcode_request(dev_priv, SKL_PCODE_CDCLK_CONTROL,
  5306. SKL_CDCLK_PREPARE_FOR_CHANGE,
  5307. SKL_CDCLK_READY_FOR_CHANGE,
  5308. SKL_CDCLK_READY_FOR_CHANGE, 3);
  5309. mutex_unlock(&dev_priv->rps.hw_lock);
  5310. if (ret) {
  5311. DRM_ERROR("Failed to inform PCU about cdclk change (%d)\n",
  5312. ret);
  5313. return;
  5314. }
  5315. /* set CDCLK_CTL */
  5316. switch (cdclk) {
  5317. case 450000:
  5318. case 432000:
  5319. freq_select = CDCLK_FREQ_450_432;
  5320. pcu_ack = 1;
  5321. break;
  5322. case 540000:
  5323. freq_select = CDCLK_FREQ_540;
  5324. pcu_ack = 2;
  5325. break;
  5326. case 308571:
  5327. case 337500:
  5328. default:
  5329. freq_select = CDCLK_FREQ_337_308;
  5330. pcu_ack = 0;
  5331. break;
  5332. case 617143:
  5333. case 675000:
  5334. freq_select = CDCLK_FREQ_675_617;
  5335. pcu_ack = 3;
  5336. break;
  5337. }
  5338. if (dev_priv->cdclk_pll.vco != 0 &&
  5339. dev_priv->cdclk_pll.vco != vco)
  5340. skl_dpll0_disable(dev_priv);
  5341. if (dev_priv->cdclk_pll.vco != vco)
  5342. skl_dpll0_enable(dev_priv, vco);
  5343. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  5344. POSTING_READ(CDCLK_CTL);
  5345. /* inform PCU of the change */
  5346. mutex_lock(&dev_priv->rps.hw_lock);
  5347. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  5348. mutex_unlock(&dev_priv->rps.hw_lock);
  5349. intel_update_cdclk(dev);
  5350. }
  5351. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  5352. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  5353. {
  5354. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  5355. }
  5356. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  5357. {
  5358. int cdclk, vco;
  5359. skl_sanitize_cdclk(dev_priv);
  5360. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  5361. /*
  5362. * Use the current vco as our initial
  5363. * guess as to what the preferred vco is.
  5364. */
  5365. if (dev_priv->skl_preferred_vco_freq == 0)
  5366. skl_set_preferred_cdclk_vco(dev_priv,
  5367. dev_priv->cdclk_pll.vco);
  5368. return;
  5369. }
  5370. vco = dev_priv->skl_preferred_vco_freq;
  5371. if (vco == 0)
  5372. vco = 8100000;
  5373. cdclk = skl_calc_cdclk(0, vco);
  5374. skl_set_cdclk(dev_priv, cdclk, vco);
  5375. }
  5376. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5377. {
  5378. uint32_t cdctl, expected;
  5379. /*
  5380. * check if the pre-os intialized the display
  5381. * There is SWF18 scratchpad register defined which is set by the
  5382. * pre-os which can be used by the OS drivers to check the status
  5383. */
  5384. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  5385. goto sanitize;
  5386. intel_update_cdclk(&dev_priv->drm);
  5387. /* Is PLL enabled and locked ? */
  5388. if (dev_priv->cdclk_pll.vco == 0 ||
  5389. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5390. goto sanitize;
  5391. /* DPLL okay; verify the cdclock
  5392. *
  5393. * Noticed in some instances that the freq selection is correct but
  5394. * decimal part is programmed wrong from BIOS where pre-os does not
  5395. * enable display. Verify the same as well.
  5396. */
  5397. cdctl = I915_READ(CDCLK_CTL);
  5398. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  5399. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5400. if (cdctl == expected)
  5401. /* All well; nothing to sanitize */
  5402. return;
  5403. sanitize:
  5404. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5405. /* force cdclk programming */
  5406. dev_priv->cdclk_freq = 0;
  5407. /* force full PLL disable + enable */
  5408. dev_priv->cdclk_pll.vco = -1;
  5409. }
  5410. /* Adjust CDclk dividers to allow high res or save power if possible */
  5411. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  5412. {
  5413. struct drm_i915_private *dev_priv = to_i915(dev);
  5414. u32 val, cmd;
  5415. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  5416. != dev_priv->cdclk_freq);
  5417. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  5418. cmd = 2;
  5419. else if (cdclk == 266667)
  5420. cmd = 1;
  5421. else
  5422. cmd = 0;
  5423. mutex_lock(&dev_priv->rps.hw_lock);
  5424. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5425. val &= ~DSPFREQGUAR_MASK;
  5426. val |= (cmd << DSPFREQGUAR_SHIFT);
  5427. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5428. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5429. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  5430. 50)) {
  5431. DRM_ERROR("timed out waiting for CDclk change\n");
  5432. }
  5433. mutex_unlock(&dev_priv->rps.hw_lock);
  5434. mutex_lock(&dev_priv->sb_lock);
  5435. if (cdclk == 400000) {
  5436. u32 divider;
  5437. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5438. /* adjust cdclk divider */
  5439. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5440. val &= ~CCK_FREQUENCY_VALUES;
  5441. val |= divider;
  5442. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  5443. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  5444. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  5445. 50))
  5446. DRM_ERROR("timed out waiting for CDclk change\n");
  5447. }
  5448. /* adjust self-refresh exit latency value */
  5449. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  5450. val &= ~0x7f;
  5451. /*
  5452. * For high bandwidth configs, we set a higher latency in the bunit
  5453. * so that the core display fetch happens in time to avoid underruns.
  5454. */
  5455. if (cdclk == 400000)
  5456. val |= 4500 / 250; /* 4.5 usec */
  5457. else
  5458. val |= 3000 / 250; /* 3.0 usec */
  5459. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  5460. mutex_unlock(&dev_priv->sb_lock);
  5461. intel_update_cdclk(dev);
  5462. }
  5463. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  5464. {
  5465. struct drm_i915_private *dev_priv = to_i915(dev);
  5466. u32 val, cmd;
  5467. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  5468. != dev_priv->cdclk_freq);
  5469. switch (cdclk) {
  5470. case 333333:
  5471. case 320000:
  5472. case 266667:
  5473. case 200000:
  5474. break;
  5475. default:
  5476. MISSING_CASE(cdclk);
  5477. return;
  5478. }
  5479. /*
  5480. * Specs are full of misinformation, but testing on actual
  5481. * hardware has shown that we just need to write the desired
  5482. * CCK divider into the Punit register.
  5483. */
  5484. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5485. mutex_lock(&dev_priv->rps.hw_lock);
  5486. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5487. val &= ~DSPFREQGUAR_MASK_CHV;
  5488. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5489. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5490. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5491. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5492. 50)) {
  5493. DRM_ERROR("timed out waiting for CDclk change\n");
  5494. }
  5495. mutex_unlock(&dev_priv->rps.hw_lock);
  5496. intel_update_cdclk(dev);
  5497. }
  5498. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5499. int max_pixclk)
  5500. {
  5501. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5502. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5503. /*
  5504. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5505. * 200MHz
  5506. * 267MHz
  5507. * 320/333MHz (depends on HPLL freq)
  5508. * 400MHz (VLV only)
  5509. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5510. * of the lower bin and adjust if needed.
  5511. *
  5512. * We seem to get an unstable or solid color picture at 200MHz.
  5513. * Not sure what's wrong. For now use 200MHz only when all pipes
  5514. * are off.
  5515. */
  5516. if (!IS_CHERRYVIEW(dev_priv) &&
  5517. max_pixclk > freq_320*limit/100)
  5518. return 400000;
  5519. else if (max_pixclk > 266667*limit/100)
  5520. return freq_320;
  5521. else if (max_pixclk > 0)
  5522. return 266667;
  5523. else
  5524. return 200000;
  5525. }
  5526. static int bxt_calc_cdclk(int max_pixclk)
  5527. {
  5528. if (max_pixclk > 576000)
  5529. return 624000;
  5530. else if (max_pixclk > 384000)
  5531. return 576000;
  5532. else if (max_pixclk > 288000)
  5533. return 384000;
  5534. else if (max_pixclk > 144000)
  5535. return 288000;
  5536. else
  5537. return 144000;
  5538. }
  5539. /* Compute the max pixel clock for new configuration. */
  5540. static int intel_mode_max_pixclk(struct drm_device *dev,
  5541. struct drm_atomic_state *state)
  5542. {
  5543. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5544. struct drm_i915_private *dev_priv = to_i915(dev);
  5545. struct drm_crtc *crtc;
  5546. struct drm_crtc_state *crtc_state;
  5547. unsigned max_pixclk = 0, i;
  5548. enum pipe pipe;
  5549. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5550. sizeof(intel_state->min_pixclk));
  5551. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5552. int pixclk = 0;
  5553. if (crtc_state->enable)
  5554. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5555. intel_state->min_pixclk[i] = pixclk;
  5556. }
  5557. for_each_pipe(dev_priv, pipe)
  5558. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5559. return max_pixclk;
  5560. }
  5561. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5562. {
  5563. struct drm_device *dev = state->dev;
  5564. struct drm_i915_private *dev_priv = to_i915(dev);
  5565. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5566. struct intel_atomic_state *intel_state =
  5567. to_intel_atomic_state(state);
  5568. intel_state->cdclk = intel_state->dev_cdclk =
  5569. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5570. if (!intel_state->active_crtcs)
  5571. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5572. return 0;
  5573. }
  5574. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  5575. {
  5576. int max_pixclk = ilk_max_pixel_rate(state);
  5577. struct intel_atomic_state *intel_state =
  5578. to_intel_atomic_state(state);
  5579. intel_state->cdclk = intel_state->dev_cdclk =
  5580. bxt_calc_cdclk(max_pixclk);
  5581. if (!intel_state->active_crtcs)
  5582. intel_state->dev_cdclk = bxt_calc_cdclk(0);
  5583. return 0;
  5584. }
  5585. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5586. {
  5587. unsigned int credits, default_credits;
  5588. if (IS_CHERRYVIEW(dev_priv))
  5589. default_credits = PFI_CREDIT(12);
  5590. else
  5591. default_credits = PFI_CREDIT(8);
  5592. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5593. /* CHV suggested value is 31 or 63 */
  5594. if (IS_CHERRYVIEW(dev_priv))
  5595. credits = PFI_CREDIT_63;
  5596. else
  5597. credits = PFI_CREDIT(15);
  5598. } else {
  5599. credits = default_credits;
  5600. }
  5601. /*
  5602. * WA - write default credits before re-programming
  5603. * FIXME: should we also set the resend bit here?
  5604. */
  5605. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5606. default_credits);
  5607. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5608. credits | PFI_CREDIT_RESEND);
  5609. /*
  5610. * FIXME is this guaranteed to clear
  5611. * immediately or should we poll for it?
  5612. */
  5613. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5614. }
  5615. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5616. {
  5617. struct drm_device *dev = old_state->dev;
  5618. struct drm_i915_private *dev_priv = to_i915(dev);
  5619. struct intel_atomic_state *old_intel_state =
  5620. to_intel_atomic_state(old_state);
  5621. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5622. /*
  5623. * FIXME: We can end up here with all power domains off, yet
  5624. * with a CDCLK frequency other than the minimum. To account
  5625. * for this take the PIPE-A power domain, which covers the HW
  5626. * blocks needed for the following programming. This can be
  5627. * removed once it's guaranteed that we get here either with
  5628. * the minimum CDCLK set, or the required power domains
  5629. * enabled.
  5630. */
  5631. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5632. if (IS_CHERRYVIEW(dev))
  5633. cherryview_set_cdclk(dev, req_cdclk);
  5634. else
  5635. valleyview_set_cdclk(dev, req_cdclk);
  5636. vlv_program_pfi_credits(dev_priv);
  5637. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5638. }
  5639. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  5640. struct drm_atomic_state *old_state)
  5641. {
  5642. struct drm_crtc *crtc = pipe_config->base.crtc;
  5643. struct drm_device *dev = crtc->dev;
  5644. struct drm_i915_private *dev_priv = to_i915(dev);
  5645. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5646. int pipe = intel_crtc->pipe;
  5647. if (WARN_ON(intel_crtc->active))
  5648. return;
  5649. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5650. intel_dp_set_m_n(intel_crtc, M1_N1);
  5651. intel_set_pipe_timings(intel_crtc);
  5652. intel_set_pipe_src_size(intel_crtc);
  5653. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5654. struct drm_i915_private *dev_priv = to_i915(dev);
  5655. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5656. I915_WRITE(CHV_CANVAS(pipe), 0);
  5657. }
  5658. i9xx_set_pipeconf(intel_crtc);
  5659. intel_crtc->active = true;
  5660. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5661. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  5662. if (IS_CHERRYVIEW(dev)) {
  5663. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5664. chv_enable_pll(intel_crtc, intel_crtc->config);
  5665. } else {
  5666. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5667. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5668. }
  5669. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5670. i9xx_pfit_enable(intel_crtc);
  5671. intel_color_load_luts(&pipe_config->base);
  5672. intel_update_watermarks(crtc);
  5673. intel_enable_pipe(intel_crtc);
  5674. assert_vblank_disabled(crtc);
  5675. drm_crtc_vblank_on(crtc);
  5676. intel_encoders_enable(crtc, pipe_config, old_state);
  5677. }
  5678. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5679. {
  5680. struct drm_device *dev = crtc->base.dev;
  5681. struct drm_i915_private *dev_priv = to_i915(dev);
  5682. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5683. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5684. }
  5685. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  5686. struct drm_atomic_state *old_state)
  5687. {
  5688. struct drm_crtc *crtc = pipe_config->base.crtc;
  5689. struct drm_device *dev = crtc->dev;
  5690. struct drm_i915_private *dev_priv = to_i915(dev);
  5691. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5692. enum pipe pipe = intel_crtc->pipe;
  5693. if (WARN_ON(intel_crtc->active))
  5694. return;
  5695. i9xx_set_pll_dividers(intel_crtc);
  5696. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5697. intel_dp_set_m_n(intel_crtc, M1_N1);
  5698. intel_set_pipe_timings(intel_crtc);
  5699. intel_set_pipe_src_size(intel_crtc);
  5700. i9xx_set_pipeconf(intel_crtc);
  5701. intel_crtc->active = true;
  5702. if (!IS_GEN2(dev))
  5703. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5704. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5705. i9xx_enable_pll(intel_crtc);
  5706. i9xx_pfit_enable(intel_crtc);
  5707. intel_color_load_luts(&pipe_config->base);
  5708. intel_update_watermarks(crtc);
  5709. intel_enable_pipe(intel_crtc);
  5710. assert_vblank_disabled(crtc);
  5711. drm_crtc_vblank_on(crtc);
  5712. intel_encoders_enable(crtc, pipe_config, old_state);
  5713. }
  5714. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5715. {
  5716. struct drm_device *dev = crtc->base.dev;
  5717. struct drm_i915_private *dev_priv = to_i915(dev);
  5718. if (!crtc->config->gmch_pfit.control)
  5719. return;
  5720. assert_pipe_disabled(dev_priv, crtc->pipe);
  5721. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5722. I915_READ(PFIT_CONTROL));
  5723. I915_WRITE(PFIT_CONTROL, 0);
  5724. }
  5725. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5726. struct drm_atomic_state *old_state)
  5727. {
  5728. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5729. struct drm_device *dev = crtc->dev;
  5730. struct drm_i915_private *dev_priv = to_i915(dev);
  5731. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5732. int pipe = intel_crtc->pipe;
  5733. /*
  5734. * On gen2 planes are double buffered but the pipe isn't, so we must
  5735. * wait for planes to fully turn off before disabling the pipe.
  5736. */
  5737. if (IS_GEN2(dev))
  5738. intel_wait_for_vblank(dev, pipe);
  5739. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5740. drm_crtc_vblank_off(crtc);
  5741. assert_vblank_disabled(crtc);
  5742. intel_disable_pipe(intel_crtc);
  5743. i9xx_pfit_disable(intel_crtc);
  5744. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5745. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5746. if (IS_CHERRYVIEW(dev))
  5747. chv_disable_pll(dev_priv, pipe);
  5748. else if (IS_VALLEYVIEW(dev))
  5749. vlv_disable_pll(dev_priv, pipe);
  5750. else
  5751. i9xx_disable_pll(intel_crtc);
  5752. }
  5753. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5754. if (!IS_GEN2(dev))
  5755. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5756. }
  5757. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5758. {
  5759. struct intel_encoder *encoder;
  5760. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5761. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5762. enum intel_display_power_domain domain;
  5763. unsigned long domains;
  5764. struct drm_atomic_state *state;
  5765. struct intel_crtc_state *crtc_state;
  5766. int ret;
  5767. if (!intel_crtc->active)
  5768. return;
  5769. if (to_intel_plane_state(crtc->primary->state)->base.visible) {
  5770. WARN_ON(intel_crtc->flip_work);
  5771. intel_pre_disable_primary_noatomic(crtc);
  5772. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5773. to_intel_plane_state(crtc->primary->state)->base.visible = false;
  5774. }
  5775. state = drm_atomic_state_alloc(crtc->dev);
  5776. if (!state) {
  5777. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  5778. crtc->base.id, crtc->name);
  5779. return;
  5780. }
  5781. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  5782. /* Everything's already locked, -EDEADLK can't happen. */
  5783. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5784. ret = drm_atomic_add_affected_connectors(state, crtc);
  5785. WARN_ON(IS_ERR(crtc_state) || ret);
  5786. dev_priv->display.crtc_disable(crtc_state, state);
  5787. drm_atomic_state_free(state);
  5788. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5789. crtc->base.id, crtc->name);
  5790. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5791. crtc->state->active = false;
  5792. intel_crtc->active = false;
  5793. crtc->enabled = false;
  5794. crtc->state->connector_mask = 0;
  5795. crtc->state->encoder_mask = 0;
  5796. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5797. encoder->base.crtc = NULL;
  5798. intel_fbc_disable(intel_crtc);
  5799. intel_update_watermarks(crtc);
  5800. intel_disable_shared_dpll(intel_crtc);
  5801. domains = intel_crtc->enabled_power_domains;
  5802. for_each_power_domain(domain, domains)
  5803. intel_display_power_put(dev_priv, domain);
  5804. intel_crtc->enabled_power_domains = 0;
  5805. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5806. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5807. }
  5808. /*
  5809. * turn all crtc's off, but do not adjust state
  5810. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5811. */
  5812. int intel_display_suspend(struct drm_device *dev)
  5813. {
  5814. struct drm_i915_private *dev_priv = to_i915(dev);
  5815. struct drm_atomic_state *state;
  5816. int ret;
  5817. state = drm_atomic_helper_suspend(dev);
  5818. ret = PTR_ERR_OR_ZERO(state);
  5819. if (ret)
  5820. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5821. else
  5822. dev_priv->modeset_restore_state = state;
  5823. return ret;
  5824. }
  5825. void intel_encoder_destroy(struct drm_encoder *encoder)
  5826. {
  5827. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5828. drm_encoder_cleanup(encoder);
  5829. kfree(intel_encoder);
  5830. }
  5831. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5832. * internal consistency). */
  5833. static void intel_connector_verify_state(struct intel_connector *connector)
  5834. {
  5835. struct drm_crtc *crtc = connector->base.state->crtc;
  5836. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5837. connector->base.base.id,
  5838. connector->base.name);
  5839. if (connector->get_hw_state(connector)) {
  5840. struct intel_encoder *encoder = connector->encoder;
  5841. struct drm_connector_state *conn_state = connector->base.state;
  5842. I915_STATE_WARN(!crtc,
  5843. "connector enabled without attached crtc\n");
  5844. if (!crtc)
  5845. return;
  5846. I915_STATE_WARN(!crtc->state->active,
  5847. "connector is active, but attached crtc isn't\n");
  5848. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5849. return;
  5850. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5851. "atomic encoder doesn't match attached encoder\n");
  5852. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5853. "attached encoder crtc differs from connector crtc\n");
  5854. } else {
  5855. I915_STATE_WARN(crtc && crtc->state->active,
  5856. "attached crtc is active, but connector isn't\n");
  5857. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5858. "best encoder set without crtc!\n");
  5859. }
  5860. }
  5861. int intel_connector_init(struct intel_connector *connector)
  5862. {
  5863. drm_atomic_helper_connector_reset(&connector->base);
  5864. if (!connector->base.state)
  5865. return -ENOMEM;
  5866. return 0;
  5867. }
  5868. struct intel_connector *intel_connector_alloc(void)
  5869. {
  5870. struct intel_connector *connector;
  5871. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5872. if (!connector)
  5873. return NULL;
  5874. if (intel_connector_init(connector) < 0) {
  5875. kfree(connector);
  5876. return NULL;
  5877. }
  5878. return connector;
  5879. }
  5880. /* Simple connector->get_hw_state implementation for encoders that support only
  5881. * one connector and no cloning and hence the encoder state determines the state
  5882. * of the connector. */
  5883. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5884. {
  5885. enum pipe pipe = 0;
  5886. struct intel_encoder *encoder = connector->encoder;
  5887. return encoder->get_hw_state(encoder, &pipe);
  5888. }
  5889. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5890. {
  5891. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5892. return crtc_state->fdi_lanes;
  5893. return 0;
  5894. }
  5895. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5896. struct intel_crtc_state *pipe_config)
  5897. {
  5898. struct drm_atomic_state *state = pipe_config->base.state;
  5899. struct intel_crtc *other_crtc;
  5900. struct intel_crtc_state *other_crtc_state;
  5901. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5902. pipe_name(pipe), pipe_config->fdi_lanes);
  5903. if (pipe_config->fdi_lanes > 4) {
  5904. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5905. pipe_name(pipe), pipe_config->fdi_lanes);
  5906. return -EINVAL;
  5907. }
  5908. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5909. if (pipe_config->fdi_lanes > 2) {
  5910. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5911. pipe_config->fdi_lanes);
  5912. return -EINVAL;
  5913. } else {
  5914. return 0;
  5915. }
  5916. }
  5917. if (INTEL_INFO(dev)->num_pipes == 2)
  5918. return 0;
  5919. /* Ivybridge 3 pipe is really complicated */
  5920. switch (pipe) {
  5921. case PIPE_A:
  5922. return 0;
  5923. case PIPE_B:
  5924. if (pipe_config->fdi_lanes <= 2)
  5925. return 0;
  5926. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5927. other_crtc_state =
  5928. intel_atomic_get_crtc_state(state, other_crtc);
  5929. if (IS_ERR(other_crtc_state))
  5930. return PTR_ERR(other_crtc_state);
  5931. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5932. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5933. pipe_name(pipe), pipe_config->fdi_lanes);
  5934. return -EINVAL;
  5935. }
  5936. return 0;
  5937. case PIPE_C:
  5938. if (pipe_config->fdi_lanes > 2) {
  5939. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5940. pipe_name(pipe), pipe_config->fdi_lanes);
  5941. return -EINVAL;
  5942. }
  5943. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5944. other_crtc_state =
  5945. intel_atomic_get_crtc_state(state, other_crtc);
  5946. if (IS_ERR(other_crtc_state))
  5947. return PTR_ERR(other_crtc_state);
  5948. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5949. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5950. return -EINVAL;
  5951. }
  5952. return 0;
  5953. default:
  5954. BUG();
  5955. }
  5956. }
  5957. #define RETRY 1
  5958. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5959. struct intel_crtc_state *pipe_config)
  5960. {
  5961. struct drm_device *dev = intel_crtc->base.dev;
  5962. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5963. int lane, link_bw, fdi_dotclock, ret;
  5964. bool needs_recompute = false;
  5965. retry:
  5966. /* FDI is a binary signal running at ~2.7GHz, encoding
  5967. * each output octet as 10 bits. The actual frequency
  5968. * is stored as a divider into a 100MHz clock, and the
  5969. * mode pixel clock is stored in units of 1KHz.
  5970. * Hence the bw of each lane in terms of the mode signal
  5971. * is:
  5972. */
  5973. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5974. fdi_dotclock = adjusted_mode->crtc_clock;
  5975. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5976. pipe_config->pipe_bpp);
  5977. pipe_config->fdi_lanes = lane;
  5978. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5979. link_bw, &pipe_config->fdi_m_n);
  5980. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5981. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5982. pipe_config->pipe_bpp -= 2*3;
  5983. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5984. pipe_config->pipe_bpp);
  5985. needs_recompute = true;
  5986. pipe_config->bw_constrained = true;
  5987. goto retry;
  5988. }
  5989. if (needs_recompute)
  5990. return RETRY;
  5991. return ret;
  5992. }
  5993. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5994. struct intel_crtc_state *pipe_config)
  5995. {
  5996. if (pipe_config->pipe_bpp > 24)
  5997. return false;
  5998. /* HSW can handle pixel rate up to cdclk? */
  5999. if (IS_HASWELL(dev_priv))
  6000. return true;
  6001. /*
  6002. * We compare against max which means we must take
  6003. * the increased cdclk requirement into account when
  6004. * calculating the new cdclk.
  6005. *
  6006. * Should measure whether using a lower cdclk w/o IPS
  6007. */
  6008. return ilk_pipe_pixel_rate(pipe_config) <=
  6009. dev_priv->max_cdclk_freq * 95 / 100;
  6010. }
  6011. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  6012. struct intel_crtc_state *pipe_config)
  6013. {
  6014. struct drm_device *dev = crtc->base.dev;
  6015. struct drm_i915_private *dev_priv = to_i915(dev);
  6016. pipe_config->ips_enabled = i915.enable_ips &&
  6017. hsw_crtc_supports_ips(crtc) &&
  6018. pipe_config_supports_ips(dev_priv, pipe_config);
  6019. }
  6020. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  6021. {
  6022. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6023. /* GDG double wide on either pipe, otherwise pipe A only */
  6024. return INTEL_INFO(dev_priv)->gen < 4 &&
  6025. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  6026. }
  6027. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  6028. struct intel_crtc_state *pipe_config)
  6029. {
  6030. struct drm_device *dev = crtc->base.dev;
  6031. struct drm_i915_private *dev_priv = to_i915(dev);
  6032. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  6033. int clock_limit = dev_priv->max_dotclk_freq;
  6034. if (INTEL_INFO(dev)->gen < 4) {
  6035. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  6036. /*
  6037. * Enable double wide mode when the dot clock
  6038. * is > 90% of the (display) core speed.
  6039. */
  6040. if (intel_crtc_supports_double_wide(crtc) &&
  6041. adjusted_mode->crtc_clock > clock_limit) {
  6042. clock_limit = dev_priv->max_dotclk_freq;
  6043. pipe_config->double_wide = true;
  6044. }
  6045. }
  6046. if (adjusted_mode->crtc_clock > clock_limit) {
  6047. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  6048. adjusted_mode->crtc_clock, clock_limit,
  6049. yesno(pipe_config->double_wide));
  6050. return -EINVAL;
  6051. }
  6052. /*
  6053. * Pipe horizontal size must be even in:
  6054. * - DVO ganged mode
  6055. * - LVDS dual channel mode
  6056. * - Double wide pipe
  6057. */
  6058. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  6059. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  6060. pipe_config->pipe_src_w &= ~1;
  6061. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  6062. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  6063. */
  6064. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  6065. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  6066. return -EINVAL;
  6067. if (HAS_IPS(dev))
  6068. hsw_compute_ips_config(crtc, pipe_config);
  6069. if (pipe_config->has_pch_encoder)
  6070. return ironlake_fdi_compute_config(crtc, pipe_config);
  6071. return 0;
  6072. }
  6073. static int skylake_get_display_clock_speed(struct drm_device *dev)
  6074. {
  6075. struct drm_i915_private *dev_priv = to_i915(dev);
  6076. uint32_t cdctl;
  6077. skl_dpll0_update(dev_priv);
  6078. if (dev_priv->cdclk_pll.vco == 0)
  6079. return dev_priv->cdclk_pll.ref;
  6080. cdctl = I915_READ(CDCLK_CTL);
  6081. if (dev_priv->cdclk_pll.vco == 8640000) {
  6082. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6083. case CDCLK_FREQ_450_432:
  6084. return 432000;
  6085. case CDCLK_FREQ_337_308:
  6086. return 308571;
  6087. case CDCLK_FREQ_540:
  6088. return 540000;
  6089. case CDCLK_FREQ_675_617:
  6090. return 617143;
  6091. default:
  6092. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6093. }
  6094. } else {
  6095. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6096. case CDCLK_FREQ_450_432:
  6097. return 450000;
  6098. case CDCLK_FREQ_337_308:
  6099. return 337500;
  6100. case CDCLK_FREQ_540:
  6101. return 540000;
  6102. case CDCLK_FREQ_675_617:
  6103. return 675000;
  6104. default:
  6105. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6106. }
  6107. }
  6108. return dev_priv->cdclk_pll.ref;
  6109. }
  6110. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  6111. {
  6112. u32 val;
  6113. dev_priv->cdclk_pll.ref = 19200;
  6114. dev_priv->cdclk_pll.vco = 0;
  6115. val = I915_READ(BXT_DE_PLL_ENABLE);
  6116. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  6117. return;
  6118. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  6119. return;
  6120. val = I915_READ(BXT_DE_PLL_CTL);
  6121. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  6122. dev_priv->cdclk_pll.ref;
  6123. }
  6124. static int broxton_get_display_clock_speed(struct drm_device *dev)
  6125. {
  6126. struct drm_i915_private *dev_priv = to_i915(dev);
  6127. u32 divider;
  6128. int div, vco;
  6129. bxt_de_pll_update(dev_priv);
  6130. vco = dev_priv->cdclk_pll.vco;
  6131. if (vco == 0)
  6132. return dev_priv->cdclk_pll.ref;
  6133. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  6134. switch (divider) {
  6135. case BXT_CDCLK_CD2X_DIV_SEL_1:
  6136. div = 2;
  6137. break;
  6138. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  6139. div = 3;
  6140. break;
  6141. case BXT_CDCLK_CD2X_DIV_SEL_2:
  6142. div = 4;
  6143. break;
  6144. case BXT_CDCLK_CD2X_DIV_SEL_4:
  6145. div = 8;
  6146. break;
  6147. default:
  6148. MISSING_CASE(divider);
  6149. return dev_priv->cdclk_pll.ref;
  6150. }
  6151. return DIV_ROUND_CLOSEST(vco, div);
  6152. }
  6153. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  6154. {
  6155. struct drm_i915_private *dev_priv = to_i915(dev);
  6156. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6157. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6158. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6159. return 800000;
  6160. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6161. return 450000;
  6162. else if (freq == LCPLL_CLK_FREQ_450)
  6163. return 450000;
  6164. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  6165. return 540000;
  6166. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  6167. return 337500;
  6168. else
  6169. return 675000;
  6170. }
  6171. static int haswell_get_display_clock_speed(struct drm_device *dev)
  6172. {
  6173. struct drm_i915_private *dev_priv = to_i915(dev);
  6174. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6175. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6176. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6177. return 800000;
  6178. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6179. return 450000;
  6180. else if (freq == LCPLL_CLK_FREQ_450)
  6181. return 450000;
  6182. else if (IS_HSW_ULT(dev))
  6183. return 337500;
  6184. else
  6185. return 540000;
  6186. }
  6187. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  6188. {
  6189. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  6190. CCK_DISPLAY_CLOCK_CONTROL);
  6191. }
  6192. static int ilk_get_display_clock_speed(struct drm_device *dev)
  6193. {
  6194. return 450000;
  6195. }
  6196. static int i945_get_display_clock_speed(struct drm_device *dev)
  6197. {
  6198. return 400000;
  6199. }
  6200. static int i915_get_display_clock_speed(struct drm_device *dev)
  6201. {
  6202. return 333333;
  6203. }
  6204. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  6205. {
  6206. return 200000;
  6207. }
  6208. static int pnv_get_display_clock_speed(struct drm_device *dev)
  6209. {
  6210. struct pci_dev *pdev = dev->pdev;
  6211. u16 gcfgc = 0;
  6212. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6213. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6214. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  6215. return 266667;
  6216. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  6217. return 333333;
  6218. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  6219. return 444444;
  6220. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  6221. return 200000;
  6222. default:
  6223. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  6224. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  6225. return 133333;
  6226. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  6227. return 166667;
  6228. }
  6229. }
  6230. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  6231. {
  6232. struct pci_dev *pdev = dev->pdev;
  6233. u16 gcfgc = 0;
  6234. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6235. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  6236. return 133333;
  6237. else {
  6238. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6239. case GC_DISPLAY_CLOCK_333_MHZ:
  6240. return 333333;
  6241. default:
  6242. case GC_DISPLAY_CLOCK_190_200_MHZ:
  6243. return 190000;
  6244. }
  6245. }
  6246. }
  6247. static int i865_get_display_clock_speed(struct drm_device *dev)
  6248. {
  6249. return 266667;
  6250. }
  6251. static int i85x_get_display_clock_speed(struct drm_device *dev)
  6252. {
  6253. struct pci_dev *pdev = dev->pdev;
  6254. u16 hpllcc = 0;
  6255. /*
  6256. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  6257. * encoding is different :(
  6258. * FIXME is this the right way to detect 852GM/852GMV?
  6259. */
  6260. if (pdev->revision == 0x1)
  6261. return 133333;
  6262. pci_bus_read_config_word(pdev->bus,
  6263. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  6264. /* Assume that the hardware is in the high speed state. This
  6265. * should be the default.
  6266. */
  6267. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  6268. case GC_CLOCK_133_200:
  6269. case GC_CLOCK_133_200_2:
  6270. case GC_CLOCK_100_200:
  6271. return 200000;
  6272. case GC_CLOCK_166_250:
  6273. return 250000;
  6274. case GC_CLOCK_100_133:
  6275. return 133333;
  6276. case GC_CLOCK_133_266:
  6277. case GC_CLOCK_133_266_2:
  6278. case GC_CLOCK_166_266:
  6279. return 266667;
  6280. }
  6281. /* Shouldn't happen */
  6282. return 0;
  6283. }
  6284. static int i830_get_display_clock_speed(struct drm_device *dev)
  6285. {
  6286. return 133333;
  6287. }
  6288. static unsigned int intel_hpll_vco(struct drm_device *dev)
  6289. {
  6290. struct drm_i915_private *dev_priv = to_i915(dev);
  6291. static const unsigned int blb_vco[8] = {
  6292. [0] = 3200000,
  6293. [1] = 4000000,
  6294. [2] = 5333333,
  6295. [3] = 4800000,
  6296. [4] = 6400000,
  6297. };
  6298. static const unsigned int pnv_vco[8] = {
  6299. [0] = 3200000,
  6300. [1] = 4000000,
  6301. [2] = 5333333,
  6302. [3] = 4800000,
  6303. [4] = 2666667,
  6304. };
  6305. static const unsigned int cl_vco[8] = {
  6306. [0] = 3200000,
  6307. [1] = 4000000,
  6308. [2] = 5333333,
  6309. [3] = 6400000,
  6310. [4] = 3333333,
  6311. [5] = 3566667,
  6312. [6] = 4266667,
  6313. };
  6314. static const unsigned int elk_vco[8] = {
  6315. [0] = 3200000,
  6316. [1] = 4000000,
  6317. [2] = 5333333,
  6318. [3] = 4800000,
  6319. };
  6320. static const unsigned int ctg_vco[8] = {
  6321. [0] = 3200000,
  6322. [1] = 4000000,
  6323. [2] = 5333333,
  6324. [3] = 6400000,
  6325. [4] = 2666667,
  6326. [5] = 4266667,
  6327. };
  6328. const unsigned int *vco_table;
  6329. unsigned int vco;
  6330. uint8_t tmp = 0;
  6331. /* FIXME other chipsets? */
  6332. if (IS_GM45(dev))
  6333. vco_table = ctg_vco;
  6334. else if (IS_G4X(dev))
  6335. vco_table = elk_vco;
  6336. else if (IS_CRESTLINE(dev))
  6337. vco_table = cl_vco;
  6338. else if (IS_PINEVIEW(dev))
  6339. vco_table = pnv_vco;
  6340. else if (IS_G33(dev))
  6341. vco_table = blb_vco;
  6342. else
  6343. return 0;
  6344. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  6345. vco = vco_table[tmp & 0x7];
  6346. if (vco == 0)
  6347. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  6348. else
  6349. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  6350. return vco;
  6351. }
  6352. static int gm45_get_display_clock_speed(struct drm_device *dev)
  6353. {
  6354. struct pci_dev *pdev = dev->pdev;
  6355. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  6356. uint16_t tmp = 0;
  6357. pci_read_config_word(pdev, GCFGC, &tmp);
  6358. cdclk_sel = (tmp >> 12) & 0x1;
  6359. switch (vco) {
  6360. case 2666667:
  6361. case 4000000:
  6362. case 5333333:
  6363. return cdclk_sel ? 333333 : 222222;
  6364. case 3200000:
  6365. return cdclk_sel ? 320000 : 228571;
  6366. default:
  6367. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  6368. return 222222;
  6369. }
  6370. }
  6371. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  6372. {
  6373. struct pci_dev *pdev = dev->pdev;
  6374. static const uint8_t div_3200[] = { 16, 10, 8 };
  6375. static const uint8_t div_4000[] = { 20, 12, 10 };
  6376. static const uint8_t div_5333[] = { 24, 16, 14 };
  6377. const uint8_t *div_table;
  6378. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  6379. uint16_t tmp = 0;
  6380. pci_read_config_word(pdev, GCFGC, &tmp);
  6381. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  6382. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6383. goto fail;
  6384. switch (vco) {
  6385. case 3200000:
  6386. div_table = div_3200;
  6387. break;
  6388. case 4000000:
  6389. div_table = div_4000;
  6390. break;
  6391. case 5333333:
  6392. div_table = div_5333;
  6393. break;
  6394. default:
  6395. goto fail;
  6396. }
  6397. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6398. fail:
  6399. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  6400. return 200000;
  6401. }
  6402. static int g33_get_display_clock_speed(struct drm_device *dev)
  6403. {
  6404. struct pci_dev *pdev = dev->pdev;
  6405. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  6406. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  6407. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  6408. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  6409. const uint8_t *div_table;
  6410. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  6411. uint16_t tmp = 0;
  6412. pci_read_config_word(pdev, GCFGC, &tmp);
  6413. cdclk_sel = (tmp >> 4) & 0x7;
  6414. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6415. goto fail;
  6416. switch (vco) {
  6417. case 3200000:
  6418. div_table = div_3200;
  6419. break;
  6420. case 4000000:
  6421. div_table = div_4000;
  6422. break;
  6423. case 4800000:
  6424. div_table = div_4800;
  6425. break;
  6426. case 5333333:
  6427. div_table = div_5333;
  6428. break;
  6429. default:
  6430. goto fail;
  6431. }
  6432. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6433. fail:
  6434. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  6435. return 190476;
  6436. }
  6437. static void
  6438. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  6439. {
  6440. while (*num > DATA_LINK_M_N_MASK ||
  6441. *den > DATA_LINK_M_N_MASK) {
  6442. *num >>= 1;
  6443. *den >>= 1;
  6444. }
  6445. }
  6446. static void compute_m_n(unsigned int m, unsigned int n,
  6447. uint32_t *ret_m, uint32_t *ret_n)
  6448. {
  6449. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  6450. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  6451. intel_reduce_m_n_ratio(ret_m, ret_n);
  6452. }
  6453. void
  6454. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  6455. int pixel_clock, int link_clock,
  6456. struct intel_link_m_n *m_n)
  6457. {
  6458. m_n->tu = 64;
  6459. compute_m_n(bits_per_pixel * pixel_clock,
  6460. link_clock * nlanes * 8,
  6461. &m_n->gmch_m, &m_n->gmch_n);
  6462. compute_m_n(pixel_clock, link_clock,
  6463. &m_n->link_m, &m_n->link_n);
  6464. }
  6465. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  6466. {
  6467. if (i915.panel_use_ssc >= 0)
  6468. return i915.panel_use_ssc != 0;
  6469. return dev_priv->vbt.lvds_use_ssc
  6470. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  6471. }
  6472. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6473. {
  6474. return (1 << dpll->n) << 16 | dpll->m2;
  6475. }
  6476. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6477. {
  6478. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6479. }
  6480. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6481. struct intel_crtc_state *crtc_state,
  6482. struct dpll *reduced_clock)
  6483. {
  6484. struct drm_device *dev = crtc->base.dev;
  6485. u32 fp, fp2 = 0;
  6486. if (IS_PINEVIEW(dev)) {
  6487. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6488. if (reduced_clock)
  6489. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6490. } else {
  6491. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6492. if (reduced_clock)
  6493. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6494. }
  6495. crtc_state->dpll_hw_state.fp0 = fp;
  6496. crtc->lowfreq_avail = false;
  6497. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6498. reduced_clock) {
  6499. crtc_state->dpll_hw_state.fp1 = fp2;
  6500. crtc->lowfreq_avail = true;
  6501. } else {
  6502. crtc_state->dpll_hw_state.fp1 = fp;
  6503. }
  6504. }
  6505. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6506. pipe)
  6507. {
  6508. u32 reg_val;
  6509. /*
  6510. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6511. * and set it to a reasonable value instead.
  6512. */
  6513. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6514. reg_val &= 0xffffff00;
  6515. reg_val |= 0x00000030;
  6516. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6517. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6518. reg_val &= 0x8cffffff;
  6519. reg_val = 0x8c000000;
  6520. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6521. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6522. reg_val &= 0xffffff00;
  6523. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6524. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6525. reg_val &= 0x00ffffff;
  6526. reg_val |= 0xb0000000;
  6527. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6528. }
  6529. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6530. struct intel_link_m_n *m_n)
  6531. {
  6532. struct drm_device *dev = crtc->base.dev;
  6533. struct drm_i915_private *dev_priv = to_i915(dev);
  6534. int pipe = crtc->pipe;
  6535. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6536. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6537. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6538. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6539. }
  6540. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6541. struct intel_link_m_n *m_n,
  6542. struct intel_link_m_n *m2_n2)
  6543. {
  6544. struct drm_device *dev = crtc->base.dev;
  6545. struct drm_i915_private *dev_priv = to_i915(dev);
  6546. int pipe = crtc->pipe;
  6547. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6548. if (INTEL_INFO(dev)->gen >= 5) {
  6549. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6550. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6551. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6552. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6553. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6554. * for gen < 8) and if DRRS is supported (to make sure the
  6555. * registers are not unnecessarily accessed).
  6556. */
  6557. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6558. crtc->config->has_drrs) {
  6559. I915_WRITE(PIPE_DATA_M2(transcoder),
  6560. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6561. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6562. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6563. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6564. }
  6565. } else {
  6566. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6567. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6568. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6569. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6570. }
  6571. }
  6572. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6573. {
  6574. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6575. if (m_n == M1_N1) {
  6576. dp_m_n = &crtc->config->dp_m_n;
  6577. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6578. } else if (m_n == M2_N2) {
  6579. /*
  6580. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6581. * needs to be programmed into M1_N1.
  6582. */
  6583. dp_m_n = &crtc->config->dp_m2_n2;
  6584. } else {
  6585. DRM_ERROR("Unsupported divider value\n");
  6586. return;
  6587. }
  6588. if (crtc->config->has_pch_encoder)
  6589. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6590. else
  6591. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6592. }
  6593. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6594. struct intel_crtc_state *pipe_config)
  6595. {
  6596. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6597. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6598. if (crtc->pipe != PIPE_A)
  6599. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6600. /* DPLL not used with DSI, but still need the rest set up */
  6601. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6602. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6603. DPLL_EXT_BUFFER_ENABLE_VLV;
  6604. pipe_config->dpll_hw_state.dpll_md =
  6605. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6606. }
  6607. static void chv_compute_dpll(struct intel_crtc *crtc,
  6608. struct intel_crtc_state *pipe_config)
  6609. {
  6610. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6611. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6612. if (crtc->pipe != PIPE_A)
  6613. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6614. /* DPLL not used with DSI, but still need the rest set up */
  6615. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6616. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6617. pipe_config->dpll_hw_state.dpll_md =
  6618. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6619. }
  6620. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6621. const struct intel_crtc_state *pipe_config)
  6622. {
  6623. struct drm_device *dev = crtc->base.dev;
  6624. struct drm_i915_private *dev_priv = to_i915(dev);
  6625. enum pipe pipe = crtc->pipe;
  6626. u32 mdiv;
  6627. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6628. u32 coreclk, reg_val;
  6629. /* Enable Refclk */
  6630. I915_WRITE(DPLL(pipe),
  6631. pipe_config->dpll_hw_state.dpll &
  6632. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6633. /* No need to actually set up the DPLL with DSI */
  6634. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6635. return;
  6636. mutex_lock(&dev_priv->sb_lock);
  6637. bestn = pipe_config->dpll.n;
  6638. bestm1 = pipe_config->dpll.m1;
  6639. bestm2 = pipe_config->dpll.m2;
  6640. bestp1 = pipe_config->dpll.p1;
  6641. bestp2 = pipe_config->dpll.p2;
  6642. /* See eDP HDMI DPIO driver vbios notes doc */
  6643. /* PLL B needs special handling */
  6644. if (pipe == PIPE_B)
  6645. vlv_pllb_recal_opamp(dev_priv, pipe);
  6646. /* Set up Tx target for periodic Rcomp update */
  6647. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6648. /* Disable target IRef on PLL */
  6649. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6650. reg_val &= 0x00ffffff;
  6651. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6652. /* Disable fast lock */
  6653. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6654. /* Set idtafcrecal before PLL is enabled */
  6655. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6656. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6657. mdiv |= ((bestn << DPIO_N_SHIFT));
  6658. mdiv |= (1 << DPIO_K_SHIFT);
  6659. /*
  6660. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6661. * but we don't support that).
  6662. * Note: don't use the DAC post divider as it seems unstable.
  6663. */
  6664. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6665. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6666. mdiv |= DPIO_ENABLE_CALIBRATION;
  6667. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6668. /* Set HBR and RBR LPF coefficients */
  6669. if (pipe_config->port_clock == 162000 ||
  6670. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  6671. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  6672. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6673. 0x009f0003);
  6674. else
  6675. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6676. 0x00d0000f);
  6677. if (intel_crtc_has_dp_encoder(pipe_config)) {
  6678. /* Use SSC source */
  6679. if (pipe == PIPE_A)
  6680. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6681. 0x0df40000);
  6682. else
  6683. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6684. 0x0df70000);
  6685. } else { /* HDMI or VGA */
  6686. /* Use bend source */
  6687. if (pipe == PIPE_A)
  6688. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6689. 0x0df70000);
  6690. else
  6691. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6692. 0x0df40000);
  6693. }
  6694. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6695. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6696. if (intel_crtc_has_dp_encoder(crtc->config))
  6697. coreclk |= 0x01000000;
  6698. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6699. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6700. mutex_unlock(&dev_priv->sb_lock);
  6701. }
  6702. static void chv_prepare_pll(struct intel_crtc *crtc,
  6703. const struct intel_crtc_state *pipe_config)
  6704. {
  6705. struct drm_device *dev = crtc->base.dev;
  6706. struct drm_i915_private *dev_priv = to_i915(dev);
  6707. enum pipe pipe = crtc->pipe;
  6708. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6709. u32 loopfilter, tribuf_calcntr;
  6710. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6711. u32 dpio_val;
  6712. int vco;
  6713. /* Enable Refclk and SSC */
  6714. I915_WRITE(DPLL(pipe),
  6715. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6716. /* No need to actually set up the DPLL with DSI */
  6717. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6718. return;
  6719. bestn = pipe_config->dpll.n;
  6720. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6721. bestm1 = pipe_config->dpll.m1;
  6722. bestm2 = pipe_config->dpll.m2 >> 22;
  6723. bestp1 = pipe_config->dpll.p1;
  6724. bestp2 = pipe_config->dpll.p2;
  6725. vco = pipe_config->dpll.vco;
  6726. dpio_val = 0;
  6727. loopfilter = 0;
  6728. mutex_lock(&dev_priv->sb_lock);
  6729. /* p1 and p2 divider */
  6730. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6731. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6732. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6733. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6734. 1 << DPIO_CHV_K_DIV_SHIFT);
  6735. /* Feedback post-divider - m2 */
  6736. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6737. /* Feedback refclk divider - n and m1 */
  6738. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6739. DPIO_CHV_M1_DIV_BY_2 |
  6740. 1 << DPIO_CHV_N_DIV_SHIFT);
  6741. /* M2 fraction division */
  6742. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6743. /* M2 fraction division enable */
  6744. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6745. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6746. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6747. if (bestm2_frac)
  6748. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6749. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6750. /* Program digital lock detect threshold */
  6751. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6752. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6753. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6754. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6755. if (!bestm2_frac)
  6756. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6757. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6758. /* Loop filter */
  6759. if (vco == 5400000) {
  6760. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6761. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6762. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6763. tribuf_calcntr = 0x9;
  6764. } else if (vco <= 6200000) {
  6765. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6766. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6767. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6768. tribuf_calcntr = 0x9;
  6769. } else if (vco <= 6480000) {
  6770. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6771. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6772. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6773. tribuf_calcntr = 0x8;
  6774. } else {
  6775. /* Not supported. Apply the same limits as in the max case */
  6776. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6777. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6778. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6779. tribuf_calcntr = 0;
  6780. }
  6781. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6782. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6783. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6784. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6785. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6786. /* AFC Recal */
  6787. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6788. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6789. DPIO_AFC_RECAL);
  6790. mutex_unlock(&dev_priv->sb_lock);
  6791. }
  6792. /**
  6793. * vlv_force_pll_on - forcibly enable just the PLL
  6794. * @dev_priv: i915 private structure
  6795. * @pipe: pipe PLL to enable
  6796. * @dpll: PLL configuration
  6797. *
  6798. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6799. * in cases where we need the PLL enabled even when @pipe is not going to
  6800. * be enabled.
  6801. */
  6802. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6803. const struct dpll *dpll)
  6804. {
  6805. struct intel_crtc *crtc =
  6806. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6807. struct intel_crtc_state *pipe_config;
  6808. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6809. if (!pipe_config)
  6810. return -ENOMEM;
  6811. pipe_config->base.crtc = &crtc->base;
  6812. pipe_config->pixel_multiplier = 1;
  6813. pipe_config->dpll = *dpll;
  6814. if (IS_CHERRYVIEW(dev)) {
  6815. chv_compute_dpll(crtc, pipe_config);
  6816. chv_prepare_pll(crtc, pipe_config);
  6817. chv_enable_pll(crtc, pipe_config);
  6818. } else {
  6819. vlv_compute_dpll(crtc, pipe_config);
  6820. vlv_prepare_pll(crtc, pipe_config);
  6821. vlv_enable_pll(crtc, pipe_config);
  6822. }
  6823. kfree(pipe_config);
  6824. return 0;
  6825. }
  6826. /**
  6827. * vlv_force_pll_off - forcibly disable just the PLL
  6828. * @dev_priv: i915 private structure
  6829. * @pipe: pipe PLL to disable
  6830. *
  6831. * Disable the PLL for @pipe. To be used in cases where we need
  6832. * the PLL enabled even when @pipe is not going to be enabled.
  6833. */
  6834. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6835. {
  6836. if (IS_CHERRYVIEW(dev))
  6837. chv_disable_pll(to_i915(dev), pipe);
  6838. else
  6839. vlv_disable_pll(to_i915(dev), pipe);
  6840. }
  6841. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6842. struct intel_crtc_state *crtc_state,
  6843. struct dpll *reduced_clock)
  6844. {
  6845. struct drm_device *dev = crtc->base.dev;
  6846. struct drm_i915_private *dev_priv = to_i915(dev);
  6847. u32 dpll;
  6848. struct dpll *clock = &crtc_state->dpll;
  6849. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6850. dpll = DPLL_VGA_MODE_DIS;
  6851. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6852. dpll |= DPLLB_MODE_LVDS;
  6853. else
  6854. dpll |= DPLLB_MODE_DAC_SERIAL;
  6855. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6856. dpll |= (crtc_state->pixel_multiplier - 1)
  6857. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6858. }
  6859. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6860. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6861. dpll |= DPLL_SDVO_HIGH_SPEED;
  6862. if (intel_crtc_has_dp_encoder(crtc_state))
  6863. dpll |= DPLL_SDVO_HIGH_SPEED;
  6864. /* compute bitmask from p1 value */
  6865. if (IS_PINEVIEW(dev))
  6866. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6867. else {
  6868. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6869. if (IS_G4X(dev) && reduced_clock)
  6870. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6871. }
  6872. switch (clock->p2) {
  6873. case 5:
  6874. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6875. break;
  6876. case 7:
  6877. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6878. break;
  6879. case 10:
  6880. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6881. break;
  6882. case 14:
  6883. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6884. break;
  6885. }
  6886. if (INTEL_INFO(dev)->gen >= 4)
  6887. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6888. if (crtc_state->sdvo_tv_clock)
  6889. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6890. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6891. intel_panel_use_ssc(dev_priv))
  6892. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6893. else
  6894. dpll |= PLL_REF_INPUT_DREFCLK;
  6895. dpll |= DPLL_VCO_ENABLE;
  6896. crtc_state->dpll_hw_state.dpll = dpll;
  6897. if (INTEL_INFO(dev)->gen >= 4) {
  6898. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6899. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6900. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6901. }
  6902. }
  6903. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6904. struct intel_crtc_state *crtc_state,
  6905. struct dpll *reduced_clock)
  6906. {
  6907. struct drm_device *dev = crtc->base.dev;
  6908. struct drm_i915_private *dev_priv = to_i915(dev);
  6909. u32 dpll;
  6910. struct dpll *clock = &crtc_state->dpll;
  6911. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6912. dpll = DPLL_VGA_MODE_DIS;
  6913. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6914. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6915. } else {
  6916. if (clock->p1 == 2)
  6917. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6918. else
  6919. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6920. if (clock->p2 == 4)
  6921. dpll |= PLL_P2_DIVIDE_BY_4;
  6922. }
  6923. if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6924. dpll |= DPLL_DVO_2X_MODE;
  6925. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6926. intel_panel_use_ssc(dev_priv))
  6927. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6928. else
  6929. dpll |= PLL_REF_INPUT_DREFCLK;
  6930. dpll |= DPLL_VCO_ENABLE;
  6931. crtc_state->dpll_hw_state.dpll = dpll;
  6932. }
  6933. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6934. {
  6935. struct drm_device *dev = intel_crtc->base.dev;
  6936. struct drm_i915_private *dev_priv = to_i915(dev);
  6937. enum pipe pipe = intel_crtc->pipe;
  6938. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6939. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6940. uint32_t crtc_vtotal, crtc_vblank_end;
  6941. int vsyncshift = 0;
  6942. /* We need to be careful not to changed the adjusted mode, for otherwise
  6943. * the hw state checker will get angry at the mismatch. */
  6944. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6945. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6946. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6947. /* the chip adds 2 halflines automatically */
  6948. crtc_vtotal -= 1;
  6949. crtc_vblank_end -= 1;
  6950. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6951. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6952. else
  6953. vsyncshift = adjusted_mode->crtc_hsync_start -
  6954. adjusted_mode->crtc_htotal / 2;
  6955. if (vsyncshift < 0)
  6956. vsyncshift += adjusted_mode->crtc_htotal;
  6957. }
  6958. if (INTEL_INFO(dev)->gen > 3)
  6959. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6960. I915_WRITE(HTOTAL(cpu_transcoder),
  6961. (adjusted_mode->crtc_hdisplay - 1) |
  6962. ((adjusted_mode->crtc_htotal - 1) << 16));
  6963. I915_WRITE(HBLANK(cpu_transcoder),
  6964. (adjusted_mode->crtc_hblank_start - 1) |
  6965. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6966. I915_WRITE(HSYNC(cpu_transcoder),
  6967. (adjusted_mode->crtc_hsync_start - 1) |
  6968. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6969. I915_WRITE(VTOTAL(cpu_transcoder),
  6970. (adjusted_mode->crtc_vdisplay - 1) |
  6971. ((crtc_vtotal - 1) << 16));
  6972. I915_WRITE(VBLANK(cpu_transcoder),
  6973. (adjusted_mode->crtc_vblank_start - 1) |
  6974. ((crtc_vblank_end - 1) << 16));
  6975. I915_WRITE(VSYNC(cpu_transcoder),
  6976. (adjusted_mode->crtc_vsync_start - 1) |
  6977. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6978. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6979. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6980. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6981. * bits. */
  6982. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6983. (pipe == PIPE_B || pipe == PIPE_C))
  6984. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6985. }
  6986. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6987. {
  6988. struct drm_device *dev = intel_crtc->base.dev;
  6989. struct drm_i915_private *dev_priv = to_i915(dev);
  6990. enum pipe pipe = intel_crtc->pipe;
  6991. /* pipesrc controls the size that is scaled from, which should
  6992. * always be the user's requested size.
  6993. */
  6994. I915_WRITE(PIPESRC(pipe),
  6995. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6996. (intel_crtc->config->pipe_src_h - 1));
  6997. }
  6998. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6999. struct intel_crtc_state *pipe_config)
  7000. {
  7001. struct drm_device *dev = crtc->base.dev;
  7002. struct drm_i915_private *dev_priv = to_i915(dev);
  7003. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  7004. uint32_t tmp;
  7005. tmp = I915_READ(HTOTAL(cpu_transcoder));
  7006. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  7007. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  7008. tmp = I915_READ(HBLANK(cpu_transcoder));
  7009. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  7010. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  7011. tmp = I915_READ(HSYNC(cpu_transcoder));
  7012. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  7013. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  7014. tmp = I915_READ(VTOTAL(cpu_transcoder));
  7015. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  7016. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  7017. tmp = I915_READ(VBLANK(cpu_transcoder));
  7018. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  7019. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  7020. tmp = I915_READ(VSYNC(cpu_transcoder));
  7021. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  7022. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  7023. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  7024. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  7025. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  7026. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  7027. }
  7028. }
  7029. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  7030. struct intel_crtc_state *pipe_config)
  7031. {
  7032. struct drm_device *dev = crtc->base.dev;
  7033. struct drm_i915_private *dev_priv = to_i915(dev);
  7034. u32 tmp;
  7035. tmp = I915_READ(PIPESRC(crtc->pipe));
  7036. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  7037. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  7038. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  7039. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  7040. }
  7041. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  7042. struct intel_crtc_state *pipe_config)
  7043. {
  7044. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  7045. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  7046. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  7047. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  7048. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  7049. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  7050. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  7051. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  7052. mode->flags = pipe_config->base.adjusted_mode.flags;
  7053. mode->type = DRM_MODE_TYPE_DRIVER;
  7054. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  7055. mode->flags |= pipe_config->base.adjusted_mode.flags;
  7056. mode->hsync = drm_mode_hsync(mode);
  7057. mode->vrefresh = drm_mode_vrefresh(mode);
  7058. drm_mode_set_name(mode);
  7059. }
  7060. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  7061. {
  7062. struct drm_device *dev = intel_crtc->base.dev;
  7063. struct drm_i915_private *dev_priv = to_i915(dev);
  7064. uint32_t pipeconf;
  7065. pipeconf = 0;
  7066. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  7067. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  7068. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  7069. if (intel_crtc->config->double_wide)
  7070. pipeconf |= PIPECONF_DOUBLE_WIDE;
  7071. /* only g4x and later have fancy bpc/dither controls */
  7072. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  7073. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  7074. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  7075. pipeconf |= PIPECONF_DITHER_EN |
  7076. PIPECONF_DITHER_TYPE_SP;
  7077. switch (intel_crtc->config->pipe_bpp) {
  7078. case 18:
  7079. pipeconf |= PIPECONF_6BPC;
  7080. break;
  7081. case 24:
  7082. pipeconf |= PIPECONF_8BPC;
  7083. break;
  7084. case 30:
  7085. pipeconf |= PIPECONF_10BPC;
  7086. break;
  7087. default:
  7088. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7089. BUG();
  7090. }
  7091. }
  7092. if (HAS_PIPE_CXSR(dev)) {
  7093. if (intel_crtc->lowfreq_avail) {
  7094. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  7095. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  7096. } else {
  7097. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  7098. }
  7099. }
  7100. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  7101. if (INTEL_INFO(dev)->gen < 4 ||
  7102. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  7103. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  7104. else
  7105. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  7106. } else
  7107. pipeconf |= PIPECONF_PROGRESSIVE;
  7108. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  7109. intel_crtc->config->limited_color_range)
  7110. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  7111. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  7112. POSTING_READ(PIPECONF(intel_crtc->pipe));
  7113. }
  7114. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  7115. struct intel_crtc_state *crtc_state)
  7116. {
  7117. struct drm_device *dev = crtc->base.dev;
  7118. struct drm_i915_private *dev_priv = to_i915(dev);
  7119. const struct intel_limit *limit;
  7120. int refclk = 48000;
  7121. memset(&crtc_state->dpll_hw_state, 0,
  7122. sizeof(crtc_state->dpll_hw_state));
  7123. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7124. if (intel_panel_use_ssc(dev_priv)) {
  7125. refclk = dev_priv->vbt.lvds_ssc_freq;
  7126. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7127. }
  7128. limit = &intel_limits_i8xx_lvds;
  7129. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  7130. limit = &intel_limits_i8xx_dvo;
  7131. } else {
  7132. limit = &intel_limits_i8xx_dac;
  7133. }
  7134. if (!crtc_state->clock_set &&
  7135. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7136. refclk, NULL, &crtc_state->dpll)) {
  7137. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7138. return -EINVAL;
  7139. }
  7140. i8xx_compute_dpll(crtc, crtc_state, NULL);
  7141. return 0;
  7142. }
  7143. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  7144. struct intel_crtc_state *crtc_state)
  7145. {
  7146. struct drm_device *dev = crtc->base.dev;
  7147. struct drm_i915_private *dev_priv = to_i915(dev);
  7148. const struct intel_limit *limit;
  7149. int refclk = 96000;
  7150. memset(&crtc_state->dpll_hw_state, 0,
  7151. sizeof(crtc_state->dpll_hw_state));
  7152. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7153. if (intel_panel_use_ssc(dev_priv)) {
  7154. refclk = dev_priv->vbt.lvds_ssc_freq;
  7155. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7156. }
  7157. if (intel_is_dual_link_lvds(dev))
  7158. limit = &intel_limits_g4x_dual_channel_lvds;
  7159. else
  7160. limit = &intel_limits_g4x_single_channel_lvds;
  7161. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  7162. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  7163. limit = &intel_limits_g4x_hdmi;
  7164. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  7165. limit = &intel_limits_g4x_sdvo;
  7166. } else {
  7167. /* The option is for other outputs */
  7168. limit = &intel_limits_i9xx_sdvo;
  7169. }
  7170. if (!crtc_state->clock_set &&
  7171. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7172. refclk, NULL, &crtc_state->dpll)) {
  7173. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7174. return -EINVAL;
  7175. }
  7176. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7177. return 0;
  7178. }
  7179. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  7180. struct intel_crtc_state *crtc_state)
  7181. {
  7182. struct drm_device *dev = crtc->base.dev;
  7183. struct drm_i915_private *dev_priv = to_i915(dev);
  7184. const struct intel_limit *limit;
  7185. int refclk = 96000;
  7186. memset(&crtc_state->dpll_hw_state, 0,
  7187. sizeof(crtc_state->dpll_hw_state));
  7188. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7189. if (intel_panel_use_ssc(dev_priv)) {
  7190. refclk = dev_priv->vbt.lvds_ssc_freq;
  7191. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7192. }
  7193. limit = &intel_limits_pineview_lvds;
  7194. } else {
  7195. limit = &intel_limits_pineview_sdvo;
  7196. }
  7197. if (!crtc_state->clock_set &&
  7198. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7199. refclk, NULL, &crtc_state->dpll)) {
  7200. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7201. return -EINVAL;
  7202. }
  7203. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7204. return 0;
  7205. }
  7206. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  7207. struct intel_crtc_state *crtc_state)
  7208. {
  7209. struct drm_device *dev = crtc->base.dev;
  7210. struct drm_i915_private *dev_priv = to_i915(dev);
  7211. const struct intel_limit *limit;
  7212. int refclk = 96000;
  7213. memset(&crtc_state->dpll_hw_state, 0,
  7214. sizeof(crtc_state->dpll_hw_state));
  7215. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7216. if (intel_panel_use_ssc(dev_priv)) {
  7217. refclk = dev_priv->vbt.lvds_ssc_freq;
  7218. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7219. }
  7220. limit = &intel_limits_i9xx_lvds;
  7221. } else {
  7222. limit = &intel_limits_i9xx_sdvo;
  7223. }
  7224. if (!crtc_state->clock_set &&
  7225. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7226. refclk, NULL, &crtc_state->dpll)) {
  7227. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7228. return -EINVAL;
  7229. }
  7230. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7231. return 0;
  7232. }
  7233. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  7234. struct intel_crtc_state *crtc_state)
  7235. {
  7236. int refclk = 100000;
  7237. const struct intel_limit *limit = &intel_limits_chv;
  7238. memset(&crtc_state->dpll_hw_state, 0,
  7239. sizeof(crtc_state->dpll_hw_state));
  7240. if (!crtc_state->clock_set &&
  7241. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7242. refclk, NULL, &crtc_state->dpll)) {
  7243. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7244. return -EINVAL;
  7245. }
  7246. chv_compute_dpll(crtc, crtc_state);
  7247. return 0;
  7248. }
  7249. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  7250. struct intel_crtc_state *crtc_state)
  7251. {
  7252. int refclk = 100000;
  7253. const struct intel_limit *limit = &intel_limits_vlv;
  7254. memset(&crtc_state->dpll_hw_state, 0,
  7255. sizeof(crtc_state->dpll_hw_state));
  7256. if (!crtc_state->clock_set &&
  7257. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7258. refclk, NULL, &crtc_state->dpll)) {
  7259. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7260. return -EINVAL;
  7261. }
  7262. vlv_compute_dpll(crtc, crtc_state);
  7263. return 0;
  7264. }
  7265. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  7266. struct intel_crtc_state *pipe_config)
  7267. {
  7268. struct drm_device *dev = crtc->base.dev;
  7269. struct drm_i915_private *dev_priv = to_i915(dev);
  7270. uint32_t tmp;
  7271. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  7272. return;
  7273. tmp = I915_READ(PFIT_CONTROL);
  7274. if (!(tmp & PFIT_ENABLE))
  7275. return;
  7276. /* Check whether the pfit is attached to our pipe. */
  7277. if (INTEL_INFO(dev)->gen < 4) {
  7278. if (crtc->pipe != PIPE_B)
  7279. return;
  7280. } else {
  7281. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  7282. return;
  7283. }
  7284. pipe_config->gmch_pfit.control = tmp;
  7285. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  7286. }
  7287. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  7288. struct intel_crtc_state *pipe_config)
  7289. {
  7290. struct drm_device *dev = crtc->base.dev;
  7291. struct drm_i915_private *dev_priv = to_i915(dev);
  7292. int pipe = pipe_config->cpu_transcoder;
  7293. struct dpll clock;
  7294. u32 mdiv;
  7295. int refclk = 100000;
  7296. /* In case of DSI, DPLL will not be used */
  7297. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7298. return;
  7299. mutex_lock(&dev_priv->sb_lock);
  7300. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  7301. mutex_unlock(&dev_priv->sb_lock);
  7302. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  7303. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  7304. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  7305. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  7306. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  7307. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  7308. }
  7309. static void
  7310. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  7311. struct intel_initial_plane_config *plane_config)
  7312. {
  7313. struct drm_device *dev = crtc->base.dev;
  7314. struct drm_i915_private *dev_priv = to_i915(dev);
  7315. u32 val, base, offset;
  7316. int pipe = crtc->pipe, plane = crtc->plane;
  7317. int fourcc, pixel_format;
  7318. unsigned int aligned_height;
  7319. struct drm_framebuffer *fb;
  7320. struct intel_framebuffer *intel_fb;
  7321. val = I915_READ(DSPCNTR(plane));
  7322. if (!(val & DISPLAY_PLANE_ENABLE))
  7323. return;
  7324. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7325. if (!intel_fb) {
  7326. DRM_DEBUG_KMS("failed to alloc fb\n");
  7327. return;
  7328. }
  7329. fb = &intel_fb->base;
  7330. if (INTEL_INFO(dev)->gen >= 4) {
  7331. if (val & DISPPLANE_TILED) {
  7332. plane_config->tiling = I915_TILING_X;
  7333. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7334. }
  7335. }
  7336. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7337. fourcc = i9xx_format_to_fourcc(pixel_format);
  7338. fb->pixel_format = fourcc;
  7339. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7340. if (INTEL_INFO(dev)->gen >= 4) {
  7341. if (plane_config->tiling)
  7342. offset = I915_READ(DSPTILEOFF(plane));
  7343. else
  7344. offset = I915_READ(DSPLINOFF(plane));
  7345. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  7346. } else {
  7347. base = I915_READ(DSPADDR(plane));
  7348. }
  7349. plane_config->base = base;
  7350. val = I915_READ(PIPESRC(pipe));
  7351. fb->width = ((val >> 16) & 0xfff) + 1;
  7352. fb->height = ((val >> 0) & 0xfff) + 1;
  7353. val = I915_READ(DSPSTRIDE(pipe));
  7354. fb->pitches[0] = val & 0xffffffc0;
  7355. aligned_height = intel_fb_align_height(dev, fb->height,
  7356. fb->pixel_format,
  7357. fb->modifier[0]);
  7358. plane_config->size = fb->pitches[0] * aligned_height;
  7359. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7360. pipe_name(pipe), plane, fb->width, fb->height,
  7361. fb->bits_per_pixel, base, fb->pitches[0],
  7362. plane_config->size);
  7363. plane_config->fb = intel_fb;
  7364. }
  7365. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  7366. struct intel_crtc_state *pipe_config)
  7367. {
  7368. struct drm_device *dev = crtc->base.dev;
  7369. struct drm_i915_private *dev_priv = to_i915(dev);
  7370. int pipe = pipe_config->cpu_transcoder;
  7371. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  7372. struct dpll clock;
  7373. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  7374. int refclk = 100000;
  7375. /* In case of DSI, DPLL will not be used */
  7376. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7377. return;
  7378. mutex_lock(&dev_priv->sb_lock);
  7379. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  7380. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  7381. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  7382. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  7383. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  7384. mutex_unlock(&dev_priv->sb_lock);
  7385. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  7386. clock.m2 = (pll_dw0 & 0xff) << 22;
  7387. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  7388. clock.m2 |= pll_dw2 & 0x3fffff;
  7389. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  7390. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  7391. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  7392. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  7393. }
  7394. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  7395. struct intel_crtc_state *pipe_config)
  7396. {
  7397. struct drm_device *dev = crtc->base.dev;
  7398. struct drm_i915_private *dev_priv = to_i915(dev);
  7399. enum intel_display_power_domain power_domain;
  7400. uint32_t tmp;
  7401. bool ret;
  7402. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7403. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7404. return false;
  7405. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7406. pipe_config->shared_dpll = NULL;
  7407. ret = false;
  7408. tmp = I915_READ(PIPECONF(crtc->pipe));
  7409. if (!(tmp & PIPECONF_ENABLE))
  7410. goto out;
  7411. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  7412. switch (tmp & PIPECONF_BPC_MASK) {
  7413. case PIPECONF_6BPC:
  7414. pipe_config->pipe_bpp = 18;
  7415. break;
  7416. case PIPECONF_8BPC:
  7417. pipe_config->pipe_bpp = 24;
  7418. break;
  7419. case PIPECONF_10BPC:
  7420. pipe_config->pipe_bpp = 30;
  7421. break;
  7422. default:
  7423. break;
  7424. }
  7425. }
  7426. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  7427. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  7428. pipe_config->limited_color_range = true;
  7429. if (INTEL_INFO(dev)->gen < 4)
  7430. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  7431. intel_get_pipe_timings(crtc, pipe_config);
  7432. intel_get_pipe_src_size(crtc, pipe_config);
  7433. i9xx_get_pfit_config(crtc, pipe_config);
  7434. if (INTEL_INFO(dev)->gen >= 4) {
  7435. /* No way to read it out on pipes B and C */
  7436. if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
  7437. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  7438. else
  7439. tmp = I915_READ(DPLL_MD(crtc->pipe));
  7440. pipe_config->pixel_multiplier =
  7441. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  7442. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  7443. pipe_config->dpll_hw_state.dpll_md = tmp;
  7444. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  7445. tmp = I915_READ(DPLL(crtc->pipe));
  7446. pipe_config->pixel_multiplier =
  7447. ((tmp & SDVO_MULTIPLIER_MASK)
  7448. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  7449. } else {
  7450. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  7451. * port and will be fixed up in the encoder->get_config
  7452. * function. */
  7453. pipe_config->pixel_multiplier = 1;
  7454. }
  7455. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  7456. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  7457. /*
  7458. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  7459. * on 830. Filter it out here so that we don't
  7460. * report errors due to that.
  7461. */
  7462. if (IS_I830(dev))
  7463. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  7464. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  7465. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  7466. } else {
  7467. /* Mask out read-only status bits. */
  7468. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  7469. DPLL_PORTC_READY_MASK |
  7470. DPLL_PORTB_READY_MASK);
  7471. }
  7472. if (IS_CHERRYVIEW(dev))
  7473. chv_crtc_clock_get(crtc, pipe_config);
  7474. else if (IS_VALLEYVIEW(dev))
  7475. vlv_crtc_clock_get(crtc, pipe_config);
  7476. else
  7477. i9xx_crtc_clock_get(crtc, pipe_config);
  7478. /*
  7479. * Normally the dotclock is filled in by the encoder .get_config()
  7480. * but in case the pipe is enabled w/o any ports we need a sane
  7481. * default.
  7482. */
  7483. pipe_config->base.adjusted_mode.crtc_clock =
  7484. pipe_config->port_clock / pipe_config->pixel_multiplier;
  7485. ret = true;
  7486. out:
  7487. intel_display_power_put(dev_priv, power_domain);
  7488. return ret;
  7489. }
  7490. static void ironlake_init_pch_refclk(struct drm_device *dev)
  7491. {
  7492. struct drm_i915_private *dev_priv = to_i915(dev);
  7493. struct intel_encoder *encoder;
  7494. int i;
  7495. u32 val, final;
  7496. bool has_lvds = false;
  7497. bool has_cpu_edp = false;
  7498. bool has_panel = false;
  7499. bool has_ck505 = false;
  7500. bool can_ssc = false;
  7501. bool using_ssc_source = false;
  7502. /* We need to take the global config into account */
  7503. for_each_intel_encoder(dev, encoder) {
  7504. switch (encoder->type) {
  7505. case INTEL_OUTPUT_LVDS:
  7506. has_panel = true;
  7507. has_lvds = true;
  7508. break;
  7509. case INTEL_OUTPUT_EDP:
  7510. has_panel = true;
  7511. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7512. has_cpu_edp = true;
  7513. break;
  7514. default:
  7515. break;
  7516. }
  7517. }
  7518. if (HAS_PCH_IBX(dev)) {
  7519. has_ck505 = dev_priv->vbt.display_clock_mode;
  7520. can_ssc = has_ck505;
  7521. } else {
  7522. has_ck505 = false;
  7523. can_ssc = true;
  7524. }
  7525. /* Check if any DPLLs are using the SSC source */
  7526. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7527. u32 temp = I915_READ(PCH_DPLL(i));
  7528. if (!(temp & DPLL_VCO_ENABLE))
  7529. continue;
  7530. if ((temp & PLL_REF_INPUT_MASK) ==
  7531. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7532. using_ssc_source = true;
  7533. break;
  7534. }
  7535. }
  7536. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7537. has_panel, has_lvds, has_ck505, using_ssc_source);
  7538. /* Ironlake: try to setup display ref clock before DPLL
  7539. * enabling. This is only under driver's control after
  7540. * PCH B stepping, previous chipset stepping should be
  7541. * ignoring this setting.
  7542. */
  7543. val = I915_READ(PCH_DREF_CONTROL);
  7544. /* As we must carefully and slowly disable/enable each source in turn,
  7545. * compute the final state we want first and check if we need to
  7546. * make any changes at all.
  7547. */
  7548. final = val;
  7549. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7550. if (has_ck505)
  7551. final |= DREF_NONSPREAD_CK505_ENABLE;
  7552. else
  7553. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7554. final &= ~DREF_SSC_SOURCE_MASK;
  7555. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7556. final &= ~DREF_SSC1_ENABLE;
  7557. if (has_panel) {
  7558. final |= DREF_SSC_SOURCE_ENABLE;
  7559. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7560. final |= DREF_SSC1_ENABLE;
  7561. if (has_cpu_edp) {
  7562. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7563. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7564. else
  7565. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7566. } else
  7567. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7568. } else if (using_ssc_source) {
  7569. final |= DREF_SSC_SOURCE_ENABLE;
  7570. final |= DREF_SSC1_ENABLE;
  7571. }
  7572. if (final == val)
  7573. return;
  7574. /* Always enable nonspread source */
  7575. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7576. if (has_ck505)
  7577. val |= DREF_NONSPREAD_CK505_ENABLE;
  7578. else
  7579. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7580. if (has_panel) {
  7581. val &= ~DREF_SSC_SOURCE_MASK;
  7582. val |= DREF_SSC_SOURCE_ENABLE;
  7583. /* SSC must be turned on before enabling the CPU output */
  7584. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7585. DRM_DEBUG_KMS("Using SSC on panel\n");
  7586. val |= DREF_SSC1_ENABLE;
  7587. } else
  7588. val &= ~DREF_SSC1_ENABLE;
  7589. /* Get SSC going before enabling the outputs */
  7590. I915_WRITE(PCH_DREF_CONTROL, val);
  7591. POSTING_READ(PCH_DREF_CONTROL);
  7592. udelay(200);
  7593. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7594. /* Enable CPU source on CPU attached eDP */
  7595. if (has_cpu_edp) {
  7596. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7597. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7598. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7599. } else
  7600. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7601. } else
  7602. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7603. I915_WRITE(PCH_DREF_CONTROL, val);
  7604. POSTING_READ(PCH_DREF_CONTROL);
  7605. udelay(200);
  7606. } else {
  7607. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7608. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7609. /* Turn off CPU output */
  7610. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7611. I915_WRITE(PCH_DREF_CONTROL, val);
  7612. POSTING_READ(PCH_DREF_CONTROL);
  7613. udelay(200);
  7614. if (!using_ssc_source) {
  7615. DRM_DEBUG_KMS("Disabling SSC source\n");
  7616. /* Turn off the SSC source */
  7617. val &= ~DREF_SSC_SOURCE_MASK;
  7618. val |= DREF_SSC_SOURCE_DISABLE;
  7619. /* Turn off SSC1 */
  7620. val &= ~DREF_SSC1_ENABLE;
  7621. I915_WRITE(PCH_DREF_CONTROL, val);
  7622. POSTING_READ(PCH_DREF_CONTROL);
  7623. udelay(200);
  7624. }
  7625. }
  7626. BUG_ON(val != final);
  7627. }
  7628. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7629. {
  7630. uint32_t tmp;
  7631. tmp = I915_READ(SOUTH_CHICKEN2);
  7632. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7633. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7634. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  7635. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7636. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7637. tmp = I915_READ(SOUTH_CHICKEN2);
  7638. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7639. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7640. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  7641. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7642. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7643. }
  7644. /* WaMPhyProgramming:hsw */
  7645. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7646. {
  7647. uint32_t tmp;
  7648. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7649. tmp &= ~(0xFF << 24);
  7650. tmp |= (0x12 << 24);
  7651. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7652. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7653. tmp |= (1 << 11);
  7654. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7655. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7656. tmp |= (1 << 11);
  7657. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7658. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7659. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7660. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7661. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7662. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7663. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7664. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7665. tmp &= ~(7 << 13);
  7666. tmp |= (5 << 13);
  7667. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7668. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7669. tmp &= ~(7 << 13);
  7670. tmp |= (5 << 13);
  7671. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7672. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7673. tmp &= ~0xFF;
  7674. tmp |= 0x1C;
  7675. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7676. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7677. tmp &= ~0xFF;
  7678. tmp |= 0x1C;
  7679. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7680. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7681. tmp &= ~(0xFF << 16);
  7682. tmp |= (0x1C << 16);
  7683. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7684. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7685. tmp &= ~(0xFF << 16);
  7686. tmp |= (0x1C << 16);
  7687. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7688. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7689. tmp |= (1 << 27);
  7690. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7691. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7692. tmp |= (1 << 27);
  7693. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7694. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7695. tmp &= ~(0xF << 28);
  7696. tmp |= (4 << 28);
  7697. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7698. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7699. tmp &= ~(0xF << 28);
  7700. tmp |= (4 << 28);
  7701. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7702. }
  7703. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7704. * Programming" based on the parameters passed:
  7705. * - Sequence to enable CLKOUT_DP
  7706. * - Sequence to enable CLKOUT_DP without spread
  7707. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7708. */
  7709. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7710. bool with_fdi)
  7711. {
  7712. struct drm_i915_private *dev_priv = to_i915(dev);
  7713. uint32_t reg, tmp;
  7714. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7715. with_spread = true;
  7716. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7717. with_fdi = false;
  7718. mutex_lock(&dev_priv->sb_lock);
  7719. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7720. tmp &= ~SBI_SSCCTL_DISABLE;
  7721. tmp |= SBI_SSCCTL_PATHALT;
  7722. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7723. udelay(24);
  7724. if (with_spread) {
  7725. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7726. tmp &= ~SBI_SSCCTL_PATHALT;
  7727. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7728. if (with_fdi) {
  7729. lpt_reset_fdi_mphy(dev_priv);
  7730. lpt_program_fdi_mphy(dev_priv);
  7731. }
  7732. }
  7733. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7734. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7735. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7736. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7737. mutex_unlock(&dev_priv->sb_lock);
  7738. }
  7739. /* Sequence to disable CLKOUT_DP */
  7740. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7741. {
  7742. struct drm_i915_private *dev_priv = to_i915(dev);
  7743. uint32_t reg, tmp;
  7744. mutex_lock(&dev_priv->sb_lock);
  7745. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7746. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7747. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7748. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7749. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7750. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7751. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7752. tmp |= SBI_SSCCTL_PATHALT;
  7753. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7754. udelay(32);
  7755. }
  7756. tmp |= SBI_SSCCTL_DISABLE;
  7757. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7758. }
  7759. mutex_unlock(&dev_priv->sb_lock);
  7760. }
  7761. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7762. static const uint16_t sscdivintphase[] = {
  7763. [BEND_IDX( 50)] = 0x3B23,
  7764. [BEND_IDX( 45)] = 0x3B23,
  7765. [BEND_IDX( 40)] = 0x3C23,
  7766. [BEND_IDX( 35)] = 0x3C23,
  7767. [BEND_IDX( 30)] = 0x3D23,
  7768. [BEND_IDX( 25)] = 0x3D23,
  7769. [BEND_IDX( 20)] = 0x3E23,
  7770. [BEND_IDX( 15)] = 0x3E23,
  7771. [BEND_IDX( 10)] = 0x3F23,
  7772. [BEND_IDX( 5)] = 0x3F23,
  7773. [BEND_IDX( 0)] = 0x0025,
  7774. [BEND_IDX( -5)] = 0x0025,
  7775. [BEND_IDX(-10)] = 0x0125,
  7776. [BEND_IDX(-15)] = 0x0125,
  7777. [BEND_IDX(-20)] = 0x0225,
  7778. [BEND_IDX(-25)] = 0x0225,
  7779. [BEND_IDX(-30)] = 0x0325,
  7780. [BEND_IDX(-35)] = 0x0325,
  7781. [BEND_IDX(-40)] = 0x0425,
  7782. [BEND_IDX(-45)] = 0x0425,
  7783. [BEND_IDX(-50)] = 0x0525,
  7784. };
  7785. /*
  7786. * Bend CLKOUT_DP
  7787. * steps -50 to 50 inclusive, in steps of 5
  7788. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7789. * change in clock period = -(steps / 10) * 5.787 ps
  7790. */
  7791. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7792. {
  7793. uint32_t tmp;
  7794. int idx = BEND_IDX(steps);
  7795. if (WARN_ON(steps % 5 != 0))
  7796. return;
  7797. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7798. return;
  7799. mutex_lock(&dev_priv->sb_lock);
  7800. if (steps % 10 != 0)
  7801. tmp = 0xAAAAAAAB;
  7802. else
  7803. tmp = 0x00000000;
  7804. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7805. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7806. tmp &= 0xffff0000;
  7807. tmp |= sscdivintphase[idx];
  7808. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7809. mutex_unlock(&dev_priv->sb_lock);
  7810. }
  7811. #undef BEND_IDX
  7812. static void lpt_init_pch_refclk(struct drm_device *dev)
  7813. {
  7814. struct intel_encoder *encoder;
  7815. bool has_vga = false;
  7816. for_each_intel_encoder(dev, encoder) {
  7817. switch (encoder->type) {
  7818. case INTEL_OUTPUT_ANALOG:
  7819. has_vga = true;
  7820. break;
  7821. default:
  7822. break;
  7823. }
  7824. }
  7825. if (has_vga) {
  7826. lpt_bend_clkout_dp(to_i915(dev), 0);
  7827. lpt_enable_clkout_dp(dev, true, true);
  7828. } else {
  7829. lpt_disable_clkout_dp(dev);
  7830. }
  7831. }
  7832. /*
  7833. * Initialize reference clocks when the driver loads
  7834. */
  7835. void intel_init_pch_refclk(struct drm_device *dev)
  7836. {
  7837. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7838. ironlake_init_pch_refclk(dev);
  7839. else if (HAS_PCH_LPT(dev))
  7840. lpt_init_pch_refclk(dev);
  7841. }
  7842. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7843. {
  7844. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7845. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7846. int pipe = intel_crtc->pipe;
  7847. uint32_t val;
  7848. val = 0;
  7849. switch (intel_crtc->config->pipe_bpp) {
  7850. case 18:
  7851. val |= PIPECONF_6BPC;
  7852. break;
  7853. case 24:
  7854. val |= PIPECONF_8BPC;
  7855. break;
  7856. case 30:
  7857. val |= PIPECONF_10BPC;
  7858. break;
  7859. case 36:
  7860. val |= PIPECONF_12BPC;
  7861. break;
  7862. default:
  7863. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7864. BUG();
  7865. }
  7866. if (intel_crtc->config->dither)
  7867. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7868. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7869. val |= PIPECONF_INTERLACED_ILK;
  7870. else
  7871. val |= PIPECONF_PROGRESSIVE;
  7872. if (intel_crtc->config->limited_color_range)
  7873. val |= PIPECONF_COLOR_RANGE_SELECT;
  7874. I915_WRITE(PIPECONF(pipe), val);
  7875. POSTING_READ(PIPECONF(pipe));
  7876. }
  7877. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7878. {
  7879. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7880. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7881. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7882. u32 val = 0;
  7883. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7884. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7885. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7886. val |= PIPECONF_INTERLACED_ILK;
  7887. else
  7888. val |= PIPECONF_PROGRESSIVE;
  7889. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7890. POSTING_READ(PIPECONF(cpu_transcoder));
  7891. }
  7892. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7893. {
  7894. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7895. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7896. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7897. u32 val = 0;
  7898. switch (intel_crtc->config->pipe_bpp) {
  7899. case 18:
  7900. val |= PIPEMISC_DITHER_6_BPC;
  7901. break;
  7902. case 24:
  7903. val |= PIPEMISC_DITHER_8_BPC;
  7904. break;
  7905. case 30:
  7906. val |= PIPEMISC_DITHER_10_BPC;
  7907. break;
  7908. case 36:
  7909. val |= PIPEMISC_DITHER_12_BPC;
  7910. break;
  7911. default:
  7912. /* Case prevented by pipe_config_set_bpp. */
  7913. BUG();
  7914. }
  7915. if (intel_crtc->config->dither)
  7916. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7917. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7918. }
  7919. }
  7920. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7921. {
  7922. /*
  7923. * Account for spread spectrum to avoid
  7924. * oversubscribing the link. Max center spread
  7925. * is 2.5%; use 5% for safety's sake.
  7926. */
  7927. u32 bps = target_clock * bpp * 21 / 20;
  7928. return DIV_ROUND_UP(bps, link_bw * 8);
  7929. }
  7930. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7931. {
  7932. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7933. }
  7934. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7935. struct intel_crtc_state *crtc_state,
  7936. struct dpll *reduced_clock)
  7937. {
  7938. struct drm_crtc *crtc = &intel_crtc->base;
  7939. struct drm_device *dev = crtc->dev;
  7940. struct drm_i915_private *dev_priv = to_i915(dev);
  7941. u32 dpll, fp, fp2;
  7942. int factor;
  7943. /* Enable autotuning of the PLL clock (if permissible) */
  7944. factor = 21;
  7945. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7946. if ((intel_panel_use_ssc(dev_priv) &&
  7947. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7948. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7949. factor = 25;
  7950. } else if (crtc_state->sdvo_tv_clock)
  7951. factor = 20;
  7952. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7953. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7954. fp |= FP_CB_TUNE;
  7955. if (reduced_clock) {
  7956. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7957. if (reduced_clock->m < factor * reduced_clock->n)
  7958. fp2 |= FP_CB_TUNE;
  7959. } else {
  7960. fp2 = fp;
  7961. }
  7962. dpll = 0;
  7963. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7964. dpll |= DPLLB_MODE_LVDS;
  7965. else
  7966. dpll |= DPLLB_MODE_DAC_SERIAL;
  7967. dpll |= (crtc_state->pixel_multiplier - 1)
  7968. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7969. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7970. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7971. dpll |= DPLL_SDVO_HIGH_SPEED;
  7972. if (intel_crtc_has_dp_encoder(crtc_state))
  7973. dpll |= DPLL_SDVO_HIGH_SPEED;
  7974. /*
  7975. * The high speed IO clock is only really required for
  7976. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  7977. * possible to share the DPLL between CRT and HDMI. Enabling
  7978. * the clock needlessly does no real harm, except use up a
  7979. * bit of power potentially.
  7980. *
  7981. * We'll limit this to IVB with 3 pipes, since it has only two
  7982. * DPLLs and so DPLL sharing is the only way to get three pipes
  7983. * driving PCH ports at the same time. On SNB we could do this,
  7984. * and potentially avoid enabling the second DPLL, but it's not
  7985. * clear if it''s a win or loss power wise. No point in doing
  7986. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  7987. */
  7988. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  7989. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  7990. dpll |= DPLL_SDVO_HIGH_SPEED;
  7991. /* compute bitmask from p1 value */
  7992. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7993. /* also FPA1 */
  7994. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7995. switch (crtc_state->dpll.p2) {
  7996. case 5:
  7997. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7998. break;
  7999. case 7:
  8000. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  8001. break;
  8002. case 10:
  8003. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  8004. break;
  8005. case 14:
  8006. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  8007. break;
  8008. }
  8009. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8010. intel_panel_use_ssc(dev_priv))
  8011. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  8012. else
  8013. dpll |= PLL_REF_INPUT_DREFCLK;
  8014. dpll |= DPLL_VCO_ENABLE;
  8015. crtc_state->dpll_hw_state.dpll = dpll;
  8016. crtc_state->dpll_hw_state.fp0 = fp;
  8017. crtc_state->dpll_hw_state.fp1 = fp2;
  8018. }
  8019. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  8020. struct intel_crtc_state *crtc_state)
  8021. {
  8022. struct drm_device *dev = crtc->base.dev;
  8023. struct drm_i915_private *dev_priv = to_i915(dev);
  8024. struct dpll reduced_clock;
  8025. bool has_reduced_clock = false;
  8026. struct intel_shared_dpll *pll;
  8027. const struct intel_limit *limit;
  8028. int refclk = 120000;
  8029. memset(&crtc_state->dpll_hw_state, 0,
  8030. sizeof(crtc_state->dpll_hw_state));
  8031. crtc->lowfreq_avail = false;
  8032. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  8033. if (!crtc_state->has_pch_encoder)
  8034. return 0;
  8035. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  8036. if (intel_panel_use_ssc(dev_priv)) {
  8037. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  8038. dev_priv->vbt.lvds_ssc_freq);
  8039. refclk = dev_priv->vbt.lvds_ssc_freq;
  8040. }
  8041. if (intel_is_dual_link_lvds(dev)) {
  8042. if (refclk == 100000)
  8043. limit = &intel_limits_ironlake_dual_lvds_100m;
  8044. else
  8045. limit = &intel_limits_ironlake_dual_lvds;
  8046. } else {
  8047. if (refclk == 100000)
  8048. limit = &intel_limits_ironlake_single_lvds_100m;
  8049. else
  8050. limit = &intel_limits_ironlake_single_lvds;
  8051. }
  8052. } else {
  8053. limit = &intel_limits_ironlake_dac;
  8054. }
  8055. if (!crtc_state->clock_set &&
  8056. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  8057. refclk, NULL, &crtc_state->dpll)) {
  8058. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  8059. return -EINVAL;
  8060. }
  8061. ironlake_compute_dpll(crtc, crtc_state,
  8062. has_reduced_clock ? &reduced_clock : NULL);
  8063. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  8064. if (pll == NULL) {
  8065. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  8066. pipe_name(crtc->pipe));
  8067. return -EINVAL;
  8068. }
  8069. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8070. has_reduced_clock)
  8071. crtc->lowfreq_avail = true;
  8072. return 0;
  8073. }
  8074. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  8075. struct intel_link_m_n *m_n)
  8076. {
  8077. struct drm_device *dev = crtc->base.dev;
  8078. struct drm_i915_private *dev_priv = to_i915(dev);
  8079. enum pipe pipe = crtc->pipe;
  8080. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  8081. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  8082. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  8083. & ~TU_SIZE_MASK;
  8084. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  8085. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  8086. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8087. }
  8088. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  8089. enum transcoder transcoder,
  8090. struct intel_link_m_n *m_n,
  8091. struct intel_link_m_n *m2_n2)
  8092. {
  8093. struct drm_device *dev = crtc->base.dev;
  8094. struct drm_i915_private *dev_priv = to_i915(dev);
  8095. enum pipe pipe = crtc->pipe;
  8096. if (INTEL_INFO(dev)->gen >= 5) {
  8097. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  8098. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  8099. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  8100. & ~TU_SIZE_MASK;
  8101. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  8102. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  8103. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8104. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  8105. * gen < 8) and if DRRS is supported (to make sure the
  8106. * registers are not unnecessarily read).
  8107. */
  8108. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  8109. crtc->config->has_drrs) {
  8110. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  8111. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  8112. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  8113. & ~TU_SIZE_MASK;
  8114. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  8115. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  8116. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8117. }
  8118. } else {
  8119. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  8120. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  8121. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  8122. & ~TU_SIZE_MASK;
  8123. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  8124. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  8125. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8126. }
  8127. }
  8128. void intel_dp_get_m_n(struct intel_crtc *crtc,
  8129. struct intel_crtc_state *pipe_config)
  8130. {
  8131. if (pipe_config->has_pch_encoder)
  8132. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  8133. else
  8134. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8135. &pipe_config->dp_m_n,
  8136. &pipe_config->dp_m2_n2);
  8137. }
  8138. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  8139. struct intel_crtc_state *pipe_config)
  8140. {
  8141. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8142. &pipe_config->fdi_m_n, NULL);
  8143. }
  8144. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  8145. struct intel_crtc_state *pipe_config)
  8146. {
  8147. struct drm_device *dev = crtc->base.dev;
  8148. struct drm_i915_private *dev_priv = to_i915(dev);
  8149. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  8150. uint32_t ps_ctrl = 0;
  8151. int id = -1;
  8152. int i;
  8153. /* find scaler attached to this pipe */
  8154. for (i = 0; i < crtc->num_scalers; i++) {
  8155. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  8156. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  8157. id = i;
  8158. pipe_config->pch_pfit.enabled = true;
  8159. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  8160. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  8161. break;
  8162. }
  8163. }
  8164. scaler_state->scaler_id = id;
  8165. if (id >= 0) {
  8166. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  8167. } else {
  8168. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8169. }
  8170. }
  8171. static void
  8172. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  8173. struct intel_initial_plane_config *plane_config)
  8174. {
  8175. struct drm_device *dev = crtc->base.dev;
  8176. struct drm_i915_private *dev_priv = to_i915(dev);
  8177. u32 val, base, offset, stride_mult, tiling;
  8178. int pipe = crtc->pipe;
  8179. int fourcc, pixel_format;
  8180. unsigned int aligned_height;
  8181. struct drm_framebuffer *fb;
  8182. struct intel_framebuffer *intel_fb;
  8183. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8184. if (!intel_fb) {
  8185. DRM_DEBUG_KMS("failed to alloc fb\n");
  8186. return;
  8187. }
  8188. fb = &intel_fb->base;
  8189. val = I915_READ(PLANE_CTL(pipe, 0));
  8190. if (!(val & PLANE_CTL_ENABLE))
  8191. goto error;
  8192. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  8193. fourcc = skl_format_to_fourcc(pixel_format,
  8194. val & PLANE_CTL_ORDER_RGBX,
  8195. val & PLANE_CTL_ALPHA_MASK);
  8196. fb->pixel_format = fourcc;
  8197. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  8198. tiling = val & PLANE_CTL_TILED_MASK;
  8199. switch (tiling) {
  8200. case PLANE_CTL_TILED_LINEAR:
  8201. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  8202. break;
  8203. case PLANE_CTL_TILED_X:
  8204. plane_config->tiling = I915_TILING_X;
  8205. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  8206. break;
  8207. case PLANE_CTL_TILED_Y:
  8208. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  8209. break;
  8210. case PLANE_CTL_TILED_YF:
  8211. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  8212. break;
  8213. default:
  8214. MISSING_CASE(tiling);
  8215. goto error;
  8216. }
  8217. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  8218. plane_config->base = base;
  8219. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  8220. val = I915_READ(PLANE_SIZE(pipe, 0));
  8221. fb->height = ((val >> 16) & 0xfff) + 1;
  8222. fb->width = ((val >> 0) & 0x1fff) + 1;
  8223. val = I915_READ(PLANE_STRIDE(pipe, 0));
  8224. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  8225. fb->pixel_format);
  8226. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  8227. aligned_height = intel_fb_align_height(dev, fb->height,
  8228. fb->pixel_format,
  8229. fb->modifier[0]);
  8230. plane_config->size = fb->pitches[0] * aligned_height;
  8231. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8232. pipe_name(pipe), fb->width, fb->height,
  8233. fb->bits_per_pixel, base, fb->pitches[0],
  8234. plane_config->size);
  8235. plane_config->fb = intel_fb;
  8236. return;
  8237. error:
  8238. kfree(intel_fb);
  8239. }
  8240. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  8241. struct intel_crtc_state *pipe_config)
  8242. {
  8243. struct drm_device *dev = crtc->base.dev;
  8244. struct drm_i915_private *dev_priv = to_i915(dev);
  8245. uint32_t tmp;
  8246. tmp = I915_READ(PF_CTL(crtc->pipe));
  8247. if (tmp & PF_ENABLE) {
  8248. pipe_config->pch_pfit.enabled = true;
  8249. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  8250. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  8251. /* We currently do not free assignements of panel fitters on
  8252. * ivb/hsw (since we don't use the higher upscaling modes which
  8253. * differentiates them) so just WARN about this case for now. */
  8254. if (IS_GEN7(dev)) {
  8255. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  8256. PF_PIPE_SEL_IVB(crtc->pipe));
  8257. }
  8258. }
  8259. }
  8260. static void
  8261. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  8262. struct intel_initial_plane_config *plane_config)
  8263. {
  8264. struct drm_device *dev = crtc->base.dev;
  8265. struct drm_i915_private *dev_priv = to_i915(dev);
  8266. u32 val, base, offset;
  8267. int pipe = crtc->pipe;
  8268. int fourcc, pixel_format;
  8269. unsigned int aligned_height;
  8270. struct drm_framebuffer *fb;
  8271. struct intel_framebuffer *intel_fb;
  8272. val = I915_READ(DSPCNTR(pipe));
  8273. if (!(val & DISPLAY_PLANE_ENABLE))
  8274. return;
  8275. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8276. if (!intel_fb) {
  8277. DRM_DEBUG_KMS("failed to alloc fb\n");
  8278. return;
  8279. }
  8280. fb = &intel_fb->base;
  8281. if (INTEL_INFO(dev)->gen >= 4) {
  8282. if (val & DISPPLANE_TILED) {
  8283. plane_config->tiling = I915_TILING_X;
  8284. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  8285. }
  8286. }
  8287. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  8288. fourcc = i9xx_format_to_fourcc(pixel_format);
  8289. fb->pixel_format = fourcc;
  8290. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  8291. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  8292. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  8293. offset = I915_READ(DSPOFFSET(pipe));
  8294. } else {
  8295. if (plane_config->tiling)
  8296. offset = I915_READ(DSPTILEOFF(pipe));
  8297. else
  8298. offset = I915_READ(DSPLINOFF(pipe));
  8299. }
  8300. plane_config->base = base;
  8301. val = I915_READ(PIPESRC(pipe));
  8302. fb->width = ((val >> 16) & 0xfff) + 1;
  8303. fb->height = ((val >> 0) & 0xfff) + 1;
  8304. val = I915_READ(DSPSTRIDE(pipe));
  8305. fb->pitches[0] = val & 0xffffffc0;
  8306. aligned_height = intel_fb_align_height(dev, fb->height,
  8307. fb->pixel_format,
  8308. fb->modifier[0]);
  8309. plane_config->size = fb->pitches[0] * aligned_height;
  8310. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8311. pipe_name(pipe), fb->width, fb->height,
  8312. fb->bits_per_pixel, base, fb->pitches[0],
  8313. plane_config->size);
  8314. plane_config->fb = intel_fb;
  8315. }
  8316. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  8317. struct intel_crtc_state *pipe_config)
  8318. {
  8319. struct drm_device *dev = crtc->base.dev;
  8320. struct drm_i915_private *dev_priv = to_i915(dev);
  8321. enum intel_display_power_domain power_domain;
  8322. uint32_t tmp;
  8323. bool ret;
  8324. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8325. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8326. return false;
  8327. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8328. pipe_config->shared_dpll = NULL;
  8329. ret = false;
  8330. tmp = I915_READ(PIPECONF(crtc->pipe));
  8331. if (!(tmp & PIPECONF_ENABLE))
  8332. goto out;
  8333. switch (tmp & PIPECONF_BPC_MASK) {
  8334. case PIPECONF_6BPC:
  8335. pipe_config->pipe_bpp = 18;
  8336. break;
  8337. case PIPECONF_8BPC:
  8338. pipe_config->pipe_bpp = 24;
  8339. break;
  8340. case PIPECONF_10BPC:
  8341. pipe_config->pipe_bpp = 30;
  8342. break;
  8343. case PIPECONF_12BPC:
  8344. pipe_config->pipe_bpp = 36;
  8345. break;
  8346. default:
  8347. break;
  8348. }
  8349. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  8350. pipe_config->limited_color_range = true;
  8351. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  8352. struct intel_shared_dpll *pll;
  8353. enum intel_dpll_id pll_id;
  8354. pipe_config->has_pch_encoder = true;
  8355. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  8356. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8357. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8358. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8359. if (HAS_PCH_IBX(dev_priv)) {
  8360. /*
  8361. * The pipe->pch transcoder and pch transcoder->pll
  8362. * mapping is fixed.
  8363. */
  8364. pll_id = (enum intel_dpll_id) crtc->pipe;
  8365. } else {
  8366. tmp = I915_READ(PCH_DPLL_SEL);
  8367. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  8368. pll_id = DPLL_ID_PCH_PLL_B;
  8369. else
  8370. pll_id= DPLL_ID_PCH_PLL_A;
  8371. }
  8372. pipe_config->shared_dpll =
  8373. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  8374. pll = pipe_config->shared_dpll;
  8375. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8376. &pipe_config->dpll_hw_state));
  8377. tmp = pipe_config->dpll_hw_state.dpll;
  8378. pipe_config->pixel_multiplier =
  8379. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  8380. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  8381. ironlake_pch_clock_get(crtc, pipe_config);
  8382. } else {
  8383. pipe_config->pixel_multiplier = 1;
  8384. }
  8385. intel_get_pipe_timings(crtc, pipe_config);
  8386. intel_get_pipe_src_size(crtc, pipe_config);
  8387. ironlake_get_pfit_config(crtc, pipe_config);
  8388. ret = true;
  8389. out:
  8390. intel_display_power_put(dev_priv, power_domain);
  8391. return ret;
  8392. }
  8393. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  8394. {
  8395. struct drm_device *dev = &dev_priv->drm;
  8396. struct intel_crtc *crtc;
  8397. for_each_intel_crtc(dev, crtc)
  8398. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  8399. pipe_name(crtc->pipe));
  8400. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  8401. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  8402. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  8403. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  8404. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  8405. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  8406. "CPU PWM1 enabled\n");
  8407. if (IS_HASWELL(dev))
  8408. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  8409. "CPU PWM2 enabled\n");
  8410. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  8411. "PCH PWM1 enabled\n");
  8412. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  8413. "Utility pin enabled\n");
  8414. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  8415. /*
  8416. * In theory we can still leave IRQs enabled, as long as only the HPD
  8417. * interrupts remain enabled. We used to check for that, but since it's
  8418. * gen-specific and since we only disable LCPLL after we fully disable
  8419. * the interrupts, the check below should be enough.
  8420. */
  8421. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  8422. }
  8423. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  8424. {
  8425. struct drm_device *dev = &dev_priv->drm;
  8426. if (IS_HASWELL(dev))
  8427. return I915_READ(D_COMP_HSW);
  8428. else
  8429. return I915_READ(D_COMP_BDW);
  8430. }
  8431. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  8432. {
  8433. struct drm_device *dev = &dev_priv->drm;
  8434. if (IS_HASWELL(dev)) {
  8435. mutex_lock(&dev_priv->rps.hw_lock);
  8436. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  8437. val))
  8438. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  8439. mutex_unlock(&dev_priv->rps.hw_lock);
  8440. } else {
  8441. I915_WRITE(D_COMP_BDW, val);
  8442. POSTING_READ(D_COMP_BDW);
  8443. }
  8444. }
  8445. /*
  8446. * This function implements pieces of two sequences from BSpec:
  8447. * - Sequence for display software to disable LCPLL
  8448. * - Sequence for display software to allow package C8+
  8449. * The steps implemented here are just the steps that actually touch the LCPLL
  8450. * register. Callers should take care of disabling all the display engine
  8451. * functions, doing the mode unset, fixing interrupts, etc.
  8452. */
  8453. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  8454. bool switch_to_fclk, bool allow_power_down)
  8455. {
  8456. uint32_t val;
  8457. assert_can_disable_lcpll(dev_priv);
  8458. val = I915_READ(LCPLL_CTL);
  8459. if (switch_to_fclk) {
  8460. val |= LCPLL_CD_SOURCE_FCLK;
  8461. I915_WRITE(LCPLL_CTL, val);
  8462. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8463. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8464. DRM_ERROR("Switching to FCLK failed\n");
  8465. val = I915_READ(LCPLL_CTL);
  8466. }
  8467. val |= LCPLL_PLL_DISABLE;
  8468. I915_WRITE(LCPLL_CTL, val);
  8469. POSTING_READ(LCPLL_CTL);
  8470. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  8471. DRM_ERROR("LCPLL still locked\n");
  8472. val = hsw_read_dcomp(dev_priv);
  8473. val |= D_COMP_COMP_DISABLE;
  8474. hsw_write_dcomp(dev_priv, val);
  8475. ndelay(100);
  8476. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  8477. 1))
  8478. DRM_ERROR("D_COMP RCOMP still in progress\n");
  8479. if (allow_power_down) {
  8480. val = I915_READ(LCPLL_CTL);
  8481. val |= LCPLL_POWER_DOWN_ALLOW;
  8482. I915_WRITE(LCPLL_CTL, val);
  8483. POSTING_READ(LCPLL_CTL);
  8484. }
  8485. }
  8486. /*
  8487. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  8488. * source.
  8489. */
  8490. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8491. {
  8492. uint32_t val;
  8493. val = I915_READ(LCPLL_CTL);
  8494. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8495. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8496. return;
  8497. /*
  8498. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8499. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8500. */
  8501. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8502. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8503. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8504. I915_WRITE(LCPLL_CTL, val);
  8505. POSTING_READ(LCPLL_CTL);
  8506. }
  8507. val = hsw_read_dcomp(dev_priv);
  8508. val |= D_COMP_COMP_FORCE;
  8509. val &= ~D_COMP_COMP_DISABLE;
  8510. hsw_write_dcomp(dev_priv, val);
  8511. val = I915_READ(LCPLL_CTL);
  8512. val &= ~LCPLL_PLL_DISABLE;
  8513. I915_WRITE(LCPLL_CTL, val);
  8514. if (intel_wait_for_register(dev_priv,
  8515. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  8516. 5))
  8517. DRM_ERROR("LCPLL not locked yet\n");
  8518. if (val & LCPLL_CD_SOURCE_FCLK) {
  8519. val = I915_READ(LCPLL_CTL);
  8520. val &= ~LCPLL_CD_SOURCE_FCLK;
  8521. I915_WRITE(LCPLL_CTL, val);
  8522. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8523. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8524. DRM_ERROR("Switching back to LCPLL failed\n");
  8525. }
  8526. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8527. intel_update_cdclk(&dev_priv->drm);
  8528. }
  8529. /*
  8530. * Package states C8 and deeper are really deep PC states that can only be
  8531. * reached when all the devices on the system allow it, so even if the graphics
  8532. * device allows PC8+, it doesn't mean the system will actually get to these
  8533. * states. Our driver only allows PC8+ when going into runtime PM.
  8534. *
  8535. * The requirements for PC8+ are that all the outputs are disabled, the power
  8536. * well is disabled and most interrupts are disabled, and these are also
  8537. * requirements for runtime PM. When these conditions are met, we manually do
  8538. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8539. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8540. * hang the machine.
  8541. *
  8542. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8543. * the state of some registers, so when we come back from PC8+ we need to
  8544. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8545. * need to take care of the registers kept by RC6. Notice that this happens even
  8546. * if we don't put the device in PCI D3 state (which is what currently happens
  8547. * because of the runtime PM support).
  8548. *
  8549. * For more, read "Display Sequences for Package C8" on the hardware
  8550. * documentation.
  8551. */
  8552. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8553. {
  8554. struct drm_device *dev = &dev_priv->drm;
  8555. uint32_t val;
  8556. DRM_DEBUG_KMS("Enabling package C8+\n");
  8557. if (HAS_PCH_LPT_LP(dev)) {
  8558. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8559. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8560. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8561. }
  8562. lpt_disable_clkout_dp(dev);
  8563. hsw_disable_lcpll(dev_priv, true, true);
  8564. }
  8565. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8566. {
  8567. struct drm_device *dev = &dev_priv->drm;
  8568. uint32_t val;
  8569. DRM_DEBUG_KMS("Disabling package C8+\n");
  8570. hsw_restore_lcpll(dev_priv);
  8571. lpt_init_pch_refclk(dev);
  8572. if (HAS_PCH_LPT_LP(dev)) {
  8573. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8574. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8575. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8576. }
  8577. }
  8578. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8579. {
  8580. struct drm_device *dev = old_state->dev;
  8581. struct intel_atomic_state *old_intel_state =
  8582. to_intel_atomic_state(old_state);
  8583. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8584. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8585. }
  8586. static int bdw_adjust_min_pipe_pixel_rate(struct intel_crtc_state *crtc_state,
  8587. int pixel_rate)
  8588. {
  8589. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  8590. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8591. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8592. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8593. /* BSpec says "Do not use DisplayPort with CDCLK less than
  8594. * 432 MHz, audio enabled, port width x4, and link rate
  8595. * HBR2 (5.4 GHz), or else there may be audio corruption or
  8596. * screen corruption."
  8597. */
  8598. if (intel_crtc_has_dp_encoder(crtc_state) &&
  8599. crtc_state->has_audio &&
  8600. crtc_state->port_clock >= 540000 &&
  8601. crtc_state->lane_count == 4)
  8602. pixel_rate = max(432000, pixel_rate);
  8603. return pixel_rate;
  8604. }
  8605. /* compute the max rate for new configuration */
  8606. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8607. {
  8608. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8609. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8610. struct drm_crtc *crtc;
  8611. struct drm_crtc_state *cstate;
  8612. struct intel_crtc_state *crtc_state;
  8613. unsigned max_pixel_rate = 0, i;
  8614. enum pipe pipe;
  8615. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8616. sizeof(intel_state->min_pixclk));
  8617. for_each_crtc_in_state(state, crtc, cstate, i) {
  8618. int pixel_rate;
  8619. crtc_state = to_intel_crtc_state(cstate);
  8620. if (!crtc_state->base.enable) {
  8621. intel_state->min_pixclk[i] = 0;
  8622. continue;
  8623. }
  8624. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8625. if (IS_BROADWELL(dev_priv) || IS_GEN9(dev_priv))
  8626. pixel_rate = bdw_adjust_min_pipe_pixel_rate(crtc_state,
  8627. pixel_rate);
  8628. intel_state->min_pixclk[i] = pixel_rate;
  8629. }
  8630. for_each_pipe(dev_priv, pipe)
  8631. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8632. return max_pixel_rate;
  8633. }
  8634. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8635. {
  8636. struct drm_i915_private *dev_priv = to_i915(dev);
  8637. uint32_t val, data;
  8638. int ret;
  8639. if (WARN((I915_READ(LCPLL_CTL) &
  8640. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8641. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8642. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8643. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8644. "trying to change cdclk frequency with cdclk not enabled\n"))
  8645. return;
  8646. mutex_lock(&dev_priv->rps.hw_lock);
  8647. ret = sandybridge_pcode_write(dev_priv,
  8648. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8649. mutex_unlock(&dev_priv->rps.hw_lock);
  8650. if (ret) {
  8651. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8652. return;
  8653. }
  8654. val = I915_READ(LCPLL_CTL);
  8655. val |= LCPLL_CD_SOURCE_FCLK;
  8656. I915_WRITE(LCPLL_CTL, val);
  8657. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8658. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8659. DRM_ERROR("Switching to FCLK failed\n");
  8660. val = I915_READ(LCPLL_CTL);
  8661. val &= ~LCPLL_CLK_FREQ_MASK;
  8662. switch (cdclk) {
  8663. case 450000:
  8664. val |= LCPLL_CLK_FREQ_450;
  8665. data = 0;
  8666. break;
  8667. case 540000:
  8668. val |= LCPLL_CLK_FREQ_54O_BDW;
  8669. data = 1;
  8670. break;
  8671. case 337500:
  8672. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8673. data = 2;
  8674. break;
  8675. case 675000:
  8676. val |= LCPLL_CLK_FREQ_675_BDW;
  8677. data = 3;
  8678. break;
  8679. default:
  8680. WARN(1, "invalid cdclk frequency\n");
  8681. return;
  8682. }
  8683. I915_WRITE(LCPLL_CTL, val);
  8684. val = I915_READ(LCPLL_CTL);
  8685. val &= ~LCPLL_CD_SOURCE_FCLK;
  8686. I915_WRITE(LCPLL_CTL, val);
  8687. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8688. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8689. DRM_ERROR("Switching back to LCPLL failed\n");
  8690. mutex_lock(&dev_priv->rps.hw_lock);
  8691. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8692. mutex_unlock(&dev_priv->rps.hw_lock);
  8693. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8694. intel_update_cdclk(dev);
  8695. WARN(cdclk != dev_priv->cdclk_freq,
  8696. "cdclk requested %d kHz but got %d kHz\n",
  8697. cdclk, dev_priv->cdclk_freq);
  8698. }
  8699. static int broadwell_calc_cdclk(int max_pixclk)
  8700. {
  8701. if (max_pixclk > 540000)
  8702. return 675000;
  8703. else if (max_pixclk > 450000)
  8704. return 540000;
  8705. else if (max_pixclk > 337500)
  8706. return 450000;
  8707. else
  8708. return 337500;
  8709. }
  8710. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8711. {
  8712. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8713. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8714. int max_pixclk = ilk_max_pixel_rate(state);
  8715. int cdclk;
  8716. /*
  8717. * FIXME should also account for plane ratio
  8718. * once 64bpp pixel formats are supported.
  8719. */
  8720. cdclk = broadwell_calc_cdclk(max_pixclk);
  8721. if (cdclk > dev_priv->max_cdclk_freq) {
  8722. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8723. cdclk, dev_priv->max_cdclk_freq);
  8724. return -EINVAL;
  8725. }
  8726. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8727. if (!intel_state->active_crtcs)
  8728. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8729. return 0;
  8730. }
  8731. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8732. {
  8733. struct drm_device *dev = old_state->dev;
  8734. struct intel_atomic_state *old_intel_state =
  8735. to_intel_atomic_state(old_state);
  8736. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8737. broadwell_set_cdclk(dev, req_cdclk);
  8738. }
  8739. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8740. {
  8741. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8742. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8743. const int max_pixclk = ilk_max_pixel_rate(state);
  8744. int vco = intel_state->cdclk_pll_vco;
  8745. int cdclk;
  8746. /*
  8747. * FIXME should also account for plane ratio
  8748. * once 64bpp pixel formats are supported.
  8749. */
  8750. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8751. /*
  8752. * FIXME move the cdclk caclulation to
  8753. * compute_config() so we can fail gracegully.
  8754. */
  8755. if (cdclk > dev_priv->max_cdclk_freq) {
  8756. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8757. cdclk, dev_priv->max_cdclk_freq);
  8758. cdclk = dev_priv->max_cdclk_freq;
  8759. }
  8760. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8761. if (!intel_state->active_crtcs)
  8762. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8763. return 0;
  8764. }
  8765. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8766. {
  8767. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8768. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8769. unsigned int req_cdclk = intel_state->dev_cdclk;
  8770. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8771. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8772. }
  8773. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8774. struct intel_crtc_state *crtc_state)
  8775. {
  8776. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  8777. if (!intel_ddi_pll_select(crtc, crtc_state))
  8778. return -EINVAL;
  8779. }
  8780. crtc->lowfreq_avail = false;
  8781. return 0;
  8782. }
  8783. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8784. enum port port,
  8785. struct intel_crtc_state *pipe_config)
  8786. {
  8787. enum intel_dpll_id id;
  8788. switch (port) {
  8789. case PORT_A:
  8790. id = DPLL_ID_SKL_DPLL0;
  8791. break;
  8792. case PORT_B:
  8793. id = DPLL_ID_SKL_DPLL1;
  8794. break;
  8795. case PORT_C:
  8796. id = DPLL_ID_SKL_DPLL2;
  8797. break;
  8798. default:
  8799. DRM_ERROR("Incorrect port type\n");
  8800. return;
  8801. }
  8802. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8803. }
  8804. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8805. enum port port,
  8806. struct intel_crtc_state *pipe_config)
  8807. {
  8808. enum intel_dpll_id id;
  8809. u32 temp;
  8810. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8811. id = temp >> (port * 3 + 1);
  8812. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  8813. return;
  8814. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8815. }
  8816. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8817. enum port port,
  8818. struct intel_crtc_state *pipe_config)
  8819. {
  8820. enum intel_dpll_id id;
  8821. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8822. switch (ddi_pll_sel) {
  8823. case PORT_CLK_SEL_WRPLL1:
  8824. id = DPLL_ID_WRPLL1;
  8825. break;
  8826. case PORT_CLK_SEL_WRPLL2:
  8827. id = DPLL_ID_WRPLL2;
  8828. break;
  8829. case PORT_CLK_SEL_SPLL:
  8830. id = DPLL_ID_SPLL;
  8831. break;
  8832. case PORT_CLK_SEL_LCPLL_810:
  8833. id = DPLL_ID_LCPLL_810;
  8834. break;
  8835. case PORT_CLK_SEL_LCPLL_1350:
  8836. id = DPLL_ID_LCPLL_1350;
  8837. break;
  8838. case PORT_CLK_SEL_LCPLL_2700:
  8839. id = DPLL_ID_LCPLL_2700;
  8840. break;
  8841. default:
  8842. MISSING_CASE(ddi_pll_sel);
  8843. /* fall through */
  8844. case PORT_CLK_SEL_NONE:
  8845. return;
  8846. }
  8847. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8848. }
  8849. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8850. struct intel_crtc_state *pipe_config,
  8851. unsigned long *power_domain_mask)
  8852. {
  8853. struct drm_device *dev = crtc->base.dev;
  8854. struct drm_i915_private *dev_priv = to_i915(dev);
  8855. enum intel_display_power_domain power_domain;
  8856. u32 tmp;
  8857. /*
  8858. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8859. * transcoder handled below.
  8860. */
  8861. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8862. /*
  8863. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8864. * consistency and less surprising code; it's in always on power).
  8865. */
  8866. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8867. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8868. enum pipe trans_edp_pipe;
  8869. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8870. default:
  8871. WARN(1, "unknown pipe linked to edp transcoder\n");
  8872. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8873. case TRANS_DDI_EDP_INPUT_A_ON:
  8874. trans_edp_pipe = PIPE_A;
  8875. break;
  8876. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8877. trans_edp_pipe = PIPE_B;
  8878. break;
  8879. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8880. trans_edp_pipe = PIPE_C;
  8881. break;
  8882. }
  8883. if (trans_edp_pipe == crtc->pipe)
  8884. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8885. }
  8886. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8887. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8888. return false;
  8889. *power_domain_mask |= BIT(power_domain);
  8890. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8891. return tmp & PIPECONF_ENABLE;
  8892. }
  8893. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8894. struct intel_crtc_state *pipe_config,
  8895. unsigned long *power_domain_mask)
  8896. {
  8897. struct drm_device *dev = crtc->base.dev;
  8898. struct drm_i915_private *dev_priv = to_i915(dev);
  8899. enum intel_display_power_domain power_domain;
  8900. enum port port;
  8901. enum transcoder cpu_transcoder;
  8902. u32 tmp;
  8903. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8904. if (port == PORT_A)
  8905. cpu_transcoder = TRANSCODER_DSI_A;
  8906. else
  8907. cpu_transcoder = TRANSCODER_DSI_C;
  8908. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8909. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8910. continue;
  8911. *power_domain_mask |= BIT(power_domain);
  8912. /*
  8913. * The PLL needs to be enabled with a valid divider
  8914. * configuration, otherwise accessing DSI registers will hang
  8915. * the machine. See BSpec North Display Engine
  8916. * registers/MIPI[BXT]. We can break out here early, since we
  8917. * need the same DSI PLL to be enabled for both DSI ports.
  8918. */
  8919. if (!intel_dsi_pll_is_enabled(dev_priv))
  8920. break;
  8921. /* XXX: this works for video mode only */
  8922. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8923. if (!(tmp & DPI_ENABLE))
  8924. continue;
  8925. tmp = I915_READ(MIPI_CTRL(port));
  8926. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8927. continue;
  8928. pipe_config->cpu_transcoder = cpu_transcoder;
  8929. break;
  8930. }
  8931. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  8932. }
  8933. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8934. struct intel_crtc_state *pipe_config)
  8935. {
  8936. struct drm_device *dev = crtc->base.dev;
  8937. struct drm_i915_private *dev_priv = to_i915(dev);
  8938. struct intel_shared_dpll *pll;
  8939. enum port port;
  8940. uint32_t tmp;
  8941. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8942. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8943. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8944. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8945. else if (IS_BROXTON(dev))
  8946. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8947. else
  8948. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8949. pll = pipe_config->shared_dpll;
  8950. if (pll) {
  8951. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8952. &pipe_config->dpll_hw_state));
  8953. }
  8954. /*
  8955. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8956. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8957. * the PCH transcoder is on.
  8958. */
  8959. if (INTEL_INFO(dev)->gen < 9 &&
  8960. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8961. pipe_config->has_pch_encoder = true;
  8962. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8963. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8964. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8965. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8966. }
  8967. }
  8968. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8969. struct intel_crtc_state *pipe_config)
  8970. {
  8971. struct drm_device *dev = crtc->base.dev;
  8972. struct drm_i915_private *dev_priv = to_i915(dev);
  8973. enum intel_display_power_domain power_domain;
  8974. unsigned long power_domain_mask;
  8975. bool active;
  8976. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8977. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8978. return false;
  8979. power_domain_mask = BIT(power_domain);
  8980. pipe_config->shared_dpll = NULL;
  8981. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8982. if (IS_BROXTON(dev_priv) &&
  8983. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  8984. WARN_ON(active);
  8985. active = true;
  8986. }
  8987. if (!active)
  8988. goto out;
  8989. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8990. haswell_get_ddi_port_state(crtc, pipe_config);
  8991. intel_get_pipe_timings(crtc, pipe_config);
  8992. }
  8993. intel_get_pipe_src_size(crtc, pipe_config);
  8994. pipe_config->gamma_mode =
  8995. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8996. if (INTEL_INFO(dev)->gen >= 9) {
  8997. skl_init_scalers(dev, crtc, pipe_config);
  8998. }
  8999. if (INTEL_INFO(dev)->gen >= 9) {
  9000. pipe_config->scaler_state.scaler_id = -1;
  9001. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  9002. }
  9003. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  9004. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  9005. power_domain_mask |= BIT(power_domain);
  9006. if (INTEL_INFO(dev)->gen >= 9)
  9007. skylake_get_pfit_config(crtc, pipe_config);
  9008. else
  9009. ironlake_get_pfit_config(crtc, pipe_config);
  9010. }
  9011. if (IS_HASWELL(dev))
  9012. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  9013. (I915_READ(IPS_CTL) & IPS_ENABLE);
  9014. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  9015. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  9016. pipe_config->pixel_multiplier =
  9017. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  9018. } else {
  9019. pipe_config->pixel_multiplier = 1;
  9020. }
  9021. out:
  9022. for_each_power_domain(power_domain, power_domain_mask)
  9023. intel_display_power_put(dev_priv, power_domain);
  9024. return active;
  9025. }
  9026. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  9027. const struct intel_plane_state *plane_state)
  9028. {
  9029. struct drm_device *dev = crtc->dev;
  9030. struct drm_i915_private *dev_priv = to_i915(dev);
  9031. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9032. uint32_t cntl = 0, size = 0;
  9033. if (plane_state && plane_state->base.visible) {
  9034. unsigned int width = plane_state->base.crtc_w;
  9035. unsigned int height = plane_state->base.crtc_h;
  9036. unsigned int stride = roundup_pow_of_two(width) * 4;
  9037. switch (stride) {
  9038. default:
  9039. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  9040. width, stride);
  9041. stride = 256;
  9042. /* fallthrough */
  9043. case 256:
  9044. case 512:
  9045. case 1024:
  9046. case 2048:
  9047. break;
  9048. }
  9049. cntl |= CURSOR_ENABLE |
  9050. CURSOR_GAMMA_ENABLE |
  9051. CURSOR_FORMAT_ARGB |
  9052. CURSOR_STRIDE(stride);
  9053. size = (height << 12) | width;
  9054. }
  9055. if (intel_crtc->cursor_cntl != 0 &&
  9056. (intel_crtc->cursor_base != base ||
  9057. intel_crtc->cursor_size != size ||
  9058. intel_crtc->cursor_cntl != cntl)) {
  9059. /* On these chipsets we can only modify the base/size/stride
  9060. * whilst the cursor is disabled.
  9061. */
  9062. I915_WRITE(CURCNTR(PIPE_A), 0);
  9063. POSTING_READ(CURCNTR(PIPE_A));
  9064. intel_crtc->cursor_cntl = 0;
  9065. }
  9066. if (intel_crtc->cursor_base != base) {
  9067. I915_WRITE(CURBASE(PIPE_A), base);
  9068. intel_crtc->cursor_base = base;
  9069. }
  9070. if (intel_crtc->cursor_size != size) {
  9071. I915_WRITE(CURSIZE, size);
  9072. intel_crtc->cursor_size = size;
  9073. }
  9074. if (intel_crtc->cursor_cntl != cntl) {
  9075. I915_WRITE(CURCNTR(PIPE_A), cntl);
  9076. POSTING_READ(CURCNTR(PIPE_A));
  9077. intel_crtc->cursor_cntl = cntl;
  9078. }
  9079. }
  9080. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  9081. const struct intel_plane_state *plane_state)
  9082. {
  9083. struct drm_device *dev = crtc->dev;
  9084. struct drm_i915_private *dev_priv = to_i915(dev);
  9085. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9086. const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
  9087. int pipe = intel_crtc->pipe;
  9088. uint32_t cntl = 0;
  9089. if (INTEL_GEN(dev_priv) >= 9 && wm->dirty_pipes & drm_crtc_mask(crtc))
  9090. skl_write_cursor_wm(intel_crtc, wm);
  9091. if (plane_state && plane_state->base.visible) {
  9092. cntl = MCURSOR_GAMMA_ENABLE;
  9093. switch (plane_state->base.crtc_w) {
  9094. case 64:
  9095. cntl |= CURSOR_MODE_64_ARGB_AX;
  9096. break;
  9097. case 128:
  9098. cntl |= CURSOR_MODE_128_ARGB_AX;
  9099. break;
  9100. case 256:
  9101. cntl |= CURSOR_MODE_256_ARGB_AX;
  9102. break;
  9103. default:
  9104. MISSING_CASE(plane_state->base.crtc_w);
  9105. return;
  9106. }
  9107. cntl |= pipe << 28; /* Connect to correct pipe */
  9108. if (HAS_DDI(dev))
  9109. cntl |= CURSOR_PIPE_CSC_ENABLE;
  9110. if (plane_state->base.rotation == DRM_ROTATE_180)
  9111. cntl |= CURSOR_ROTATE_180;
  9112. }
  9113. if (intel_crtc->cursor_cntl != cntl) {
  9114. I915_WRITE(CURCNTR(pipe), cntl);
  9115. POSTING_READ(CURCNTR(pipe));
  9116. intel_crtc->cursor_cntl = cntl;
  9117. }
  9118. /* and commit changes on next vblank */
  9119. I915_WRITE(CURBASE(pipe), base);
  9120. POSTING_READ(CURBASE(pipe));
  9121. intel_crtc->cursor_base = base;
  9122. }
  9123. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  9124. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  9125. const struct intel_plane_state *plane_state)
  9126. {
  9127. struct drm_device *dev = crtc->dev;
  9128. struct drm_i915_private *dev_priv = to_i915(dev);
  9129. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9130. int pipe = intel_crtc->pipe;
  9131. u32 base = intel_crtc->cursor_addr;
  9132. u32 pos = 0;
  9133. if (plane_state) {
  9134. int x = plane_state->base.crtc_x;
  9135. int y = plane_state->base.crtc_y;
  9136. if (x < 0) {
  9137. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  9138. x = -x;
  9139. }
  9140. pos |= x << CURSOR_X_SHIFT;
  9141. if (y < 0) {
  9142. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  9143. y = -y;
  9144. }
  9145. pos |= y << CURSOR_Y_SHIFT;
  9146. /* ILK+ do this automagically */
  9147. if (HAS_GMCH_DISPLAY(dev) &&
  9148. plane_state->base.rotation == DRM_ROTATE_180) {
  9149. base += (plane_state->base.crtc_h *
  9150. plane_state->base.crtc_w - 1) * 4;
  9151. }
  9152. }
  9153. I915_WRITE(CURPOS(pipe), pos);
  9154. if (IS_845G(dev) || IS_I865G(dev))
  9155. i845_update_cursor(crtc, base, plane_state);
  9156. else
  9157. i9xx_update_cursor(crtc, base, plane_state);
  9158. }
  9159. static bool cursor_size_ok(struct drm_device *dev,
  9160. uint32_t width, uint32_t height)
  9161. {
  9162. if (width == 0 || height == 0)
  9163. return false;
  9164. /*
  9165. * 845g/865g are special in that they are only limited by
  9166. * the width of their cursors, the height is arbitrary up to
  9167. * the precision of the register. Everything else requires
  9168. * square cursors, limited to a few power-of-two sizes.
  9169. */
  9170. if (IS_845G(dev) || IS_I865G(dev)) {
  9171. if ((width & 63) != 0)
  9172. return false;
  9173. if (width > (IS_845G(dev) ? 64 : 512))
  9174. return false;
  9175. if (height > 1023)
  9176. return false;
  9177. } else {
  9178. switch (width | height) {
  9179. case 256:
  9180. case 128:
  9181. if (IS_GEN2(dev))
  9182. return false;
  9183. case 64:
  9184. break;
  9185. default:
  9186. return false;
  9187. }
  9188. }
  9189. return true;
  9190. }
  9191. /* VESA 640x480x72Hz mode to set on the pipe */
  9192. static struct drm_display_mode load_detect_mode = {
  9193. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  9194. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  9195. };
  9196. struct drm_framebuffer *
  9197. __intel_framebuffer_create(struct drm_device *dev,
  9198. struct drm_mode_fb_cmd2 *mode_cmd,
  9199. struct drm_i915_gem_object *obj)
  9200. {
  9201. struct intel_framebuffer *intel_fb;
  9202. int ret;
  9203. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  9204. if (!intel_fb)
  9205. return ERR_PTR(-ENOMEM);
  9206. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  9207. if (ret)
  9208. goto err;
  9209. return &intel_fb->base;
  9210. err:
  9211. kfree(intel_fb);
  9212. return ERR_PTR(ret);
  9213. }
  9214. static struct drm_framebuffer *
  9215. intel_framebuffer_create(struct drm_device *dev,
  9216. struct drm_mode_fb_cmd2 *mode_cmd,
  9217. struct drm_i915_gem_object *obj)
  9218. {
  9219. struct drm_framebuffer *fb;
  9220. int ret;
  9221. ret = i915_mutex_lock_interruptible(dev);
  9222. if (ret)
  9223. return ERR_PTR(ret);
  9224. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  9225. mutex_unlock(&dev->struct_mutex);
  9226. return fb;
  9227. }
  9228. static u32
  9229. intel_framebuffer_pitch_for_width(int width, int bpp)
  9230. {
  9231. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  9232. return ALIGN(pitch, 64);
  9233. }
  9234. static u32
  9235. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  9236. {
  9237. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  9238. return PAGE_ALIGN(pitch * mode->vdisplay);
  9239. }
  9240. static struct drm_framebuffer *
  9241. intel_framebuffer_create_for_mode(struct drm_device *dev,
  9242. struct drm_display_mode *mode,
  9243. int depth, int bpp)
  9244. {
  9245. struct drm_framebuffer *fb;
  9246. struct drm_i915_gem_object *obj;
  9247. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  9248. obj = i915_gem_object_create(dev,
  9249. intel_framebuffer_size_for_mode(mode, bpp));
  9250. if (IS_ERR(obj))
  9251. return ERR_CAST(obj);
  9252. mode_cmd.width = mode->hdisplay;
  9253. mode_cmd.height = mode->vdisplay;
  9254. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  9255. bpp);
  9256. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  9257. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  9258. if (IS_ERR(fb))
  9259. i915_gem_object_put_unlocked(obj);
  9260. return fb;
  9261. }
  9262. static struct drm_framebuffer *
  9263. mode_fits_in_fbdev(struct drm_device *dev,
  9264. struct drm_display_mode *mode)
  9265. {
  9266. #ifdef CONFIG_DRM_FBDEV_EMULATION
  9267. struct drm_i915_private *dev_priv = to_i915(dev);
  9268. struct drm_i915_gem_object *obj;
  9269. struct drm_framebuffer *fb;
  9270. if (!dev_priv->fbdev)
  9271. return NULL;
  9272. if (!dev_priv->fbdev->fb)
  9273. return NULL;
  9274. obj = dev_priv->fbdev->fb->obj;
  9275. BUG_ON(!obj);
  9276. fb = &dev_priv->fbdev->fb->base;
  9277. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  9278. fb->bits_per_pixel))
  9279. return NULL;
  9280. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  9281. return NULL;
  9282. drm_framebuffer_reference(fb);
  9283. return fb;
  9284. #else
  9285. return NULL;
  9286. #endif
  9287. }
  9288. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  9289. struct drm_crtc *crtc,
  9290. struct drm_display_mode *mode,
  9291. struct drm_framebuffer *fb,
  9292. int x, int y)
  9293. {
  9294. struct drm_plane_state *plane_state;
  9295. int hdisplay, vdisplay;
  9296. int ret;
  9297. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  9298. if (IS_ERR(plane_state))
  9299. return PTR_ERR(plane_state);
  9300. if (mode)
  9301. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9302. else
  9303. hdisplay = vdisplay = 0;
  9304. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  9305. if (ret)
  9306. return ret;
  9307. drm_atomic_set_fb_for_plane(plane_state, fb);
  9308. plane_state->crtc_x = 0;
  9309. plane_state->crtc_y = 0;
  9310. plane_state->crtc_w = hdisplay;
  9311. plane_state->crtc_h = vdisplay;
  9312. plane_state->src_x = x << 16;
  9313. plane_state->src_y = y << 16;
  9314. plane_state->src_w = hdisplay << 16;
  9315. plane_state->src_h = vdisplay << 16;
  9316. return 0;
  9317. }
  9318. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  9319. struct drm_display_mode *mode,
  9320. struct intel_load_detect_pipe *old,
  9321. struct drm_modeset_acquire_ctx *ctx)
  9322. {
  9323. struct intel_crtc *intel_crtc;
  9324. struct intel_encoder *intel_encoder =
  9325. intel_attached_encoder(connector);
  9326. struct drm_crtc *possible_crtc;
  9327. struct drm_encoder *encoder = &intel_encoder->base;
  9328. struct drm_crtc *crtc = NULL;
  9329. struct drm_device *dev = encoder->dev;
  9330. struct drm_framebuffer *fb;
  9331. struct drm_mode_config *config = &dev->mode_config;
  9332. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  9333. struct drm_connector_state *connector_state;
  9334. struct intel_crtc_state *crtc_state;
  9335. int ret, i = -1;
  9336. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9337. connector->base.id, connector->name,
  9338. encoder->base.id, encoder->name);
  9339. old->restore_state = NULL;
  9340. retry:
  9341. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  9342. if (ret)
  9343. goto fail;
  9344. /*
  9345. * Algorithm gets a little messy:
  9346. *
  9347. * - if the connector already has an assigned crtc, use it (but make
  9348. * sure it's on first)
  9349. *
  9350. * - try to find the first unused crtc that can drive this connector,
  9351. * and use that if we find one
  9352. */
  9353. /* See if we already have a CRTC for this connector */
  9354. if (connector->state->crtc) {
  9355. crtc = connector->state->crtc;
  9356. ret = drm_modeset_lock(&crtc->mutex, ctx);
  9357. if (ret)
  9358. goto fail;
  9359. /* Make sure the crtc and connector are running */
  9360. goto found;
  9361. }
  9362. /* Find an unused one (if possible) */
  9363. for_each_crtc(dev, possible_crtc) {
  9364. i++;
  9365. if (!(encoder->possible_crtcs & (1 << i)))
  9366. continue;
  9367. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  9368. if (ret)
  9369. goto fail;
  9370. if (possible_crtc->state->enable) {
  9371. drm_modeset_unlock(&possible_crtc->mutex);
  9372. continue;
  9373. }
  9374. crtc = possible_crtc;
  9375. break;
  9376. }
  9377. /*
  9378. * If we didn't find an unused CRTC, don't use any.
  9379. */
  9380. if (!crtc) {
  9381. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  9382. goto fail;
  9383. }
  9384. found:
  9385. intel_crtc = to_intel_crtc(crtc);
  9386. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  9387. if (ret)
  9388. goto fail;
  9389. state = drm_atomic_state_alloc(dev);
  9390. restore_state = drm_atomic_state_alloc(dev);
  9391. if (!state || !restore_state) {
  9392. ret = -ENOMEM;
  9393. goto fail;
  9394. }
  9395. state->acquire_ctx = ctx;
  9396. restore_state->acquire_ctx = ctx;
  9397. connector_state = drm_atomic_get_connector_state(state, connector);
  9398. if (IS_ERR(connector_state)) {
  9399. ret = PTR_ERR(connector_state);
  9400. goto fail;
  9401. }
  9402. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  9403. if (ret)
  9404. goto fail;
  9405. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  9406. if (IS_ERR(crtc_state)) {
  9407. ret = PTR_ERR(crtc_state);
  9408. goto fail;
  9409. }
  9410. crtc_state->base.active = crtc_state->base.enable = true;
  9411. if (!mode)
  9412. mode = &load_detect_mode;
  9413. /* We need a framebuffer large enough to accommodate all accesses
  9414. * that the plane may generate whilst we perform load detection.
  9415. * We can not rely on the fbcon either being present (we get called
  9416. * during its initialisation to detect all boot displays, or it may
  9417. * not even exist) or that it is large enough to satisfy the
  9418. * requested mode.
  9419. */
  9420. fb = mode_fits_in_fbdev(dev, mode);
  9421. if (fb == NULL) {
  9422. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  9423. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  9424. } else
  9425. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  9426. if (IS_ERR(fb)) {
  9427. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  9428. goto fail;
  9429. }
  9430. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  9431. if (ret)
  9432. goto fail;
  9433. drm_framebuffer_unreference(fb);
  9434. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  9435. if (ret)
  9436. goto fail;
  9437. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  9438. if (!ret)
  9439. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  9440. if (!ret)
  9441. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  9442. if (ret) {
  9443. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  9444. goto fail;
  9445. }
  9446. ret = drm_atomic_commit(state);
  9447. if (ret) {
  9448. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  9449. goto fail;
  9450. }
  9451. old->restore_state = restore_state;
  9452. /* let the connector get through one full cycle before testing */
  9453. intel_wait_for_vblank(dev, intel_crtc->pipe);
  9454. return true;
  9455. fail:
  9456. drm_atomic_state_free(state);
  9457. drm_atomic_state_free(restore_state);
  9458. restore_state = state = NULL;
  9459. if (ret == -EDEADLK) {
  9460. drm_modeset_backoff(ctx);
  9461. goto retry;
  9462. }
  9463. return false;
  9464. }
  9465. void intel_release_load_detect_pipe(struct drm_connector *connector,
  9466. struct intel_load_detect_pipe *old,
  9467. struct drm_modeset_acquire_ctx *ctx)
  9468. {
  9469. struct intel_encoder *intel_encoder =
  9470. intel_attached_encoder(connector);
  9471. struct drm_encoder *encoder = &intel_encoder->base;
  9472. struct drm_atomic_state *state = old->restore_state;
  9473. int ret;
  9474. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9475. connector->base.id, connector->name,
  9476. encoder->base.id, encoder->name);
  9477. if (!state)
  9478. return;
  9479. ret = drm_atomic_commit(state);
  9480. if (ret) {
  9481. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  9482. drm_atomic_state_free(state);
  9483. }
  9484. }
  9485. static int i9xx_pll_refclk(struct drm_device *dev,
  9486. const struct intel_crtc_state *pipe_config)
  9487. {
  9488. struct drm_i915_private *dev_priv = to_i915(dev);
  9489. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9490. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  9491. return dev_priv->vbt.lvds_ssc_freq;
  9492. else if (HAS_PCH_SPLIT(dev))
  9493. return 120000;
  9494. else if (!IS_GEN2(dev))
  9495. return 96000;
  9496. else
  9497. return 48000;
  9498. }
  9499. /* Returns the clock of the currently programmed mode of the given pipe. */
  9500. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  9501. struct intel_crtc_state *pipe_config)
  9502. {
  9503. struct drm_device *dev = crtc->base.dev;
  9504. struct drm_i915_private *dev_priv = to_i915(dev);
  9505. int pipe = pipe_config->cpu_transcoder;
  9506. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9507. u32 fp;
  9508. struct dpll clock;
  9509. int port_clock;
  9510. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9511. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9512. fp = pipe_config->dpll_hw_state.fp0;
  9513. else
  9514. fp = pipe_config->dpll_hw_state.fp1;
  9515. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9516. if (IS_PINEVIEW(dev)) {
  9517. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9518. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9519. } else {
  9520. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9521. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9522. }
  9523. if (!IS_GEN2(dev)) {
  9524. if (IS_PINEVIEW(dev))
  9525. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9526. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9527. else
  9528. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9529. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9530. switch (dpll & DPLL_MODE_MASK) {
  9531. case DPLLB_MODE_DAC_SERIAL:
  9532. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9533. 5 : 10;
  9534. break;
  9535. case DPLLB_MODE_LVDS:
  9536. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9537. 7 : 14;
  9538. break;
  9539. default:
  9540. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9541. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9542. return;
  9543. }
  9544. if (IS_PINEVIEW(dev))
  9545. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9546. else
  9547. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9548. } else {
  9549. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  9550. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9551. if (is_lvds) {
  9552. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9553. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9554. if (lvds & LVDS_CLKB_POWER_UP)
  9555. clock.p2 = 7;
  9556. else
  9557. clock.p2 = 14;
  9558. } else {
  9559. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9560. clock.p1 = 2;
  9561. else {
  9562. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9563. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9564. }
  9565. if (dpll & PLL_P2_DIVIDE_BY_4)
  9566. clock.p2 = 4;
  9567. else
  9568. clock.p2 = 2;
  9569. }
  9570. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9571. }
  9572. /*
  9573. * This value includes pixel_multiplier. We will use
  9574. * port_clock to compute adjusted_mode.crtc_clock in the
  9575. * encoder's get_config() function.
  9576. */
  9577. pipe_config->port_clock = port_clock;
  9578. }
  9579. int intel_dotclock_calculate(int link_freq,
  9580. const struct intel_link_m_n *m_n)
  9581. {
  9582. /*
  9583. * The calculation for the data clock is:
  9584. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9585. * But we want to avoid losing precison if possible, so:
  9586. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9587. *
  9588. * and the link clock is simpler:
  9589. * link_clock = (m * link_clock) / n
  9590. */
  9591. if (!m_n->link_n)
  9592. return 0;
  9593. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9594. }
  9595. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9596. struct intel_crtc_state *pipe_config)
  9597. {
  9598. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9599. /* read out port_clock from the DPLL */
  9600. i9xx_crtc_clock_get(crtc, pipe_config);
  9601. /*
  9602. * In case there is an active pipe without active ports,
  9603. * we may need some idea for the dotclock anyway.
  9604. * Calculate one based on the FDI configuration.
  9605. */
  9606. pipe_config->base.adjusted_mode.crtc_clock =
  9607. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9608. &pipe_config->fdi_m_n);
  9609. }
  9610. /** Returns the currently programmed mode of the given pipe. */
  9611. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9612. struct drm_crtc *crtc)
  9613. {
  9614. struct drm_i915_private *dev_priv = to_i915(dev);
  9615. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9616. enum transcoder cpu_transcoder;
  9617. struct drm_display_mode *mode;
  9618. struct intel_crtc_state *pipe_config;
  9619. u32 htot, hsync, vtot, vsync;
  9620. enum pipe pipe = intel_crtc->pipe;
  9621. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9622. if (!mode)
  9623. return NULL;
  9624. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9625. if (!pipe_config) {
  9626. kfree(mode);
  9627. return NULL;
  9628. }
  9629. /*
  9630. * Construct a pipe_config sufficient for getting the clock info
  9631. * back out of crtc_clock_get.
  9632. *
  9633. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9634. * to use a real value here instead.
  9635. */
  9636. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9637. pipe_config->pixel_multiplier = 1;
  9638. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9639. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9640. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9641. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9642. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9643. cpu_transcoder = pipe_config->cpu_transcoder;
  9644. htot = I915_READ(HTOTAL(cpu_transcoder));
  9645. hsync = I915_READ(HSYNC(cpu_transcoder));
  9646. vtot = I915_READ(VTOTAL(cpu_transcoder));
  9647. vsync = I915_READ(VSYNC(cpu_transcoder));
  9648. mode->hdisplay = (htot & 0xffff) + 1;
  9649. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9650. mode->hsync_start = (hsync & 0xffff) + 1;
  9651. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9652. mode->vdisplay = (vtot & 0xffff) + 1;
  9653. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9654. mode->vsync_start = (vsync & 0xffff) + 1;
  9655. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9656. drm_mode_set_name(mode);
  9657. kfree(pipe_config);
  9658. return mode;
  9659. }
  9660. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9661. {
  9662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9663. struct drm_device *dev = crtc->dev;
  9664. struct intel_flip_work *work;
  9665. spin_lock_irq(&dev->event_lock);
  9666. work = intel_crtc->flip_work;
  9667. intel_crtc->flip_work = NULL;
  9668. spin_unlock_irq(&dev->event_lock);
  9669. if (work) {
  9670. cancel_work_sync(&work->mmio_work);
  9671. cancel_work_sync(&work->unpin_work);
  9672. kfree(work);
  9673. }
  9674. drm_crtc_cleanup(crtc);
  9675. kfree(intel_crtc);
  9676. }
  9677. static void intel_unpin_work_fn(struct work_struct *__work)
  9678. {
  9679. struct intel_flip_work *work =
  9680. container_of(__work, struct intel_flip_work, unpin_work);
  9681. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9682. struct drm_device *dev = crtc->base.dev;
  9683. struct drm_plane *primary = crtc->base.primary;
  9684. if (is_mmio_work(work))
  9685. flush_work(&work->mmio_work);
  9686. mutex_lock(&dev->struct_mutex);
  9687. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9688. i915_gem_object_put(work->pending_flip_obj);
  9689. mutex_unlock(&dev->struct_mutex);
  9690. i915_gem_request_put(work->flip_queued_req);
  9691. intel_frontbuffer_flip_complete(to_i915(dev),
  9692. to_intel_plane(primary)->frontbuffer_bit);
  9693. intel_fbc_post_update(crtc);
  9694. drm_framebuffer_unreference(work->old_fb);
  9695. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9696. atomic_dec(&crtc->unpin_work_count);
  9697. kfree(work);
  9698. }
  9699. /* Is 'a' after or equal to 'b'? */
  9700. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9701. {
  9702. return !((a - b) & 0x80000000);
  9703. }
  9704. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9705. struct intel_flip_work *work)
  9706. {
  9707. struct drm_device *dev = crtc->base.dev;
  9708. struct drm_i915_private *dev_priv = to_i915(dev);
  9709. if (abort_flip_on_reset(crtc))
  9710. return true;
  9711. /*
  9712. * The relevant registers doen't exist on pre-ctg.
  9713. * As the flip done interrupt doesn't trigger for mmio
  9714. * flips on gmch platforms, a flip count check isn't
  9715. * really needed there. But since ctg has the registers,
  9716. * include it in the check anyway.
  9717. */
  9718. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9719. return true;
  9720. /*
  9721. * BDW signals flip done immediately if the plane
  9722. * is disabled, even if the plane enable is already
  9723. * armed to occur at the next vblank :(
  9724. */
  9725. /*
  9726. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9727. * used the same base address. In that case the mmio flip might
  9728. * have completed, but the CS hasn't even executed the flip yet.
  9729. *
  9730. * A flip count check isn't enough as the CS might have updated
  9731. * the base address just after start of vblank, but before we
  9732. * managed to process the interrupt. This means we'd complete the
  9733. * CS flip too soon.
  9734. *
  9735. * Combining both checks should get us a good enough result. It may
  9736. * still happen that the CS flip has been executed, but has not
  9737. * yet actually completed. But in case the base address is the same
  9738. * anyway, we don't really care.
  9739. */
  9740. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9741. crtc->flip_work->gtt_offset &&
  9742. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9743. crtc->flip_work->flip_count);
  9744. }
  9745. static bool
  9746. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9747. struct intel_flip_work *work)
  9748. {
  9749. /*
  9750. * MMIO work completes when vblank is different from
  9751. * flip_queued_vblank.
  9752. *
  9753. * Reset counter value doesn't matter, this is handled by
  9754. * i915_wait_request finishing early, so no need to handle
  9755. * reset here.
  9756. */
  9757. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9758. }
  9759. static bool pageflip_finished(struct intel_crtc *crtc,
  9760. struct intel_flip_work *work)
  9761. {
  9762. if (!atomic_read(&work->pending))
  9763. return false;
  9764. smp_rmb();
  9765. if (is_mmio_work(work))
  9766. return __pageflip_finished_mmio(crtc, work);
  9767. else
  9768. return __pageflip_finished_cs(crtc, work);
  9769. }
  9770. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9771. {
  9772. struct drm_device *dev = &dev_priv->drm;
  9773. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9774. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9775. struct intel_flip_work *work;
  9776. unsigned long flags;
  9777. /* Ignore early vblank irqs */
  9778. if (!crtc)
  9779. return;
  9780. /*
  9781. * This is called both by irq handlers and the reset code (to complete
  9782. * lost pageflips) so needs the full irqsave spinlocks.
  9783. */
  9784. spin_lock_irqsave(&dev->event_lock, flags);
  9785. work = intel_crtc->flip_work;
  9786. if (work != NULL &&
  9787. !is_mmio_work(work) &&
  9788. pageflip_finished(intel_crtc, work))
  9789. page_flip_completed(intel_crtc);
  9790. spin_unlock_irqrestore(&dev->event_lock, flags);
  9791. }
  9792. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9793. {
  9794. struct drm_device *dev = &dev_priv->drm;
  9795. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9796. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9797. struct intel_flip_work *work;
  9798. unsigned long flags;
  9799. /* Ignore early vblank irqs */
  9800. if (!crtc)
  9801. return;
  9802. /*
  9803. * This is called both by irq handlers and the reset code (to complete
  9804. * lost pageflips) so needs the full irqsave spinlocks.
  9805. */
  9806. spin_lock_irqsave(&dev->event_lock, flags);
  9807. work = intel_crtc->flip_work;
  9808. if (work != NULL &&
  9809. is_mmio_work(work) &&
  9810. pageflip_finished(intel_crtc, work))
  9811. page_flip_completed(intel_crtc);
  9812. spin_unlock_irqrestore(&dev->event_lock, flags);
  9813. }
  9814. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9815. struct intel_flip_work *work)
  9816. {
  9817. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9818. /* Ensure that the work item is consistent when activating it ... */
  9819. smp_mb__before_atomic();
  9820. atomic_set(&work->pending, 1);
  9821. }
  9822. static int intel_gen2_queue_flip(struct drm_device *dev,
  9823. struct drm_crtc *crtc,
  9824. struct drm_framebuffer *fb,
  9825. struct drm_i915_gem_object *obj,
  9826. struct drm_i915_gem_request *req,
  9827. uint32_t flags)
  9828. {
  9829. struct intel_ring *ring = req->ring;
  9830. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9831. u32 flip_mask;
  9832. int ret;
  9833. ret = intel_ring_begin(req, 6);
  9834. if (ret)
  9835. return ret;
  9836. /* Can't queue multiple flips, so wait for the previous
  9837. * one to finish before executing the next.
  9838. */
  9839. if (intel_crtc->plane)
  9840. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9841. else
  9842. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9843. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9844. intel_ring_emit(ring, MI_NOOP);
  9845. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9846. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9847. intel_ring_emit(ring, fb->pitches[0]);
  9848. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9849. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9850. return 0;
  9851. }
  9852. static int intel_gen3_queue_flip(struct drm_device *dev,
  9853. struct drm_crtc *crtc,
  9854. struct drm_framebuffer *fb,
  9855. struct drm_i915_gem_object *obj,
  9856. struct drm_i915_gem_request *req,
  9857. uint32_t flags)
  9858. {
  9859. struct intel_ring *ring = req->ring;
  9860. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9861. u32 flip_mask;
  9862. int ret;
  9863. ret = intel_ring_begin(req, 6);
  9864. if (ret)
  9865. return ret;
  9866. if (intel_crtc->plane)
  9867. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9868. else
  9869. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9870. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9871. intel_ring_emit(ring, MI_NOOP);
  9872. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9873. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9874. intel_ring_emit(ring, fb->pitches[0]);
  9875. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9876. intel_ring_emit(ring, MI_NOOP);
  9877. return 0;
  9878. }
  9879. static int intel_gen4_queue_flip(struct drm_device *dev,
  9880. struct drm_crtc *crtc,
  9881. struct drm_framebuffer *fb,
  9882. struct drm_i915_gem_object *obj,
  9883. struct drm_i915_gem_request *req,
  9884. uint32_t flags)
  9885. {
  9886. struct intel_ring *ring = req->ring;
  9887. struct drm_i915_private *dev_priv = to_i915(dev);
  9888. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9889. uint32_t pf, pipesrc;
  9890. int ret;
  9891. ret = intel_ring_begin(req, 4);
  9892. if (ret)
  9893. return ret;
  9894. /* i965+ uses the linear or tiled offsets from the
  9895. * Display Registers (which do not change across a page-flip)
  9896. * so we need only reprogram the base address.
  9897. */
  9898. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9899. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9900. intel_ring_emit(ring, fb->pitches[0]);
  9901. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
  9902. intel_fb_modifier_to_tiling(fb->modifier[0]));
  9903. /* XXX Enabling the panel-fitter across page-flip is so far
  9904. * untested on non-native modes, so ignore it for now.
  9905. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9906. */
  9907. pf = 0;
  9908. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9909. intel_ring_emit(ring, pf | pipesrc);
  9910. return 0;
  9911. }
  9912. static int intel_gen6_queue_flip(struct drm_device *dev,
  9913. struct drm_crtc *crtc,
  9914. struct drm_framebuffer *fb,
  9915. struct drm_i915_gem_object *obj,
  9916. struct drm_i915_gem_request *req,
  9917. uint32_t flags)
  9918. {
  9919. struct intel_ring *ring = req->ring;
  9920. struct drm_i915_private *dev_priv = to_i915(dev);
  9921. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9922. uint32_t pf, pipesrc;
  9923. int ret;
  9924. ret = intel_ring_begin(req, 4);
  9925. if (ret)
  9926. return ret;
  9927. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9928. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9929. intel_ring_emit(ring, fb->pitches[0] |
  9930. intel_fb_modifier_to_tiling(fb->modifier[0]));
  9931. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9932. /* Contrary to the suggestions in the documentation,
  9933. * "Enable Panel Fitter" does not seem to be required when page
  9934. * flipping with a non-native mode, and worse causes a normal
  9935. * modeset to fail.
  9936. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9937. */
  9938. pf = 0;
  9939. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9940. intel_ring_emit(ring, pf | pipesrc);
  9941. return 0;
  9942. }
  9943. static int intel_gen7_queue_flip(struct drm_device *dev,
  9944. struct drm_crtc *crtc,
  9945. struct drm_framebuffer *fb,
  9946. struct drm_i915_gem_object *obj,
  9947. struct drm_i915_gem_request *req,
  9948. uint32_t flags)
  9949. {
  9950. struct intel_ring *ring = req->ring;
  9951. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9952. uint32_t plane_bit = 0;
  9953. int len, ret;
  9954. switch (intel_crtc->plane) {
  9955. case PLANE_A:
  9956. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9957. break;
  9958. case PLANE_B:
  9959. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9960. break;
  9961. case PLANE_C:
  9962. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9963. break;
  9964. default:
  9965. WARN_ONCE(1, "unknown plane in flip command\n");
  9966. return -ENODEV;
  9967. }
  9968. len = 4;
  9969. if (req->engine->id == RCS) {
  9970. len += 6;
  9971. /*
  9972. * On Gen 8, SRM is now taking an extra dword to accommodate
  9973. * 48bits addresses, and we need a NOOP for the batch size to
  9974. * stay even.
  9975. */
  9976. if (IS_GEN8(dev))
  9977. len += 2;
  9978. }
  9979. /*
  9980. * BSpec MI_DISPLAY_FLIP for IVB:
  9981. * "The full packet must be contained within the same cache line."
  9982. *
  9983. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9984. * cacheline, if we ever start emitting more commands before
  9985. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9986. * then do the cacheline alignment, and finally emit the
  9987. * MI_DISPLAY_FLIP.
  9988. */
  9989. ret = intel_ring_cacheline_align(req);
  9990. if (ret)
  9991. return ret;
  9992. ret = intel_ring_begin(req, len);
  9993. if (ret)
  9994. return ret;
  9995. /* Unmask the flip-done completion message. Note that the bspec says that
  9996. * we should do this for both the BCS and RCS, and that we must not unmask
  9997. * more than one flip event at any time (or ensure that one flip message
  9998. * can be sent by waiting for flip-done prior to queueing new flips).
  9999. * Experimentation says that BCS works despite DERRMR masking all
  10000. * flip-done completion events and that unmasking all planes at once
  10001. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  10002. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  10003. */
  10004. if (req->engine->id == RCS) {
  10005. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  10006. intel_ring_emit_reg(ring, DERRMR);
  10007. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  10008. DERRMR_PIPEB_PRI_FLIP_DONE |
  10009. DERRMR_PIPEC_PRI_FLIP_DONE));
  10010. if (IS_GEN8(dev))
  10011. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  10012. MI_SRM_LRM_GLOBAL_GTT);
  10013. else
  10014. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  10015. MI_SRM_LRM_GLOBAL_GTT);
  10016. intel_ring_emit_reg(ring, DERRMR);
  10017. intel_ring_emit(ring,
  10018. i915_ggtt_offset(req->engine->scratch) + 256);
  10019. if (IS_GEN8(dev)) {
  10020. intel_ring_emit(ring, 0);
  10021. intel_ring_emit(ring, MI_NOOP);
  10022. }
  10023. }
  10024. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  10025. intel_ring_emit(ring, fb->pitches[0] |
  10026. intel_fb_modifier_to_tiling(fb->modifier[0]));
  10027. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  10028. intel_ring_emit(ring, (MI_NOOP));
  10029. return 0;
  10030. }
  10031. static bool use_mmio_flip(struct intel_engine_cs *engine,
  10032. struct drm_i915_gem_object *obj)
  10033. {
  10034. struct reservation_object *resv;
  10035. /*
  10036. * This is not being used for older platforms, because
  10037. * non-availability of flip done interrupt forces us to use
  10038. * CS flips. Older platforms derive flip done using some clever
  10039. * tricks involving the flip_pending status bits and vblank irqs.
  10040. * So using MMIO flips there would disrupt this mechanism.
  10041. */
  10042. if (engine == NULL)
  10043. return true;
  10044. if (INTEL_GEN(engine->i915) < 5)
  10045. return false;
  10046. if (i915.use_mmio_flip < 0)
  10047. return false;
  10048. else if (i915.use_mmio_flip > 0)
  10049. return true;
  10050. else if (i915.enable_execlists)
  10051. return true;
  10052. resv = i915_gem_object_get_dmabuf_resv(obj);
  10053. if (resv && !reservation_object_test_signaled_rcu(resv, false))
  10054. return true;
  10055. return engine != i915_gem_active_get_engine(&obj->last_write,
  10056. &obj->base.dev->struct_mutex);
  10057. }
  10058. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  10059. unsigned int rotation,
  10060. struct intel_flip_work *work)
  10061. {
  10062. struct drm_device *dev = intel_crtc->base.dev;
  10063. struct drm_i915_private *dev_priv = to_i915(dev);
  10064. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10065. const enum pipe pipe = intel_crtc->pipe;
  10066. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  10067. ctl = I915_READ(PLANE_CTL(pipe, 0));
  10068. ctl &= ~PLANE_CTL_TILED_MASK;
  10069. switch (fb->modifier[0]) {
  10070. case DRM_FORMAT_MOD_NONE:
  10071. break;
  10072. case I915_FORMAT_MOD_X_TILED:
  10073. ctl |= PLANE_CTL_TILED_X;
  10074. break;
  10075. case I915_FORMAT_MOD_Y_TILED:
  10076. ctl |= PLANE_CTL_TILED_Y;
  10077. break;
  10078. case I915_FORMAT_MOD_Yf_TILED:
  10079. ctl |= PLANE_CTL_TILED_YF;
  10080. break;
  10081. default:
  10082. MISSING_CASE(fb->modifier[0]);
  10083. }
  10084. /*
  10085. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  10086. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  10087. */
  10088. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  10089. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  10090. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  10091. POSTING_READ(PLANE_SURF(pipe, 0));
  10092. }
  10093. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  10094. struct intel_flip_work *work)
  10095. {
  10096. struct drm_device *dev = intel_crtc->base.dev;
  10097. struct drm_i915_private *dev_priv = to_i915(dev);
  10098. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10099. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  10100. u32 dspcntr;
  10101. dspcntr = I915_READ(reg);
  10102. if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
  10103. dspcntr |= DISPPLANE_TILED;
  10104. else
  10105. dspcntr &= ~DISPPLANE_TILED;
  10106. I915_WRITE(reg, dspcntr);
  10107. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  10108. POSTING_READ(DSPSURF(intel_crtc->plane));
  10109. }
  10110. static void intel_mmio_flip_work_func(struct work_struct *w)
  10111. {
  10112. struct intel_flip_work *work =
  10113. container_of(w, struct intel_flip_work, mmio_work);
  10114. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  10115. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10116. struct intel_framebuffer *intel_fb =
  10117. to_intel_framebuffer(crtc->base.primary->fb);
  10118. struct drm_i915_gem_object *obj = intel_fb->obj;
  10119. struct reservation_object *resv;
  10120. if (work->flip_queued_req)
  10121. WARN_ON(i915_wait_request(work->flip_queued_req,
  10122. 0, NULL, NO_WAITBOOST));
  10123. /* For framebuffer backed by dmabuf, wait for fence */
  10124. resv = i915_gem_object_get_dmabuf_resv(obj);
  10125. if (resv)
  10126. WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
  10127. MAX_SCHEDULE_TIMEOUT) < 0);
  10128. intel_pipe_update_start(crtc);
  10129. if (INTEL_GEN(dev_priv) >= 9)
  10130. skl_do_mmio_flip(crtc, work->rotation, work);
  10131. else
  10132. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  10133. ilk_do_mmio_flip(crtc, work);
  10134. intel_pipe_update_end(crtc, work);
  10135. }
  10136. static int intel_default_queue_flip(struct drm_device *dev,
  10137. struct drm_crtc *crtc,
  10138. struct drm_framebuffer *fb,
  10139. struct drm_i915_gem_object *obj,
  10140. struct drm_i915_gem_request *req,
  10141. uint32_t flags)
  10142. {
  10143. return -ENODEV;
  10144. }
  10145. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  10146. struct intel_crtc *intel_crtc,
  10147. struct intel_flip_work *work)
  10148. {
  10149. u32 addr, vblank;
  10150. if (!atomic_read(&work->pending))
  10151. return false;
  10152. smp_rmb();
  10153. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  10154. if (work->flip_ready_vblank == 0) {
  10155. if (work->flip_queued_req &&
  10156. !i915_gem_request_completed(work->flip_queued_req))
  10157. return false;
  10158. work->flip_ready_vblank = vblank;
  10159. }
  10160. if (vblank - work->flip_ready_vblank < 3)
  10161. return false;
  10162. /* Potential stall - if we see that the flip has happened,
  10163. * assume a missed interrupt. */
  10164. if (INTEL_GEN(dev_priv) >= 4)
  10165. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  10166. else
  10167. addr = I915_READ(DSPADDR(intel_crtc->plane));
  10168. /* There is a potential issue here with a false positive after a flip
  10169. * to the same address. We could address this by checking for a
  10170. * non-incrementing frame counter.
  10171. */
  10172. return addr == work->gtt_offset;
  10173. }
  10174. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  10175. {
  10176. struct drm_device *dev = &dev_priv->drm;
  10177. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  10178. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10179. struct intel_flip_work *work;
  10180. WARN_ON(!in_interrupt());
  10181. if (crtc == NULL)
  10182. return;
  10183. spin_lock(&dev->event_lock);
  10184. work = intel_crtc->flip_work;
  10185. if (work != NULL && !is_mmio_work(work) &&
  10186. __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
  10187. WARN_ONCE(1,
  10188. "Kicking stuck page flip: queued at %d, now %d\n",
  10189. work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
  10190. page_flip_completed(intel_crtc);
  10191. work = NULL;
  10192. }
  10193. if (work != NULL && !is_mmio_work(work) &&
  10194. intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
  10195. intel_queue_rps_boost_for_request(work->flip_queued_req);
  10196. spin_unlock(&dev->event_lock);
  10197. }
  10198. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  10199. struct drm_framebuffer *fb,
  10200. struct drm_pending_vblank_event *event,
  10201. uint32_t page_flip_flags)
  10202. {
  10203. struct drm_device *dev = crtc->dev;
  10204. struct drm_i915_private *dev_priv = to_i915(dev);
  10205. struct drm_framebuffer *old_fb = crtc->primary->fb;
  10206. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10207. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10208. struct drm_plane *primary = crtc->primary;
  10209. enum pipe pipe = intel_crtc->pipe;
  10210. struct intel_flip_work *work;
  10211. struct intel_engine_cs *engine;
  10212. bool mmio_flip;
  10213. struct drm_i915_gem_request *request;
  10214. struct i915_vma *vma;
  10215. int ret;
  10216. /*
  10217. * drm_mode_page_flip_ioctl() should already catch this, but double
  10218. * check to be safe. In the future we may enable pageflipping from
  10219. * a disabled primary plane.
  10220. */
  10221. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  10222. return -EBUSY;
  10223. /* Can't change pixel format via MI display flips. */
  10224. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  10225. return -EINVAL;
  10226. /*
  10227. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  10228. * Note that pitch changes could also affect these register.
  10229. */
  10230. if (INTEL_INFO(dev)->gen > 3 &&
  10231. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  10232. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  10233. return -EINVAL;
  10234. if (i915_terminally_wedged(&dev_priv->gpu_error))
  10235. goto out_hang;
  10236. work = kzalloc(sizeof(*work), GFP_KERNEL);
  10237. if (work == NULL)
  10238. return -ENOMEM;
  10239. work->event = event;
  10240. work->crtc = crtc;
  10241. work->old_fb = old_fb;
  10242. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  10243. ret = drm_crtc_vblank_get(crtc);
  10244. if (ret)
  10245. goto free_work;
  10246. /* We borrow the event spin lock for protecting flip_work */
  10247. spin_lock_irq(&dev->event_lock);
  10248. if (intel_crtc->flip_work) {
  10249. /* Before declaring the flip queue wedged, check if
  10250. * the hardware completed the operation behind our backs.
  10251. */
  10252. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  10253. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  10254. page_flip_completed(intel_crtc);
  10255. } else {
  10256. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  10257. spin_unlock_irq(&dev->event_lock);
  10258. drm_crtc_vblank_put(crtc);
  10259. kfree(work);
  10260. return -EBUSY;
  10261. }
  10262. }
  10263. intel_crtc->flip_work = work;
  10264. spin_unlock_irq(&dev->event_lock);
  10265. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  10266. flush_workqueue(dev_priv->wq);
  10267. /* Reference the objects for the scheduled work. */
  10268. drm_framebuffer_reference(work->old_fb);
  10269. crtc->primary->fb = fb;
  10270. update_state_fb(crtc->primary);
  10271. work->pending_flip_obj = i915_gem_object_get(obj);
  10272. ret = i915_mutex_lock_interruptible(dev);
  10273. if (ret)
  10274. goto cleanup;
  10275. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  10276. if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
  10277. ret = -EIO;
  10278. goto unlock;
  10279. }
  10280. atomic_inc(&intel_crtc->unpin_work_count);
  10281. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  10282. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  10283. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  10284. engine = &dev_priv->engine[BCS];
  10285. if (fb->modifier[0] != old_fb->modifier[0])
  10286. /* vlv: DISPLAY_FLIP fails to change tiling */
  10287. engine = NULL;
  10288. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  10289. engine = &dev_priv->engine[BCS];
  10290. } else if (INTEL_INFO(dev)->gen >= 7) {
  10291. engine = i915_gem_active_get_engine(&obj->last_write,
  10292. &obj->base.dev->struct_mutex);
  10293. if (engine == NULL || engine->id != RCS)
  10294. engine = &dev_priv->engine[BCS];
  10295. } else {
  10296. engine = &dev_priv->engine[RCS];
  10297. }
  10298. mmio_flip = use_mmio_flip(engine, obj);
  10299. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  10300. if (IS_ERR(vma)) {
  10301. ret = PTR_ERR(vma);
  10302. goto cleanup_pending;
  10303. }
  10304. work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
  10305. work->gtt_offset += intel_crtc->dspaddr_offset;
  10306. work->rotation = crtc->primary->state->rotation;
  10307. /*
  10308. * There's the potential that the next frame will not be compatible with
  10309. * FBC, so we want to call pre_update() before the actual page flip.
  10310. * The problem is that pre_update() caches some information about the fb
  10311. * object, so we want to do this only after the object is pinned. Let's
  10312. * be on the safe side and do this immediately before scheduling the
  10313. * flip.
  10314. */
  10315. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  10316. to_intel_plane_state(primary->state));
  10317. if (mmio_flip) {
  10318. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  10319. work->flip_queued_req = i915_gem_active_get(&obj->last_write,
  10320. &obj->base.dev->struct_mutex);
  10321. schedule_work(&work->mmio_work);
  10322. } else {
  10323. request = i915_gem_request_alloc(engine, engine->last_context);
  10324. if (IS_ERR(request)) {
  10325. ret = PTR_ERR(request);
  10326. goto cleanup_unpin;
  10327. }
  10328. ret = i915_gem_request_await_object(request, obj, false);
  10329. if (ret)
  10330. goto cleanup_request;
  10331. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  10332. page_flip_flags);
  10333. if (ret)
  10334. goto cleanup_request;
  10335. intel_mark_page_flip_active(intel_crtc, work);
  10336. work->flip_queued_req = i915_gem_request_get(request);
  10337. i915_add_request_no_flush(request);
  10338. }
  10339. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  10340. to_intel_plane(primary)->frontbuffer_bit);
  10341. mutex_unlock(&dev->struct_mutex);
  10342. intel_frontbuffer_flip_prepare(to_i915(dev),
  10343. to_intel_plane(primary)->frontbuffer_bit);
  10344. trace_i915_flip_request(intel_crtc->plane, obj);
  10345. return 0;
  10346. cleanup_request:
  10347. i915_add_request_no_flush(request);
  10348. cleanup_unpin:
  10349. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  10350. cleanup_pending:
  10351. atomic_dec(&intel_crtc->unpin_work_count);
  10352. unlock:
  10353. mutex_unlock(&dev->struct_mutex);
  10354. cleanup:
  10355. crtc->primary->fb = old_fb;
  10356. update_state_fb(crtc->primary);
  10357. i915_gem_object_put_unlocked(obj);
  10358. drm_framebuffer_unreference(work->old_fb);
  10359. spin_lock_irq(&dev->event_lock);
  10360. intel_crtc->flip_work = NULL;
  10361. spin_unlock_irq(&dev->event_lock);
  10362. drm_crtc_vblank_put(crtc);
  10363. free_work:
  10364. kfree(work);
  10365. if (ret == -EIO) {
  10366. struct drm_atomic_state *state;
  10367. struct drm_plane_state *plane_state;
  10368. out_hang:
  10369. state = drm_atomic_state_alloc(dev);
  10370. if (!state)
  10371. return -ENOMEM;
  10372. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  10373. retry:
  10374. plane_state = drm_atomic_get_plane_state(state, primary);
  10375. ret = PTR_ERR_OR_ZERO(plane_state);
  10376. if (!ret) {
  10377. drm_atomic_set_fb_for_plane(plane_state, fb);
  10378. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  10379. if (!ret)
  10380. ret = drm_atomic_commit(state);
  10381. }
  10382. if (ret == -EDEADLK) {
  10383. drm_modeset_backoff(state->acquire_ctx);
  10384. drm_atomic_state_clear(state);
  10385. goto retry;
  10386. }
  10387. if (ret)
  10388. drm_atomic_state_free(state);
  10389. if (ret == 0 && event) {
  10390. spin_lock_irq(&dev->event_lock);
  10391. drm_crtc_send_vblank_event(crtc, event);
  10392. spin_unlock_irq(&dev->event_lock);
  10393. }
  10394. }
  10395. return ret;
  10396. }
  10397. /**
  10398. * intel_wm_need_update - Check whether watermarks need updating
  10399. * @plane: drm plane
  10400. * @state: new plane state
  10401. *
  10402. * Check current plane state versus the new one to determine whether
  10403. * watermarks need to be recalculated.
  10404. *
  10405. * Returns true or false.
  10406. */
  10407. static bool intel_wm_need_update(struct drm_plane *plane,
  10408. struct drm_plane_state *state)
  10409. {
  10410. struct intel_plane_state *new = to_intel_plane_state(state);
  10411. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  10412. /* Update watermarks on tiling or size changes. */
  10413. if (new->base.visible != cur->base.visible)
  10414. return true;
  10415. if (!cur->base.fb || !new->base.fb)
  10416. return false;
  10417. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  10418. cur->base.rotation != new->base.rotation ||
  10419. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  10420. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  10421. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  10422. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  10423. return true;
  10424. return false;
  10425. }
  10426. static bool needs_scaling(struct intel_plane_state *state)
  10427. {
  10428. int src_w = drm_rect_width(&state->base.src) >> 16;
  10429. int src_h = drm_rect_height(&state->base.src) >> 16;
  10430. int dst_w = drm_rect_width(&state->base.dst);
  10431. int dst_h = drm_rect_height(&state->base.dst);
  10432. return (src_w != dst_w || src_h != dst_h);
  10433. }
  10434. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  10435. struct drm_plane_state *plane_state)
  10436. {
  10437. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  10438. struct drm_crtc *crtc = crtc_state->crtc;
  10439. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10440. struct drm_plane *plane = plane_state->plane;
  10441. struct drm_device *dev = crtc->dev;
  10442. struct drm_i915_private *dev_priv = to_i915(dev);
  10443. struct intel_plane_state *old_plane_state =
  10444. to_intel_plane_state(plane->state);
  10445. bool mode_changed = needs_modeset(crtc_state);
  10446. bool was_crtc_enabled = crtc->state->active;
  10447. bool is_crtc_enabled = crtc_state->active;
  10448. bool turn_off, turn_on, visible, was_visible;
  10449. struct drm_framebuffer *fb = plane_state->fb;
  10450. int ret;
  10451. if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  10452. ret = skl_update_scaler_plane(
  10453. to_intel_crtc_state(crtc_state),
  10454. to_intel_plane_state(plane_state));
  10455. if (ret)
  10456. return ret;
  10457. }
  10458. was_visible = old_plane_state->base.visible;
  10459. visible = to_intel_plane_state(plane_state)->base.visible;
  10460. if (!was_crtc_enabled && WARN_ON(was_visible))
  10461. was_visible = false;
  10462. /*
  10463. * Visibility is calculated as if the crtc was on, but
  10464. * after scaler setup everything depends on it being off
  10465. * when the crtc isn't active.
  10466. *
  10467. * FIXME this is wrong for watermarks. Watermarks should also
  10468. * be computed as if the pipe would be active. Perhaps move
  10469. * per-plane wm computation to the .check_plane() hook, and
  10470. * only combine the results from all planes in the current place?
  10471. */
  10472. if (!is_crtc_enabled)
  10473. to_intel_plane_state(plane_state)->base.visible = visible = false;
  10474. if (!was_visible && !visible)
  10475. return 0;
  10476. if (fb != old_plane_state->base.fb)
  10477. pipe_config->fb_changed = true;
  10478. turn_off = was_visible && (!visible || mode_changed);
  10479. turn_on = visible && (!was_visible || mode_changed);
  10480. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  10481. intel_crtc->base.base.id,
  10482. intel_crtc->base.name,
  10483. plane->base.id, plane->name,
  10484. fb ? fb->base.id : -1);
  10485. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  10486. plane->base.id, plane->name,
  10487. was_visible, visible,
  10488. turn_off, turn_on, mode_changed);
  10489. if (turn_on) {
  10490. pipe_config->update_wm_pre = true;
  10491. /* must disable cxsr around plane enable/disable */
  10492. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10493. pipe_config->disable_cxsr = true;
  10494. } else if (turn_off) {
  10495. pipe_config->update_wm_post = true;
  10496. /* must disable cxsr around plane enable/disable */
  10497. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10498. pipe_config->disable_cxsr = true;
  10499. } else if (intel_wm_need_update(plane, plane_state)) {
  10500. /* FIXME bollocks */
  10501. pipe_config->update_wm_pre = true;
  10502. pipe_config->update_wm_post = true;
  10503. }
  10504. /* Pre-gen9 platforms need two-step watermark updates */
  10505. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10506. INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
  10507. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10508. if (visible || was_visible)
  10509. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10510. /*
  10511. * WaCxSRDisabledForSpriteScaling:ivb
  10512. *
  10513. * cstate->update_wm was already set above, so this flag will
  10514. * take effect when we commit and program watermarks.
  10515. */
  10516. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
  10517. needs_scaling(to_intel_plane_state(plane_state)) &&
  10518. !needs_scaling(old_plane_state))
  10519. pipe_config->disable_lp_wm = true;
  10520. return 0;
  10521. }
  10522. static bool encoders_cloneable(const struct intel_encoder *a,
  10523. const struct intel_encoder *b)
  10524. {
  10525. /* masks could be asymmetric, so check both ways */
  10526. return a == b || (a->cloneable & (1 << b->type) &&
  10527. b->cloneable & (1 << a->type));
  10528. }
  10529. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10530. struct intel_crtc *crtc,
  10531. struct intel_encoder *encoder)
  10532. {
  10533. struct intel_encoder *source_encoder;
  10534. struct drm_connector *connector;
  10535. struct drm_connector_state *connector_state;
  10536. int i;
  10537. for_each_connector_in_state(state, connector, connector_state, i) {
  10538. if (connector_state->crtc != &crtc->base)
  10539. continue;
  10540. source_encoder =
  10541. to_intel_encoder(connector_state->best_encoder);
  10542. if (!encoders_cloneable(encoder, source_encoder))
  10543. return false;
  10544. }
  10545. return true;
  10546. }
  10547. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10548. struct drm_crtc_state *crtc_state)
  10549. {
  10550. struct drm_device *dev = crtc->dev;
  10551. struct drm_i915_private *dev_priv = to_i915(dev);
  10552. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10553. struct intel_crtc_state *pipe_config =
  10554. to_intel_crtc_state(crtc_state);
  10555. struct drm_atomic_state *state = crtc_state->state;
  10556. int ret;
  10557. bool mode_changed = needs_modeset(crtc_state);
  10558. if (mode_changed && !crtc_state->active)
  10559. pipe_config->update_wm_post = true;
  10560. if (mode_changed && crtc_state->enable &&
  10561. dev_priv->display.crtc_compute_clock &&
  10562. !WARN_ON(pipe_config->shared_dpll)) {
  10563. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10564. pipe_config);
  10565. if (ret)
  10566. return ret;
  10567. }
  10568. if (crtc_state->color_mgmt_changed) {
  10569. ret = intel_color_check(crtc, crtc_state);
  10570. if (ret)
  10571. return ret;
  10572. /*
  10573. * Changing color management on Intel hardware is
  10574. * handled as part of planes update.
  10575. */
  10576. crtc_state->planes_changed = true;
  10577. }
  10578. ret = 0;
  10579. if (dev_priv->display.compute_pipe_wm) {
  10580. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10581. if (ret) {
  10582. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10583. return ret;
  10584. }
  10585. }
  10586. if (dev_priv->display.compute_intermediate_wm &&
  10587. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10588. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10589. return 0;
  10590. /*
  10591. * Calculate 'intermediate' watermarks that satisfy both the
  10592. * old state and the new state. We can program these
  10593. * immediately.
  10594. */
  10595. ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
  10596. intel_crtc,
  10597. pipe_config);
  10598. if (ret) {
  10599. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10600. return ret;
  10601. }
  10602. } else if (dev_priv->display.compute_intermediate_wm) {
  10603. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10604. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10605. }
  10606. if (INTEL_INFO(dev)->gen >= 9) {
  10607. if (mode_changed)
  10608. ret = skl_update_scaler_crtc(pipe_config);
  10609. if (!ret)
  10610. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10611. pipe_config);
  10612. }
  10613. return ret;
  10614. }
  10615. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10616. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10617. .atomic_begin = intel_begin_crtc_commit,
  10618. .atomic_flush = intel_finish_crtc_commit,
  10619. .atomic_check = intel_crtc_atomic_check,
  10620. };
  10621. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10622. {
  10623. struct intel_connector *connector;
  10624. for_each_intel_connector(dev, connector) {
  10625. if (connector->base.state->crtc)
  10626. drm_connector_unreference(&connector->base);
  10627. if (connector->base.encoder) {
  10628. connector->base.state->best_encoder =
  10629. connector->base.encoder;
  10630. connector->base.state->crtc =
  10631. connector->base.encoder->crtc;
  10632. drm_connector_reference(&connector->base);
  10633. } else {
  10634. connector->base.state->best_encoder = NULL;
  10635. connector->base.state->crtc = NULL;
  10636. }
  10637. }
  10638. }
  10639. static void
  10640. connected_sink_compute_bpp(struct intel_connector *connector,
  10641. struct intel_crtc_state *pipe_config)
  10642. {
  10643. const struct drm_display_info *info = &connector->base.display_info;
  10644. int bpp = pipe_config->pipe_bpp;
  10645. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10646. connector->base.base.id,
  10647. connector->base.name);
  10648. /* Don't use an invalid EDID bpc value */
  10649. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  10650. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10651. bpp, info->bpc * 3);
  10652. pipe_config->pipe_bpp = info->bpc * 3;
  10653. }
  10654. /* Clamp bpp to 8 on screens without EDID 1.4 */
  10655. if (info->bpc == 0 && bpp > 24) {
  10656. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  10657. bpp);
  10658. pipe_config->pipe_bpp = 24;
  10659. }
  10660. }
  10661. static int
  10662. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10663. struct intel_crtc_state *pipe_config)
  10664. {
  10665. struct drm_device *dev = crtc->base.dev;
  10666. struct drm_atomic_state *state;
  10667. struct drm_connector *connector;
  10668. struct drm_connector_state *connector_state;
  10669. int bpp, i;
  10670. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  10671. bpp = 10*3;
  10672. else if (INTEL_INFO(dev)->gen >= 5)
  10673. bpp = 12*3;
  10674. else
  10675. bpp = 8*3;
  10676. pipe_config->pipe_bpp = bpp;
  10677. state = pipe_config->base.state;
  10678. /* Clamp display bpp to EDID value */
  10679. for_each_connector_in_state(state, connector, connector_state, i) {
  10680. if (connector_state->crtc != &crtc->base)
  10681. continue;
  10682. connected_sink_compute_bpp(to_intel_connector(connector),
  10683. pipe_config);
  10684. }
  10685. return bpp;
  10686. }
  10687. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10688. {
  10689. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10690. "type: 0x%x flags: 0x%x\n",
  10691. mode->crtc_clock,
  10692. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10693. mode->crtc_hsync_end, mode->crtc_htotal,
  10694. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10695. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10696. }
  10697. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10698. struct intel_crtc_state *pipe_config,
  10699. const char *context)
  10700. {
  10701. struct drm_device *dev = crtc->base.dev;
  10702. struct drm_plane *plane;
  10703. struct intel_plane *intel_plane;
  10704. struct intel_plane_state *state;
  10705. struct drm_framebuffer *fb;
  10706. DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
  10707. crtc->base.base.id, crtc->base.name,
  10708. context, pipe_config, pipe_name(crtc->pipe));
  10709. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  10710. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10711. pipe_config->pipe_bpp, pipe_config->dither);
  10712. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10713. pipe_config->has_pch_encoder,
  10714. pipe_config->fdi_lanes,
  10715. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10716. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10717. pipe_config->fdi_m_n.tu);
  10718. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10719. intel_crtc_has_dp_encoder(pipe_config),
  10720. pipe_config->lane_count,
  10721. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10722. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10723. pipe_config->dp_m_n.tu);
  10724. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10725. intel_crtc_has_dp_encoder(pipe_config),
  10726. pipe_config->lane_count,
  10727. pipe_config->dp_m2_n2.gmch_m,
  10728. pipe_config->dp_m2_n2.gmch_n,
  10729. pipe_config->dp_m2_n2.link_m,
  10730. pipe_config->dp_m2_n2.link_n,
  10731. pipe_config->dp_m2_n2.tu);
  10732. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10733. pipe_config->has_audio,
  10734. pipe_config->has_infoframe);
  10735. DRM_DEBUG_KMS("requested mode:\n");
  10736. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10737. DRM_DEBUG_KMS("adjusted mode:\n");
  10738. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10739. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10740. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10741. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10742. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10743. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10744. crtc->num_scalers,
  10745. pipe_config->scaler_state.scaler_users,
  10746. pipe_config->scaler_state.scaler_id);
  10747. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10748. pipe_config->gmch_pfit.control,
  10749. pipe_config->gmch_pfit.pgm_ratios,
  10750. pipe_config->gmch_pfit.lvds_border_bits);
  10751. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10752. pipe_config->pch_pfit.pos,
  10753. pipe_config->pch_pfit.size,
  10754. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10755. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10756. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10757. if (IS_BROXTON(dev)) {
  10758. DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10759. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10760. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10761. pipe_config->dpll_hw_state.ebb0,
  10762. pipe_config->dpll_hw_state.ebb4,
  10763. pipe_config->dpll_hw_state.pll0,
  10764. pipe_config->dpll_hw_state.pll1,
  10765. pipe_config->dpll_hw_state.pll2,
  10766. pipe_config->dpll_hw_state.pll3,
  10767. pipe_config->dpll_hw_state.pll6,
  10768. pipe_config->dpll_hw_state.pll8,
  10769. pipe_config->dpll_hw_state.pll9,
  10770. pipe_config->dpll_hw_state.pll10,
  10771. pipe_config->dpll_hw_state.pcsdw12);
  10772. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10773. DRM_DEBUG_KMS("dpll_hw_state: "
  10774. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10775. pipe_config->dpll_hw_state.ctrl1,
  10776. pipe_config->dpll_hw_state.cfgcr1,
  10777. pipe_config->dpll_hw_state.cfgcr2);
  10778. } else if (HAS_DDI(dev)) {
  10779. DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10780. pipe_config->dpll_hw_state.wrpll,
  10781. pipe_config->dpll_hw_state.spll);
  10782. } else {
  10783. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10784. "fp0: 0x%x, fp1: 0x%x\n",
  10785. pipe_config->dpll_hw_state.dpll,
  10786. pipe_config->dpll_hw_state.dpll_md,
  10787. pipe_config->dpll_hw_state.fp0,
  10788. pipe_config->dpll_hw_state.fp1);
  10789. }
  10790. DRM_DEBUG_KMS("planes on this crtc\n");
  10791. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10792. char *format_name;
  10793. intel_plane = to_intel_plane(plane);
  10794. if (intel_plane->pipe != crtc->pipe)
  10795. continue;
  10796. state = to_intel_plane_state(plane->state);
  10797. fb = state->base.fb;
  10798. if (!fb) {
  10799. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10800. plane->base.id, plane->name, state->scaler_id);
  10801. continue;
  10802. }
  10803. format_name = drm_get_format_name(fb->pixel_format);
  10804. DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
  10805. plane->base.id, plane->name);
  10806. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
  10807. fb->base.id, fb->width, fb->height, format_name);
  10808. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10809. state->scaler_id,
  10810. state->base.src.x1 >> 16,
  10811. state->base.src.y1 >> 16,
  10812. drm_rect_width(&state->base.src) >> 16,
  10813. drm_rect_height(&state->base.src) >> 16,
  10814. state->base.dst.x1, state->base.dst.y1,
  10815. drm_rect_width(&state->base.dst),
  10816. drm_rect_height(&state->base.dst));
  10817. kfree(format_name);
  10818. }
  10819. }
  10820. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10821. {
  10822. struct drm_device *dev = state->dev;
  10823. struct drm_connector *connector;
  10824. unsigned int used_ports = 0;
  10825. unsigned int used_mst_ports = 0;
  10826. /*
  10827. * Walk the connector list instead of the encoder
  10828. * list to detect the problem on ddi platforms
  10829. * where there's just one encoder per digital port.
  10830. */
  10831. drm_for_each_connector(connector, dev) {
  10832. struct drm_connector_state *connector_state;
  10833. struct intel_encoder *encoder;
  10834. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10835. if (!connector_state)
  10836. connector_state = connector->state;
  10837. if (!connector_state->best_encoder)
  10838. continue;
  10839. encoder = to_intel_encoder(connector_state->best_encoder);
  10840. WARN_ON(!connector_state->crtc);
  10841. switch (encoder->type) {
  10842. unsigned int port_mask;
  10843. case INTEL_OUTPUT_UNKNOWN:
  10844. if (WARN_ON(!HAS_DDI(dev)))
  10845. break;
  10846. case INTEL_OUTPUT_DP:
  10847. case INTEL_OUTPUT_HDMI:
  10848. case INTEL_OUTPUT_EDP:
  10849. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10850. /* the same port mustn't appear more than once */
  10851. if (used_ports & port_mask)
  10852. return false;
  10853. used_ports |= port_mask;
  10854. break;
  10855. case INTEL_OUTPUT_DP_MST:
  10856. used_mst_ports |=
  10857. 1 << enc_to_mst(&encoder->base)->primary->port;
  10858. break;
  10859. default:
  10860. break;
  10861. }
  10862. }
  10863. /* can't mix MST and SST/HDMI on the same port */
  10864. if (used_ports & used_mst_ports)
  10865. return false;
  10866. return true;
  10867. }
  10868. static void
  10869. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10870. {
  10871. struct drm_crtc_state tmp_state;
  10872. struct intel_crtc_scaler_state scaler_state;
  10873. struct intel_dpll_hw_state dpll_hw_state;
  10874. struct intel_shared_dpll *shared_dpll;
  10875. bool force_thru;
  10876. /* FIXME: before the switch to atomic started, a new pipe_config was
  10877. * kzalloc'd. Code that depends on any field being zero should be
  10878. * fixed, so that the crtc_state can be safely duplicated. For now,
  10879. * only fields that are know to not cause problems are preserved. */
  10880. tmp_state = crtc_state->base;
  10881. scaler_state = crtc_state->scaler_state;
  10882. shared_dpll = crtc_state->shared_dpll;
  10883. dpll_hw_state = crtc_state->dpll_hw_state;
  10884. force_thru = crtc_state->pch_pfit.force_thru;
  10885. memset(crtc_state, 0, sizeof *crtc_state);
  10886. crtc_state->base = tmp_state;
  10887. crtc_state->scaler_state = scaler_state;
  10888. crtc_state->shared_dpll = shared_dpll;
  10889. crtc_state->dpll_hw_state = dpll_hw_state;
  10890. crtc_state->pch_pfit.force_thru = force_thru;
  10891. }
  10892. static int
  10893. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10894. struct intel_crtc_state *pipe_config)
  10895. {
  10896. struct drm_atomic_state *state = pipe_config->base.state;
  10897. struct intel_encoder *encoder;
  10898. struct drm_connector *connector;
  10899. struct drm_connector_state *connector_state;
  10900. int base_bpp, ret = -EINVAL;
  10901. int i;
  10902. bool retry = true;
  10903. clear_intel_crtc_state(pipe_config);
  10904. pipe_config->cpu_transcoder =
  10905. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10906. /*
  10907. * Sanitize sync polarity flags based on requested ones. If neither
  10908. * positive or negative polarity is requested, treat this as meaning
  10909. * negative polarity.
  10910. */
  10911. if (!(pipe_config->base.adjusted_mode.flags &
  10912. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10913. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10914. if (!(pipe_config->base.adjusted_mode.flags &
  10915. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10916. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10917. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10918. pipe_config);
  10919. if (base_bpp < 0)
  10920. goto fail;
  10921. /*
  10922. * Determine the real pipe dimensions. Note that stereo modes can
  10923. * increase the actual pipe size due to the frame doubling and
  10924. * insertion of additional space for blanks between the frame. This
  10925. * is stored in the crtc timings. We use the requested mode to do this
  10926. * computation to clearly distinguish it from the adjusted mode, which
  10927. * can be changed by the connectors in the below retry loop.
  10928. */
  10929. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10930. &pipe_config->pipe_src_w,
  10931. &pipe_config->pipe_src_h);
  10932. for_each_connector_in_state(state, connector, connector_state, i) {
  10933. if (connector_state->crtc != crtc)
  10934. continue;
  10935. encoder = to_intel_encoder(connector_state->best_encoder);
  10936. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  10937. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10938. goto fail;
  10939. }
  10940. /*
  10941. * Determine output_types before calling the .compute_config()
  10942. * hooks so that the hooks can use this information safely.
  10943. */
  10944. pipe_config->output_types |= 1 << encoder->type;
  10945. }
  10946. encoder_retry:
  10947. /* Ensure the port clock defaults are reset when retrying. */
  10948. pipe_config->port_clock = 0;
  10949. pipe_config->pixel_multiplier = 1;
  10950. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10951. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10952. CRTC_STEREO_DOUBLE);
  10953. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10954. * adjust it according to limitations or connector properties, and also
  10955. * a chance to reject the mode entirely.
  10956. */
  10957. for_each_connector_in_state(state, connector, connector_state, i) {
  10958. if (connector_state->crtc != crtc)
  10959. continue;
  10960. encoder = to_intel_encoder(connector_state->best_encoder);
  10961. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  10962. DRM_DEBUG_KMS("Encoder config failure\n");
  10963. goto fail;
  10964. }
  10965. }
  10966. /* Set default port clock if not overwritten by the encoder. Needs to be
  10967. * done afterwards in case the encoder adjusts the mode. */
  10968. if (!pipe_config->port_clock)
  10969. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10970. * pipe_config->pixel_multiplier;
  10971. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10972. if (ret < 0) {
  10973. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10974. goto fail;
  10975. }
  10976. if (ret == RETRY) {
  10977. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10978. ret = -EINVAL;
  10979. goto fail;
  10980. }
  10981. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10982. retry = false;
  10983. goto encoder_retry;
  10984. }
  10985. /* Dithering seems to not pass-through bits correctly when it should, so
  10986. * only enable it on 6bpc panels. */
  10987. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10988. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10989. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10990. fail:
  10991. return ret;
  10992. }
  10993. static void
  10994. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10995. {
  10996. struct drm_crtc *crtc;
  10997. struct drm_crtc_state *crtc_state;
  10998. int i;
  10999. /* Double check state. */
  11000. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11001. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  11002. /* Update hwmode for vblank functions */
  11003. if (crtc->state->active)
  11004. crtc->hwmode = crtc->state->adjusted_mode;
  11005. else
  11006. crtc->hwmode.crtc_clock = 0;
  11007. /*
  11008. * Update legacy state to satisfy fbc code. This can
  11009. * be removed when fbc uses the atomic state.
  11010. */
  11011. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  11012. struct drm_plane_state *plane_state = crtc->primary->state;
  11013. crtc->primary->fb = plane_state->fb;
  11014. crtc->x = plane_state->src_x >> 16;
  11015. crtc->y = plane_state->src_y >> 16;
  11016. }
  11017. }
  11018. }
  11019. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  11020. {
  11021. int diff;
  11022. if (clock1 == clock2)
  11023. return true;
  11024. if (!clock1 || !clock2)
  11025. return false;
  11026. diff = abs(clock1 - clock2);
  11027. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  11028. return true;
  11029. return false;
  11030. }
  11031. static bool
  11032. intel_compare_m_n(unsigned int m, unsigned int n,
  11033. unsigned int m2, unsigned int n2,
  11034. bool exact)
  11035. {
  11036. if (m == m2 && n == n2)
  11037. return true;
  11038. if (exact || !m || !n || !m2 || !n2)
  11039. return false;
  11040. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  11041. if (n > n2) {
  11042. while (n > n2) {
  11043. m2 <<= 1;
  11044. n2 <<= 1;
  11045. }
  11046. } else if (n < n2) {
  11047. while (n < n2) {
  11048. m <<= 1;
  11049. n <<= 1;
  11050. }
  11051. }
  11052. if (n != n2)
  11053. return false;
  11054. return intel_fuzzy_clock_check(m, m2);
  11055. }
  11056. static bool
  11057. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  11058. struct intel_link_m_n *m2_n2,
  11059. bool adjust)
  11060. {
  11061. if (m_n->tu == m2_n2->tu &&
  11062. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  11063. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  11064. intel_compare_m_n(m_n->link_m, m_n->link_n,
  11065. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  11066. if (adjust)
  11067. *m2_n2 = *m_n;
  11068. return true;
  11069. }
  11070. return false;
  11071. }
  11072. static bool
  11073. intel_pipe_config_compare(struct drm_device *dev,
  11074. struct intel_crtc_state *current_config,
  11075. struct intel_crtc_state *pipe_config,
  11076. bool adjust)
  11077. {
  11078. bool ret = true;
  11079. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  11080. do { \
  11081. if (!adjust) \
  11082. DRM_ERROR(fmt, ##__VA_ARGS__); \
  11083. else \
  11084. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  11085. } while (0)
  11086. #define PIPE_CONF_CHECK_X(name) \
  11087. if (current_config->name != pipe_config->name) { \
  11088. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11089. "(expected 0x%08x, found 0x%08x)\n", \
  11090. current_config->name, \
  11091. pipe_config->name); \
  11092. ret = false; \
  11093. }
  11094. #define PIPE_CONF_CHECK_I(name) \
  11095. if (current_config->name != pipe_config->name) { \
  11096. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11097. "(expected %i, found %i)\n", \
  11098. current_config->name, \
  11099. pipe_config->name); \
  11100. ret = false; \
  11101. }
  11102. #define PIPE_CONF_CHECK_P(name) \
  11103. if (current_config->name != pipe_config->name) { \
  11104. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11105. "(expected %p, found %p)\n", \
  11106. current_config->name, \
  11107. pipe_config->name); \
  11108. ret = false; \
  11109. }
  11110. #define PIPE_CONF_CHECK_M_N(name) \
  11111. if (!intel_compare_link_m_n(&current_config->name, \
  11112. &pipe_config->name,\
  11113. adjust)) { \
  11114. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11115. "(expected tu %i gmch %i/%i link %i/%i, " \
  11116. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11117. current_config->name.tu, \
  11118. current_config->name.gmch_m, \
  11119. current_config->name.gmch_n, \
  11120. current_config->name.link_m, \
  11121. current_config->name.link_n, \
  11122. pipe_config->name.tu, \
  11123. pipe_config->name.gmch_m, \
  11124. pipe_config->name.gmch_n, \
  11125. pipe_config->name.link_m, \
  11126. pipe_config->name.link_n); \
  11127. ret = false; \
  11128. }
  11129. /* This is required for BDW+ where there is only one set of registers for
  11130. * switching between high and low RR.
  11131. * This macro can be used whenever a comparison has to be made between one
  11132. * hw state and multiple sw state variables.
  11133. */
  11134. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  11135. if (!intel_compare_link_m_n(&current_config->name, \
  11136. &pipe_config->name, adjust) && \
  11137. !intel_compare_link_m_n(&current_config->alt_name, \
  11138. &pipe_config->name, adjust)) { \
  11139. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11140. "(expected tu %i gmch %i/%i link %i/%i, " \
  11141. "or tu %i gmch %i/%i link %i/%i, " \
  11142. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11143. current_config->name.tu, \
  11144. current_config->name.gmch_m, \
  11145. current_config->name.gmch_n, \
  11146. current_config->name.link_m, \
  11147. current_config->name.link_n, \
  11148. current_config->alt_name.tu, \
  11149. current_config->alt_name.gmch_m, \
  11150. current_config->alt_name.gmch_n, \
  11151. current_config->alt_name.link_m, \
  11152. current_config->alt_name.link_n, \
  11153. pipe_config->name.tu, \
  11154. pipe_config->name.gmch_m, \
  11155. pipe_config->name.gmch_n, \
  11156. pipe_config->name.link_m, \
  11157. pipe_config->name.link_n); \
  11158. ret = false; \
  11159. }
  11160. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  11161. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  11162. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  11163. "(expected %i, found %i)\n", \
  11164. current_config->name & (mask), \
  11165. pipe_config->name & (mask)); \
  11166. ret = false; \
  11167. }
  11168. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  11169. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  11170. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11171. "(expected %i, found %i)\n", \
  11172. current_config->name, \
  11173. pipe_config->name); \
  11174. ret = false; \
  11175. }
  11176. #define PIPE_CONF_QUIRK(quirk) \
  11177. ((current_config->quirks | pipe_config->quirks) & (quirk))
  11178. PIPE_CONF_CHECK_I(cpu_transcoder);
  11179. PIPE_CONF_CHECK_I(has_pch_encoder);
  11180. PIPE_CONF_CHECK_I(fdi_lanes);
  11181. PIPE_CONF_CHECK_M_N(fdi_m_n);
  11182. PIPE_CONF_CHECK_I(lane_count);
  11183. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  11184. if (INTEL_INFO(dev)->gen < 8) {
  11185. PIPE_CONF_CHECK_M_N(dp_m_n);
  11186. if (current_config->has_drrs)
  11187. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  11188. } else
  11189. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  11190. PIPE_CONF_CHECK_X(output_types);
  11191. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  11192. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  11193. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  11194. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  11195. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  11196. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  11197. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  11198. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  11199. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  11200. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  11201. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  11202. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  11203. PIPE_CONF_CHECK_I(pixel_multiplier);
  11204. PIPE_CONF_CHECK_I(has_hdmi_sink);
  11205. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  11206. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  11207. PIPE_CONF_CHECK_I(limited_color_range);
  11208. PIPE_CONF_CHECK_I(has_infoframe);
  11209. PIPE_CONF_CHECK_I(has_audio);
  11210. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11211. DRM_MODE_FLAG_INTERLACE);
  11212. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  11213. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11214. DRM_MODE_FLAG_PHSYNC);
  11215. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11216. DRM_MODE_FLAG_NHSYNC);
  11217. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11218. DRM_MODE_FLAG_PVSYNC);
  11219. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11220. DRM_MODE_FLAG_NVSYNC);
  11221. }
  11222. PIPE_CONF_CHECK_X(gmch_pfit.control);
  11223. /* pfit ratios are autocomputed by the hw on gen4+ */
  11224. if (INTEL_INFO(dev)->gen < 4)
  11225. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  11226. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  11227. if (!adjust) {
  11228. PIPE_CONF_CHECK_I(pipe_src_w);
  11229. PIPE_CONF_CHECK_I(pipe_src_h);
  11230. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  11231. if (current_config->pch_pfit.enabled) {
  11232. PIPE_CONF_CHECK_X(pch_pfit.pos);
  11233. PIPE_CONF_CHECK_X(pch_pfit.size);
  11234. }
  11235. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  11236. }
  11237. /* BDW+ don't expose a synchronous way to read the state */
  11238. if (IS_HASWELL(dev))
  11239. PIPE_CONF_CHECK_I(ips_enabled);
  11240. PIPE_CONF_CHECK_I(double_wide);
  11241. PIPE_CONF_CHECK_P(shared_dpll);
  11242. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  11243. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  11244. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  11245. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  11246. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  11247. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  11248. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  11249. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  11250. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  11251. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  11252. PIPE_CONF_CHECK_X(dsi_pll.div);
  11253. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  11254. PIPE_CONF_CHECK_I(pipe_bpp);
  11255. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  11256. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  11257. #undef PIPE_CONF_CHECK_X
  11258. #undef PIPE_CONF_CHECK_I
  11259. #undef PIPE_CONF_CHECK_P
  11260. #undef PIPE_CONF_CHECK_FLAGS
  11261. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  11262. #undef PIPE_CONF_QUIRK
  11263. #undef INTEL_ERR_OR_DBG_KMS
  11264. return ret;
  11265. }
  11266. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  11267. const struct intel_crtc_state *pipe_config)
  11268. {
  11269. if (pipe_config->has_pch_encoder) {
  11270. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  11271. &pipe_config->fdi_m_n);
  11272. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  11273. /*
  11274. * FDI already provided one idea for the dotclock.
  11275. * Yell if the encoder disagrees.
  11276. */
  11277. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  11278. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  11279. fdi_dotclock, dotclock);
  11280. }
  11281. }
  11282. static void verify_wm_state(struct drm_crtc *crtc,
  11283. struct drm_crtc_state *new_state)
  11284. {
  11285. struct drm_device *dev = crtc->dev;
  11286. struct drm_i915_private *dev_priv = to_i915(dev);
  11287. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  11288. struct skl_ddb_entry *hw_entry, *sw_entry;
  11289. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11290. const enum pipe pipe = intel_crtc->pipe;
  11291. int plane;
  11292. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  11293. return;
  11294. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  11295. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  11296. /* planes */
  11297. for_each_plane(dev_priv, pipe, plane) {
  11298. hw_entry = &hw_ddb.plane[pipe][plane];
  11299. sw_entry = &sw_ddb->plane[pipe][plane];
  11300. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  11301. continue;
  11302. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  11303. "(expected (%u,%u), found (%u,%u))\n",
  11304. pipe_name(pipe), plane + 1,
  11305. sw_entry->start, sw_entry->end,
  11306. hw_entry->start, hw_entry->end);
  11307. }
  11308. /*
  11309. * cursor
  11310. * If the cursor plane isn't active, we may not have updated it's ddb
  11311. * allocation. In that case since the ddb allocation will be updated
  11312. * once the plane becomes visible, we can skip this check
  11313. */
  11314. if (intel_crtc->cursor_addr) {
  11315. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  11316. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  11317. if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
  11318. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  11319. "(expected (%u,%u), found (%u,%u))\n",
  11320. pipe_name(pipe),
  11321. sw_entry->start, sw_entry->end,
  11322. hw_entry->start, hw_entry->end);
  11323. }
  11324. }
  11325. }
  11326. static void
  11327. verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
  11328. {
  11329. struct drm_connector *connector;
  11330. drm_for_each_connector(connector, dev) {
  11331. struct drm_encoder *encoder = connector->encoder;
  11332. struct drm_connector_state *state = connector->state;
  11333. if (state->crtc != crtc)
  11334. continue;
  11335. intel_connector_verify_state(to_intel_connector(connector));
  11336. I915_STATE_WARN(state->best_encoder != encoder,
  11337. "connector's atomic encoder doesn't match legacy encoder\n");
  11338. }
  11339. }
  11340. static void
  11341. verify_encoder_state(struct drm_device *dev)
  11342. {
  11343. struct intel_encoder *encoder;
  11344. struct intel_connector *connector;
  11345. for_each_intel_encoder(dev, encoder) {
  11346. bool enabled = false;
  11347. enum pipe pipe;
  11348. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  11349. encoder->base.base.id,
  11350. encoder->base.name);
  11351. for_each_intel_connector(dev, connector) {
  11352. if (connector->base.state->best_encoder != &encoder->base)
  11353. continue;
  11354. enabled = true;
  11355. I915_STATE_WARN(connector->base.state->crtc !=
  11356. encoder->base.crtc,
  11357. "connector's crtc doesn't match encoder crtc\n");
  11358. }
  11359. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  11360. "encoder's enabled state mismatch "
  11361. "(expected %i, found %i)\n",
  11362. !!encoder->base.crtc, enabled);
  11363. if (!encoder->base.crtc) {
  11364. bool active;
  11365. active = encoder->get_hw_state(encoder, &pipe);
  11366. I915_STATE_WARN(active,
  11367. "encoder detached but still enabled on pipe %c.\n",
  11368. pipe_name(pipe));
  11369. }
  11370. }
  11371. }
  11372. static void
  11373. verify_crtc_state(struct drm_crtc *crtc,
  11374. struct drm_crtc_state *old_crtc_state,
  11375. struct drm_crtc_state *new_crtc_state)
  11376. {
  11377. struct drm_device *dev = crtc->dev;
  11378. struct drm_i915_private *dev_priv = to_i915(dev);
  11379. struct intel_encoder *encoder;
  11380. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11381. struct intel_crtc_state *pipe_config, *sw_config;
  11382. struct drm_atomic_state *old_state;
  11383. bool active;
  11384. old_state = old_crtc_state->state;
  11385. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  11386. pipe_config = to_intel_crtc_state(old_crtc_state);
  11387. memset(pipe_config, 0, sizeof(*pipe_config));
  11388. pipe_config->base.crtc = crtc;
  11389. pipe_config->base.state = old_state;
  11390. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  11391. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  11392. /* hw state is inconsistent with the pipe quirk */
  11393. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  11394. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  11395. active = new_crtc_state->active;
  11396. I915_STATE_WARN(new_crtc_state->active != active,
  11397. "crtc active state doesn't match with hw state "
  11398. "(expected %i, found %i)\n", new_crtc_state->active, active);
  11399. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  11400. "transitional active state does not match atomic hw state "
  11401. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  11402. for_each_encoder_on_crtc(dev, crtc, encoder) {
  11403. enum pipe pipe;
  11404. active = encoder->get_hw_state(encoder, &pipe);
  11405. I915_STATE_WARN(active != new_crtc_state->active,
  11406. "[ENCODER:%i] active %i with crtc active %i\n",
  11407. encoder->base.base.id, active, new_crtc_state->active);
  11408. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  11409. "Encoder connected to wrong pipe %c\n",
  11410. pipe_name(pipe));
  11411. if (active) {
  11412. pipe_config->output_types |= 1 << encoder->type;
  11413. encoder->get_config(encoder, pipe_config);
  11414. }
  11415. }
  11416. if (!new_crtc_state->active)
  11417. return;
  11418. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  11419. sw_config = to_intel_crtc_state(crtc->state);
  11420. if (!intel_pipe_config_compare(dev, sw_config,
  11421. pipe_config, false)) {
  11422. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  11423. intel_dump_pipe_config(intel_crtc, pipe_config,
  11424. "[hw state]");
  11425. intel_dump_pipe_config(intel_crtc, sw_config,
  11426. "[sw state]");
  11427. }
  11428. }
  11429. static void
  11430. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  11431. struct intel_shared_dpll *pll,
  11432. struct drm_crtc *crtc,
  11433. struct drm_crtc_state *new_state)
  11434. {
  11435. struct intel_dpll_hw_state dpll_hw_state;
  11436. unsigned crtc_mask;
  11437. bool active;
  11438. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  11439. DRM_DEBUG_KMS("%s\n", pll->name);
  11440. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  11441. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  11442. I915_STATE_WARN(!pll->on && pll->active_mask,
  11443. "pll in active use but not on in sw tracking\n");
  11444. I915_STATE_WARN(pll->on && !pll->active_mask,
  11445. "pll is on but not used by any active crtc\n");
  11446. I915_STATE_WARN(pll->on != active,
  11447. "pll on state mismatch (expected %i, found %i)\n",
  11448. pll->on, active);
  11449. }
  11450. if (!crtc) {
  11451. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  11452. "more active pll users than references: %x vs %x\n",
  11453. pll->active_mask, pll->config.crtc_mask);
  11454. return;
  11455. }
  11456. crtc_mask = 1 << drm_crtc_index(crtc);
  11457. if (new_state->active)
  11458. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  11459. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  11460. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11461. else
  11462. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11463. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  11464. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11465. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  11466. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  11467. crtc_mask, pll->config.crtc_mask);
  11468. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  11469. &dpll_hw_state,
  11470. sizeof(dpll_hw_state)),
  11471. "pll hw state mismatch\n");
  11472. }
  11473. static void
  11474. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  11475. struct drm_crtc_state *old_crtc_state,
  11476. struct drm_crtc_state *new_crtc_state)
  11477. {
  11478. struct drm_i915_private *dev_priv = to_i915(dev);
  11479. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  11480. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  11481. if (new_state->shared_dpll)
  11482. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  11483. if (old_state->shared_dpll &&
  11484. old_state->shared_dpll != new_state->shared_dpll) {
  11485. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  11486. struct intel_shared_dpll *pll = old_state->shared_dpll;
  11487. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11488. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  11489. pipe_name(drm_crtc_index(crtc)));
  11490. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  11491. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  11492. pipe_name(drm_crtc_index(crtc)));
  11493. }
  11494. }
  11495. static void
  11496. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11497. struct drm_crtc_state *old_state,
  11498. struct drm_crtc_state *new_state)
  11499. {
  11500. if (!needs_modeset(new_state) &&
  11501. !to_intel_crtc_state(new_state)->update_pipe)
  11502. return;
  11503. verify_wm_state(crtc, new_state);
  11504. verify_connector_state(crtc->dev, crtc);
  11505. verify_crtc_state(crtc, old_state, new_state);
  11506. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11507. }
  11508. static void
  11509. verify_disabled_dpll_state(struct drm_device *dev)
  11510. {
  11511. struct drm_i915_private *dev_priv = to_i915(dev);
  11512. int i;
  11513. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11514. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11515. }
  11516. static void
  11517. intel_modeset_verify_disabled(struct drm_device *dev)
  11518. {
  11519. verify_encoder_state(dev);
  11520. verify_connector_state(dev, NULL);
  11521. verify_disabled_dpll_state(dev);
  11522. }
  11523. static void update_scanline_offset(struct intel_crtc *crtc)
  11524. {
  11525. struct drm_device *dev = crtc->base.dev;
  11526. /*
  11527. * The scanline counter increments at the leading edge of hsync.
  11528. *
  11529. * On most platforms it starts counting from vtotal-1 on the
  11530. * first active line. That means the scanline counter value is
  11531. * always one less than what we would expect. Ie. just after
  11532. * start of vblank, which also occurs at start of hsync (on the
  11533. * last active line), the scanline counter will read vblank_start-1.
  11534. *
  11535. * On gen2 the scanline counter starts counting from 1 instead
  11536. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11537. * to keep the value positive), instead of adding one.
  11538. *
  11539. * On HSW+ the behaviour of the scanline counter depends on the output
  11540. * type. For DP ports it behaves like most other platforms, but on HDMI
  11541. * there's an extra 1 line difference. So we need to add two instead of
  11542. * one to the value.
  11543. *
  11544. * On VLV/CHV DSI the scanline counter would appear to increment
  11545. * approx. 1/3 of a scanline before start of vblank. Unfortunately
  11546. * that means we can't tell whether we're in vblank or not while
  11547. * we're on that particular line. We must still set scanline_offset
  11548. * to 1 so that the vblank timestamps come out correct when we query
  11549. * the scanline counter from within the vblank interrupt handler.
  11550. * However if queried just before the start of vblank we'll get an
  11551. * answer that's slightly in the future.
  11552. */
  11553. if (IS_GEN2(dev)) {
  11554. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11555. int vtotal;
  11556. vtotal = adjusted_mode->crtc_vtotal;
  11557. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11558. vtotal /= 2;
  11559. crtc->scanline_offset = vtotal - 1;
  11560. } else if (HAS_DDI(dev) &&
  11561. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  11562. crtc->scanline_offset = 2;
  11563. } else
  11564. crtc->scanline_offset = 1;
  11565. }
  11566. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11567. {
  11568. struct drm_device *dev = state->dev;
  11569. struct drm_i915_private *dev_priv = to_i915(dev);
  11570. struct intel_shared_dpll_config *shared_dpll = NULL;
  11571. struct drm_crtc *crtc;
  11572. struct drm_crtc_state *crtc_state;
  11573. int i;
  11574. if (!dev_priv->display.crtc_compute_clock)
  11575. return;
  11576. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11577. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11578. struct intel_shared_dpll *old_dpll =
  11579. to_intel_crtc_state(crtc->state)->shared_dpll;
  11580. if (!needs_modeset(crtc_state))
  11581. continue;
  11582. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11583. if (!old_dpll)
  11584. continue;
  11585. if (!shared_dpll)
  11586. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11587. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11588. }
  11589. }
  11590. /*
  11591. * This implements the workaround described in the "notes" section of the mode
  11592. * set sequence documentation. When going from no pipes or single pipe to
  11593. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11594. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11595. */
  11596. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11597. {
  11598. struct drm_crtc_state *crtc_state;
  11599. struct intel_crtc *intel_crtc;
  11600. struct drm_crtc *crtc;
  11601. struct intel_crtc_state *first_crtc_state = NULL;
  11602. struct intel_crtc_state *other_crtc_state = NULL;
  11603. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11604. int i;
  11605. /* look at all crtc's that are going to be enabled in during modeset */
  11606. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11607. intel_crtc = to_intel_crtc(crtc);
  11608. if (!crtc_state->active || !needs_modeset(crtc_state))
  11609. continue;
  11610. if (first_crtc_state) {
  11611. other_crtc_state = to_intel_crtc_state(crtc_state);
  11612. break;
  11613. } else {
  11614. first_crtc_state = to_intel_crtc_state(crtc_state);
  11615. first_pipe = intel_crtc->pipe;
  11616. }
  11617. }
  11618. /* No workaround needed? */
  11619. if (!first_crtc_state)
  11620. return 0;
  11621. /* w/a possibly needed, check how many crtc's are already enabled. */
  11622. for_each_intel_crtc(state->dev, intel_crtc) {
  11623. struct intel_crtc_state *pipe_config;
  11624. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11625. if (IS_ERR(pipe_config))
  11626. return PTR_ERR(pipe_config);
  11627. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11628. if (!pipe_config->base.active ||
  11629. needs_modeset(&pipe_config->base))
  11630. continue;
  11631. /* 2 or more enabled crtcs means no need for w/a */
  11632. if (enabled_pipe != INVALID_PIPE)
  11633. return 0;
  11634. enabled_pipe = intel_crtc->pipe;
  11635. }
  11636. if (enabled_pipe != INVALID_PIPE)
  11637. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11638. else if (other_crtc_state)
  11639. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11640. return 0;
  11641. }
  11642. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11643. {
  11644. struct drm_crtc *crtc;
  11645. struct drm_crtc_state *crtc_state;
  11646. int ret = 0;
  11647. /* add all active pipes to the state */
  11648. for_each_crtc(state->dev, crtc) {
  11649. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11650. if (IS_ERR(crtc_state))
  11651. return PTR_ERR(crtc_state);
  11652. if (!crtc_state->active || needs_modeset(crtc_state))
  11653. continue;
  11654. crtc_state->mode_changed = true;
  11655. ret = drm_atomic_add_affected_connectors(state, crtc);
  11656. if (ret)
  11657. break;
  11658. ret = drm_atomic_add_affected_planes(state, crtc);
  11659. if (ret)
  11660. break;
  11661. }
  11662. return ret;
  11663. }
  11664. static int intel_modeset_checks(struct drm_atomic_state *state)
  11665. {
  11666. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11667. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11668. struct drm_crtc *crtc;
  11669. struct drm_crtc_state *crtc_state;
  11670. int ret = 0, i;
  11671. if (!check_digital_port_conflicts(state)) {
  11672. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11673. return -EINVAL;
  11674. }
  11675. intel_state->modeset = true;
  11676. intel_state->active_crtcs = dev_priv->active_crtcs;
  11677. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11678. if (crtc_state->active)
  11679. intel_state->active_crtcs |= 1 << i;
  11680. else
  11681. intel_state->active_crtcs &= ~(1 << i);
  11682. if (crtc_state->active != crtc->state->active)
  11683. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11684. }
  11685. /*
  11686. * See if the config requires any additional preparation, e.g.
  11687. * to adjust global state with pipes off. We need to do this
  11688. * here so we can get the modeset_pipe updated config for the new
  11689. * mode set on this crtc. For other crtcs we need to use the
  11690. * adjusted_mode bits in the crtc directly.
  11691. */
  11692. if (dev_priv->display.modeset_calc_cdclk) {
  11693. if (!intel_state->cdclk_pll_vco)
  11694. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11695. if (!intel_state->cdclk_pll_vco)
  11696. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11697. ret = dev_priv->display.modeset_calc_cdclk(state);
  11698. if (ret < 0)
  11699. return ret;
  11700. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11701. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
  11702. ret = intel_modeset_all_pipes(state);
  11703. if (ret < 0)
  11704. return ret;
  11705. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11706. intel_state->cdclk, intel_state->dev_cdclk);
  11707. } else {
  11708. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11709. }
  11710. intel_modeset_clear_plls(state);
  11711. if (IS_HASWELL(dev_priv))
  11712. return haswell_mode_set_planes_workaround(state);
  11713. return 0;
  11714. }
  11715. /*
  11716. * Handle calculation of various watermark data at the end of the atomic check
  11717. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11718. * handlers to ensure that all derived state has been updated.
  11719. */
  11720. static int calc_watermark_data(struct drm_atomic_state *state)
  11721. {
  11722. struct drm_device *dev = state->dev;
  11723. struct drm_i915_private *dev_priv = to_i915(dev);
  11724. /* Is there platform-specific watermark information to calculate? */
  11725. if (dev_priv->display.compute_global_watermarks)
  11726. return dev_priv->display.compute_global_watermarks(state);
  11727. return 0;
  11728. }
  11729. /**
  11730. * intel_atomic_check - validate state object
  11731. * @dev: drm device
  11732. * @state: state to validate
  11733. */
  11734. static int intel_atomic_check(struct drm_device *dev,
  11735. struct drm_atomic_state *state)
  11736. {
  11737. struct drm_i915_private *dev_priv = to_i915(dev);
  11738. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11739. struct drm_crtc *crtc;
  11740. struct drm_crtc_state *crtc_state;
  11741. int ret, i;
  11742. bool any_ms = false;
  11743. ret = drm_atomic_helper_check_modeset(dev, state);
  11744. if (ret)
  11745. return ret;
  11746. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11747. struct intel_crtc_state *pipe_config =
  11748. to_intel_crtc_state(crtc_state);
  11749. /* Catch I915_MODE_FLAG_INHERITED */
  11750. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11751. crtc_state->mode_changed = true;
  11752. if (!needs_modeset(crtc_state))
  11753. continue;
  11754. if (!crtc_state->enable) {
  11755. any_ms = true;
  11756. continue;
  11757. }
  11758. /* FIXME: For only active_changed we shouldn't need to do any
  11759. * state recomputation at all. */
  11760. ret = drm_atomic_add_affected_connectors(state, crtc);
  11761. if (ret)
  11762. return ret;
  11763. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11764. if (ret) {
  11765. intel_dump_pipe_config(to_intel_crtc(crtc),
  11766. pipe_config, "[failed]");
  11767. return ret;
  11768. }
  11769. if (i915.fastboot &&
  11770. intel_pipe_config_compare(dev,
  11771. to_intel_crtc_state(crtc->state),
  11772. pipe_config, true)) {
  11773. crtc_state->mode_changed = false;
  11774. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11775. }
  11776. if (needs_modeset(crtc_state))
  11777. any_ms = true;
  11778. ret = drm_atomic_add_affected_planes(state, crtc);
  11779. if (ret)
  11780. return ret;
  11781. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11782. needs_modeset(crtc_state) ?
  11783. "[modeset]" : "[fastset]");
  11784. }
  11785. if (any_ms) {
  11786. ret = intel_modeset_checks(state);
  11787. if (ret)
  11788. return ret;
  11789. } else {
  11790. intel_state->cdclk = dev_priv->atomic_cdclk_freq;
  11791. }
  11792. ret = drm_atomic_helper_check_planes(dev, state);
  11793. if (ret)
  11794. return ret;
  11795. intel_fbc_choose_crtc(dev_priv, state);
  11796. return calc_watermark_data(state);
  11797. }
  11798. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11799. struct drm_atomic_state *state,
  11800. bool nonblock)
  11801. {
  11802. struct drm_i915_private *dev_priv = to_i915(dev);
  11803. struct drm_plane_state *plane_state;
  11804. struct drm_crtc_state *crtc_state;
  11805. struct drm_plane *plane;
  11806. struct drm_crtc *crtc;
  11807. int i, ret;
  11808. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11809. if (state->legacy_cursor_update)
  11810. continue;
  11811. ret = intel_crtc_wait_for_pending_flips(crtc);
  11812. if (ret)
  11813. return ret;
  11814. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11815. flush_workqueue(dev_priv->wq);
  11816. }
  11817. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11818. if (ret)
  11819. return ret;
  11820. ret = drm_atomic_helper_prepare_planes(dev, state);
  11821. mutex_unlock(&dev->struct_mutex);
  11822. if (!ret && !nonblock) {
  11823. for_each_plane_in_state(state, plane, plane_state, i) {
  11824. struct intel_plane_state *intel_plane_state =
  11825. to_intel_plane_state(plane_state);
  11826. if (!intel_plane_state->wait_req)
  11827. continue;
  11828. ret = i915_wait_request(intel_plane_state->wait_req,
  11829. I915_WAIT_INTERRUPTIBLE,
  11830. NULL, NULL);
  11831. if (ret) {
  11832. /* Any hang should be swallowed by the wait */
  11833. WARN_ON(ret == -EIO);
  11834. mutex_lock(&dev->struct_mutex);
  11835. drm_atomic_helper_cleanup_planes(dev, state);
  11836. mutex_unlock(&dev->struct_mutex);
  11837. break;
  11838. }
  11839. }
  11840. }
  11841. return ret;
  11842. }
  11843. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11844. {
  11845. struct drm_device *dev = crtc->base.dev;
  11846. if (!dev->max_vblank_count)
  11847. return drm_accurate_vblank_count(&crtc->base);
  11848. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11849. }
  11850. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11851. struct drm_i915_private *dev_priv,
  11852. unsigned crtc_mask)
  11853. {
  11854. unsigned last_vblank_count[I915_MAX_PIPES];
  11855. enum pipe pipe;
  11856. int ret;
  11857. if (!crtc_mask)
  11858. return;
  11859. for_each_pipe(dev_priv, pipe) {
  11860. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11861. if (!((1 << pipe) & crtc_mask))
  11862. continue;
  11863. ret = drm_crtc_vblank_get(crtc);
  11864. if (WARN_ON(ret != 0)) {
  11865. crtc_mask &= ~(1 << pipe);
  11866. continue;
  11867. }
  11868. last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
  11869. }
  11870. for_each_pipe(dev_priv, pipe) {
  11871. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11872. long lret;
  11873. if (!((1 << pipe) & crtc_mask))
  11874. continue;
  11875. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11876. last_vblank_count[pipe] !=
  11877. drm_crtc_vblank_count(crtc),
  11878. msecs_to_jiffies(50));
  11879. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11880. drm_crtc_vblank_put(crtc);
  11881. }
  11882. }
  11883. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11884. {
  11885. /* fb updated, need to unpin old fb */
  11886. if (crtc_state->fb_changed)
  11887. return true;
  11888. /* wm changes, need vblank before final wm's */
  11889. if (crtc_state->update_wm_post)
  11890. return true;
  11891. /*
  11892. * cxsr is re-enabled after vblank.
  11893. * This is already handled by crtc_state->update_wm_post,
  11894. * but added for clarity.
  11895. */
  11896. if (crtc_state->disable_cxsr)
  11897. return true;
  11898. return false;
  11899. }
  11900. static void intel_update_crtc(struct drm_crtc *crtc,
  11901. struct drm_atomic_state *state,
  11902. struct drm_crtc_state *old_crtc_state,
  11903. unsigned int *crtc_vblank_mask)
  11904. {
  11905. struct drm_device *dev = crtc->dev;
  11906. struct drm_i915_private *dev_priv = to_i915(dev);
  11907. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11908. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
  11909. bool modeset = needs_modeset(crtc->state);
  11910. if (modeset) {
  11911. update_scanline_offset(intel_crtc);
  11912. dev_priv->display.crtc_enable(pipe_config, state);
  11913. } else {
  11914. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11915. }
  11916. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  11917. intel_fbc_enable(
  11918. intel_crtc, pipe_config,
  11919. to_intel_plane_state(crtc->primary->state));
  11920. }
  11921. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11922. if (needs_vblank_wait(pipe_config))
  11923. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  11924. }
  11925. static void intel_update_crtcs(struct drm_atomic_state *state,
  11926. unsigned int *crtc_vblank_mask)
  11927. {
  11928. struct drm_crtc *crtc;
  11929. struct drm_crtc_state *old_crtc_state;
  11930. int i;
  11931. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11932. if (!crtc->state->active)
  11933. continue;
  11934. intel_update_crtc(crtc, state, old_crtc_state,
  11935. crtc_vblank_mask);
  11936. }
  11937. }
  11938. static void skl_update_crtcs(struct drm_atomic_state *state,
  11939. unsigned int *crtc_vblank_mask)
  11940. {
  11941. struct drm_device *dev = state->dev;
  11942. struct drm_i915_private *dev_priv = to_i915(dev);
  11943. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11944. struct drm_crtc *crtc;
  11945. struct drm_crtc_state *old_crtc_state;
  11946. struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
  11947. struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
  11948. unsigned int updated = 0;
  11949. bool progress;
  11950. enum pipe pipe;
  11951. /*
  11952. * Whenever the number of active pipes changes, we need to make sure we
  11953. * update the pipes in the right order so that their ddb allocations
  11954. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  11955. * cause pipe underruns and other bad stuff.
  11956. */
  11957. do {
  11958. int i;
  11959. progress = false;
  11960. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11961. bool vbl_wait = false;
  11962. unsigned int cmask = drm_crtc_mask(crtc);
  11963. pipe = to_intel_crtc(crtc)->pipe;
  11964. if (updated & cmask || !crtc->state->active)
  11965. continue;
  11966. if (skl_ddb_allocation_overlaps(state, cur_ddb, new_ddb,
  11967. pipe))
  11968. continue;
  11969. updated |= cmask;
  11970. /*
  11971. * If this is an already active pipe, it's DDB changed,
  11972. * and this isn't the last pipe that needs updating
  11973. * then we need to wait for a vblank to pass for the
  11974. * new ddb allocation to take effect.
  11975. */
  11976. if (!skl_ddb_allocation_equals(cur_ddb, new_ddb, pipe) &&
  11977. !crtc->state->active_changed &&
  11978. intel_state->wm_results.dirty_pipes != updated)
  11979. vbl_wait = true;
  11980. intel_update_crtc(crtc, state, old_crtc_state,
  11981. crtc_vblank_mask);
  11982. if (vbl_wait)
  11983. intel_wait_for_vblank(dev, pipe);
  11984. progress = true;
  11985. }
  11986. } while (progress);
  11987. }
  11988. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  11989. {
  11990. struct drm_device *dev = state->dev;
  11991. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11992. struct drm_i915_private *dev_priv = to_i915(dev);
  11993. struct drm_crtc_state *old_crtc_state;
  11994. struct drm_crtc *crtc;
  11995. struct intel_crtc_state *intel_cstate;
  11996. struct drm_plane *plane;
  11997. struct drm_plane_state *plane_state;
  11998. bool hw_check = intel_state->modeset;
  11999. unsigned long put_domains[I915_MAX_PIPES] = {};
  12000. unsigned crtc_vblank_mask = 0;
  12001. int i, ret;
  12002. for_each_plane_in_state(state, plane, plane_state, i) {
  12003. struct intel_plane_state *intel_plane_state =
  12004. to_intel_plane_state(plane->state);
  12005. if (!intel_plane_state->wait_req)
  12006. continue;
  12007. ret = i915_wait_request(intel_plane_state->wait_req,
  12008. 0, NULL, NULL);
  12009. /* EIO should be eaten, and we can't get interrupted in the
  12010. * worker, and blocking commits have waited already. */
  12011. WARN_ON(ret);
  12012. }
  12013. drm_atomic_helper_wait_for_dependencies(state);
  12014. if (intel_state->modeset) {
  12015. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  12016. sizeof(intel_state->min_pixclk));
  12017. dev_priv->active_crtcs = intel_state->active_crtcs;
  12018. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  12019. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  12020. }
  12021. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12022. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12023. if (needs_modeset(crtc->state) ||
  12024. to_intel_crtc_state(crtc->state)->update_pipe) {
  12025. hw_check = true;
  12026. put_domains[to_intel_crtc(crtc)->pipe] =
  12027. modeset_get_crtc_power_domains(crtc,
  12028. to_intel_crtc_state(crtc->state));
  12029. }
  12030. if (!needs_modeset(crtc->state))
  12031. continue;
  12032. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  12033. if (old_crtc_state->active) {
  12034. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  12035. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  12036. intel_crtc->active = false;
  12037. intel_fbc_disable(intel_crtc);
  12038. intel_disable_shared_dpll(intel_crtc);
  12039. /*
  12040. * Underruns don't always raise
  12041. * interrupts, so check manually.
  12042. */
  12043. intel_check_cpu_fifo_underruns(dev_priv);
  12044. intel_check_pch_fifo_underruns(dev_priv);
  12045. if (!crtc->state->active)
  12046. intel_update_watermarks(crtc);
  12047. }
  12048. }
  12049. /* Only after disabling all output pipelines that will be changed can we
  12050. * update the the output configuration. */
  12051. intel_modeset_update_crtc_state(state);
  12052. if (intel_state->modeset) {
  12053. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  12054. if (dev_priv->display.modeset_commit_cdclk &&
  12055. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  12056. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  12057. dev_priv->display.modeset_commit_cdclk(state);
  12058. /*
  12059. * SKL workaround: bspec recommends we disable the SAGV when we
  12060. * have more then one pipe enabled
  12061. */
  12062. if (!intel_can_enable_sagv(state))
  12063. intel_disable_sagv(dev_priv);
  12064. intel_modeset_verify_disabled(dev);
  12065. }
  12066. /* Complete the events for pipes that have now been disabled */
  12067. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12068. bool modeset = needs_modeset(crtc->state);
  12069. /* Complete events for now disable pipes here. */
  12070. if (modeset && !crtc->state->active && crtc->state->event) {
  12071. spin_lock_irq(&dev->event_lock);
  12072. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  12073. spin_unlock_irq(&dev->event_lock);
  12074. crtc->state->event = NULL;
  12075. }
  12076. }
  12077. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  12078. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  12079. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  12080. * already, but still need the state for the delayed optimization. To
  12081. * fix this:
  12082. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  12083. * - schedule that vblank worker _before_ calling hw_done
  12084. * - at the start of commit_tail, cancel it _synchrously
  12085. * - switch over to the vblank wait helper in the core after that since
  12086. * we don't need out special handling any more.
  12087. */
  12088. if (!state->legacy_cursor_update)
  12089. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  12090. /*
  12091. * Now that the vblank has passed, we can go ahead and program the
  12092. * optimal watermarks on platforms that need two-step watermark
  12093. * programming.
  12094. *
  12095. * TODO: Move this (and other cleanup) to an async worker eventually.
  12096. */
  12097. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12098. intel_cstate = to_intel_crtc_state(crtc->state);
  12099. if (dev_priv->display.optimize_watermarks)
  12100. dev_priv->display.optimize_watermarks(intel_cstate);
  12101. }
  12102. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12103. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  12104. if (put_domains[i])
  12105. modeset_put_power_domains(dev_priv, put_domains[i]);
  12106. intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
  12107. }
  12108. if (intel_state->modeset && intel_can_enable_sagv(state))
  12109. intel_enable_sagv(dev_priv);
  12110. drm_atomic_helper_commit_hw_done(state);
  12111. if (intel_state->modeset)
  12112. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  12113. mutex_lock(&dev->struct_mutex);
  12114. drm_atomic_helper_cleanup_planes(dev, state);
  12115. mutex_unlock(&dev->struct_mutex);
  12116. drm_atomic_helper_commit_cleanup_done(state);
  12117. drm_atomic_state_free(state);
  12118. /* As one of the primary mmio accessors, KMS has a high likelihood
  12119. * of triggering bugs in unclaimed access. After we finish
  12120. * modesetting, see if an error has been flagged, and if so
  12121. * enable debugging for the next modeset - and hope we catch
  12122. * the culprit.
  12123. *
  12124. * XXX note that we assume display power is on at this point.
  12125. * This might hold true now but we need to add pm helper to check
  12126. * unclaimed only when the hardware is on, as atomic commits
  12127. * can happen also when the device is completely off.
  12128. */
  12129. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  12130. }
  12131. static void intel_atomic_commit_work(struct work_struct *work)
  12132. {
  12133. struct drm_atomic_state *state = container_of(work,
  12134. struct drm_atomic_state,
  12135. commit_work);
  12136. intel_atomic_commit_tail(state);
  12137. }
  12138. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  12139. {
  12140. struct drm_plane_state *old_plane_state;
  12141. struct drm_plane *plane;
  12142. int i;
  12143. for_each_plane_in_state(state, plane, old_plane_state, i)
  12144. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  12145. intel_fb_obj(plane->state->fb),
  12146. to_intel_plane(plane)->frontbuffer_bit);
  12147. }
  12148. /**
  12149. * intel_atomic_commit - commit validated state object
  12150. * @dev: DRM device
  12151. * @state: the top-level driver state object
  12152. * @nonblock: nonblocking commit
  12153. *
  12154. * This function commits a top-level state object that has been validated
  12155. * with drm_atomic_helper_check().
  12156. *
  12157. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  12158. * nonblocking commits are only safe for pure plane updates. Everything else
  12159. * should work though.
  12160. *
  12161. * RETURNS
  12162. * Zero for success or -errno.
  12163. */
  12164. static int intel_atomic_commit(struct drm_device *dev,
  12165. struct drm_atomic_state *state,
  12166. bool nonblock)
  12167. {
  12168. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12169. struct drm_i915_private *dev_priv = to_i915(dev);
  12170. int ret = 0;
  12171. if (intel_state->modeset && nonblock) {
  12172. DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
  12173. return -EINVAL;
  12174. }
  12175. ret = drm_atomic_helper_setup_commit(state, nonblock);
  12176. if (ret)
  12177. return ret;
  12178. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  12179. ret = intel_atomic_prepare_commit(dev, state, nonblock);
  12180. if (ret) {
  12181. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  12182. return ret;
  12183. }
  12184. drm_atomic_helper_swap_state(state, true);
  12185. dev_priv->wm.distrust_bios_wm = false;
  12186. dev_priv->wm.skl_results = intel_state->wm_results;
  12187. intel_shared_dpll_commit(state);
  12188. intel_atomic_track_fbs(state);
  12189. if (nonblock)
  12190. queue_work(system_unbound_wq, &state->commit_work);
  12191. else
  12192. intel_atomic_commit_tail(state);
  12193. return 0;
  12194. }
  12195. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  12196. {
  12197. struct drm_device *dev = crtc->dev;
  12198. struct drm_atomic_state *state;
  12199. struct drm_crtc_state *crtc_state;
  12200. int ret;
  12201. state = drm_atomic_state_alloc(dev);
  12202. if (!state) {
  12203. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  12204. crtc->base.id, crtc->name);
  12205. return;
  12206. }
  12207. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  12208. retry:
  12209. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  12210. ret = PTR_ERR_OR_ZERO(crtc_state);
  12211. if (!ret) {
  12212. if (!crtc_state->active)
  12213. goto out;
  12214. crtc_state->mode_changed = true;
  12215. ret = drm_atomic_commit(state);
  12216. }
  12217. if (ret == -EDEADLK) {
  12218. drm_atomic_state_clear(state);
  12219. drm_modeset_backoff(state->acquire_ctx);
  12220. goto retry;
  12221. }
  12222. if (ret)
  12223. out:
  12224. drm_atomic_state_free(state);
  12225. }
  12226. /*
  12227. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  12228. * drm_atomic_helper_legacy_gamma_set() directly.
  12229. */
  12230. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  12231. u16 *red, u16 *green, u16 *blue,
  12232. uint32_t size)
  12233. {
  12234. struct drm_device *dev = crtc->dev;
  12235. struct drm_mode_config *config = &dev->mode_config;
  12236. struct drm_crtc_state *state;
  12237. int ret;
  12238. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  12239. if (ret)
  12240. return ret;
  12241. /*
  12242. * Make sure we update the legacy properties so this works when
  12243. * atomic is not enabled.
  12244. */
  12245. state = crtc->state;
  12246. drm_object_property_set_value(&crtc->base,
  12247. config->degamma_lut_property,
  12248. (state->degamma_lut) ?
  12249. state->degamma_lut->base.id : 0);
  12250. drm_object_property_set_value(&crtc->base,
  12251. config->ctm_property,
  12252. (state->ctm) ?
  12253. state->ctm->base.id : 0);
  12254. drm_object_property_set_value(&crtc->base,
  12255. config->gamma_lut_property,
  12256. (state->gamma_lut) ?
  12257. state->gamma_lut->base.id : 0);
  12258. return 0;
  12259. }
  12260. static const struct drm_crtc_funcs intel_crtc_funcs = {
  12261. .gamma_set = intel_atomic_legacy_gamma_set,
  12262. .set_config = drm_atomic_helper_set_config,
  12263. .set_property = drm_atomic_helper_crtc_set_property,
  12264. .destroy = intel_crtc_destroy,
  12265. .page_flip = intel_crtc_page_flip,
  12266. .atomic_duplicate_state = intel_crtc_duplicate_state,
  12267. .atomic_destroy_state = intel_crtc_destroy_state,
  12268. };
  12269. /**
  12270. * intel_prepare_plane_fb - Prepare fb for usage on plane
  12271. * @plane: drm plane to prepare for
  12272. * @fb: framebuffer to prepare for presentation
  12273. *
  12274. * Prepares a framebuffer for usage on a display plane. Generally this
  12275. * involves pinning the underlying object and updating the frontbuffer tracking
  12276. * bits. Some older platforms need special physical address handling for
  12277. * cursor planes.
  12278. *
  12279. * Must be called with struct_mutex held.
  12280. *
  12281. * Returns 0 on success, negative error code on failure.
  12282. */
  12283. int
  12284. intel_prepare_plane_fb(struct drm_plane *plane,
  12285. struct drm_plane_state *new_state)
  12286. {
  12287. struct drm_device *dev = plane->dev;
  12288. struct drm_framebuffer *fb = new_state->fb;
  12289. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12290. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  12291. struct reservation_object *resv;
  12292. int ret = 0;
  12293. if (!obj && !old_obj)
  12294. return 0;
  12295. if (old_obj) {
  12296. struct drm_crtc_state *crtc_state =
  12297. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  12298. /* Big Hammer, we also need to ensure that any pending
  12299. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  12300. * current scanout is retired before unpinning the old
  12301. * framebuffer. Note that we rely on userspace rendering
  12302. * into the buffer attached to the pipe they are waiting
  12303. * on. If not, userspace generates a GPU hang with IPEHR
  12304. * point to the MI_WAIT_FOR_EVENT.
  12305. *
  12306. * This should only fail upon a hung GPU, in which case we
  12307. * can safely continue.
  12308. */
  12309. if (needs_modeset(crtc_state))
  12310. ret = i915_gem_object_wait_rendering(old_obj, true);
  12311. if (ret) {
  12312. /* GPU hangs should have been swallowed by the wait */
  12313. WARN_ON(ret == -EIO);
  12314. return ret;
  12315. }
  12316. }
  12317. if (!obj)
  12318. return 0;
  12319. /* For framebuffer backed by dmabuf, wait for fence */
  12320. resv = i915_gem_object_get_dmabuf_resv(obj);
  12321. if (resv) {
  12322. long lret;
  12323. lret = reservation_object_wait_timeout_rcu(resv, false, true,
  12324. MAX_SCHEDULE_TIMEOUT);
  12325. if (lret == -ERESTARTSYS)
  12326. return lret;
  12327. WARN(lret < 0, "waiting returns %li\n", lret);
  12328. }
  12329. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  12330. INTEL_INFO(dev)->cursor_needs_physical) {
  12331. int align = IS_I830(dev) ? 16 * 1024 : 256;
  12332. ret = i915_gem_object_attach_phys(obj, align);
  12333. if (ret)
  12334. DRM_DEBUG_KMS("failed to attach phys object\n");
  12335. } else {
  12336. struct i915_vma *vma;
  12337. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  12338. if (IS_ERR(vma))
  12339. ret = PTR_ERR(vma);
  12340. }
  12341. if (ret == 0) {
  12342. to_intel_plane_state(new_state)->wait_req =
  12343. i915_gem_active_get(&obj->last_write,
  12344. &obj->base.dev->struct_mutex);
  12345. }
  12346. return ret;
  12347. }
  12348. /**
  12349. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  12350. * @plane: drm plane to clean up for
  12351. * @fb: old framebuffer that was on plane
  12352. *
  12353. * Cleans up a framebuffer that has just been removed from a plane.
  12354. *
  12355. * Must be called with struct_mutex held.
  12356. */
  12357. void
  12358. intel_cleanup_plane_fb(struct drm_plane *plane,
  12359. struct drm_plane_state *old_state)
  12360. {
  12361. struct drm_device *dev = plane->dev;
  12362. struct intel_plane_state *old_intel_state;
  12363. struct intel_plane_state *intel_state = to_intel_plane_state(plane->state);
  12364. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  12365. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  12366. old_intel_state = to_intel_plane_state(old_state);
  12367. if (!obj && !old_obj)
  12368. return;
  12369. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  12370. !INTEL_INFO(dev)->cursor_needs_physical))
  12371. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  12372. i915_gem_request_assign(&intel_state->wait_req, NULL);
  12373. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  12374. }
  12375. int
  12376. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  12377. {
  12378. int max_scale;
  12379. int crtc_clock, cdclk;
  12380. if (!intel_crtc || !crtc_state->base.enable)
  12381. return DRM_PLANE_HELPER_NO_SCALING;
  12382. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  12383. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  12384. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  12385. return DRM_PLANE_HELPER_NO_SCALING;
  12386. /*
  12387. * skl max scale is lower of:
  12388. * close to 3 but not 3, -1 is for that purpose
  12389. * or
  12390. * cdclk/crtc_clock
  12391. */
  12392. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  12393. return max_scale;
  12394. }
  12395. static int
  12396. intel_check_primary_plane(struct drm_plane *plane,
  12397. struct intel_crtc_state *crtc_state,
  12398. struct intel_plane_state *state)
  12399. {
  12400. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12401. struct drm_crtc *crtc = state->base.crtc;
  12402. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  12403. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  12404. bool can_position = false;
  12405. int ret;
  12406. if (INTEL_GEN(dev_priv) >= 9) {
  12407. /* use scaler when colorkey is not required */
  12408. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  12409. min_scale = 1;
  12410. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  12411. }
  12412. can_position = true;
  12413. }
  12414. ret = drm_plane_helper_check_state(&state->base,
  12415. &state->clip,
  12416. min_scale, max_scale,
  12417. can_position, true);
  12418. if (ret)
  12419. return ret;
  12420. if (!state->base.fb)
  12421. return 0;
  12422. if (INTEL_GEN(dev_priv) >= 9) {
  12423. ret = skl_check_plane_surface(state);
  12424. if (ret)
  12425. return ret;
  12426. }
  12427. return 0;
  12428. }
  12429. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  12430. struct drm_crtc_state *old_crtc_state)
  12431. {
  12432. struct drm_device *dev = crtc->dev;
  12433. struct drm_i915_private *dev_priv = to_i915(dev);
  12434. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12435. struct intel_crtc_state *old_intel_state =
  12436. to_intel_crtc_state(old_crtc_state);
  12437. bool modeset = needs_modeset(crtc->state);
  12438. enum pipe pipe = intel_crtc->pipe;
  12439. /* Perform vblank evasion around commit operation */
  12440. intel_pipe_update_start(intel_crtc);
  12441. if (modeset)
  12442. return;
  12443. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  12444. intel_color_set_csc(crtc->state);
  12445. intel_color_load_luts(crtc->state);
  12446. }
  12447. if (to_intel_crtc_state(crtc->state)->update_pipe)
  12448. intel_update_pipe_config(intel_crtc, old_intel_state);
  12449. else if (INTEL_GEN(dev_priv) >= 9) {
  12450. skl_detach_scalers(intel_crtc);
  12451. I915_WRITE(PIPE_WM_LINETIME(pipe),
  12452. dev_priv->wm.skl_hw.wm_linetime[pipe]);
  12453. }
  12454. }
  12455. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  12456. struct drm_crtc_state *old_crtc_state)
  12457. {
  12458. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12459. intel_pipe_update_end(intel_crtc, NULL);
  12460. }
  12461. /**
  12462. * intel_plane_destroy - destroy a plane
  12463. * @plane: plane to destroy
  12464. *
  12465. * Common destruction function for all types of planes (primary, cursor,
  12466. * sprite).
  12467. */
  12468. void intel_plane_destroy(struct drm_plane *plane)
  12469. {
  12470. if (!plane)
  12471. return;
  12472. drm_plane_cleanup(plane);
  12473. kfree(to_intel_plane(plane));
  12474. }
  12475. const struct drm_plane_funcs intel_plane_funcs = {
  12476. .update_plane = drm_atomic_helper_update_plane,
  12477. .disable_plane = drm_atomic_helper_disable_plane,
  12478. .destroy = intel_plane_destroy,
  12479. .set_property = drm_atomic_helper_plane_set_property,
  12480. .atomic_get_property = intel_plane_atomic_get_property,
  12481. .atomic_set_property = intel_plane_atomic_set_property,
  12482. .atomic_duplicate_state = intel_plane_duplicate_state,
  12483. .atomic_destroy_state = intel_plane_destroy_state,
  12484. };
  12485. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  12486. int pipe)
  12487. {
  12488. struct intel_plane *primary = NULL;
  12489. struct intel_plane_state *state = NULL;
  12490. const uint32_t *intel_primary_formats;
  12491. unsigned int num_formats;
  12492. int ret;
  12493. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  12494. if (!primary)
  12495. goto fail;
  12496. state = intel_create_plane_state(&primary->base);
  12497. if (!state)
  12498. goto fail;
  12499. primary->base.state = &state->base;
  12500. primary->can_scale = false;
  12501. primary->max_downscale = 1;
  12502. if (INTEL_INFO(dev)->gen >= 9) {
  12503. primary->can_scale = true;
  12504. state->scaler_id = -1;
  12505. }
  12506. primary->pipe = pipe;
  12507. primary->plane = pipe;
  12508. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  12509. primary->check_plane = intel_check_primary_plane;
  12510. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  12511. primary->plane = !pipe;
  12512. if (INTEL_INFO(dev)->gen >= 9) {
  12513. intel_primary_formats = skl_primary_formats;
  12514. num_formats = ARRAY_SIZE(skl_primary_formats);
  12515. primary->update_plane = skylake_update_primary_plane;
  12516. primary->disable_plane = skylake_disable_primary_plane;
  12517. } else if (HAS_PCH_SPLIT(dev)) {
  12518. intel_primary_formats = i965_primary_formats;
  12519. num_formats = ARRAY_SIZE(i965_primary_formats);
  12520. primary->update_plane = ironlake_update_primary_plane;
  12521. primary->disable_plane = i9xx_disable_primary_plane;
  12522. } else if (INTEL_INFO(dev)->gen >= 4) {
  12523. intel_primary_formats = i965_primary_formats;
  12524. num_formats = ARRAY_SIZE(i965_primary_formats);
  12525. primary->update_plane = i9xx_update_primary_plane;
  12526. primary->disable_plane = i9xx_disable_primary_plane;
  12527. } else {
  12528. intel_primary_formats = i8xx_primary_formats;
  12529. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  12530. primary->update_plane = i9xx_update_primary_plane;
  12531. primary->disable_plane = i9xx_disable_primary_plane;
  12532. }
  12533. if (INTEL_INFO(dev)->gen >= 9)
  12534. ret = drm_universal_plane_init(dev, &primary->base, 0,
  12535. &intel_plane_funcs,
  12536. intel_primary_formats, num_formats,
  12537. DRM_PLANE_TYPE_PRIMARY,
  12538. "plane 1%c", pipe_name(pipe));
  12539. else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  12540. ret = drm_universal_plane_init(dev, &primary->base, 0,
  12541. &intel_plane_funcs,
  12542. intel_primary_formats, num_formats,
  12543. DRM_PLANE_TYPE_PRIMARY,
  12544. "primary %c", pipe_name(pipe));
  12545. else
  12546. ret = drm_universal_plane_init(dev, &primary->base, 0,
  12547. &intel_plane_funcs,
  12548. intel_primary_formats, num_formats,
  12549. DRM_PLANE_TYPE_PRIMARY,
  12550. "plane %c", plane_name(primary->plane));
  12551. if (ret)
  12552. goto fail;
  12553. if (INTEL_INFO(dev)->gen >= 4)
  12554. intel_create_rotation_property(dev, primary);
  12555. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  12556. return &primary->base;
  12557. fail:
  12558. kfree(state);
  12559. kfree(primary);
  12560. return NULL;
  12561. }
  12562. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  12563. {
  12564. if (!dev->mode_config.rotation_property) {
  12565. unsigned long flags = DRM_ROTATE_0 |
  12566. DRM_ROTATE_180;
  12567. if (INTEL_INFO(dev)->gen >= 9)
  12568. flags |= DRM_ROTATE_90 | DRM_ROTATE_270;
  12569. dev->mode_config.rotation_property =
  12570. drm_mode_create_rotation_property(dev, flags);
  12571. }
  12572. if (dev->mode_config.rotation_property)
  12573. drm_object_attach_property(&plane->base.base,
  12574. dev->mode_config.rotation_property,
  12575. plane->base.state->rotation);
  12576. }
  12577. static int
  12578. intel_check_cursor_plane(struct drm_plane *plane,
  12579. struct intel_crtc_state *crtc_state,
  12580. struct intel_plane_state *state)
  12581. {
  12582. struct drm_framebuffer *fb = state->base.fb;
  12583. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12584. enum pipe pipe = to_intel_plane(plane)->pipe;
  12585. unsigned stride;
  12586. int ret;
  12587. ret = drm_plane_helper_check_state(&state->base,
  12588. &state->clip,
  12589. DRM_PLANE_HELPER_NO_SCALING,
  12590. DRM_PLANE_HELPER_NO_SCALING,
  12591. true, true);
  12592. if (ret)
  12593. return ret;
  12594. /* if we want to turn off the cursor ignore width and height */
  12595. if (!obj)
  12596. return 0;
  12597. /* Check for which cursor types we support */
  12598. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  12599. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  12600. state->base.crtc_w, state->base.crtc_h);
  12601. return -EINVAL;
  12602. }
  12603. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  12604. if (obj->base.size < stride * state->base.crtc_h) {
  12605. DRM_DEBUG_KMS("buffer is too small\n");
  12606. return -ENOMEM;
  12607. }
  12608. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  12609. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  12610. return -EINVAL;
  12611. }
  12612. /*
  12613. * There's something wrong with the cursor on CHV pipe C.
  12614. * If it straddles the left edge of the screen then
  12615. * moving it away from the edge or disabling it often
  12616. * results in a pipe underrun, and often that can lead to
  12617. * dead pipe (constant underrun reported, and it scans
  12618. * out just a solid color). To recover from that, the
  12619. * display power well must be turned off and on again.
  12620. * Refuse the put the cursor into that compromised position.
  12621. */
  12622. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  12623. state->base.visible && state->base.crtc_x < 0) {
  12624. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12625. return -EINVAL;
  12626. }
  12627. return 0;
  12628. }
  12629. static void
  12630. intel_disable_cursor_plane(struct drm_plane *plane,
  12631. struct drm_crtc *crtc)
  12632. {
  12633. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12634. intel_crtc->cursor_addr = 0;
  12635. intel_crtc_update_cursor(crtc, NULL);
  12636. }
  12637. static void
  12638. intel_update_cursor_plane(struct drm_plane *plane,
  12639. const struct intel_crtc_state *crtc_state,
  12640. const struct intel_plane_state *state)
  12641. {
  12642. struct drm_crtc *crtc = crtc_state->base.crtc;
  12643. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12644. struct drm_device *dev = plane->dev;
  12645. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12646. uint32_t addr;
  12647. if (!obj)
  12648. addr = 0;
  12649. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  12650. addr = i915_gem_object_ggtt_offset(obj, NULL);
  12651. else
  12652. addr = obj->phys_handle->busaddr;
  12653. intel_crtc->cursor_addr = addr;
  12654. intel_crtc_update_cursor(crtc, state);
  12655. }
  12656. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  12657. int pipe)
  12658. {
  12659. struct intel_plane *cursor = NULL;
  12660. struct intel_plane_state *state = NULL;
  12661. int ret;
  12662. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12663. if (!cursor)
  12664. goto fail;
  12665. state = intel_create_plane_state(&cursor->base);
  12666. if (!state)
  12667. goto fail;
  12668. cursor->base.state = &state->base;
  12669. cursor->can_scale = false;
  12670. cursor->max_downscale = 1;
  12671. cursor->pipe = pipe;
  12672. cursor->plane = pipe;
  12673. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12674. cursor->check_plane = intel_check_cursor_plane;
  12675. cursor->update_plane = intel_update_cursor_plane;
  12676. cursor->disable_plane = intel_disable_cursor_plane;
  12677. ret = drm_universal_plane_init(dev, &cursor->base, 0,
  12678. &intel_plane_funcs,
  12679. intel_cursor_formats,
  12680. ARRAY_SIZE(intel_cursor_formats),
  12681. DRM_PLANE_TYPE_CURSOR,
  12682. "cursor %c", pipe_name(pipe));
  12683. if (ret)
  12684. goto fail;
  12685. if (INTEL_INFO(dev)->gen >= 4) {
  12686. if (!dev->mode_config.rotation_property)
  12687. dev->mode_config.rotation_property =
  12688. drm_mode_create_rotation_property(dev,
  12689. DRM_ROTATE_0 |
  12690. DRM_ROTATE_180);
  12691. if (dev->mode_config.rotation_property)
  12692. drm_object_attach_property(&cursor->base.base,
  12693. dev->mode_config.rotation_property,
  12694. state->base.rotation);
  12695. }
  12696. if (INTEL_INFO(dev)->gen >=9)
  12697. state->scaler_id = -1;
  12698. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12699. return &cursor->base;
  12700. fail:
  12701. kfree(state);
  12702. kfree(cursor);
  12703. return NULL;
  12704. }
  12705. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  12706. struct intel_crtc_state *crtc_state)
  12707. {
  12708. int i;
  12709. struct intel_scaler *intel_scaler;
  12710. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  12711. for (i = 0; i < intel_crtc->num_scalers; i++) {
  12712. intel_scaler = &scaler_state->scalers[i];
  12713. intel_scaler->in_use = 0;
  12714. intel_scaler->mode = PS_SCALER_MODE_DYN;
  12715. }
  12716. scaler_state->scaler_id = -1;
  12717. }
  12718. static void intel_crtc_init(struct drm_device *dev, int pipe)
  12719. {
  12720. struct drm_i915_private *dev_priv = to_i915(dev);
  12721. struct intel_crtc *intel_crtc;
  12722. struct intel_crtc_state *crtc_state = NULL;
  12723. struct drm_plane *primary = NULL;
  12724. struct drm_plane *cursor = NULL;
  12725. int ret;
  12726. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12727. if (intel_crtc == NULL)
  12728. return;
  12729. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12730. if (!crtc_state)
  12731. goto fail;
  12732. intel_crtc->config = crtc_state;
  12733. intel_crtc->base.state = &crtc_state->base;
  12734. crtc_state->base.crtc = &intel_crtc->base;
  12735. /* initialize shared scalers */
  12736. if (INTEL_INFO(dev)->gen >= 9) {
  12737. if (pipe == PIPE_C)
  12738. intel_crtc->num_scalers = 1;
  12739. else
  12740. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  12741. skl_init_scalers(dev, intel_crtc, crtc_state);
  12742. }
  12743. primary = intel_primary_plane_create(dev, pipe);
  12744. if (!primary)
  12745. goto fail;
  12746. cursor = intel_cursor_plane_create(dev, pipe);
  12747. if (!cursor)
  12748. goto fail;
  12749. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  12750. cursor, &intel_crtc_funcs,
  12751. "pipe %c", pipe_name(pipe));
  12752. if (ret)
  12753. goto fail;
  12754. /*
  12755. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  12756. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  12757. */
  12758. intel_crtc->pipe = pipe;
  12759. intel_crtc->plane = pipe;
  12760. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  12761. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  12762. intel_crtc->plane = !pipe;
  12763. }
  12764. intel_crtc->cursor_base = ~0;
  12765. intel_crtc->cursor_cntl = ~0;
  12766. intel_crtc->cursor_size = ~0;
  12767. intel_crtc->wm.cxsr_allowed = true;
  12768. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12769. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12770. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  12771. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  12772. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12773. intel_color_init(&intel_crtc->base);
  12774. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12775. return;
  12776. fail:
  12777. intel_plane_destroy(primary);
  12778. intel_plane_destroy(cursor);
  12779. kfree(crtc_state);
  12780. kfree(intel_crtc);
  12781. }
  12782. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12783. {
  12784. struct drm_encoder *encoder = connector->base.encoder;
  12785. struct drm_device *dev = connector->base.dev;
  12786. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12787. if (!encoder || WARN_ON(!encoder->crtc))
  12788. return INVALID_PIPE;
  12789. return to_intel_crtc(encoder->crtc)->pipe;
  12790. }
  12791. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12792. struct drm_file *file)
  12793. {
  12794. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12795. struct drm_crtc *drmmode_crtc;
  12796. struct intel_crtc *crtc;
  12797. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12798. if (!drmmode_crtc)
  12799. return -ENOENT;
  12800. crtc = to_intel_crtc(drmmode_crtc);
  12801. pipe_from_crtc_id->pipe = crtc->pipe;
  12802. return 0;
  12803. }
  12804. static int intel_encoder_clones(struct intel_encoder *encoder)
  12805. {
  12806. struct drm_device *dev = encoder->base.dev;
  12807. struct intel_encoder *source_encoder;
  12808. int index_mask = 0;
  12809. int entry = 0;
  12810. for_each_intel_encoder(dev, source_encoder) {
  12811. if (encoders_cloneable(encoder, source_encoder))
  12812. index_mask |= (1 << entry);
  12813. entry++;
  12814. }
  12815. return index_mask;
  12816. }
  12817. static bool has_edp_a(struct drm_device *dev)
  12818. {
  12819. struct drm_i915_private *dev_priv = to_i915(dev);
  12820. if (!IS_MOBILE(dev))
  12821. return false;
  12822. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12823. return false;
  12824. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12825. return false;
  12826. return true;
  12827. }
  12828. static bool intel_crt_present(struct drm_device *dev)
  12829. {
  12830. struct drm_i915_private *dev_priv = to_i915(dev);
  12831. if (INTEL_INFO(dev)->gen >= 9)
  12832. return false;
  12833. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  12834. return false;
  12835. if (IS_CHERRYVIEW(dev))
  12836. return false;
  12837. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12838. return false;
  12839. /* DDI E can't be used if DDI A requires 4 lanes */
  12840. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12841. return false;
  12842. if (!dev_priv->vbt.int_crt_support)
  12843. return false;
  12844. return true;
  12845. }
  12846. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  12847. {
  12848. int pps_num;
  12849. int pps_idx;
  12850. if (HAS_DDI(dev_priv))
  12851. return;
  12852. /*
  12853. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  12854. * everywhere where registers can be write protected.
  12855. */
  12856. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12857. pps_num = 2;
  12858. else
  12859. pps_num = 1;
  12860. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  12861. u32 val = I915_READ(PP_CONTROL(pps_idx));
  12862. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  12863. I915_WRITE(PP_CONTROL(pps_idx), val);
  12864. }
  12865. }
  12866. static void intel_pps_init(struct drm_i915_private *dev_priv)
  12867. {
  12868. if (HAS_PCH_SPLIT(dev_priv) || IS_BROXTON(dev_priv))
  12869. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  12870. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12871. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  12872. else
  12873. dev_priv->pps_mmio_base = PPS_BASE;
  12874. intel_pps_unlock_regs_wa(dev_priv);
  12875. }
  12876. static void intel_setup_outputs(struct drm_device *dev)
  12877. {
  12878. struct drm_i915_private *dev_priv = to_i915(dev);
  12879. struct intel_encoder *encoder;
  12880. bool dpd_is_edp = false;
  12881. intel_pps_init(dev_priv);
  12882. /*
  12883. * intel_edp_init_connector() depends on this completing first, to
  12884. * prevent the registeration of both eDP and LVDS and the incorrect
  12885. * sharing of the PPS.
  12886. */
  12887. intel_lvds_init(dev);
  12888. if (intel_crt_present(dev))
  12889. intel_crt_init(dev);
  12890. if (IS_BROXTON(dev)) {
  12891. /*
  12892. * FIXME: Broxton doesn't support port detection via the
  12893. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12894. * detect the ports.
  12895. */
  12896. intel_ddi_init(dev, PORT_A);
  12897. intel_ddi_init(dev, PORT_B);
  12898. intel_ddi_init(dev, PORT_C);
  12899. intel_dsi_init(dev);
  12900. } else if (HAS_DDI(dev)) {
  12901. int found;
  12902. /*
  12903. * Haswell uses DDI functions to detect digital outputs.
  12904. * On SKL pre-D0 the strap isn't connected, so we assume
  12905. * it's there.
  12906. */
  12907. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12908. /* WaIgnoreDDIAStrap: skl */
  12909. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12910. intel_ddi_init(dev, PORT_A);
  12911. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12912. * register */
  12913. found = I915_READ(SFUSE_STRAP);
  12914. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12915. intel_ddi_init(dev, PORT_B);
  12916. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12917. intel_ddi_init(dev, PORT_C);
  12918. if (found & SFUSE_STRAP_DDID_DETECTED)
  12919. intel_ddi_init(dev, PORT_D);
  12920. /*
  12921. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12922. */
  12923. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  12924. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12925. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12926. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12927. intel_ddi_init(dev, PORT_E);
  12928. } else if (HAS_PCH_SPLIT(dev)) {
  12929. int found;
  12930. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12931. if (has_edp_a(dev))
  12932. intel_dp_init(dev, DP_A, PORT_A);
  12933. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12934. /* PCH SDVOB multiplex with HDMIB */
  12935. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12936. if (!found)
  12937. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12938. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12939. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12940. }
  12941. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12942. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12943. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12944. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12945. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12946. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12947. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12948. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12949. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12950. bool has_edp, has_port;
  12951. /*
  12952. * The DP_DETECTED bit is the latched state of the DDC
  12953. * SDA pin at boot. However since eDP doesn't require DDC
  12954. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12955. * eDP ports may have been muxed to an alternate function.
  12956. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12957. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12958. * detect eDP ports.
  12959. *
  12960. * Sadly the straps seem to be missing sometimes even for HDMI
  12961. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  12962. * and VBT for the presence of the port. Additionally we can't
  12963. * trust the port type the VBT declares as we've seen at least
  12964. * HDMI ports that the VBT claim are DP or eDP.
  12965. */
  12966. has_edp = intel_dp_is_edp(dev, PORT_B);
  12967. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  12968. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  12969. has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
  12970. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  12971. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  12972. has_edp = intel_dp_is_edp(dev, PORT_C);
  12973. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  12974. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  12975. has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
  12976. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  12977. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  12978. if (IS_CHERRYVIEW(dev)) {
  12979. /*
  12980. * eDP not supported on port D,
  12981. * so no need to worry about it
  12982. */
  12983. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  12984. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  12985. intel_dp_init(dev, CHV_DP_D, PORT_D);
  12986. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  12987. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  12988. }
  12989. intel_dsi_init(dev);
  12990. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12991. bool found = false;
  12992. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12993. DRM_DEBUG_KMS("probing SDVOB\n");
  12994. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  12995. if (!found && IS_G4X(dev)) {
  12996. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12997. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12998. }
  12999. if (!found && IS_G4X(dev))
  13000. intel_dp_init(dev, DP_B, PORT_B);
  13001. }
  13002. /* Before G4X SDVOC doesn't have its own detect register */
  13003. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13004. DRM_DEBUG_KMS("probing SDVOC\n");
  13005. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  13006. }
  13007. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  13008. if (IS_G4X(dev)) {
  13009. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  13010. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  13011. }
  13012. if (IS_G4X(dev))
  13013. intel_dp_init(dev, DP_C, PORT_C);
  13014. }
  13015. if (IS_G4X(dev) &&
  13016. (I915_READ(DP_D) & DP_DETECTED))
  13017. intel_dp_init(dev, DP_D, PORT_D);
  13018. } else if (IS_GEN2(dev))
  13019. intel_dvo_init(dev);
  13020. if (SUPPORTS_TV(dev))
  13021. intel_tv_init(dev);
  13022. intel_psr_init(dev);
  13023. for_each_intel_encoder(dev, encoder) {
  13024. encoder->base.possible_crtcs = encoder->crtc_mask;
  13025. encoder->base.possible_clones =
  13026. intel_encoder_clones(encoder);
  13027. }
  13028. intel_init_pch_refclk(dev);
  13029. drm_helper_move_panel_connectors_to_head(dev);
  13030. }
  13031. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  13032. {
  13033. struct drm_device *dev = fb->dev;
  13034. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13035. drm_framebuffer_cleanup(fb);
  13036. mutex_lock(&dev->struct_mutex);
  13037. WARN_ON(!intel_fb->obj->framebuffer_references--);
  13038. i915_gem_object_put(intel_fb->obj);
  13039. mutex_unlock(&dev->struct_mutex);
  13040. kfree(intel_fb);
  13041. }
  13042. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  13043. struct drm_file *file,
  13044. unsigned int *handle)
  13045. {
  13046. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13047. struct drm_i915_gem_object *obj = intel_fb->obj;
  13048. if (obj->userptr.mm) {
  13049. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  13050. return -EINVAL;
  13051. }
  13052. return drm_gem_handle_create(file, &obj->base, handle);
  13053. }
  13054. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  13055. struct drm_file *file,
  13056. unsigned flags, unsigned color,
  13057. struct drm_clip_rect *clips,
  13058. unsigned num_clips)
  13059. {
  13060. struct drm_device *dev = fb->dev;
  13061. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13062. struct drm_i915_gem_object *obj = intel_fb->obj;
  13063. mutex_lock(&dev->struct_mutex);
  13064. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  13065. mutex_unlock(&dev->struct_mutex);
  13066. return 0;
  13067. }
  13068. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  13069. .destroy = intel_user_framebuffer_destroy,
  13070. .create_handle = intel_user_framebuffer_create_handle,
  13071. .dirty = intel_user_framebuffer_dirty,
  13072. };
  13073. static
  13074. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  13075. uint32_t pixel_format)
  13076. {
  13077. u32 gen = INTEL_INFO(dev)->gen;
  13078. if (gen >= 9) {
  13079. int cpp = drm_format_plane_cpp(pixel_format, 0);
  13080. /* "The stride in bytes must not exceed the of the size of 8K
  13081. * pixels and 32K bytes."
  13082. */
  13083. return min(8192 * cpp, 32768);
  13084. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  13085. return 32*1024;
  13086. } else if (gen >= 4) {
  13087. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13088. return 16*1024;
  13089. else
  13090. return 32*1024;
  13091. } else if (gen >= 3) {
  13092. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13093. return 8*1024;
  13094. else
  13095. return 16*1024;
  13096. } else {
  13097. /* XXX DSPC is limited to 4k tiled */
  13098. return 8*1024;
  13099. }
  13100. }
  13101. static int intel_framebuffer_init(struct drm_device *dev,
  13102. struct intel_framebuffer *intel_fb,
  13103. struct drm_mode_fb_cmd2 *mode_cmd,
  13104. struct drm_i915_gem_object *obj)
  13105. {
  13106. struct drm_i915_private *dev_priv = to_i915(dev);
  13107. unsigned int tiling = i915_gem_object_get_tiling(obj);
  13108. int ret;
  13109. u32 pitch_limit, stride_alignment;
  13110. char *format_name;
  13111. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  13112. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  13113. /*
  13114. * If there's a fence, enforce that
  13115. * the fb modifier and tiling mode match.
  13116. */
  13117. if (tiling != I915_TILING_NONE &&
  13118. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13119. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  13120. return -EINVAL;
  13121. }
  13122. } else {
  13123. if (tiling == I915_TILING_X) {
  13124. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  13125. } else if (tiling == I915_TILING_Y) {
  13126. DRM_DEBUG("No Y tiling for legacy addfb\n");
  13127. return -EINVAL;
  13128. }
  13129. }
  13130. /* Passed in modifier sanity checking. */
  13131. switch (mode_cmd->modifier[0]) {
  13132. case I915_FORMAT_MOD_Y_TILED:
  13133. case I915_FORMAT_MOD_Yf_TILED:
  13134. if (INTEL_INFO(dev)->gen < 9) {
  13135. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  13136. mode_cmd->modifier[0]);
  13137. return -EINVAL;
  13138. }
  13139. case DRM_FORMAT_MOD_NONE:
  13140. case I915_FORMAT_MOD_X_TILED:
  13141. break;
  13142. default:
  13143. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  13144. mode_cmd->modifier[0]);
  13145. return -EINVAL;
  13146. }
  13147. /*
  13148. * gen2/3 display engine uses the fence if present,
  13149. * so the tiling mode must match the fb modifier exactly.
  13150. */
  13151. if (INTEL_INFO(dev_priv)->gen < 4 &&
  13152. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13153. DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
  13154. return -EINVAL;
  13155. }
  13156. stride_alignment = intel_fb_stride_alignment(dev_priv,
  13157. mode_cmd->modifier[0],
  13158. mode_cmd->pixel_format);
  13159. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  13160. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  13161. mode_cmd->pitches[0], stride_alignment);
  13162. return -EINVAL;
  13163. }
  13164. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  13165. mode_cmd->pixel_format);
  13166. if (mode_cmd->pitches[0] > pitch_limit) {
  13167. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  13168. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  13169. "tiled" : "linear",
  13170. mode_cmd->pitches[0], pitch_limit);
  13171. return -EINVAL;
  13172. }
  13173. /*
  13174. * If there's a fence, enforce that
  13175. * the fb pitch and fence stride match.
  13176. */
  13177. if (tiling != I915_TILING_NONE &&
  13178. mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
  13179. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  13180. mode_cmd->pitches[0],
  13181. i915_gem_object_get_stride(obj));
  13182. return -EINVAL;
  13183. }
  13184. /* Reject formats not supported by any plane early. */
  13185. switch (mode_cmd->pixel_format) {
  13186. case DRM_FORMAT_C8:
  13187. case DRM_FORMAT_RGB565:
  13188. case DRM_FORMAT_XRGB8888:
  13189. case DRM_FORMAT_ARGB8888:
  13190. break;
  13191. case DRM_FORMAT_XRGB1555:
  13192. if (INTEL_INFO(dev)->gen > 3) {
  13193. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13194. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13195. kfree(format_name);
  13196. return -EINVAL;
  13197. }
  13198. break;
  13199. case DRM_FORMAT_ABGR8888:
  13200. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  13201. INTEL_INFO(dev)->gen < 9) {
  13202. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13203. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13204. kfree(format_name);
  13205. return -EINVAL;
  13206. }
  13207. break;
  13208. case DRM_FORMAT_XBGR8888:
  13209. case DRM_FORMAT_XRGB2101010:
  13210. case DRM_FORMAT_XBGR2101010:
  13211. if (INTEL_INFO(dev)->gen < 4) {
  13212. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13213. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13214. kfree(format_name);
  13215. return -EINVAL;
  13216. }
  13217. break;
  13218. case DRM_FORMAT_ABGR2101010:
  13219. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  13220. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13221. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13222. kfree(format_name);
  13223. return -EINVAL;
  13224. }
  13225. break;
  13226. case DRM_FORMAT_YUYV:
  13227. case DRM_FORMAT_UYVY:
  13228. case DRM_FORMAT_YVYU:
  13229. case DRM_FORMAT_VYUY:
  13230. if (INTEL_INFO(dev)->gen < 5) {
  13231. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13232. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13233. kfree(format_name);
  13234. return -EINVAL;
  13235. }
  13236. break;
  13237. default:
  13238. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13239. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13240. kfree(format_name);
  13241. return -EINVAL;
  13242. }
  13243. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  13244. if (mode_cmd->offsets[0] != 0)
  13245. return -EINVAL;
  13246. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  13247. intel_fb->obj = obj;
  13248. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  13249. if (ret)
  13250. return ret;
  13251. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  13252. if (ret) {
  13253. DRM_ERROR("framebuffer init failed %d\n", ret);
  13254. return ret;
  13255. }
  13256. intel_fb->obj->framebuffer_references++;
  13257. return 0;
  13258. }
  13259. static struct drm_framebuffer *
  13260. intel_user_framebuffer_create(struct drm_device *dev,
  13261. struct drm_file *filp,
  13262. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  13263. {
  13264. struct drm_framebuffer *fb;
  13265. struct drm_i915_gem_object *obj;
  13266. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  13267. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  13268. if (!obj)
  13269. return ERR_PTR(-ENOENT);
  13270. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  13271. if (IS_ERR(fb))
  13272. i915_gem_object_put_unlocked(obj);
  13273. return fb;
  13274. }
  13275. #ifndef CONFIG_DRM_FBDEV_EMULATION
  13276. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  13277. {
  13278. }
  13279. #endif
  13280. static const struct drm_mode_config_funcs intel_mode_funcs = {
  13281. .fb_create = intel_user_framebuffer_create,
  13282. .output_poll_changed = intel_fbdev_output_poll_changed,
  13283. .atomic_check = intel_atomic_check,
  13284. .atomic_commit = intel_atomic_commit,
  13285. .atomic_state_alloc = intel_atomic_state_alloc,
  13286. .atomic_state_clear = intel_atomic_state_clear,
  13287. };
  13288. /**
  13289. * intel_init_display_hooks - initialize the display modesetting hooks
  13290. * @dev_priv: device private
  13291. */
  13292. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  13293. {
  13294. if (INTEL_INFO(dev_priv)->gen >= 9) {
  13295. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13296. dev_priv->display.get_initial_plane_config =
  13297. skylake_get_initial_plane_config;
  13298. dev_priv->display.crtc_compute_clock =
  13299. haswell_crtc_compute_clock;
  13300. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13301. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13302. } else if (HAS_DDI(dev_priv)) {
  13303. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13304. dev_priv->display.get_initial_plane_config =
  13305. ironlake_get_initial_plane_config;
  13306. dev_priv->display.crtc_compute_clock =
  13307. haswell_crtc_compute_clock;
  13308. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13309. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13310. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13311. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  13312. dev_priv->display.get_initial_plane_config =
  13313. ironlake_get_initial_plane_config;
  13314. dev_priv->display.crtc_compute_clock =
  13315. ironlake_crtc_compute_clock;
  13316. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  13317. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  13318. } else if (IS_CHERRYVIEW(dev_priv)) {
  13319. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13320. dev_priv->display.get_initial_plane_config =
  13321. i9xx_get_initial_plane_config;
  13322. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  13323. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13324. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13325. } else if (IS_VALLEYVIEW(dev_priv)) {
  13326. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13327. dev_priv->display.get_initial_plane_config =
  13328. i9xx_get_initial_plane_config;
  13329. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  13330. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13331. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13332. } else if (IS_G4X(dev_priv)) {
  13333. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13334. dev_priv->display.get_initial_plane_config =
  13335. i9xx_get_initial_plane_config;
  13336. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  13337. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13338. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13339. } else if (IS_PINEVIEW(dev_priv)) {
  13340. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13341. dev_priv->display.get_initial_plane_config =
  13342. i9xx_get_initial_plane_config;
  13343. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  13344. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13345. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13346. } else if (!IS_GEN2(dev_priv)) {
  13347. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13348. dev_priv->display.get_initial_plane_config =
  13349. i9xx_get_initial_plane_config;
  13350. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  13351. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13352. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13353. } else {
  13354. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13355. dev_priv->display.get_initial_plane_config =
  13356. i9xx_get_initial_plane_config;
  13357. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  13358. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13359. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13360. }
  13361. /* Returns the core display clock speed */
  13362. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  13363. dev_priv->display.get_display_clock_speed =
  13364. skylake_get_display_clock_speed;
  13365. else if (IS_BROXTON(dev_priv))
  13366. dev_priv->display.get_display_clock_speed =
  13367. broxton_get_display_clock_speed;
  13368. else if (IS_BROADWELL(dev_priv))
  13369. dev_priv->display.get_display_clock_speed =
  13370. broadwell_get_display_clock_speed;
  13371. else if (IS_HASWELL(dev_priv))
  13372. dev_priv->display.get_display_clock_speed =
  13373. haswell_get_display_clock_speed;
  13374. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13375. dev_priv->display.get_display_clock_speed =
  13376. valleyview_get_display_clock_speed;
  13377. else if (IS_GEN5(dev_priv))
  13378. dev_priv->display.get_display_clock_speed =
  13379. ilk_get_display_clock_speed;
  13380. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  13381. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  13382. dev_priv->display.get_display_clock_speed =
  13383. i945_get_display_clock_speed;
  13384. else if (IS_GM45(dev_priv))
  13385. dev_priv->display.get_display_clock_speed =
  13386. gm45_get_display_clock_speed;
  13387. else if (IS_CRESTLINE(dev_priv))
  13388. dev_priv->display.get_display_clock_speed =
  13389. i965gm_get_display_clock_speed;
  13390. else if (IS_PINEVIEW(dev_priv))
  13391. dev_priv->display.get_display_clock_speed =
  13392. pnv_get_display_clock_speed;
  13393. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  13394. dev_priv->display.get_display_clock_speed =
  13395. g33_get_display_clock_speed;
  13396. else if (IS_I915G(dev_priv))
  13397. dev_priv->display.get_display_clock_speed =
  13398. i915_get_display_clock_speed;
  13399. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  13400. dev_priv->display.get_display_clock_speed =
  13401. i9xx_misc_get_display_clock_speed;
  13402. else if (IS_I915GM(dev_priv))
  13403. dev_priv->display.get_display_clock_speed =
  13404. i915gm_get_display_clock_speed;
  13405. else if (IS_I865G(dev_priv))
  13406. dev_priv->display.get_display_clock_speed =
  13407. i865_get_display_clock_speed;
  13408. else if (IS_I85X(dev_priv))
  13409. dev_priv->display.get_display_clock_speed =
  13410. i85x_get_display_clock_speed;
  13411. else { /* 830 */
  13412. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  13413. dev_priv->display.get_display_clock_speed =
  13414. i830_get_display_clock_speed;
  13415. }
  13416. if (IS_GEN5(dev_priv)) {
  13417. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  13418. } else if (IS_GEN6(dev_priv)) {
  13419. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  13420. } else if (IS_IVYBRIDGE(dev_priv)) {
  13421. /* FIXME: detect B0+ stepping and use auto training */
  13422. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  13423. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  13424. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  13425. }
  13426. if (IS_BROADWELL(dev_priv)) {
  13427. dev_priv->display.modeset_commit_cdclk =
  13428. broadwell_modeset_commit_cdclk;
  13429. dev_priv->display.modeset_calc_cdclk =
  13430. broadwell_modeset_calc_cdclk;
  13431. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13432. dev_priv->display.modeset_commit_cdclk =
  13433. valleyview_modeset_commit_cdclk;
  13434. dev_priv->display.modeset_calc_cdclk =
  13435. valleyview_modeset_calc_cdclk;
  13436. } else if (IS_BROXTON(dev_priv)) {
  13437. dev_priv->display.modeset_commit_cdclk =
  13438. bxt_modeset_commit_cdclk;
  13439. dev_priv->display.modeset_calc_cdclk =
  13440. bxt_modeset_calc_cdclk;
  13441. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  13442. dev_priv->display.modeset_commit_cdclk =
  13443. skl_modeset_commit_cdclk;
  13444. dev_priv->display.modeset_calc_cdclk =
  13445. skl_modeset_calc_cdclk;
  13446. }
  13447. if (dev_priv->info.gen >= 9)
  13448. dev_priv->display.update_crtcs = skl_update_crtcs;
  13449. else
  13450. dev_priv->display.update_crtcs = intel_update_crtcs;
  13451. switch (INTEL_INFO(dev_priv)->gen) {
  13452. case 2:
  13453. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  13454. break;
  13455. case 3:
  13456. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  13457. break;
  13458. case 4:
  13459. case 5:
  13460. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  13461. break;
  13462. case 6:
  13463. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  13464. break;
  13465. case 7:
  13466. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  13467. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  13468. break;
  13469. case 9:
  13470. /* Drop through - unsupported since execlist only. */
  13471. default:
  13472. /* Default just returns -ENODEV to indicate unsupported */
  13473. dev_priv->display.queue_flip = intel_default_queue_flip;
  13474. }
  13475. }
  13476. /*
  13477. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  13478. * resume, or other times. This quirk makes sure that's the case for
  13479. * affected systems.
  13480. */
  13481. static void quirk_pipea_force(struct drm_device *dev)
  13482. {
  13483. struct drm_i915_private *dev_priv = to_i915(dev);
  13484. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  13485. DRM_INFO("applying pipe a force quirk\n");
  13486. }
  13487. static void quirk_pipeb_force(struct drm_device *dev)
  13488. {
  13489. struct drm_i915_private *dev_priv = to_i915(dev);
  13490. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  13491. DRM_INFO("applying pipe b force quirk\n");
  13492. }
  13493. /*
  13494. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  13495. */
  13496. static void quirk_ssc_force_disable(struct drm_device *dev)
  13497. {
  13498. struct drm_i915_private *dev_priv = to_i915(dev);
  13499. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  13500. DRM_INFO("applying lvds SSC disable quirk\n");
  13501. }
  13502. /*
  13503. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  13504. * brightness value
  13505. */
  13506. static void quirk_invert_brightness(struct drm_device *dev)
  13507. {
  13508. struct drm_i915_private *dev_priv = to_i915(dev);
  13509. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  13510. DRM_INFO("applying inverted panel brightness quirk\n");
  13511. }
  13512. /* Some VBT's incorrectly indicate no backlight is present */
  13513. static void quirk_backlight_present(struct drm_device *dev)
  13514. {
  13515. struct drm_i915_private *dev_priv = to_i915(dev);
  13516. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  13517. DRM_INFO("applying backlight present quirk\n");
  13518. }
  13519. struct intel_quirk {
  13520. int device;
  13521. int subsystem_vendor;
  13522. int subsystem_device;
  13523. void (*hook)(struct drm_device *dev);
  13524. };
  13525. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  13526. struct intel_dmi_quirk {
  13527. void (*hook)(struct drm_device *dev);
  13528. const struct dmi_system_id (*dmi_id_list)[];
  13529. };
  13530. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  13531. {
  13532. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  13533. return 1;
  13534. }
  13535. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  13536. {
  13537. .dmi_id_list = &(const struct dmi_system_id[]) {
  13538. {
  13539. .callback = intel_dmi_reverse_brightness,
  13540. .ident = "NCR Corporation",
  13541. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  13542. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  13543. },
  13544. },
  13545. { } /* terminating entry */
  13546. },
  13547. .hook = quirk_invert_brightness,
  13548. },
  13549. };
  13550. static struct intel_quirk intel_quirks[] = {
  13551. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  13552. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  13553. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  13554. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  13555. /* 830 needs to leave pipe A & dpll A up */
  13556. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  13557. /* 830 needs to leave pipe B & dpll B up */
  13558. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  13559. /* Lenovo U160 cannot use SSC on LVDS */
  13560. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  13561. /* Sony Vaio Y cannot use SSC on LVDS */
  13562. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  13563. /* Acer Aspire 5734Z must invert backlight brightness */
  13564. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  13565. /* Acer/eMachines G725 */
  13566. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  13567. /* Acer/eMachines e725 */
  13568. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  13569. /* Acer/Packard Bell NCL20 */
  13570. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  13571. /* Acer Aspire 4736Z */
  13572. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  13573. /* Acer Aspire 5336 */
  13574. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  13575. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  13576. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  13577. /* Acer C720 Chromebook (Core i3 4005U) */
  13578. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  13579. /* Apple Macbook 2,1 (Core 2 T7400) */
  13580. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  13581. /* Apple Macbook 4,1 */
  13582. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  13583. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  13584. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  13585. /* HP Chromebook 14 (Celeron 2955U) */
  13586. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  13587. /* Dell Chromebook 11 */
  13588. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  13589. /* Dell Chromebook 11 (2015 version) */
  13590. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  13591. };
  13592. static void intel_init_quirks(struct drm_device *dev)
  13593. {
  13594. struct pci_dev *d = dev->pdev;
  13595. int i;
  13596. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  13597. struct intel_quirk *q = &intel_quirks[i];
  13598. if (d->device == q->device &&
  13599. (d->subsystem_vendor == q->subsystem_vendor ||
  13600. q->subsystem_vendor == PCI_ANY_ID) &&
  13601. (d->subsystem_device == q->subsystem_device ||
  13602. q->subsystem_device == PCI_ANY_ID))
  13603. q->hook(dev);
  13604. }
  13605. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  13606. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  13607. intel_dmi_quirks[i].hook(dev);
  13608. }
  13609. }
  13610. /* Disable the VGA plane that we never use */
  13611. static void i915_disable_vga(struct drm_device *dev)
  13612. {
  13613. struct drm_i915_private *dev_priv = to_i915(dev);
  13614. struct pci_dev *pdev = dev_priv->drm.pdev;
  13615. u8 sr1;
  13616. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13617. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  13618. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  13619. outb(SR01, VGA_SR_INDEX);
  13620. sr1 = inb(VGA_SR_DATA);
  13621. outb(sr1 | 1<<5, VGA_SR_DATA);
  13622. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  13623. udelay(300);
  13624. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  13625. POSTING_READ(vga_reg);
  13626. }
  13627. void intel_modeset_init_hw(struct drm_device *dev)
  13628. {
  13629. struct drm_i915_private *dev_priv = to_i915(dev);
  13630. intel_update_cdclk(dev);
  13631. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13632. intel_init_clock_gating(dev);
  13633. }
  13634. /*
  13635. * Calculate what we think the watermarks should be for the state we've read
  13636. * out of the hardware and then immediately program those watermarks so that
  13637. * we ensure the hardware settings match our internal state.
  13638. *
  13639. * We can calculate what we think WM's should be by creating a duplicate of the
  13640. * current state (which was constructed during hardware readout) and running it
  13641. * through the atomic check code to calculate new watermark values in the
  13642. * state object.
  13643. */
  13644. static void sanitize_watermarks(struct drm_device *dev)
  13645. {
  13646. struct drm_i915_private *dev_priv = to_i915(dev);
  13647. struct drm_atomic_state *state;
  13648. struct drm_crtc *crtc;
  13649. struct drm_crtc_state *cstate;
  13650. struct drm_modeset_acquire_ctx ctx;
  13651. int ret;
  13652. int i;
  13653. /* Only supported on platforms that use atomic watermark design */
  13654. if (!dev_priv->display.optimize_watermarks)
  13655. return;
  13656. /*
  13657. * We need to hold connection_mutex before calling duplicate_state so
  13658. * that the connector loop is protected.
  13659. */
  13660. drm_modeset_acquire_init(&ctx, 0);
  13661. retry:
  13662. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13663. if (ret == -EDEADLK) {
  13664. drm_modeset_backoff(&ctx);
  13665. goto retry;
  13666. } else if (WARN_ON(ret)) {
  13667. goto fail;
  13668. }
  13669. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13670. if (WARN_ON(IS_ERR(state)))
  13671. goto fail;
  13672. /*
  13673. * Hardware readout is the only time we don't want to calculate
  13674. * intermediate watermarks (since we don't trust the current
  13675. * watermarks).
  13676. */
  13677. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13678. ret = intel_atomic_check(dev, state);
  13679. if (ret) {
  13680. /*
  13681. * If we fail here, it means that the hardware appears to be
  13682. * programmed in a way that shouldn't be possible, given our
  13683. * understanding of watermark requirements. This might mean a
  13684. * mistake in the hardware readout code or a mistake in the
  13685. * watermark calculations for a given platform. Raise a WARN
  13686. * so that this is noticeable.
  13687. *
  13688. * If this actually happens, we'll have to just leave the
  13689. * BIOS-programmed watermarks untouched and hope for the best.
  13690. */
  13691. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13692. goto fail;
  13693. }
  13694. /* Write calculated watermark values back */
  13695. for_each_crtc_in_state(state, crtc, cstate, i) {
  13696. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13697. cs->wm.need_postvbl_update = true;
  13698. dev_priv->display.optimize_watermarks(cs);
  13699. }
  13700. drm_atomic_state_free(state);
  13701. fail:
  13702. drm_modeset_drop_locks(&ctx);
  13703. drm_modeset_acquire_fini(&ctx);
  13704. }
  13705. void intel_modeset_init(struct drm_device *dev)
  13706. {
  13707. struct drm_i915_private *dev_priv = to_i915(dev);
  13708. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13709. int sprite, ret;
  13710. enum pipe pipe;
  13711. struct intel_crtc *crtc;
  13712. drm_mode_config_init(dev);
  13713. dev->mode_config.min_width = 0;
  13714. dev->mode_config.min_height = 0;
  13715. dev->mode_config.preferred_depth = 24;
  13716. dev->mode_config.prefer_shadow = 1;
  13717. dev->mode_config.allow_fb_modifiers = true;
  13718. dev->mode_config.funcs = &intel_mode_funcs;
  13719. intel_init_quirks(dev);
  13720. intel_init_pm(dev);
  13721. if (INTEL_INFO(dev)->num_pipes == 0)
  13722. return;
  13723. /*
  13724. * There may be no VBT; and if the BIOS enabled SSC we can
  13725. * just keep using it to avoid unnecessary flicker. Whereas if the
  13726. * BIOS isn't using it, don't assume it will work even if the VBT
  13727. * indicates as much.
  13728. */
  13729. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  13730. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13731. DREF_SSC1_ENABLE);
  13732. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13733. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13734. bios_lvds_use_ssc ? "en" : "dis",
  13735. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13736. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13737. }
  13738. }
  13739. if (IS_GEN2(dev)) {
  13740. dev->mode_config.max_width = 2048;
  13741. dev->mode_config.max_height = 2048;
  13742. } else if (IS_GEN3(dev)) {
  13743. dev->mode_config.max_width = 4096;
  13744. dev->mode_config.max_height = 4096;
  13745. } else {
  13746. dev->mode_config.max_width = 8192;
  13747. dev->mode_config.max_height = 8192;
  13748. }
  13749. if (IS_845G(dev) || IS_I865G(dev)) {
  13750. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  13751. dev->mode_config.cursor_height = 1023;
  13752. } else if (IS_GEN2(dev)) {
  13753. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13754. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13755. } else {
  13756. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13757. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13758. }
  13759. dev->mode_config.fb_base = ggtt->mappable_base;
  13760. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13761. INTEL_INFO(dev)->num_pipes,
  13762. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  13763. for_each_pipe(dev_priv, pipe) {
  13764. intel_crtc_init(dev, pipe);
  13765. for_each_sprite(dev_priv, pipe, sprite) {
  13766. ret = intel_plane_init(dev, pipe, sprite);
  13767. if (ret)
  13768. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  13769. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  13770. }
  13771. }
  13772. intel_update_czclk(dev_priv);
  13773. intel_update_cdclk(dev);
  13774. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13775. intel_shared_dpll_init(dev);
  13776. if (dev_priv->max_cdclk_freq == 0)
  13777. intel_update_max_cdclk(dev);
  13778. /* Just disable it once at startup */
  13779. i915_disable_vga(dev);
  13780. intel_setup_outputs(dev);
  13781. drm_modeset_lock_all(dev);
  13782. intel_modeset_setup_hw_state(dev);
  13783. drm_modeset_unlock_all(dev);
  13784. for_each_intel_crtc(dev, crtc) {
  13785. struct intel_initial_plane_config plane_config = {};
  13786. if (!crtc->active)
  13787. continue;
  13788. /*
  13789. * Note that reserving the BIOS fb up front prevents us
  13790. * from stuffing other stolen allocations like the ring
  13791. * on top. This prevents some ugliness at boot time, and
  13792. * can even allow for smooth boot transitions if the BIOS
  13793. * fb is large enough for the active pipe configuration.
  13794. */
  13795. dev_priv->display.get_initial_plane_config(crtc,
  13796. &plane_config);
  13797. /*
  13798. * If the fb is shared between multiple heads, we'll
  13799. * just get the first one.
  13800. */
  13801. intel_find_initial_plane_obj(crtc, &plane_config);
  13802. }
  13803. /*
  13804. * Make sure hardware watermarks really match the state we read out.
  13805. * Note that we need to do this after reconstructing the BIOS fb's
  13806. * since the watermark calculation done here will use pstate->fb.
  13807. */
  13808. sanitize_watermarks(dev);
  13809. }
  13810. static void intel_enable_pipe_a(struct drm_device *dev)
  13811. {
  13812. struct intel_connector *connector;
  13813. struct drm_connector *crt = NULL;
  13814. struct intel_load_detect_pipe load_detect_temp;
  13815. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13816. /* We can't just switch on the pipe A, we need to set things up with a
  13817. * proper mode and output configuration. As a gross hack, enable pipe A
  13818. * by enabling the load detect pipe once. */
  13819. for_each_intel_connector(dev, connector) {
  13820. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  13821. crt = &connector->base;
  13822. break;
  13823. }
  13824. }
  13825. if (!crt)
  13826. return;
  13827. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  13828. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  13829. }
  13830. static bool
  13831. intel_check_plane_mapping(struct intel_crtc *crtc)
  13832. {
  13833. struct drm_device *dev = crtc->base.dev;
  13834. struct drm_i915_private *dev_priv = to_i915(dev);
  13835. u32 val;
  13836. if (INTEL_INFO(dev)->num_pipes == 1)
  13837. return true;
  13838. val = I915_READ(DSPCNTR(!crtc->plane));
  13839. if ((val & DISPLAY_PLANE_ENABLE) &&
  13840. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13841. return false;
  13842. return true;
  13843. }
  13844. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13845. {
  13846. struct drm_device *dev = crtc->base.dev;
  13847. struct intel_encoder *encoder;
  13848. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13849. return true;
  13850. return false;
  13851. }
  13852. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  13853. {
  13854. struct drm_device *dev = encoder->base.dev;
  13855. struct intel_connector *connector;
  13856. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13857. return connector;
  13858. return NULL;
  13859. }
  13860. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  13861. enum transcoder pch_transcoder)
  13862. {
  13863. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  13864. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  13865. }
  13866. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13867. {
  13868. struct drm_device *dev = crtc->base.dev;
  13869. struct drm_i915_private *dev_priv = to_i915(dev);
  13870. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13871. /* Clear any frame start delays used for debugging left by the BIOS */
  13872. if (!transcoder_is_dsi(cpu_transcoder)) {
  13873. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13874. I915_WRITE(reg,
  13875. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13876. }
  13877. /* restore vblank interrupts to correct state */
  13878. drm_crtc_vblank_reset(&crtc->base);
  13879. if (crtc->active) {
  13880. struct intel_plane *plane;
  13881. drm_crtc_vblank_on(&crtc->base);
  13882. /* Disable everything but the primary plane */
  13883. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13884. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13885. continue;
  13886. plane->disable_plane(&plane->base, &crtc->base);
  13887. }
  13888. }
  13889. /* We need to sanitize the plane -> pipe mapping first because this will
  13890. * disable the crtc (and hence change the state) if it is wrong. Note
  13891. * that gen4+ has a fixed plane -> pipe mapping. */
  13892. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  13893. bool plane;
  13894. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  13895. crtc->base.base.id, crtc->base.name);
  13896. /* Pipe has the wrong plane attached and the plane is active.
  13897. * Temporarily change the plane mapping and disable everything
  13898. * ... */
  13899. plane = crtc->plane;
  13900. to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
  13901. crtc->plane = !plane;
  13902. intel_crtc_disable_noatomic(&crtc->base);
  13903. crtc->plane = plane;
  13904. }
  13905. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13906. crtc->pipe == PIPE_A && !crtc->active) {
  13907. /* BIOS forgot to enable pipe A, this mostly happens after
  13908. * resume. Force-enable the pipe to fix this, the update_dpms
  13909. * call below we restore the pipe to the right state, but leave
  13910. * the required bits on. */
  13911. intel_enable_pipe_a(dev);
  13912. }
  13913. /* Adjust the state of the output pipe according to whether we
  13914. * have active connectors/encoders. */
  13915. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13916. intel_crtc_disable_noatomic(&crtc->base);
  13917. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  13918. /*
  13919. * We start out with underrun reporting disabled to avoid races.
  13920. * For correct bookkeeping mark this on active crtcs.
  13921. *
  13922. * Also on gmch platforms we dont have any hardware bits to
  13923. * disable the underrun reporting. Which means we need to start
  13924. * out with underrun reporting disabled also on inactive pipes,
  13925. * since otherwise we'll complain about the garbage we read when
  13926. * e.g. coming up after runtime pm.
  13927. *
  13928. * No protection against concurrent access is required - at
  13929. * worst a fifo underrun happens which also sets this to false.
  13930. */
  13931. crtc->cpu_fifo_underrun_disabled = true;
  13932. /*
  13933. * We track the PCH trancoder underrun reporting state
  13934. * within the crtc. With crtc for pipe A housing the underrun
  13935. * reporting state for PCH transcoder A, crtc for pipe B housing
  13936. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  13937. * and marking underrun reporting as disabled for the non-existing
  13938. * PCH transcoders B and C would prevent enabling the south
  13939. * error interrupt (see cpt_can_enable_serr_int()).
  13940. */
  13941. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  13942. crtc->pch_fifo_underrun_disabled = true;
  13943. }
  13944. }
  13945. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13946. {
  13947. struct intel_connector *connector;
  13948. /* We need to check both for a crtc link (meaning that the
  13949. * encoder is active and trying to read from a pipe) and the
  13950. * pipe itself being active. */
  13951. bool has_active_crtc = encoder->base.crtc &&
  13952. to_intel_crtc(encoder->base.crtc)->active;
  13953. connector = intel_encoder_find_connector(encoder);
  13954. if (connector && !has_active_crtc) {
  13955. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13956. encoder->base.base.id,
  13957. encoder->base.name);
  13958. /* Connector is active, but has no active pipe. This is
  13959. * fallout from our resume register restoring. Disable
  13960. * the encoder manually again. */
  13961. if (encoder->base.crtc) {
  13962. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  13963. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13964. encoder->base.base.id,
  13965. encoder->base.name);
  13966. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  13967. if (encoder->post_disable)
  13968. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  13969. }
  13970. encoder->base.crtc = NULL;
  13971. /* Inconsistent output/port/pipe state happens presumably due to
  13972. * a bug in one of the get_hw_state functions. Or someplace else
  13973. * in our code, like the register restore mess on resume. Clamp
  13974. * things to off as a safer default. */
  13975. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13976. connector->base.encoder = NULL;
  13977. }
  13978. /* Enabled encoders without active connectors will be fixed in
  13979. * the crtc fixup. */
  13980. }
  13981. void i915_redisable_vga_power_on(struct drm_device *dev)
  13982. {
  13983. struct drm_i915_private *dev_priv = to_i915(dev);
  13984. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13985. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13986. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13987. i915_disable_vga(dev);
  13988. }
  13989. }
  13990. void i915_redisable_vga(struct drm_device *dev)
  13991. {
  13992. struct drm_i915_private *dev_priv = to_i915(dev);
  13993. /* This function can be called both from intel_modeset_setup_hw_state or
  13994. * at a very early point in our resume sequence, where the power well
  13995. * structures are not yet restored. Since this function is at a very
  13996. * paranoid "someone might have enabled VGA while we were not looking"
  13997. * level, just check if the power well is enabled instead of trying to
  13998. * follow the "don't touch the power well if we don't need it" policy
  13999. * the rest of the driver uses. */
  14000. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  14001. return;
  14002. i915_redisable_vga_power_on(dev);
  14003. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  14004. }
  14005. static bool primary_get_hw_state(struct intel_plane *plane)
  14006. {
  14007. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  14008. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  14009. }
  14010. /* FIXME read out full plane state for all planes */
  14011. static void readout_plane_state(struct intel_crtc *crtc)
  14012. {
  14013. struct drm_plane *primary = crtc->base.primary;
  14014. struct intel_plane_state *plane_state =
  14015. to_intel_plane_state(primary->state);
  14016. plane_state->base.visible = crtc->active &&
  14017. primary_get_hw_state(to_intel_plane(primary));
  14018. if (plane_state->base.visible)
  14019. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  14020. }
  14021. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  14022. {
  14023. struct drm_i915_private *dev_priv = to_i915(dev);
  14024. enum pipe pipe;
  14025. struct intel_crtc *crtc;
  14026. struct intel_encoder *encoder;
  14027. struct intel_connector *connector;
  14028. int i;
  14029. dev_priv->active_crtcs = 0;
  14030. for_each_intel_crtc(dev, crtc) {
  14031. struct intel_crtc_state *crtc_state = crtc->config;
  14032. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  14033. memset(crtc_state, 0, sizeof(*crtc_state));
  14034. crtc_state->base.crtc = &crtc->base;
  14035. crtc_state->base.active = crtc_state->base.enable =
  14036. dev_priv->display.get_pipe_config(crtc, crtc_state);
  14037. crtc->base.enabled = crtc_state->base.enable;
  14038. crtc->active = crtc_state->base.active;
  14039. if (crtc_state->base.active)
  14040. dev_priv->active_crtcs |= 1 << crtc->pipe;
  14041. readout_plane_state(crtc);
  14042. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  14043. crtc->base.base.id, crtc->base.name,
  14044. crtc->active ? "enabled" : "disabled");
  14045. }
  14046. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14047. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14048. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  14049. &pll->config.hw_state);
  14050. pll->config.crtc_mask = 0;
  14051. for_each_intel_crtc(dev, crtc) {
  14052. if (crtc->active && crtc->config->shared_dpll == pll)
  14053. pll->config.crtc_mask |= 1 << crtc->pipe;
  14054. }
  14055. pll->active_mask = pll->config.crtc_mask;
  14056. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  14057. pll->name, pll->config.crtc_mask, pll->on);
  14058. }
  14059. for_each_intel_encoder(dev, encoder) {
  14060. pipe = 0;
  14061. if (encoder->get_hw_state(encoder, &pipe)) {
  14062. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  14063. encoder->base.crtc = &crtc->base;
  14064. crtc->config->output_types |= 1 << encoder->type;
  14065. encoder->get_config(encoder, crtc->config);
  14066. } else {
  14067. encoder->base.crtc = NULL;
  14068. }
  14069. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  14070. encoder->base.base.id,
  14071. encoder->base.name,
  14072. encoder->base.crtc ? "enabled" : "disabled",
  14073. pipe_name(pipe));
  14074. }
  14075. for_each_intel_connector(dev, connector) {
  14076. if (connector->get_hw_state(connector)) {
  14077. connector->base.dpms = DRM_MODE_DPMS_ON;
  14078. encoder = connector->encoder;
  14079. connector->base.encoder = &encoder->base;
  14080. if (encoder->base.crtc &&
  14081. encoder->base.crtc->state->active) {
  14082. /*
  14083. * This has to be done during hardware readout
  14084. * because anything calling .crtc_disable may
  14085. * rely on the connector_mask being accurate.
  14086. */
  14087. encoder->base.crtc->state->connector_mask |=
  14088. 1 << drm_connector_index(&connector->base);
  14089. encoder->base.crtc->state->encoder_mask |=
  14090. 1 << drm_encoder_index(&encoder->base);
  14091. }
  14092. } else {
  14093. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14094. connector->base.encoder = NULL;
  14095. }
  14096. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  14097. connector->base.base.id,
  14098. connector->base.name,
  14099. connector->base.encoder ? "enabled" : "disabled");
  14100. }
  14101. for_each_intel_crtc(dev, crtc) {
  14102. int pixclk = 0;
  14103. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  14104. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  14105. if (crtc->base.state->active) {
  14106. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  14107. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  14108. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  14109. /*
  14110. * The initial mode needs to be set in order to keep
  14111. * the atomic core happy. It wants a valid mode if the
  14112. * crtc's enabled, so we do the above call.
  14113. *
  14114. * At this point some state updated by the connectors
  14115. * in their ->detect() callback has not run yet, so
  14116. * no recalculation can be done yet.
  14117. *
  14118. * Even if we could do a recalculation and modeset
  14119. * right now it would cause a double modeset if
  14120. * fbdev or userspace chooses a different initial mode.
  14121. *
  14122. * If that happens, someone indicated they wanted a
  14123. * mode change, which means it's safe to do a full
  14124. * recalculation.
  14125. */
  14126. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  14127. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  14128. pixclk = ilk_pipe_pixel_rate(crtc->config);
  14129. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14130. pixclk = crtc->config->base.adjusted_mode.crtc_clock;
  14131. else
  14132. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  14133. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  14134. if (IS_BROADWELL(dev_priv) && crtc->config->ips_enabled)
  14135. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  14136. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  14137. update_scanline_offset(crtc);
  14138. }
  14139. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  14140. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  14141. }
  14142. }
  14143. /* Scan out the current hw modeset state,
  14144. * and sanitizes it to the current state
  14145. */
  14146. static void
  14147. intel_modeset_setup_hw_state(struct drm_device *dev)
  14148. {
  14149. struct drm_i915_private *dev_priv = to_i915(dev);
  14150. enum pipe pipe;
  14151. struct intel_crtc *crtc;
  14152. struct intel_encoder *encoder;
  14153. int i;
  14154. intel_modeset_readout_hw_state(dev);
  14155. /* HW state is read out, now we need to sanitize this mess. */
  14156. for_each_intel_encoder(dev, encoder) {
  14157. intel_sanitize_encoder(encoder);
  14158. }
  14159. for_each_pipe(dev_priv, pipe) {
  14160. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  14161. intel_sanitize_crtc(crtc);
  14162. intel_dump_pipe_config(crtc, crtc->config,
  14163. "[setup_hw_state]");
  14164. }
  14165. intel_modeset_update_connector_atomic_state(dev);
  14166. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14167. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14168. if (!pll->on || pll->active_mask)
  14169. continue;
  14170. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  14171. pll->funcs.disable(dev_priv, pll);
  14172. pll->on = false;
  14173. }
  14174. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  14175. vlv_wm_get_hw_state(dev);
  14176. else if (IS_GEN9(dev))
  14177. skl_wm_get_hw_state(dev);
  14178. else if (HAS_PCH_SPLIT(dev))
  14179. ilk_wm_get_hw_state(dev);
  14180. for_each_intel_crtc(dev, crtc) {
  14181. unsigned long put_domains;
  14182. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  14183. if (WARN_ON(put_domains))
  14184. modeset_put_power_domains(dev_priv, put_domains);
  14185. }
  14186. intel_display_set_init_power(dev_priv, false);
  14187. intel_fbc_init_pipe_state(dev_priv);
  14188. }
  14189. void intel_display_resume(struct drm_device *dev)
  14190. {
  14191. struct drm_i915_private *dev_priv = to_i915(dev);
  14192. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  14193. struct drm_modeset_acquire_ctx ctx;
  14194. int ret;
  14195. dev_priv->modeset_restore_state = NULL;
  14196. if (state)
  14197. state->acquire_ctx = &ctx;
  14198. /*
  14199. * This is a cludge because with real atomic modeset mode_config.mutex
  14200. * won't be taken. Unfortunately some probed state like
  14201. * audio_codec_enable is still protected by mode_config.mutex, so lock
  14202. * it here for now.
  14203. */
  14204. mutex_lock(&dev->mode_config.mutex);
  14205. drm_modeset_acquire_init(&ctx, 0);
  14206. while (1) {
  14207. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  14208. if (ret != -EDEADLK)
  14209. break;
  14210. drm_modeset_backoff(&ctx);
  14211. }
  14212. if (!ret)
  14213. ret = __intel_display_resume(dev, state);
  14214. drm_modeset_drop_locks(&ctx);
  14215. drm_modeset_acquire_fini(&ctx);
  14216. mutex_unlock(&dev->mode_config.mutex);
  14217. if (ret) {
  14218. DRM_ERROR("Restoring old state failed with %i\n", ret);
  14219. drm_atomic_state_free(state);
  14220. }
  14221. }
  14222. void intel_modeset_gem_init(struct drm_device *dev)
  14223. {
  14224. struct drm_i915_private *dev_priv = to_i915(dev);
  14225. struct drm_crtc *c;
  14226. struct drm_i915_gem_object *obj;
  14227. intel_init_gt_powersave(dev_priv);
  14228. intel_modeset_init_hw(dev);
  14229. intel_setup_overlay(dev_priv);
  14230. /*
  14231. * Make sure any fbs we allocated at startup are properly
  14232. * pinned & fenced. When we do the allocation it's too early
  14233. * for this.
  14234. */
  14235. for_each_crtc(dev, c) {
  14236. struct i915_vma *vma;
  14237. obj = intel_fb_obj(c->primary->fb);
  14238. if (obj == NULL)
  14239. continue;
  14240. mutex_lock(&dev->struct_mutex);
  14241. vma = intel_pin_and_fence_fb_obj(c->primary->fb,
  14242. c->primary->state->rotation);
  14243. mutex_unlock(&dev->struct_mutex);
  14244. if (IS_ERR(vma)) {
  14245. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  14246. to_intel_crtc(c)->pipe);
  14247. drm_framebuffer_unreference(c->primary->fb);
  14248. c->primary->fb = NULL;
  14249. c->primary->crtc = c->primary->state->crtc = NULL;
  14250. update_state_fb(c->primary);
  14251. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  14252. }
  14253. }
  14254. }
  14255. int intel_connector_register(struct drm_connector *connector)
  14256. {
  14257. struct intel_connector *intel_connector = to_intel_connector(connector);
  14258. int ret;
  14259. ret = intel_backlight_device_register(intel_connector);
  14260. if (ret)
  14261. goto err;
  14262. return 0;
  14263. err:
  14264. return ret;
  14265. }
  14266. void intel_connector_unregister(struct drm_connector *connector)
  14267. {
  14268. struct intel_connector *intel_connector = to_intel_connector(connector);
  14269. intel_backlight_device_unregister(intel_connector);
  14270. intel_panel_destroy_backlight(connector);
  14271. }
  14272. void intel_modeset_cleanup(struct drm_device *dev)
  14273. {
  14274. struct drm_i915_private *dev_priv = to_i915(dev);
  14275. intel_disable_gt_powersave(dev_priv);
  14276. /*
  14277. * Interrupts and polling as the first thing to avoid creating havoc.
  14278. * Too much stuff here (turning of connectors, ...) would
  14279. * experience fancy races otherwise.
  14280. */
  14281. intel_irq_uninstall(dev_priv);
  14282. /*
  14283. * Due to the hpd irq storm handling the hotplug work can re-arm the
  14284. * poll handlers. Hence disable polling after hpd handling is shut down.
  14285. */
  14286. drm_kms_helper_poll_fini(dev);
  14287. intel_unregister_dsm_handler();
  14288. intel_fbc_global_disable(dev_priv);
  14289. /* flush any delayed tasks or pending work */
  14290. flush_scheduled_work();
  14291. drm_mode_config_cleanup(dev);
  14292. intel_cleanup_overlay(dev_priv);
  14293. intel_cleanup_gt_powersave(dev_priv);
  14294. intel_teardown_gmbus(dev);
  14295. }
  14296. void intel_connector_attach_encoder(struct intel_connector *connector,
  14297. struct intel_encoder *encoder)
  14298. {
  14299. connector->encoder = encoder;
  14300. drm_mode_connector_attach_encoder(&connector->base,
  14301. &encoder->base);
  14302. }
  14303. /*
  14304. * set vga decode state - true == enable VGA decode
  14305. */
  14306. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  14307. {
  14308. struct drm_i915_private *dev_priv = to_i915(dev);
  14309. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  14310. u16 gmch_ctrl;
  14311. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  14312. DRM_ERROR("failed to read control word\n");
  14313. return -EIO;
  14314. }
  14315. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  14316. return 0;
  14317. if (state)
  14318. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  14319. else
  14320. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  14321. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  14322. DRM_ERROR("failed to write control word\n");
  14323. return -EIO;
  14324. }
  14325. return 0;
  14326. }
  14327. struct intel_display_error_state {
  14328. u32 power_well_driver;
  14329. int num_transcoders;
  14330. struct intel_cursor_error_state {
  14331. u32 control;
  14332. u32 position;
  14333. u32 base;
  14334. u32 size;
  14335. } cursor[I915_MAX_PIPES];
  14336. struct intel_pipe_error_state {
  14337. bool power_domain_on;
  14338. u32 source;
  14339. u32 stat;
  14340. } pipe[I915_MAX_PIPES];
  14341. struct intel_plane_error_state {
  14342. u32 control;
  14343. u32 stride;
  14344. u32 size;
  14345. u32 pos;
  14346. u32 addr;
  14347. u32 surface;
  14348. u32 tile_offset;
  14349. } plane[I915_MAX_PIPES];
  14350. struct intel_transcoder_error_state {
  14351. bool power_domain_on;
  14352. enum transcoder cpu_transcoder;
  14353. u32 conf;
  14354. u32 htotal;
  14355. u32 hblank;
  14356. u32 hsync;
  14357. u32 vtotal;
  14358. u32 vblank;
  14359. u32 vsync;
  14360. } transcoder[4];
  14361. };
  14362. struct intel_display_error_state *
  14363. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  14364. {
  14365. struct intel_display_error_state *error;
  14366. int transcoders[] = {
  14367. TRANSCODER_A,
  14368. TRANSCODER_B,
  14369. TRANSCODER_C,
  14370. TRANSCODER_EDP,
  14371. };
  14372. int i;
  14373. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  14374. return NULL;
  14375. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  14376. if (error == NULL)
  14377. return NULL;
  14378. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14379. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  14380. for_each_pipe(dev_priv, i) {
  14381. error->pipe[i].power_domain_on =
  14382. __intel_display_power_is_enabled(dev_priv,
  14383. POWER_DOMAIN_PIPE(i));
  14384. if (!error->pipe[i].power_domain_on)
  14385. continue;
  14386. error->cursor[i].control = I915_READ(CURCNTR(i));
  14387. error->cursor[i].position = I915_READ(CURPOS(i));
  14388. error->cursor[i].base = I915_READ(CURBASE(i));
  14389. error->plane[i].control = I915_READ(DSPCNTR(i));
  14390. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  14391. if (INTEL_GEN(dev_priv) <= 3) {
  14392. error->plane[i].size = I915_READ(DSPSIZE(i));
  14393. error->plane[i].pos = I915_READ(DSPPOS(i));
  14394. }
  14395. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14396. error->plane[i].addr = I915_READ(DSPADDR(i));
  14397. if (INTEL_GEN(dev_priv) >= 4) {
  14398. error->plane[i].surface = I915_READ(DSPSURF(i));
  14399. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  14400. }
  14401. error->pipe[i].source = I915_READ(PIPESRC(i));
  14402. if (HAS_GMCH_DISPLAY(dev_priv))
  14403. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  14404. }
  14405. /* Note: this does not include DSI transcoders. */
  14406. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  14407. if (HAS_DDI(dev_priv))
  14408. error->num_transcoders++; /* Account for eDP. */
  14409. for (i = 0; i < error->num_transcoders; i++) {
  14410. enum transcoder cpu_transcoder = transcoders[i];
  14411. error->transcoder[i].power_domain_on =
  14412. __intel_display_power_is_enabled(dev_priv,
  14413. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  14414. if (!error->transcoder[i].power_domain_on)
  14415. continue;
  14416. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  14417. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  14418. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  14419. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  14420. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  14421. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  14422. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  14423. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  14424. }
  14425. return error;
  14426. }
  14427. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  14428. void
  14429. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  14430. struct drm_device *dev,
  14431. struct intel_display_error_state *error)
  14432. {
  14433. struct drm_i915_private *dev_priv = to_i915(dev);
  14434. int i;
  14435. if (!error)
  14436. return;
  14437. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  14438. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  14439. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  14440. error->power_well_driver);
  14441. for_each_pipe(dev_priv, i) {
  14442. err_printf(m, "Pipe [%d]:\n", i);
  14443. err_printf(m, " Power: %s\n",
  14444. onoff(error->pipe[i].power_domain_on));
  14445. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  14446. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  14447. err_printf(m, "Plane [%d]:\n", i);
  14448. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  14449. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  14450. if (INTEL_INFO(dev)->gen <= 3) {
  14451. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  14452. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  14453. }
  14454. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  14455. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  14456. if (INTEL_INFO(dev)->gen >= 4) {
  14457. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  14458. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  14459. }
  14460. err_printf(m, "Cursor [%d]:\n", i);
  14461. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  14462. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  14463. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  14464. }
  14465. for (i = 0; i < error->num_transcoders; i++) {
  14466. err_printf(m, "CPU transcoder: %s\n",
  14467. transcoder_name(error->transcoder[i].cpu_transcoder));
  14468. err_printf(m, " Power: %s\n",
  14469. onoff(error->transcoder[i].power_domain_on));
  14470. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  14471. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  14472. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  14473. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  14474. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  14475. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  14476. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  14477. }
  14478. }