intel_display.c 452 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_atomic.h>
  40. #include <drm/drm_atomic_helper.h>
  41. #include <drm/drm_dp_helper.h>
  42. #include <drm/drm_crtc_helper.h>
  43. #include <drm/drm_plane_helper.h>
  44. #include <drm/drm_rect.h>
  45. #include <linux/dma_remapping.h>
  46. #include <linux/reservation.h>
  47. #include <linux/dma-buf.h>
  48. /* Primary plane formats for gen <= 3 */
  49. static const uint32_t i8xx_primary_formats[] = {
  50. DRM_FORMAT_C8,
  51. DRM_FORMAT_RGB565,
  52. DRM_FORMAT_XRGB1555,
  53. DRM_FORMAT_XRGB8888,
  54. };
  55. /* Primary plane formats for gen >= 4 */
  56. static const uint32_t i965_primary_formats[] = {
  57. DRM_FORMAT_C8,
  58. DRM_FORMAT_RGB565,
  59. DRM_FORMAT_XRGB8888,
  60. DRM_FORMAT_XBGR8888,
  61. DRM_FORMAT_XRGB2101010,
  62. DRM_FORMAT_XBGR2101010,
  63. };
  64. static const uint32_t skl_primary_formats[] = {
  65. DRM_FORMAT_C8,
  66. DRM_FORMAT_RGB565,
  67. DRM_FORMAT_XRGB8888,
  68. DRM_FORMAT_XBGR8888,
  69. DRM_FORMAT_ARGB8888,
  70. DRM_FORMAT_ABGR8888,
  71. DRM_FORMAT_XRGB2101010,
  72. DRM_FORMAT_XBGR2101010,
  73. DRM_FORMAT_YUYV,
  74. DRM_FORMAT_YVYU,
  75. DRM_FORMAT_UYVY,
  76. DRM_FORMAT_VYUY,
  77. };
  78. /* Cursor formats */
  79. static const uint32_t intel_cursor_formats[] = {
  80. DRM_FORMAT_ARGB8888,
  81. };
  82. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  83. struct intel_crtc_state *pipe_config);
  84. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  85. struct intel_crtc_state *pipe_config);
  86. static int intel_framebuffer_init(struct drm_device *dev,
  87. struct intel_framebuffer *ifb,
  88. struct drm_mode_fb_cmd2 *mode_cmd,
  89. struct drm_i915_gem_object *obj);
  90. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  91. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  92. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  93. struct intel_link_m_n *m_n,
  94. struct intel_link_m_n *m2_n2);
  95. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  96. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  97. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  98. static void vlv_prepare_pll(struct intel_crtc *crtc,
  99. const struct intel_crtc_state *pipe_config);
  100. static void chv_prepare_pll(struct intel_crtc *crtc,
  101. const struct intel_crtc_state *pipe_config);
  102. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  103. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  104. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  105. struct intel_crtc_state *crtc_state);
  106. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  107. int num_connectors);
  108. static void skylake_pfit_enable(struct intel_crtc *crtc);
  109. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  110. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  111. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  112. static void intel_pre_disable_primary(struct drm_crtc *crtc);
  113. typedef struct {
  114. int min, max;
  115. } intel_range_t;
  116. typedef struct {
  117. int dot_limit;
  118. int p2_slow, p2_fast;
  119. } intel_p2_t;
  120. typedef struct intel_limit intel_limit_t;
  121. struct intel_limit {
  122. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  123. intel_p2_t p2;
  124. };
  125. /* returns HPLL frequency in kHz */
  126. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  127. {
  128. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  129. /* Obtain SKU information */
  130. mutex_lock(&dev_priv->sb_lock);
  131. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  132. CCK_FUSE_HPLL_FREQ_MASK;
  133. mutex_unlock(&dev_priv->sb_lock);
  134. return vco_freq[hpll_freq] * 1000;
  135. }
  136. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  137. const char *name, u32 reg)
  138. {
  139. u32 val;
  140. int divider;
  141. if (dev_priv->hpll_freq == 0)
  142. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  143. mutex_lock(&dev_priv->sb_lock);
  144. val = vlv_cck_read(dev_priv, reg);
  145. mutex_unlock(&dev_priv->sb_lock);
  146. divider = val & CCK_FREQUENCY_VALUES;
  147. WARN((val & CCK_FREQUENCY_STATUS) !=
  148. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  149. "%s change in progress\n", name);
  150. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  151. }
  152. int
  153. intel_pch_rawclk(struct drm_device *dev)
  154. {
  155. struct drm_i915_private *dev_priv = dev->dev_private;
  156. WARN_ON(!HAS_PCH_SPLIT(dev));
  157. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  158. }
  159. /* hrawclock is 1/4 the FSB frequency */
  160. int intel_hrawclk(struct drm_device *dev)
  161. {
  162. struct drm_i915_private *dev_priv = dev->dev_private;
  163. uint32_t clkcfg;
  164. /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
  165. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  166. return 200;
  167. clkcfg = I915_READ(CLKCFG);
  168. switch (clkcfg & CLKCFG_FSB_MASK) {
  169. case CLKCFG_FSB_400:
  170. return 100;
  171. case CLKCFG_FSB_533:
  172. return 133;
  173. case CLKCFG_FSB_667:
  174. return 166;
  175. case CLKCFG_FSB_800:
  176. return 200;
  177. case CLKCFG_FSB_1067:
  178. return 266;
  179. case CLKCFG_FSB_1333:
  180. return 333;
  181. /* these two are just a guess; one of them might be right */
  182. case CLKCFG_FSB_1600:
  183. case CLKCFG_FSB_1600_ALT:
  184. return 400;
  185. default:
  186. return 133;
  187. }
  188. }
  189. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  190. {
  191. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  192. return;
  193. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  194. CCK_CZ_CLOCK_CONTROL);
  195. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  196. }
  197. static inline u32 /* units of 100MHz */
  198. intel_fdi_link_freq(struct drm_device *dev)
  199. {
  200. if (IS_GEN5(dev)) {
  201. struct drm_i915_private *dev_priv = dev->dev_private;
  202. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  203. } else
  204. return 27;
  205. }
  206. static const intel_limit_t intel_limits_i8xx_dac = {
  207. .dot = { .min = 25000, .max = 350000 },
  208. .vco = { .min = 908000, .max = 1512000 },
  209. .n = { .min = 2, .max = 16 },
  210. .m = { .min = 96, .max = 140 },
  211. .m1 = { .min = 18, .max = 26 },
  212. .m2 = { .min = 6, .max = 16 },
  213. .p = { .min = 4, .max = 128 },
  214. .p1 = { .min = 2, .max = 33 },
  215. .p2 = { .dot_limit = 165000,
  216. .p2_slow = 4, .p2_fast = 2 },
  217. };
  218. static const intel_limit_t intel_limits_i8xx_dvo = {
  219. .dot = { .min = 25000, .max = 350000 },
  220. .vco = { .min = 908000, .max = 1512000 },
  221. .n = { .min = 2, .max = 16 },
  222. .m = { .min = 96, .max = 140 },
  223. .m1 = { .min = 18, .max = 26 },
  224. .m2 = { .min = 6, .max = 16 },
  225. .p = { .min = 4, .max = 128 },
  226. .p1 = { .min = 2, .max = 33 },
  227. .p2 = { .dot_limit = 165000,
  228. .p2_slow = 4, .p2_fast = 4 },
  229. };
  230. static const intel_limit_t intel_limits_i8xx_lvds = {
  231. .dot = { .min = 25000, .max = 350000 },
  232. .vco = { .min = 908000, .max = 1512000 },
  233. .n = { .min = 2, .max = 16 },
  234. .m = { .min = 96, .max = 140 },
  235. .m1 = { .min = 18, .max = 26 },
  236. .m2 = { .min = 6, .max = 16 },
  237. .p = { .min = 4, .max = 128 },
  238. .p1 = { .min = 1, .max = 6 },
  239. .p2 = { .dot_limit = 165000,
  240. .p2_slow = 14, .p2_fast = 7 },
  241. };
  242. static const intel_limit_t intel_limits_i9xx_sdvo = {
  243. .dot = { .min = 20000, .max = 400000 },
  244. .vco = { .min = 1400000, .max = 2800000 },
  245. .n = { .min = 1, .max = 6 },
  246. .m = { .min = 70, .max = 120 },
  247. .m1 = { .min = 8, .max = 18 },
  248. .m2 = { .min = 3, .max = 7 },
  249. .p = { .min = 5, .max = 80 },
  250. .p1 = { .min = 1, .max = 8 },
  251. .p2 = { .dot_limit = 200000,
  252. .p2_slow = 10, .p2_fast = 5 },
  253. };
  254. static const intel_limit_t intel_limits_i9xx_lvds = {
  255. .dot = { .min = 20000, .max = 400000 },
  256. .vco = { .min = 1400000, .max = 2800000 },
  257. .n = { .min = 1, .max = 6 },
  258. .m = { .min = 70, .max = 120 },
  259. .m1 = { .min = 8, .max = 18 },
  260. .m2 = { .min = 3, .max = 7 },
  261. .p = { .min = 7, .max = 98 },
  262. .p1 = { .min = 1, .max = 8 },
  263. .p2 = { .dot_limit = 112000,
  264. .p2_slow = 14, .p2_fast = 7 },
  265. };
  266. static const intel_limit_t intel_limits_g4x_sdvo = {
  267. .dot = { .min = 25000, .max = 270000 },
  268. .vco = { .min = 1750000, .max = 3500000},
  269. .n = { .min = 1, .max = 4 },
  270. .m = { .min = 104, .max = 138 },
  271. .m1 = { .min = 17, .max = 23 },
  272. .m2 = { .min = 5, .max = 11 },
  273. .p = { .min = 10, .max = 30 },
  274. .p1 = { .min = 1, .max = 3},
  275. .p2 = { .dot_limit = 270000,
  276. .p2_slow = 10,
  277. .p2_fast = 10
  278. },
  279. };
  280. static const intel_limit_t intel_limits_g4x_hdmi = {
  281. .dot = { .min = 22000, .max = 400000 },
  282. .vco = { .min = 1750000, .max = 3500000},
  283. .n = { .min = 1, .max = 4 },
  284. .m = { .min = 104, .max = 138 },
  285. .m1 = { .min = 16, .max = 23 },
  286. .m2 = { .min = 5, .max = 11 },
  287. .p = { .min = 5, .max = 80 },
  288. .p1 = { .min = 1, .max = 8},
  289. .p2 = { .dot_limit = 165000,
  290. .p2_slow = 10, .p2_fast = 5 },
  291. };
  292. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  293. .dot = { .min = 20000, .max = 115000 },
  294. .vco = { .min = 1750000, .max = 3500000 },
  295. .n = { .min = 1, .max = 3 },
  296. .m = { .min = 104, .max = 138 },
  297. .m1 = { .min = 17, .max = 23 },
  298. .m2 = { .min = 5, .max = 11 },
  299. .p = { .min = 28, .max = 112 },
  300. .p1 = { .min = 2, .max = 8 },
  301. .p2 = { .dot_limit = 0,
  302. .p2_slow = 14, .p2_fast = 14
  303. },
  304. };
  305. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  306. .dot = { .min = 80000, .max = 224000 },
  307. .vco = { .min = 1750000, .max = 3500000 },
  308. .n = { .min = 1, .max = 3 },
  309. .m = { .min = 104, .max = 138 },
  310. .m1 = { .min = 17, .max = 23 },
  311. .m2 = { .min = 5, .max = 11 },
  312. .p = { .min = 14, .max = 42 },
  313. .p1 = { .min = 2, .max = 6 },
  314. .p2 = { .dot_limit = 0,
  315. .p2_slow = 7, .p2_fast = 7
  316. },
  317. };
  318. static const intel_limit_t intel_limits_pineview_sdvo = {
  319. .dot = { .min = 20000, .max = 400000},
  320. .vco = { .min = 1700000, .max = 3500000 },
  321. /* Pineview's Ncounter is a ring counter */
  322. .n = { .min = 3, .max = 6 },
  323. .m = { .min = 2, .max = 256 },
  324. /* Pineview only has one combined m divider, which we treat as m2. */
  325. .m1 = { .min = 0, .max = 0 },
  326. .m2 = { .min = 0, .max = 254 },
  327. .p = { .min = 5, .max = 80 },
  328. .p1 = { .min = 1, .max = 8 },
  329. .p2 = { .dot_limit = 200000,
  330. .p2_slow = 10, .p2_fast = 5 },
  331. };
  332. static const intel_limit_t intel_limits_pineview_lvds = {
  333. .dot = { .min = 20000, .max = 400000 },
  334. .vco = { .min = 1700000, .max = 3500000 },
  335. .n = { .min = 3, .max = 6 },
  336. .m = { .min = 2, .max = 256 },
  337. .m1 = { .min = 0, .max = 0 },
  338. .m2 = { .min = 0, .max = 254 },
  339. .p = { .min = 7, .max = 112 },
  340. .p1 = { .min = 1, .max = 8 },
  341. .p2 = { .dot_limit = 112000,
  342. .p2_slow = 14, .p2_fast = 14 },
  343. };
  344. /* Ironlake / Sandybridge
  345. *
  346. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  347. * the range value for them is (actual_value - 2).
  348. */
  349. static const intel_limit_t intel_limits_ironlake_dac = {
  350. .dot = { .min = 25000, .max = 350000 },
  351. .vco = { .min = 1760000, .max = 3510000 },
  352. .n = { .min = 1, .max = 5 },
  353. .m = { .min = 79, .max = 127 },
  354. .m1 = { .min = 12, .max = 22 },
  355. .m2 = { .min = 5, .max = 9 },
  356. .p = { .min = 5, .max = 80 },
  357. .p1 = { .min = 1, .max = 8 },
  358. .p2 = { .dot_limit = 225000,
  359. .p2_slow = 10, .p2_fast = 5 },
  360. };
  361. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  362. .dot = { .min = 25000, .max = 350000 },
  363. .vco = { .min = 1760000, .max = 3510000 },
  364. .n = { .min = 1, .max = 3 },
  365. .m = { .min = 79, .max = 118 },
  366. .m1 = { .min = 12, .max = 22 },
  367. .m2 = { .min = 5, .max = 9 },
  368. .p = { .min = 28, .max = 112 },
  369. .p1 = { .min = 2, .max = 8 },
  370. .p2 = { .dot_limit = 225000,
  371. .p2_slow = 14, .p2_fast = 14 },
  372. };
  373. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  374. .dot = { .min = 25000, .max = 350000 },
  375. .vco = { .min = 1760000, .max = 3510000 },
  376. .n = { .min = 1, .max = 3 },
  377. .m = { .min = 79, .max = 127 },
  378. .m1 = { .min = 12, .max = 22 },
  379. .m2 = { .min = 5, .max = 9 },
  380. .p = { .min = 14, .max = 56 },
  381. .p1 = { .min = 2, .max = 8 },
  382. .p2 = { .dot_limit = 225000,
  383. .p2_slow = 7, .p2_fast = 7 },
  384. };
  385. /* LVDS 100mhz refclk limits. */
  386. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  387. .dot = { .min = 25000, .max = 350000 },
  388. .vco = { .min = 1760000, .max = 3510000 },
  389. .n = { .min = 1, .max = 2 },
  390. .m = { .min = 79, .max = 126 },
  391. .m1 = { .min = 12, .max = 22 },
  392. .m2 = { .min = 5, .max = 9 },
  393. .p = { .min = 28, .max = 112 },
  394. .p1 = { .min = 2, .max = 8 },
  395. .p2 = { .dot_limit = 225000,
  396. .p2_slow = 14, .p2_fast = 14 },
  397. };
  398. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  399. .dot = { .min = 25000, .max = 350000 },
  400. .vco = { .min = 1760000, .max = 3510000 },
  401. .n = { .min = 1, .max = 3 },
  402. .m = { .min = 79, .max = 126 },
  403. .m1 = { .min = 12, .max = 22 },
  404. .m2 = { .min = 5, .max = 9 },
  405. .p = { .min = 14, .max = 42 },
  406. .p1 = { .min = 2, .max = 6 },
  407. .p2 = { .dot_limit = 225000,
  408. .p2_slow = 7, .p2_fast = 7 },
  409. };
  410. static const intel_limit_t intel_limits_vlv = {
  411. /*
  412. * These are the data rate limits (measured in fast clocks)
  413. * since those are the strictest limits we have. The fast
  414. * clock and actual rate limits are more relaxed, so checking
  415. * them would make no difference.
  416. */
  417. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  418. .vco = { .min = 4000000, .max = 6000000 },
  419. .n = { .min = 1, .max = 7 },
  420. .m1 = { .min = 2, .max = 3 },
  421. .m2 = { .min = 11, .max = 156 },
  422. .p1 = { .min = 2, .max = 3 },
  423. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  424. };
  425. static const intel_limit_t intel_limits_chv = {
  426. /*
  427. * These are the data rate limits (measured in fast clocks)
  428. * since those are the strictest limits we have. The fast
  429. * clock and actual rate limits are more relaxed, so checking
  430. * them would make no difference.
  431. */
  432. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  433. .vco = { .min = 4800000, .max = 6480000 },
  434. .n = { .min = 1, .max = 1 },
  435. .m1 = { .min = 2, .max = 2 },
  436. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  437. .p1 = { .min = 2, .max = 4 },
  438. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  439. };
  440. static const intel_limit_t intel_limits_bxt = {
  441. /* FIXME: find real dot limits */
  442. .dot = { .min = 0, .max = INT_MAX },
  443. .vco = { .min = 4800000, .max = 6700000 },
  444. .n = { .min = 1, .max = 1 },
  445. .m1 = { .min = 2, .max = 2 },
  446. /* FIXME: find real m2 limits */
  447. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  448. .p1 = { .min = 2, .max = 4 },
  449. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  450. };
  451. static bool
  452. needs_modeset(struct drm_crtc_state *state)
  453. {
  454. return drm_atomic_crtc_needs_modeset(state);
  455. }
  456. /**
  457. * Returns whether any output on the specified pipe is of the specified type
  458. */
  459. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  460. {
  461. struct drm_device *dev = crtc->base.dev;
  462. struct intel_encoder *encoder;
  463. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  464. if (encoder->type == type)
  465. return true;
  466. return false;
  467. }
  468. /**
  469. * Returns whether any output on the specified pipe will have the specified
  470. * type after a staged modeset is complete, i.e., the same as
  471. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  472. * encoder->crtc.
  473. */
  474. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  475. int type)
  476. {
  477. struct drm_atomic_state *state = crtc_state->base.state;
  478. struct drm_connector *connector;
  479. struct drm_connector_state *connector_state;
  480. struct intel_encoder *encoder;
  481. int i, num_connectors = 0;
  482. for_each_connector_in_state(state, connector, connector_state, i) {
  483. if (connector_state->crtc != crtc_state->base.crtc)
  484. continue;
  485. num_connectors++;
  486. encoder = to_intel_encoder(connector_state->best_encoder);
  487. if (encoder->type == type)
  488. return true;
  489. }
  490. WARN_ON(num_connectors == 0);
  491. return false;
  492. }
  493. static const intel_limit_t *
  494. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  495. {
  496. struct drm_device *dev = crtc_state->base.crtc->dev;
  497. const intel_limit_t *limit;
  498. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  499. if (intel_is_dual_link_lvds(dev)) {
  500. if (refclk == 100000)
  501. limit = &intel_limits_ironlake_dual_lvds_100m;
  502. else
  503. limit = &intel_limits_ironlake_dual_lvds;
  504. } else {
  505. if (refclk == 100000)
  506. limit = &intel_limits_ironlake_single_lvds_100m;
  507. else
  508. limit = &intel_limits_ironlake_single_lvds;
  509. }
  510. } else
  511. limit = &intel_limits_ironlake_dac;
  512. return limit;
  513. }
  514. static const intel_limit_t *
  515. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  516. {
  517. struct drm_device *dev = crtc_state->base.crtc->dev;
  518. const intel_limit_t *limit;
  519. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  520. if (intel_is_dual_link_lvds(dev))
  521. limit = &intel_limits_g4x_dual_channel_lvds;
  522. else
  523. limit = &intel_limits_g4x_single_channel_lvds;
  524. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  525. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  526. limit = &intel_limits_g4x_hdmi;
  527. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  528. limit = &intel_limits_g4x_sdvo;
  529. } else /* The option is for other outputs */
  530. limit = &intel_limits_i9xx_sdvo;
  531. return limit;
  532. }
  533. static const intel_limit_t *
  534. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  535. {
  536. struct drm_device *dev = crtc_state->base.crtc->dev;
  537. const intel_limit_t *limit;
  538. if (IS_BROXTON(dev))
  539. limit = &intel_limits_bxt;
  540. else if (HAS_PCH_SPLIT(dev))
  541. limit = intel_ironlake_limit(crtc_state, refclk);
  542. else if (IS_G4X(dev)) {
  543. limit = intel_g4x_limit(crtc_state);
  544. } else if (IS_PINEVIEW(dev)) {
  545. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  546. limit = &intel_limits_pineview_lvds;
  547. else
  548. limit = &intel_limits_pineview_sdvo;
  549. } else if (IS_CHERRYVIEW(dev)) {
  550. limit = &intel_limits_chv;
  551. } else if (IS_VALLEYVIEW(dev)) {
  552. limit = &intel_limits_vlv;
  553. } else if (!IS_GEN2(dev)) {
  554. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  555. limit = &intel_limits_i9xx_lvds;
  556. else
  557. limit = &intel_limits_i9xx_sdvo;
  558. } else {
  559. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  560. limit = &intel_limits_i8xx_lvds;
  561. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  562. limit = &intel_limits_i8xx_dvo;
  563. else
  564. limit = &intel_limits_i8xx_dac;
  565. }
  566. return limit;
  567. }
  568. /*
  569. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  570. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  571. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  572. * The helpers' return value is the rate of the clock that is fed to the
  573. * display engine's pipe which can be the above fast dot clock rate or a
  574. * divided-down version of it.
  575. */
  576. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  577. static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
  578. {
  579. clock->m = clock->m2 + 2;
  580. clock->p = clock->p1 * clock->p2;
  581. if (WARN_ON(clock->n == 0 || clock->p == 0))
  582. return 0;
  583. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  584. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  585. return clock->dot;
  586. }
  587. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  588. {
  589. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  590. }
  591. static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
  592. {
  593. clock->m = i9xx_dpll_compute_m(clock);
  594. clock->p = clock->p1 * clock->p2;
  595. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  596. return 0;
  597. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  598. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  599. return clock->dot;
  600. }
  601. static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
  602. {
  603. clock->m = clock->m1 * clock->m2;
  604. clock->p = clock->p1 * clock->p2;
  605. if (WARN_ON(clock->n == 0 || clock->p == 0))
  606. return 0;
  607. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  608. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  609. return clock->dot / 5;
  610. }
  611. int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
  612. {
  613. clock->m = clock->m1 * clock->m2;
  614. clock->p = clock->p1 * clock->p2;
  615. if (WARN_ON(clock->n == 0 || clock->p == 0))
  616. return 0;
  617. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  618. clock->n << 22);
  619. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  620. return clock->dot / 5;
  621. }
  622. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  623. /**
  624. * Returns whether the given set of divisors are valid for a given refclk with
  625. * the given connectors.
  626. */
  627. static bool intel_PLL_is_valid(struct drm_device *dev,
  628. const intel_limit_t *limit,
  629. const intel_clock_t *clock)
  630. {
  631. if (clock->n < limit->n.min || limit->n.max < clock->n)
  632. INTELPllInvalid("n out of range\n");
  633. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  634. INTELPllInvalid("p1 out of range\n");
  635. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  636. INTELPllInvalid("m2 out of range\n");
  637. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  638. INTELPllInvalid("m1 out of range\n");
  639. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
  640. !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
  641. if (clock->m1 <= clock->m2)
  642. INTELPllInvalid("m1 <= m2\n");
  643. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
  644. if (clock->p < limit->p.min || limit->p.max < clock->p)
  645. INTELPllInvalid("p out of range\n");
  646. if (clock->m < limit->m.min || limit->m.max < clock->m)
  647. INTELPllInvalid("m out of range\n");
  648. }
  649. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  650. INTELPllInvalid("vco out of range\n");
  651. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  652. * connector, etc., rather than just a single range.
  653. */
  654. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  655. INTELPllInvalid("dot out of range\n");
  656. return true;
  657. }
  658. static int
  659. i9xx_select_p2_div(const intel_limit_t *limit,
  660. const struct intel_crtc_state *crtc_state,
  661. int target)
  662. {
  663. struct drm_device *dev = crtc_state->base.crtc->dev;
  664. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  665. /*
  666. * For LVDS just rely on its current settings for dual-channel.
  667. * We haven't figured out how to reliably set up different
  668. * single/dual channel state, if we even can.
  669. */
  670. if (intel_is_dual_link_lvds(dev))
  671. return limit->p2.p2_fast;
  672. else
  673. return limit->p2.p2_slow;
  674. } else {
  675. if (target < limit->p2.dot_limit)
  676. return limit->p2.p2_slow;
  677. else
  678. return limit->p2.p2_fast;
  679. }
  680. }
  681. static bool
  682. i9xx_find_best_dpll(const intel_limit_t *limit,
  683. struct intel_crtc_state *crtc_state,
  684. int target, int refclk, intel_clock_t *match_clock,
  685. intel_clock_t *best_clock)
  686. {
  687. struct drm_device *dev = crtc_state->base.crtc->dev;
  688. intel_clock_t clock;
  689. int err = target;
  690. memset(best_clock, 0, sizeof(*best_clock));
  691. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  692. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  693. clock.m1++) {
  694. for (clock.m2 = limit->m2.min;
  695. clock.m2 <= limit->m2.max; clock.m2++) {
  696. if (clock.m2 >= clock.m1)
  697. break;
  698. for (clock.n = limit->n.min;
  699. clock.n <= limit->n.max; clock.n++) {
  700. for (clock.p1 = limit->p1.min;
  701. clock.p1 <= limit->p1.max; clock.p1++) {
  702. int this_err;
  703. i9xx_calc_dpll_params(refclk, &clock);
  704. if (!intel_PLL_is_valid(dev, limit,
  705. &clock))
  706. continue;
  707. if (match_clock &&
  708. clock.p != match_clock->p)
  709. continue;
  710. this_err = abs(clock.dot - target);
  711. if (this_err < err) {
  712. *best_clock = clock;
  713. err = this_err;
  714. }
  715. }
  716. }
  717. }
  718. }
  719. return (err != target);
  720. }
  721. static bool
  722. pnv_find_best_dpll(const intel_limit_t *limit,
  723. struct intel_crtc_state *crtc_state,
  724. int target, int refclk, intel_clock_t *match_clock,
  725. intel_clock_t *best_clock)
  726. {
  727. struct drm_device *dev = crtc_state->base.crtc->dev;
  728. intel_clock_t clock;
  729. int err = target;
  730. memset(best_clock, 0, sizeof(*best_clock));
  731. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  732. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  733. clock.m1++) {
  734. for (clock.m2 = limit->m2.min;
  735. clock.m2 <= limit->m2.max; clock.m2++) {
  736. for (clock.n = limit->n.min;
  737. clock.n <= limit->n.max; clock.n++) {
  738. for (clock.p1 = limit->p1.min;
  739. clock.p1 <= limit->p1.max; clock.p1++) {
  740. int this_err;
  741. pnv_calc_dpll_params(refclk, &clock);
  742. if (!intel_PLL_is_valid(dev, limit,
  743. &clock))
  744. continue;
  745. if (match_clock &&
  746. clock.p != match_clock->p)
  747. continue;
  748. this_err = abs(clock.dot - target);
  749. if (this_err < err) {
  750. *best_clock = clock;
  751. err = this_err;
  752. }
  753. }
  754. }
  755. }
  756. }
  757. return (err != target);
  758. }
  759. static bool
  760. g4x_find_best_dpll(const intel_limit_t *limit,
  761. struct intel_crtc_state *crtc_state,
  762. int target, int refclk, intel_clock_t *match_clock,
  763. intel_clock_t *best_clock)
  764. {
  765. struct drm_device *dev = crtc_state->base.crtc->dev;
  766. intel_clock_t clock;
  767. int max_n;
  768. bool found = false;
  769. /* approximately equals target * 0.00585 */
  770. int err_most = (target >> 8) + (target >> 9);
  771. memset(best_clock, 0, sizeof(*best_clock));
  772. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  773. max_n = limit->n.max;
  774. /* based on hardware requirement, prefer smaller n to precision */
  775. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  776. /* based on hardware requirement, prefere larger m1,m2 */
  777. for (clock.m1 = limit->m1.max;
  778. clock.m1 >= limit->m1.min; clock.m1--) {
  779. for (clock.m2 = limit->m2.max;
  780. clock.m2 >= limit->m2.min; clock.m2--) {
  781. for (clock.p1 = limit->p1.max;
  782. clock.p1 >= limit->p1.min; clock.p1--) {
  783. int this_err;
  784. i9xx_calc_dpll_params(refclk, &clock);
  785. if (!intel_PLL_is_valid(dev, limit,
  786. &clock))
  787. continue;
  788. this_err = abs(clock.dot - target);
  789. if (this_err < err_most) {
  790. *best_clock = clock;
  791. err_most = this_err;
  792. max_n = clock.n;
  793. found = true;
  794. }
  795. }
  796. }
  797. }
  798. }
  799. return found;
  800. }
  801. /*
  802. * Check if the calculated PLL configuration is more optimal compared to the
  803. * best configuration and error found so far. Return the calculated error.
  804. */
  805. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  806. const intel_clock_t *calculated_clock,
  807. const intel_clock_t *best_clock,
  808. unsigned int best_error_ppm,
  809. unsigned int *error_ppm)
  810. {
  811. /*
  812. * For CHV ignore the error and consider only the P value.
  813. * Prefer a bigger P value based on HW requirements.
  814. */
  815. if (IS_CHERRYVIEW(dev)) {
  816. *error_ppm = 0;
  817. return calculated_clock->p > best_clock->p;
  818. }
  819. if (WARN_ON_ONCE(!target_freq))
  820. return false;
  821. *error_ppm = div_u64(1000000ULL *
  822. abs(target_freq - calculated_clock->dot),
  823. target_freq);
  824. /*
  825. * Prefer a better P value over a better (smaller) error if the error
  826. * is small. Ensure this preference for future configurations too by
  827. * setting the error to 0.
  828. */
  829. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  830. *error_ppm = 0;
  831. return true;
  832. }
  833. return *error_ppm + 10 < best_error_ppm;
  834. }
  835. static bool
  836. vlv_find_best_dpll(const intel_limit_t *limit,
  837. struct intel_crtc_state *crtc_state,
  838. int target, int refclk, intel_clock_t *match_clock,
  839. intel_clock_t *best_clock)
  840. {
  841. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  842. struct drm_device *dev = crtc->base.dev;
  843. intel_clock_t clock;
  844. unsigned int bestppm = 1000000;
  845. /* min update 19.2 MHz */
  846. int max_n = min(limit->n.max, refclk / 19200);
  847. bool found = false;
  848. target *= 5; /* fast clock */
  849. memset(best_clock, 0, sizeof(*best_clock));
  850. /* based on hardware requirement, prefer smaller n to precision */
  851. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  852. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  853. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  854. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  855. clock.p = clock.p1 * clock.p2;
  856. /* based on hardware requirement, prefer bigger m1,m2 values */
  857. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  858. unsigned int ppm;
  859. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  860. refclk * clock.m1);
  861. vlv_calc_dpll_params(refclk, &clock);
  862. if (!intel_PLL_is_valid(dev, limit,
  863. &clock))
  864. continue;
  865. if (!vlv_PLL_is_optimal(dev, target,
  866. &clock,
  867. best_clock,
  868. bestppm, &ppm))
  869. continue;
  870. *best_clock = clock;
  871. bestppm = ppm;
  872. found = true;
  873. }
  874. }
  875. }
  876. }
  877. return found;
  878. }
  879. static bool
  880. chv_find_best_dpll(const intel_limit_t *limit,
  881. struct intel_crtc_state *crtc_state,
  882. int target, int refclk, intel_clock_t *match_clock,
  883. intel_clock_t *best_clock)
  884. {
  885. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  886. struct drm_device *dev = crtc->base.dev;
  887. unsigned int best_error_ppm;
  888. intel_clock_t clock;
  889. uint64_t m2;
  890. int found = false;
  891. memset(best_clock, 0, sizeof(*best_clock));
  892. best_error_ppm = 1000000;
  893. /*
  894. * Based on hardware doc, the n always set to 1, and m1 always
  895. * set to 2. If requires to support 200Mhz refclk, we need to
  896. * revisit this because n may not 1 anymore.
  897. */
  898. clock.n = 1, clock.m1 = 2;
  899. target *= 5; /* fast clock */
  900. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  901. for (clock.p2 = limit->p2.p2_fast;
  902. clock.p2 >= limit->p2.p2_slow;
  903. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  904. unsigned int error_ppm;
  905. clock.p = clock.p1 * clock.p2;
  906. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  907. clock.n) << 22, refclk * clock.m1);
  908. if (m2 > INT_MAX/clock.m1)
  909. continue;
  910. clock.m2 = m2;
  911. chv_calc_dpll_params(refclk, &clock);
  912. if (!intel_PLL_is_valid(dev, limit, &clock))
  913. continue;
  914. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  915. best_error_ppm, &error_ppm))
  916. continue;
  917. *best_clock = clock;
  918. best_error_ppm = error_ppm;
  919. found = true;
  920. }
  921. }
  922. return found;
  923. }
  924. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  925. intel_clock_t *best_clock)
  926. {
  927. int refclk = i9xx_get_refclk(crtc_state, 0);
  928. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  929. target_clock, refclk, NULL, best_clock);
  930. }
  931. bool intel_crtc_active(struct drm_crtc *crtc)
  932. {
  933. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  934. /* Be paranoid as we can arrive here with only partial
  935. * state retrieved from the hardware during setup.
  936. *
  937. * We can ditch the adjusted_mode.crtc_clock check as soon
  938. * as Haswell has gained clock readout/fastboot support.
  939. *
  940. * We can ditch the crtc->primary->fb check as soon as we can
  941. * properly reconstruct framebuffers.
  942. *
  943. * FIXME: The intel_crtc->active here should be switched to
  944. * crtc->state->active once we have proper CRTC states wired up
  945. * for atomic.
  946. */
  947. return intel_crtc->active && crtc->primary->state->fb &&
  948. intel_crtc->config->base.adjusted_mode.crtc_clock;
  949. }
  950. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  951. enum pipe pipe)
  952. {
  953. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  954. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  955. return intel_crtc->config->cpu_transcoder;
  956. }
  957. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  958. {
  959. struct drm_i915_private *dev_priv = dev->dev_private;
  960. i915_reg_t reg = PIPEDSL(pipe);
  961. u32 line1, line2;
  962. u32 line_mask;
  963. if (IS_GEN2(dev))
  964. line_mask = DSL_LINEMASK_GEN2;
  965. else
  966. line_mask = DSL_LINEMASK_GEN3;
  967. line1 = I915_READ(reg) & line_mask;
  968. msleep(5);
  969. line2 = I915_READ(reg) & line_mask;
  970. return line1 == line2;
  971. }
  972. /*
  973. * intel_wait_for_pipe_off - wait for pipe to turn off
  974. * @crtc: crtc whose pipe to wait for
  975. *
  976. * After disabling a pipe, we can't wait for vblank in the usual way,
  977. * spinning on the vblank interrupt status bit, since we won't actually
  978. * see an interrupt when the pipe is disabled.
  979. *
  980. * On Gen4 and above:
  981. * wait for the pipe register state bit to turn off
  982. *
  983. * Otherwise:
  984. * wait for the display line value to settle (it usually
  985. * ends up stopping at the start of the next frame).
  986. *
  987. */
  988. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  989. {
  990. struct drm_device *dev = crtc->base.dev;
  991. struct drm_i915_private *dev_priv = dev->dev_private;
  992. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  993. enum pipe pipe = crtc->pipe;
  994. if (INTEL_INFO(dev)->gen >= 4) {
  995. i915_reg_t reg = PIPECONF(cpu_transcoder);
  996. /* Wait for the Pipe State to go off */
  997. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  998. 100))
  999. WARN(1, "pipe_off wait timed out\n");
  1000. } else {
  1001. /* Wait for the display line to settle */
  1002. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  1003. WARN(1, "pipe_off wait timed out\n");
  1004. }
  1005. }
  1006. /* Only for pre-ILK configs */
  1007. void assert_pll(struct drm_i915_private *dev_priv,
  1008. enum pipe pipe, bool state)
  1009. {
  1010. u32 val;
  1011. bool cur_state;
  1012. val = I915_READ(DPLL(pipe));
  1013. cur_state = !!(val & DPLL_VCO_ENABLE);
  1014. I915_STATE_WARN(cur_state != state,
  1015. "PLL state assertion failure (expected %s, current %s)\n",
  1016. onoff(state), onoff(cur_state));
  1017. }
  1018. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1019. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1020. {
  1021. u32 val;
  1022. bool cur_state;
  1023. mutex_lock(&dev_priv->sb_lock);
  1024. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1025. mutex_unlock(&dev_priv->sb_lock);
  1026. cur_state = val & DSI_PLL_VCO_EN;
  1027. I915_STATE_WARN(cur_state != state,
  1028. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1029. onoff(state), onoff(cur_state));
  1030. }
  1031. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1032. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1033. struct intel_shared_dpll *
  1034. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1035. {
  1036. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1037. if (crtc->config->shared_dpll < 0)
  1038. return NULL;
  1039. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1040. }
  1041. /* For ILK+ */
  1042. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1043. struct intel_shared_dpll *pll,
  1044. bool state)
  1045. {
  1046. bool cur_state;
  1047. struct intel_dpll_hw_state hw_state;
  1048. if (WARN(!pll, "asserting DPLL %s with no DPLL\n", onoff(state)))
  1049. return;
  1050. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1051. I915_STATE_WARN(cur_state != state,
  1052. "%s assertion failure (expected %s, current %s)\n",
  1053. pll->name, onoff(state), onoff(cur_state));
  1054. }
  1055. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1056. enum pipe pipe, bool state)
  1057. {
  1058. bool cur_state;
  1059. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1060. pipe);
  1061. if (HAS_DDI(dev_priv->dev)) {
  1062. /* DDI does not have a specific FDI_TX register */
  1063. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1064. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1065. } else {
  1066. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1067. cur_state = !!(val & FDI_TX_ENABLE);
  1068. }
  1069. I915_STATE_WARN(cur_state != state,
  1070. "FDI TX state assertion failure (expected %s, current %s)\n",
  1071. onoff(state), onoff(cur_state));
  1072. }
  1073. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1074. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1075. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1076. enum pipe pipe, bool state)
  1077. {
  1078. u32 val;
  1079. bool cur_state;
  1080. val = I915_READ(FDI_RX_CTL(pipe));
  1081. cur_state = !!(val & FDI_RX_ENABLE);
  1082. I915_STATE_WARN(cur_state != state,
  1083. "FDI RX state assertion failure (expected %s, current %s)\n",
  1084. onoff(state), onoff(cur_state));
  1085. }
  1086. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1087. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1088. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1089. enum pipe pipe)
  1090. {
  1091. u32 val;
  1092. /* ILK FDI PLL is always enabled */
  1093. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1094. return;
  1095. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1096. if (HAS_DDI(dev_priv->dev))
  1097. return;
  1098. val = I915_READ(FDI_TX_CTL(pipe));
  1099. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1100. }
  1101. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1102. enum pipe pipe, bool state)
  1103. {
  1104. u32 val;
  1105. bool cur_state;
  1106. val = I915_READ(FDI_RX_CTL(pipe));
  1107. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1108. I915_STATE_WARN(cur_state != state,
  1109. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1110. onoff(state), onoff(cur_state));
  1111. }
  1112. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1113. enum pipe pipe)
  1114. {
  1115. struct drm_device *dev = dev_priv->dev;
  1116. i915_reg_t pp_reg;
  1117. u32 val;
  1118. enum pipe panel_pipe = PIPE_A;
  1119. bool locked = true;
  1120. if (WARN_ON(HAS_DDI(dev)))
  1121. return;
  1122. if (HAS_PCH_SPLIT(dev)) {
  1123. u32 port_sel;
  1124. pp_reg = PCH_PP_CONTROL;
  1125. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1126. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1127. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1128. panel_pipe = PIPE_B;
  1129. /* XXX: else fix for eDP */
  1130. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1131. /* presumably write lock depends on pipe, not port select */
  1132. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1133. panel_pipe = pipe;
  1134. } else {
  1135. pp_reg = PP_CONTROL;
  1136. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1137. panel_pipe = PIPE_B;
  1138. }
  1139. val = I915_READ(pp_reg);
  1140. if (!(val & PANEL_POWER_ON) ||
  1141. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1142. locked = false;
  1143. I915_STATE_WARN(panel_pipe == pipe && locked,
  1144. "panel assertion failure, pipe %c regs locked\n",
  1145. pipe_name(pipe));
  1146. }
  1147. static void assert_cursor(struct drm_i915_private *dev_priv,
  1148. enum pipe pipe, bool state)
  1149. {
  1150. struct drm_device *dev = dev_priv->dev;
  1151. bool cur_state;
  1152. if (IS_845G(dev) || IS_I865G(dev))
  1153. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1154. else
  1155. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1156. I915_STATE_WARN(cur_state != state,
  1157. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1158. pipe_name(pipe), onoff(state), onoff(cur_state));
  1159. }
  1160. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1161. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1162. void assert_pipe(struct drm_i915_private *dev_priv,
  1163. enum pipe pipe, bool state)
  1164. {
  1165. bool cur_state;
  1166. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1167. pipe);
  1168. /* if we need the pipe quirk it must be always on */
  1169. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1170. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1171. state = true;
  1172. if (!intel_display_power_is_enabled(dev_priv,
  1173. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1174. cur_state = false;
  1175. } else {
  1176. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1177. cur_state = !!(val & PIPECONF_ENABLE);
  1178. }
  1179. I915_STATE_WARN(cur_state != state,
  1180. "pipe %c assertion failure (expected %s, current %s)\n",
  1181. pipe_name(pipe), onoff(state), onoff(cur_state));
  1182. }
  1183. static void assert_plane(struct drm_i915_private *dev_priv,
  1184. enum plane plane, bool state)
  1185. {
  1186. u32 val;
  1187. bool cur_state;
  1188. val = I915_READ(DSPCNTR(plane));
  1189. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1190. I915_STATE_WARN(cur_state != state,
  1191. "plane %c assertion failure (expected %s, current %s)\n",
  1192. plane_name(plane), onoff(state), onoff(cur_state));
  1193. }
  1194. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1195. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1196. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1197. enum pipe pipe)
  1198. {
  1199. struct drm_device *dev = dev_priv->dev;
  1200. int i;
  1201. /* Primary planes are fixed to pipes on gen4+ */
  1202. if (INTEL_INFO(dev)->gen >= 4) {
  1203. u32 val = I915_READ(DSPCNTR(pipe));
  1204. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1205. "plane %c assertion failure, should be disabled but not\n",
  1206. plane_name(pipe));
  1207. return;
  1208. }
  1209. /* Need to check both planes against the pipe */
  1210. for_each_pipe(dev_priv, i) {
  1211. u32 val = I915_READ(DSPCNTR(i));
  1212. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1213. DISPPLANE_SEL_PIPE_SHIFT;
  1214. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1215. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1216. plane_name(i), pipe_name(pipe));
  1217. }
  1218. }
  1219. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1220. enum pipe pipe)
  1221. {
  1222. struct drm_device *dev = dev_priv->dev;
  1223. int sprite;
  1224. if (INTEL_INFO(dev)->gen >= 9) {
  1225. for_each_sprite(dev_priv, pipe, sprite) {
  1226. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1227. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1228. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1229. sprite, pipe_name(pipe));
  1230. }
  1231. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1232. for_each_sprite(dev_priv, pipe, sprite) {
  1233. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1234. I915_STATE_WARN(val & SP_ENABLE,
  1235. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1236. sprite_name(pipe, sprite), pipe_name(pipe));
  1237. }
  1238. } else if (INTEL_INFO(dev)->gen >= 7) {
  1239. u32 val = I915_READ(SPRCTL(pipe));
  1240. I915_STATE_WARN(val & SPRITE_ENABLE,
  1241. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1242. plane_name(pipe), pipe_name(pipe));
  1243. } else if (INTEL_INFO(dev)->gen >= 5) {
  1244. u32 val = I915_READ(DVSCNTR(pipe));
  1245. I915_STATE_WARN(val & DVS_ENABLE,
  1246. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1247. plane_name(pipe), pipe_name(pipe));
  1248. }
  1249. }
  1250. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1251. {
  1252. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1253. drm_crtc_vblank_put(crtc);
  1254. }
  1255. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1256. {
  1257. u32 val;
  1258. bool enabled;
  1259. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1260. val = I915_READ(PCH_DREF_CONTROL);
  1261. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1262. DREF_SUPERSPREAD_SOURCE_MASK));
  1263. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1264. }
  1265. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1266. enum pipe pipe)
  1267. {
  1268. u32 val;
  1269. bool enabled;
  1270. val = I915_READ(PCH_TRANSCONF(pipe));
  1271. enabled = !!(val & TRANS_ENABLE);
  1272. I915_STATE_WARN(enabled,
  1273. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1274. pipe_name(pipe));
  1275. }
  1276. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1277. enum pipe pipe, u32 port_sel, u32 val)
  1278. {
  1279. if ((val & DP_PORT_EN) == 0)
  1280. return false;
  1281. if (HAS_PCH_CPT(dev_priv->dev)) {
  1282. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1283. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1284. return false;
  1285. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1286. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1287. return false;
  1288. } else {
  1289. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1290. return false;
  1291. }
  1292. return true;
  1293. }
  1294. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1295. enum pipe pipe, u32 val)
  1296. {
  1297. if ((val & SDVO_ENABLE) == 0)
  1298. return false;
  1299. if (HAS_PCH_CPT(dev_priv->dev)) {
  1300. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1301. return false;
  1302. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1303. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1304. return false;
  1305. } else {
  1306. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1307. return false;
  1308. }
  1309. return true;
  1310. }
  1311. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1312. enum pipe pipe, u32 val)
  1313. {
  1314. if ((val & LVDS_PORT_EN) == 0)
  1315. return false;
  1316. if (HAS_PCH_CPT(dev_priv->dev)) {
  1317. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1318. return false;
  1319. } else {
  1320. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1321. return false;
  1322. }
  1323. return true;
  1324. }
  1325. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1326. enum pipe pipe, u32 val)
  1327. {
  1328. if ((val & ADPA_DAC_ENABLE) == 0)
  1329. return false;
  1330. if (HAS_PCH_CPT(dev_priv->dev)) {
  1331. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1332. return false;
  1333. } else {
  1334. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1335. return false;
  1336. }
  1337. return true;
  1338. }
  1339. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1340. enum pipe pipe, i915_reg_t reg,
  1341. u32 port_sel)
  1342. {
  1343. u32 val = I915_READ(reg);
  1344. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1345. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1346. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1347. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1348. && (val & DP_PIPEB_SELECT),
  1349. "IBX PCH dp port still using transcoder B\n");
  1350. }
  1351. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1352. enum pipe pipe, i915_reg_t reg)
  1353. {
  1354. u32 val = I915_READ(reg);
  1355. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1356. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1357. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1358. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1359. && (val & SDVO_PIPE_B_SELECT),
  1360. "IBX PCH hdmi port still using transcoder B\n");
  1361. }
  1362. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1363. enum pipe pipe)
  1364. {
  1365. u32 val;
  1366. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1367. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1368. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1369. val = I915_READ(PCH_ADPA);
  1370. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1371. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1372. pipe_name(pipe));
  1373. val = I915_READ(PCH_LVDS);
  1374. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1375. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1376. pipe_name(pipe));
  1377. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1378. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1379. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1380. }
  1381. static void vlv_enable_pll(struct intel_crtc *crtc,
  1382. const struct intel_crtc_state *pipe_config)
  1383. {
  1384. struct drm_device *dev = crtc->base.dev;
  1385. struct drm_i915_private *dev_priv = dev->dev_private;
  1386. i915_reg_t reg = DPLL(crtc->pipe);
  1387. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1388. assert_pipe_disabled(dev_priv, crtc->pipe);
  1389. /* PLL is protected by panel, make sure we can write it */
  1390. if (IS_MOBILE(dev_priv->dev))
  1391. assert_panel_unlocked(dev_priv, crtc->pipe);
  1392. I915_WRITE(reg, dpll);
  1393. POSTING_READ(reg);
  1394. udelay(150);
  1395. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1396. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1397. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1398. POSTING_READ(DPLL_MD(crtc->pipe));
  1399. /* We do this three times for luck */
  1400. I915_WRITE(reg, dpll);
  1401. POSTING_READ(reg);
  1402. udelay(150); /* wait for warmup */
  1403. I915_WRITE(reg, dpll);
  1404. POSTING_READ(reg);
  1405. udelay(150); /* wait for warmup */
  1406. I915_WRITE(reg, dpll);
  1407. POSTING_READ(reg);
  1408. udelay(150); /* wait for warmup */
  1409. }
  1410. static void chv_enable_pll(struct intel_crtc *crtc,
  1411. const struct intel_crtc_state *pipe_config)
  1412. {
  1413. struct drm_device *dev = crtc->base.dev;
  1414. struct drm_i915_private *dev_priv = dev->dev_private;
  1415. int pipe = crtc->pipe;
  1416. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1417. u32 tmp;
  1418. assert_pipe_disabled(dev_priv, crtc->pipe);
  1419. mutex_lock(&dev_priv->sb_lock);
  1420. /* Enable back the 10bit clock to display controller */
  1421. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1422. tmp |= DPIO_DCLKP_EN;
  1423. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1424. mutex_unlock(&dev_priv->sb_lock);
  1425. /*
  1426. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1427. */
  1428. udelay(1);
  1429. /* Enable PLL */
  1430. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1431. /* Check PLL is locked */
  1432. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1433. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1434. /* not sure when this should be written */
  1435. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1436. POSTING_READ(DPLL_MD(pipe));
  1437. }
  1438. static int intel_num_dvo_pipes(struct drm_device *dev)
  1439. {
  1440. struct intel_crtc *crtc;
  1441. int count = 0;
  1442. for_each_intel_crtc(dev, crtc)
  1443. count += crtc->base.state->active &&
  1444. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1445. return count;
  1446. }
  1447. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1448. {
  1449. struct drm_device *dev = crtc->base.dev;
  1450. struct drm_i915_private *dev_priv = dev->dev_private;
  1451. i915_reg_t reg = DPLL(crtc->pipe);
  1452. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1453. assert_pipe_disabled(dev_priv, crtc->pipe);
  1454. /* No really, not for ILK+ */
  1455. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1456. /* PLL is protected by panel, make sure we can write it */
  1457. if (IS_MOBILE(dev) && !IS_I830(dev))
  1458. assert_panel_unlocked(dev_priv, crtc->pipe);
  1459. /* Enable DVO 2x clock on both PLLs if necessary */
  1460. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1461. /*
  1462. * It appears to be important that we don't enable this
  1463. * for the current pipe before otherwise configuring the
  1464. * PLL. No idea how this should be handled if multiple
  1465. * DVO outputs are enabled simultaneosly.
  1466. */
  1467. dpll |= DPLL_DVO_2X_MODE;
  1468. I915_WRITE(DPLL(!crtc->pipe),
  1469. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1470. }
  1471. /*
  1472. * Apparently we need to have VGA mode enabled prior to changing
  1473. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1474. * dividers, even though the register value does change.
  1475. */
  1476. I915_WRITE(reg, 0);
  1477. I915_WRITE(reg, dpll);
  1478. /* Wait for the clocks to stabilize. */
  1479. POSTING_READ(reg);
  1480. udelay(150);
  1481. if (INTEL_INFO(dev)->gen >= 4) {
  1482. I915_WRITE(DPLL_MD(crtc->pipe),
  1483. crtc->config->dpll_hw_state.dpll_md);
  1484. } else {
  1485. /* The pixel multiplier can only be updated once the
  1486. * DPLL is enabled and the clocks are stable.
  1487. *
  1488. * So write it again.
  1489. */
  1490. I915_WRITE(reg, dpll);
  1491. }
  1492. /* We do this three times for luck */
  1493. I915_WRITE(reg, dpll);
  1494. POSTING_READ(reg);
  1495. udelay(150); /* wait for warmup */
  1496. I915_WRITE(reg, dpll);
  1497. POSTING_READ(reg);
  1498. udelay(150); /* wait for warmup */
  1499. I915_WRITE(reg, dpll);
  1500. POSTING_READ(reg);
  1501. udelay(150); /* wait for warmup */
  1502. }
  1503. /**
  1504. * i9xx_disable_pll - disable a PLL
  1505. * @dev_priv: i915 private structure
  1506. * @pipe: pipe PLL to disable
  1507. *
  1508. * Disable the PLL for @pipe, making sure the pipe is off first.
  1509. *
  1510. * Note! This is for pre-ILK only.
  1511. */
  1512. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1513. {
  1514. struct drm_device *dev = crtc->base.dev;
  1515. struct drm_i915_private *dev_priv = dev->dev_private;
  1516. enum pipe pipe = crtc->pipe;
  1517. /* Disable DVO 2x clock on both PLLs if necessary */
  1518. if (IS_I830(dev) &&
  1519. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1520. !intel_num_dvo_pipes(dev)) {
  1521. I915_WRITE(DPLL(PIPE_B),
  1522. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1523. I915_WRITE(DPLL(PIPE_A),
  1524. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1525. }
  1526. /* Don't disable pipe or pipe PLLs if needed */
  1527. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1528. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1529. return;
  1530. /* Make sure the pipe isn't still relying on us */
  1531. assert_pipe_disabled(dev_priv, pipe);
  1532. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1533. POSTING_READ(DPLL(pipe));
  1534. }
  1535. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1536. {
  1537. u32 val;
  1538. /* Make sure the pipe isn't still relying on us */
  1539. assert_pipe_disabled(dev_priv, pipe);
  1540. /*
  1541. * Leave integrated clock source and reference clock enabled for pipe B.
  1542. * The latter is needed for VGA hotplug / manual detection.
  1543. */
  1544. val = DPLL_VGA_MODE_DIS;
  1545. if (pipe == PIPE_B)
  1546. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
  1547. I915_WRITE(DPLL(pipe), val);
  1548. POSTING_READ(DPLL(pipe));
  1549. }
  1550. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1551. {
  1552. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1553. u32 val;
  1554. /* Make sure the pipe isn't still relying on us */
  1555. assert_pipe_disabled(dev_priv, pipe);
  1556. /* Set PLL en = 0 */
  1557. val = DPLL_SSC_REF_CLK_CHV |
  1558. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1559. if (pipe != PIPE_A)
  1560. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1561. I915_WRITE(DPLL(pipe), val);
  1562. POSTING_READ(DPLL(pipe));
  1563. mutex_lock(&dev_priv->sb_lock);
  1564. /* Disable 10bit clock to display controller */
  1565. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1566. val &= ~DPIO_DCLKP_EN;
  1567. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1568. mutex_unlock(&dev_priv->sb_lock);
  1569. }
  1570. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1571. struct intel_digital_port *dport,
  1572. unsigned int expected_mask)
  1573. {
  1574. u32 port_mask;
  1575. i915_reg_t dpll_reg;
  1576. switch (dport->port) {
  1577. case PORT_B:
  1578. port_mask = DPLL_PORTB_READY_MASK;
  1579. dpll_reg = DPLL(0);
  1580. break;
  1581. case PORT_C:
  1582. port_mask = DPLL_PORTC_READY_MASK;
  1583. dpll_reg = DPLL(0);
  1584. expected_mask <<= 4;
  1585. break;
  1586. case PORT_D:
  1587. port_mask = DPLL_PORTD_READY_MASK;
  1588. dpll_reg = DPIO_PHY_STATUS;
  1589. break;
  1590. default:
  1591. BUG();
  1592. }
  1593. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1594. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1595. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1596. }
  1597. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1598. {
  1599. struct drm_device *dev = crtc->base.dev;
  1600. struct drm_i915_private *dev_priv = dev->dev_private;
  1601. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1602. if (WARN_ON(pll == NULL))
  1603. return;
  1604. WARN_ON(!pll->config.crtc_mask);
  1605. if (pll->active == 0) {
  1606. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1607. WARN_ON(pll->on);
  1608. assert_shared_dpll_disabled(dev_priv, pll);
  1609. pll->mode_set(dev_priv, pll);
  1610. }
  1611. }
  1612. /**
  1613. * intel_enable_shared_dpll - enable PCH PLL
  1614. * @dev_priv: i915 private structure
  1615. * @pipe: pipe PLL to enable
  1616. *
  1617. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1618. * drives the transcoder clock.
  1619. */
  1620. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1621. {
  1622. struct drm_device *dev = crtc->base.dev;
  1623. struct drm_i915_private *dev_priv = dev->dev_private;
  1624. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1625. if (WARN_ON(pll == NULL))
  1626. return;
  1627. if (WARN_ON(pll->config.crtc_mask == 0))
  1628. return;
  1629. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1630. pll->name, pll->active, pll->on,
  1631. crtc->base.base.id);
  1632. if (pll->active++) {
  1633. WARN_ON(!pll->on);
  1634. assert_shared_dpll_enabled(dev_priv, pll);
  1635. return;
  1636. }
  1637. WARN_ON(pll->on);
  1638. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1639. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1640. pll->enable(dev_priv, pll);
  1641. pll->on = true;
  1642. }
  1643. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1644. {
  1645. struct drm_device *dev = crtc->base.dev;
  1646. struct drm_i915_private *dev_priv = dev->dev_private;
  1647. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1648. /* PCH only available on ILK+ */
  1649. if (INTEL_INFO(dev)->gen < 5)
  1650. return;
  1651. if (pll == NULL)
  1652. return;
  1653. if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
  1654. return;
  1655. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1656. pll->name, pll->active, pll->on,
  1657. crtc->base.base.id);
  1658. if (WARN_ON(pll->active == 0)) {
  1659. assert_shared_dpll_disabled(dev_priv, pll);
  1660. return;
  1661. }
  1662. assert_shared_dpll_enabled(dev_priv, pll);
  1663. WARN_ON(!pll->on);
  1664. if (--pll->active)
  1665. return;
  1666. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1667. pll->disable(dev_priv, pll);
  1668. pll->on = false;
  1669. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1670. }
  1671. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1672. enum pipe pipe)
  1673. {
  1674. struct drm_device *dev = dev_priv->dev;
  1675. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1676. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1677. i915_reg_t reg;
  1678. uint32_t val, pipeconf_val;
  1679. /* PCH only available on ILK+ */
  1680. BUG_ON(!HAS_PCH_SPLIT(dev));
  1681. /* Make sure PCH DPLL is enabled */
  1682. assert_shared_dpll_enabled(dev_priv,
  1683. intel_crtc_to_shared_dpll(intel_crtc));
  1684. /* FDI must be feeding us bits for PCH ports */
  1685. assert_fdi_tx_enabled(dev_priv, pipe);
  1686. assert_fdi_rx_enabled(dev_priv, pipe);
  1687. if (HAS_PCH_CPT(dev)) {
  1688. /* Workaround: Set the timing override bit before enabling the
  1689. * pch transcoder. */
  1690. reg = TRANS_CHICKEN2(pipe);
  1691. val = I915_READ(reg);
  1692. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1693. I915_WRITE(reg, val);
  1694. }
  1695. reg = PCH_TRANSCONF(pipe);
  1696. val = I915_READ(reg);
  1697. pipeconf_val = I915_READ(PIPECONF(pipe));
  1698. if (HAS_PCH_IBX(dev_priv->dev)) {
  1699. /*
  1700. * Make the BPC in transcoder be consistent with
  1701. * that in pipeconf reg. For HDMI we must use 8bpc
  1702. * here for both 8bpc and 12bpc.
  1703. */
  1704. val &= ~PIPECONF_BPC_MASK;
  1705. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1706. val |= PIPECONF_8BPC;
  1707. else
  1708. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1709. }
  1710. val &= ~TRANS_INTERLACE_MASK;
  1711. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1712. if (HAS_PCH_IBX(dev_priv->dev) &&
  1713. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1714. val |= TRANS_LEGACY_INTERLACED_ILK;
  1715. else
  1716. val |= TRANS_INTERLACED;
  1717. else
  1718. val |= TRANS_PROGRESSIVE;
  1719. I915_WRITE(reg, val | TRANS_ENABLE);
  1720. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1721. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1722. }
  1723. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1724. enum transcoder cpu_transcoder)
  1725. {
  1726. u32 val, pipeconf_val;
  1727. /* PCH only available on ILK+ */
  1728. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1729. /* FDI must be feeding us bits for PCH ports */
  1730. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1731. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1732. /* Workaround: set timing override bit. */
  1733. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1734. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1735. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1736. val = TRANS_ENABLE;
  1737. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1738. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1739. PIPECONF_INTERLACED_ILK)
  1740. val |= TRANS_INTERLACED;
  1741. else
  1742. val |= TRANS_PROGRESSIVE;
  1743. I915_WRITE(LPT_TRANSCONF, val);
  1744. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1745. DRM_ERROR("Failed to enable PCH transcoder\n");
  1746. }
  1747. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1748. enum pipe pipe)
  1749. {
  1750. struct drm_device *dev = dev_priv->dev;
  1751. i915_reg_t reg;
  1752. uint32_t val;
  1753. /* FDI relies on the transcoder */
  1754. assert_fdi_tx_disabled(dev_priv, pipe);
  1755. assert_fdi_rx_disabled(dev_priv, pipe);
  1756. /* Ports must be off as well */
  1757. assert_pch_ports_disabled(dev_priv, pipe);
  1758. reg = PCH_TRANSCONF(pipe);
  1759. val = I915_READ(reg);
  1760. val &= ~TRANS_ENABLE;
  1761. I915_WRITE(reg, val);
  1762. /* wait for PCH transcoder off, transcoder state */
  1763. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1764. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1765. if (HAS_PCH_CPT(dev)) {
  1766. /* Workaround: Clear the timing override chicken bit again. */
  1767. reg = TRANS_CHICKEN2(pipe);
  1768. val = I915_READ(reg);
  1769. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1770. I915_WRITE(reg, val);
  1771. }
  1772. }
  1773. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1774. {
  1775. u32 val;
  1776. val = I915_READ(LPT_TRANSCONF);
  1777. val &= ~TRANS_ENABLE;
  1778. I915_WRITE(LPT_TRANSCONF, val);
  1779. /* wait for PCH transcoder off, transcoder state */
  1780. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1781. DRM_ERROR("Failed to disable PCH transcoder\n");
  1782. /* Workaround: clear timing override bit. */
  1783. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1784. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1785. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1786. }
  1787. /**
  1788. * intel_enable_pipe - enable a pipe, asserting requirements
  1789. * @crtc: crtc responsible for the pipe
  1790. *
  1791. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1792. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1793. */
  1794. static void intel_enable_pipe(struct intel_crtc *crtc)
  1795. {
  1796. struct drm_device *dev = crtc->base.dev;
  1797. struct drm_i915_private *dev_priv = dev->dev_private;
  1798. enum pipe pipe = crtc->pipe;
  1799. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1800. enum pipe pch_transcoder;
  1801. i915_reg_t reg;
  1802. u32 val;
  1803. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1804. assert_planes_disabled(dev_priv, pipe);
  1805. assert_cursor_disabled(dev_priv, pipe);
  1806. assert_sprites_disabled(dev_priv, pipe);
  1807. if (HAS_PCH_LPT(dev_priv->dev))
  1808. pch_transcoder = TRANSCODER_A;
  1809. else
  1810. pch_transcoder = pipe;
  1811. /*
  1812. * A pipe without a PLL won't actually be able to drive bits from
  1813. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1814. * need the check.
  1815. */
  1816. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1817. if (crtc->config->has_dsi_encoder)
  1818. assert_dsi_pll_enabled(dev_priv);
  1819. else
  1820. assert_pll_enabled(dev_priv, pipe);
  1821. else {
  1822. if (crtc->config->has_pch_encoder) {
  1823. /* if driving the PCH, we need FDI enabled */
  1824. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1825. assert_fdi_tx_pll_enabled(dev_priv,
  1826. (enum pipe) cpu_transcoder);
  1827. }
  1828. /* FIXME: assert CPU port conditions for SNB+ */
  1829. }
  1830. reg = PIPECONF(cpu_transcoder);
  1831. val = I915_READ(reg);
  1832. if (val & PIPECONF_ENABLE) {
  1833. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1834. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1835. return;
  1836. }
  1837. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1838. POSTING_READ(reg);
  1839. /*
  1840. * Until the pipe starts DSL will read as 0, which would cause
  1841. * an apparent vblank timestamp jump, which messes up also the
  1842. * frame count when it's derived from the timestamps. So let's
  1843. * wait for the pipe to start properly before we call
  1844. * drm_crtc_vblank_on()
  1845. */
  1846. if (dev->max_vblank_count == 0 &&
  1847. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1848. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1849. }
  1850. /**
  1851. * intel_disable_pipe - disable a pipe, asserting requirements
  1852. * @crtc: crtc whose pipes is to be disabled
  1853. *
  1854. * Disable the pipe of @crtc, making sure that various hardware
  1855. * specific requirements are met, if applicable, e.g. plane
  1856. * disabled, panel fitter off, etc.
  1857. *
  1858. * Will wait until the pipe has shut down before returning.
  1859. */
  1860. static void intel_disable_pipe(struct intel_crtc *crtc)
  1861. {
  1862. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1863. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1864. enum pipe pipe = crtc->pipe;
  1865. i915_reg_t reg;
  1866. u32 val;
  1867. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1868. /*
  1869. * Make sure planes won't keep trying to pump pixels to us,
  1870. * or we might hang the display.
  1871. */
  1872. assert_planes_disabled(dev_priv, pipe);
  1873. assert_cursor_disabled(dev_priv, pipe);
  1874. assert_sprites_disabled(dev_priv, pipe);
  1875. reg = PIPECONF(cpu_transcoder);
  1876. val = I915_READ(reg);
  1877. if ((val & PIPECONF_ENABLE) == 0)
  1878. return;
  1879. /*
  1880. * Double wide has implications for planes
  1881. * so best keep it disabled when not needed.
  1882. */
  1883. if (crtc->config->double_wide)
  1884. val &= ~PIPECONF_DOUBLE_WIDE;
  1885. /* Don't disable pipe or pipe PLLs if needed */
  1886. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1887. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1888. val &= ~PIPECONF_ENABLE;
  1889. I915_WRITE(reg, val);
  1890. if ((val & PIPECONF_ENABLE) == 0)
  1891. intel_wait_for_pipe_off(crtc);
  1892. }
  1893. static bool need_vtd_wa(struct drm_device *dev)
  1894. {
  1895. #ifdef CONFIG_INTEL_IOMMU
  1896. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1897. return true;
  1898. #endif
  1899. return false;
  1900. }
  1901. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1902. {
  1903. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1904. }
  1905. static unsigned int intel_tile_width(const struct drm_i915_private *dev_priv,
  1906. uint64_t fb_modifier, unsigned int cpp)
  1907. {
  1908. switch (fb_modifier) {
  1909. case DRM_FORMAT_MOD_NONE:
  1910. return cpp;
  1911. case I915_FORMAT_MOD_X_TILED:
  1912. if (IS_GEN2(dev_priv))
  1913. return 128;
  1914. else
  1915. return 512;
  1916. case I915_FORMAT_MOD_Y_TILED:
  1917. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1918. return 128;
  1919. else
  1920. return 512;
  1921. case I915_FORMAT_MOD_Yf_TILED:
  1922. switch (cpp) {
  1923. case 1:
  1924. return 64;
  1925. case 2:
  1926. case 4:
  1927. return 128;
  1928. case 8:
  1929. case 16:
  1930. return 256;
  1931. default:
  1932. MISSING_CASE(cpp);
  1933. return cpp;
  1934. }
  1935. break;
  1936. default:
  1937. MISSING_CASE(fb_modifier);
  1938. return cpp;
  1939. }
  1940. }
  1941. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1942. uint64_t fb_modifier, unsigned int cpp)
  1943. {
  1944. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1945. return 1;
  1946. else
  1947. return intel_tile_size(dev_priv) /
  1948. intel_tile_width(dev_priv, fb_modifier, cpp);
  1949. }
  1950. unsigned int
  1951. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1952. uint32_t pixel_format, uint64_t fb_modifier)
  1953. {
  1954. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1955. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1956. return ALIGN(height, tile_height);
  1957. }
  1958. static void
  1959. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1960. const struct drm_plane_state *plane_state)
  1961. {
  1962. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1963. struct intel_rotation_info *info = &view->params.rotated;
  1964. unsigned int tile_size, tile_width, tile_height, cpp;
  1965. *view = i915_ggtt_view_normal;
  1966. if (!plane_state)
  1967. return;
  1968. if (!intel_rotation_90_or_270(plane_state->rotation))
  1969. return;
  1970. *view = i915_ggtt_view_rotated;
  1971. info->height = fb->height;
  1972. info->pixel_format = fb->pixel_format;
  1973. info->pitch = fb->pitches[0];
  1974. info->uv_offset = fb->offsets[1];
  1975. info->fb_modifier = fb->modifier[0];
  1976. tile_size = intel_tile_size(dev_priv);
  1977. cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  1978. tile_width = intel_tile_width(dev_priv, fb->modifier[0], cpp);
  1979. tile_height = tile_size / tile_width;
  1980. info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_width);
  1981. info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
  1982. info->size = info->width_pages * info->height_pages * tile_size;
  1983. if (info->pixel_format == DRM_FORMAT_NV12) {
  1984. cpp = drm_format_plane_cpp(fb->pixel_format, 1);
  1985. tile_width = intel_tile_width(dev_priv, fb->modifier[1], cpp);
  1986. tile_height = tile_size / tile_width;
  1987. info->width_pages_uv = DIV_ROUND_UP(fb->pitches[1], tile_width);
  1988. info->height_pages_uv = DIV_ROUND_UP(fb->height / 2, tile_height);
  1989. info->size_uv = info->width_pages_uv * info->height_pages_uv * tile_size;
  1990. }
  1991. }
  1992. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1993. {
  1994. if (INTEL_INFO(dev_priv)->gen >= 9)
  1995. return 256 * 1024;
  1996. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1997. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1998. return 128 * 1024;
  1999. else if (INTEL_INFO(dev_priv)->gen >= 4)
  2000. return 4 * 1024;
  2001. else
  2002. return 0;
  2003. }
  2004. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  2005. uint64_t fb_modifier)
  2006. {
  2007. switch (fb_modifier) {
  2008. case DRM_FORMAT_MOD_NONE:
  2009. return intel_linear_alignment(dev_priv);
  2010. case I915_FORMAT_MOD_X_TILED:
  2011. if (INTEL_INFO(dev_priv)->gen >= 9)
  2012. return 256 * 1024;
  2013. return 0;
  2014. case I915_FORMAT_MOD_Y_TILED:
  2015. case I915_FORMAT_MOD_Yf_TILED:
  2016. return 1 * 1024 * 1024;
  2017. default:
  2018. MISSING_CASE(fb_modifier);
  2019. return 0;
  2020. }
  2021. }
  2022. int
  2023. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2024. struct drm_framebuffer *fb,
  2025. const struct drm_plane_state *plane_state)
  2026. {
  2027. struct drm_device *dev = fb->dev;
  2028. struct drm_i915_private *dev_priv = dev->dev_private;
  2029. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2030. struct i915_ggtt_view view;
  2031. u32 alignment;
  2032. int ret;
  2033. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2034. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  2035. intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2036. /* Note that the w/a also requires 64 PTE of padding following the
  2037. * bo. We currently fill all unused PTE with the shadow page and so
  2038. * we should always have valid PTE following the scanout preventing
  2039. * the VT-d warning.
  2040. */
  2041. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2042. alignment = 256 * 1024;
  2043. /*
  2044. * Global gtt pte registers are special registers which actually forward
  2045. * writes to a chunk of system memory. Which means that there is no risk
  2046. * that the register values disappear as soon as we call
  2047. * intel_runtime_pm_put(), so it is correct to wrap only the
  2048. * pin/unpin/fence and not more.
  2049. */
  2050. intel_runtime_pm_get(dev_priv);
  2051. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  2052. &view);
  2053. if (ret)
  2054. goto err_pm;
  2055. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2056. * fence, whereas 965+ only requires a fence if using
  2057. * framebuffer compression. For simplicity, we always install
  2058. * a fence as the cost is not that onerous.
  2059. */
  2060. if (view.type == I915_GGTT_VIEW_NORMAL) {
  2061. ret = i915_gem_object_get_fence(obj);
  2062. if (ret == -EDEADLK) {
  2063. /*
  2064. * -EDEADLK means there are no free fences
  2065. * no pending flips.
  2066. *
  2067. * This is propagated to atomic, but it uses
  2068. * -EDEADLK to force a locking recovery, so
  2069. * change the returned error to -EBUSY.
  2070. */
  2071. ret = -EBUSY;
  2072. goto err_unpin;
  2073. } else if (ret)
  2074. goto err_unpin;
  2075. i915_gem_object_pin_fence(obj);
  2076. }
  2077. intel_runtime_pm_put(dev_priv);
  2078. return 0;
  2079. err_unpin:
  2080. i915_gem_object_unpin_from_display_plane(obj, &view);
  2081. err_pm:
  2082. intel_runtime_pm_put(dev_priv);
  2083. return ret;
  2084. }
  2085. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2086. const struct drm_plane_state *plane_state)
  2087. {
  2088. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2089. struct i915_ggtt_view view;
  2090. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2091. intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2092. if (view.type == I915_GGTT_VIEW_NORMAL)
  2093. i915_gem_object_unpin_fence(obj);
  2094. i915_gem_object_unpin_from_display_plane(obj, &view);
  2095. }
  2096. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2097. * is assumed to be a power-of-two. */
  2098. u32 intel_compute_tile_offset(struct drm_i915_private *dev_priv,
  2099. int *x, int *y,
  2100. uint64_t fb_modifier,
  2101. unsigned int cpp,
  2102. unsigned int pitch)
  2103. {
  2104. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2105. unsigned int tile_size, tile_width, tile_height;
  2106. unsigned int tile_rows, tiles;
  2107. tile_size = intel_tile_size(dev_priv);
  2108. tile_width = intel_tile_width(dev_priv, fb_modifier, cpp);
  2109. tile_height = tile_size / tile_width;
  2110. tile_rows = *y / tile_height;
  2111. *y %= tile_height;
  2112. tiles = *x / (tile_width/cpp);
  2113. *x %= tile_width/cpp;
  2114. return tile_rows * pitch * tile_height + tiles * tile_size;
  2115. } else {
  2116. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2117. unsigned int offset;
  2118. offset = *y * pitch + *x * cpp;
  2119. *y = (offset & alignment) / pitch;
  2120. *x = ((offset & alignment) - *y * pitch) / cpp;
  2121. return offset & ~alignment;
  2122. }
  2123. }
  2124. static int i9xx_format_to_fourcc(int format)
  2125. {
  2126. switch (format) {
  2127. case DISPPLANE_8BPP:
  2128. return DRM_FORMAT_C8;
  2129. case DISPPLANE_BGRX555:
  2130. return DRM_FORMAT_XRGB1555;
  2131. case DISPPLANE_BGRX565:
  2132. return DRM_FORMAT_RGB565;
  2133. default:
  2134. case DISPPLANE_BGRX888:
  2135. return DRM_FORMAT_XRGB8888;
  2136. case DISPPLANE_RGBX888:
  2137. return DRM_FORMAT_XBGR8888;
  2138. case DISPPLANE_BGRX101010:
  2139. return DRM_FORMAT_XRGB2101010;
  2140. case DISPPLANE_RGBX101010:
  2141. return DRM_FORMAT_XBGR2101010;
  2142. }
  2143. }
  2144. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2145. {
  2146. switch (format) {
  2147. case PLANE_CTL_FORMAT_RGB_565:
  2148. return DRM_FORMAT_RGB565;
  2149. default:
  2150. case PLANE_CTL_FORMAT_XRGB_8888:
  2151. if (rgb_order) {
  2152. if (alpha)
  2153. return DRM_FORMAT_ABGR8888;
  2154. else
  2155. return DRM_FORMAT_XBGR8888;
  2156. } else {
  2157. if (alpha)
  2158. return DRM_FORMAT_ARGB8888;
  2159. else
  2160. return DRM_FORMAT_XRGB8888;
  2161. }
  2162. case PLANE_CTL_FORMAT_XRGB_2101010:
  2163. if (rgb_order)
  2164. return DRM_FORMAT_XBGR2101010;
  2165. else
  2166. return DRM_FORMAT_XRGB2101010;
  2167. }
  2168. }
  2169. static bool
  2170. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2171. struct intel_initial_plane_config *plane_config)
  2172. {
  2173. struct drm_device *dev = crtc->base.dev;
  2174. struct drm_i915_private *dev_priv = to_i915(dev);
  2175. struct drm_i915_gem_object *obj = NULL;
  2176. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2177. struct drm_framebuffer *fb = &plane_config->fb->base;
  2178. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2179. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2180. PAGE_SIZE);
  2181. size_aligned -= base_aligned;
  2182. if (plane_config->size == 0)
  2183. return false;
  2184. /* If the FB is too big, just don't use it since fbdev is not very
  2185. * important and we should probably use that space with FBC or other
  2186. * features. */
  2187. if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
  2188. return false;
  2189. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2190. base_aligned,
  2191. base_aligned,
  2192. size_aligned);
  2193. if (!obj)
  2194. return false;
  2195. obj->tiling_mode = plane_config->tiling;
  2196. if (obj->tiling_mode == I915_TILING_X)
  2197. obj->stride = fb->pitches[0];
  2198. mode_cmd.pixel_format = fb->pixel_format;
  2199. mode_cmd.width = fb->width;
  2200. mode_cmd.height = fb->height;
  2201. mode_cmd.pitches[0] = fb->pitches[0];
  2202. mode_cmd.modifier[0] = fb->modifier[0];
  2203. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2204. mutex_lock(&dev->struct_mutex);
  2205. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2206. &mode_cmd, obj)) {
  2207. DRM_DEBUG_KMS("intel fb init failed\n");
  2208. goto out_unref_obj;
  2209. }
  2210. mutex_unlock(&dev->struct_mutex);
  2211. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2212. return true;
  2213. out_unref_obj:
  2214. drm_gem_object_unreference(&obj->base);
  2215. mutex_unlock(&dev->struct_mutex);
  2216. return false;
  2217. }
  2218. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2219. static void
  2220. update_state_fb(struct drm_plane *plane)
  2221. {
  2222. if (plane->fb == plane->state->fb)
  2223. return;
  2224. if (plane->state->fb)
  2225. drm_framebuffer_unreference(plane->state->fb);
  2226. plane->state->fb = plane->fb;
  2227. if (plane->state->fb)
  2228. drm_framebuffer_reference(plane->state->fb);
  2229. }
  2230. static void
  2231. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2232. struct intel_initial_plane_config *plane_config)
  2233. {
  2234. struct drm_device *dev = intel_crtc->base.dev;
  2235. struct drm_i915_private *dev_priv = dev->dev_private;
  2236. struct drm_crtc *c;
  2237. struct intel_crtc *i;
  2238. struct drm_i915_gem_object *obj;
  2239. struct drm_plane *primary = intel_crtc->base.primary;
  2240. struct drm_plane_state *plane_state = primary->state;
  2241. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2242. struct intel_plane *intel_plane = to_intel_plane(primary);
  2243. struct intel_plane_state *intel_state =
  2244. to_intel_plane_state(plane_state);
  2245. struct drm_framebuffer *fb;
  2246. if (!plane_config->fb)
  2247. return;
  2248. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2249. fb = &plane_config->fb->base;
  2250. goto valid_fb;
  2251. }
  2252. kfree(plane_config->fb);
  2253. /*
  2254. * Failed to alloc the obj, check to see if we should share
  2255. * an fb with another CRTC instead
  2256. */
  2257. for_each_crtc(dev, c) {
  2258. i = to_intel_crtc(c);
  2259. if (c == &intel_crtc->base)
  2260. continue;
  2261. if (!i->active)
  2262. continue;
  2263. fb = c->primary->fb;
  2264. if (!fb)
  2265. continue;
  2266. obj = intel_fb_obj(fb);
  2267. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2268. drm_framebuffer_reference(fb);
  2269. goto valid_fb;
  2270. }
  2271. }
  2272. /*
  2273. * We've failed to reconstruct the BIOS FB. Current display state
  2274. * indicates that the primary plane is visible, but has a NULL FB,
  2275. * which will lead to problems later if we don't fix it up. The
  2276. * simplest solution is to just disable the primary plane now and
  2277. * pretend the BIOS never had it enabled.
  2278. */
  2279. to_intel_plane_state(plane_state)->visible = false;
  2280. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2281. intel_pre_disable_primary(&intel_crtc->base);
  2282. intel_plane->disable_plane(primary, &intel_crtc->base);
  2283. return;
  2284. valid_fb:
  2285. plane_state->src_x = 0;
  2286. plane_state->src_y = 0;
  2287. plane_state->src_w = fb->width << 16;
  2288. plane_state->src_h = fb->height << 16;
  2289. plane_state->crtc_x = 0;
  2290. plane_state->crtc_y = 0;
  2291. plane_state->crtc_w = fb->width;
  2292. plane_state->crtc_h = fb->height;
  2293. intel_state->src.x1 = plane_state->src_x;
  2294. intel_state->src.y1 = plane_state->src_y;
  2295. intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
  2296. intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
  2297. intel_state->dst.x1 = plane_state->crtc_x;
  2298. intel_state->dst.y1 = plane_state->crtc_y;
  2299. intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2300. intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2301. obj = intel_fb_obj(fb);
  2302. if (obj->tiling_mode != I915_TILING_NONE)
  2303. dev_priv->preserve_bios_swizzle = true;
  2304. drm_framebuffer_reference(fb);
  2305. primary->fb = primary->state->fb = fb;
  2306. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2307. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2308. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2309. }
  2310. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2311. const struct intel_crtc_state *crtc_state,
  2312. const struct intel_plane_state *plane_state)
  2313. {
  2314. struct drm_device *dev = primary->dev;
  2315. struct drm_i915_private *dev_priv = dev->dev_private;
  2316. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2317. struct drm_framebuffer *fb = plane_state->base.fb;
  2318. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2319. int plane = intel_crtc->plane;
  2320. u32 linear_offset;
  2321. u32 dspcntr;
  2322. i915_reg_t reg = DSPCNTR(plane);
  2323. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2324. int x = plane_state->src.x1 >> 16;
  2325. int y = plane_state->src.y1 >> 16;
  2326. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2327. dspcntr |= DISPLAY_PLANE_ENABLE;
  2328. if (INTEL_INFO(dev)->gen < 4) {
  2329. if (intel_crtc->pipe == PIPE_B)
  2330. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2331. /* pipesrc and dspsize control the size that is scaled from,
  2332. * which should always be the user's requested size.
  2333. */
  2334. I915_WRITE(DSPSIZE(plane),
  2335. ((crtc_state->pipe_src_h - 1) << 16) |
  2336. (crtc_state->pipe_src_w - 1));
  2337. I915_WRITE(DSPPOS(plane), 0);
  2338. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2339. I915_WRITE(PRIMSIZE(plane),
  2340. ((crtc_state->pipe_src_h - 1) << 16) |
  2341. (crtc_state->pipe_src_w - 1));
  2342. I915_WRITE(PRIMPOS(plane), 0);
  2343. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2344. }
  2345. switch (fb->pixel_format) {
  2346. case DRM_FORMAT_C8:
  2347. dspcntr |= DISPPLANE_8BPP;
  2348. break;
  2349. case DRM_FORMAT_XRGB1555:
  2350. dspcntr |= DISPPLANE_BGRX555;
  2351. break;
  2352. case DRM_FORMAT_RGB565:
  2353. dspcntr |= DISPPLANE_BGRX565;
  2354. break;
  2355. case DRM_FORMAT_XRGB8888:
  2356. dspcntr |= DISPPLANE_BGRX888;
  2357. break;
  2358. case DRM_FORMAT_XBGR8888:
  2359. dspcntr |= DISPPLANE_RGBX888;
  2360. break;
  2361. case DRM_FORMAT_XRGB2101010:
  2362. dspcntr |= DISPPLANE_BGRX101010;
  2363. break;
  2364. case DRM_FORMAT_XBGR2101010:
  2365. dspcntr |= DISPPLANE_RGBX101010;
  2366. break;
  2367. default:
  2368. BUG();
  2369. }
  2370. if (INTEL_INFO(dev)->gen >= 4 &&
  2371. obj->tiling_mode != I915_TILING_NONE)
  2372. dspcntr |= DISPPLANE_TILED;
  2373. if (IS_G4X(dev))
  2374. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2375. linear_offset = y * fb->pitches[0] + x * cpp;
  2376. if (INTEL_INFO(dev)->gen >= 4) {
  2377. intel_crtc->dspaddr_offset =
  2378. intel_compute_tile_offset(dev_priv, &x, &y,
  2379. fb->modifier[0], cpp,
  2380. fb->pitches[0]);
  2381. linear_offset -= intel_crtc->dspaddr_offset;
  2382. } else {
  2383. intel_crtc->dspaddr_offset = linear_offset;
  2384. }
  2385. if (plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  2386. dspcntr |= DISPPLANE_ROTATE_180;
  2387. x += (crtc_state->pipe_src_w - 1);
  2388. y += (crtc_state->pipe_src_h - 1);
  2389. /* Finding the last pixel of the last line of the display
  2390. data and adding to linear_offset*/
  2391. linear_offset +=
  2392. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2393. (crtc_state->pipe_src_w - 1) * cpp;
  2394. }
  2395. intel_crtc->adjusted_x = x;
  2396. intel_crtc->adjusted_y = y;
  2397. I915_WRITE(reg, dspcntr);
  2398. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2399. if (INTEL_INFO(dev)->gen >= 4) {
  2400. I915_WRITE(DSPSURF(plane),
  2401. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2402. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2403. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2404. } else
  2405. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2406. POSTING_READ(reg);
  2407. }
  2408. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2409. struct drm_crtc *crtc)
  2410. {
  2411. struct drm_device *dev = crtc->dev;
  2412. struct drm_i915_private *dev_priv = dev->dev_private;
  2413. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2414. int plane = intel_crtc->plane;
  2415. I915_WRITE(DSPCNTR(plane), 0);
  2416. if (INTEL_INFO(dev_priv)->gen >= 4)
  2417. I915_WRITE(DSPSURF(plane), 0);
  2418. else
  2419. I915_WRITE(DSPADDR(plane), 0);
  2420. POSTING_READ(DSPCNTR(plane));
  2421. }
  2422. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2423. const struct intel_crtc_state *crtc_state,
  2424. const struct intel_plane_state *plane_state)
  2425. {
  2426. struct drm_device *dev = primary->dev;
  2427. struct drm_i915_private *dev_priv = dev->dev_private;
  2428. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2429. struct drm_framebuffer *fb = plane_state->base.fb;
  2430. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2431. int plane = intel_crtc->plane;
  2432. u32 linear_offset;
  2433. u32 dspcntr;
  2434. i915_reg_t reg = DSPCNTR(plane);
  2435. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2436. int x = plane_state->src.x1 >> 16;
  2437. int y = plane_state->src.y1 >> 16;
  2438. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2439. dspcntr |= DISPLAY_PLANE_ENABLE;
  2440. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2441. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2442. switch (fb->pixel_format) {
  2443. case DRM_FORMAT_C8:
  2444. dspcntr |= DISPPLANE_8BPP;
  2445. break;
  2446. case DRM_FORMAT_RGB565:
  2447. dspcntr |= DISPPLANE_BGRX565;
  2448. break;
  2449. case DRM_FORMAT_XRGB8888:
  2450. dspcntr |= DISPPLANE_BGRX888;
  2451. break;
  2452. case DRM_FORMAT_XBGR8888:
  2453. dspcntr |= DISPPLANE_RGBX888;
  2454. break;
  2455. case DRM_FORMAT_XRGB2101010:
  2456. dspcntr |= DISPPLANE_BGRX101010;
  2457. break;
  2458. case DRM_FORMAT_XBGR2101010:
  2459. dspcntr |= DISPPLANE_RGBX101010;
  2460. break;
  2461. default:
  2462. BUG();
  2463. }
  2464. if (obj->tiling_mode != I915_TILING_NONE)
  2465. dspcntr |= DISPPLANE_TILED;
  2466. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2467. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2468. linear_offset = y * fb->pitches[0] + x * cpp;
  2469. intel_crtc->dspaddr_offset =
  2470. intel_compute_tile_offset(dev_priv, &x, &y,
  2471. fb->modifier[0], cpp,
  2472. fb->pitches[0]);
  2473. linear_offset -= intel_crtc->dspaddr_offset;
  2474. if (plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  2475. dspcntr |= DISPPLANE_ROTATE_180;
  2476. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2477. x += (crtc_state->pipe_src_w - 1);
  2478. y += (crtc_state->pipe_src_h - 1);
  2479. /* Finding the last pixel of the last line of the display
  2480. data and adding to linear_offset*/
  2481. linear_offset +=
  2482. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2483. (crtc_state->pipe_src_w - 1) * cpp;
  2484. }
  2485. }
  2486. intel_crtc->adjusted_x = x;
  2487. intel_crtc->adjusted_y = y;
  2488. I915_WRITE(reg, dspcntr);
  2489. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2490. I915_WRITE(DSPSURF(plane),
  2491. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2492. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2493. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2494. } else {
  2495. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2496. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2497. }
  2498. POSTING_READ(reg);
  2499. }
  2500. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2501. uint64_t fb_modifier, uint32_t pixel_format)
  2502. {
  2503. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2504. return 64;
  2505. } else {
  2506. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2507. return intel_tile_width(dev_priv, fb_modifier, cpp);
  2508. }
  2509. }
  2510. u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
  2511. struct drm_i915_gem_object *obj,
  2512. unsigned int plane)
  2513. {
  2514. struct i915_ggtt_view view;
  2515. struct i915_vma *vma;
  2516. u64 offset;
  2517. intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
  2518. intel_plane->base.state);
  2519. vma = i915_gem_obj_to_ggtt_view(obj, &view);
  2520. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2521. view.type))
  2522. return -1;
  2523. offset = vma->node.start;
  2524. if (plane == 1) {
  2525. offset += vma->ggtt_view.params.rotated.uv_start_page *
  2526. PAGE_SIZE;
  2527. }
  2528. WARN_ON(upper_32_bits(offset));
  2529. return lower_32_bits(offset);
  2530. }
  2531. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2532. {
  2533. struct drm_device *dev = intel_crtc->base.dev;
  2534. struct drm_i915_private *dev_priv = dev->dev_private;
  2535. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2536. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2537. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2538. }
  2539. /*
  2540. * This function detaches (aka. unbinds) unused scalers in hardware
  2541. */
  2542. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2543. {
  2544. struct intel_crtc_scaler_state *scaler_state;
  2545. int i;
  2546. scaler_state = &intel_crtc->config->scaler_state;
  2547. /* loop through and disable scalers that aren't in use */
  2548. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2549. if (!scaler_state->scalers[i].in_use)
  2550. skl_detach_scaler(intel_crtc, i);
  2551. }
  2552. }
  2553. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2554. {
  2555. switch (pixel_format) {
  2556. case DRM_FORMAT_C8:
  2557. return PLANE_CTL_FORMAT_INDEXED;
  2558. case DRM_FORMAT_RGB565:
  2559. return PLANE_CTL_FORMAT_RGB_565;
  2560. case DRM_FORMAT_XBGR8888:
  2561. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2562. case DRM_FORMAT_XRGB8888:
  2563. return PLANE_CTL_FORMAT_XRGB_8888;
  2564. /*
  2565. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2566. * to be already pre-multiplied. We need to add a knob (or a different
  2567. * DRM_FORMAT) for user-space to configure that.
  2568. */
  2569. case DRM_FORMAT_ABGR8888:
  2570. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2571. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2572. case DRM_FORMAT_ARGB8888:
  2573. return PLANE_CTL_FORMAT_XRGB_8888 |
  2574. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2575. case DRM_FORMAT_XRGB2101010:
  2576. return PLANE_CTL_FORMAT_XRGB_2101010;
  2577. case DRM_FORMAT_XBGR2101010:
  2578. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2579. case DRM_FORMAT_YUYV:
  2580. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2581. case DRM_FORMAT_YVYU:
  2582. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2583. case DRM_FORMAT_UYVY:
  2584. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2585. case DRM_FORMAT_VYUY:
  2586. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2587. default:
  2588. MISSING_CASE(pixel_format);
  2589. }
  2590. return 0;
  2591. }
  2592. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2593. {
  2594. switch (fb_modifier) {
  2595. case DRM_FORMAT_MOD_NONE:
  2596. break;
  2597. case I915_FORMAT_MOD_X_TILED:
  2598. return PLANE_CTL_TILED_X;
  2599. case I915_FORMAT_MOD_Y_TILED:
  2600. return PLANE_CTL_TILED_Y;
  2601. case I915_FORMAT_MOD_Yf_TILED:
  2602. return PLANE_CTL_TILED_YF;
  2603. default:
  2604. MISSING_CASE(fb_modifier);
  2605. }
  2606. return 0;
  2607. }
  2608. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2609. {
  2610. switch (rotation) {
  2611. case BIT(DRM_ROTATE_0):
  2612. break;
  2613. /*
  2614. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2615. * while i915 HW rotation is clockwise, thats why this swapping.
  2616. */
  2617. case BIT(DRM_ROTATE_90):
  2618. return PLANE_CTL_ROTATE_270;
  2619. case BIT(DRM_ROTATE_180):
  2620. return PLANE_CTL_ROTATE_180;
  2621. case BIT(DRM_ROTATE_270):
  2622. return PLANE_CTL_ROTATE_90;
  2623. default:
  2624. MISSING_CASE(rotation);
  2625. }
  2626. return 0;
  2627. }
  2628. static void skylake_update_primary_plane(struct drm_plane *plane,
  2629. const struct intel_crtc_state *crtc_state,
  2630. const struct intel_plane_state *plane_state)
  2631. {
  2632. struct drm_device *dev = plane->dev;
  2633. struct drm_i915_private *dev_priv = dev->dev_private;
  2634. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2635. struct drm_framebuffer *fb = plane_state->base.fb;
  2636. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2637. int pipe = intel_crtc->pipe;
  2638. u32 plane_ctl, stride_div, stride;
  2639. u32 tile_height, plane_offset, plane_size;
  2640. unsigned int rotation = plane_state->base.rotation;
  2641. int x_offset, y_offset;
  2642. u32 surf_addr;
  2643. int scaler_id = plane_state->scaler_id;
  2644. int src_x = plane_state->src.x1 >> 16;
  2645. int src_y = plane_state->src.y1 >> 16;
  2646. int src_w = drm_rect_width(&plane_state->src) >> 16;
  2647. int src_h = drm_rect_height(&plane_state->src) >> 16;
  2648. int dst_x = plane_state->dst.x1;
  2649. int dst_y = plane_state->dst.y1;
  2650. int dst_w = drm_rect_width(&plane_state->dst);
  2651. int dst_h = drm_rect_height(&plane_state->dst);
  2652. plane_ctl = PLANE_CTL_ENABLE |
  2653. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2654. PLANE_CTL_PIPE_CSC_ENABLE;
  2655. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2656. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2657. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2658. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2659. stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2660. fb->pixel_format);
  2661. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2662. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2663. if (intel_rotation_90_or_270(rotation)) {
  2664. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2665. /* stride = Surface height in tiles */
  2666. tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2667. stride = DIV_ROUND_UP(fb->height, tile_height);
  2668. x_offset = stride * tile_height - src_y - src_h;
  2669. y_offset = src_x;
  2670. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2671. } else {
  2672. stride = fb->pitches[0] / stride_div;
  2673. x_offset = src_x;
  2674. y_offset = src_y;
  2675. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2676. }
  2677. plane_offset = y_offset << 16 | x_offset;
  2678. intel_crtc->adjusted_x = x_offset;
  2679. intel_crtc->adjusted_y = y_offset;
  2680. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2681. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2682. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2683. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2684. if (scaler_id >= 0) {
  2685. uint32_t ps_ctrl = 0;
  2686. WARN_ON(!dst_w || !dst_h);
  2687. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2688. crtc_state->scaler_state.scalers[scaler_id].mode;
  2689. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2690. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2691. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2692. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2693. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2694. } else {
  2695. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2696. }
  2697. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2698. POSTING_READ(PLANE_SURF(pipe, 0));
  2699. }
  2700. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2701. struct drm_crtc *crtc)
  2702. {
  2703. struct drm_device *dev = crtc->dev;
  2704. struct drm_i915_private *dev_priv = dev->dev_private;
  2705. int pipe = to_intel_crtc(crtc)->pipe;
  2706. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2707. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2708. POSTING_READ(PLANE_SURF(pipe, 0));
  2709. }
  2710. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2711. static int
  2712. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2713. int x, int y, enum mode_set_atomic state)
  2714. {
  2715. /* Support for kgdboc is disabled, this needs a major rework. */
  2716. DRM_ERROR("legacy panic handler not supported any more.\n");
  2717. return -ENODEV;
  2718. }
  2719. static void intel_complete_page_flips(struct drm_device *dev)
  2720. {
  2721. struct drm_crtc *crtc;
  2722. for_each_crtc(dev, crtc) {
  2723. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2724. enum plane plane = intel_crtc->plane;
  2725. intel_prepare_page_flip(dev, plane);
  2726. intel_finish_page_flip_plane(dev, plane);
  2727. }
  2728. }
  2729. static void intel_update_primary_planes(struct drm_device *dev)
  2730. {
  2731. struct drm_crtc *crtc;
  2732. for_each_crtc(dev, crtc) {
  2733. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2734. struct intel_plane_state *plane_state;
  2735. drm_modeset_lock_crtc(crtc, &plane->base);
  2736. plane_state = to_intel_plane_state(plane->base.state);
  2737. if (plane_state->visible)
  2738. plane->update_plane(&plane->base,
  2739. to_intel_crtc_state(crtc->state),
  2740. plane_state);
  2741. drm_modeset_unlock_crtc(crtc);
  2742. }
  2743. }
  2744. void intel_prepare_reset(struct drm_device *dev)
  2745. {
  2746. /* no reset support for gen2 */
  2747. if (IS_GEN2(dev))
  2748. return;
  2749. /* reset doesn't touch the display */
  2750. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2751. return;
  2752. drm_modeset_lock_all(dev);
  2753. /*
  2754. * Disabling the crtcs gracefully seems nicer. Also the
  2755. * g33 docs say we should at least disable all the planes.
  2756. */
  2757. intel_display_suspend(dev);
  2758. }
  2759. void intel_finish_reset(struct drm_device *dev)
  2760. {
  2761. struct drm_i915_private *dev_priv = to_i915(dev);
  2762. /*
  2763. * Flips in the rings will be nuked by the reset,
  2764. * so complete all pending flips so that user space
  2765. * will get its events and not get stuck.
  2766. */
  2767. intel_complete_page_flips(dev);
  2768. /* no reset support for gen2 */
  2769. if (IS_GEN2(dev))
  2770. return;
  2771. /* reset doesn't touch the display */
  2772. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2773. /*
  2774. * Flips in the rings have been nuked by the reset,
  2775. * so update the base address of all primary
  2776. * planes to the the last fb to make sure we're
  2777. * showing the correct fb after a reset.
  2778. *
  2779. * FIXME: Atomic will make this obsolete since we won't schedule
  2780. * CS-based flips (which might get lost in gpu resets) any more.
  2781. */
  2782. intel_update_primary_planes(dev);
  2783. return;
  2784. }
  2785. /*
  2786. * The display has been reset as well,
  2787. * so need a full re-initialization.
  2788. */
  2789. intel_runtime_pm_disable_interrupts(dev_priv);
  2790. intel_runtime_pm_enable_interrupts(dev_priv);
  2791. intel_modeset_init_hw(dev);
  2792. spin_lock_irq(&dev_priv->irq_lock);
  2793. if (dev_priv->display.hpd_irq_setup)
  2794. dev_priv->display.hpd_irq_setup(dev);
  2795. spin_unlock_irq(&dev_priv->irq_lock);
  2796. intel_display_resume(dev);
  2797. intel_hpd_init(dev_priv);
  2798. drm_modeset_unlock_all(dev);
  2799. }
  2800. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2801. {
  2802. struct drm_device *dev = crtc->dev;
  2803. struct drm_i915_private *dev_priv = dev->dev_private;
  2804. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2805. bool pending;
  2806. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2807. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2808. return false;
  2809. spin_lock_irq(&dev->event_lock);
  2810. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2811. spin_unlock_irq(&dev->event_lock);
  2812. return pending;
  2813. }
  2814. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2815. struct intel_crtc_state *old_crtc_state)
  2816. {
  2817. struct drm_device *dev = crtc->base.dev;
  2818. struct drm_i915_private *dev_priv = dev->dev_private;
  2819. struct intel_crtc_state *pipe_config =
  2820. to_intel_crtc_state(crtc->base.state);
  2821. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2822. crtc->base.mode = crtc->base.state->mode;
  2823. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2824. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2825. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2826. if (HAS_DDI(dev))
  2827. intel_set_pipe_csc(&crtc->base);
  2828. /*
  2829. * Update pipe size and adjust fitter if needed: the reason for this is
  2830. * that in compute_mode_changes we check the native mode (not the pfit
  2831. * mode) to see if we can flip rather than do a full mode set. In the
  2832. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2833. * pfit state, we'll end up with a big fb scanned out into the wrong
  2834. * sized surface.
  2835. */
  2836. I915_WRITE(PIPESRC(crtc->pipe),
  2837. ((pipe_config->pipe_src_w - 1) << 16) |
  2838. (pipe_config->pipe_src_h - 1));
  2839. /* on skylake this is done by detaching scalers */
  2840. if (INTEL_INFO(dev)->gen >= 9) {
  2841. skl_detach_scalers(crtc);
  2842. if (pipe_config->pch_pfit.enabled)
  2843. skylake_pfit_enable(crtc);
  2844. } else if (HAS_PCH_SPLIT(dev)) {
  2845. if (pipe_config->pch_pfit.enabled)
  2846. ironlake_pfit_enable(crtc);
  2847. else if (old_crtc_state->pch_pfit.enabled)
  2848. ironlake_pfit_disable(crtc, true);
  2849. }
  2850. }
  2851. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2852. {
  2853. struct drm_device *dev = crtc->dev;
  2854. struct drm_i915_private *dev_priv = dev->dev_private;
  2855. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2856. int pipe = intel_crtc->pipe;
  2857. i915_reg_t reg;
  2858. u32 temp;
  2859. /* enable normal train */
  2860. reg = FDI_TX_CTL(pipe);
  2861. temp = I915_READ(reg);
  2862. if (IS_IVYBRIDGE(dev)) {
  2863. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2864. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2865. } else {
  2866. temp &= ~FDI_LINK_TRAIN_NONE;
  2867. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2868. }
  2869. I915_WRITE(reg, temp);
  2870. reg = FDI_RX_CTL(pipe);
  2871. temp = I915_READ(reg);
  2872. if (HAS_PCH_CPT(dev)) {
  2873. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2874. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2875. } else {
  2876. temp &= ~FDI_LINK_TRAIN_NONE;
  2877. temp |= FDI_LINK_TRAIN_NONE;
  2878. }
  2879. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2880. /* wait one idle pattern time */
  2881. POSTING_READ(reg);
  2882. udelay(1000);
  2883. /* IVB wants error correction enabled */
  2884. if (IS_IVYBRIDGE(dev))
  2885. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2886. FDI_FE_ERRC_ENABLE);
  2887. }
  2888. /* The FDI link training functions for ILK/Ibexpeak. */
  2889. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2890. {
  2891. struct drm_device *dev = crtc->dev;
  2892. struct drm_i915_private *dev_priv = dev->dev_private;
  2893. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2894. int pipe = intel_crtc->pipe;
  2895. i915_reg_t reg;
  2896. u32 temp, tries;
  2897. /* FDI needs bits from pipe first */
  2898. assert_pipe_enabled(dev_priv, pipe);
  2899. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2900. for train result */
  2901. reg = FDI_RX_IMR(pipe);
  2902. temp = I915_READ(reg);
  2903. temp &= ~FDI_RX_SYMBOL_LOCK;
  2904. temp &= ~FDI_RX_BIT_LOCK;
  2905. I915_WRITE(reg, temp);
  2906. I915_READ(reg);
  2907. udelay(150);
  2908. /* enable CPU FDI TX and PCH FDI RX */
  2909. reg = FDI_TX_CTL(pipe);
  2910. temp = I915_READ(reg);
  2911. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2912. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2913. temp &= ~FDI_LINK_TRAIN_NONE;
  2914. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2915. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2916. reg = FDI_RX_CTL(pipe);
  2917. temp = I915_READ(reg);
  2918. temp &= ~FDI_LINK_TRAIN_NONE;
  2919. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2920. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2921. POSTING_READ(reg);
  2922. udelay(150);
  2923. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2924. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2925. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2926. FDI_RX_PHASE_SYNC_POINTER_EN);
  2927. reg = FDI_RX_IIR(pipe);
  2928. for (tries = 0; tries < 5; tries++) {
  2929. temp = I915_READ(reg);
  2930. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2931. if ((temp & FDI_RX_BIT_LOCK)) {
  2932. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2933. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2934. break;
  2935. }
  2936. }
  2937. if (tries == 5)
  2938. DRM_ERROR("FDI train 1 fail!\n");
  2939. /* Train 2 */
  2940. reg = FDI_TX_CTL(pipe);
  2941. temp = I915_READ(reg);
  2942. temp &= ~FDI_LINK_TRAIN_NONE;
  2943. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2944. I915_WRITE(reg, temp);
  2945. reg = FDI_RX_CTL(pipe);
  2946. temp = I915_READ(reg);
  2947. temp &= ~FDI_LINK_TRAIN_NONE;
  2948. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2949. I915_WRITE(reg, temp);
  2950. POSTING_READ(reg);
  2951. udelay(150);
  2952. reg = FDI_RX_IIR(pipe);
  2953. for (tries = 0; tries < 5; tries++) {
  2954. temp = I915_READ(reg);
  2955. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2956. if (temp & FDI_RX_SYMBOL_LOCK) {
  2957. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2958. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2959. break;
  2960. }
  2961. }
  2962. if (tries == 5)
  2963. DRM_ERROR("FDI train 2 fail!\n");
  2964. DRM_DEBUG_KMS("FDI train done\n");
  2965. }
  2966. static const int snb_b_fdi_train_param[] = {
  2967. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2968. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2969. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2970. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2971. };
  2972. /* The FDI link training functions for SNB/Cougarpoint. */
  2973. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2974. {
  2975. struct drm_device *dev = crtc->dev;
  2976. struct drm_i915_private *dev_priv = dev->dev_private;
  2977. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2978. int pipe = intel_crtc->pipe;
  2979. i915_reg_t reg;
  2980. u32 temp, i, retry;
  2981. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2982. for train result */
  2983. reg = FDI_RX_IMR(pipe);
  2984. temp = I915_READ(reg);
  2985. temp &= ~FDI_RX_SYMBOL_LOCK;
  2986. temp &= ~FDI_RX_BIT_LOCK;
  2987. I915_WRITE(reg, temp);
  2988. POSTING_READ(reg);
  2989. udelay(150);
  2990. /* enable CPU FDI TX and PCH FDI RX */
  2991. reg = FDI_TX_CTL(pipe);
  2992. temp = I915_READ(reg);
  2993. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2994. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2995. temp &= ~FDI_LINK_TRAIN_NONE;
  2996. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2997. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2998. /* SNB-B */
  2999. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3000. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3001. I915_WRITE(FDI_RX_MISC(pipe),
  3002. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3003. reg = FDI_RX_CTL(pipe);
  3004. temp = I915_READ(reg);
  3005. if (HAS_PCH_CPT(dev)) {
  3006. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3007. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3008. } else {
  3009. temp &= ~FDI_LINK_TRAIN_NONE;
  3010. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3011. }
  3012. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3013. POSTING_READ(reg);
  3014. udelay(150);
  3015. for (i = 0; i < 4; i++) {
  3016. reg = FDI_TX_CTL(pipe);
  3017. temp = I915_READ(reg);
  3018. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3019. temp |= snb_b_fdi_train_param[i];
  3020. I915_WRITE(reg, temp);
  3021. POSTING_READ(reg);
  3022. udelay(500);
  3023. for (retry = 0; retry < 5; retry++) {
  3024. reg = FDI_RX_IIR(pipe);
  3025. temp = I915_READ(reg);
  3026. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3027. if (temp & FDI_RX_BIT_LOCK) {
  3028. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3029. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3030. break;
  3031. }
  3032. udelay(50);
  3033. }
  3034. if (retry < 5)
  3035. break;
  3036. }
  3037. if (i == 4)
  3038. DRM_ERROR("FDI train 1 fail!\n");
  3039. /* Train 2 */
  3040. reg = FDI_TX_CTL(pipe);
  3041. temp = I915_READ(reg);
  3042. temp &= ~FDI_LINK_TRAIN_NONE;
  3043. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3044. if (IS_GEN6(dev)) {
  3045. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3046. /* SNB-B */
  3047. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3048. }
  3049. I915_WRITE(reg, temp);
  3050. reg = FDI_RX_CTL(pipe);
  3051. temp = I915_READ(reg);
  3052. if (HAS_PCH_CPT(dev)) {
  3053. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3054. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3055. } else {
  3056. temp &= ~FDI_LINK_TRAIN_NONE;
  3057. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3058. }
  3059. I915_WRITE(reg, temp);
  3060. POSTING_READ(reg);
  3061. udelay(150);
  3062. for (i = 0; i < 4; i++) {
  3063. reg = FDI_TX_CTL(pipe);
  3064. temp = I915_READ(reg);
  3065. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3066. temp |= snb_b_fdi_train_param[i];
  3067. I915_WRITE(reg, temp);
  3068. POSTING_READ(reg);
  3069. udelay(500);
  3070. for (retry = 0; retry < 5; retry++) {
  3071. reg = FDI_RX_IIR(pipe);
  3072. temp = I915_READ(reg);
  3073. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3074. if (temp & FDI_RX_SYMBOL_LOCK) {
  3075. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3076. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3077. break;
  3078. }
  3079. udelay(50);
  3080. }
  3081. if (retry < 5)
  3082. break;
  3083. }
  3084. if (i == 4)
  3085. DRM_ERROR("FDI train 2 fail!\n");
  3086. DRM_DEBUG_KMS("FDI train done.\n");
  3087. }
  3088. /* Manual link training for Ivy Bridge A0 parts */
  3089. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3090. {
  3091. struct drm_device *dev = crtc->dev;
  3092. struct drm_i915_private *dev_priv = dev->dev_private;
  3093. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3094. int pipe = intel_crtc->pipe;
  3095. i915_reg_t reg;
  3096. u32 temp, i, j;
  3097. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3098. for train result */
  3099. reg = FDI_RX_IMR(pipe);
  3100. temp = I915_READ(reg);
  3101. temp &= ~FDI_RX_SYMBOL_LOCK;
  3102. temp &= ~FDI_RX_BIT_LOCK;
  3103. I915_WRITE(reg, temp);
  3104. POSTING_READ(reg);
  3105. udelay(150);
  3106. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3107. I915_READ(FDI_RX_IIR(pipe)));
  3108. /* Try each vswing and preemphasis setting twice before moving on */
  3109. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3110. /* disable first in case we need to retry */
  3111. reg = FDI_TX_CTL(pipe);
  3112. temp = I915_READ(reg);
  3113. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3114. temp &= ~FDI_TX_ENABLE;
  3115. I915_WRITE(reg, temp);
  3116. reg = FDI_RX_CTL(pipe);
  3117. temp = I915_READ(reg);
  3118. temp &= ~FDI_LINK_TRAIN_AUTO;
  3119. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3120. temp &= ~FDI_RX_ENABLE;
  3121. I915_WRITE(reg, temp);
  3122. /* enable CPU FDI TX and PCH FDI RX */
  3123. reg = FDI_TX_CTL(pipe);
  3124. temp = I915_READ(reg);
  3125. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3126. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3127. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3128. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3129. temp |= snb_b_fdi_train_param[j/2];
  3130. temp |= FDI_COMPOSITE_SYNC;
  3131. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3132. I915_WRITE(FDI_RX_MISC(pipe),
  3133. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3134. reg = FDI_RX_CTL(pipe);
  3135. temp = I915_READ(reg);
  3136. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3137. temp |= FDI_COMPOSITE_SYNC;
  3138. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3139. POSTING_READ(reg);
  3140. udelay(1); /* should be 0.5us */
  3141. for (i = 0; i < 4; i++) {
  3142. reg = FDI_RX_IIR(pipe);
  3143. temp = I915_READ(reg);
  3144. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3145. if (temp & FDI_RX_BIT_LOCK ||
  3146. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3147. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3148. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3149. i);
  3150. break;
  3151. }
  3152. udelay(1); /* should be 0.5us */
  3153. }
  3154. if (i == 4) {
  3155. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3156. continue;
  3157. }
  3158. /* Train 2 */
  3159. reg = FDI_TX_CTL(pipe);
  3160. temp = I915_READ(reg);
  3161. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3162. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3163. I915_WRITE(reg, temp);
  3164. reg = FDI_RX_CTL(pipe);
  3165. temp = I915_READ(reg);
  3166. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3167. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3168. I915_WRITE(reg, temp);
  3169. POSTING_READ(reg);
  3170. udelay(2); /* should be 1.5us */
  3171. for (i = 0; i < 4; i++) {
  3172. reg = FDI_RX_IIR(pipe);
  3173. temp = I915_READ(reg);
  3174. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3175. if (temp & FDI_RX_SYMBOL_LOCK ||
  3176. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3177. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3178. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3179. i);
  3180. goto train_done;
  3181. }
  3182. udelay(2); /* should be 1.5us */
  3183. }
  3184. if (i == 4)
  3185. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3186. }
  3187. train_done:
  3188. DRM_DEBUG_KMS("FDI train done.\n");
  3189. }
  3190. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3191. {
  3192. struct drm_device *dev = intel_crtc->base.dev;
  3193. struct drm_i915_private *dev_priv = dev->dev_private;
  3194. int pipe = intel_crtc->pipe;
  3195. i915_reg_t reg;
  3196. u32 temp;
  3197. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3198. reg = FDI_RX_CTL(pipe);
  3199. temp = I915_READ(reg);
  3200. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3201. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3202. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3203. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3204. POSTING_READ(reg);
  3205. udelay(200);
  3206. /* Switch from Rawclk to PCDclk */
  3207. temp = I915_READ(reg);
  3208. I915_WRITE(reg, temp | FDI_PCDCLK);
  3209. POSTING_READ(reg);
  3210. udelay(200);
  3211. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3212. reg = FDI_TX_CTL(pipe);
  3213. temp = I915_READ(reg);
  3214. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3215. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3216. POSTING_READ(reg);
  3217. udelay(100);
  3218. }
  3219. }
  3220. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3221. {
  3222. struct drm_device *dev = intel_crtc->base.dev;
  3223. struct drm_i915_private *dev_priv = dev->dev_private;
  3224. int pipe = intel_crtc->pipe;
  3225. i915_reg_t reg;
  3226. u32 temp;
  3227. /* Switch from PCDclk to Rawclk */
  3228. reg = FDI_RX_CTL(pipe);
  3229. temp = I915_READ(reg);
  3230. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3231. /* Disable CPU FDI TX PLL */
  3232. reg = FDI_TX_CTL(pipe);
  3233. temp = I915_READ(reg);
  3234. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3235. POSTING_READ(reg);
  3236. udelay(100);
  3237. reg = FDI_RX_CTL(pipe);
  3238. temp = I915_READ(reg);
  3239. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3240. /* Wait for the clocks to turn off. */
  3241. POSTING_READ(reg);
  3242. udelay(100);
  3243. }
  3244. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3245. {
  3246. struct drm_device *dev = crtc->dev;
  3247. struct drm_i915_private *dev_priv = dev->dev_private;
  3248. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3249. int pipe = intel_crtc->pipe;
  3250. i915_reg_t reg;
  3251. u32 temp;
  3252. /* disable CPU FDI tx and PCH FDI rx */
  3253. reg = FDI_TX_CTL(pipe);
  3254. temp = I915_READ(reg);
  3255. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3256. POSTING_READ(reg);
  3257. reg = FDI_RX_CTL(pipe);
  3258. temp = I915_READ(reg);
  3259. temp &= ~(0x7 << 16);
  3260. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3261. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3262. POSTING_READ(reg);
  3263. udelay(100);
  3264. /* Ironlake workaround, disable clock pointer after downing FDI */
  3265. if (HAS_PCH_IBX(dev))
  3266. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3267. /* still set train pattern 1 */
  3268. reg = FDI_TX_CTL(pipe);
  3269. temp = I915_READ(reg);
  3270. temp &= ~FDI_LINK_TRAIN_NONE;
  3271. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3272. I915_WRITE(reg, temp);
  3273. reg = FDI_RX_CTL(pipe);
  3274. temp = I915_READ(reg);
  3275. if (HAS_PCH_CPT(dev)) {
  3276. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3277. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3278. } else {
  3279. temp &= ~FDI_LINK_TRAIN_NONE;
  3280. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3281. }
  3282. /* BPC in FDI rx is consistent with that in PIPECONF */
  3283. temp &= ~(0x07 << 16);
  3284. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3285. I915_WRITE(reg, temp);
  3286. POSTING_READ(reg);
  3287. udelay(100);
  3288. }
  3289. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3290. {
  3291. struct intel_crtc *crtc;
  3292. /* Note that we don't need to be called with mode_config.lock here
  3293. * as our list of CRTC objects is static for the lifetime of the
  3294. * device and so cannot disappear as we iterate. Similarly, we can
  3295. * happily treat the predicates as racy, atomic checks as userspace
  3296. * cannot claim and pin a new fb without at least acquring the
  3297. * struct_mutex and so serialising with us.
  3298. */
  3299. for_each_intel_crtc(dev, crtc) {
  3300. if (atomic_read(&crtc->unpin_work_count) == 0)
  3301. continue;
  3302. if (crtc->unpin_work)
  3303. intel_wait_for_vblank(dev, crtc->pipe);
  3304. return true;
  3305. }
  3306. return false;
  3307. }
  3308. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3309. {
  3310. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3311. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3312. /* ensure that the unpin work is consistent wrt ->pending. */
  3313. smp_rmb();
  3314. intel_crtc->unpin_work = NULL;
  3315. if (work->event)
  3316. drm_send_vblank_event(intel_crtc->base.dev,
  3317. intel_crtc->pipe,
  3318. work->event);
  3319. drm_crtc_vblank_put(&intel_crtc->base);
  3320. wake_up_all(&dev_priv->pending_flip_queue);
  3321. queue_work(dev_priv->wq, &work->work);
  3322. trace_i915_flip_complete(intel_crtc->plane,
  3323. work->pending_flip_obj);
  3324. }
  3325. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3326. {
  3327. struct drm_device *dev = crtc->dev;
  3328. struct drm_i915_private *dev_priv = dev->dev_private;
  3329. long ret;
  3330. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3331. ret = wait_event_interruptible_timeout(
  3332. dev_priv->pending_flip_queue,
  3333. !intel_crtc_has_pending_flip(crtc),
  3334. 60*HZ);
  3335. if (ret < 0)
  3336. return ret;
  3337. if (ret == 0) {
  3338. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3339. spin_lock_irq(&dev->event_lock);
  3340. if (intel_crtc->unpin_work) {
  3341. WARN_ONCE(1, "Removing stuck page flip\n");
  3342. page_flip_completed(intel_crtc);
  3343. }
  3344. spin_unlock_irq(&dev->event_lock);
  3345. }
  3346. return 0;
  3347. }
  3348. static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3349. {
  3350. u32 temp;
  3351. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3352. mutex_lock(&dev_priv->sb_lock);
  3353. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3354. temp |= SBI_SSCCTL_DISABLE;
  3355. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3356. mutex_unlock(&dev_priv->sb_lock);
  3357. }
  3358. /* Program iCLKIP clock to the desired frequency */
  3359. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3360. {
  3361. struct drm_device *dev = crtc->dev;
  3362. struct drm_i915_private *dev_priv = dev->dev_private;
  3363. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3364. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3365. u32 temp;
  3366. lpt_disable_iclkip(dev_priv);
  3367. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3368. if (clock == 20000) {
  3369. auxdiv = 1;
  3370. divsel = 0x41;
  3371. phaseinc = 0x20;
  3372. } else {
  3373. /* The iCLK virtual clock root frequency is in MHz,
  3374. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3375. * divisors, it is necessary to divide one by another, so we
  3376. * convert the virtual clock precision to KHz here for higher
  3377. * precision.
  3378. */
  3379. u32 iclk_virtual_root_freq = 172800 * 1000;
  3380. u32 iclk_pi_range = 64;
  3381. u32 desired_divisor, msb_divisor_value, pi_value;
  3382. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq, clock);
  3383. msb_divisor_value = desired_divisor / iclk_pi_range;
  3384. pi_value = desired_divisor % iclk_pi_range;
  3385. auxdiv = 0;
  3386. divsel = msb_divisor_value - 2;
  3387. phaseinc = pi_value;
  3388. }
  3389. /* This should not happen with any sane values */
  3390. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3391. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3392. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3393. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3394. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3395. clock,
  3396. auxdiv,
  3397. divsel,
  3398. phasedir,
  3399. phaseinc);
  3400. mutex_lock(&dev_priv->sb_lock);
  3401. /* Program SSCDIVINTPHASE6 */
  3402. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3403. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3404. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3405. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3406. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3407. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3408. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3409. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3410. /* Program SSCAUXDIV */
  3411. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3412. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3413. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3414. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3415. /* Enable modulator and associated divider */
  3416. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3417. temp &= ~SBI_SSCCTL_DISABLE;
  3418. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3419. mutex_unlock(&dev_priv->sb_lock);
  3420. /* Wait for initialization time */
  3421. udelay(24);
  3422. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3423. }
  3424. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3425. enum pipe pch_transcoder)
  3426. {
  3427. struct drm_device *dev = crtc->base.dev;
  3428. struct drm_i915_private *dev_priv = dev->dev_private;
  3429. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3430. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3431. I915_READ(HTOTAL(cpu_transcoder)));
  3432. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3433. I915_READ(HBLANK(cpu_transcoder)));
  3434. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3435. I915_READ(HSYNC(cpu_transcoder)));
  3436. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3437. I915_READ(VTOTAL(cpu_transcoder)));
  3438. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3439. I915_READ(VBLANK(cpu_transcoder)));
  3440. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3441. I915_READ(VSYNC(cpu_transcoder)));
  3442. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3443. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3444. }
  3445. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3446. {
  3447. struct drm_i915_private *dev_priv = dev->dev_private;
  3448. uint32_t temp;
  3449. temp = I915_READ(SOUTH_CHICKEN1);
  3450. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3451. return;
  3452. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3453. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3454. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3455. if (enable)
  3456. temp |= FDI_BC_BIFURCATION_SELECT;
  3457. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3458. I915_WRITE(SOUTH_CHICKEN1, temp);
  3459. POSTING_READ(SOUTH_CHICKEN1);
  3460. }
  3461. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3462. {
  3463. struct drm_device *dev = intel_crtc->base.dev;
  3464. switch (intel_crtc->pipe) {
  3465. case PIPE_A:
  3466. break;
  3467. case PIPE_B:
  3468. if (intel_crtc->config->fdi_lanes > 2)
  3469. cpt_set_fdi_bc_bifurcation(dev, false);
  3470. else
  3471. cpt_set_fdi_bc_bifurcation(dev, true);
  3472. break;
  3473. case PIPE_C:
  3474. cpt_set_fdi_bc_bifurcation(dev, true);
  3475. break;
  3476. default:
  3477. BUG();
  3478. }
  3479. }
  3480. /* Return which DP Port should be selected for Transcoder DP control */
  3481. static enum port
  3482. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3483. {
  3484. struct drm_device *dev = crtc->dev;
  3485. struct intel_encoder *encoder;
  3486. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3487. if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
  3488. encoder->type == INTEL_OUTPUT_EDP)
  3489. return enc_to_dig_port(&encoder->base)->port;
  3490. }
  3491. return -1;
  3492. }
  3493. /*
  3494. * Enable PCH resources required for PCH ports:
  3495. * - PCH PLLs
  3496. * - FDI training & RX/TX
  3497. * - update transcoder timings
  3498. * - DP transcoding bits
  3499. * - transcoder
  3500. */
  3501. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3502. {
  3503. struct drm_device *dev = crtc->dev;
  3504. struct drm_i915_private *dev_priv = dev->dev_private;
  3505. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3506. int pipe = intel_crtc->pipe;
  3507. u32 temp;
  3508. assert_pch_transcoder_disabled(dev_priv, pipe);
  3509. if (IS_IVYBRIDGE(dev))
  3510. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3511. /* Write the TU size bits before fdi link training, so that error
  3512. * detection works. */
  3513. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3514. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3515. /*
  3516. * Sometimes spurious CPU pipe underruns happen during FDI
  3517. * training, at least with VGA+HDMI cloning. Suppress them.
  3518. */
  3519. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3520. /* For PCH output, training FDI link */
  3521. dev_priv->display.fdi_link_train(crtc);
  3522. /* We need to program the right clock selection before writing the pixel
  3523. * mutliplier into the DPLL. */
  3524. if (HAS_PCH_CPT(dev)) {
  3525. u32 sel;
  3526. temp = I915_READ(PCH_DPLL_SEL);
  3527. temp |= TRANS_DPLL_ENABLE(pipe);
  3528. sel = TRANS_DPLLB_SEL(pipe);
  3529. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3530. temp |= sel;
  3531. else
  3532. temp &= ~sel;
  3533. I915_WRITE(PCH_DPLL_SEL, temp);
  3534. }
  3535. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3536. * transcoder, and we actually should do this to not upset any PCH
  3537. * transcoder that already use the clock when we share it.
  3538. *
  3539. * Note that enable_shared_dpll tries to do the right thing, but
  3540. * get_shared_dpll unconditionally resets the pll - we need that to have
  3541. * the right LVDS enable sequence. */
  3542. intel_enable_shared_dpll(intel_crtc);
  3543. /* set transcoder timing, panel must allow it */
  3544. assert_panel_unlocked(dev_priv, pipe);
  3545. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3546. intel_fdi_normal_train(crtc);
  3547. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3548. /* For PCH DP, enable TRANS_DP_CTL */
  3549. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3550. const struct drm_display_mode *adjusted_mode =
  3551. &intel_crtc->config->base.adjusted_mode;
  3552. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3553. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3554. temp = I915_READ(reg);
  3555. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3556. TRANS_DP_SYNC_MASK |
  3557. TRANS_DP_BPC_MASK);
  3558. temp |= TRANS_DP_OUTPUT_ENABLE;
  3559. temp |= bpc << 9; /* same format but at 11:9 */
  3560. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3561. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3562. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3563. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3564. switch (intel_trans_dp_port_sel(crtc)) {
  3565. case PORT_B:
  3566. temp |= TRANS_DP_PORT_SEL_B;
  3567. break;
  3568. case PORT_C:
  3569. temp |= TRANS_DP_PORT_SEL_C;
  3570. break;
  3571. case PORT_D:
  3572. temp |= TRANS_DP_PORT_SEL_D;
  3573. break;
  3574. default:
  3575. BUG();
  3576. }
  3577. I915_WRITE(reg, temp);
  3578. }
  3579. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3580. }
  3581. static void lpt_pch_enable(struct drm_crtc *crtc)
  3582. {
  3583. struct drm_device *dev = crtc->dev;
  3584. struct drm_i915_private *dev_priv = dev->dev_private;
  3585. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3586. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3587. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3588. lpt_program_iclkip(crtc);
  3589. /* Set transcoder timing. */
  3590. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3591. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3592. }
  3593. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3594. struct intel_crtc_state *crtc_state)
  3595. {
  3596. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3597. struct intel_shared_dpll *pll;
  3598. struct intel_shared_dpll_config *shared_dpll;
  3599. enum intel_dpll_id i;
  3600. int max = dev_priv->num_shared_dpll;
  3601. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3602. if (HAS_PCH_IBX(dev_priv->dev)) {
  3603. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3604. i = (enum intel_dpll_id) crtc->pipe;
  3605. pll = &dev_priv->shared_dplls[i];
  3606. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3607. crtc->base.base.id, pll->name);
  3608. WARN_ON(shared_dpll[i].crtc_mask);
  3609. goto found;
  3610. }
  3611. if (IS_BROXTON(dev_priv->dev)) {
  3612. /* PLL is attached to port in bxt */
  3613. struct intel_encoder *encoder;
  3614. struct intel_digital_port *intel_dig_port;
  3615. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3616. if (WARN_ON(!encoder))
  3617. return NULL;
  3618. intel_dig_port = enc_to_dig_port(&encoder->base);
  3619. /* 1:1 mapping between ports and PLLs */
  3620. i = (enum intel_dpll_id)intel_dig_port->port;
  3621. pll = &dev_priv->shared_dplls[i];
  3622. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3623. crtc->base.base.id, pll->name);
  3624. WARN_ON(shared_dpll[i].crtc_mask);
  3625. goto found;
  3626. } else if (INTEL_INFO(dev_priv)->gen < 9 && HAS_DDI(dev_priv))
  3627. /* Do not consider SPLL */
  3628. max = 2;
  3629. for (i = 0; i < max; i++) {
  3630. pll = &dev_priv->shared_dplls[i];
  3631. /* Only want to check enabled timings first */
  3632. if (shared_dpll[i].crtc_mask == 0)
  3633. continue;
  3634. if (memcmp(&crtc_state->dpll_hw_state,
  3635. &shared_dpll[i].hw_state,
  3636. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3637. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3638. crtc->base.base.id, pll->name,
  3639. shared_dpll[i].crtc_mask,
  3640. pll->active);
  3641. goto found;
  3642. }
  3643. }
  3644. /* Ok no matching timings, maybe there's a free one? */
  3645. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3646. pll = &dev_priv->shared_dplls[i];
  3647. if (shared_dpll[i].crtc_mask == 0) {
  3648. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3649. crtc->base.base.id, pll->name);
  3650. goto found;
  3651. }
  3652. }
  3653. return NULL;
  3654. found:
  3655. if (shared_dpll[i].crtc_mask == 0)
  3656. shared_dpll[i].hw_state =
  3657. crtc_state->dpll_hw_state;
  3658. crtc_state->shared_dpll = i;
  3659. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3660. pipe_name(crtc->pipe));
  3661. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3662. return pll;
  3663. }
  3664. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3665. {
  3666. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3667. struct intel_shared_dpll_config *shared_dpll;
  3668. struct intel_shared_dpll *pll;
  3669. enum intel_dpll_id i;
  3670. if (!to_intel_atomic_state(state)->dpll_set)
  3671. return;
  3672. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3673. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3674. pll = &dev_priv->shared_dplls[i];
  3675. pll->config = shared_dpll[i];
  3676. }
  3677. }
  3678. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3679. {
  3680. struct drm_i915_private *dev_priv = dev->dev_private;
  3681. i915_reg_t dslreg = PIPEDSL(pipe);
  3682. u32 temp;
  3683. temp = I915_READ(dslreg);
  3684. udelay(500);
  3685. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3686. if (wait_for(I915_READ(dslreg) != temp, 5))
  3687. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3688. }
  3689. }
  3690. static int
  3691. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3692. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3693. int src_w, int src_h, int dst_w, int dst_h)
  3694. {
  3695. struct intel_crtc_scaler_state *scaler_state =
  3696. &crtc_state->scaler_state;
  3697. struct intel_crtc *intel_crtc =
  3698. to_intel_crtc(crtc_state->base.crtc);
  3699. int need_scaling;
  3700. need_scaling = intel_rotation_90_or_270(rotation) ?
  3701. (src_h != dst_w || src_w != dst_h):
  3702. (src_w != dst_w || src_h != dst_h);
  3703. /*
  3704. * if plane is being disabled or scaler is no more required or force detach
  3705. * - free scaler binded to this plane/crtc
  3706. * - in order to do this, update crtc->scaler_usage
  3707. *
  3708. * Here scaler state in crtc_state is set free so that
  3709. * scaler can be assigned to other user. Actual register
  3710. * update to free the scaler is done in plane/panel-fit programming.
  3711. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3712. */
  3713. if (force_detach || !need_scaling) {
  3714. if (*scaler_id >= 0) {
  3715. scaler_state->scaler_users &= ~(1 << scaler_user);
  3716. scaler_state->scalers[*scaler_id].in_use = 0;
  3717. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3718. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3719. intel_crtc->pipe, scaler_user, *scaler_id,
  3720. scaler_state->scaler_users);
  3721. *scaler_id = -1;
  3722. }
  3723. return 0;
  3724. }
  3725. /* range checks */
  3726. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3727. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3728. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3729. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3730. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3731. "size is out of scaler range\n",
  3732. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3733. return -EINVAL;
  3734. }
  3735. /* mark this plane as a scaler user in crtc_state */
  3736. scaler_state->scaler_users |= (1 << scaler_user);
  3737. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3738. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3739. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3740. scaler_state->scaler_users);
  3741. return 0;
  3742. }
  3743. /**
  3744. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3745. *
  3746. * @state: crtc's scaler state
  3747. *
  3748. * Return
  3749. * 0 - scaler_usage updated successfully
  3750. * error - requested scaling cannot be supported or other error condition
  3751. */
  3752. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3753. {
  3754. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3755. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3756. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3757. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3758. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3759. &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
  3760. state->pipe_src_w, state->pipe_src_h,
  3761. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3762. }
  3763. /**
  3764. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3765. *
  3766. * @state: crtc's scaler state
  3767. * @plane_state: atomic plane state to update
  3768. *
  3769. * Return
  3770. * 0 - scaler_usage updated successfully
  3771. * error - requested scaling cannot be supported or other error condition
  3772. */
  3773. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3774. struct intel_plane_state *plane_state)
  3775. {
  3776. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3777. struct intel_plane *intel_plane =
  3778. to_intel_plane(plane_state->base.plane);
  3779. struct drm_framebuffer *fb = plane_state->base.fb;
  3780. int ret;
  3781. bool force_detach = !fb || !plane_state->visible;
  3782. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3783. intel_plane->base.base.id, intel_crtc->pipe,
  3784. drm_plane_index(&intel_plane->base));
  3785. ret = skl_update_scaler(crtc_state, force_detach,
  3786. drm_plane_index(&intel_plane->base),
  3787. &plane_state->scaler_id,
  3788. plane_state->base.rotation,
  3789. drm_rect_width(&plane_state->src) >> 16,
  3790. drm_rect_height(&plane_state->src) >> 16,
  3791. drm_rect_width(&plane_state->dst),
  3792. drm_rect_height(&plane_state->dst));
  3793. if (ret || plane_state->scaler_id < 0)
  3794. return ret;
  3795. /* check colorkey */
  3796. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3797. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3798. intel_plane->base.base.id);
  3799. return -EINVAL;
  3800. }
  3801. /* Check src format */
  3802. switch (fb->pixel_format) {
  3803. case DRM_FORMAT_RGB565:
  3804. case DRM_FORMAT_XBGR8888:
  3805. case DRM_FORMAT_XRGB8888:
  3806. case DRM_FORMAT_ABGR8888:
  3807. case DRM_FORMAT_ARGB8888:
  3808. case DRM_FORMAT_XRGB2101010:
  3809. case DRM_FORMAT_XBGR2101010:
  3810. case DRM_FORMAT_YUYV:
  3811. case DRM_FORMAT_YVYU:
  3812. case DRM_FORMAT_UYVY:
  3813. case DRM_FORMAT_VYUY:
  3814. break;
  3815. default:
  3816. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3817. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3818. return -EINVAL;
  3819. }
  3820. return 0;
  3821. }
  3822. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3823. {
  3824. int i;
  3825. for (i = 0; i < crtc->num_scalers; i++)
  3826. skl_detach_scaler(crtc, i);
  3827. }
  3828. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3829. {
  3830. struct drm_device *dev = crtc->base.dev;
  3831. struct drm_i915_private *dev_priv = dev->dev_private;
  3832. int pipe = crtc->pipe;
  3833. struct intel_crtc_scaler_state *scaler_state =
  3834. &crtc->config->scaler_state;
  3835. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3836. if (crtc->config->pch_pfit.enabled) {
  3837. int id;
  3838. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3839. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3840. return;
  3841. }
  3842. id = scaler_state->scaler_id;
  3843. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3844. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3845. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3846. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3847. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3848. }
  3849. }
  3850. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3851. {
  3852. struct drm_device *dev = crtc->base.dev;
  3853. struct drm_i915_private *dev_priv = dev->dev_private;
  3854. int pipe = crtc->pipe;
  3855. if (crtc->config->pch_pfit.enabled) {
  3856. /* Force use of hard-coded filter coefficients
  3857. * as some pre-programmed values are broken,
  3858. * e.g. x201.
  3859. */
  3860. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3861. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3862. PF_PIPE_SEL_IVB(pipe));
  3863. else
  3864. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3865. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3866. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3867. }
  3868. }
  3869. void hsw_enable_ips(struct intel_crtc *crtc)
  3870. {
  3871. struct drm_device *dev = crtc->base.dev;
  3872. struct drm_i915_private *dev_priv = dev->dev_private;
  3873. if (!crtc->config->ips_enabled)
  3874. return;
  3875. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3876. intel_wait_for_vblank(dev, crtc->pipe);
  3877. assert_plane_enabled(dev_priv, crtc->plane);
  3878. if (IS_BROADWELL(dev)) {
  3879. mutex_lock(&dev_priv->rps.hw_lock);
  3880. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3881. mutex_unlock(&dev_priv->rps.hw_lock);
  3882. /* Quoting Art Runyan: "its not safe to expect any particular
  3883. * value in IPS_CTL bit 31 after enabling IPS through the
  3884. * mailbox." Moreover, the mailbox may return a bogus state,
  3885. * so we need to just enable it and continue on.
  3886. */
  3887. } else {
  3888. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3889. /* The bit only becomes 1 in the next vblank, so this wait here
  3890. * is essentially intel_wait_for_vblank. If we don't have this
  3891. * and don't wait for vblanks until the end of crtc_enable, then
  3892. * the HW state readout code will complain that the expected
  3893. * IPS_CTL value is not the one we read. */
  3894. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3895. DRM_ERROR("Timed out waiting for IPS enable\n");
  3896. }
  3897. }
  3898. void hsw_disable_ips(struct intel_crtc *crtc)
  3899. {
  3900. struct drm_device *dev = crtc->base.dev;
  3901. struct drm_i915_private *dev_priv = dev->dev_private;
  3902. if (!crtc->config->ips_enabled)
  3903. return;
  3904. assert_plane_enabled(dev_priv, crtc->plane);
  3905. if (IS_BROADWELL(dev)) {
  3906. mutex_lock(&dev_priv->rps.hw_lock);
  3907. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3908. mutex_unlock(&dev_priv->rps.hw_lock);
  3909. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3910. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3911. DRM_ERROR("Timed out waiting for IPS disable\n");
  3912. } else {
  3913. I915_WRITE(IPS_CTL, 0);
  3914. POSTING_READ(IPS_CTL);
  3915. }
  3916. /* We need to wait for a vblank before we can disable the plane. */
  3917. intel_wait_for_vblank(dev, crtc->pipe);
  3918. }
  3919. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3920. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3921. {
  3922. struct drm_device *dev = crtc->dev;
  3923. struct drm_i915_private *dev_priv = dev->dev_private;
  3924. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3925. enum pipe pipe = intel_crtc->pipe;
  3926. int i;
  3927. bool reenable_ips = false;
  3928. /* The clocks have to be on to load the palette. */
  3929. if (!crtc->state->active)
  3930. return;
  3931. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3932. if (intel_crtc->config->has_dsi_encoder)
  3933. assert_dsi_pll_enabled(dev_priv);
  3934. else
  3935. assert_pll_enabled(dev_priv, pipe);
  3936. }
  3937. /* Workaround : Do not read or write the pipe palette/gamma data while
  3938. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3939. */
  3940. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3941. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3942. GAMMA_MODE_MODE_SPLIT)) {
  3943. hsw_disable_ips(intel_crtc);
  3944. reenable_ips = true;
  3945. }
  3946. for (i = 0; i < 256; i++) {
  3947. i915_reg_t palreg;
  3948. if (HAS_GMCH_DISPLAY(dev))
  3949. palreg = PALETTE(pipe, i);
  3950. else
  3951. palreg = LGC_PALETTE(pipe, i);
  3952. I915_WRITE(palreg,
  3953. (intel_crtc->lut_r[i] << 16) |
  3954. (intel_crtc->lut_g[i] << 8) |
  3955. intel_crtc->lut_b[i]);
  3956. }
  3957. if (reenable_ips)
  3958. hsw_enable_ips(intel_crtc);
  3959. }
  3960. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3961. {
  3962. if (intel_crtc->overlay) {
  3963. struct drm_device *dev = intel_crtc->base.dev;
  3964. struct drm_i915_private *dev_priv = dev->dev_private;
  3965. mutex_lock(&dev->struct_mutex);
  3966. dev_priv->mm.interruptible = false;
  3967. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3968. dev_priv->mm.interruptible = true;
  3969. mutex_unlock(&dev->struct_mutex);
  3970. }
  3971. /* Let userspace switch the overlay on again. In most cases userspace
  3972. * has to recompute where to put it anyway.
  3973. */
  3974. }
  3975. /**
  3976. * intel_post_enable_primary - Perform operations after enabling primary plane
  3977. * @crtc: the CRTC whose primary plane was just enabled
  3978. *
  3979. * Performs potentially sleeping operations that must be done after the primary
  3980. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3981. * called due to an explicit primary plane update, or due to an implicit
  3982. * re-enable that is caused when a sprite plane is updated to no longer
  3983. * completely hide the primary plane.
  3984. */
  3985. static void
  3986. intel_post_enable_primary(struct drm_crtc *crtc)
  3987. {
  3988. struct drm_device *dev = crtc->dev;
  3989. struct drm_i915_private *dev_priv = dev->dev_private;
  3990. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3991. int pipe = intel_crtc->pipe;
  3992. /*
  3993. * FIXME IPS should be fine as long as one plane is
  3994. * enabled, but in practice it seems to have problems
  3995. * when going from primary only to sprite only and vice
  3996. * versa.
  3997. */
  3998. hsw_enable_ips(intel_crtc);
  3999. /*
  4000. * Gen2 reports pipe underruns whenever all planes are disabled.
  4001. * So don't enable underrun reporting before at least some planes
  4002. * are enabled.
  4003. * FIXME: Need to fix the logic to work when we turn off all planes
  4004. * but leave the pipe running.
  4005. */
  4006. if (IS_GEN2(dev))
  4007. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4008. /* Underruns don't always raise interrupts, so check manually. */
  4009. intel_check_cpu_fifo_underruns(dev_priv);
  4010. intel_check_pch_fifo_underruns(dev_priv);
  4011. }
  4012. /**
  4013. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4014. * @crtc: the CRTC whose primary plane is to be disabled
  4015. *
  4016. * Performs potentially sleeping operations that must be done before the
  4017. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4018. * be called due to an explicit primary plane update, or due to an implicit
  4019. * disable that is caused when a sprite plane completely hides the primary
  4020. * plane.
  4021. */
  4022. static void
  4023. intel_pre_disable_primary(struct drm_crtc *crtc)
  4024. {
  4025. struct drm_device *dev = crtc->dev;
  4026. struct drm_i915_private *dev_priv = dev->dev_private;
  4027. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4028. int pipe = intel_crtc->pipe;
  4029. /*
  4030. * Gen2 reports pipe underruns whenever all planes are disabled.
  4031. * So diasble underrun reporting before all the planes get disabled.
  4032. * FIXME: Need to fix the logic to work when we turn off all planes
  4033. * but leave the pipe running.
  4034. */
  4035. if (IS_GEN2(dev))
  4036. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4037. /*
  4038. * Vblank time updates from the shadow to live plane control register
  4039. * are blocked if the memory self-refresh mode is active at that
  4040. * moment. So to make sure the plane gets truly disabled, disable
  4041. * first the self-refresh mode. The self-refresh enable bit in turn
  4042. * will be checked/applied by the HW only at the next frame start
  4043. * event which is after the vblank start event, so we need to have a
  4044. * wait-for-vblank between disabling the plane and the pipe.
  4045. */
  4046. if (HAS_GMCH_DISPLAY(dev)) {
  4047. intel_set_memory_cxsr(dev_priv, false);
  4048. dev_priv->wm.vlv.cxsr = false;
  4049. intel_wait_for_vblank(dev, pipe);
  4050. }
  4051. /*
  4052. * FIXME IPS should be fine as long as one plane is
  4053. * enabled, but in practice it seems to have problems
  4054. * when going from primary only to sprite only and vice
  4055. * versa.
  4056. */
  4057. hsw_disable_ips(intel_crtc);
  4058. }
  4059. static void intel_post_plane_update(struct intel_crtc *crtc)
  4060. {
  4061. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4062. struct intel_crtc_state *pipe_config =
  4063. to_intel_crtc_state(crtc->base.state);
  4064. struct drm_device *dev = crtc->base.dev;
  4065. if (atomic->wait_vblank)
  4066. intel_wait_for_vblank(dev, crtc->pipe);
  4067. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4068. crtc->wm.cxsr_allowed = true;
  4069. if (pipe_config->wm_changed && pipe_config->base.active)
  4070. intel_update_watermarks(&crtc->base);
  4071. if (atomic->update_fbc)
  4072. intel_fbc_post_update(crtc);
  4073. if (atomic->post_enable_primary)
  4074. intel_post_enable_primary(&crtc->base);
  4075. memset(atomic, 0, sizeof(*atomic));
  4076. }
  4077. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  4078. {
  4079. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4080. struct drm_device *dev = crtc->base.dev;
  4081. struct drm_i915_private *dev_priv = dev->dev_private;
  4082. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4083. struct intel_crtc_state *pipe_config =
  4084. to_intel_crtc_state(crtc->base.state);
  4085. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4086. struct drm_plane *primary = crtc->base.primary;
  4087. struct drm_plane_state *old_pri_state =
  4088. drm_atomic_get_existing_plane_state(old_state, primary);
  4089. bool modeset = needs_modeset(&pipe_config->base);
  4090. if (atomic->update_fbc)
  4091. intel_fbc_pre_update(crtc);
  4092. if (old_pri_state) {
  4093. struct intel_plane_state *primary_state =
  4094. to_intel_plane_state(primary->state);
  4095. struct intel_plane_state *old_primary_state =
  4096. to_intel_plane_state(old_pri_state);
  4097. if (old_primary_state->visible &&
  4098. (modeset || !primary_state->visible))
  4099. intel_pre_disable_primary(&crtc->base);
  4100. }
  4101. if (pipe_config->disable_cxsr) {
  4102. crtc->wm.cxsr_allowed = false;
  4103. if (old_crtc_state->base.active)
  4104. intel_set_memory_cxsr(dev_priv, false);
  4105. }
  4106. if (!needs_modeset(&pipe_config->base) && pipe_config->wm_changed)
  4107. intel_update_watermarks(&crtc->base);
  4108. }
  4109. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4110. {
  4111. struct drm_device *dev = crtc->dev;
  4112. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4113. struct drm_plane *p;
  4114. int pipe = intel_crtc->pipe;
  4115. intel_crtc_dpms_overlay_disable(intel_crtc);
  4116. drm_for_each_plane_mask(p, dev, plane_mask)
  4117. to_intel_plane(p)->disable_plane(p, crtc);
  4118. /*
  4119. * FIXME: Once we grow proper nuclear flip support out of this we need
  4120. * to compute the mask of flip planes precisely. For the time being
  4121. * consider this a flip to a NULL plane.
  4122. */
  4123. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4124. }
  4125. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4126. {
  4127. struct drm_device *dev = crtc->dev;
  4128. struct drm_i915_private *dev_priv = dev->dev_private;
  4129. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4130. struct intel_encoder *encoder;
  4131. int pipe = intel_crtc->pipe;
  4132. if (WARN_ON(intel_crtc->active))
  4133. return;
  4134. if (intel_crtc->config->has_pch_encoder)
  4135. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4136. if (intel_crtc->config->has_pch_encoder)
  4137. intel_prepare_shared_dpll(intel_crtc);
  4138. if (intel_crtc->config->has_dp_encoder)
  4139. intel_dp_set_m_n(intel_crtc, M1_N1);
  4140. intel_set_pipe_timings(intel_crtc);
  4141. if (intel_crtc->config->has_pch_encoder) {
  4142. intel_cpu_transcoder_set_m_n(intel_crtc,
  4143. &intel_crtc->config->fdi_m_n, NULL);
  4144. }
  4145. ironlake_set_pipeconf(crtc);
  4146. intel_crtc->active = true;
  4147. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4148. for_each_encoder_on_crtc(dev, crtc, encoder)
  4149. if (encoder->pre_enable)
  4150. encoder->pre_enable(encoder);
  4151. if (intel_crtc->config->has_pch_encoder) {
  4152. /* Note: FDI PLL enabling _must_ be done before we enable the
  4153. * cpu pipes, hence this is separate from all the other fdi/pch
  4154. * enabling. */
  4155. ironlake_fdi_pll_enable(intel_crtc);
  4156. } else {
  4157. assert_fdi_tx_disabled(dev_priv, pipe);
  4158. assert_fdi_rx_disabled(dev_priv, pipe);
  4159. }
  4160. ironlake_pfit_enable(intel_crtc);
  4161. /*
  4162. * On ILK+ LUT must be loaded before the pipe is running but with
  4163. * clocks enabled
  4164. */
  4165. intel_crtc_load_lut(crtc);
  4166. intel_update_watermarks(crtc);
  4167. intel_enable_pipe(intel_crtc);
  4168. if (intel_crtc->config->has_pch_encoder)
  4169. ironlake_pch_enable(crtc);
  4170. assert_vblank_disabled(crtc);
  4171. drm_crtc_vblank_on(crtc);
  4172. for_each_encoder_on_crtc(dev, crtc, encoder)
  4173. encoder->enable(encoder);
  4174. if (HAS_PCH_CPT(dev))
  4175. cpt_verify_modeset(dev, intel_crtc->pipe);
  4176. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4177. if (intel_crtc->config->has_pch_encoder)
  4178. intel_wait_for_vblank(dev, pipe);
  4179. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4180. }
  4181. /* IPS only exists on ULT machines and is tied to pipe A. */
  4182. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4183. {
  4184. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4185. }
  4186. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4187. {
  4188. struct drm_device *dev = crtc->dev;
  4189. struct drm_i915_private *dev_priv = dev->dev_private;
  4190. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4191. struct intel_encoder *encoder;
  4192. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4193. struct intel_crtc_state *pipe_config =
  4194. to_intel_crtc_state(crtc->state);
  4195. if (WARN_ON(intel_crtc->active))
  4196. return;
  4197. if (intel_crtc->config->has_pch_encoder)
  4198. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4199. false);
  4200. if (intel_crtc_to_shared_dpll(intel_crtc))
  4201. intel_enable_shared_dpll(intel_crtc);
  4202. if (intel_crtc->config->has_dp_encoder)
  4203. intel_dp_set_m_n(intel_crtc, M1_N1);
  4204. intel_set_pipe_timings(intel_crtc);
  4205. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4206. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4207. intel_crtc->config->pixel_multiplier - 1);
  4208. }
  4209. if (intel_crtc->config->has_pch_encoder) {
  4210. intel_cpu_transcoder_set_m_n(intel_crtc,
  4211. &intel_crtc->config->fdi_m_n, NULL);
  4212. }
  4213. haswell_set_pipeconf(crtc);
  4214. intel_set_pipe_csc(crtc);
  4215. intel_crtc->active = true;
  4216. if (intel_crtc->config->has_pch_encoder)
  4217. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4218. else
  4219. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4220. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4221. if (encoder->pre_enable)
  4222. encoder->pre_enable(encoder);
  4223. }
  4224. if (intel_crtc->config->has_pch_encoder)
  4225. dev_priv->display.fdi_link_train(crtc);
  4226. if (!intel_crtc->config->has_dsi_encoder)
  4227. intel_ddi_enable_pipe_clock(intel_crtc);
  4228. if (INTEL_INFO(dev)->gen >= 9)
  4229. skylake_pfit_enable(intel_crtc);
  4230. else
  4231. ironlake_pfit_enable(intel_crtc);
  4232. /*
  4233. * On ILK+ LUT must be loaded before the pipe is running but with
  4234. * clocks enabled
  4235. */
  4236. intel_crtc_load_lut(crtc);
  4237. intel_ddi_set_pipe_settings(crtc);
  4238. if (!intel_crtc->config->has_dsi_encoder)
  4239. intel_ddi_enable_transcoder_func(crtc);
  4240. intel_update_watermarks(crtc);
  4241. intel_enable_pipe(intel_crtc);
  4242. if (intel_crtc->config->has_pch_encoder)
  4243. lpt_pch_enable(crtc);
  4244. if (intel_crtc->config->dp_encoder_is_mst)
  4245. intel_ddi_set_vc_payload_alloc(crtc, true);
  4246. assert_vblank_disabled(crtc);
  4247. drm_crtc_vblank_on(crtc);
  4248. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4249. encoder->enable(encoder);
  4250. intel_opregion_notify_encoder(encoder, true);
  4251. }
  4252. if (intel_crtc->config->has_pch_encoder) {
  4253. intel_wait_for_vblank(dev, pipe);
  4254. intel_wait_for_vblank(dev, pipe);
  4255. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4256. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4257. true);
  4258. }
  4259. /* If we change the relative order between pipe/planes enabling, we need
  4260. * to change the workaround. */
  4261. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4262. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4263. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4264. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4265. }
  4266. }
  4267. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4268. {
  4269. struct drm_device *dev = crtc->base.dev;
  4270. struct drm_i915_private *dev_priv = dev->dev_private;
  4271. int pipe = crtc->pipe;
  4272. /* To avoid upsetting the power well on haswell only disable the pfit if
  4273. * it's in use. The hw state code will make sure we get this right. */
  4274. if (force || crtc->config->pch_pfit.enabled) {
  4275. I915_WRITE(PF_CTL(pipe), 0);
  4276. I915_WRITE(PF_WIN_POS(pipe), 0);
  4277. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4278. }
  4279. }
  4280. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4281. {
  4282. struct drm_device *dev = crtc->dev;
  4283. struct drm_i915_private *dev_priv = dev->dev_private;
  4284. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4285. struct intel_encoder *encoder;
  4286. int pipe = intel_crtc->pipe;
  4287. if (intel_crtc->config->has_pch_encoder)
  4288. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4289. for_each_encoder_on_crtc(dev, crtc, encoder)
  4290. encoder->disable(encoder);
  4291. drm_crtc_vblank_off(crtc);
  4292. assert_vblank_disabled(crtc);
  4293. /*
  4294. * Sometimes spurious CPU pipe underruns happen when the
  4295. * pipe is already disabled, but FDI RX/TX is still enabled.
  4296. * Happens at least with VGA+HDMI cloning. Suppress them.
  4297. */
  4298. if (intel_crtc->config->has_pch_encoder)
  4299. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4300. intel_disable_pipe(intel_crtc);
  4301. ironlake_pfit_disable(intel_crtc, false);
  4302. if (intel_crtc->config->has_pch_encoder) {
  4303. ironlake_fdi_disable(crtc);
  4304. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4305. }
  4306. for_each_encoder_on_crtc(dev, crtc, encoder)
  4307. if (encoder->post_disable)
  4308. encoder->post_disable(encoder);
  4309. if (intel_crtc->config->has_pch_encoder) {
  4310. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4311. if (HAS_PCH_CPT(dev)) {
  4312. i915_reg_t reg;
  4313. u32 temp;
  4314. /* disable TRANS_DP_CTL */
  4315. reg = TRANS_DP_CTL(pipe);
  4316. temp = I915_READ(reg);
  4317. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4318. TRANS_DP_PORT_SEL_MASK);
  4319. temp |= TRANS_DP_PORT_SEL_NONE;
  4320. I915_WRITE(reg, temp);
  4321. /* disable DPLL_SEL */
  4322. temp = I915_READ(PCH_DPLL_SEL);
  4323. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4324. I915_WRITE(PCH_DPLL_SEL, temp);
  4325. }
  4326. ironlake_fdi_pll_disable(intel_crtc);
  4327. }
  4328. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4329. }
  4330. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4331. {
  4332. struct drm_device *dev = crtc->dev;
  4333. struct drm_i915_private *dev_priv = dev->dev_private;
  4334. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4335. struct intel_encoder *encoder;
  4336. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4337. if (intel_crtc->config->has_pch_encoder)
  4338. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4339. false);
  4340. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4341. intel_opregion_notify_encoder(encoder, false);
  4342. encoder->disable(encoder);
  4343. }
  4344. drm_crtc_vblank_off(crtc);
  4345. assert_vblank_disabled(crtc);
  4346. intel_disable_pipe(intel_crtc);
  4347. if (intel_crtc->config->dp_encoder_is_mst)
  4348. intel_ddi_set_vc_payload_alloc(crtc, false);
  4349. if (!intel_crtc->config->has_dsi_encoder)
  4350. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4351. if (INTEL_INFO(dev)->gen >= 9)
  4352. skylake_scaler_disable(intel_crtc);
  4353. else
  4354. ironlake_pfit_disable(intel_crtc, false);
  4355. if (!intel_crtc->config->has_dsi_encoder)
  4356. intel_ddi_disable_pipe_clock(intel_crtc);
  4357. for_each_encoder_on_crtc(dev, crtc, encoder)
  4358. if (encoder->post_disable)
  4359. encoder->post_disable(encoder);
  4360. if (intel_crtc->config->has_pch_encoder) {
  4361. lpt_disable_pch_transcoder(dev_priv);
  4362. lpt_disable_iclkip(dev_priv);
  4363. intel_ddi_fdi_disable(crtc);
  4364. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4365. true);
  4366. }
  4367. }
  4368. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4369. {
  4370. struct drm_device *dev = crtc->base.dev;
  4371. struct drm_i915_private *dev_priv = dev->dev_private;
  4372. struct intel_crtc_state *pipe_config = crtc->config;
  4373. if (!pipe_config->gmch_pfit.control)
  4374. return;
  4375. /*
  4376. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4377. * according to register description and PRM.
  4378. */
  4379. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4380. assert_pipe_disabled(dev_priv, crtc->pipe);
  4381. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4382. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4383. /* Border color in case we don't scale up to the full screen. Black by
  4384. * default, change to something else for debugging. */
  4385. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4386. }
  4387. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4388. {
  4389. switch (port) {
  4390. case PORT_A:
  4391. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4392. case PORT_B:
  4393. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4394. case PORT_C:
  4395. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4396. case PORT_D:
  4397. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4398. case PORT_E:
  4399. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4400. default:
  4401. MISSING_CASE(port);
  4402. return POWER_DOMAIN_PORT_OTHER;
  4403. }
  4404. }
  4405. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4406. {
  4407. switch (port) {
  4408. case PORT_A:
  4409. return POWER_DOMAIN_AUX_A;
  4410. case PORT_B:
  4411. return POWER_DOMAIN_AUX_B;
  4412. case PORT_C:
  4413. return POWER_DOMAIN_AUX_C;
  4414. case PORT_D:
  4415. return POWER_DOMAIN_AUX_D;
  4416. case PORT_E:
  4417. /* FIXME: Check VBT for actual wiring of PORT E */
  4418. return POWER_DOMAIN_AUX_D;
  4419. default:
  4420. MISSING_CASE(port);
  4421. return POWER_DOMAIN_AUX_A;
  4422. }
  4423. }
  4424. enum intel_display_power_domain
  4425. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4426. {
  4427. struct drm_device *dev = intel_encoder->base.dev;
  4428. struct intel_digital_port *intel_dig_port;
  4429. switch (intel_encoder->type) {
  4430. case INTEL_OUTPUT_UNKNOWN:
  4431. /* Only DDI platforms should ever use this output type */
  4432. WARN_ON_ONCE(!HAS_DDI(dev));
  4433. case INTEL_OUTPUT_DISPLAYPORT:
  4434. case INTEL_OUTPUT_HDMI:
  4435. case INTEL_OUTPUT_EDP:
  4436. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4437. return port_to_power_domain(intel_dig_port->port);
  4438. case INTEL_OUTPUT_DP_MST:
  4439. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4440. return port_to_power_domain(intel_dig_port->port);
  4441. case INTEL_OUTPUT_ANALOG:
  4442. return POWER_DOMAIN_PORT_CRT;
  4443. case INTEL_OUTPUT_DSI:
  4444. return POWER_DOMAIN_PORT_DSI;
  4445. default:
  4446. return POWER_DOMAIN_PORT_OTHER;
  4447. }
  4448. }
  4449. enum intel_display_power_domain
  4450. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4451. {
  4452. struct drm_device *dev = intel_encoder->base.dev;
  4453. struct intel_digital_port *intel_dig_port;
  4454. switch (intel_encoder->type) {
  4455. case INTEL_OUTPUT_UNKNOWN:
  4456. case INTEL_OUTPUT_HDMI:
  4457. /*
  4458. * Only DDI platforms should ever use these output types.
  4459. * We can get here after the HDMI detect code has already set
  4460. * the type of the shared encoder. Since we can't be sure
  4461. * what's the status of the given connectors, play safe and
  4462. * run the DP detection too.
  4463. */
  4464. WARN_ON_ONCE(!HAS_DDI(dev));
  4465. case INTEL_OUTPUT_DISPLAYPORT:
  4466. case INTEL_OUTPUT_EDP:
  4467. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4468. return port_to_aux_power_domain(intel_dig_port->port);
  4469. case INTEL_OUTPUT_DP_MST:
  4470. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4471. return port_to_aux_power_domain(intel_dig_port->port);
  4472. default:
  4473. MISSING_CASE(intel_encoder->type);
  4474. return POWER_DOMAIN_AUX_A;
  4475. }
  4476. }
  4477. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4478. {
  4479. struct drm_device *dev = crtc->dev;
  4480. struct intel_encoder *intel_encoder;
  4481. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4482. enum pipe pipe = intel_crtc->pipe;
  4483. unsigned long mask;
  4484. enum transcoder transcoder = intel_crtc->config->cpu_transcoder;
  4485. if (!crtc->state->active)
  4486. return 0;
  4487. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4488. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4489. if (intel_crtc->config->pch_pfit.enabled ||
  4490. intel_crtc->config->pch_pfit.force_thru)
  4491. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4492. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4493. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4494. return mask;
  4495. }
  4496. static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
  4497. {
  4498. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4499. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4500. enum intel_display_power_domain domain;
  4501. unsigned long domains, new_domains, old_domains;
  4502. old_domains = intel_crtc->enabled_power_domains;
  4503. intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
  4504. domains = new_domains & ~old_domains;
  4505. for_each_power_domain(domain, domains)
  4506. intel_display_power_get(dev_priv, domain);
  4507. return old_domains & ~new_domains;
  4508. }
  4509. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4510. unsigned long domains)
  4511. {
  4512. enum intel_display_power_domain domain;
  4513. for_each_power_domain(domain, domains)
  4514. intel_display_power_put(dev_priv, domain);
  4515. }
  4516. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4517. {
  4518. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  4519. struct drm_device *dev = state->dev;
  4520. struct drm_i915_private *dev_priv = dev->dev_private;
  4521. unsigned long put_domains[I915_MAX_PIPES] = {};
  4522. struct drm_crtc_state *crtc_state;
  4523. struct drm_crtc *crtc;
  4524. int i;
  4525. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  4526. if (needs_modeset(crtc->state))
  4527. put_domains[to_intel_crtc(crtc)->pipe] =
  4528. modeset_get_crtc_power_domains(crtc);
  4529. }
  4530. if (dev_priv->display.modeset_commit_cdclk &&
  4531. intel_state->dev_cdclk != dev_priv->cdclk_freq)
  4532. dev_priv->display.modeset_commit_cdclk(state);
  4533. for (i = 0; i < I915_MAX_PIPES; i++)
  4534. if (put_domains[i])
  4535. modeset_put_power_domains(dev_priv, put_domains[i]);
  4536. }
  4537. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4538. {
  4539. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4540. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4541. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4542. return max_cdclk_freq;
  4543. else if (IS_CHERRYVIEW(dev_priv))
  4544. return max_cdclk_freq*95/100;
  4545. else if (INTEL_INFO(dev_priv)->gen < 4)
  4546. return 2*max_cdclk_freq*90/100;
  4547. else
  4548. return max_cdclk_freq*90/100;
  4549. }
  4550. static void intel_update_max_cdclk(struct drm_device *dev)
  4551. {
  4552. struct drm_i915_private *dev_priv = dev->dev_private;
  4553. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4554. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4555. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4556. dev_priv->max_cdclk_freq = 675000;
  4557. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4558. dev_priv->max_cdclk_freq = 540000;
  4559. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4560. dev_priv->max_cdclk_freq = 450000;
  4561. else
  4562. dev_priv->max_cdclk_freq = 337500;
  4563. } else if (IS_BROADWELL(dev)) {
  4564. /*
  4565. * FIXME with extra cooling we can allow
  4566. * 540 MHz for ULX and 675 Mhz for ULT.
  4567. * How can we know if extra cooling is
  4568. * available? PCI ID, VTB, something else?
  4569. */
  4570. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4571. dev_priv->max_cdclk_freq = 450000;
  4572. else if (IS_BDW_ULX(dev))
  4573. dev_priv->max_cdclk_freq = 450000;
  4574. else if (IS_BDW_ULT(dev))
  4575. dev_priv->max_cdclk_freq = 540000;
  4576. else
  4577. dev_priv->max_cdclk_freq = 675000;
  4578. } else if (IS_CHERRYVIEW(dev)) {
  4579. dev_priv->max_cdclk_freq = 320000;
  4580. } else if (IS_VALLEYVIEW(dev)) {
  4581. dev_priv->max_cdclk_freq = 400000;
  4582. } else {
  4583. /* otherwise assume cdclk is fixed */
  4584. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4585. }
  4586. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4587. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4588. dev_priv->max_cdclk_freq);
  4589. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4590. dev_priv->max_dotclk_freq);
  4591. }
  4592. static void intel_update_cdclk(struct drm_device *dev)
  4593. {
  4594. struct drm_i915_private *dev_priv = dev->dev_private;
  4595. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4596. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4597. dev_priv->cdclk_freq);
  4598. /*
  4599. * Program the gmbus_freq based on the cdclk frequency.
  4600. * BSpec erroneously claims we should aim for 4MHz, but
  4601. * in fact 1MHz is the correct frequency.
  4602. */
  4603. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  4604. /*
  4605. * Program the gmbus_freq based on the cdclk frequency.
  4606. * BSpec erroneously claims we should aim for 4MHz, but
  4607. * in fact 1MHz is the correct frequency.
  4608. */
  4609. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4610. }
  4611. if (dev_priv->max_cdclk_freq == 0)
  4612. intel_update_max_cdclk(dev);
  4613. }
  4614. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4615. {
  4616. struct drm_i915_private *dev_priv = dev->dev_private;
  4617. uint32_t divider;
  4618. uint32_t ratio;
  4619. uint32_t current_freq;
  4620. int ret;
  4621. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4622. switch (frequency) {
  4623. case 144000:
  4624. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4625. ratio = BXT_DE_PLL_RATIO(60);
  4626. break;
  4627. case 288000:
  4628. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4629. ratio = BXT_DE_PLL_RATIO(60);
  4630. break;
  4631. case 384000:
  4632. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4633. ratio = BXT_DE_PLL_RATIO(60);
  4634. break;
  4635. case 576000:
  4636. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4637. ratio = BXT_DE_PLL_RATIO(60);
  4638. break;
  4639. case 624000:
  4640. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4641. ratio = BXT_DE_PLL_RATIO(65);
  4642. break;
  4643. case 19200:
  4644. /*
  4645. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4646. * to suppress GCC warning.
  4647. */
  4648. ratio = 0;
  4649. divider = 0;
  4650. break;
  4651. default:
  4652. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4653. return;
  4654. }
  4655. mutex_lock(&dev_priv->rps.hw_lock);
  4656. /* Inform power controller of upcoming frequency change */
  4657. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4658. 0x80000000);
  4659. mutex_unlock(&dev_priv->rps.hw_lock);
  4660. if (ret) {
  4661. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4662. ret, frequency);
  4663. return;
  4664. }
  4665. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4666. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4667. current_freq = current_freq * 500 + 1000;
  4668. /*
  4669. * DE PLL has to be disabled when
  4670. * - setting to 19.2MHz (bypass, PLL isn't used)
  4671. * - before setting to 624MHz (PLL needs toggling)
  4672. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4673. */
  4674. if (frequency == 19200 || frequency == 624000 ||
  4675. current_freq == 624000) {
  4676. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4677. /* Timeout 200us */
  4678. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4679. 1))
  4680. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4681. }
  4682. if (frequency != 19200) {
  4683. uint32_t val;
  4684. val = I915_READ(BXT_DE_PLL_CTL);
  4685. val &= ~BXT_DE_PLL_RATIO_MASK;
  4686. val |= ratio;
  4687. I915_WRITE(BXT_DE_PLL_CTL, val);
  4688. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4689. /* Timeout 200us */
  4690. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4691. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4692. val = I915_READ(CDCLK_CTL);
  4693. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4694. val |= divider;
  4695. /*
  4696. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4697. * enable otherwise.
  4698. */
  4699. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4700. if (frequency >= 500000)
  4701. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4702. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4703. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4704. val |= (frequency - 1000) / 500;
  4705. I915_WRITE(CDCLK_CTL, val);
  4706. }
  4707. mutex_lock(&dev_priv->rps.hw_lock);
  4708. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4709. DIV_ROUND_UP(frequency, 25000));
  4710. mutex_unlock(&dev_priv->rps.hw_lock);
  4711. if (ret) {
  4712. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4713. ret, frequency);
  4714. return;
  4715. }
  4716. intel_update_cdclk(dev);
  4717. }
  4718. void broxton_init_cdclk(struct drm_device *dev)
  4719. {
  4720. struct drm_i915_private *dev_priv = dev->dev_private;
  4721. uint32_t val;
  4722. /*
  4723. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4724. * or else the reset will hang because there is no PCH to respond.
  4725. * Move the handshake programming to initialization sequence.
  4726. * Previously was left up to BIOS.
  4727. */
  4728. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4729. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4730. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4731. /* Enable PG1 for cdclk */
  4732. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4733. /* check if cd clock is enabled */
  4734. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4735. DRM_DEBUG_KMS("Display already initialized\n");
  4736. return;
  4737. }
  4738. /*
  4739. * FIXME:
  4740. * - The initial CDCLK needs to be read from VBT.
  4741. * Need to make this change after VBT has changes for BXT.
  4742. * - check if setting the max (or any) cdclk freq is really necessary
  4743. * here, it belongs to modeset time
  4744. */
  4745. broxton_set_cdclk(dev, 624000);
  4746. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4747. POSTING_READ(DBUF_CTL);
  4748. udelay(10);
  4749. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4750. DRM_ERROR("DBuf power enable timeout!\n");
  4751. }
  4752. void broxton_uninit_cdclk(struct drm_device *dev)
  4753. {
  4754. struct drm_i915_private *dev_priv = dev->dev_private;
  4755. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4756. POSTING_READ(DBUF_CTL);
  4757. udelay(10);
  4758. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4759. DRM_ERROR("DBuf power disable timeout!\n");
  4760. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4761. broxton_set_cdclk(dev, 19200);
  4762. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4763. }
  4764. static const struct skl_cdclk_entry {
  4765. unsigned int freq;
  4766. unsigned int vco;
  4767. } skl_cdclk_frequencies[] = {
  4768. { .freq = 308570, .vco = 8640 },
  4769. { .freq = 337500, .vco = 8100 },
  4770. { .freq = 432000, .vco = 8640 },
  4771. { .freq = 450000, .vco = 8100 },
  4772. { .freq = 540000, .vco = 8100 },
  4773. { .freq = 617140, .vco = 8640 },
  4774. { .freq = 675000, .vco = 8100 },
  4775. };
  4776. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4777. {
  4778. return (freq - 1000) / 500;
  4779. }
  4780. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4781. {
  4782. unsigned int i;
  4783. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4784. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4785. if (e->freq == freq)
  4786. return e->vco;
  4787. }
  4788. return 8100;
  4789. }
  4790. static void
  4791. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4792. {
  4793. unsigned int min_freq;
  4794. u32 val;
  4795. /* select the minimum CDCLK before enabling DPLL 0 */
  4796. val = I915_READ(CDCLK_CTL);
  4797. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4798. val |= CDCLK_FREQ_337_308;
  4799. if (required_vco == 8640)
  4800. min_freq = 308570;
  4801. else
  4802. min_freq = 337500;
  4803. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4804. I915_WRITE(CDCLK_CTL, val);
  4805. POSTING_READ(CDCLK_CTL);
  4806. /*
  4807. * We always enable DPLL0 with the lowest link rate possible, but still
  4808. * taking into account the VCO required to operate the eDP panel at the
  4809. * desired frequency. The usual DP link rates operate with a VCO of
  4810. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4811. * The modeset code is responsible for the selection of the exact link
  4812. * rate later on, with the constraint of choosing a frequency that
  4813. * works with required_vco.
  4814. */
  4815. val = I915_READ(DPLL_CTRL1);
  4816. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4817. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4818. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4819. if (required_vco == 8640)
  4820. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4821. SKL_DPLL0);
  4822. else
  4823. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4824. SKL_DPLL0);
  4825. I915_WRITE(DPLL_CTRL1, val);
  4826. POSTING_READ(DPLL_CTRL1);
  4827. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4828. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4829. DRM_ERROR("DPLL0 not locked\n");
  4830. }
  4831. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4832. {
  4833. int ret;
  4834. u32 val;
  4835. /* inform PCU we want to change CDCLK */
  4836. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4837. mutex_lock(&dev_priv->rps.hw_lock);
  4838. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4839. mutex_unlock(&dev_priv->rps.hw_lock);
  4840. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4841. }
  4842. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4843. {
  4844. unsigned int i;
  4845. for (i = 0; i < 15; i++) {
  4846. if (skl_cdclk_pcu_ready(dev_priv))
  4847. return true;
  4848. udelay(10);
  4849. }
  4850. return false;
  4851. }
  4852. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4853. {
  4854. struct drm_device *dev = dev_priv->dev;
  4855. u32 freq_select, pcu_ack;
  4856. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4857. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4858. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4859. return;
  4860. }
  4861. /* set CDCLK_CTL */
  4862. switch(freq) {
  4863. case 450000:
  4864. case 432000:
  4865. freq_select = CDCLK_FREQ_450_432;
  4866. pcu_ack = 1;
  4867. break;
  4868. case 540000:
  4869. freq_select = CDCLK_FREQ_540;
  4870. pcu_ack = 2;
  4871. break;
  4872. case 308570:
  4873. case 337500:
  4874. default:
  4875. freq_select = CDCLK_FREQ_337_308;
  4876. pcu_ack = 0;
  4877. break;
  4878. case 617140:
  4879. case 675000:
  4880. freq_select = CDCLK_FREQ_675_617;
  4881. pcu_ack = 3;
  4882. break;
  4883. }
  4884. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4885. POSTING_READ(CDCLK_CTL);
  4886. /* inform PCU of the change */
  4887. mutex_lock(&dev_priv->rps.hw_lock);
  4888. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4889. mutex_unlock(&dev_priv->rps.hw_lock);
  4890. intel_update_cdclk(dev);
  4891. }
  4892. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4893. {
  4894. /* disable DBUF power */
  4895. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4896. POSTING_READ(DBUF_CTL);
  4897. udelay(10);
  4898. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4899. DRM_ERROR("DBuf power disable timeout\n");
  4900. /* disable DPLL0 */
  4901. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4902. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4903. DRM_ERROR("Couldn't disable DPLL0\n");
  4904. }
  4905. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4906. {
  4907. unsigned int required_vco;
  4908. /* DPLL0 not enabled (happens on early BIOS versions) */
  4909. if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
  4910. /* enable DPLL0 */
  4911. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4912. skl_dpll0_enable(dev_priv, required_vco);
  4913. }
  4914. /* set CDCLK to the frequency the BIOS chose */
  4915. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4916. /* enable DBUF power */
  4917. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4918. POSTING_READ(DBUF_CTL);
  4919. udelay(10);
  4920. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4921. DRM_ERROR("DBuf power enable timeout\n");
  4922. }
  4923. int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4924. {
  4925. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  4926. uint32_t cdctl = I915_READ(CDCLK_CTL);
  4927. int freq = dev_priv->skl_boot_cdclk;
  4928. /*
  4929. * check if the pre-os intialized the display
  4930. * There is SWF18 scratchpad register defined which is set by the
  4931. * pre-os which can be used by the OS drivers to check the status
  4932. */
  4933. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4934. goto sanitize;
  4935. /* Is PLL enabled and locked ? */
  4936. if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
  4937. goto sanitize;
  4938. /* DPLL okay; verify the cdclock
  4939. *
  4940. * Noticed in some instances that the freq selection is correct but
  4941. * decimal part is programmed wrong from BIOS where pre-os does not
  4942. * enable display. Verify the same as well.
  4943. */
  4944. if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
  4945. /* All well; nothing to sanitize */
  4946. return false;
  4947. sanitize:
  4948. /*
  4949. * As of now initialize with max cdclk till
  4950. * we get dynamic cdclk support
  4951. * */
  4952. dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
  4953. skl_init_cdclk(dev_priv);
  4954. /* we did have to sanitize */
  4955. return true;
  4956. }
  4957. /* Adjust CDclk dividers to allow high res or save power if possible */
  4958. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4959. {
  4960. struct drm_i915_private *dev_priv = dev->dev_private;
  4961. u32 val, cmd;
  4962. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4963. != dev_priv->cdclk_freq);
  4964. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4965. cmd = 2;
  4966. else if (cdclk == 266667)
  4967. cmd = 1;
  4968. else
  4969. cmd = 0;
  4970. mutex_lock(&dev_priv->rps.hw_lock);
  4971. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4972. val &= ~DSPFREQGUAR_MASK;
  4973. val |= (cmd << DSPFREQGUAR_SHIFT);
  4974. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4975. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4976. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4977. 50)) {
  4978. DRM_ERROR("timed out waiting for CDclk change\n");
  4979. }
  4980. mutex_unlock(&dev_priv->rps.hw_lock);
  4981. mutex_lock(&dev_priv->sb_lock);
  4982. if (cdclk == 400000) {
  4983. u32 divider;
  4984. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4985. /* adjust cdclk divider */
  4986. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4987. val &= ~CCK_FREQUENCY_VALUES;
  4988. val |= divider;
  4989. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4990. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4991. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4992. 50))
  4993. DRM_ERROR("timed out waiting for CDclk change\n");
  4994. }
  4995. /* adjust self-refresh exit latency value */
  4996. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4997. val &= ~0x7f;
  4998. /*
  4999. * For high bandwidth configs, we set a higher latency in the bunit
  5000. * so that the core display fetch happens in time to avoid underruns.
  5001. */
  5002. if (cdclk == 400000)
  5003. val |= 4500 / 250; /* 4.5 usec */
  5004. else
  5005. val |= 3000 / 250; /* 3.0 usec */
  5006. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  5007. mutex_unlock(&dev_priv->sb_lock);
  5008. intel_update_cdclk(dev);
  5009. }
  5010. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  5011. {
  5012. struct drm_i915_private *dev_priv = dev->dev_private;
  5013. u32 val, cmd;
  5014. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  5015. != dev_priv->cdclk_freq);
  5016. switch (cdclk) {
  5017. case 333333:
  5018. case 320000:
  5019. case 266667:
  5020. case 200000:
  5021. break;
  5022. default:
  5023. MISSING_CASE(cdclk);
  5024. return;
  5025. }
  5026. /*
  5027. * Specs are full of misinformation, but testing on actual
  5028. * hardware has shown that we just need to write the desired
  5029. * CCK divider into the Punit register.
  5030. */
  5031. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5032. mutex_lock(&dev_priv->rps.hw_lock);
  5033. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5034. val &= ~DSPFREQGUAR_MASK_CHV;
  5035. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5036. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5037. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5038. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5039. 50)) {
  5040. DRM_ERROR("timed out waiting for CDclk change\n");
  5041. }
  5042. mutex_unlock(&dev_priv->rps.hw_lock);
  5043. intel_update_cdclk(dev);
  5044. }
  5045. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5046. int max_pixclk)
  5047. {
  5048. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5049. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5050. /*
  5051. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5052. * 200MHz
  5053. * 267MHz
  5054. * 320/333MHz (depends on HPLL freq)
  5055. * 400MHz (VLV only)
  5056. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5057. * of the lower bin and adjust if needed.
  5058. *
  5059. * We seem to get an unstable or solid color picture at 200MHz.
  5060. * Not sure what's wrong. For now use 200MHz only when all pipes
  5061. * are off.
  5062. */
  5063. if (!IS_CHERRYVIEW(dev_priv) &&
  5064. max_pixclk > freq_320*limit/100)
  5065. return 400000;
  5066. else if (max_pixclk > 266667*limit/100)
  5067. return freq_320;
  5068. else if (max_pixclk > 0)
  5069. return 266667;
  5070. else
  5071. return 200000;
  5072. }
  5073. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  5074. int max_pixclk)
  5075. {
  5076. /*
  5077. * FIXME:
  5078. * - remove the guardband, it's not needed on BXT
  5079. * - set 19.2MHz bypass frequency if there are no active pipes
  5080. */
  5081. if (max_pixclk > 576000*9/10)
  5082. return 624000;
  5083. else if (max_pixclk > 384000*9/10)
  5084. return 576000;
  5085. else if (max_pixclk > 288000*9/10)
  5086. return 384000;
  5087. else if (max_pixclk > 144000*9/10)
  5088. return 288000;
  5089. else
  5090. return 144000;
  5091. }
  5092. /* Compute the max pixel clock for new configuration. Uses atomic state if
  5093. * that's non-NULL, look at current state otherwise. */
  5094. static int intel_mode_max_pixclk(struct drm_device *dev,
  5095. struct drm_atomic_state *state)
  5096. {
  5097. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5098. struct drm_i915_private *dev_priv = dev->dev_private;
  5099. struct drm_crtc *crtc;
  5100. struct drm_crtc_state *crtc_state;
  5101. unsigned max_pixclk = 0, i;
  5102. enum pipe pipe;
  5103. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5104. sizeof(intel_state->min_pixclk));
  5105. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5106. int pixclk = 0;
  5107. if (crtc_state->enable)
  5108. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5109. intel_state->min_pixclk[i] = pixclk;
  5110. }
  5111. if (!intel_state->active_crtcs)
  5112. return 0;
  5113. for_each_pipe(dev_priv, pipe)
  5114. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5115. return max_pixclk;
  5116. }
  5117. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5118. {
  5119. struct drm_device *dev = state->dev;
  5120. struct drm_i915_private *dev_priv = dev->dev_private;
  5121. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5122. struct intel_atomic_state *intel_state =
  5123. to_intel_atomic_state(state);
  5124. if (max_pixclk < 0)
  5125. return max_pixclk;
  5126. intel_state->cdclk = intel_state->dev_cdclk =
  5127. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5128. if (!intel_state->active_crtcs)
  5129. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5130. return 0;
  5131. }
  5132. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5133. {
  5134. struct drm_device *dev = state->dev;
  5135. struct drm_i915_private *dev_priv = dev->dev_private;
  5136. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5137. struct intel_atomic_state *intel_state =
  5138. to_intel_atomic_state(state);
  5139. if (max_pixclk < 0)
  5140. return max_pixclk;
  5141. intel_state->cdclk = intel_state->dev_cdclk =
  5142. broxton_calc_cdclk(dev_priv, max_pixclk);
  5143. if (!intel_state->active_crtcs)
  5144. intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
  5145. return 0;
  5146. }
  5147. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5148. {
  5149. unsigned int credits, default_credits;
  5150. if (IS_CHERRYVIEW(dev_priv))
  5151. default_credits = PFI_CREDIT(12);
  5152. else
  5153. default_credits = PFI_CREDIT(8);
  5154. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5155. /* CHV suggested value is 31 or 63 */
  5156. if (IS_CHERRYVIEW(dev_priv))
  5157. credits = PFI_CREDIT_63;
  5158. else
  5159. credits = PFI_CREDIT(15);
  5160. } else {
  5161. credits = default_credits;
  5162. }
  5163. /*
  5164. * WA - write default credits before re-programming
  5165. * FIXME: should we also set the resend bit here?
  5166. */
  5167. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5168. default_credits);
  5169. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5170. credits | PFI_CREDIT_RESEND);
  5171. /*
  5172. * FIXME is this guaranteed to clear
  5173. * immediately or should we poll for it?
  5174. */
  5175. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5176. }
  5177. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5178. {
  5179. struct drm_device *dev = old_state->dev;
  5180. struct drm_i915_private *dev_priv = dev->dev_private;
  5181. struct intel_atomic_state *old_intel_state =
  5182. to_intel_atomic_state(old_state);
  5183. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5184. /*
  5185. * FIXME: We can end up here with all power domains off, yet
  5186. * with a CDCLK frequency other than the minimum. To account
  5187. * for this take the PIPE-A power domain, which covers the HW
  5188. * blocks needed for the following programming. This can be
  5189. * removed once it's guaranteed that we get here either with
  5190. * the minimum CDCLK set, or the required power domains
  5191. * enabled.
  5192. */
  5193. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5194. if (IS_CHERRYVIEW(dev))
  5195. cherryview_set_cdclk(dev, req_cdclk);
  5196. else
  5197. valleyview_set_cdclk(dev, req_cdclk);
  5198. vlv_program_pfi_credits(dev_priv);
  5199. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5200. }
  5201. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5202. {
  5203. struct drm_device *dev = crtc->dev;
  5204. struct drm_i915_private *dev_priv = to_i915(dev);
  5205. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5206. struct intel_encoder *encoder;
  5207. int pipe = intel_crtc->pipe;
  5208. if (WARN_ON(intel_crtc->active))
  5209. return;
  5210. if (intel_crtc->config->has_dp_encoder)
  5211. intel_dp_set_m_n(intel_crtc, M1_N1);
  5212. intel_set_pipe_timings(intel_crtc);
  5213. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5214. struct drm_i915_private *dev_priv = dev->dev_private;
  5215. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5216. I915_WRITE(CHV_CANVAS(pipe), 0);
  5217. }
  5218. i9xx_set_pipeconf(intel_crtc);
  5219. intel_crtc->active = true;
  5220. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5221. for_each_encoder_on_crtc(dev, crtc, encoder)
  5222. if (encoder->pre_pll_enable)
  5223. encoder->pre_pll_enable(encoder);
  5224. if (!intel_crtc->config->has_dsi_encoder) {
  5225. if (IS_CHERRYVIEW(dev)) {
  5226. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5227. chv_enable_pll(intel_crtc, intel_crtc->config);
  5228. } else {
  5229. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5230. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5231. }
  5232. }
  5233. for_each_encoder_on_crtc(dev, crtc, encoder)
  5234. if (encoder->pre_enable)
  5235. encoder->pre_enable(encoder);
  5236. i9xx_pfit_enable(intel_crtc);
  5237. intel_crtc_load_lut(crtc);
  5238. intel_enable_pipe(intel_crtc);
  5239. assert_vblank_disabled(crtc);
  5240. drm_crtc_vblank_on(crtc);
  5241. for_each_encoder_on_crtc(dev, crtc, encoder)
  5242. encoder->enable(encoder);
  5243. }
  5244. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5245. {
  5246. struct drm_device *dev = crtc->base.dev;
  5247. struct drm_i915_private *dev_priv = dev->dev_private;
  5248. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5249. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5250. }
  5251. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5252. {
  5253. struct drm_device *dev = crtc->dev;
  5254. struct drm_i915_private *dev_priv = to_i915(dev);
  5255. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5256. struct intel_encoder *encoder;
  5257. int pipe = intel_crtc->pipe;
  5258. if (WARN_ON(intel_crtc->active))
  5259. return;
  5260. i9xx_set_pll_dividers(intel_crtc);
  5261. if (intel_crtc->config->has_dp_encoder)
  5262. intel_dp_set_m_n(intel_crtc, M1_N1);
  5263. intel_set_pipe_timings(intel_crtc);
  5264. i9xx_set_pipeconf(intel_crtc);
  5265. intel_crtc->active = true;
  5266. if (!IS_GEN2(dev))
  5267. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5268. for_each_encoder_on_crtc(dev, crtc, encoder)
  5269. if (encoder->pre_enable)
  5270. encoder->pre_enable(encoder);
  5271. i9xx_enable_pll(intel_crtc);
  5272. i9xx_pfit_enable(intel_crtc);
  5273. intel_crtc_load_lut(crtc);
  5274. intel_update_watermarks(crtc);
  5275. intel_enable_pipe(intel_crtc);
  5276. assert_vblank_disabled(crtc);
  5277. drm_crtc_vblank_on(crtc);
  5278. for_each_encoder_on_crtc(dev, crtc, encoder)
  5279. encoder->enable(encoder);
  5280. }
  5281. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5282. {
  5283. struct drm_device *dev = crtc->base.dev;
  5284. struct drm_i915_private *dev_priv = dev->dev_private;
  5285. if (!crtc->config->gmch_pfit.control)
  5286. return;
  5287. assert_pipe_disabled(dev_priv, crtc->pipe);
  5288. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5289. I915_READ(PFIT_CONTROL));
  5290. I915_WRITE(PFIT_CONTROL, 0);
  5291. }
  5292. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5293. {
  5294. struct drm_device *dev = crtc->dev;
  5295. struct drm_i915_private *dev_priv = dev->dev_private;
  5296. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5297. struct intel_encoder *encoder;
  5298. int pipe = intel_crtc->pipe;
  5299. /*
  5300. * On gen2 planes are double buffered but the pipe isn't, so we must
  5301. * wait for planes to fully turn off before disabling the pipe.
  5302. * We also need to wait on all gmch platforms because of the
  5303. * self-refresh mode constraint explained above.
  5304. */
  5305. intel_wait_for_vblank(dev, pipe);
  5306. for_each_encoder_on_crtc(dev, crtc, encoder)
  5307. encoder->disable(encoder);
  5308. drm_crtc_vblank_off(crtc);
  5309. assert_vblank_disabled(crtc);
  5310. intel_disable_pipe(intel_crtc);
  5311. i9xx_pfit_disable(intel_crtc);
  5312. for_each_encoder_on_crtc(dev, crtc, encoder)
  5313. if (encoder->post_disable)
  5314. encoder->post_disable(encoder);
  5315. if (!intel_crtc->config->has_dsi_encoder) {
  5316. if (IS_CHERRYVIEW(dev))
  5317. chv_disable_pll(dev_priv, pipe);
  5318. else if (IS_VALLEYVIEW(dev))
  5319. vlv_disable_pll(dev_priv, pipe);
  5320. else
  5321. i9xx_disable_pll(intel_crtc);
  5322. }
  5323. for_each_encoder_on_crtc(dev, crtc, encoder)
  5324. if (encoder->post_pll_disable)
  5325. encoder->post_pll_disable(encoder);
  5326. if (!IS_GEN2(dev))
  5327. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5328. }
  5329. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5330. {
  5331. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5332. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5333. enum intel_display_power_domain domain;
  5334. unsigned long domains;
  5335. if (!intel_crtc->active)
  5336. return;
  5337. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5338. WARN_ON(intel_crtc->unpin_work);
  5339. intel_pre_disable_primary(crtc);
  5340. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5341. to_intel_plane_state(crtc->primary->state)->visible = false;
  5342. }
  5343. dev_priv->display.crtc_disable(crtc);
  5344. intel_crtc->active = false;
  5345. intel_fbc_disable(intel_crtc);
  5346. intel_update_watermarks(crtc);
  5347. intel_disable_shared_dpll(intel_crtc);
  5348. domains = intel_crtc->enabled_power_domains;
  5349. for_each_power_domain(domain, domains)
  5350. intel_display_power_put(dev_priv, domain);
  5351. intel_crtc->enabled_power_domains = 0;
  5352. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5353. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5354. }
  5355. /*
  5356. * turn all crtc's off, but do not adjust state
  5357. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5358. */
  5359. int intel_display_suspend(struct drm_device *dev)
  5360. {
  5361. struct drm_mode_config *config = &dev->mode_config;
  5362. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5363. struct drm_atomic_state *state;
  5364. struct drm_crtc *crtc;
  5365. unsigned crtc_mask = 0;
  5366. int ret = 0;
  5367. if (WARN_ON(!ctx))
  5368. return 0;
  5369. lockdep_assert_held(&ctx->ww_ctx);
  5370. state = drm_atomic_state_alloc(dev);
  5371. if (WARN_ON(!state))
  5372. return -ENOMEM;
  5373. state->acquire_ctx = ctx;
  5374. state->allow_modeset = true;
  5375. for_each_crtc(dev, crtc) {
  5376. struct drm_crtc_state *crtc_state =
  5377. drm_atomic_get_crtc_state(state, crtc);
  5378. ret = PTR_ERR_OR_ZERO(crtc_state);
  5379. if (ret)
  5380. goto free;
  5381. if (!crtc_state->active)
  5382. continue;
  5383. crtc_state->active = false;
  5384. crtc_mask |= 1 << drm_crtc_index(crtc);
  5385. }
  5386. if (crtc_mask) {
  5387. ret = drm_atomic_commit(state);
  5388. if (!ret) {
  5389. for_each_crtc(dev, crtc)
  5390. if (crtc_mask & (1 << drm_crtc_index(crtc)))
  5391. crtc->state->active = true;
  5392. return ret;
  5393. }
  5394. }
  5395. free:
  5396. if (ret)
  5397. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5398. drm_atomic_state_free(state);
  5399. return ret;
  5400. }
  5401. void intel_encoder_destroy(struct drm_encoder *encoder)
  5402. {
  5403. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5404. drm_encoder_cleanup(encoder);
  5405. kfree(intel_encoder);
  5406. }
  5407. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5408. * internal consistency). */
  5409. static void intel_connector_check_state(struct intel_connector *connector)
  5410. {
  5411. struct drm_crtc *crtc = connector->base.state->crtc;
  5412. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5413. connector->base.base.id,
  5414. connector->base.name);
  5415. if (connector->get_hw_state(connector)) {
  5416. struct intel_encoder *encoder = connector->encoder;
  5417. struct drm_connector_state *conn_state = connector->base.state;
  5418. I915_STATE_WARN(!crtc,
  5419. "connector enabled without attached crtc\n");
  5420. if (!crtc)
  5421. return;
  5422. I915_STATE_WARN(!crtc->state->active,
  5423. "connector is active, but attached crtc isn't\n");
  5424. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5425. return;
  5426. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5427. "atomic encoder doesn't match attached encoder\n");
  5428. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5429. "attached encoder crtc differs from connector crtc\n");
  5430. } else {
  5431. I915_STATE_WARN(crtc && crtc->state->active,
  5432. "attached crtc is active, but connector isn't\n");
  5433. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5434. "best encoder set without crtc!\n");
  5435. }
  5436. }
  5437. int intel_connector_init(struct intel_connector *connector)
  5438. {
  5439. drm_atomic_helper_connector_reset(&connector->base);
  5440. if (!connector->base.state)
  5441. return -ENOMEM;
  5442. return 0;
  5443. }
  5444. struct intel_connector *intel_connector_alloc(void)
  5445. {
  5446. struct intel_connector *connector;
  5447. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5448. if (!connector)
  5449. return NULL;
  5450. if (intel_connector_init(connector) < 0) {
  5451. kfree(connector);
  5452. return NULL;
  5453. }
  5454. return connector;
  5455. }
  5456. /* Simple connector->get_hw_state implementation for encoders that support only
  5457. * one connector and no cloning and hence the encoder state determines the state
  5458. * of the connector. */
  5459. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5460. {
  5461. enum pipe pipe = 0;
  5462. struct intel_encoder *encoder = connector->encoder;
  5463. return encoder->get_hw_state(encoder, &pipe);
  5464. }
  5465. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5466. {
  5467. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5468. return crtc_state->fdi_lanes;
  5469. return 0;
  5470. }
  5471. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5472. struct intel_crtc_state *pipe_config)
  5473. {
  5474. struct drm_atomic_state *state = pipe_config->base.state;
  5475. struct intel_crtc *other_crtc;
  5476. struct intel_crtc_state *other_crtc_state;
  5477. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5478. pipe_name(pipe), pipe_config->fdi_lanes);
  5479. if (pipe_config->fdi_lanes > 4) {
  5480. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5481. pipe_name(pipe), pipe_config->fdi_lanes);
  5482. return -EINVAL;
  5483. }
  5484. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5485. if (pipe_config->fdi_lanes > 2) {
  5486. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5487. pipe_config->fdi_lanes);
  5488. return -EINVAL;
  5489. } else {
  5490. return 0;
  5491. }
  5492. }
  5493. if (INTEL_INFO(dev)->num_pipes == 2)
  5494. return 0;
  5495. /* Ivybridge 3 pipe is really complicated */
  5496. switch (pipe) {
  5497. case PIPE_A:
  5498. return 0;
  5499. case PIPE_B:
  5500. if (pipe_config->fdi_lanes <= 2)
  5501. return 0;
  5502. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5503. other_crtc_state =
  5504. intel_atomic_get_crtc_state(state, other_crtc);
  5505. if (IS_ERR(other_crtc_state))
  5506. return PTR_ERR(other_crtc_state);
  5507. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5508. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5509. pipe_name(pipe), pipe_config->fdi_lanes);
  5510. return -EINVAL;
  5511. }
  5512. return 0;
  5513. case PIPE_C:
  5514. if (pipe_config->fdi_lanes > 2) {
  5515. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5516. pipe_name(pipe), pipe_config->fdi_lanes);
  5517. return -EINVAL;
  5518. }
  5519. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5520. other_crtc_state =
  5521. intel_atomic_get_crtc_state(state, other_crtc);
  5522. if (IS_ERR(other_crtc_state))
  5523. return PTR_ERR(other_crtc_state);
  5524. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5525. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5526. return -EINVAL;
  5527. }
  5528. return 0;
  5529. default:
  5530. BUG();
  5531. }
  5532. }
  5533. #define RETRY 1
  5534. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5535. struct intel_crtc_state *pipe_config)
  5536. {
  5537. struct drm_device *dev = intel_crtc->base.dev;
  5538. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5539. int lane, link_bw, fdi_dotclock, ret;
  5540. bool needs_recompute = false;
  5541. retry:
  5542. /* FDI is a binary signal running at ~2.7GHz, encoding
  5543. * each output octet as 10 bits. The actual frequency
  5544. * is stored as a divider into a 100MHz clock, and the
  5545. * mode pixel clock is stored in units of 1KHz.
  5546. * Hence the bw of each lane in terms of the mode signal
  5547. * is:
  5548. */
  5549. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5550. fdi_dotclock = adjusted_mode->crtc_clock;
  5551. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5552. pipe_config->pipe_bpp);
  5553. pipe_config->fdi_lanes = lane;
  5554. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5555. link_bw, &pipe_config->fdi_m_n);
  5556. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5557. intel_crtc->pipe, pipe_config);
  5558. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5559. pipe_config->pipe_bpp -= 2*3;
  5560. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5561. pipe_config->pipe_bpp);
  5562. needs_recompute = true;
  5563. pipe_config->bw_constrained = true;
  5564. goto retry;
  5565. }
  5566. if (needs_recompute)
  5567. return RETRY;
  5568. return ret;
  5569. }
  5570. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5571. struct intel_crtc_state *pipe_config)
  5572. {
  5573. if (pipe_config->pipe_bpp > 24)
  5574. return false;
  5575. /* HSW can handle pixel rate up to cdclk? */
  5576. if (IS_HASWELL(dev_priv->dev))
  5577. return true;
  5578. /*
  5579. * We compare against max which means we must take
  5580. * the increased cdclk requirement into account when
  5581. * calculating the new cdclk.
  5582. *
  5583. * Should measure whether using a lower cdclk w/o IPS
  5584. */
  5585. return ilk_pipe_pixel_rate(pipe_config) <=
  5586. dev_priv->max_cdclk_freq * 95 / 100;
  5587. }
  5588. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5589. struct intel_crtc_state *pipe_config)
  5590. {
  5591. struct drm_device *dev = crtc->base.dev;
  5592. struct drm_i915_private *dev_priv = dev->dev_private;
  5593. pipe_config->ips_enabled = i915.enable_ips &&
  5594. hsw_crtc_supports_ips(crtc) &&
  5595. pipe_config_supports_ips(dev_priv, pipe_config);
  5596. }
  5597. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5598. {
  5599. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5600. /* GDG double wide on either pipe, otherwise pipe A only */
  5601. return INTEL_INFO(dev_priv)->gen < 4 &&
  5602. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5603. }
  5604. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5605. struct intel_crtc_state *pipe_config)
  5606. {
  5607. struct drm_device *dev = crtc->base.dev;
  5608. struct drm_i915_private *dev_priv = dev->dev_private;
  5609. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5610. /* FIXME should check pixel clock limits on all platforms */
  5611. if (INTEL_INFO(dev)->gen < 4) {
  5612. int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5613. /*
  5614. * Enable double wide mode when the dot clock
  5615. * is > 90% of the (display) core speed.
  5616. */
  5617. if (intel_crtc_supports_double_wide(crtc) &&
  5618. adjusted_mode->crtc_clock > clock_limit) {
  5619. clock_limit *= 2;
  5620. pipe_config->double_wide = true;
  5621. }
  5622. if (adjusted_mode->crtc_clock > clock_limit) {
  5623. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5624. adjusted_mode->crtc_clock, clock_limit,
  5625. yesno(pipe_config->double_wide));
  5626. return -EINVAL;
  5627. }
  5628. }
  5629. /*
  5630. * Pipe horizontal size must be even in:
  5631. * - DVO ganged mode
  5632. * - LVDS dual channel mode
  5633. * - Double wide pipe
  5634. */
  5635. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5636. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5637. pipe_config->pipe_src_w &= ~1;
  5638. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5639. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5640. */
  5641. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5642. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5643. return -EINVAL;
  5644. if (HAS_IPS(dev))
  5645. hsw_compute_ips_config(crtc, pipe_config);
  5646. if (pipe_config->has_pch_encoder)
  5647. return ironlake_fdi_compute_config(crtc, pipe_config);
  5648. return 0;
  5649. }
  5650. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5651. {
  5652. struct drm_i915_private *dev_priv = to_i915(dev);
  5653. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5654. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5655. uint32_t linkrate;
  5656. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5657. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5658. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5659. return 540000;
  5660. linkrate = (I915_READ(DPLL_CTRL1) &
  5661. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5662. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5663. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5664. /* vco 8640 */
  5665. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5666. case CDCLK_FREQ_450_432:
  5667. return 432000;
  5668. case CDCLK_FREQ_337_308:
  5669. return 308570;
  5670. case CDCLK_FREQ_675_617:
  5671. return 617140;
  5672. default:
  5673. WARN(1, "Unknown cd freq selection\n");
  5674. }
  5675. } else {
  5676. /* vco 8100 */
  5677. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5678. case CDCLK_FREQ_450_432:
  5679. return 450000;
  5680. case CDCLK_FREQ_337_308:
  5681. return 337500;
  5682. case CDCLK_FREQ_675_617:
  5683. return 675000;
  5684. default:
  5685. WARN(1, "Unknown cd freq selection\n");
  5686. }
  5687. }
  5688. /* error case, do as if DPLL0 isn't enabled */
  5689. return 24000;
  5690. }
  5691. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5692. {
  5693. struct drm_i915_private *dev_priv = to_i915(dev);
  5694. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5695. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5696. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5697. int cdclk;
  5698. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5699. return 19200;
  5700. cdclk = 19200 * pll_ratio / 2;
  5701. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5702. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5703. return cdclk; /* 576MHz or 624MHz */
  5704. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5705. return cdclk * 2 / 3; /* 384MHz */
  5706. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5707. return cdclk / 2; /* 288MHz */
  5708. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5709. return cdclk / 4; /* 144MHz */
  5710. }
  5711. /* error case, do as if DE PLL isn't enabled */
  5712. return 19200;
  5713. }
  5714. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5715. {
  5716. struct drm_i915_private *dev_priv = dev->dev_private;
  5717. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5718. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5719. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5720. return 800000;
  5721. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5722. return 450000;
  5723. else if (freq == LCPLL_CLK_FREQ_450)
  5724. return 450000;
  5725. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5726. return 540000;
  5727. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5728. return 337500;
  5729. else
  5730. return 675000;
  5731. }
  5732. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5733. {
  5734. struct drm_i915_private *dev_priv = dev->dev_private;
  5735. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5736. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5737. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5738. return 800000;
  5739. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5740. return 450000;
  5741. else if (freq == LCPLL_CLK_FREQ_450)
  5742. return 450000;
  5743. else if (IS_HSW_ULT(dev))
  5744. return 337500;
  5745. else
  5746. return 540000;
  5747. }
  5748. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5749. {
  5750. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5751. CCK_DISPLAY_CLOCK_CONTROL);
  5752. }
  5753. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5754. {
  5755. return 450000;
  5756. }
  5757. static int i945_get_display_clock_speed(struct drm_device *dev)
  5758. {
  5759. return 400000;
  5760. }
  5761. static int i915_get_display_clock_speed(struct drm_device *dev)
  5762. {
  5763. return 333333;
  5764. }
  5765. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5766. {
  5767. return 200000;
  5768. }
  5769. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5770. {
  5771. u16 gcfgc = 0;
  5772. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5773. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5774. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5775. return 266667;
  5776. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5777. return 333333;
  5778. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5779. return 444444;
  5780. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5781. return 200000;
  5782. default:
  5783. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5784. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5785. return 133333;
  5786. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5787. return 166667;
  5788. }
  5789. }
  5790. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5791. {
  5792. u16 gcfgc = 0;
  5793. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5794. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5795. return 133333;
  5796. else {
  5797. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5798. case GC_DISPLAY_CLOCK_333_MHZ:
  5799. return 333333;
  5800. default:
  5801. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5802. return 190000;
  5803. }
  5804. }
  5805. }
  5806. static int i865_get_display_clock_speed(struct drm_device *dev)
  5807. {
  5808. return 266667;
  5809. }
  5810. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5811. {
  5812. u16 hpllcc = 0;
  5813. /*
  5814. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5815. * encoding is different :(
  5816. * FIXME is this the right way to detect 852GM/852GMV?
  5817. */
  5818. if (dev->pdev->revision == 0x1)
  5819. return 133333;
  5820. pci_bus_read_config_word(dev->pdev->bus,
  5821. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5822. /* Assume that the hardware is in the high speed state. This
  5823. * should be the default.
  5824. */
  5825. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5826. case GC_CLOCK_133_200:
  5827. case GC_CLOCK_133_200_2:
  5828. case GC_CLOCK_100_200:
  5829. return 200000;
  5830. case GC_CLOCK_166_250:
  5831. return 250000;
  5832. case GC_CLOCK_100_133:
  5833. return 133333;
  5834. case GC_CLOCK_133_266:
  5835. case GC_CLOCK_133_266_2:
  5836. case GC_CLOCK_166_266:
  5837. return 266667;
  5838. }
  5839. /* Shouldn't happen */
  5840. return 0;
  5841. }
  5842. static int i830_get_display_clock_speed(struct drm_device *dev)
  5843. {
  5844. return 133333;
  5845. }
  5846. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5847. {
  5848. struct drm_i915_private *dev_priv = dev->dev_private;
  5849. static const unsigned int blb_vco[8] = {
  5850. [0] = 3200000,
  5851. [1] = 4000000,
  5852. [2] = 5333333,
  5853. [3] = 4800000,
  5854. [4] = 6400000,
  5855. };
  5856. static const unsigned int pnv_vco[8] = {
  5857. [0] = 3200000,
  5858. [1] = 4000000,
  5859. [2] = 5333333,
  5860. [3] = 4800000,
  5861. [4] = 2666667,
  5862. };
  5863. static const unsigned int cl_vco[8] = {
  5864. [0] = 3200000,
  5865. [1] = 4000000,
  5866. [2] = 5333333,
  5867. [3] = 6400000,
  5868. [4] = 3333333,
  5869. [5] = 3566667,
  5870. [6] = 4266667,
  5871. };
  5872. static const unsigned int elk_vco[8] = {
  5873. [0] = 3200000,
  5874. [1] = 4000000,
  5875. [2] = 5333333,
  5876. [3] = 4800000,
  5877. };
  5878. static const unsigned int ctg_vco[8] = {
  5879. [0] = 3200000,
  5880. [1] = 4000000,
  5881. [2] = 5333333,
  5882. [3] = 6400000,
  5883. [4] = 2666667,
  5884. [5] = 4266667,
  5885. };
  5886. const unsigned int *vco_table;
  5887. unsigned int vco;
  5888. uint8_t tmp = 0;
  5889. /* FIXME other chipsets? */
  5890. if (IS_GM45(dev))
  5891. vco_table = ctg_vco;
  5892. else if (IS_G4X(dev))
  5893. vco_table = elk_vco;
  5894. else if (IS_CRESTLINE(dev))
  5895. vco_table = cl_vco;
  5896. else if (IS_PINEVIEW(dev))
  5897. vco_table = pnv_vco;
  5898. else if (IS_G33(dev))
  5899. vco_table = blb_vco;
  5900. else
  5901. return 0;
  5902. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5903. vco = vco_table[tmp & 0x7];
  5904. if (vco == 0)
  5905. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5906. else
  5907. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5908. return vco;
  5909. }
  5910. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5911. {
  5912. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5913. uint16_t tmp = 0;
  5914. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5915. cdclk_sel = (tmp >> 12) & 0x1;
  5916. switch (vco) {
  5917. case 2666667:
  5918. case 4000000:
  5919. case 5333333:
  5920. return cdclk_sel ? 333333 : 222222;
  5921. case 3200000:
  5922. return cdclk_sel ? 320000 : 228571;
  5923. default:
  5924. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5925. return 222222;
  5926. }
  5927. }
  5928. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5929. {
  5930. static const uint8_t div_3200[] = { 16, 10, 8 };
  5931. static const uint8_t div_4000[] = { 20, 12, 10 };
  5932. static const uint8_t div_5333[] = { 24, 16, 14 };
  5933. const uint8_t *div_table;
  5934. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5935. uint16_t tmp = 0;
  5936. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5937. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5938. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5939. goto fail;
  5940. switch (vco) {
  5941. case 3200000:
  5942. div_table = div_3200;
  5943. break;
  5944. case 4000000:
  5945. div_table = div_4000;
  5946. break;
  5947. case 5333333:
  5948. div_table = div_5333;
  5949. break;
  5950. default:
  5951. goto fail;
  5952. }
  5953. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5954. fail:
  5955. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5956. return 200000;
  5957. }
  5958. static int g33_get_display_clock_speed(struct drm_device *dev)
  5959. {
  5960. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5961. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5962. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5963. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5964. const uint8_t *div_table;
  5965. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5966. uint16_t tmp = 0;
  5967. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5968. cdclk_sel = (tmp >> 4) & 0x7;
  5969. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5970. goto fail;
  5971. switch (vco) {
  5972. case 3200000:
  5973. div_table = div_3200;
  5974. break;
  5975. case 4000000:
  5976. div_table = div_4000;
  5977. break;
  5978. case 4800000:
  5979. div_table = div_4800;
  5980. break;
  5981. case 5333333:
  5982. div_table = div_5333;
  5983. break;
  5984. default:
  5985. goto fail;
  5986. }
  5987. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5988. fail:
  5989. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5990. return 190476;
  5991. }
  5992. static void
  5993. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5994. {
  5995. while (*num > DATA_LINK_M_N_MASK ||
  5996. *den > DATA_LINK_M_N_MASK) {
  5997. *num >>= 1;
  5998. *den >>= 1;
  5999. }
  6000. }
  6001. static void compute_m_n(unsigned int m, unsigned int n,
  6002. uint32_t *ret_m, uint32_t *ret_n)
  6003. {
  6004. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  6005. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  6006. intel_reduce_m_n_ratio(ret_m, ret_n);
  6007. }
  6008. void
  6009. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  6010. int pixel_clock, int link_clock,
  6011. struct intel_link_m_n *m_n)
  6012. {
  6013. m_n->tu = 64;
  6014. compute_m_n(bits_per_pixel * pixel_clock,
  6015. link_clock * nlanes * 8,
  6016. &m_n->gmch_m, &m_n->gmch_n);
  6017. compute_m_n(pixel_clock, link_clock,
  6018. &m_n->link_m, &m_n->link_n);
  6019. }
  6020. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  6021. {
  6022. if (i915.panel_use_ssc >= 0)
  6023. return i915.panel_use_ssc != 0;
  6024. return dev_priv->vbt.lvds_use_ssc
  6025. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  6026. }
  6027. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  6028. int num_connectors)
  6029. {
  6030. struct drm_device *dev = crtc_state->base.crtc->dev;
  6031. struct drm_i915_private *dev_priv = dev->dev_private;
  6032. int refclk;
  6033. WARN_ON(!crtc_state->base.state);
  6034. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) {
  6035. refclk = 100000;
  6036. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6037. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  6038. refclk = dev_priv->vbt.lvds_ssc_freq;
  6039. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6040. } else if (!IS_GEN2(dev)) {
  6041. refclk = 96000;
  6042. } else {
  6043. refclk = 48000;
  6044. }
  6045. return refclk;
  6046. }
  6047. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6048. {
  6049. return (1 << dpll->n) << 16 | dpll->m2;
  6050. }
  6051. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6052. {
  6053. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6054. }
  6055. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6056. struct intel_crtc_state *crtc_state,
  6057. intel_clock_t *reduced_clock)
  6058. {
  6059. struct drm_device *dev = crtc->base.dev;
  6060. u32 fp, fp2 = 0;
  6061. if (IS_PINEVIEW(dev)) {
  6062. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6063. if (reduced_clock)
  6064. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6065. } else {
  6066. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6067. if (reduced_clock)
  6068. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6069. }
  6070. crtc_state->dpll_hw_state.fp0 = fp;
  6071. crtc->lowfreq_avail = false;
  6072. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6073. reduced_clock) {
  6074. crtc_state->dpll_hw_state.fp1 = fp2;
  6075. crtc->lowfreq_avail = true;
  6076. } else {
  6077. crtc_state->dpll_hw_state.fp1 = fp;
  6078. }
  6079. }
  6080. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6081. pipe)
  6082. {
  6083. u32 reg_val;
  6084. /*
  6085. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6086. * and set it to a reasonable value instead.
  6087. */
  6088. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6089. reg_val &= 0xffffff00;
  6090. reg_val |= 0x00000030;
  6091. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6092. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6093. reg_val &= 0x8cffffff;
  6094. reg_val = 0x8c000000;
  6095. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6096. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6097. reg_val &= 0xffffff00;
  6098. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6099. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6100. reg_val &= 0x00ffffff;
  6101. reg_val |= 0xb0000000;
  6102. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6103. }
  6104. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6105. struct intel_link_m_n *m_n)
  6106. {
  6107. struct drm_device *dev = crtc->base.dev;
  6108. struct drm_i915_private *dev_priv = dev->dev_private;
  6109. int pipe = crtc->pipe;
  6110. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6111. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6112. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6113. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6114. }
  6115. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6116. struct intel_link_m_n *m_n,
  6117. struct intel_link_m_n *m2_n2)
  6118. {
  6119. struct drm_device *dev = crtc->base.dev;
  6120. struct drm_i915_private *dev_priv = dev->dev_private;
  6121. int pipe = crtc->pipe;
  6122. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6123. if (INTEL_INFO(dev)->gen >= 5) {
  6124. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6125. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6126. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6127. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6128. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6129. * for gen < 8) and if DRRS is supported (to make sure the
  6130. * registers are not unnecessarily accessed).
  6131. */
  6132. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6133. crtc->config->has_drrs) {
  6134. I915_WRITE(PIPE_DATA_M2(transcoder),
  6135. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6136. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6137. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6138. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6139. }
  6140. } else {
  6141. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6142. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6143. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6144. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6145. }
  6146. }
  6147. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6148. {
  6149. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6150. if (m_n == M1_N1) {
  6151. dp_m_n = &crtc->config->dp_m_n;
  6152. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6153. } else if (m_n == M2_N2) {
  6154. /*
  6155. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6156. * needs to be programmed into M1_N1.
  6157. */
  6158. dp_m_n = &crtc->config->dp_m2_n2;
  6159. } else {
  6160. DRM_ERROR("Unsupported divider value\n");
  6161. return;
  6162. }
  6163. if (crtc->config->has_pch_encoder)
  6164. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6165. else
  6166. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6167. }
  6168. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6169. struct intel_crtc_state *pipe_config)
  6170. {
  6171. u32 dpll, dpll_md;
  6172. /*
  6173. * Enable DPIO clock input. We should never disable the reference
  6174. * clock for pipe B, since VGA hotplug / manual detection depends
  6175. * on it.
  6176. */
  6177. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
  6178. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
  6179. /* We should never disable this, set it here for state tracking */
  6180. if (crtc->pipe == PIPE_B)
  6181. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6182. dpll |= DPLL_VCO_ENABLE;
  6183. pipe_config->dpll_hw_state.dpll = dpll;
  6184. dpll_md = (pipe_config->pixel_multiplier - 1)
  6185. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6186. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6187. }
  6188. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6189. const struct intel_crtc_state *pipe_config)
  6190. {
  6191. struct drm_device *dev = crtc->base.dev;
  6192. struct drm_i915_private *dev_priv = dev->dev_private;
  6193. int pipe = crtc->pipe;
  6194. u32 mdiv;
  6195. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6196. u32 coreclk, reg_val;
  6197. mutex_lock(&dev_priv->sb_lock);
  6198. bestn = pipe_config->dpll.n;
  6199. bestm1 = pipe_config->dpll.m1;
  6200. bestm2 = pipe_config->dpll.m2;
  6201. bestp1 = pipe_config->dpll.p1;
  6202. bestp2 = pipe_config->dpll.p2;
  6203. /* See eDP HDMI DPIO driver vbios notes doc */
  6204. /* PLL B needs special handling */
  6205. if (pipe == PIPE_B)
  6206. vlv_pllb_recal_opamp(dev_priv, pipe);
  6207. /* Set up Tx target for periodic Rcomp update */
  6208. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6209. /* Disable target IRef on PLL */
  6210. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6211. reg_val &= 0x00ffffff;
  6212. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6213. /* Disable fast lock */
  6214. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6215. /* Set idtafcrecal before PLL is enabled */
  6216. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6217. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6218. mdiv |= ((bestn << DPIO_N_SHIFT));
  6219. mdiv |= (1 << DPIO_K_SHIFT);
  6220. /*
  6221. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6222. * but we don't support that).
  6223. * Note: don't use the DAC post divider as it seems unstable.
  6224. */
  6225. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6226. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6227. mdiv |= DPIO_ENABLE_CALIBRATION;
  6228. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6229. /* Set HBR and RBR LPF coefficients */
  6230. if (pipe_config->port_clock == 162000 ||
  6231. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6232. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6233. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6234. 0x009f0003);
  6235. else
  6236. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6237. 0x00d0000f);
  6238. if (pipe_config->has_dp_encoder) {
  6239. /* Use SSC source */
  6240. if (pipe == PIPE_A)
  6241. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6242. 0x0df40000);
  6243. else
  6244. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6245. 0x0df70000);
  6246. } else { /* HDMI or VGA */
  6247. /* Use bend source */
  6248. if (pipe == PIPE_A)
  6249. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6250. 0x0df70000);
  6251. else
  6252. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6253. 0x0df40000);
  6254. }
  6255. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6256. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6257. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6258. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6259. coreclk |= 0x01000000;
  6260. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6261. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6262. mutex_unlock(&dev_priv->sb_lock);
  6263. }
  6264. static void chv_compute_dpll(struct intel_crtc *crtc,
  6265. struct intel_crtc_state *pipe_config)
  6266. {
  6267. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6268. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6269. DPLL_VCO_ENABLE;
  6270. if (crtc->pipe != PIPE_A)
  6271. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6272. pipe_config->dpll_hw_state.dpll_md =
  6273. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6274. }
  6275. static void chv_prepare_pll(struct intel_crtc *crtc,
  6276. const struct intel_crtc_state *pipe_config)
  6277. {
  6278. struct drm_device *dev = crtc->base.dev;
  6279. struct drm_i915_private *dev_priv = dev->dev_private;
  6280. int pipe = crtc->pipe;
  6281. i915_reg_t dpll_reg = DPLL(crtc->pipe);
  6282. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6283. u32 loopfilter, tribuf_calcntr;
  6284. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6285. u32 dpio_val;
  6286. int vco;
  6287. bestn = pipe_config->dpll.n;
  6288. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6289. bestm1 = pipe_config->dpll.m1;
  6290. bestm2 = pipe_config->dpll.m2 >> 22;
  6291. bestp1 = pipe_config->dpll.p1;
  6292. bestp2 = pipe_config->dpll.p2;
  6293. vco = pipe_config->dpll.vco;
  6294. dpio_val = 0;
  6295. loopfilter = 0;
  6296. /*
  6297. * Enable Refclk and SSC
  6298. */
  6299. I915_WRITE(dpll_reg,
  6300. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6301. mutex_lock(&dev_priv->sb_lock);
  6302. /* p1 and p2 divider */
  6303. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6304. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6305. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6306. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6307. 1 << DPIO_CHV_K_DIV_SHIFT);
  6308. /* Feedback post-divider - m2 */
  6309. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6310. /* Feedback refclk divider - n and m1 */
  6311. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6312. DPIO_CHV_M1_DIV_BY_2 |
  6313. 1 << DPIO_CHV_N_DIV_SHIFT);
  6314. /* M2 fraction division */
  6315. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6316. /* M2 fraction division enable */
  6317. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6318. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6319. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6320. if (bestm2_frac)
  6321. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6322. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6323. /* Program digital lock detect threshold */
  6324. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6325. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6326. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6327. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6328. if (!bestm2_frac)
  6329. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6330. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6331. /* Loop filter */
  6332. if (vco == 5400000) {
  6333. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6334. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6335. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6336. tribuf_calcntr = 0x9;
  6337. } else if (vco <= 6200000) {
  6338. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6339. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6340. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6341. tribuf_calcntr = 0x9;
  6342. } else if (vco <= 6480000) {
  6343. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6344. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6345. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6346. tribuf_calcntr = 0x8;
  6347. } else {
  6348. /* Not supported. Apply the same limits as in the max case */
  6349. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6350. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6351. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6352. tribuf_calcntr = 0;
  6353. }
  6354. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6355. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6356. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6357. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6358. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6359. /* AFC Recal */
  6360. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6361. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6362. DPIO_AFC_RECAL);
  6363. mutex_unlock(&dev_priv->sb_lock);
  6364. }
  6365. /**
  6366. * vlv_force_pll_on - forcibly enable just the PLL
  6367. * @dev_priv: i915 private structure
  6368. * @pipe: pipe PLL to enable
  6369. * @dpll: PLL configuration
  6370. *
  6371. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6372. * in cases where we need the PLL enabled even when @pipe is not going to
  6373. * be enabled.
  6374. */
  6375. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6376. const struct dpll *dpll)
  6377. {
  6378. struct intel_crtc *crtc =
  6379. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6380. struct intel_crtc_state *pipe_config;
  6381. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6382. if (!pipe_config)
  6383. return -ENOMEM;
  6384. pipe_config->base.crtc = &crtc->base;
  6385. pipe_config->pixel_multiplier = 1;
  6386. pipe_config->dpll = *dpll;
  6387. if (IS_CHERRYVIEW(dev)) {
  6388. chv_compute_dpll(crtc, pipe_config);
  6389. chv_prepare_pll(crtc, pipe_config);
  6390. chv_enable_pll(crtc, pipe_config);
  6391. } else {
  6392. vlv_compute_dpll(crtc, pipe_config);
  6393. vlv_prepare_pll(crtc, pipe_config);
  6394. vlv_enable_pll(crtc, pipe_config);
  6395. }
  6396. kfree(pipe_config);
  6397. return 0;
  6398. }
  6399. /**
  6400. * vlv_force_pll_off - forcibly disable just the PLL
  6401. * @dev_priv: i915 private structure
  6402. * @pipe: pipe PLL to disable
  6403. *
  6404. * Disable the PLL for @pipe. To be used in cases where we need
  6405. * the PLL enabled even when @pipe is not going to be enabled.
  6406. */
  6407. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6408. {
  6409. if (IS_CHERRYVIEW(dev))
  6410. chv_disable_pll(to_i915(dev), pipe);
  6411. else
  6412. vlv_disable_pll(to_i915(dev), pipe);
  6413. }
  6414. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6415. struct intel_crtc_state *crtc_state,
  6416. intel_clock_t *reduced_clock,
  6417. int num_connectors)
  6418. {
  6419. struct drm_device *dev = crtc->base.dev;
  6420. struct drm_i915_private *dev_priv = dev->dev_private;
  6421. u32 dpll;
  6422. bool is_sdvo;
  6423. struct dpll *clock = &crtc_state->dpll;
  6424. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6425. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6426. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6427. dpll = DPLL_VGA_MODE_DIS;
  6428. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6429. dpll |= DPLLB_MODE_LVDS;
  6430. else
  6431. dpll |= DPLLB_MODE_DAC_SERIAL;
  6432. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6433. dpll |= (crtc_state->pixel_multiplier - 1)
  6434. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6435. }
  6436. if (is_sdvo)
  6437. dpll |= DPLL_SDVO_HIGH_SPEED;
  6438. if (crtc_state->has_dp_encoder)
  6439. dpll |= DPLL_SDVO_HIGH_SPEED;
  6440. /* compute bitmask from p1 value */
  6441. if (IS_PINEVIEW(dev))
  6442. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6443. else {
  6444. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6445. if (IS_G4X(dev) && reduced_clock)
  6446. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6447. }
  6448. switch (clock->p2) {
  6449. case 5:
  6450. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6451. break;
  6452. case 7:
  6453. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6454. break;
  6455. case 10:
  6456. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6457. break;
  6458. case 14:
  6459. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6460. break;
  6461. }
  6462. if (INTEL_INFO(dev)->gen >= 4)
  6463. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6464. if (crtc_state->sdvo_tv_clock)
  6465. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6466. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6467. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6468. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6469. else
  6470. dpll |= PLL_REF_INPUT_DREFCLK;
  6471. dpll |= DPLL_VCO_ENABLE;
  6472. crtc_state->dpll_hw_state.dpll = dpll;
  6473. if (INTEL_INFO(dev)->gen >= 4) {
  6474. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6475. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6476. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6477. }
  6478. }
  6479. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6480. struct intel_crtc_state *crtc_state,
  6481. intel_clock_t *reduced_clock,
  6482. int num_connectors)
  6483. {
  6484. struct drm_device *dev = crtc->base.dev;
  6485. struct drm_i915_private *dev_priv = dev->dev_private;
  6486. u32 dpll;
  6487. struct dpll *clock = &crtc_state->dpll;
  6488. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6489. dpll = DPLL_VGA_MODE_DIS;
  6490. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6491. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6492. } else {
  6493. if (clock->p1 == 2)
  6494. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6495. else
  6496. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6497. if (clock->p2 == 4)
  6498. dpll |= PLL_P2_DIVIDE_BY_4;
  6499. }
  6500. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6501. dpll |= DPLL_DVO_2X_MODE;
  6502. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6503. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6504. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6505. else
  6506. dpll |= PLL_REF_INPUT_DREFCLK;
  6507. dpll |= DPLL_VCO_ENABLE;
  6508. crtc_state->dpll_hw_state.dpll = dpll;
  6509. }
  6510. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6511. {
  6512. struct drm_device *dev = intel_crtc->base.dev;
  6513. struct drm_i915_private *dev_priv = dev->dev_private;
  6514. enum pipe pipe = intel_crtc->pipe;
  6515. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6516. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6517. uint32_t crtc_vtotal, crtc_vblank_end;
  6518. int vsyncshift = 0;
  6519. /* We need to be careful not to changed the adjusted mode, for otherwise
  6520. * the hw state checker will get angry at the mismatch. */
  6521. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6522. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6523. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6524. /* the chip adds 2 halflines automatically */
  6525. crtc_vtotal -= 1;
  6526. crtc_vblank_end -= 1;
  6527. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6528. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6529. else
  6530. vsyncshift = adjusted_mode->crtc_hsync_start -
  6531. adjusted_mode->crtc_htotal / 2;
  6532. if (vsyncshift < 0)
  6533. vsyncshift += adjusted_mode->crtc_htotal;
  6534. }
  6535. if (INTEL_INFO(dev)->gen > 3)
  6536. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6537. I915_WRITE(HTOTAL(cpu_transcoder),
  6538. (adjusted_mode->crtc_hdisplay - 1) |
  6539. ((adjusted_mode->crtc_htotal - 1) << 16));
  6540. I915_WRITE(HBLANK(cpu_transcoder),
  6541. (adjusted_mode->crtc_hblank_start - 1) |
  6542. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6543. I915_WRITE(HSYNC(cpu_transcoder),
  6544. (adjusted_mode->crtc_hsync_start - 1) |
  6545. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6546. I915_WRITE(VTOTAL(cpu_transcoder),
  6547. (adjusted_mode->crtc_vdisplay - 1) |
  6548. ((crtc_vtotal - 1) << 16));
  6549. I915_WRITE(VBLANK(cpu_transcoder),
  6550. (adjusted_mode->crtc_vblank_start - 1) |
  6551. ((crtc_vblank_end - 1) << 16));
  6552. I915_WRITE(VSYNC(cpu_transcoder),
  6553. (adjusted_mode->crtc_vsync_start - 1) |
  6554. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6555. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6556. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6557. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6558. * bits. */
  6559. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6560. (pipe == PIPE_B || pipe == PIPE_C))
  6561. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6562. /* pipesrc controls the size that is scaled from, which should
  6563. * always be the user's requested size.
  6564. */
  6565. I915_WRITE(PIPESRC(pipe),
  6566. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6567. (intel_crtc->config->pipe_src_h - 1));
  6568. }
  6569. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6570. struct intel_crtc_state *pipe_config)
  6571. {
  6572. struct drm_device *dev = crtc->base.dev;
  6573. struct drm_i915_private *dev_priv = dev->dev_private;
  6574. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6575. uint32_t tmp;
  6576. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6577. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6578. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6579. tmp = I915_READ(HBLANK(cpu_transcoder));
  6580. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6581. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6582. tmp = I915_READ(HSYNC(cpu_transcoder));
  6583. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6584. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6585. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6586. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6587. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6588. tmp = I915_READ(VBLANK(cpu_transcoder));
  6589. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6590. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6591. tmp = I915_READ(VSYNC(cpu_transcoder));
  6592. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6593. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6594. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6595. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6596. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6597. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6598. }
  6599. tmp = I915_READ(PIPESRC(crtc->pipe));
  6600. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6601. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6602. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6603. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6604. }
  6605. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6606. struct intel_crtc_state *pipe_config)
  6607. {
  6608. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6609. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6610. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6611. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6612. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6613. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6614. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6615. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6616. mode->flags = pipe_config->base.adjusted_mode.flags;
  6617. mode->type = DRM_MODE_TYPE_DRIVER;
  6618. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6619. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6620. mode->hsync = drm_mode_hsync(mode);
  6621. mode->vrefresh = drm_mode_vrefresh(mode);
  6622. drm_mode_set_name(mode);
  6623. }
  6624. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6625. {
  6626. struct drm_device *dev = intel_crtc->base.dev;
  6627. struct drm_i915_private *dev_priv = dev->dev_private;
  6628. uint32_t pipeconf;
  6629. pipeconf = 0;
  6630. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6631. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6632. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6633. if (intel_crtc->config->double_wide)
  6634. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6635. /* only g4x and later have fancy bpc/dither controls */
  6636. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6637. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6638. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6639. pipeconf |= PIPECONF_DITHER_EN |
  6640. PIPECONF_DITHER_TYPE_SP;
  6641. switch (intel_crtc->config->pipe_bpp) {
  6642. case 18:
  6643. pipeconf |= PIPECONF_6BPC;
  6644. break;
  6645. case 24:
  6646. pipeconf |= PIPECONF_8BPC;
  6647. break;
  6648. case 30:
  6649. pipeconf |= PIPECONF_10BPC;
  6650. break;
  6651. default:
  6652. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6653. BUG();
  6654. }
  6655. }
  6656. if (HAS_PIPE_CXSR(dev)) {
  6657. if (intel_crtc->lowfreq_avail) {
  6658. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6659. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6660. } else {
  6661. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6662. }
  6663. }
  6664. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6665. if (INTEL_INFO(dev)->gen < 4 ||
  6666. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6667. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6668. else
  6669. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6670. } else
  6671. pipeconf |= PIPECONF_PROGRESSIVE;
  6672. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6673. intel_crtc->config->limited_color_range)
  6674. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6675. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6676. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6677. }
  6678. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6679. struct intel_crtc_state *crtc_state)
  6680. {
  6681. struct drm_device *dev = crtc->base.dev;
  6682. struct drm_i915_private *dev_priv = dev->dev_private;
  6683. int refclk, num_connectors = 0;
  6684. intel_clock_t clock;
  6685. bool ok;
  6686. const intel_limit_t *limit;
  6687. struct drm_atomic_state *state = crtc_state->base.state;
  6688. struct drm_connector *connector;
  6689. struct drm_connector_state *connector_state;
  6690. int i;
  6691. memset(&crtc_state->dpll_hw_state, 0,
  6692. sizeof(crtc_state->dpll_hw_state));
  6693. if (crtc_state->has_dsi_encoder)
  6694. return 0;
  6695. for_each_connector_in_state(state, connector, connector_state, i) {
  6696. if (connector_state->crtc == &crtc->base)
  6697. num_connectors++;
  6698. }
  6699. if (!crtc_state->clock_set) {
  6700. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6701. /*
  6702. * Returns a set of divisors for the desired target clock with
  6703. * the given refclk, or FALSE. The returned values represent
  6704. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6705. * 2) / p1 / p2.
  6706. */
  6707. limit = intel_limit(crtc_state, refclk);
  6708. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6709. crtc_state->port_clock,
  6710. refclk, NULL, &clock);
  6711. if (!ok) {
  6712. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6713. return -EINVAL;
  6714. }
  6715. /* Compat-code for transition, will disappear. */
  6716. crtc_state->dpll.n = clock.n;
  6717. crtc_state->dpll.m1 = clock.m1;
  6718. crtc_state->dpll.m2 = clock.m2;
  6719. crtc_state->dpll.p1 = clock.p1;
  6720. crtc_state->dpll.p2 = clock.p2;
  6721. }
  6722. if (IS_GEN2(dev)) {
  6723. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6724. num_connectors);
  6725. } else if (IS_CHERRYVIEW(dev)) {
  6726. chv_compute_dpll(crtc, crtc_state);
  6727. } else if (IS_VALLEYVIEW(dev)) {
  6728. vlv_compute_dpll(crtc, crtc_state);
  6729. } else {
  6730. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6731. num_connectors);
  6732. }
  6733. return 0;
  6734. }
  6735. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6736. struct intel_crtc_state *pipe_config)
  6737. {
  6738. struct drm_device *dev = crtc->base.dev;
  6739. struct drm_i915_private *dev_priv = dev->dev_private;
  6740. uint32_t tmp;
  6741. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6742. return;
  6743. tmp = I915_READ(PFIT_CONTROL);
  6744. if (!(tmp & PFIT_ENABLE))
  6745. return;
  6746. /* Check whether the pfit is attached to our pipe. */
  6747. if (INTEL_INFO(dev)->gen < 4) {
  6748. if (crtc->pipe != PIPE_B)
  6749. return;
  6750. } else {
  6751. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6752. return;
  6753. }
  6754. pipe_config->gmch_pfit.control = tmp;
  6755. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6756. if (INTEL_INFO(dev)->gen < 5)
  6757. pipe_config->gmch_pfit.lvds_border_bits =
  6758. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6759. }
  6760. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6761. struct intel_crtc_state *pipe_config)
  6762. {
  6763. struct drm_device *dev = crtc->base.dev;
  6764. struct drm_i915_private *dev_priv = dev->dev_private;
  6765. int pipe = pipe_config->cpu_transcoder;
  6766. intel_clock_t clock;
  6767. u32 mdiv;
  6768. int refclk = 100000;
  6769. /* In case of MIPI DPLL will not even be used */
  6770. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6771. return;
  6772. mutex_lock(&dev_priv->sb_lock);
  6773. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6774. mutex_unlock(&dev_priv->sb_lock);
  6775. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6776. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6777. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6778. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6779. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6780. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6781. }
  6782. static void
  6783. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6784. struct intel_initial_plane_config *plane_config)
  6785. {
  6786. struct drm_device *dev = crtc->base.dev;
  6787. struct drm_i915_private *dev_priv = dev->dev_private;
  6788. u32 val, base, offset;
  6789. int pipe = crtc->pipe, plane = crtc->plane;
  6790. int fourcc, pixel_format;
  6791. unsigned int aligned_height;
  6792. struct drm_framebuffer *fb;
  6793. struct intel_framebuffer *intel_fb;
  6794. val = I915_READ(DSPCNTR(plane));
  6795. if (!(val & DISPLAY_PLANE_ENABLE))
  6796. return;
  6797. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6798. if (!intel_fb) {
  6799. DRM_DEBUG_KMS("failed to alloc fb\n");
  6800. return;
  6801. }
  6802. fb = &intel_fb->base;
  6803. if (INTEL_INFO(dev)->gen >= 4) {
  6804. if (val & DISPPLANE_TILED) {
  6805. plane_config->tiling = I915_TILING_X;
  6806. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6807. }
  6808. }
  6809. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6810. fourcc = i9xx_format_to_fourcc(pixel_format);
  6811. fb->pixel_format = fourcc;
  6812. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6813. if (INTEL_INFO(dev)->gen >= 4) {
  6814. if (plane_config->tiling)
  6815. offset = I915_READ(DSPTILEOFF(plane));
  6816. else
  6817. offset = I915_READ(DSPLINOFF(plane));
  6818. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6819. } else {
  6820. base = I915_READ(DSPADDR(plane));
  6821. }
  6822. plane_config->base = base;
  6823. val = I915_READ(PIPESRC(pipe));
  6824. fb->width = ((val >> 16) & 0xfff) + 1;
  6825. fb->height = ((val >> 0) & 0xfff) + 1;
  6826. val = I915_READ(DSPSTRIDE(pipe));
  6827. fb->pitches[0] = val & 0xffffffc0;
  6828. aligned_height = intel_fb_align_height(dev, fb->height,
  6829. fb->pixel_format,
  6830. fb->modifier[0]);
  6831. plane_config->size = fb->pitches[0] * aligned_height;
  6832. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6833. pipe_name(pipe), plane, fb->width, fb->height,
  6834. fb->bits_per_pixel, base, fb->pitches[0],
  6835. plane_config->size);
  6836. plane_config->fb = intel_fb;
  6837. }
  6838. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6839. struct intel_crtc_state *pipe_config)
  6840. {
  6841. struct drm_device *dev = crtc->base.dev;
  6842. struct drm_i915_private *dev_priv = dev->dev_private;
  6843. int pipe = pipe_config->cpu_transcoder;
  6844. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6845. intel_clock_t clock;
  6846. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6847. int refclk = 100000;
  6848. mutex_lock(&dev_priv->sb_lock);
  6849. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6850. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6851. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6852. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6853. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6854. mutex_unlock(&dev_priv->sb_lock);
  6855. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6856. clock.m2 = (pll_dw0 & 0xff) << 22;
  6857. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6858. clock.m2 |= pll_dw2 & 0x3fffff;
  6859. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6860. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6861. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6862. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6863. }
  6864. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6865. struct intel_crtc_state *pipe_config)
  6866. {
  6867. struct drm_device *dev = crtc->base.dev;
  6868. struct drm_i915_private *dev_priv = dev->dev_private;
  6869. uint32_t tmp;
  6870. if (!intel_display_power_is_enabled(dev_priv,
  6871. POWER_DOMAIN_PIPE(crtc->pipe)))
  6872. return false;
  6873. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6874. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6875. tmp = I915_READ(PIPECONF(crtc->pipe));
  6876. if (!(tmp & PIPECONF_ENABLE))
  6877. return false;
  6878. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6879. switch (tmp & PIPECONF_BPC_MASK) {
  6880. case PIPECONF_6BPC:
  6881. pipe_config->pipe_bpp = 18;
  6882. break;
  6883. case PIPECONF_8BPC:
  6884. pipe_config->pipe_bpp = 24;
  6885. break;
  6886. case PIPECONF_10BPC:
  6887. pipe_config->pipe_bpp = 30;
  6888. break;
  6889. default:
  6890. break;
  6891. }
  6892. }
  6893. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6894. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6895. pipe_config->limited_color_range = true;
  6896. if (INTEL_INFO(dev)->gen < 4)
  6897. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6898. intel_get_pipe_timings(crtc, pipe_config);
  6899. i9xx_get_pfit_config(crtc, pipe_config);
  6900. if (INTEL_INFO(dev)->gen >= 4) {
  6901. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6902. pipe_config->pixel_multiplier =
  6903. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6904. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6905. pipe_config->dpll_hw_state.dpll_md = tmp;
  6906. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6907. tmp = I915_READ(DPLL(crtc->pipe));
  6908. pipe_config->pixel_multiplier =
  6909. ((tmp & SDVO_MULTIPLIER_MASK)
  6910. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6911. } else {
  6912. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6913. * port and will be fixed up in the encoder->get_config
  6914. * function. */
  6915. pipe_config->pixel_multiplier = 1;
  6916. }
  6917. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6918. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  6919. /*
  6920. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6921. * on 830. Filter it out here so that we don't
  6922. * report errors due to that.
  6923. */
  6924. if (IS_I830(dev))
  6925. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6926. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6927. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6928. } else {
  6929. /* Mask out read-only status bits. */
  6930. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6931. DPLL_PORTC_READY_MASK |
  6932. DPLL_PORTB_READY_MASK);
  6933. }
  6934. if (IS_CHERRYVIEW(dev))
  6935. chv_crtc_clock_get(crtc, pipe_config);
  6936. else if (IS_VALLEYVIEW(dev))
  6937. vlv_crtc_clock_get(crtc, pipe_config);
  6938. else
  6939. i9xx_crtc_clock_get(crtc, pipe_config);
  6940. /*
  6941. * Normally the dotclock is filled in by the encoder .get_config()
  6942. * but in case the pipe is enabled w/o any ports we need a sane
  6943. * default.
  6944. */
  6945. pipe_config->base.adjusted_mode.crtc_clock =
  6946. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6947. return true;
  6948. }
  6949. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6950. {
  6951. struct drm_i915_private *dev_priv = dev->dev_private;
  6952. struct intel_encoder *encoder;
  6953. u32 val, final;
  6954. bool has_lvds = false;
  6955. bool has_cpu_edp = false;
  6956. bool has_panel = false;
  6957. bool has_ck505 = false;
  6958. bool can_ssc = false;
  6959. /* We need to take the global config into account */
  6960. for_each_intel_encoder(dev, encoder) {
  6961. switch (encoder->type) {
  6962. case INTEL_OUTPUT_LVDS:
  6963. has_panel = true;
  6964. has_lvds = true;
  6965. break;
  6966. case INTEL_OUTPUT_EDP:
  6967. has_panel = true;
  6968. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6969. has_cpu_edp = true;
  6970. break;
  6971. default:
  6972. break;
  6973. }
  6974. }
  6975. if (HAS_PCH_IBX(dev)) {
  6976. has_ck505 = dev_priv->vbt.display_clock_mode;
  6977. can_ssc = has_ck505;
  6978. } else {
  6979. has_ck505 = false;
  6980. can_ssc = true;
  6981. }
  6982. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6983. has_panel, has_lvds, has_ck505);
  6984. /* Ironlake: try to setup display ref clock before DPLL
  6985. * enabling. This is only under driver's control after
  6986. * PCH B stepping, previous chipset stepping should be
  6987. * ignoring this setting.
  6988. */
  6989. val = I915_READ(PCH_DREF_CONTROL);
  6990. /* As we must carefully and slowly disable/enable each source in turn,
  6991. * compute the final state we want first and check if we need to
  6992. * make any changes at all.
  6993. */
  6994. final = val;
  6995. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6996. if (has_ck505)
  6997. final |= DREF_NONSPREAD_CK505_ENABLE;
  6998. else
  6999. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7000. final &= ~DREF_SSC_SOURCE_MASK;
  7001. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7002. final &= ~DREF_SSC1_ENABLE;
  7003. if (has_panel) {
  7004. final |= DREF_SSC_SOURCE_ENABLE;
  7005. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7006. final |= DREF_SSC1_ENABLE;
  7007. if (has_cpu_edp) {
  7008. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7009. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7010. else
  7011. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7012. } else
  7013. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7014. } else {
  7015. final |= DREF_SSC_SOURCE_DISABLE;
  7016. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7017. }
  7018. if (final == val)
  7019. return;
  7020. /* Always enable nonspread source */
  7021. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7022. if (has_ck505)
  7023. val |= DREF_NONSPREAD_CK505_ENABLE;
  7024. else
  7025. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7026. if (has_panel) {
  7027. val &= ~DREF_SSC_SOURCE_MASK;
  7028. val |= DREF_SSC_SOURCE_ENABLE;
  7029. /* SSC must be turned on before enabling the CPU output */
  7030. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7031. DRM_DEBUG_KMS("Using SSC on panel\n");
  7032. val |= DREF_SSC1_ENABLE;
  7033. } else
  7034. val &= ~DREF_SSC1_ENABLE;
  7035. /* Get SSC going before enabling the outputs */
  7036. I915_WRITE(PCH_DREF_CONTROL, val);
  7037. POSTING_READ(PCH_DREF_CONTROL);
  7038. udelay(200);
  7039. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7040. /* Enable CPU source on CPU attached eDP */
  7041. if (has_cpu_edp) {
  7042. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7043. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7044. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7045. } else
  7046. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7047. } else
  7048. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7049. I915_WRITE(PCH_DREF_CONTROL, val);
  7050. POSTING_READ(PCH_DREF_CONTROL);
  7051. udelay(200);
  7052. } else {
  7053. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  7054. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7055. /* Turn off CPU output */
  7056. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7057. I915_WRITE(PCH_DREF_CONTROL, val);
  7058. POSTING_READ(PCH_DREF_CONTROL);
  7059. udelay(200);
  7060. /* Turn off the SSC source */
  7061. val &= ~DREF_SSC_SOURCE_MASK;
  7062. val |= DREF_SSC_SOURCE_DISABLE;
  7063. /* Turn off SSC1 */
  7064. val &= ~DREF_SSC1_ENABLE;
  7065. I915_WRITE(PCH_DREF_CONTROL, val);
  7066. POSTING_READ(PCH_DREF_CONTROL);
  7067. udelay(200);
  7068. }
  7069. BUG_ON(val != final);
  7070. }
  7071. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7072. {
  7073. uint32_t tmp;
  7074. tmp = I915_READ(SOUTH_CHICKEN2);
  7075. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7076. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7077. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7078. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7079. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7080. tmp = I915_READ(SOUTH_CHICKEN2);
  7081. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7082. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7083. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7084. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7085. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7086. }
  7087. /* WaMPhyProgramming:hsw */
  7088. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7089. {
  7090. uint32_t tmp;
  7091. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7092. tmp &= ~(0xFF << 24);
  7093. tmp |= (0x12 << 24);
  7094. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7095. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7096. tmp |= (1 << 11);
  7097. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7098. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7099. tmp |= (1 << 11);
  7100. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7101. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7102. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7103. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7104. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7105. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7106. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7107. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7108. tmp &= ~(7 << 13);
  7109. tmp |= (5 << 13);
  7110. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7111. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7112. tmp &= ~(7 << 13);
  7113. tmp |= (5 << 13);
  7114. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7115. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7116. tmp &= ~0xFF;
  7117. tmp |= 0x1C;
  7118. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7119. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7120. tmp &= ~0xFF;
  7121. tmp |= 0x1C;
  7122. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7123. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7124. tmp &= ~(0xFF << 16);
  7125. tmp |= (0x1C << 16);
  7126. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7127. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7128. tmp &= ~(0xFF << 16);
  7129. tmp |= (0x1C << 16);
  7130. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7131. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7132. tmp |= (1 << 27);
  7133. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7134. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7135. tmp |= (1 << 27);
  7136. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7137. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7138. tmp &= ~(0xF << 28);
  7139. tmp |= (4 << 28);
  7140. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7141. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7142. tmp &= ~(0xF << 28);
  7143. tmp |= (4 << 28);
  7144. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7145. }
  7146. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7147. * Programming" based on the parameters passed:
  7148. * - Sequence to enable CLKOUT_DP
  7149. * - Sequence to enable CLKOUT_DP without spread
  7150. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7151. */
  7152. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7153. bool with_fdi)
  7154. {
  7155. struct drm_i915_private *dev_priv = dev->dev_private;
  7156. uint32_t reg, tmp;
  7157. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7158. with_spread = true;
  7159. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7160. with_fdi = false;
  7161. mutex_lock(&dev_priv->sb_lock);
  7162. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7163. tmp &= ~SBI_SSCCTL_DISABLE;
  7164. tmp |= SBI_SSCCTL_PATHALT;
  7165. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7166. udelay(24);
  7167. if (with_spread) {
  7168. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7169. tmp &= ~SBI_SSCCTL_PATHALT;
  7170. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7171. if (with_fdi) {
  7172. lpt_reset_fdi_mphy(dev_priv);
  7173. lpt_program_fdi_mphy(dev_priv);
  7174. }
  7175. }
  7176. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7177. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7178. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7179. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7180. mutex_unlock(&dev_priv->sb_lock);
  7181. }
  7182. /* Sequence to disable CLKOUT_DP */
  7183. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7184. {
  7185. struct drm_i915_private *dev_priv = dev->dev_private;
  7186. uint32_t reg, tmp;
  7187. mutex_lock(&dev_priv->sb_lock);
  7188. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7189. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7190. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7191. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7192. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7193. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7194. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7195. tmp |= SBI_SSCCTL_PATHALT;
  7196. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7197. udelay(32);
  7198. }
  7199. tmp |= SBI_SSCCTL_DISABLE;
  7200. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7201. }
  7202. mutex_unlock(&dev_priv->sb_lock);
  7203. }
  7204. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7205. static const uint16_t sscdivintphase[] = {
  7206. [BEND_IDX( 50)] = 0x3B23,
  7207. [BEND_IDX( 45)] = 0x3B23,
  7208. [BEND_IDX( 40)] = 0x3C23,
  7209. [BEND_IDX( 35)] = 0x3C23,
  7210. [BEND_IDX( 30)] = 0x3D23,
  7211. [BEND_IDX( 25)] = 0x3D23,
  7212. [BEND_IDX( 20)] = 0x3E23,
  7213. [BEND_IDX( 15)] = 0x3E23,
  7214. [BEND_IDX( 10)] = 0x3F23,
  7215. [BEND_IDX( 5)] = 0x3F23,
  7216. [BEND_IDX( 0)] = 0x0025,
  7217. [BEND_IDX( -5)] = 0x0025,
  7218. [BEND_IDX(-10)] = 0x0125,
  7219. [BEND_IDX(-15)] = 0x0125,
  7220. [BEND_IDX(-20)] = 0x0225,
  7221. [BEND_IDX(-25)] = 0x0225,
  7222. [BEND_IDX(-30)] = 0x0325,
  7223. [BEND_IDX(-35)] = 0x0325,
  7224. [BEND_IDX(-40)] = 0x0425,
  7225. [BEND_IDX(-45)] = 0x0425,
  7226. [BEND_IDX(-50)] = 0x0525,
  7227. };
  7228. /*
  7229. * Bend CLKOUT_DP
  7230. * steps -50 to 50 inclusive, in steps of 5
  7231. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7232. * change in clock period = -(steps / 10) * 5.787 ps
  7233. */
  7234. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7235. {
  7236. uint32_t tmp;
  7237. int idx = BEND_IDX(steps);
  7238. if (WARN_ON(steps % 5 != 0))
  7239. return;
  7240. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7241. return;
  7242. mutex_lock(&dev_priv->sb_lock);
  7243. if (steps % 10 != 0)
  7244. tmp = 0xAAAAAAAB;
  7245. else
  7246. tmp = 0x00000000;
  7247. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7248. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7249. tmp &= 0xffff0000;
  7250. tmp |= sscdivintphase[idx];
  7251. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7252. mutex_unlock(&dev_priv->sb_lock);
  7253. }
  7254. #undef BEND_IDX
  7255. static void lpt_init_pch_refclk(struct drm_device *dev)
  7256. {
  7257. struct intel_encoder *encoder;
  7258. bool has_vga = false;
  7259. for_each_intel_encoder(dev, encoder) {
  7260. switch (encoder->type) {
  7261. case INTEL_OUTPUT_ANALOG:
  7262. has_vga = true;
  7263. break;
  7264. default:
  7265. break;
  7266. }
  7267. }
  7268. if (has_vga) {
  7269. lpt_bend_clkout_dp(to_i915(dev), 0);
  7270. lpt_enable_clkout_dp(dev, true, true);
  7271. } else {
  7272. lpt_disable_clkout_dp(dev);
  7273. }
  7274. }
  7275. /*
  7276. * Initialize reference clocks when the driver loads
  7277. */
  7278. void intel_init_pch_refclk(struct drm_device *dev)
  7279. {
  7280. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7281. ironlake_init_pch_refclk(dev);
  7282. else if (HAS_PCH_LPT(dev))
  7283. lpt_init_pch_refclk(dev);
  7284. }
  7285. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7286. {
  7287. struct drm_device *dev = crtc_state->base.crtc->dev;
  7288. struct drm_i915_private *dev_priv = dev->dev_private;
  7289. struct drm_atomic_state *state = crtc_state->base.state;
  7290. struct drm_connector *connector;
  7291. struct drm_connector_state *connector_state;
  7292. struct intel_encoder *encoder;
  7293. int num_connectors = 0, i;
  7294. bool is_lvds = false;
  7295. for_each_connector_in_state(state, connector, connector_state, i) {
  7296. if (connector_state->crtc != crtc_state->base.crtc)
  7297. continue;
  7298. encoder = to_intel_encoder(connector_state->best_encoder);
  7299. switch (encoder->type) {
  7300. case INTEL_OUTPUT_LVDS:
  7301. is_lvds = true;
  7302. break;
  7303. default:
  7304. break;
  7305. }
  7306. num_connectors++;
  7307. }
  7308. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7309. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7310. dev_priv->vbt.lvds_ssc_freq);
  7311. return dev_priv->vbt.lvds_ssc_freq;
  7312. }
  7313. return 120000;
  7314. }
  7315. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7316. {
  7317. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7318. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7319. int pipe = intel_crtc->pipe;
  7320. uint32_t val;
  7321. val = 0;
  7322. switch (intel_crtc->config->pipe_bpp) {
  7323. case 18:
  7324. val |= PIPECONF_6BPC;
  7325. break;
  7326. case 24:
  7327. val |= PIPECONF_8BPC;
  7328. break;
  7329. case 30:
  7330. val |= PIPECONF_10BPC;
  7331. break;
  7332. case 36:
  7333. val |= PIPECONF_12BPC;
  7334. break;
  7335. default:
  7336. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7337. BUG();
  7338. }
  7339. if (intel_crtc->config->dither)
  7340. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7341. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7342. val |= PIPECONF_INTERLACED_ILK;
  7343. else
  7344. val |= PIPECONF_PROGRESSIVE;
  7345. if (intel_crtc->config->limited_color_range)
  7346. val |= PIPECONF_COLOR_RANGE_SELECT;
  7347. I915_WRITE(PIPECONF(pipe), val);
  7348. POSTING_READ(PIPECONF(pipe));
  7349. }
  7350. /*
  7351. * Set up the pipe CSC unit.
  7352. *
  7353. * Currently only full range RGB to limited range RGB conversion
  7354. * is supported, but eventually this should handle various
  7355. * RGB<->YCbCr scenarios as well.
  7356. */
  7357. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7358. {
  7359. struct drm_device *dev = crtc->dev;
  7360. struct drm_i915_private *dev_priv = dev->dev_private;
  7361. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7362. int pipe = intel_crtc->pipe;
  7363. uint16_t coeff = 0x7800; /* 1.0 */
  7364. /*
  7365. * TODO: Check what kind of values actually come out of the pipe
  7366. * with these coeff/postoff values and adjust to get the best
  7367. * accuracy. Perhaps we even need to take the bpc value into
  7368. * consideration.
  7369. */
  7370. if (intel_crtc->config->limited_color_range)
  7371. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7372. /*
  7373. * GY/GU and RY/RU should be the other way around according
  7374. * to BSpec, but reality doesn't agree. Just set them up in
  7375. * a way that results in the correct picture.
  7376. */
  7377. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7378. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7379. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7380. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7381. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7382. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7383. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7384. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7385. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7386. if (INTEL_INFO(dev)->gen > 6) {
  7387. uint16_t postoff = 0;
  7388. if (intel_crtc->config->limited_color_range)
  7389. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7390. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7391. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7392. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7393. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7394. } else {
  7395. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7396. if (intel_crtc->config->limited_color_range)
  7397. mode |= CSC_BLACK_SCREEN_OFFSET;
  7398. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7399. }
  7400. }
  7401. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7402. {
  7403. struct drm_device *dev = crtc->dev;
  7404. struct drm_i915_private *dev_priv = dev->dev_private;
  7405. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7406. enum pipe pipe = intel_crtc->pipe;
  7407. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7408. uint32_t val;
  7409. val = 0;
  7410. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7411. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7412. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7413. val |= PIPECONF_INTERLACED_ILK;
  7414. else
  7415. val |= PIPECONF_PROGRESSIVE;
  7416. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7417. POSTING_READ(PIPECONF(cpu_transcoder));
  7418. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7419. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7420. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7421. val = 0;
  7422. switch (intel_crtc->config->pipe_bpp) {
  7423. case 18:
  7424. val |= PIPEMISC_DITHER_6_BPC;
  7425. break;
  7426. case 24:
  7427. val |= PIPEMISC_DITHER_8_BPC;
  7428. break;
  7429. case 30:
  7430. val |= PIPEMISC_DITHER_10_BPC;
  7431. break;
  7432. case 36:
  7433. val |= PIPEMISC_DITHER_12_BPC;
  7434. break;
  7435. default:
  7436. /* Case prevented by pipe_config_set_bpp. */
  7437. BUG();
  7438. }
  7439. if (intel_crtc->config->dither)
  7440. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7441. I915_WRITE(PIPEMISC(pipe), val);
  7442. }
  7443. }
  7444. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7445. struct intel_crtc_state *crtc_state,
  7446. intel_clock_t *clock,
  7447. bool *has_reduced_clock,
  7448. intel_clock_t *reduced_clock)
  7449. {
  7450. struct drm_device *dev = crtc->dev;
  7451. struct drm_i915_private *dev_priv = dev->dev_private;
  7452. int refclk;
  7453. const intel_limit_t *limit;
  7454. bool ret;
  7455. refclk = ironlake_get_refclk(crtc_state);
  7456. /*
  7457. * Returns a set of divisors for the desired target clock with the given
  7458. * refclk, or FALSE. The returned values represent the clock equation:
  7459. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7460. */
  7461. limit = intel_limit(crtc_state, refclk);
  7462. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7463. crtc_state->port_clock,
  7464. refclk, NULL, clock);
  7465. if (!ret)
  7466. return false;
  7467. return true;
  7468. }
  7469. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7470. {
  7471. /*
  7472. * Account for spread spectrum to avoid
  7473. * oversubscribing the link. Max center spread
  7474. * is 2.5%; use 5% for safety's sake.
  7475. */
  7476. u32 bps = target_clock * bpp * 21 / 20;
  7477. return DIV_ROUND_UP(bps, link_bw * 8);
  7478. }
  7479. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7480. {
  7481. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7482. }
  7483. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7484. struct intel_crtc_state *crtc_state,
  7485. u32 *fp,
  7486. intel_clock_t *reduced_clock, u32 *fp2)
  7487. {
  7488. struct drm_crtc *crtc = &intel_crtc->base;
  7489. struct drm_device *dev = crtc->dev;
  7490. struct drm_i915_private *dev_priv = dev->dev_private;
  7491. struct drm_atomic_state *state = crtc_state->base.state;
  7492. struct drm_connector *connector;
  7493. struct drm_connector_state *connector_state;
  7494. struct intel_encoder *encoder;
  7495. uint32_t dpll;
  7496. int factor, num_connectors = 0, i;
  7497. bool is_lvds = false, is_sdvo = false;
  7498. for_each_connector_in_state(state, connector, connector_state, i) {
  7499. if (connector_state->crtc != crtc_state->base.crtc)
  7500. continue;
  7501. encoder = to_intel_encoder(connector_state->best_encoder);
  7502. switch (encoder->type) {
  7503. case INTEL_OUTPUT_LVDS:
  7504. is_lvds = true;
  7505. break;
  7506. case INTEL_OUTPUT_SDVO:
  7507. case INTEL_OUTPUT_HDMI:
  7508. is_sdvo = true;
  7509. break;
  7510. default:
  7511. break;
  7512. }
  7513. num_connectors++;
  7514. }
  7515. /* Enable autotuning of the PLL clock (if permissible) */
  7516. factor = 21;
  7517. if (is_lvds) {
  7518. if ((intel_panel_use_ssc(dev_priv) &&
  7519. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7520. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7521. factor = 25;
  7522. } else if (crtc_state->sdvo_tv_clock)
  7523. factor = 20;
  7524. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7525. *fp |= FP_CB_TUNE;
  7526. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7527. *fp2 |= FP_CB_TUNE;
  7528. dpll = 0;
  7529. if (is_lvds)
  7530. dpll |= DPLLB_MODE_LVDS;
  7531. else
  7532. dpll |= DPLLB_MODE_DAC_SERIAL;
  7533. dpll |= (crtc_state->pixel_multiplier - 1)
  7534. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7535. if (is_sdvo)
  7536. dpll |= DPLL_SDVO_HIGH_SPEED;
  7537. if (crtc_state->has_dp_encoder)
  7538. dpll |= DPLL_SDVO_HIGH_SPEED;
  7539. /* compute bitmask from p1 value */
  7540. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7541. /* also FPA1 */
  7542. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7543. switch (crtc_state->dpll.p2) {
  7544. case 5:
  7545. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7546. break;
  7547. case 7:
  7548. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7549. break;
  7550. case 10:
  7551. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7552. break;
  7553. case 14:
  7554. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7555. break;
  7556. }
  7557. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7558. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7559. else
  7560. dpll |= PLL_REF_INPUT_DREFCLK;
  7561. return dpll | DPLL_VCO_ENABLE;
  7562. }
  7563. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7564. struct intel_crtc_state *crtc_state)
  7565. {
  7566. struct drm_device *dev = crtc->base.dev;
  7567. intel_clock_t clock, reduced_clock;
  7568. u32 dpll = 0, fp = 0, fp2 = 0;
  7569. bool ok, has_reduced_clock = false;
  7570. bool is_lvds = false;
  7571. struct intel_shared_dpll *pll;
  7572. memset(&crtc_state->dpll_hw_state, 0,
  7573. sizeof(crtc_state->dpll_hw_state));
  7574. is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
  7575. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7576. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7577. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7578. &has_reduced_clock, &reduced_clock);
  7579. if (!ok && !crtc_state->clock_set) {
  7580. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7581. return -EINVAL;
  7582. }
  7583. /* Compat-code for transition, will disappear. */
  7584. if (!crtc_state->clock_set) {
  7585. crtc_state->dpll.n = clock.n;
  7586. crtc_state->dpll.m1 = clock.m1;
  7587. crtc_state->dpll.m2 = clock.m2;
  7588. crtc_state->dpll.p1 = clock.p1;
  7589. crtc_state->dpll.p2 = clock.p2;
  7590. }
  7591. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7592. if (crtc_state->has_pch_encoder) {
  7593. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7594. if (has_reduced_clock)
  7595. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7596. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7597. &fp, &reduced_clock,
  7598. has_reduced_clock ? &fp2 : NULL);
  7599. crtc_state->dpll_hw_state.dpll = dpll;
  7600. crtc_state->dpll_hw_state.fp0 = fp;
  7601. if (has_reduced_clock)
  7602. crtc_state->dpll_hw_state.fp1 = fp2;
  7603. else
  7604. crtc_state->dpll_hw_state.fp1 = fp;
  7605. pll = intel_get_shared_dpll(crtc, crtc_state);
  7606. if (pll == NULL) {
  7607. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7608. pipe_name(crtc->pipe));
  7609. return -EINVAL;
  7610. }
  7611. }
  7612. if (is_lvds && has_reduced_clock)
  7613. crtc->lowfreq_avail = true;
  7614. else
  7615. crtc->lowfreq_avail = false;
  7616. return 0;
  7617. }
  7618. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7619. struct intel_link_m_n *m_n)
  7620. {
  7621. struct drm_device *dev = crtc->base.dev;
  7622. struct drm_i915_private *dev_priv = dev->dev_private;
  7623. enum pipe pipe = crtc->pipe;
  7624. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7625. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7626. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7627. & ~TU_SIZE_MASK;
  7628. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7629. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7630. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7631. }
  7632. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7633. enum transcoder transcoder,
  7634. struct intel_link_m_n *m_n,
  7635. struct intel_link_m_n *m2_n2)
  7636. {
  7637. struct drm_device *dev = crtc->base.dev;
  7638. struct drm_i915_private *dev_priv = dev->dev_private;
  7639. enum pipe pipe = crtc->pipe;
  7640. if (INTEL_INFO(dev)->gen >= 5) {
  7641. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7642. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7643. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7644. & ~TU_SIZE_MASK;
  7645. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7646. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7647. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7648. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7649. * gen < 8) and if DRRS is supported (to make sure the
  7650. * registers are not unnecessarily read).
  7651. */
  7652. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7653. crtc->config->has_drrs) {
  7654. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7655. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7656. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7657. & ~TU_SIZE_MASK;
  7658. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7659. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7660. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7661. }
  7662. } else {
  7663. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7664. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7665. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7666. & ~TU_SIZE_MASK;
  7667. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7668. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7669. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7670. }
  7671. }
  7672. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7673. struct intel_crtc_state *pipe_config)
  7674. {
  7675. if (pipe_config->has_pch_encoder)
  7676. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7677. else
  7678. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7679. &pipe_config->dp_m_n,
  7680. &pipe_config->dp_m2_n2);
  7681. }
  7682. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7683. struct intel_crtc_state *pipe_config)
  7684. {
  7685. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7686. &pipe_config->fdi_m_n, NULL);
  7687. }
  7688. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7689. struct intel_crtc_state *pipe_config)
  7690. {
  7691. struct drm_device *dev = crtc->base.dev;
  7692. struct drm_i915_private *dev_priv = dev->dev_private;
  7693. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7694. uint32_t ps_ctrl = 0;
  7695. int id = -1;
  7696. int i;
  7697. /* find scaler attached to this pipe */
  7698. for (i = 0; i < crtc->num_scalers; i++) {
  7699. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7700. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7701. id = i;
  7702. pipe_config->pch_pfit.enabled = true;
  7703. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7704. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7705. break;
  7706. }
  7707. }
  7708. scaler_state->scaler_id = id;
  7709. if (id >= 0) {
  7710. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7711. } else {
  7712. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7713. }
  7714. }
  7715. static void
  7716. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7717. struct intel_initial_plane_config *plane_config)
  7718. {
  7719. struct drm_device *dev = crtc->base.dev;
  7720. struct drm_i915_private *dev_priv = dev->dev_private;
  7721. u32 val, base, offset, stride_mult, tiling;
  7722. int pipe = crtc->pipe;
  7723. int fourcc, pixel_format;
  7724. unsigned int aligned_height;
  7725. struct drm_framebuffer *fb;
  7726. struct intel_framebuffer *intel_fb;
  7727. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7728. if (!intel_fb) {
  7729. DRM_DEBUG_KMS("failed to alloc fb\n");
  7730. return;
  7731. }
  7732. fb = &intel_fb->base;
  7733. val = I915_READ(PLANE_CTL(pipe, 0));
  7734. if (!(val & PLANE_CTL_ENABLE))
  7735. goto error;
  7736. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7737. fourcc = skl_format_to_fourcc(pixel_format,
  7738. val & PLANE_CTL_ORDER_RGBX,
  7739. val & PLANE_CTL_ALPHA_MASK);
  7740. fb->pixel_format = fourcc;
  7741. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7742. tiling = val & PLANE_CTL_TILED_MASK;
  7743. switch (tiling) {
  7744. case PLANE_CTL_TILED_LINEAR:
  7745. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7746. break;
  7747. case PLANE_CTL_TILED_X:
  7748. plane_config->tiling = I915_TILING_X;
  7749. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7750. break;
  7751. case PLANE_CTL_TILED_Y:
  7752. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7753. break;
  7754. case PLANE_CTL_TILED_YF:
  7755. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7756. break;
  7757. default:
  7758. MISSING_CASE(tiling);
  7759. goto error;
  7760. }
  7761. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7762. plane_config->base = base;
  7763. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7764. val = I915_READ(PLANE_SIZE(pipe, 0));
  7765. fb->height = ((val >> 16) & 0xfff) + 1;
  7766. fb->width = ((val >> 0) & 0x1fff) + 1;
  7767. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7768. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  7769. fb->pixel_format);
  7770. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7771. aligned_height = intel_fb_align_height(dev, fb->height,
  7772. fb->pixel_format,
  7773. fb->modifier[0]);
  7774. plane_config->size = fb->pitches[0] * aligned_height;
  7775. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7776. pipe_name(pipe), fb->width, fb->height,
  7777. fb->bits_per_pixel, base, fb->pitches[0],
  7778. plane_config->size);
  7779. plane_config->fb = intel_fb;
  7780. return;
  7781. error:
  7782. kfree(fb);
  7783. }
  7784. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7785. struct intel_crtc_state *pipe_config)
  7786. {
  7787. struct drm_device *dev = crtc->base.dev;
  7788. struct drm_i915_private *dev_priv = dev->dev_private;
  7789. uint32_t tmp;
  7790. tmp = I915_READ(PF_CTL(crtc->pipe));
  7791. if (tmp & PF_ENABLE) {
  7792. pipe_config->pch_pfit.enabled = true;
  7793. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7794. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7795. /* We currently do not free assignements of panel fitters on
  7796. * ivb/hsw (since we don't use the higher upscaling modes which
  7797. * differentiates them) so just WARN about this case for now. */
  7798. if (IS_GEN7(dev)) {
  7799. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7800. PF_PIPE_SEL_IVB(crtc->pipe));
  7801. }
  7802. }
  7803. }
  7804. static void
  7805. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7806. struct intel_initial_plane_config *plane_config)
  7807. {
  7808. struct drm_device *dev = crtc->base.dev;
  7809. struct drm_i915_private *dev_priv = dev->dev_private;
  7810. u32 val, base, offset;
  7811. int pipe = crtc->pipe;
  7812. int fourcc, pixel_format;
  7813. unsigned int aligned_height;
  7814. struct drm_framebuffer *fb;
  7815. struct intel_framebuffer *intel_fb;
  7816. val = I915_READ(DSPCNTR(pipe));
  7817. if (!(val & DISPLAY_PLANE_ENABLE))
  7818. return;
  7819. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7820. if (!intel_fb) {
  7821. DRM_DEBUG_KMS("failed to alloc fb\n");
  7822. return;
  7823. }
  7824. fb = &intel_fb->base;
  7825. if (INTEL_INFO(dev)->gen >= 4) {
  7826. if (val & DISPPLANE_TILED) {
  7827. plane_config->tiling = I915_TILING_X;
  7828. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7829. }
  7830. }
  7831. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7832. fourcc = i9xx_format_to_fourcc(pixel_format);
  7833. fb->pixel_format = fourcc;
  7834. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7835. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7836. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7837. offset = I915_READ(DSPOFFSET(pipe));
  7838. } else {
  7839. if (plane_config->tiling)
  7840. offset = I915_READ(DSPTILEOFF(pipe));
  7841. else
  7842. offset = I915_READ(DSPLINOFF(pipe));
  7843. }
  7844. plane_config->base = base;
  7845. val = I915_READ(PIPESRC(pipe));
  7846. fb->width = ((val >> 16) & 0xfff) + 1;
  7847. fb->height = ((val >> 0) & 0xfff) + 1;
  7848. val = I915_READ(DSPSTRIDE(pipe));
  7849. fb->pitches[0] = val & 0xffffffc0;
  7850. aligned_height = intel_fb_align_height(dev, fb->height,
  7851. fb->pixel_format,
  7852. fb->modifier[0]);
  7853. plane_config->size = fb->pitches[0] * aligned_height;
  7854. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7855. pipe_name(pipe), fb->width, fb->height,
  7856. fb->bits_per_pixel, base, fb->pitches[0],
  7857. plane_config->size);
  7858. plane_config->fb = intel_fb;
  7859. }
  7860. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7861. struct intel_crtc_state *pipe_config)
  7862. {
  7863. struct drm_device *dev = crtc->base.dev;
  7864. struct drm_i915_private *dev_priv = dev->dev_private;
  7865. uint32_t tmp;
  7866. if (!intel_display_power_is_enabled(dev_priv,
  7867. POWER_DOMAIN_PIPE(crtc->pipe)))
  7868. return false;
  7869. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7870. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7871. tmp = I915_READ(PIPECONF(crtc->pipe));
  7872. if (!(tmp & PIPECONF_ENABLE))
  7873. return false;
  7874. switch (tmp & PIPECONF_BPC_MASK) {
  7875. case PIPECONF_6BPC:
  7876. pipe_config->pipe_bpp = 18;
  7877. break;
  7878. case PIPECONF_8BPC:
  7879. pipe_config->pipe_bpp = 24;
  7880. break;
  7881. case PIPECONF_10BPC:
  7882. pipe_config->pipe_bpp = 30;
  7883. break;
  7884. case PIPECONF_12BPC:
  7885. pipe_config->pipe_bpp = 36;
  7886. break;
  7887. default:
  7888. break;
  7889. }
  7890. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7891. pipe_config->limited_color_range = true;
  7892. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7893. struct intel_shared_dpll *pll;
  7894. pipe_config->has_pch_encoder = true;
  7895. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7896. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7897. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7898. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7899. if (HAS_PCH_IBX(dev_priv->dev)) {
  7900. pipe_config->shared_dpll =
  7901. (enum intel_dpll_id) crtc->pipe;
  7902. } else {
  7903. tmp = I915_READ(PCH_DPLL_SEL);
  7904. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7905. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7906. else
  7907. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7908. }
  7909. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7910. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7911. &pipe_config->dpll_hw_state));
  7912. tmp = pipe_config->dpll_hw_state.dpll;
  7913. pipe_config->pixel_multiplier =
  7914. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7915. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7916. ironlake_pch_clock_get(crtc, pipe_config);
  7917. } else {
  7918. pipe_config->pixel_multiplier = 1;
  7919. }
  7920. intel_get_pipe_timings(crtc, pipe_config);
  7921. ironlake_get_pfit_config(crtc, pipe_config);
  7922. return true;
  7923. }
  7924. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7925. {
  7926. struct drm_device *dev = dev_priv->dev;
  7927. struct intel_crtc *crtc;
  7928. for_each_intel_crtc(dev, crtc)
  7929. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7930. pipe_name(crtc->pipe));
  7931. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7932. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7933. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7934. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7935. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7936. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7937. "CPU PWM1 enabled\n");
  7938. if (IS_HASWELL(dev))
  7939. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7940. "CPU PWM2 enabled\n");
  7941. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7942. "PCH PWM1 enabled\n");
  7943. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7944. "Utility pin enabled\n");
  7945. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7946. /*
  7947. * In theory we can still leave IRQs enabled, as long as only the HPD
  7948. * interrupts remain enabled. We used to check for that, but since it's
  7949. * gen-specific and since we only disable LCPLL after we fully disable
  7950. * the interrupts, the check below should be enough.
  7951. */
  7952. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7953. }
  7954. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7955. {
  7956. struct drm_device *dev = dev_priv->dev;
  7957. if (IS_HASWELL(dev))
  7958. return I915_READ(D_COMP_HSW);
  7959. else
  7960. return I915_READ(D_COMP_BDW);
  7961. }
  7962. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7963. {
  7964. struct drm_device *dev = dev_priv->dev;
  7965. if (IS_HASWELL(dev)) {
  7966. mutex_lock(&dev_priv->rps.hw_lock);
  7967. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7968. val))
  7969. DRM_ERROR("Failed to write to D_COMP\n");
  7970. mutex_unlock(&dev_priv->rps.hw_lock);
  7971. } else {
  7972. I915_WRITE(D_COMP_BDW, val);
  7973. POSTING_READ(D_COMP_BDW);
  7974. }
  7975. }
  7976. /*
  7977. * This function implements pieces of two sequences from BSpec:
  7978. * - Sequence for display software to disable LCPLL
  7979. * - Sequence for display software to allow package C8+
  7980. * The steps implemented here are just the steps that actually touch the LCPLL
  7981. * register. Callers should take care of disabling all the display engine
  7982. * functions, doing the mode unset, fixing interrupts, etc.
  7983. */
  7984. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7985. bool switch_to_fclk, bool allow_power_down)
  7986. {
  7987. uint32_t val;
  7988. assert_can_disable_lcpll(dev_priv);
  7989. val = I915_READ(LCPLL_CTL);
  7990. if (switch_to_fclk) {
  7991. val |= LCPLL_CD_SOURCE_FCLK;
  7992. I915_WRITE(LCPLL_CTL, val);
  7993. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7994. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7995. DRM_ERROR("Switching to FCLK failed\n");
  7996. val = I915_READ(LCPLL_CTL);
  7997. }
  7998. val |= LCPLL_PLL_DISABLE;
  7999. I915_WRITE(LCPLL_CTL, val);
  8000. POSTING_READ(LCPLL_CTL);
  8001. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  8002. DRM_ERROR("LCPLL still locked\n");
  8003. val = hsw_read_dcomp(dev_priv);
  8004. val |= D_COMP_COMP_DISABLE;
  8005. hsw_write_dcomp(dev_priv, val);
  8006. ndelay(100);
  8007. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  8008. 1))
  8009. DRM_ERROR("D_COMP RCOMP still in progress\n");
  8010. if (allow_power_down) {
  8011. val = I915_READ(LCPLL_CTL);
  8012. val |= LCPLL_POWER_DOWN_ALLOW;
  8013. I915_WRITE(LCPLL_CTL, val);
  8014. POSTING_READ(LCPLL_CTL);
  8015. }
  8016. }
  8017. /*
  8018. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  8019. * source.
  8020. */
  8021. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8022. {
  8023. uint32_t val;
  8024. val = I915_READ(LCPLL_CTL);
  8025. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8026. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8027. return;
  8028. /*
  8029. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8030. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8031. */
  8032. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8033. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8034. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8035. I915_WRITE(LCPLL_CTL, val);
  8036. POSTING_READ(LCPLL_CTL);
  8037. }
  8038. val = hsw_read_dcomp(dev_priv);
  8039. val |= D_COMP_COMP_FORCE;
  8040. val &= ~D_COMP_COMP_DISABLE;
  8041. hsw_write_dcomp(dev_priv, val);
  8042. val = I915_READ(LCPLL_CTL);
  8043. val &= ~LCPLL_PLL_DISABLE;
  8044. I915_WRITE(LCPLL_CTL, val);
  8045. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  8046. DRM_ERROR("LCPLL not locked yet\n");
  8047. if (val & LCPLL_CD_SOURCE_FCLK) {
  8048. val = I915_READ(LCPLL_CTL);
  8049. val &= ~LCPLL_CD_SOURCE_FCLK;
  8050. I915_WRITE(LCPLL_CTL, val);
  8051. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8052. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8053. DRM_ERROR("Switching back to LCPLL failed\n");
  8054. }
  8055. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8056. intel_update_cdclk(dev_priv->dev);
  8057. }
  8058. /*
  8059. * Package states C8 and deeper are really deep PC states that can only be
  8060. * reached when all the devices on the system allow it, so even if the graphics
  8061. * device allows PC8+, it doesn't mean the system will actually get to these
  8062. * states. Our driver only allows PC8+ when going into runtime PM.
  8063. *
  8064. * The requirements for PC8+ are that all the outputs are disabled, the power
  8065. * well is disabled and most interrupts are disabled, and these are also
  8066. * requirements for runtime PM. When these conditions are met, we manually do
  8067. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8068. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8069. * hang the machine.
  8070. *
  8071. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8072. * the state of some registers, so when we come back from PC8+ we need to
  8073. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8074. * need to take care of the registers kept by RC6. Notice that this happens even
  8075. * if we don't put the device in PCI D3 state (which is what currently happens
  8076. * because of the runtime PM support).
  8077. *
  8078. * For more, read "Display Sequences for Package C8" on the hardware
  8079. * documentation.
  8080. */
  8081. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8082. {
  8083. struct drm_device *dev = dev_priv->dev;
  8084. uint32_t val;
  8085. DRM_DEBUG_KMS("Enabling package C8+\n");
  8086. if (HAS_PCH_LPT_LP(dev)) {
  8087. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8088. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8089. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8090. }
  8091. lpt_disable_clkout_dp(dev);
  8092. hsw_disable_lcpll(dev_priv, true, true);
  8093. }
  8094. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8095. {
  8096. struct drm_device *dev = dev_priv->dev;
  8097. uint32_t val;
  8098. DRM_DEBUG_KMS("Disabling package C8+\n");
  8099. hsw_restore_lcpll(dev_priv);
  8100. lpt_init_pch_refclk(dev);
  8101. if (HAS_PCH_LPT_LP(dev)) {
  8102. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8103. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8104. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8105. }
  8106. }
  8107. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8108. {
  8109. struct drm_device *dev = old_state->dev;
  8110. struct intel_atomic_state *old_intel_state =
  8111. to_intel_atomic_state(old_state);
  8112. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8113. broxton_set_cdclk(dev, req_cdclk);
  8114. }
  8115. /* compute the max rate for new configuration */
  8116. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8117. {
  8118. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8119. struct drm_i915_private *dev_priv = state->dev->dev_private;
  8120. struct drm_crtc *crtc;
  8121. struct drm_crtc_state *cstate;
  8122. struct intel_crtc_state *crtc_state;
  8123. unsigned max_pixel_rate = 0, i;
  8124. enum pipe pipe;
  8125. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8126. sizeof(intel_state->min_pixclk));
  8127. for_each_crtc_in_state(state, crtc, cstate, i) {
  8128. int pixel_rate;
  8129. crtc_state = to_intel_crtc_state(cstate);
  8130. if (!crtc_state->base.enable) {
  8131. intel_state->min_pixclk[i] = 0;
  8132. continue;
  8133. }
  8134. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8135. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8136. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8137. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8138. intel_state->min_pixclk[i] = pixel_rate;
  8139. }
  8140. if (!intel_state->active_crtcs)
  8141. return 0;
  8142. for_each_pipe(dev_priv, pipe)
  8143. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8144. return max_pixel_rate;
  8145. }
  8146. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8147. {
  8148. struct drm_i915_private *dev_priv = dev->dev_private;
  8149. uint32_t val, data;
  8150. int ret;
  8151. if (WARN((I915_READ(LCPLL_CTL) &
  8152. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8153. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8154. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8155. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8156. "trying to change cdclk frequency with cdclk not enabled\n"))
  8157. return;
  8158. mutex_lock(&dev_priv->rps.hw_lock);
  8159. ret = sandybridge_pcode_write(dev_priv,
  8160. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8161. mutex_unlock(&dev_priv->rps.hw_lock);
  8162. if (ret) {
  8163. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8164. return;
  8165. }
  8166. val = I915_READ(LCPLL_CTL);
  8167. val |= LCPLL_CD_SOURCE_FCLK;
  8168. I915_WRITE(LCPLL_CTL, val);
  8169. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  8170. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8171. DRM_ERROR("Switching to FCLK failed\n");
  8172. val = I915_READ(LCPLL_CTL);
  8173. val &= ~LCPLL_CLK_FREQ_MASK;
  8174. switch (cdclk) {
  8175. case 450000:
  8176. val |= LCPLL_CLK_FREQ_450;
  8177. data = 0;
  8178. break;
  8179. case 540000:
  8180. val |= LCPLL_CLK_FREQ_54O_BDW;
  8181. data = 1;
  8182. break;
  8183. case 337500:
  8184. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8185. data = 2;
  8186. break;
  8187. case 675000:
  8188. val |= LCPLL_CLK_FREQ_675_BDW;
  8189. data = 3;
  8190. break;
  8191. default:
  8192. WARN(1, "invalid cdclk frequency\n");
  8193. return;
  8194. }
  8195. I915_WRITE(LCPLL_CTL, val);
  8196. val = I915_READ(LCPLL_CTL);
  8197. val &= ~LCPLL_CD_SOURCE_FCLK;
  8198. I915_WRITE(LCPLL_CTL, val);
  8199. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8200. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8201. DRM_ERROR("Switching back to LCPLL failed\n");
  8202. mutex_lock(&dev_priv->rps.hw_lock);
  8203. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8204. mutex_unlock(&dev_priv->rps.hw_lock);
  8205. intel_update_cdclk(dev);
  8206. WARN(cdclk != dev_priv->cdclk_freq,
  8207. "cdclk requested %d kHz but got %d kHz\n",
  8208. cdclk, dev_priv->cdclk_freq);
  8209. }
  8210. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8211. {
  8212. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8213. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8214. int max_pixclk = ilk_max_pixel_rate(state);
  8215. int cdclk;
  8216. /*
  8217. * FIXME should also account for plane ratio
  8218. * once 64bpp pixel formats are supported.
  8219. */
  8220. if (max_pixclk > 540000)
  8221. cdclk = 675000;
  8222. else if (max_pixclk > 450000)
  8223. cdclk = 540000;
  8224. else if (max_pixclk > 337500)
  8225. cdclk = 450000;
  8226. else
  8227. cdclk = 337500;
  8228. if (cdclk > dev_priv->max_cdclk_freq) {
  8229. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8230. cdclk, dev_priv->max_cdclk_freq);
  8231. return -EINVAL;
  8232. }
  8233. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8234. if (!intel_state->active_crtcs)
  8235. intel_state->dev_cdclk = 337500;
  8236. return 0;
  8237. }
  8238. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8239. {
  8240. struct drm_device *dev = old_state->dev;
  8241. struct intel_atomic_state *old_intel_state =
  8242. to_intel_atomic_state(old_state);
  8243. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8244. broadwell_set_cdclk(dev, req_cdclk);
  8245. }
  8246. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8247. struct intel_crtc_state *crtc_state)
  8248. {
  8249. struct intel_encoder *intel_encoder =
  8250. intel_ddi_get_crtc_new_encoder(crtc_state);
  8251. if (intel_encoder->type != INTEL_OUTPUT_DSI) {
  8252. if (!intel_ddi_pll_select(crtc, crtc_state))
  8253. return -EINVAL;
  8254. }
  8255. crtc->lowfreq_avail = false;
  8256. return 0;
  8257. }
  8258. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8259. enum port port,
  8260. struct intel_crtc_state *pipe_config)
  8261. {
  8262. switch (port) {
  8263. case PORT_A:
  8264. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8265. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8266. break;
  8267. case PORT_B:
  8268. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8269. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8270. break;
  8271. case PORT_C:
  8272. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8273. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8274. break;
  8275. default:
  8276. DRM_ERROR("Incorrect port type\n");
  8277. }
  8278. }
  8279. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8280. enum port port,
  8281. struct intel_crtc_state *pipe_config)
  8282. {
  8283. u32 temp, dpll_ctl1;
  8284. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8285. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8286. switch (pipe_config->ddi_pll_sel) {
  8287. case SKL_DPLL0:
  8288. /*
  8289. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8290. * of the shared DPLL framework and thus needs to be read out
  8291. * separately
  8292. */
  8293. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8294. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8295. break;
  8296. case SKL_DPLL1:
  8297. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8298. break;
  8299. case SKL_DPLL2:
  8300. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8301. break;
  8302. case SKL_DPLL3:
  8303. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8304. break;
  8305. }
  8306. }
  8307. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8308. enum port port,
  8309. struct intel_crtc_state *pipe_config)
  8310. {
  8311. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8312. switch (pipe_config->ddi_pll_sel) {
  8313. case PORT_CLK_SEL_WRPLL1:
  8314. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8315. break;
  8316. case PORT_CLK_SEL_WRPLL2:
  8317. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8318. break;
  8319. case PORT_CLK_SEL_SPLL:
  8320. pipe_config->shared_dpll = DPLL_ID_SPLL;
  8321. break;
  8322. }
  8323. }
  8324. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8325. struct intel_crtc_state *pipe_config)
  8326. {
  8327. struct drm_device *dev = crtc->base.dev;
  8328. struct drm_i915_private *dev_priv = dev->dev_private;
  8329. struct intel_shared_dpll *pll;
  8330. enum port port;
  8331. uint32_t tmp;
  8332. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8333. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8334. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8335. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8336. else if (IS_BROXTON(dev))
  8337. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8338. else
  8339. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8340. if (pipe_config->shared_dpll >= 0) {
  8341. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8342. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8343. &pipe_config->dpll_hw_state));
  8344. }
  8345. /*
  8346. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8347. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8348. * the PCH transcoder is on.
  8349. */
  8350. if (INTEL_INFO(dev)->gen < 9 &&
  8351. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8352. pipe_config->has_pch_encoder = true;
  8353. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8354. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8355. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8356. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8357. }
  8358. }
  8359. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8360. struct intel_crtc_state *pipe_config)
  8361. {
  8362. struct drm_device *dev = crtc->base.dev;
  8363. struct drm_i915_private *dev_priv = dev->dev_private;
  8364. enum intel_display_power_domain pfit_domain;
  8365. uint32_t tmp;
  8366. if (!intel_display_power_is_enabled(dev_priv,
  8367. POWER_DOMAIN_PIPE(crtc->pipe)))
  8368. return false;
  8369. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8370. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8371. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8372. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8373. enum pipe trans_edp_pipe;
  8374. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8375. default:
  8376. WARN(1, "unknown pipe linked to edp transcoder\n");
  8377. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8378. case TRANS_DDI_EDP_INPUT_A_ON:
  8379. trans_edp_pipe = PIPE_A;
  8380. break;
  8381. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8382. trans_edp_pipe = PIPE_B;
  8383. break;
  8384. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8385. trans_edp_pipe = PIPE_C;
  8386. break;
  8387. }
  8388. if (trans_edp_pipe == crtc->pipe)
  8389. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8390. }
  8391. if (!intel_display_power_is_enabled(dev_priv,
  8392. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8393. return false;
  8394. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8395. if (!(tmp & PIPECONF_ENABLE))
  8396. return false;
  8397. haswell_get_ddi_port_state(crtc, pipe_config);
  8398. intel_get_pipe_timings(crtc, pipe_config);
  8399. if (INTEL_INFO(dev)->gen >= 9) {
  8400. skl_init_scalers(dev, crtc, pipe_config);
  8401. }
  8402. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8403. if (INTEL_INFO(dev)->gen >= 9) {
  8404. pipe_config->scaler_state.scaler_id = -1;
  8405. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8406. }
  8407. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8408. if (INTEL_INFO(dev)->gen >= 9)
  8409. skylake_get_pfit_config(crtc, pipe_config);
  8410. else
  8411. ironlake_get_pfit_config(crtc, pipe_config);
  8412. }
  8413. if (IS_HASWELL(dev))
  8414. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8415. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8416. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8417. pipe_config->pixel_multiplier =
  8418. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8419. } else {
  8420. pipe_config->pixel_multiplier = 1;
  8421. }
  8422. return true;
  8423. }
  8424. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  8425. const struct intel_plane_state *plane_state)
  8426. {
  8427. struct drm_device *dev = crtc->dev;
  8428. struct drm_i915_private *dev_priv = dev->dev_private;
  8429. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8430. uint32_t cntl = 0, size = 0;
  8431. if (plane_state && plane_state->visible) {
  8432. unsigned int width = plane_state->base.crtc_w;
  8433. unsigned int height = plane_state->base.crtc_h;
  8434. unsigned int stride = roundup_pow_of_two(width) * 4;
  8435. switch (stride) {
  8436. default:
  8437. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8438. width, stride);
  8439. stride = 256;
  8440. /* fallthrough */
  8441. case 256:
  8442. case 512:
  8443. case 1024:
  8444. case 2048:
  8445. break;
  8446. }
  8447. cntl |= CURSOR_ENABLE |
  8448. CURSOR_GAMMA_ENABLE |
  8449. CURSOR_FORMAT_ARGB |
  8450. CURSOR_STRIDE(stride);
  8451. size = (height << 12) | width;
  8452. }
  8453. if (intel_crtc->cursor_cntl != 0 &&
  8454. (intel_crtc->cursor_base != base ||
  8455. intel_crtc->cursor_size != size ||
  8456. intel_crtc->cursor_cntl != cntl)) {
  8457. /* On these chipsets we can only modify the base/size/stride
  8458. * whilst the cursor is disabled.
  8459. */
  8460. I915_WRITE(CURCNTR(PIPE_A), 0);
  8461. POSTING_READ(CURCNTR(PIPE_A));
  8462. intel_crtc->cursor_cntl = 0;
  8463. }
  8464. if (intel_crtc->cursor_base != base) {
  8465. I915_WRITE(CURBASE(PIPE_A), base);
  8466. intel_crtc->cursor_base = base;
  8467. }
  8468. if (intel_crtc->cursor_size != size) {
  8469. I915_WRITE(CURSIZE, size);
  8470. intel_crtc->cursor_size = size;
  8471. }
  8472. if (intel_crtc->cursor_cntl != cntl) {
  8473. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8474. POSTING_READ(CURCNTR(PIPE_A));
  8475. intel_crtc->cursor_cntl = cntl;
  8476. }
  8477. }
  8478. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  8479. const struct intel_plane_state *plane_state)
  8480. {
  8481. struct drm_device *dev = crtc->dev;
  8482. struct drm_i915_private *dev_priv = dev->dev_private;
  8483. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8484. int pipe = intel_crtc->pipe;
  8485. uint32_t cntl = 0;
  8486. if (plane_state && plane_state->visible) {
  8487. cntl = MCURSOR_GAMMA_ENABLE;
  8488. switch (plane_state->base.crtc_w) {
  8489. case 64:
  8490. cntl |= CURSOR_MODE_64_ARGB_AX;
  8491. break;
  8492. case 128:
  8493. cntl |= CURSOR_MODE_128_ARGB_AX;
  8494. break;
  8495. case 256:
  8496. cntl |= CURSOR_MODE_256_ARGB_AX;
  8497. break;
  8498. default:
  8499. MISSING_CASE(plane_state->base.crtc_w);
  8500. return;
  8501. }
  8502. cntl |= pipe << 28; /* Connect to correct pipe */
  8503. if (HAS_DDI(dev))
  8504. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8505. if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
  8506. cntl |= CURSOR_ROTATE_180;
  8507. }
  8508. if (intel_crtc->cursor_cntl != cntl) {
  8509. I915_WRITE(CURCNTR(pipe), cntl);
  8510. POSTING_READ(CURCNTR(pipe));
  8511. intel_crtc->cursor_cntl = cntl;
  8512. }
  8513. /* and commit changes on next vblank */
  8514. I915_WRITE(CURBASE(pipe), base);
  8515. POSTING_READ(CURBASE(pipe));
  8516. intel_crtc->cursor_base = base;
  8517. }
  8518. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8519. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8520. const struct intel_plane_state *plane_state)
  8521. {
  8522. struct drm_device *dev = crtc->dev;
  8523. struct drm_i915_private *dev_priv = dev->dev_private;
  8524. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8525. int pipe = intel_crtc->pipe;
  8526. u32 base = intel_crtc->cursor_addr;
  8527. u32 pos = 0;
  8528. if (plane_state) {
  8529. int x = plane_state->base.crtc_x;
  8530. int y = plane_state->base.crtc_y;
  8531. if (x < 0) {
  8532. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8533. x = -x;
  8534. }
  8535. pos |= x << CURSOR_X_SHIFT;
  8536. if (y < 0) {
  8537. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8538. y = -y;
  8539. }
  8540. pos |= y << CURSOR_Y_SHIFT;
  8541. /* ILK+ do this automagically */
  8542. if (HAS_GMCH_DISPLAY(dev) &&
  8543. plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  8544. base += (plane_state->base.crtc_h *
  8545. plane_state->base.crtc_w - 1) * 4;
  8546. }
  8547. }
  8548. I915_WRITE(CURPOS(pipe), pos);
  8549. if (IS_845G(dev) || IS_I865G(dev))
  8550. i845_update_cursor(crtc, base, plane_state);
  8551. else
  8552. i9xx_update_cursor(crtc, base, plane_state);
  8553. }
  8554. static bool cursor_size_ok(struct drm_device *dev,
  8555. uint32_t width, uint32_t height)
  8556. {
  8557. if (width == 0 || height == 0)
  8558. return false;
  8559. /*
  8560. * 845g/865g are special in that they are only limited by
  8561. * the width of their cursors, the height is arbitrary up to
  8562. * the precision of the register. Everything else requires
  8563. * square cursors, limited to a few power-of-two sizes.
  8564. */
  8565. if (IS_845G(dev) || IS_I865G(dev)) {
  8566. if ((width & 63) != 0)
  8567. return false;
  8568. if (width > (IS_845G(dev) ? 64 : 512))
  8569. return false;
  8570. if (height > 1023)
  8571. return false;
  8572. } else {
  8573. switch (width | height) {
  8574. case 256:
  8575. case 128:
  8576. if (IS_GEN2(dev))
  8577. return false;
  8578. case 64:
  8579. break;
  8580. default:
  8581. return false;
  8582. }
  8583. }
  8584. return true;
  8585. }
  8586. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8587. u16 *blue, uint32_t start, uint32_t size)
  8588. {
  8589. int end = (start + size > 256) ? 256 : start + size, i;
  8590. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8591. for (i = start; i < end; i++) {
  8592. intel_crtc->lut_r[i] = red[i] >> 8;
  8593. intel_crtc->lut_g[i] = green[i] >> 8;
  8594. intel_crtc->lut_b[i] = blue[i] >> 8;
  8595. }
  8596. intel_crtc_load_lut(crtc);
  8597. }
  8598. /* VESA 640x480x72Hz mode to set on the pipe */
  8599. static struct drm_display_mode load_detect_mode = {
  8600. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8601. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8602. };
  8603. struct drm_framebuffer *
  8604. __intel_framebuffer_create(struct drm_device *dev,
  8605. struct drm_mode_fb_cmd2 *mode_cmd,
  8606. struct drm_i915_gem_object *obj)
  8607. {
  8608. struct intel_framebuffer *intel_fb;
  8609. int ret;
  8610. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8611. if (!intel_fb)
  8612. return ERR_PTR(-ENOMEM);
  8613. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8614. if (ret)
  8615. goto err;
  8616. return &intel_fb->base;
  8617. err:
  8618. kfree(intel_fb);
  8619. return ERR_PTR(ret);
  8620. }
  8621. static struct drm_framebuffer *
  8622. intel_framebuffer_create(struct drm_device *dev,
  8623. struct drm_mode_fb_cmd2 *mode_cmd,
  8624. struct drm_i915_gem_object *obj)
  8625. {
  8626. struct drm_framebuffer *fb;
  8627. int ret;
  8628. ret = i915_mutex_lock_interruptible(dev);
  8629. if (ret)
  8630. return ERR_PTR(ret);
  8631. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8632. mutex_unlock(&dev->struct_mutex);
  8633. return fb;
  8634. }
  8635. static u32
  8636. intel_framebuffer_pitch_for_width(int width, int bpp)
  8637. {
  8638. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8639. return ALIGN(pitch, 64);
  8640. }
  8641. static u32
  8642. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8643. {
  8644. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8645. return PAGE_ALIGN(pitch * mode->vdisplay);
  8646. }
  8647. static struct drm_framebuffer *
  8648. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8649. struct drm_display_mode *mode,
  8650. int depth, int bpp)
  8651. {
  8652. struct drm_framebuffer *fb;
  8653. struct drm_i915_gem_object *obj;
  8654. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8655. obj = i915_gem_alloc_object(dev,
  8656. intel_framebuffer_size_for_mode(mode, bpp));
  8657. if (obj == NULL)
  8658. return ERR_PTR(-ENOMEM);
  8659. mode_cmd.width = mode->hdisplay;
  8660. mode_cmd.height = mode->vdisplay;
  8661. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8662. bpp);
  8663. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8664. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8665. if (IS_ERR(fb))
  8666. drm_gem_object_unreference_unlocked(&obj->base);
  8667. return fb;
  8668. }
  8669. static struct drm_framebuffer *
  8670. mode_fits_in_fbdev(struct drm_device *dev,
  8671. struct drm_display_mode *mode)
  8672. {
  8673. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8674. struct drm_i915_private *dev_priv = dev->dev_private;
  8675. struct drm_i915_gem_object *obj;
  8676. struct drm_framebuffer *fb;
  8677. if (!dev_priv->fbdev)
  8678. return NULL;
  8679. if (!dev_priv->fbdev->fb)
  8680. return NULL;
  8681. obj = dev_priv->fbdev->fb->obj;
  8682. BUG_ON(!obj);
  8683. fb = &dev_priv->fbdev->fb->base;
  8684. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8685. fb->bits_per_pixel))
  8686. return NULL;
  8687. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8688. return NULL;
  8689. return fb;
  8690. #else
  8691. return NULL;
  8692. #endif
  8693. }
  8694. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8695. struct drm_crtc *crtc,
  8696. struct drm_display_mode *mode,
  8697. struct drm_framebuffer *fb,
  8698. int x, int y)
  8699. {
  8700. struct drm_plane_state *plane_state;
  8701. int hdisplay, vdisplay;
  8702. int ret;
  8703. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8704. if (IS_ERR(plane_state))
  8705. return PTR_ERR(plane_state);
  8706. if (mode)
  8707. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8708. else
  8709. hdisplay = vdisplay = 0;
  8710. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8711. if (ret)
  8712. return ret;
  8713. drm_atomic_set_fb_for_plane(plane_state, fb);
  8714. plane_state->crtc_x = 0;
  8715. plane_state->crtc_y = 0;
  8716. plane_state->crtc_w = hdisplay;
  8717. plane_state->crtc_h = vdisplay;
  8718. plane_state->src_x = x << 16;
  8719. plane_state->src_y = y << 16;
  8720. plane_state->src_w = hdisplay << 16;
  8721. plane_state->src_h = vdisplay << 16;
  8722. return 0;
  8723. }
  8724. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8725. struct drm_display_mode *mode,
  8726. struct intel_load_detect_pipe *old,
  8727. struct drm_modeset_acquire_ctx *ctx)
  8728. {
  8729. struct intel_crtc *intel_crtc;
  8730. struct intel_encoder *intel_encoder =
  8731. intel_attached_encoder(connector);
  8732. struct drm_crtc *possible_crtc;
  8733. struct drm_encoder *encoder = &intel_encoder->base;
  8734. struct drm_crtc *crtc = NULL;
  8735. struct drm_device *dev = encoder->dev;
  8736. struct drm_framebuffer *fb;
  8737. struct drm_mode_config *config = &dev->mode_config;
  8738. struct drm_atomic_state *state = NULL;
  8739. struct drm_connector_state *connector_state;
  8740. struct intel_crtc_state *crtc_state;
  8741. int ret, i = -1;
  8742. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8743. connector->base.id, connector->name,
  8744. encoder->base.id, encoder->name);
  8745. retry:
  8746. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8747. if (ret)
  8748. goto fail;
  8749. /*
  8750. * Algorithm gets a little messy:
  8751. *
  8752. * - if the connector already has an assigned crtc, use it (but make
  8753. * sure it's on first)
  8754. *
  8755. * - try to find the first unused crtc that can drive this connector,
  8756. * and use that if we find one
  8757. */
  8758. /* See if we already have a CRTC for this connector */
  8759. if (encoder->crtc) {
  8760. crtc = encoder->crtc;
  8761. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8762. if (ret)
  8763. goto fail;
  8764. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8765. if (ret)
  8766. goto fail;
  8767. old->dpms_mode = connector->dpms;
  8768. old->load_detect_temp = false;
  8769. /* Make sure the crtc and connector are running */
  8770. if (connector->dpms != DRM_MODE_DPMS_ON)
  8771. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8772. return true;
  8773. }
  8774. /* Find an unused one (if possible) */
  8775. for_each_crtc(dev, possible_crtc) {
  8776. i++;
  8777. if (!(encoder->possible_crtcs & (1 << i)))
  8778. continue;
  8779. if (possible_crtc->state->enable)
  8780. continue;
  8781. crtc = possible_crtc;
  8782. break;
  8783. }
  8784. /*
  8785. * If we didn't find an unused CRTC, don't use any.
  8786. */
  8787. if (!crtc) {
  8788. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8789. goto fail;
  8790. }
  8791. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8792. if (ret)
  8793. goto fail;
  8794. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8795. if (ret)
  8796. goto fail;
  8797. intel_crtc = to_intel_crtc(crtc);
  8798. old->dpms_mode = connector->dpms;
  8799. old->load_detect_temp = true;
  8800. old->release_fb = NULL;
  8801. state = drm_atomic_state_alloc(dev);
  8802. if (!state)
  8803. return false;
  8804. state->acquire_ctx = ctx;
  8805. connector_state = drm_atomic_get_connector_state(state, connector);
  8806. if (IS_ERR(connector_state)) {
  8807. ret = PTR_ERR(connector_state);
  8808. goto fail;
  8809. }
  8810. connector_state->crtc = crtc;
  8811. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8812. if (IS_ERR(crtc_state)) {
  8813. ret = PTR_ERR(crtc_state);
  8814. goto fail;
  8815. }
  8816. crtc_state->base.active = crtc_state->base.enable = true;
  8817. if (!mode)
  8818. mode = &load_detect_mode;
  8819. /* We need a framebuffer large enough to accommodate all accesses
  8820. * that the plane may generate whilst we perform load detection.
  8821. * We can not rely on the fbcon either being present (we get called
  8822. * during its initialisation to detect all boot displays, or it may
  8823. * not even exist) or that it is large enough to satisfy the
  8824. * requested mode.
  8825. */
  8826. fb = mode_fits_in_fbdev(dev, mode);
  8827. if (fb == NULL) {
  8828. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8829. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8830. old->release_fb = fb;
  8831. } else
  8832. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8833. if (IS_ERR(fb)) {
  8834. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8835. goto fail;
  8836. }
  8837. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8838. if (ret)
  8839. goto fail;
  8840. drm_mode_copy(&crtc_state->base.mode, mode);
  8841. if (drm_atomic_commit(state)) {
  8842. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8843. if (old->release_fb)
  8844. old->release_fb->funcs->destroy(old->release_fb);
  8845. goto fail;
  8846. }
  8847. crtc->primary->crtc = crtc;
  8848. /* let the connector get through one full cycle before testing */
  8849. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8850. return true;
  8851. fail:
  8852. drm_atomic_state_free(state);
  8853. state = NULL;
  8854. if (ret == -EDEADLK) {
  8855. drm_modeset_backoff(ctx);
  8856. goto retry;
  8857. }
  8858. return false;
  8859. }
  8860. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8861. struct intel_load_detect_pipe *old,
  8862. struct drm_modeset_acquire_ctx *ctx)
  8863. {
  8864. struct drm_device *dev = connector->dev;
  8865. struct intel_encoder *intel_encoder =
  8866. intel_attached_encoder(connector);
  8867. struct drm_encoder *encoder = &intel_encoder->base;
  8868. struct drm_crtc *crtc = encoder->crtc;
  8869. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8870. struct drm_atomic_state *state;
  8871. struct drm_connector_state *connector_state;
  8872. struct intel_crtc_state *crtc_state;
  8873. int ret;
  8874. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8875. connector->base.id, connector->name,
  8876. encoder->base.id, encoder->name);
  8877. if (old->load_detect_temp) {
  8878. state = drm_atomic_state_alloc(dev);
  8879. if (!state)
  8880. goto fail;
  8881. state->acquire_ctx = ctx;
  8882. connector_state = drm_atomic_get_connector_state(state, connector);
  8883. if (IS_ERR(connector_state))
  8884. goto fail;
  8885. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8886. if (IS_ERR(crtc_state))
  8887. goto fail;
  8888. connector_state->crtc = NULL;
  8889. crtc_state->base.enable = crtc_state->base.active = false;
  8890. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8891. 0, 0);
  8892. if (ret)
  8893. goto fail;
  8894. ret = drm_atomic_commit(state);
  8895. if (ret)
  8896. goto fail;
  8897. if (old->release_fb) {
  8898. drm_framebuffer_unregister_private(old->release_fb);
  8899. drm_framebuffer_unreference(old->release_fb);
  8900. }
  8901. return;
  8902. }
  8903. /* Switch crtc and encoder back off if necessary */
  8904. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8905. connector->funcs->dpms(connector, old->dpms_mode);
  8906. return;
  8907. fail:
  8908. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8909. drm_atomic_state_free(state);
  8910. }
  8911. static int i9xx_pll_refclk(struct drm_device *dev,
  8912. const struct intel_crtc_state *pipe_config)
  8913. {
  8914. struct drm_i915_private *dev_priv = dev->dev_private;
  8915. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8916. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8917. return dev_priv->vbt.lvds_ssc_freq;
  8918. else if (HAS_PCH_SPLIT(dev))
  8919. return 120000;
  8920. else if (!IS_GEN2(dev))
  8921. return 96000;
  8922. else
  8923. return 48000;
  8924. }
  8925. /* Returns the clock of the currently programmed mode of the given pipe. */
  8926. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8927. struct intel_crtc_state *pipe_config)
  8928. {
  8929. struct drm_device *dev = crtc->base.dev;
  8930. struct drm_i915_private *dev_priv = dev->dev_private;
  8931. int pipe = pipe_config->cpu_transcoder;
  8932. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8933. u32 fp;
  8934. intel_clock_t clock;
  8935. int port_clock;
  8936. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8937. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8938. fp = pipe_config->dpll_hw_state.fp0;
  8939. else
  8940. fp = pipe_config->dpll_hw_state.fp1;
  8941. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8942. if (IS_PINEVIEW(dev)) {
  8943. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8944. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8945. } else {
  8946. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8947. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8948. }
  8949. if (!IS_GEN2(dev)) {
  8950. if (IS_PINEVIEW(dev))
  8951. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8952. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8953. else
  8954. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8955. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8956. switch (dpll & DPLL_MODE_MASK) {
  8957. case DPLLB_MODE_DAC_SERIAL:
  8958. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8959. 5 : 10;
  8960. break;
  8961. case DPLLB_MODE_LVDS:
  8962. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8963. 7 : 14;
  8964. break;
  8965. default:
  8966. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8967. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8968. return;
  8969. }
  8970. if (IS_PINEVIEW(dev))
  8971. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8972. else
  8973. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8974. } else {
  8975. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8976. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8977. if (is_lvds) {
  8978. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8979. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8980. if (lvds & LVDS_CLKB_POWER_UP)
  8981. clock.p2 = 7;
  8982. else
  8983. clock.p2 = 14;
  8984. } else {
  8985. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8986. clock.p1 = 2;
  8987. else {
  8988. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8989. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8990. }
  8991. if (dpll & PLL_P2_DIVIDE_BY_4)
  8992. clock.p2 = 4;
  8993. else
  8994. clock.p2 = 2;
  8995. }
  8996. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8997. }
  8998. /*
  8999. * This value includes pixel_multiplier. We will use
  9000. * port_clock to compute adjusted_mode.crtc_clock in the
  9001. * encoder's get_config() function.
  9002. */
  9003. pipe_config->port_clock = port_clock;
  9004. }
  9005. int intel_dotclock_calculate(int link_freq,
  9006. const struct intel_link_m_n *m_n)
  9007. {
  9008. /*
  9009. * The calculation for the data clock is:
  9010. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9011. * But we want to avoid losing precison if possible, so:
  9012. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9013. *
  9014. * and the link clock is simpler:
  9015. * link_clock = (m * link_clock) / n
  9016. */
  9017. if (!m_n->link_n)
  9018. return 0;
  9019. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9020. }
  9021. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9022. struct intel_crtc_state *pipe_config)
  9023. {
  9024. struct drm_device *dev = crtc->base.dev;
  9025. /* read out port_clock from the DPLL */
  9026. i9xx_crtc_clock_get(crtc, pipe_config);
  9027. /*
  9028. * This value does not include pixel_multiplier.
  9029. * We will check that port_clock and adjusted_mode.crtc_clock
  9030. * agree once we know their relationship in the encoder's
  9031. * get_config() function.
  9032. */
  9033. pipe_config->base.adjusted_mode.crtc_clock =
  9034. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  9035. &pipe_config->fdi_m_n);
  9036. }
  9037. /** Returns the currently programmed mode of the given pipe. */
  9038. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9039. struct drm_crtc *crtc)
  9040. {
  9041. struct drm_i915_private *dev_priv = dev->dev_private;
  9042. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9043. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9044. struct drm_display_mode *mode;
  9045. struct intel_crtc_state *pipe_config;
  9046. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9047. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9048. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9049. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9050. enum pipe pipe = intel_crtc->pipe;
  9051. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9052. if (!mode)
  9053. return NULL;
  9054. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9055. if (!pipe_config) {
  9056. kfree(mode);
  9057. return NULL;
  9058. }
  9059. /*
  9060. * Construct a pipe_config sufficient for getting the clock info
  9061. * back out of crtc_clock_get.
  9062. *
  9063. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9064. * to use a real value here instead.
  9065. */
  9066. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9067. pipe_config->pixel_multiplier = 1;
  9068. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9069. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9070. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9071. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9072. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9073. mode->hdisplay = (htot & 0xffff) + 1;
  9074. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9075. mode->hsync_start = (hsync & 0xffff) + 1;
  9076. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9077. mode->vdisplay = (vtot & 0xffff) + 1;
  9078. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9079. mode->vsync_start = (vsync & 0xffff) + 1;
  9080. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9081. drm_mode_set_name(mode);
  9082. kfree(pipe_config);
  9083. return mode;
  9084. }
  9085. void intel_mark_busy(struct drm_device *dev)
  9086. {
  9087. struct drm_i915_private *dev_priv = dev->dev_private;
  9088. if (dev_priv->mm.busy)
  9089. return;
  9090. intel_runtime_pm_get(dev_priv);
  9091. i915_update_gfx_val(dev_priv);
  9092. if (INTEL_INFO(dev)->gen >= 6)
  9093. gen6_rps_busy(dev_priv);
  9094. dev_priv->mm.busy = true;
  9095. }
  9096. void intel_mark_idle(struct drm_device *dev)
  9097. {
  9098. struct drm_i915_private *dev_priv = dev->dev_private;
  9099. if (!dev_priv->mm.busy)
  9100. return;
  9101. dev_priv->mm.busy = false;
  9102. if (INTEL_INFO(dev)->gen >= 6)
  9103. gen6_rps_idle(dev->dev_private);
  9104. intel_runtime_pm_put(dev_priv);
  9105. }
  9106. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9107. {
  9108. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9109. struct drm_device *dev = crtc->dev;
  9110. struct intel_unpin_work *work;
  9111. spin_lock_irq(&dev->event_lock);
  9112. work = intel_crtc->unpin_work;
  9113. intel_crtc->unpin_work = NULL;
  9114. spin_unlock_irq(&dev->event_lock);
  9115. if (work) {
  9116. cancel_work_sync(&work->work);
  9117. kfree(work);
  9118. }
  9119. drm_crtc_cleanup(crtc);
  9120. kfree(intel_crtc);
  9121. }
  9122. static void intel_unpin_work_fn(struct work_struct *__work)
  9123. {
  9124. struct intel_unpin_work *work =
  9125. container_of(__work, struct intel_unpin_work, work);
  9126. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9127. struct drm_device *dev = crtc->base.dev;
  9128. struct drm_plane *primary = crtc->base.primary;
  9129. mutex_lock(&dev->struct_mutex);
  9130. intel_unpin_fb_obj(work->old_fb, primary->state);
  9131. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9132. if (work->flip_queued_req)
  9133. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9134. mutex_unlock(&dev->struct_mutex);
  9135. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  9136. intel_fbc_post_update(crtc);
  9137. drm_framebuffer_unreference(work->old_fb);
  9138. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9139. atomic_dec(&crtc->unpin_work_count);
  9140. kfree(work);
  9141. }
  9142. static void do_intel_finish_page_flip(struct drm_device *dev,
  9143. struct drm_crtc *crtc)
  9144. {
  9145. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9146. struct intel_unpin_work *work;
  9147. unsigned long flags;
  9148. /* Ignore early vblank irqs */
  9149. if (intel_crtc == NULL)
  9150. return;
  9151. /*
  9152. * This is called both by irq handlers and the reset code (to complete
  9153. * lost pageflips) so needs the full irqsave spinlocks.
  9154. */
  9155. spin_lock_irqsave(&dev->event_lock, flags);
  9156. work = intel_crtc->unpin_work;
  9157. /* Ensure we don't miss a work->pending update ... */
  9158. smp_rmb();
  9159. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9160. spin_unlock_irqrestore(&dev->event_lock, flags);
  9161. return;
  9162. }
  9163. page_flip_completed(intel_crtc);
  9164. spin_unlock_irqrestore(&dev->event_lock, flags);
  9165. }
  9166. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9167. {
  9168. struct drm_i915_private *dev_priv = dev->dev_private;
  9169. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9170. do_intel_finish_page_flip(dev, crtc);
  9171. }
  9172. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9173. {
  9174. struct drm_i915_private *dev_priv = dev->dev_private;
  9175. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9176. do_intel_finish_page_flip(dev, crtc);
  9177. }
  9178. /* Is 'a' after or equal to 'b'? */
  9179. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9180. {
  9181. return !((a - b) & 0x80000000);
  9182. }
  9183. static bool page_flip_finished(struct intel_crtc *crtc)
  9184. {
  9185. struct drm_device *dev = crtc->base.dev;
  9186. struct drm_i915_private *dev_priv = dev->dev_private;
  9187. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9188. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9189. return true;
  9190. /*
  9191. * The relevant registers doen't exist on pre-ctg.
  9192. * As the flip done interrupt doesn't trigger for mmio
  9193. * flips on gmch platforms, a flip count check isn't
  9194. * really needed there. But since ctg has the registers,
  9195. * include it in the check anyway.
  9196. */
  9197. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9198. return true;
  9199. /*
  9200. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9201. * used the same base address. In that case the mmio flip might
  9202. * have completed, but the CS hasn't even executed the flip yet.
  9203. *
  9204. * A flip count check isn't enough as the CS might have updated
  9205. * the base address just after start of vblank, but before we
  9206. * managed to process the interrupt. This means we'd complete the
  9207. * CS flip too soon.
  9208. *
  9209. * Combining both checks should get us a good enough result. It may
  9210. * still happen that the CS flip has been executed, but has not
  9211. * yet actually completed. But in case the base address is the same
  9212. * anyway, we don't really care.
  9213. */
  9214. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9215. crtc->unpin_work->gtt_offset &&
  9216. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9217. crtc->unpin_work->flip_count);
  9218. }
  9219. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9220. {
  9221. struct drm_i915_private *dev_priv = dev->dev_private;
  9222. struct intel_crtc *intel_crtc =
  9223. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9224. unsigned long flags;
  9225. /*
  9226. * This is called both by irq handlers and the reset code (to complete
  9227. * lost pageflips) so needs the full irqsave spinlocks.
  9228. *
  9229. * NB: An MMIO update of the plane base pointer will also
  9230. * generate a page-flip completion irq, i.e. every modeset
  9231. * is also accompanied by a spurious intel_prepare_page_flip().
  9232. */
  9233. spin_lock_irqsave(&dev->event_lock, flags);
  9234. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9235. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9236. spin_unlock_irqrestore(&dev->event_lock, flags);
  9237. }
  9238. static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
  9239. {
  9240. /* Ensure that the work item is consistent when activating it ... */
  9241. smp_wmb();
  9242. atomic_set(&work->pending, INTEL_FLIP_PENDING);
  9243. /* and that it is marked active as soon as the irq could fire. */
  9244. smp_wmb();
  9245. }
  9246. static int intel_gen2_queue_flip(struct drm_device *dev,
  9247. struct drm_crtc *crtc,
  9248. struct drm_framebuffer *fb,
  9249. struct drm_i915_gem_object *obj,
  9250. struct drm_i915_gem_request *req,
  9251. uint32_t flags)
  9252. {
  9253. struct intel_engine_cs *ring = req->ring;
  9254. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9255. u32 flip_mask;
  9256. int ret;
  9257. ret = intel_ring_begin(req, 6);
  9258. if (ret)
  9259. return ret;
  9260. /* Can't queue multiple flips, so wait for the previous
  9261. * one to finish before executing the next.
  9262. */
  9263. if (intel_crtc->plane)
  9264. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9265. else
  9266. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9267. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9268. intel_ring_emit(ring, MI_NOOP);
  9269. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9270. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9271. intel_ring_emit(ring, fb->pitches[0]);
  9272. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9273. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9274. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9275. return 0;
  9276. }
  9277. static int intel_gen3_queue_flip(struct drm_device *dev,
  9278. struct drm_crtc *crtc,
  9279. struct drm_framebuffer *fb,
  9280. struct drm_i915_gem_object *obj,
  9281. struct drm_i915_gem_request *req,
  9282. uint32_t flags)
  9283. {
  9284. struct intel_engine_cs *ring = req->ring;
  9285. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9286. u32 flip_mask;
  9287. int ret;
  9288. ret = intel_ring_begin(req, 6);
  9289. if (ret)
  9290. return ret;
  9291. if (intel_crtc->plane)
  9292. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9293. else
  9294. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9295. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9296. intel_ring_emit(ring, MI_NOOP);
  9297. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9298. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9299. intel_ring_emit(ring, fb->pitches[0]);
  9300. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9301. intel_ring_emit(ring, MI_NOOP);
  9302. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9303. return 0;
  9304. }
  9305. static int intel_gen4_queue_flip(struct drm_device *dev,
  9306. struct drm_crtc *crtc,
  9307. struct drm_framebuffer *fb,
  9308. struct drm_i915_gem_object *obj,
  9309. struct drm_i915_gem_request *req,
  9310. uint32_t flags)
  9311. {
  9312. struct intel_engine_cs *ring = req->ring;
  9313. struct drm_i915_private *dev_priv = dev->dev_private;
  9314. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9315. uint32_t pf, pipesrc;
  9316. int ret;
  9317. ret = intel_ring_begin(req, 4);
  9318. if (ret)
  9319. return ret;
  9320. /* i965+ uses the linear or tiled offsets from the
  9321. * Display Registers (which do not change across a page-flip)
  9322. * so we need only reprogram the base address.
  9323. */
  9324. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9325. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9326. intel_ring_emit(ring, fb->pitches[0]);
  9327. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9328. obj->tiling_mode);
  9329. /* XXX Enabling the panel-fitter across page-flip is so far
  9330. * untested on non-native modes, so ignore it for now.
  9331. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9332. */
  9333. pf = 0;
  9334. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9335. intel_ring_emit(ring, pf | pipesrc);
  9336. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9337. return 0;
  9338. }
  9339. static int intel_gen6_queue_flip(struct drm_device *dev,
  9340. struct drm_crtc *crtc,
  9341. struct drm_framebuffer *fb,
  9342. struct drm_i915_gem_object *obj,
  9343. struct drm_i915_gem_request *req,
  9344. uint32_t flags)
  9345. {
  9346. struct intel_engine_cs *ring = req->ring;
  9347. struct drm_i915_private *dev_priv = dev->dev_private;
  9348. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9349. uint32_t pf, pipesrc;
  9350. int ret;
  9351. ret = intel_ring_begin(req, 4);
  9352. if (ret)
  9353. return ret;
  9354. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9355. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9356. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9357. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9358. /* Contrary to the suggestions in the documentation,
  9359. * "Enable Panel Fitter" does not seem to be required when page
  9360. * flipping with a non-native mode, and worse causes a normal
  9361. * modeset to fail.
  9362. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9363. */
  9364. pf = 0;
  9365. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9366. intel_ring_emit(ring, pf | pipesrc);
  9367. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9368. return 0;
  9369. }
  9370. static int intel_gen7_queue_flip(struct drm_device *dev,
  9371. struct drm_crtc *crtc,
  9372. struct drm_framebuffer *fb,
  9373. struct drm_i915_gem_object *obj,
  9374. struct drm_i915_gem_request *req,
  9375. uint32_t flags)
  9376. {
  9377. struct intel_engine_cs *ring = req->ring;
  9378. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9379. uint32_t plane_bit = 0;
  9380. int len, ret;
  9381. switch (intel_crtc->plane) {
  9382. case PLANE_A:
  9383. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9384. break;
  9385. case PLANE_B:
  9386. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9387. break;
  9388. case PLANE_C:
  9389. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9390. break;
  9391. default:
  9392. WARN_ONCE(1, "unknown plane in flip command\n");
  9393. return -ENODEV;
  9394. }
  9395. len = 4;
  9396. if (ring->id == RCS) {
  9397. len += 6;
  9398. /*
  9399. * On Gen 8, SRM is now taking an extra dword to accommodate
  9400. * 48bits addresses, and we need a NOOP for the batch size to
  9401. * stay even.
  9402. */
  9403. if (IS_GEN8(dev))
  9404. len += 2;
  9405. }
  9406. /*
  9407. * BSpec MI_DISPLAY_FLIP for IVB:
  9408. * "The full packet must be contained within the same cache line."
  9409. *
  9410. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9411. * cacheline, if we ever start emitting more commands before
  9412. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9413. * then do the cacheline alignment, and finally emit the
  9414. * MI_DISPLAY_FLIP.
  9415. */
  9416. ret = intel_ring_cacheline_align(req);
  9417. if (ret)
  9418. return ret;
  9419. ret = intel_ring_begin(req, len);
  9420. if (ret)
  9421. return ret;
  9422. /* Unmask the flip-done completion message. Note that the bspec says that
  9423. * we should do this for both the BCS and RCS, and that we must not unmask
  9424. * more than one flip event at any time (or ensure that one flip message
  9425. * can be sent by waiting for flip-done prior to queueing new flips).
  9426. * Experimentation says that BCS works despite DERRMR masking all
  9427. * flip-done completion events and that unmasking all planes at once
  9428. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9429. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9430. */
  9431. if (ring->id == RCS) {
  9432. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9433. intel_ring_emit_reg(ring, DERRMR);
  9434. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9435. DERRMR_PIPEB_PRI_FLIP_DONE |
  9436. DERRMR_PIPEC_PRI_FLIP_DONE));
  9437. if (IS_GEN8(dev))
  9438. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  9439. MI_SRM_LRM_GLOBAL_GTT);
  9440. else
  9441. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  9442. MI_SRM_LRM_GLOBAL_GTT);
  9443. intel_ring_emit_reg(ring, DERRMR);
  9444. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9445. if (IS_GEN8(dev)) {
  9446. intel_ring_emit(ring, 0);
  9447. intel_ring_emit(ring, MI_NOOP);
  9448. }
  9449. }
  9450. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9451. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9452. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9453. intel_ring_emit(ring, (MI_NOOP));
  9454. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9455. return 0;
  9456. }
  9457. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9458. struct drm_i915_gem_object *obj)
  9459. {
  9460. /*
  9461. * This is not being used for older platforms, because
  9462. * non-availability of flip done interrupt forces us to use
  9463. * CS flips. Older platforms derive flip done using some clever
  9464. * tricks involving the flip_pending status bits and vblank irqs.
  9465. * So using MMIO flips there would disrupt this mechanism.
  9466. */
  9467. if (ring == NULL)
  9468. return true;
  9469. if (INTEL_INFO(ring->dev)->gen < 5)
  9470. return false;
  9471. if (i915.use_mmio_flip < 0)
  9472. return false;
  9473. else if (i915.use_mmio_flip > 0)
  9474. return true;
  9475. else if (i915.enable_execlists)
  9476. return true;
  9477. else if (obj->base.dma_buf &&
  9478. !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
  9479. false))
  9480. return true;
  9481. else
  9482. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9483. }
  9484. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9485. unsigned int rotation,
  9486. struct intel_unpin_work *work)
  9487. {
  9488. struct drm_device *dev = intel_crtc->base.dev;
  9489. struct drm_i915_private *dev_priv = dev->dev_private;
  9490. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9491. const enum pipe pipe = intel_crtc->pipe;
  9492. u32 ctl, stride, tile_height;
  9493. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9494. ctl &= ~PLANE_CTL_TILED_MASK;
  9495. switch (fb->modifier[0]) {
  9496. case DRM_FORMAT_MOD_NONE:
  9497. break;
  9498. case I915_FORMAT_MOD_X_TILED:
  9499. ctl |= PLANE_CTL_TILED_X;
  9500. break;
  9501. case I915_FORMAT_MOD_Y_TILED:
  9502. ctl |= PLANE_CTL_TILED_Y;
  9503. break;
  9504. case I915_FORMAT_MOD_Yf_TILED:
  9505. ctl |= PLANE_CTL_TILED_YF;
  9506. break;
  9507. default:
  9508. MISSING_CASE(fb->modifier[0]);
  9509. }
  9510. /*
  9511. * The stride is either expressed as a multiple of 64 bytes chunks for
  9512. * linear buffers or in number of tiles for tiled buffers.
  9513. */
  9514. if (intel_rotation_90_or_270(rotation)) {
  9515. /* stride = Surface height in tiles */
  9516. tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
  9517. stride = DIV_ROUND_UP(fb->height, tile_height);
  9518. } else {
  9519. stride = fb->pitches[0] /
  9520. intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  9521. fb->pixel_format);
  9522. }
  9523. /*
  9524. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9525. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9526. */
  9527. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9528. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9529. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9530. POSTING_READ(PLANE_SURF(pipe, 0));
  9531. }
  9532. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9533. struct intel_unpin_work *work)
  9534. {
  9535. struct drm_device *dev = intel_crtc->base.dev;
  9536. struct drm_i915_private *dev_priv = dev->dev_private;
  9537. struct intel_framebuffer *intel_fb =
  9538. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9539. struct drm_i915_gem_object *obj = intel_fb->obj;
  9540. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  9541. u32 dspcntr;
  9542. dspcntr = I915_READ(reg);
  9543. if (obj->tiling_mode != I915_TILING_NONE)
  9544. dspcntr |= DISPPLANE_TILED;
  9545. else
  9546. dspcntr &= ~DISPPLANE_TILED;
  9547. I915_WRITE(reg, dspcntr);
  9548. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9549. POSTING_READ(DSPSURF(intel_crtc->plane));
  9550. }
  9551. /*
  9552. * XXX: This is the temporary way to update the plane registers until we get
  9553. * around to using the usual plane update functions for MMIO flips
  9554. */
  9555. static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
  9556. {
  9557. struct intel_crtc *crtc = mmio_flip->crtc;
  9558. struct intel_unpin_work *work;
  9559. spin_lock_irq(&crtc->base.dev->event_lock);
  9560. work = crtc->unpin_work;
  9561. spin_unlock_irq(&crtc->base.dev->event_lock);
  9562. if (work == NULL)
  9563. return;
  9564. intel_mark_page_flip_active(work);
  9565. intel_pipe_update_start(crtc);
  9566. if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
  9567. skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
  9568. else
  9569. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9570. ilk_do_mmio_flip(crtc, work);
  9571. intel_pipe_update_end(crtc);
  9572. }
  9573. static void intel_mmio_flip_work_func(struct work_struct *work)
  9574. {
  9575. struct intel_mmio_flip *mmio_flip =
  9576. container_of(work, struct intel_mmio_flip, work);
  9577. struct intel_framebuffer *intel_fb =
  9578. to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
  9579. struct drm_i915_gem_object *obj = intel_fb->obj;
  9580. if (mmio_flip->req) {
  9581. WARN_ON(__i915_wait_request(mmio_flip->req,
  9582. mmio_flip->crtc->reset_counter,
  9583. false, NULL,
  9584. &mmio_flip->i915->rps.mmioflips));
  9585. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9586. }
  9587. /* For framebuffer backed by dmabuf, wait for fence */
  9588. if (obj->base.dma_buf)
  9589. WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  9590. false, false,
  9591. MAX_SCHEDULE_TIMEOUT) < 0);
  9592. intel_do_mmio_flip(mmio_flip);
  9593. kfree(mmio_flip);
  9594. }
  9595. static int intel_queue_mmio_flip(struct drm_device *dev,
  9596. struct drm_crtc *crtc,
  9597. struct drm_i915_gem_object *obj)
  9598. {
  9599. struct intel_mmio_flip *mmio_flip;
  9600. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9601. if (mmio_flip == NULL)
  9602. return -ENOMEM;
  9603. mmio_flip->i915 = to_i915(dev);
  9604. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9605. mmio_flip->crtc = to_intel_crtc(crtc);
  9606. mmio_flip->rotation = crtc->primary->state->rotation;
  9607. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9608. schedule_work(&mmio_flip->work);
  9609. return 0;
  9610. }
  9611. static int intel_default_queue_flip(struct drm_device *dev,
  9612. struct drm_crtc *crtc,
  9613. struct drm_framebuffer *fb,
  9614. struct drm_i915_gem_object *obj,
  9615. struct drm_i915_gem_request *req,
  9616. uint32_t flags)
  9617. {
  9618. return -ENODEV;
  9619. }
  9620. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9621. struct drm_crtc *crtc)
  9622. {
  9623. struct drm_i915_private *dev_priv = dev->dev_private;
  9624. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9625. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9626. u32 addr;
  9627. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9628. return true;
  9629. if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
  9630. return false;
  9631. if (!work->enable_stall_check)
  9632. return false;
  9633. if (work->flip_ready_vblank == 0) {
  9634. if (work->flip_queued_req &&
  9635. !i915_gem_request_completed(work->flip_queued_req, true))
  9636. return false;
  9637. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9638. }
  9639. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9640. return false;
  9641. /* Potential stall - if we see that the flip has happened,
  9642. * assume a missed interrupt. */
  9643. if (INTEL_INFO(dev)->gen >= 4)
  9644. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9645. else
  9646. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9647. /* There is a potential issue here with a false positive after a flip
  9648. * to the same address. We could address this by checking for a
  9649. * non-incrementing frame counter.
  9650. */
  9651. return addr == work->gtt_offset;
  9652. }
  9653. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9654. {
  9655. struct drm_i915_private *dev_priv = dev->dev_private;
  9656. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9657. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9658. struct intel_unpin_work *work;
  9659. WARN_ON(!in_interrupt());
  9660. if (crtc == NULL)
  9661. return;
  9662. spin_lock(&dev->event_lock);
  9663. work = intel_crtc->unpin_work;
  9664. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9665. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9666. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9667. page_flip_completed(intel_crtc);
  9668. work = NULL;
  9669. }
  9670. if (work != NULL &&
  9671. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9672. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9673. spin_unlock(&dev->event_lock);
  9674. }
  9675. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9676. struct drm_framebuffer *fb,
  9677. struct drm_pending_vblank_event *event,
  9678. uint32_t page_flip_flags)
  9679. {
  9680. struct drm_device *dev = crtc->dev;
  9681. struct drm_i915_private *dev_priv = dev->dev_private;
  9682. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9683. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9684. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9685. struct drm_plane *primary = crtc->primary;
  9686. enum pipe pipe = intel_crtc->pipe;
  9687. struct intel_unpin_work *work;
  9688. struct intel_engine_cs *ring;
  9689. bool mmio_flip;
  9690. struct drm_i915_gem_request *request = NULL;
  9691. int ret;
  9692. /*
  9693. * drm_mode_page_flip_ioctl() should already catch this, but double
  9694. * check to be safe. In the future we may enable pageflipping from
  9695. * a disabled primary plane.
  9696. */
  9697. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9698. return -EBUSY;
  9699. /* Can't change pixel format via MI display flips. */
  9700. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9701. return -EINVAL;
  9702. /*
  9703. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9704. * Note that pitch changes could also affect these register.
  9705. */
  9706. if (INTEL_INFO(dev)->gen > 3 &&
  9707. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9708. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9709. return -EINVAL;
  9710. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9711. goto out_hang;
  9712. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9713. if (work == NULL)
  9714. return -ENOMEM;
  9715. work->event = event;
  9716. work->crtc = crtc;
  9717. work->old_fb = old_fb;
  9718. INIT_WORK(&work->work, intel_unpin_work_fn);
  9719. ret = drm_crtc_vblank_get(crtc);
  9720. if (ret)
  9721. goto free_work;
  9722. /* We borrow the event spin lock for protecting unpin_work */
  9723. spin_lock_irq(&dev->event_lock);
  9724. if (intel_crtc->unpin_work) {
  9725. /* Before declaring the flip queue wedged, check if
  9726. * the hardware completed the operation behind our backs.
  9727. */
  9728. if (__intel_pageflip_stall_check(dev, crtc)) {
  9729. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9730. page_flip_completed(intel_crtc);
  9731. } else {
  9732. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9733. spin_unlock_irq(&dev->event_lock);
  9734. drm_crtc_vblank_put(crtc);
  9735. kfree(work);
  9736. return -EBUSY;
  9737. }
  9738. }
  9739. intel_crtc->unpin_work = work;
  9740. spin_unlock_irq(&dev->event_lock);
  9741. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9742. flush_workqueue(dev_priv->wq);
  9743. /* Reference the objects for the scheduled work. */
  9744. drm_framebuffer_reference(work->old_fb);
  9745. drm_gem_object_reference(&obj->base);
  9746. crtc->primary->fb = fb;
  9747. update_state_fb(crtc->primary);
  9748. intel_fbc_pre_update(intel_crtc);
  9749. work->pending_flip_obj = obj;
  9750. ret = i915_mutex_lock_interruptible(dev);
  9751. if (ret)
  9752. goto cleanup;
  9753. atomic_inc(&intel_crtc->unpin_work_count);
  9754. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9755. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9756. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9757. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  9758. ring = &dev_priv->ring[BCS];
  9759. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9760. /* vlv: DISPLAY_FLIP fails to change tiling */
  9761. ring = NULL;
  9762. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9763. ring = &dev_priv->ring[BCS];
  9764. } else if (INTEL_INFO(dev)->gen >= 7) {
  9765. ring = i915_gem_request_get_ring(obj->last_write_req);
  9766. if (ring == NULL || ring->id != RCS)
  9767. ring = &dev_priv->ring[BCS];
  9768. } else {
  9769. ring = &dev_priv->ring[RCS];
  9770. }
  9771. mmio_flip = use_mmio_flip(ring, obj);
  9772. /* When using CS flips, we want to emit semaphores between rings.
  9773. * However, when using mmio flips we will create a task to do the
  9774. * synchronisation, so all we want here is to pin the framebuffer
  9775. * into the display plane and skip any waits.
  9776. */
  9777. if (!mmio_flip) {
  9778. ret = i915_gem_object_sync(obj, ring, &request);
  9779. if (ret)
  9780. goto cleanup_pending;
  9781. }
  9782. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9783. crtc->primary->state);
  9784. if (ret)
  9785. goto cleanup_pending;
  9786. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9787. obj, 0);
  9788. work->gtt_offset += intel_crtc->dspaddr_offset;
  9789. if (mmio_flip) {
  9790. ret = intel_queue_mmio_flip(dev, crtc, obj);
  9791. if (ret)
  9792. goto cleanup_unpin;
  9793. i915_gem_request_assign(&work->flip_queued_req,
  9794. obj->last_write_req);
  9795. } else {
  9796. if (!request) {
  9797. request = i915_gem_request_alloc(ring, NULL);
  9798. if (IS_ERR(request)) {
  9799. ret = PTR_ERR(request);
  9800. goto cleanup_unpin;
  9801. }
  9802. }
  9803. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9804. page_flip_flags);
  9805. if (ret)
  9806. goto cleanup_unpin;
  9807. i915_gem_request_assign(&work->flip_queued_req, request);
  9808. }
  9809. if (request)
  9810. i915_add_request_no_flush(request);
  9811. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9812. work->enable_stall_check = true;
  9813. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9814. to_intel_plane(primary)->frontbuffer_bit);
  9815. mutex_unlock(&dev->struct_mutex);
  9816. intel_frontbuffer_flip_prepare(dev,
  9817. to_intel_plane(primary)->frontbuffer_bit);
  9818. trace_i915_flip_request(intel_crtc->plane, obj);
  9819. return 0;
  9820. cleanup_unpin:
  9821. intel_unpin_fb_obj(fb, crtc->primary->state);
  9822. cleanup_pending:
  9823. if (!IS_ERR_OR_NULL(request))
  9824. i915_gem_request_cancel(request);
  9825. atomic_dec(&intel_crtc->unpin_work_count);
  9826. mutex_unlock(&dev->struct_mutex);
  9827. cleanup:
  9828. crtc->primary->fb = old_fb;
  9829. update_state_fb(crtc->primary);
  9830. drm_gem_object_unreference_unlocked(&obj->base);
  9831. drm_framebuffer_unreference(work->old_fb);
  9832. spin_lock_irq(&dev->event_lock);
  9833. intel_crtc->unpin_work = NULL;
  9834. spin_unlock_irq(&dev->event_lock);
  9835. drm_crtc_vblank_put(crtc);
  9836. free_work:
  9837. kfree(work);
  9838. if (ret == -EIO) {
  9839. struct drm_atomic_state *state;
  9840. struct drm_plane_state *plane_state;
  9841. out_hang:
  9842. state = drm_atomic_state_alloc(dev);
  9843. if (!state)
  9844. return -ENOMEM;
  9845. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9846. retry:
  9847. plane_state = drm_atomic_get_plane_state(state, primary);
  9848. ret = PTR_ERR_OR_ZERO(plane_state);
  9849. if (!ret) {
  9850. drm_atomic_set_fb_for_plane(plane_state, fb);
  9851. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9852. if (!ret)
  9853. ret = drm_atomic_commit(state);
  9854. }
  9855. if (ret == -EDEADLK) {
  9856. drm_modeset_backoff(state->acquire_ctx);
  9857. drm_atomic_state_clear(state);
  9858. goto retry;
  9859. }
  9860. if (ret)
  9861. drm_atomic_state_free(state);
  9862. if (ret == 0 && event) {
  9863. spin_lock_irq(&dev->event_lock);
  9864. drm_send_vblank_event(dev, pipe, event);
  9865. spin_unlock_irq(&dev->event_lock);
  9866. }
  9867. }
  9868. return ret;
  9869. }
  9870. /**
  9871. * intel_wm_need_update - Check whether watermarks need updating
  9872. * @plane: drm plane
  9873. * @state: new plane state
  9874. *
  9875. * Check current plane state versus the new one to determine whether
  9876. * watermarks need to be recalculated.
  9877. *
  9878. * Returns true or false.
  9879. */
  9880. static bool intel_wm_need_update(struct drm_plane *plane,
  9881. struct drm_plane_state *state)
  9882. {
  9883. struct intel_plane_state *new = to_intel_plane_state(state);
  9884. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9885. /* Update watermarks on tiling or size changes. */
  9886. if (new->visible != cur->visible)
  9887. return true;
  9888. if (!cur->base.fb || !new->base.fb)
  9889. return false;
  9890. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  9891. cur->base.rotation != new->base.rotation ||
  9892. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9893. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9894. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9895. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9896. return true;
  9897. return false;
  9898. }
  9899. static bool needs_scaling(struct intel_plane_state *state)
  9900. {
  9901. int src_w = drm_rect_width(&state->src) >> 16;
  9902. int src_h = drm_rect_height(&state->src) >> 16;
  9903. int dst_w = drm_rect_width(&state->dst);
  9904. int dst_h = drm_rect_height(&state->dst);
  9905. return (src_w != dst_w || src_h != dst_h);
  9906. }
  9907. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9908. struct drm_plane_state *plane_state)
  9909. {
  9910. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9911. struct drm_crtc *crtc = crtc_state->crtc;
  9912. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9913. struct drm_plane *plane = plane_state->plane;
  9914. struct drm_device *dev = crtc->dev;
  9915. struct intel_plane_state *old_plane_state =
  9916. to_intel_plane_state(plane->state);
  9917. int idx = intel_crtc->base.base.id, ret;
  9918. int i = drm_plane_index(plane);
  9919. bool mode_changed = needs_modeset(crtc_state);
  9920. bool was_crtc_enabled = crtc->state->active;
  9921. bool is_crtc_enabled = crtc_state->active;
  9922. bool turn_off, turn_on, visible, was_visible;
  9923. struct drm_framebuffer *fb = plane_state->fb;
  9924. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9925. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9926. ret = skl_update_scaler_plane(
  9927. to_intel_crtc_state(crtc_state),
  9928. to_intel_plane_state(plane_state));
  9929. if (ret)
  9930. return ret;
  9931. }
  9932. was_visible = old_plane_state->visible;
  9933. visible = to_intel_plane_state(plane_state)->visible;
  9934. if (!was_crtc_enabled && WARN_ON(was_visible))
  9935. was_visible = false;
  9936. /*
  9937. * Visibility is calculated as if the crtc was on, but
  9938. * after scaler setup everything depends on it being off
  9939. * when the crtc isn't active.
  9940. */
  9941. if (!is_crtc_enabled)
  9942. to_intel_plane_state(plane_state)->visible = visible = false;
  9943. if (!was_visible && !visible)
  9944. return 0;
  9945. turn_off = was_visible && (!visible || mode_changed);
  9946. turn_on = visible && (!was_visible || mode_changed);
  9947. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9948. plane->base.id, fb ? fb->base.id : -1);
  9949. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9950. plane->base.id, was_visible, visible,
  9951. turn_off, turn_on, mode_changed);
  9952. if (turn_on || turn_off) {
  9953. pipe_config->wm_changed = true;
  9954. /* must disable cxsr around plane enable/disable */
  9955. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9956. if (is_crtc_enabled)
  9957. intel_crtc->atomic.wait_vblank = true;
  9958. pipe_config->disable_cxsr = true;
  9959. }
  9960. } else if (intel_wm_need_update(plane, plane_state)) {
  9961. pipe_config->wm_changed = true;
  9962. }
  9963. if (visible || was_visible)
  9964. intel_crtc->atomic.fb_bits |=
  9965. to_intel_plane(plane)->frontbuffer_bit;
  9966. switch (plane->type) {
  9967. case DRM_PLANE_TYPE_PRIMARY:
  9968. intel_crtc->atomic.post_enable_primary = turn_on;
  9969. intel_crtc->atomic.update_fbc = true;
  9970. /*
  9971. * BDW signals flip done immediately if the plane
  9972. * is disabled, even if the plane enable is already
  9973. * armed to occur at the next vblank :(
  9974. */
  9975. if (turn_on && IS_BROADWELL(dev))
  9976. intel_crtc->atomic.wait_vblank = true;
  9977. break;
  9978. case DRM_PLANE_TYPE_CURSOR:
  9979. break;
  9980. case DRM_PLANE_TYPE_OVERLAY:
  9981. /*
  9982. * WaCxSRDisabledForSpriteScaling:ivb
  9983. *
  9984. * cstate->update_wm was already set above, so this flag will
  9985. * take effect when we commit and program watermarks.
  9986. */
  9987. if (IS_IVYBRIDGE(dev) &&
  9988. needs_scaling(to_intel_plane_state(plane_state)) &&
  9989. !needs_scaling(old_plane_state)) {
  9990. to_intel_crtc_state(crtc_state)->disable_lp_wm = true;
  9991. } else if (turn_off && !mode_changed) {
  9992. intel_crtc->atomic.wait_vblank = true;
  9993. intel_crtc->atomic.update_sprite_watermarks |=
  9994. 1 << i;
  9995. }
  9996. break;
  9997. }
  9998. return 0;
  9999. }
  10000. static bool encoders_cloneable(const struct intel_encoder *a,
  10001. const struct intel_encoder *b)
  10002. {
  10003. /* masks could be asymmetric, so check both ways */
  10004. return a == b || (a->cloneable & (1 << b->type) &&
  10005. b->cloneable & (1 << a->type));
  10006. }
  10007. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10008. struct intel_crtc *crtc,
  10009. struct intel_encoder *encoder)
  10010. {
  10011. struct intel_encoder *source_encoder;
  10012. struct drm_connector *connector;
  10013. struct drm_connector_state *connector_state;
  10014. int i;
  10015. for_each_connector_in_state(state, connector, connector_state, i) {
  10016. if (connector_state->crtc != &crtc->base)
  10017. continue;
  10018. source_encoder =
  10019. to_intel_encoder(connector_state->best_encoder);
  10020. if (!encoders_cloneable(encoder, source_encoder))
  10021. return false;
  10022. }
  10023. return true;
  10024. }
  10025. static bool check_encoder_cloning(struct drm_atomic_state *state,
  10026. struct intel_crtc *crtc)
  10027. {
  10028. struct intel_encoder *encoder;
  10029. struct drm_connector *connector;
  10030. struct drm_connector_state *connector_state;
  10031. int i;
  10032. for_each_connector_in_state(state, connector, connector_state, i) {
  10033. if (connector_state->crtc != &crtc->base)
  10034. continue;
  10035. encoder = to_intel_encoder(connector_state->best_encoder);
  10036. if (!check_single_encoder_cloning(state, crtc, encoder))
  10037. return false;
  10038. }
  10039. return true;
  10040. }
  10041. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10042. struct drm_crtc_state *crtc_state)
  10043. {
  10044. struct drm_device *dev = crtc->dev;
  10045. struct drm_i915_private *dev_priv = dev->dev_private;
  10046. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10047. struct intel_crtc_state *pipe_config =
  10048. to_intel_crtc_state(crtc_state);
  10049. struct drm_atomic_state *state = crtc_state->state;
  10050. int ret;
  10051. bool mode_changed = needs_modeset(crtc_state);
  10052. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  10053. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10054. return -EINVAL;
  10055. }
  10056. if (mode_changed && !crtc_state->active)
  10057. pipe_config->wm_changed = true;
  10058. if (mode_changed && crtc_state->enable &&
  10059. dev_priv->display.crtc_compute_clock &&
  10060. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  10061. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10062. pipe_config);
  10063. if (ret)
  10064. return ret;
  10065. }
  10066. ret = 0;
  10067. if (dev_priv->display.compute_pipe_wm) {
  10068. ret = dev_priv->display.compute_pipe_wm(intel_crtc, state);
  10069. if (ret)
  10070. return ret;
  10071. }
  10072. if (INTEL_INFO(dev)->gen >= 9) {
  10073. if (mode_changed)
  10074. ret = skl_update_scaler_crtc(pipe_config);
  10075. if (!ret)
  10076. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10077. pipe_config);
  10078. }
  10079. return ret;
  10080. }
  10081. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10082. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10083. .load_lut = intel_crtc_load_lut,
  10084. .atomic_begin = intel_begin_crtc_commit,
  10085. .atomic_flush = intel_finish_crtc_commit,
  10086. .atomic_check = intel_crtc_atomic_check,
  10087. };
  10088. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10089. {
  10090. struct intel_connector *connector;
  10091. for_each_intel_connector(dev, connector) {
  10092. if (connector->base.encoder) {
  10093. connector->base.state->best_encoder =
  10094. connector->base.encoder;
  10095. connector->base.state->crtc =
  10096. connector->base.encoder->crtc;
  10097. } else {
  10098. connector->base.state->best_encoder = NULL;
  10099. connector->base.state->crtc = NULL;
  10100. }
  10101. }
  10102. }
  10103. static void
  10104. connected_sink_compute_bpp(struct intel_connector *connector,
  10105. struct intel_crtc_state *pipe_config)
  10106. {
  10107. int bpp = pipe_config->pipe_bpp;
  10108. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10109. connector->base.base.id,
  10110. connector->base.name);
  10111. /* Don't use an invalid EDID bpc value */
  10112. if (connector->base.display_info.bpc &&
  10113. connector->base.display_info.bpc * 3 < bpp) {
  10114. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10115. bpp, connector->base.display_info.bpc*3);
  10116. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10117. }
  10118. /* Clamp bpp to default limit on screens without EDID 1.4 */
  10119. if (connector->base.display_info.bpc == 0) {
  10120. int type = connector->base.connector_type;
  10121. int clamp_bpp = 24;
  10122. /* Fall back to 18 bpp when DP sink capability is unknown. */
  10123. if (type == DRM_MODE_CONNECTOR_DisplayPort ||
  10124. type == DRM_MODE_CONNECTOR_eDP)
  10125. clamp_bpp = 18;
  10126. if (bpp > clamp_bpp) {
  10127. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
  10128. bpp, clamp_bpp);
  10129. pipe_config->pipe_bpp = clamp_bpp;
  10130. }
  10131. }
  10132. }
  10133. static int
  10134. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10135. struct intel_crtc_state *pipe_config)
  10136. {
  10137. struct drm_device *dev = crtc->base.dev;
  10138. struct drm_atomic_state *state;
  10139. struct drm_connector *connector;
  10140. struct drm_connector_state *connector_state;
  10141. int bpp, i;
  10142. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  10143. bpp = 10*3;
  10144. else if (INTEL_INFO(dev)->gen >= 5)
  10145. bpp = 12*3;
  10146. else
  10147. bpp = 8*3;
  10148. pipe_config->pipe_bpp = bpp;
  10149. state = pipe_config->base.state;
  10150. /* Clamp display bpp to EDID value */
  10151. for_each_connector_in_state(state, connector, connector_state, i) {
  10152. if (connector_state->crtc != &crtc->base)
  10153. continue;
  10154. connected_sink_compute_bpp(to_intel_connector(connector),
  10155. pipe_config);
  10156. }
  10157. return bpp;
  10158. }
  10159. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10160. {
  10161. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10162. "type: 0x%x flags: 0x%x\n",
  10163. mode->crtc_clock,
  10164. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10165. mode->crtc_hsync_end, mode->crtc_htotal,
  10166. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10167. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10168. }
  10169. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10170. struct intel_crtc_state *pipe_config,
  10171. const char *context)
  10172. {
  10173. struct drm_device *dev = crtc->base.dev;
  10174. struct drm_plane *plane;
  10175. struct intel_plane *intel_plane;
  10176. struct intel_plane_state *state;
  10177. struct drm_framebuffer *fb;
  10178. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  10179. context, pipe_config, pipe_name(crtc->pipe));
  10180. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  10181. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10182. pipe_config->pipe_bpp, pipe_config->dither);
  10183. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10184. pipe_config->has_pch_encoder,
  10185. pipe_config->fdi_lanes,
  10186. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10187. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10188. pipe_config->fdi_m_n.tu);
  10189. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10190. pipe_config->has_dp_encoder,
  10191. pipe_config->lane_count,
  10192. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10193. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10194. pipe_config->dp_m_n.tu);
  10195. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10196. pipe_config->has_dp_encoder,
  10197. pipe_config->lane_count,
  10198. pipe_config->dp_m2_n2.gmch_m,
  10199. pipe_config->dp_m2_n2.gmch_n,
  10200. pipe_config->dp_m2_n2.link_m,
  10201. pipe_config->dp_m2_n2.link_n,
  10202. pipe_config->dp_m2_n2.tu);
  10203. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10204. pipe_config->has_audio,
  10205. pipe_config->has_infoframe);
  10206. DRM_DEBUG_KMS("requested mode:\n");
  10207. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10208. DRM_DEBUG_KMS("adjusted mode:\n");
  10209. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10210. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10211. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10212. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10213. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10214. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10215. crtc->num_scalers,
  10216. pipe_config->scaler_state.scaler_users,
  10217. pipe_config->scaler_state.scaler_id);
  10218. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10219. pipe_config->gmch_pfit.control,
  10220. pipe_config->gmch_pfit.pgm_ratios,
  10221. pipe_config->gmch_pfit.lvds_border_bits);
  10222. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10223. pipe_config->pch_pfit.pos,
  10224. pipe_config->pch_pfit.size,
  10225. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10226. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10227. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10228. if (IS_BROXTON(dev)) {
  10229. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10230. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10231. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10232. pipe_config->ddi_pll_sel,
  10233. pipe_config->dpll_hw_state.ebb0,
  10234. pipe_config->dpll_hw_state.ebb4,
  10235. pipe_config->dpll_hw_state.pll0,
  10236. pipe_config->dpll_hw_state.pll1,
  10237. pipe_config->dpll_hw_state.pll2,
  10238. pipe_config->dpll_hw_state.pll3,
  10239. pipe_config->dpll_hw_state.pll6,
  10240. pipe_config->dpll_hw_state.pll8,
  10241. pipe_config->dpll_hw_state.pll9,
  10242. pipe_config->dpll_hw_state.pll10,
  10243. pipe_config->dpll_hw_state.pcsdw12);
  10244. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10245. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10246. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10247. pipe_config->ddi_pll_sel,
  10248. pipe_config->dpll_hw_state.ctrl1,
  10249. pipe_config->dpll_hw_state.cfgcr1,
  10250. pipe_config->dpll_hw_state.cfgcr2);
  10251. } else if (HAS_DDI(dev)) {
  10252. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10253. pipe_config->ddi_pll_sel,
  10254. pipe_config->dpll_hw_state.wrpll,
  10255. pipe_config->dpll_hw_state.spll);
  10256. } else {
  10257. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10258. "fp0: 0x%x, fp1: 0x%x\n",
  10259. pipe_config->dpll_hw_state.dpll,
  10260. pipe_config->dpll_hw_state.dpll_md,
  10261. pipe_config->dpll_hw_state.fp0,
  10262. pipe_config->dpll_hw_state.fp1);
  10263. }
  10264. DRM_DEBUG_KMS("planes on this crtc\n");
  10265. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10266. intel_plane = to_intel_plane(plane);
  10267. if (intel_plane->pipe != crtc->pipe)
  10268. continue;
  10269. state = to_intel_plane_state(plane->state);
  10270. fb = state->base.fb;
  10271. if (!fb) {
  10272. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10273. "disabled, scaler_id = %d\n",
  10274. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10275. plane->base.id, intel_plane->pipe,
  10276. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10277. drm_plane_index(plane), state->scaler_id);
  10278. continue;
  10279. }
  10280. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10281. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10282. plane->base.id, intel_plane->pipe,
  10283. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10284. drm_plane_index(plane));
  10285. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10286. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10287. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10288. state->scaler_id,
  10289. state->src.x1 >> 16, state->src.y1 >> 16,
  10290. drm_rect_width(&state->src) >> 16,
  10291. drm_rect_height(&state->src) >> 16,
  10292. state->dst.x1, state->dst.y1,
  10293. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10294. }
  10295. }
  10296. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10297. {
  10298. struct drm_device *dev = state->dev;
  10299. struct drm_connector *connector;
  10300. unsigned int used_ports = 0;
  10301. /*
  10302. * Walk the connector list instead of the encoder
  10303. * list to detect the problem on ddi platforms
  10304. * where there's just one encoder per digital port.
  10305. */
  10306. drm_for_each_connector(connector, dev) {
  10307. struct drm_connector_state *connector_state;
  10308. struct intel_encoder *encoder;
  10309. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10310. if (!connector_state)
  10311. connector_state = connector->state;
  10312. if (!connector_state->best_encoder)
  10313. continue;
  10314. encoder = to_intel_encoder(connector_state->best_encoder);
  10315. WARN_ON(!connector_state->crtc);
  10316. switch (encoder->type) {
  10317. unsigned int port_mask;
  10318. case INTEL_OUTPUT_UNKNOWN:
  10319. if (WARN_ON(!HAS_DDI(dev)))
  10320. break;
  10321. case INTEL_OUTPUT_DISPLAYPORT:
  10322. case INTEL_OUTPUT_HDMI:
  10323. case INTEL_OUTPUT_EDP:
  10324. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10325. /* the same port mustn't appear more than once */
  10326. if (used_ports & port_mask)
  10327. return false;
  10328. used_ports |= port_mask;
  10329. default:
  10330. break;
  10331. }
  10332. }
  10333. return true;
  10334. }
  10335. static void
  10336. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10337. {
  10338. struct drm_crtc_state tmp_state;
  10339. struct intel_crtc_scaler_state scaler_state;
  10340. struct intel_dpll_hw_state dpll_hw_state;
  10341. enum intel_dpll_id shared_dpll;
  10342. uint32_t ddi_pll_sel;
  10343. bool force_thru;
  10344. /* FIXME: before the switch to atomic started, a new pipe_config was
  10345. * kzalloc'd. Code that depends on any field being zero should be
  10346. * fixed, so that the crtc_state can be safely duplicated. For now,
  10347. * only fields that are know to not cause problems are preserved. */
  10348. tmp_state = crtc_state->base;
  10349. scaler_state = crtc_state->scaler_state;
  10350. shared_dpll = crtc_state->shared_dpll;
  10351. dpll_hw_state = crtc_state->dpll_hw_state;
  10352. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10353. force_thru = crtc_state->pch_pfit.force_thru;
  10354. memset(crtc_state, 0, sizeof *crtc_state);
  10355. crtc_state->base = tmp_state;
  10356. crtc_state->scaler_state = scaler_state;
  10357. crtc_state->shared_dpll = shared_dpll;
  10358. crtc_state->dpll_hw_state = dpll_hw_state;
  10359. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10360. crtc_state->pch_pfit.force_thru = force_thru;
  10361. }
  10362. static int
  10363. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10364. struct intel_crtc_state *pipe_config)
  10365. {
  10366. struct drm_atomic_state *state = pipe_config->base.state;
  10367. struct intel_encoder *encoder;
  10368. struct drm_connector *connector;
  10369. struct drm_connector_state *connector_state;
  10370. int base_bpp, ret = -EINVAL;
  10371. int i;
  10372. bool retry = true;
  10373. clear_intel_crtc_state(pipe_config);
  10374. pipe_config->cpu_transcoder =
  10375. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10376. /*
  10377. * Sanitize sync polarity flags based on requested ones. If neither
  10378. * positive or negative polarity is requested, treat this as meaning
  10379. * negative polarity.
  10380. */
  10381. if (!(pipe_config->base.adjusted_mode.flags &
  10382. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10383. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10384. if (!(pipe_config->base.adjusted_mode.flags &
  10385. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10386. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10387. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10388. pipe_config);
  10389. if (base_bpp < 0)
  10390. goto fail;
  10391. /*
  10392. * Determine the real pipe dimensions. Note that stereo modes can
  10393. * increase the actual pipe size due to the frame doubling and
  10394. * insertion of additional space for blanks between the frame. This
  10395. * is stored in the crtc timings. We use the requested mode to do this
  10396. * computation to clearly distinguish it from the adjusted mode, which
  10397. * can be changed by the connectors in the below retry loop.
  10398. */
  10399. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10400. &pipe_config->pipe_src_w,
  10401. &pipe_config->pipe_src_h);
  10402. encoder_retry:
  10403. /* Ensure the port clock defaults are reset when retrying. */
  10404. pipe_config->port_clock = 0;
  10405. pipe_config->pixel_multiplier = 1;
  10406. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10407. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10408. CRTC_STEREO_DOUBLE);
  10409. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10410. * adjust it according to limitations or connector properties, and also
  10411. * a chance to reject the mode entirely.
  10412. */
  10413. for_each_connector_in_state(state, connector, connector_state, i) {
  10414. if (connector_state->crtc != crtc)
  10415. continue;
  10416. encoder = to_intel_encoder(connector_state->best_encoder);
  10417. if (!(encoder->compute_config(encoder, pipe_config))) {
  10418. DRM_DEBUG_KMS("Encoder config failure\n");
  10419. goto fail;
  10420. }
  10421. }
  10422. /* Set default port clock if not overwritten by the encoder. Needs to be
  10423. * done afterwards in case the encoder adjusts the mode. */
  10424. if (!pipe_config->port_clock)
  10425. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10426. * pipe_config->pixel_multiplier;
  10427. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10428. if (ret < 0) {
  10429. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10430. goto fail;
  10431. }
  10432. if (ret == RETRY) {
  10433. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10434. ret = -EINVAL;
  10435. goto fail;
  10436. }
  10437. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10438. retry = false;
  10439. goto encoder_retry;
  10440. }
  10441. /* Dithering seems to not pass-through bits correctly when it should, so
  10442. * only enable it on 6bpc panels. */
  10443. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10444. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10445. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10446. fail:
  10447. return ret;
  10448. }
  10449. static void
  10450. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10451. {
  10452. struct drm_crtc *crtc;
  10453. struct drm_crtc_state *crtc_state;
  10454. int i;
  10455. /* Double check state. */
  10456. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10457. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10458. /* Update hwmode for vblank functions */
  10459. if (crtc->state->active)
  10460. crtc->hwmode = crtc->state->adjusted_mode;
  10461. else
  10462. crtc->hwmode.crtc_clock = 0;
  10463. /*
  10464. * Update legacy state to satisfy fbc code. This can
  10465. * be removed when fbc uses the atomic state.
  10466. */
  10467. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10468. struct drm_plane_state *plane_state = crtc->primary->state;
  10469. crtc->primary->fb = plane_state->fb;
  10470. crtc->x = plane_state->src_x >> 16;
  10471. crtc->y = plane_state->src_y >> 16;
  10472. }
  10473. }
  10474. }
  10475. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10476. {
  10477. int diff;
  10478. if (clock1 == clock2)
  10479. return true;
  10480. if (!clock1 || !clock2)
  10481. return false;
  10482. diff = abs(clock1 - clock2);
  10483. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10484. return true;
  10485. return false;
  10486. }
  10487. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10488. list_for_each_entry((intel_crtc), \
  10489. &(dev)->mode_config.crtc_list, \
  10490. base.head) \
  10491. for_each_if (mask & (1 <<(intel_crtc)->pipe))
  10492. static bool
  10493. intel_compare_m_n(unsigned int m, unsigned int n,
  10494. unsigned int m2, unsigned int n2,
  10495. bool exact)
  10496. {
  10497. if (m == m2 && n == n2)
  10498. return true;
  10499. if (exact || !m || !n || !m2 || !n2)
  10500. return false;
  10501. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10502. if (n > n2) {
  10503. while (n > n2) {
  10504. m2 <<= 1;
  10505. n2 <<= 1;
  10506. }
  10507. } else if (n < n2) {
  10508. while (n < n2) {
  10509. m <<= 1;
  10510. n <<= 1;
  10511. }
  10512. }
  10513. if (n != n2)
  10514. return false;
  10515. return intel_fuzzy_clock_check(m, m2);
  10516. }
  10517. static bool
  10518. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10519. struct intel_link_m_n *m2_n2,
  10520. bool adjust)
  10521. {
  10522. if (m_n->tu == m2_n2->tu &&
  10523. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10524. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10525. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10526. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10527. if (adjust)
  10528. *m2_n2 = *m_n;
  10529. return true;
  10530. }
  10531. return false;
  10532. }
  10533. static bool
  10534. intel_pipe_config_compare(struct drm_device *dev,
  10535. struct intel_crtc_state *current_config,
  10536. struct intel_crtc_state *pipe_config,
  10537. bool adjust)
  10538. {
  10539. bool ret = true;
  10540. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10541. do { \
  10542. if (!adjust) \
  10543. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10544. else \
  10545. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10546. } while (0)
  10547. #define PIPE_CONF_CHECK_X(name) \
  10548. if (current_config->name != pipe_config->name) { \
  10549. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10550. "(expected 0x%08x, found 0x%08x)\n", \
  10551. current_config->name, \
  10552. pipe_config->name); \
  10553. ret = false; \
  10554. }
  10555. #define PIPE_CONF_CHECK_I(name) \
  10556. if (current_config->name != pipe_config->name) { \
  10557. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10558. "(expected %i, found %i)\n", \
  10559. current_config->name, \
  10560. pipe_config->name); \
  10561. ret = false; \
  10562. }
  10563. #define PIPE_CONF_CHECK_M_N(name) \
  10564. if (!intel_compare_link_m_n(&current_config->name, \
  10565. &pipe_config->name,\
  10566. adjust)) { \
  10567. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10568. "(expected tu %i gmch %i/%i link %i/%i, " \
  10569. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10570. current_config->name.tu, \
  10571. current_config->name.gmch_m, \
  10572. current_config->name.gmch_n, \
  10573. current_config->name.link_m, \
  10574. current_config->name.link_n, \
  10575. pipe_config->name.tu, \
  10576. pipe_config->name.gmch_m, \
  10577. pipe_config->name.gmch_n, \
  10578. pipe_config->name.link_m, \
  10579. pipe_config->name.link_n); \
  10580. ret = false; \
  10581. }
  10582. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10583. if (!intel_compare_link_m_n(&current_config->name, \
  10584. &pipe_config->name, adjust) && \
  10585. !intel_compare_link_m_n(&current_config->alt_name, \
  10586. &pipe_config->name, adjust)) { \
  10587. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10588. "(expected tu %i gmch %i/%i link %i/%i, " \
  10589. "or tu %i gmch %i/%i link %i/%i, " \
  10590. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10591. current_config->name.tu, \
  10592. current_config->name.gmch_m, \
  10593. current_config->name.gmch_n, \
  10594. current_config->name.link_m, \
  10595. current_config->name.link_n, \
  10596. current_config->alt_name.tu, \
  10597. current_config->alt_name.gmch_m, \
  10598. current_config->alt_name.gmch_n, \
  10599. current_config->alt_name.link_m, \
  10600. current_config->alt_name.link_n, \
  10601. pipe_config->name.tu, \
  10602. pipe_config->name.gmch_m, \
  10603. pipe_config->name.gmch_n, \
  10604. pipe_config->name.link_m, \
  10605. pipe_config->name.link_n); \
  10606. ret = false; \
  10607. }
  10608. /* This is required for BDW+ where there is only one set of registers for
  10609. * switching between high and low RR.
  10610. * This macro can be used whenever a comparison has to be made between one
  10611. * hw state and multiple sw state variables.
  10612. */
  10613. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10614. if ((current_config->name != pipe_config->name) && \
  10615. (current_config->alt_name != pipe_config->name)) { \
  10616. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10617. "(expected %i or %i, found %i)\n", \
  10618. current_config->name, \
  10619. current_config->alt_name, \
  10620. pipe_config->name); \
  10621. ret = false; \
  10622. }
  10623. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10624. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10625. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10626. "(expected %i, found %i)\n", \
  10627. current_config->name & (mask), \
  10628. pipe_config->name & (mask)); \
  10629. ret = false; \
  10630. }
  10631. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10632. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10633. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10634. "(expected %i, found %i)\n", \
  10635. current_config->name, \
  10636. pipe_config->name); \
  10637. ret = false; \
  10638. }
  10639. #define PIPE_CONF_QUIRK(quirk) \
  10640. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10641. PIPE_CONF_CHECK_I(cpu_transcoder);
  10642. PIPE_CONF_CHECK_I(has_pch_encoder);
  10643. PIPE_CONF_CHECK_I(fdi_lanes);
  10644. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10645. PIPE_CONF_CHECK_I(has_dp_encoder);
  10646. PIPE_CONF_CHECK_I(lane_count);
  10647. if (INTEL_INFO(dev)->gen < 8) {
  10648. PIPE_CONF_CHECK_M_N(dp_m_n);
  10649. if (current_config->has_drrs)
  10650. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10651. } else
  10652. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10653. PIPE_CONF_CHECK_I(has_dsi_encoder);
  10654. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10655. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10656. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10657. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10658. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10659. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10660. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10661. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10662. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10663. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10664. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10665. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10666. PIPE_CONF_CHECK_I(pixel_multiplier);
  10667. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10668. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10669. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  10670. PIPE_CONF_CHECK_I(limited_color_range);
  10671. PIPE_CONF_CHECK_I(has_infoframe);
  10672. PIPE_CONF_CHECK_I(has_audio);
  10673. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10674. DRM_MODE_FLAG_INTERLACE);
  10675. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10676. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10677. DRM_MODE_FLAG_PHSYNC);
  10678. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10679. DRM_MODE_FLAG_NHSYNC);
  10680. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10681. DRM_MODE_FLAG_PVSYNC);
  10682. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10683. DRM_MODE_FLAG_NVSYNC);
  10684. }
  10685. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10686. /* pfit ratios are autocomputed by the hw on gen4+ */
  10687. if (INTEL_INFO(dev)->gen < 4)
  10688. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10689. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10690. if (!adjust) {
  10691. PIPE_CONF_CHECK_I(pipe_src_w);
  10692. PIPE_CONF_CHECK_I(pipe_src_h);
  10693. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10694. if (current_config->pch_pfit.enabled) {
  10695. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10696. PIPE_CONF_CHECK_X(pch_pfit.size);
  10697. }
  10698. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10699. }
  10700. /* BDW+ don't expose a synchronous way to read the state */
  10701. if (IS_HASWELL(dev))
  10702. PIPE_CONF_CHECK_I(ips_enabled);
  10703. PIPE_CONF_CHECK_I(double_wide);
  10704. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10705. PIPE_CONF_CHECK_I(shared_dpll);
  10706. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10707. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10708. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10709. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10710. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10711. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10712. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10713. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10714. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10715. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10716. PIPE_CONF_CHECK_I(pipe_bpp);
  10717. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10718. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10719. #undef PIPE_CONF_CHECK_X
  10720. #undef PIPE_CONF_CHECK_I
  10721. #undef PIPE_CONF_CHECK_I_ALT
  10722. #undef PIPE_CONF_CHECK_FLAGS
  10723. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10724. #undef PIPE_CONF_QUIRK
  10725. #undef INTEL_ERR_OR_DBG_KMS
  10726. return ret;
  10727. }
  10728. static void check_wm_state(struct drm_device *dev)
  10729. {
  10730. struct drm_i915_private *dev_priv = dev->dev_private;
  10731. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10732. struct intel_crtc *intel_crtc;
  10733. int plane;
  10734. if (INTEL_INFO(dev)->gen < 9)
  10735. return;
  10736. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10737. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10738. for_each_intel_crtc(dev, intel_crtc) {
  10739. struct skl_ddb_entry *hw_entry, *sw_entry;
  10740. const enum pipe pipe = intel_crtc->pipe;
  10741. if (!intel_crtc->active)
  10742. continue;
  10743. /* planes */
  10744. for_each_plane(dev_priv, pipe, plane) {
  10745. hw_entry = &hw_ddb.plane[pipe][plane];
  10746. sw_entry = &sw_ddb->plane[pipe][plane];
  10747. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10748. continue;
  10749. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10750. "(expected (%u,%u), found (%u,%u))\n",
  10751. pipe_name(pipe), plane + 1,
  10752. sw_entry->start, sw_entry->end,
  10753. hw_entry->start, hw_entry->end);
  10754. }
  10755. /* cursor */
  10756. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10757. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10758. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10759. continue;
  10760. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10761. "(expected (%u,%u), found (%u,%u))\n",
  10762. pipe_name(pipe),
  10763. sw_entry->start, sw_entry->end,
  10764. hw_entry->start, hw_entry->end);
  10765. }
  10766. }
  10767. static void
  10768. check_connector_state(struct drm_device *dev,
  10769. struct drm_atomic_state *old_state)
  10770. {
  10771. struct drm_connector_state *old_conn_state;
  10772. struct drm_connector *connector;
  10773. int i;
  10774. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  10775. struct drm_encoder *encoder = connector->encoder;
  10776. struct drm_connector_state *state = connector->state;
  10777. /* This also checks the encoder/connector hw state with the
  10778. * ->get_hw_state callbacks. */
  10779. intel_connector_check_state(to_intel_connector(connector));
  10780. I915_STATE_WARN(state->best_encoder != encoder,
  10781. "connector's atomic encoder doesn't match legacy encoder\n");
  10782. }
  10783. }
  10784. static void
  10785. check_encoder_state(struct drm_device *dev)
  10786. {
  10787. struct intel_encoder *encoder;
  10788. struct intel_connector *connector;
  10789. for_each_intel_encoder(dev, encoder) {
  10790. bool enabled = false;
  10791. enum pipe pipe;
  10792. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10793. encoder->base.base.id,
  10794. encoder->base.name);
  10795. for_each_intel_connector(dev, connector) {
  10796. if (connector->base.state->best_encoder != &encoder->base)
  10797. continue;
  10798. enabled = true;
  10799. I915_STATE_WARN(connector->base.state->crtc !=
  10800. encoder->base.crtc,
  10801. "connector's crtc doesn't match encoder crtc\n");
  10802. }
  10803. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10804. "encoder's enabled state mismatch "
  10805. "(expected %i, found %i)\n",
  10806. !!encoder->base.crtc, enabled);
  10807. if (!encoder->base.crtc) {
  10808. bool active;
  10809. active = encoder->get_hw_state(encoder, &pipe);
  10810. I915_STATE_WARN(active,
  10811. "encoder detached but still enabled on pipe %c.\n",
  10812. pipe_name(pipe));
  10813. }
  10814. }
  10815. }
  10816. static void
  10817. check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
  10818. {
  10819. struct drm_i915_private *dev_priv = dev->dev_private;
  10820. struct intel_encoder *encoder;
  10821. struct drm_crtc_state *old_crtc_state;
  10822. struct drm_crtc *crtc;
  10823. int i;
  10824. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  10825. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10826. struct intel_crtc_state *pipe_config, *sw_config;
  10827. bool active;
  10828. if (!needs_modeset(crtc->state) &&
  10829. !to_intel_crtc_state(crtc->state)->update_pipe)
  10830. continue;
  10831. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10832. pipe_config = to_intel_crtc_state(old_crtc_state);
  10833. memset(pipe_config, 0, sizeof(*pipe_config));
  10834. pipe_config->base.crtc = crtc;
  10835. pipe_config->base.state = old_state;
  10836. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10837. crtc->base.id);
  10838. active = dev_priv->display.get_pipe_config(intel_crtc,
  10839. pipe_config);
  10840. /* hw state is inconsistent with the pipe quirk */
  10841. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10842. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10843. active = crtc->state->active;
  10844. I915_STATE_WARN(crtc->state->active != active,
  10845. "crtc active state doesn't match with hw state "
  10846. "(expected %i, found %i)\n", crtc->state->active, active);
  10847. I915_STATE_WARN(intel_crtc->active != crtc->state->active,
  10848. "transitional active state does not match atomic hw state "
  10849. "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
  10850. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10851. enum pipe pipe;
  10852. active = encoder->get_hw_state(encoder, &pipe);
  10853. I915_STATE_WARN(active != crtc->state->active,
  10854. "[ENCODER:%i] active %i with crtc active %i\n",
  10855. encoder->base.base.id, active, crtc->state->active);
  10856. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10857. "Encoder connected to wrong pipe %c\n",
  10858. pipe_name(pipe));
  10859. if (active)
  10860. encoder->get_config(encoder, pipe_config);
  10861. }
  10862. if (!crtc->state->active)
  10863. continue;
  10864. sw_config = to_intel_crtc_state(crtc->state);
  10865. if (!intel_pipe_config_compare(dev, sw_config,
  10866. pipe_config, false)) {
  10867. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10868. intel_dump_pipe_config(intel_crtc, pipe_config,
  10869. "[hw state]");
  10870. intel_dump_pipe_config(intel_crtc, sw_config,
  10871. "[sw state]");
  10872. }
  10873. }
  10874. }
  10875. static void
  10876. check_shared_dpll_state(struct drm_device *dev)
  10877. {
  10878. struct drm_i915_private *dev_priv = dev->dev_private;
  10879. struct intel_crtc *crtc;
  10880. struct intel_dpll_hw_state dpll_hw_state;
  10881. int i;
  10882. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10883. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10884. int enabled_crtcs = 0, active_crtcs = 0;
  10885. bool active;
  10886. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10887. DRM_DEBUG_KMS("%s\n", pll->name);
  10888. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10889. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10890. "more active pll users than references: %i vs %i\n",
  10891. pll->active, hweight32(pll->config.crtc_mask));
  10892. I915_STATE_WARN(pll->active && !pll->on,
  10893. "pll in active use but not on in sw tracking\n");
  10894. I915_STATE_WARN(pll->on && !pll->active,
  10895. "pll in on but not on in use in sw tracking\n");
  10896. I915_STATE_WARN(pll->on != active,
  10897. "pll on state mismatch (expected %i, found %i)\n",
  10898. pll->on, active);
  10899. for_each_intel_crtc(dev, crtc) {
  10900. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10901. enabled_crtcs++;
  10902. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10903. active_crtcs++;
  10904. }
  10905. I915_STATE_WARN(pll->active != active_crtcs,
  10906. "pll active crtcs mismatch (expected %i, found %i)\n",
  10907. pll->active, active_crtcs);
  10908. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10909. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10910. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10911. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10912. sizeof(dpll_hw_state)),
  10913. "pll hw state mismatch\n");
  10914. }
  10915. }
  10916. static void
  10917. intel_modeset_check_state(struct drm_device *dev,
  10918. struct drm_atomic_state *old_state)
  10919. {
  10920. check_wm_state(dev);
  10921. check_connector_state(dev, old_state);
  10922. check_encoder_state(dev);
  10923. check_crtc_state(dev, old_state);
  10924. check_shared_dpll_state(dev);
  10925. }
  10926. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10927. int dotclock)
  10928. {
  10929. /*
  10930. * FDI already provided one idea for the dotclock.
  10931. * Yell if the encoder disagrees.
  10932. */
  10933. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10934. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10935. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10936. }
  10937. static void update_scanline_offset(struct intel_crtc *crtc)
  10938. {
  10939. struct drm_device *dev = crtc->base.dev;
  10940. /*
  10941. * The scanline counter increments at the leading edge of hsync.
  10942. *
  10943. * On most platforms it starts counting from vtotal-1 on the
  10944. * first active line. That means the scanline counter value is
  10945. * always one less than what we would expect. Ie. just after
  10946. * start of vblank, which also occurs at start of hsync (on the
  10947. * last active line), the scanline counter will read vblank_start-1.
  10948. *
  10949. * On gen2 the scanline counter starts counting from 1 instead
  10950. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10951. * to keep the value positive), instead of adding one.
  10952. *
  10953. * On HSW+ the behaviour of the scanline counter depends on the output
  10954. * type. For DP ports it behaves like most other platforms, but on HDMI
  10955. * there's an extra 1 line difference. So we need to add two instead of
  10956. * one to the value.
  10957. */
  10958. if (IS_GEN2(dev)) {
  10959. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10960. int vtotal;
  10961. vtotal = adjusted_mode->crtc_vtotal;
  10962. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10963. vtotal /= 2;
  10964. crtc->scanline_offset = vtotal - 1;
  10965. } else if (HAS_DDI(dev) &&
  10966. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10967. crtc->scanline_offset = 2;
  10968. } else
  10969. crtc->scanline_offset = 1;
  10970. }
  10971. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10972. {
  10973. struct drm_device *dev = state->dev;
  10974. struct drm_i915_private *dev_priv = to_i915(dev);
  10975. struct intel_shared_dpll_config *shared_dpll = NULL;
  10976. struct drm_crtc *crtc;
  10977. struct drm_crtc_state *crtc_state;
  10978. int i;
  10979. if (!dev_priv->display.crtc_compute_clock)
  10980. return;
  10981. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10982. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10983. int old_dpll = to_intel_crtc_state(crtc->state)->shared_dpll;
  10984. if (!needs_modeset(crtc_state))
  10985. continue;
  10986. to_intel_crtc_state(crtc_state)->shared_dpll = DPLL_ID_PRIVATE;
  10987. if (old_dpll == DPLL_ID_PRIVATE)
  10988. continue;
  10989. if (!shared_dpll)
  10990. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10991. shared_dpll[old_dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10992. }
  10993. }
  10994. /*
  10995. * This implements the workaround described in the "notes" section of the mode
  10996. * set sequence documentation. When going from no pipes or single pipe to
  10997. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10998. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10999. */
  11000. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11001. {
  11002. struct drm_crtc_state *crtc_state;
  11003. struct intel_crtc *intel_crtc;
  11004. struct drm_crtc *crtc;
  11005. struct intel_crtc_state *first_crtc_state = NULL;
  11006. struct intel_crtc_state *other_crtc_state = NULL;
  11007. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11008. int i;
  11009. /* look at all crtc's that are going to be enabled in during modeset */
  11010. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11011. intel_crtc = to_intel_crtc(crtc);
  11012. if (!crtc_state->active || !needs_modeset(crtc_state))
  11013. continue;
  11014. if (first_crtc_state) {
  11015. other_crtc_state = to_intel_crtc_state(crtc_state);
  11016. break;
  11017. } else {
  11018. first_crtc_state = to_intel_crtc_state(crtc_state);
  11019. first_pipe = intel_crtc->pipe;
  11020. }
  11021. }
  11022. /* No workaround needed? */
  11023. if (!first_crtc_state)
  11024. return 0;
  11025. /* w/a possibly needed, check how many crtc's are already enabled. */
  11026. for_each_intel_crtc(state->dev, intel_crtc) {
  11027. struct intel_crtc_state *pipe_config;
  11028. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11029. if (IS_ERR(pipe_config))
  11030. return PTR_ERR(pipe_config);
  11031. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11032. if (!pipe_config->base.active ||
  11033. needs_modeset(&pipe_config->base))
  11034. continue;
  11035. /* 2 or more enabled crtcs means no need for w/a */
  11036. if (enabled_pipe != INVALID_PIPE)
  11037. return 0;
  11038. enabled_pipe = intel_crtc->pipe;
  11039. }
  11040. if (enabled_pipe != INVALID_PIPE)
  11041. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11042. else if (other_crtc_state)
  11043. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11044. return 0;
  11045. }
  11046. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11047. {
  11048. struct drm_crtc *crtc;
  11049. struct drm_crtc_state *crtc_state;
  11050. int ret = 0;
  11051. /* add all active pipes to the state */
  11052. for_each_crtc(state->dev, crtc) {
  11053. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11054. if (IS_ERR(crtc_state))
  11055. return PTR_ERR(crtc_state);
  11056. if (!crtc_state->active || needs_modeset(crtc_state))
  11057. continue;
  11058. crtc_state->mode_changed = true;
  11059. ret = drm_atomic_add_affected_connectors(state, crtc);
  11060. if (ret)
  11061. break;
  11062. ret = drm_atomic_add_affected_planes(state, crtc);
  11063. if (ret)
  11064. break;
  11065. }
  11066. return ret;
  11067. }
  11068. static int intel_modeset_checks(struct drm_atomic_state *state)
  11069. {
  11070. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11071. struct drm_i915_private *dev_priv = state->dev->dev_private;
  11072. struct drm_crtc *crtc;
  11073. struct drm_crtc_state *crtc_state;
  11074. int ret = 0, i;
  11075. if (!check_digital_port_conflicts(state)) {
  11076. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11077. return -EINVAL;
  11078. }
  11079. intel_state->modeset = true;
  11080. intel_state->active_crtcs = dev_priv->active_crtcs;
  11081. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11082. if (crtc_state->active)
  11083. intel_state->active_crtcs |= 1 << i;
  11084. else
  11085. intel_state->active_crtcs &= ~(1 << i);
  11086. }
  11087. /*
  11088. * See if the config requires any additional preparation, e.g.
  11089. * to adjust global state with pipes off. We need to do this
  11090. * here so we can get the modeset_pipe updated config for the new
  11091. * mode set on this crtc. For other crtcs we need to use the
  11092. * adjusted_mode bits in the crtc directly.
  11093. */
  11094. if (dev_priv->display.modeset_calc_cdclk) {
  11095. ret = dev_priv->display.modeset_calc_cdclk(state);
  11096. if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
  11097. ret = intel_modeset_all_pipes(state);
  11098. if (ret < 0)
  11099. return ret;
  11100. } else
  11101. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11102. intel_modeset_clear_plls(state);
  11103. if (IS_HASWELL(dev_priv))
  11104. return haswell_mode_set_planes_workaround(state);
  11105. return 0;
  11106. }
  11107. /*
  11108. * Handle calculation of various watermark data at the end of the atomic check
  11109. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11110. * handlers to ensure that all derived state has been updated.
  11111. */
  11112. static void calc_watermark_data(struct drm_atomic_state *state)
  11113. {
  11114. struct drm_device *dev = state->dev;
  11115. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11116. struct drm_crtc *crtc;
  11117. struct drm_crtc_state *cstate;
  11118. struct drm_plane *plane;
  11119. struct drm_plane_state *pstate;
  11120. /*
  11121. * Calculate watermark configuration details now that derived
  11122. * plane/crtc state is all properly updated.
  11123. */
  11124. drm_for_each_crtc(crtc, dev) {
  11125. cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
  11126. crtc->state;
  11127. if (cstate->active)
  11128. intel_state->wm_config.num_pipes_active++;
  11129. }
  11130. drm_for_each_legacy_plane(plane, dev) {
  11131. pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
  11132. plane->state;
  11133. if (!to_intel_plane_state(pstate)->visible)
  11134. continue;
  11135. intel_state->wm_config.sprites_enabled = true;
  11136. if (pstate->crtc_w != pstate->src_w >> 16 ||
  11137. pstate->crtc_h != pstate->src_h >> 16)
  11138. intel_state->wm_config.sprites_scaled = true;
  11139. }
  11140. }
  11141. /**
  11142. * intel_atomic_check - validate state object
  11143. * @dev: drm device
  11144. * @state: state to validate
  11145. */
  11146. static int intel_atomic_check(struct drm_device *dev,
  11147. struct drm_atomic_state *state)
  11148. {
  11149. struct drm_i915_private *dev_priv = to_i915(dev);
  11150. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11151. struct drm_crtc *crtc;
  11152. struct drm_crtc_state *crtc_state;
  11153. int ret, i;
  11154. bool any_ms = false;
  11155. ret = drm_atomic_helper_check_modeset(dev, state);
  11156. if (ret)
  11157. return ret;
  11158. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11159. struct intel_crtc_state *pipe_config =
  11160. to_intel_crtc_state(crtc_state);
  11161. memset(&to_intel_crtc(crtc)->atomic, 0,
  11162. sizeof(struct intel_crtc_atomic_commit));
  11163. /* Catch I915_MODE_FLAG_INHERITED */
  11164. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11165. crtc_state->mode_changed = true;
  11166. if (!crtc_state->enable) {
  11167. if (needs_modeset(crtc_state))
  11168. any_ms = true;
  11169. continue;
  11170. }
  11171. if (!needs_modeset(crtc_state))
  11172. continue;
  11173. /* FIXME: For only active_changed we shouldn't need to do any
  11174. * state recomputation at all. */
  11175. ret = drm_atomic_add_affected_connectors(state, crtc);
  11176. if (ret)
  11177. return ret;
  11178. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11179. if (ret)
  11180. return ret;
  11181. if (i915.fastboot &&
  11182. intel_pipe_config_compare(dev,
  11183. to_intel_crtc_state(crtc->state),
  11184. pipe_config, true)) {
  11185. crtc_state->mode_changed = false;
  11186. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11187. }
  11188. if (needs_modeset(crtc_state)) {
  11189. any_ms = true;
  11190. ret = drm_atomic_add_affected_planes(state, crtc);
  11191. if (ret)
  11192. return ret;
  11193. }
  11194. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11195. needs_modeset(crtc_state) ?
  11196. "[modeset]" : "[fastset]");
  11197. }
  11198. if (any_ms) {
  11199. ret = intel_modeset_checks(state);
  11200. if (ret)
  11201. return ret;
  11202. } else
  11203. intel_state->cdclk = dev_priv->cdclk_freq;
  11204. ret = drm_atomic_helper_check_planes(dev, state);
  11205. if (ret)
  11206. return ret;
  11207. intel_fbc_choose_crtc(dev_priv, state);
  11208. calc_watermark_data(state);
  11209. return 0;
  11210. }
  11211. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11212. struct drm_atomic_state *state,
  11213. bool async)
  11214. {
  11215. struct drm_i915_private *dev_priv = dev->dev_private;
  11216. struct drm_plane_state *plane_state;
  11217. struct drm_crtc_state *crtc_state;
  11218. struct drm_plane *plane;
  11219. struct drm_crtc *crtc;
  11220. int i, ret;
  11221. if (async) {
  11222. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  11223. return -EINVAL;
  11224. }
  11225. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11226. ret = intel_crtc_wait_for_pending_flips(crtc);
  11227. if (ret)
  11228. return ret;
  11229. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11230. flush_workqueue(dev_priv->wq);
  11231. }
  11232. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11233. if (ret)
  11234. return ret;
  11235. ret = drm_atomic_helper_prepare_planes(dev, state);
  11236. if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
  11237. u32 reset_counter;
  11238. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  11239. mutex_unlock(&dev->struct_mutex);
  11240. for_each_plane_in_state(state, plane, plane_state, i) {
  11241. struct intel_plane_state *intel_plane_state =
  11242. to_intel_plane_state(plane_state);
  11243. if (!intel_plane_state->wait_req)
  11244. continue;
  11245. ret = __i915_wait_request(intel_plane_state->wait_req,
  11246. reset_counter, true,
  11247. NULL, NULL);
  11248. /* Swallow -EIO errors to allow updates during hw lockup. */
  11249. if (ret == -EIO)
  11250. ret = 0;
  11251. if (ret)
  11252. break;
  11253. }
  11254. if (!ret)
  11255. return 0;
  11256. mutex_lock(&dev->struct_mutex);
  11257. drm_atomic_helper_cleanup_planes(dev, state);
  11258. }
  11259. mutex_unlock(&dev->struct_mutex);
  11260. return ret;
  11261. }
  11262. /**
  11263. * intel_atomic_commit - commit validated state object
  11264. * @dev: DRM device
  11265. * @state: the top-level driver state object
  11266. * @async: asynchronous commit
  11267. *
  11268. * This function commits a top-level state object that has been validated
  11269. * with drm_atomic_helper_check().
  11270. *
  11271. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11272. * we can only handle plane-related operations and do not yet support
  11273. * asynchronous commit.
  11274. *
  11275. * RETURNS
  11276. * Zero for success or -errno.
  11277. */
  11278. static int intel_atomic_commit(struct drm_device *dev,
  11279. struct drm_atomic_state *state,
  11280. bool async)
  11281. {
  11282. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11283. struct drm_i915_private *dev_priv = dev->dev_private;
  11284. struct drm_crtc_state *crtc_state;
  11285. struct drm_crtc *crtc;
  11286. int ret = 0, i;
  11287. bool hw_check = intel_state->modeset;
  11288. ret = intel_atomic_prepare_commit(dev, state, async);
  11289. if (ret) {
  11290. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11291. return ret;
  11292. }
  11293. drm_atomic_helper_swap_state(dev, state);
  11294. dev_priv->wm.config = to_intel_atomic_state(state)->wm_config;
  11295. if (intel_state->modeset) {
  11296. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  11297. sizeof(intel_state->min_pixclk));
  11298. dev_priv->active_crtcs = intel_state->active_crtcs;
  11299. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  11300. }
  11301. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11302. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11303. if (!needs_modeset(crtc->state))
  11304. continue;
  11305. intel_pre_plane_update(to_intel_crtc_state(crtc_state));
  11306. if (crtc_state->active) {
  11307. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  11308. dev_priv->display.crtc_disable(crtc);
  11309. intel_crtc->active = false;
  11310. intel_fbc_disable(intel_crtc);
  11311. intel_disable_shared_dpll(intel_crtc);
  11312. /*
  11313. * Underruns don't always raise
  11314. * interrupts, so check manually.
  11315. */
  11316. intel_check_cpu_fifo_underruns(dev_priv);
  11317. intel_check_pch_fifo_underruns(dev_priv);
  11318. if (!crtc->state->active)
  11319. intel_update_watermarks(crtc);
  11320. }
  11321. }
  11322. /* Only after disabling all output pipelines that will be changed can we
  11323. * update the the output configuration. */
  11324. intel_modeset_update_crtc_state(state);
  11325. if (intel_state->modeset) {
  11326. intel_shared_dpll_commit(state);
  11327. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11328. modeset_update_crtc_power_domains(state);
  11329. }
  11330. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11331. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11332. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11333. bool modeset = needs_modeset(crtc->state);
  11334. bool update_pipe = !modeset &&
  11335. to_intel_crtc_state(crtc->state)->update_pipe;
  11336. unsigned long put_domains = 0;
  11337. if (modeset)
  11338. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  11339. if (modeset && crtc->state->active) {
  11340. update_scanline_offset(to_intel_crtc(crtc));
  11341. dev_priv->display.crtc_enable(crtc);
  11342. }
  11343. if (update_pipe) {
  11344. put_domains = modeset_get_crtc_power_domains(crtc);
  11345. /* make sure intel_modeset_check_state runs */
  11346. hw_check = true;
  11347. }
  11348. if (!modeset)
  11349. intel_pre_plane_update(to_intel_crtc_state(crtc_state));
  11350. if (crtc->state->active && intel_crtc->atomic.update_fbc)
  11351. intel_fbc_enable(intel_crtc);
  11352. if (crtc->state->active &&
  11353. (crtc->state->planes_changed || update_pipe))
  11354. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11355. if (put_domains)
  11356. modeset_put_power_domains(dev_priv, put_domains);
  11357. intel_post_plane_update(intel_crtc);
  11358. if (modeset)
  11359. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  11360. }
  11361. /* FIXME: add subpixel order */
  11362. drm_atomic_helper_wait_for_vblanks(dev, state);
  11363. mutex_lock(&dev->struct_mutex);
  11364. drm_atomic_helper_cleanup_planes(dev, state);
  11365. mutex_unlock(&dev->struct_mutex);
  11366. if (hw_check)
  11367. intel_modeset_check_state(dev, state);
  11368. drm_atomic_state_free(state);
  11369. /* As one of the primary mmio accessors, KMS has a high likelihood
  11370. * of triggering bugs in unclaimed access. After we finish
  11371. * modesetting, see if an error has been flagged, and if so
  11372. * enable debugging for the next modeset - and hope we catch
  11373. * the culprit.
  11374. *
  11375. * XXX note that we assume display power is on at this point.
  11376. * This might hold true now but we need to add pm helper to check
  11377. * unclaimed only when the hardware is on, as atomic commits
  11378. * can happen also when the device is completely off.
  11379. */
  11380. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  11381. return 0;
  11382. }
  11383. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11384. {
  11385. struct drm_device *dev = crtc->dev;
  11386. struct drm_atomic_state *state;
  11387. struct drm_crtc_state *crtc_state;
  11388. int ret;
  11389. state = drm_atomic_state_alloc(dev);
  11390. if (!state) {
  11391. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11392. crtc->base.id);
  11393. return;
  11394. }
  11395. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11396. retry:
  11397. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11398. ret = PTR_ERR_OR_ZERO(crtc_state);
  11399. if (!ret) {
  11400. if (!crtc_state->active)
  11401. goto out;
  11402. crtc_state->mode_changed = true;
  11403. ret = drm_atomic_commit(state);
  11404. }
  11405. if (ret == -EDEADLK) {
  11406. drm_atomic_state_clear(state);
  11407. drm_modeset_backoff(state->acquire_ctx);
  11408. goto retry;
  11409. }
  11410. if (ret)
  11411. out:
  11412. drm_atomic_state_free(state);
  11413. }
  11414. #undef for_each_intel_crtc_masked
  11415. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11416. .gamma_set = intel_crtc_gamma_set,
  11417. .set_config = drm_atomic_helper_set_config,
  11418. .destroy = intel_crtc_destroy,
  11419. .page_flip = intel_crtc_page_flip,
  11420. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11421. .atomic_destroy_state = intel_crtc_destroy_state,
  11422. };
  11423. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11424. struct intel_shared_dpll *pll,
  11425. struct intel_dpll_hw_state *hw_state)
  11426. {
  11427. uint32_t val;
  11428. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11429. return false;
  11430. val = I915_READ(PCH_DPLL(pll->id));
  11431. hw_state->dpll = val;
  11432. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11433. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11434. return val & DPLL_VCO_ENABLE;
  11435. }
  11436. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11437. struct intel_shared_dpll *pll)
  11438. {
  11439. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11440. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11441. }
  11442. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11443. struct intel_shared_dpll *pll)
  11444. {
  11445. /* PCH refclock must be enabled first */
  11446. ibx_assert_pch_refclk_enabled(dev_priv);
  11447. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11448. /* Wait for the clocks to stabilize. */
  11449. POSTING_READ(PCH_DPLL(pll->id));
  11450. udelay(150);
  11451. /* The pixel multiplier can only be updated once the
  11452. * DPLL is enabled and the clocks are stable.
  11453. *
  11454. * So write it again.
  11455. */
  11456. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11457. POSTING_READ(PCH_DPLL(pll->id));
  11458. udelay(200);
  11459. }
  11460. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11461. struct intel_shared_dpll *pll)
  11462. {
  11463. struct drm_device *dev = dev_priv->dev;
  11464. struct intel_crtc *crtc;
  11465. /* Make sure no transcoder isn't still depending on us. */
  11466. for_each_intel_crtc(dev, crtc) {
  11467. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11468. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11469. }
  11470. I915_WRITE(PCH_DPLL(pll->id), 0);
  11471. POSTING_READ(PCH_DPLL(pll->id));
  11472. udelay(200);
  11473. }
  11474. static char *ibx_pch_dpll_names[] = {
  11475. "PCH DPLL A",
  11476. "PCH DPLL B",
  11477. };
  11478. static void ibx_pch_dpll_init(struct drm_device *dev)
  11479. {
  11480. struct drm_i915_private *dev_priv = dev->dev_private;
  11481. int i;
  11482. dev_priv->num_shared_dpll = 2;
  11483. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11484. dev_priv->shared_dplls[i].id = i;
  11485. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11486. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11487. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11488. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11489. dev_priv->shared_dplls[i].get_hw_state =
  11490. ibx_pch_dpll_get_hw_state;
  11491. }
  11492. }
  11493. static void intel_shared_dpll_init(struct drm_device *dev)
  11494. {
  11495. struct drm_i915_private *dev_priv = dev->dev_private;
  11496. if (HAS_DDI(dev))
  11497. intel_ddi_pll_init(dev);
  11498. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11499. ibx_pch_dpll_init(dev);
  11500. else
  11501. dev_priv->num_shared_dpll = 0;
  11502. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11503. }
  11504. /**
  11505. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11506. * @plane: drm plane to prepare for
  11507. * @fb: framebuffer to prepare for presentation
  11508. *
  11509. * Prepares a framebuffer for usage on a display plane. Generally this
  11510. * involves pinning the underlying object and updating the frontbuffer tracking
  11511. * bits. Some older platforms need special physical address handling for
  11512. * cursor planes.
  11513. *
  11514. * Must be called with struct_mutex held.
  11515. *
  11516. * Returns 0 on success, negative error code on failure.
  11517. */
  11518. int
  11519. intel_prepare_plane_fb(struct drm_plane *plane,
  11520. const struct drm_plane_state *new_state)
  11521. {
  11522. struct drm_device *dev = plane->dev;
  11523. struct drm_framebuffer *fb = new_state->fb;
  11524. struct intel_plane *intel_plane = to_intel_plane(plane);
  11525. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11526. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11527. int ret = 0;
  11528. if (!obj && !old_obj)
  11529. return 0;
  11530. if (old_obj) {
  11531. struct drm_crtc_state *crtc_state =
  11532. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11533. /* Big Hammer, we also need to ensure that any pending
  11534. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11535. * current scanout is retired before unpinning the old
  11536. * framebuffer. Note that we rely on userspace rendering
  11537. * into the buffer attached to the pipe they are waiting
  11538. * on. If not, userspace generates a GPU hang with IPEHR
  11539. * point to the MI_WAIT_FOR_EVENT.
  11540. *
  11541. * This should only fail upon a hung GPU, in which case we
  11542. * can safely continue.
  11543. */
  11544. if (needs_modeset(crtc_state))
  11545. ret = i915_gem_object_wait_rendering(old_obj, true);
  11546. /* Swallow -EIO errors to allow updates during hw lockup. */
  11547. if (ret && ret != -EIO)
  11548. return ret;
  11549. }
  11550. /* For framebuffer backed by dmabuf, wait for fence */
  11551. if (obj && obj->base.dma_buf) {
  11552. long lret;
  11553. lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  11554. false, true,
  11555. MAX_SCHEDULE_TIMEOUT);
  11556. if (lret == -ERESTARTSYS)
  11557. return lret;
  11558. WARN(lret < 0, "waiting returns %li\n", lret);
  11559. }
  11560. if (!obj) {
  11561. ret = 0;
  11562. } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11563. INTEL_INFO(dev)->cursor_needs_physical) {
  11564. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11565. ret = i915_gem_object_attach_phys(obj, align);
  11566. if (ret)
  11567. DRM_DEBUG_KMS("failed to attach phys object\n");
  11568. } else {
  11569. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state);
  11570. }
  11571. if (ret == 0) {
  11572. if (obj) {
  11573. struct intel_plane_state *plane_state =
  11574. to_intel_plane_state(new_state);
  11575. i915_gem_request_assign(&plane_state->wait_req,
  11576. obj->last_write_req);
  11577. }
  11578. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11579. }
  11580. return ret;
  11581. }
  11582. /**
  11583. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11584. * @plane: drm plane to clean up for
  11585. * @fb: old framebuffer that was on plane
  11586. *
  11587. * Cleans up a framebuffer that has just been removed from a plane.
  11588. *
  11589. * Must be called with struct_mutex held.
  11590. */
  11591. void
  11592. intel_cleanup_plane_fb(struct drm_plane *plane,
  11593. const struct drm_plane_state *old_state)
  11594. {
  11595. struct drm_device *dev = plane->dev;
  11596. struct intel_plane *intel_plane = to_intel_plane(plane);
  11597. struct intel_plane_state *old_intel_state;
  11598. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11599. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11600. old_intel_state = to_intel_plane_state(old_state);
  11601. if (!obj && !old_obj)
  11602. return;
  11603. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11604. !INTEL_INFO(dev)->cursor_needs_physical))
  11605. intel_unpin_fb_obj(old_state->fb, old_state);
  11606. /* prepare_fb aborted? */
  11607. if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
  11608. (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
  11609. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11610. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11611. }
  11612. int
  11613. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11614. {
  11615. int max_scale;
  11616. struct drm_device *dev;
  11617. struct drm_i915_private *dev_priv;
  11618. int crtc_clock, cdclk;
  11619. if (!intel_crtc || !crtc_state->base.enable)
  11620. return DRM_PLANE_HELPER_NO_SCALING;
  11621. dev = intel_crtc->base.dev;
  11622. dev_priv = dev->dev_private;
  11623. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11624. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11625. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11626. return DRM_PLANE_HELPER_NO_SCALING;
  11627. /*
  11628. * skl max scale is lower of:
  11629. * close to 3 but not 3, -1 is for that purpose
  11630. * or
  11631. * cdclk/crtc_clock
  11632. */
  11633. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11634. return max_scale;
  11635. }
  11636. static int
  11637. intel_check_primary_plane(struct drm_plane *plane,
  11638. struct intel_crtc_state *crtc_state,
  11639. struct intel_plane_state *state)
  11640. {
  11641. struct drm_crtc *crtc = state->base.crtc;
  11642. struct drm_framebuffer *fb = state->base.fb;
  11643. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11644. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11645. bool can_position = false;
  11646. if (INTEL_INFO(plane->dev)->gen >= 9) {
  11647. /* use scaler when colorkey is not required */
  11648. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11649. min_scale = 1;
  11650. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11651. }
  11652. can_position = true;
  11653. }
  11654. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11655. &state->dst, &state->clip,
  11656. min_scale, max_scale,
  11657. can_position, true,
  11658. &state->visible);
  11659. }
  11660. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11661. struct drm_crtc_state *old_crtc_state)
  11662. {
  11663. struct drm_device *dev = crtc->dev;
  11664. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11665. struct intel_crtc_state *old_intel_state =
  11666. to_intel_crtc_state(old_crtc_state);
  11667. bool modeset = needs_modeset(crtc->state);
  11668. /* Perform vblank evasion around commit operation */
  11669. intel_pipe_update_start(intel_crtc);
  11670. if (modeset)
  11671. return;
  11672. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11673. intel_update_pipe_config(intel_crtc, old_intel_state);
  11674. else if (INTEL_INFO(dev)->gen >= 9)
  11675. skl_detach_scalers(intel_crtc);
  11676. }
  11677. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11678. struct drm_crtc_state *old_crtc_state)
  11679. {
  11680. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11681. intel_pipe_update_end(intel_crtc);
  11682. }
  11683. /**
  11684. * intel_plane_destroy - destroy a plane
  11685. * @plane: plane to destroy
  11686. *
  11687. * Common destruction function for all types of planes (primary, cursor,
  11688. * sprite).
  11689. */
  11690. void intel_plane_destroy(struct drm_plane *plane)
  11691. {
  11692. struct intel_plane *intel_plane = to_intel_plane(plane);
  11693. drm_plane_cleanup(plane);
  11694. kfree(intel_plane);
  11695. }
  11696. const struct drm_plane_funcs intel_plane_funcs = {
  11697. .update_plane = drm_atomic_helper_update_plane,
  11698. .disable_plane = drm_atomic_helper_disable_plane,
  11699. .destroy = intel_plane_destroy,
  11700. .set_property = drm_atomic_helper_plane_set_property,
  11701. .atomic_get_property = intel_plane_atomic_get_property,
  11702. .atomic_set_property = intel_plane_atomic_set_property,
  11703. .atomic_duplicate_state = intel_plane_duplicate_state,
  11704. .atomic_destroy_state = intel_plane_destroy_state,
  11705. };
  11706. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11707. int pipe)
  11708. {
  11709. struct intel_plane *primary;
  11710. struct intel_plane_state *state;
  11711. const uint32_t *intel_primary_formats;
  11712. unsigned int num_formats;
  11713. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11714. if (primary == NULL)
  11715. return NULL;
  11716. state = intel_create_plane_state(&primary->base);
  11717. if (!state) {
  11718. kfree(primary);
  11719. return NULL;
  11720. }
  11721. primary->base.state = &state->base;
  11722. primary->can_scale = false;
  11723. primary->max_downscale = 1;
  11724. if (INTEL_INFO(dev)->gen >= 9) {
  11725. primary->can_scale = true;
  11726. state->scaler_id = -1;
  11727. }
  11728. primary->pipe = pipe;
  11729. primary->plane = pipe;
  11730. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11731. primary->check_plane = intel_check_primary_plane;
  11732. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11733. primary->plane = !pipe;
  11734. if (INTEL_INFO(dev)->gen >= 9) {
  11735. intel_primary_formats = skl_primary_formats;
  11736. num_formats = ARRAY_SIZE(skl_primary_formats);
  11737. primary->update_plane = skylake_update_primary_plane;
  11738. primary->disable_plane = skylake_disable_primary_plane;
  11739. } else if (HAS_PCH_SPLIT(dev)) {
  11740. intel_primary_formats = i965_primary_formats;
  11741. num_formats = ARRAY_SIZE(i965_primary_formats);
  11742. primary->update_plane = ironlake_update_primary_plane;
  11743. primary->disable_plane = i9xx_disable_primary_plane;
  11744. } else if (INTEL_INFO(dev)->gen >= 4) {
  11745. intel_primary_formats = i965_primary_formats;
  11746. num_formats = ARRAY_SIZE(i965_primary_formats);
  11747. primary->update_plane = i9xx_update_primary_plane;
  11748. primary->disable_plane = i9xx_disable_primary_plane;
  11749. } else {
  11750. intel_primary_formats = i8xx_primary_formats;
  11751. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11752. primary->update_plane = i9xx_update_primary_plane;
  11753. primary->disable_plane = i9xx_disable_primary_plane;
  11754. }
  11755. drm_universal_plane_init(dev, &primary->base, 0,
  11756. &intel_plane_funcs,
  11757. intel_primary_formats, num_formats,
  11758. DRM_PLANE_TYPE_PRIMARY, NULL);
  11759. if (INTEL_INFO(dev)->gen >= 4)
  11760. intel_create_rotation_property(dev, primary);
  11761. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11762. return &primary->base;
  11763. }
  11764. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11765. {
  11766. if (!dev->mode_config.rotation_property) {
  11767. unsigned long flags = BIT(DRM_ROTATE_0) |
  11768. BIT(DRM_ROTATE_180);
  11769. if (INTEL_INFO(dev)->gen >= 9)
  11770. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11771. dev->mode_config.rotation_property =
  11772. drm_mode_create_rotation_property(dev, flags);
  11773. }
  11774. if (dev->mode_config.rotation_property)
  11775. drm_object_attach_property(&plane->base.base,
  11776. dev->mode_config.rotation_property,
  11777. plane->base.state->rotation);
  11778. }
  11779. static int
  11780. intel_check_cursor_plane(struct drm_plane *plane,
  11781. struct intel_crtc_state *crtc_state,
  11782. struct intel_plane_state *state)
  11783. {
  11784. struct drm_crtc *crtc = crtc_state->base.crtc;
  11785. struct drm_framebuffer *fb = state->base.fb;
  11786. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11787. enum pipe pipe = to_intel_plane(plane)->pipe;
  11788. unsigned stride;
  11789. int ret;
  11790. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11791. &state->dst, &state->clip,
  11792. DRM_PLANE_HELPER_NO_SCALING,
  11793. DRM_PLANE_HELPER_NO_SCALING,
  11794. true, true, &state->visible);
  11795. if (ret)
  11796. return ret;
  11797. /* if we want to turn off the cursor ignore width and height */
  11798. if (!obj)
  11799. return 0;
  11800. /* Check for which cursor types we support */
  11801. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11802. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11803. state->base.crtc_w, state->base.crtc_h);
  11804. return -EINVAL;
  11805. }
  11806. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11807. if (obj->base.size < stride * state->base.crtc_h) {
  11808. DRM_DEBUG_KMS("buffer is too small\n");
  11809. return -ENOMEM;
  11810. }
  11811. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11812. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11813. return -EINVAL;
  11814. }
  11815. /*
  11816. * There's something wrong with the cursor on CHV pipe C.
  11817. * If it straddles the left edge of the screen then
  11818. * moving it away from the edge or disabling it often
  11819. * results in a pipe underrun, and often that can lead to
  11820. * dead pipe (constant underrun reported, and it scans
  11821. * out just a solid color). To recover from that, the
  11822. * display power well must be turned off and on again.
  11823. * Refuse the put the cursor into that compromised position.
  11824. */
  11825. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  11826. state->visible && state->base.crtc_x < 0) {
  11827. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  11828. return -EINVAL;
  11829. }
  11830. return 0;
  11831. }
  11832. static void
  11833. intel_disable_cursor_plane(struct drm_plane *plane,
  11834. struct drm_crtc *crtc)
  11835. {
  11836. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11837. intel_crtc->cursor_addr = 0;
  11838. intel_crtc_update_cursor(crtc, NULL);
  11839. }
  11840. static void
  11841. intel_update_cursor_plane(struct drm_plane *plane,
  11842. const struct intel_crtc_state *crtc_state,
  11843. const struct intel_plane_state *state)
  11844. {
  11845. struct drm_crtc *crtc = crtc_state->base.crtc;
  11846. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11847. struct drm_device *dev = plane->dev;
  11848. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11849. uint32_t addr;
  11850. if (!obj)
  11851. addr = 0;
  11852. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11853. addr = i915_gem_obj_ggtt_offset(obj);
  11854. else
  11855. addr = obj->phys_handle->busaddr;
  11856. intel_crtc->cursor_addr = addr;
  11857. intel_crtc_update_cursor(crtc, state);
  11858. }
  11859. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11860. int pipe)
  11861. {
  11862. struct intel_plane *cursor;
  11863. struct intel_plane_state *state;
  11864. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11865. if (cursor == NULL)
  11866. return NULL;
  11867. state = intel_create_plane_state(&cursor->base);
  11868. if (!state) {
  11869. kfree(cursor);
  11870. return NULL;
  11871. }
  11872. cursor->base.state = &state->base;
  11873. cursor->can_scale = false;
  11874. cursor->max_downscale = 1;
  11875. cursor->pipe = pipe;
  11876. cursor->plane = pipe;
  11877. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11878. cursor->check_plane = intel_check_cursor_plane;
  11879. cursor->update_plane = intel_update_cursor_plane;
  11880. cursor->disable_plane = intel_disable_cursor_plane;
  11881. drm_universal_plane_init(dev, &cursor->base, 0,
  11882. &intel_plane_funcs,
  11883. intel_cursor_formats,
  11884. ARRAY_SIZE(intel_cursor_formats),
  11885. DRM_PLANE_TYPE_CURSOR, NULL);
  11886. if (INTEL_INFO(dev)->gen >= 4) {
  11887. if (!dev->mode_config.rotation_property)
  11888. dev->mode_config.rotation_property =
  11889. drm_mode_create_rotation_property(dev,
  11890. BIT(DRM_ROTATE_0) |
  11891. BIT(DRM_ROTATE_180));
  11892. if (dev->mode_config.rotation_property)
  11893. drm_object_attach_property(&cursor->base.base,
  11894. dev->mode_config.rotation_property,
  11895. state->base.rotation);
  11896. }
  11897. if (INTEL_INFO(dev)->gen >=9)
  11898. state->scaler_id = -1;
  11899. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11900. return &cursor->base;
  11901. }
  11902. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11903. struct intel_crtc_state *crtc_state)
  11904. {
  11905. int i;
  11906. struct intel_scaler *intel_scaler;
  11907. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11908. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11909. intel_scaler = &scaler_state->scalers[i];
  11910. intel_scaler->in_use = 0;
  11911. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11912. }
  11913. scaler_state->scaler_id = -1;
  11914. }
  11915. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11916. {
  11917. struct drm_i915_private *dev_priv = dev->dev_private;
  11918. struct intel_crtc *intel_crtc;
  11919. struct intel_crtc_state *crtc_state = NULL;
  11920. struct drm_plane *primary = NULL;
  11921. struct drm_plane *cursor = NULL;
  11922. int i, ret;
  11923. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11924. if (intel_crtc == NULL)
  11925. return;
  11926. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11927. if (!crtc_state)
  11928. goto fail;
  11929. intel_crtc->config = crtc_state;
  11930. intel_crtc->base.state = &crtc_state->base;
  11931. crtc_state->base.crtc = &intel_crtc->base;
  11932. /* initialize shared scalers */
  11933. if (INTEL_INFO(dev)->gen >= 9) {
  11934. if (pipe == PIPE_C)
  11935. intel_crtc->num_scalers = 1;
  11936. else
  11937. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11938. skl_init_scalers(dev, intel_crtc, crtc_state);
  11939. }
  11940. primary = intel_primary_plane_create(dev, pipe);
  11941. if (!primary)
  11942. goto fail;
  11943. cursor = intel_cursor_plane_create(dev, pipe);
  11944. if (!cursor)
  11945. goto fail;
  11946. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11947. cursor, &intel_crtc_funcs, NULL);
  11948. if (ret)
  11949. goto fail;
  11950. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11951. for (i = 0; i < 256; i++) {
  11952. intel_crtc->lut_r[i] = i;
  11953. intel_crtc->lut_g[i] = i;
  11954. intel_crtc->lut_b[i] = i;
  11955. }
  11956. /*
  11957. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11958. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11959. */
  11960. intel_crtc->pipe = pipe;
  11961. intel_crtc->plane = pipe;
  11962. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11963. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11964. intel_crtc->plane = !pipe;
  11965. }
  11966. intel_crtc->cursor_base = ~0;
  11967. intel_crtc->cursor_cntl = ~0;
  11968. intel_crtc->cursor_size = ~0;
  11969. intel_crtc->wm.cxsr_allowed = true;
  11970. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11971. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11972. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11973. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11974. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11975. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11976. return;
  11977. fail:
  11978. if (primary)
  11979. drm_plane_cleanup(primary);
  11980. if (cursor)
  11981. drm_plane_cleanup(cursor);
  11982. kfree(crtc_state);
  11983. kfree(intel_crtc);
  11984. }
  11985. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11986. {
  11987. struct drm_encoder *encoder = connector->base.encoder;
  11988. struct drm_device *dev = connector->base.dev;
  11989. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11990. if (!encoder || WARN_ON(!encoder->crtc))
  11991. return INVALID_PIPE;
  11992. return to_intel_crtc(encoder->crtc)->pipe;
  11993. }
  11994. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11995. struct drm_file *file)
  11996. {
  11997. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11998. struct drm_crtc *drmmode_crtc;
  11999. struct intel_crtc *crtc;
  12000. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12001. if (!drmmode_crtc) {
  12002. DRM_ERROR("no such CRTC id\n");
  12003. return -ENOENT;
  12004. }
  12005. crtc = to_intel_crtc(drmmode_crtc);
  12006. pipe_from_crtc_id->pipe = crtc->pipe;
  12007. return 0;
  12008. }
  12009. static int intel_encoder_clones(struct intel_encoder *encoder)
  12010. {
  12011. struct drm_device *dev = encoder->base.dev;
  12012. struct intel_encoder *source_encoder;
  12013. int index_mask = 0;
  12014. int entry = 0;
  12015. for_each_intel_encoder(dev, source_encoder) {
  12016. if (encoders_cloneable(encoder, source_encoder))
  12017. index_mask |= (1 << entry);
  12018. entry++;
  12019. }
  12020. return index_mask;
  12021. }
  12022. static bool has_edp_a(struct drm_device *dev)
  12023. {
  12024. struct drm_i915_private *dev_priv = dev->dev_private;
  12025. if (!IS_MOBILE(dev))
  12026. return false;
  12027. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12028. return false;
  12029. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12030. return false;
  12031. return true;
  12032. }
  12033. static bool intel_crt_present(struct drm_device *dev)
  12034. {
  12035. struct drm_i915_private *dev_priv = dev->dev_private;
  12036. if (INTEL_INFO(dev)->gen >= 9)
  12037. return false;
  12038. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  12039. return false;
  12040. if (IS_CHERRYVIEW(dev))
  12041. return false;
  12042. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12043. return false;
  12044. /* DDI E can't be used if DDI A requires 4 lanes */
  12045. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12046. return false;
  12047. if (!dev_priv->vbt.int_crt_support)
  12048. return false;
  12049. return true;
  12050. }
  12051. static void intel_setup_outputs(struct drm_device *dev)
  12052. {
  12053. struct drm_i915_private *dev_priv = dev->dev_private;
  12054. struct intel_encoder *encoder;
  12055. bool dpd_is_edp = false;
  12056. intel_lvds_init(dev);
  12057. if (intel_crt_present(dev))
  12058. intel_crt_init(dev);
  12059. if (IS_BROXTON(dev)) {
  12060. /*
  12061. * FIXME: Broxton doesn't support port detection via the
  12062. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12063. * detect the ports.
  12064. */
  12065. intel_ddi_init(dev, PORT_A);
  12066. intel_ddi_init(dev, PORT_B);
  12067. intel_ddi_init(dev, PORT_C);
  12068. } else if (HAS_DDI(dev)) {
  12069. int found;
  12070. /*
  12071. * Haswell uses DDI functions to detect digital outputs.
  12072. * On SKL pre-D0 the strap isn't connected, so we assume
  12073. * it's there.
  12074. */
  12075. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12076. /* WaIgnoreDDIAStrap: skl */
  12077. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12078. intel_ddi_init(dev, PORT_A);
  12079. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12080. * register */
  12081. found = I915_READ(SFUSE_STRAP);
  12082. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12083. intel_ddi_init(dev, PORT_B);
  12084. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12085. intel_ddi_init(dev, PORT_C);
  12086. if (found & SFUSE_STRAP_DDID_DETECTED)
  12087. intel_ddi_init(dev, PORT_D);
  12088. /*
  12089. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12090. */
  12091. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  12092. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12093. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12094. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12095. intel_ddi_init(dev, PORT_E);
  12096. } else if (HAS_PCH_SPLIT(dev)) {
  12097. int found;
  12098. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12099. if (has_edp_a(dev))
  12100. intel_dp_init(dev, DP_A, PORT_A);
  12101. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12102. /* PCH SDVOB multiplex with HDMIB */
  12103. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12104. if (!found)
  12105. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12106. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12107. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12108. }
  12109. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12110. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12111. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12112. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12113. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12114. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12115. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12116. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12117. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12118. /*
  12119. * The DP_DETECTED bit is the latched state of the DDC
  12120. * SDA pin at boot. However since eDP doesn't require DDC
  12121. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12122. * eDP ports may have been muxed to an alternate function.
  12123. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12124. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12125. * detect eDP ports.
  12126. */
  12127. if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
  12128. !intel_dp_is_edp(dev, PORT_B))
  12129. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  12130. if (I915_READ(VLV_DP_B) & DP_DETECTED ||
  12131. intel_dp_is_edp(dev, PORT_B))
  12132. intel_dp_init(dev, VLV_DP_B, PORT_B);
  12133. if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
  12134. !intel_dp_is_edp(dev, PORT_C))
  12135. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  12136. if (I915_READ(VLV_DP_C) & DP_DETECTED ||
  12137. intel_dp_is_edp(dev, PORT_C))
  12138. intel_dp_init(dev, VLV_DP_C, PORT_C);
  12139. if (IS_CHERRYVIEW(dev)) {
  12140. /* eDP not supported on port D, so don't check VBT */
  12141. if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
  12142. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  12143. if (I915_READ(CHV_DP_D) & DP_DETECTED)
  12144. intel_dp_init(dev, CHV_DP_D, PORT_D);
  12145. }
  12146. intel_dsi_init(dev);
  12147. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12148. bool found = false;
  12149. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12150. DRM_DEBUG_KMS("probing SDVOB\n");
  12151. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  12152. if (!found && IS_G4X(dev)) {
  12153. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12154. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12155. }
  12156. if (!found && IS_G4X(dev))
  12157. intel_dp_init(dev, DP_B, PORT_B);
  12158. }
  12159. /* Before G4X SDVOC doesn't have its own detect register */
  12160. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12161. DRM_DEBUG_KMS("probing SDVOC\n");
  12162. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  12163. }
  12164. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12165. if (IS_G4X(dev)) {
  12166. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12167. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12168. }
  12169. if (IS_G4X(dev))
  12170. intel_dp_init(dev, DP_C, PORT_C);
  12171. }
  12172. if (IS_G4X(dev) &&
  12173. (I915_READ(DP_D) & DP_DETECTED))
  12174. intel_dp_init(dev, DP_D, PORT_D);
  12175. } else if (IS_GEN2(dev))
  12176. intel_dvo_init(dev);
  12177. if (SUPPORTS_TV(dev))
  12178. intel_tv_init(dev);
  12179. intel_psr_init(dev);
  12180. for_each_intel_encoder(dev, encoder) {
  12181. encoder->base.possible_crtcs = encoder->crtc_mask;
  12182. encoder->base.possible_clones =
  12183. intel_encoder_clones(encoder);
  12184. }
  12185. intel_init_pch_refclk(dev);
  12186. drm_helper_move_panel_connectors_to_head(dev);
  12187. }
  12188. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12189. {
  12190. struct drm_device *dev = fb->dev;
  12191. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12192. drm_framebuffer_cleanup(fb);
  12193. mutex_lock(&dev->struct_mutex);
  12194. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12195. drm_gem_object_unreference(&intel_fb->obj->base);
  12196. mutex_unlock(&dev->struct_mutex);
  12197. kfree(intel_fb);
  12198. }
  12199. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12200. struct drm_file *file,
  12201. unsigned int *handle)
  12202. {
  12203. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12204. struct drm_i915_gem_object *obj = intel_fb->obj;
  12205. if (obj->userptr.mm) {
  12206. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  12207. return -EINVAL;
  12208. }
  12209. return drm_gem_handle_create(file, &obj->base, handle);
  12210. }
  12211. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12212. struct drm_file *file,
  12213. unsigned flags, unsigned color,
  12214. struct drm_clip_rect *clips,
  12215. unsigned num_clips)
  12216. {
  12217. struct drm_device *dev = fb->dev;
  12218. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12219. struct drm_i915_gem_object *obj = intel_fb->obj;
  12220. mutex_lock(&dev->struct_mutex);
  12221. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  12222. mutex_unlock(&dev->struct_mutex);
  12223. return 0;
  12224. }
  12225. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12226. .destroy = intel_user_framebuffer_destroy,
  12227. .create_handle = intel_user_framebuffer_create_handle,
  12228. .dirty = intel_user_framebuffer_dirty,
  12229. };
  12230. static
  12231. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12232. uint32_t pixel_format)
  12233. {
  12234. u32 gen = INTEL_INFO(dev)->gen;
  12235. if (gen >= 9) {
  12236. int cpp = drm_format_plane_cpp(pixel_format, 0);
  12237. /* "The stride in bytes must not exceed the of the size of 8K
  12238. * pixels and 32K bytes."
  12239. */
  12240. return min(8192 * cpp, 32768);
  12241. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12242. return 32*1024;
  12243. } else if (gen >= 4) {
  12244. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12245. return 16*1024;
  12246. else
  12247. return 32*1024;
  12248. } else if (gen >= 3) {
  12249. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12250. return 8*1024;
  12251. else
  12252. return 16*1024;
  12253. } else {
  12254. /* XXX DSPC is limited to 4k tiled */
  12255. return 8*1024;
  12256. }
  12257. }
  12258. static int intel_framebuffer_init(struct drm_device *dev,
  12259. struct intel_framebuffer *intel_fb,
  12260. struct drm_mode_fb_cmd2 *mode_cmd,
  12261. struct drm_i915_gem_object *obj)
  12262. {
  12263. struct drm_i915_private *dev_priv = to_i915(dev);
  12264. unsigned int aligned_height;
  12265. int ret;
  12266. u32 pitch_limit, stride_alignment;
  12267. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12268. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12269. /* Enforce that fb modifier and tiling mode match, but only for
  12270. * X-tiled. This is needed for FBC. */
  12271. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12272. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12273. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12274. return -EINVAL;
  12275. }
  12276. } else {
  12277. if (obj->tiling_mode == I915_TILING_X)
  12278. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12279. else if (obj->tiling_mode == I915_TILING_Y) {
  12280. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12281. return -EINVAL;
  12282. }
  12283. }
  12284. /* Passed in modifier sanity checking. */
  12285. switch (mode_cmd->modifier[0]) {
  12286. case I915_FORMAT_MOD_Y_TILED:
  12287. case I915_FORMAT_MOD_Yf_TILED:
  12288. if (INTEL_INFO(dev)->gen < 9) {
  12289. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12290. mode_cmd->modifier[0]);
  12291. return -EINVAL;
  12292. }
  12293. case DRM_FORMAT_MOD_NONE:
  12294. case I915_FORMAT_MOD_X_TILED:
  12295. break;
  12296. default:
  12297. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12298. mode_cmd->modifier[0]);
  12299. return -EINVAL;
  12300. }
  12301. stride_alignment = intel_fb_stride_alignment(dev_priv,
  12302. mode_cmd->modifier[0],
  12303. mode_cmd->pixel_format);
  12304. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12305. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12306. mode_cmd->pitches[0], stride_alignment);
  12307. return -EINVAL;
  12308. }
  12309. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12310. mode_cmd->pixel_format);
  12311. if (mode_cmd->pitches[0] > pitch_limit) {
  12312. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12313. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12314. "tiled" : "linear",
  12315. mode_cmd->pitches[0], pitch_limit);
  12316. return -EINVAL;
  12317. }
  12318. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12319. mode_cmd->pitches[0] != obj->stride) {
  12320. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12321. mode_cmd->pitches[0], obj->stride);
  12322. return -EINVAL;
  12323. }
  12324. /* Reject formats not supported by any plane early. */
  12325. switch (mode_cmd->pixel_format) {
  12326. case DRM_FORMAT_C8:
  12327. case DRM_FORMAT_RGB565:
  12328. case DRM_FORMAT_XRGB8888:
  12329. case DRM_FORMAT_ARGB8888:
  12330. break;
  12331. case DRM_FORMAT_XRGB1555:
  12332. if (INTEL_INFO(dev)->gen > 3) {
  12333. DRM_DEBUG("unsupported pixel format: %s\n",
  12334. drm_get_format_name(mode_cmd->pixel_format));
  12335. return -EINVAL;
  12336. }
  12337. break;
  12338. case DRM_FORMAT_ABGR8888:
  12339. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  12340. INTEL_INFO(dev)->gen < 9) {
  12341. DRM_DEBUG("unsupported pixel format: %s\n",
  12342. drm_get_format_name(mode_cmd->pixel_format));
  12343. return -EINVAL;
  12344. }
  12345. break;
  12346. case DRM_FORMAT_XBGR8888:
  12347. case DRM_FORMAT_XRGB2101010:
  12348. case DRM_FORMAT_XBGR2101010:
  12349. if (INTEL_INFO(dev)->gen < 4) {
  12350. DRM_DEBUG("unsupported pixel format: %s\n",
  12351. drm_get_format_name(mode_cmd->pixel_format));
  12352. return -EINVAL;
  12353. }
  12354. break;
  12355. case DRM_FORMAT_ABGR2101010:
  12356. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12357. DRM_DEBUG("unsupported pixel format: %s\n",
  12358. drm_get_format_name(mode_cmd->pixel_format));
  12359. return -EINVAL;
  12360. }
  12361. break;
  12362. case DRM_FORMAT_YUYV:
  12363. case DRM_FORMAT_UYVY:
  12364. case DRM_FORMAT_YVYU:
  12365. case DRM_FORMAT_VYUY:
  12366. if (INTEL_INFO(dev)->gen < 5) {
  12367. DRM_DEBUG("unsupported pixel format: %s\n",
  12368. drm_get_format_name(mode_cmd->pixel_format));
  12369. return -EINVAL;
  12370. }
  12371. break;
  12372. default:
  12373. DRM_DEBUG("unsupported pixel format: %s\n",
  12374. drm_get_format_name(mode_cmd->pixel_format));
  12375. return -EINVAL;
  12376. }
  12377. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12378. if (mode_cmd->offsets[0] != 0)
  12379. return -EINVAL;
  12380. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12381. mode_cmd->pixel_format,
  12382. mode_cmd->modifier[0]);
  12383. /* FIXME drm helper for size checks (especially planar formats)? */
  12384. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12385. return -EINVAL;
  12386. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12387. intel_fb->obj = obj;
  12388. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12389. if (ret) {
  12390. DRM_ERROR("framebuffer init failed %d\n", ret);
  12391. return ret;
  12392. }
  12393. intel_fb->obj->framebuffer_references++;
  12394. return 0;
  12395. }
  12396. static struct drm_framebuffer *
  12397. intel_user_framebuffer_create(struct drm_device *dev,
  12398. struct drm_file *filp,
  12399. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12400. {
  12401. struct drm_framebuffer *fb;
  12402. struct drm_i915_gem_object *obj;
  12403. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12404. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12405. mode_cmd.handles[0]));
  12406. if (&obj->base == NULL)
  12407. return ERR_PTR(-ENOENT);
  12408. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  12409. if (IS_ERR(fb))
  12410. drm_gem_object_unreference_unlocked(&obj->base);
  12411. return fb;
  12412. }
  12413. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12414. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12415. {
  12416. }
  12417. #endif
  12418. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12419. .fb_create = intel_user_framebuffer_create,
  12420. .output_poll_changed = intel_fbdev_output_poll_changed,
  12421. .atomic_check = intel_atomic_check,
  12422. .atomic_commit = intel_atomic_commit,
  12423. .atomic_state_alloc = intel_atomic_state_alloc,
  12424. .atomic_state_clear = intel_atomic_state_clear,
  12425. };
  12426. /* Set up chip specific display functions */
  12427. static void intel_init_display(struct drm_device *dev)
  12428. {
  12429. struct drm_i915_private *dev_priv = dev->dev_private;
  12430. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12431. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12432. else if (IS_CHERRYVIEW(dev))
  12433. dev_priv->display.find_dpll = chv_find_best_dpll;
  12434. else if (IS_VALLEYVIEW(dev))
  12435. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12436. else if (IS_PINEVIEW(dev))
  12437. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12438. else
  12439. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12440. if (INTEL_INFO(dev)->gen >= 9) {
  12441. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12442. dev_priv->display.get_initial_plane_config =
  12443. skylake_get_initial_plane_config;
  12444. dev_priv->display.crtc_compute_clock =
  12445. haswell_crtc_compute_clock;
  12446. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12447. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12448. } else if (HAS_DDI(dev)) {
  12449. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12450. dev_priv->display.get_initial_plane_config =
  12451. ironlake_get_initial_plane_config;
  12452. dev_priv->display.crtc_compute_clock =
  12453. haswell_crtc_compute_clock;
  12454. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12455. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12456. } else if (HAS_PCH_SPLIT(dev)) {
  12457. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12458. dev_priv->display.get_initial_plane_config =
  12459. ironlake_get_initial_plane_config;
  12460. dev_priv->display.crtc_compute_clock =
  12461. ironlake_crtc_compute_clock;
  12462. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12463. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12464. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12465. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12466. dev_priv->display.get_initial_plane_config =
  12467. i9xx_get_initial_plane_config;
  12468. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12469. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12470. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12471. } else {
  12472. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12473. dev_priv->display.get_initial_plane_config =
  12474. i9xx_get_initial_plane_config;
  12475. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12476. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12477. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12478. }
  12479. /* Returns the core display clock speed */
  12480. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12481. dev_priv->display.get_display_clock_speed =
  12482. skylake_get_display_clock_speed;
  12483. else if (IS_BROXTON(dev))
  12484. dev_priv->display.get_display_clock_speed =
  12485. broxton_get_display_clock_speed;
  12486. else if (IS_BROADWELL(dev))
  12487. dev_priv->display.get_display_clock_speed =
  12488. broadwell_get_display_clock_speed;
  12489. else if (IS_HASWELL(dev))
  12490. dev_priv->display.get_display_clock_speed =
  12491. haswell_get_display_clock_speed;
  12492. else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  12493. dev_priv->display.get_display_clock_speed =
  12494. valleyview_get_display_clock_speed;
  12495. else if (IS_GEN5(dev))
  12496. dev_priv->display.get_display_clock_speed =
  12497. ilk_get_display_clock_speed;
  12498. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12499. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12500. dev_priv->display.get_display_clock_speed =
  12501. i945_get_display_clock_speed;
  12502. else if (IS_GM45(dev))
  12503. dev_priv->display.get_display_clock_speed =
  12504. gm45_get_display_clock_speed;
  12505. else if (IS_CRESTLINE(dev))
  12506. dev_priv->display.get_display_clock_speed =
  12507. i965gm_get_display_clock_speed;
  12508. else if (IS_PINEVIEW(dev))
  12509. dev_priv->display.get_display_clock_speed =
  12510. pnv_get_display_clock_speed;
  12511. else if (IS_G33(dev) || IS_G4X(dev))
  12512. dev_priv->display.get_display_clock_speed =
  12513. g33_get_display_clock_speed;
  12514. else if (IS_I915G(dev))
  12515. dev_priv->display.get_display_clock_speed =
  12516. i915_get_display_clock_speed;
  12517. else if (IS_I945GM(dev) || IS_845G(dev))
  12518. dev_priv->display.get_display_clock_speed =
  12519. i9xx_misc_get_display_clock_speed;
  12520. else if (IS_I915GM(dev))
  12521. dev_priv->display.get_display_clock_speed =
  12522. i915gm_get_display_clock_speed;
  12523. else if (IS_I865G(dev))
  12524. dev_priv->display.get_display_clock_speed =
  12525. i865_get_display_clock_speed;
  12526. else if (IS_I85X(dev))
  12527. dev_priv->display.get_display_clock_speed =
  12528. i85x_get_display_clock_speed;
  12529. else { /* 830 */
  12530. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12531. dev_priv->display.get_display_clock_speed =
  12532. i830_get_display_clock_speed;
  12533. }
  12534. if (IS_GEN5(dev)) {
  12535. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12536. } else if (IS_GEN6(dev)) {
  12537. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12538. } else if (IS_IVYBRIDGE(dev)) {
  12539. /* FIXME: detect B0+ stepping and use auto training */
  12540. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12541. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12542. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12543. if (IS_BROADWELL(dev)) {
  12544. dev_priv->display.modeset_commit_cdclk =
  12545. broadwell_modeset_commit_cdclk;
  12546. dev_priv->display.modeset_calc_cdclk =
  12547. broadwell_modeset_calc_cdclk;
  12548. }
  12549. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12550. dev_priv->display.modeset_commit_cdclk =
  12551. valleyview_modeset_commit_cdclk;
  12552. dev_priv->display.modeset_calc_cdclk =
  12553. valleyview_modeset_calc_cdclk;
  12554. } else if (IS_BROXTON(dev)) {
  12555. dev_priv->display.modeset_commit_cdclk =
  12556. broxton_modeset_commit_cdclk;
  12557. dev_priv->display.modeset_calc_cdclk =
  12558. broxton_modeset_calc_cdclk;
  12559. }
  12560. switch (INTEL_INFO(dev)->gen) {
  12561. case 2:
  12562. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12563. break;
  12564. case 3:
  12565. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12566. break;
  12567. case 4:
  12568. case 5:
  12569. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12570. break;
  12571. case 6:
  12572. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12573. break;
  12574. case 7:
  12575. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12576. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12577. break;
  12578. case 9:
  12579. /* Drop through - unsupported since execlist only. */
  12580. default:
  12581. /* Default just returns -ENODEV to indicate unsupported */
  12582. dev_priv->display.queue_flip = intel_default_queue_flip;
  12583. }
  12584. mutex_init(&dev_priv->pps_mutex);
  12585. }
  12586. /*
  12587. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12588. * resume, or other times. This quirk makes sure that's the case for
  12589. * affected systems.
  12590. */
  12591. static void quirk_pipea_force(struct drm_device *dev)
  12592. {
  12593. struct drm_i915_private *dev_priv = dev->dev_private;
  12594. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12595. DRM_INFO("applying pipe a force quirk\n");
  12596. }
  12597. static void quirk_pipeb_force(struct drm_device *dev)
  12598. {
  12599. struct drm_i915_private *dev_priv = dev->dev_private;
  12600. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12601. DRM_INFO("applying pipe b force quirk\n");
  12602. }
  12603. /*
  12604. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12605. */
  12606. static void quirk_ssc_force_disable(struct drm_device *dev)
  12607. {
  12608. struct drm_i915_private *dev_priv = dev->dev_private;
  12609. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12610. DRM_INFO("applying lvds SSC disable quirk\n");
  12611. }
  12612. /*
  12613. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12614. * brightness value
  12615. */
  12616. static void quirk_invert_brightness(struct drm_device *dev)
  12617. {
  12618. struct drm_i915_private *dev_priv = dev->dev_private;
  12619. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12620. DRM_INFO("applying inverted panel brightness quirk\n");
  12621. }
  12622. /* Some VBT's incorrectly indicate no backlight is present */
  12623. static void quirk_backlight_present(struct drm_device *dev)
  12624. {
  12625. struct drm_i915_private *dev_priv = dev->dev_private;
  12626. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12627. DRM_INFO("applying backlight present quirk\n");
  12628. }
  12629. struct intel_quirk {
  12630. int device;
  12631. int subsystem_vendor;
  12632. int subsystem_device;
  12633. void (*hook)(struct drm_device *dev);
  12634. };
  12635. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12636. struct intel_dmi_quirk {
  12637. void (*hook)(struct drm_device *dev);
  12638. const struct dmi_system_id (*dmi_id_list)[];
  12639. };
  12640. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12641. {
  12642. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12643. return 1;
  12644. }
  12645. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12646. {
  12647. .dmi_id_list = &(const struct dmi_system_id[]) {
  12648. {
  12649. .callback = intel_dmi_reverse_brightness,
  12650. .ident = "NCR Corporation",
  12651. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12652. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12653. },
  12654. },
  12655. { } /* terminating entry */
  12656. },
  12657. .hook = quirk_invert_brightness,
  12658. },
  12659. };
  12660. static struct intel_quirk intel_quirks[] = {
  12661. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12662. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12663. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12664. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12665. /* 830 needs to leave pipe A & dpll A up */
  12666. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12667. /* 830 needs to leave pipe B & dpll B up */
  12668. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12669. /* Lenovo U160 cannot use SSC on LVDS */
  12670. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12671. /* Sony Vaio Y cannot use SSC on LVDS */
  12672. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12673. /* Acer Aspire 5734Z must invert backlight brightness */
  12674. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12675. /* Acer/eMachines G725 */
  12676. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12677. /* Acer/eMachines e725 */
  12678. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12679. /* Acer/Packard Bell NCL20 */
  12680. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12681. /* Acer Aspire 4736Z */
  12682. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12683. /* Acer Aspire 5336 */
  12684. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12685. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12686. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12687. /* Acer C720 Chromebook (Core i3 4005U) */
  12688. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12689. /* Apple Macbook 2,1 (Core 2 T7400) */
  12690. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12691. /* Apple Macbook 4,1 */
  12692. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12693. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12694. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12695. /* HP Chromebook 14 (Celeron 2955U) */
  12696. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12697. /* Dell Chromebook 11 */
  12698. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12699. /* Dell Chromebook 11 (2015 version) */
  12700. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12701. };
  12702. static void intel_init_quirks(struct drm_device *dev)
  12703. {
  12704. struct pci_dev *d = dev->pdev;
  12705. int i;
  12706. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12707. struct intel_quirk *q = &intel_quirks[i];
  12708. if (d->device == q->device &&
  12709. (d->subsystem_vendor == q->subsystem_vendor ||
  12710. q->subsystem_vendor == PCI_ANY_ID) &&
  12711. (d->subsystem_device == q->subsystem_device ||
  12712. q->subsystem_device == PCI_ANY_ID))
  12713. q->hook(dev);
  12714. }
  12715. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12716. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12717. intel_dmi_quirks[i].hook(dev);
  12718. }
  12719. }
  12720. /* Disable the VGA plane that we never use */
  12721. static void i915_disable_vga(struct drm_device *dev)
  12722. {
  12723. struct drm_i915_private *dev_priv = dev->dev_private;
  12724. u8 sr1;
  12725. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  12726. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12727. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12728. outb(SR01, VGA_SR_INDEX);
  12729. sr1 = inb(VGA_SR_DATA);
  12730. outb(sr1 | 1<<5, VGA_SR_DATA);
  12731. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12732. udelay(300);
  12733. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12734. POSTING_READ(vga_reg);
  12735. }
  12736. void intel_modeset_init_hw(struct drm_device *dev)
  12737. {
  12738. struct drm_i915_private *dev_priv = dev->dev_private;
  12739. intel_update_cdclk(dev);
  12740. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  12741. intel_init_clock_gating(dev);
  12742. intel_enable_gt_powersave(dev);
  12743. }
  12744. /*
  12745. * Calculate what we think the watermarks should be for the state we've read
  12746. * out of the hardware and then immediately program those watermarks so that
  12747. * we ensure the hardware settings match our internal state.
  12748. *
  12749. * We can calculate what we think WM's should be by creating a duplicate of the
  12750. * current state (which was constructed during hardware readout) and running it
  12751. * through the atomic check code to calculate new watermark values in the
  12752. * state object.
  12753. */
  12754. static void sanitize_watermarks(struct drm_device *dev)
  12755. {
  12756. struct drm_i915_private *dev_priv = to_i915(dev);
  12757. struct drm_atomic_state *state;
  12758. struct drm_crtc *crtc;
  12759. struct drm_crtc_state *cstate;
  12760. struct drm_modeset_acquire_ctx ctx;
  12761. int ret;
  12762. int i;
  12763. /* Only supported on platforms that use atomic watermark design */
  12764. if (!dev_priv->display.program_watermarks)
  12765. return;
  12766. /*
  12767. * We need to hold connection_mutex before calling duplicate_state so
  12768. * that the connector loop is protected.
  12769. */
  12770. drm_modeset_acquire_init(&ctx, 0);
  12771. retry:
  12772. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12773. if (ret == -EDEADLK) {
  12774. drm_modeset_backoff(&ctx);
  12775. goto retry;
  12776. } else if (WARN_ON(ret)) {
  12777. goto fail;
  12778. }
  12779. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12780. if (WARN_ON(IS_ERR(state)))
  12781. goto fail;
  12782. ret = intel_atomic_check(dev, state);
  12783. if (ret) {
  12784. /*
  12785. * If we fail here, it means that the hardware appears to be
  12786. * programmed in a way that shouldn't be possible, given our
  12787. * understanding of watermark requirements. This might mean a
  12788. * mistake in the hardware readout code or a mistake in the
  12789. * watermark calculations for a given platform. Raise a WARN
  12790. * so that this is noticeable.
  12791. *
  12792. * If this actually happens, we'll have to just leave the
  12793. * BIOS-programmed watermarks untouched and hope for the best.
  12794. */
  12795. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12796. goto fail;
  12797. }
  12798. /* Write calculated watermark values back */
  12799. to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
  12800. for_each_crtc_in_state(state, crtc, cstate, i) {
  12801. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12802. dev_priv->display.program_watermarks(cs);
  12803. }
  12804. drm_atomic_state_free(state);
  12805. fail:
  12806. drm_modeset_drop_locks(&ctx);
  12807. drm_modeset_acquire_fini(&ctx);
  12808. }
  12809. void intel_modeset_init(struct drm_device *dev)
  12810. {
  12811. struct drm_i915_private *dev_priv = dev->dev_private;
  12812. int sprite, ret;
  12813. enum pipe pipe;
  12814. struct intel_crtc *crtc;
  12815. drm_mode_config_init(dev);
  12816. dev->mode_config.min_width = 0;
  12817. dev->mode_config.min_height = 0;
  12818. dev->mode_config.preferred_depth = 24;
  12819. dev->mode_config.prefer_shadow = 1;
  12820. dev->mode_config.allow_fb_modifiers = true;
  12821. dev->mode_config.funcs = &intel_mode_funcs;
  12822. intel_init_quirks(dev);
  12823. intel_init_pm(dev);
  12824. if (INTEL_INFO(dev)->num_pipes == 0)
  12825. return;
  12826. /*
  12827. * There may be no VBT; and if the BIOS enabled SSC we can
  12828. * just keep using it to avoid unnecessary flicker. Whereas if the
  12829. * BIOS isn't using it, don't assume it will work even if the VBT
  12830. * indicates as much.
  12831. */
  12832. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12833. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12834. DREF_SSC1_ENABLE);
  12835. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12836. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12837. bios_lvds_use_ssc ? "en" : "dis",
  12838. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12839. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12840. }
  12841. }
  12842. intel_init_display(dev);
  12843. intel_init_audio(dev);
  12844. if (IS_GEN2(dev)) {
  12845. dev->mode_config.max_width = 2048;
  12846. dev->mode_config.max_height = 2048;
  12847. } else if (IS_GEN3(dev)) {
  12848. dev->mode_config.max_width = 4096;
  12849. dev->mode_config.max_height = 4096;
  12850. } else {
  12851. dev->mode_config.max_width = 8192;
  12852. dev->mode_config.max_height = 8192;
  12853. }
  12854. if (IS_845G(dev) || IS_I865G(dev)) {
  12855. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12856. dev->mode_config.cursor_height = 1023;
  12857. } else if (IS_GEN2(dev)) {
  12858. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12859. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12860. } else {
  12861. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12862. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12863. }
  12864. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12865. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12866. INTEL_INFO(dev)->num_pipes,
  12867. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12868. for_each_pipe(dev_priv, pipe) {
  12869. intel_crtc_init(dev, pipe);
  12870. for_each_sprite(dev_priv, pipe, sprite) {
  12871. ret = intel_plane_init(dev, pipe, sprite);
  12872. if (ret)
  12873. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12874. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12875. }
  12876. }
  12877. intel_update_czclk(dev_priv);
  12878. intel_update_cdclk(dev);
  12879. intel_shared_dpll_init(dev);
  12880. /* Just disable it once at startup */
  12881. i915_disable_vga(dev);
  12882. intel_setup_outputs(dev);
  12883. drm_modeset_lock_all(dev);
  12884. intel_modeset_setup_hw_state(dev);
  12885. drm_modeset_unlock_all(dev);
  12886. for_each_intel_crtc(dev, crtc) {
  12887. struct intel_initial_plane_config plane_config = {};
  12888. if (!crtc->active)
  12889. continue;
  12890. /*
  12891. * Note that reserving the BIOS fb up front prevents us
  12892. * from stuffing other stolen allocations like the ring
  12893. * on top. This prevents some ugliness at boot time, and
  12894. * can even allow for smooth boot transitions if the BIOS
  12895. * fb is large enough for the active pipe configuration.
  12896. */
  12897. dev_priv->display.get_initial_plane_config(crtc,
  12898. &plane_config);
  12899. /*
  12900. * If the fb is shared between multiple heads, we'll
  12901. * just get the first one.
  12902. */
  12903. intel_find_initial_plane_obj(crtc, &plane_config);
  12904. }
  12905. /*
  12906. * Make sure hardware watermarks really match the state we read out.
  12907. * Note that we need to do this after reconstructing the BIOS fb's
  12908. * since the watermark calculation done here will use pstate->fb.
  12909. */
  12910. sanitize_watermarks(dev);
  12911. }
  12912. static void intel_enable_pipe_a(struct drm_device *dev)
  12913. {
  12914. struct intel_connector *connector;
  12915. struct drm_connector *crt = NULL;
  12916. struct intel_load_detect_pipe load_detect_temp;
  12917. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12918. /* We can't just switch on the pipe A, we need to set things up with a
  12919. * proper mode and output configuration. As a gross hack, enable pipe A
  12920. * by enabling the load detect pipe once. */
  12921. for_each_intel_connector(dev, connector) {
  12922. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12923. crt = &connector->base;
  12924. break;
  12925. }
  12926. }
  12927. if (!crt)
  12928. return;
  12929. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12930. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12931. }
  12932. static bool
  12933. intel_check_plane_mapping(struct intel_crtc *crtc)
  12934. {
  12935. struct drm_device *dev = crtc->base.dev;
  12936. struct drm_i915_private *dev_priv = dev->dev_private;
  12937. u32 val;
  12938. if (INTEL_INFO(dev)->num_pipes == 1)
  12939. return true;
  12940. val = I915_READ(DSPCNTR(!crtc->plane));
  12941. if ((val & DISPLAY_PLANE_ENABLE) &&
  12942. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12943. return false;
  12944. return true;
  12945. }
  12946. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12947. {
  12948. struct drm_device *dev = crtc->base.dev;
  12949. struct intel_encoder *encoder;
  12950. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12951. return true;
  12952. return false;
  12953. }
  12954. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12955. {
  12956. struct drm_device *dev = crtc->base.dev;
  12957. struct drm_i915_private *dev_priv = dev->dev_private;
  12958. i915_reg_t reg = PIPECONF(crtc->config->cpu_transcoder);
  12959. /* Clear any frame start delays used for debugging left by the BIOS */
  12960. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12961. /* restore vblank interrupts to correct state */
  12962. drm_crtc_vblank_reset(&crtc->base);
  12963. if (crtc->active) {
  12964. struct intel_plane *plane;
  12965. drm_crtc_vblank_on(&crtc->base);
  12966. /* Disable everything but the primary plane */
  12967. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12968. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12969. continue;
  12970. plane->disable_plane(&plane->base, &crtc->base);
  12971. }
  12972. }
  12973. /* We need to sanitize the plane -> pipe mapping first because this will
  12974. * disable the crtc (and hence change the state) if it is wrong. Note
  12975. * that gen4+ has a fixed plane -> pipe mapping. */
  12976. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12977. bool plane;
  12978. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12979. crtc->base.base.id);
  12980. /* Pipe has the wrong plane attached and the plane is active.
  12981. * Temporarily change the plane mapping and disable everything
  12982. * ... */
  12983. plane = crtc->plane;
  12984. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12985. crtc->plane = !plane;
  12986. intel_crtc_disable_noatomic(&crtc->base);
  12987. crtc->plane = plane;
  12988. }
  12989. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12990. crtc->pipe == PIPE_A && !crtc->active) {
  12991. /* BIOS forgot to enable pipe A, this mostly happens after
  12992. * resume. Force-enable the pipe to fix this, the update_dpms
  12993. * call below we restore the pipe to the right state, but leave
  12994. * the required bits on. */
  12995. intel_enable_pipe_a(dev);
  12996. }
  12997. /* Adjust the state of the output pipe according to whether we
  12998. * have active connectors/encoders. */
  12999. if (!intel_crtc_has_encoders(crtc))
  13000. intel_crtc_disable_noatomic(&crtc->base);
  13001. if (crtc->active != crtc->base.state->active) {
  13002. struct intel_encoder *encoder;
  13003. /* This can happen either due to bugs in the get_hw_state
  13004. * functions or because of calls to intel_crtc_disable_noatomic,
  13005. * or because the pipe is force-enabled due to the
  13006. * pipe A quirk. */
  13007. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  13008. crtc->base.base.id,
  13009. crtc->base.state->enable ? "enabled" : "disabled",
  13010. crtc->active ? "enabled" : "disabled");
  13011. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
  13012. crtc->base.state->active = crtc->active;
  13013. crtc->base.enabled = crtc->active;
  13014. crtc->base.state->connector_mask = 0;
  13015. crtc->base.state->encoder_mask = 0;
  13016. /* Because we only establish the connector -> encoder ->
  13017. * crtc links if something is active, this means the
  13018. * crtc is now deactivated. Break the links. connector
  13019. * -> encoder links are only establish when things are
  13020. * actually up, hence no need to break them. */
  13021. WARN_ON(crtc->active);
  13022. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13023. encoder->base.crtc = NULL;
  13024. }
  13025. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  13026. /*
  13027. * We start out with underrun reporting disabled to avoid races.
  13028. * For correct bookkeeping mark this on active crtcs.
  13029. *
  13030. * Also on gmch platforms we dont have any hardware bits to
  13031. * disable the underrun reporting. Which means we need to start
  13032. * out with underrun reporting disabled also on inactive pipes,
  13033. * since otherwise we'll complain about the garbage we read when
  13034. * e.g. coming up after runtime pm.
  13035. *
  13036. * No protection against concurrent access is required - at
  13037. * worst a fifo underrun happens which also sets this to false.
  13038. */
  13039. crtc->cpu_fifo_underrun_disabled = true;
  13040. crtc->pch_fifo_underrun_disabled = true;
  13041. }
  13042. }
  13043. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13044. {
  13045. struct intel_connector *connector;
  13046. struct drm_device *dev = encoder->base.dev;
  13047. bool active = false;
  13048. /* We need to check both for a crtc link (meaning that the
  13049. * encoder is active and trying to read from a pipe) and the
  13050. * pipe itself being active. */
  13051. bool has_active_crtc = encoder->base.crtc &&
  13052. to_intel_crtc(encoder->base.crtc)->active;
  13053. for_each_intel_connector(dev, connector) {
  13054. if (connector->base.encoder != &encoder->base)
  13055. continue;
  13056. active = true;
  13057. break;
  13058. }
  13059. if (active && !has_active_crtc) {
  13060. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13061. encoder->base.base.id,
  13062. encoder->base.name);
  13063. /* Connector is active, but has no active pipe. This is
  13064. * fallout from our resume register restoring. Disable
  13065. * the encoder manually again. */
  13066. if (encoder->base.crtc) {
  13067. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13068. encoder->base.base.id,
  13069. encoder->base.name);
  13070. encoder->disable(encoder);
  13071. if (encoder->post_disable)
  13072. encoder->post_disable(encoder);
  13073. }
  13074. encoder->base.crtc = NULL;
  13075. /* Inconsistent output/port/pipe state happens presumably due to
  13076. * a bug in one of the get_hw_state functions. Or someplace else
  13077. * in our code, like the register restore mess on resume. Clamp
  13078. * things to off as a safer default. */
  13079. for_each_intel_connector(dev, connector) {
  13080. if (connector->encoder != encoder)
  13081. continue;
  13082. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13083. connector->base.encoder = NULL;
  13084. }
  13085. }
  13086. /* Enabled encoders without active connectors will be fixed in
  13087. * the crtc fixup. */
  13088. }
  13089. void i915_redisable_vga_power_on(struct drm_device *dev)
  13090. {
  13091. struct drm_i915_private *dev_priv = dev->dev_private;
  13092. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13093. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13094. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13095. i915_disable_vga(dev);
  13096. }
  13097. }
  13098. void i915_redisable_vga(struct drm_device *dev)
  13099. {
  13100. struct drm_i915_private *dev_priv = dev->dev_private;
  13101. /* This function can be called both from intel_modeset_setup_hw_state or
  13102. * at a very early point in our resume sequence, where the power well
  13103. * structures are not yet restored. Since this function is at a very
  13104. * paranoid "someone might have enabled VGA while we were not looking"
  13105. * level, just check if the power well is enabled instead of trying to
  13106. * follow the "don't touch the power well if we don't need it" policy
  13107. * the rest of the driver uses. */
  13108. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  13109. return;
  13110. i915_redisable_vga_power_on(dev);
  13111. }
  13112. static bool primary_get_hw_state(struct intel_plane *plane)
  13113. {
  13114. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  13115. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  13116. }
  13117. /* FIXME read out full plane state for all planes */
  13118. static void readout_plane_state(struct intel_crtc *crtc)
  13119. {
  13120. struct drm_plane *primary = crtc->base.primary;
  13121. struct intel_plane_state *plane_state =
  13122. to_intel_plane_state(primary->state);
  13123. plane_state->visible = crtc->active &&
  13124. primary_get_hw_state(to_intel_plane(primary));
  13125. if (plane_state->visible)
  13126. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  13127. }
  13128. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13129. {
  13130. struct drm_i915_private *dev_priv = dev->dev_private;
  13131. enum pipe pipe;
  13132. struct intel_crtc *crtc;
  13133. struct intel_encoder *encoder;
  13134. struct intel_connector *connector;
  13135. int i;
  13136. dev_priv->active_crtcs = 0;
  13137. for_each_intel_crtc(dev, crtc) {
  13138. struct intel_crtc_state *crtc_state = crtc->config;
  13139. int pixclk = 0;
  13140. __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
  13141. memset(crtc_state, 0, sizeof(*crtc_state));
  13142. crtc_state->base.crtc = &crtc->base;
  13143. crtc_state->base.active = crtc_state->base.enable =
  13144. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13145. crtc->base.enabled = crtc_state->base.enable;
  13146. crtc->active = crtc_state->base.active;
  13147. if (crtc_state->base.active) {
  13148. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13149. if (IS_BROADWELL(dev_priv)) {
  13150. pixclk = ilk_pipe_pixel_rate(crtc_state);
  13151. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  13152. if (crtc_state->ips_enabled)
  13153. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  13154. } else if (IS_VALLEYVIEW(dev_priv) ||
  13155. IS_CHERRYVIEW(dev_priv) ||
  13156. IS_BROXTON(dev_priv))
  13157. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  13158. else
  13159. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  13160. }
  13161. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  13162. readout_plane_state(crtc);
  13163. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  13164. crtc->base.base.id,
  13165. crtc->active ? "enabled" : "disabled");
  13166. }
  13167. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13168. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13169. pll->on = pll->get_hw_state(dev_priv, pll,
  13170. &pll->config.hw_state);
  13171. pll->active = 0;
  13172. pll->config.crtc_mask = 0;
  13173. for_each_intel_crtc(dev, crtc) {
  13174. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  13175. pll->active++;
  13176. pll->config.crtc_mask |= 1 << crtc->pipe;
  13177. }
  13178. }
  13179. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13180. pll->name, pll->config.crtc_mask, pll->on);
  13181. if (pll->config.crtc_mask)
  13182. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  13183. }
  13184. for_each_intel_encoder(dev, encoder) {
  13185. pipe = 0;
  13186. if (encoder->get_hw_state(encoder, &pipe)) {
  13187. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13188. encoder->base.crtc = &crtc->base;
  13189. encoder->get_config(encoder, crtc->config);
  13190. } else {
  13191. encoder->base.crtc = NULL;
  13192. }
  13193. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13194. encoder->base.base.id,
  13195. encoder->base.name,
  13196. encoder->base.crtc ? "enabled" : "disabled",
  13197. pipe_name(pipe));
  13198. }
  13199. for_each_intel_connector(dev, connector) {
  13200. if (connector->get_hw_state(connector)) {
  13201. connector->base.dpms = DRM_MODE_DPMS_ON;
  13202. encoder = connector->encoder;
  13203. connector->base.encoder = &encoder->base;
  13204. if (encoder->base.crtc &&
  13205. encoder->base.crtc->state->active) {
  13206. /*
  13207. * This has to be done during hardware readout
  13208. * because anything calling .crtc_disable may
  13209. * rely on the connector_mask being accurate.
  13210. */
  13211. encoder->base.crtc->state->connector_mask |=
  13212. 1 << drm_connector_index(&connector->base);
  13213. encoder->base.crtc->state->encoder_mask |=
  13214. 1 << drm_encoder_index(&encoder->base);
  13215. }
  13216. } else {
  13217. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13218. connector->base.encoder = NULL;
  13219. }
  13220. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13221. connector->base.base.id,
  13222. connector->base.name,
  13223. connector->base.encoder ? "enabled" : "disabled");
  13224. }
  13225. for_each_intel_crtc(dev, crtc) {
  13226. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  13227. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13228. if (crtc->base.state->active) {
  13229. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  13230. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  13231. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13232. /*
  13233. * The initial mode needs to be set in order to keep
  13234. * the atomic core happy. It wants a valid mode if the
  13235. * crtc's enabled, so we do the above call.
  13236. *
  13237. * At this point some state updated by the connectors
  13238. * in their ->detect() callback has not run yet, so
  13239. * no recalculation can be done yet.
  13240. *
  13241. * Even if we could do a recalculation and modeset
  13242. * right now it would cause a double modeset if
  13243. * fbdev or userspace chooses a different initial mode.
  13244. *
  13245. * If that happens, someone indicated they wanted a
  13246. * mode change, which means it's safe to do a full
  13247. * recalculation.
  13248. */
  13249. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  13250. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  13251. update_scanline_offset(crtc);
  13252. }
  13253. }
  13254. }
  13255. /* Scan out the current hw modeset state,
  13256. * and sanitizes it to the current state
  13257. */
  13258. static void
  13259. intel_modeset_setup_hw_state(struct drm_device *dev)
  13260. {
  13261. struct drm_i915_private *dev_priv = dev->dev_private;
  13262. enum pipe pipe;
  13263. struct intel_crtc *crtc;
  13264. struct intel_encoder *encoder;
  13265. int i;
  13266. intel_modeset_readout_hw_state(dev);
  13267. /* HW state is read out, now we need to sanitize this mess. */
  13268. for_each_intel_encoder(dev, encoder) {
  13269. intel_sanitize_encoder(encoder);
  13270. }
  13271. for_each_pipe(dev_priv, pipe) {
  13272. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13273. intel_sanitize_crtc(crtc);
  13274. intel_dump_pipe_config(crtc, crtc->config,
  13275. "[setup_hw_state]");
  13276. }
  13277. intel_modeset_update_connector_atomic_state(dev);
  13278. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13279. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13280. if (!pll->on || pll->active)
  13281. continue;
  13282. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13283. pll->disable(dev_priv, pll);
  13284. pll->on = false;
  13285. }
  13286. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  13287. vlv_wm_get_hw_state(dev);
  13288. else if (IS_GEN9(dev))
  13289. skl_wm_get_hw_state(dev);
  13290. else if (HAS_PCH_SPLIT(dev))
  13291. ilk_wm_get_hw_state(dev);
  13292. for_each_intel_crtc(dev, crtc) {
  13293. unsigned long put_domains;
  13294. put_domains = modeset_get_crtc_power_domains(&crtc->base);
  13295. if (WARN_ON(put_domains))
  13296. modeset_put_power_domains(dev_priv, put_domains);
  13297. }
  13298. intel_display_set_init_power(dev_priv, false);
  13299. intel_fbc_init_pipe_state(dev_priv);
  13300. }
  13301. void intel_display_resume(struct drm_device *dev)
  13302. {
  13303. struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
  13304. struct intel_connector *conn;
  13305. struct intel_plane *plane;
  13306. struct drm_crtc *crtc;
  13307. int ret;
  13308. if (!state)
  13309. return;
  13310. state->acquire_ctx = dev->mode_config.acquire_ctx;
  13311. for_each_crtc(dev, crtc) {
  13312. struct drm_crtc_state *crtc_state =
  13313. drm_atomic_get_crtc_state(state, crtc);
  13314. ret = PTR_ERR_OR_ZERO(crtc_state);
  13315. if (ret)
  13316. goto err;
  13317. /* force a restore */
  13318. crtc_state->mode_changed = true;
  13319. }
  13320. for_each_intel_plane(dev, plane) {
  13321. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
  13322. if (ret)
  13323. goto err;
  13324. }
  13325. for_each_intel_connector(dev, conn) {
  13326. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
  13327. if (ret)
  13328. goto err;
  13329. }
  13330. intel_modeset_setup_hw_state(dev);
  13331. i915_redisable_vga(dev);
  13332. ret = drm_atomic_commit(state);
  13333. if (!ret)
  13334. return;
  13335. err:
  13336. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13337. drm_atomic_state_free(state);
  13338. }
  13339. void intel_modeset_gem_init(struct drm_device *dev)
  13340. {
  13341. struct drm_crtc *c;
  13342. struct drm_i915_gem_object *obj;
  13343. int ret;
  13344. mutex_lock(&dev->struct_mutex);
  13345. intel_init_gt_powersave(dev);
  13346. mutex_unlock(&dev->struct_mutex);
  13347. intel_modeset_init_hw(dev);
  13348. intel_setup_overlay(dev);
  13349. /*
  13350. * Make sure any fbs we allocated at startup are properly
  13351. * pinned & fenced. When we do the allocation it's too early
  13352. * for this.
  13353. */
  13354. for_each_crtc(dev, c) {
  13355. obj = intel_fb_obj(c->primary->fb);
  13356. if (obj == NULL)
  13357. continue;
  13358. mutex_lock(&dev->struct_mutex);
  13359. ret = intel_pin_and_fence_fb_obj(c->primary,
  13360. c->primary->fb,
  13361. c->primary->state);
  13362. mutex_unlock(&dev->struct_mutex);
  13363. if (ret) {
  13364. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13365. to_intel_crtc(c)->pipe);
  13366. drm_framebuffer_unreference(c->primary->fb);
  13367. c->primary->fb = NULL;
  13368. c->primary->crtc = c->primary->state->crtc = NULL;
  13369. update_state_fb(c->primary);
  13370. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13371. }
  13372. }
  13373. intel_backlight_register(dev);
  13374. }
  13375. void intel_connector_unregister(struct intel_connector *intel_connector)
  13376. {
  13377. struct drm_connector *connector = &intel_connector->base;
  13378. intel_panel_destroy_backlight(connector);
  13379. drm_connector_unregister(connector);
  13380. }
  13381. void intel_modeset_cleanup(struct drm_device *dev)
  13382. {
  13383. struct drm_i915_private *dev_priv = dev->dev_private;
  13384. struct intel_connector *connector;
  13385. intel_disable_gt_powersave(dev);
  13386. intel_backlight_unregister(dev);
  13387. /*
  13388. * Interrupts and polling as the first thing to avoid creating havoc.
  13389. * Too much stuff here (turning of connectors, ...) would
  13390. * experience fancy races otherwise.
  13391. */
  13392. intel_irq_uninstall(dev_priv);
  13393. /*
  13394. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13395. * poll handlers. Hence disable polling after hpd handling is shut down.
  13396. */
  13397. drm_kms_helper_poll_fini(dev);
  13398. intel_unregister_dsm_handler();
  13399. intel_fbc_global_disable(dev_priv);
  13400. /* flush any delayed tasks or pending work */
  13401. flush_scheduled_work();
  13402. /* destroy the backlight and sysfs files before encoders/connectors */
  13403. for_each_intel_connector(dev, connector)
  13404. connector->unregister(connector);
  13405. drm_mode_config_cleanup(dev);
  13406. intel_cleanup_overlay(dev);
  13407. mutex_lock(&dev->struct_mutex);
  13408. intel_cleanup_gt_powersave(dev);
  13409. mutex_unlock(&dev->struct_mutex);
  13410. intel_teardown_gmbus(dev);
  13411. }
  13412. /*
  13413. * Return which encoder is currently attached for connector.
  13414. */
  13415. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13416. {
  13417. return &intel_attached_encoder(connector)->base;
  13418. }
  13419. void intel_connector_attach_encoder(struct intel_connector *connector,
  13420. struct intel_encoder *encoder)
  13421. {
  13422. connector->encoder = encoder;
  13423. drm_mode_connector_attach_encoder(&connector->base,
  13424. &encoder->base);
  13425. }
  13426. /*
  13427. * set vga decode state - true == enable VGA decode
  13428. */
  13429. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13430. {
  13431. struct drm_i915_private *dev_priv = dev->dev_private;
  13432. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13433. u16 gmch_ctrl;
  13434. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13435. DRM_ERROR("failed to read control word\n");
  13436. return -EIO;
  13437. }
  13438. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13439. return 0;
  13440. if (state)
  13441. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13442. else
  13443. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13444. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13445. DRM_ERROR("failed to write control word\n");
  13446. return -EIO;
  13447. }
  13448. return 0;
  13449. }
  13450. struct intel_display_error_state {
  13451. u32 power_well_driver;
  13452. int num_transcoders;
  13453. struct intel_cursor_error_state {
  13454. u32 control;
  13455. u32 position;
  13456. u32 base;
  13457. u32 size;
  13458. } cursor[I915_MAX_PIPES];
  13459. struct intel_pipe_error_state {
  13460. bool power_domain_on;
  13461. u32 source;
  13462. u32 stat;
  13463. } pipe[I915_MAX_PIPES];
  13464. struct intel_plane_error_state {
  13465. u32 control;
  13466. u32 stride;
  13467. u32 size;
  13468. u32 pos;
  13469. u32 addr;
  13470. u32 surface;
  13471. u32 tile_offset;
  13472. } plane[I915_MAX_PIPES];
  13473. struct intel_transcoder_error_state {
  13474. bool power_domain_on;
  13475. enum transcoder cpu_transcoder;
  13476. u32 conf;
  13477. u32 htotal;
  13478. u32 hblank;
  13479. u32 hsync;
  13480. u32 vtotal;
  13481. u32 vblank;
  13482. u32 vsync;
  13483. } transcoder[4];
  13484. };
  13485. struct intel_display_error_state *
  13486. intel_display_capture_error_state(struct drm_device *dev)
  13487. {
  13488. struct drm_i915_private *dev_priv = dev->dev_private;
  13489. struct intel_display_error_state *error;
  13490. int transcoders[] = {
  13491. TRANSCODER_A,
  13492. TRANSCODER_B,
  13493. TRANSCODER_C,
  13494. TRANSCODER_EDP,
  13495. };
  13496. int i;
  13497. if (INTEL_INFO(dev)->num_pipes == 0)
  13498. return NULL;
  13499. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13500. if (error == NULL)
  13501. return NULL;
  13502. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13503. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13504. for_each_pipe(dev_priv, i) {
  13505. error->pipe[i].power_domain_on =
  13506. __intel_display_power_is_enabled(dev_priv,
  13507. POWER_DOMAIN_PIPE(i));
  13508. if (!error->pipe[i].power_domain_on)
  13509. continue;
  13510. error->cursor[i].control = I915_READ(CURCNTR(i));
  13511. error->cursor[i].position = I915_READ(CURPOS(i));
  13512. error->cursor[i].base = I915_READ(CURBASE(i));
  13513. error->plane[i].control = I915_READ(DSPCNTR(i));
  13514. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13515. if (INTEL_INFO(dev)->gen <= 3) {
  13516. error->plane[i].size = I915_READ(DSPSIZE(i));
  13517. error->plane[i].pos = I915_READ(DSPPOS(i));
  13518. }
  13519. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13520. error->plane[i].addr = I915_READ(DSPADDR(i));
  13521. if (INTEL_INFO(dev)->gen >= 4) {
  13522. error->plane[i].surface = I915_READ(DSPSURF(i));
  13523. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13524. }
  13525. error->pipe[i].source = I915_READ(PIPESRC(i));
  13526. if (HAS_GMCH_DISPLAY(dev))
  13527. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13528. }
  13529. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13530. if (HAS_DDI(dev_priv->dev))
  13531. error->num_transcoders++; /* Account for eDP. */
  13532. for (i = 0; i < error->num_transcoders; i++) {
  13533. enum transcoder cpu_transcoder = transcoders[i];
  13534. error->transcoder[i].power_domain_on =
  13535. __intel_display_power_is_enabled(dev_priv,
  13536. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13537. if (!error->transcoder[i].power_domain_on)
  13538. continue;
  13539. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13540. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13541. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13542. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13543. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13544. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13545. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13546. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13547. }
  13548. return error;
  13549. }
  13550. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13551. void
  13552. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13553. struct drm_device *dev,
  13554. struct intel_display_error_state *error)
  13555. {
  13556. struct drm_i915_private *dev_priv = dev->dev_private;
  13557. int i;
  13558. if (!error)
  13559. return;
  13560. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13561. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13562. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13563. error->power_well_driver);
  13564. for_each_pipe(dev_priv, i) {
  13565. err_printf(m, "Pipe [%d]:\n", i);
  13566. err_printf(m, " Power: %s\n",
  13567. onoff(error->pipe[i].power_domain_on));
  13568. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13569. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13570. err_printf(m, "Plane [%d]:\n", i);
  13571. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13572. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13573. if (INTEL_INFO(dev)->gen <= 3) {
  13574. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13575. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13576. }
  13577. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13578. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13579. if (INTEL_INFO(dev)->gen >= 4) {
  13580. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13581. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13582. }
  13583. err_printf(m, "Cursor [%d]:\n", i);
  13584. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13585. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13586. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13587. }
  13588. for (i = 0; i < error->num_transcoders; i++) {
  13589. err_printf(m, "CPU transcoder: %c\n",
  13590. transcoder_name(error->transcoder[i].cpu_transcoder));
  13591. err_printf(m, " Power: %s\n",
  13592. onoff(error->transcoder[i].power_domain_on));
  13593. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13594. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13595. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13596. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13597. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13598. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13599. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13600. }
  13601. }