intel_display.c 438 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918
  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. /* Primary plane formats for gen <= 3 */
  47. static const uint32_t i8xx_primary_formats[] = {
  48. DRM_FORMAT_C8,
  49. DRM_FORMAT_RGB565,
  50. DRM_FORMAT_XRGB1555,
  51. DRM_FORMAT_XRGB8888,
  52. };
  53. /* Primary plane formats for gen >= 4 */
  54. static const uint32_t i965_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB8888,
  58. DRM_FORMAT_XBGR8888,
  59. DRM_FORMAT_XRGB2101010,
  60. DRM_FORMAT_XBGR2101010,
  61. };
  62. static const uint32_t skl_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_ARGB8888,
  68. DRM_FORMAT_ABGR8888,
  69. DRM_FORMAT_XRGB2101010,
  70. DRM_FORMAT_XBGR2101010,
  71. DRM_FORMAT_YUYV,
  72. DRM_FORMAT_YVYU,
  73. DRM_FORMAT_UYVY,
  74. DRM_FORMAT_VYUY,
  75. };
  76. /* Cursor formats */
  77. static const uint32_t intel_cursor_formats[] = {
  78. DRM_FORMAT_ARGB8888,
  79. };
  80. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  81. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  82. struct intel_crtc_state *pipe_config);
  83. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  84. struct intel_crtc_state *pipe_config);
  85. static int intel_framebuffer_init(struct drm_device *dev,
  86. struct intel_framebuffer *ifb,
  87. struct drm_mode_fb_cmd2 *mode_cmd,
  88. struct drm_i915_gem_object *obj);
  89. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  90. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  91. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  92. struct intel_link_m_n *m_n,
  93. struct intel_link_m_n *m2_n2);
  94. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  95. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  96. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  97. static void vlv_prepare_pll(struct intel_crtc *crtc,
  98. const struct intel_crtc_state *pipe_config);
  99. static void chv_prepare_pll(struct intel_crtc *crtc,
  100. const struct intel_crtc_state *pipe_config);
  101. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  102. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  103. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  104. struct intel_crtc_state *crtc_state);
  105. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  106. int num_connectors);
  107. static void skylake_pfit_enable(struct intel_crtc *crtc);
  108. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  109. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  110. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  111. typedef struct {
  112. int min, max;
  113. } intel_range_t;
  114. typedef struct {
  115. int dot_limit;
  116. int p2_slow, p2_fast;
  117. } intel_p2_t;
  118. typedef struct intel_limit intel_limit_t;
  119. struct intel_limit {
  120. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  121. intel_p2_t p2;
  122. };
  123. /* returns HPLL frequency in kHz */
  124. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  125. {
  126. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  127. /* Obtain SKU information */
  128. mutex_lock(&dev_priv->sb_lock);
  129. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  130. CCK_FUSE_HPLL_FREQ_MASK;
  131. mutex_unlock(&dev_priv->sb_lock);
  132. return vco_freq[hpll_freq] * 1000;
  133. }
  134. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  135. const char *name, u32 reg)
  136. {
  137. u32 val;
  138. int divider;
  139. if (dev_priv->hpll_freq == 0)
  140. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  141. mutex_lock(&dev_priv->sb_lock);
  142. val = vlv_cck_read(dev_priv, reg);
  143. mutex_unlock(&dev_priv->sb_lock);
  144. divider = val & CCK_FREQUENCY_VALUES;
  145. WARN((val & CCK_FREQUENCY_STATUS) !=
  146. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  147. "%s change in progress\n", name);
  148. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  149. }
  150. int
  151. intel_pch_rawclk(struct drm_device *dev)
  152. {
  153. struct drm_i915_private *dev_priv = dev->dev_private;
  154. WARN_ON(!HAS_PCH_SPLIT(dev));
  155. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  156. }
  157. /* hrawclock is 1/4 the FSB frequency */
  158. int intel_hrawclk(struct drm_device *dev)
  159. {
  160. struct drm_i915_private *dev_priv = dev->dev_private;
  161. uint32_t clkcfg;
  162. /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
  163. if (IS_VALLEYVIEW(dev))
  164. return 200;
  165. clkcfg = I915_READ(CLKCFG);
  166. switch (clkcfg & CLKCFG_FSB_MASK) {
  167. case CLKCFG_FSB_400:
  168. return 100;
  169. case CLKCFG_FSB_533:
  170. return 133;
  171. case CLKCFG_FSB_667:
  172. return 166;
  173. case CLKCFG_FSB_800:
  174. return 200;
  175. case CLKCFG_FSB_1067:
  176. return 266;
  177. case CLKCFG_FSB_1333:
  178. return 333;
  179. /* these two are just a guess; one of them might be right */
  180. case CLKCFG_FSB_1600:
  181. case CLKCFG_FSB_1600_ALT:
  182. return 400;
  183. default:
  184. return 133;
  185. }
  186. }
  187. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  188. {
  189. if (!IS_VALLEYVIEW(dev_priv))
  190. return;
  191. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  192. CCK_CZ_CLOCK_CONTROL);
  193. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  194. }
  195. static inline u32 /* units of 100MHz */
  196. intel_fdi_link_freq(struct drm_device *dev)
  197. {
  198. if (IS_GEN5(dev)) {
  199. struct drm_i915_private *dev_priv = dev->dev_private;
  200. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  201. } else
  202. return 27;
  203. }
  204. static const intel_limit_t intel_limits_i8xx_dac = {
  205. .dot = { .min = 25000, .max = 350000 },
  206. .vco = { .min = 908000, .max = 1512000 },
  207. .n = { .min = 2, .max = 16 },
  208. .m = { .min = 96, .max = 140 },
  209. .m1 = { .min = 18, .max = 26 },
  210. .m2 = { .min = 6, .max = 16 },
  211. .p = { .min = 4, .max = 128 },
  212. .p1 = { .min = 2, .max = 33 },
  213. .p2 = { .dot_limit = 165000,
  214. .p2_slow = 4, .p2_fast = 2 },
  215. };
  216. static const intel_limit_t intel_limits_i8xx_dvo = {
  217. .dot = { .min = 25000, .max = 350000 },
  218. .vco = { .min = 908000, .max = 1512000 },
  219. .n = { .min = 2, .max = 16 },
  220. .m = { .min = 96, .max = 140 },
  221. .m1 = { .min = 18, .max = 26 },
  222. .m2 = { .min = 6, .max = 16 },
  223. .p = { .min = 4, .max = 128 },
  224. .p1 = { .min = 2, .max = 33 },
  225. .p2 = { .dot_limit = 165000,
  226. .p2_slow = 4, .p2_fast = 4 },
  227. };
  228. static const intel_limit_t intel_limits_i8xx_lvds = {
  229. .dot = { .min = 25000, .max = 350000 },
  230. .vco = { .min = 908000, .max = 1512000 },
  231. .n = { .min = 2, .max = 16 },
  232. .m = { .min = 96, .max = 140 },
  233. .m1 = { .min = 18, .max = 26 },
  234. .m2 = { .min = 6, .max = 16 },
  235. .p = { .min = 4, .max = 128 },
  236. .p1 = { .min = 1, .max = 6 },
  237. .p2 = { .dot_limit = 165000,
  238. .p2_slow = 14, .p2_fast = 7 },
  239. };
  240. static const intel_limit_t intel_limits_i9xx_sdvo = {
  241. .dot = { .min = 20000, .max = 400000 },
  242. .vco = { .min = 1400000, .max = 2800000 },
  243. .n = { .min = 1, .max = 6 },
  244. .m = { .min = 70, .max = 120 },
  245. .m1 = { .min = 8, .max = 18 },
  246. .m2 = { .min = 3, .max = 7 },
  247. .p = { .min = 5, .max = 80 },
  248. .p1 = { .min = 1, .max = 8 },
  249. .p2 = { .dot_limit = 200000,
  250. .p2_slow = 10, .p2_fast = 5 },
  251. };
  252. static const intel_limit_t intel_limits_i9xx_lvds = {
  253. .dot = { .min = 20000, .max = 400000 },
  254. .vco = { .min = 1400000, .max = 2800000 },
  255. .n = { .min = 1, .max = 6 },
  256. .m = { .min = 70, .max = 120 },
  257. .m1 = { .min = 8, .max = 18 },
  258. .m2 = { .min = 3, .max = 7 },
  259. .p = { .min = 7, .max = 98 },
  260. .p1 = { .min = 1, .max = 8 },
  261. .p2 = { .dot_limit = 112000,
  262. .p2_slow = 14, .p2_fast = 7 },
  263. };
  264. static const intel_limit_t intel_limits_g4x_sdvo = {
  265. .dot = { .min = 25000, .max = 270000 },
  266. .vco = { .min = 1750000, .max = 3500000},
  267. .n = { .min = 1, .max = 4 },
  268. .m = { .min = 104, .max = 138 },
  269. .m1 = { .min = 17, .max = 23 },
  270. .m2 = { .min = 5, .max = 11 },
  271. .p = { .min = 10, .max = 30 },
  272. .p1 = { .min = 1, .max = 3},
  273. .p2 = { .dot_limit = 270000,
  274. .p2_slow = 10,
  275. .p2_fast = 10
  276. },
  277. };
  278. static const intel_limit_t intel_limits_g4x_hdmi = {
  279. .dot = { .min = 22000, .max = 400000 },
  280. .vco = { .min = 1750000, .max = 3500000},
  281. .n = { .min = 1, .max = 4 },
  282. .m = { .min = 104, .max = 138 },
  283. .m1 = { .min = 16, .max = 23 },
  284. .m2 = { .min = 5, .max = 11 },
  285. .p = { .min = 5, .max = 80 },
  286. .p1 = { .min = 1, .max = 8},
  287. .p2 = { .dot_limit = 165000,
  288. .p2_slow = 10, .p2_fast = 5 },
  289. };
  290. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  291. .dot = { .min = 20000, .max = 115000 },
  292. .vco = { .min = 1750000, .max = 3500000 },
  293. .n = { .min = 1, .max = 3 },
  294. .m = { .min = 104, .max = 138 },
  295. .m1 = { .min = 17, .max = 23 },
  296. .m2 = { .min = 5, .max = 11 },
  297. .p = { .min = 28, .max = 112 },
  298. .p1 = { .min = 2, .max = 8 },
  299. .p2 = { .dot_limit = 0,
  300. .p2_slow = 14, .p2_fast = 14
  301. },
  302. };
  303. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  304. .dot = { .min = 80000, .max = 224000 },
  305. .vco = { .min = 1750000, .max = 3500000 },
  306. .n = { .min = 1, .max = 3 },
  307. .m = { .min = 104, .max = 138 },
  308. .m1 = { .min = 17, .max = 23 },
  309. .m2 = { .min = 5, .max = 11 },
  310. .p = { .min = 14, .max = 42 },
  311. .p1 = { .min = 2, .max = 6 },
  312. .p2 = { .dot_limit = 0,
  313. .p2_slow = 7, .p2_fast = 7
  314. },
  315. };
  316. static const intel_limit_t intel_limits_pineview_sdvo = {
  317. .dot = { .min = 20000, .max = 400000},
  318. .vco = { .min = 1700000, .max = 3500000 },
  319. /* Pineview's Ncounter is a ring counter */
  320. .n = { .min = 3, .max = 6 },
  321. .m = { .min = 2, .max = 256 },
  322. /* Pineview only has one combined m divider, which we treat as m2. */
  323. .m1 = { .min = 0, .max = 0 },
  324. .m2 = { .min = 0, .max = 254 },
  325. .p = { .min = 5, .max = 80 },
  326. .p1 = { .min = 1, .max = 8 },
  327. .p2 = { .dot_limit = 200000,
  328. .p2_slow = 10, .p2_fast = 5 },
  329. };
  330. static const intel_limit_t intel_limits_pineview_lvds = {
  331. .dot = { .min = 20000, .max = 400000 },
  332. .vco = { .min = 1700000, .max = 3500000 },
  333. .n = { .min = 3, .max = 6 },
  334. .m = { .min = 2, .max = 256 },
  335. .m1 = { .min = 0, .max = 0 },
  336. .m2 = { .min = 0, .max = 254 },
  337. .p = { .min = 7, .max = 112 },
  338. .p1 = { .min = 1, .max = 8 },
  339. .p2 = { .dot_limit = 112000,
  340. .p2_slow = 14, .p2_fast = 14 },
  341. };
  342. /* Ironlake / Sandybridge
  343. *
  344. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  345. * the range value for them is (actual_value - 2).
  346. */
  347. static const intel_limit_t intel_limits_ironlake_dac = {
  348. .dot = { .min = 25000, .max = 350000 },
  349. .vco = { .min = 1760000, .max = 3510000 },
  350. .n = { .min = 1, .max = 5 },
  351. .m = { .min = 79, .max = 127 },
  352. .m1 = { .min = 12, .max = 22 },
  353. .m2 = { .min = 5, .max = 9 },
  354. .p = { .min = 5, .max = 80 },
  355. .p1 = { .min = 1, .max = 8 },
  356. .p2 = { .dot_limit = 225000,
  357. .p2_slow = 10, .p2_fast = 5 },
  358. };
  359. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  360. .dot = { .min = 25000, .max = 350000 },
  361. .vco = { .min = 1760000, .max = 3510000 },
  362. .n = { .min = 1, .max = 3 },
  363. .m = { .min = 79, .max = 118 },
  364. .m1 = { .min = 12, .max = 22 },
  365. .m2 = { .min = 5, .max = 9 },
  366. .p = { .min = 28, .max = 112 },
  367. .p1 = { .min = 2, .max = 8 },
  368. .p2 = { .dot_limit = 225000,
  369. .p2_slow = 14, .p2_fast = 14 },
  370. };
  371. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  372. .dot = { .min = 25000, .max = 350000 },
  373. .vco = { .min = 1760000, .max = 3510000 },
  374. .n = { .min = 1, .max = 3 },
  375. .m = { .min = 79, .max = 127 },
  376. .m1 = { .min = 12, .max = 22 },
  377. .m2 = { .min = 5, .max = 9 },
  378. .p = { .min = 14, .max = 56 },
  379. .p1 = { .min = 2, .max = 8 },
  380. .p2 = { .dot_limit = 225000,
  381. .p2_slow = 7, .p2_fast = 7 },
  382. };
  383. /* LVDS 100mhz refclk limits. */
  384. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  385. .dot = { .min = 25000, .max = 350000 },
  386. .vco = { .min = 1760000, .max = 3510000 },
  387. .n = { .min = 1, .max = 2 },
  388. .m = { .min = 79, .max = 126 },
  389. .m1 = { .min = 12, .max = 22 },
  390. .m2 = { .min = 5, .max = 9 },
  391. .p = { .min = 28, .max = 112 },
  392. .p1 = { .min = 2, .max = 8 },
  393. .p2 = { .dot_limit = 225000,
  394. .p2_slow = 14, .p2_fast = 14 },
  395. };
  396. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  397. .dot = { .min = 25000, .max = 350000 },
  398. .vco = { .min = 1760000, .max = 3510000 },
  399. .n = { .min = 1, .max = 3 },
  400. .m = { .min = 79, .max = 126 },
  401. .m1 = { .min = 12, .max = 22 },
  402. .m2 = { .min = 5, .max = 9 },
  403. .p = { .min = 14, .max = 42 },
  404. .p1 = { .min = 2, .max = 6 },
  405. .p2 = { .dot_limit = 225000,
  406. .p2_slow = 7, .p2_fast = 7 },
  407. };
  408. static const intel_limit_t intel_limits_vlv = {
  409. /*
  410. * These are the data rate limits (measured in fast clocks)
  411. * since those are the strictest limits we have. The fast
  412. * clock and actual rate limits are more relaxed, so checking
  413. * them would make no difference.
  414. */
  415. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  416. .vco = { .min = 4000000, .max = 6000000 },
  417. .n = { .min = 1, .max = 7 },
  418. .m1 = { .min = 2, .max = 3 },
  419. .m2 = { .min = 11, .max = 156 },
  420. .p1 = { .min = 2, .max = 3 },
  421. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  422. };
  423. static const intel_limit_t intel_limits_chv = {
  424. /*
  425. * These are the data rate limits (measured in fast clocks)
  426. * since those are the strictest limits we have. The fast
  427. * clock and actual rate limits are more relaxed, so checking
  428. * them would make no difference.
  429. */
  430. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  431. .vco = { .min = 4800000, .max = 6480000 },
  432. .n = { .min = 1, .max = 1 },
  433. .m1 = { .min = 2, .max = 2 },
  434. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  435. .p1 = { .min = 2, .max = 4 },
  436. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  437. };
  438. static const intel_limit_t intel_limits_bxt = {
  439. /* FIXME: find real dot limits */
  440. .dot = { .min = 0, .max = INT_MAX },
  441. .vco = { .min = 4800000, .max = 6700000 },
  442. .n = { .min = 1, .max = 1 },
  443. .m1 = { .min = 2, .max = 2 },
  444. /* FIXME: find real m2 limits */
  445. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  446. .p1 = { .min = 2, .max = 4 },
  447. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  448. };
  449. static bool
  450. needs_modeset(struct drm_crtc_state *state)
  451. {
  452. return drm_atomic_crtc_needs_modeset(state);
  453. }
  454. /**
  455. * Returns whether any output on the specified pipe is of the specified type
  456. */
  457. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  458. {
  459. struct drm_device *dev = crtc->base.dev;
  460. struct intel_encoder *encoder;
  461. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  462. if (encoder->type == type)
  463. return true;
  464. return false;
  465. }
  466. /**
  467. * Returns whether any output on the specified pipe will have the specified
  468. * type after a staged modeset is complete, i.e., the same as
  469. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  470. * encoder->crtc.
  471. */
  472. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  473. int type)
  474. {
  475. struct drm_atomic_state *state = crtc_state->base.state;
  476. struct drm_connector *connector;
  477. struct drm_connector_state *connector_state;
  478. struct intel_encoder *encoder;
  479. int i, num_connectors = 0;
  480. for_each_connector_in_state(state, connector, connector_state, i) {
  481. if (connector_state->crtc != crtc_state->base.crtc)
  482. continue;
  483. num_connectors++;
  484. encoder = to_intel_encoder(connector_state->best_encoder);
  485. if (encoder->type == type)
  486. return true;
  487. }
  488. WARN_ON(num_connectors == 0);
  489. return false;
  490. }
  491. static const intel_limit_t *
  492. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  493. {
  494. struct drm_device *dev = crtc_state->base.crtc->dev;
  495. const intel_limit_t *limit;
  496. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  497. if (intel_is_dual_link_lvds(dev)) {
  498. if (refclk == 100000)
  499. limit = &intel_limits_ironlake_dual_lvds_100m;
  500. else
  501. limit = &intel_limits_ironlake_dual_lvds;
  502. } else {
  503. if (refclk == 100000)
  504. limit = &intel_limits_ironlake_single_lvds_100m;
  505. else
  506. limit = &intel_limits_ironlake_single_lvds;
  507. }
  508. } else
  509. limit = &intel_limits_ironlake_dac;
  510. return limit;
  511. }
  512. static const intel_limit_t *
  513. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  514. {
  515. struct drm_device *dev = crtc_state->base.crtc->dev;
  516. const intel_limit_t *limit;
  517. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  518. if (intel_is_dual_link_lvds(dev))
  519. limit = &intel_limits_g4x_dual_channel_lvds;
  520. else
  521. limit = &intel_limits_g4x_single_channel_lvds;
  522. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  523. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  524. limit = &intel_limits_g4x_hdmi;
  525. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  526. limit = &intel_limits_g4x_sdvo;
  527. } else /* The option is for other outputs */
  528. limit = &intel_limits_i9xx_sdvo;
  529. return limit;
  530. }
  531. static const intel_limit_t *
  532. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  533. {
  534. struct drm_device *dev = crtc_state->base.crtc->dev;
  535. const intel_limit_t *limit;
  536. if (IS_BROXTON(dev))
  537. limit = &intel_limits_bxt;
  538. else if (HAS_PCH_SPLIT(dev))
  539. limit = intel_ironlake_limit(crtc_state, refclk);
  540. else if (IS_G4X(dev)) {
  541. limit = intel_g4x_limit(crtc_state);
  542. } else if (IS_PINEVIEW(dev)) {
  543. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  544. limit = &intel_limits_pineview_lvds;
  545. else
  546. limit = &intel_limits_pineview_sdvo;
  547. } else if (IS_CHERRYVIEW(dev)) {
  548. limit = &intel_limits_chv;
  549. } else if (IS_VALLEYVIEW(dev)) {
  550. limit = &intel_limits_vlv;
  551. } else if (!IS_GEN2(dev)) {
  552. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  553. limit = &intel_limits_i9xx_lvds;
  554. else
  555. limit = &intel_limits_i9xx_sdvo;
  556. } else {
  557. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  558. limit = &intel_limits_i8xx_lvds;
  559. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  560. limit = &intel_limits_i8xx_dvo;
  561. else
  562. limit = &intel_limits_i8xx_dac;
  563. }
  564. return limit;
  565. }
  566. /*
  567. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  568. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  569. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  570. * The helpers' return value is the rate of the clock that is fed to the
  571. * display engine's pipe which can be the above fast dot clock rate or a
  572. * divided-down version of it.
  573. */
  574. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  575. static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
  576. {
  577. clock->m = clock->m2 + 2;
  578. clock->p = clock->p1 * clock->p2;
  579. if (WARN_ON(clock->n == 0 || clock->p == 0))
  580. return 0;
  581. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  582. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  583. return clock->dot;
  584. }
  585. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  586. {
  587. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  588. }
  589. static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
  590. {
  591. clock->m = i9xx_dpll_compute_m(clock);
  592. clock->p = clock->p1 * clock->p2;
  593. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  594. return 0;
  595. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  596. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  597. return clock->dot;
  598. }
  599. static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
  600. {
  601. clock->m = clock->m1 * clock->m2;
  602. clock->p = clock->p1 * clock->p2;
  603. if (WARN_ON(clock->n == 0 || clock->p == 0))
  604. return 0;
  605. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  606. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  607. return clock->dot / 5;
  608. }
  609. int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
  610. {
  611. clock->m = clock->m1 * clock->m2;
  612. clock->p = clock->p1 * clock->p2;
  613. if (WARN_ON(clock->n == 0 || clock->p == 0))
  614. return 0;
  615. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  616. clock->n << 22);
  617. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  618. return clock->dot / 5;
  619. }
  620. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  621. /**
  622. * Returns whether the given set of divisors are valid for a given refclk with
  623. * the given connectors.
  624. */
  625. static bool intel_PLL_is_valid(struct drm_device *dev,
  626. const intel_limit_t *limit,
  627. const intel_clock_t *clock)
  628. {
  629. if (clock->n < limit->n.min || limit->n.max < clock->n)
  630. INTELPllInvalid("n out of range\n");
  631. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  632. INTELPllInvalid("p1 out of range\n");
  633. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  634. INTELPllInvalid("m2 out of range\n");
  635. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  636. INTELPllInvalid("m1 out of range\n");
  637. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  638. if (clock->m1 <= clock->m2)
  639. INTELPllInvalid("m1 <= m2\n");
  640. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  641. if (clock->p < limit->p.min || limit->p.max < clock->p)
  642. INTELPllInvalid("p out of range\n");
  643. if (clock->m < limit->m.min || limit->m.max < clock->m)
  644. INTELPllInvalid("m out of range\n");
  645. }
  646. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  647. INTELPllInvalid("vco out of range\n");
  648. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  649. * connector, etc., rather than just a single range.
  650. */
  651. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  652. INTELPllInvalid("dot out of range\n");
  653. return true;
  654. }
  655. static int
  656. i9xx_select_p2_div(const intel_limit_t *limit,
  657. const struct intel_crtc_state *crtc_state,
  658. int target)
  659. {
  660. struct drm_device *dev = crtc_state->base.crtc->dev;
  661. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  662. /*
  663. * For LVDS just rely on its current settings for dual-channel.
  664. * We haven't figured out how to reliably set up different
  665. * single/dual channel state, if we even can.
  666. */
  667. if (intel_is_dual_link_lvds(dev))
  668. return limit->p2.p2_fast;
  669. else
  670. return limit->p2.p2_slow;
  671. } else {
  672. if (target < limit->p2.dot_limit)
  673. return limit->p2.p2_slow;
  674. else
  675. return limit->p2.p2_fast;
  676. }
  677. }
  678. static bool
  679. i9xx_find_best_dpll(const intel_limit_t *limit,
  680. struct intel_crtc_state *crtc_state,
  681. int target, int refclk, intel_clock_t *match_clock,
  682. intel_clock_t *best_clock)
  683. {
  684. struct drm_device *dev = crtc_state->base.crtc->dev;
  685. intel_clock_t clock;
  686. int err = target;
  687. memset(best_clock, 0, sizeof(*best_clock));
  688. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  689. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  690. clock.m1++) {
  691. for (clock.m2 = limit->m2.min;
  692. clock.m2 <= limit->m2.max; clock.m2++) {
  693. if (clock.m2 >= clock.m1)
  694. break;
  695. for (clock.n = limit->n.min;
  696. clock.n <= limit->n.max; clock.n++) {
  697. for (clock.p1 = limit->p1.min;
  698. clock.p1 <= limit->p1.max; clock.p1++) {
  699. int this_err;
  700. i9xx_calc_dpll_params(refclk, &clock);
  701. if (!intel_PLL_is_valid(dev, limit,
  702. &clock))
  703. continue;
  704. if (match_clock &&
  705. clock.p != match_clock->p)
  706. continue;
  707. this_err = abs(clock.dot - target);
  708. if (this_err < err) {
  709. *best_clock = clock;
  710. err = this_err;
  711. }
  712. }
  713. }
  714. }
  715. }
  716. return (err != target);
  717. }
  718. static bool
  719. pnv_find_best_dpll(const intel_limit_t *limit,
  720. struct intel_crtc_state *crtc_state,
  721. int target, int refclk, intel_clock_t *match_clock,
  722. intel_clock_t *best_clock)
  723. {
  724. struct drm_device *dev = crtc_state->base.crtc->dev;
  725. intel_clock_t clock;
  726. int err = target;
  727. memset(best_clock, 0, sizeof(*best_clock));
  728. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  729. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  730. clock.m1++) {
  731. for (clock.m2 = limit->m2.min;
  732. clock.m2 <= limit->m2.max; clock.m2++) {
  733. for (clock.n = limit->n.min;
  734. clock.n <= limit->n.max; clock.n++) {
  735. for (clock.p1 = limit->p1.min;
  736. clock.p1 <= limit->p1.max; clock.p1++) {
  737. int this_err;
  738. pnv_calc_dpll_params(refclk, &clock);
  739. if (!intel_PLL_is_valid(dev, limit,
  740. &clock))
  741. continue;
  742. if (match_clock &&
  743. clock.p != match_clock->p)
  744. continue;
  745. this_err = abs(clock.dot - target);
  746. if (this_err < err) {
  747. *best_clock = clock;
  748. err = this_err;
  749. }
  750. }
  751. }
  752. }
  753. }
  754. return (err != target);
  755. }
  756. static bool
  757. g4x_find_best_dpll(const intel_limit_t *limit,
  758. struct intel_crtc_state *crtc_state,
  759. int target, int refclk, intel_clock_t *match_clock,
  760. intel_clock_t *best_clock)
  761. {
  762. struct drm_device *dev = crtc_state->base.crtc->dev;
  763. intel_clock_t clock;
  764. int max_n;
  765. bool found = false;
  766. /* approximately equals target * 0.00585 */
  767. int err_most = (target >> 8) + (target >> 9);
  768. memset(best_clock, 0, sizeof(*best_clock));
  769. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  770. max_n = limit->n.max;
  771. /* based on hardware requirement, prefer smaller n to precision */
  772. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  773. /* based on hardware requirement, prefere larger m1,m2 */
  774. for (clock.m1 = limit->m1.max;
  775. clock.m1 >= limit->m1.min; clock.m1--) {
  776. for (clock.m2 = limit->m2.max;
  777. clock.m2 >= limit->m2.min; clock.m2--) {
  778. for (clock.p1 = limit->p1.max;
  779. clock.p1 >= limit->p1.min; clock.p1--) {
  780. int this_err;
  781. i9xx_calc_dpll_params(refclk, &clock);
  782. if (!intel_PLL_is_valid(dev, limit,
  783. &clock))
  784. continue;
  785. this_err = abs(clock.dot - target);
  786. if (this_err < err_most) {
  787. *best_clock = clock;
  788. err_most = this_err;
  789. max_n = clock.n;
  790. found = true;
  791. }
  792. }
  793. }
  794. }
  795. }
  796. return found;
  797. }
  798. /*
  799. * Check if the calculated PLL configuration is more optimal compared to the
  800. * best configuration and error found so far. Return the calculated error.
  801. */
  802. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  803. const intel_clock_t *calculated_clock,
  804. const intel_clock_t *best_clock,
  805. unsigned int best_error_ppm,
  806. unsigned int *error_ppm)
  807. {
  808. /*
  809. * For CHV ignore the error and consider only the P value.
  810. * Prefer a bigger P value based on HW requirements.
  811. */
  812. if (IS_CHERRYVIEW(dev)) {
  813. *error_ppm = 0;
  814. return calculated_clock->p > best_clock->p;
  815. }
  816. if (WARN_ON_ONCE(!target_freq))
  817. return false;
  818. *error_ppm = div_u64(1000000ULL *
  819. abs(target_freq - calculated_clock->dot),
  820. target_freq);
  821. /*
  822. * Prefer a better P value over a better (smaller) error if the error
  823. * is small. Ensure this preference for future configurations too by
  824. * setting the error to 0.
  825. */
  826. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  827. *error_ppm = 0;
  828. return true;
  829. }
  830. return *error_ppm + 10 < best_error_ppm;
  831. }
  832. static bool
  833. vlv_find_best_dpll(const intel_limit_t *limit,
  834. struct intel_crtc_state *crtc_state,
  835. int target, int refclk, intel_clock_t *match_clock,
  836. intel_clock_t *best_clock)
  837. {
  838. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  839. struct drm_device *dev = crtc->base.dev;
  840. intel_clock_t clock;
  841. unsigned int bestppm = 1000000;
  842. /* min update 19.2 MHz */
  843. int max_n = min(limit->n.max, refclk / 19200);
  844. bool found = false;
  845. target *= 5; /* fast clock */
  846. memset(best_clock, 0, sizeof(*best_clock));
  847. /* based on hardware requirement, prefer smaller n to precision */
  848. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  849. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  850. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  851. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  852. clock.p = clock.p1 * clock.p2;
  853. /* based on hardware requirement, prefer bigger m1,m2 values */
  854. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  855. unsigned int ppm;
  856. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  857. refclk * clock.m1);
  858. vlv_calc_dpll_params(refclk, &clock);
  859. if (!intel_PLL_is_valid(dev, limit,
  860. &clock))
  861. continue;
  862. if (!vlv_PLL_is_optimal(dev, target,
  863. &clock,
  864. best_clock,
  865. bestppm, &ppm))
  866. continue;
  867. *best_clock = clock;
  868. bestppm = ppm;
  869. found = true;
  870. }
  871. }
  872. }
  873. }
  874. return found;
  875. }
  876. static bool
  877. chv_find_best_dpll(const intel_limit_t *limit,
  878. struct intel_crtc_state *crtc_state,
  879. int target, int refclk, intel_clock_t *match_clock,
  880. intel_clock_t *best_clock)
  881. {
  882. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  883. struct drm_device *dev = crtc->base.dev;
  884. unsigned int best_error_ppm;
  885. intel_clock_t clock;
  886. uint64_t m2;
  887. int found = false;
  888. memset(best_clock, 0, sizeof(*best_clock));
  889. best_error_ppm = 1000000;
  890. /*
  891. * Based on hardware doc, the n always set to 1, and m1 always
  892. * set to 2. If requires to support 200Mhz refclk, we need to
  893. * revisit this because n may not 1 anymore.
  894. */
  895. clock.n = 1, clock.m1 = 2;
  896. target *= 5; /* fast clock */
  897. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  898. for (clock.p2 = limit->p2.p2_fast;
  899. clock.p2 >= limit->p2.p2_slow;
  900. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  901. unsigned int error_ppm;
  902. clock.p = clock.p1 * clock.p2;
  903. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  904. clock.n) << 22, refclk * clock.m1);
  905. if (m2 > INT_MAX/clock.m1)
  906. continue;
  907. clock.m2 = m2;
  908. chv_calc_dpll_params(refclk, &clock);
  909. if (!intel_PLL_is_valid(dev, limit, &clock))
  910. continue;
  911. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  912. best_error_ppm, &error_ppm))
  913. continue;
  914. *best_clock = clock;
  915. best_error_ppm = error_ppm;
  916. found = true;
  917. }
  918. }
  919. return found;
  920. }
  921. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  922. intel_clock_t *best_clock)
  923. {
  924. int refclk = i9xx_get_refclk(crtc_state, 0);
  925. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  926. target_clock, refclk, NULL, best_clock);
  927. }
  928. bool intel_crtc_active(struct drm_crtc *crtc)
  929. {
  930. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  931. /* Be paranoid as we can arrive here with only partial
  932. * state retrieved from the hardware during setup.
  933. *
  934. * We can ditch the adjusted_mode.crtc_clock check as soon
  935. * as Haswell has gained clock readout/fastboot support.
  936. *
  937. * We can ditch the crtc->primary->fb check as soon as we can
  938. * properly reconstruct framebuffers.
  939. *
  940. * FIXME: The intel_crtc->active here should be switched to
  941. * crtc->state->active once we have proper CRTC states wired up
  942. * for atomic.
  943. */
  944. return intel_crtc->active && crtc->primary->state->fb &&
  945. intel_crtc->config->base.adjusted_mode.crtc_clock;
  946. }
  947. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  948. enum pipe pipe)
  949. {
  950. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  951. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  952. return intel_crtc->config->cpu_transcoder;
  953. }
  954. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  955. {
  956. struct drm_i915_private *dev_priv = dev->dev_private;
  957. u32 reg = PIPEDSL(pipe);
  958. u32 line1, line2;
  959. u32 line_mask;
  960. if (IS_GEN2(dev))
  961. line_mask = DSL_LINEMASK_GEN2;
  962. else
  963. line_mask = DSL_LINEMASK_GEN3;
  964. line1 = I915_READ(reg) & line_mask;
  965. msleep(5);
  966. line2 = I915_READ(reg) & line_mask;
  967. return line1 == line2;
  968. }
  969. /*
  970. * intel_wait_for_pipe_off - wait for pipe to turn off
  971. * @crtc: crtc whose pipe to wait for
  972. *
  973. * After disabling a pipe, we can't wait for vblank in the usual way,
  974. * spinning on the vblank interrupt status bit, since we won't actually
  975. * see an interrupt when the pipe is disabled.
  976. *
  977. * On Gen4 and above:
  978. * wait for the pipe register state bit to turn off
  979. *
  980. * Otherwise:
  981. * wait for the display line value to settle (it usually
  982. * ends up stopping at the start of the next frame).
  983. *
  984. */
  985. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  986. {
  987. struct drm_device *dev = crtc->base.dev;
  988. struct drm_i915_private *dev_priv = dev->dev_private;
  989. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  990. enum pipe pipe = crtc->pipe;
  991. if (INTEL_INFO(dev)->gen >= 4) {
  992. int reg = PIPECONF(cpu_transcoder);
  993. /* Wait for the Pipe State to go off */
  994. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  995. 100))
  996. WARN(1, "pipe_off wait timed out\n");
  997. } else {
  998. /* Wait for the display line to settle */
  999. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  1000. WARN(1, "pipe_off wait timed out\n");
  1001. }
  1002. }
  1003. static const char *state_string(bool enabled)
  1004. {
  1005. return enabled ? "on" : "off";
  1006. }
  1007. /* Only for pre-ILK configs */
  1008. void assert_pll(struct drm_i915_private *dev_priv,
  1009. enum pipe pipe, bool state)
  1010. {
  1011. u32 val;
  1012. bool cur_state;
  1013. val = I915_READ(DPLL(pipe));
  1014. cur_state = !!(val & DPLL_VCO_ENABLE);
  1015. I915_STATE_WARN(cur_state != state,
  1016. "PLL state assertion failure (expected %s, current %s)\n",
  1017. state_string(state), state_string(cur_state));
  1018. }
  1019. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1020. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1021. {
  1022. u32 val;
  1023. bool cur_state;
  1024. mutex_lock(&dev_priv->sb_lock);
  1025. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1026. mutex_unlock(&dev_priv->sb_lock);
  1027. cur_state = val & DSI_PLL_VCO_EN;
  1028. I915_STATE_WARN(cur_state != state,
  1029. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1030. state_string(state), state_string(cur_state));
  1031. }
  1032. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1033. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1034. struct intel_shared_dpll *
  1035. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1036. {
  1037. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1038. if (crtc->config->shared_dpll < 0)
  1039. return NULL;
  1040. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1041. }
  1042. /* For ILK+ */
  1043. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1044. struct intel_shared_dpll *pll,
  1045. bool state)
  1046. {
  1047. bool cur_state;
  1048. struct intel_dpll_hw_state hw_state;
  1049. if (WARN (!pll,
  1050. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1051. return;
  1052. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1053. I915_STATE_WARN(cur_state != state,
  1054. "%s assertion failure (expected %s, current %s)\n",
  1055. pll->name, state_string(state), state_string(cur_state));
  1056. }
  1057. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1058. enum pipe pipe, bool state)
  1059. {
  1060. bool cur_state;
  1061. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1062. pipe);
  1063. if (HAS_DDI(dev_priv->dev)) {
  1064. /* DDI does not have a specific FDI_TX register */
  1065. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1066. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1067. } else {
  1068. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1069. cur_state = !!(val & FDI_TX_ENABLE);
  1070. }
  1071. I915_STATE_WARN(cur_state != state,
  1072. "FDI TX state assertion failure (expected %s, current %s)\n",
  1073. state_string(state), state_string(cur_state));
  1074. }
  1075. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1076. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1077. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1078. enum pipe pipe, bool state)
  1079. {
  1080. u32 val;
  1081. bool cur_state;
  1082. val = I915_READ(FDI_RX_CTL(pipe));
  1083. cur_state = !!(val & FDI_RX_ENABLE);
  1084. I915_STATE_WARN(cur_state != state,
  1085. "FDI RX state assertion failure (expected %s, current %s)\n",
  1086. state_string(state), state_string(cur_state));
  1087. }
  1088. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1089. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1090. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1091. enum pipe pipe)
  1092. {
  1093. u32 val;
  1094. /* ILK FDI PLL is always enabled */
  1095. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1096. return;
  1097. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1098. if (HAS_DDI(dev_priv->dev))
  1099. return;
  1100. val = I915_READ(FDI_TX_CTL(pipe));
  1101. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1102. }
  1103. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1104. enum pipe pipe, bool state)
  1105. {
  1106. u32 val;
  1107. bool cur_state;
  1108. val = I915_READ(FDI_RX_CTL(pipe));
  1109. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1110. I915_STATE_WARN(cur_state != state,
  1111. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1112. state_string(state), state_string(cur_state));
  1113. }
  1114. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1115. enum pipe pipe)
  1116. {
  1117. struct drm_device *dev = dev_priv->dev;
  1118. int pp_reg;
  1119. u32 val;
  1120. enum pipe panel_pipe = PIPE_A;
  1121. bool locked = true;
  1122. if (WARN_ON(HAS_DDI(dev)))
  1123. return;
  1124. if (HAS_PCH_SPLIT(dev)) {
  1125. u32 port_sel;
  1126. pp_reg = PCH_PP_CONTROL;
  1127. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1128. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1129. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1130. panel_pipe = PIPE_B;
  1131. /* XXX: else fix for eDP */
  1132. } else if (IS_VALLEYVIEW(dev)) {
  1133. /* presumably write lock depends on pipe, not port select */
  1134. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1135. panel_pipe = pipe;
  1136. } else {
  1137. pp_reg = PP_CONTROL;
  1138. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1139. panel_pipe = PIPE_B;
  1140. }
  1141. val = I915_READ(pp_reg);
  1142. if (!(val & PANEL_POWER_ON) ||
  1143. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1144. locked = false;
  1145. I915_STATE_WARN(panel_pipe == pipe && locked,
  1146. "panel assertion failure, pipe %c regs locked\n",
  1147. pipe_name(pipe));
  1148. }
  1149. static void assert_cursor(struct drm_i915_private *dev_priv,
  1150. enum pipe pipe, bool state)
  1151. {
  1152. struct drm_device *dev = dev_priv->dev;
  1153. bool cur_state;
  1154. if (IS_845G(dev) || IS_I865G(dev))
  1155. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1156. else
  1157. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1158. I915_STATE_WARN(cur_state != state,
  1159. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1160. pipe_name(pipe), state_string(state), state_string(cur_state));
  1161. }
  1162. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1163. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1164. void assert_pipe(struct drm_i915_private *dev_priv,
  1165. enum pipe pipe, bool state)
  1166. {
  1167. bool cur_state;
  1168. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1169. pipe);
  1170. /* if we need the pipe quirk it must be always on */
  1171. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1172. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1173. state = true;
  1174. if (!intel_display_power_is_enabled(dev_priv,
  1175. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1176. cur_state = false;
  1177. } else {
  1178. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1179. cur_state = !!(val & PIPECONF_ENABLE);
  1180. }
  1181. I915_STATE_WARN(cur_state != state,
  1182. "pipe %c assertion failure (expected %s, current %s)\n",
  1183. pipe_name(pipe), state_string(state), state_string(cur_state));
  1184. }
  1185. static void assert_plane(struct drm_i915_private *dev_priv,
  1186. enum plane plane, bool state)
  1187. {
  1188. u32 val;
  1189. bool cur_state;
  1190. val = I915_READ(DSPCNTR(plane));
  1191. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1192. I915_STATE_WARN(cur_state != state,
  1193. "plane %c assertion failure (expected %s, current %s)\n",
  1194. plane_name(plane), state_string(state), state_string(cur_state));
  1195. }
  1196. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1197. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1198. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1199. enum pipe pipe)
  1200. {
  1201. struct drm_device *dev = dev_priv->dev;
  1202. int i;
  1203. /* Primary planes are fixed to pipes on gen4+ */
  1204. if (INTEL_INFO(dev)->gen >= 4) {
  1205. u32 val = I915_READ(DSPCNTR(pipe));
  1206. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1207. "plane %c assertion failure, should be disabled but not\n",
  1208. plane_name(pipe));
  1209. return;
  1210. }
  1211. /* Need to check both planes against the pipe */
  1212. for_each_pipe(dev_priv, i) {
  1213. u32 val = I915_READ(DSPCNTR(i));
  1214. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1215. DISPPLANE_SEL_PIPE_SHIFT;
  1216. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1217. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1218. plane_name(i), pipe_name(pipe));
  1219. }
  1220. }
  1221. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1222. enum pipe pipe)
  1223. {
  1224. struct drm_device *dev = dev_priv->dev;
  1225. int sprite;
  1226. if (INTEL_INFO(dev)->gen >= 9) {
  1227. for_each_sprite(dev_priv, pipe, sprite) {
  1228. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1229. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1230. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1231. sprite, pipe_name(pipe));
  1232. }
  1233. } else if (IS_VALLEYVIEW(dev)) {
  1234. for_each_sprite(dev_priv, pipe, sprite) {
  1235. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1236. I915_STATE_WARN(val & SP_ENABLE,
  1237. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1238. sprite_name(pipe, sprite), pipe_name(pipe));
  1239. }
  1240. } else if (INTEL_INFO(dev)->gen >= 7) {
  1241. u32 val = I915_READ(SPRCTL(pipe));
  1242. I915_STATE_WARN(val & SPRITE_ENABLE,
  1243. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1244. plane_name(pipe), pipe_name(pipe));
  1245. } else if (INTEL_INFO(dev)->gen >= 5) {
  1246. u32 val = I915_READ(DVSCNTR(pipe));
  1247. I915_STATE_WARN(val & DVS_ENABLE,
  1248. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1249. plane_name(pipe), pipe_name(pipe));
  1250. }
  1251. }
  1252. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1253. {
  1254. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1255. drm_crtc_vblank_put(crtc);
  1256. }
  1257. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1258. {
  1259. u32 val;
  1260. bool enabled;
  1261. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1262. val = I915_READ(PCH_DREF_CONTROL);
  1263. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1264. DREF_SUPERSPREAD_SOURCE_MASK));
  1265. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1266. }
  1267. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1268. enum pipe pipe)
  1269. {
  1270. u32 val;
  1271. bool enabled;
  1272. val = I915_READ(PCH_TRANSCONF(pipe));
  1273. enabled = !!(val & TRANS_ENABLE);
  1274. I915_STATE_WARN(enabled,
  1275. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1276. pipe_name(pipe));
  1277. }
  1278. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1279. enum pipe pipe, u32 port_sel, u32 val)
  1280. {
  1281. if ((val & DP_PORT_EN) == 0)
  1282. return false;
  1283. if (HAS_PCH_CPT(dev_priv->dev)) {
  1284. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1285. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1286. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1287. return false;
  1288. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1289. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1290. return false;
  1291. } else {
  1292. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1293. return false;
  1294. }
  1295. return true;
  1296. }
  1297. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1298. enum pipe pipe, u32 val)
  1299. {
  1300. if ((val & SDVO_ENABLE) == 0)
  1301. return false;
  1302. if (HAS_PCH_CPT(dev_priv->dev)) {
  1303. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1304. return false;
  1305. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1306. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1307. return false;
  1308. } else {
  1309. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1310. return false;
  1311. }
  1312. return true;
  1313. }
  1314. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1315. enum pipe pipe, u32 val)
  1316. {
  1317. if ((val & LVDS_PORT_EN) == 0)
  1318. return false;
  1319. if (HAS_PCH_CPT(dev_priv->dev)) {
  1320. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1321. return false;
  1322. } else {
  1323. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1324. return false;
  1325. }
  1326. return true;
  1327. }
  1328. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1329. enum pipe pipe, u32 val)
  1330. {
  1331. if ((val & ADPA_DAC_ENABLE) == 0)
  1332. return false;
  1333. if (HAS_PCH_CPT(dev_priv->dev)) {
  1334. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1335. return false;
  1336. } else {
  1337. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1338. return false;
  1339. }
  1340. return true;
  1341. }
  1342. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1343. enum pipe pipe, int reg, u32 port_sel)
  1344. {
  1345. u32 val = I915_READ(reg);
  1346. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1347. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1348. reg, pipe_name(pipe));
  1349. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1350. && (val & DP_PIPEB_SELECT),
  1351. "IBX PCH dp port still using transcoder B\n");
  1352. }
  1353. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1354. enum pipe pipe, int reg)
  1355. {
  1356. u32 val = I915_READ(reg);
  1357. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1358. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1359. reg, pipe_name(pipe));
  1360. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1361. && (val & SDVO_PIPE_B_SELECT),
  1362. "IBX PCH hdmi port still using transcoder B\n");
  1363. }
  1364. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1365. enum pipe pipe)
  1366. {
  1367. u32 val;
  1368. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1369. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1370. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1371. val = I915_READ(PCH_ADPA);
  1372. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1373. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1374. pipe_name(pipe));
  1375. val = I915_READ(PCH_LVDS);
  1376. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1377. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1378. pipe_name(pipe));
  1379. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1380. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1381. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1382. }
  1383. static void vlv_enable_pll(struct intel_crtc *crtc,
  1384. const struct intel_crtc_state *pipe_config)
  1385. {
  1386. struct drm_device *dev = crtc->base.dev;
  1387. struct drm_i915_private *dev_priv = dev->dev_private;
  1388. int reg = DPLL(crtc->pipe);
  1389. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1390. assert_pipe_disabled(dev_priv, crtc->pipe);
  1391. /* No really, not for ILK+ */
  1392. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1393. /* PLL is protected by panel, make sure we can write it */
  1394. if (IS_MOBILE(dev_priv->dev))
  1395. assert_panel_unlocked(dev_priv, crtc->pipe);
  1396. I915_WRITE(reg, dpll);
  1397. POSTING_READ(reg);
  1398. udelay(150);
  1399. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1400. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1401. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1402. POSTING_READ(DPLL_MD(crtc->pipe));
  1403. /* We do this three times for luck */
  1404. I915_WRITE(reg, dpll);
  1405. POSTING_READ(reg);
  1406. udelay(150); /* wait for warmup */
  1407. I915_WRITE(reg, dpll);
  1408. POSTING_READ(reg);
  1409. udelay(150); /* wait for warmup */
  1410. I915_WRITE(reg, dpll);
  1411. POSTING_READ(reg);
  1412. udelay(150); /* wait for warmup */
  1413. }
  1414. static void chv_enable_pll(struct intel_crtc *crtc,
  1415. const struct intel_crtc_state *pipe_config)
  1416. {
  1417. struct drm_device *dev = crtc->base.dev;
  1418. struct drm_i915_private *dev_priv = dev->dev_private;
  1419. int pipe = crtc->pipe;
  1420. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1421. u32 tmp;
  1422. assert_pipe_disabled(dev_priv, crtc->pipe);
  1423. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1424. mutex_lock(&dev_priv->sb_lock);
  1425. /* Enable back the 10bit clock to display controller */
  1426. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1427. tmp |= DPIO_DCLKP_EN;
  1428. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1429. mutex_unlock(&dev_priv->sb_lock);
  1430. /*
  1431. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1432. */
  1433. udelay(1);
  1434. /* Enable PLL */
  1435. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1436. /* Check PLL is locked */
  1437. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1438. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1439. /* not sure when this should be written */
  1440. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1441. POSTING_READ(DPLL_MD(pipe));
  1442. }
  1443. static int intel_num_dvo_pipes(struct drm_device *dev)
  1444. {
  1445. struct intel_crtc *crtc;
  1446. int count = 0;
  1447. for_each_intel_crtc(dev, crtc)
  1448. count += crtc->base.state->active &&
  1449. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1450. return count;
  1451. }
  1452. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1453. {
  1454. struct drm_device *dev = crtc->base.dev;
  1455. struct drm_i915_private *dev_priv = dev->dev_private;
  1456. int reg = DPLL(crtc->pipe);
  1457. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1458. assert_pipe_disabled(dev_priv, crtc->pipe);
  1459. /* No really, not for ILK+ */
  1460. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1461. /* PLL is protected by panel, make sure we can write it */
  1462. if (IS_MOBILE(dev) && !IS_I830(dev))
  1463. assert_panel_unlocked(dev_priv, crtc->pipe);
  1464. /* Enable DVO 2x clock on both PLLs if necessary */
  1465. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1466. /*
  1467. * It appears to be important that we don't enable this
  1468. * for the current pipe before otherwise configuring the
  1469. * PLL. No idea how this should be handled if multiple
  1470. * DVO outputs are enabled simultaneosly.
  1471. */
  1472. dpll |= DPLL_DVO_2X_MODE;
  1473. I915_WRITE(DPLL(!crtc->pipe),
  1474. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1475. }
  1476. /* Wait for the clocks to stabilize. */
  1477. POSTING_READ(reg);
  1478. udelay(150);
  1479. if (INTEL_INFO(dev)->gen >= 4) {
  1480. I915_WRITE(DPLL_MD(crtc->pipe),
  1481. crtc->config->dpll_hw_state.dpll_md);
  1482. } else {
  1483. /* The pixel multiplier can only be updated once the
  1484. * DPLL is enabled and the clocks are stable.
  1485. *
  1486. * So write it again.
  1487. */
  1488. I915_WRITE(reg, dpll);
  1489. }
  1490. /* We do this three times for luck */
  1491. I915_WRITE(reg, dpll);
  1492. POSTING_READ(reg);
  1493. udelay(150); /* wait for warmup */
  1494. I915_WRITE(reg, dpll);
  1495. POSTING_READ(reg);
  1496. udelay(150); /* wait for warmup */
  1497. I915_WRITE(reg, dpll);
  1498. POSTING_READ(reg);
  1499. udelay(150); /* wait for warmup */
  1500. }
  1501. /**
  1502. * i9xx_disable_pll - disable a PLL
  1503. * @dev_priv: i915 private structure
  1504. * @pipe: pipe PLL to disable
  1505. *
  1506. * Disable the PLL for @pipe, making sure the pipe is off first.
  1507. *
  1508. * Note! This is for pre-ILK only.
  1509. */
  1510. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1511. {
  1512. struct drm_device *dev = crtc->base.dev;
  1513. struct drm_i915_private *dev_priv = dev->dev_private;
  1514. enum pipe pipe = crtc->pipe;
  1515. /* Disable DVO 2x clock on both PLLs if necessary */
  1516. if (IS_I830(dev) &&
  1517. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1518. !intel_num_dvo_pipes(dev)) {
  1519. I915_WRITE(DPLL(PIPE_B),
  1520. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1521. I915_WRITE(DPLL(PIPE_A),
  1522. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1523. }
  1524. /* Don't disable pipe or pipe PLLs if needed */
  1525. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1526. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1527. return;
  1528. /* Make sure the pipe isn't still relying on us */
  1529. assert_pipe_disabled(dev_priv, pipe);
  1530. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1531. POSTING_READ(DPLL(pipe));
  1532. }
  1533. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1534. {
  1535. u32 val;
  1536. /* Make sure the pipe isn't still relying on us */
  1537. assert_pipe_disabled(dev_priv, pipe);
  1538. /*
  1539. * Leave integrated clock source and reference clock enabled for pipe B.
  1540. * The latter is needed for VGA hotplug / manual detection.
  1541. */
  1542. val = DPLL_VGA_MODE_DIS;
  1543. if (pipe == PIPE_B)
  1544. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
  1545. I915_WRITE(DPLL(pipe), val);
  1546. POSTING_READ(DPLL(pipe));
  1547. }
  1548. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1549. {
  1550. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1551. u32 val;
  1552. /* Make sure the pipe isn't still relying on us */
  1553. assert_pipe_disabled(dev_priv, pipe);
  1554. /* Set PLL en = 0 */
  1555. val = DPLL_SSC_REF_CLK_CHV |
  1556. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1557. if (pipe != PIPE_A)
  1558. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1559. I915_WRITE(DPLL(pipe), val);
  1560. POSTING_READ(DPLL(pipe));
  1561. mutex_lock(&dev_priv->sb_lock);
  1562. /* Disable 10bit clock to display controller */
  1563. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1564. val &= ~DPIO_DCLKP_EN;
  1565. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1566. mutex_unlock(&dev_priv->sb_lock);
  1567. }
  1568. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1569. struct intel_digital_port *dport,
  1570. unsigned int expected_mask)
  1571. {
  1572. u32 port_mask;
  1573. int dpll_reg;
  1574. switch (dport->port) {
  1575. case PORT_B:
  1576. port_mask = DPLL_PORTB_READY_MASK;
  1577. dpll_reg = DPLL(0);
  1578. break;
  1579. case PORT_C:
  1580. port_mask = DPLL_PORTC_READY_MASK;
  1581. dpll_reg = DPLL(0);
  1582. expected_mask <<= 4;
  1583. break;
  1584. case PORT_D:
  1585. port_mask = DPLL_PORTD_READY_MASK;
  1586. dpll_reg = DPIO_PHY_STATUS;
  1587. break;
  1588. default:
  1589. BUG();
  1590. }
  1591. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1592. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1593. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1594. }
  1595. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1596. {
  1597. struct drm_device *dev = crtc->base.dev;
  1598. struct drm_i915_private *dev_priv = dev->dev_private;
  1599. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1600. if (WARN_ON(pll == NULL))
  1601. return;
  1602. WARN_ON(!pll->config.crtc_mask);
  1603. if (pll->active == 0) {
  1604. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1605. WARN_ON(pll->on);
  1606. assert_shared_dpll_disabled(dev_priv, pll);
  1607. pll->mode_set(dev_priv, pll);
  1608. }
  1609. }
  1610. /**
  1611. * intel_enable_shared_dpll - enable PCH PLL
  1612. * @dev_priv: i915 private structure
  1613. * @pipe: pipe PLL to enable
  1614. *
  1615. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1616. * drives the transcoder clock.
  1617. */
  1618. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1619. {
  1620. struct drm_device *dev = crtc->base.dev;
  1621. struct drm_i915_private *dev_priv = dev->dev_private;
  1622. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1623. if (WARN_ON(pll == NULL))
  1624. return;
  1625. if (WARN_ON(pll->config.crtc_mask == 0))
  1626. return;
  1627. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1628. pll->name, pll->active, pll->on,
  1629. crtc->base.base.id);
  1630. if (pll->active++) {
  1631. WARN_ON(!pll->on);
  1632. assert_shared_dpll_enabled(dev_priv, pll);
  1633. return;
  1634. }
  1635. WARN_ON(pll->on);
  1636. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1637. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1638. pll->enable(dev_priv, pll);
  1639. pll->on = true;
  1640. }
  1641. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1642. {
  1643. struct drm_device *dev = crtc->base.dev;
  1644. struct drm_i915_private *dev_priv = dev->dev_private;
  1645. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1646. /* PCH only available on ILK+ */
  1647. if (INTEL_INFO(dev)->gen < 5)
  1648. return;
  1649. if (pll == NULL)
  1650. return;
  1651. if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
  1652. return;
  1653. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1654. pll->name, pll->active, pll->on,
  1655. crtc->base.base.id);
  1656. if (WARN_ON(pll->active == 0)) {
  1657. assert_shared_dpll_disabled(dev_priv, pll);
  1658. return;
  1659. }
  1660. assert_shared_dpll_enabled(dev_priv, pll);
  1661. WARN_ON(!pll->on);
  1662. if (--pll->active)
  1663. return;
  1664. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1665. pll->disable(dev_priv, pll);
  1666. pll->on = false;
  1667. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1668. }
  1669. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1670. enum pipe pipe)
  1671. {
  1672. struct drm_device *dev = dev_priv->dev;
  1673. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1674. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1675. uint32_t reg, val, pipeconf_val;
  1676. /* PCH only available on ILK+ */
  1677. BUG_ON(!HAS_PCH_SPLIT(dev));
  1678. /* Make sure PCH DPLL is enabled */
  1679. assert_shared_dpll_enabled(dev_priv,
  1680. intel_crtc_to_shared_dpll(intel_crtc));
  1681. /* FDI must be feeding us bits for PCH ports */
  1682. assert_fdi_tx_enabled(dev_priv, pipe);
  1683. assert_fdi_rx_enabled(dev_priv, pipe);
  1684. if (HAS_PCH_CPT(dev)) {
  1685. /* Workaround: Set the timing override bit before enabling the
  1686. * pch transcoder. */
  1687. reg = TRANS_CHICKEN2(pipe);
  1688. val = I915_READ(reg);
  1689. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1690. I915_WRITE(reg, val);
  1691. }
  1692. reg = PCH_TRANSCONF(pipe);
  1693. val = I915_READ(reg);
  1694. pipeconf_val = I915_READ(PIPECONF(pipe));
  1695. if (HAS_PCH_IBX(dev_priv->dev)) {
  1696. /*
  1697. * Make the BPC in transcoder be consistent with
  1698. * that in pipeconf reg. For HDMI we must use 8bpc
  1699. * here for both 8bpc and 12bpc.
  1700. */
  1701. val &= ~PIPECONF_BPC_MASK;
  1702. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1703. val |= PIPECONF_8BPC;
  1704. else
  1705. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1706. }
  1707. val &= ~TRANS_INTERLACE_MASK;
  1708. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1709. if (HAS_PCH_IBX(dev_priv->dev) &&
  1710. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1711. val |= TRANS_LEGACY_INTERLACED_ILK;
  1712. else
  1713. val |= TRANS_INTERLACED;
  1714. else
  1715. val |= TRANS_PROGRESSIVE;
  1716. I915_WRITE(reg, val | TRANS_ENABLE);
  1717. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1718. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1719. }
  1720. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1721. enum transcoder cpu_transcoder)
  1722. {
  1723. u32 val, pipeconf_val;
  1724. /* PCH only available on ILK+ */
  1725. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1726. /* FDI must be feeding us bits for PCH ports */
  1727. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1728. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1729. /* Workaround: set timing override bit. */
  1730. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1731. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1732. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1733. val = TRANS_ENABLE;
  1734. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1735. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1736. PIPECONF_INTERLACED_ILK)
  1737. val |= TRANS_INTERLACED;
  1738. else
  1739. val |= TRANS_PROGRESSIVE;
  1740. I915_WRITE(LPT_TRANSCONF, val);
  1741. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1742. DRM_ERROR("Failed to enable PCH transcoder\n");
  1743. }
  1744. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1745. enum pipe pipe)
  1746. {
  1747. struct drm_device *dev = dev_priv->dev;
  1748. uint32_t reg, val;
  1749. /* FDI relies on the transcoder */
  1750. assert_fdi_tx_disabled(dev_priv, pipe);
  1751. assert_fdi_rx_disabled(dev_priv, pipe);
  1752. /* Ports must be off as well */
  1753. assert_pch_ports_disabled(dev_priv, pipe);
  1754. reg = PCH_TRANSCONF(pipe);
  1755. val = I915_READ(reg);
  1756. val &= ~TRANS_ENABLE;
  1757. I915_WRITE(reg, val);
  1758. /* wait for PCH transcoder off, transcoder state */
  1759. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1760. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1761. if (!HAS_PCH_IBX(dev)) {
  1762. /* Workaround: Clear the timing override chicken bit again. */
  1763. reg = TRANS_CHICKEN2(pipe);
  1764. val = I915_READ(reg);
  1765. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1766. I915_WRITE(reg, val);
  1767. }
  1768. }
  1769. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1770. {
  1771. u32 val;
  1772. val = I915_READ(LPT_TRANSCONF);
  1773. val &= ~TRANS_ENABLE;
  1774. I915_WRITE(LPT_TRANSCONF, val);
  1775. /* wait for PCH transcoder off, transcoder state */
  1776. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1777. DRM_ERROR("Failed to disable PCH transcoder\n");
  1778. /* Workaround: clear timing override bit. */
  1779. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1780. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1781. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1782. }
  1783. /**
  1784. * intel_enable_pipe - enable a pipe, asserting requirements
  1785. * @crtc: crtc responsible for the pipe
  1786. *
  1787. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1788. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1789. */
  1790. static void intel_enable_pipe(struct intel_crtc *crtc)
  1791. {
  1792. struct drm_device *dev = crtc->base.dev;
  1793. struct drm_i915_private *dev_priv = dev->dev_private;
  1794. enum pipe pipe = crtc->pipe;
  1795. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1796. enum pipe pch_transcoder;
  1797. int reg;
  1798. u32 val;
  1799. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1800. assert_planes_disabled(dev_priv, pipe);
  1801. assert_cursor_disabled(dev_priv, pipe);
  1802. assert_sprites_disabled(dev_priv, pipe);
  1803. if (HAS_PCH_LPT(dev_priv->dev))
  1804. pch_transcoder = TRANSCODER_A;
  1805. else
  1806. pch_transcoder = pipe;
  1807. /*
  1808. * A pipe without a PLL won't actually be able to drive bits from
  1809. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1810. * need the check.
  1811. */
  1812. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1813. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1814. assert_dsi_pll_enabled(dev_priv);
  1815. else
  1816. assert_pll_enabled(dev_priv, pipe);
  1817. else {
  1818. if (crtc->config->has_pch_encoder) {
  1819. /* if driving the PCH, we need FDI enabled */
  1820. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1821. assert_fdi_tx_pll_enabled(dev_priv,
  1822. (enum pipe) cpu_transcoder);
  1823. }
  1824. /* FIXME: assert CPU port conditions for SNB+ */
  1825. }
  1826. reg = PIPECONF(cpu_transcoder);
  1827. val = I915_READ(reg);
  1828. if (val & PIPECONF_ENABLE) {
  1829. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1830. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1831. return;
  1832. }
  1833. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1834. POSTING_READ(reg);
  1835. }
  1836. /**
  1837. * intel_disable_pipe - disable a pipe, asserting requirements
  1838. * @crtc: crtc whose pipes is to be disabled
  1839. *
  1840. * Disable the pipe of @crtc, making sure that various hardware
  1841. * specific requirements are met, if applicable, e.g. plane
  1842. * disabled, panel fitter off, etc.
  1843. *
  1844. * Will wait until the pipe has shut down before returning.
  1845. */
  1846. static void intel_disable_pipe(struct intel_crtc *crtc)
  1847. {
  1848. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1849. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1850. enum pipe pipe = crtc->pipe;
  1851. int reg;
  1852. u32 val;
  1853. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1854. /*
  1855. * Make sure planes won't keep trying to pump pixels to us,
  1856. * or we might hang the display.
  1857. */
  1858. assert_planes_disabled(dev_priv, pipe);
  1859. assert_cursor_disabled(dev_priv, pipe);
  1860. assert_sprites_disabled(dev_priv, pipe);
  1861. reg = PIPECONF(cpu_transcoder);
  1862. val = I915_READ(reg);
  1863. if ((val & PIPECONF_ENABLE) == 0)
  1864. return;
  1865. /*
  1866. * Double wide has implications for planes
  1867. * so best keep it disabled when not needed.
  1868. */
  1869. if (crtc->config->double_wide)
  1870. val &= ~PIPECONF_DOUBLE_WIDE;
  1871. /* Don't disable pipe or pipe PLLs if needed */
  1872. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1873. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1874. val &= ~PIPECONF_ENABLE;
  1875. I915_WRITE(reg, val);
  1876. if ((val & PIPECONF_ENABLE) == 0)
  1877. intel_wait_for_pipe_off(crtc);
  1878. }
  1879. static bool need_vtd_wa(struct drm_device *dev)
  1880. {
  1881. #ifdef CONFIG_INTEL_IOMMU
  1882. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1883. return true;
  1884. #endif
  1885. return false;
  1886. }
  1887. unsigned int
  1888. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1889. uint64_t fb_format_modifier, unsigned int plane)
  1890. {
  1891. unsigned int tile_height;
  1892. uint32_t pixel_bytes;
  1893. switch (fb_format_modifier) {
  1894. case DRM_FORMAT_MOD_NONE:
  1895. tile_height = 1;
  1896. break;
  1897. case I915_FORMAT_MOD_X_TILED:
  1898. tile_height = IS_GEN2(dev) ? 16 : 8;
  1899. break;
  1900. case I915_FORMAT_MOD_Y_TILED:
  1901. tile_height = 32;
  1902. break;
  1903. case I915_FORMAT_MOD_Yf_TILED:
  1904. pixel_bytes = drm_format_plane_cpp(pixel_format, plane);
  1905. switch (pixel_bytes) {
  1906. default:
  1907. case 1:
  1908. tile_height = 64;
  1909. break;
  1910. case 2:
  1911. case 4:
  1912. tile_height = 32;
  1913. break;
  1914. case 8:
  1915. tile_height = 16;
  1916. break;
  1917. case 16:
  1918. WARN_ONCE(1,
  1919. "128-bit pixels are not supported for display!");
  1920. tile_height = 16;
  1921. break;
  1922. }
  1923. break;
  1924. default:
  1925. MISSING_CASE(fb_format_modifier);
  1926. tile_height = 1;
  1927. break;
  1928. }
  1929. return tile_height;
  1930. }
  1931. unsigned int
  1932. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1933. uint32_t pixel_format, uint64_t fb_format_modifier)
  1934. {
  1935. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1936. fb_format_modifier, 0));
  1937. }
  1938. static int
  1939. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1940. const struct drm_plane_state *plane_state)
  1941. {
  1942. struct intel_rotation_info *info = &view->rotation_info;
  1943. unsigned int tile_height, tile_pitch;
  1944. *view = i915_ggtt_view_normal;
  1945. if (!plane_state)
  1946. return 0;
  1947. if (!intel_rotation_90_or_270(plane_state->rotation))
  1948. return 0;
  1949. *view = i915_ggtt_view_rotated;
  1950. info->height = fb->height;
  1951. info->pixel_format = fb->pixel_format;
  1952. info->pitch = fb->pitches[0];
  1953. info->uv_offset = fb->offsets[1];
  1954. info->fb_modifier = fb->modifier[0];
  1955. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1956. fb->modifier[0], 0);
  1957. tile_pitch = PAGE_SIZE / tile_height;
  1958. info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1959. info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
  1960. info->size = info->width_pages * info->height_pages * PAGE_SIZE;
  1961. if (info->pixel_format == DRM_FORMAT_NV12) {
  1962. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1963. fb->modifier[0], 1);
  1964. tile_pitch = PAGE_SIZE / tile_height;
  1965. info->width_pages_uv = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1966. info->height_pages_uv = DIV_ROUND_UP(fb->height / 2,
  1967. tile_height);
  1968. info->size_uv = info->width_pages_uv * info->height_pages_uv *
  1969. PAGE_SIZE;
  1970. }
  1971. return 0;
  1972. }
  1973. static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
  1974. {
  1975. if (INTEL_INFO(dev_priv)->gen >= 9)
  1976. return 256 * 1024;
  1977. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1978. IS_VALLEYVIEW(dev_priv))
  1979. return 128 * 1024;
  1980. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1981. return 4 * 1024;
  1982. else
  1983. return 0;
  1984. }
  1985. int
  1986. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1987. struct drm_framebuffer *fb,
  1988. const struct drm_plane_state *plane_state)
  1989. {
  1990. struct drm_device *dev = fb->dev;
  1991. struct drm_i915_private *dev_priv = dev->dev_private;
  1992. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1993. struct i915_ggtt_view view;
  1994. u32 alignment;
  1995. int ret;
  1996. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1997. switch (fb->modifier[0]) {
  1998. case DRM_FORMAT_MOD_NONE:
  1999. alignment = intel_linear_alignment(dev_priv);
  2000. break;
  2001. case I915_FORMAT_MOD_X_TILED:
  2002. if (INTEL_INFO(dev)->gen >= 9)
  2003. alignment = 256 * 1024;
  2004. else {
  2005. /* pin() will align the object as required by fence */
  2006. alignment = 0;
  2007. }
  2008. break;
  2009. case I915_FORMAT_MOD_Y_TILED:
  2010. case I915_FORMAT_MOD_Yf_TILED:
  2011. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2012. "Y tiling bo slipped through, driver bug!\n"))
  2013. return -EINVAL;
  2014. alignment = 1 * 1024 * 1024;
  2015. break;
  2016. default:
  2017. MISSING_CASE(fb->modifier[0]);
  2018. return -EINVAL;
  2019. }
  2020. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2021. if (ret)
  2022. return ret;
  2023. /* Note that the w/a also requires 64 PTE of padding following the
  2024. * bo. We currently fill all unused PTE with the shadow page and so
  2025. * we should always have valid PTE following the scanout preventing
  2026. * the VT-d warning.
  2027. */
  2028. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2029. alignment = 256 * 1024;
  2030. /*
  2031. * Global gtt pte registers are special registers which actually forward
  2032. * writes to a chunk of system memory. Which means that there is no risk
  2033. * that the register values disappear as soon as we call
  2034. * intel_runtime_pm_put(), so it is correct to wrap only the
  2035. * pin/unpin/fence and not more.
  2036. */
  2037. intel_runtime_pm_get(dev_priv);
  2038. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  2039. &view);
  2040. if (ret)
  2041. goto err_pm;
  2042. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2043. * fence, whereas 965+ only requires a fence if using
  2044. * framebuffer compression. For simplicity, we always install
  2045. * a fence as the cost is not that onerous.
  2046. */
  2047. ret = i915_gem_object_get_fence(obj);
  2048. if (ret == -EDEADLK) {
  2049. /*
  2050. * -EDEADLK means there are no free fences
  2051. * no pending flips.
  2052. *
  2053. * This is propagated to atomic, but it uses
  2054. * -EDEADLK to force a locking recovery, so
  2055. * change the returned error to -EBUSY.
  2056. */
  2057. ret = -EBUSY;
  2058. goto err_unpin;
  2059. } else if (ret)
  2060. goto err_unpin;
  2061. i915_gem_object_pin_fence(obj);
  2062. intel_runtime_pm_put(dev_priv);
  2063. return 0;
  2064. err_unpin:
  2065. i915_gem_object_unpin_from_display_plane(obj, &view);
  2066. err_pm:
  2067. intel_runtime_pm_put(dev_priv);
  2068. return ret;
  2069. }
  2070. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2071. const struct drm_plane_state *plane_state)
  2072. {
  2073. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2074. struct i915_ggtt_view view;
  2075. int ret;
  2076. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2077. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2078. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2079. i915_gem_object_unpin_fence(obj);
  2080. i915_gem_object_unpin_from_display_plane(obj, &view);
  2081. }
  2082. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2083. * is assumed to be a power-of-two. */
  2084. unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
  2085. int *x, int *y,
  2086. unsigned int tiling_mode,
  2087. unsigned int cpp,
  2088. unsigned int pitch)
  2089. {
  2090. if (tiling_mode != I915_TILING_NONE) {
  2091. unsigned int tile_rows, tiles;
  2092. tile_rows = *y / 8;
  2093. *y %= 8;
  2094. tiles = *x / (512/cpp);
  2095. *x %= 512/cpp;
  2096. return tile_rows * pitch * 8 + tiles * 4096;
  2097. } else {
  2098. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2099. unsigned int offset;
  2100. offset = *y * pitch + *x * cpp;
  2101. *y = (offset & alignment) / pitch;
  2102. *x = ((offset & alignment) - *y * pitch) / cpp;
  2103. return offset & ~alignment;
  2104. }
  2105. }
  2106. static int i9xx_format_to_fourcc(int format)
  2107. {
  2108. switch (format) {
  2109. case DISPPLANE_8BPP:
  2110. return DRM_FORMAT_C8;
  2111. case DISPPLANE_BGRX555:
  2112. return DRM_FORMAT_XRGB1555;
  2113. case DISPPLANE_BGRX565:
  2114. return DRM_FORMAT_RGB565;
  2115. default:
  2116. case DISPPLANE_BGRX888:
  2117. return DRM_FORMAT_XRGB8888;
  2118. case DISPPLANE_RGBX888:
  2119. return DRM_FORMAT_XBGR8888;
  2120. case DISPPLANE_BGRX101010:
  2121. return DRM_FORMAT_XRGB2101010;
  2122. case DISPPLANE_RGBX101010:
  2123. return DRM_FORMAT_XBGR2101010;
  2124. }
  2125. }
  2126. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2127. {
  2128. switch (format) {
  2129. case PLANE_CTL_FORMAT_RGB_565:
  2130. return DRM_FORMAT_RGB565;
  2131. default:
  2132. case PLANE_CTL_FORMAT_XRGB_8888:
  2133. if (rgb_order) {
  2134. if (alpha)
  2135. return DRM_FORMAT_ABGR8888;
  2136. else
  2137. return DRM_FORMAT_XBGR8888;
  2138. } else {
  2139. if (alpha)
  2140. return DRM_FORMAT_ARGB8888;
  2141. else
  2142. return DRM_FORMAT_XRGB8888;
  2143. }
  2144. case PLANE_CTL_FORMAT_XRGB_2101010:
  2145. if (rgb_order)
  2146. return DRM_FORMAT_XBGR2101010;
  2147. else
  2148. return DRM_FORMAT_XRGB2101010;
  2149. }
  2150. }
  2151. static bool
  2152. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2153. struct intel_initial_plane_config *plane_config)
  2154. {
  2155. struct drm_device *dev = crtc->base.dev;
  2156. struct drm_i915_private *dev_priv = to_i915(dev);
  2157. struct drm_i915_gem_object *obj = NULL;
  2158. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2159. struct drm_framebuffer *fb = &plane_config->fb->base;
  2160. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2161. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2162. PAGE_SIZE);
  2163. size_aligned -= base_aligned;
  2164. if (plane_config->size == 0)
  2165. return false;
  2166. /* If the FB is too big, just don't use it since fbdev is not very
  2167. * important and we should probably use that space with FBC or other
  2168. * features. */
  2169. if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
  2170. return false;
  2171. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2172. base_aligned,
  2173. base_aligned,
  2174. size_aligned);
  2175. if (!obj)
  2176. return false;
  2177. obj->tiling_mode = plane_config->tiling;
  2178. if (obj->tiling_mode == I915_TILING_X)
  2179. obj->stride = fb->pitches[0];
  2180. mode_cmd.pixel_format = fb->pixel_format;
  2181. mode_cmd.width = fb->width;
  2182. mode_cmd.height = fb->height;
  2183. mode_cmd.pitches[0] = fb->pitches[0];
  2184. mode_cmd.modifier[0] = fb->modifier[0];
  2185. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2186. mutex_lock(&dev->struct_mutex);
  2187. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2188. &mode_cmd, obj)) {
  2189. DRM_DEBUG_KMS("intel fb init failed\n");
  2190. goto out_unref_obj;
  2191. }
  2192. mutex_unlock(&dev->struct_mutex);
  2193. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2194. return true;
  2195. out_unref_obj:
  2196. drm_gem_object_unreference(&obj->base);
  2197. mutex_unlock(&dev->struct_mutex);
  2198. return false;
  2199. }
  2200. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2201. static void
  2202. update_state_fb(struct drm_plane *plane)
  2203. {
  2204. if (plane->fb == plane->state->fb)
  2205. return;
  2206. if (plane->state->fb)
  2207. drm_framebuffer_unreference(plane->state->fb);
  2208. plane->state->fb = plane->fb;
  2209. if (plane->state->fb)
  2210. drm_framebuffer_reference(plane->state->fb);
  2211. }
  2212. static void
  2213. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2214. struct intel_initial_plane_config *plane_config)
  2215. {
  2216. struct drm_device *dev = intel_crtc->base.dev;
  2217. struct drm_i915_private *dev_priv = dev->dev_private;
  2218. struct drm_crtc *c;
  2219. struct intel_crtc *i;
  2220. struct drm_i915_gem_object *obj;
  2221. struct drm_plane *primary = intel_crtc->base.primary;
  2222. struct drm_plane_state *plane_state = primary->state;
  2223. struct drm_framebuffer *fb;
  2224. if (!plane_config->fb)
  2225. return;
  2226. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2227. fb = &plane_config->fb->base;
  2228. goto valid_fb;
  2229. }
  2230. kfree(plane_config->fb);
  2231. /*
  2232. * Failed to alloc the obj, check to see if we should share
  2233. * an fb with another CRTC instead
  2234. */
  2235. for_each_crtc(dev, c) {
  2236. i = to_intel_crtc(c);
  2237. if (c == &intel_crtc->base)
  2238. continue;
  2239. if (!i->active)
  2240. continue;
  2241. fb = c->primary->fb;
  2242. if (!fb)
  2243. continue;
  2244. obj = intel_fb_obj(fb);
  2245. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2246. drm_framebuffer_reference(fb);
  2247. goto valid_fb;
  2248. }
  2249. }
  2250. return;
  2251. valid_fb:
  2252. plane_state->src_x = plane_state->src_y = 0;
  2253. plane_state->src_w = fb->width << 16;
  2254. plane_state->src_h = fb->height << 16;
  2255. plane_state->crtc_x = plane_state->src_y = 0;
  2256. plane_state->crtc_w = fb->width;
  2257. plane_state->crtc_h = fb->height;
  2258. obj = intel_fb_obj(fb);
  2259. if (obj->tiling_mode != I915_TILING_NONE)
  2260. dev_priv->preserve_bios_swizzle = true;
  2261. drm_framebuffer_reference(fb);
  2262. primary->fb = primary->state->fb = fb;
  2263. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2264. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2265. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2266. }
  2267. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2268. struct drm_framebuffer *fb,
  2269. int x, int y)
  2270. {
  2271. struct drm_device *dev = crtc->dev;
  2272. struct drm_i915_private *dev_priv = dev->dev_private;
  2273. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2274. struct drm_plane *primary = crtc->primary;
  2275. bool visible = to_intel_plane_state(primary->state)->visible;
  2276. struct drm_i915_gem_object *obj;
  2277. int plane = intel_crtc->plane;
  2278. unsigned long linear_offset;
  2279. u32 dspcntr;
  2280. u32 reg = DSPCNTR(plane);
  2281. int pixel_size;
  2282. if (!visible || !fb) {
  2283. I915_WRITE(reg, 0);
  2284. if (INTEL_INFO(dev)->gen >= 4)
  2285. I915_WRITE(DSPSURF(plane), 0);
  2286. else
  2287. I915_WRITE(DSPADDR(plane), 0);
  2288. POSTING_READ(reg);
  2289. return;
  2290. }
  2291. obj = intel_fb_obj(fb);
  2292. if (WARN_ON(obj == NULL))
  2293. return;
  2294. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2295. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2296. dspcntr |= DISPLAY_PLANE_ENABLE;
  2297. if (INTEL_INFO(dev)->gen < 4) {
  2298. if (intel_crtc->pipe == PIPE_B)
  2299. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2300. /* pipesrc and dspsize control the size that is scaled from,
  2301. * which should always be the user's requested size.
  2302. */
  2303. I915_WRITE(DSPSIZE(plane),
  2304. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2305. (intel_crtc->config->pipe_src_w - 1));
  2306. I915_WRITE(DSPPOS(plane), 0);
  2307. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2308. I915_WRITE(PRIMSIZE(plane),
  2309. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2310. (intel_crtc->config->pipe_src_w - 1));
  2311. I915_WRITE(PRIMPOS(plane), 0);
  2312. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2313. }
  2314. switch (fb->pixel_format) {
  2315. case DRM_FORMAT_C8:
  2316. dspcntr |= DISPPLANE_8BPP;
  2317. break;
  2318. case DRM_FORMAT_XRGB1555:
  2319. dspcntr |= DISPPLANE_BGRX555;
  2320. break;
  2321. case DRM_FORMAT_RGB565:
  2322. dspcntr |= DISPPLANE_BGRX565;
  2323. break;
  2324. case DRM_FORMAT_XRGB8888:
  2325. dspcntr |= DISPPLANE_BGRX888;
  2326. break;
  2327. case DRM_FORMAT_XBGR8888:
  2328. dspcntr |= DISPPLANE_RGBX888;
  2329. break;
  2330. case DRM_FORMAT_XRGB2101010:
  2331. dspcntr |= DISPPLANE_BGRX101010;
  2332. break;
  2333. case DRM_FORMAT_XBGR2101010:
  2334. dspcntr |= DISPPLANE_RGBX101010;
  2335. break;
  2336. default:
  2337. BUG();
  2338. }
  2339. if (INTEL_INFO(dev)->gen >= 4 &&
  2340. obj->tiling_mode != I915_TILING_NONE)
  2341. dspcntr |= DISPPLANE_TILED;
  2342. if (IS_G4X(dev))
  2343. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2344. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2345. if (INTEL_INFO(dev)->gen >= 4) {
  2346. intel_crtc->dspaddr_offset =
  2347. intel_gen4_compute_page_offset(dev_priv,
  2348. &x, &y, obj->tiling_mode,
  2349. pixel_size,
  2350. fb->pitches[0]);
  2351. linear_offset -= intel_crtc->dspaddr_offset;
  2352. } else {
  2353. intel_crtc->dspaddr_offset = linear_offset;
  2354. }
  2355. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2356. dspcntr |= DISPPLANE_ROTATE_180;
  2357. x += (intel_crtc->config->pipe_src_w - 1);
  2358. y += (intel_crtc->config->pipe_src_h - 1);
  2359. /* Finding the last pixel of the last line of the display
  2360. data and adding to linear_offset*/
  2361. linear_offset +=
  2362. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2363. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2364. }
  2365. intel_crtc->adjusted_x = x;
  2366. intel_crtc->adjusted_y = y;
  2367. I915_WRITE(reg, dspcntr);
  2368. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2369. if (INTEL_INFO(dev)->gen >= 4) {
  2370. I915_WRITE(DSPSURF(plane),
  2371. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2372. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2373. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2374. } else
  2375. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2376. POSTING_READ(reg);
  2377. }
  2378. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2379. struct drm_framebuffer *fb,
  2380. int x, int y)
  2381. {
  2382. struct drm_device *dev = crtc->dev;
  2383. struct drm_i915_private *dev_priv = dev->dev_private;
  2384. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2385. struct drm_plane *primary = crtc->primary;
  2386. bool visible = to_intel_plane_state(primary->state)->visible;
  2387. struct drm_i915_gem_object *obj;
  2388. int plane = intel_crtc->plane;
  2389. unsigned long linear_offset;
  2390. u32 dspcntr;
  2391. u32 reg = DSPCNTR(plane);
  2392. int pixel_size;
  2393. if (!visible || !fb) {
  2394. I915_WRITE(reg, 0);
  2395. I915_WRITE(DSPSURF(plane), 0);
  2396. POSTING_READ(reg);
  2397. return;
  2398. }
  2399. obj = intel_fb_obj(fb);
  2400. if (WARN_ON(obj == NULL))
  2401. return;
  2402. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2403. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2404. dspcntr |= DISPLAY_PLANE_ENABLE;
  2405. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2406. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2407. switch (fb->pixel_format) {
  2408. case DRM_FORMAT_C8:
  2409. dspcntr |= DISPPLANE_8BPP;
  2410. break;
  2411. case DRM_FORMAT_RGB565:
  2412. dspcntr |= DISPPLANE_BGRX565;
  2413. break;
  2414. case DRM_FORMAT_XRGB8888:
  2415. dspcntr |= DISPPLANE_BGRX888;
  2416. break;
  2417. case DRM_FORMAT_XBGR8888:
  2418. dspcntr |= DISPPLANE_RGBX888;
  2419. break;
  2420. case DRM_FORMAT_XRGB2101010:
  2421. dspcntr |= DISPPLANE_BGRX101010;
  2422. break;
  2423. case DRM_FORMAT_XBGR2101010:
  2424. dspcntr |= DISPPLANE_RGBX101010;
  2425. break;
  2426. default:
  2427. BUG();
  2428. }
  2429. if (obj->tiling_mode != I915_TILING_NONE)
  2430. dspcntr |= DISPPLANE_TILED;
  2431. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2432. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2433. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2434. intel_crtc->dspaddr_offset =
  2435. intel_gen4_compute_page_offset(dev_priv,
  2436. &x, &y, obj->tiling_mode,
  2437. pixel_size,
  2438. fb->pitches[0]);
  2439. linear_offset -= intel_crtc->dspaddr_offset;
  2440. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2441. dspcntr |= DISPPLANE_ROTATE_180;
  2442. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2443. x += (intel_crtc->config->pipe_src_w - 1);
  2444. y += (intel_crtc->config->pipe_src_h - 1);
  2445. /* Finding the last pixel of the last line of the display
  2446. data and adding to linear_offset*/
  2447. linear_offset +=
  2448. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2449. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2450. }
  2451. }
  2452. intel_crtc->adjusted_x = x;
  2453. intel_crtc->adjusted_y = y;
  2454. I915_WRITE(reg, dspcntr);
  2455. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2456. I915_WRITE(DSPSURF(plane),
  2457. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2458. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2459. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2460. } else {
  2461. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2462. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2463. }
  2464. POSTING_READ(reg);
  2465. }
  2466. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2467. uint32_t pixel_format)
  2468. {
  2469. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2470. /*
  2471. * The stride is either expressed as a multiple of 64 bytes
  2472. * chunks for linear buffers or in number of tiles for tiled
  2473. * buffers.
  2474. */
  2475. switch (fb_modifier) {
  2476. case DRM_FORMAT_MOD_NONE:
  2477. return 64;
  2478. case I915_FORMAT_MOD_X_TILED:
  2479. if (INTEL_INFO(dev)->gen == 2)
  2480. return 128;
  2481. return 512;
  2482. case I915_FORMAT_MOD_Y_TILED:
  2483. /* No need to check for old gens and Y tiling since this is
  2484. * about the display engine and those will be blocked before
  2485. * we get here.
  2486. */
  2487. return 128;
  2488. case I915_FORMAT_MOD_Yf_TILED:
  2489. if (bits_per_pixel == 8)
  2490. return 64;
  2491. else
  2492. return 128;
  2493. default:
  2494. MISSING_CASE(fb_modifier);
  2495. return 64;
  2496. }
  2497. }
  2498. u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
  2499. struct drm_i915_gem_object *obj,
  2500. unsigned int plane)
  2501. {
  2502. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2503. struct i915_vma *vma;
  2504. u64 offset;
  2505. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2506. view = &i915_ggtt_view_rotated;
  2507. vma = i915_gem_obj_to_ggtt_view(obj, view);
  2508. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2509. view->type))
  2510. return -1;
  2511. offset = vma->node.start;
  2512. if (plane == 1) {
  2513. offset += vma->ggtt_view.rotation_info.uv_start_page *
  2514. PAGE_SIZE;
  2515. }
  2516. WARN_ON(upper_32_bits(offset));
  2517. return lower_32_bits(offset);
  2518. }
  2519. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2520. {
  2521. struct drm_device *dev = intel_crtc->base.dev;
  2522. struct drm_i915_private *dev_priv = dev->dev_private;
  2523. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2524. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2525. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2526. }
  2527. /*
  2528. * This function detaches (aka. unbinds) unused scalers in hardware
  2529. */
  2530. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2531. {
  2532. struct intel_crtc_scaler_state *scaler_state;
  2533. int i;
  2534. scaler_state = &intel_crtc->config->scaler_state;
  2535. /* loop through and disable scalers that aren't in use */
  2536. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2537. if (!scaler_state->scalers[i].in_use)
  2538. skl_detach_scaler(intel_crtc, i);
  2539. }
  2540. }
  2541. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2542. {
  2543. switch (pixel_format) {
  2544. case DRM_FORMAT_C8:
  2545. return PLANE_CTL_FORMAT_INDEXED;
  2546. case DRM_FORMAT_RGB565:
  2547. return PLANE_CTL_FORMAT_RGB_565;
  2548. case DRM_FORMAT_XBGR8888:
  2549. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2550. case DRM_FORMAT_XRGB8888:
  2551. return PLANE_CTL_FORMAT_XRGB_8888;
  2552. /*
  2553. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2554. * to be already pre-multiplied. We need to add a knob (or a different
  2555. * DRM_FORMAT) for user-space to configure that.
  2556. */
  2557. case DRM_FORMAT_ABGR8888:
  2558. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2559. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2560. case DRM_FORMAT_ARGB8888:
  2561. return PLANE_CTL_FORMAT_XRGB_8888 |
  2562. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2563. case DRM_FORMAT_XRGB2101010:
  2564. return PLANE_CTL_FORMAT_XRGB_2101010;
  2565. case DRM_FORMAT_XBGR2101010:
  2566. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2567. case DRM_FORMAT_YUYV:
  2568. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2569. case DRM_FORMAT_YVYU:
  2570. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2571. case DRM_FORMAT_UYVY:
  2572. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2573. case DRM_FORMAT_VYUY:
  2574. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2575. default:
  2576. MISSING_CASE(pixel_format);
  2577. }
  2578. return 0;
  2579. }
  2580. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2581. {
  2582. switch (fb_modifier) {
  2583. case DRM_FORMAT_MOD_NONE:
  2584. break;
  2585. case I915_FORMAT_MOD_X_TILED:
  2586. return PLANE_CTL_TILED_X;
  2587. case I915_FORMAT_MOD_Y_TILED:
  2588. return PLANE_CTL_TILED_Y;
  2589. case I915_FORMAT_MOD_Yf_TILED:
  2590. return PLANE_CTL_TILED_YF;
  2591. default:
  2592. MISSING_CASE(fb_modifier);
  2593. }
  2594. return 0;
  2595. }
  2596. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2597. {
  2598. switch (rotation) {
  2599. case BIT(DRM_ROTATE_0):
  2600. break;
  2601. /*
  2602. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2603. * while i915 HW rotation is clockwise, thats why this swapping.
  2604. */
  2605. case BIT(DRM_ROTATE_90):
  2606. return PLANE_CTL_ROTATE_270;
  2607. case BIT(DRM_ROTATE_180):
  2608. return PLANE_CTL_ROTATE_180;
  2609. case BIT(DRM_ROTATE_270):
  2610. return PLANE_CTL_ROTATE_90;
  2611. default:
  2612. MISSING_CASE(rotation);
  2613. }
  2614. return 0;
  2615. }
  2616. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2617. struct drm_framebuffer *fb,
  2618. int x, int y)
  2619. {
  2620. struct drm_device *dev = crtc->dev;
  2621. struct drm_i915_private *dev_priv = dev->dev_private;
  2622. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2623. struct drm_plane *plane = crtc->primary;
  2624. bool visible = to_intel_plane_state(plane->state)->visible;
  2625. struct drm_i915_gem_object *obj;
  2626. int pipe = intel_crtc->pipe;
  2627. u32 plane_ctl, stride_div, stride;
  2628. u32 tile_height, plane_offset, plane_size;
  2629. unsigned int rotation;
  2630. int x_offset, y_offset;
  2631. u32 surf_addr;
  2632. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2633. struct intel_plane_state *plane_state;
  2634. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2635. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2636. int scaler_id = -1;
  2637. plane_state = to_intel_plane_state(plane->state);
  2638. if (!visible || !fb) {
  2639. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2640. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2641. POSTING_READ(PLANE_CTL(pipe, 0));
  2642. return;
  2643. }
  2644. plane_ctl = PLANE_CTL_ENABLE |
  2645. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2646. PLANE_CTL_PIPE_CSC_ENABLE;
  2647. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2648. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2649. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2650. rotation = plane->state->rotation;
  2651. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2652. obj = intel_fb_obj(fb);
  2653. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2654. fb->pixel_format);
  2655. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2656. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2657. scaler_id = plane_state->scaler_id;
  2658. src_x = plane_state->src.x1 >> 16;
  2659. src_y = plane_state->src.y1 >> 16;
  2660. src_w = drm_rect_width(&plane_state->src) >> 16;
  2661. src_h = drm_rect_height(&plane_state->src) >> 16;
  2662. dst_x = plane_state->dst.x1;
  2663. dst_y = plane_state->dst.y1;
  2664. dst_w = drm_rect_width(&plane_state->dst);
  2665. dst_h = drm_rect_height(&plane_state->dst);
  2666. WARN_ON(x != src_x || y != src_y);
  2667. if (intel_rotation_90_or_270(rotation)) {
  2668. /* stride = Surface height in tiles */
  2669. tile_height = intel_tile_height(dev, fb->pixel_format,
  2670. fb->modifier[0], 0);
  2671. stride = DIV_ROUND_UP(fb->height, tile_height);
  2672. x_offset = stride * tile_height - y - src_h;
  2673. y_offset = x;
  2674. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2675. } else {
  2676. stride = fb->pitches[0] / stride_div;
  2677. x_offset = x;
  2678. y_offset = y;
  2679. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2680. }
  2681. plane_offset = y_offset << 16 | x_offset;
  2682. intel_crtc->adjusted_x = x_offset;
  2683. intel_crtc->adjusted_y = y_offset;
  2684. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2685. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2686. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2687. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2688. if (scaler_id >= 0) {
  2689. uint32_t ps_ctrl = 0;
  2690. WARN_ON(!dst_w || !dst_h);
  2691. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2692. crtc_state->scaler_state.scalers[scaler_id].mode;
  2693. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2694. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2695. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2696. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2697. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2698. } else {
  2699. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2700. }
  2701. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2702. POSTING_READ(PLANE_SURF(pipe, 0));
  2703. }
  2704. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2705. static int
  2706. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2707. int x, int y, enum mode_set_atomic state)
  2708. {
  2709. struct drm_device *dev = crtc->dev;
  2710. struct drm_i915_private *dev_priv = dev->dev_private;
  2711. if (dev_priv->fbc.disable_fbc)
  2712. dev_priv->fbc.disable_fbc(dev_priv);
  2713. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2714. return 0;
  2715. }
  2716. static void intel_complete_page_flips(struct drm_device *dev)
  2717. {
  2718. struct drm_crtc *crtc;
  2719. for_each_crtc(dev, crtc) {
  2720. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2721. enum plane plane = intel_crtc->plane;
  2722. intel_prepare_page_flip(dev, plane);
  2723. intel_finish_page_flip_plane(dev, plane);
  2724. }
  2725. }
  2726. static void intel_update_primary_planes(struct drm_device *dev)
  2727. {
  2728. struct drm_crtc *crtc;
  2729. for_each_crtc(dev, crtc) {
  2730. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2731. struct intel_plane_state *plane_state;
  2732. drm_modeset_lock_crtc(crtc, &plane->base);
  2733. plane_state = to_intel_plane_state(plane->base.state);
  2734. if (crtc->state->active && plane_state->base.fb)
  2735. plane->commit_plane(&plane->base, plane_state);
  2736. drm_modeset_unlock_crtc(crtc);
  2737. }
  2738. }
  2739. void intel_prepare_reset(struct drm_device *dev)
  2740. {
  2741. /* no reset support for gen2 */
  2742. if (IS_GEN2(dev))
  2743. return;
  2744. /* reset doesn't touch the display */
  2745. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2746. return;
  2747. drm_modeset_lock_all(dev);
  2748. /*
  2749. * Disabling the crtcs gracefully seems nicer. Also the
  2750. * g33 docs say we should at least disable all the planes.
  2751. */
  2752. intel_display_suspend(dev);
  2753. }
  2754. void intel_finish_reset(struct drm_device *dev)
  2755. {
  2756. struct drm_i915_private *dev_priv = to_i915(dev);
  2757. /*
  2758. * Flips in the rings will be nuked by the reset,
  2759. * so complete all pending flips so that user space
  2760. * will get its events and not get stuck.
  2761. */
  2762. intel_complete_page_flips(dev);
  2763. /* no reset support for gen2 */
  2764. if (IS_GEN2(dev))
  2765. return;
  2766. /* reset doesn't touch the display */
  2767. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2768. /*
  2769. * Flips in the rings have been nuked by the reset,
  2770. * so update the base address of all primary
  2771. * planes to the the last fb to make sure we're
  2772. * showing the correct fb after a reset.
  2773. *
  2774. * FIXME: Atomic will make this obsolete since we won't schedule
  2775. * CS-based flips (which might get lost in gpu resets) any more.
  2776. */
  2777. intel_update_primary_planes(dev);
  2778. return;
  2779. }
  2780. /*
  2781. * The display has been reset as well,
  2782. * so need a full re-initialization.
  2783. */
  2784. intel_runtime_pm_disable_interrupts(dev_priv);
  2785. intel_runtime_pm_enable_interrupts(dev_priv);
  2786. intel_modeset_init_hw(dev);
  2787. spin_lock_irq(&dev_priv->irq_lock);
  2788. if (dev_priv->display.hpd_irq_setup)
  2789. dev_priv->display.hpd_irq_setup(dev);
  2790. spin_unlock_irq(&dev_priv->irq_lock);
  2791. intel_display_resume(dev);
  2792. intel_hpd_init(dev_priv);
  2793. drm_modeset_unlock_all(dev);
  2794. }
  2795. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2796. {
  2797. struct drm_device *dev = crtc->dev;
  2798. struct drm_i915_private *dev_priv = dev->dev_private;
  2799. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2800. bool pending;
  2801. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2802. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2803. return false;
  2804. spin_lock_irq(&dev->event_lock);
  2805. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2806. spin_unlock_irq(&dev->event_lock);
  2807. return pending;
  2808. }
  2809. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2810. struct intel_crtc_state *old_crtc_state)
  2811. {
  2812. struct drm_device *dev = crtc->base.dev;
  2813. struct drm_i915_private *dev_priv = dev->dev_private;
  2814. struct intel_crtc_state *pipe_config =
  2815. to_intel_crtc_state(crtc->base.state);
  2816. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2817. crtc->base.mode = crtc->base.state->mode;
  2818. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2819. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2820. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2821. if (HAS_DDI(dev))
  2822. intel_set_pipe_csc(&crtc->base);
  2823. /*
  2824. * Update pipe size and adjust fitter if needed: the reason for this is
  2825. * that in compute_mode_changes we check the native mode (not the pfit
  2826. * mode) to see if we can flip rather than do a full mode set. In the
  2827. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2828. * pfit state, we'll end up with a big fb scanned out into the wrong
  2829. * sized surface.
  2830. */
  2831. I915_WRITE(PIPESRC(crtc->pipe),
  2832. ((pipe_config->pipe_src_w - 1) << 16) |
  2833. (pipe_config->pipe_src_h - 1));
  2834. /* on skylake this is done by detaching scalers */
  2835. if (INTEL_INFO(dev)->gen >= 9) {
  2836. skl_detach_scalers(crtc);
  2837. if (pipe_config->pch_pfit.enabled)
  2838. skylake_pfit_enable(crtc);
  2839. } else if (HAS_PCH_SPLIT(dev)) {
  2840. if (pipe_config->pch_pfit.enabled)
  2841. ironlake_pfit_enable(crtc);
  2842. else if (old_crtc_state->pch_pfit.enabled)
  2843. ironlake_pfit_disable(crtc, true);
  2844. }
  2845. }
  2846. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2847. {
  2848. struct drm_device *dev = crtc->dev;
  2849. struct drm_i915_private *dev_priv = dev->dev_private;
  2850. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2851. int pipe = intel_crtc->pipe;
  2852. u32 reg, temp;
  2853. /* enable normal train */
  2854. reg = FDI_TX_CTL(pipe);
  2855. temp = I915_READ(reg);
  2856. if (IS_IVYBRIDGE(dev)) {
  2857. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2858. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2859. } else {
  2860. temp &= ~FDI_LINK_TRAIN_NONE;
  2861. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2862. }
  2863. I915_WRITE(reg, temp);
  2864. reg = FDI_RX_CTL(pipe);
  2865. temp = I915_READ(reg);
  2866. if (HAS_PCH_CPT(dev)) {
  2867. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2868. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2869. } else {
  2870. temp &= ~FDI_LINK_TRAIN_NONE;
  2871. temp |= FDI_LINK_TRAIN_NONE;
  2872. }
  2873. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2874. /* wait one idle pattern time */
  2875. POSTING_READ(reg);
  2876. udelay(1000);
  2877. /* IVB wants error correction enabled */
  2878. if (IS_IVYBRIDGE(dev))
  2879. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2880. FDI_FE_ERRC_ENABLE);
  2881. }
  2882. /* The FDI link training functions for ILK/Ibexpeak. */
  2883. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2884. {
  2885. struct drm_device *dev = crtc->dev;
  2886. struct drm_i915_private *dev_priv = dev->dev_private;
  2887. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2888. int pipe = intel_crtc->pipe;
  2889. u32 reg, temp, tries;
  2890. /* FDI needs bits from pipe first */
  2891. assert_pipe_enabled(dev_priv, pipe);
  2892. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2893. for train result */
  2894. reg = FDI_RX_IMR(pipe);
  2895. temp = I915_READ(reg);
  2896. temp &= ~FDI_RX_SYMBOL_LOCK;
  2897. temp &= ~FDI_RX_BIT_LOCK;
  2898. I915_WRITE(reg, temp);
  2899. I915_READ(reg);
  2900. udelay(150);
  2901. /* enable CPU FDI TX and PCH FDI RX */
  2902. reg = FDI_TX_CTL(pipe);
  2903. temp = I915_READ(reg);
  2904. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2905. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2906. temp &= ~FDI_LINK_TRAIN_NONE;
  2907. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2908. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2909. reg = FDI_RX_CTL(pipe);
  2910. temp = I915_READ(reg);
  2911. temp &= ~FDI_LINK_TRAIN_NONE;
  2912. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2913. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2914. POSTING_READ(reg);
  2915. udelay(150);
  2916. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2917. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2918. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2919. FDI_RX_PHASE_SYNC_POINTER_EN);
  2920. reg = FDI_RX_IIR(pipe);
  2921. for (tries = 0; tries < 5; tries++) {
  2922. temp = I915_READ(reg);
  2923. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2924. if ((temp & FDI_RX_BIT_LOCK)) {
  2925. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2926. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2927. break;
  2928. }
  2929. }
  2930. if (tries == 5)
  2931. DRM_ERROR("FDI train 1 fail!\n");
  2932. /* Train 2 */
  2933. reg = FDI_TX_CTL(pipe);
  2934. temp = I915_READ(reg);
  2935. temp &= ~FDI_LINK_TRAIN_NONE;
  2936. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2937. I915_WRITE(reg, temp);
  2938. reg = FDI_RX_CTL(pipe);
  2939. temp = I915_READ(reg);
  2940. temp &= ~FDI_LINK_TRAIN_NONE;
  2941. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2942. I915_WRITE(reg, temp);
  2943. POSTING_READ(reg);
  2944. udelay(150);
  2945. reg = FDI_RX_IIR(pipe);
  2946. for (tries = 0; tries < 5; tries++) {
  2947. temp = I915_READ(reg);
  2948. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2949. if (temp & FDI_RX_SYMBOL_LOCK) {
  2950. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2951. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2952. break;
  2953. }
  2954. }
  2955. if (tries == 5)
  2956. DRM_ERROR("FDI train 2 fail!\n");
  2957. DRM_DEBUG_KMS("FDI train done\n");
  2958. }
  2959. static const int snb_b_fdi_train_param[] = {
  2960. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2961. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2962. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2963. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2964. };
  2965. /* The FDI link training functions for SNB/Cougarpoint. */
  2966. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2967. {
  2968. struct drm_device *dev = crtc->dev;
  2969. struct drm_i915_private *dev_priv = dev->dev_private;
  2970. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2971. int pipe = intel_crtc->pipe;
  2972. u32 reg, temp, i, retry;
  2973. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2974. for train result */
  2975. reg = FDI_RX_IMR(pipe);
  2976. temp = I915_READ(reg);
  2977. temp &= ~FDI_RX_SYMBOL_LOCK;
  2978. temp &= ~FDI_RX_BIT_LOCK;
  2979. I915_WRITE(reg, temp);
  2980. POSTING_READ(reg);
  2981. udelay(150);
  2982. /* enable CPU FDI TX and PCH FDI RX */
  2983. reg = FDI_TX_CTL(pipe);
  2984. temp = I915_READ(reg);
  2985. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2986. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2987. temp &= ~FDI_LINK_TRAIN_NONE;
  2988. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2989. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2990. /* SNB-B */
  2991. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2992. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2993. I915_WRITE(FDI_RX_MISC(pipe),
  2994. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2995. reg = FDI_RX_CTL(pipe);
  2996. temp = I915_READ(reg);
  2997. if (HAS_PCH_CPT(dev)) {
  2998. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2999. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3000. } else {
  3001. temp &= ~FDI_LINK_TRAIN_NONE;
  3002. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3003. }
  3004. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3005. POSTING_READ(reg);
  3006. udelay(150);
  3007. for (i = 0; i < 4; i++) {
  3008. reg = FDI_TX_CTL(pipe);
  3009. temp = I915_READ(reg);
  3010. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3011. temp |= snb_b_fdi_train_param[i];
  3012. I915_WRITE(reg, temp);
  3013. POSTING_READ(reg);
  3014. udelay(500);
  3015. for (retry = 0; retry < 5; retry++) {
  3016. reg = FDI_RX_IIR(pipe);
  3017. temp = I915_READ(reg);
  3018. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3019. if (temp & FDI_RX_BIT_LOCK) {
  3020. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3021. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3022. break;
  3023. }
  3024. udelay(50);
  3025. }
  3026. if (retry < 5)
  3027. break;
  3028. }
  3029. if (i == 4)
  3030. DRM_ERROR("FDI train 1 fail!\n");
  3031. /* Train 2 */
  3032. reg = FDI_TX_CTL(pipe);
  3033. temp = I915_READ(reg);
  3034. temp &= ~FDI_LINK_TRAIN_NONE;
  3035. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3036. if (IS_GEN6(dev)) {
  3037. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3038. /* SNB-B */
  3039. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3040. }
  3041. I915_WRITE(reg, temp);
  3042. reg = FDI_RX_CTL(pipe);
  3043. temp = I915_READ(reg);
  3044. if (HAS_PCH_CPT(dev)) {
  3045. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3046. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3047. } else {
  3048. temp &= ~FDI_LINK_TRAIN_NONE;
  3049. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3050. }
  3051. I915_WRITE(reg, temp);
  3052. POSTING_READ(reg);
  3053. udelay(150);
  3054. for (i = 0; i < 4; i++) {
  3055. reg = FDI_TX_CTL(pipe);
  3056. temp = I915_READ(reg);
  3057. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3058. temp |= snb_b_fdi_train_param[i];
  3059. I915_WRITE(reg, temp);
  3060. POSTING_READ(reg);
  3061. udelay(500);
  3062. for (retry = 0; retry < 5; retry++) {
  3063. reg = FDI_RX_IIR(pipe);
  3064. temp = I915_READ(reg);
  3065. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3066. if (temp & FDI_RX_SYMBOL_LOCK) {
  3067. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3068. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3069. break;
  3070. }
  3071. udelay(50);
  3072. }
  3073. if (retry < 5)
  3074. break;
  3075. }
  3076. if (i == 4)
  3077. DRM_ERROR("FDI train 2 fail!\n");
  3078. DRM_DEBUG_KMS("FDI train done.\n");
  3079. }
  3080. /* Manual link training for Ivy Bridge A0 parts */
  3081. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3082. {
  3083. struct drm_device *dev = crtc->dev;
  3084. struct drm_i915_private *dev_priv = dev->dev_private;
  3085. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3086. int pipe = intel_crtc->pipe;
  3087. u32 reg, temp, i, j;
  3088. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3089. for train result */
  3090. reg = FDI_RX_IMR(pipe);
  3091. temp = I915_READ(reg);
  3092. temp &= ~FDI_RX_SYMBOL_LOCK;
  3093. temp &= ~FDI_RX_BIT_LOCK;
  3094. I915_WRITE(reg, temp);
  3095. POSTING_READ(reg);
  3096. udelay(150);
  3097. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3098. I915_READ(FDI_RX_IIR(pipe)));
  3099. /* Try each vswing and preemphasis setting twice before moving on */
  3100. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3101. /* disable first in case we need to retry */
  3102. reg = FDI_TX_CTL(pipe);
  3103. temp = I915_READ(reg);
  3104. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3105. temp &= ~FDI_TX_ENABLE;
  3106. I915_WRITE(reg, temp);
  3107. reg = FDI_RX_CTL(pipe);
  3108. temp = I915_READ(reg);
  3109. temp &= ~FDI_LINK_TRAIN_AUTO;
  3110. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3111. temp &= ~FDI_RX_ENABLE;
  3112. I915_WRITE(reg, temp);
  3113. /* enable CPU FDI TX and PCH FDI RX */
  3114. reg = FDI_TX_CTL(pipe);
  3115. temp = I915_READ(reg);
  3116. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3117. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3118. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3119. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3120. temp |= snb_b_fdi_train_param[j/2];
  3121. temp |= FDI_COMPOSITE_SYNC;
  3122. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3123. I915_WRITE(FDI_RX_MISC(pipe),
  3124. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3125. reg = FDI_RX_CTL(pipe);
  3126. temp = I915_READ(reg);
  3127. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3128. temp |= FDI_COMPOSITE_SYNC;
  3129. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3130. POSTING_READ(reg);
  3131. udelay(1); /* should be 0.5us */
  3132. for (i = 0; i < 4; i++) {
  3133. reg = FDI_RX_IIR(pipe);
  3134. temp = I915_READ(reg);
  3135. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3136. if (temp & FDI_RX_BIT_LOCK ||
  3137. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3138. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3139. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3140. i);
  3141. break;
  3142. }
  3143. udelay(1); /* should be 0.5us */
  3144. }
  3145. if (i == 4) {
  3146. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3147. continue;
  3148. }
  3149. /* Train 2 */
  3150. reg = FDI_TX_CTL(pipe);
  3151. temp = I915_READ(reg);
  3152. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3153. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3154. I915_WRITE(reg, temp);
  3155. reg = FDI_RX_CTL(pipe);
  3156. temp = I915_READ(reg);
  3157. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3158. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3159. I915_WRITE(reg, temp);
  3160. POSTING_READ(reg);
  3161. udelay(2); /* should be 1.5us */
  3162. for (i = 0; i < 4; i++) {
  3163. reg = FDI_RX_IIR(pipe);
  3164. temp = I915_READ(reg);
  3165. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3166. if (temp & FDI_RX_SYMBOL_LOCK ||
  3167. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3168. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3169. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3170. i);
  3171. goto train_done;
  3172. }
  3173. udelay(2); /* should be 1.5us */
  3174. }
  3175. if (i == 4)
  3176. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3177. }
  3178. train_done:
  3179. DRM_DEBUG_KMS("FDI train done.\n");
  3180. }
  3181. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3182. {
  3183. struct drm_device *dev = intel_crtc->base.dev;
  3184. struct drm_i915_private *dev_priv = dev->dev_private;
  3185. int pipe = intel_crtc->pipe;
  3186. u32 reg, temp;
  3187. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3188. reg = FDI_RX_CTL(pipe);
  3189. temp = I915_READ(reg);
  3190. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3191. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3192. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3193. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3194. POSTING_READ(reg);
  3195. udelay(200);
  3196. /* Switch from Rawclk to PCDclk */
  3197. temp = I915_READ(reg);
  3198. I915_WRITE(reg, temp | FDI_PCDCLK);
  3199. POSTING_READ(reg);
  3200. udelay(200);
  3201. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3202. reg = FDI_TX_CTL(pipe);
  3203. temp = I915_READ(reg);
  3204. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3205. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3206. POSTING_READ(reg);
  3207. udelay(100);
  3208. }
  3209. }
  3210. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3211. {
  3212. struct drm_device *dev = intel_crtc->base.dev;
  3213. struct drm_i915_private *dev_priv = dev->dev_private;
  3214. int pipe = intel_crtc->pipe;
  3215. u32 reg, temp;
  3216. /* Switch from PCDclk to Rawclk */
  3217. reg = FDI_RX_CTL(pipe);
  3218. temp = I915_READ(reg);
  3219. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3220. /* Disable CPU FDI TX PLL */
  3221. reg = FDI_TX_CTL(pipe);
  3222. temp = I915_READ(reg);
  3223. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3224. POSTING_READ(reg);
  3225. udelay(100);
  3226. reg = FDI_RX_CTL(pipe);
  3227. temp = I915_READ(reg);
  3228. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3229. /* Wait for the clocks to turn off. */
  3230. POSTING_READ(reg);
  3231. udelay(100);
  3232. }
  3233. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3234. {
  3235. struct drm_device *dev = crtc->dev;
  3236. struct drm_i915_private *dev_priv = dev->dev_private;
  3237. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3238. int pipe = intel_crtc->pipe;
  3239. u32 reg, temp;
  3240. /* disable CPU FDI tx and PCH FDI rx */
  3241. reg = FDI_TX_CTL(pipe);
  3242. temp = I915_READ(reg);
  3243. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3244. POSTING_READ(reg);
  3245. reg = FDI_RX_CTL(pipe);
  3246. temp = I915_READ(reg);
  3247. temp &= ~(0x7 << 16);
  3248. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3249. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3250. POSTING_READ(reg);
  3251. udelay(100);
  3252. /* Ironlake workaround, disable clock pointer after downing FDI */
  3253. if (HAS_PCH_IBX(dev))
  3254. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3255. /* still set train pattern 1 */
  3256. reg = FDI_TX_CTL(pipe);
  3257. temp = I915_READ(reg);
  3258. temp &= ~FDI_LINK_TRAIN_NONE;
  3259. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3260. I915_WRITE(reg, temp);
  3261. reg = FDI_RX_CTL(pipe);
  3262. temp = I915_READ(reg);
  3263. if (HAS_PCH_CPT(dev)) {
  3264. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3265. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3266. } else {
  3267. temp &= ~FDI_LINK_TRAIN_NONE;
  3268. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3269. }
  3270. /* BPC in FDI rx is consistent with that in PIPECONF */
  3271. temp &= ~(0x07 << 16);
  3272. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3273. I915_WRITE(reg, temp);
  3274. POSTING_READ(reg);
  3275. udelay(100);
  3276. }
  3277. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3278. {
  3279. struct intel_crtc *crtc;
  3280. /* Note that we don't need to be called with mode_config.lock here
  3281. * as our list of CRTC objects is static for the lifetime of the
  3282. * device and so cannot disappear as we iterate. Similarly, we can
  3283. * happily treat the predicates as racy, atomic checks as userspace
  3284. * cannot claim and pin a new fb without at least acquring the
  3285. * struct_mutex and so serialising with us.
  3286. */
  3287. for_each_intel_crtc(dev, crtc) {
  3288. if (atomic_read(&crtc->unpin_work_count) == 0)
  3289. continue;
  3290. if (crtc->unpin_work)
  3291. intel_wait_for_vblank(dev, crtc->pipe);
  3292. return true;
  3293. }
  3294. return false;
  3295. }
  3296. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3297. {
  3298. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3299. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3300. /* ensure that the unpin work is consistent wrt ->pending. */
  3301. smp_rmb();
  3302. intel_crtc->unpin_work = NULL;
  3303. if (work->event)
  3304. drm_send_vblank_event(intel_crtc->base.dev,
  3305. intel_crtc->pipe,
  3306. work->event);
  3307. drm_crtc_vblank_put(&intel_crtc->base);
  3308. wake_up_all(&dev_priv->pending_flip_queue);
  3309. queue_work(dev_priv->wq, &work->work);
  3310. trace_i915_flip_complete(intel_crtc->plane,
  3311. work->pending_flip_obj);
  3312. }
  3313. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3314. {
  3315. struct drm_device *dev = crtc->dev;
  3316. struct drm_i915_private *dev_priv = dev->dev_private;
  3317. long ret;
  3318. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3319. ret = wait_event_interruptible_timeout(
  3320. dev_priv->pending_flip_queue,
  3321. !intel_crtc_has_pending_flip(crtc),
  3322. 60*HZ);
  3323. if (ret < 0)
  3324. return ret;
  3325. if (ret == 0) {
  3326. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3327. spin_lock_irq(&dev->event_lock);
  3328. if (intel_crtc->unpin_work) {
  3329. WARN_ONCE(1, "Removing stuck page flip\n");
  3330. page_flip_completed(intel_crtc);
  3331. }
  3332. spin_unlock_irq(&dev->event_lock);
  3333. }
  3334. return 0;
  3335. }
  3336. /* Program iCLKIP clock to the desired frequency */
  3337. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3338. {
  3339. struct drm_device *dev = crtc->dev;
  3340. struct drm_i915_private *dev_priv = dev->dev_private;
  3341. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3342. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3343. u32 temp;
  3344. mutex_lock(&dev_priv->sb_lock);
  3345. /* It is necessary to ungate the pixclk gate prior to programming
  3346. * the divisors, and gate it back when it is done.
  3347. */
  3348. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3349. /* Disable SSCCTL */
  3350. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3351. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3352. SBI_SSCCTL_DISABLE,
  3353. SBI_ICLK);
  3354. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3355. if (clock == 20000) {
  3356. auxdiv = 1;
  3357. divsel = 0x41;
  3358. phaseinc = 0x20;
  3359. } else {
  3360. /* The iCLK virtual clock root frequency is in MHz,
  3361. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3362. * divisors, it is necessary to divide one by another, so we
  3363. * convert the virtual clock precision to KHz here for higher
  3364. * precision.
  3365. */
  3366. u32 iclk_virtual_root_freq = 172800 * 1000;
  3367. u32 iclk_pi_range = 64;
  3368. u32 desired_divisor, msb_divisor_value, pi_value;
  3369. desired_divisor = (iclk_virtual_root_freq / clock);
  3370. msb_divisor_value = desired_divisor / iclk_pi_range;
  3371. pi_value = desired_divisor % iclk_pi_range;
  3372. auxdiv = 0;
  3373. divsel = msb_divisor_value - 2;
  3374. phaseinc = pi_value;
  3375. }
  3376. /* This should not happen with any sane values */
  3377. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3378. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3379. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3380. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3381. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3382. clock,
  3383. auxdiv,
  3384. divsel,
  3385. phasedir,
  3386. phaseinc);
  3387. /* Program SSCDIVINTPHASE6 */
  3388. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3389. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3390. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3391. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3392. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3393. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3394. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3395. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3396. /* Program SSCAUXDIV */
  3397. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3398. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3399. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3400. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3401. /* Enable modulator and associated divider */
  3402. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3403. temp &= ~SBI_SSCCTL_DISABLE;
  3404. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3405. /* Wait for initialization time */
  3406. udelay(24);
  3407. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3408. mutex_unlock(&dev_priv->sb_lock);
  3409. }
  3410. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3411. enum pipe pch_transcoder)
  3412. {
  3413. struct drm_device *dev = crtc->base.dev;
  3414. struct drm_i915_private *dev_priv = dev->dev_private;
  3415. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3416. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3417. I915_READ(HTOTAL(cpu_transcoder)));
  3418. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3419. I915_READ(HBLANK(cpu_transcoder)));
  3420. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3421. I915_READ(HSYNC(cpu_transcoder)));
  3422. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3423. I915_READ(VTOTAL(cpu_transcoder)));
  3424. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3425. I915_READ(VBLANK(cpu_transcoder)));
  3426. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3427. I915_READ(VSYNC(cpu_transcoder)));
  3428. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3429. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3430. }
  3431. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3432. {
  3433. struct drm_i915_private *dev_priv = dev->dev_private;
  3434. uint32_t temp;
  3435. temp = I915_READ(SOUTH_CHICKEN1);
  3436. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3437. return;
  3438. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3439. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3440. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3441. if (enable)
  3442. temp |= FDI_BC_BIFURCATION_SELECT;
  3443. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3444. I915_WRITE(SOUTH_CHICKEN1, temp);
  3445. POSTING_READ(SOUTH_CHICKEN1);
  3446. }
  3447. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3448. {
  3449. struct drm_device *dev = intel_crtc->base.dev;
  3450. switch (intel_crtc->pipe) {
  3451. case PIPE_A:
  3452. break;
  3453. case PIPE_B:
  3454. if (intel_crtc->config->fdi_lanes > 2)
  3455. cpt_set_fdi_bc_bifurcation(dev, false);
  3456. else
  3457. cpt_set_fdi_bc_bifurcation(dev, true);
  3458. break;
  3459. case PIPE_C:
  3460. cpt_set_fdi_bc_bifurcation(dev, true);
  3461. break;
  3462. default:
  3463. BUG();
  3464. }
  3465. }
  3466. /*
  3467. * Enable PCH resources required for PCH ports:
  3468. * - PCH PLLs
  3469. * - FDI training & RX/TX
  3470. * - update transcoder timings
  3471. * - DP transcoding bits
  3472. * - transcoder
  3473. */
  3474. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3475. {
  3476. struct drm_device *dev = crtc->dev;
  3477. struct drm_i915_private *dev_priv = dev->dev_private;
  3478. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3479. int pipe = intel_crtc->pipe;
  3480. u32 reg, temp;
  3481. assert_pch_transcoder_disabled(dev_priv, pipe);
  3482. if (IS_IVYBRIDGE(dev))
  3483. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3484. /* Write the TU size bits before fdi link training, so that error
  3485. * detection works. */
  3486. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3487. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3488. /* For PCH output, training FDI link */
  3489. dev_priv->display.fdi_link_train(crtc);
  3490. /* We need to program the right clock selection before writing the pixel
  3491. * mutliplier into the DPLL. */
  3492. if (HAS_PCH_CPT(dev)) {
  3493. u32 sel;
  3494. temp = I915_READ(PCH_DPLL_SEL);
  3495. temp |= TRANS_DPLL_ENABLE(pipe);
  3496. sel = TRANS_DPLLB_SEL(pipe);
  3497. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3498. temp |= sel;
  3499. else
  3500. temp &= ~sel;
  3501. I915_WRITE(PCH_DPLL_SEL, temp);
  3502. }
  3503. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3504. * transcoder, and we actually should do this to not upset any PCH
  3505. * transcoder that already use the clock when we share it.
  3506. *
  3507. * Note that enable_shared_dpll tries to do the right thing, but
  3508. * get_shared_dpll unconditionally resets the pll - we need that to have
  3509. * the right LVDS enable sequence. */
  3510. intel_enable_shared_dpll(intel_crtc);
  3511. /* set transcoder timing, panel must allow it */
  3512. assert_panel_unlocked(dev_priv, pipe);
  3513. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3514. intel_fdi_normal_train(crtc);
  3515. /* For PCH DP, enable TRANS_DP_CTL */
  3516. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3517. const struct drm_display_mode *adjusted_mode =
  3518. &intel_crtc->config->base.adjusted_mode;
  3519. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3520. reg = TRANS_DP_CTL(pipe);
  3521. temp = I915_READ(reg);
  3522. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3523. TRANS_DP_SYNC_MASK |
  3524. TRANS_DP_BPC_MASK);
  3525. temp |= TRANS_DP_OUTPUT_ENABLE;
  3526. temp |= bpc << 9; /* same format but at 11:9 */
  3527. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3528. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3529. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3530. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3531. switch (intel_trans_dp_port_sel(crtc)) {
  3532. case PCH_DP_B:
  3533. temp |= TRANS_DP_PORT_SEL_B;
  3534. break;
  3535. case PCH_DP_C:
  3536. temp |= TRANS_DP_PORT_SEL_C;
  3537. break;
  3538. case PCH_DP_D:
  3539. temp |= TRANS_DP_PORT_SEL_D;
  3540. break;
  3541. default:
  3542. BUG();
  3543. }
  3544. I915_WRITE(reg, temp);
  3545. }
  3546. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3547. }
  3548. static void lpt_pch_enable(struct drm_crtc *crtc)
  3549. {
  3550. struct drm_device *dev = crtc->dev;
  3551. struct drm_i915_private *dev_priv = dev->dev_private;
  3552. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3553. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3554. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3555. lpt_program_iclkip(crtc);
  3556. /* Set transcoder timing. */
  3557. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3558. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3559. }
  3560. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3561. struct intel_crtc_state *crtc_state)
  3562. {
  3563. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3564. struct intel_shared_dpll *pll;
  3565. struct intel_shared_dpll_config *shared_dpll;
  3566. enum intel_dpll_id i;
  3567. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3568. if (HAS_PCH_IBX(dev_priv->dev)) {
  3569. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3570. i = (enum intel_dpll_id) crtc->pipe;
  3571. pll = &dev_priv->shared_dplls[i];
  3572. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3573. crtc->base.base.id, pll->name);
  3574. WARN_ON(shared_dpll[i].crtc_mask);
  3575. goto found;
  3576. }
  3577. if (IS_BROXTON(dev_priv->dev)) {
  3578. /* PLL is attached to port in bxt */
  3579. struct intel_encoder *encoder;
  3580. struct intel_digital_port *intel_dig_port;
  3581. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3582. if (WARN_ON(!encoder))
  3583. return NULL;
  3584. intel_dig_port = enc_to_dig_port(&encoder->base);
  3585. /* 1:1 mapping between ports and PLLs */
  3586. i = (enum intel_dpll_id)intel_dig_port->port;
  3587. pll = &dev_priv->shared_dplls[i];
  3588. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3589. crtc->base.base.id, pll->name);
  3590. WARN_ON(shared_dpll[i].crtc_mask);
  3591. goto found;
  3592. }
  3593. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3594. pll = &dev_priv->shared_dplls[i];
  3595. /* Only want to check enabled timings first */
  3596. if (shared_dpll[i].crtc_mask == 0)
  3597. continue;
  3598. if (memcmp(&crtc_state->dpll_hw_state,
  3599. &shared_dpll[i].hw_state,
  3600. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3601. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3602. crtc->base.base.id, pll->name,
  3603. shared_dpll[i].crtc_mask,
  3604. pll->active);
  3605. goto found;
  3606. }
  3607. }
  3608. /* Ok no matching timings, maybe there's a free one? */
  3609. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3610. pll = &dev_priv->shared_dplls[i];
  3611. if (shared_dpll[i].crtc_mask == 0) {
  3612. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3613. crtc->base.base.id, pll->name);
  3614. goto found;
  3615. }
  3616. }
  3617. return NULL;
  3618. found:
  3619. if (shared_dpll[i].crtc_mask == 0)
  3620. shared_dpll[i].hw_state =
  3621. crtc_state->dpll_hw_state;
  3622. crtc_state->shared_dpll = i;
  3623. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3624. pipe_name(crtc->pipe));
  3625. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3626. return pll;
  3627. }
  3628. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3629. {
  3630. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3631. struct intel_shared_dpll_config *shared_dpll;
  3632. struct intel_shared_dpll *pll;
  3633. enum intel_dpll_id i;
  3634. if (!to_intel_atomic_state(state)->dpll_set)
  3635. return;
  3636. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3637. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3638. pll = &dev_priv->shared_dplls[i];
  3639. pll->config = shared_dpll[i];
  3640. }
  3641. }
  3642. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3643. {
  3644. struct drm_i915_private *dev_priv = dev->dev_private;
  3645. int dslreg = PIPEDSL(pipe);
  3646. u32 temp;
  3647. temp = I915_READ(dslreg);
  3648. udelay(500);
  3649. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3650. if (wait_for(I915_READ(dslreg) != temp, 5))
  3651. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3652. }
  3653. }
  3654. static int
  3655. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3656. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3657. int src_w, int src_h, int dst_w, int dst_h)
  3658. {
  3659. struct intel_crtc_scaler_state *scaler_state =
  3660. &crtc_state->scaler_state;
  3661. struct intel_crtc *intel_crtc =
  3662. to_intel_crtc(crtc_state->base.crtc);
  3663. int need_scaling;
  3664. need_scaling = intel_rotation_90_or_270(rotation) ?
  3665. (src_h != dst_w || src_w != dst_h):
  3666. (src_w != dst_w || src_h != dst_h);
  3667. /*
  3668. * if plane is being disabled or scaler is no more required or force detach
  3669. * - free scaler binded to this plane/crtc
  3670. * - in order to do this, update crtc->scaler_usage
  3671. *
  3672. * Here scaler state in crtc_state is set free so that
  3673. * scaler can be assigned to other user. Actual register
  3674. * update to free the scaler is done in plane/panel-fit programming.
  3675. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3676. */
  3677. if (force_detach || !need_scaling) {
  3678. if (*scaler_id >= 0) {
  3679. scaler_state->scaler_users &= ~(1 << scaler_user);
  3680. scaler_state->scalers[*scaler_id].in_use = 0;
  3681. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3682. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3683. intel_crtc->pipe, scaler_user, *scaler_id,
  3684. scaler_state->scaler_users);
  3685. *scaler_id = -1;
  3686. }
  3687. return 0;
  3688. }
  3689. /* range checks */
  3690. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3691. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3692. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3693. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3694. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3695. "size is out of scaler range\n",
  3696. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3697. return -EINVAL;
  3698. }
  3699. /* mark this plane as a scaler user in crtc_state */
  3700. scaler_state->scaler_users |= (1 << scaler_user);
  3701. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3702. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3703. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3704. scaler_state->scaler_users);
  3705. return 0;
  3706. }
  3707. /**
  3708. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3709. *
  3710. * @state: crtc's scaler state
  3711. *
  3712. * Return
  3713. * 0 - scaler_usage updated successfully
  3714. * error - requested scaling cannot be supported or other error condition
  3715. */
  3716. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3717. {
  3718. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3719. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3720. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3721. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3722. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3723. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3724. state->pipe_src_w, state->pipe_src_h,
  3725. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3726. }
  3727. /**
  3728. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3729. *
  3730. * @state: crtc's scaler state
  3731. * @plane_state: atomic plane state to update
  3732. *
  3733. * Return
  3734. * 0 - scaler_usage updated successfully
  3735. * error - requested scaling cannot be supported or other error condition
  3736. */
  3737. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3738. struct intel_plane_state *plane_state)
  3739. {
  3740. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3741. struct intel_plane *intel_plane =
  3742. to_intel_plane(plane_state->base.plane);
  3743. struct drm_framebuffer *fb = plane_state->base.fb;
  3744. int ret;
  3745. bool force_detach = !fb || !plane_state->visible;
  3746. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3747. intel_plane->base.base.id, intel_crtc->pipe,
  3748. drm_plane_index(&intel_plane->base));
  3749. ret = skl_update_scaler(crtc_state, force_detach,
  3750. drm_plane_index(&intel_plane->base),
  3751. &plane_state->scaler_id,
  3752. plane_state->base.rotation,
  3753. drm_rect_width(&plane_state->src) >> 16,
  3754. drm_rect_height(&plane_state->src) >> 16,
  3755. drm_rect_width(&plane_state->dst),
  3756. drm_rect_height(&plane_state->dst));
  3757. if (ret || plane_state->scaler_id < 0)
  3758. return ret;
  3759. /* check colorkey */
  3760. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3761. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3762. intel_plane->base.base.id);
  3763. return -EINVAL;
  3764. }
  3765. /* Check src format */
  3766. switch (fb->pixel_format) {
  3767. case DRM_FORMAT_RGB565:
  3768. case DRM_FORMAT_XBGR8888:
  3769. case DRM_FORMAT_XRGB8888:
  3770. case DRM_FORMAT_ABGR8888:
  3771. case DRM_FORMAT_ARGB8888:
  3772. case DRM_FORMAT_XRGB2101010:
  3773. case DRM_FORMAT_XBGR2101010:
  3774. case DRM_FORMAT_YUYV:
  3775. case DRM_FORMAT_YVYU:
  3776. case DRM_FORMAT_UYVY:
  3777. case DRM_FORMAT_VYUY:
  3778. break;
  3779. default:
  3780. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3781. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3782. return -EINVAL;
  3783. }
  3784. return 0;
  3785. }
  3786. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3787. {
  3788. int i;
  3789. for (i = 0; i < crtc->num_scalers; i++)
  3790. skl_detach_scaler(crtc, i);
  3791. }
  3792. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3793. {
  3794. struct drm_device *dev = crtc->base.dev;
  3795. struct drm_i915_private *dev_priv = dev->dev_private;
  3796. int pipe = crtc->pipe;
  3797. struct intel_crtc_scaler_state *scaler_state =
  3798. &crtc->config->scaler_state;
  3799. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3800. if (crtc->config->pch_pfit.enabled) {
  3801. int id;
  3802. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3803. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3804. return;
  3805. }
  3806. id = scaler_state->scaler_id;
  3807. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3808. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3809. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3810. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3811. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3812. }
  3813. }
  3814. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3815. {
  3816. struct drm_device *dev = crtc->base.dev;
  3817. struct drm_i915_private *dev_priv = dev->dev_private;
  3818. int pipe = crtc->pipe;
  3819. if (crtc->config->pch_pfit.enabled) {
  3820. /* Force use of hard-coded filter coefficients
  3821. * as some pre-programmed values are broken,
  3822. * e.g. x201.
  3823. */
  3824. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3825. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3826. PF_PIPE_SEL_IVB(pipe));
  3827. else
  3828. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3829. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3830. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3831. }
  3832. }
  3833. void hsw_enable_ips(struct intel_crtc *crtc)
  3834. {
  3835. struct drm_device *dev = crtc->base.dev;
  3836. struct drm_i915_private *dev_priv = dev->dev_private;
  3837. if (!crtc->config->ips_enabled)
  3838. return;
  3839. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3840. intel_wait_for_vblank(dev, crtc->pipe);
  3841. assert_plane_enabled(dev_priv, crtc->plane);
  3842. if (IS_BROADWELL(dev)) {
  3843. mutex_lock(&dev_priv->rps.hw_lock);
  3844. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3845. mutex_unlock(&dev_priv->rps.hw_lock);
  3846. /* Quoting Art Runyan: "its not safe to expect any particular
  3847. * value in IPS_CTL bit 31 after enabling IPS through the
  3848. * mailbox." Moreover, the mailbox may return a bogus state,
  3849. * so we need to just enable it and continue on.
  3850. */
  3851. } else {
  3852. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3853. /* The bit only becomes 1 in the next vblank, so this wait here
  3854. * is essentially intel_wait_for_vblank. If we don't have this
  3855. * and don't wait for vblanks until the end of crtc_enable, then
  3856. * the HW state readout code will complain that the expected
  3857. * IPS_CTL value is not the one we read. */
  3858. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3859. DRM_ERROR("Timed out waiting for IPS enable\n");
  3860. }
  3861. }
  3862. void hsw_disable_ips(struct intel_crtc *crtc)
  3863. {
  3864. struct drm_device *dev = crtc->base.dev;
  3865. struct drm_i915_private *dev_priv = dev->dev_private;
  3866. if (!crtc->config->ips_enabled)
  3867. return;
  3868. assert_plane_enabled(dev_priv, crtc->plane);
  3869. if (IS_BROADWELL(dev)) {
  3870. mutex_lock(&dev_priv->rps.hw_lock);
  3871. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3872. mutex_unlock(&dev_priv->rps.hw_lock);
  3873. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3874. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3875. DRM_ERROR("Timed out waiting for IPS disable\n");
  3876. } else {
  3877. I915_WRITE(IPS_CTL, 0);
  3878. POSTING_READ(IPS_CTL);
  3879. }
  3880. /* We need to wait for a vblank before we can disable the plane. */
  3881. intel_wait_for_vblank(dev, crtc->pipe);
  3882. }
  3883. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3884. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3885. {
  3886. struct drm_device *dev = crtc->dev;
  3887. struct drm_i915_private *dev_priv = dev->dev_private;
  3888. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3889. enum pipe pipe = intel_crtc->pipe;
  3890. int i;
  3891. bool reenable_ips = false;
  3892. /* The clocks have to be on to load the palette. */
  3893. if (!crtc->state->active)
  3894. return;
  3895. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3896. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3897. assert_dsi_pll_enabled(dev_priv);
  3898. else
  3899. assert_pll_enabled(dev_priv, pipe);
  3900. }
  3901. /* Workaround : Do not read or write the pipe palette/gamma data while
  3902. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3903. */
  3904. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3905. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3906. GAMMA_MODE_MODE_SPLIT)) {
  3907. hsw_disable_ips(intel_crtc);
  3908. reenable_ips = true;
  3909. }
  3910. for (i = 0; i < 256; i++) {
  3911. u32 palreg;
  3912. if (HAS_GMCH_DISPLAY(dev))
  3913. palreg = PALETTE(pipe, i);
  3914. else
  3915. palreg = LGC_PALETTE(pipe, i);
  3916. I915_WRITE(palreg,
  3917. (intel_crtc->lut_r[i] << 16) |
  3918. (intel_crtc->lut_g[i] << 8) |
  3919. intel_crtc->lut_b[i]);
  3920. }
  3921. if (reenable_ips)
  3922. hsw_enable_ips(intel_crtc);
  3923. }
  3924. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3925. {
  3926. if (intel_crtc->overlay) {
  3927. struct drm_device *dev = intel_crtc->base.dev;
  3928. struct drm_i915_private *dev_priv = dev->dev_private;
  3929. mutex_lock(&dev->struct_mutex);
  3930. dev_priv->mm.interruptible = false;
  3931. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3932. dev_priv->mm.interruptible = true;
  3933. mutex_unlock(&dev->struct_mutex);
  3934. }
  3935. /* Let userspace switch the overlay on again. In most cases userspace
  3936. * has to recompute where to put it anyway.
  3937. */
  3938. }
  3939. /**
  3940. * intel_post_enable_primary - Perform operations after enabling primary plane
  3941. * @crtc: the CRTC whose primary plane was just enabled
  3942. *
  3943. * Performs potentially sleeping operations that must be done after the primary
  3944. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3945. * called due to an explicit primary plane update, or due to an implicit
  3946. * re-enable that is caused when a sprite plane is updated to no longer
  3947. * completely hide the primary plane.
  3948. */
  3949. static void
  3950. intel_post_enable_primary(struct drm_crtc *crtc)
  3951. {
  3952. struct drm_device *dev = crtc->dev;
  3953. struct drm_i915_private *dev_priv = dev->dev_private;
  3954. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3955. int pipe = intel_crtc->pipe;
  3956. /*
  3957. * BDW signals flip done immediately if the plane
  3958. * is disabled, even if the plane enable is already
  3959. * armed to occur at the next vblank :(
  3960. */
  3961. if (IS_BROADWELL(dev))
  3962. intel_wait_for_vblank(dev, pipe);
  3963. /*
  3964. * FIXME IPS should be fine as long as one plane is
  3965. * enabled, but in practice it seems to have problems
  3966. * when going from primary only to sprite only and vice
  3967. * versa.
  3968. */
  3969. hsw_enable_ips(intel_crtc);
  3970. /*
  3971. * Gen2 reports pipe underruns whenever all planes are disabled.
  3972. * So don't enable underrun reporting before at least some planes
  3973. * are enabled.
  3974. * FIXME: Need to fix the logic to work when we turn off all planes
  3975. * but leave the pipe running.
  3976. */
  3977. if (IS_GEN2(dev))
  3978. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3979. /* Underruns don't always raise interrupts, so check manually. */
  3980. intel_check_cpu_fifo_underruns(dev_priv);
  3981. intel_check_pch_fifo_underruns(dev_priv);
  3982. }
  3983. /**
  3984. * intel_pre_disable_primary - Perform operations before disabling primary plane
  3985. * @crtc: the CRTC whose primary plane is to be disabled
  3986. *
  3987. * Performs potentially sleeping operations that must be done before the
  3988. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  3989. * be called due to an explicit primary plane update, or due to an implicit
  3990. * disable that is caused when a sprite plane completely hides the primary
  3991. * plane.
  3992. */
  3993. static void
  3994. intel_pre_disable_primary(struct drm_crtc *crtc)
  3995. {
  3996. struct drm_device *dev = crtc->dev;
  3997. struct drm_i915_private *dev_priv = dev->dev_private;
  3998. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3999. int pipe = intel_crtc->pipe;
  4000. /*
  4001. * Gen2 reports pipe underruns whenever all planes are disabled.
  4002. * So diasble underrun reporting before all the planes get disabled.
  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, false);
  4008. /*
  4009. * Vblank time updates from the shadow to live plane control register
  4010. * are blocked if the memory self-refresh mode is active at that
  4011. * moment. So to make sure the plane gets truly disabled, disable
  4012. * first the self-refresh mode. The self-refresh enable bit in turn
  4013. * will be checked/applied by the HW only at the next frame start
  4014. * event which is after the vblank start event, so we need to have a
  4015. * wait-for-vblank between disabling the plane and the pipe.
  4016. */
  4017. if (HAS_GMCH_DISPLAY(dev)) {
  4018. intel_set_memory_cxsr(dev_priv, false);
  4019. dev_priv->wm.vlv.cxsr = false;
  4020. intel_wait_for_vblank(dev, pipe);
  4021. }
  4022. /*
  4023. * FIXME IPS should be fine as long as one plane is
  4024. * enabled, but in practice it seems to have problems
  4025. * when going from primary only to sprite only and vice
  4026. * versa.
  4027. */
  4028. hsw_disable_ips(intel_crtc);
  4029. }
  4030. static void intel_post_plane_update(struct intel_crtc *crtc)
  4031. {
  4032. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4033. struct drm_device *dev = crtc->base.dev;
  4034. struct drm_i915_private *dev_priv = dev->dev_private;
  4035. if (atomic->wait_vblank)
  4036. intel_wait_for_vblank(dev, crtc->pipe);
  4037. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4038. if (atomic->disable_cxsr)
  4039. crtc->wm.cxsr_allowed = true;
  4040. if (crtc->atomic.update_wm_post)
  4041. intel_update_watermarks(&crtc->base);
  4042. if (atomic->update_fbc)
  4043. intel_fbc_update(dev_priv);
  4044. if (atomic->post_enable_primary)
  4045. intel_post_enable_primary(&crtc->base);
  4046. memset(atomic, 0, sizeof(*atomic));
  4047. }
  4048. static void intel_pre_plane_update(struct intel_crtc *crtc)
  4049. {
  4050. struct drm_device *dev = crtc->base.dev;
  4051. struct drm_i915_private *dev_priv = dev->dev_private;
  4052. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4053. if (atomic->disable_fbc)
  4054. intel_fbc_disable_crtc(crtc);
  4055. if (crtc->atomic.disable_ips)
  4056. hsw_disable_ips(crtc);
  4057. if (atomic->pre_disable_primary)
  4058. intel_pre_disable_primary(&crtc->base);
  4059. if (atomic->disable_cxsr) {
  4060. crtc->wm.cxsr_allowed = false;
  4061. intel_set_memory_cxsr(dev_priv, false);
  4062. }
  4063. }
  4064. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4065. {
  4066. struct drm_device *dev = crtc->dev;
  4067. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4068. struct drm_plane *p;
  4069. int pipe = intel_crtc->pipe;
  4070. intel_crtc_dpms_overlay_disable(intel_crtc);
  4071. drm_for_each_plane_mask(p, dev, plane_mask)
  4072. to_intel_plane(p)->disable_plane(p, crtc);
  4073. /*
  4074. * FIXME: Once we grow proper nuclear flip support out of this we need
  4075. * to compute the mask of flip planes precisely. For the time being
  4076. * consider this a flip to a NULL plane.
  4077. */
  4078. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4079. }
  4080. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4081. {
  4082. struct drm_device *dev = crtc->dev;
  4083. struct drm_i915_private *dev_priv = dev->dev_private;
  4084. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4085. struct intel_encoder *encoder;
  4086. int pipe = intel_crtc->pipe;
  4087. if (WARN_ON(intel_crtc->active))
  4088. return;
  4089. if (intel_crtc->config->has_pch_encoder)
  4090. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4091. if (intel_crtc->config->has_pch_encoder)
  4092. intel_prepare_shared_dpll(intel_crtc);
  4093. if (intel_crtc->config->has_dp_encoder)
  4094. intel_dp_set_m_n(intel_crtc, M1_N1);
  4095. intel_set_pipe_timings(intel_crtc);
  4096. if (intel_crtc->config->has_pch_encoder) {
  4097. intel_cpu_transcoder_set_m_n(intel_crtc,
  4098. &intel_crtc->config->fdi_m_n, NULL);
  4099. }
  4100. ironlake_set_pipeconf(crtc);
  4101. intel_crtc->active = true;
  4102. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4103. for_each_encoder_on_crtc(dev, crtc, encoder)
  4104. if (encoder->pre_enable)
  4105. encoder->pre_enable(encoder);
  4106. if (intel_crtc->config->has_pch_encoder) {
  4107. /* Note: FDI PLL enabling _must_ be done before we enable the
  4108. * cpu pipes, hence this is separate from all the other fdi/pch
  4109. * enabling. */
  4110. ironlake_fdi_pll_enable(intel_crtc);
  4111. } else {
  4112. assert_fdi_tx_disabled(dev_priv, pipe);
  4113. assert_fdi_rx_disabled(dev_priv, pipe);
  4114. }
  4115. ironlake_pfit_enable(intel_crtc);
  4116. /*
  4117. * On ILK+ LUT must be loaded before the pipe is running but with
  4118. * clocks enabled
  4119. */
  4120. intel_crtc_load_lut(crtc);
  4121. intel_update_watermarks(crtc);
  4122. intel_enable_pipe(intel_crtc);
  4123. if (intel_crtc->config->has_pch_encoder)
  4124. ironlake_pch_enable(crtc);
  4125. assert_vblank_disabled(crtc);
  4126. drm_crtc_vblank_on(crtc);
  4127. for_each_encoder_on_crtc(dev, crtc, encoder)
  4128. encoder->enable(encoder);
  4129. if (HAS_PCH_CPT(dev))
  4130. cpt_verify_modeset(dev, intel_crtc->pipe);
  4131. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4132. if (intel_crtc->config->has_pch_encoder)
  4133. intel_wait_for_vblank(dev, pipe);
  4134. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4135. }
  4136. /* IPS only exists on ULT machines and is tied to pipe A. */
  4137. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4138. {
  4139. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4140. }
  4141. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4142. {
  4143. struct drm_device *dev = crtc->dev;
  4144. struct drm_i915_private *dev_priv = dev->dev_private;
  4145. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4146. struct intel_encoder *encoder;
  4147. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4148. struct intel_crtc_state *pipe_config =
  4149. to_intel_crtc_state(crtc->state);
  4150. bool is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4151. if (WARN_ON(intel_crtc->active))
  4152. return;
  4153. if (intel_crtc->config->has_pch_encoder)
  4154. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4155. false);
  4156. if (intel_crtc_to_shared_dpll(intel_crtc))
  4157. intel_enable_shared_dpll(intel_crtc);
  4158. if (intel_crtc->config->has_dp_encoder)
  4159. intel_dp_set_m_n(intel_crtc, M1_N1);
  4160. intel_set_pipe_timings(intel_crtc);
  4161. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4162. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4163. intel_crtc->config->pixel_multiplier - 1);
  4164. }
  4165. if (intel_crtc->config->has_pch_encoder) {
  4166. intel_cpu_transcoder_set_m_n(intel_crtc,
  4167. &intel_crtc->config->fdi_m_n, NULL);
  4168. }
  4169. haswell_set_pipeconf(crtc);
  4170. intel_set_pipe_csc(crtc);
  4171. intel_crtc->active = true;
  4172. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4173. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4174. if (encoder->pre_pll_enable)
  4175. encoder->pre_pll_enable(encoder);
  4176. if (encoder->pre_enable)
  4177. encoder->pre_enable(encoder);
  4178. }
  4179. if (intel_crtc->config->has_pch_encoder)
  4180. dev_priv->display.fdi_link_train(crtc);
  4181. if (!is_dsi)
  4182. intel_ddi_enable_pipe_clock(intel_crtc);
  4183. if (INTEL_INFO(dev)->gen >= 9)
  4184. skylake_pfit_enable(intel_crtc);
  4185. else
  4186. ironlake_pfit_enable(intel_crtc);
  4187. /*
  4188. * On ILK+ LUT must be loaded before the pipe is running but with
  4189. * clocks enabled
  4190. */
  4191. intel_crtc_load_lut(crtc);
  4192. intel_ddi_set_pipe_settings(crtc);
  4193. if (!is_dsi)
  4194. intel_ddi_enable_transcoder_func(crtc);
  4195. intel_update_watermarks(crtc);
  4196. intel_enable_pipe(intel_crtc);
  4197. if (intel_crtc->config->has_pch_encoder)
  4198. lpt_pch_enable(crtc);
  4199. if (intel_crtc->config->dp_encoder_is_mst && !is_dsi)
  4200. intel_ddi_set_vc_payload_alloc(crtc, true);
  4201. assert_vblank_disabled(crtc);
  4202. drm_crtc_vblank_on(crtc);
  4203. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4204. encoder->enable(encoder);
  4205. intel_opregion_notify_encoder(encoder, true);
  4206. }
  4207. if (intel_crtc->config->has_pch_encoder)
  4208. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4209. true);
  4210. /* If we change the relative order between pipe/planes enabling, we need
  4211. * to change the workaround. */
  4212. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4213. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4214. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4215. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4216. }
  4217. }
  4218. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4219. {
  4220. struct drm_device *dev = crtc->base.dev;
  4221. struct drm_i915_private *dev_priv = dev->dev_private;
  4222. int pipe = crtc->pipe;
  4223. /* To avoid upsetting the power well on haswell only disable the pfit if
  4224. * it's in use. The hw state code will make sure we get this right. */
  4225. if (force || crtc->config->pch_pfit.enabled) {
  4226. I915_WRITE(PF_CTL(pipe), 0);
  4227. I915_WRITE(PF_WIN_POS(pipe), 0);
  4228. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4229. }
  4230. }
  4231. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4232. {
  4233. struct drm_device *dev = crtc->dev;
  4234. struct drm_i915_private *dev_priv = dev->dev_private;
  4235. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4236. struct intel_encoder *encoder;
  4237. int pipe = intel_crtc->pipe;
  4238. u32 reg, temp;
  4239. if (intel_crtc->config->has_pch_encoder)
  4240. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4241. for_each_encoder_on_crtc(dev, crtc, encoder)
  4242. encoder->disable(encoder);
  4243. drm_crtc_vblank_off(crtc);
  4244. assert_vblank_disabled(crtc);
  4245. intel_disable_pipe(intel_crtc);
  4246. ironlake_pfit_disable(intel_crtc, false);
  4247. if (intel_crtc->config->has_pch_encoder)
  4248. ironlake_fdi_disable(crtc);
  4249. for_each_encoder_on_crtc(dev, crtc, encoder)
  4250. if (encoder->post_disable)
  4251. encoder->post_disable(encoder);
  4252. if (intel_crtc->config->has_pch_encoder) {
  4253. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4254. if (HAS_PCH_CPT(dev)) {
  4255. /* disable TRANS_DP_CTL */
  4256. reg = TRANS_DP_CTL(pipe);
  4257. temp = I915_READ(reg);
  4258. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4259. TRANS_DP_PORT_SEL_MASK);
  4260. temp |= TRANS_DP_PORT_SEL_NONE;
  4261. I915_WRITE(reg, temp);
  4262. /* disable DPLL_SEL */
  4263. temp = I915_READ(PCH_DPLL_SEL);
  4264. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4265. I915_WRITE(PCH_DPLL_SEL, temp);
  4266. }
  4267. ironlake_fdi_pll_disable(intel_crtc);
  4268. }
  4269. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4270. }
  4271. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4272. {
  4273. struct drm_device *dev = crtc->dev;
  4274. struct drm_i915_private *dev_priv = dev->dev_private;
  4275. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4276. struct intel_encoder *encoder;
  4277. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4278. bool is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4279. if (intel_crtc->config->has_pch_encoder)
  4280. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4281. false);
  4282. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4283. intel_opregion_notify_encoder(encoder, false);
  4284. encoder->disable(encoder);
  4285. }
  4286. drm_crtc_vblank_off(crtc);
  4287. assert_vblank_disabled(crtc);
  4288. intel_disable_pipe(intel_crtc);
  4289. if (intel_crtc->config->dp_encoder_is_mst)
  4290. intel_ddi_set_vc_payload_alloc(crtc, false);
  4291. if (!is_dsi)
  4292. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4293. if (INTEL_INFO(dev)->gen >= 9)
  4294. skylake_scaler_disable(intel_crtc);
  4295. else
  4296. ironlake_pfit_disable(intel_crtc, false);
  4297. if (!is_dsi)
  4298. intel_ddi_disable_pipe_clock(intel_crtc);
  4299. if (intel_crtc->config->has_pch_encoder) {
  4300. lpt_disable_pch_transcoder(dev_priv);
  4301. intel_ddi_fdi_disable(crtc);
  4302. }
  4303. for_each_encoder_on_crtc(dev, crtc, encoder)
  4304. if (encoder->post_disable)
  4305. encoder->post_disable(encoder);
  4306. if (intel_crtc->config->has_pch_encoder)
  4307. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4308. true);
  4309. }
  4310. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4311. {
  4312. struct drm_device *dev = crtc->base.dev;
  4313. struct drm_i915_private *dev_priv = dev->dev_private;
  4314. struct intel_crtc_state *pipe_config = crtc->config;
  4315. if (!pipe_config->gmch_pfit.control)
  4316. return;
  4317. /*
  4318. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4319. * according to register description and PRM.
  4320. */
  4321. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4322. assert_pipe_disabled(dev_priv, crtc->pipe);
  4323. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4324. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4325. /* Border color in case we don't scale up to the full screen. Black by
  4326. * default, change to something else for debugging. */
  4327. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4328. }
  4329. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4330. {
  4331. switch (port) {
  4332. case PORT_A:
  4333. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4334. case PORT_B:
  4335. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4336. case PORT_C:
  4337. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4338. case PORT_D:
  4339. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4340. case PORT_E:
  4341. return POWER_DOMAIN_PORT_DDI_E_2_LANES;
  4342. default:
  4343. WARN_ON_ONCE(1);
  4344. return POWER_DOMAIN_PORT_OTHER;
  4345. }
  4346. }
  4347. #define for_each_power_domain(domain, mask) \
  4348. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4349. if ((1 << (domain)) & (mask))
  4350. enum intel_display_power_domain
  4351. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4352. {
  4353. struct drm_device *dev = intel_encoder->base.dev;
  4354. struct intel_digital_port *intel_dig_port;
  4355. switch (intel_encoder->type) {
  4356. case INTEL_OUTPUT_UNKNOWN:
  4357. /* Only DDI platforms should ever use this output type */
  4358. WARN_ON_ONCE(!HAS_DDI(dev));
  4359. case INTEL_OUTPUT_DISPLAYPORT:
  4360. case INTEL_OUTPUT_HDMI:
  4361. case INTEL_OUTPUT_EDP:
  4362. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4363. return port_to_power_domain(intel_dig_port->port);
  4364. case INTEL_OUTPUT_DP_MST:
  4365. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4366. return port_to_power_domain(intel_dig_port->port);
  4367. case INTEL_OUTPUT_ANALOG:
  4368. return POWER_DOMAIN_PORT_CRT;
  4369. case INTEL_OUTPUT_DSI:
  4370. return POWER_DOMAIN_PORT_DSI;
  4371. default:
  4372. return POWER_DOMAIN_PORT_OTHER;
  4373. }
  4374. }
  4375. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4376. {
  4377. struct drm_device *dev = crtc->dev;
  4378. struct intel_encoder *intel_encoder;
  4379. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4380. enum pipe pipe = intel_crtc->pipe;
  4381. unsigned long mask;
  4382. enum transcoder transcoder = intel_crtc->config->cpu_transcoder;
  4383. if (!crtc->state->active)
  4384. return 0;
  4385. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4386. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4387. if (intel_crtc->config->pch_pfit.enabled ||
  4388. intel_crtc->config->pch_pfit.force_thru)
  4389. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4390. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4391. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4392. return mask;
  4393. }
  4394. static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
  4395. {
  4396. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4397. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4398. enum intel_display_power_domain domain;
  4399. unsigned long domains, new_domains, old_domains;
  4400. old_domains = intel_crtc->enabled_power_domains;
  4401. intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
  4402. domains = new_domains & ~old_domains;
  4403. for_each_power_domain(domain, domains)
  4404. intel_display_power_get(dev_priv, domain);
  4405. return old_domains & ~new_domains;
  4406. }
  4407. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4408. unsigned long domains)
  4409. {
  4410. enum intel_display_power_domain domain;
  4411. for_each_power_domain(domain, domains)
  4412. intel_display_power_put(dev_priv, domain);
  4413. }
  4414. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4415. {
  4416. struct drm_device *dev = state->dev;
  4417. struct drm_i915_private *dev_priv = dev->dev_private;
  4418. unsigned long put_domains[I915_MAX_PIPES] = {};
  4419. struct drm_crtc_state *crtc_state;
  4420. struct drm_crtc *crtc;
  4421. int i;
  4422. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  4423. if (needs_modeset(crtc->state))
  4424. put_domains[to_intel_crtc(crtc)->pipe] =
  4425. modeset_get_crtc_power_domains(crtc);
  4426. }
  4427. if (dev_priv->display.modeset_commit_cdclk) {
  4428. unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
  4429. if (cdclk != dev_priv->cdclk_freq &&
  4430. !WARN_ON(!state->allow_modeset))
  4431. dev_priv->display.modeset_commit_cdclk(state);
  4432. }
  4433. for (i = 0; i < I915_MAX_PIPES; i++)
  4434. if (put_domains[i])
  4435. modeset_put_power_domains(dev_priv, put_domains[i]);
  4436. }
  4437. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4438. {
  4439. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4440. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4441. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4442. return max_cdclk_freq;
  4443. else if (IS_CHERRYVIEW(dev_priv))
  4444. return max_cdclk_freq*95/100;
  4445. else if (INTEL_INFO(dev_priv)->gen < 4)
  4446. return 2*max_cdclk_freq*90/100;
  4447. else
  4448. return max_cdclk_freq*90/100;
  4449. }
  4450. static void intel_update_max_cdclk(struct drm_device *dev)
  4451. {
  4452. struct drm_i915_private *dev_priv = dev->dev_private;
  4453. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4454. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4455. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4456. dev_priv->max_cdclk_freq = 675000;
  4457. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4458. dev_priv->max_cdclk_freq = 540000;
  4459. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4460. dev_priv->max_cdclk_freq = 450000;
  4461. else
  4462. dev_priv->max_cdclk_freq = 337500;
  4463. } else if (IS_BROADWELL(dev)) {
  4464. /*
  4465. * FIXME with extra cooling we can allow
  4466. * 540 MHz for ULX and 675 Mhz for ULT.
  4467. * How can we know if extra cooling is
  4468. * available? PCI ID, VTB, something else?
  4469. */
  4470. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4471. dev_priv->max_cdclk_freq = 450000;
  4472. else if (IS_BDW_ULX(dev))
  4473. dev_priv->max_cdclk_freq = 450000;
  4474. else if (IS_BDW_ULT(dev))
  4475. dev_priv->max_cdclk_freq = 540000;
  4476. else
  4477. dev_priv->max_cdclk_freq = 675000;
  4478. } else if (IS_CHERRYVIEW(dev)) {
  4479. dev_priv->max_cdclk_freq = 320000;
  4480. } else if (IS_VALLEYVIEW(dev)) {
  4481. dev_priv->max_cdclk_freq = 400000;
  4482. } else {
  4483. /* otherwise assume cdclk is fixed */
  4484. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4485. }
  4486. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4487. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4488. dev_priv->max_cdclk_freq);
  4489. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4490. dev_priv->max_dotclk_freq);
  4491. }
  4492. static void intel_update_cdclk(struct drm_device *dev)
  4493. {
  4494. struct drm_i915_private *dev_priv = dev->dev_private;
  4495. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4496. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4497. dev_priv->cdclk_freq);
  4498. /*
  4499. * Program the gmbus_freq based on the cdclk frequency.
  4500. * BSpec erroneously claims we should aim for 4MHz, but
  4501. * in fact 1MHz is the correct frequency.
  4502. */
  4503. if (IS_VALLEYVIEW(dev)) {
  4504. /*
  4505. * Program the gmbus_freq based on the cdclk frequency.
  4506. * BSpec erroneously claims we should aim for 4MHz, but
  4507. * in fact 1MHz is the correct frequency.
  4508. */
  4509. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4510. }
  4511. if (dev_priv->max_cdclk_freq == 0)
  4512. intel_update_max_cdclk(dev);
  4513. }
  4514. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4515. {
  4516. struct drm_i915_private *dev_priv = dev->dev_private;
  4517. uint32_t divider;
  4518. uint32_t ratio;
  4519. uint32_t current_freq;
  4520. int ret;
  4521. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4522. switch (frequency) {
  4523. case 144000:
  4524. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4525. ratio = BXT_DE_PLL_RATIO(60);
  4526. break;
  4527. case 288000:
  4528. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4529. ratio = BXT_DE_PLL_RATIO(60);
  4530. break;
  4531. case 384000:
  4532. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4533. ratio = BXT_DE_PLL_RATIO(60);
  4534. break;
  4535. case 576000:
  4536. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4537. ratio = BXT_DE_PLL_RATIO(60);
  4538. break;
  4539. case 624000:
  4540. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4541. ratio = BXT_DE_PLL_RATIO(65);
  4542. break;
  4543. case 19200:
  4544. /*
  4545. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4546. * to suppress GCC warning.
  4547. */
  4548. ratio = 0;
  4549. divider = 0;
  4550. break;
  4551. default:
  4552. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4553. return;
  4554. }
  4555. mutex_lock(&dev_priv->rps.hw_lock);
  4556. /* Inform power controller of upcoming frequency change */
  4557. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4558. 0x80000000);
  4559. mutex_unlock(&dev_priv->rps.hw_lock);
  4560. if (ret) {
  4561. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4562. ret, frequency);
  4563. return;
  4564. }
  4565. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4566. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4567. current_freq = current_freq * 500 + 1000;
  4568. /*
  4569. * DE PLL has to be disabled when
  4570. * - setting to 19.2MHz (bypass, PLL isn't used)
  4571. * - before setting to 624MHz (PLL needs toggling)
  4572. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4573. */
  4574. if (frequency == 19200 || frequency == 624000 ||
  4575. current_freq == 624000) {
  4576. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4577. /* Timeout 200us */
  4578. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4579. 1))
  4580. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4581. }
  4582. if (frequency != 19200) {
  4583. uint32_t val;
  4584. val = I915_READ(BXT_DE_PLL_CTL);
  4585. val &= ~BXT_DE_PLL_RATIO_MASK;
  4586. val |= ratio;
  4587. I915_WRITE(BXT_DE_PLL_CTL, val);
  4588. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4589. /* Timeout 200us */
  4590. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4591. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4592. val = I915_READ(CDCLK_CTL);
  4593. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4594. val |= divider;
  4595. /*
  4596. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4597. * enable otherwise.
  4598. */
  4599. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4600. if (frequency >= 500000)
  4601. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4602. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4603. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4604. val |= (frequency - 1000) / 500;
  4605. I915_WRITE(CDCLK_CTL, val);
  4606. }
  4607. mutex_lock(&dev_priv->rps.hw_lock);
  4608. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4609. DIV_ROUND_UP(frequency, 25000));
  4610. mutex_unlock(&dev_priv->rps.hw_lock);
  4611. if (ret) {
  4612. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4613. ret, frequency);
  4614. return;
  4615. }
  4616. intel_update_cdclk(dev);
  4617. }
  4618. void broxton_init_cdclk(struct drm_device *dev)
  4619. {
  4620. struct drm_i915_private *dev_priv = dev->dev_private;
  4621. uint32_t val;
  4622. /*
  4623. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4624. * or else the reset will hang because there is no PCH to respond.
  4625. * Move the handshake programming to initialization sequence.
  4626. * Previously was left up to BIOS.
  4627. */
  4628. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4629. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4630. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4631. /* Enable PG1 for cdclk */
  4632. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4633. /* check if cd clock is enabled */
  4634. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4635. DRM_DEBUG_KMS("Display already initialized\n");
  4636. return;
  4637. }
  4638. /*
  4639. * FIXME:
  4640. * - The initial CDCLK needs to be read from VBT.
  4641. * Need to make this change after VBT has changes for BXT.
  4642. * - check if setting the max (or any) cdclk freq is really necessary
  4643. * here, it belongs to modeset time
  4644. */
  4645. broxton_set_cdclk(dev, 624000);
  4646. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4647. POSTING_READ(DBUF_CTL);
  4648. udelay(10);
  4649. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4650. DRM_ERROR("DBuf power enable timeout!\n");
  4651. }
  4652. void broxton_uninit_cdclk(struct drm_device *dev)
  4653. {
  4654. struct drm_i915_private *dev_priv = dev->dev_private;
  4655. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4656. POSTING_READ(DBUF_CTL);
  4657. udelay(10);
  4658. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4659. DRM_ERROR("DBuf power disable timeout!\n");
  4660. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4661. broxton_set_cdclk(dev, 19200);
  4662. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4663. }
  4664. static const struct skl_cdclk_entry {
  4665. unsigned int freq;
  4666. unsigned int vco;
  4667. } skl_cdclk_frequencies[] = {
  4668. { .freq = 308570, .vco = 8640 },
  4669. { .freq = 337500, .vco = 8100 },
  4670. { .freq = 432000, .vco = 8640 },
  4671. { .freq = 450000, .vco = 8100 },
  4672. { .freq = 540000, .vco = 8100 },
  4673. { .freq = 617140, .vco = 8640 },
  4674. { .freq = 675000, .vco = 8100 },
  4675. };
  4676. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4677. {
  4678. return (freq - 1000) / 500;
  4679. }
  4680. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4681. {
  4682. unsigned int i;
  4683. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4684. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4685. if (e->freq == freq)
  4686. return e->vco;
  4687. }
  4688. return 8100;
  4689. }
  4690. static void
  4691. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4692. {
  4693. unsigned int min_freq;
  4694. u32 val;
  4695. /* select the minimum CDCLK before enabling DPLL 0 */
  4696. val = I915_READ(CDCLK_CTL);
  4697. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4698. val |= CDCLK_FREQ_337_308;
  4699. if (required_vco == 8640)
  4700. min_freq = 308570;
  4701. else
  4702. min_freq = 337500;
  4703. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4704. I915_WRITE(CDCLK_CTL, val);
  4705. POSTING_READ(CDCLK_CTL);
  4706. /*
  4707. * We always enable DPLL0 with the lowest link rate possible, but still
  4708. * taking into account the VCO required to operate the eDP panel at the
  4709. * desired frequency. The usual DP link rates operate with a VCO of
  4710. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4711. * The modeset code is responsible for the selection of the exact link
  4712. * rate later on, with the constraint of choosing a frequency that
  4713. * works with required_vco.
  4714. */
  4715. val = I915_READ(DPLL_CTRL1);
  4716. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4717. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4718. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4719. if (required_vco == 8640)
  4720. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4721. SKL_DPLL0);
  4722. else
  4723. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4724. SKL_DPLL0);
  4725. I915_WRITE(DPLL_CTRL1, val);
  4726. POSTING_READ(DPLL_CTRL1);
  4727. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4728. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4729. DRM_ERROR("DPLL0 not locked\n");
  4730. }
  4731. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4732. {
  4733. int ret;
  4734. u32 val;
  4735. /* inform PCU we want to change CDCLK */
  4736. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4737. mutex_lock(&dev_priv->rps.hw_lock);
  4738. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4739. mutex_unlock(&dev_priv->rps.hw_lock);
  4740. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4741. }
  4742. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4743. {
  4744. unsigned int i;
  4745. for (i = 0; i < 15; i++) {
  4746. if (skl_cdclk_pcu_ready(dev_priv))
  4747. return true;
  4748. udelay(10);
  4749. }
  4750. return false;
  4751. }
  4752. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4753. {
  4754. struct drm_device *dev = dev_priv->dev;
  4755. u32 freq_select, pcu_ack;
  4756. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4757. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4758. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4759. return;
  4760. }
  4761. /* set CDCLK_CTL */
  4762. switch(freq) {
  4763. case 450000:
  4764. case 432000:
  4765. freq_select = CDCLK_FREQ_450_432;
  4766. pcu_ack = 1;
  4767. break;
  4768. case 540000:
  4769. freq_select = CDCLK_FREQ_540;
  4770. pcu_ack = 2;
  4771. break;
  4772. case 308570:
  4773. case 337500:
  4774. default:
  4775. freq_select = CDCLK_FREQ_337_308;
  4776. pcu_ack = 0;
  4777. break;
  4778. case 617140:
  4779. case 675000:
  4780. freq_select = CDCLK_FREQ_675_617;
  4781. pcu_ack = 3;
  4782. break;
  4783. }
  4784. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4785. POSTING_READ(CDCLK_CTL);
  4786. /* inform PCU of the change */
  4787. mutex_lock(&dev_priv->rps.hw_lock);
  4788. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4789. mutex_unlock(&dev_priv->rps.hw_lock);
  4790. intel_update_cdclk(dev);
  4791. }
  4792. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4793. {
  4794. /* disable DBUF power */
  4795. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4796. POSTING_READ(DBUF_CTL);
  4797. udelay(10);
  4798. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4799. DRM_ERROR("DBuf power disable timeout\n");
  4800. /*
  4801. * DMC assumes ownership of LCPLL and will get confused if we touch it.
  4802. */
  4803. if (dev_priv->csr.dmc_payload) {
  4804. /* disable DPLL0 */
  4805. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) &
  4806. ~LCPLL_PLL_ENABLE);
  4807. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4808. DRM_ERROR("Couldn't disable DPLL0\n");
  4809. }
  4810. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4811. }
  4812. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4813. {
  4814. u32 val;
  4815. unsigned int required_vco;
  4816. /* enable PCH reset handshake */
  4817. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4818. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4819. /* enable PG1 and Misc I/O */
  4820. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4821. /* DPLL0 not enabled (happens on early BIOS versions) */
  4822. if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
  4823. /* enable DPLL0 */
  4824. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4825. skl_dpll0_enable(dev_priv, required_vco);
  4826. }
  4827. /* set CDCLK to the frequency the BIOS chose */
  4828. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4829. /* enable DBUF power */
  4830. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4831. POSTING_READ(DBUF_CTL);
  4832. udelay(10);
  4833. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4834. DRM_ERROR("DBuf power enable timeout\n");
  4835. }
  4836. int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4837. {
  4838. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  4839. uint32_t cdctl = I915_READ(CDCLK_CTL);
  4840. int freq = dev_priv->skl_boot_cdclk;
  4841. /*
  4842. * check if the pre-os intialized the display
  4843. * There is SWF18 scratchpad register defined which is set by the
  4844. * pre-os which can be used by the OS drivers to check the status
  4845. */
  4846. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4847. goto sanitize;
  4848. /* Is PLL enabled and locked ? */
  4849. if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
  4850. goto sanitize;
  4851. /* DPLL okay; verify the cdclock
  4852. *
  4853. * Noticed in some instances that the freq selection is correct but
  4854. * decimal part is programmed wrong from BIOS where pre-os does not
  4855. * enable display. Verify the same as well.
  4856. */
  4857. if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
  4858. /* All well; nothing to sanitize */
  4859. return false;
  4860. sanitize:
  4861. /*
  4862. * As of now initialize with max cdclk till
  4863. * we get dynamic cdclk support
  4864. * */
  4865. dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
  4866. skl_init_cdclk(dev_priv);
  4867. /* we did have to sanitize */
  4868. return true;
  4869. }
  4870. /* Adjust CDclk dividers to allow high res or save power if possible */
  4871. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4872. {
  4873. struct drm_i915_private *dev_priv = dev->dev_private;
  4874. u32 val, cmd;
  4875. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4876. != dev_priv->cdclk_freq);
  4877. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4878. cmd = 2;
  4879. else if (cdclk == 266667)
  4880. cmd = 1;
  4881. else
  4882. cmd = 0;
  4883. mutex_lock(&dev_priv->rps.hw_lock);
  4884. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4885. val &= ~DSPFREQGUAR_MASK;
  4886. val |= (cmd << DSPFREQGUAR_SHIFT);
  4887. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4888. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4889. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4890. 50)) {
  4891. DRM_ERROR("timed out waiting for CDclk change\n");
  4892. }
  4893. mutex_unlock(&dev_priv->rps.hw_lock);
  4894. mutex_lock(&dev_priv->sb_lock);
  4895. if (cdclk == 400000) {
  4896. u32 divider;
  4897. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4898. /* adjust cdclk divider */
  4899. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4900. val &= ~CCK_FREQUENCY_VALUES;
  4901. val |= divider;
  4902. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4903. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4904. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4905. 50))
  4906. DRM_ERROR("timed out waiting for CDclk change\n");
  4907. }
  4908. /* adjust self-refresh exit latency value */
  4909. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4910. val &= ~0x7f;
  4911. /*
  4912. * For high bandwidth configs, we set a higher latency in the bunit
  4913. * so that the core display fetch happens in time to avoid underruns.
  4914. */
  4915. if (cdclk == 400000)
  4916. val |= 4500 / 250; /* 4.5 usec */
  4917. else
  4918. val |= 3000 / 250; /* 3.0 usec */
  4919. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4920. mutex_unlock(&dev_priv->sb_lock);
  4921. intel_update_cdclk(dev);
  4922. }
  4923. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4924. {
  4925. struct drm_i915_private *dev_priv = dev->dev_private;
  4926. u32 val, cmd;
  4927. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4928. != dev_priv->cdclk_freq);
  4929. switch (cdclk) {
  4930. case 333333:
  4931. case 320000:
  4932. case 266667:
  4933. case 200000:
  4934. break;
  4935. default:
  4936. MISSING_CASE(cdclk);
  4937. return;
  4938. }
  4939. /*
  4940. * Specs are full of misinformation, but testing on actual
  4941. * hardware has shown that we just need to write the desired
  4942. * CCK divider into the Punit register.
  4943. */
  4944. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4945. mutex_lock(&dev_priv->rps.hw_lock);
  4946. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4947. val &= ~DSPFREQGUAR_MASK_CHV;
  4948. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4949. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4950. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4951. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4952. 50)) {
  4953. DRM_ERROR("timed out waiting for CDclk change\n");
  4954. }
  4955. mutex_unlock(&dev_priv->rps.hw_lock);
  4956. intel_update_cdclk(dev);
  4957. }
  4958. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4959. int max_pixclk)
  4960. {
  4961. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4962. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4963. /*
  4964. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4965. * 200MHz
  4966. * 267MHz
  4967. * 320/333MHz (depends on HPLL freq)
  4968. * 400MHz (VLV only)
  4969. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4970. * of the lower bin and adjust if needed.
  4971. *
  4972. * We seem to get an unstable or solid color picture at 200MHz.
  4973. * Not sure what's wrong. For now use 200MHz only when all pipes
  4974. * are off.
  4975. */
  4976. if (!IS_CHERRYVIEW(dev_priv) &&
  4977. max_pixclk > freq_320*limit/100)
  4978. return 400000;
  4979. else if (max_pixclk > 266667*limit/100)
  4980. return freq_320;
  4981. else if (max_pixclk > 0)
  4982. return 266667;
  4983. else
  4984. return 200000;
  4985. }
  4986. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4987. int max_pixclk)
  4988. {
  4989. /*
  4990. * FIXME:
  4991. * - remove the guardband, it's not needed on BXT
  4992. * - set 19.2MHz bypass frequency if there are no active pipes
  4993. */
  4994. if (max_pixclk > 576000*9/10)
  4995. return 624000;
  4996. else if (max_pixclk > 384000*9/10)
  4997. return 576000;
  4998. else if (max_pixclk > 288000*9/10)
  4999. return 384000;
  5000. else if (max_pixclk > 144000*9/10)
  5001. return 288000;
  5002. else
  5003. return 144000;
  5004. }
  5005. /* Compute the max pixel clock for new configuration. Uses atomic state if
  5006. * that's non-NULL, look at current state otherwise. */
  5007. static int intel_mode_max_pixclk(struct drm_device *dev,
  5008. struct drm_atomic_state *state)
  5009. {
  5010. struct intel_crtc *intel_crtc;
  5011. struct intel_crtc_state *crtc_state;
  5012. int max_pixclk = 0;
  5013. for_each_intel_crtc(dev, intel_crtc) {
  5014. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5015. if (IS_ERR(crtc_state))
  5016. return PTR_ERR(crtc_state);
  5017. if (!crtc_state->base.enable)
  5018. continue;
  5019. max_pixclk = max(max_pixclk,
  5020. crtc_state->base.adjusted_mode.crtc_clock);
  5021. }
  5022. return max_pixclk;
  5023. }
  5024. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5025. {
  5026. struct drm_device *dev = state->dev;
  5027. struct drm_i915_private *dev_priv = dev->dev_private;
  5028. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5029. if (max_pixclk < 0)
  5030. return max_pixclk;
  5031. to_intel_atomic_state(state)->cdclk =
  5032. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5033. return 0;
  5034. }
  5035. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5036. {
  5037. struct drm_device *dev = state->dev;
  5038. struct drm_i915_private *dev_priv = dev->dev_private;
  5039. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5040. if (max_pixclk < 0)
  5041. return max_pixclk;
  5042. to_intel_atomic_state(state)->cdclk =
  5043. broxton_calc_cdclk(dev_priv, max_pixclk);
  5044. return 0;
  5045. }
  5046. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5047. {
  5048. unsigned int credits, default_credits;
  5049. if (IS_CHERRYVIEW(dev_priv))
  5050. default_credits = PFI_CREDIT(12);
  5051. else
  5052. default_credits = PFI_CREDIT(8);
  5053. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5054. /* CHV suggested value is 31 or 63 */
  5055. if (IS_CHERRYVIEW(dev_priv))
  5056. credits = PFI_CREDIT_63;
  5057. else
  5058. credits = PFI_CREDIT(15);
  5059. } else {
  5060. credits = default_credits;
  5061. }
  5062. /*
  5063. * WA - write default credits before re-programming
  5064. * FIXME: should we also set the resend bit here?
  5065. */
  5066. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5067. default_credits);
  5068. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5069. credits | PFI_CREDIT_RESEND);
  5070. /*
  5071. * FIXME is this guaranteed to clear
  5072. * immediately or should we poll for it?
  5073. */
  5074. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5075. }
  5076. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5077. {
  5078. struct drm_device *dev = old_state->dev;
  5079. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  5080. struct drm_i915_private *dev_priv = dev->dev_private;
  5081. /*
  5082. * FIXME: We can end up here with all power domains off, yet
  5083. * with a CDCLK frequency other than the minimum. To account
  5084. * for this take the PIPE-A power domain, which covers the HW
  5085. * blocks needed for the following programming. This can be
  5086. * removed once it's guaranteed that we get here either with
  5087. * the minimum CDCLK set, or the required power domains
  5088. * enabled.
  5089. */
  5090. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5091. if (IS_CHERRYVIEW(dev))
  5092. cherryview_set_cdclk(dev, req_cdclk);
  5093. else
  5094. valleyview_set_cdclk(dev, req_cdclk);
  5095. vlv_program_pfi_credits(dev_priv);
  5096. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5097. }
  5098. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5099. {
  5100. struct drm_device *dev = crtc->dev;
  5101. struct drm_i915_private *dev_priv = to_i915(dev);
  5102. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5103. struct intel_encoder *encoder;
  5104. int pipe = intel_crtc->pipe;
  5105. bool is_dsi;
  5106. if (WARN_ON(intel_crtc->active))
  5107. return;
  5108. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5109. if (intel_crtc->config->has_dp_encoder)
  5110. intel_dp_set_m_n(intel_crtc, M1_N1);
  5111. intel_set_pipe_timings(intel_crtc);
  5112. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5113. struct drm_i915_private *dev_priv = dev->dev_private;
  5114. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5115. I915_WRITE(CHV_CANVAS(pipe), 0);
  5116. }
  5117. i9xx_set_pipeconf(intel_crtc);
  5118. intel_crtc->active = true;
  5119. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5120. for_each_encoder_on_crtc(dev, crtc, encoder)
  5121. if (encoder->pre_pll_enable)
  5122. encoder->pre_pll_enable(encoder);
  5123. if (!is_dsi) {
  5124. if (IS_CHERRYVIEW(dev)) {
  5125. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5126. chv_enable_pll(intel_crtc, intel_crtc->config);
  5127. } else {
  5128. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5129. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5130. }
  5131. }
  5132. for_each_encoder_on_crtc(dev, crtc, encoder)
  5133. if (encoder->pre_enable)
  5134. encoder->pre_enable(encoder);
  5135. i9xx_pfit_enable(intel_crtc);
  5136. intel_crtc_load_lut(crtc);
  5137. intel_enable_pipe(intel_crtc);
  5138. assert_vblank_disabled(crtc);
  5139. drm_crtc_vblank_on(crtc);
  5140. for_each_encoder_on_crtc(dev, crtc, encoder)
  5141. encoder->enable(encoder);
  5142. }
  5143. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5144. {
  5145. struct drm_device *dev = crtc->base.dev;
  5146. struct drm_i915_private *dev_priv = dev->dev_private;
  5147. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5148. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5149. }
  5150. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5151. {
  5152. struct drm_device *dev = crtc->dev;
  5153. struct drm_i915_private *dev_priv = to_i915(dev);
  5154. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5155. struct intel_encoder *encoder;
  5156. int pipe = intel_crtc->pipe;
  5157. if (WARN_ON(intel_crtc->active))
  5158. return;
  5159. i9xx_set_pll_dividers(intel_crtc);
  5160. if (intel_crtc->config->has_dp_encoder)
  5161. intel_dp_set_m_n(intel_crtc, M1_N1);
  5162. intel_set_pipe_timings(intel_crtc);
  5163. i9xx_set_pipeconf(intel_crtc);
  5164. intel_crtc->active = true;
  5165. if (!IS_GEN2(dev))
  5166. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5167. for_each_encoder_on_crtc(dev, crtc, encoder)
  5168. if (encoder->pre_enable)
  5169. encoder->pre_enable(encoder);
  5170. i9xx_enable_pll(intel_crtc);
  5171. i9xx_pfit_enable(intel_crtc);
  5172. intel_crtc_load_lut(crtc);
  5173. intel_update_watermarks(crtc);
  5174. intel_enable_pipe(intel_crtc);
  5175. assert_vblank_disabled(crtc);
  5176. drm_crtc_vblank_on(crtc);
  5177. for_each_encoder_on_crtc(dev, crtc, encoder)
  5178. encoder->enable(encoder);
  5179. }
  5180. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5181. {
  5182. struct drm_device *dev = crtc->base.dev;
  5183. struct drm_i915_private *dev_priv = dev->dev_private;
  5184. if (!crtc->config->gmch_pfit.control)
  5185. return;
  5186. assert_pipe_disabled(dev_priv, crtc->pipe);
  5187. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5188. I915_READ(PFIT_CONTROL));
  5189. I915_WRITE(PFIT_CONTROL, 0);
  5190. }
  5191. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5192. {
  5193. struct drm_device *dev = crtc->dev;
  5194. struct drm_i915_private *dev_priv = dev->dev_private;
  5195. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5196. struct intel_encoder *encoder;
  5197. int pipe = intel_crtc->pipe;
  5198. /*
  5199. * On gen2 planes are double buffered but the pipe isn't, so we must
  5200. * wait for planes to fully turn off before disabling the pipe.
  5201. * We also need to wait on all gmch platforms because of the
  5202. * self-refresh mode constraint explained above.
  5203. */
  5204. intel_wait_for_vblank(dev, pipe);
  5205. for_each_encoder_on_crtc(dev, crtc, encoder)
  5206. encoder->disable(encoder);
  5207. drm_crtc_vblank_off(crtc);
  5208. assert_vblank_disabled(crtc);
  5209. intel_disable_pipe(intel_crtc);
  5210. i9xx_pfit_disable(intel_crtc);
  5211. for_each_encoder_on_crtc(dev, crtc, encoder)
  5212. if (encoder->post_disable)
  5213. encoder->post_disable(encoder);
  5214. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5215. if (IS_CHERRYVIEW(dev))
  5216. chv_disable_pll(dev_priv, pipe);
  5217. else if (IS_VALLEYVIEW(dev))
  5218. vlv_disable_pll(dev_priv, pipe);
  5219. else
  5220. i9xx_disable_pll(intel_crtc);
  5221. }
  5222. for_each_encoder_on_crtc(dev, crtc, encoder)
  5223. if (encoder->post_pll_disable)
  5224. encoder->post_pll_disable(encoder);
  5225. if (!IS_GEN2(dev))
  5226. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5227. }
  5228. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5229. {
  5230. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5231. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5232. enum intel_display_power_domain domain;
  5233. unsigned long domains;
  5234. if (!intel_crtc->active)
  5235. return;
  5236. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5237. WARN_ON(intel_crtc->unpin_work);
  5238. intel_pre_disable_primary(crtc);
  5239. }
  5240. intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
  5241. dev_priv->display.crtc_disable(crtc);
  5242. intel_crtc->active = false;
  5243. intel_update_watermarks(crtc);
  5244. intel_disable_shared_dpll(intel_crtc);
  5245. domains = intel_crtc->enabled_power_domains;
  5246. for_each_power_domain(domain, domains)
  5247. intel_display_power_put(dev_priv, domain);
  5248. intel_crtc->enabled_power_domains = 0;
  5249. }
  5250. /*
  5251. * turn all crtc's off, but do not adjust state
  5252. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5253. */
  5254. int intel_display_suspend(struct drm_device *dev)
  5255. {
  5256. struct drm_mode_config *config = &dev->mode_config;
  5257. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5258. struct drm_atomic_state *state;
  5259. struct drm_crtc *crtc;
  5260. unsigned crtc_mask = 0;
  5261. int ret = 0;
  5262. if (WARN_ON(!ctx))
  5263. return 0;
  5264. lockdep_assert_held(&ctx->ww_ctx);
  5265. state = drm_atomic_state_alloc(dev);
  5266. if (WARN_ON(!state))
  5267. return -ENOMEM;
  5268. state->acquire_ctx = ctx;
  5269. state->allow_modeset = true;
  5270. for_each_crtc(dev, crtc) {
  5271. struct drm_crtc_state *crtc_state =
  5272. drm_atomic_get_crtc_state(state, crtc);
  5273. ret = PTR_ERR_OR_ZERO(crtc_state);
  5274. if (ret)
  5275. goto free;
  5276. if (!crtc_state->active)
  5277. continue;
  5278. crtc_state->active = false;
  5279. crtc_mask |= 1 << drm_crtc_index(crtc);
  5280. }
  5281. if (crtc_mask) {
  5282. ret = drm_atomic_commit(state);
  5283. if (!ret) {
  5284. for_each_crtc(dev, crtc)
  5285. if (crtc_mask & (1 << drm_crtc_index(crtc)))
  5286. crtc->state->active = true;
  5287. return ret;
  5288. }
  5289. }
  5290. free:
  5291. if (ret)
  5292. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5293. drm_atomic_state_free(state);
  5294. return ret;
  5295. }
  5296. void intel_encoder_destroy(struct drm_encoder *encoder)
  5297. {
  5298. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5299. drm_encoder_cleanup(encoder);
  5300. kfree(intel_encoder);
  5301. }
  5302. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5303. * internal consistency). */
  5304. static void intel_connector_check_state(struct intel_connector *connector)
  5305. {
  5306. struct drm_crtc *crtc = connector->base.state->crtc;
  5307. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5308. connector->base.base.id,
  5309. connector->base.name);
  5310. if (connector->get_hw_state(connector)) {
  5311. struct intel_encoder *encoder = connector->encoder;
  5312. struct drm_connector_state *conn_state = connector->base.state;
  5313. I915_STATE_WARN(!crtc,
  5314. "connector enabled without attached crtc\n");
  5315. if (!crtc)
  5316. return;
  5317. I915_STATE_WARN(!crtc->state->active,
  5318. "connector is active, but attached crtc isn't\n");
  5319. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5320. return;
  5321. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5322. "atomic encoder doesn't match attached encoder\n");
  5323. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5324. "attached encoder crtc differs from connector crtc\n");
  5325. } else {
  5326. I915_STATE_WARN(crtc && crtc->state->active,
  5327. "attached crtc is active, but connector isn't\n");
  5328. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5329. "best encoder set without crtc!\n");
  5330. }
  5331. }
  5332. int intel_connector_init(struct intel_connector *connector)
  5333. {
  5334. struct drm_connector_state *connector_state;
  5335. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5336. if (!connector_state)
  5337. return -ENOMEM;
  5338. connector->base.state = connector_state;
  5339. return 0;
  5340. }
  5341. struct intel_connector *intel_connector_alloc(void)
  5342. {
  5343. struct intel_connector *connector;
  5344. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5345. if (!connector)
  5346. return NULL;
  5347. if (intel_connector_init(connector) < 0) {
  5348. kfree(connector);
  5349. return NULL;
  5350. }
  5351. return connector;
  5352. }
  5353. /* Simple connector->get_hw_state implementation for encoders that support only
  5354. * one connector and no cloning and hence the encoder state determines the state
  5355. * of the connector. */
  5356. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5357. {
  5358. enum pipe pipe = 0;
  5359. struct intel_encoder *encoder = connector->encoder;
  5360. return encoder->get_hw_state(encoder, &pipe);
  5361. }
  5362. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5363. {
  5364. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5365. return crtc_state->fdi_lanes;
  5366. return 0;
  5367. }
  5368. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5369. struct intel_crtc_state *pipe_config)
  5370. {
  5371. struct drm_atomic_state *state = pipe_config->base.state;
  5372. struct intel_crtc *other_crtc;
  5373. struct intel_crtc_state *other_crtc_state;
  5374. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5375. pipe_name(pipe), pipe_config->fdi_lanes);
  5376. if (pipe_config->fdi_lanes > 4) {
  5377. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5378. pipe_name(pipe), pipe_config->fdi_lanes);
  5379. return -EINVAL;
  5380. }
  5381. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5382. if (pipe_config->fdi_lanes > 2) {
  5383. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5384. pipe_config->fdi_lanes);
  5385. return -EINVAL;
  5386. } else {
  5387. return 0;
  5388. }
  5389. }
  5390. if (INTEL_INFO(dev)->num_pipes == 2)
  5391. return 0;
  5392. /* Ivybridge 3 pipe is really complicated */
  5393. switch (pipe) {
  5394. case PIPE_A:
  5395. return 0;
  5396. case PIPE_B:
  5397. if (pipe_config->fdi_lanes <= 2)
  5398. return 0;
  5399. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5400. other_crtc_state =
  5401. intel_atomic_get_crtc_state(state, other_crtc);
  5402. if (IS_ERR(other_crtc_state))
  5403. return PTR_ERR(other_crtc_state);
  5404. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5405. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5406. pipe_name(pipe), pipe_config->fdi_lanes);
  5407. return -EINVAL;
  5408. }
  5409. return 0;
  5410. case PIPE_C:
  5411. if (pipe_config->fdi_lanes > 2) {
  5412. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5413. pipe_name(pipe), pipe_config->fdi_lanes);
  5414. return -EINVAL;
  5415. }
  5416. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5417. other_crtc_state =
  5418. intel_atomic_get_crtc_state(state, other_crtc);
  5419. if (IS_ERR(other_crtc_state))
  5420. return PTR_ERR(other_crtc_state);
  5421. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5422. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5423. return -EINVAL;
  5424. }
  5425. return 0;
  5426. default:
  5427. BUG();
  5428. }
  5429. }
  5430. #define RETRY 1
  5431. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5432. struct intel_crtc_state *pipe_config)
  5433. {
  5434. struct drm_device *dev = intel_crtc->base.dev;
  5435. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5436. int lane, link_bw, fdi_dotclock, ret;
  5437. bool needs_recompute = false;
  5438. retry:
  5439. /* FDI is a binary signal running at ~2.7GHz, encoding
  5440. * each output octet as 10 bits. The actual frequency
  5441. * is stored as a divider into a 100MHz clock, and the
  5442. * mode pixel clock is stored in units of 1KHz.
  5443. * Hence the bw of each lane in terms of the mode signal
  5444. * is:
  5445. */
  5446. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5447. fdi_dotclock = adjusted_mode->crtc_clock;
  5448. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5449. pipe_config->pipe_bpp);
  5450. pipe_config->fdi_lanes = lane;
  5451. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5452. link_bw, &pipe_config->fdi_m_n);
  5453. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5454. intel_crtc->pipe, pipe_config);
  5455. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5456. pipe_config->pipe_bpp -= 2*3;
  5457. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5458. pipe_config->pipe_bpp);
  5459. needs_recompute = true;
  5460. pipe_config->bw_constrained = true;
  5461. goto retry;
  5462. }
  5463. if (needs_recompute)
  5464. return RETRY;
  5465. return ret;
  5466. }
  5467. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5468. struct intel_crtc_state *pipe_config)
  5469. {
  5470. if (pipe_config->pipe_bpp > 24)
  5471. return false;
  5472. /* HSW can handle pixel rate up to cdclk? */
  5473. if (IS_HASWELL(dev_priv->dev))
  5474. return true;
  5475. /*
  5476. * We compare against max which means we must take
  5477. * the increased cdclk requirement into account when
  5478. * calculating the new cdclk.
  5479. *
  5480. * Should measure whether using a lower cdclk w/o IPS
  5481. */
  5482. return ilk_pipe_pixel_rate(pipe_config) <=
  5483. dev_priv->max_cdclk_freq * 95 / 100;
  5484. }
  5485. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5486. struct intel_crtc_state *pipe_config)
  5487. {
  5488. struct drm_device *dev = crtc->base.dev;
  5489. struct drm_i915_private *dev_priv = dev->dev_private;
  5490. pipe_config->ips_enabled = i915.enable_ips &&
  5491. hsw_crtc_supports_ips(crtc) &&
  5492. pipe_config_supports_ips(dev_priv, pipe_config);
  5493. }
  5494. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5495. {
  5496. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5497. /* GDG double wide on either pipe, otherwise pipe A only */
  5498. return INTEL_INFO(dev_priv)->gen < 4 &&
  5499. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5500. }
  5501. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5502. struct intel_crtc_state *pipe_config)
  5503. {
  5504. struct drm_device *dev = crtc->base.dev;
  5505. struct drm_i915_private *dev_priv = dev->dev_private;
  5506. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5507. /* FIXME should check pixel clock limits on all platforms */
  5508. if (INTEL_INFO(dev)->gen < 4) {
  5509. int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5510. /*
  5511. * Enable double wide mode when the dot clock
  5512. * is > 90% of the (display) core speed.
  5513. */
  5514. if (intel_crtc_supports_double_wide(crtc) &&
  5515. adjusted_mode->crtc_clock > clock_limit) {
  5516. clock_limit *= 2;
  5517. pipe_config->double_wide = true;
  5518. }
  5519. if (adjusted_mode->crtc_clock > clock_limit) {
  5520. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5521. adjusted_mode->crtc_clock, clock_limit,
  5522. yesno(pipe_config->double_wide));
  5523. return -EINVAL;
  5524. }
  5525. }
  5526. /*
  5527. * Pipe horizontal size must be even in:
  5528. * - DVO ganged mode
  5529. * - LVDS dual channel mode
  5530. * - Double wide pipe
  5531. */
  5532. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5533. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5534. pipe_config->pipe_src_w &= ~1;
  5535. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5536. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5537. */
  5538. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5539. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5540. return -EINVAL;
  5541. if (HAS_IPS(dev))
  5542. hsw_compute_ips_config(crtc, pipe_config);
  5543. if (pipe_config->has_pch_encoder)
  5544. return ironlake_fdi_compute_config(crtc, pipe_config);
  5545. return 0;
  5546. }
  5547. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5548. {
  5549. struct drm_i915_private *dev_priv = to_i915(dev);
  5550. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5551. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5552. uint32_t linkrate;
  5553. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5554. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5555. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5556. return 540000;
  5557. linkrate = (I915_READ(DPLL_CTRL1) &
  5558. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5559. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5560. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5561. /* vco 8640 */
  5562. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5563. case CDCLK_FREQ_450_432:
  5564. return 432000;
  5565. case CDCLK_FREQ_337_308:
  5566. return 308570;
  5567. case CDCLK_FREQ_675_617:
  5568. return 617140;
  5569. default:
  5570. WARN(1, "Unknown cd freq selection\n");
  5571. }
  5572. } else {
  5573. /* vco 8100 */
  5574. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5575. case CDCLK_FREQ_450_432:
  5576. return 450000;
  5577. case CDCLK_FREQ_337_308:
  5578. return 337500;
  5579. case CDCLK_FREQ_675_617:
  5580. return 675000;
  5581. default:
  5582. WARN(1, "Unknown cd freq selection\n");
  5583. }
  5584. }
  5585. /* error case, do as if DPLL0 isn't enabled */
  5586. return 24000;
  5587. }
  5588. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5589. {
  5590. struct drm_i915_private *dev_priv = to_i915(dev);
  5591. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5592. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5593. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5594. int cdclk;
  5595. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5596. return 19200;
  5597. cdclk = 19200 * pll_ratio / 2;
  5598. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5599. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5600. return cdclk; /* 576MHz or 624MHz */
  5601. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5602. return cdclk * 2 / 3; /* 384MHz */
  5603. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5604. return cdclk / 2; /* 288MHz */
  5605. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5606. return cdclk / 4; /* 144MHz */
  5607. }
  5608. /* error case, do as if DE PLL isn't enabled */
  5609. return 19200;
  5610. }
  5611. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5612. {
  5613. struct drm_i915_private *dev_priv = dev->dev_private;
  5614. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5615. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5616. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5617. return 800000;
  5618. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5619. return 450000;
  5620. else if (freq == LCPLL_CLK_FREQ_450)
  5621. return 450000;
  5622. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5623. return 540000;
  5624. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5625. return 337500;
  5626. else
  5627. return 675000;
  5628. }
  5629. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5630. {
  5631. struct drm_i915_private *dev_priv = dev->dev_private;
  5632. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5633. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5634. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5635. return 800000;
  5636. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5637. return 450000;
  5638. else if (freq == LCPLL_CLK_FREQ_450)
  5639. return 450000;
  5640. else if (IS_HSW_ULT(dev))
  5641. return 337500;
  5642. else
  5643. return 540000;
  5644. }
  5645. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5646. {
  5647. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5648. CCK_DISPLAY_CLOCK_CONTROL);
  5649. }
  5650. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5651. {
  5652. return 450000;
  5653. }
  5654. static int i945_get_display_clock_speed(struct drm_device *dev)
  5655. {
  5656. return 400000;
  5657. }
  5658. static int i915_get_display_clock_speed(struct drm_device *dev)
  5659. {
  5660. return 333333;
  5661. }
  5662. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5663. {
  5664. return 200000;
  5665. }
  5666. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5667. {
  5668. u16 gcfgc = 0;
  5669. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5670. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5671. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5672. return 266667;
  5673. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5674. return 333333;
  5675. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5676. return 444444;
  5677. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5678. return 200000;
  5679. default:
  5680. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5681. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5682. return 133333;
  5683. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5684. return 166667;
  5685. }
  5686. }
  5687. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5688. {
  5689. u16 gcfgc = 0;
  5690. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5691. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5692. return 133333;
  5693. else {
  5694. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5695. case GC_DISPLAY_CLOCK_333_MHZ:
  5696. return 333333;
  5697. default:
  5698. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5699. return 190000;
  5700. }
  5701. }
  5702. }
  5703. static int i865_get_display_clock_speed(struct drm_device *dev)
  5704. {
  5705. return 266667;
  5706. }
  5707. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5708. {
  5709. u16 hpllcc = 0;
  5710. /*
  5711. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5712. * encoding is different :(
  5713. * FIXME is this the right way to detect 852GM/852GMV?
  5714. */
  5715. if (dev->pdev->revision == 0x1)
  5716. return 133333;
  5717. pci_bus_read_config_word(dev->pdev->bus,
  5718. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5719. /* Assume that the hardware is in the high speed state. This
  5720. * should be the default.
  5721. */
  5722. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5723. case GC_CLOCK_133_200:
  5724. case GC_CLOCK_133_200_2:
  5725. case GC_CLOCK_100_200:
  5726. return 200000;
  5727. case GC_CLOCK_166_250:
  5728. return 250000;
  5729. case GC_CLOCK_100_133:
  5730. return 133333;
  5731. case GC_CLOCK_133_266:
  5732. case GC_CLOCK_133_266_2:
  5733. case GC_CLOCK_166_266:
  5734. return 266667;
  5735. }
  5736. /* Shouldn't happen */
  5737. return 0;
  5738. }
  5739. static int i830_get_display_clock_speed(struct drm_device *dev)
  5740. {
  5741. return 133333;
  5742. }
  5743. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5744. {
  5745. struct drm_i915_private *dev_priv = dev->dev_private;
  5746. static const unsigned int blb_vco[8] = {
  5747. [0] = 3200000,
  5748. [1] = 4000000,
  5749. [2] = 5333333,
  5750. [3] = 4800000,
  5751. [4] = 6400000,
  5752. };
  5753. static const unsigned int pnv_vco[8] = {
  5754. [0] = 3200000,
  5755. [1] = 4000000,
  5756. [2] = 5333333,
  5757. [3] = 4800000,
  5758. [4] = 2666667,
  5759. };
  5760. static const unsigned int cl_vco[8] = {
  5761. [0] = 3200000,
  5762. [1] = 4000000,
  5763. [2] = 5333333,
  5764. [3] = 6400000,
  5765. [4] = 3333333,
  5766. [5] = 3566667,
  5767. [6] = 4266667,
  5768. };
  5769. static const unsigned int elk_vco[8] = {
  5770. [0] = 3200000,
  5771. [1] = 4000000,
  5772. [2] = 5333333,
  5773. [3] = 4800000,
  5774. };
  5775. static const unsigned int ctg_vco[8] = {
  5776. [0] = 3200000,
  5777. [1] = 4000000,
  5778. [2] = 5333333,
  5779. [3] = 6400000,
  5780. [4] = 2666667,
  5781. [5] = 4266667,
  5782. };
  5783. const unsigned int *vco_table;
  5784. unsigned int vco;
  5785. uint8_t tmp = 0;
  5786. /* FIXME other chipsets? */
  5787. if (IS_GM45(dev))
  5788. vco_table = ctg_vco;
  5789. else if (IS_G4X(dev))
  5790. vco_table = elk_vco;
  5791. else if (IS_CRESTLINE(dev))
  5792. vco_table = cl_vco;
  5793. else if (IS_PINEVIEW(dev))
  5794. vco_table = pnv_vco;
  5795. else if (IS_G33(dev))
  5796. vco_table = blb_vco;
  5797. else
  5798. return 0;
  5799. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5800. vco = vco_table[tmp & 0x7];
  5801. if (vco == 0)
  5802. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5803. else
  5804. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5805. return vco;
  5806. }
  5807. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5808. {
  5809. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5810. uint16_t tmp = 0;
  5811. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5812. cdclk_sel = (tmp >> 12) & 0x1;
  5813. switch (vco) {
  5814. case 2666667:
  5815. case 4000000:
  5816. case 5333333:
  5817. return cdclk_sel ? 333333 : 222222;
  5818. case 3200000:
  5819. return cdclk_sel ? 320000 : 228571;
  5820. default:
  5821. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5822. return 222222;
  5823. }
  5824. }
  5825. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5826. {
  5827. static const uint8_t div_3200[] = { 16, 10, 8 };
  5828. static const uint8_t div_4000[] = { 20, 12, 10 };
  5829. static const uint8_t div_5333[] = { 24, 16, 14 };
  5830. const uint8_t *div_table;
  5831. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5832. uint16_t tmp = 0;
  5833. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5834. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5835. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5836. goto fail;
  5837. switch (vco) {
  5838. case 3200000:
  5839. div_table = div_3200;
  5840. break;
  5841. case 4000000:
  5842. div_table = div_4000;
  5843. break;
  5844. case 5333333:
  5845. div_table = div_5333;
  5846. break;
  5847. default:
  5848. goto fail;
  5849. }
  5850. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5851. fail:
  5852. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5853. return 200000;
  5854. }
  5855. static int g33_get_display_clock_speed(struct drm_device *dev)
  5856. {
  5857. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5858. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5859. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5860. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5861. const uint8_t *div_table;
  5862. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5863. uint16_t tmp = 0;
  5864. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5865. cdclk_sel = (tmp >> 4) & 0x7;
  5866. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5867. goto fail;
  5868. switch (vco) {
  5869. case 3200000:
  5870. div_table = div_3200;
  5871. break;
  5872. case 4000000:
  5873. div_table = div_4000;
  5874. break;
  5875. case 4800000:
  5876. div_table = div_4800;
  5877. break;
  5878. case 5333333:
  5879. div_table = div_5333;
  5880. break;
  5881. default:
  5882. goto fail;
  5883. }
  5884. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5885. fail:
  5886. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5887. return 190476;
  5888. }
  5889. static void
  5890. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5891. {
  5892. while (*num > DATA_LINK_M_N_MASK ||
  5893. *den > DATA_LINK_M_N_MASK) {
  5894. *num >>= 1;
  5895. *den >>= 1;
  5896. }
  5897. }
  5898. static void compute_m_n(unsigned int m, unsigned int n,
  5899. uint32_t *ret_m, uint32_t *ret_n)
  5900. {
  5901. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5902. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5903. intel_reduce_m_n_ratio(ret_m, ret_n);
  5904. }
  5905. void
  5906. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5907. int pixel_clock, int link_clock,
  5908. struct intel_link_m_n *m_n)
  5909. {
  5910. m_n->tu = 64;
  5911. compute_m_n(bits_per_pixel * pixel_clock,
  5912. link_clock * nlanes * 8,
  5913. &m_n->gmch_m, &m_n->gmch_n);
  5914. compute_m_n(pixel_clock, link_clock,
  5915. &m_n->link_m, &m_n->link_n);
  5916. }
  5917. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5918. {
  5919. if (i915.panel_use_ssc >= 0)
  5920. return i915.panel_use_ssc != 0;
  5921. return dev_priv->vbt.lvds_use_ssc
  5922. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5923. }
  5924. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5925. int num_connectors)
  5926. {
  5927. struct drm_device *dev = crtc_state->base.crtc->dev;
  5928. struct drm_i915_private *dev_priv = dev->dev_private;
  5929. int refclk;
  5930. WARN_ON(!crtc_state->base.state);
  5931. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5932. refclk = 100000;
  5933. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5934. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5935. refclk = dev_priv->vbt.lvds_ssc_freq;
  5936. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5937. } else if (!IS_GEN2(dev)) {
  5938. refclk = 96000;
  5939. } else {
  5940. refclk = 48000;
  5941. }
  5942. return refclk;
  5943. }
  5944. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5945. {
  5946. return (1 << dpll->n) << 16 | dpll->m2;
  5947. }
  5948. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5949. {
  5950. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5951. }
  5952. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5953. struct intel_crtc_state *crtc_state,
  5954. intel_clock_t *reduced_clock)
  5955. {
  5956. struct drm_device *dev = crtc->base.dev;
  5957. u32 fp, fp2 = 0;
  5958. if (IS_PINEVIEW(dev)) {
  5959. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5960. if (reduced_clock)
  5961. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5962. } else {
  5963. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5964. if (reduced_clock)
  5965. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5966. }
  5967. crtc_state->dpll_hw_state.fp0 = fp;
  5968. crtc->lowfreq_avail = false;
  5969. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5970. reduced_clock) {
  5971. crtc_state->dpll_hw_state.fp1 = fp2;
  5972. crtc->lowfreq_avail = true;
  5973. } else {
  5974. crtc_state->dpll_hw_state.fp1 = fp;
  5975. }
  5976. }
  5977. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5978. pipe)
  5979. {
  5980. u32 reg_val;
  5981. /*
  5982. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5983. * and set it to a reasonable value instead.
  5984. */
  5985. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5986. reg_val &= 0xffffff00;
  5987. reg_val |= 0x00000030;
  5988. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5989. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5990. reg_val &= 0x8cffffff;
  5991. reg_val = 0x8c000000;
  5992. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5993. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5994. reg_val &= 0xffffff00;
  5995. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5996. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5997. reg_val &= 0x00ffffff;
  5998. reg_val |= 0xb0000000;
  5999. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6000. }
  6001. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6002. struct intel_link_m_n *m_n)
  6003. {
  6004. struct drm_device *dev = crtc->base.dev;
  6005. struct drm_i915_private *dev_priv = dev->dev_private;
  6006. int pipe = crtc->pipe;
  6007. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6008. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6009. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6010. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6011. }
  6012. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6013. struct intel_link_m_n *m_n,
  6014. struct intel_link_m_n *m2_n2)
  6015. {
  6016. struct drm_device *dev = crtc->base.dev;
  6017. struct drm_i915_private *dev_priv = dev->dev_private;
  6018. int pipe = crtc->pipe;
  6019. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6020. if (INTEL_INFO(dev)->gen >= 5) {
  6021. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6022. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6023. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6024. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6025. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6026. * for gen < 8) and if DRRS is supported (to make sure the
  6027. * registers are not unnecessarily accessed).
  6028. */
  6029. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6030. crtc->config->has_drrs) {
  6031. I915_WRITE(PIPE_DATA_M2(transcoder),
  6032. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6033. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6034. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6035. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6036. }
  6037. } else {
  6038. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6039. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6040. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6041. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6042. }
  6043. }
  6044. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6045. {
  6046. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6047. if (m_n == M1_N1) {
  6048. dp_m_n = &crtc->config->dp_m_n;
  6049. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6050. } else if (m_n == M2_N2) {
  6051. /*
  6052. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6053. * needs to be programmed into M1_N1.
  6054. */
  6055. dp_m_n = &crtc->config->dp_m2_n2;
  6056. } else {
  6057. DRM_ERROR("Unsupported divider value\n");
  6058. return;
  6059. }
  6060. if (crtc->config->has_pch_encoder)
  6061. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6062. else
  6063. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6064. }
  6065. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6066. struct intel_crtc_state *pipe_config)
  6067. {
  6068. u32 dpll, dpll_md;
  6069. /*
  6070. * Enable DPIO clock input. We should never disable the reference
  6071. * clock for pipe B, since VGA hotplug / manual detection depends
  6072. * on it.
  6073. */
  6074. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
  6075. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
  6076. /* We should never disable this, set it here for state tracking */
  6077. if (crtc->pipe == PIPE_B)
  6078. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6079. dpll |= DPLL_VCO_ENABLE;
  6080. pipe_config->dpll_hw_state.dpll = dpll;
  6081. dpll_md = (pipe_config->pixel_multiplier - 1)
  6082. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6083. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6084. }
  6085. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6086. const struct intel_crtc_state *pipe_config)
  6087. {
  6088. struct drm_device *dev = crtc->base.dev;
  6089. struct drm_i915_private *dev_priv = dev->dev_private;
  6090. int pipe = crtc->pipe;
  6091. u32 mdiv;
  6092. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6093. u32 coreclk, reg_val;
  6094. mutex_lock(&dev_priv->sb_lock);
  6095. bestn = pipe_config->dpll.n;
  6096. bestm1 = pipe_config->dpll.m1;
  6097. bestm2 = pipe_config->dpll.m2;
  6098. bestp1 = pipe_config->dpll.p1;
  6099. bestp2 = pipe_config->dpll.p2;
  6100. /* See eDP HDMI DPIO driver vbios notes doc */
  6101. /* PLL B needs special handling */
  6102. if (pipe == PIPE_B)
  6103. vlv_pllb_recal_opamp(dev_priv, pipe);
  6104. /* Set up Tx target for periodic Rcomp update */
  6105. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6106. /* Disable target IRef on PLL */
  6107. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6108. reg_val &= 0x00ffffff;
  6109. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6110. /* Disable fast lock */
  6111. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6112. /* Set idtafcrecal before PLL is enabled */
  6113. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6114. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6115. mdiv |= ((bestn << DPIO_N_SHIFT));
  6116. mdiv |= (1 << DPIO_K_SHIFT);
  6117. /*
  6118. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6119. * but we don't support that).
  6120. * Note: don't use the DAC post divider as it seems unstable.
  6121. */
  6122. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6123. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6124. mdiv |= DPIO_ENABLE_CALIBRATION;
  6125. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6126. /* Set HBR and RBR LPF coefficients */
  6127. if (pipe_config->port_clock == 162000 ||
  6128. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6129. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6130. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6131. 0x009f0003);
  6132. else
  6133. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6134. 0x00d0000f);
  6135. if (pipe_config->has_dp_encoder) {
  6136. /* Use SSC source */
  6137. if (pipe == PIPE_A)
  6138. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6139. 0x0df40000);
  6140. else
  6141. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6142. 0x0df70000);
  6143. } else { /* HDMI or VGA */
  6144. /* Use bend source */
  6145. if (pipe == PIPE_A)
  6146. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6147. 0x0df70000);
  6148. else
  6149. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6150. 0x0df40000);
  6151. }
  6152. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6153. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6154. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6155. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6156. coreclk |= 0x01000000;
  6157. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6158. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6159. mutex_unlock(&dev_priv->sb_lock);
  6160. }
  6161. static void chv_compute_dpll(struct intel_crtc *crtc,
  6162. struct intel_crtc_state *pipe_config)
  6163. {
  6164. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6165. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6166. DPLL_VCO_ENABLE;
  6167. if (crtc->pipe != PIPE_A)
  6168. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6169. pipe_config->dpll_hw_state.dpll_md =
  6170. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6171. }
  6172. static void chv_prepare_pll(struct intel_crtc *crtc,
  6173. const struct intel_crtc_state *pipe_config)
  6174. {
  6175. struct drm_device *dev = crtc->base.dev;
  6176. struct drm_i915_private *dev_priv = dev->dev_private;
  6177. int pipe = crtc->pipe;
  6178. int dpll_reg = DPLL(crtc->pipe);
  6179. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6180. u32 loopfilter, tribuf_calcntr;
  6181. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6182. u32 dpio_val;
  6183. int vco;
  6184. bestn = pipe_config->dpll.n;
  6185. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6186. bestm1 = pipe_config->dpll.m1;
  6187. bestm2 = pipe_config->dpll.m2 >> 22;
  6188. bestp1 = pipe_config->dpll.p1;
  6189. bestp2 = pipe_config->dpll.p2;
  6190. vco = pipe_config->dpll.vco;
  6191. dpio_val = 0;
  6192. loopfilter = 0;
  6193. /*
  6194. * Enable Refclk and SSC
  6195. */
  6196. I915_WRITE(dpll_reg,
  6197. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6198. mutex_lock(&dev_priv->sb_lock);
  6199. /* p1 and p2 divider */
  6200. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6201. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6202. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6203. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6204. 1 << DPIO_CHV_K_DIV_SHIFT);
  6205. /* Feedback post-divider - m2 */
  6206. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6207. /* Feedback refclk divider - n and m1 */
  6208. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6209. DPIO_CHV_M1_DIV_BY_2 |
  6210. 1 << DPIO_CHV_N_DIV_SHIFT);
  6211. /* M2 fraction division */
  6212. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6213. /* M2 fraction division enable */
  6214. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6215. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6216. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6217. if (bestm2_frac)
  6218. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6219. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6220. /* Program digital lock detect threshold */
  6221. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6222. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6223. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6224. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6225. if (!bestm2_frac)
  6226. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6227. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6228. /* Loop filter */
  6229. if (vco == 5400000) {
  6230. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6231. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6232. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6233. tribuf_calcntr = 0x9;
  6234. } else if (vco <= 6200000) {
  6235. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6236. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6237. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6238. tribuf_calcntr = 0x9;
  6239. } else if (vco <= 6480000) {
  6240. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6241. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6242. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6243. tribuf_calcntr = 0x8;
  6244. } else {
  6245. /* Not supported. Apply the same limits as in the max case */
  6246. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6247. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6248. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6249. tribuf_calcntr = 0;
  6250. }
  6251. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6252. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6253. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6254. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6255. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6256. /* AFC Recal */
  6257. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6258. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6259. DPIO_AFC_RECAL);
  6260. mutex_unlock(&dev_priv->sb_lock);
  6261. }
  6262. /**
  6263. * vlv_force_pll_on - forcibly enable just the PLL
  6264. * @dev_priv: i915 private structure
  6265. * @pipe: pipe PLL to enable
  6266. * @dpll: PLL configuration
  6267. *
  6268. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6269. * in cases where we need the PLL enabled even when @pipe is not going to
  6270. * be enabled.
  6271. */
  6272. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6273. const struct dpll *dpll)
  6274. {
  6275. struct intel_crtc *crtc =
  6276. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6277. struct intel_crtc_state pipe_config = {
  6278. .base.crtc = &crtc->base,
  6279. .pixel_multiplier = 1,
  6280. .dpll = *dpll,
  6281. };
  6282. if (IS_CHERRYVIEW(dev)) {
  6283. chv_compute_dpll(crtc, &pipe_config);
  6284. chv_prepare_pll(crtc, &pipe_config);
  6285. chv_enable_pll(crtc, &pipe_config);
  6286. } else {
  6287. vlv_compute_dpll(crtc, &pipe_config);
  6288. vlv_prepare_pll(crtc, &pipe_config);
  6289. vlv_enable_pll(crtc, &pipe_config);
  6290. }
  6291. }
  6292. /**
  6293. * vlv_force_pll_off - forcibly disable just the PLL
  6294. * @dev_priv: i915 private structure
  6295. * @pipe: pipe PLL to disable
  6296. *
  6297. * Disable the PLL for @pipe. To be used in cases where we need
  6298. * the PLL enabled even when @pipe is not going to be enabled.
  6299. */
  6300. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6301. {
  6302. if (IS_CHERRYVIEW(dev))
  6303. chv_disable_pll(to_i915(dev), pipe);
  6304. else
  6305. vlv_disable_pll(to_i915(dev), pipe);
  6306. }
  6307. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6308. struct intel_crtc_state *crtc_state,
  6309. intel_clock_t *reduced_clock,
  6310. int num_connectors)
  6311. {
  6312. struct drm_device *dev = crtc->base.dev;
  6313. struct drm_i915_private *dev_priv = dev->dev_private;
  6314. u32 dpll;
  6315. bool is_sdvo;
  6316. struct dpll *clock = &crtc_state->dpll;
  6317. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6318. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6319. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6320. dpll = DPLL_VGA_MODE_DIS;
  6321. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6322. dpll |= DPLLB_MODE_LVDS;
  6323. else
  6324. dpll |= DPLLB_MODE_DAC_SERIAL;
  6325. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6326. dpll |= (crtc_state->pixel_multiplier - 1)
  6327. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6328. }
  6329. if (is_sdvo)
  6330. dpll |= DPLL_SDVO_HIGH_SPEED;
  6331. if (crtc_state->has_dp_encoder)
  6332. dpll |= DPLL_SDVO_HIGH_SPEED;
  6333. /* compute bitmask from p1 value */
  6334. if (IS_PINEVIEW(dev))
  6335. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6336. else {
  6337. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6338. if (IS_G4X(dev) && reduced_clock)
  6339. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6340. }
  6341. switch (clock->p2) {
  6342. case 5:
  6343. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6344. break;
  6345. case 7:
  6346. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6347. break;
  6348. case 10:
  6349. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6350. break;
  6351. case 14:
  6352. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6353. break;
  6354. }
  6355. if (INTEL_INFO(dev)->gen >= 4)
  6356. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6357. if (crtc_state->sdvo_tv_clock)
  6358. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6359. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6360. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6361. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6362. else
  6363. dpll |= PLL_REF_INPUT_DREFCLK;
  6364. dpll |= DPLL_VCO_ENABLE;
  6365. crtc_state->dpll_hw_state.dpll = dpll;
  6366. if (INTEL_INFO(dev)->gen >= 4) {
  6367. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6368. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6369. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6370. }
  6371. }
  6372. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6373. struct intel_crtc_state *crtc_state,
  6374. intel_clock_t *reduced_clock,
  6375. int num_connectors)
  6376. {
  6377. struct drm_device *dev = crtc->base.dev;
  6378. struct drm_i915_private *dev_priv = dev->dev_private;
  6379. u32 dpll;
  6380. struct dpll *clock = &crtc_state->dpll;
  6381. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6382. dpll = DPLL_VGA_MODE_DIS;
  6383. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6384. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6385. } else {
  6386. if (clock->p1 == 2)
  6387. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6388. else
  6389. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6390. if (clock->p2 == 4)
  6391. dpll |= PLL_P2_DIVIDE_BY_4;
  6392. }
  6393. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6394. dpll |= DPLL_DVO_2X_MODE;
  6395. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6396. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6397. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6398. else
  6399. dpll |= PLL_REF_INPUT_DREFCLK;
  6400. dpll |= DPLL_VCO_ENABLE;
  6401. crtc_state->dpll_hw_state.dpll = dpll;
  6402. }
  6403. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6404. {
  6405. struct drm_device *dev = intel_crtc->base.dev;
  6406. struct drm_i915_private *dev_priv = dev->dev_private;
  6407. enum pipe pipe = intel_crtc->pipe;
  6408. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6409. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6410. uint32_t crtc_vtotal, crtc_vblank_end;
  6411. int vsyncshift = 0;
  6412. /* We need to be careful not to changed the adjusted mode, for otherwise
  6413. * the hw state checker will get angry at the mismatch. */
  6414. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6415. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6416. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6417. /* the chip adds 2 halflines automatically */
  6418. crtc_vtotal -= 1;
  6419. crtc_vblank_end -= 1;
  6420. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6421. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6422. else
  6423. vsyncshift = adjusted_mode->crtc_hsync_start -
  6424. adjusted_mode->crtc_htotal / 2;
  6425. if (vsyncshift < 0)
  6426. vsyncshift += adjusted_mode->crtc_htotal;
  6427. }
  6428. if (INTEL_INFO(dev)->gen > 3)
  6429. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6430. I915_WRITE(HTOTAL(cpu_transcoder),
  6431. (adjusted_mode->crtc_hdisplay - 1) |
  6432. ((adjusted_mode->crtc_htotal - 1) << 16));
  6433. I915_WRITE(HBLANK(cpu_transcoder),
  6434. (adjusted_mode->crtc_hblank_start - 1) |
  6435. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6436. I915_WRITE(HSYNC(cpu_transcoder),
  6437. (adjusted_mode->crtc_hsync_start - 1) |
  6438. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6439. I915_WRITE(VTOTAL(cpu_transcoder),
  6440. (adjusted_mode->crtc_vdisplay - 1) |
  6441. ((crtc_vtotal - 1) << 16));
  6442. I915_WRITE(VBLANK(cpu_transcoder),
  6443. (adjusted_mode->crtc_vblank_start - 1) |
  6444. ((crtc_vblank_end - 1) << 16));
  6445. I915_WRITE(VSYNC(cpu_transcoder),
  6446. (adjusted_mode->crtc_vsync_start - 1) |
  6447. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6448. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6449. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6450. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6451. * bits. */
  6452. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6453. (pipe == PIPE_B || pipe == PIPE_C))
  6454. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6455. /* pipesrc controls the size that is scaled from, which should
  6456. * always be the user's requested size.
  6457. */
  6458. I915_WRITE(PIPESRC(pipe),
  6459. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6460. (intel_crtc->config->pipe_src_h - 1));
  6461. }
  6462. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6463. struct intel_crtc_state *pipe_config)
  6464. {
  6465. struct drm_device *dev = crtc->base.dev;
  6466. struct drm_i915_private *dev_priv = dev->dev_private;
  6467. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6468. uint32_t tmp;
  6469. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6470. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6471. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6472. tmp = I915_READ(HBLANK(cpu_transcoder));
  6473. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6474. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6475. tmp = I915_READ(HSYNC(cpu_transcoder));
  6476. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6477. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6478. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6479. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6480. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6481. tmp = I915_READ(VBLANK(cpu_transcoder));
  6482. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6483. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6484. tmp = I915_READ(VSYNC(cpu_transcoder));
  6485. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6486. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6487. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6488. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6489. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6490. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6491. }
  6492. tmp = I915_READ(PIPESRC(crtc->pipe));
  6493. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6494. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6495. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6496. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6497. }
  6498. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6499. struct intel_crtc_state *pipe_config)
  6500. {
  6501. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6502. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6503. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6504. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6505. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6506. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6507. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6508. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6509. mode->flags = pipe_config->base.adjusted_mode.flags;
  6510. mode->type = DRM_MODE_TYPE_DRIVER;
  6511. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6512. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6513. mode->hsync = drm_mode_hsync(mode);
  6514. mode->vrefresh = drm_mode_vrefresh(mode);
  6515. drm_mode_set_name(mode);
  6516. }
  6517. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6518. {
  6519. struct drm_device *dev = intel_crtc->base.dev;
  6520. struct drm_i915_private *dev_priv = dev->dev_private;
  6521. uint32_t pipeconf;
  6522. pipeconf = 0;
  6523. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6524. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6525. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6526. if (intel_crtc->config->double_wide)
  6527. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6528. /* only g4x and later have fancy bpc/dither controls */
  6529. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6530. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6531. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6532. pipeconf |= PIPECONF_DITHER_EN |
  6533. PIPECONF_DITHER_TYPE_SP;
  6534. switch (intel_crtc->config->pipe_bpp) {
  6535. case 18:
  6536. pipeconf |= PIPECONF_6BPC;
  6537. break;
  6538. case 24:
  6539. pipeconf |= PIPECONF_8BPC;
  6540. break;
  6541. case 30:
  6542. pipeconf |= PIPECONF_10BPC;
  6543. break;
  6544. default:
  6545. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6546. BUG();
  6547. }
  6548. }
  6549. if (HAS_PIPE_CXSR(dev)) {
  6550. if (intel_crtc->lowfreq_avail) {
  6551. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6552. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6553. } else {
  6554. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6555. }
  6556. }
  6557. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6558. if (INTEL_INFO(dev)->gen < 4 ||
  6559. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6560. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6561. else
  6562. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6563. } else
  6564. pipeconf |= PIPECONF_PROGRESSIVE;
  6565. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6566. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6567. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6568. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6569. }
  6570. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6571. struct intel_crtc_state *crtc_state)
  6572. {
  6573. struct drm_device *dev = crtc->base.dev;
  6574. struct drm_i915_private *dev_priv = dev->dev_private;
  6575. int refclk, num_connectors = 0;
  6576. intel_clock_t clock;
  6577. bool ok;
  6578. bool is_dsi = false;
  6579. struct intel_encoder *encoder;
  6580. const intel_limit_t *limit;
  6581. struct drm_atomic_state *state = crtc_state->base.state;
  6582. struct drm_connector *connector;
  6583. struct drm_connector_state *connector_state;
  6584. int i;
  6585. memset(&crtc_state->dpll_hw_state, 0,
  6586. sizeof(crtc_state->dpll_hw_state));
  6587. for_each_connector_in_state(state, connector, connector_state, i) {
  6588. if (connector_state->crtc != &crtc->base)
  6589. continue;
  6590. encoder = to_intel_encoder(connector_state->best_encoder);
  6591. switch (encoder->type) {
  6592. case INTEL_OUTPUT_DSI:
  6593. is_dsi = true;
  6594. break;
  6595. default:
  6596. break;
  6597. }
  6598. num_connectors++;
  6599. }
  6600. if (is_dsi)
  6601. return 0;
  6602. if (!crtc_state->clock_set) {
  6603. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6604. /*
  6605. * Returns a set of divisors for the desired target clock with
  6606. * the given refclk, or FALSE. The returned values represent
  6607. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6608. * 2) / p1 / p2.
  6609. */
  6610. limit = intel_limit(crtc_state, refclk);
  6611. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6612. crtc_state->port_clock,
  6613. refclk, NULL, &clock);
  6614. if (!ok) {
  6615. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6616. return -EINVAL;
  6617. }
  6618. /* Compat-code for transition, will disappear. */
  6619. crtc_state->dpll.n = clock.n;
  6620. crtc_state->dpll.m1 = clock.m1;
  6621. crtc_state->dpll.m2 = clock.m2;
  6622. crtc_state->dpll.p1 = clock.p1;
  6623. crtc_state->dpll.p2 = clock.p2;
  6624. }
  6625. if (IS_GEN2(dev)) {
  6626. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6627. num_connectors);
  6628. } else if (IS_CHERRYVIEW(dev)) {
  6629. chv_compute_dpll(crtc, crtc_state);
  6630. } else if (IS_VALLEYVIEW(dev)) {
  6631. vlv_compute_dpll(crtc, crtc_state);
  6632. } else {
  6633. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6634. num_connectors);
  6635. }
  6636. return 0;
  6637. }
  6638. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6639. struct intel_crtc_state *pipe_config)
  6640. {
  6641. struct drm_device *dev = crtc->base.dev;
  6642. struct drm_i915_private *dev_priv = dev->dev_private;
  6643. uint32_t tmp;
  6644. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6645. return;
  6646. tmp = I915_READ(PFIT_CONTROL);
  6647. if (!(tmp & PFIT_ENABLE))
  6648. return;
  6649. /* Check whether the pfit is attached to our pipe. */
  6650. if (INTEL_INFO(dev)->gen < 4) {
  6651. if (crtc->pipe != PIPE_B)
  6652. return;
  6653. } else {
  6654. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6655. return;
  6656. }
  6657. pipe_config->gmch_pfit.control = tmp;
  6658. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6659. if (INTEL_INFO(dev)->gen < 5)
  6660. pipe_config->gmch_pfit.lvds_border_bits =
  6661. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6662. }
  6663. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6664. struct intel_crtc_state *pipe_config)
  6665. {
  6666. struct drm_device *dev = crtc->base.dev;
  6667. struct drm_i915_private *dev_priv = dev->dev_private;
  6668. int pipe = pipe_config->cpu_transcoder;
  6669. intel_clock_t clock;
  6670. u32 mdiv;
  6671. int refclk = 100000;
  6672. /* In case of MIPI DPLL will not even be used */
  6673. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6674. return;
  6675. mutex_lock(&dev_priv->sb_lock);
  6676. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6677. mutex_unlock(&dev_priv->sb_lock);
  6678. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6679. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6680. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6681. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6682. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6683. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6684. }
  6685. static void
  6686. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6687. struct intel_initial_plane_config *plane_config)
  6688. {
  6689. struct drm_device *dev = crtc->base.dev;
  6690. struct drm_i915_private *dev_priv = dev->dev_private;
  6691. u32 val, base, offset;
  6692. int pipe = crtc->pipe, plane = crtc->plane;
  6693. int fourcc, pixel_format;
  6694. unsigned int aligned_height;
  6695. struct drm_framebuffer *fb;
  6696. struct intel_framebuffer *intel_fb;
  6697. val = I915_READ(DSPCNTR(plane));
  6698. if (!(val & DISPLAY_PLANE_ENABLE))
  6699. return;
  6700. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6701. if (!intel_fb) {
  6702. DRM_DEBUG_KMS("failed to alloc fb\n");
  6703. return;
  6704. }
  6705. fb = &intel_fb->base;
  6706. if (INTEL_INFO(dev)->gen >= 4) {
  6707. if (val & DISPPLANE_TILED) {
  6708. plane_config->tiling = I915_TILING_X;
  6709. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6710. }
  6711. }
  6712. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6713. fourcc = i9xx_format_to_fourcc(pixel_format);
  6714. fb->pixel_format = fourcc;
  6715. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6716. if (INTEL_INFO(dev)->gen >= 4) {
  6717. if (plane_config->tiling)
  6718. offset = I915_READ(DSPTILEOFF(plane));
  6719. else
  6720. offset = I915_READ(DSPLINOFF(plane));
  6721. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6722. } else {
  6723. base = I915_READ(DSPADDR(plane));
  6724. }
  6725. plane_config->base = base;
  6726. val = I915_READ(PIPESRC(pipe));
  6727. fb->width = ((val >> 16) & 0xfff) + 1;
  6728. fb->height = ((val >> 0) & 0xfff) + 1;
  6729. val = I915_READ(DSPSTRIDE(pipe));
  6730. fb->pitches[0] = val & 0xffffffc0;
  6731. aligned_height = intel_fb_align_height(dev, fb->height,
  6732. fb->pixel_format,
  6733. fb->modifier[0]);
  6734. plane_config->size = fb->pitches[0] * aligned_height;
  6735. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6736. pipe_name(pipe), plane, fb->width, fb->height,
  6737. fb->bits_per_pixel, base, fb->pitches[0],
  6738. plane_config->size);
  6739. plane_config->fb = intel_fb;
  6740. }
  6741. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6742. struct intel_crtc_state *pipe_config)
  6743. {
  6744. struct drm_device *dev = crtc->base.dev;
  6745. struct drm_i915_private *dev_priv = dev->dev_private;
  6746. int pipe = pipe_config->cpu_transcoder;
  6747. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6748. intel_clock_t clock;
  6749. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6750. int refclk = 100000;
  6751. mutex_lock(&dev_priv->sb_lock);
  6752. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6753. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6754. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6755. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6756. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6757. mutex_unlock(&dev_priv->sb_lock);
  6758. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6759. clock.m2 = (pll_dw0 & 0xff) << 22;
  6760. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6761. clock.m2 |= pll_dw2 & 0x3fffff;
  6762. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6763. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6764. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6765. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6766. }
  6767. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6768. struct intel_crtc_state *pipe_config)
  6769. {
  6770. struct drm_device *dev = crtc->base.dev;
  6771. struct drm_i915_private *dev_priv = dev->dev_private;
  6772. uint32_t tmp;
  6773. if (!intel_display_power_is_enabled(dev_priv,
  6774. POWER_DOMAIN_PIPE(crtc->pipe)))
  6775. return false;
  6776. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6777. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6778. tmp = I915_READ(PIPECONF(crtc->pipe));
  6779. if (!(tmp & PIPECONF_ENABLE))
  6780. return false;
  6781. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6782. switch (tmp & PIPECONF_BPC_MASK) {
  6783. case PIPECONF_6BPC:
  6784. pipe_config->pipe_bpp = 18;
  6785. break;
  6786. case PIPECONF_8BPC:
  6787. pipe_config->pipe_bpp = 24;
  6788. break;
  6789. case PIPECONF_10BPC:
  6790. pipe_config->pipe_bpp = 30;
  6791. break;
  6792. default:
  6793. break;
  6794. }
  6795. }
  6796. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6797. pipe_config->limited_color_range = true;
  6798. if (INTEL_INFO(dev)->gen < 4)
  6799. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6800. intel_get_pipe_timings(crtc, pipe_config);
  6801. i9xx_get_pfit_config(crtc, pipe_config);
  6802. if (INTEL_INFO(dev)->gen >= 4) {
  6803. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6804. pipe_config->pixel_multiplier =
  6805. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6806. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6807. pipe_config->dpll_hw_state.dpll_md = tmp;
  6808. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6809. tmp = I915_READ(DPLL(crtc->pipe));
  6810. pipe_config->pixel_multiplier =
  6811. ((tmp & SDVO_MULTIPLIER_MASK)
  6812. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6813. } else {
  6814. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6815. * port and will be fixed up in the encoder->get_config
  6816. * function. */
  6817. pipe_config->pixel_multiplier = 1;
  6818. }
  6819. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6820. if (!IS_VALLEYVIEW(dev)) {
  6821. /*
  6822. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6823. * on 830. Filter it out here so that we don't
  6824. * report errors due to that.
  6825. */
  6826. if (IS_I830(dev))
  6827. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6828. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6829. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6830. } else {
  6831. /* Mask out read-only status bits. */
  6832. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6833. DPLL_PORTC_READY_MASK |
  6834. DPLL_PORTB_READY_MASK);
  6835. }
  6836. if (IS_CHERRYVIEW(dev))
  6837. chv_crtc_clock_get(crtc, pipe_config);
  6838. else if (IS_VALLEYVIEW(dev))
  6839. vlv_crtc_clock_get(crtc, pipe_config);
  6840. else
  6841. i9xx_crtc_clock_get(crtc, pipe_config);
  6842. /*
  6843. * Normally the dotclock is filled in by the encoder .get_config()
  6844. * but in case the pipe is enabled w/o any ports we need a sane
  6845. * default.
  6846. */
  6847. pipe_config->base.adjusted_mode.crtc_clock =
  6848. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6849. return true;
  6850. }
  6851. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6852. {
  6853. struct drm_i915_private *dev_priv = dev->dev_private;
  6854. struct intel_encoder *encoder;
  6855. u32 val, final;
  6856. bool has_lvds = false;
  6857. bool has_cpu_edp = false;
  6858. bool has_panel = false;
  6859. bool has_ck505 = false;
  6860. bool can_ssc = false;
  6861. /* We need to take the global config into account */
  6862. for_each_intel_encoder(dev, encoder) {
  6863. switch (encoder->type) {
  6864. case INTEL_OUTPUT_LVDS:
  6865. has_panel = true;
  6866. has_lvds = true;
  6867. break;
  6868. case INTEL_OUTPUT_EDP:
  6869. has_panel = true;
  6870. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6871. has_cpu_edp = true;
  6872. break;
  6873. default:
  6874. break;
  6875. }
  6876. }
  6877. if (HAS_PCH_IBX(dev)) {
  6878. has_ck505 = dev_priv->vbt.display_clock_mode;
  6879. can_ssc = has_ck505;
  6880. } else {
  6881. has_ck505 = false;
  6882. can_ssc = true;
  6883. }
  6884. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6885. has_panel, has_lvds, has_ck505);
  6886. /* Ironlake: try to setup display ref clock before DPLL
  6887. * enabling. This is only under driver's control after
  6888. * PCH B stepping, previous chipset stepping should be
  6889. * ignoring this setting.
  6890. */
  6891. val = I915_READ(PCH_DREF_CONTROL);
  6892. /* As we must carefully and slowly disable/enable each source in turn,
  6893. * compute the final state we want first and check if we need to
  6894. * make any changes at all.
  6895. */
  6896. final = val;
  6897. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6898. if (has_ck505)
  6899. final |= DREF_NONSPREAD_CK505_ENABLE;
  6900. else
  6901. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6902. final &= ~DREF_SSC_SOURCE_MASK;
  6903. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6904. final &= ~DREF_SSC1_ENABLE;
  6905. if (has_panel) {
  6906. final |= DREF_SSC_SOURCE_ENABLE;
  6907. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6908. final |= DREF_SSC1_ENABLE;
  6909. if (has_cpu_edp) {
  6910. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6911. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6912. else
  6913. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6914. } else
  6915. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6916. } else {
  6917. final |= DREF_SSC_SOURCE_DISABLE;
  6918. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6919. }
  6920. if (final == val)
  6921. return;
  6922. /* Always enable nonspread source */
  6923. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6924. if (has_ck505)
  6925. val |= DREF_NONSPREAD_CK505_ENABLE;
  6926. else
  6927. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6928. if (has_panel) {
  6929. val &= ~DREF_SSC_SOURCE_MASK;
  6930. val |= DREF_SSC_SOURCE_ENABLE;
  6931. /* SSC must be turned on before enabling the CPU output */
  6932. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6933. DRM_DEBUG_KMS("Using SSC on panel\n");
  6934. val |= DREF_SSC1_ENABLE;
  6935. } else
  6936. val &= ~DREF_SSC1_ENABLE;
  6937. /* Get SSC going before enabling the outputs */
  6938. I915_WRITE(PCH_DREF_CONTROL, val);
  6939. POSTING_READ(PCH_DREF_CONTROL);
  6940. udelay(200);
  6941. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6942. /* Enable CPU source on CPU attached eDP */
  6943. if (has_cpu_edp) {
  6944. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6945. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6946. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6947. } else
  6948. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6949. } else
  6950. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6951. I915_WRITE(PCH_DREF_CONTROL, val);
  6952. POSTING_READ(PCH_DREF_CONTROL);
  6953. udelay(200);
  6954. } else {
  6955. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6956. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6957. /* Turn off CPU output */
  6958. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6959. I915_WRITE(PCH_DREF_CONTROL, val);
  6960. POSTING_READ(PCH_DREF_CONTROL);
  6961. udelay(200);
  6962. /* Turn off the SSC source */
  6963. val &= ~DREF_SSC_SOURCE_MASK;
  6964. val |= DREF_SSC_SOURCE_DISABLE;
  6965. /* Turn off SSC1 */
  6966. val &= ~DREF_SSC1_ENABLE;
  6967. I915_WRITE(PCH_DREF_CONTROL, val);
  6968. POSTING_READ(PCH_DREF_CONTROL);
  6969. udelay(200);
  6970. }
  6971. BUG_ON(val != final);
  6972. }
  6973. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6974. {
  6975. uint32_t tmp;
  6976. tmp = I915_READ(SOUTH_CHICKEN2);
  6977. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6978. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6979. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6980. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6981. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6982. tmp = I915_READ(SOUTH_CHICKEN2);
  6983. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6984. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6985. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6986. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6987. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6988. }
  6989. /* WaMPhyProgramming:hsw */
  6990. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6991. {
  6992. uint32_t tmp;
  6993. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6994. tmp &= ~(0xFF << 24);
  6995. tmp |= (0x12 << 24);
  6996. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6997. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6998. tmp |= (1 << 11);
  6999. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7000. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7001. tmp |= (1 << 11);
  7002. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7003. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7004. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7005. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7006. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7007. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7008. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7009. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7010. tmp &= ~(7 << 13);
  7011. tmp |= (5 << 13);
  7012. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7013. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7014. tmp &= ~(7 << 13);
  7015. tmp |= (5 << 13);
  7016. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7017. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7018. tmp &= ~0xFF;
  7019. tmp |= 0x1C;
  7020. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7021. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7022. tmp &= ~0xFF;
  7023. tmp |= 0x1C;
  7024. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7025. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7026. tmp &= ~(0xFF << 16);
  7027. tmp |= (0x1C << 16);
  7028. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7029. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7030. tmp &= ~(0xFF << 16);
  7031. tmp |= (0x1C << 16);
  7032. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7033. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7034. tmp |= (1 << 27);
  7035. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7036. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7037. tmp |= (1 << 27);
  7038. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7039. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7040. tmp &= ~(0xF << 28);
  7041. tmp |= (4 << 28);
  7042. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7043. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7044. tmp &= ~(0xF << 28);
  7045. tmp |= (4 << 28);
  7046. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7047. }
  7048. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7049. * Programming" based on the parameters passed:
  7050. * - Sequence to enable CLKOUT_DP
  7051. * - Sequence to enable CLKOUT_DP without spread
  7052. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7053. */
  7054. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7055. bool with_fdi)
  7056. {
  7057. struct drm_i915_private *dev_priv = dev->dev_private;
  7058. uint32_t reg, tmp;
  7059. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7060. with_spread = true;
  7061. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7062. with_fdi = false;
  7063. mutex_lock(&dev_priv->sb_lock);
  7064. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7065. tmp &= ~SBI_SSCCTL_DISABLE;
  7066. tmp |= SBI_SSCCTL_PATHALT;
  7067. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7068. udelay(24);
  7069. if (with_spread) {
  7070. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7071. tmp &= ~SBI_SSCCTL_PATHALT;
  7072. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7073. if (with_fdi) {
  7074. lpt_reset_fdi_mphy(dev_priv);
  7075. lpt_program_fdi_mphy(dev_priv);
  7076. }
  7077. }
  7078. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7079. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7080. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7081. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7082. mutex_unlock(&dev_priv->sb_lock);
  7083. }
  7084. /* Sequence to disable CLKOUT_DP */
  7085. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7086. {
  7087. struct drm_i915_private *dev_priv = dev->dev_private;
  7088. uint32_t reg, tmp;
  7089. mutex_lock(&dev_priv->sb_lock);
  7090. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7091. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7092. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7093. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7094. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7095. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7096. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7097. tmp |= SBI_SSCCTL_PATHALT;
  7098. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7099. udelay(32);
  7100. }
  7101. tmp |= SBI_SSCCTL_DISABLE;
  7102. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7103. }
  7104. mutex_unlock(&dev_priv->sb_lock);
  7105. }
  7106. static void lpt_init_pch_refclk(struct drm_device *dev)
  7107. {
  7108. struct intel_encoder *encoder;
  7109. bool has_vga = false;
  7110. for_each_intel_encoder(dev, encoder) {
  7111. switch (encoder->type) {
  7112. case INTEL_OUTPUT_ANALOG:
  7113. has_vga = true;
  7114. break;
  7115. default:
  7116. break;
  7117. }
  7118. }
  7119. if (has_vga)
  7120. lpt_enable_clkout_dp(dev, true, true);
  7121. else
  7122. lpt_disable_clkout_dp(dev);
  7123. }
  7124. /*
  7125. * Initialize reference clocks when the driver loads
  7126. */
  7127. void intel_init_pch_refclk(struct drm_device *dev)
  7128. {
  7129. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7130. ironlake_init_pch_refclk(dev);
  7131. else if (HAS_PCH_LPT(dev))
  7132. lpt_init_pch_refclk(dev);
  7133. }
  7134. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7135. {
  7136. struct drm_device *dev = crtc_state->base.crtc->dev;
  7137. struct drm_i915_private *dev_priv = dev->dev_private;
  7138. struct drm_atomic_state *state = crtc_state->base.state;
  7139. struct drm_connector *connector;
  7140. struct drm_connector_state *connector_state;
  7141. struct intel_encoder *encoder;
  7142. int num_connectors = 0, i;
  7143. bool is_lvds = false;
  7144. for_each_connector_in_state(state, connector, connector_state, i) {
  7145. if (connector_state->crtc != crtc_state->base.crtc)
  7146. continue;
  7147. encoder = to_intel_encoder(connector_state->best_encoder);
  7148. switch (encoder->type) {
  7149. case INTEL_OUTPUT_LVDS:
  7150. is_lvds = true;
  7151. break;
  7152. default:
  7153. break;
  7154. }
  7155. num_connectors++;
  7156. }
  7157. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7158. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7159. dev_priv->vbt.lvds_ssc_freq);
  7160. return dev_priv->vbt.lvds_ssc_freq;
  7161. }
  7162. return 120000;
  7163. }
  7164. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7165. {
  7166. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7167. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7168. int pipe = intel_crtc->pipe;
  7169. uint32_t val;
  7170. val = 0;
  7171. switch (intel_crtc->config->pipe_bpp) {
  7172. case 18:
  7173. val |= PIPECONF_6BPC;
  7174. break;
  7175. case 24:
  7176. val |= PIPECONF_8BPC;
  7177. break;
  7178. case 30:
  7179. val |= PIPECONF_10BPC;
  7180. break;
  7181. case 36:
  7182. val |= PIPECONF_12BPC;
  7183. break;
  7184. default:
  7185. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7186. BUG();
  7187. }
  7188. if (intel_crtc->config->dither)
  7189. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7190. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7191. val |= PIPECONF_INTERLACED_ILK;
  7192. else
  7193. val |= PIPECONF_PROGRESSIVE;
  7194. if (intel_crtc->config->limited_color_range)
  7195. val |= PIPECONF_COLOR_RANGE_SELECT;
  7196. I915_WRITE(PIPECONF(pipe), val);
  7197. POSTING_READ(PIPECONF(pipe));
  7198. }
  7199. /*
  7200. * Set up the pipe CSC unit.
  7201. *
  7202. * Currently only full range RGB to limited range RGB conversion
  7203. * is supported, but eventually this should handle various
  7204. * RGB<->YCbCr scenarios as well.
  7205. */
  7206. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7207. {
  7208. struct drm_device *dev = crtc->dev;
  7209. struct drm_i915_private *dev_priv = dev->dev_private;
  7210. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7211. int pipe = intel_crtc->pipe;
  7212. uint16_t coeff = 0x7800; /* 1.0 */
  7213. /*
  7214. * TODO: Check what kind of values actually come out of the pipe
  7215. * with these coeff/postoff values and adjust to get the best
  7216. * accuracy. Perhaps we even need to take the bpc value into
  7217. * consideration.
  7218. */
  7219. if (intel_crtc->config->limited_color_range)
  7220. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7221. /*
  7222. * GY/GU and RY/RU should be the other way around according
  7223. * to BSpec, but reality doesn't agree. Just set them up in
  7224. * a way that results in the correct picture.
  7225. */
  7226. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7227. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7228. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7229. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7230. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7231. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7232. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7233. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7234. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7235. if (INTEL_INFO(dev)->gen > 6) {
  7236. uint16_t postoff = 0;
  7237. if (intel_crtc->config->limited_color_range)
  7238. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7239. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7240. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7241. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7242. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7243. } else {
  7244. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7245. if (intel_crtc->config->limited_color_range)
  7246. mode |= CSC_BLACK_SCREEN_OFFSET;
  7247. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7248. }
  7249. }
  7250. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7251. {
  7252. struct drm_device *dev = crtc->dev;
  7253. struct drm_i915_private *dev_priv = dev->dev_private;
  7254. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7255. enum pipe pipe = intel_crtc->pipe;
  7256. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7257. uint32_t val;
  7258. val = 0;
  7259. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7260. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7261. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7262. val |= PIPECONF_INTERLACED_ILK;
  7263. else
  7264. val |= PIPECONF_PROGRESSIVE;
  7265. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7266. POSTING_READ(PIPECONF(cpu_transcoder));
  7267. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7268. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7269. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7270. val = 0;
  7271. switch (intel_crtc->config->pipe_bpp) {
  7272. case 18:
  7273. val |= PIPEMISC_DITHER_6_BPC;
  7274. break;
  7275. case 24:
  7276. val |= PIPEMISC_DITHER_8_BPC;
  7277. break;
  7278. case 30:
  7279. val |= PIPEMISC_DITHER_10_BPC;
  7280. break;
  7281. case 36:
  7282. val |= PIPEMISC_DITHER_12_BPC;
  7283. break;
  7284. default:
  7285. /* Case prevented by pipe_config_set_bpp. */
  7286. BUG();
  7287. }
  7288. if (intel_crtc->config->dither)
  7289. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7290. I915_WRITE(PIPEMISC(pipe), val);
  7291. }
  7292. }
  7293. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7294. struct intel_crtc_state *crtc_state,
  7295. intel_clock_t *clock,
  7296. bool *has_reduced_clock,
  7297. intel_clock_t *reduced_clock)
  7298. {
  7299. struct drm_device *dev = crtc->dev;
  7300. struct drm_i915_private *dev_priv = dev->dev_private;
  7301. int refclk;
  7302. const intel_limit_t *limit;
  7303. bool ret;
  7304. refclk = ironlake_get_refclk(crtc_state);
  7305. /*
  7306. * Returns a set of divisors for the desired target clock with the given
  7307. * refclk, or FALSE. The returned values represent the clock equation:
  7308. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7309. */
  7310. limit = intel_limit(crtc_state, refclk);
  7311. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7312. crtc_state->port_clock,
  7313. refclk, NULL, clock);
  7314. if (!ret)
  7315. return false;
  7316. return true;
  7317. }
  7318. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7319. {
  7320. /*
  7321. * Account for spread spectrum to avoid
  7322. * oversubscribing the link. Max center spread
  7323. * is 2.5%; use 5% for safety's sake.
  7324. */
  7325. u32 bps = target_clock * bpp * 21 / 20;
  7326. return DIV_ROUND_UP(bps, link_bw * 8);
  7327. }
  7328. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7329. {
  7330. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7331. }
  7332. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7333. struct intel_crtc_state *crtc_state,
  7334. u32 *fp,
  7335. intel_clock_t *reduced_clock, u32 *fp2)
  7336. {
  7337. struct drm_crtc *crtc = &intel_crtc->base;
  7338. struct drm_device *dev = crtc->dev;
  7339. struct drm_i915_private *dev_priv = dev->dev_private;
  7340. struct drm_atomic_state *state = crtc_state->base.state;
  7341. struct drm_connector *connector;
  7342. struct drm_connector_state *connector_state;
  7343. struct intel_encoder *encoder;
  7344. uint32_t dpll;
  7345. int factor, num_connectors = 0, i;
  7346. bool is_lvds = false, is_sdvo = false;
  7347. for_each_connector_in_state(state, connector, connector_state, i) {
  7348. if (connector_state->crtc != crtc_state->base.crtc)
  7349. continue;
  7350. encoder = to_intel_encoder(connector_state->best_encoder);
  7351. switch (encoder->type) {
  7352. case INTEL_OUTPUT_LVDS:
  7353. is_lvds = true;
  7354. break;
  7355. case INTEL_OUTPUT_SDVO:
  7356. case INTEL_OUTPUT_HDMI:
  7357. is_sdvo = true;
  7358. break;
  7359. default:
  7360. break;
  7361. }
  7362. num_connectors++;
  7363. }
  7364. /* Enable autotuning of the PLL clock (if permissible) */
  7365. factor = 21;
  7366. if (is_lvds) {
  7367. if ((intel_panel_use_ssc(dev_priv) &&
  7368. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7369. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7370. factor = 25;
  7371. } else if (crtc_state->sdvo_tv_clock)
  7372. factor = 20;
  7373. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7374. *fp |= FP_CB_TUNE;
  7375. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7376. *fp2 |= FP_CB_TUNE;
  7377. dpll = 0;
  7378. if (is_lvds)
  7379. dpll |= DPLLB_MODE_LVDS;
  7380. else
  7381. dpll |= DPLLB_MODE_DAC_SERIAL;
  7382. dpll |= (crtc_state->pixel_multiplier - 1)
  7383. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7384. if (is_sdvo)
  7385. dpll |= DPLL_SDVO_HIGH_SPEED;
  7386. if (crtc_state->has_dp_encoder)
  7387. dpll |= DPLL_SDVO_HIGH_SPEED;
  7388. /* compute bitmask from p1 value */
  7389. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7390. /* also FPA1 */
  7391. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7392. switch (crtc_state->dpll.p2) {
  7393. case 5:
  7394. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7395. break;
  7396. case 7:
  7397. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7398. break;
  7399. case 10:
  7400. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7401. break;
  7402. case 14:
  7403. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7404. break;
  7405. }
  7406. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7407. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7408. else
  7409. dpll |= PLL_REF_INPUT_DREFCLK;
  7410. return dpll | DPLL_VCO_ENABLE;
  7411. }
  7412. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7413. struct intel_crtc_state *crtc_state)
  7414. {
  7415. struct drm_device *dev = crtc->base.dev;
  7416. intel_clock_t clock, reduced_clock;
  7417. u32 dpll = 0, fp = 0, fp2 = 0;
  7418. bool ok, has_reduced_clock = false;
  7419. bool is_lvds = false;
  7420. struct intel_shared_dpll *pll;
  7421. memset(&crtc_state->dpll_hw_state, 0,
  7422. sizeof(crtc_state->dpll_hw_state));
  7423. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7424. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7425. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7426. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7427. &has_reduced_clock, &reduced_clock);
  7428. if (!ok && !crtc_state->clock_set) {
  7429. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7430. return -EINVAL;
  7431. }
  7432. /* Compat-code for transition, will disappear. */
  7433. if (!crtc_state->clock_set) {
  7434. crtc_state->dpll.n = clock.n;
  7435. crtc_state->dpll.m1 = clock.m1;
  7436. crtc_state->dpll.m2 = clock.m2;
  7437. crtc_state->dpll.p1 = clock.p1;
  7438. crtc_state->dpll.p2 = clock.p2;
  7439. }
  7440. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7441. if (crtc_state->has_pch_encoder) {
  7442. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7443. if (has_reduced_clock)
  7444. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7445. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7446. &fp, &reduced_clock,
  7447. has_reduced_clock ? &fp2 : NULL);
  7448. crtc_state->dpll_hw_state.dpll = dpll;
  7449. crtc_state->dpll_hw_state.fp0 = fp;
  7450. if (has_reduced_clock)
  7451. crtc_state->dpll_hw_state.fp1 = fp2;
  7452. else
  7453. crtc_state->dpll_hw_state.fp1 = fp;
  7454. pll = intel_get_shared_dpll(crtc, crtc_state);
  7455. if (pll == NULL) {
  7456. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7457. pipe_name(crtc->pipe));
  7458. return -EINVAL;
  7459. }
  7460. }
  7461. if (is_lvds && has_reduced_clock)
  7462. crtc->lowfreq_avail = true;
  7463. else
  7464. crtc->lowfreq_avail = false;
  7465. return 0;
  7466. }
  7467. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7468. struct intel_link_m_n *m_n)
  7469. {
  7470. struct drm_device *dev = crtc->base.dev;
  7471. struct drm_i915_private *dev_priv = dev->dev_private;
  7472. enum pipe pipe = crtc->pipe;
  7473. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7474. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7475. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7476. & ~TU_SIZE_MASK;
  7477. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7478. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7479. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7480. }
  7481. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7482. enum transcoder transcoder,
  7483. struct intel_link_m_n *m_n,
  7484. struct intel_link_m_n *m2_n2)
  7485. {
  7486. struct drm_device *dev = crtc->base.dev;
  7487. struct drm_i915_private *dev_priv = dev->dev_private;
  7488. enum pipe pipe = crtc->pipe;
  7489. if (INTEL_INFO(dev)->gen >= 5) {
  7490. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7491. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7492. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7493. & ~TU_SIZE_MASK;
  7494. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7495. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7496. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7497. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7498. * gen < 8) and if DRRS is supported (to make sure the
  7499. * registers are not unnecessarily read).
  7500. */
  7501. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7502. crtc->config->has_drrs) {
  7503. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7504. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7505. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7506. & ~TU_SIZE_MASK;
  7507. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7508. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7509. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7510. }
  7511. } else {
  7512. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7513. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7514. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7515. & ~TU_SIZE_MASK;
  7516. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7517. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7518. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7519. }
  7520. }
  7521. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7522. struct intel_crtc_state *pipe_config)
  7523. {
  7524. if (pipe_config->has_pch_encoder)
  7525. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7526. else
  7527. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7528. &pipe_config->dp_m_n,
  7529. &pipe_config->dp_m2_n2);
  7530. }
  7531. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7532. struct intel_crtc_state *pipe_config)
  7533. {
  7534. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7535. &pipe_config->fdi_m_n, NULL);
  7536. }
  7537. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7538. struct intel_crtc_state *pipe_config)
  7539. {
  7540. struct drm_device *dev = crtc->base.dev;
  7541. struct drm_i915_private *dev_priv = dev->dev_private;
  7542. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7543. uint32_t ps_ctrl = 0;
  7544. int id = -1;
  7545. int i;
  7546. /* find scaler attached to this pipe */
  7547. for (i = 0; i < crtc->num_scalers; i++) {
  7548. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7549. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7550. id = i;
  7551. pipe_config->pch_pfit.enabled = true;
  7552. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7553. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7554. break;
  7555. }
  7556. }
  7557. scaler_state->scaler_id = id;
  7558. if (id >= 0) {
  7559. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7560. } else {
  7561. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7562. }
  7563. }
  7564. static void
  7565. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7566. struct intel_initial_plane_config *plane_config)
  7567. {
  7568. struct drm_device *dev = crtc->base.dev;
  7569. struct drm_i915_private *dev_priv = dev->dev_private;
  7570. u32 val, base, offset, stride_mult, tiling;
  7571. int pipe = crtc->pipe;
  7572. int fourcc, pixel_format;
  7573. unsigned int aligned_height;
  7574. struct drm_framebuffer *fb;
  7575. struct intel_framebuffer *intel_fb;
  7576. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7577. if (!intel_fb) {
  7578. DRM_DEBUG_KMS("failed to alloc fb\n");
  7579. return;
  7580. }
  7581. fb = &intel_fb->base;
  7582. val = I915_READ(PLANE_CTL(pipe, 0));
  7583. if (!(val & PLANE_CTL_ENABLE))
  7584. goto error;
  7585. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7586. fourcc = skl_format_to_fourcc(pixel_format,
  7587. val & PLANE_CTL_ORDER_RGBX,
  7588. val & PLANE_CTL_ALPHA_MASK);
  7589. fb->pixel_format = fourcc;
  7590. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7591. tiling = val & PLANE_CTL_TILED_MASK;
  7592. switch (tiling) {
  7593. case PLANE_CTL_TILED_LINEAR:
  7594. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7595. break;
  7596. case PLANE_CTL_TILED_X:
  7597. plane_config->tiling = I915_TILING_X;
  7598. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7599. break;
  7600. case PLANE_CTL_TILED_Y:
  7601. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7602. break;
  7603. case PLANE_CTL_TILED_YF:
  7604. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7605. break;
  7606. default:
  7607. MISSING_CASE(tiling);
  7608. goto error;
  7609. }
  7610. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7611. plane_config->base = base;
  7612. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7613. val = I915_READ(PLANE_SIZE(pipe, 0));
  7614. fb->height = ((val >> 16) & 0xfff) + 1;
  7615. fb->width = ((val >> 0) & 0x1fff) + 1;
  7616. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7617. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7618. fb->pixel_format);
  7619. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7620. aligned_height = intel_fb_align_height(dev, fb->height,
  7621. fb->pixel_format,
  7622. fb->modifier[0]);
  7623. plane_config->size = fb->pitches[0] * aligned_height;
  7624. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7625. pipe_name(pipe), fb->width, fb->height,
  7626. fb->bits_per_pixel, base, fb->pitches[0],
  7627. plane_config->size);
  7628. plane_config->fb = intel_fb;
  7629. return;
  7630. error:
  7631. kfree(fb);
  7632. }
  7633. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7634. struct intel_crtc_state *pipe_config)
  7635. {
  7636. struct drm_device *dev = crtc->base.dev;
  7637. struct drm_i915_private *dev_priv = dev->dev_private;
  7638. uint32_t tmp;
  7639. tmp = I915_READ(PF_CTL(crtc->pipe));
  7640. if (tmp & PF_ENABLE) {
  7641. pipe_config->pch_pfit.enabled = true;
  7642. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7643. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7644. /* We currently do not free assignements of panel fitters on
  7645. * ivb/hsw (since we don't use the higher upscaling modes which
  7646. * differentiates them) so just WARN about this case for now. */
  7647. if (IS_GEN7(dev)) {
  7648. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7649. PF_PIPE_SEL_IVB(crtc->pipe));
  7650. }
  7651. }
  7652. }
  7653. static void
  7654. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7655. struct intel_initial_plane_config *plane_config)
  7656. {
  7657. struct drm_device *dev = crtc->base.dev;
  7658. struct drm_i915_private *dev_priv = dev->dev_private;
  7659. u32 val, base, offset;
  7660. int pipe = crtc->pipe;
  7661. int fourcc, pixel_format;
  7662. unsigned int aligned_height;
  7663. struct drm_framebuffer *fb;
  7664. struct intel_framebuffer *intel_fb;
  7665. val = I915_READ(DSPCNTR(pipe));
  7666. if (!(val & DISPLAY_PLANE_ENABLE))
  7667. return;
  7668. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7669. if (!intel_fb) {
  7670. DRM_DEBUG_KMS("failed to alloc fb\n");
  7671. return;
  7672. }
  7673. fb = &intel_fb->base;
  7674. if (INTEL_INFO(dev)->gen >= 4) {
  7675. if (val & DISPPLANE_TILED) {
  7676. plane_config->tiling = I915_TILING_X;
  7677. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7678. }
  7679. }
  7680. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7681. fourcc = i9xx_format_to_fourcc(pixel_format);
  7682. fb->pixel_format = fourcc;
  7683. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7684. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7685. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7686. offset = I915_READ(DSPOFFSET(pipe));
  7687. } else {
  7688. if (plane_config->tiling)
  7689. offset = I915_READ(DSPTILEOFF(pipe));
  7690. else
  7691. offset = I915_READ(DSPLINOFF(pipe));
  7692. }
  7693. plane_config->base = base;
  7694. val = I915_READ(PIPESRC(pipe));
  7695. fb->width = ((val >> 16) & 0xfff) + 1;
  7696. fb->height = ((val >> 0) & 0xfff) + 1;
  7697. val = I915_READ(DSPSTRIDE(pipe));
  7698. fb->pitches[0] = val & 0xffffffc0;
  7699. aligned_height = intel_fb_align_height(dev, fb->height,
  7700. fb->pixel_format,
  7701. fb->modifier[0]);
  7702. plane_config->size = fb->pitches[0] * aligned_height;
  7703. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7704. pipe_name(pipe), fb->width, fb->height,
  7705. fb->bits_per_pixel, base, fb->pitches[0],
  7706. plane_config->size);
  7707. plane_config->fb = intel_fb;
  7708. }
  7709. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7710. struct intel_crtc_state *pipe_config)
  7711. {
  7712. struct drm_device *dev = crtc->base.dev;
  7713. struct drm_i915_private *dev_priv = dev->dev_private;
  7714. uint32_t tmp;
  7715. if (!intel_display_power_is_enabled(dev_priv,
  7716. POWER_DOMAIN_PIPE(crtc->pipe)))
  7717. return false;
  7718. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7719. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7720. tmp = I915_READ(PIPECONF(crtc->pipe));
  7721. if (!(tmp & PIPECONF_ENABLE))
  7722. return false;
  7723. switch (tmp & PIPECONF_BPC_MASK) {
  7724. case PIPECONF_6BPC:
  7725. pipe_config->pipe_bpp = 18;
  7726. break;
  7727. case PIPECONF_8BPC:
  7728. pipe_config->pipe_bpp = 24;
  7729. break;
  7730. case PIPECONF_10BPC:
  7731. pipe_config->pipe_bpp = 30;
  7732. break;
  7733. case PIPECONF_12BPC:
  7734. pipe_config->pipe_bpp = 36;
  7735. break;
  7736. default:
  7737. break;
  7738. }
  7739. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7740. pipe_config->limited_color_range = true;
  7741. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7742. struct intel_shared_dpll *pll;
  7743. pipe_config->has_pch_encoder = true;
  7744. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7745. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7746. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7747. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7748. if (HAS_PCH_IBX(dev_priv->dev)) {
  7749. pipe_config->shared_dpll =
  7750. (enum intel_dpll_id) crtc->pipe;
  7751. } else {
  7752. tmp = I915_READ(PCH_DPLL_SEL);
  7753. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7754. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7755. else
  7756. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7757. }
  7758. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7759. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7760. &pipe_config->dpll_hw_state));
  7761. tmp = pipe_config->dpll_hw_state.dpll;
  7762. pipe_config->pixel_multiplier =
  7763. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7764. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7765. ironlake_pch_clock_get(crtc, pipe_config);
  7766. } else {
  7767. pipe_config->pixel_multiplier = 1;
  7768. }
  7769. intel_get_pipe_timings(crtc, pipe_config);
  7770. ironlake_get_pfit_config(crtc, pipe_config);
  7771. return true;
  7772. }
  7773. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7774. {
  7775. struct drm_device *dev = dev_priv->dev;
  7776. struct intel_crtc *crtc;
  7777. for_each_intel_crtc(dev, crtc)
  7778. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7779. pipe_name(crtc->pipe));
  7780. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7781. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7782. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7783. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7784. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7785. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7786. "CPU PWM1 enabled\n");
  7787. if (IS_HASWELL(dev))
  7788. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7789. "CPU PWM2 enabled\n");
  7790. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7791. "PCH PWM1 enabled\n");
  7792. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7793. "Utility pin enabled\n");
  7794. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7795. /*
  7796. * In theory we can still leave IRQs enabled, as long as only the HPD
  7797. * interrupts remain enabled. We used to check for that, but since it's
  7798. * gen-specific and since we only disable LCPLL after we fully disable
  7799. * the interrupts, the check below should be enough.
  7800. */
  7801. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7802. }
  7803. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7804. {
  7805. struct drm_device *dev = dev_priv->dev;
  7806. if (IS_HASWELL(dev))
  7807. return I915_READ(D_COMP_HSW);
  7808. else
  7809. return I915_READ(D_COMP_BDW);
  7810. }
  7811. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7812. {
  7813. struct drm_device *dev = dev_priv->dev;
  7814. if (IS_HASWELL(dev)) {
  7815. mutex_lock(&dev_priv->rps.hw_lock);
  7816. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7817. val))
  7818. DRM_ERROR("Failed to write to D_COMP\n");
  7819. mutex_unlock(&dev_priv->rps.hw_lock);
  7820. } else {
  7821. I915_WRITE(D_COMP_BDW, val);
  7822. POSTING_READ(D_COMP_BDW);
  7823. }
  7824. }
  7825. /*
  7826. * This function implements pieces of two sequences from BSpec:
  7827. * - Sequence for display software to disable LCPLL
  7828. * - Sequence for display software to allow package C8+
  7829. * The steps implemented here are just the steps that actually touch the LCPLL
  7830. * register. Callers should take care of disabling all the display engine
  7831. * functions, doing the mode unset, fixing interrupts, etc.
  7832. */
  7833. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7834. bool switch_to_fclk, bool allow_power_down)
  7835. {
  7836. uint32_t val;
  7837. assert_can_disable_lcpll(dev_priv);
  7838. val = I915_READ(LCPLL_CTL);
  7839. if (switch_to_fclk) {
  7840. val |= LCPLL_CD_SOURCE_FCLK;
  7841. I915_WRITE(LCPLL_CTL, val);
  7842. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7843. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7844. DRM_ERROR("Switching to FCLK failed\n");
  7845. val = I915_READ(LCPLL_CTL);
  7846. }
  7847. val |= LCPLL_PLL_DISABLE;
  7848. I915_WRITE(LCPLL_CTL, val);
  7849. POSTING_READ(LCPLL_CTL);
  7850. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7851. DRM_ERROR("LCPLL still locked\n");
  7852. val = hsw_read_dcomp(dev_priv);
  7853. val |= D_COMP_COMP_DISABLE;
  7854. hsw_write_dcomp(dev_priv, val);
  7855. ndelay(100);
  7856. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7857. 1))
  7858. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7859. if (allow_power_down) {
  7860. val = I915_READ(LCPLL_CTL);
  7861. val |= LCPLL_POWER_DOWN_ALLOW;
  7862. I915_WRITE(LCPLL_CTL, val);
  7863. POSTING_READ(LCPLL_CTL);
  7864. }
  7865. }
  7866. /*
  7867. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7868. * source.
  7869. */
  7870. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7871. {
  7872. uint32_t val;
  7873. val = I915_READ(LCPLL_CTL);
  7874. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7875. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7876. return;
  7877. /*
  7878. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7879. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7880. */
  7881. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7882. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7883. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7884. I915_WRITE(LCPLL_CTL, val);
  7885. POSTING_READ(LCPLL_CTL);
  7886. }
  7887. val = hsw_read_dcomp(dev_priv);
  7888. val |= D_COMP_COMP_FORCE;
  7889. val &= ~D_COMP_COMP_DISABLE;
  7890. hsw_write_dcomp(dev_priv, val);
  7891. val = I915_READ(LCPLL_CTL);
  7892. val &= ~LCPLL_PLL_DISABLE;
  7893. I915_WRITE(LCPLL_CTL, val);
  7894. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7895. DRM_ERROR("LCPLL not locked yet\n");
  7896. if (val & LCPLL_CD_SOURCE_FCLK) {
  7897. val = I915_READ(LCPLL_CTL);
  7898. val &= ~LCPLL_CD_SOURCE_FCLK;
  7899. I915_WRITE(LCPLL_CTL, val);
  7900. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7901. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7902. DRM_ERROR("Switching back to LCPLL failed\n");
  7903. }
  7904. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7905. intel_update_cdclk(dev_priv->dev);
  7906. }
  7907. /*
  7908. * Package states C8 and deeper are really deep PC states that can only be
  7909. * reached when all the devices on the system allow it, so even if the graphics
  7910. * device allows PC8+, it doesn't mean the system will actually get to these
  7911. * states. Our driver only allows PC8+ when going into runtime PM.
  7912. *
  7913. * The requirements for PC8+ are that all the outputs are disabled, the power
  7914. * well is disabled and most interrupts are disabled, and these are also
  7915. * requirements for runtime PM. When these conditions are met, we manually do
  7916. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7917. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7918. * hang the machine.
  7919. *
  7920. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7921. * the state of some registers, so when we come back from PC8+ we need to
  7922. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7923. * need to take care of the registers kept by RC6. Notice that this happens even
  7924. * if we don't put the device in PCI D3 state (which is what currently happens
  7925. * because of the runtime PM support).
  7926. *
  7927. * For more, read "Display Sequences for Package C8" on the hardware
  7928. * documentation.
  7929. */
  7930. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7931. {
  7932. struct drm_device *dev = dev_priv->dev;
  7933. uint32_t val;
  7934. DRM_DEBUG_KMS("Enabling package C8+\n");
  7935. if (HAS_PCH_LPT_LP(dev)) {
  7936. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7937. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7938. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7939. }
  7940. lpt_disable_clkout_dp(dev);
  7941. hsw_disable_lcpll(dev_priv, true, true);
  7942. }
  7943. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7944. {
  7945. struct drm_device *dev = dev_priv->dev;
  7946. uint32_t val;
  7947. DRM_DEBUG_KMS("Disabling package C8+\n");
  7948. hsw_restore_lcpll(dev_priv);
  7949. lpt_init_pch_refclk(dev);
  7950. if (HAS_PCH_LPT_LP(dev)) {
  7951. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7952. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7953. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7954. }
  7955. intel_prepare_ddi(dev);
  7956. }
  7957. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  7958. {
  7959. struct drm_device *dev = old_state->dev;
  7960. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  7961. broxton_set_cdclk(dev, req_cdclk);
  7962. }
  7963. /* compute the max rate for new configuration */
  7964. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  7965. {
  7966. struct intel_crtc *intel_crtc;
  7967. struct intel_crtc_state *crtc_state;
  7968. int max_pixel_rate = 0;
  7969. for_each_intel_crtc(state->dev, intel_crtc) {
  7970. int pixel_rate;
  7971. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  7972. if (IS_ERR(crtc_state))
  7973. return PTR_ERR(crtc_state);
  7974. if (!crtc_state->base.enable)
  7975. continue;
  7976. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  7977. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  7978. if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
  7979. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  7980. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  7981. }
  7982. return max_pixel_rate;
  7983. }
  7984. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  7985. {
  7986. struct drm_i915_private *dev_priv = dev->dev_private;
  7987. uint32_t val, data;
  7988. int ret;
  7989. if (WARN((I915_READ(LCPLL_CTL) &
  7990. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  7991. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  7992. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  7993. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  7994. "trying to change cdclk frequency with cdclk not enabled\n"))
  7995. return;
  7996. mutex_lock(&dev_priv->rps.hw_lock);
  7997. ret = sandybridge_pcode_write(dev_priv,
  7998. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  7999. mutex_unlock(&dev_priv->rps.hw_lock);
  8000. if (ret) {
  8001. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8002. return;
  8003. }
  8004. val = I915_READ(LCPLL_CTL);
  8005. val |= LCPLL_CD_SOURCE_FCLK;
  8006. I915_WRITE(LCPLL_CTL, val);
  8007. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  8008. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8009. DRM_ERROR("Switching to FCLK failed\n");
  8010. val = I915_READ(LCPLL_CTL);
  8011. val &= ~LCPLL_CLK_FREQ_MASK;
  8012. switch (cdclk) {
  8013. case 450000:
  8014. val |= LCPLL_CLK_FREQ_450;
  8015. data = 0;
  8016. break;
  8017. case 540000:
  8018. val |= LCPLL_CLK_FREQ_54O_BDW;
  8019. data = 1;
  8020. break;
  8021. case 337500:
  8022. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8023. data = 2;
  8024. break;
  8025. case 675000:
  8026. val |= LCPLL_CLK_FREQ_675_BDW;
  8027. data = 3;
  8028. break;
  8029. default:
  8030. WARN(1, "invalid cdclk frequency\n");
  8031. return;
  8032. }
  8033. I915_WRITE(LCPLL_CTL, val);
  8034. val = I915_READ(LCPLL_CTL);
  8035. val &= ~LCPLL_CD_SOURCE_FCLK;
  8036. I915_WRITE(LCPLL_CTL, val);
  8037. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8038. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8039. DRM_ERROR("Switching back to LCPLL failed\n");
  8040. mutex_lock(&dev_priv->rps.hw_lock);
  8041. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8042. mutex_unlock(&dev_priv->rps.hw_lock);
  8043. intel_update_cdclk(dev);
  8044. WARN(cdclk != dev_priv->cdclk_freq,
  8045. "cdclk requested %d kHz but got %d kHz\n",
  8046. cdclk, dev_priv->cdclk_freq);
  8047. }
  8048. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8049. {
  8050. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8051. int max_pixclk = ilk_max_pixel_rate(state);
  8052. int cdclk;
  8053. /*
  8054. * FIXME should also account for plane ratio
  8055. * once 64bpp pixel formats are supported.
  8056. */
  8057. if (max_pixclk > 540000)
  8058. cdclk = 675000;
  8059. else if (max_pixclk > 450000)
  8060. cdclk = 540000;
  8061. else if (max_pixclk > 337500)
  8062. cdclk = 450000;
  8063. else
  8064. cdclk = 337500;
  8065. /*
  8066. * FIXME move the cdclk caclulation to
  8067. * compute_config() so we can fail gracegully.
  8068. */
  8069. if (cdclk > dev_priv->max_cdclk_freq) {
  8070. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8071. cdclk, dev_priv->max_cdclk_freq);
  8072. cdclk = dev_priv->max_cdclk_freq;
  8073. }
  8074. to_intel_atomic_state(state)->cdclk = cdclk;
  8075. return 0;
  8076. }
  8077. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8078. {
  8079. struct drm_device *dev = old_state->dev;
  8080. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  8081. broadwell_set_cdclk(dev, req_cdclk);
  8082. }
  8083. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8084. struct intel_crtc_state *crtc_state)
  8085. {
  8086. if (!intel_ddi_pll_select(crtc, crtc_state))
  8087. return -EINVAL;
  8088. crtc->lowfreq_avail = false;
  8089. return 0;
  8090. }
  8091. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8092. enum port port,
  8093. struct intel_crtc_state *pipe_config)
  8094. {
  8095. switch (port) {
  8096. case PORT_A:
  8097. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8098. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8099. break;
  8100. case PORT_B:
  8101. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8102. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8103. break;
  8104. case PORT_C:
  8105. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8106. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8107. break;
  8108. default:
  8109. DRM_ERROR("Incorrect port type\n");
  8110. }
  8111. }
  8112. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8113. enum port port,
  8114. struct intel_crtc_state *pipe_config)
  8115. {
  8116. u32 temp, dpll_ctl1;
  8117. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8118. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8119. switch (pipe_config->ddi_pll_sel) {
  8120. case SKL_DPLL0:
  8121. /*
  8122. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8123. * of the shared DPLL framework and thus needs to be read out
  8124. * separately
  8125. */
  8126. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8127. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8128. break;
  8129. case SKL_DPLL1:
  8130. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8131. break;
  8132. case SKL_DPLL2:
  8133. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8134. break;
  8135. case SKL_DPLL3:
  8136. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8137. break;
  8138. }
  8139. }
  8140. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8141. enum port port,
  8142. struct intel_crtc_state *pipe_config)
  8143. {
  8144. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8145. switch (pipe_config->ddi_pll_sel) {
  8146. case PORT_CLK_SEL_WRPLL1:
  8147. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8148. break;
  8149. case PORT_CLK_SEL_WRPLL2:
  8150. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8151. break;
  8152. }
  8153. }
  8154. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8155. struct intel_crtc_state *pipe_config)
  8156. {
  8157. struct drm_device *dev = crtc->base.dev;
  8158. struct drm_i915_private *dev_priv = dev->dev_private;
  8159. struct intel_shared_dpll *pll;
  8160. enum port port;
  8161. uint32_t tmp;
  8162. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8163. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8164. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8165. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8166. else if (IS_BROXTON(dev))
  8167. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8168. else
  8169. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8170. if (pipe_config->shared_dpll >= 0) {
  8171. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8172. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8173. &pipe_config->dpll_hw_state));
  8174. }
  8175. /*
  8176. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8177. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8178. * the PCH transcoder is on.
  8179. */
  8180. if (INTEL_INFO(dev)->gen < 9 &&
  8181. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8182. pipe_config->has_pch_encoder = true;
  8183. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8184. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8185. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8186. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8187. }
  8188. }
  8189. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8190. struct intel_crtc_state *pipe_config)
  8191. {
  8192. struct drm_device *dev = crtc->base.dev;
  8193. struct drm_i915_private *dev_priv = dev->dev_private;
  8194. enum intel_display_power_domain pfit_domain;
  8195. uint32_t tmp;
  8196. if (!intel_display_power_is_enabled(dev_priv,
  8197. POWER_DOMAIN_PIPE(crtc->pipe)))
  8198. return false;
  8199. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8200. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8201. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8202. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8203. enum pipe trans_edp_pipe;
  8204. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8205. default:
  8206. WARN(1, "unknown pipe linked to edp transcoder\n");
  8207. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8208. case TRANS_DDI_EDP_INPUT_A_ON:
  8209. trans_edp_pipe = PIPE_A;
  8210. break;
  8211. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8212. trans_edp_pipe = PIPE_B;
  8213. break;
  8214. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8215. trans_edp_pipe = PIPE_C;
  8216. break;
  8217. }
  8218. if (trans_edp_pipe == crtc->pipe)
  8219. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8220. }
  8221. if (!intel_display_power_is_enabled(dev_priv,
  8222. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8223. return false;
  8224. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8225. if (!(tmp & PIPECONF_ENABLE))
  8226. return false;
  8227. haswell_get_ddi_port_state(crtc, pipe_config);
  8228. intel_get_pipe_timings(crtc, pipe_config);
  8229. if (INTEL_INFO(dev)->gen >= 9) {
  8230. skl_init_scalers(dev, crtc, pipe_config);
  8231. }
  8232. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8233. if (INTEL_INFO(dev)->gen >= 9) {
  8234. pipe_config->scaler_state.scaler_id = -1;
  8235. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8236. }
  8237. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8238. if (INTEL_INFO(dev)->gen >= 9)
  8239. skylake_get_pfit_config(crtc, pipe_config);
  8240. else
  8241. ironlake_get_pfit_config(crtc, pipe_config);
  8242. }
  8243. if (IS_HASWELL(dev))
  8244. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8245. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8246. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8247. pipe_config->pixel_multiplier =
  8248. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8249. } else {
  8250. pipe_config->pixel_multiplier = 1;
  8251. }
  8252. return true;
  8253. }
  8254. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8255. {
  8256. struct drm_device *dev = crtc->dev;
  8257. struct drm_i915_private *dev_priv = dev->dev_private;
  8258. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8259. uint32_t cntl = 0, size = 0;
  8260. if (base) {
  8261. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8262. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8263. unsigned int stride = roundup_pow_of_two(width) * 4;
  8264. switch (stride) {
  8265. default:
  8266. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8267. width, stride);
  8268. stride = 256;
  8269. /* fallthrough */
  8270. case 256:
  8271. case 512:
  8272. case 1024:
  8273. case 2048:
  8274. break;
  8275. }
  8276. cntl |= CURSOR_ENABLE |
  8277. CURSOR_GAMMA_ENABLE |
  8278. CURSOR_FORMAT_ARGB |
  8279. CURSOR_STRIDE(stride);
  8280. size = (height << 12) | width;
  8281. }
  8282. if (intel_crtc->cursor_cntl != 0 &&
  8283. (intel_crtc->cursor_base != base ||
  8284. intel_crtc->cursor_size != size ||
  8285. intel_crtc->cursor_cntl != cntl)) {
  8286. /* On these chipsets we can only modify the base/size/stride
  8287. * whilst the cursor is disabled.
  8288. */
  8289. I915_WRITE(CURCNTR(PIPE_A), 0);
  8290. POSTING_READ(CURCNTR(PIPE_A));
  8291. intel_crtc->cursor_cntl = 0;
  8292. }
  8293. if (intel_crtc->cursor_base != base) {
  8294. I915_WRITE(CURBASE(PIPE_A), base);
  8295. intel_crtc->cursor_base = base;
  8296. }
  8297. if (intel_crtc->cursor_size != size) {
  8298. I915_WRITE(CURSIZE, size);
  8299. intel_crtc->cursor_size = size;
  8300. }
  8301. if (intel_crtc->cursor_cntl != cntl) {
  8302. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8303. POSTING_READ(CURCNTR(PIPE_A));
  8304. intel_crtc->cursor_cntl = cntl;
  8305. }
  8306. }
  8307. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8308. {
  8309. struct drm_device *dev = crtc->dev;
  8310. struct drm_i915_private *dev_priv = dev->dev_private;
  8311. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8312. int pipe = intel_crtc->pipe;
  8313. uint32_t cntl;
  8314. cntl = 0;
  8315. if (base) {
  8316. cntl = MCURSOR_GAMMA_ENABLE;
  8317. switch (intel_crtc->base.cursor->state->crtc_w) {
  8318. case 64:
  8319. cntl |= CURSOR_MODE_64_ARGB_AX;
  8320. break;
  8321. case 128:
  8322. cntl |= CURSOR_MODE_128_ARGB_AX;
  8323. break;
  8324. case 256:
  8325. cntl |= CURSOR_MODE_256_ARGB_AX;
  8326. break;
  8327. default:
  8328. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8329. return;
  8330. }
  8331. cntl |= pipe << 28; /* Connect to correct pipe */
  8332. if (HAS_DDI(dev))
  8333. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8334. }
  8335. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8336. cntl |= CURSOR_ROTATE_180;
  8337. if (intel_crtc->cursor_cntl != cntl) {
  8338. I915_WRITE(CURCNTR(pipe), cntl);
  8339. POSTING_READ(CURCNTR(pipe));
  8340. intel_crtc->cursor_cntl = cntl;
  8341. }
  8342. /* and commit changes on next vblank */
  8343. I915_WRITE(CURBASE(pipe), base);
  8344. POSTING_READ(CURBASE(pipe));
  8345. intel_crtc->cursor_base = base;
  8346. }
  8347. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8348. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8349. bool on)
  8350. {
  8351. struct drm_device *dev = crtc->dev;
  8352. struct drm_i915_private *dev_priv = dev->dev_private;
  8353. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8354. int pipe = intel_crtc->pipe;
  8355. struct drm_plane_state *cursor_state = crtc->cursor->state;
  8356. int x = cursor_state->crtc_x;
  8357. int y = cursor_state->crtc_y;
  8358. u32 base = 0, pos = 0;
  8359. if (on)
  8360. base = intel_crtc->cursor_addr;
  8361. if (x >= intel_crtc->config->pipe_src_w)
  8362. base = 0;
  8363. if (y >= intel_crtc->config->pipe_src_h)
  8364. base = 0;
  8365. if (x < 0) {
  8366. if (x + cursor_state->crtc_w <= 0)
  8367. base = 0;
  8368. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8369. x = -x;
  8370. }
  8371. pos |= x << CURSOR_X_SHIFT;
  8372. if (y < 0) {
  8373. if (y + cursor_state->crtc_h <= 0)
  8374. base = 0;
  8375. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8376. y = -y;
  8377. }
  8378. pos |= y << CURSOR_Y_SHIFT;
  8379. if (base == 0 && intel_crtc->cursor_base == 0)
  8380. return;
  8381. I915_WRITE(CURPOS(pipe), pos);
  8382. /* ILK+ do this automagically */
  8383. if (HAS_GMCH_DISPLAY(dev) &&
  8384. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8385. base += (cursor_state->crtc_h *
  8386. cursor_state->crtc_w - 1) * 4;
  8387. }
  8388. if (IS_845G(dev) || IS_I865G(dev))
  8389. i845_update_cursor(crtc, base);
  8390. else
  8391. i9xx_update_cursor(crtc, base);
  8392. }
  8393. static bool cursor_size_ok(struct drm_device *dev,
  8394. uint32_t width, uint32_t height)
  8395. {
  8396. if (width == 0 || height == 0)
  8397. return false;
  8398. /*
  8399. * 845g/865g are special in that they are only limited by
  8400. * the width of their cursors, the height is arbitrary up to
  8401. * the precision of the register. Everything else requires
  8402. * square cursors, limited to a few power-of-two sizes.
  8403. */
  8404. if (IS_845G(dev) || IS_I865G(dev)) {
  8405. if ((width & 63) != 0)
  8406. return false;
  8407. if (width > (IS_845G(dev) ? 64 : 512))
  8408. return false;
  8409. if (height > 1023)
  8410. return false;
  8411. } else {
  8412. switch (width | height) {
  8413. case 256:
  8414. case 128:
  8415. if (IS_GEN2(dev))
  8416. return false;
  8417. case 64:
  8418. break;
  8419. default:
  8420. return false;
  8421. }
  8422. }
  8423. return true;
  8424. }
  8425. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8426. u16 *blue, uint32_t start, uint32_t size)
  8427. {
  8428. int end = (start + size > 256) ? 256 : start + size, i;
  8429. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8430. for (i = start; i < end; i++) {
  8431. intel_crtc->lut_r[i] = red[i] >> 8;
  8432. intel_crtc->lut_g[i] = green[i] >> 8;
  8433. intel_crtc->lut_b[i] = blue[i] >> 8;
  8434. }
  8435. intel_crtc_load_lut(crtc);
  8436. }
  8437. /* VESA 640x480x72Hz mode to set on the pipe */
  8438. static struct drm_display_mode load_detect_mode = {
  8439. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8440. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8441. };
  8442. struct drm_framebuffer *
  8443. __intel_framebuffer_create(struct drm_device *dev,
  8444. struct drm_mode_fb_cmd2 *mode_cmd,
  8445. struct drm_i915_gem_object *obj)
  8446. {
  8447. struct intel_framebuffer *intel_fb;
  8448. int ret;
  8449. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8450. if (!intel_fb)
  8451. return ERR_PTR(-ENOMEM);
  8452. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8453. if (ret)
  8454. goto err;
  8455. return &intel_fb->base;
  8456. err:
  8457. kfree(intel_fb);
  8458. return ERR_PTR(ret);
  8459. }
  8460. static struct drm_framebuffer *
  8461. intel_framebuffer_create(struct drm_device *dev,
  8462. struct drm_mode_fb_cmd2 *mode_cmd,
  8463. struct drm_i915_gem_object *obj)
  8464. {
  8465. struct drm_framebuffer *fb;
  8466. int ret;
  8467. ret = i915_mutex_lock_interruptible(dev);
  8468. if (ret)
  8469. return ERR_PTR(ret);
  8470. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8471. mutex_unlock(&dev->struct_mutex);
  8472. return fb;
  8473. }
  8474. static u32
  8475. intel_framebuffer_pitch_for_width(int width, int bpp)
  8476. {
  8477. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8478. return ALIGN(pitch, 64);
  8479. }
  8480. static u32
  8481. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8482. {
  8483. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8484. return PAGE_ALIGN(pitch * mode->vdisplay);
  8485. }
  8486. static struct drm_framebuffer *
  8487. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8488. struct drm_display_mode *mode,
  8489. int depth, int bpp)
  8490. {
  8491. struct drm_framebuffer *fb;
  8492. struct drm_i915_gem_object *obj;
  8493. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8494. obj = i915_gem_alloc_object(dev,
  8495. intel_framebuffer_size_for_mode(mode, bpp));
  8496. if (obj == NULL)
  8497. return ERR_PTR(-ENOMEM);
  8498. mode_cmd.width = mode->hdisplay;
  8499. mode_cmd.height = mode->vdisplay;
  8500. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8501. bpp);
  8502. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8503. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8504. if (IS_ERR(fb))
  8505. drm_gem_object_unreference_unlocked(&obj->base);
  8506. return fb;
  8507. }
  8508. static struct drm_framebuffer *
  8509. mode_fits_in_fbdev(struct drm_device *dev,
  8510. struct drm_display_mode *mode)
  8511. {
  8512. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8513. struct drm_i915_private *dev_priv = dev->dev_private;
  8514. struct drm_i915_gem_object *obj;
  8515. struct drm_framebuffer *fb;
  8516. if (!dev_priv->fbdev)
  8517. return NULL;
  8518. if (!dev_priv->fbdev->fb)
  8519. return NULL;
  8520. obj = dev_priv->fbdev->fb->obj;
  8521. BUG_ON(!obj);
  8522. fb = &dev_priv->fbdev->fb->base;
  8523. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8524. fb->bits_per_pixel))
  8525. return NULL;
  8526. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8527. return NULL;
  8528. return fb;
  8529. #else
  8530. return NULL;
  8531. #endif
  8532. }
  8533. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8534. struct drm_crtc *crtc,
  8535. struct drm_display_mode *mode,
  8536. struct drm_framebuffer *fb,
  8537. int x, int y)
  8538. {
  8539. struct drm_plane_state *plane_state;
  8540. int hdisplay, vdisplay;
  8541. int ret;
  8542. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8543. if (IS_ERR(plane_state))
  8544. return PTR_ERR(plane_state);
  8545. if (mode)
  8546. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8547. else
  8548. hdisplay = vdisplay = 0;
  8549. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8550. if (ret)
  8551. return ret;
  8552. drm_atomic_set_fb_for_plane(plane_state, fb);
  8553. plane_state->crtc_x = 0;
  8554. plane_state->crtc_y = 0;
  8555. plane_state->crtc_w = hdisplay;
  8556. plane_state->crtc_h = vdisplay;
  8557. plane_state->src_x = x << 16;
  8558. plane_state->src_y = y << 16;
  8559. plane_state->src_w = hdisplay << 16;
  8560. plane_state->src_h = vdisplay << 16;
  8561. return 0;
  8562. }
  8563. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8564. struct drm_display_mode *mode,
  8565. struct intel_load_detect_pipe *old,
  8566. struct drm_modeset_acquire_ctx *ctx)
  8567. {
  8568. struct intel_crtc *intel_crtc;
  8569. struct intel_encoder *intel_encoder =
  8570. intel_attached_encoder(connector);
  8571. struct drm_crtc *possible_crtc;
  8572. struct drm_encoder *encoder = &intel_encoder->base;
  8573. struct drm_crtc *crtc = NULL;
  8574. struct drm_device *dev = encoder->dev;
  8575. struct drm_framebuffer *fb;
  8576. struct drm_mode_config *config = &dev->mode_config;
  8577. struct drm_atomic_state *state = NULL;
  8578. struct drm_connector_state *connector_state;
  8579. struct intel_crtc_state *crtc_state;
  8580. int ret, i = -1;
  8581. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8582. connector->base.id, connector->name,
  8583. encoder->base.id, encoder->name);
  8584. retry:
  8585. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8586. if (ret)
  8587. goto fail;
  8588. /*
  8589. * Algorithm gets a little messy:
  8590. *
  8591. * - if the connector already has an assigned crtc, use it (but make
  8592. * sure it's on first)
  8593. *
  8594. * - try to find the first unused crtc that can drive this connector,
  8595. * and use that if we find one
  8596. */
  8597. /* See if we already have a CRTC for this connector */
  8598. if (encoder->crtc) {
  8599. crtc = encoder->crtc;
  8600. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8601. if (ret)
  8602. goto fail;
  8603. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8604. if (ret)
  8605. goto fail;
  8606. old->dpms_mode = connector->dpms;
  8607. old->load_detect_temp = false;
  8608. /* Make sure the crtc and connector are running */
  8609. if (connector->dpms != DRM_MODE_DPMS_ON)
  8610. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8611. return true;
  8612. }
  8613. /* Find an unused one (if possible) */
  8614. for_each_crtc(dev, possible_crtc) {
  8615. i++;
  8616. if (!(encoder->possible_crtcs & (1 << i)))
  8617. continue;
  8618. if (possible_crtc->state->enable)
  8619. continue;
  8620. crtc = possible_crtc;
  8621. break;
  8622. }
  8623. /*
  8624. * If we didn't find an unused CRTC, don't use any.
  8625. */
  8626. if (!crtc) {
  8627. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8628. goto fail;
  8629. }
  8630. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8631. if (ret)
  8632. goto fail;
  8633. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8634. if (ret)
  8635. goto fail;
  8636. intel_crtc = to_intel_crtc(crtc);
  8637. old->dpms_mode = connector->dpms;
  8638. old->load_detect_temp = true;
  8639. old->release_fb = NULL;
  8640. state = drm_atomic_state_alloc(dev);
  8641. if (!state)
  8642. return false;
  8643. state->acquire_ctx = ctx;
  8644. connector_state = drm_atomic_get_connector_state(state, connector);
  8645. if (IS_ERR(connector_state)) {
  8646. ret = PTR_ERR(connector_state);
  8647. goto fail;
  8648. }
  8649. connector_state->crtc = crtc;
  8650. connector_state->best_encoder = &intel_encoder->base;
  8651. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8652. if (IS_ERR(crtc_state)) {
  8653. ret = PTR_ERR(crtc_state);
  8654. goto fail;
  8655. }
  8656. crtc_state->base.active = crtc_state->base.enable = true;
  8657. if (!mode)
  8658. mode = &load_detect_mode;
  8659. /* We need a framebuffer large enough to accommodate all accesses
  8660. * that the plane may generate whilst we perform load detection.
  8661. * We can not rely on the fbcon either being present (we get called
  8662. * during its initialisation to detect all boot displays, or it may
  8663. * not even exist) or that it is large enough to satisfy the
  8664. * requested mode.
  8665. */
  8666. fb = mode_fits_in_fbdev(dev, mode);
  8667. if (fb == NULL) {
  8668. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8669. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8670. old->release_fb = fb;
  8671. } else
  8672. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8673. if (IS_ERR(fb)) {
  8674. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8675. goto fail;
  8676. }
  8677. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8678. if (ret)
  8679. goto fail;
  8680. drm_mode_copy(&crtc_state->base.mode, mode);
  8681. if (drm_atomic_commit(state)) {
  8682. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8683. if (old->release_fb)
  8684. old->release_fb->funcs->destroy(old->release_fb);
  8685. goto fail;
  8686. }
  8687. crtc->primary->crtc = crtc;
  8688. /* let the connector get through one full cycle before testing */
  8689. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8690. return true;
  8691. fail:
  8692. drm_atomic_state_free(state);
  8693. state = NULL;
  8694. if (ret == -EDEADLK) {
  8695. drm_modeset_backoff(ctx);
  8696. goto retry;
  8697. }
  8698. return false;
  8699. }
  8700. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8701. struct intel_load_detect_pipe *old,
  8702. struct drm_modeset_acquire_ctx *ctx)
  8703. {
  8704. struct drm_device *dev = connector->dev;
  8705. struct intel_encoder *intel_encoder =
  8706. intel_attached_encoder(connector);
  8707. struct drm_encoder *encoder = &intel_encoder->base;
  8708. struct drm_crtc *crtc = encoder->crtc;
  8709. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8710. struct drm_atomic_state *state;
  8711. struct drm_connector_state *connector_state;
  8712. struct intel_crtc_state *crtc_state;
  8713. int ret;
  8714. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8715. connector->base.id, connector->name,
  8716. encoder->base.id, encoder->name);
  8717. if (old->load_detect_temp) {
  8718. state = drm_atomic_state_alloc(dev);
  8719. if (!state)
  8720. goto fail;
  8721. state->acquire_ctx = ctx;
  8722. connector_state = drm_atomic_get_connector_state(state, connector);
  8723. if (IS_ERR(connector_state))
  8724. goto fail;
  8725. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8726. if (IS_ERR(crtc_state))
  8727. goto fail;
  8728. connector_state->best_encoder = NULL;
  8729. connector_state->crtc = NULL;
  8730. crtc_state->base.enable = crtc_state->base.active = false;
  8731. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8732. 0, 0);
  8733. if (ret)
  8734. goto fail;
  8735. ret = drm_atomic_commit(state);
  8736. if (ret)
  8737. goto fail;
  8738. if (old->release_fb) {
  8739. drm_framebuffer_unregister_private(old->release_fb);
  8740. drm_framebuffer_unreference(old->release_fb);
  8741. }
  8742. return;
  8743. }
  8744. /* Switch crtc and encoder back off if necessary */
  8745. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8746. connector->funcs->dpms(connector, old->dpms_mode);
  8747. return;
  8748. fail:
  8749. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8750. drm_atomic_state_free(state);
  8751. }
  8752. static int i9xx_pll_refclk(struct drm_device *dev,
  8753. const struct intel_crtc_state *pipe_config)
  8754. {
  8755. struct drm_i915_private *dev_priv = dev->dev_private;
  8756. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8757. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8758. return dev_priv->vbt.lvds_ssc_freq;
  8759. else if (HAS_PCH_SPLIT(dev))
  8760. return 120000;
  8761. else if (!IS_GEN2(dev))
  8762. return 96000;
  8763. else
  8764. return 48000;
  8765. }
  8766. /* Returns the clock of the currently programmed mode of the given pipe. */
  8767. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8768. struct intel_crtc_state *pipe_config)
  8769. {
  8770. struct drm_device *dev = crtc->base.dev;
  8771. struct drm_i915_private *dev_priv = dev->dev_private;
  8772. int pipe = pipe_config->cpu_transcoder;
  8773. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8774. u32 fp;
  8775. intel_clock_t clock;
  8776. int port_clock;
  8777. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8778. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8779. fp = pipe_config->dpll_hw_state.fp0;
  8780. else
  8781. fp = pipe_config->dpll_hw_state.fp1;
  8782. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8783. if (IS_PINEVIEW(dev)) {
  8784. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8785. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8786. } else {
  8787. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8788. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8789. }
  8790. if (!IS_GEN2(dev)) {
  8791. if (IS_PINEVIEW(dev))
  8792. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8793. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8794. else
  8795. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8796. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8797. switch (dpll & DPLL_MODE_MASK) {
  8798. case DPLLB_MODE_DAC_SERIAL:
  8799. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8800. 5 : 10;
  8801. break;
  8802. case DPLLB_MODE_LVDS:
  8803. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8804. 7 : 14;
  8805. break;
  8806. default:
  8807. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8808. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8809. return;
  8810. }
  8811. if (IS_PINEVIEW(dev))
  8812. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8813. else
  8814. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8815. } else {
  8816. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8817. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8818. if (is_lvds) {
  8819. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8820. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8821. if (lvds & LVDS_CLKB_POWER_UP)
  8822. clock.p2 = 7;
  8823. else
  8824. clock.p2 = 14;
  8825. } else {
  8826. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8827. clock.p1 = 2;
  8828. else {
  8829. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8830. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8831. }
  8832. if (dpll & PLL_P2_DIVIDE_BY_4)
  8833. clock.p2 = 4;
  8834. else
  8835. clock.p2 = 2;
  8836. }
  8837. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8838. }
  8839. /*
  8840. * This value includes pixel_multiplier. We will use
  8841. * port_clock to compute adjusted_mode.crtc_clock in the
  8842. * encoder's get_config() function.
  8843. */
  8844. pipe_config->port_clock = port_clock;
  8845. }
  8846. int intel_dotclock_calculate(int link_freq,
  8847. const struct intel_link_m_n *m_n)
  8848. {
  8849. /*
  8850. * The calculation for the data clock is:
  8851. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8852. * But we want to avoid losing precison if possible, so:
  8853. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8854. *
  8855. * and the link clock is simpler:
  8856. * link_clock = (m * link_clock) / n
  8857. */
  8858. if (!m_n->link_n)
  8859. return 0;
  8860. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8861. }
  8862. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8863. struct intel_crtc_state *pipe_config)
  8864. {
  8865. struct drm_device *dev = crtc->base.dev;
  8866. /* read out port_clock from the DPLL */
  8867. i9xx_crtc_clock_get(crtc, pipe_config);
  8868. /*
  8869. * This value does not include pixel_multiplier.
  8870. * We will check that port_clock and adjusted_mode.crtc_clock
  8871. * agree once we know their relationship in the encoder's
  8872. * get_config() function.
  8873. */
  8874. pipe_config->base.adjusted_mode.crtc_clock =
  8875. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8876. &pipe_config->fdi_m_n);
  8877. }
  8878. /** Returns the currently programmed mode of the given pipe. */
  8879. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8880. struct drm_crtc *crtc)
  8881. {
  8882. struct drm_i915_private *dev_priv = dev->dev_private;
  8883. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8884. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8885. struct drm_display_mode *mode;
  8886. struct intel_crtc_state pipe_config;
  8887. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8888. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8889. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8890. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8891. enum pipe pipe = intel_crtc->pipe;
  8892. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8893. if (!mode)
  8894. return NULL;
  8895. /*
  8896. * Construct a pipe_config sufficient for getting the clock info
  8897. * back out of crtc_clock_get.
  8898. *
  8899. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8900. * to use a real value here instead.
  8901. */
  8902. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8903. pipe_config.pixel_multiplier = 1;
  8904. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8905. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8906. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8907. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8908. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8909. mode->hdisplay = (htot & 0xffff) + 1;
  8910. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8911. mode->hsync_start = (hsync & 0xffff) + 1;
  8912. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8913. mode->vdisplay = (vtot & 0xffff) + 1;
  8914. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8915. mode->vsync_start = (vsync & 0xffff) + 1;
  8916. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8917. drm_mode_set_name(mode);
  8918. return mode;
  8919. }
  8920. void intel_mark_busy(struct drm_device *dev)
  8921. {
  8922. struct drm_i915_private *dev_priv = dev->dev_private;
  8923. if (dev_priv->mm.busy)
  8924. return;
  8925. intel_runtime_pm_get(dev_priv);
  8926. i915_update_gfx_val(dev_priv);
  8927. if (INTEL_INFO(dev)->gen >= 6)
  8928. gen6_rps_busy(dev_priv);
  8929. dev_priv->mm.busy = true;
  8930. }
  8931. void intel_mark_idle(struct drm_device *dev)
  8932. {
  8933. struct drm_i915_private *dev_priv = dev->dev_private;
  8934. if (!dev_priv->mm.busy)
  8935. return;
  8936. dev_priv->mm.busy = false;
  8937. if (INTEL_INFO(dev)->gen >= 6)
  8938. gen6_rps_idle(dev->dev_private);
  8939. intel_runtime_pm_put(dev_priv);
  8940. }
  8941. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8942. {
  8943. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8944. struct drm_device *dev = crtc->dev;
  8945. struct intel_unpin_work *work;
  8946. spin_lock_irq(&dev->event_lock);
  8947. work = intel_crtc->unpin_work;
  8948. intel_crtc->unpin_work = NULL;
  8949. spin_unlock_irq(&dev->event_lock);
  8950. if (work) {
  8951. cancel_work_sync(&work->work);
  8952. kfree(work);
  8953. }
  8954. drm_crtc_cleanup(crtc);
  8955. kfree(intel_crtc);
  8956. }
  8957. static void intel_unpin_work_fn(struct work_struct *__work)
  8958. {
  8959. struct intel_unpin_work *work =
  8960. container_of(__work, struct intel_unpin_work, work);
  8961. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8962. struct drm_device *dev = crtc->base.dev;
  8963. struct drm_plane *primary = crtc->base.primary;
  8964. mutex_lock(&dev->struct_mutex);
  8965. intel_unpin_fb_obj(work->old_fb, primary->state);
  8966. drm_gem_object_unreference(&work->pending_flip_obj->base);
  8967. if (work->flip_queued_req)
  8968. i915_gem_request_assign(&work->flip_queued_req, NULL);
  8969. mutex_unlock(&dev->struct_mutex);
  8970. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  8971. drm_framebuffer_unreference(work->old_fb);
  8972. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8973. atomic_dec(&crtc->unpin_work_count);
  8974. kfree(work);
  8975. }
  8976. static void do_intel_finish_page_flip(struct drm_device *dev,
  8977. struct drm_crtc *crtc)
  8978. {
  8979. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8980. struct intel_unpin_work *work;
  8981. unsigned long flags;
  8982. /* Ignore early vblank irqs */
  8983. if (intel_crtc == NULL)
  8984. return;
  8985. /*
  8986. * This is called both by irq handlers and the reset code (to complete
  8987. * lost pageflips) so needs the full irqsave spinlocks.
  8988. */
  8989. spin_lock_irqsave(&dev->event_lock, flags);
  8990. work = intel_crtc->unpin_work;
  8991. /* Ensure we don't miss a work->pending update ... */
  8992. smp_rmb();
  8993. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  8994. spin_unlock_irqrestore(&dev->event_lock, flags);
  8995. return;
  8996. }
  8997. page_flip_completed(intel_crtc);
  8998. spin_unlock_irqrestore(&dev->event_lock, flags);
  8999. }
  9000. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9001. {
  9002. struct drm_i915_private *dev_priv = dev->dev_private;
  9003. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9004. do_intel_finish_page_flip(dev, crtc);
  9005. }
  9006. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9007. {
  9008. struct drm_i915_private *dev_priv = dev->dev_private;
  9009. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9010. do_intel_finish_page_flip(dev, crtc);
  9011. }
  9012. /* Is 'a' after or equal to 'b'? */
  9013. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9014. {
  9015. return !((a - b) & 0x80000000);
  9016. }
  9017. static bool page_flip_finished(struct intel_crtc *crtc)
  9018. {
  9019. struct drm_device *dev = crtc->base.dev;
  9020. struct drm_i915_private *dev_priv = dev->dev_private;
  9021. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9022. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9023. return true;
  9024. /*
  9025. * The relevant registers doen't exist on pre-ctg.
  9026. * As the flip done interrupt doesn't trigger for mmio
  9027. * flips on gmch platforms, a flip count check isn't
  9028. * really needed there. But since ctg has the registers,
  9029. * include it in the check anyway.
  9030. */
  9031. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9032. return true;
  9033. /*
  9034. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9035. * used the same base address. In that case the mmio flip might
  9036. * have completed, but the CS hasn't even executed the flip yet.
  9037. *
  9038. * A flip count check isn't enough as the CS might have updated
  9039. * the base address just after start of vblank, but before we
  9040. * managed to process the interrupt. This means we'd complete the
  9041. * CS flip too soon.
  9042. *
  9043. * Combining both checks should get us a good enough result. It may
  9044. * still happen that the CS flip has been executed, but has not
  9045. * yet actually completed. But in case the base address is the same
  9046. * anyway, we don't really care.
  9047. */
  9048. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9049. crtc->unpin_work->gtt_offset &&
  9050. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9051. crtc->unpin_work->flip_count);
  9052. }
  9053. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9054. {
  9055. struct drm_i915_private *dev_priv = dev->dev_private;
  9056. struct intel_crtc *intel_crtc =
  9057. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9058. unsigned long flags;
  9059. /*
  9060. * This is called both by irq handlers and the reset code (to complete
  9061. * lost pageflips) so needs the full irqsave spinlocks.
  9062. *
  9063. * NB: An MMIO update of the plane base pointer will also
  9064. * generate a page-flip completion irq, i.e. every modeset
  9065. * is also accompanied by a spurious intel_prepare_page_flip().
  9066. */
  9067. spin_lock_irqsave(&dev->event_lock, flags);
  9068. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9069. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9070. spin_unlock_irqrestore(&dev->event_lock, flags);
  9071. }
  9072. static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
  9073. {
  9074. /* Ensure that the work item is consistent when activating it ... */
  9075. smp_wmb();
  9076. atomic_set(&work->pending, INTEL_FLIP_PENDING);
  9077. /* and that it is marked active as soon as the irq could fire. */
  9078. smp_wmb();
  9079. }
  9080. static int intel_gen2_queue_flip(struct drm_device *dev,
  9081. struct drm_crtc *crtc,
  9082. struct drm_framebuffer *fb,
  9083. struct drm_i915_gem_object *obj,
  9084. struct drm_i915_gem_request *req,
  9085. uint32_t flags)
  9086. {
  9087. struct intel_engine_cs *ring = req->ring;
  9088. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9089. u32 flip_mask;
  9090. int ret;
  9091. ret = intel_ring_begin(req, 6);
  9092. if (ret)
  9093. return ret;
  9094. /* Can't queue multiple flips, so wait for the previous
  9095. * one to finish before executing the next.
  9096. */
  9097. if (intel_crtc->plane)
  9098. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9099. else
  9100. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9101. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9102. intel_ring_emit(ring, MI_NOOP);
  9103. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9104. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9105. intel_ring_emit(ring, fb->pitches[0]);
  9106. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9107. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9108. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9109. return 0;
  9110. }
  9111. static int intel_gen3_queue_flip(struct drm_device *dev,
  9112. struct drm_crtc *crtc,
  9113. struct drm_framebuffer *fb,
  9114. struct drm_i915_gem_object *obj,
  9115. struct drm_i915_gem_request *req,
  9116. uint32_t flags)
  9117. {
  9118. struct intel_engine_cs *ring = req->ring;
  9119. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9120. u32 flip_mask;
  9121. int ret;
  9122. ret = intel_ring_begin(req, 6);
  9123. if (ret)
  9124. return ret;
  9125. if (intel_crtc->plane)
  9126. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9127. else
  9128. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9129. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9130. intel_ring_emit(ring, MI_NOOP);
  9131. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9132. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9133. intel_ring_emit(ring, fb->pitches[0]);
  9134. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9135. intel_ring_emit(ring, MI_NOOP);
  9136. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9137. return 0;
  9138. }
  9139. static int intel_gen4_queue_flip(struct drm_device *dev,
  9140. struct drm_crtc *crtc,
  9141. struct drm_framebuffer *fb,
  9142. struct drm_i915_gem_object *obj,
  9143. struct drm_i915_gem_request *req,
  9144. uint32_t flags)
  9145. {
  9146. struct intel_engine_cs *ring = req->ring;
  9147. struct drm_i915_private *dev_priv = dev->dev_private;
  9148. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9149. uint32_t pf, pipesrc;
  9150. int ret;
  9151. ret = intel_ring_begin(req, 4);
  9152. if (ret)
  9153. return ret;
  9154. /* i965+ uses the linear or tiled offsets from the
  9155. * Display Registers (which do not change across a page-flip)
  9156. * so we need only reprogram the base address.
  9157. */
  9158. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9159. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9160. intel_ring_emit(ring, fb->pitches[0]);
  9161. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9162. obj->tiling_mode);
  9163. /* XXX Enabling the panel-fitter across page-flip is so far
  9164. * untested on non-native modes, so ignore it for now.
  9165. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9166. */
  9167. pf = 0;
  9168. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9169. intel_ring_emit(ring, pf | pipesrc);
  9170. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9171. return 0;
  9172. }
  9173. static int intel_gen6_queue_flip(struct drm_device *dev,
  9174. struct drm_crtc *crtc,
  9175. struct drm_framebuffer *fb,
  9176. struct drm_i915_gem_object *obj,
  9177. struct drm_i915_gem_request *req,
  9178. uint32_t flags)
  9179. {
  9180. struct intel_engine_cs *ring = req->ring;
  9181. struct drm_i915_private *dev_priv = dev->dev_private;
  9182. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9183. uint32_t pf, pipesrc;
  9184. int ret;
  9185. ret = intel_ring_begin(req, 4);
  9186. if (ret)
  9187. return ret;
  9188. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9189. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9190. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9191. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9192. /* Contrary to the suggestions in the documentation,
  9193. * "Enable Panel Fitter" does not seem to be required when page
  9194. * flipping with a non-native mode, and worse causes a normal
  9195. * modeset to fail.
  9196. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9197. */
  9198. pf = 0;
  9199. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9200. intel_ring_emit(ring, pf | pipesrc);
  9201. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9202. return 0;
  9203. }
  9204. static int intel_gen7_queue_flip(struct drm_device *dev,
  9205. struct drm_crtc *crtc,
  9206. struct drm_framebuffer *fb,
  9207. struct drm_i915_gem_object *obj,
  9208. struct drm_i915_gem_request *req,
  9209. uint32_t flags)
  9210. {
  9211. struct intel_engine_cs *ring = req->ring;
  9212. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9213. uint32_t plane_bit = 0;
  9214. int len, ret;
  9215. switch (intel_crtc->plane) {
  9216. case PLANE_A:
  9217. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9218. break;
  9219. case PLANE_B:
  9220. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9221. break;
  9222. case PLANE_C:
  9223. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9224. break;
  9225. default:
  9226. WARN_ONCE(1, "unknown plane in flip command\n");
  9227. return -ENODEV;
  9228. }
  9229. len = 4;
  9230. if (ring->id == RCS) {
  9231. len += 6;
  9232. /*
  9233. * On Gen 8, SRM is now taking an extra dword to accommodate
  9234. * 48bits addresses, and we need a NOOP for the batch size to
  9235. * stay even.
  9236. */
  9237. if (IS_GEN8(dev))
  9238. len += 2;
  9239. }
  9240. /*
  9241. * BSpec MI_DISPLAY_FLIP for IVB:
  9242. * "The full packet must be contained within the same cache line."
  9243. *
  9244. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9245. * cacheline, if we ever start emitting more commands before
  9246. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9247. * then do the cacheline alignment, and finally emit the
  9248. * MI_DISPLAY_FLIP.
  9249. */
  9250. ret = intel_ring_cacheline_align(req);
  9251. if (ret)
  9252. return ret;
  9253. ret = intel_ring_begin(req, len);
  9254. if (ret)
  9255. return ret;
  9256. /* Unmask the flip-done completion message. Note that the bspec says that
  9257. * we should do this for both the BCS and RCS, and that we must not unmask
  9258. * more than one flip event at any time (or ensure that one flip message
  9259. * can be sent by waiting for flip-done prior to queueing new flips).
  9260. * Experimentation says that BCS works despite DERRMR masking all
  9261. * flip-done completion events and that unmasking all planes at once
  9262. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9263. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9264. */
  9265. if (ring->id == RCS) {
  9266. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9267. intel_ring_emit(ring, DERRMR);
  9268. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9269. DERRMR_PIPEB_PRI_FLIP_DONE |
  9270. DERRMR_PIPEC_PRI_FLIP_DONE));
  9271. if (IS_GEN8(dev))
  9272. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  9273. MI_SRM_LRM_GLOBAL_GTT);
  9274. else
  9275. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  9276. MI_SRM_LRM_GLOBAL_GTT);
  9277. intel_ring_emit(ring, DERRMR);
  9278. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9279. if (IS_GEN8(dev)) {
  9280. intel_ring_emit(ring, 0);
  9281. intel_ring_emit(ring, MI_NOOP);
  9282. }
  9283. }
  9284. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9285. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9286. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9287. intel_ring_emit(ring, (MI_NOOP));
  9288. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9289. return 0;
  9290. }
  9291. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9292. struct drm_i915_gem_object *obj)
  9293. {
  9294. /*
  9295. * This is not being used for older platforms, because
  9296. * non-availability of flip done interrupt forces us to use
  9297. * CS flips. Older platforms derive flip done using some clever
  9298. * tricks involving the flip_pending status bits and vblank irqs.
  9299. * So using MMIO flips there would disrupt this mechanism.
  9300. */
  9301. if (ring == NULL)
  9302. return true;
  9303. if (INTEL_INFO(ring->dev)->gen < 5)
  9304. return false;
  9305. if (i915.use_mmio_flip < 0)
  9306. return false;
  9307. else if (i915.use_mmio_flip > 0)
  9308. return true;
  9309. else if (i915.enable_execlists)
  9310. return true;
  9311. else
  9312. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9313. }
  9314. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9315. unsigned int rotation,
  9316. struct intel_unpin_work *work)
  9317. {
  9318. struct drm_device *dev = intel_crtc->base.dev;
  9319. struct drm_i915_private *dev_priv = dev->dev_private;
  9320. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9321. const enum pipe pipe = intel_crtc->pipe;
  9322. u32 ctl, stride, tile_height;
  9323. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9324. ctl &= ~PLANE_CTL_TILED_MASK;
  9325. switch (fb->modifier[0]) {
  9326. case DRM_FORMAT_MOD_NONE:
  9327. break;
  9328. case I915_FORMAT_MOD_X_TILED:
  9329. ctl |= PLANE_CTL_TILED_X;
  9330. break;
  9331. case I915_FORMAT_MOD_Y_TILED:
  9332. ctl |= PLANE_CTL_TILED_Y;
  9333. break;
  9334. case I915_FORMAT_MOD_Yf_TILED:
  9335. ctl |= PLANE_CTL_TILED_YF;
  9336. break;
  9337. default:
  9338. MISSING_CASE(fb->modifier[0]);
  9339. }
  9340. /*
  9341. * The stride is either expressed as a multiple of 64 bytes chunks for
  9342. * linear buffers or in number of tiles for tiled buffers.
  9343. */
  9344. if (intel_rotation_90_or_270(rotation)) {
  9345. /* stride = Surface height in tiles */
  9346. tile_height = intel_tile_height(dev, fb->pixel_format,
  9347. fb->modifier[0], 0);
  9348. stride = DIV_ROUND_UP(fb->height, tile_height);
  9349. } else {
  9350. stride = fb->pitches[0] /
  9351. intel_fb_stride_alignment(dev, fb->modifier[0],
  9352. fb->pixel_format);
  9353. }
  9354. /*
  9355. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9356. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9357. */
  9358. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9359. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9360. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9361. POSTING_READ(PLANE_SURF(pipe, 0));
  9362. }
  9363. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9364. struct intel_unpin_work *work)
  9365. {
  9366. struct drm_device *dev = intel_crtc->base.dev;
  9367. struct drm_i915_private *dev_priv = dev->dev_private;
  9368. struct intel_framebuffer *intel_fb =
  9369. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9370. struct drm_i915_gem_object *obj = intel_fb->obj;
  9371. u32 dspcntr;
  9372. u32 reg;
  9373. reg = DSPCNTR(intel_crtc->plane);
  9374. dspcntr = I915_READ(reg);
  9375. if (obj->tiling_mode != I915_TILING_NONE)
  9376. dspcntr |= DISPPLANE_TILED;
  9377. else
  9378. dspcntr &= ~DISPPLANE_TILED;
  9379. I915_WRITE(reg, dspcntr);
  9380. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9381. POSTING_READ(DSPSURF(intel_crtc->plane));
  9382. }
  9383. /*
  9384. * XXX: This is the temporary way to update the plane registers until we get
  9385. * around to using the usual plane update functions for MMIO flips
  9386. */
  9387. static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
  9388. {
  9389. struct intel_crtc *crtc = mmio_flip->crtc;
  9390. struct intel_unpin_work *work;
  9391. spin_lock_irq(&crtc->base.dev->event_lock);
  9392. work = crtc->unpin_work;
  9393. spin_unlock_irq(&crtc->base.dev->event_lock);
  9394. if (work == NULL)
  9395. return;
  9396. intel_mark_page_flip_active(work);
  9397. intel_pipe_update_start(crtc);
  9398. if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
  9399. skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
  9400. else
  9401. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9402. ilk_do_mmio_flip(crtc, work);
  9403. intel_pipe_update_end(crtc);
  9404. }
  9405. static void intel_mmio_flip_work_func(struct work_struct *work)
  9406. {
  9407. struct intel_mmio_flip *mmio_flip =
  9408. container_of(work, struct intel_mmio_flip, work);
  9409. if (mmio_flip->req) {
  9410. WARN_ON(__i915_wait_request(mmio_flip->req,
  9411. mmio_flip->crtc->reset_counter,
  9412. false, NULL,
  9413. &mmio_flip->i915->rps.mmioflips));
  9414. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9415. }
  9416. intel_do_mmio_flip(mmio_flip);
  9417. kfree(mmio_flip);
  9418. }
  9419. static int intel_queue_mmio_flip(struct drm_device *dev,
  9420. struct drm_crtc *crtc,
  9421. struct drm_i915_gem_object *obj)
  9422. {
  9423. struct intel_mmio_flip *mmio_flip;
  9424. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9425. if (mmio_flip == NULL)
  9426. return -ENOMEM;
  9427. mmio_flip->i915 = to_i915(dev);
  9428. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9429. mmio_flip->crtc = to_intel_crtc(crtc);
  9430. mmio_flip->rotation = crtc->primary->state->rotation;
  9431. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9432. schedule_work(&mmio_flip->work);
  9433. return 0;
  9434. }
  9435. static int intel_default_queue_flip(struct drm_device *dev,
  9436. struct drm_crtc *crtc,
  9437. struct drm_framebuffer *fb,
  9438. struct drm_i915_gem_object *obj,
  9439. struct drm_i915_gem_request *req,
  9440. uint32_t flags)
  9441. {
  9442. return -ENODEV;
  9443. }
  9444. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9445. struct drm_crtc *crtc)
  9446. {
  9447. struct drm_i915_private *dev_priv = dev->dev_private;
  9448. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9449. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9450. u32 addr;
  9451. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9452. return true;
  9453. if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
  9454. return false;
  9455. if (!work->enable_stall_check)
  9456. return false;
  9457. if (work->flip_ready_vblank == 0) {
  9458. if (work->flip_queued_req &&
  9459. !i915_gem_request_completed(work->flip_queued_req, true))
  9460. return false;
  9461. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9462. }
  9463. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9464. return false;
  9465. /* Potential stall - if we see that the flip has happened,
  9466. * assume a missed interrupt. */
  9467. if (INTEL_INFO(dev)->gen >= 4)
  9468. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9469. else
  9470. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9471. /* There is a potential issue here with a false positive after a flip
  9472. * to the same address. We could address this by checking for a
  9473. * non-incrementing frame counter.
  9474. */
  9475. return addr == work->gtt_offset;
  9476. }
  9477. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9478. {
  9479. struct drm_i915_private *dev_priv = dev->dev_private;
  9480. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9481. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9482. struct intel_unpin_work *work;
  9483. WARN_ON(!in_interrupt());
  9484. if (crtc == NULL)
  9485. return;
  9486. spin_lock(&dev->event_lock);
  9487. work = intel_crtc->unpin_work;
  9488. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9489. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9490. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9491. page_flip_completed(intel_crtc);
  9492. work = NULL;
  9493. }
  9494. if (work != NULL &&
  9495. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9496. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9497. spin_unlock(&dev->event_lock);
  9498. }
  9499. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9500. struct drm_framebuffer *fb,
  9501. struct drm_pending_vblank_event *event,
  9502. uint32_t page_flip_flags)
  9503. {
  9504. struct drm_device *dev = crtc->dev;
  9505. struct drm_i915_private *dev_priv = dev->dev_private;
  9506. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9507. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9508. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9509. struct drm_plane *primary = crtc->primary;
  9510. enum pipe pipe = intel_crtc->pipe;
  9511. struct intel_unpin_work *work;
  9512. struct intel_engine_cs *ring;
  9513. bool mmio_flip;
  9514. struct drm_i915_gem_request *request = NULL;
  9515. int ret;
  9516. /*
  9517. * drm_mode_page_flip_ioctl() should already catch this, but double
  9518. * check to be safe. In the future we may enable pageflipping from
  9519. * a disabled primary plane.
  9520. */
  9521. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9522. return -EBUSY;
  9523. /* Can't change pixel format via MI display flips. */
  9524. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9525. return -EINVAL;
  9526. /*
  9527. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9528. * Note that pitch changes could also affect these register.
  9529. */
  9530. if (INTEL_INFO(dev)->gen > 3 &&
  9531. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9532. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9533. return -EINVAL;
  9534. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9535. goto out_hang;
  9536. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9537. if (work == NULL)
  9538. return -ENOMEM;
  9539. work->event = event;
  9540. work->crtc = crtc;
  9541. work->old_fb = old_fb;
  9542. INIT_WORK(&work->work, intel_unpin_work_fn);
  9543. ret = drm_crtc_vblank_get(crtc);
  9544. if (ret)
  9545. goto free_work;
  9546. /* We borrow the event spin lock for protecting unpin_work */
  9547. spin_lock_irq(&dev->event_lock);
  9548. if (intel_crtc->unpin_work) {
  9549. /* Before declaring the flip queue wedged, check if
  9550. * the hardware completed the operation behind our backs.
  9551. */
  9552. if (__intel_pageflip_stall_check(dev, crtc)) {
  9553. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9554. page_flip_completed(intel_crtc);
  9555. } else {
  9556. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9557. spin_unlock_irq(&dev->event_lock);
  9558. drm_crtc_vblank_put(crtc);
  9559. kfree(work);
  9560. return -EBUSY;
  9561. }
  9562. }
  9563. intel_crtc->unpin_work = work;
  9564. spin_unlock_irq(&dev->event_lock);
  9565. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9566. flush_workqueue(dev_priv->wq);
  9567. /* Reference the objects for the scheduled work. */
  9568. drm_framebuffer_reference(work->old_fb);
  9569. drm_gem_object_reference(&obj->base);
  9570. crtc->primary->fb = fb;
  9571. update_state_fb(crtc->primary);
  9572. work->pending_flip_obj = obj;
  9573. ret = i915_mutex_lock_interruptible(dev);
  9574. if (ret)
  9575. goto cleanup;
  9576. atomic_inc(&intel_crtc->unpin_work_count);
  9577. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9578. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9579. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9580. if (IS_VALLEYVIEW(dev)) {
  9581. ring = &dev_priv->ring[BCS];
  9582. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9583. /* vlv: DISPLAY_FLIP fails to change tiling */
  9584. ring = NULL;
  9585. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9586. ring = &dev_priv->ring[BCS];
  9587. } else if (INTEL_INFO(dev)->gen >= 7) {
  9588. ring = i915_gem_request_get_ring(obj->last_write_req);
  9589. if (ring == NULL || ring->id != RCS)
  9590. ring = &dev_priv->ring[BCS];
  9591. } else {
  9592. ring = &dev_priv->ring[RCS];
  9593. }
  9594. mmio_flip = use_mmio_flip(ring, obj);
  9595. /* When using CS flips, we want to emit semaphores between rings.
  9596. * However, when using mmio flips we will create a task to do the
  9597. * synchronisation, so all we want here is to pin the framebuffer
  9598. * into the display plane and skip any waits.
  9599. */
  9600. if (!mmio_flip) {
  9601. ret = i915_gem_object_sync(obj, ring, &request);
  9602. if (ret)
  9603. goto cleanup_pending;
  9604. }
  9605. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9606. crtc->primary->state);
  9607. if (ret)
  9608. goto cleanup_pending;
  9609. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9610. obj, 0);
  9611. work->gtt_offset += intel_crtc->dspaddr_offset;
  9612. if (mmio_flip) {
  9613. ret = intel_queue_mmio_flip(dev, crtc, obj);
  9614. if (ret)
  9615. goto cleanup_unpin;
  9616. i915_gem_request_assign(&work->flip_queued_req,
  9617. obj->last_write_req);
  9618. } else {
  9619. if (!request) {
  9620. ret = i915_gem_request_alloc(ring, ring->default_context, &request);
  9621. if (ret)
  9622. goto cleanup_unpin;
  9623. }
  9624. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9625. page_flip_flags);
  9626. if (ret)
  9627. goto cleanup_unpin;
  9628. i915_gem_request_assign(&work->flip_queued_req, request);
  9629. }
  9630. if (request)
  9631. i915_add_request_no_flush(request);
  9632. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9633. work->enable_stall_check = true;
  9634. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9635. to_intel_plane(primary)->frontbuffer_bit);
  9636. mutex_unlock(&dev->struct_mutex);
  9637. intel_fbc_disable_crtc(intel_crtc);
  9638. intel_frontbuffer_flip_prepare(dev,
  9639. to_intel_plane(primary)->frontbuffer_bit);
  9640. trace_i915_flip_request(intel_crtc->plane, obj);
  9641. return 0;
  9642. cleanup_unpin:
  9643. intel_unpin_fb_obj(fb, crtc->primary->state);
  9644. cleanup_pending:
  9645. if (request)
  9646. i915_gem_request_cancel(request);
  9647. atomic_dec(&intel_crtc->unpin_work_count);
  9648. mutex_unlock(&dev->struct_mutex);
  9649. cleanup:
  9650. crtc->primary->fb = old_fb;
  9651. update_state_fb(crtc->primary);
  9652. drm_gem_object_unreference_unlocked(&obj->base);
  9653. drm_framebuffer_unreference(work->old_fb);
  9654. spin_lock_irq(&dev->event_lock);
  9655. intel_crtc->unpin_work = NULL;
  9656. spin_unlock_irq(&dev->event_lock);
  9657. drm_crtc_vblank_put(crtc);
  9658. free_work:
  9659. kfree(work);
  9660. if (ret == -EIO) {
  9661. struct drm_atomic_state *state;
  9662. struct drm_plane_state *plane_state;
  9663. out_hang:
  9664. state = drm_atomic_state_alloc(dev);
  9665. if (!state)
  9666. return -ENOMEM;
  9667. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9668. retry:
  9669. plane_state = drm_atomic_get_plane_state(state, primary);
  9670. ret = PTR_ERR_OR_ZERO(plane_state);
  9671. if (!ret) {
  9672. drm_atomic_set_fb_for_plane(plane_state, fb);
  9673. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9674. if (!ret)
  9675. ret = drm_atomic_commit(state);
  9676. }
  9677. if (ret == -EDEADLK) {
  9678. drm_modeset_backoff(state->acquire_ctx);
  9679. drm_atomic_state_clear(state);
  9680. goto retry;
  9681. }
  9682. if (ret)
  9683. drm_atomic_state_free(state);
  9684. if (ret == 0 && event) {
  9685. spin_lock_irq(&dev->event_lock);
  9686. drm_send_vblank_event(dev, pipe, event);
  9687. spin_unlock_irq(&dev->event_lock);
  9688. }
  9689. }
  9690. return ret;
  9691. }
  9692. /**
  9693. * intel_wm_need_update - Check whether watermarks need updating
  9694. * @plane: drm plane
  9695. * @state: new plane state
  9696. *
  9697. * Check current plane state versus the new one to determine whether
  9698. * watermarks need to be recalculated.
  9699. *
  9700. * Returns true or false.
  9701. */
  9702. static bool intel_wm_need_update(struct drm_plane *plane,
  9703. struct drm_plane_state *state)
  9704. {
  9705. struct intel_plane_state *new = to_intel_plane_state(state);
  9706. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9707. /* Update watermarks on tiling or size changes. */
  9708. if (!plane->state->fb || !state->fb ||
  9709. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  9710. plane->state->rotation != state->rotation ||
  9711. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9712. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9713. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9714. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9715. return true;
  9716. return false;
  9717. }
  9718. static bool needs_scaling(struct intel_plane_state *state)
  9719. {
  9720. int src_w = drm_rect_width(&state->src) >> 16;
  9721. int src_h = drm_rect_height(&state->src) >> 16;
  9722. int dst_w = drm_rect_width(&state->dst);
  9723. int dst_h = drm_rect_height(&state->dst);
  9724. return (src_w != dst_w || src_h != dst_h);
  9725. }
  9726. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9727. struct drm_plane_state *plane_state)
  9728. {
  9729. struct drm_crtc *crtc = crtc_state->crtc;
  9730. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9731. struct drm_plane *plane = plane_state->plane;
  9732. struct drm_device *dev = crtc->dev;
  9733. struct drm_i915_private *dev_priv = dev->dev_private;
  9734. struct intel_plane_state *old_plane_state =
  9735. to_intel_plane_state(plane->state);
  9736. int idx = intel_crtc->base.base.id, ret;
  9737. int i = drm_plane_index(plane);
  9738. bool mode_changed = needs_modeset(crtc_state);
  9739. bool was_crtc_enabled = crtc->state->active;
  9740. bool is_crtc_enabled = crtc_state->active;
  9741. bool turn_off, turn_on, visible, was_visible;
  9742. struct drm_framebuffer *fb = plane_state->fb;
  9743. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9744. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9745. ret = skl_update_scaler_plane(
  9746. to_intel_crtc_state(crtc_state),
  9747. to_intel_plane_state(plane_state));
  9748. if (ret)
  9749. return ret;
  9750. }
  9751. was_visible = old_plane_state->visible;
  9752. visible = to_intel_plane_state(plane_state)->visible;
  9753. if (!was_crtc_enabled && WARN_ON(was_visible))
  9754. was_visible = false;
  9755. if (!is_crtc_enabled && WARN_ON(visible))
  9756. visible = false;
  9757. if (!was_visible && !visible)
  9758. return 0;
  9759. turn_off = was_visible && (!visible || mode_changed);
  9760. turn_on = visible && (!was_visible || mode_changed);
  9761. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9762. plane->base.id, fb ? fb->base.id : -1);
  9763. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9764. plane->base.id, was_visible, visible,
  9765. turn_off, turn_on, mode_changed);
  9766. if (turn_on) {
  9767. intel_crtc->atomic.update_wm_pre = true;
  9768. /* must disable cxsr around plane enable/disable */
  9769. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9770. intel_crtc->atomic.disable_cxsr = true;
  9771. /* to potentially re-enable cxsr */
  9772. intel_crtc->atomic.wait_vblank = true;
  9773. intel_crtc->atomic.update_wm_post = true;
  9774. }
  9775. } else if (turn_off) {
  9776. intel_crtc->atomic.update_wm_post = true;
  9777. /* must disable cxsr around plane enable/disable */
  9778. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9779. if (is_crtc_enabled)
  9780. intel_crtc->atomic.wait_vblank = true;
  9781. intel_crtc->atomic.disable_cxsr = true;
  9782. }
  9783. } else if (intel_wm_need_update(plane, plane_state)) {
  9784. intel_crtc->atomic.update_wm_pre = true;
  9785. }
  9786. if (visible || was_visible)
  9787. intel_crtc->atomic.fb_bits |=
  9788. to_intel_plane(plane)->frontbuffer_bit;
  9789. switch (plane->type) {
  9790. case DRM_PLANE_TYPE_PRIMARY:
  9791. intel_crtc->atomic.pre_disable_primary = turn_off;
  9792. intel_crtc->atomic.post_enable_primary = turn_on;
  9793. if (turn_off) {
  9794. /*
  9795. * FIXME: Actually if we will still have any other
  9796. * plane enabled on the pipe we could let IPS enabled
  9797. * still, but for now lets consider that when we make
  9798. * primary invisible by setting DSPCNTR to 0 on
  9799. * update_primary_plane function IPS needs to be
  9800. * disable.
  9801. */
  9802. intel_crtc->atomic.disable_ips = true;
  9803. intel_crtc->atomic.disable_fbc = true;
  9804. }
  9805. /*
  9806. * FBC does not work on some platforms for rotated
  9807. * planes, so disable it when rotation is not 0 and
  9808. * update it when rotation is set back to 0.
  9809. *
  9810. * FIXME: This is redundant with the fbc update done in
  9811. * the primary plane enable function except that that
  9812. * one is done too late. We eventually need to unify
  9813. * this.
  9814. */
  9815. if (visible &&
  9816. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9817. dev_priv->fbc.crtc == intel_crtc &&
  9818. plane_state->rotation != BIT(DRM_ROTATE_0))
  9819. intel_crtc->atomic.disable_fbc = true;
  9820. /*
  9821. * BDW signals flip done immediately if the plane
  9822. * is disabled, even if the plane enable is already
  9823. * armed to occur at the next vblank :(
  9824. */
  9825. if (turn_on && IS_BROADWELL(dev))
  9826. intel_crtc->atomic.wait_vblank = true;
  9827. intel_crtc->atomic.update_fbc |= visible || mode_changed;
  9828. break;
  9829. case DRM_PLANE_TYPE_CURSOR:
  9830. break;
  9831. case DRM_PLANE_TYPE_OVERLAY:
  9832. /*
  9833. * WaCxSRDisabledForSpriteScaling:ivb
  9834. *
  9835. * cstate->update_wm was already set above, so this flag will
  9836. * take effect when we commit and program watermarks.
  9837. */
  9838. if (IS_IVYBRIDGE(dev) &&
  9839. needs_scaling(to_intel_plane_state(plane_state)) &&
  9840. !needs_scaling(old_plane_state)) {
  9841. to_intel_crtc_state(crtc_state)->disable_lp_wm = true;
  9842. } else if (turn_off && !mode_changed) {
  9843. intel_crtc->atomic.wait_vblank = true;
  9844. intel_crtc->atomic.update_sprite_watermarks |=
  9845. 1 << i;
  9846. }
  9847. break;
  9848. }
  9849. return 0;
  9850. }
  9851. static bool encoders_cloneable(const struct intel_encoder *a,
  9852. const struct intel_encoder *b)
  9853. {
  9854. /* masks could be asymmetric, so check both ways */
  9855. return a == b || (a->cloneable & (1 << b->type) &&
  9856. b->cloneable & (1 << a->type));
  9857. }
  9858. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9859. struct intel_crtc *crtc,
  9860. struct intel_encoder *encoder)
  9861. {
  9862. struct intel_encoder *source_encoder;
  9863. struct drm_connector *connector;
  9864. struct drm_connector_state *connector_state;
  9865. int i;
  9866. for_each_connector_in_state(state, connector, connector_state, i) {
  9867. if (connector_state->crtc != &crtc->base)
  9868. continue;
  9869. source_encoder =
  9870. to_intel_encoder(connector_state->best_encoder);
  9871. if (!encoders_cloneable(encoder, source_encoder))
  9872. return false;
  9873. }
  9874. return true;
  9875. }
  9876. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9877. struct intel_crtc *crtc)
  9878. {
  9879. struct intel_encoder *encoder;
  9880. struct drm_connector *connector;
  9881. struct drm_connector_state *connector_state;
  9882. int i;
  9883. for_each_connector_in_state(state, connector, connector_state, i) {
  9884. if (connector_state->crtc != &crtc->base)
  9885. continue;
  9886. encoder = to_intel_encoder(connector_state->best_encoder);
  9887. if (!check_single_encoder_cloning(state, crtc, encoder))
  9888. return false;
  9889. }
  9890. return true;
  9891. }
  9892. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9893. struct drm_crtc_state *crtc_state)
  9894. {
  9895. struct drm_device *dev = crtc->dev;
  9896. struct drm_i915_private *dev_priv = dev->dev_private;
  9897. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9898. struct intel_crtc_state *pipe_config =
  9899. to_intel_crtc_state(crtc_state);
  9900. struct drm_atomic_state *state = crtc_state->state;
  9901. int ret;
  9902. bool mode_changed = needs_modeset(crtc_state);
  9903. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  9904. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9905. return -EINVAL;
  9906. }
  9907. if (mode_changed && !crtc_state->active)
  9908. intel_crtc->atomic.update_wm_post = true;
  9909. if (mode_changed && crtc_state->enable &&
  9910. dev_priv->display.crtc_compute_clock &&
  9911. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  9912. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9913. pipe_config);
  9914. if (ret)
  9915. return ret;
  9916. }
  9917. ret = 0;
  9918. if (dev_priv->display.compute_pipe_wm) {
  9919. ret = dev_priv->display.compute_pipe_wm(intel_crtc, state);
  9920. if (ret)
  9921. return ret;
  9922. }
  9923. if (INTEL_INFO(dev)->gen >= 9) {
  9924. if (mode_changed)
  9925. ret = skl_update_scaler_crtc(pipe_config);
  9926. if (!ret)
  9927. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  9928. pipe_config);
  9929. }
  9930. return ret;
  9931. }
  9932. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9933. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9934. .load_lut = intel_crtc_load_lut,
  9935. .atomic_begin = intel_begin_crtc_commit,
  9936. .atomic_flush = intel_finish_crtc_commit,
  9937. .atomic_check = intel_crtc_atomic_check,
  9938. };
  9939. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9940. {
  9941. struct intel_connector *connector;
  9942. for_each_intel_connector(dev, connector) {
  9943. if (connector->base.encoder) {
  9944. connector->base.state->best_encoder =
  9945. connector->base.encoder;
  9946. connector->base.state->crtc =
  9947. connector->base.encoder->crtc;
  9948. } else {
  9949. connector->base.state->best_encoder = NULL;
  9950. connector->base.state->crtc = NULL;
  9951. }
  9952. }
  9953. }
  9954. static void
  9955. connected_sink_compute_bpp(struct intel_connector *connector,
  9956. struct intel_crtc_state *pipe_config)
  9957. {
  9958. int bpp = pipe_config->pipe_bpp;
  9959. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9960. connector->base.base.id,
  9961. connector->base.name);
  9962. /* Don't use an invalid EDID bpc value */
  9963. if (connector->base.display_info.bpc &&
  9964. connector->base.display_info.bpc * 3 < bpp) {
  9965. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9966. bpp, connector->base.display_info.bpc*3);
  9967. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9968. }
  9969. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9970. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9971. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9972. bpp);
  9973. pipe_config->pipe_bpp = 24;
  9974. }
  9975. }
  9976. static int
  9977. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9978. struct intel_crtc_state *pipe_config)
  9979. {
  9980. struct drm_device *dev = crtc->base.dev;
  9981. struct drm_atomic_state *state;
  9982. struct drm_connector *connector;
  9983. struct drm_connector_state *connector_state;
  9984. int bpp, i;
  9985. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9986. bpp = 10*3;
  9987. else if (INTEL_INFO(dev)->gen >= 5)
  9988. bpp = 12*3;
  9989. else
  9990. bpp = 8*3;
  9991. pipe_config->pipe_bpp = bpp;
  9992. state = pipe_config->base.state;
  9993. /* Clamp display bpp to EDID value */
  9994. for_each_connector_in_state(state, connector, connector_state, i) {
  9995. if (connector_state->crtc != &crtc->base)
  9996. continue;
  9997. connected_sink_compute_bpp(to_intel_connector(connector),
  9998. pipe_config);
  9999. }
  10000. return bpp;
  10001. }
  10002. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10003. {
  10004. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10005. "type: 0x%x flags: 0x%x\n",
  10006. mode->crtc_clock,
  10007. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10008. mode->crtc_hsync_end, mode->crtc_htotal,
  10009. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10010. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10011. }
  10012. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10013. struct intel_crtc_state *pipe_config,
  10014. const char *context)
  10015. {
  10016. struct drm_device *dev = crtc->base.dev;
  10017. struct drm_plane *plane;
  10018. struct intel_plane *intel_plane;
  10019. struct intel_plane_state *state;
  10020. struct drm_framebuffer *fb;
  10021. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  10022. context, pipe_config, pipe_name(crtc->pipe));
  10023. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  10024. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10025. pipe_config->pipe_bpp, pipe_config->dither);
  10026. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10027. pipe_config->has_pch_encoder,
  10028. pipe_config->fdi_lanes,
  10029. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10030. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10031. pipe_config->fdi_m_n.tu);
  10032. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10033. pipe_config->has_dp_encoder,
  10034. pipe_config->lane_count,
  10035. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10036. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10037. pipe_config->dp_m_n.tu);
  10038. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10039. pipe_config->has_dp_encoder,
  10040. pipe_config->lane_count,
  10041. pipe_config->dp_m2_n2.gmch_m,
  10042. pipe_config->dp_m2_n2.gmch_n,
  10043. pipe_config->dp_m2_n2.link_m,
  10044. pipe_config->dp_m2_n2.link_n,
  10045. pipe_config->dp_m2_n2.tu);
  10046. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10047. pipe_config->has_audio,
  10048. pipe_config->has_infoframe);
  10049. DRM_DEBUG_KMS("requested mode:\n");
  10050. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10051. DRM_DEBUG_KMS("adjusted mode:\n");
  10052. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10053. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10054. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10055. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10056. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10057. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10058. crtc->num_scalers,
  10059. pipe_config->scaler_state.scaler_users,
  10060. pipe_config->scaler_state.scaler_id);
  10061. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10062. pipe_config->gmch_pfit.control,
  10063. pipe_config->gmch_pfit.pgm_ratios,
  10064. pipe_config->gmch_pfit.lvds_border_bits);
  10065. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10066. pipe_config->pch_pfit.pos,
  10067. pipe_config->pch_pfit.size,
  10068. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10069. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10070. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10071. if (IS_BROXTON(dev)) {
  10072. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10073. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10074. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10075. pipe_config->ddi_pll_sel,
  10076. pipe_config->dpll_hw_state.ebb0,
  10077. pipe_config->dpll_hw_state.ebb4,
  10078. pipe_config->dpll_hw_state.pll0,
  10079. pipe_config->dpll_hw_state.pll1,
  10080. pipe_config->dpll_hw_state.pll2,
  10081. pipe_config->dpll_hw_state.pll3,
  10082. pipe_config->dpll_hw_state.pll6,
  10083. pipe_config->dpll_hw_state.pll8,
  10084. pipe_config->dpll_hw_state.pll9,
  10085. pipe_config->dpll_hw_state.pll10,
  10086. pipe_config->dpll_hw_state.pcsdw12);
  10087. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10088. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10089. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10090. pipe_config->ddi_pll_sel,
  10091. pipe_config->dpll_hw_state.ctrl1,
  10092. pipe_config->dpll_hw_state.cfgcr1,
  10093. pipe_config->dpll_hw_state.cfgcr2);
  10094. } else if (HAS_DDI(dev)) {
  10095. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  10096. pipe_config->ddi_pll_sel,
  10097. pipe_config->dpll_hw_state.wrpll);
  10098. } else {
  10099. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10100. "fp0: 0x%x, fp1: 0x%x\n",
  10101. pipe_config->dpll_hw_state.dpll,
  10102. pipe_config->dpll_hw_state.dpll_md,
  10103. pipe_config->dpll_hw_state.fp0,
  10104. pipe_config->dpll_hw_state.fp1);
  10105. }
  10106. DRM_DEBUG_KMS("planes on this crtc\n");
  10107. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10108. intel_plane = to_intel_plane(plane);
  10109. if (intel_plane->pipe != crtc->pipe)
  10110. continue;
  10111. state = to_intel_plane_state(plane->state);
  10112. fb = state->base.fb;
  10113. if (!fb) {
  10114. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10115. "disabled, scaler_id = %d\n",
  10116. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10117. plane->base.id, intel_plane->pipe,
  10118. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10119. drm_plane_index(plane), state->scaler_id);
  10120. continue;
  10121. }
  10122. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10123. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10124. plane->base.id, intel_plane->pipe,
  10125. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10126. drm_plane_index(plane));
  10127. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10128. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10129. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10130. state->scaler_id,
  10131. state->src.x1 >> 16, state->src.y1 >> 16,
  10132. drm_rect_width(&state->src) >> 16,
  10133. drm_rect_height(&state->src) >> 16,
  10134. state->dst.x1, state->dst.y1,
  10135. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10136. }
  10137. }
  10138. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10139. {
  10140. struct drm_device *dev = state->dev;
  10141. struct intel_encoder *encoder;
  10142. struct drm_connector *connector;
  10143. struct drm_connector_state *connector_state;
  10144. unsigned int used_ports = 0;
  10145. int i;
  10146. /*
  10147. * Walk the connector list instead of the encoder
  10148. * list to detect the problem on ddi platforms
  10149. * where there's just one encoder per digital port.
  10150. */
  10151. for_each_connector_in_state(state, connector, connector_state, i) {
  10152. if (!connector_state->best_encoder)
  10153. continue;
  10154. encoder = to_intel_encoder(connector_state->best_encoder);
  10155. WARN_ON(!connector_state->crtc);
  10156. switch (encoder->type) {
  10157. unsigned int port_mask;
  10158. case INTEL_OUTPUT_UNKNOWN:
  10159. if (WARN_ON(!HAS_DDI(dev)))
  10160. break;
  10161. case INTEL_OUTPUT_DISPLAYPORT:
  10162. case INTEL_OUTPUT_HDMI:
  10163. case INTEL_OUTPUT_EDP:
  10164. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10165. /* the same port mustn't appear more than once */
  10166. if (used_ports & port_mask)
  10167. return false;
  10168. used_ports |= port_mask;
  10169. default:
  10170. break;
  10171. }
  10172. }
  10173. return true;
  10174. }
  10175. static void
  10176. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10177. {
  10178. struct drm_crtc_state tmp_state;
  10179. struct intel_crtc_scaler_state scaler_state;
  10180. struct intel_dpll_hw_state dpll_hw_state;
  10181. enum intel_dpll_id shared_dpll;
  10182. uint32_t ddi_pll_sel;
  10183. bool force_thru;
  10184. /* FIXME: before the switch to atomic started, a new pipe_config was
  10185. * kzalloc'd. Code that depends on any field being zero should be
  10186. * fixed, so that the crtc_state can be safely duplicated. For now,
  10187. * only fields that are know to not cause problems are preserved. */
  10188. tmp_state = crtc_state->base;
  10189. scaler_state = crtc_state->scaler_state;
  10190. shared_dpll = crtc_state->shared_dpll;
  10191. dpll_hw_state = crtc_state->dpll_hw_state;
  10192. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10193. force_thru = crtc_state->pch_pfit.force_thru;
  10194. memset(crtc_state, 0, sizeof *crtc_state);
  10195. crtc_state->base = tmp_state;
  10196. crtc_state->scaler_state = scaler_state;
  10197. crtc_state->shared_dpll = shared_dpll;
  10198. crtc_state->dpll_hw_state = dpll_hw_state;
  10199. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10200. crtc_state->pch_pfit.force_thru = force_thru;
  10201. }
  10202. static int
  10203. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10204. struct intel_crtc_state *pipe_config)
  10205. {
  10206. struct drm_atomic_state *state = pipe_config->base.state;
  10207. struct intel_encoder *encoder;
  10208. struct drm_connector *connector;
  10209. struct drm_connector_state *connector_state;
  10210. int base_bpp, ret = -EINVAL;
  10211. int i;
  10212. bool retry = true;
  10213. clear_intel_crtc_state(pipe_config);
  10214. pipe_config->cpu_transcoder =
  10215. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10216. /*
  10217. * Sanitize sync polarity flags based on requested ones. If neither
  10218. * positive or negative polarity is requested, treat this as meaning
  10219. * negative polarity.
  10220. */
  10221. if (!(pipe_config->base.adjusted_mode.flags &
  10222. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10223. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10224. if (!(pipe_config->base.adjusted_mode.flags &
  10225. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10226. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10227. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10228. pipe_config);
  10229. if (base_bpp < 0)
  10230. goto fail;
  10231. /*
  10232. * Determine the real pipe dimensions. Note that stereo modes can
  10233. * increase the actual pipe size due to the frame doubling and
  10234. * insertion of additional space for blanks between the frame. This
  10235. * is stored in the crtc timings. We use the requested mode to do this
  10236. * computation to clearly distinguish it from the adjusted mode, which
  10237. * can be changed by the connectors in the below retry loop.
  10238. */
  10239. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10240. &pipe_config->pipe_src_w,
  10241. &pipe_config->pipe_src_h);
  10242. encoder_retry:
  10243. /* Ensure the port clock defaults are reset when retrying. */
  10244. pipe_config->port_clock = 0;
  10245. pipe_config->pixel_multiplier = 1;
  10246. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10247. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10248. CRTC_STEREO_DOUBLE);
  10249. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10250. * adjust it according to limitations or connector properties, and also
  10251. * a chance to reject the mode entirely.
  10252. */
  10253. for_each_connector_in_state(state, connector, connector_state, i) {
  10254. if (connector_state->crtc != crtc)
  10255. continue;
  10256. encoder = to_intel_encoder(connector_state->best_encoder);
  10257. if (!(encoder->compute_config(encoder, pipe_config))) {
  10258. DRM_DEBUG_KMS("Encoder config failure\n");
  10259. goto fail;
  10260. }
  10261. }
  10262. /* Set default port clock if not overwritten by the encoder. Needs to be
  10263. * done afterwards in case the encoder adjusts the mode. */
  10264. if (!pipe_config->port_clock)
  10265. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10266. * pipe_config->pixel_multiplier;
  10267. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10268. if (ret < 0) {
  10269. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10270. goto fail;
  10271. }
  10272. if (ret == RETRY) {
  10273. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10274. ret = -EINVAL;
  10275. goto fail;
  10276. }
  10277. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10278. retry = false;
  10279. goto encoder_retry;
  10280. }
  10281. /* Dithering seems to not pass-through bits correctly when it should, so
  10282. * only enable it on 6bpc panels. */
  10283. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10284. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10285. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10286. fail:
  10287. return ret;
  10288. }
  10289. static void
  10290. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10291. {
  10292. struct drm_crtc *crtc;
  10293. struct drm_crtc_state *crtc_state;
  10294. int i;
  10295. /* Double check state. */
  10296. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10297. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10298. /* Update hwmode for vblank functions */
  10299. if (crtc->state->active)
  10300. crtc->hwmode = crtc->state->adjusted_mode;
  10301. else
  10302. crtc->hwmode.crtc_clock = 0;
  10303. /*
  10304. * Update legacy state to satisfy fbc code. This can
  10305. * be removed when fbc uses the atomic state.
  10306. */
  10307. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10308. struct drm_plane_state *plane_state = crtc->primary->state;
  10309. crtc->primary->fb = plane_state->fb;
  10310. crtc->x = plane_state->src_x >> 16;
  10311. crtc->y = plane_state->src_y >> 16;
  10312. }
  10313. }
  10314. }
  10315. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10316. {
  10317. int diff;
  10318. if (clock1 == clock2)
  10319. return true;
  10320. if (!clock1 || !clock2)
  10321. return false;
  10322. diff = abs(clock1 - clock2);
  10323. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10324. return true;
  10325. return false;
  10326. }
  10327. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10328. list_for_each_entry((intel_crtc), \
  10329. &(dev)->mode_config.crtc_list, \
  10330. base.head) \
  10331. if (mask & (1 <<(intel_crtc)->pipe))
  10332. static bool
  10333. intel_compare_m_n(unsigned int m, unsigned int n,
  10334. unsigned int m2, unsigned int n2,
  10335. bool exact)
  10336. {
  10337. if (m == m2 && n == n2)
  10338. return true;
  10339. if (exact || !m || !n || !m2 || !n2)
  10340. return false;
  10341. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10342. if (m > m2) {
  10343. while (m > m2) {
  10344. m2 <<= 1;
  10345. n2 <<= 1;
  10346. }
  10347. } else if (m < m2) {
  10348. while (m < m2) {
  10349. m <<= 1;
  10350. n <<= 1;
  10351. }
  10352. }
  10353. return m == m2 && n == n2;
  10354. }
  10355. static bool
  10356. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10357. struct intel_link_m_n *m2_n2,
  10358. bool adjust)
  10359. {
  10360. if (m_n->tu == m2_n2->tu &&
  10361. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10362. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10363. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10364. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10365. if (adjust)
  10366. *m2_n2 = *m_n;
  10367. return true;
  10368. }
  10369. return false;
  10370. }
  10371. static bool
  10372. intel_pipe_config_compare(struct drm_device *dev,
  10373. struct intel_crtc_state *current_config,
  10374. struct intel_crtc_state *pipe_config,
  10375. bool adjust)
  10376. {
  10377. bool ret = true;
  10378. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10379. do { \
  10380. if (!adjust) \
  10381. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10382. else \
  10383. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10384. } while (0)
  10385. #define PIPE_CONF_CHECK_X(name) \
  10386. if (current_config->name != pipe_config->name) { \
  10387. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10388. "(expected 0x%08x, found 0x%08x)\n", \
  10389. current_config->name, \
  10390. pipe_config->name); \
  10391. ret = false; \
  10392. }
  10393. #define PIPE_CONF_CHECK_I(name) \
  10394. if (current_config->name != pipe_config->name) { \
  10395. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10396. "(expected %i, found %i)\n", \
  10397. current_config->name, \
  10398. pipe_config->name); \
  10399. ret = false; \
  10400. }
  10401. #define PIPE_CONF_CHECK_M_N(name) \
  10402. if (!intel_compare_link_m_n(&current_config->name, \
  10403. &pipe_config->name,\
  10404. adjust)) { \
  10405. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10406. "(expected tu %i gmch %i/%i link %i/%i, " \
  10407. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10408. current_config->name.tu, \
  10409. current_config->name.gmch_m, \
  10410. current_config->name.gmch_n, \
  10411. current_config->name.link_m, \
  10412. current_config->name.link_n, \
  10413. pipe_config->name.tu, \
  10414. pipe_config->name.gmch_m, \
  10415. pipe_config->name.gmch_n, \
  10416. pipe_config->name.link_m, \
  10417. pipe_config->name.link_n); \
  10418. ret = false; \
  10419. }
  10420. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10421. if (!intel_compare_link_m_n(&current_config->name, \
  10422. &pipe_config->name, adjust) && \
  10423. !intel_compare_link_m_n(&current_config->alt_name, \
  10424. &pipe_config->name, adjust)) { \
  10425. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10426. "(expected tu %i gmch %i/%i link %i/%i, " \
  10427. "or tu %i gmch %i/%i link %i/%i, " \
  10428. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10429. current_config->name.tu, \
  10430. current_config->name.gmch_m, \
  10431. current_config->name.gmch_n, \
  10432. current_config->name.link_m, \
  10433. current_config->name.link_n, \
  10434. current_config->alt_name.tu, \
  10435. current_config->alt_name.gmch_m, \
  10436. current_config->alt_name.gmch_n, \
  10437. current_config->alt_name.link_m, \
  10438. current_config->alt_name.link_n, \
  10439. pipe_config->name.tu, \
  10440. pipe_config->name.gmch_m, \
  10441. pipe_config->name.gmch_n, \
  10442. pipe_config->name.link_m, \
  10443. pipe_config->name.link_n); \
  10444. ret = false; \
  10445. }
  10446. /* This is required for BDW+ where there is only one set of registers for
  10447. * switching between high and low RR.
  10448. * This macro can be used whenever a comparison has to be made between one
  10449. * hw state and multiple sw state variables.
  10450. */
  10451. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10452. if ((current_config->name != pipe_config->name) && \
  10453. (current_config->alt_name != pipe_config->name)) { \
  10454. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10455. "(expected %i or %i, found %i)\n", \
  10456. current_config->name, \
  10457. current_config->alt_name, \
  10458. pipe_config->name); \
  10459. ret = false; \
  10460. }
  10461. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10462. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10463. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10464. "(expected %i, found %i)\n", \
  10465. current_config->name & (mask), \
  10466. pipe_config->name & (mask)); \
  10467. ret = false; \
  10468. }
  10469. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10470. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10471. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10472. "(expected %i, found %i)\n", \
  10473. current_config->name, \
  10474. pipe_config->name); \
  10475. ret = false; \
  10476. }
  10477. #define PIPE_CONF_QUIRK(quirk) \
  10478. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10479. PIPE_CONF_CHECK_I(cpu_transcoder);
  10480. PIPE_CONF_CHECK_I(has_pch_encoder);
  10481. PIPE_CONF_CHECK_I(fdi_lanes);
  10482. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10483. PIPE_CONF_CHECK_I(has_dp_encoder);
  10484. PIPE_CONF_CHECK_I(lane_count);
  10485. if (INTEL_INFO(dev)->gen < 8) {
  10486. PIPE_CONF_CHECK_M_N(dp_m_n);
  10487. PIPE_CONF_CHECK_I(has_drrs);
  10488. if (current_config->has_drrs)
  10489. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10490. } else
  10491. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10492. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10493. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10494. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10495. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10496. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10497. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10498. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10499. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10500. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10501. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10502. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10503. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10504. PIPE_CONF_CHECK_I(pixel_multiplier);
  10505. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10506. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10507. IS_VALLEYVIEW(dev))
  10508. PIPE_CONF_CHECK_I(limited_color_range);
  10509. PIPE_CONF_CHECK_I(has_infoframe);
  10510. PIPE_CONF_CHECK_I(has_audio);
  10511. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10512. DRM_MODE_FLAG_INTERLACE);
  10513. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10514. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10515. DRM_MODE_FLAG_PHSYNC);
  10516. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10517. DRM_MODE_FLAG_NHSYNC);
  10518. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10519. DRM_MODE_FLAG_PVSYNC);
  10520. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10521. DRM_MODE_FLAG_NVSYNC);
  10522. }
  10523. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10524. /* pfit ratios are autocomputed by the hw on gen4+ */
  10525. if (INTEL_INFO(dev)->gen < 4)
  10526. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10527. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10528. if (!adjust) {
  10529. PIPE_CONF_CHECK_I(pipe_src_w);
  10530. PIPE_CONF_CHECK_I(pipe_src_h);
  10531. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10532. if (current_config->pch_pfit.enabled) {
  10533. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10534. PIPE_CONF_CHECK_X(pch_pfit.size);
  10535. }
  10536. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10537. }
  10538. /* BDW+ don't expose a synchronous way to read the state */
  10539. if (IS_HASWELL(dev))
  10540. PIPE_CONF_CHECK_I(ips_enabled);
  10541. PIPE_CONF_CHECK_I(double_wide);
  10542. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10543. PIPE_CONF_CHECK_I(shared_dpll);
  10544. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10545. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10546. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10547. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10548. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10549. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10550. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10551. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10552. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10553. PIPE_CONF_CHECK_I(pipe_bpp);
  10554. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10555. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10556. #undef PIPE_CONF_CHECK_X
  10557. #undef PIPE_CONF_CHECK_I
  10558. #undef PIPE_CONF_CHECK_I_ALT
  10559. #undef PIPE_CONF_CHECK_FLAGS
  10560. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10561. #undef PIPE_CONF_QUIRK
  10562. #undef INTEL_ERR_OR_DBG_KMS
  10563. return ret;
  10564. }
  10565. static void check_wm_state(struct drm_device *dev)
  10566. {
  10567. struct drm_i915_private *dev_priv = dev->dev_private;
  10568. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10569. struct intel_crtc *intel_crtc;
  10570. int plane;
  10571. if (INTEL_INFO(dev)->gen < 9)
  10572. return;
  10573. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10574. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10575. for_each_intel_crtc(dev, intel_crtc) {
  10576. struct skl_ddb_entry *hw_entry, *sw_entry;
  10577. const enum pipe pipe = intel_crtc->pipe;
  10578. if (!intel_crtc->active)
  10579. continue;
  10580. /* planes */
  10581. for_each_plane(dev_priv, pipe, plane) {
  10582. hw_entry = &hw_ddb.plane[pipe][plane];
  10583. sw_entry = &sw_ddb->plane[pipe][plane];
  10584. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10585. continue;
  10586. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10587. "(expected (%u,%u), found (%u,%u))\n",
  10588. pipe_name(pipe), plane + 1,
  10589. sw_entry->start, sw_entry->end,
  10590. hw_entry->start, hw_entry->end);
  10591. }
  10592. /* cursor */
  10593. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10594. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10595. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10596. continue;
  10597. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10598. "(expected (%u,%u), found (%u,%u))\n",
  10599. pipe_name(pipe),
  10600. sw_entry->start, sw_entry->end,
  10601. hw_entry->start, hw_entry->end);
  10602. }
  10603. }
  10604. static void
  10605. check_connector_state(struct drm_device *dev,
  10606. struct drm_atomic_state *old_state)
  10607. {
  10608. struct drm_connector_state *old_conn_state;
  10609. struct drm_connector *connector;
  10610. int i;
  10611. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  10612. struct drm_encoder *encoder = connector->encoder;
  10613. struct drm_connector_state *state = connector->state;
  10614. /* This also checks the encoder/connector hw state with the
  10615. * ->get_hw_state callbacks. */
  10616. intel_connector_check_state(to_intel_connector(connector));
  10617. I915_STATE_WARN(state->best_encoder != encoder,
  10618. "connector's atomic encoder doesn't match legacy encoder\n");
  10619. }
  10620. }
  10621. static void
  10622. check_encoder_state(struct drm_device *dev)
  10623. {
  10624. struct intel_encoder *encoder;
  10625. struct intel_connector *connector;
  10626. for_each_intel_encoder(dev, encoder) {
  10627. bool enabled = false;
  10628. enum pipe pipe;
  10629. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10630. encoder->base.base.id,
  10631. encoder->base.name);
  10632. for_each_intel_connector(dev, connector) {
  10633. if (connector->base.state->best_encoder != &encoder->base)
  10634. continue;
  10635. enabled = true;
  10636. I915_STATE_WARN(connector->base.state->crtc !=
  10637. encoder->base.crtc,
  10638. "connector's crtc doesn't match encoder crtc\n");
  10639. }
  10640. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10641. "encoder's enabled state mismatch "
  10642. "(expected %i, found %i)\n",
  10643. !!encoder->base.crtc, enabled);
  10644. if (!encoder->base.crtc) {
  10645. bool active;
  10646. active = encoder->get_hw_state(encoder, &pipe);
  10647. I915_STATE_WARN(active,
  10648. "encoder detached but still enabled on pipe %c.\n",
  10649. pipe_name(pipe));
  10650. }
  10651. }
  10652. }
  10653. static void
  10654. check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
  10655. {
  10656. struct drm_i915_private *dev_priv = dev->dev_private;
  10657. struct intel_encoder *encoder;
  10658. struct drm_crtc_state *old_crtc_state;
  10659. struct drm_crtc *crtc;
  10660. int i;
  10661. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  10662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10663. struct intel_crtc_state *pipe_config, *sw_config;
  10664. bool active;
  10665. if (!needs_modeset(crtc->state) &&
  10666. !to_intel_crtc_state(crtc->state)->update_pipe)
  10667. continue;
  10668. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10669. pipe_config = to_intel_crtc_state(old_crtc_state);
  10670. memset(pipe_config, 0, sizeof(*pipe_config));
  10671. pipe_config->base.crtc = crtc;
  10672. pipe_config->base.state = old_state;
  10673. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10674. crtc->base.id);
  10675. active = dev_priv->display.get_pipe_config(intel_crtc,
  10676. pipe_config);
  10677. /* hw state is inconsistent with the pipe quirk */
  10678. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10679. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10680. active = crtc->state->active;
  10681. I915_STATE_WARN(crtc->state->active != active,
  10682. "crtc active state doesn't match with hw state "
  10683. "(expected %i, found %i)\n", crtc->state->active, active);
  10684. I915_STATE_WARN(intel_crtc->active != crtc->state->active,
  10685. "transitional active state does not match atomic hw state "
  10686. "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
  10687. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10688. enum pipe pipe;
  10689. active = encoder->get_hw_state(encoder, &pipe);
  10690. I915_STATE_WARN(active != crtc->state->active,
  10691. "[ENCODER:%i] active %i with crtc active %i\n",
  10692. encoder->base.base.id, active, crtc->state->active);
  10693. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10694. "Encoder connected to wrong pipe %c\n",
  10695. pipe_name(pipe));
  10696. if (active)
  10697. encoder->get_config(encoder, pipe_config);
  10698. }
  10699. if (!crtc->state->active)
  10700. continue;
  10701. sw_config = to_intel_crtc_state(crtc->state);
  10702. if (!intel_pipe_config_compare(dev, sw_config,
  10703. pipe_config, false)) {
  10704. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10705. intel_dump_pipe_config(intel_crtc, pipe_config,
  10706. "[hw state]");
  10707. intel_dump_pipe_config(intel_crtc, sw_config,
  10708. "[sw state]");
  10709. }
  10710. }
  10711. }
  10712. static void
  10713. check_shared_dpll_state(struct drm_device *dev)
  10714. {
  10715. struct drm_i915_private *dev_priv = dev->dev_private;
  10716. struct intel_crtc *crtc;
  10717. struct intel_dpll_hw_state dpll_hw_state;
  10718. int i;
  10719. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10720. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10721. int enabled_crtcs = 0, active_crtcs = 0;
  10722. bool active;
  10723. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10724. DRM_DEBUG_KMS("%s\n", pll->name);
  10725. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10726. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10727. "more active pll users than references: %i vs %i\n",
  10728. pll->active, hweight32(pll->config.crtc_mask));
  10729. I915_STATE_WARN(pll->active && !pll->on,
  10730. "pll in active use but not on in sw tracking\n");
  10731. I915_STATE_WARN(pll->on && !pll->active,
  10732. "pll in on but not on in use in sw tracking\n");
  10733. I915_STATE_WARN(pll->on != active,
  10734. "pll on state mismatch (expected %i, found %i)\n",
  10735. pll->on, active);
  10736. for_each_intel_crtc(dev, crtc) {
  10737. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10738. enabled_crtcs++;
  10739. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10740. active_crtcs++;
  10741. }
  10742. I915_STATE_WARN(pll->active != active_crtcs,
  10743. "pll active crtcs mismatch (expected %i, found %i)\n",
  10744. pll->active, active_crtcs);
  10745. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10746. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10747. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10748. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10749. sizeof(dpll_hw_state)),
  10750. "pll hw state mismatch\n");
  10751. }
  10752. }
  10753. static void
  10754. intel_modeset_check_state(struct drm_device *dev,
  10755. struct drm_atomic_state *old_state)
  10756. {
  10757. check_wm_state(dev);
  10758. check_connector_state(dev, old_state);
  10759. check_encoder_state(dev);
  10760. check_crtc_state(dev, old_state);
  10761. check_shared_dpll_state(dev);
  10762. }
  10763. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10764. int dotclock)
  10765. {
  10766. /*
  10767. * FDI already provided one idea for the dotclock.
  10768. * Yell if the encoder disagrees.
  10769. */
  10770. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10771. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10772. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10773. }
  10774. static void update_scanline_offset(struct intel_crtc *crtc)
  10775. {
  10776. struct drm_device *dev = crtc->base.dev;
  10777. /*
  10778. * The scanline counter increments at the leading edge of hsync.
  10779. *
  10780. * On most platforms it starts counting from vtotal-1 on the
  10781. * first active line. That means the scanline counter value is
  10782. * always one less than what we would expect. Ie. just after
  10783. * start of vblank, which also occurs at start of hsync (on the
  10784. * last active line), the scanline counter will read vblank_start-1.
  10785. *
  10786. * On gen2 the scanline counter starts counting from 1 instead
  10787. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10788. * to keep the value positive), instead of adding one.
  10789. *
  10790. * On HSW+ the behaviour of the scanline counter depends on the output
  10791. * type. For DP ports it behaves like most other platforms, but on HDMI
  10792. * there's an extra 1 line difference. So we need to add two instead of
  10793. * one to the value.
  10794. */
  10795. if (IS_GEN2(dev)) {
  10796. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10797. int vtotal;
  10798. vtotal = adjusted_mode->crtc_vtotal;
  10799. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10800. vtotal /= 2;
  10801. crtc->scanline_offset = vtotal - 1;
  10802. } else if (HAS_DDI(dev) &&
  10803. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10804. crtc->scanline_offset = 2;
  10805. } else
  10806. crtc->scanline_offset = 1;
  10807. }
  10808. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10809. {
  10810. struct drm_device *dev = state->dev;
  10811. struct drm_i915_private *dev_priv = to_i915(dev);
  10812. struct intel_shared_dpll_config *shared_dpll = NULL;
  10813. struct intel_crtc *intel_crtc;
  10814. struct intel_crtc_state *intel_crtc_state;
  10815. struct drm_crtc *crtc;
  10816. struct drm_crtc_state *crtc_state;
  10817. int i;
  10818. if (!dev_priv->display.crtc_compute_clock)
  10819. return;
  10820. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10821. int dpll;
  10822. intel_crtc = to_intel_crtc(crtc);
  10823. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10824. dpll = intel_crtc_state->shared_dpll;
  10825. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10826. continue;
  10827. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10828. if (!shared_dpll)
  10829. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10830. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10831. }
  10832. }
  10833. /*
  10834. * This implements the workaround described in the "notes" section of the mode
  10835. * set sequence documentation. When going from no pipes or single pipe to
  10836. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10837. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10838. */
  10839. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10840. {
  10841. struct drm_crtc_state *crtc_state;
  10842. struct intel_crtc *intel_crtc;
  10843. struct drm_crtc *crtc;
  10844. struct intel_crtc_state *first_crtc_state = NULL;
  10845. struct intel_crtc_state *other_crtc_state = NULL;
  10846. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10847. int i;
  10848. /* look at all crtc's that are going to be enabled in during modeset */
  10849. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10850. intel_crtc = to_intel_crtc(crtc);
  10851. if (!crtc_state->active || !needs_modeset(crtc_state))
  10852. continue;
  10853. if (first_crtc_state) {
  10854. other_crtc_state = to_intel_crtc_state(crtc_state);
  10855. break;
  10856. } else {
  10857. first_crtc_state = to_intel_crtc_state(crtc_state);
  10858. first_pipe = intel_crtc->pipe;
  10859. }
  10860. }
  10861. /* No workaround needed? */
  10862. if (!first_crtc_state)
  10863. return 0;
  10864. /* w/a possibly needed, check how many crtc's are already enabled. */
  10865. for_each_intel_crtc(state->dev, intel_crtc) {
  10866. struct intel_crtc_state *pipe_config;
  10867. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10868. if (IS_ERR(pipe_config))
  10869. return PTR_ERR(pipe_config);
  10870. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10871. if (!pipe_config->base.active ||
  10872. needs_modeset(&pipe_config->base))
  10873. continue;
  10874. /* 2 or more enabled crtcs means no need for w/a */
  10875. if (enabled_pipe != INVALID_PIPE)
  10876. return 0;
  10877. enabled_pipe = intel_crtc->pipe;
  10878. }
  10879. if (enabled_pipe != INVALID_PIPE)
  10880. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10881. else if (other_crtc_state)
  10882. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10883. return 0;
  10884. }
  10885. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10886. {
  10887. struct drm_crtc *crtc;
  10888. struct drm_crtc_state *crtc_state;
  10889. int ret = 0;
  10890. /* add all active pipes to the state */
  10891. for_each_crtc(state->dev, crtc) {
  10892. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10893. if (IS_ERR(crtc_state))
  10894. return PTR_ERR(crtc_state);
  10895. if (!crtc_state->active || needs_modeset(crtc_state))
  10896. continue;
  10897. crtc_state->mode_changed = true;
  10898. ret = drm_atomic_add_affected_connectors(state, crtc);
  10899. if (ret)
  10900. break;
  10901. ret = drm_atomic_add_affected_planes(state, crtc);
  10902. if (ret)
  10903. break;
  10904. }
  10905. return ret;
  10906. }
  10907. static int intel_modeset_checks(struct drm_atomic_state *state)
  10908. {
  10909. struct drm_device *dev = state->dev;
  10910. struct drm_i915_private *dev_priv = dev->dev_private;
  10911. int ret;
  10912. if (!check_digital_port_conflicts(state)) {
  10913. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10914. return -EINVAL;
  10915. }
  10916. /*
  10917. * See if the config requires any additional preparation, e.g.
  10918. * to adjust global state with pipes off. We need to do this
  10919. * here so we can get the modeset_pipe updated config for the new
  10920. * mode set on this crtc. For other crtcs we need to use the
  10921. * adjusted_mode bits in the crtc directly.
  10922. */
  10923. if (dev_priv->display.modeset_calc_cdclk) {
  10924. unsigned int cdclk;
  10925. ret = dev_priv->display.modeset_calc_cdclk(state);
  10926. cdclk = to_intel_atomic_state(state)->cdclk;
  10927. if (!ret && cdclk != dev_priv->cdclk_freq)
  10928. ret = intel_modeset_all_pipes(state);
  10929. if (ret < 0)
  10930. return ret;
  10931. } else
  10932. to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
  10933. intel_modeset_clear_plls(state);
  10934. if (IS_HASWELL(dev))
  10935. return haswell_mode_set_planes_workaround(state);
  10936. return 0;
  10937. }
  10938. /*
  10939. * Handle calculation of various watermark data at the end of the atomic check
  10940. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10941. * handlers to ensure that all derived state has been updated.
  10942. */
  10943. static void calc_watermark_data(struct drm_atomic_state *state)
  10944. {
  10945. struct drm_device *dev = state->dev;
  10946. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10947. struct drm_crtc *crtc;
  10948. struct drm_crtc_state *cstate;
  10949. struct drm_plane *plane;
  10950. struct drm_plane_state *pstate;
  10951. /*
  10952. * Calculate watermark configuration details now that derived
  10953. * plane/crtc state is all properly updated.
  10954. */
  10955. drm_for_each_crtc(crtc, dev) {
  10956. cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
  10957. crtc->state;
  10958. if (cstate->active)
  10959. intel_state->wm_config.num_pipes_active++;
  10960. }
  10961. drm_for_each_legacy_plane(plane, dev) {
  10962. pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
  10963. plane->state;
  10964. if (!to_intel_plane_state(pstate)->visible)
  10965. continue;
  10966. intel_state->wm_config.sprites_enabled = true;
  10967. if (pstate->crtc_w != pstate->src_w >> 16 ||
  10968. pstate->crtc_h != pstate->src_h >> 16)
  10969. intel_state->wm_config.sprites_scaled = true;
  10970. }
  10971. }
  10972. /**
  10973. * intel_atomic_check - validate state object
  10974. * @dev: drm device
  10975. * @state: state to validate
  10976. */
  10977. static int intel_atomic_check(struct drm_device *dev,
  10978. struct drm_atomic_state *state)
  10979. {
  10980. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10981. struct drm_crtc *crtc;
  10982. struct drm_crtc_state *crtc_state;
  10983. int ret, i;
  10984. bool any_ms = false;
  10985. ret = drm_atomic_helper_check_modeset(dev, state);
  10986. if (ret)
  10987. return ret;
  10988. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10989. struct intel_crtc_state *pipe_config =
  10990. to_intel_crtc_state(crtc_state);
  10991. /* Catch I915_MODE_FLAG_INHERITED */
  10992. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  10993. crtc_state->mode_changed = true;
  10994. if (!crtc_state->enable) {
  10995. if (needs_modeset(crtc_state))
  10996. any_ms = true;
  10997. continue;
  10998. }
  10999. if (!needs_modeset(crtc_state))
  11000. continue;
  11001. /* FIXME: For only active_changed we shouldn't need to do any
  11002. * state recomputation at all. */
  11003. ret = drm_atomic_add_affected_connectors(state, crtc);
  11004. if (ret)
  11005. return ret;
  11006. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11007. if (ret)
  11008. return ret;
  11009. if (intel_pipe_config_compare(state->dev,
  11010. to_intel_crtc_state(crtc->state),
  11011. pipe_config, true)) {
  11012. crtc_state->mode_changed = false;
  11013. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11014. }
  11015. if (needs_modeset(crtc_state)) {
  11016. any_ms = true;
  11017. ret = drm_atomic_add_affected_planes(state, crtc);
  11018. if (ret)
  11019. return ret;
  11020. }
  11021. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11022. needs_modeset(crtc_state) ?
  11023. "[modeset]" : "[fastset]");
  11024. }
  11025. if (any_ms) {
  11026. ret = intel_modeset_checks(state);
  11027. if (ret)
  11028. return ret;
  11029. } else
  11030. intel_state->cdclk = to_i915(state->dev)->cdclk_freq;
  11031. ret = drm_atomic_helper_check_planes(state->dev, state);
  11032. if (ret)
  11033. return ret;
  11034. calc_watermark_data(state);
  11035. return 0;
  11036. }
  11037. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11038. struct drm_atomic_state *state,
  11039. bool async)
  11040. {
  11041. struct drm_i915_private *dev_priv = dev->dev_private;
  11042. struct drm_plane_state *plane_state;
  11043. struct drm_crtc_state *crtc_state;
  11044. struct drm_plane *plane;
  11045. struct drm_crtc *crtc;
  11046. int i, ret;
  11047. if (async) {
  11048. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  11049. return -EINVAL;
  11050. }
  11051. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11052. ret = intel_crtc_wait_for_pending_flips(crtc);
  11053. if (ret)
  11054. return ret;
  11055. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11056. flush_workqueue(dev_priv->wq);
  11057. }
  11058. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11059. if (ret)
  11060. return ret;
  11061. ret = drm_atomic_helper_prepare_planes(dev, state);
  11062. if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
  11063. u32 reset_counter;
  11064. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  11065. mutex_unlock(&dev->struct_mutex);
  11066. for_each_plane_in_state(state, plane, plane_state, i) {
  11067. struct intel_plane_state *intel_plane_state =
  11068. to_intel_plane_state(plane_state);
  11069. if (!intel_plane_state->wait_req)
  11070. continue;
  11071. ret = __i915_wait_request(intel_plane_state->wait_req,
  11072. reset_counter, true,
  11073. NULL, NULL);
  11074. /* Swallow -EIO errors to allow updates during hw lockup. */
  11075. if (ret == -EIO)
  11076. ret = 0;
  11077. if (ret)
  11078. break;
  11079. }
  11080. if (!ret)
  11081. return 0;
  11082. mutex_lock(&dev->struct_mutex);
  11083. drm_atomic_helper_cleanup_planes(dev, state);
  11084. }
  11085. mutex_unlock(&dev->struct_mutex);
  11086. return ret;
  11087. }
  11088. /**
  11089. * intel_atomic_commit - commit validated state object
  11090. * @dev: DRM device
  11091. * @state: the top-level driver state object
  11092. * @async: asynchronous commit
  11093. *
  11094. * This function commits a top-level state object that has been validated
  11095. * with drm_atomic_helper_check().
  11096. *
  11097. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11098. * we can only handle plane-related operations and do not yet support
  11099. * asynchronous commit.
  11100. *
  11101. * RETURNS
  11102. * Zero for success or -errno.
  11103. */
  11104. static int intel_atomic_commit(struct drm_device *dev,
  11105. struct drm_atomic_state *state,
  11106. bool async)
  11107. {
  11108. struct drm_i915_private *dev_priv = dev->dev_private;
  11109. struct drm_crtc_state *crtc_state;
  11110. struct drm_crtc *crtc;
  11111. int ret = 0;
  11112. int i;
  11113. bool any_ms = false;
  11114. ret = intel_atomic_prepare_commit(dev, state, async);
  11115. if (ret) {
  11116. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11117. return ret;
  11118. }
  11119. drm_atomic_helper_swap_state(dev, state);
  11120. dev_priv->wm.config = to_intel_atomic_state(state)->wm_config;
  11121. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11122. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11123. if (!needs_modeset(crtc->state))
  11124. continue;
  11125. any_ms = true;
  11126. intel_pre_plane_update(intel_crtc);
  11127. if (crtc_state->active) {
  11128. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  11129. dev_priv->display.crtc_disable(crtc);
  11130. intel_crtc->active = false;
  11131. intel_disable_shared_dpll(intel_crtc);
  11132. }
  11133. }
  11134. /* Only after disabling all output pipelines that will be changed can we
  11135. * update the the output configuration. */
  11136. intel_modeset_update_crtc_state(state);
  11137. if (any_ms) {
  11138. intel_shared_dpll_commit(state);
  11139. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11140. modeset_update_crtc_power_domains(state);
  11141. }
  11142. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11143. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11144. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11145. bool modeset = needs_modeset(crtc->state);
  11146. bool update_pipe = !modeset &&
  11147. to_intel_crtc_state(crtc->state)->update_pipe;
  11148. unsigned long put_domains = 0;
  11149. if (modeset && crtc->state->active) {
  11150. update_scanline_offset(to_intel_crtc(crtc));
  11151. dev_priv->display.crtc_enable(crtc);
  11152. }
  11153. if (update_pipe) {
  11154. put_domains = modeset_get_crtc_power_domains(crtc);
  11155. /* make sure intel_modeset_check_state runs */
  11156. any_ms = true;
  11157. }
  11158. if (!modeset)
  11159. intel_pre_plane_update(intel_crtc);
  11160. if (crtc->state->active &&
  11161. (crtc->state->planes_changed || update_pipe))
  11162. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11163. if (put_domains)
  11164. modeset_put_power_domains(dev_priv, put_domains);
  11165. intel_post_plane_update(intel_crtc);
  11166. }
  11167. /* FIXME: add subpixel order */
  11168. drm_atomic_helper_wait_for_vblanks(dev, state);
  11169. mutex_lock(&dev->struct_mutex);
  11170. drm_atomic_helper_cleanup_planes(dev, state);
  11171. mutex_unlock(&dev->struct_mutex);
  11172. if (any_ms)
  11173. intel_modeset_check_state(dev, state);
  11174. drm_atomic_state_free(state);
  11175. return 0;
  11176. }
  11177. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11178. {
  11179. struct drm_device *dev = crtc->dev;
  11180. struct drm_atomic_state *state;
  11181. struct drm_crtc_state *crtc_state;
  11182. int ret;
  11183. state = drm_atomic_state_alloc(dev);
  11184. if (!state) {
  11185. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11186. crtc->base.id);
  11187. return;
  11188. }
  11189. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11190. retry:
  11191. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11192. ret = PTR_ERR_OR_ZERO(crtc_state);
  11193. if (!ret) {
  11194. if (!crtc_state->active)
  11195. goto out;
  11196. crtc_state->mode_changed = true;
  11197. ret = drm_atomic_commit(state);
  11198. }
  11199. if (ret == -EDEADLK) {
  11200. drm_atomic_state_clear(state);
  11201. drm_modeset_backoff(state->acquire_ctx);
  11202. goto retry;
  11203. }
  11204. if (ret)
  11205. out:
  11206. drm_atomic_state_free(state);
  11207. }
  11208. #undef for_each_intel_crtc_masked
  11209. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11210. .gamma_set = intel_crtc_gamma_set,
  11211. .set_config = drm_atomic_helper_set_config,
  11212. .destroy = intel_crtc_destroy,
  11213. .page_flip = intel_crtc_page_flip,
  11214. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11215. .atomic_destroy_state = intel_crtc_destroy_state,
  11216. };
  11217. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11218. struct intel_shared_dpll *pll,
  11219. struct intel_dpll_hw_state *hw_state)
  11220. {
  11221. uint32_t val;
  11222. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11223. return false;
  11224. val = I915_READ(PCH_DPLL(pll->id));
  11225. hw_state->dpll = val;
  11226. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11227. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11228. return val & DPLL_VCO_ENABLE;
  11229. }
  11230. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11231. struct intel_shared_dpll *pll)
  11232. {
  11233. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11234. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11235. }
  11236. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11237. struct intel_shared_dpll *pll)
  11238. {
  11239. /* PCH refclock must be enabled first */
  11240. ibx_assert_pch_refclk_enabled(dev_priv);
  11241. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11242. /* Wait for the clocks to stabilize. */
  11243. POSTING_READ(PCH_DPLL(pll->id));
  11244. udelay(150);
  11245. /* The pixel multiplier can only be updated once the
  11246. * DPLL is enabled and the clocks are stable.
  11247. *
  11248. * So write it again.
  11249. */
  11250. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11251. POSTING_READ(PCH_DPLL(pll->id));
  11252. udelay(200);
  11253. }
  11254. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11255. struct intel_shared_dpll *pll)
  11256. {
  11257. struct drm_device *dev = dev_priv->dev;
  11258. struct intel_crtc *crtc;
  11259. /* Make sure no transcoder isn't still depending on us. */
  11260. for_each_intel_crtc(dev, crtc) {
  11261. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11262. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11263. }
  11264. I915_WRITE(PCH_DPLL(pll->id), 0);
  11265. POSTING_READ(PCH_DPLL(pll->id));
  11266. udelay(200);
  11267. }
  11268. static char *ibx_pch_dpll_names[] = {
  11269. "PCH DPLL A",
  11270. "PCH DPLL B",
  11271. };
  11272. static void ibx_pch_dpll_init(struct drm_device *dev)
  11273. {
  11274. struct drm_i915_private *dev_priv = dev->dev_private;
  11275. int i;
  11276. dev_priv->num_shared_dpll = 2;
  11277. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11278. dev_priv->shared_dplls[i].id = i;
  11279. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11280. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11281. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11282. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11283. dev_priv->shared_dplls[i].get_hw_state =
  11284. ibx_pch_dpll_get_hw_state;
  11285. }
  11286. }
  11287. static void intel_shared_dpll_init(struct drm_device *dev)
  11288. {
  11289. struct drm_i915_private *dev_priv = dev->dev_private;
  11290. if (HAS_DDI(dev))
  11291. intel_ddi_pll_init(dev);
  11292. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11293. ibx_pch_dpll_init(dev);
  11294. else
  11295. dev_priv->num_shared_dpll = 0;
  11296. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11297. }
  11298. /**
  11299. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11300. * @plane: drm plane to prepare for
  11301. * @fb: framebuffer to prepare for presentation
  11302. *
  11303. * Prepares a framebuffer for usage on a display plane. Generally this
  11304. * involves pinning the underlying object and updating the frontbuffer tracking
  11305. * bits. Some older platforms need special physical address handling for
  11306. * cursor planes.
  11307. *
  11308. * Must be called with struct_mutex held.
  11309. *
  11310. * Returns 0 on success, negative error code on failure.
  11311. */
  11312. int
  11313. intel_prepare_plane_fb(struct drm_plane *plane,
  11314. const struct drm_plane_state *new_state)
  11315. {
  11316. struct drm_device *dev = plane->dev;
  11317. struct drm_framebuffer *fb = new_state->fb;
  11318. struct intel_plane *intel_plane = to_intel_plane(plane);
  11319. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11320. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11321. int ret = 0;
  11322. if (!obj && !old_obj)
  11323. return 0;
  11324. if (old_obj) {
  11325. struct drm_crtc_state *crtc_state =
  11326. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11327. /* Big Hammer, we also need to ensure that any pending
  11328. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11329. * current scanout is retired before unpinning the old
  11330. * framebuffer. Note that we rely on userspace rendering
  11331. * into the buffer attached to the pipe they are waiting
  11332. * on. If not, userspace generates a GPU hang with IPEHR
  11333. * point to the MI_WAIT_FOR_EVENT.
  11334. *
  11335. * This should only fail upon a hung GPU, in which case we
  11336. * can safely continue.
  11337. */
  11338. if (needs_modeset(crtc_state))
  11339. ret = i915_gem_object_wait_rendering(old_obj, true);
  11340. /* Swallow -EIO errors to allow updates during hw lockup. */
  11341. if (ret && ret != -EIO)
  11342. return ret;
  11343. }
  11344. if (!obj) {
  11345. ret = 0;
  11346. } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11347. INTEL_INFO(dev)->cursor_needs_physical) {
  11348. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11349. ret = i915_gem_object_attach_phys(obj, align);
  11350. if (ret)
  11351. DRM_DEBUG_KMS("failed to attach phys object\n");
  11352. } else {
  11353. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state);
  11354. }
  11355. if (ret == 0) {
  11356. if (obj) {
  11357. struct intel_plane_state *plane_state =
  11358. to_intel_plane_state(new_state);
  11359. i915_gem_request_assign(&plane_state->wait_req,
  11360. obj->last_write_req);
  11361. }
  11362. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11363. }
  11364. return ret;
  11365. }
  11366. /**
  11367. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11368. * @plane: drm plane to clean up for
  11369. * @fb: old framebuffer that was on plane
  11370. *
  11371. * Cleans up a framebuffer that has just been removed from a plane.
  11372. *
  11373. * Must be called with struct_mutex held.
  11374. */
  11375. void
  11376. intel_cleanup_plane_fb(struct drm_plane *plane,
  11377. const struct drm_plane_state *old_state)
  11378. {
  11379. struct drm_device *dev = plane->dev;
  11380. struct intel_plane *intel_plane = to_intel_plane(plane);
  11381. struct intel_plane_state *old_intel_state;
  11382. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11383. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11384. old_intel_state = to_intel_plane_state(old_state);
  11385. if (!obj && !old_obj)
  11386. return;
  11387. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11388. !INTEL_INFO(dev)->cursor_needs_physical))
  11389. intel_unpin_fb_obj(old_state->fb, old_state);
  11390. /* prepare_fb aborted? */
  11391. if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
  11392. (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
  11393. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11394. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11395. }
  11396. int
  11397. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11398. {
  11399. int max_scale;
  11400. struct drm_device *dev;
  11401. struct drm_i915_private *dev_priv;
  11402. int crtc_clock, cdclk;
  11403. if (!intel_crtc || !crtc_state)
  11404. return DRM_PLANE_HELPER_NO_SCALING;
  11405. dev = intel_crtc->base.dev;
  11406. dev_priv = dev->dev_private;
  11407. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11408. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11409. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11410. return DRM_PLANE_HELPER_NO_SCALING;
  11411. /*
  11412. * skl max scale is lower of:
  11413. * close to 3 but not 3, -1 is for that purpose
  11414. * or
  11415. * cdclk/crtc_clock
  11416. */
  11417. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11418. return max_scale;
  11419. }
  11420. static int
  11421. intel_check_primary_plane(struct drm_plane *plane,
  11422. struct intel_crtc_state *crtc_state,
  11423. struct intel_plane_state *state)
  11424. {
  11425. struct drm_crtc *crtc = state->base.crtc;
  11426. struct drm_framebuffer *fb = state->base.fb;
  11427. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11428. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11429. bool can_position = false;
  11430. /* use scaler when colorkey is not required */
  11431. if (INTEL_INFO(plane->dev)->gen >= 9 &&
  11432. state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11433. min_scale = 1;
  11434. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11435. can_position = true;
  11436. }
  11437. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11438. &state->dst, &state->clip,
  11439. min_scale, max_scale,
  11440. can_position, true,
  11441. &state->visible);
  11442. }
  11443. static void
  11444. intel_commit_primary_plane(struct drm_plane *plane,
  11445. struct intel_plane_state *state)
  11446. {
  11447. struct drm_crtc *crtc = state->base.crtc;
  11448. struct drm_framebuffer *fb = state->base.fb;
  11449. struct drm_device *dev = plane->dev;
  11450. struct drm_i915_private *dev_priv = dev->dev_private;
  11451. crtc = crtc ? crtc : plane->crtc;
  11452. dev_priv->display.update_primary_plane(crtc, fb,
  11453. state->src.x1 >> 16,
  11454. state->src.y1 >> 16);
  11455. }
  11456. static void
  11457. intel_disable_primary_plane(struct drm_plane *plane,
  11458. struct drm_crtc *crtc)
  11459. {
  11460. struct drm_device *dev = plane->dev;
  11461. struct drm_i915_private *dev_priv = dev->dev_private;
  11462. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11463. }
  11464. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11465. struct drm_crtc_state *old_crtc_state)
  11466. {
  11467. struct drm_device *dev = crtc->dev;
  11468. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11469. struct intel_crtc_state *old_intel_state =
  11470. to_intel_crtc_state(old_crtc_state);
  11471. bool modeset = needs_modeset(crtc->state);
  11472. if (intel_crtc->atomic.update_wm_pre)
  11473. intel_update_watermarks(crtc);
  11474. /* Perform vblank evasion around commit operation */
  11475. intel_pipe_update_start(intel_crtc);
  11476. if (modeset)
  11477. return;
  11478. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11479. intel_update_pipe_config(intel_crtc, old_intel_state);
  11480. else if (INTEL_INFO(dev)->gen >= 9)
  11481. skl_detach_scalers(intel_crtc);
  11482. }
  11483. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11484. struct drm_crtc_state *old_crtc_state)
  11485. {
  11486. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11487. intel_pipe_update_end(intel_crtc);
  11488. }
  11489. /**
  11490. * intel_plane_destroy - destroy a plane
  11491. * @plane: plane to destroy
  11492. *
  11493. * Common destruction function for all types of planes (primary, cursor,
  11494. * sprite).
  11495. */
  11496. void intel_plane_destroy(struct drm_plane *plane)
  11497. {
  11498. struct intel_plane *intel_plane = to_intel_plane(plane);
  11499. drm_plane_cleanup(plane);
  11500. kfree(intel_plane);
  11501. }
  11502. const struct drm_plane_funcs intel_plane_funcs = {
  11503. .update_plane = drm_atomic_helper_update_plane,
  11504. .disable_plane = drm_atomic_helper_disable_plane,
  11505. .destroy = intel_plane_destroy,
  11506. .set_property = drm_atomic_helper_plane_set_property,
  11507. .atomic_get_property = intel_plane_atomic_get_property,
  11508. .atomic_set_property = intel_plane_atomic_set_property,
  11509. .atomic_duplicate_state = intel_plane_duplicate_state,
  11510. .atomic_destroy_state = intel_plane_destroy_state,
  11511. };
  11512. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11513. int pipe)
  11514. {
  11515. struct intel_plane *primary;
  11516. struct intel_plane_state *state;
  11517. const uint32_t *intel_primary_formats;
  11518. unsigned int num_formats;
  11519. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11520. if (primary == NULL)
  11521. return NULL;
  11522. state = intel_create_plane_state(&primary->base);
  11523. if (!state) {
  11524. kfree(primary);
  11525. return NULL;
  11526. }
  11527. primary->base.state = &state->base;
  11528. primary->can_scale = false;
  11529. primary->max_downscale = 1;
  11530. if (INTEL_INFO(dev)->gen >= 9) {
  11531. primary->can_scale = true;
  11532. state->scaler_id = -1;
  11533. }
  11534. primary->pipe = pipe;
  11535. primary->plane = pipe;
  11536. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11537. primary->check_plane = intel_check_primary_plane;
  11538. primary->commit_plane = intel_commit_primary_plane;
  11539. primary->disable_plane = intel_disable_primary_plane;
  11540. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11541. primary->plane = !pipe;
  11542. if (INTEL_INFO(dev)->gen >= 9) {
  11543. intel_primary_formats = skl_primary_formats;
  11544. num_formats = ARRAY_SIZE(skl_primary_formats);
  11545. } else if (INTEL_INFO(dev)->gen >= 4) {
  11546. intel_primary_formats = i965_primary_formats;
  11547. num_formats = ARRAY_SIZE(i965_primary_formats);
  11548. } else {
  11549. intel_primary_formats = i8xx_primary_formats;
  11550. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11551. }
  11552. drm_universal_plane_init(dev, &primary->base, 0,
  11553. &intel_plane_funcs,
  11554. intel_primary_formats, num_formats,
  11555. DRM_PLANE_TYPE_PRIMARY);
  11556. if (INTEL_INFO(dev)->gen >= 4)
  11557. intel_create_rotation_property(dev, primary);
  11558. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11559. return &primary->base;
  11560. }
  11561. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11562. {
  11563. if (!dev->mode_config.rotation_property) {
  11564. unsigned long flags = BIT(DRM_ROTATE_0) |
  11565. BIT(DRM_ROTATE_180);
  11566. if (INTEL_INFO(dev)->gen >= 9)
  11567. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11568. dev->mode_config.rotation_property =
  11569. drm_mode_create_rotation_property(dev, flags);
  11570. }
  11571. if (dev->mode_config.rotation_property)
  11572. drm_object_attach_property(&plane->base.base,
  11573. dev->mode_config.rotation_property,
  11574. plane->base.state->rotation);
  11575. }
  11576. static int
  11577. intel_check_cursor_plane(struct drm_plane *plane,
  11578. struct intel_crtc_state *crtc_state,
  11579. struct intel_plane_state *state)
  11580. {
  11581. struct drm_crtc *crtc = crtc_state->base.crtc;
  11582. struct drm_framebuffer *fb = state->base.fb;
  11583. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11584. unsigned stride;
  11585. int ret;
  11586. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11587. &state->dst, &state->clip,
  11588. DRM_PLANE_HELPER_NO_SCALING,
  11589. DRM_PLANE_HELPER_NO_SCALING,
  11590. true, true, &state->visible);
  11591. if (ret)
  11592. return ret;
  11593. /* if we want to turn off the cursor ignore width and height */
  11594. if (!obj)
  11595. return 0;
  11596. /* Check for which cursor types we support */
  11597. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11598. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11599. state->base.crtc_w, state->base.crtc_h);
  11600. return -EINVAL;
  11601. }
  11602. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11603. if (obj->base.size < stride * state->base.crtc_h) {
  11604. DRM_DEBUG_KMS("buffer is too small\n");
  11605. return -ENOMEM;
  11606. }
  11607. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11608. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11609. return -EINVAL;
  11610. }
  11611. return 0;
  11612. }
  11613. static void
  11614. intel_disable_cursor_plane(struct drm_plane *plane,
  11615. struct drm_crtc *crtc)
  11616. {
  11617. intel_crtc_update_cursor(crtc, false);
  11618. }
  11619. static void
  11620. intel_commit_cursor_plane(struct drm_plane *plane,
  11621. struct intel_plane_state *state)
  11622. {
  11623. struct drm_crtc *crtc = state->base.crtc;
  11624. struct drm_device *dev = plane->dev;
  11625. struct intel_crtc *intel_crtc;
  11626. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11627. uint32_t addr;
  11628. crtc = crtc ? crtc : plane->crtc;
  11629. intel_crtc = to_intel_crtc(crtc);
  11630. if (intel_crtc->cursor_bo == obj)
  11631. goto update;
  11632. if (!obj)
  11633. addr = 0;
  11634. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11635. addr = i915_gem_obj_ggtt_offset(obj);
  11636. else
  11637. addr = obj->phys_handle->busaddr;
  11638. intel_crtc->cursor_addr = addr;
  11639. intel_crtc->cursor_bo = obj;
  11640. update:
  11641. intel_crtc_update_cursor(crtc, state->visible);
  11642. }
  11643. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11644. int pipe)
  11645. {
  11646. struct intel_plane *cursor;
  11647. struct intel_plane_state *state;
  11648. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11649. if (cursor == NULL)
  11650. return NULL;
  11651. state = intel_create_plane_state(&cursor->base);
  11652. if (!state) {
  11653. kfree(cursor);
  11654. return NULL;
  11655. }
  11656. cursor->base.state = &state->base;
  11657. cursor->can_scale = false;
  11658. cursor->max_downscale = 1;
  11659. cursor->pipe = pipe;
  11660. cursor->plane = pipe;
  11661. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11662. cursor->check_plane = intel_check_cursor_plane;
  11663. cursor->commit_plane = intel_commit_cursor_plane;
  11664. cursor->disable_plane = intel_disable_cursor_plane;
  11665. drm_universal_plane_init(dev, &cursor->base, 0,
  11666. &intel_plane_funcs,
  11667. intel_cursor_formats,
  11668. ARRAY_SIZE(intel_cursor_formats),
  11669. DRM_PLANE_TYPE_CURSOR);
  11670. if (INTEL_INFO(dev)->gen >= 4) {
  11671. if (!dev->mode_config.rotation_property)
  11672. dev->mode_config.rotation_property =
  11673. drm_mode_create_rotation_property(dev,
  11674. BIT(DRM_ROTATE_0) |
  11675. BIT(DRM_ROTATE_180));
  11676. if (dev->mode_config.rotation_property)
  11677. drm_object_attach_property(&cursor->base.base,
  11678. dev->mode_config.rotation_property,
  11679. state->base.rotation);
  11680. }
  11681. if (INTEL_INFO(dev)->gen >=9)
  11682. state->scaler_id = -1;
  11683. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11684. return &cursor->base;
  11685. }
  11686. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11687. struct intel_crtc_state *crtc_state)
  11688. {
  11689. int i;
  11690. struct intel_scaler *intel_scaler;
  11691. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11692. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11693. intel_scaler = &scaler_state->scalers[i];
  11694. intel_scaler->in_use = 0;
  11695. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11696. }
  11697. scaler_state->scaler_id = -1;
  11698. }
  11699. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11700. {
  11701. struct drm_i915_private *dev_priv = dev->dev_private;
  11702. struct intel_crtc *intel_crtc;
  11703. struct intel_crtc_state *crtc_state = NULL;
  11704. struct drm_plane *primary = NULL;
  11705. struct drm_plane *cursor = NULL;
  11706. int i, ret;
  11707. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11708. if (intel_crtc == NULL)
  11709. return;
  11710. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11711. if (!crtc_state)
  11712. goto fail;
  11713. intel_crtc->config = crtc_state;
  11714. intel_crtc->base.state = &crtc_state->base;
  11715. crtc_state->base.crtc = &intel_crtc->base;
  11716. /* initialize shared scalers */
  11717. if (INTEL_INFO(dev)->gen >= 9) {
  11718. if (pipe == PIPE_C)
  11719. intel_crtc->num_scalers = 1;
  11720. else
  11721. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11722. skl_init_scalers(dev, intel_crtc, crtc_state);
  11723. }
  11724. primary = intel_primary_plane_create(dev, pipe);
  11725. if (!primary)
  11726. goto fail;
  11727. cursor = intel_cursor_plane_create(dev, pipe);
  11728. if (!cursor)
  11729. goto fail;
  11730. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11731. cursor, &intel_crtc_funcs);
  11732. if (ret)
  11733. goto fail;
  11734. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11735. for (i = 0; i < 256; i++) {
  11736. intel_crtc->lut_r[i] = i;
  11737. intel_crtc->lut_g[i] = i;
  11738. intel_crtc->lut_b[i] = i;
  11739. }
  11740. /*
  11741. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11742. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11743. */
  11744. intel_crtc->pipe = pipe;
  11745. intel_crtc->plane = pipe;
  11746. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11747. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11748. intel_crtc->plane = !pipe;
  11749. }
  11750. intel_crtc->cursor_base = ~0;
  11751. intel_crtc->cursor_cntl = ~0;
  11752. intel_crtc->cursor_size = ~0;
  11753. intel_crtc->wm.cxsr_allowed = true;
  11754. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11755. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11756. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11757. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11758. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11759. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11760. return;
  11761. fail:
  11762. if (primary)
  11763. drm_plane_cleanup(primary);
  11764. if (cursor)
  11765. drm_plane_cleanup(cursor);
  11766. kfree(crtc_state);
  11767. kfree(intel_crtc);
  11768. }
  11769. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11770. {
  11771. struct drm_encoder *encoder = connector->base.encoder;
  11772. struct drm_device *dev = connector->base.dev;
  11773. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11774. if (!encoder || WARN_ON(!encoder->crtc))
  11775. return INVALID_PIPE;
  11776. return to_intel_crtc(encoder->crtc)->pipe;
  11777. }
  11778. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11779. struct drm_file *file)
  11780. {
  11781. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11782. struct drm_crtc *drmmode_crtc;
  11783. struct intel_crtc *crtc;
  11784. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11785. if (!drmmode_crtc) {
  11786. DRM_ERROR("no such CRTC id\n");
  11787. return -ENOENT;
  11788. }
  11789. crtc = to_intel_crtc(drmmode_crtc);
  11790. pipe_from_crtc_id->pipe = crtc->pipe;
  11791. return 0;
  11792. }
  11793. static int intel_encoder_clones(struct intel_encoder *encoder)
  11794. {
  11795. struct drm_device *dev = encoder->base.dev;
  11796. struct intel_encoder *source_encoder;
  11797. int index_mask = 0;
  11798. int entry = 0;
  11799. for_each_intel_encoder(dev, source_encoder) {
  11800. if (encoders_cloneable(encoder, source_encoder))
  11801. index_mask |= (1 << entry);
  11802. entry++;
  11803. }
  11804. return index_mask;
  11805. }
  11806. static bool has_edp_a(struct drm_device *dev)
  11807. {
  11808. struct drm_i915_private *dev_priv = dev->dev_private;
  11809. if (!IS_MOBILE(dev))
  11810. return false;
  11811. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11812. return false;
  11813. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11814. return false;
  11815. return true;
  11816. }
  11817. static bool intel_crt_present(struct drm_device *dev)
  11818. {
  11819. struct drm_i915_private *dev_priv = dev->dev_private;
  11820. if (INTEL_INFO(dev)->gen >= 9)
  11821. return false;
  11822. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11823. return false;
  11824. if (IS_CHERRYVIEW(dev))
  11825. return false;
  11826. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11827. return false;
  11828. return true;
  11829. }
  11830. static void intel_setup_outputs(struct drm_device *dev)
  11831. {
  11832. struct drm_i915_private *dev_priv = dev->dev_private;
  11833. struct intel_encoder *encoder;
  11834. bool dpd_is_edp = false;
  11835. intel_lvds_init(dev);
  11836. if (intel_crt_present(dev))
  11837. intel_crt_init(dev);
  11838. if (IS_BROXTON(dev)) {
  11839. /*
  11840. * FIXME: Broxton doesn't support port detection via the
  11841. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11842. * detect the ports.
  11843. */
  11844. intel_ddi_init(dev, PORT_A);
  11845. intel_ddi_init(dev, PORT_B);
  11846. intel_ddi_init(dev, PORT_C);
  11847. } else if (HAS_DDI(dev)) {
  11848. int found;
  11849. /*
  11850. * Haswell uses DDI functions to detect digital outputs.
  11851. * On SKL pre-D0 the strap isn't connected, so we assume
  11852. * it's there.
  11853. */
  11854. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11855. /* WaIgnoreDDIAStrap: skl */
  11856. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  11857. intel_ddi_init(dev, PORT_A);
  11858. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11859. * register */
  11860. found = I915_READ(SFUSE_STRAP);
  11861. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11862. intel_ddi_init(dev, PORT_B);
  11863. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11864. intel_ddi_init(dev, PORT_C);
  11865. if (found & SFUSE_STRAP_DDID_DETECTED)
  11866. intel_ddi_init(dev, PORT_D);
  11867. /*
  11868. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11869. */
  11870. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  11871. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11872. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11873. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11874. intel_ddi_init(dev, PORT_E);
  11875. } else if (HAS_PCH_SPLIT(dev)) {
  11876. int found;
  11877. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11878. if (has_edp_a(dev))
  11879. intel_dp_init(dev, DP_A, PORT_A);
  11880. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11881. /* PCH SDVOB multiplex with HDMIB */
  11882. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11883. if (!found)
  11884. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11885. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11886. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11887. }
  11888. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11889. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11890. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11891. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11892. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11893. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11894. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11895. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11896. } else if (IS_VALLEYVIEW(dev)) {
  11897. /*
  11898. * The DP_DETECTED bit is the latched state of the DDC
  11899. * SDA pin at boot. However since eDP doesn't require DDC
  11900. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11901. * eDP ports may have been muxed to an alternate function.
  11902. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11903. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11904. * detect eDP ports.
  11905. */
  11906. if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
  11907. !intel_dp_is_edp(dev, PORT_B))
  11908. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  11909. if (I915_READ(VLV_DP_B) & DP_DETECTED ||
  11910. intel_dp_is_edp(dev, PORT_B))
  11911. intel_dp_init(dev, VLV_DP_B, PORT_B);
  11912. if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
  11913. !intel_dp_is_edp(dev, PORT_C))
  11914. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  11915. if (I915_READ(VLV_DP_C) & DP_DETECTED ||
  11916. intel_dp_is_edp(dev, PORT_C))
  11917. intel_dp_init(dev, VLV_DP_C, PORT_C);
  11918. if (IS_CHERRYVIEW(dev)) {
  11919. /* eDP not supported on port D, so don't check VBT */
  11920. if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
  11921. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  11922. if (I915_READ(CHV_DP_D) & DP_DETECTED)
  11923. intel_dp_init(dev, CHV_DP_D, PORT_D);
  11924. }
  11925. intel_dsi_init(dev);
  11926. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  11927. bool found = false;
  11928. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11929. DRM_DEBUG_KMS("probing SDVOB\n");
  11930. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11931. if (!found && IS_G4X(dev)) {
  11932. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11933. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11934. }
  11935. if (!found && IS_G4X(dev))
  11936. intel_dp_init(dev, DP_B, PORT_B);
  11937. }
  11938. /* Before G4X SDVOC doesn't have its own detect register */
  11939. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11940. DRM_DEBUG_KMS("probing SDVOC\n");
  11941. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11942. }
  11943. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11944. if (IS_G4X(dev)) {
  11945. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11946. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11947. }
  11948. if (IS_G4X(dev))
  11949. intel_dp_init(dev, DP_C, PORT_C);
  11950. }
  11951. if (IS_G4X(dev) &&
  11952. (I915_READ(DP_D) & DP_DETECTED))
  11953. intel_dp_init(dev, DP_D, PORT_D);
  11954. } else if (IS_GEN2(dev))
  11955. intel_dvo_init(dev);
  11956. if (SUPPORTS_TV(dev))
  11957. intel_tv_init(dev);
  11958. intel_psr_init(dev);
  11959. for_each_intel_encoder(dev, encoder) {
  11960. encoder->base.possible_crtcs = encoder->crtc_mask;
  11961. encoder->base.possible_clones =
  11962. intel_encoder_clones(encoder);
  11963. }
  11964. intel_init_pch_refclk(dev);
  11965. drm_helper_move_panel_connectors_to_head(dev);
  11966. }
  11967. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11968. {
  11969. struct drm_device *dev = fb->dev;
  11970. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11971. drm_framebuffer_cleanup(fb);
  11972. mutex_lock(&dev->struct_mutex);
  11973. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11974. drm_gem_object_unreference(&intel_fb->obj->base);
  11975. mutex_unlock(&dev->struct_mutex);
  11976. kfree(intel_fb);
  11977. }
  11978. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11979. struct drm_file *file,
  11980. unsigned int *handle)
  11981. {
  11982. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11983. struct drm_i915_gem_object *obj = intel_fb->obj;
  11984. return drm_gem_handle_create(file, &obj->base, handle);
  11985. }
  11986. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11987. struct drm_file *file,
  11988. unsigned flags, unsigned color,
  11989. struct drm_clip_rect *clips,
  11990. unsigned num_clips)
  11991. {
  11992. struct drm_device *dev = fb->dev;
  11993. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11994. struct drm_i915_gem_object *obj = intel_fb->obj;
  11995. mutex_lock(&dev->struct_mutex);
  11996. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  11997. mutex_unlock(&dev->struct_mutex);
  11998. return 0;
  11999. }
  12000. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12001. .destroy = intel_user_framebuffer_destroy,
  12002. .create_handle = intel_user_framebuffer_create_handle,
  12003. .dirty = intel_user_framebuffer_dirty,
  12004. };
  12005. static
  12006. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12007. uint32_t pixel_format)
  12008. {
  12009. u32 gen = INTEL_INFO(dev)->gen;
  12010. if (gen >= 9) {
  12011. /* "The stride in bytes must not exceed the of the size of 8K
  12012. * pixels and 32K bytes."
  12013. */
  12014. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  12015. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  12016. return 32*1024;
  12017. } else if (gen >= 4) {
  12018. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12019. return 16*1024;
  12020. else
  12021. return 32*1024;
  12022. } else if (gen >= 3) {
  12023. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12024. return 8*1024;
  12025. else
  12026. return 16*1024;
  12027. } else {
  12028. /* XXX DSPC is limited to 4k tiled */
  12029. return 8*1024;
  12030. }
  12031. }
  12032. static int intel_framebuffer_init(struct drm_device *dev,
  12033. struct intel_framebuffer *intel_fb,
  12034. struct drm_mode_fb_cmd2 *mode_cmd,
  12035. struct drm_i915_gem_object *obj)
  12036. {
  12037. unsigned int aligned_height;
  12038. int ret;
  12039. u32 pitch_limit, stride_alignment;
  12040. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12041. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12042. /* Enforce that fb modifier and tiling mode match, but only for
  12043. * X-tiled. This is needed for FBC. */
  12044. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12045. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12046. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12047. return -EINVAL;
  12048. }
  12049. } else {
  12050. if (obj->tiling_mode == I915_TILING_X)
  12051. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12052. else if (obj->tiling_mode == I915_TILING_Y) {
  12053. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12054. return -EINVAL;
  12055. }
  12056. }
  12057. /* Passed in modifier sanity checking. */
  12058. switch (mode_cmd->modifier[0]) {
  12059. case I915_FORMAT_MOD_Y_TILED:
  12060. case I915_FORMAT_MOD_Yf_TILED:
  12061. if (INTEL_INFO(dev)->gen < 9) {
  12062. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12063. mode_cmd->modifier[0]);
  12064. return -EINVAL;
  12065. }
  12066. case DRM_FORMAT_MOD_NONE:
  12067. case I915_FORMAT_MOD_X_TILED:
  12068. break;
  12069. default:
  12070. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12071. mode_cmd->modifier[0]);
  12072. return -EINVAL;
  12073. }
  12074. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  12075. mode_cmd->pixel_format);
  12076. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12077. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12078. mode_cmd->pitches[0], stride_alignment);
  12079. return -EINVAL;
  12080. }
  12081. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12082. mode_cmd->pixel_format);
  12083. if (mode_cmd->pitches[0] > pitch_limit) {
  12084. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12085. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12086. "tiled" : "linear",
  12087. mode_cmd->pitches[0], pitch_limit);
  12088. return -EINVAL;
  12089. }
  12090. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12091. mode_cmd->pitches[0] != obj->stride) {
  12092. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12093. mode_cmd->pitches[0], obj->stride);
  12094. return -EINVAL;
  12095. }
  12096. /* Reject formats not supported by any plane early. */
  12097. switch (mode_cmd->pixel_format) {
  12098. case DRM_FORMAT_C8:
  12099. case DRM_FORMAT_RGB565:
  12100. case DRM_FORMAT_XRGB8888:
  12101. case DRM_FORMAT_ARGB8888:
  12102. break;
  12103. case DRM_FORMAT_XRGB1555:
  12104. if (INTEL_INFO(dev)->gen > 3) {
  12105. DRM_DEBUG("unsupported pixel format: %s\n",
  12106. drm_get_format_name(mode_cmd->pixel_format));
  12107. return -EINVAL;
  12108. }
  12109. break;
  12110. case DRM_FORMAT_ABGR8888:
  12111. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  12112. DRM_DEBUG("unsupported pixel format: %s\n",
  12113. drm_get_format_name(mode_cmd->pixel_format));
  12114. return -EINVAL;
  12115. }
  12116. break;
  12117. case DRM_FORMAT_XBGR8888:
  12118. case DRM_FORMAT_XRGB2101010:
  12119. case DRM_FORMAT_XBGR2101010:
  12120. if (INTEL_INFO(dev)->gen < 4) {
  12121. DRM_DEBUG("unsupported pixel format: %s\n",
  12122. drm_get_format_name(mode_cmd->pixel_format));
  12123. return -EINVAL;
  12124. }
  12125. break;
  12126. case DRM_FORMAT_ABGR2101010:
  12127. if (!IS_VALLEYVIEW(dev)) {
  12128. DRM_DEBUG("unsupported pixel format: %s\n",
  12129. drm_get_format_name(mode_cmd->pixel_format));
  12130. return -EINVAL;
  12131. }
  12132. break;
  12133. case DRM_FORMAT_YUYV:
  12134. case DRM_FORMAT_UYVY:
  12135. case DRM_FORMAT_YVYU:
  12136. case DRM_FORMAT_VYUY:
  12137. if (INTEL_INFO(dev)->gen < 5) {
  12138. DRM_DEBUG("unsupported pixel format: %s\n",
  12139. drm_get_format_name(mode_cmd->pixel_format));
  12140. return -EINVAL;
  12141. }
  12142. break;
  12143. default:
  12144. DRM_DEBUG("unsupported pixel format: %s\n",
  12145. drm_get_format_name(mode_cmd->pixel_format));
  12146. return -EINVAL;
  12147. }
  12148. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12149. if (mode_cmd->offsets[0] != 0)
  12150. return -EINVAL;
  12151. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12152. mode_cmd->pixel_format,
  12153. mode_cmd->modifier[0]);
  12154. /* FIXME drm helper for size checks (especially planar formats)? */
  12155. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12156. return -EINVAL;
  12157. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12158. intel_fb->obj = obj;
  12159. intel_fb->obj->framebuffer_references++;
  12160. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12161. if (ret) {
  12162. DRM_ERROR("framebuffer init failed %d\n", ret);
  12163. return ret;
  12164. }
  12165. return 0;
  12166. }
  12167. static struct drm_framebuffer *
  12168. intel_user_framebuffer_create(struct drm_device *dev,
  12169. struct drm_file *filp,
  12170. struct drm_mode_fb_cmd2 *mode_cmd)
  12171. {
  12172. struct drm_framebuffer *fb;
  12173. struct drm_i915_gem_object *obj;
  12174. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12175. mode_cmd->handles[0]));
  12176. if (&obj->base == NULL)
  12177. return ERR_PTR(-ENOENT);
  12178. fb = intel_framebuffer_create(dev, mode_cmd, obj);
  12179. if (IS_ERR(fb))
  12180. drm_gem_object_unreference_unlocked(&obj->base);
  12181. return fb;
  12182. }
  12183. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12184. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12185. {
  12186. }
  12187. #endif
  12188. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12189. .fb_create = intel_user_framebuffer_create,
  12190. .output_poll_changed = intel_fbdev_output_poll_changed,
  12191. .atomic_check = intel_atomic_check,
  12192. .atomic_commit = intel_atomic_commit,
  12193. .atomic_state_alloc = intel_atomic_state_alloc,
  12194. .atomic_state_clear = intel_atomic_state_clear,
  12195. };
  12196. /* Set up chip specific display functions */
  12197. static void intel_init_display(struct drm_device *dev)
  12198. {
  12199. struct drm_i915_private *dev_priv = dev->dev_private;
  12200. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12201. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12202. else if (IS_CHERRYVIEW(dev))
  12203. dev_priv->display.find_dpll = chv_find_best_dpll;
  12204. else if (IS_VALLEYVIEW(dev))
  12205. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12206. else if (IS_PINEVIEW(dev))
  12207. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12208. else
  12209. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12210. if (INTEL_INFO(dev)->gen >= 9) {
  12211. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12212. dev_priv->display.get_initial_plane_config =
  12213. skylake_get_initial_plane_config;
  12214. dev_priv->display.crtc_compute_clock =
  12215. haswell_crtc_compute_clock;
  12216. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12217. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12218. dev_priv->display.update_primary_plane =
  12219. skylake_update_primary_plane;
  12220. } else if (HAS_DDI(dev)) {
  12221. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12222. dev_priv->display.get_initial_plane_config =
  12223. ironlake_get_initial_plane_config;
  12224. dev_priv->display.crtc_compute_clock =
  12225. haswell_crtc_compute_clock;
  12226. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12227. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12228. dev_priv->display.update_primary_plane =
  12229. ironlake_update_primary_plane;
  12230. } else if (HAS_PCH_SPLIT(dev)) {
  12231. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12232. dev_priv->display.get_initial_plane_config =
  12233. ironlake_get_initial_plane_config;
  12234. dev_priv->display.crtc_compute_clock =
  12235. ironlake_crtc_compute_clock;
  12236. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12237. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12238. dev_priv->display.update_primary_plane =
  12239. ironlake_update_primary_plane;
  12240. } else if (IS_VALLEYVIEW(dev)) {
  12241. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12242. dev_priv->display.get_initial_plane_config =
  12243. i9xx_get_initial_plane_config;
  12244. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12245. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12246. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12247. dev_priv->display.update_primary_plane =
  12248. i9xx_update_primary_plane;
  12249. } else {
  12250. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12251. dev_priv->display.get_initial_plane_config =
  12252. i9xx_get_initial_plane_config;
  12253. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12254. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12255. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12256. dev_priv->display.update_primary_plane =
  12257. i9xx_update_primary_plane;
  12258. }
  12259. /* Returns the core display clock speed */
  12260. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12261. dev_priv->display.get_display_clock_speed =
  12262. skylake_get_display_clock_speed;
  12263. else if (IS_BROXTON(dev))
  12264. dev_priv->display.get_display_clock_speed =
  12265. broxton_get_display_clock_speed;
  12266. else if (IS_BROADWELL(dev))
  12267. dev_priv->display.get_display_clock_speed =
  12268. broadwell_get_display_clock_speed;
  12269. else if (IS_HASWELL(dev))
  12270. dev_priv->display.get_display_clock_speed =
  12271. haswell_get_display_clock_speed;
  12272. else if (IS_VALLEYVIEW(dev))
  12273. dev_priv->display.get_display_clock_speed =
  12274. valleyview_get_display_clock_speed;
  12275. else if (IS_GEN5(dev))
  12276. dev_priv->display.get_display_clock_speed =
  12277. ilk_get_display_clock_speed;
  12278. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12279. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12280. dev_priv->display.get_display_clock_speed =
  12281. i945_get_display_clock_speed;
  12282. else if (IS_GM45(dev))
  12283. dev_priv->display.get_display_clock_speed =
  12284. gm45_get_display_clock_speed;
  12285. else if (IS_CRESTLINE(dev))
  12286. dev_priv->display.get_display_clock_speed =
  12287. i965gm_get_display_clock_speed;
  12288. else if (IS_PINEVIEW(dev))
  12289. dev_priv->display.get_display_clock_speed =
  12290. pnv_get_display_clock_speed;
  12291. else if (IS_G33(dev) || IS_G4X(dev))
  12292. dev_priv->display.get_display_clock_speed =
  12293. g33_get_display_clock_speed;
  12294. else if (IS_I915G(dev))
  12295. dev_priv->display.get_display_clock_speed =
  12296. i915_get_display_clock_speed;
  12297. else if (IS_I945GM(dev) || IS_845G(dev))
  12298. dev_priv->display.get_display_clock_speed =
  12299. i9xx_misc_get_display_clock_speed;
  12300. else if (IS_PINEVIEW(dev))
  12301. dev_priv->display.get_display_clock_speed =
  12302. pnv_get_display_clock_speed;
  12303. else if (IS_I915GM(dev))
  12304. dev_priv->display.get_display_clock_speed =
  12305. i915gm_get_display_clock_speed;
  12306. else if (IS_I865G(dev))
  12307. dev_priv->display.get_display_clock_speed =
  12308. i865_get_display_clock_speed;
  12309. else if (IS_I85X(dev))
  12310. dev_priv->display.get_display_clock_speed =
  12311. i85x_get_display_clock_speed;
  12312. else { /* 830 */
  12313. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12314. dev_priv->display.get_display_clock_speed =
  12315. i830_get_display_clock_speed;
  12316. }
  12317. if (IS_GEN5(dev)) {
  12318. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12319. } else if (IS_GEN6(dev)) {
  12320. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12321. } else if (IS_IVYBRIDGE(dev)) {
  12322. /* FIXME: detect B0+ stepping and use auto training */
  12323. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12324. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12325. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12326. if (IS_BROADWELL(dev)) {
  12327. dev_priv->display.modeset_commit_cdclk =
  12328. broadwell_modeset_commit_cdclk;
  12329. dev_priv->display.modeset_calc_cdclk =
  12330. broadwell_modeset_calc_cdclk;
  12331. }
  12332. } else if (IS_VALLEYVIEW(dev)) {
  12333. dev_priv->display.modeset_commit_cdclk =
  12334. valleyview_modeset_commit_cdclk;
  12335. dev_priv->display.modeset_calc_cdclk =
  12336. valleyview_modeset_calc_cdclk;
  12337. } else if (IS_BROXTON(dev)) {
  12338. dev_priv->display.modeset_commit_cdclk =
  12339. broxton_modeset_commit_cdclk;
  12340. dev_priv->display.modeset_calc_cdclk =
  12341. broxton_modeset_calc_cdclk;
  12342. }
  12343. switch (INTEL_INFO(dev)->gen) {
  12344. case 2:
  12345. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12346. break;
  12347. case 3:
  12348. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12349. break;
  12350. case 4:
  12351. case 5:
  12352. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12353. break;
  12354. case 6:
  12355. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12356. break;
  12357. case 7:
  12358. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12359. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12360. break;
  12361. case 9:
  12362. /* Drop through - unsupported since execlist only. */
  12363. default:
  12364. /* Default just returns -ENODEV to indicate unsupported */
  12365. dev_priv->display.queue_flip = intel_default_queue_flip;
  12366. }
  12367. mutex_init(&dev_priv->pps_mutex);
  12368. }
  12369. /*
  12370. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12371. * resume, or other times. This quirk makes sure that's the case for
  12372. * affected systems.
  12373. */
  12374. static void quirk_pipea_force(struct drm_device *dev)
  12375. {
  12376. struct drm_i915_private *dev_priv = dev->dev_private;
  12377. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12378. DRM_INFO("applying pipe a force quirk\n");
  12379. }
  12380. static void quirk_pipeb_force(struct drm_device *dev)
  12381. {
  12382. struct drm_i915_private *dev_priv = dev->dev_private;
  12383. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12384. DRM_INFO("applying pipe b force quirk\n");
  12385. }
  12386. /*
  12387. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12388. */
  12389. static void quirk_ssc_force_disable(struct drm_device *dev)
  12390. {
  12391. struct drm_i915_private *dev_priv = dev->dev_private;
  12392. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12393. DRM_INFO("applying lvds SSC disable quirk\n");
  12394. }
  12395. /*
  12396. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12397. * brightness value
  12398. */
  12399. static void quirk_invert_brightness(struct drm_device *dev)
  12400. {
  12401. struct drm_i915_private *dev_priv = dev->dev_private;
  12402. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12403. DRM_INFO("applying inverted panel brightness quirk\n");
  12404. }
  12405. /* Some VBT's incorrectly indicate no backlight is present */
  12406. static void quirk_backlight_present(struct drm_device *dev)
  12407. {
  12408. struct drm_i915_private *dev_priv = dev->dev_private;
  12409. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12410. DRM_INFO("applying backlight present quirk\n");
  12411. }
  12412. struct intel_quirk {
  12413. int device;
  12414. int subsystem_vendor;
  12415. int subsystem_device;
  12416. void (*hook)(struct drm_device *dev);
  12417. };
  12418. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12419. struct intel_dmi_quirk {
  12420. void (*hook)(struct drm_device *dev);
  12421. const struct dmi_system_id (*dmi_id_list)[];
  12422. };
  12423. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12424. {
  12425. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12426. return 1;
  12427. }
  12428. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12429. {
  12430. .dmi_id_list = &(const struct dmi_system_id[]) {
  12431. {
  12432. .callback = intel_dmi_reverse_brightness,
  12433. .ident = "NCR Corporation",
  12434. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12435. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12436. },
  12437. },
  12438. { } /* terminating entry */
  12439. },
  12440. .hook = quirk_invert_brightness,
  12441. },
  12442. };
  12443. static struct intel_quirk intel_quirks[] = {
  12444. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12445. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12446. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12447. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12448. /* 830 needs to leave pipe A & dpll A up */
  12449. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12450. /* 830 needs to leave pipe B & dpll B up */
  12451. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12452. /* Lenovo U160 cannot use SSC on LVDS */
  12453. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12454. /* Sony Vaio Y cannot use SSC on LVDS */
  12455. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12456. /* Acer Aspire 5734Z must invert backlight brightness */
  12457. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12458. /* Acer/eMachines G725 */
  12459. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12460. /* Acer/eMachines e725 */
  12461. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12462. /* Acer/Packard Bell NCL20 */
  12463. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12464. /* Acer Aspire 4736Z */
  12465. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12466. /* Acer Aspire 5336 */
  12467. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12468. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12469. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12470. /* Acer C720 Chromebook (Core i3 4005U) */
  12471. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12472. /* Apple Macbook 2,1 (Core 2 T7400) */
  12473. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12474. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12475. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12476. /* HP Chromebook 14 (Celeron 2955U) */
  12477. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12478. /* Dell Chromebook 11 */
  12479. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12480. };
  12481. static void intel_init_quirks(struct drm_device *dev)
  12482. {
  12483. struct pci_dev *d = dev->pdev;
  12484. int i;
  12485. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12486. struct intel_quirk *q = &intel_quirks[i];
  12487. if (d->device == q->device &&
  12488. (d->subsystem_vendor == q->subsystem_vendor ||
  12489. q->subsystem_vendor == PCI_ANY_ID) &&
  12490. (d->subsystem_device == q->subsystem_device ||
  12491. q->subsystem_device == PCI_ANY_ID))
  12492. q->hook(dev);
  12493. }
  12494. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12495. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12496. intel_dmi_quirks[i].hook(dev);
  12497. }
  12498. }
  12499. /* Disable the VGA plane that we never use */
  12500. static void i915_disable_vga(struct drm_device *dev)
  12501. {
  12502. struct drm_i915_private *dev_priv = dev->dev_private;
  12503. u8 sr1;
  12504. u32 vga_reg = i915_vgacntrl_reg(dev);
  12505. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12506. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12507. outb(SR01, VGA_SR_INDEX);
  12508. sr1 = inb(VGA_SR_DATA);
  12509. outb(sr1 | 1<<5, VGA_SR_DATA);
  12510. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12511. udelay(300);
  12512. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12513. POSTING_READ(vga_reg);
  12514. }
  12515. void intel_modeset_init_hw(struct drm_device *dev)
  12516. {
  12517. intel_update_cdclk(dev);
  12518. intel_prepare_ddi(dev);
  12519. intel_init_clock_gating(dev);
  12520. intel_enable_gt_powersave(dev);
  12521. }
  12522. void intel_modeset_init(struct drm_device *dev)
  12523. {
  12524. struct drm_i915_private *dev_priv = dev->dev_private;
  12525. int sprite, ret;
  12526. enum pipe pipe;
  12527. struct intel_crtc *crtc;
  12528. drm_mode_config_init(dev);
  12529. dev->mode_config.min_width = 0;
  12530. dev->mode_config.min_height = 0;
  12531. dev->mode_config.preferred_depth = 24;
  12532. dev->mode_config.prefer_shadow = 1;
  12533. dev->mode_config.allow_fb_modifiers = true;
  12534. dev->mode_config.funcs = &intel_mode_funcs;
  12535. intel_init_quirks(dev);
  12536. intel_init_pm(dev);
  12537. if (INTEL_INFO(dev)->num_pipes == 0)
  12538. return;
  12539. /*
  12540. * There may be no VBT; and if the BIOS enabled SSC we can
  12541. * just keep using it to avoid unnecessary flicker. Whereas if the
  12542. * BIOS isn't using it, don't assume it will work even if the VBT
  12543. * indicates as much.
  12544. */
  12545. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12546. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12547. DREF_SSC1_ENABLE);
  12548. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12549. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12550. bios_lvds_use_ssc ? "en" : "dis",
  12551. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12552. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12553. }
  12554. }
  12555. intel_init_display(dev);
  12556. intel_init_audio(dev);
  12557. if (IS_GEN2(dev)) {
  12558. dev->mode_config.max_width = 2048;
  12559. dev->mode_config.max_height = 2048;
  12560. } else if (IS_GEN3(dev)) {
  12561. dev->mode_config.max_width = 4096;
  12562. dev->mode_config.max_height = 4096;
  12563. } else {
  12564. dev->mode_config.max_width = 8192;
  12565. dev->mode_config.max_height = 8192;
  12566. }
  12567. if (IS_845G(dev) || IS_I865G(dev)) {
  12568. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12569. dev->mode_config.cursor_height = 1023;
  12570. } else if (IS_GEN2(dev)) {
  12571. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12572. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12573. } else {
  12574. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12575. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12576. }
  12577. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12578. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12579. INTEL_INFO(dev)->num_pipes,
  12580. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12581. for_each_pipe(dev_priv, pipe) {
  12582. intel_crtc_init(dev, pipe);
  12583. for_each_sprite(dev_priv, pipe, sprite) {
  12584. ret = intel_plane_init(dev, pipe, sprite);
  12585. if (ret)
  12586. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12587. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12588. }
  12589. }
  12590. intel_update_czclk(dev_priv);
  12591. intel_update_cdclk(dev);
  12592. intel_shared_dpll_init(dev);
  12593. /* Just disable it once at startup */
  12594. i915_disable_vga(dev);
  12595. intel_setup_outputs(dev);
  12596. drm_modeset_lock_all(dev);
  12597. intel_modeset_setup_hw_state(dev);
  12598. drm_modeset_unlock_all(dev);
  12599. for_each_intel_crtc(dev, crtc) {
  12600. struct intel_initial_plane_config plane_config = {};
  12601. if (!crtc->active)
  12602. continue;
  12603. /*
  12604. * Note that reserving the BIOS fb up front prevents us
  12605. * from stuffing other stolen allocations like the ring
  12606. * on top. This prevents some ugliness at boot time, and
  12607. * can even allow for smooth boot transitions if the BIOS
  12608. * fb is large enough for the active pipe configuration.
  12609. */
  12610. dev_priv->display.get_initial_plane_config(crtc,
  12611. &plane_config);
  12612. /*
  12613. * If the fb is shared between multiple heads, we'll
  12614. * just get the first one.
  12615. */
  12616. intel_find_initial_plane_obj(crtc, &plane_config);
  12617. }
  12618. }
  12619. static void intel_enable_pipe_a(struct drm_device *dev)
  12620. {
  12621. struct intel_connector *connector;
  12622. struct drm_connector *crt = NULL;
  12623. struct intel_load_detect_pipe load_detect_temp;
  12624. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12625. /* We can't just switch on the pipe A, we need to set things up with a
  12626. * proper mode and output configuration. As a gross hack, enable pipe A
  12627. * by enabling the load detect pipe once. */
  12628. for_each_intel_connector(dev, connector) {
  12629. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12630. crt = &connector->base;
  12631. break;
  12632. }
  12633. }
  12634. if (!crt)
  12635. return;
  12636. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12637. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12638. }
  12639. static bool
  12640. intel_check_plane_mapping(struct intel_crtc *crtc)
  12641. {
  12642. struct drm_device *dev = crtc->base.dev;
  12643. struct drm_i915_private *dev_priv = dev->dev_private;
  12644. u32 val;
  12645. if (INTEL_INFO(dev)->num_pipes == 1)
  12646. return true;
  12647. val = I915_READ(DSPCNTR(!crtc->plane));
  12648. if ((val & DISPLAY_PLANE_ENABLE) &&
  12649. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12650. return false;
  12651. return true;
  12652. }
  12653. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12654. {
  12655. struct drm_device *dev = crtc->base.dev;
  12656. struct intel_encoder *encoder;
  12657. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12658. return true;
  12659. return false;
  12660. }
  12661. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12662. {
  12663. struct drm_device *dev = crtc->base.dev;
  12664. struct drm_i915_private *dev_priv = dev->dev_private;
  12665. u32 reg;
  12666. /* Clear any frame start delays used for debugging left by the BIOS */
  12667. reg = PIPECONF(crtc->config->cpu_transcoder);
  12668. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12669. /* restore vblank interrupts to correct state */
  12670. drm_crtc_vblank_reset(&crtc->base);
  12671. if (crtc->active) {
  12672. struct intel_plane *plane;
  12673. drm_crtc_vblank_on(&crtc->base);
  12674. /* Disable everything but the primary plane */
  12675. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12676. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12677. continue;
  12678. plane->disable_plane(&plane->base, &crtc->base);
  12679. }
  12680. }
  12681. /* We need to sanitize the plane -> pipe mapping first because this will
  12682. * disable the crtc (and hence change the state) if it is wrong. Note
  12683. * that gen4+ has a fixed plane -> pipe mapping. */
  12684. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12685. bool plane;
  12686. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12687. crtc->base.base.id);
  12688. /* Pipe has the wrong plane attached and the plane is active.
  12689. * Temporarily change the plane mapping and disable everything
  12690. * ... */
  12691. plane = crtc->plane;
  12692. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12693. crtc->plane = !plane;
  12694. intel_crtc_disable_noatomic(&crtc->base);
  12695. crtc->plane = plane;
  12696. }
  12697. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12698. crtc->pipe == PIPE_A && !crtc->active) {
  12699. /* BIOS forgot to enable pipe A, this mostly happens after
  12700. * resume. Force-enable the pipe to fix this, the update_dpms
  12701. * call below we restore the pipe to the right state, but leave
  12702. * the required bits on. */
  12703. intel_enable_pipe_a(dev);
  12704. }
  12705. /* Adjust the state of the output pipe according to whether we
  12706. * have active connectors/encoders. */
  12707. if (!intel_crtc_has_encoders(crtc))
  12708. intel_crtc_disable_noatomic(&crtc->base);
  12709. if (crtc->active != crtc->base.state->active) {
  12710. struct intel_encoder *encoder;
  12711. /* This can happen either due to bugs in the get_hw_state
  12712. * functions or because of calls to intel_crtc_disable_noatomic,
  12713. * or because the pipe is force-enabled due to the
  12714. * pipe A quirk. */
  12715. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12716. crtc->base.base.id,
  12717. crtc->base.state->enable ? "enabled" : "disabled",
  12718. crtc->active ? "enabled" : "disabled");
  12719. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
  12720. crtc->base.state->active = crtc->active;
  12721. crtc->base.enabled = crtc->active;
  12722. /* Because we only establish the connector -> encoder ->
  12723. * crtc links if something is active, this means the
  12724. * crtc is now deactivated. Break the links. connector
  12725. * -> encoder links are only establish when things are
  12726. * actually up, hence no need to break them. */
  12727. WARN_ON(crtc->active);
  12728. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12729. encoder->base.crtc = NULL;
  12730. }
  12731. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12732. /*
  12733. * We start out with underrun reporting disabled to avoid races.
  12734. * For correct bookkeeping mark this on active crtcs.
  12735. *
  12736. * Also on gmch platforms we dont have any hardware bits to
  12737. * disable the underrun reporting. Which means we need to start
  12738. * out with underrun reporting disabled also on inactive pipes,
  12739. * since otherwise we'll complain about the garbage we read when
  12740. * e.g. coming up after runtime pm.
  12741. *
  12742. * No protection against concurrent access is required - at
  12743. * worst a fifo underrun happens which also sets this to false.
  12744. */
  12745. crtc->cpu_fifo_underrun_disabled = true;
  12746. crtc->pch_fifo_underrun_disabled = true;
  12747. }
  12748. }
  12749. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12750. {
  12751. struct intel_connector *connector;
  12752. struct drm_device *dev = encoder->base.dev;
  12753. bool active = false;
  12754. /* We need to check both for a crtc link (meaning that the
  12755. * encoder is active and trying to read from a pipe) and the
  12756. * pipe itself being active. */
  12757. bool has_active_crtc = encoder->base.crtc &&
  12758. to_intel_crtc(encoder->base.crtc)->active;
  12759. for_each_intel_connector(dev, connector) {
  12760. if (connector->base.encoder != &encoder->base)
  12761. continue;
  12762. active = true;
  12763. break;
  12764. }
  12765. if (active && !has_active_crtc) {
  12766. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12767. encoder->base.base.id,
  12768. encoder->base.name);
  12769. /* Connector is active, but has no active pipe. This is
  12770. * fallout from our resume register restoring. Disable
  12771. * the encoder manually again. */
  12772. if (encoder->base.crtc) {
  12773. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12774. encoder->base.base.id,
  12775. encoder->base.name);
  12776. encoder->disable(encoder);
  12777. if (encoder->post_disable)
  12778. encoder->post_disable(encoder);
  12779. }
  12780. encoder->base.crtc = NULL;
  12781. /* Inconsistent output/port/pipe state happens presumably due to
  12782. * a bug in one of the get_hw_state functions. Or someplace else
  12783. * in our code, like the register restore mess on resume. Clamp
  12784. * things to off as a safer default. */
  12785. for_each_intel_connector(dev, connector) {
  12786. if (connector->encoder != encoder)
  12787. continue;
  12788. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12789. connector->base.encoder = NULL;
  12790. }
  12791. }
  12792. /* Enabled encoders without active connectors will be fixed in
  12793. * the crtc fixup. */
  12794. }
  12795. void i915_redisable_vga_power_on(struct drm_device *dev)
  12796. {
  12797. struct drm_i915_private *dev_priv = dev->dev_private;
  12798. u32 vga_reg = i915_vgacntrl_reg(dev);
  12799. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12800. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12801. i915_disable_vga(dev);
  12802. }
  12803. }
  12804. void i915_redisable_vga(struct drm_device *dev)
  12805. {
  12806. struct drm_i915_private *dev_priv = dev->dev_private;
  12807. /* This function can be called both from intel_modeset_setup_hw_state or
  12808. * at a very early point in our resume sequence, where the power well
  12809. * structures are not yet restored. Since this function is at a very
  12810. * paranoid "someone might have enabled VGA while we were not looking"
  12811. * level, just check if the power well is enabled instead of trying to
  12812. * follow the "don't touch the power well if we don't need it" policy
  12813. * the rest of the driver uses. */
  12814. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12815. return;
  12816. i915_redisable_vga_power_on(dev);
  12817. }
  12818. static bool primary_get_hw_state(struct intel_plane *plane)
  12819. {
  12820. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12821. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12822. }
  12823. /* FIXME read out full plane state for all planes */
  12824. static void readout_plane_state(struct intel_crtc *crtc)
  12825. {
  12826. struct drm_plane *primary = crtc->base.primary;
  12827. struct intel_plane_state *plane_state =
  12828. to_intel_plane_state(primary->state);
  12829. plane_state->visible = crtc->active &&
  12830. primary_get_hw_state(to_intel_plane(primary));
  12831. if (plane_state->visible)
  12832. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  12833. }
  12834. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12835. {
  12836. struct drm_i915_private *dev_priv = dev->dev_private;
  12837. enum pipe pipe;
  12838. struct intel_crtc *crtc;
  12839. struct intel_encoder *encoder;
  12840. struct intel_connector *connector;
  12841. int i;
  12842. for_each_intel_crtc(dev, crtc) {
  12843. __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
  12844. memset(crtc->config, 0, sizeof(*crtc->config));
  12845. crtc->config->base.crtc = &crtc->base;
  12846. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12847. crtc->config);
  12848. crtc->base.state->active = crtc->active;
  12849. crtc->base.enabled = crtc->active;
  12850. readout_plane_state(crtc);
  12851. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12852. crtc->base.base.id,
  12853. crtc->active ? "enabled" : "disabled");
  12854. }
  12855. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12856. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12857. pll->on = pll->get_hw_state(dev_priv, pll,
  12858. &pll->config.hw_state);
  12859. pll->active = 0;
  12860. pll->config.crtc_mask = 0;
  12861. for_each_intel_crtc(dev, crtc) {
  12862. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12863. pll->active++;
  12864. pll->config.crtc_mask |= 1 << crtc->pipe;
  12865. }
  12866. }
  12867. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12868. pll->name, pll->config.crtc_mask, pll->on);
  12869. if (pll->config.crtc_mask)
  12870. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12871. }
  12872. for_each_intel_encoder(dev, encoder) {
  12873. pipe = 0;
  12874. if (encoder->get_hw_state(encoder, &pipe)) {
  12875. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12876. encoder->base.crtc = &crtc->base;
  12877. encoder->get_config(encoder, crtc->config);
  12878. } else {
  12879. encoder->base.crtc = NULL;
  12880. }
  12881. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12882. encoder->base.base.id,
  12883. encoder->base.name,
  12884. encoder->base.crtc ? "enabled" : "disabled",
  12885. pipe_name(pipe));
  12886. }
  12887. for_each_intel_connector(dev, connector) {
  12888. if (connector->get_hw_state(connector)) {
  12889. connector->base.dpms = DRM_MODE_DPMS_ON;
  12890. connector->base.encoder = &connector->encoder->base;
  12891. } else {
  12892. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12893. connector->base.encoder = NULL;
  12894. }
  12895. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12896. connector->base.base.id,
  12897. connector->base.name,
  12898. connector->base.encoder ? "enabled" : "disabled");
  12899. }
  12900. for_each_intel_crtc(dev, crtc) {
  12901. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12902. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12903. if (crtc->base.state->active) {
  12904. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  12905. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  12906. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12907. /*
  12908. * The initial mode needs to be set in order to keep
  12909. * the atomic core happy. It wants a valid mode if the
  12910. * crtc's enabled, so we do the above call.
  12911. *
  12912. * At this point some state updated by the connectors
  12913. * in their ->detect() callback has not run yet, so
  12914. * no recalculation can be done yet.
  12915. *
  12916. * Even if we could do a recalculation and modeset
  12917. * right now it would cause a double modeset if
  12918. * fbdev or userspace chooses a different initial mode.
  12919. *
  12920. * If that happens, someone indicated they wanted a
  12921. * mode change, which means it's safe to do a full
  12922. * recalculation.
  12923. */
  12924. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  12925. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12926. update_scanline_offset(crtc);
  12927. }
  12928. }
  12929. }
  12930. /* Scan out the current hw modeset state,
  12931. * and sanitizes it to the current state
  12932. */
  12933. static void
  12934. intel_modeset_setup_hw_state(struct drm_device *dev)
  12935. {
  12936. struct drm_i915_private *dev_priv = dev->dev_private;
  12937. enum pipe pipe;
  12938. struct intel_crtc *crtc;
  12939. struct intel_encoder *encoder;
  12940. int i;
  12941. intel_modeset_readout_hw_state(dev);
  12942. /* HW state is read out, now we need to sanitize this mess. */
  12943. for_each_intel_encoder(dev, encoder) {
  12944. intel_sanitize_encoder(encoder);
  12945. }
  12946. for_each_pipe(dev_priv, pipe) {
  12947. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12948. intel_sanitize_crtc(crtc);
  12949. intel_dump_pipe_config(crtc, crtc->config,
  12950. "[setup_hw_state]");
  12951. }
  12952. intel_modeset_update_connector_atomic_state(dev);
  12953. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12954. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12955. if (!pll->on || pll->active)
  12956. continue;
  12957. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12958. pll->disable(dev_priv, pll);
  12959. pll->on = false;
  12960. }
  12961. if (IS_VALLEYVIEW(dev))
  12962. vlv_wm_get_hw_state(dev);
  12963. else if (IS_GEN9(dev))
  12964. skl_wm_get_hw_state(dev);
  12965. else if (HAS_PCH_SPLIT(dev))
  12966. ilk_wm_get_hw_state(dev);
  12967. for_each_intel_crtc(dev, crtc) {
  12968. unsigned long put_domains;
  12969. put_domains = modeset_get_crtc_power_domains(&crtc->base);
  12970. if (WARN_ON(put_domains))
  12971. modeset_put_power_domains(dev_priv, put_domains);
  12972. }
  12973. intel_display_set_init_power(dev_priv, false);
  12974. }
  12975. void intel_display_resume(struct drm_device *dev)
  12976. {
  12977. struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
  12978. struct intel_connector *conn;
  12979. struct intel_plane *plane;
  12980. struct drm_crtc *crtc;
  12981. int ret;
  12982. if (!state)
  12983. return;
  12984. state->acquire_ctx = dev->mode_config.acquire_ctx;
  12985. /* preserve complete old state, including dpll */
  12986. intel_atomic_get_shared_dpll_state(state);
  12987. for_each_crtc(dev, crtc) {
  12988. struct drm_crtc_state *crtc_state =
  12989. drm_atomic_get_crtc_state(state, crtc);
  12990. ret = PTR_ERR_OR_ZERO(crtc_state);
  12991. if (ret)
  12992. goto err;
  12993. /* force a restore */
  12994. crtc_state->mode_changed = true;
  12995. }
  12996. for_each_intel_plane(dev, plane) {
  12997. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
  12998. if (ret)
  12999. goto err;
  13000. }
  13001. for_each_intel_connector(dev, conn) {
  13002. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
  13003. if (ret)
  13004. goto err;
  13005. }
  13006. intel_modeset_setup_hw_state(dev);
  13007. i915_redisable_vga(dev);
  13008. ret = drm_atomic_commit(state);
  13009. if (!ret)
  13010. return;
  13011. err:
  13012. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13013. drm_atomic_state_free(state);
  13014. }
  13015. void intel_modeset_gem_init(struct drm_device *dev)
  13016. {
  13017. struct drm_crtc *c;
  13018. struct drm_i915_gem_object *obj;
  13019. int ret;
  13020. mutex_lock(&dev->struct_mutex);
  13021. intel_init_gt_powersave(dev);
  13022. mutex_unlock(&dev->struct_mutex);
  13023. intel_modeset_init_hw(dev);
  13024. intel_setup_overlay(dev);
  13025. /*
  13026. * Make sure any fbs we allocated at startup are properly
  13027. * pinned & fenced. When we do the allocation it's too early
  13028. * for this.
  13029. */
  13030. for_each_crtc(dev, c) {
  13031. obj = intel_fb_obj(c->primary->fb);
  13032. if (obj == NULL)
  13033. continue;
  13034. mutex_lock(&dev->struct_mutex);
  13035. ret = intel_pin_and_fence_fb_obj(c->primary,
  13036. c->primary->fb,
  13037. c->primary->state);
  13038. mutex_unlock(&dev->struct_mutex);
  13039. if (ret) {
  13040. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13041. to_intel_crtc(c)->pipe);
  13042. drm_framebuffer_unreference(c->primary->fb);
  13043. c->primary->fb = NULL;
  13044. c->primary->crtc = c->primary->state->crtc = NULL;
  13045. update_state_fb(c->primary);
  13046. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13047. }
  13048. }
  13049. intel_backlight_register(dev);
  13050. }
  13051. void intel_connector_unregister(struct intel_connector *intel_connector)
  13052. {
  13053. struct drm_connector *connector = &intel_connector->base;
  13054. intel_panel_destroy_backlight(connector);
  13055. drm_connector_unregister(connector);
  13056. }
  13057. void intel_modeset_cleanup(struct drm_device *dev)
  13058. {
  13059. struct drm_i915_private *dev_priv = dev->dev_private;
  13060. struct drm_connector *connector;
  13061. intel_disable_gt_powersave(dev);
  13062. intel_backlight_unregister(dev);
  13063. /*
  13064. * Interrupts and polling as the first thing to avoid creating havoc.
  13065. * Too much stuff here (turning of connectors, ...) would
  13066. * experience fancy races otherwise.
  13067. */
  13068. intel_irq_uninstall(dev_priv);
  13069. /*
  13070. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13071. * poll handlers. Hence disable polling after hpd handling is shut down.
  13072. */
  13073. drm_kms_helper_poll_fini(dev);
  13074. intel_unregister_dsm_handler();
  13075. intel_fbc_disable(dev_priv);
  13076. /* flush any delayed tasks or pending work */
  13077. flush_scheduled_work();
  13078. /* destroy the backlight and sysfs files before encoders/connectors */
  13079. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  13080. struct intel_connector *intel_connector;
  13081. intel_connector = to_intel_connector(connector);
  13082. intel_connector->unregister(intel_connector);
  13083. }
  13084. drm_mode_config_cleanup(dev);
  13085. intel_cleanup_overlay(dev);
  13086. mutex_lock(&dev->struct_mutex);
  13087. intel_cleanup_gt_powersave(dev);
  13088. mutex_unlock(&dev->struct_mutex);
  13089. }
  13090. /*
  13091. * Return which encoder is currently attached for connector.
  13092. */
  13093. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13094. {
  13095. return &intel_attached_encoder(connector)->base;
  13096. }
  13097. void intel_connector_attach_encoder(struct intel_connector *connector,
  13098. struct intel_encoder *encoder)
  13099. {
  13100. connector->encoder = encoder;
  13101. drm_mode_connector_attach_encoder(&connector->base,
  13102. &encoder->base);
  13103. }
  13104. /*
  13105. * set vga decode state - true == enable VGA decode
  13106. */
  13107. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13108. {
  13109. struct drm_i915_private *dev_priv = dev->dev_private;
  13110. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13111. u16 gmch_ctrl;
  13112. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13113. DRM_ERROR("failed to read control word\n");
  13114. return -EIO;
  13115. }
  13116. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13117. return 0;
  13118. if (state)
  13119. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13120. else
  13121. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13122. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13123. DRM_ERROR("failed to write control word\n");
  13124. return -EIO;
  13125. }
  13126. return 0;
  13127. }
  13128. struct intel_display_error_state {
  13129. u32 power_well_driver;
  13130. int num_transcoders;
  13131. struct intel_cursor_error_state {
  13132. u32 control;
  13133. u32 position;
  13134. u32 base;
  13135. u32 size;
  13136. } cursor[I915_MAX_PIPES];
  13137. struct intel_pipe_error_state {
  13138. bool power_domain_on;
  13139. u32 source;
  13140. u32 stat;
  13141. } pipe[I915_MAX_PIPES];
  13142. struct intel_plane_error_state {
  13143. u32 control;
  13144. u32 stride;
  13145. u32 size;
  13146. u32 pos;
  13147. u32 addr;
  13148. u32 surface;
  13149. u32 tile_offset;
  13150. } plane[I915_MAX_PIPES];
  13151. struct intel_transcoder_error_state {
  13152. bool power_domain_on;
  13153. enum transcoder cpu_transcoder;
  13154. u32 conf;
  13155. u32 htotal;
  13156. u32 hblank;
  13157. u32 hsync;
  13158. u32 vtotal;
  13159. u32 vblank;
  13160. u32 vsync;
  13161. } transcoder[4];
  13162. };
  13163. struct intel_display_error_state *
  13164. intel_display_capture_error_state(struct drm_device *dev)
  13165. {
  13166. struct drm_i915_private *dev_priv = dev->dev_private;
  13167. struct intel_display_error_state *error;
  13168. int transcoders[] = {
  13169. TRANSCODER_A,
  13170. TRANSCODER_B,
  13171. TRANSCODER_C,
  13172. TRANSCODER_EDP,
  13173. };
  13174. int i;
  13175. if (INTEL_INFO(dev)->num_pipes == 0)
  13176. return NULL;
  13177. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13178. if (error == NULL)
  13179. return NULL;
  13180. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13181. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13182. for_each_pipe(dev_priv, i) {
  13183. error->pipe[i].power_domain_on =
  13184. __intel_display_power_is_enabled(dev_priv,
  13185. POWER_DOMAIN_PIPE(i));
  13186. if (!error->pipe[i].power_domain_on)
  13187. continue;
  13188. error->cursor[i].control = I915_READ(CURCNTR(i));
  13189. error->cursor[i].position = I915_READ(CURPOS(i));
  13190. error->cursor[i].base = I915_READ(CURBASE(i));
  13191. error->plane[i].control = I915_READ(DSPCNTR(i));
  13192. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13193. if (INTEL_INFO(dev)->gen <= 3) {
  13194. error->plane[i].size = I915_READ(DSPSIZE(i));
  13195. error->plane[i].pos = I915_READ(DSPPOS(i));
  13196. }
  13197. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13198. error->plane[i].addr = I915_READ(DSPADDR(i));
  13199. if (INTEL_INFO(dev)->gen >= 4) {
  13200. error->plane[i].surface = I915_READ(DSPSURF(i));
  13201. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13202. }
  13203. error->pipe[i].source = I915_READ(PIPESRC(i));
  13204. if (HAS_GMCH_DISPLAY(dev))
  13205. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13206. }
  13207. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13208. if (HAS_DDI(dev_priv->dev))
  13209. error->num_transcoders++; /* Account for eDP. */
  13210. for (i = 0; i < error->num_transcoders; i++) {
  13211. enum transcoder cpu_transcoder = transcoders[i];
  13212. error->transcoder[i].power_domain_on =
  13213. __intel_display_power_is_enabled(dev_priv,
  13214. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13215. if (!error->transcoder[i].power_domain_on)
  13216. continue;
  13217. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13218. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13219. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13220. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13221. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13222. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13223. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13224. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13225. }
  13226. return error;
  13227. }
  13228. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13229. void
  13230. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13231. struct drm_device *dev,
  13232. struct intel_display_error_state *error)
  13233. {
  13234. struct drm_i915_private *dev_priv = dev->dev_private;
  13235. int i;
  13236. if (!error)
  13237. return;
  13238. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13239. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13240. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13241. error->power_well_driver);
  13242. for_each_pipe(dev_priv, i) {
  13243. err_printf(m, "Pipe [%d]:\n", i);
  13244. err_printf(m, " Power: %s\n",
  13245. error->pipe[i].power_domain_on ? "on" : "off");
  13246. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13247. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13248. err_printf(m, "Plane [%d]:\n", i);
  13249. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13250. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13251. if (INTEL_INFO(dev)->gen <= 3) {
  13252. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13253. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13254. }
  13255. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13256. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13257. if (INTEL_INFO(dev)->gen >= 4) {
  13258. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13259. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13260. }
  13261. err_printf(m, "Cursor [%d]:\n", i);
  13262. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13263. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13264. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13265. }
  13266. for (i = 0; i < error->num_transcoders; i++) {
  13267. err_printf(m, "CPU transcoder: %c\n",
  13268. transcoder_name(error->transcoder[i].cpu_transcoder));
  13269. err_printf(m, " Power: %s\n",
  13270. error->transcoder[i].power_domain_on ? "on" : "off");
  13271. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13272. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13273. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13274. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13275. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13276. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13277. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13278. }
  13279. }
  13280. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13281. {
  13282. struct intel_crtc *crtc;
  13283. for_each_intel_crtc(dev, crtc) {
  13284. struct intel_unpin_work *work;
  13285. spin_lock_irq(&dev->event_lock);
  13286. work = crtc->unpin_work;
  13287. if (work && work->event &&
  13288. work->event->base.file_priv == file) {
  13289. kfree(work->event);
  13290. work->event = NULL;
  13291. }
  13292. spin_unlock_irq(&dev->event_lock);
  13293. }
  13294. }